Subversion Repositories planix.SVN

Rev

Rev 69 | Rev 71 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
/* already in plan9.h #include <sys/types.h> *//* for struct passwd, struct group, struct stat ... */
2
/* plan9.h is first to get the large file support definitions as early as possible */
3
#include <plan9.h>
4
#include <sys/stat.h>	/* for stat, umask */
5
#include <stdlib.h>	/* for malloc */
6
#include <string.h>	/* for strcpy, memmove */
7
#include <pwd.h>	/* for getpwnam, getpwuid */
8
#include <grp.h>	/* for getgrnam, getgrgid */
9
#include <unistd.h>	/* for gethostname, pread, pwrite, read, write */
10
#include <utime.h>	/* for utime */
11
#include <dirent.h>	/* for readdir */
12
#include <errno.h>	/* for errno */
13
#include <stdio.h>	/* for remove [sic] */
14
#include <fcntl.h>	/* for O_RDONLY, etc. */
15
 
16
#include <sys/socket.h>	/* various networking crud */
17
#include <netinet/in.h>
18
#include <netdb.h>
19
 
20
#include <fcall.h>
21
#include <oldfcall.h>
22
#include <u9fs.h>
23
 
24
/* #ifndef because can be given in makefile */
25
#ifndef DEFAULTLOG
26
#define DEFAULTLOG "/tmp/u9fs.log"
27
#endif
28
 
65 7u83 29
 
2 - 30
char *logfile = DEFAULTLOG;
31
 
32
#define S_ISSPECIAL(m) (S_ISCHR(m) || S_ISBLK(m) || S_ISFIFO(m))
33
 
34
enum {
35
	Tdot = 1,
36
	Tdotdot
37
};
38
 
39
enum {
40
	P9P1,
41
	P9P2000
42
};
43
 
44
typedef struct User User;
45
struct User {
46
	int id;
47
	gid_t defaultgid;
48
	char *name;
49
	char **mem;	/* group members */
50
	int nmem;
51
	User *next;
52
};
53
 
54
struct Fid {
55
	int fid;
56
	char *path;
57
	struct stat st;
58
	User *u;
59
	int omode;
60
	DIR *dir;
61
	int diroffset;
62
	int fd;
63
	struct dirent *dirent;
64
	int direof;
65
	Fid *next;
66
	Fid *prev;
67
	int auth;
68
	void *authmagic;
69
};
70
 
71
void*	emalloc(size_t);
72
void*	erealloc(void*, size_t);
73
char*	estrdup(char*);
74
char*	estrpath(char*, char*, int);
75
void	sysfatal(char*, ...);
76
int	okuser(char*);
77
 
78
void	rversion(Fcall*, Fcall*);
79
void	rauth(Fcall*, Fcall*);
80
void	rattach(Fcall*, Fcall*);
81
void	rflush(Fcall*, Fcall*);
82
void	rclone(Fcall*, Fcall*);
83
void	rwalk(Fcall*, Fcall*);
84
void	ropen(Fcall*, Fcall*);
85
void	rcreate(Fcall*, Fcall*);
86
void	rread(Fcall*, Fcall*);
87
void	rwrite(Fcall*, Fcall*);
88
void	rclunk(Fcall*, Fcall*);
89
void	rstat(Fcall*, Fcall*);
90
void	rwstat(Fcall*, Fcall*);
91
void	rclwalk(Fcall*, Fcall*);
92
void	rremove(Fcall*, Fcall*);
93
 
94
User*	uname2user(char*);
95
User*	gname2user(char*);
96
User*	uid2user(int);
97
User*	gid2user(int);
98
 
99
Fid*	newfid(int, char**);
100
Fid*	oldfidex(int, int, char**);
101
Fid*	oldfid(int, char**);
102
int	fidstat(Fid*, char**);
103
void	freefid(Fid*);
104
 
105
int	userchange(User*, char**);
106
int	userwalk(User*, char**, char*, Qid*, char**);
107
int	useropen(Fid*, int, char**);
108
int	usercreate(Fid*, char*, int, long, char**);
109
int	userremove(Fid*, char**);
110
int	userperm(User*, char*, int, int);
111
int	useringroup(User*, User*);
112
 
113
Qid	stat2qid(struct stat*);
114
 
115
void	getfcallold(int, Fcall*, int);
116
void	putfcallold(int, Fcall*);
117
 
118
char	Eauth[] =	"authentication failed";
119
char	Ebadfid[] =	"fid unknown or out of range";
120
char	Ebadoffset[] =	"bad offset in directory read";
121
char	Ebadusefid[] =	"bad use of fid";
122
char	Edirchange[] =	"wstat can't convert between files and directories";
123
char	Eexist[] =	"file or directory already exists";
124
char	Efidactive[] =	"fid already in use";
125
char	Enotdir[] =	"not a directory";
126
char	Enotingroup[] =	"not a member of proposed group";
127
char	Enotowner[] = 	"only owner can change group in wstat";
128
char	Eperm[] =	"permission denied";
129
char	Especial0[] =	"already attached without access to special files";
130
char	Especial1[] =	"already attached with access to special files";
131
char	Especial[] =	"no access to special file";
132
char	Etoolarge[] =	"i/o count too large";
133
char	Eunknowngroup[] = "unknown group";
134
char	Eunknownuser[] = "unknown user";
135
char	Ewstatbuffer[] = "bogus wstat buffer";
136
 
137
ulong	msize = IOHDRSZ+8192;
138
uchar*	rxbuf;
139
uchar*	txbuf;
140
void*	databuf;
141
int	connected;
142
int	devallowed;
143
char*	autharg;
144
char*	defaultuser;
145
char	hostname[256];
146
char	remotehostname[256];
147
int	chatty9p = 0;
148
int	network = 1;
149
int	old9p = -1;
150
int	authed;
151
User*	none;
152
 
153
Auth *authmethods[] = {	/* first is default */
154
	&authrhosts,
155
	&authp9any,
156
	&authnone,
157
};
158
 
159
Auth *auth;
160
 
65 7u83 161
 
162
 
2 - 163
/*
164
 * frogs: characters not valid in plan9
165
 * filenames, keep this list in sync with
166
 * /sys/src/9/port/chan.c:1656
167
 */
168
char isfrog[256]={
169
	/*NUL*/	1, 1, 1, 1, 1, 1, 1, 1,
170
	/*BKS*/	1, 1, 1, 1, 1, 1, 1, 1,
171
	/*DLE*/	1, 1, 1, 1, 1, 1, 1, 1,
172
	/*CAN*/	1, 1, 1, 1, 1, 1, 1, 1,
70 7u83 173
	['/'] = 1,
174
	[0x7f] = 1,
2 - 175
};
176
 
65 7u83 177
int groupchange(User *u, User *g, char **ep);
178
 
179
 
180
 
2 - 181
void
182
getfcallnew(int fd, Fcall *fc, int have)
183
{
184
	int len;
185
 
186
	if(have > BIT32SZ)
187
		sysfatal("cannot happen");
188
 
189
	if(have < BIT32SZ && readn(fd, rxbuf+have, BIT32SZ-have) != BIT32SZ-have)
190
		sysfatal("couldn't read message");
191
 
192
	len = GBIT32(rxbuf);
193
	if(len <= BIT32SZ)
194
		sysfatal("bogus message");
195
 
196
	len -= BIT32SZ;
197
	if(readn(fd, rxbuf+BIT32SZ, len) != len)
198
		sysfatal("short message");
199
 
200
	if(convM2S(rxbuf, len+BIT32SZ, fc) != len+BIT32SZ)
201
		sysfatal("badly sized message type %d", rxbuf[0]);
202
}
203
 
204
void
205
getfcallold(int fd, Fcall *fc, int have)
206
{
207
	int len, n;
208
 
209
	if(have > 3)
210
		sysfatal("cannot happen");
211
 
212
	if(have < 3 && readn(fd, rxbuf, 3-have) != 3-have)
213
		sysfatal("couldn't read message");
214
 
215
	len = oldhdrsize(rxbuf[0]);
216
	if(len < 3)
217
		sysfatal("bad message %d", rxbuf[0]);
218
	if(len > 3 && readn(fd, rxbuf+3, len-3) != len-3)
219
		sysfatal("couldn't read message");
220
 
221
	n = iosize(rxbuf);
222
	if(readn(fd, rxbuf+len, n) != n)
223
		sysfatal("couldn't read message");
224
	len += n;
225
 
226
	if(convM2Sold(rxbuf, len, fc) != len)
227
		sysfatal("badly sized message type %d", rxbuf[0]);
228
}
229
 
230
void
231
putfcallnew(int wfd, Fcall *tx)
232
{
233
	uint n;
234
 
235
	if((n = convS2M(tx, txbuf, msize)) == 0)
236
		sysfatal("couldn't format message type %d", tx->type);
237
	if(write(wfd, txbuf, n) != n)
238
		sysfatal("couldn't send message");
239
}
240
 
241
void
242
putfcallold(int wfd, Fcall *tx)
243
{
244
	uint n;
245
 
246
	if((n = convS2Mold(tx, txbuf, msize)) == 0)
247
		sysfatal("couldn't format message type %d", tx->type);
248
	if(write(wfd, txbuf, n) != n)
249
		sysfatal("couldn't send message");
250
}
251
 
252
void
253
getfcall(int fd, Fcall *fc)
254
{
255
	if(old9p == 1){
256
		getfcallold(fd, fc, 0);
257
		return;
258
	}
259
	if(old9p == 0){
260
		getfcallnew(fd, fc, 0);
261
		return;
262
	}
263
 
264
	/* auto-detect */
265
	if(readn(fd, rxbuf, 3) != 3)
266
		sysfatal("couldn't read message");
267
 
268
	/* is it an old (9P1) message? */
269
	if(50 <= rxbuf[0] && rxbuf[0] <= 87 && (rxbuf[0]&1)==0 && GBIT16(rxbuf+1) == 0xFFFF){
270
		old9p = 1;
271
		getfcallold(fd, fc, 3);
272
		return;
273
	}
274
 
275
	getfcallnew(fd, fc, 3);
276
	old9p = 0;
277
}
278
 
279
void
280
seterror(Fcall *f, char *error)
281
{
282
	f->type = Rerror;
283
	f->ename = error ? error : "programmer error";
284
}
285
 
286
int
287
isowner(User *u, Fid *f)
288
{
289
	return u->id == f->st.st_uid;
290
}
291
 
292
 
293
 
294
void
295
serve(int rfd, int wfd)
296
{
297
	Fcall rx, tx;
298
 
299
	for(;;){
300
		getfcall(rfd, &rx);
301
 
302
		if(chatty9p)
303
			fprint(2, "<- %F\n", &rx);
304
 
305
		memset(&tx, 0, sizeof tx);
306
		tx.type = rx.type+1;
307
		tx.tag = rx.tag;
308
		switch(rx.type){
309
		case Tflush:
310
			break;
311
		case Tversion:
312
			rversion(&rx, &tx);
313
			break;
314
		case Tauth:
315
			rauth(&rx, &tx);
316
			break;
317
		case Tattach:
318
			rattach(&rx, &tx);
319
			break;
320
		case Twalk:
321
			rwalk(&rx, &tx);
322
			break;
323
		case Tstat:
324
			tx.stat = databuf;
325
			rstat(&rx, &tx);
326
			break;
327
		case Twstat:
328
			rwstat(&rx, &tx);
329
			break;
330
		case Topen:
331
			ropen(&rx, &tx);
332
			break;
333
		case Tcreate:
334
			rcreate(&rx, &tx);
335
			break;
336
		case Tread:
337
			tx.data = databuf;
338
			rread(&rx, &tx);
339
			break;
340
		case Twrite:
341
			rwrite(&rx, &tx);
342
			break;
343
		case Tclunk:
344
			rclunk(&rx, &tx);
345
			break;
346
		case Tremove:
347
			rremove(&rx, &tx);
348
			break;
349
		default:
350
			fprint(2, "unknown message %F\n", &rx);
351
			seterror(&tx, "bad message");
352
			break;
353
		}
354
 
355
		if(chatty9p)
356
			fprint(2, "-> %F\n", &tx);
357
 
358
		(old9p ? putfcallold : putfcallnew)(wfd, &tx);
359
	}
360
}
361
 
362
void
363
rversion(Fcall *rx, Fcall *tx)
364
{
365
	if(msize > rx->msize)
366
		msize = rx->msize;
367
	tx->msize = msize;
368
	if(strncmp(rx->version, "9P", 2) != 0)
369
		tx->version = "unknown";
370
	else
371
		tx->version = "9P2000";
372
}
373
 
374
void
375
rauth(Fcall *rx, Fcall *tx)
376
{
377
	char *e;
378
 
379
	if((e = auth->auth(rx, tx)) != nil)
380
		seterror(tx, e);
381
}
382
 
383
void
384
rattach(Fcall *rx, Fcall *tx)
385
{
386
	char *e;
387
	Fid *fid;
388
	User *u;
389
 
390
	if(rx->aname == nil)
391
		rx->aname = "";
392
 
393
	if(strcmp(rx->aname, "device") == 0){
394
		if(connected && !devallowed){
395
			seterror(tx, Especial0);
396
			return;
397
		}
398
		devallowed = 1;
399
	}else{
400
		if(connected && devallowed){
401
			seterror(tx, Especial1);
402
			return;
403
		}
404
	}
405
 
406
	if(strcmp(rx->uname, "none") == 0){
407
		if(authed == 0){
408
			seterror(tx, Eauth);
409
			return;
410
		}
411
		if (none != nil)
412
			rx->uname = none->name;
413
	} else {
414
		if((e = auth->attach(rx, tx)) != nil){
415
			seterror(tx, e);
416
			return;
417
		}
418
		authed++;
419
	}
420
 
421
	if((fid = newfid(rx->fid, &e)) == nil){
422
		seterror(tx, e);
423
		return;
424
	}
425
	fid->path = estrdup("/");
426
	if(fidstat(fid, &e) < 0){
427
		seterror(tx, e);
428
		freefid(fid);
429
		return;
430
	}
431
 
432
	if(defaultuser)
433
		rx->uname = defaultuser;
434
 
435
	if((u = uname2user(rx->uname)) == nil
436
	|| (!defaultuser && u->id == 0)){
437
		/* we don't know anyone named root... */
438
		seterror(tx, Eunknownuser);
439
		freefid(fid);
440
		return;
441
	}
442
 
68 7u83 443
 
2 - 444
	fid->u = u;
445
	tx->qid = stat2qid(&fid->st);
69 7u83 446
	chroot("/srv");
2 - 447
	return;
448
}
449
 
450
void
451
rwalk(Fcall *rx, Fcall *tx)
452
{
69 7u83 453
 
2 - 454
	int i;
455
	char *path, *e;
456
	Fid *fid, *nfid;
457
 
458
	e = nil;
459
	if((fid = oldfid(rx->fid, &e)) == nil){
460
		seterror(tx, e);
461
		return;
462
	}
463
 
464
	if(fid->omode != -1){
465
		seterror(tx, Ebadusefid);
466
		return;
467
	}
468
 
469
	if(fidstat(fid, &e) < 0){
470
		seterror(tx, e);
471
		return;
472
	}
473
 
474
	if(!S_ISDIR(fid->st.st_mode) && rx->nwname){
475
		seterror(tx, Enotdir);
476
		return;
477
	}
478
 
479
	nfid = nil;
480
	if(rx->newfid != rx->fid && (nfid = newfid(rx->newfid, &e)) == nil){
481
		seterror(tx, e);
482
		return;
483
	}
484
 
485
	path = estrdup(fid->path);
486
	e = nil;
487
	for(i=0; i<rx->nwname; i++)
488
		if(userwalk(fid->u, &path, rx->wname[i], &tx->wqid[i], &e) < 0)
489
			break;
490
 
491
	if(i == rx->nwname){		/* successful clone or walk */
492
		tx->nwqid = i;
493
		if(nfid){
494
			nfid->path = path;
495
			nfid->u = fid->u;
496
		}else{
497
			free(fid->path);
498
			fid->path = path;
499
		}
500
	}else{
501
		if(i > 0)		/* partial walk? */
502
			tx->nwqid = i;
503
		else
504
			seterror(tx, e);
505
 
506
		if(nfid)		/* clone implicit new fid */
507
			freefid(nfid);
508
		free(path);
509
	}
510
	return;
511
}
512
 
513
void
514
ropen(Fcall *rx, Fcall *tx)
515
{
516
	char *e;
517
	Fid *fid;
518
 
519
	if((fid = oldfid(rx->fid, &e)) == nil){
520
		seterror(tx, e);
521
		return;
522
	}
523
 
524
	if(fid->omode != -1){
525
		seterror(tx, Ebadusefid);
526
		return;
527
	}
528
 
529
	if(fidstat(fid, &e) < 0){
530
		seterror(tx, e);
531
		return;
532
	}
533
 
534
	if(!devallowed && S_ISSPECIAL(fid->st.st_mode)){
535
		seterror(tx, Especial);
536
		return;
537
	}
538
 
539
	if(useropen(fid, rx->mode, &e) < 0){
540
		seterror(tx, e);
541
		return;
542
	}
543
 
544
	tx->iounit = 0;
545
	tx->qid = stat2qid(&fid->st);
546
}
547
 
548
void
549
rcreate(Fcall *rx, Fcall *tx)
550
{
551
	char *e;
552
	Fid *fid;
553
 
554
	if((fid = oldfid(rx->fid, &e)) == nil){
555
		seterror(tx, e);
556
		return;
557
	}
558
 
559
	if(fid->omode != -1){
560
		seterror(tx, Ebadusefid);
561
		return;
562
	}
563
 
564
	if(fidstat(fid, &e) < 0){
565
		seterror(tx, e);
566
		return;
567
	}
568
 
569
	if(!S_ISDIR(fid->st.st_mode)){
570
		seterror(tx, Enotdir);
571
		return;
572
	}
573
 
574
	if(usercreate(fid, rx->name, rx->mode, rx->perm, &e) < 0){
575
		seterror(tx, e);
576
		return;
577
	}
578
 
579
	if(fidstat(fid, &e) < 0){
580
		seterror(tx, e);
581
		return;
582
	}
583
 
584
	tx->iounit = 0;
585
	tx->qid = stat2qid(&fid->st);
586
}
587
 
588
uchar
589
modebyte(struct stat *st)
590
{
591
	uchar b;
592
 
593
	b = 0;
594
 
595
	if(S_ISDIR(st->st_mode))
596
		b |= QTDIR;
597
 
598
	/* no way to test append-only */
599
	/* no real way to test exclusive use, but mark devices as such */
600
	if(S_ISSPECIAL(st->st_mode))
601
		b |= QTEXCL;
602
 
603
	return b;
604
}
605
 
606
ulong
607
plan9mode(struct stat *st)
608
{
609
	return ((ulong)modebyte(st)<<24) | (st->st_mode & 0777);
610
}
611
 
612
/* 
613
 * this is for chmod, so don't worry about S_IFDIR
614
 */
615
mode_t
616
unixmode(Dir *d)
617
{
618
	return (mode_t)(d->mode&0777);
619
}
620
 
621
Qid
622
stat2qid(struct stat *st)
623
{
624
	uchar *p, *ep, *q;
625
	Qid qid;
626
 
627
	/*
628
	 * For now, ignore the device number.
629
	 */
630
	qid.path = 0;
631
	p = (uchar*)&qid.path;
632
	ep = p+sizeof(qid.path);
633
	q = p+sizeof(ino_t);
634
	if(q > ep){
635
		fprint(2, "warning: inode number too big\n");
636
		q = ep;
637
	}
638
	memmove(p, &st->st_ino, q-p);
639
	q = q+sizeof(dev_t);
640
	if(q > ep){
641
/*
642
 *		fprint(2, "warning: inode number + device number too big %d+%d\n",
643
 *			sizeof(ino_t), sizeof(dev_t));
644
 */
645
		q = ep - sizeof(dev_t);
646
		if(q < p)
647
			fprint(2, "warning: device number too big by itself\n");
648
		else
649
			*(dev_t*)q ^= st->st_dev;
650
	}
651
 
652
	qid.vers = st->st_mtime ^ (st->st_size << 8);
653
	qid.type = modebyte(st);
654
	return qid;
655
}
656
 
657
char *
658
enfrog(char *src)
659
{
660
	char *d, *dst;
661
	uchar *s;
662
 
663
	d = dst = emalloc(strlen(src)*3 + 1);
664
	for (s = (uchar *)src; *s; s++)
665
		if(isfrog[*s] || *s == '\\')
666
			d += sprintf(d, "\\%02x", *s);
667
		else
668
			*d++ = *s;
669
	*d = 0;
670
	return dst;
671
}
672
 
673
char *
674
defrog(char *s)
675
{
676
	char *d, *dst, buf[3];
677
 
678
	d = dst = emalloc(strlen(s) + 1);
679
	for(; *s; s++)
680
		if(*s == '\\' && strlen(s) >= 3){
681
			buf[0] = *++s;			/* skip \ */
682
			buf[1] = *++s;
683
			buf[2] = 0;
684
			*d++ = strtoul(buf, NULL, 16);
685
		} else
686
			*d++ = *s;
687
	*d = 0;
688
	return dst;
689
}
690
 
691
void
692
stat2dir(char *path, struct stat *st, Dir *d)
693
{
694
	User *u;
695
	char *q, *p, *npath;
696
 
697
	memset(d, 0, sizeof(*d));
698
	d->qid = stat2qid(st);
699
	d->mode = plan9mode(st);
700
	d->atime = st->st_atime;
701
	d->mtime = st->st_mtime;
702
	d->length = st->st_size;
703
 
704
	d->uid = (u = uid2user(st->st_uid)) ? u->name : "???";
705
	d->gid = (u = gid2user(st->st_gid)) ? u->name : "???";
706
	d->muid = "";
707
 
708
	if((q = strrchr(path, '/')) != nil)
709
		d->name = enfrog(q+1);
710
	else
711
		d->name = enfrog(path);
712
}
713
 
714
void
715
rread(Fcall *rx, Fcall *tx)
716
{
717
	char *e, *path;
718
	uchar *p, *ep;
719
	int n;
720
	Fid *fid;
721
	Dir d;
722
	struct stat st;
723
 
724
	if(rx->count > msize-IOHDRSZ){
725
		seterror(tx, Etoolarge);
726
		return;
727
	}
728
 
729
	if((fid = oldfidex(rx->fid, -1, &e)) == nil){
730
		seterror(tx, e);
731
		return;
732
	}
733
 
734
	if (fid->auth) {
735
		char *e;
736
		e = auth->read(rx, tx);
737
		if (e)
738
			seterror(tx, e);
739
		return;
740
	}
741
 
742
	if(fid->omode == -1 || (fid->omode&3) == OWRITE){
743
		seterror(tx, Ebadusefid);
744
		return;
745
	}
746
 
747
	if(fid->dir){
748
		if(rx->offset != fid->diroffset){
749
			if(rx->offset != 0){
750
				seterror(tx, Ebadoffset);
751
				return;
752
			}
753
			rewinddir(fid->dir);
754
			fid->diroffset = 0;
755
			fid->direof = 0;
756
		}
757
		if(fid->direof){
758
			tx->count = 0;
759
			return;
760
		}
761
 
762
		p = (uchar*)tx->data;
763
		ep = (uchar*)tx->data+rx->count;
764
		for(;;){
765
			if(p+BIT16SZ >= ep)
766
				break;
767
			if(fid->dirent == nil)	/* one entry cache for when convD2M fails */
768
				if((fid->dirent = readdir(fid->dir)) == nil){
769
					fid->direof = 1;
770
					break;
771
				}
772
			if(strcmp(fid->dirent->d_name, ".") == 0
773
			|| strcmp(fid->dirent->d_name, "..") == 0){
774
				fid->dirent = nil;
775
				continue;
776
			}
777
			path = estrpath(fid->path, fid->dirent->d_name, 0);
778
			memset(&st, 0, sizeof st);
779
			if(stat(path, &st) < 0){
780
				fprint(2, "dirread: stat(%s) failed: %s\n", path, strerror(errno));
781
				fid->dirent = nil;
782
				free(path);
783
				continue;
784
			}
785
			free(path);
786
			stat2dir(fid->dirent->d_name, &st, &d);
787
			if((n=(old9p ? convD2Mold : convD2M)(&d, p, ep-p)) <= BIT16SZ)
788
				break;
789
			p += n;
790
			fid->dirent = nil;
791
		}
792
		tx->count = p - (uchar*)tx->data;
793
		fid->diroffset += tx->count;
794
	}else{
795
		if((n = pread(fid->fd, tx->data, rx->count, rx->offset)) < 0){
796
			seterror(tx, strerror(errno));
797
			return;
798
		}
799
		tx->count = n;
800
	}
801
}
802
 
803
void
804
rwrite(Fcall *rx, Fcall *tx)
805
{
806
	char *e;
807
	Fid *fid;
808
	int n;
809
 
810
	if(rx->count > msize-IOHDRSZ){
811
		seterror(tx, Etoolarge);
812
		return;
813
	}
814
 
815
	if((fid = oldfidex(rx->fid, -1, &e)) == nil){
816
		seterror(tx, e);
817
		return;
818
	}
819
 
820
	if (fid->auth) {
821
		char *e;
822
		e = auth->write(rx, tx);
823
		if (e)
824
			seterror(tx, e);
825
		return;
826
	}
827
 
828
	if(fid->omode == -1 || (fid->omode&3) == OREAD || (fid->omode&3) == OEXEC){
829
		seterror(tx, Ebadusefid);
830
		return;
831
	}
832
 
833
	if((n = pwrite(fid->fd, rx->data, rx->count, rx->offset)) < 0){
834
		seterror(tx, strerror(errno));
835
		return;
836
	}
837
	tx->count = n;
838
}
839
 
840
void
841
rclunk(Fcall *rx, Fcall *tx)
842
{
843
	char *e;
844
	Fid *fid;
845
 
846
	if((fid = oldfidex(rx->fid, -1, &e)) == nil){
847
		seterror(tx, e);
848
		return;
849
	}
850
	if (fid->auth) {
851
		if (auth->clunk) {
852
			e = (*auth->clunk)(rx, tx);
853
			if (e) {
854
				seterror(tx, e);
855
				return;
856
			}
857
		}
858
	}
859
	else if(fid->omode != -1 && fid->omode&ORCLOSE)
860
		remove(fid->path);
861
	freefid(fid);
862
}
863
 
864
void
865
rremove(Fcall *rx, Fcall *tx)
866
{
867
	char *e;
868
	Fid *fid;
869
 
870
	if((fid = oldfid(rx->fid, &e)) == nil){
871
		seterror(tx, e);
872
		return;
873
	}
874
	if(userremove(fid, &e) < 0)
875
		seterror(tx, e);
876
	freefid(fid);
877
}
878
 
879
void
880
rstat(Fcall *rx, Fcall *tx)
881
{
882
	char *e;
883
	Fid *fid;
884
	Dir d;
885
 
886
	if((fid = oldfid(rx->fid, &e)) == nil){
887
		seterror(tx, e);
888
		return;
889
	}
890
 
891
	if(fidstat(fid, &e) < 0){
892
		seterror(tx, e);
893
		return;
894
	}
895
 
896
	stat2dir(fid->path, &fid->st, &d);
897
	if((tx->nstat=(old9p ? convD2Mold : convD2M)(&d, tx->stat, msize)) <= BIT16SZ)
898
		seterror(tx, "convD2M fails");
899
}
900
 
901
void
902
rwstat(Fcall *rx, Fcall *tx)
903
{
904
	char *e;
905
	char *p, *old, *new, *dir;
906
	gid_t gid;
907
	Dir d;
908
	Fid *fid;
909
 
910
	if((fid = oldfid(rx->fid, &e)) == nil){
911
		seterror(tx, e);
912
		return;
913
	}
914
 
915
	/*
916
	 * wstat is supposed to be atomic.
917
	 * we check all the things we can before trying anything.
918
	 * still, if we are told to truncate a file and rename it and only
919
	 * one works, we're screwed.  in such cases we leave things
920
	 * half broken and return an error.  it's hardly perfect.
921
	 */
922
	if((old9p ? convM2Dold : convM2D)(rx->stat, rx->nstat, &d, (char*)rx->stat) <= BIT16SZ){
923
		seterror(tx, Ewstatbuffer);
924
		return;
925
	}
926
 
927
	if(fidstat(fid, &e) < 0){
928
		seterror(tx, e);
929
		return;
930
	}
931
 
932
	/*
933
	 * The casting is necessary because d.mode is ulong and might,
934
	 * on some systems, be 64 bits.  We only want to compare the
935
	 * bottom 32 bits, since that's all that gets sent in the protocol.
936
	 * 
937
	 * Same situation for d.mtime and d.length (although that last check
938
	 * is admittedly superfluous, given the current lack of 128-bit machines).
939
	 */
940
	gid = (gid_t)-1;
941
	if(d.gid[0] != '\0'){
942
		User *g;
943
 
944
		g = gname2user(d.gid);
945
		if(g == nil){
946
			seterror(tx, Eunknowngroup);
947
			return;
948
		}
949
		gid = (gid_t)g->id;
950
 
951
		if(groupchange(fid->u, gid2user(gid), &e) < 0){
952
			seterror(tx, e);
953
			return;
954
		}		
955
	}
956
 
957
	if((u32int)d.mode != (u32int)~0 && (((d.mode&DMDIR)!=0) ^ (S_ISDIR(fid->st.st_mode)!=0))){
958
		seterror(tx, Edirchange);
959
		return;
960
	}
961
 
962
	if(strcmp(fid->path, "/") == 0){
963
		seterror(tx, "no wstat of root");
964
		return;
965
	}
966
 
967
	/*
968
	 * try things in increasing order of harm to the file.
969
	 * mtime should come after truncate so that if you
970
	 * do both the mtime actually takes effect, but i'd rather
971
	 * leave truncate until last.
972
	 * (see above comment about atomicity).
973
	 */
974
	if((u32int)d.mode != (u32int)~0 && chmod(fid->path, unixmode(&d)) < 0){
975
		if(chatty9p)
976
			fprint(2, "chmod(%s, 0%luo) failed\n", fid->path, unixmode(&d));
977
		seterror(tx, strerror(errno));
978
		return;
979
	}
980
 
981
	if((u32int)d.mtime != (u32int)~0){
982
		struct utimbuf t;
983
 
984
		t.actime = 0;
985
		t.modtime = d.mtime;
986
		if(utime(fid->path, &t) < 0){
987
			if(chatty9p)
988
				fprint(2, "utime(%s) failed\n", fid->path);
989
			seterror(tx, strerror(errno));
990
			return;
991
		}
992
	}
993
 
994
	if(gid != (gid_t)-1 && gid != fid->st.st_gid){
995
		if(chown(fid->path, (uid_t)-1, gid) < 0){
996
			if(chatty9p)
997
				fprint(2, "chgrp(%s, %d) failed\n", fid->path, gid);
998
			seterror(tx, strerror(errno));
999
			return;
1000
		}
1001
	}
1002
 
1003
	if(d.name[0]){
1004
		old = fid->path;
1005
		dir = estrdup(fid->path);
1006
		if((p = strrchr(dir, '/')) > dir)
1007
			*p = '\0';
1008
		else{
1009
			seterror(tx, "whoops: can't happen in u9fs");
1010
			return;
1011
		}
1012
		new = estrpath(dir, d.name, 1);
1013
		if(strcmp(old, new) != 0 && rename(old, new) < 0){
1014
			if(chatty9p)
1015
				fprint(2, "rename(%s, %s) failed\n", old, new);
1016
			seterror(tx, strerror(errno));
1017
			free(new);
1018
			free(dir);
1019
			return;
1020
		}
1021
		fid->path = new;
1022
		free(old);
1023
		free(dir);
1024
	}
1025
 
1026
	if((u64int)d.length != (u64int)~0 && truncate(fid->path, d.length) < 0){
1027
		fprint(2, "truncate(%s, %lld) failed\n", fid->path, d.length);
1028
		seterror(tx, strerror(errno));
1029
		return;
1030
	}
1031
}
1032
 
1033
/*
1034
 * we keep a table by numeric id.  by name lookups happen infrequently
1035
 * while by-number lookups happen once for every directory entry read
1036
 * and every stat request.
1037
 */
1038
User *utab[64];
1039
User *gtab[64];
1040
 
1041
User*
1042
adduser(struct passwd *p)
1043
{
1044
	User *u;
1045
 
1046
	u = emalloc(sizeof(*u));
1047
	u->id = p->pw_uid;
1048
	u->name = estrdup(p->pw_name);
1049
	u->next = utab[p->pw_uid%nelem(utab)];
1050
	u->defaultgid = p->pw_gid;
1051
	utab[p->pw_uid%nelem(utab)] = u;
1052
	return u;
1053
}
1054
 
1055
int
1056
useringroup(User *u, User *g)
1057
{
1058
	int i;
1059
 
1060
	for(i=0; i<g->nmem; i++)
1061
		if(strcmp(g->mem[i], u->name) == 0)
1062
			return 1;
1063
 
1064
	/*
1065
	 * Hack around common Unix problem that everyone has
1066
	 * default group "user" but /etc/group lists no members.
1067
	 */
1068
	if(u->defaultgid == g->id)
1069
		return 1;
1070
	return 0;
1071
}
1072
 
1073
User*
1074
addgroup(struct group *g)
1075
{
1076
	User *u;
1077
	char **p;
1078
	int n;
1079
 
1080
	u = emalloc(sizeof(*u));
1081
	n = 0;
1082
	for(p=g->gr_mem; *p; p++)
1083
		n++;
1084
	u->mem = emalloc(sizeof(u->mem[0])*n);
1085
	n = 0;
1086
	for(p=g->gr_mem; *p; p++)
1087
		u->mem[n++] = estrdup(*p);
1088
	u->nmem = n;
1089
	u->id = g->gr_gid;
1090
	u->name = estrdup(g->gr_name);
1091
	u->next = gtab[g->gr_gid%nelem(gtab)];
1092
	gtab[g->gr_gid%nelem(gtab)] = u;
1093
	return u;
1094
}
1095
 
1096
User*
1097
uname2user(char *name)
1098
{
1099
	int i;
1100
	User *u;
1101
	struct passwd *p;
1102
 
1103
	for(i=0; i<nelem(utab); i++)
1104
		for(u=utab[i]; u; u=u->next)
1105
			if(strcmp(u->name, name) == 0)
1106
				return u;
1107
 
1108
	if((p = getpwnam(name)) == nil)
1109
		return nil;
1110
	return adduser(p);
1111
}
1112
 
1113
User*
1114
uid2user(int id)
1115
{
1116
	User *u;
1117
	struct passwd *p;
1118
 
1119
	for(u=utab[id%nelem(utab)]; u; u=u->next)
1120
		if(u->id == id)
1121
			return u;
1122
 
1123
	if((p = getpwuid(id)) == nil)
1124
		return nil;
1125
	return adduser(p);
1126
}
1127
 
1128
User*
1129
gname2user(char *name)
1130
{
1131
	int i;
1132
	User *u;
1133
	struct group *g;
1134
 
1135
	for(i=0; i<nelem(gtab); i++)
1136
		for(u=gtab[i]; u; u=u->next)
1137
			if(strcmp(u->name, name) == 0)
1138
				return u;
1139
 
1140
	if((g = getgrnam(name)) == nil)
1141
		return nil;
1142
	return addgroup(g);
1143
}
1144
 
1145
User*
1146
gid2user(int id)
1147
{
1148
	User *u;
1149
	struct group *g;
1150
 
1151
	for(u=gtab[id%nelem(gtab)]; u; u=u->next)
1152
		if(u->id == id)
1153
			return u;
1154
 
1155
	if((g = getgrgid(id)) == nil)
1156
		return nil;
1157
	return addgroup(g);
1158
}
1159
 
1160
void
1161
sysfatal(char *fmt, ...)
1162
{
1163
	char buf[1024];
1164
	va_list va, temp;
1165
 
1166
	va_start(va, fmt);
1167
	va_copy(temp, va);
1168
	doprint(buf, buf+sizeof buf, fmt, &temp);
1169
	va_end(temp);
1170
	va_end(va);
1171
	fprint(2, "u9fs: %s\n", buf);
1172
	fprint(2, "last unix error: %s\n", strerror(errno));
1173
	exit(1);
1174
}
1175
 
1176
void*
1177
emalloc(size_t n)
1178
{
1179
	void *p;
1180
 
1181
	if(n == 0)
1182
		n = 1;
1183
	p = malloc(n);
1184
	if(p == 0)
1185
		sysfatal("malloc(%ld) fails", (long)n);
1186
	memset(p, 0, n);
1187
	return p;
1188
}
1189
 
1190
void*
1191
erealloc(void *p, size_t n)
1192
{
1193
	if(p == 0)
1194
		p = malloc(n);
1195
	else
1196
		p = realloc(p, n);
1197
	if(p == 0)
1198
		sysfatal("realloc(..., %ld) fails", (long)n);
1199
	return p;
1200
}
1201
 
1202
char*
1203
estrdup(char *p)
1204
{
1205
	p = strdup(p);
1206
	if(p == 0)
1207
		sysfatal("strdup(%.20s) fails", p);
1208
	return p;
1209
}
1210
 
1211
char*
1212
estrpath(char *p, char *q, int frog)
1213
{
1214
	char *r, *s;
1215
 
1216
	if(strcmp(q, "..") == 0){
1217
		r = estrdup(p);
1218
		if((s = strrchr(r, '/')) && s > r)
1219
			*s = '\0';
1220
		else if(s == r)
1221
			s[1] = '\0';
1222
		return r;
1223
	}
1224
 
1225
	if(frog)
1226
		q = defrog(q);
1227
	else
1228
		q = strdup(q);
1229
	r = emalloc(strlen(p)+1+strlen(q)+1);
1230
	strcpy(r, p);
1231
	if(r[0]=='\0' || r[strlen(r)-1] != '/')
1232
		strcat(r, "/");
1233
	strcat(r, q);
1234
	free(q);
1235
	return r;
1236
}
1237
 
1238
Fid *fidtab[1];
1239
 
1240
Fid*
1241
lookupfid(int fid)
1242
{
1243
	Fid *f;
1244
 
1245
	for(f=fidtab[fid%nelem(fidtab)]; f; f=f->next)
1246
		if(f->fid == fid)
1247
			return f;
1248
	return nil;
1249
}
1250
 
1251
Fid*
1252
newfid(int fid, char **ep)
1253
{
1254
	Fid *f;
1255
 
1256
	if(lookupfid(fid) != nil){
1257
		*ep = Efidactive;
1258
		return nil;
1259
	}
1260
 
1261
	f = emalloc(sizeof(*f));
1262
	f->next = fidtab[fid%nelem(fidtab)];
1263
	if(f->next)
1264
		f->next->prev = f;
1265
	fidtab[fid%nelem(fidtab)] = f;
1266
	f->fid = fid;
1267
	f->fd = -1;
1268
	f->omode = -1;
1269
	return f;
1270
}
1271
 
1272
Fid*
1273
newauthfid(int fid, void *magic, char **ep)
1274
{
1275
	Fid *af;
1276
	af = newfid(fid, ep);
1277
	if (af == nil)
1278
		return nil;
1279
	af->auth = 1;
1280
	af->authmagic = magic;
1281
	return af;
1282
}
1283
 
1284
Fid*
1285
oldfidex(int fid, int auth, char **ep)
1286
{
1287
	Fid *f;
1288
 
1289
	if((f = lookupfid(fid)) == nil){
1290
		*ep = Ebadfid;
1291
		return nil;
1292
	}
1293
 
1294
	if (auth != -1 && f->auth != auth) {
1295
		*ep = Ebadfid;
1296
		return nil;
1297
	}
1298
 
1299
	if (!f->auth) {
1300
		if(userchange(f->u, ep) < 0)
1301
			return nil;
1302
	}
1303
 
1304
	return f;
1305
}
1306
 
1307
Fid*
1308
oldfid(int fid, char **ep)
1309
{
1310
	return oldfidex(fid, 0, ep);
1311
}
1312
 
1313
Fid*
1314
oldauthfid(int fid, void **magic, char **ep)
1315
{
1316
	Fid *af;
1317
	af = oldfidex(fid, 1, ep);
1318
	if (af == nil)
1319
		return nil;
1320
	*magic = af->authmagic;
1321
	return af;
1322
}
1323
 
1324
void
1325
freefid(Fid *f)
1326
{
1327
	if(f->prev)
1328
		f->prev->next = f->next;
1329
	else
1330
		fidtab[f->fid%nelem(fidtab)] = f->next;
1331
	if(f->next)
1332
		f->next->prev = f->prev;
1333
	if(f->dir)
1334
		closedir(f->dir);
1335
	if(f->fd)
1336
		close(f->fd);
1337
	free(f->path);
1338
	free(f);
1339
}
1340
 
1341
int
1342
fidstat(Fid *fid, char **ep)
1343
{
1344
	if(stat(fid->path, &fid->st) < 0){
1345
		fprint(2, "fidstat(%s) failed\n", fid->path);
1346
		if(ep)
1347
			*ep = strerror(errno);
1348
		return -1;
1349
	}
1350
	if(S_ISDIR(fid->st.st_mode))
1351
		fid->st.st_size = 0;
1352
	return 0;
1353
}
1354
 
1355
int
1356
userchange(User *u, char **ep)
1357
{
1358
	if(defaultuser)
1359
		return 0;
1360
 
1361
	if(setreuid(0, 0) < 0){
1362
		fprint(2, "setreuid(0, 0) failed\n");
1363
		*ep = "cannot setuid back to root";
1364
		return -1;
1365
	}
1366
 
1367
	/*
1368
	 * Initgroups does not appear to be SUSV standard.
1369
	 * But it exists on SGI and on Linux, which makes me
1370
	 * think it's standard enough.  We have to do something
1371
	 * like this, and the closest other function I can find is
1372
	 * setgroups (which initgroups eventually calls).
1373
	 * Setgroups is the same as far as standardization though,
1374
	 * so we're stuck using a non-SUSV call.  Sigh.
1375
	 */
1376
	if(initgroups(u->name, u->defaultgid) < 0)
1377
		fprint(2, "initgroups(%s) failed: %s\n", u->name, strerror(errno));
1378
 
1379
	if(setreuid(-1, u->id) < 0){
1380
		fprint(2, "setreuid(-1, %s) failed\n", u->name);
1381
		*ep = strerror(errno);
1382
		return -1;
1383
	}
1384
 
1385
	return 0;
1386
}
1387
 
1388
/*
1389
 * We do our own checking here, then switch to root temporarily
1390
 * to set our gid.  In a perfect world, you'd be allowed to set your
1391
 * egid to any of the supplemental groups of your euid, but this
1392
 * is not the case on Linux 2.2.14 (and perhaps others).
1393
 *
1394
 * This is a race, of course, but it's a race against processes
1395
 * that can edit the group lists.  If you can do that, you can
1396
 * change your own group without our help.
1397
 */
1398
int
1399
groupchange(User *u, User *g, char **ep)
1400
{
1401
	if(g == nil)
1402
		return -1;
1403
	if(!useringroup(u, g)){
1404
		if(chatty9p)
1405
			fprint(2, "%s not in group %s\n", u->name, g->name);
1406
		*ep = Enotingroup;
1407
		return -1;
1408
	}
1409
 
1410
	setreuid(0,0);
1411
	if(setregid(-1, g->id) < 0){
1412
		fprint(2, "setegid(%s/%d) failed in groupchange\n", g->name, g->id);
1413
		*ep = strerror(errno);
1414
		return -1;
1415
	}
1416
	if(userchange(u, ep) < 0)
1417
		return -1;
1418
 
1419
	return 0;
1420
}
1421
 
1422
 
1423
/*
1424
 * An attempt to enforce permissions by looking at the 
1425
 * file system.  Separation of checking permission and
1426
 * actually performing the action is a terrible idea, of 
1427
 * course, so we use setreuid for most of the permission
1428
 * enforcement.  This is here only so we can give errors
1429
 * on open(ORCLOSE) in some cases.
1430
 */
1431
int
1432
userperm(User *u, char *path, int type, int need)
1433
{
1434
	char *p, *q;
1435
	int i, have;
1436
	struct stat st;
1437
	User *g;
1438
 
1439
	switch(type){
1440
	default:
1441
		fprint(2, "bad type %d in userperm\n", type);
1442
		return -1;
1443
	case Tdot:
1444
		if(stat(path, &st) < 0){
1445
			fprint(2, "userperm: stat(%s) failed\n", path);
1446
			return -1;
1447
		}
1448
		break;
1449
	case Tdotdot:
1450
		p = estrdup(path);
1451
		if((q = strrchr(p, '/'))==nil){
1452
			fprint(2, "userperm(%s, ..): bad path\n", p);
1453
			free(p);
1454
			return -1;
1455
		}
1456
		if(q > p)
1457
			*q = '\0';
1458
		else
1459
			*(q+1) = '\0';
1460
		if(stat(p, &st) < 0){
1461
			fprint(2, "userperm: stat(%s) (dotdot of %s) failed\n",
1462
				p, path);
1463
			free(p);
1464
			return -1;
1465
		}
1466
		free(p);
1467
		break;
1468
	}
1469
 
1470
	if(u == none){
1471
		fprint(2, "userperm: none wants %d in 0%luo\n", need, st.st_mode);
1472
		have = st.st_mode&7;
1473
		if((have&need)==need)
1474
			return 0;
1475
		return -1;
1476
	}
1477
	have = st.st_mode&7;
1478
	if((uid_t)u->id == st.st_uid)
1479
		have |= (st.st_mode>>6)&7;
1480
	if((have&need)==need)
1481
		return 0;
1482
	if(((have|((st.st_mode>>3)&7))&need) != need)	/* group won't help */
1483
		return -1;
1484
	g = gid2user(st.st_gid);
1485
	for(i=0; i<g->nmem; i++){
1486
		if(strcmp(g->mem[i], u->name) == 0){
1487
			have |= (st.st_mode>>3)&7;
1488
			break;
1489
		}
1490
	}
1491
	if((have&need)==need)
1492
		return 0;
1493
	return -1;
1494
}
1495
 
1496
int
1497
userwalk(User *u, char **path, char *elem, Qid *qid, char **ep)
1498
{
1499
	char *npath;
1500
	struct stat st;
1501
 
1502
	npath = estrpath(*path, elem, 1);
1503
	if(stat(npath, &st) < 0){
1504
		free(npath);
1505
		*ep = strerror(errno);
1506
		return -1;
1507
	}
1508
	*qid = stat2qid(&st);
1509
	free(*path);
1510
	*path = npath;
1511
	return 0;
1512
}
1513
 
1514
int
1515
useropen(Fid *fid, int omode, char **ep)
1516
{
1517
	int a, o;
1518
 
1519
	/*
1520
	 * Check this anyway, to try to head off problems later.
1521
	 */
1522
	if((omode&ORCLOSE) && userperm(fid->u, fid->path, Tdotdot, W_OK) < 0){
1523
		*ep = Eperm;
1524
		return -1;
1525
	}
1526
 
1527
	switch(omode&3){
1528
	default:
1529
		*ep = "programmer error";
1530
		return -1;
1531
	case OREAD:
1532
		a = R_OK;
1533
		o = O_RDONLY;
1534
		break;
1535
	case ORDWR:
1536
		a = R_OK|W_OK;
1537
		o = O_RDWR;
1538
		break;
1539
	case OWRITE:
1540
		a = W_OK;
1541
		o = O_WRONLY;
1542
		break;
1543
	case OEXEC:
1544
		a = X_OK;
1545
		o = O_RDONLY;
1546
		break;
1547
	}
1548
	if(omode & OTRUNC){
1549
		a |= W_OK;
1550
		o |= O_TRUNC;
1551
	}
1552
 
1553
	if(S_ISDIR(fid->st.st_mode)){
1554
		if(a != R_OK){
1555
			fprint(2, "attempt by %s to open dir %d\n", fid->u->name, omode);
1556
			*ep = Eperm;
1557
			return -1;
1558
		}
1559
		if((fid->dir = opendir(fid->path)) == nil){
1560
			*ep = strerror(errno);
1561
			return -1;
1562
		}
1563
	}else{
1564
		/*
1565
		 * This is wrong because access used the real uid
1566
		 * and not the effective uid.  Let the open sort it out.
1567
		 *
1568
		if(access(fid->path, a) < 0){
1569
			*ep = strerror(errno);
1570
			return -1;
1571
		}
1572
		 *
1573
		 */
1574
		if((fid->fd = open(fid->path, o)) < 0){
1575
			*ep = strerror(errno);
1576
			return -1;
1577
		}
1578
	}
1579
	fid->omode = omode;
1580
	return 0;
1581
}
1582
 
1583
int
1584
usercreate(Fid *fid, char *elem, int omode, long perm, char **ep)
1585
{
1586
	int o, m;
1587
	char *opath, *npath;
1588
	struct stat st, parent;
1589
 
1590
	if(stat(fid->path, &parent) < 0){
1591
		*ep = strerror(errno);
1592
		return -1;
1593
	}
1594
 
1595
	/*
1596
	 * Change group so that created file has expected group
1597
	 * by Plan 9 semantics.  If that fails, might as well go
1598
	 * with the user's default group.
1599
	 */
1600
	if(groupchange(fid->u, gid2user(parent.st_gid), ep) < 0
1601
	&& groupchange(fid->u, gid2user(fid->u->defaultgid), ep) < 0)
1602
		return -1;
1603
 
1604
	m = (perm & DMDIR) ? 0777 : 0666;
1605
	perm = perm & (~m | (fid->st.st_mode & m));
1606
 
1607
	npath = estrpath(fid->path, elem, 1);
1608
	if(perm & DMDIR){
1609
		if((omode&~ORCLOSE) != OREAD){
1610
			*ep = Eperm;
1611
			free(npath);
1612
			return -1;
1613
		}
1614
		if(stat(npath, &st) >= 0 || errno != ENOENT){
1615
			*ep = Eexist;
1616
			free(npath);
1617
			return -1;
1618
		}
1619
		/* race */
1620
		if(mkdir(npath, (perm|0400)&0777) < 0){
1621
			*ep = strerror(errno);
1622
			free(npath);
1623
			return -1;
1624
		}
1625
		if((fid->dir = opendir(npath)) == nil){
1626
			*ep = strerror(errno);
1627
			remove(npath);		/* race */
1628
			free(npath);
1629
			return -1;
1630
		}
1631
	}else{
1632
		o = O_CREAT|O_EXCL;
1633
		switch(omode&3){
1634
		default:
1635
			*ep = "programmer error";
1636
			return -1;
1637
		case OREAD:
1638
		case OEXEC:
1639
			o |= O_RDONLY;
1640
			break;
1641
		case ORDWR:
1642
			o |= O_RDWR;
1643
			break;
1644
		case OWRITE:
1645
			o |= O_WRONLY;
1646
			break;
1647
		}
1648
		if(omode & OTRUNC)
1649
			o |= O_TRUNC;
1650
		if((fid->fd = open(npath, o, perm&0777)) < 0){
1651
			if(chatty9p)
1652
				fprint(2, "create(%s, 0x%x, 0%o) failed\n", npath, o, perm&0777);
1653
			*ep = strerror(errno);
1654
			free(npath);
1655
			return -1;
1656
		}
1657
	}
1658
 
1659
	opath = fid->path;
1660
	fid->path = npath;
1661
	if(fidstat(fid, ep) < 0){
1662
		fprint(2, "stat after create on %s failed\n", npath);
1663
		remove(npath);	/* race */
1664
		free(npath);
1665
		fid->path = opath;
1666
		if(fid->fd >= 0){
1667
			close(fid->fd);
1668
			fid->fd = -1;
1669
		}else{
1670
			closedir(fid->dir);
1671
			fid->dir = nil;
1672
		}
1673
		return -1;
1674
	}
1675
	fid->omode = omode;
1676
	free(opath);
1677
	return 0;
1678
}
1679
 
1680
int
1681
userremove(Fid *fid, char **ep)
1682
{
1683
	if(remove(fid->path) < 0){
1684
		*ep = strerror(errno);
1685
		return -1;
1686
	}
1687
	return 0;
1688
}
1689
 
1690
void
1691
usage(void)
1692
{
1693
	fprint(2, "usage: u9fs [-Dnz] [-a authmethod] [-m msize] [-u user] [root]\n");
1694
	exit(1);
1695
}
1696
 
1697
int
1698
main(int argc, char **argv)
1699
{
1700
	char *authtype;
1701
	int i;
1702
	int fd;
1703
	int logflag;
1704
 
1705
	auth = authmethods[0];
1706
	logflag = O_WRONLY|O_APPEND|O_CREAT;
1707
	ARGBEGIN{
1708
	case 'D':
1709
		chatty9p = 1;
1710
		break;
1711
	case 'a':
1712
		authtype = EARGF(usage());
1713
		auth = nil;
1714
		for(i=0; i<nelem(authmethods); i++)
1715
			if(strcmp(authmethods[i]->name, authtype)==0)
1716
				auth = authmethods[i];
1717
		if(auth == nil)
1718
			sysfatal("unknown auth type '%s'", authtype);
1719
		break;
1720
	case 'A':
1721
		autharg = EARGF(usage());
1722
		break;
1723
	case 'l':
1724
		logfile = EARGF(usage());
1725
		break;
1726
	case 'm':
1727
		msize = strtol(EARGF(usage()), 0, 0);
1728
		break;
1729
	case 'n':
1730
		network = 0;
1731
		break;
1732
	case 'u':
1733
		defaultuser = EARGF(usage());
1734
		break;
1735
	case 'z':
1736
		logflag |= O_TRUNC;
1737
	}ARGEND
1738
 
1739
	if(argc > 1)
1740
		usage();
1741
 
1742
	fd = open(logfile, logflag, 0666);
1743
	if(fd < 0)
1744
		sysfatal("cannot open log '%s'", logfile);
1745
 
1746
	if(dup2(fd, 2) < 0)
1747
		sysfatal("cannot dup fd onto stderr");
1748
	fprint(2, "u9fs\nkill %d\n", (int)getpid());
1749
 
1750
	fmtinstall('F', fcallconv);
1751
	fmtinstall('D', dirconv);
1752
	fmtinstall('M', dirmodeconv);
1753
 
1754
	rxbuf = emalloc(msize);
1755
	txbuf = emalloc(msize);
1756
	databuf = emalloc(msize);
1757
 
1758
	if(auth->init)
1759
		auth->init();
1760
 
1761
	if(network)
1762
		getremotehostname(remotehostname, sizeof remotehostname);
1763
 
1764
	if(gethostname(hostname, sizeof hostname) < 0)
1765
		strcpy(hostname, "gnot");
1766
 
1767
	umask(0);
1768
 
1769
	if(argc == 1)
1770
		if(chroot(argv[0]) < 0)
1771
			sysfatal("chroot '%s' failed", argv[0]);
1772
 
1773
	none = uname2user("none");
1774
	if(none == nil)
1775
		none = uname2user("nobody");
1776
 
1777
	serve(0, 1);
1778
	return 0;
1779
}