~kris/9p

9hist

ref: ad718a20afbbd9fadfeb0180d0ee3cc586d3b7ea 9hist/port/auth.c -rw-r--r-- 4.8 KiB
ad718a20 — David du Colombier Plan 9 from Bell Labs 2001-08-18 25 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include	"u.h"
#include	"../port/lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"../port/error.h"

#include	<auth.h>

char	*eve;
char	evekey[DESKEYLEN];
char	hostdomain[DOMLEN];

/*
 *  return true if current user is eve
 */
int
iseve(void)
{
	return strcmp(eve, up->user) == 0;
}

long
sysfversion(ulong *arg)
{
	Fcall f;	/* two Fcalls should be big enough for reply */
	uchar *msg;
	char *vers;
	uint arglen, n, m, msize;
	Chan *c;
	uvlong oo;

	msize = arg[1];
	vers = (char*)arg[2];
	arglen = arg[3];
	validaddr(arg[2], arglen, 1);
	/* check there's a NUL in the version string */
	if(memchr(vers, 0, arglen) == 0)
		error(Ebadarg);
	c = fdtochan(arg[0], ORDWR, 0, 1);
	if(waserror()){
		cclose(c);
		nexterror();
	}

	if((c->flag&CMSG) && c->version!=nil) /* BUG: insufficient; should check compatibility */
		goto Return;

	f.type = Tversion;
	f.tag = NOTAG;
	if(msize == 0)
		msize = IOHDRSZ+8192;	/* reasonable default */
	f.msize = msize;
	if(vers[0] == '\0')
		vers = VERSION9P;
	f.version = vers;
	msg = smalloc(8192+IOHDRSZ);
	if(waserror()){
		free(msg);
		nexterror();
	}
	n = convS2M(&f, msg, 8192+IOHDRSZ);
	if(n == 0)
		error("bad fversion conversion on send");

	lock(c);
	oo = c->offset;
	c->offset += n;
	unlock(c);

	m = devtab[c->type]->write(c, msg, n, oo);

	if(m < n){
		lock(c);
		c->offset -= n - m;
		unlock(c);
		error("short write in fversion");
	}

	/* message sent; receive and decode reply */
	m = devtab[c->type]->read(c, msg, 8192+IOHDRSZ, c->offset);
	if(m <= 0)
		error("EOF receiving fversion reply");

	lock(c);
	c->offset += m;
	unlock(c);

	n = convM2S(msg, m, &f);
	if(n != m)
		error("bad fversion conversion on reply");
	if(f.type != Rversion)
		error("unexpected reply type in fversion");
	if(f.msize > msize)
		error("server tries to increase msize in fversion");
	if(f.msize<256 || f.msize>1024*1024)
		error("nonsense value of msize in fversion");
	kstrdup(&c->version, f.version);
	c->iounit = f.msize;
	free(msg);
	poperror();

Return:
	m = strlen(c->version);
	if(m > arglen)
		m = arglen;
	memmove((char*)arg[2], c->version, m);

	cclose(c);
	poperror();
	return m;
}

long
sysfsession(ulong *arg)
{
	Fcall f;
	uchar *msg;
	uint authlen, n, m;
	Chan *c;
	uvlong oo;

//BUG print("warning: stub fsession being used\n");
	authlen = arg[2];
	validaddr(arg[1], authlen, 1);
	c = fdtochan(arg[0], ORDWR, 0, 1);
	if(waserror()){
		cclose(c);
		nexterror();
	}

	if(c->flag & CMSG){
//BUG what to do?
		((uchar*)arg[1])[0] = 0;
		poperror();
		cclose(c);
		return 0;
	}

	f.type = Tsession;
	f.tag = NOTAG;
	f.nchal = 0;
	f.chal = (uchar*)"";
	msg = smalloc(8192+IOHDRSZ);
	if(waserror()){
		free(msg);
		nexterror();
	}
	n = convS2M(&f, msg, 8192+IOHDRSZ);
	if(n == 0)
		error("bad fsession conversion on send");

	lock(c);
	oo = c->offset;
	c->offset += n;
	unlock(c);

	m = devtab[c->type]->write(c, msg, n, oo);

	if(m < n){
		lock(c);
		c->offset -= n - m;
		unlock(c);
		error("short write in fsession");
	}

	/* message sent; receive and decode reply */
	m = devtab[c->type]->read(c, msg, 8192+IOHDRSZ, c->offset);
	if(m <= 0)
		error("EOF receiving fsession reply");

	lock(c);
	c->offset += m;
	unlock(c);

	n = convM2S(msg, m, &f);
	if(n != m)
		error("bad fsession conversion on reply");
	if(f.type != Rsession)
		error("unexpected reply type in fsession");
	m = f.nchal;
	if(m > authlen)
		error(Eshort);
//BUG print("auth stuff ignored; noauth by default\n");
	((uchar*)arg[1])[0] = 0;

	free(msg);
	poperror();
	poperror();
	cclose(c);
	return m;
}

long
sysfauth(ulong *)
{
	error("sysfauth unimplemented");
	return -1;
}

/*
 *  called by devcons() for key device
 */
long
keyread(char *a, int n, long offset)
{
	if(n<DESKEYLEN || offset != 0)
		error(Ebadarg);
	if(!cpuserver || !iseve())
		error(Eperm);
	memmove(a, evekey, DESKEYLEN);
	return DESKEYLEN;
}

long
keywrite(char *a, int n)
{
	if(n != DESKEYLEN)
		error(Ebadarg);
	if(!iseve())
		error(Eperm);
	memmove(evekey, a, DESKEYLEN);
	return DESKEYLEN;
}

/*
 *  called by devcons() for user device
 *
 *  anyone can become none
 */
long
userwrite(char *a, int n)
{
	if(!iseve() || strcmp(a, "none") != 0)
		error(Eperm);
	kstrdup(&up->user, "none");
	up->basepri = PriNormal;
	return n;
}

/*
 *  called by devcons() for host owner/domain
 *
 *  writing hostowner also sets user
 */
long
hostownerwrite(char *a, int n)
{
	char buf[128];

	if(!iseve())
		error(Eperm);
	if(n >= sizeof buf)
		error(Ebadarg);
	strncpy(buf, a, n+1);
	if(buf[0] == '\0')
		error(Ebadarg);

	renameuser(eve, buf);
	kstrdup(&eve, buf);
	kstrdup(&up->user, buf);
	up->basepri = PriNormal;
	return n;
}

long
hostdomainwrite(char *a, int n)
{
	char buf[DOMLEN];

	if(!iseve())
		error(Eperm);
	if(n >= DOMLEN)
		error(Ebadarg);
	memset(buf, 0, DOMLEN);
	strncpy(buf, a, n);
	if(buf[0] == 0)
		error(Ebadarg);
	memmove(hostdomain, buf, DOMLEN);
	return n;
}