Subversion Repositories planix.SVN

Rev

Rev 65 | Rev 69 | 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,
173
	['/']	1,
174
	[0x7f]	1,
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
//	chroot("/root/planix/sys");
444
 
2 - 445
	fid->u = u;
446
	tx->qid = stat2qid(&fid->st);
447
	return;
448
}
449
 
450
void
451
rwalk(Fcall *rx, Fcall *tx)
452
{
453
	int i;
454
	char *path, *e;
455
	Fid *fid, *nfid;
456
 
457
	e = nil;
458
	if((fid = oldfid(rx->fid, &e)) == nil){
459
		seterror(tx, e);
460
		return;
461
	}
462
 
463
	if(fid->omode != -1){
464
		seterror(tx, Ebadusefid);
465
		return;
466
	}
467
 
468
	if(fidstat(fid, &e) < 0){
469
		seterror(tx, e);
470
		return;
471
	}
472
 
473
	if(!S_ISDIR(fid->st.st_mode) && rx->nwname){
474
		seterror(tx, Enotdir);
475
		return;
476
	}
477
 
478
	nfid = nil;
479
	if(rx->newfid != rx->fid && (nfid = newfid(rx->newfid, &e)) == nil){
480
		seterror(tx, e);
481
		return;
482
	}
483
 
484
	path = estrdup(fid->path);
485
	e = nil;
486
	for(i=0; i<rx->nwname; i++)
487
		if(userwalk(fid->u, &path, rx->wname[i], &tx->wqid[i], &e) < 0)
488
			break;
489
 
490
	if(i == rx->nwname){		/* successful clone or walk */
491
		tx->nwqid = i;
492
		if(nfid){
493
			nfid->path = path;
494
			nfid->u = fid->u;
495
		}else{
496
			free(fid->path);
497
			fid->path = path;
498
		}
499
	}else{
500
		if(i > 0)		/* partial walk? */
501
			tx->nwqid = i;
502
		else
503
			seterror(tx, e);
504
 
505
		if(nfid)		/* clone implicit new fid */
506
			freefid(nfid);
507
		free(path);
508
	}
509
	return;
510
}
511
 
512
void
513
ropen(Fcall *rx, Fcall *tx)
514
{
515
	char *e;
516
	Fid *fid;
517
 
518
	if((fid = oldfid(rx->fid, &e)) == nil){
519
		seterror(tx, e);
520
		return;
521
	}
522
 
523
	if(fid->omode != -1){
524
		seterror(tx, Ebadusefid);
525
		return;
526
	}
527
 
528
	if(fidstat(fid, &e) < 0){
529
		seterror(tx, e);
530
		return;
531
	}
532
 
533
	if(!devallowed && S_ISSPECIAL(fid->st.st_mode)){
534
		seterror(tx, Especial);
535
		return;
536
	}
537
 
538
	if(useropen(fid, rx->mode, &e) < 0){
539
		seterror(tx, e);
540
		return;
541
	}
542
 
543
	tx->iounit = 0;
544
	tx->qid = stat2qid(&fid->st);
545
}
546
 
547
void
548
rcreate(Fcall *rx, Fcall *tx)
549
{
550
	char *e;
551
	Fid *fid;
552
 
553
	if((fid = oldfid(rx->fid, &e)) == nil){
554
		seterror(tx, e);
555
		return;
556
	}
557
 
558
	if(fid->omode != -1){
559
		seterror(tx, Ebadusefid);
560
		return;
561
	}
562
 
563
	if(fidstat(fid, &e) < 0){
564
		seterror(tx, e);
565
		return;
566
	}
567
 
568
	if(!S_ISDIR(fid->st.st_mode)){
569
		seterror(tx, Enotdir);
570
		return;
571
	}
572
 
573
	if(usercreate(fid, rx->name, rx->mode, rx->perm, &e) < 0){
574
		seterror(tx, e);
575
		return;
576
	}
577
 
578
	if(fidstat(fid, &e) < 0){
579
		seterror(tx, e);
580
		return;
581
	}
582
 
583
	tx->iounit = 0;
584
	tx->qid = stat2qid(&fid->st);
585
}
586
 
587
uchar
588
modebyte(struct stat *st)
589
{
590
	uchar b;
591
 
592
	b = 0;
593
 
594
	if(S_ISDIR(st->st_mode))
595
		b |= QTDIR;
596
 
597
	/* no way to test append-only */
598
	/* no real way to test exclusive use, but mark devices as such */
599
	if(S_ISSPECIAL(st->st_mode))
600
		b |= QTEXCL;
601
 
602
	return b;
603
}
604
 
605
ulong
606
plan9mode(struct stat *st)
607
{
608
	return ((ulong)modebyte(st)<<24) | (st->st_mode & 0777);
609
}
610
 
611
/* 
612
 * this is for chmod, so don't worry about S_IFDIR
613
 */
614
mode_t
615
unixmode(Dir *d)
616
{
617
	return (mode_t)(d->mode&0777);
618
}
619
 
620
Qid
621
stat2qid(struct stat *st)
622
{
623
	uchar *p, *ep, *q;
624
	Qid qid;
625
 
626
	/*
627
	 * For now, ignore the device number.
628
	 */
629
	qid.path = 0;
630
	p = (uchar*)&qid.path;
631
	ep = p+sizeof(qid.path);
632
	q = p+sizeof(ino_t);
633
	if(q > ep){
634
		fprint(2, "warning: inode number too big\n");
635
		q = ep;
636
	}
637
	memmove(p, &st->st_ino, q-p);
638
	q = q+sizeof(dev_t);
639
	if(q > ep){
640
/*
641
 *		fprint(2, "warning: inode number + device number too big %d+%d\n",
642
 *			sizeof(ino_t), sizeof(dev_t));
643
 */
644
		q = ep - sizeof(dev_t);
645
		if(q < p)
646
			fprint(2, "warning: device number too big by itself\n");
647
		else
648
			*(dev_t*)q ^= st->st_dev;
649
	}
650
 
651
	qid.vers = st->st_mtime ^ (st->st_size << 8);
652
	qid.type = modebyte(st);
653
	return qid;
654
}
655
 
656
char *
657
enfrog(char *src)
658
{
659
	char *d, *dst;
660
	uchar *s;
661
 
662
	d = dst = emalloc(strlen(src)*3 + 1);
663
	for (s = (uchar *)src; *s; s++)
664
		if(isfrog[*s] || *s == '\\')
665
			d += sprintf(d, "\\%02x", *s);
666
		else
667
			*d++ = *s;
668
	*d = 0;
669
	return dst;
670
}
671
 
672
char *
673
defrog(char *s)
674
{
675
	char *d, *dst, buf[3];
676
 
677
	d = dst = emalloc(strlen(s) + 1);
678
	for(; *s; s++)
679
		if(*s == '\\' && strlen(s) >= 3){
680
			buf[0] = *++s;			/* skip \ */
681
			buf[1] = *++s;
682
			buf[2] = 0;
683
			*d++ = strtoul(buf, NULL, 16);
684
		} else
685
			*d++ = *s;
686
	*d = 0;
687
	return dst;
688
}
689
 
690
void
691
stat2dir(char *path, struct stat *st, Dir *d)
692
{
693
	User *u;
694
	char *q, *p, *npath;
695
 
696
	memset(d, 0, sizeof(*d));
697
	d->qid = stat2qid(st);
698
	d->mode = plan9mode(st);
699
	d->atime = st->st_atime;
700
	d->mtime = st->st_mtime;
701
	d->length = st->st_size;
702
 
703
	d->uid = (u = uid2user(st->st_uid)) ? u->name : "???";
704
	d->gid = (u = gid2user(st->st_gid)) ? u->name : "???";
705
	d->muid = "";
706
 
707
	if((q = strrchr(path, '/')) != nil)
708
		d->name = enfrog(q+1);
709
	else
710
		d->name = enfrog(path);
711
}
712
 
713
void
714
rread(Fcall *rx, Fcall *tx)
715
{
716
	char *e, *path;
717
	uchar *p, *ep;
718
	int n;
719
	Fid *fid;
720
	Dir d;
721
	struct stat st;
722
 
723
	if(rx->count > msize-IOHDRSZ){
724
		seterror(tx, Etoolarge);
725
		return;
726
	}
727
 
728
	if((fid = oldfidex(rx->fid, -1, &e)) == nil){
729
		seterror(tx, e);
730
		return;
731
	}
732
 
733
	if (fid->auth) {
734
		char *e;
735
		e = auth->read(rx, tx);
736
		if (e)
737
			seterror(tx, e);
738
		return;
739
	}
740
 
741
	if(fid->omode == -1 || (fid->omode&3) == OWRITE){
742
		seterror(tx, Ebadusefid);
743
		return;
744
	}
745
 
746
	if(fid->dir){
747
		if(rx->offset != fid->diroffset){
748
			if(rx->offset != 0){
749
				seterror(tx, Ebadoffset);
750
				return;
751
			}
752
			rewinddir(fid->dir);
753
			fid->diroffset = 0;
754
			fid->direof = 0;
755
		}
756
		if(fid->direof){
757
			tx->count = 0;
758
			return;
759
		}
760
 
761
		p = (uchar*)tx->data;
762
		ep = (uchar*)tx->data+rx->count;
763
		for(;;){
764
			if(p+BIT16SZ >= ep)
765
				break;
766
			if(fid->dirent == nil)	/* one entry cache for when convD2M fails */
767
				if((fid->dirent = readdir(fid->dir)) == nil){
768
					fid->direof = 1;
769
					break;
770
				}
771
			if(strcmp(fid->dirent->d_name, ".") == 0
772
			|| strcmp(fid->dirent->d_name, "..") == 0){
773
				fid->dirent = nil;
774
				continue;
775
			}
776
			path = estrpath(fid->path, fid->dirent->d_name, 0);
777
			memset(&st, 0, sizeof st);
778
			if(stat(path, &st) < 0){
779
				fprint(2, "dirread: stat(%s) failed: %s\n", path, strerror(errno));
780
				fid->dirent = nil;
781
				free(path);
782
				continue;
783
			}
784
			free(path);
785
			stat2dir(fid->dirent->d_name, &st, &d);
786
			if((n=(old9p ? convD2Mold : convD2M)(&d, p, ep-p)) <= BIT16SZ)
787
				break;
788
			p += n;
789
			fid->dirent = nil;
790
		}
791
		tx->count = p - (uchar*)tx->data;
792
		fid->diroffset += tx->count;
793
	}else{
794
		if((n = pread(fid->fd, tx->data, rx->count, rx->offset)) < 0){
795
			seterror(tx, strerror(errno));
796
			return;
797
		}
798
		tx->count = n;
799
	}
800
}
801
 
802
void
803
rwrite(Fcall *rx, Fcall *tx)
804
{
805
	char *e;
806
	Fid *fid;
807
	int n;
808
 
809
	if(rx->count > msize-IOHDRSZ){
810
		seterror(tx, Etoolarge);
811
		return;
812
	}
813
 
814
	if((fid = oldfidex(rx->fid, -1, &e)) == nil){
815
		seterror(tx, e);
816
		return;
817
	}
818
 
819
	if (fid->auth) {
820
		char *e;
821
		e = auth->write(rx, tx);
822
		if (e)
823
			seterror(tx, e);
824
		return;
825
	}
826
 
827
	if(fid->omode == -1 || (fid->omode&3) == OREAD || (fid->omode&3) == OEXEC){
828
		seterror(tx, Ebadusefid);
829
		return;
830
	}
831
 
832
	if((n = pwrite(fid->fd, rx->data, rx->count, rx->offset)) < 0){
833
		seterror(tx, strerror(errno));
834
		return;
835
	}
836
	tx->count = n;
837
}
838
 
839
void
840
rclunk(Fcall *rx, Fcall *tx)
841
{
842
	char *e;
843
	Fid *fid;
844
 
845
	if((fid = oldfidex(rx->fid, -1, &e)) == nil){
846
		seterror(tx, e);
847
		return;
848
	}
849
	if (fid->auth) {
850
		if (auth->clunk) {
851
			e = (*auth->clunk)(rx, tx);
852
			if (e) {
853
				seterror(tx, e);
854
				return;
855
			}
856
		}
857
	}
858
	else if(fid->omode != -1 && fid->omode&ORCLOSE)
859
		remove(fid->path);
860
	freefid(fid);
861
}
862
 
863
void
864
rremove(Fcall *rx, Fcall *tx)
865
{
866
	char *e;
867
	Fid *fid;
868
 
869
	if((fid = oldfid(rx->fid, &e)) == nil){
870
		seterror(tx, e);
871
		return;
872
	}
873
	if(userremove(fid, &e) < 0)
874
		seterror(tx, e);
875
	freefid(fid);
876
}
877
 
878
void
879
rstat(Fcall *rx, Fcall *tx)
880
{
881
	char *e;
882
	Fid *fid;
883
	Dir d;
884
 
885
	if((fid = oldfid(rx->fid, &e)) == nil){
886
		seterror(tx, e);
887
		return;
888
	}
889
 
890
	if(fidstat(fid, &e) < 0){
891
		seterror(tx, e);
892
		return;
893
	}
894
 
895
	stat2dir(fid->path, &fid->st, &d);
896
	if((tx->nstat=(old9p ? convD2Mold : convD2M)(&d, tx->stat, msize)) <= BIT16SZ)
897
		seterror(tx, "convD2M fails");
898
}
899
 
900
void
901
rwstat(Fcall *rx, Fcall *tx)
902
{
903
	char *e;
904
	char *p, *old, *new, *dir;
905
	gid_t gid;
906
	Dir d;
907
	Fid *fid;
908
 
909
	if((fid = oldfid(rx->fid, &e)) == nil){
910
		seterror(tx, e);
911
		return;
912
	}
913
 
914
	/*
915
	 * wstat is supposed to be atomic.
916
	 * we check all the things we can before trying anything.
917
	 * still, if we are told to truncate a file and rename it and only
918
	 * one works, we're screwed.  in such cases we leave things
919
	 * half broken and return an error.  it's hardly perfect.
920
	 */
921
	if((old9p ? convM2Dold : convM2D)(rx->stat, rx->nstat, &d, (char*)rx->stat) <= BIT16SZ){
922
		seterror(tx, Ewstatbuffer);
923
		return;
924
	}
925
 
926
	if(fidstat(fid, &e) < 0){
927
		seterror(tx, e);
928
		return;
929
	}
930
 
931
	/*
932
	 * The casting is necessary because d.mode is ulong and might,
933
	 * on some systems, be 64 bits.  We only want to compare the
934
	 * bottom 32 bits, since that's all that gets sent in the protocol.
935
	 * 
936
	 * Same situation for d.mtime and d.length (although that last check
937
	 * is admittedly superfluous, given the current lack of 128-bit machines).
938
	 */
939
	gid = (gid_t)-1;
940
	if(d.gid[0] != '\0'){
941
		User *g;
942
 
943
		g = gname2user(d.gid);
944
		if(g == nil){
945
			seterror(tx, Eunknowngroup);
946
			return;
947
		}
948
		gid = (gid_t)g->id;
949
 
950
		if(groupchange(fid->u, gid2user(gid), &e) < 0){
951
			seterror(tx, e);
952
			return;
953
		}		
954
	}
955
 
956
	if((u32int)d.mode != (u32int)~0 && (((d.mode&DMDIR)!=0) ^ (S_ISDIR(fid->st.st_mode)!=0))){
957
		seterror(tx, Edirchange);
958
		return;
959
	}
960
 
961
	if(strcmp(fid->path, "/") == 0){
962
		seterror(tx, "no wstat of root");
963
		return;
964
	}
965
 
966
	/*
967
	 * try things in increasing order of harm to the file.
968
	 * mtime should come after truncate so that if you
969
	 * do both the mtime actually takes effect, but i'd rather
970
	 * leave truncate until last.
971
	 * (see above comment about atomicity).
972
	 */
973
	if((u32int)d.mode != (u32int)~0 && chmod(fid->path, unixmode(&d)) < 0){
974
		if(chatty9p)
975
			fprint(2, "chmod(%s, 0%luo) failed\n", fid->path, unixmode(&d));
976
		seterror(tx, strerror(errno));
977
		return;
978
	}
979
 
980
	if((u32int)d.mtime != (u32int)~0){
981
		struct utimbuf t;
982
 
983
		t.actime = 0;
984
		t.modtime = d.mtime;
985
		if(utime(fid->path, &t) < 0){
986
			if(chatty9p)
987
				fprint(2, "utime(%s) failed\n", fid->path);
988
			seterror(tx, strerror(errno));
989
			return;
990
		}
991
	}
992
 
993
	if(gid != (gid_t)-1 && gid != fid->st.st_gid){
994
		if(chown(fid->path, (uid_t)-1, gid) < 0){
995
			if(chatty9p)
996
				fprint(2, "chgrp(%s, %d) failed\n", fid->path, gid);
997
			seterror(tx, strerror(errno));
998
			return;
999
		}
1000
	}
1001
 
1002
	if(d.name[0]){
1003
		old = fid->path;
1004
		dir = estrdup(fid->path);
1005
		if((p = strrchr(dir, '/')) > dir)
1006
			*p = '\0';
1007
		else{
1008
			seterror(tx, "whoops: can't happen in u9fs");
1009
			return;
1010
		}
1011
		new = estrpath(dir, d.name, 1);
1012
		if(strcmp(old, new) != 0 && rename(old, new) < 0){
1013
			if(chatty9p)
1014
				fprint(2, "rename(%s, %s) failed\n", old, new);
1015
			seterror(tx, strerror(errno));
1016
			free(new);
1017
			free(dir);
1018
			return;
1019
		}
1020
		fid->path = new;
1021
		free(old);
1022
		free(dir);
1023
	}
1024
 
1025
	if((u64int)d.length != (u64int)~0 && truncate(fid->path, d.length) < 0){
1026
		fprint(2, "truncate(%s, %lld) failed\n", fid->path, d.length);
1027
		seterror(tx, strerror(errno));
1028
		return;
1029
	}
1030
}
1031
 
1032
/*
1033
 * we keep a table by numeric id.  by name lookups happen infrequently
1034
 * while by-number lookups happen once for every directory entry read
1035
 * and every stat request.
1036
 */
1037
User *utab[64];
1038
User *gtab[64];
1039
 
1040
User*
1041
adduser(struct passwd *p)
1042
{
1043
	User *u;
1044
 
1045
	u = emalloc(sizeof(*u));
1046
	u->id = p->pw_uid;
1047
	u->name = estrdup(p->pw_name);
1048
	u->next = utab[p->pw_uid%nelem(utab)];
1049
	u->defaultgid = p->pw_gid;
1050
	utab[p->pw_uid%nelem(utab)] = u;
1051
	return u;
1052
}
1053
 
1054
int
1055
useringroup(User *u, User *g)
1056
{
1057
	int i;
1058
 
1059
	for(i=0; i<g->nmem; i++)
1060
		if(strcmp(g->mem[i], u->name) == 0)
1061
			return 1;
1062
 
1063
	/*
1064
	 * Hack around common Unix problem that everyone has
1065
	 * default group "user" but /etc/group lists no members.
1066
	 */
1067
	if(u->defaultgid == g->id)
1068
		return 1;
1069
	return 0;
1070
}
1071
 
1072
User*
1073
addgroup(struct group *g)
1074
{
1075
	User *u;
1076
	char **p;
1077
	int n;
1078
 
1079
	u = emalloc(sizeof(*u));
1080
	n = 0;
1081
	for(p=g->gr_mem; *p; p++)
1082
		n++;
1083
	u->mem = emalloc(sizeof(u->mem[0])*n);
1084
	n = 0;
1085
	for(p=g->gr_mem; *p; p++)
1086
		u->mem[n++] = estrdup(*p);
1087
	u->nmem = n;
1088
	u->id = g->gr_gid;
1089
	u->name = estrdup(g->gr_name);
1090
	u->next = gtab[g->gr_gid%nelem(gtab)];
1091
	gtab[g->gr_gid%nelem(gtab)] = u;
1092
	return u;
1093
}
1094
 
1095
User*
1096
uname2user(char *name)
1097
{
1098
	int i;
1099
	User *u;
1100
	struct passwd *p;
1101
 
1102
	for(i=0; i<nelem(utab); i++)
1103
		for(u=utab[i]; u; u=u->next)
1104
			if(strcmp(u->name, name) == 0)
1105
				return u;
1106
 
1107
	if((p = getpwnam(name)) == nil)
1108
		return nil;
1109
	return adduser(p);
1110
}
1111
 
1112
User*
1113
uid2user(int id)
1114
{
1115
	User *u;
1116
	struct passwd *p;
1117
 
1118
	for(u=utab[id%nelem(utab)]; u; u=u->next)
1119
		if(u->id == id)
1120
			return u;
1121
 
1122
	if((p = getpwuid(id)) == nil)
1123
		return nil;
1124
	return adduser(p);
1125
}
1126
 
1127
User*
1128
gname2user(char *name)
1129
{
1130
	int i;
1131
	User *u;
1132
	struct group *g;
1133
 
1134
	for(i=0; i<nelem(gtab); i++)
1135
		for(u=gtab[i]; u; u=u->next)
1136
			if(strcmp(u->name, name) == 0)
1137
				return u;
1138
 
1139
	if((g = getgrnam(name)) == nil)
1140
		return nil;
1141
	return addgroup(g);
1142
}
1143
 
1144
User*
1145
gid2user(int id)
1146
{
1147
	User *u;
1148
	struct group *g;
1149
 
1150
	for(u=gtab[id%nelem(gtab)]; u; u=u->next)
1151
		if(u->id == id)
1152
			return u;
1153
 
1154
	if((g = getgrgid(id)) == nil)
1155
		return nil;
1156
	return addgroup(g);
1157
}
1158
 
1159
void
1160
sysfatal(char *fmt, ...)
1161
{
1162
	char buf[1024];
1163
	va_list va, temp;
1164
 
1165
	va_start(va, fmt);
1166
	va_copy(temp, va);
1167
	doprint(buf, buf+sizeof buf, fmt, &temp);
1168
	va_end(temp);
1169
	va_end(va);
1170
	fprint(2, "u9fs: %s\n", buf);
1171
	fprint(2, "last unix error: %s\n", strerror(errno));
1172
	exit(1);
1173
}
1174
 
1175
void*
1176
emalloc(size_t n)
1177
{
1178
	void *p;
1179
 
1180
	if(n == 0)
1181
		n = 1;
1182
	p = malloc(n);
1183
	if(p == 0)
1184
		sysfatal("malloc(%ld) fails", (long)n);
1185
	memset(p, 0, n);
1186
	return p;
1187
}
1188
 
1189
void*
1190
erealloc(void *p, size_t n)
1191
{
1192
	if(p == 0)
1193
		p = malloc(n);
1194
	else
1195
		p = realloc(p, n);
1196
	if(p == 0)
1197
		sysfatal("realloc(..., %ld) fails", (long)n);
1198
	return p;
1199
}
1200
 
1201
char*
1202
estrdup(char *p)
1203
{
1204
	p = strdup(p);
1205
	if(p == 0)
1206
		sysfatal("strdup(%.20s) fails", p);
1207
	return p;
1208
}
1209
 
1210
char*
1211
estrpath(char *p, char *q, int frog)
1212
{
1213
	char *r, *s;
1214
 
1215
	if(strcmp(q, "..") == 0){
1216
		r = estrdup(p);
1217
		if((s = strrchr(r, '/')) && s > r)
1218
			*s = '\0';
1219
		else if(s == r)
1220
			s[1] = '\0';
1221
		return r;
1222
	}
1223
 
1224
	if(frog)
1225
		q = defrog(q);
1226
	else
1227
		q = strdup(q);
1228
	r = emalloc(strlen(p)+1+strlen(q)+1);
1229
	strcpy(r, p);
1230
	if(r[0]=='\0' || r[strlen(r)-1] != '/')
1231
		strcat(r, "/");
1232
	strcat(r, q);
1233
	free(q);
1234
	return r;
1235
}
1236
 
1237
Fid *fidtab[1];
1238
 
1239
Fid*
1240
lookupfid(int fid)
1241
{
1242
	Fid *f;
1243
 
1244
	for(f=fidtab[fid%nelem(fidtab)]; f; f=f->next)
1245
		if(f->fid == fid)
1246
			return f;
1247
	return nil;
1248
}
1249
 
1250
Fid*
1251
newfid(int fid, char **ep)
1252
{
1253
	Fid *f;
1254
 
1255
	if(lookupfid(fid) != nil){
1256
		*ep = Efidactive;
1257
		return nil;
1258
	}
1259
 
1260
	f = emalloc(sizeof(*f));
1261
	f->next = fidtab[fid%nelem(fidtab)];
1262
	if(f->next)
1263
		f->next->prev = f;
1264
	fidtab[fid%nelem(fidtab)] = f;
1265
	f->fid = fid;
1266
	f->fd = -1;
1267
	f->omode = -1;
1268
	return f;
1269
}
1270
 
1271
Fid*
1272
newauthfid(int fid, void *magic, char **ep)
1273
{
1274
	Fid *af;
1275
	af = newfid(fid, ep);
1276
	if (af == nil)
1277
		return nil;
1278
	af->auth = 1;
1279
	af->authmagic = magic;
1280
	return af;
1281
}
1282
 
1283
Fid*
1284
oldfidex(int fid, int auth, char **ep)
1285
{
1286
	Fid *f;
1287
 
1288
	if((f = lookupfid(fid)) == nil){
1289
		*ep = Ebadfid;
1290
		return nil;
1291
	}
1292
 
1293
	if (auth != -1 && f->auth != auth) {
1294
		*ep = Ebadfid;
1295
		return nil;
1296
	}
1297
 
1298
	if (!f->auth) {
1299
		if(userchange(f->u, ep) < 0)
1300
			return nil;
1301
	}
1302
 
1303
	return f;
1304
}
1305
 
1306
Fid*
1307
oldfid(int fid, char **ep)
1308
{
1309
	return oldfidex(fid, 0, ep);
1310
}
1311
 
1312
Fid*
1313
oldauthfid(int fid, void **magic, char **ep)
1314
{
1315
	Fid *af;
1316
	af = oldfidex(fid, 1, ep);
1317
	if (af == nil)
1318
		return nil;
1319
	*magic = af->authmagic;
1320
	return af;
1321
}
1322
 
1323
void
1324
freefid(Fid *f)
1325
{
1326
	if(f->prev)
1327
		f->prev->next = f->next;
1328
	else
1329
		fidtab[f->fid%nelem(fidtab)] = f->next;
1330
	if(f->next)
1331
		f->next->prev = f->prev;
1332
	if(f->dir)
1333
		closedir(f->dir);
1334
	if(f->fd)
1335
		close(f->fd);
1336
	free(f->path);
1337
	free(f);
1338
}
1339
 
1340
int
1341
fidstat(Fid *fid, char **ep)
1342
{
1343
	if(stat(fid->path, &fid->st) < 0){
1344
		fprint(2, "fidstat(%s) failed\n", fid->path);
1345
		if(ep)
1346
			*ep = strerror(errno);
1347
		return -1;
1348
	}
1349
	if(S_ISDIR(fid->st.st_mode))
1350
		fid->st.st_size = 0;
1351
	return 0;
1352
}
1353
 
1354
int
1355
userchange(User *u, char **ep)
1356
{
1357
	if(defaultuser)
1358
		return 0;
1359
 
1360
	if(setreuid(0, 0) < 0){
1361
		fprint(2, "setreuid(0, 0) failed\n");
1362
		*ep = "cannot setuid back to root";
1363
		return -1;
1364
	}
1365
 
1366
	/*
1367
	 * Initgroups does not appear to be SUSV standard.
1368
	 * But it exists on SGI and on Linux, which makes me
1369
	 * think it's standard enough.  We have to do something
1370
	 * like this, and the closest other function I can find is
1371
	 * setgroups (which initgroups eventually calls).
1372
	 * Setgroups is the same as far as standardization though,
1373
	 * so we're stuck using a non-SUSV call.  Sigh.
1374
	 */
1375
	if(initgroups(u->name, u->defaultgid) < 0)
1376
		fprint(2, "initgroups(%s) failed: %s\n", u->name, strerror(errno));
1377
 
1378
	if(setreuid(-1, u->id) < 0){
1379
		fprint(2, "setreuid(-1, %s) failed\n", u->name);
1380
		*ep = strerror(errno);
1381
		return -1;
1382
	}
1383
 
1384
	return 0;
1385
}
1386
 
1387
/*
1388
 * We do our own checking here, then switch to root temporarily
1389
 * to set our gid.  In a perfect world, you'd be allowed to set your
1390
 * egid to any of the supplemental groups of your euid, but this
1391
 * is not the case on Linux 2.2.14 (and perhaps others).
1392
 *
1393
 * This is a race, of course, but it's a race against processes
1394
 * that can edit the group lists.  If you can do that, you can
1395
 * change your own group without our help.
1396
 */
1397
int
1398
groupchange(User *u, User *g, char **ep)
1399
{
1400
	if(g == nil)
1401
		return -1;
1402
	if(!useringroup(u, g)){
1403
		if(chatty9p)
1404
			fprint(2, "%s not in group %s\n", u->name, g->name);
1405
		*ep = Enotingroup;
1406
		return -1;
1407
	}
1408
 
1409
	setreuid(0,0);
1410
	if(setregid(-1, g->id) < 0){
1411
		fprint(2, "setegid(%s/%d) failed in groupchange\n", g->name, g->id);
1412
		*ep = strerror(errno);
1413
		return -1;
1414
	}
1415
	if(userchange(u, ep) < 0)
1416
		return -1;
1417
 
1418
	return 0;
1419
}
1420
 
1421
 
1422
/*
1423
 * An attempt to enforce permissions by looking at the 
1424
 * file system.  Separation of checking permission and
1425
 * actually performing the action is a terrible idea, of 
1426
 * course, so we use setreuid for most of the permission
1427
 * enforcement.  This is here only so we can give errors
1428
 * on open(ORCLOSE) in some cases.
1429
 */
1430
int
1431
userperm(User *u, char *path, int type, int need)
1432
{
1433
	char *p, *q;
1434
	int i, have;
1435
	struct stat st;
1436
	User *g;
1437
 
1438
	switch(type){
1439
	default:
1440
		fprint(2, "bad type %d in userperm\n", type);
1441
		return -1;
1442
	case Tdot:
1443
		if(stat(path, &st) < 0){
1444
			fprint(2, "userperm: stat(%s) failed\n", path);
1445
			return -1;
1446
		}
1447
		break;
1448
	case Tdotdot:
1449
		p = estrdup(path);
1450
		if((q = strrchr(p, '/'))==nil){
1451
			fprint(2, "userperm(%s, ..): bad path\n", p);
1452
			free(p);
1453
			return -1;
1454
		}
1455
		if(q > p)
1456
			*q = '\0';
1457
		else
1458
			*(q+1) = '\0';
1459
		if(stat(p, &st) < 0){
1460
			fprint(2, "userperm: stat(%s) (dotdot of %s) failed\n",
1461
				p, path);
1462
			free(p);
1463
			return -1;
1464
		}
1465
		free(p);
1466
		break;
1467
	}
1468
 
1469
	if(u == none){
1470
		fprint(2, "userperm: none wants %d in 0%luo\n", need, st.st_mode);
1471
		have = st.st_mode&7;
1472
		if((have&need)==need)
1473
			return 0;
1474
		return -1;
1475
	}
1476
	have = st.st_mode&7;
1477
	if((uid_t)u->id == st.st_uid)
1478
		have |= (st.st_mode>>6)&7;
1479
	if((have&need)==need)
1480
		return 0;
1481
	if(((have|((st.st_mode>>3)&7))&need) != need)	/* group won't help */
1482
		return -1;
1483
	g = gid2user(st.st_gid);
1484
	for(i=0; i<g->nmem; i++){
1485
		if(strcmp(g->mem[i], u->name) == 0){
1486
			have |= (st.st_mode>>3)&7;
1487
			break;
1488
		}
1489
	}
1490
	if((have&need)==need)
1491
		return 0;
1492
	return -1;
1493
}
1494
 
1495
int
1496
userwalk(User *u, char **path, char *elem, Qid *qid, char **ep)
1497
{
1498
	char *npath;
1499
	struct stat st;
1500
 
1501
	npath = estrpath(*path, elem, 1);
1502
	if(stat(npath, &st) < 0){
1503
		free(npath);
1504
		*ep = strerror(errno);
1505
		return -1;
1506
	}
1507
	*qid = stat2qid(&st);
1508
	free(*path);
1509
	*path = npath;
1510
	return 0;
1511
}
1512
 
1513
int
1514
useropen(Fid *fid, int omode, char **ep)
1515
{
1516
	int a, o;
1517
 
1518
	/*
1519
	 * Check this anyway, to try to head off problems later.
1520
	 */
1521
	if((omode&ORCLOSE) && userperm(fid->u, fid->path, Tdotdot, W_OK) < 0){
1522
		*ep = Eperm;
1523
		return -1;
1524
	}
1525
 
1526
	switch(omode&3){
1527
	default:
1528
		*ep = "programmer error";
1529
		return -1;
1530
	case OREAD:
1531
		a = R_OK;
1532
		o = O_RDONLY;
1533
		break;
1534
	case ORDWR:
1535
		a = R_OK|W_OK;
1536
		o = O_RDWR;
1537
		break;
1538
	case OWRITE:
1539
		a = W_OK;
1540
		o = O_WRONLY;
1541
		break;
1542
	case OEXEC:
1543
		a = X_OK;
1544
		o = O_RDONLY;
1545
		break;
1546
	}
1547
	if(omode & OTRUNC){
1548
		a |= W_OK;
1549
		o |= O_TRUNC;
1550
	}
1551
 
1552
	if(S_ISDIR(fid->st.st_mode)){
1553
		if(a != R_OK){
1554
			fprint(2, "attempt by %s to open dir %d\n", fid->u->name, omode);
1555
			*ep = Eperm;
1556
			return -1;
1557
		}
1558
		if((fid->dir = opendir(fid->path)) == nil){
1559
			*ep = strerror(errno);
1560
			return -1;
1561
		}
1562
	}else{
1563
		/*
1564
		 * This is wrong because access used the real uid
1565
		 * and not the effective uid.  Let the open sort it out.
1566
		 *
1567
		if(access(fid->path, a) < 0){
1568
			*ep = strerror(errno);
1569
			return -1;
1570
		}
1571
		 *
1572
		 */
1573
		if((fid->fd = open(fid->path, o)) < 0){
1574
			*ep = strerror(errno);
1575
			return -1;
1576
		}
1577
	}
1578
	fid->omode = omode;
1579
	return 0;
1580
}
1581
 
1582
int
1583
usercreate(Fid *fid, char *elem, int omode, long perm, char **ep)
1584
{
1585
	int o, m;
1586
	char *opath, *npath;
1587
	struct stat st, parent;
1588
 
1589
	if(stat(fid->path, &parent) < 0){
1590
		*ep = strerror(errno);
1591
		return -1;
1592
	}
1593
 
1594
	/*
1595
	 * Change group so that created file has expected group
1596
	 * by Plan 9 semantics.  If that fails, might as well go
1597
	 * with the user's default group.
1598
	 */
1599
	if(groupchange(fid->u, gid2user(parent.st_gid), ep) < 0
1600
	&& groupchange(fid->u, gid2user(fid->u->defaultgid), ep) < 0)
1601
		return -1;
1602
 
1603
	m = (perm & DMDIR) ? 0777 : 0666;
1604
	perm = perm & (~m | (fid->st.st_mode & m));
1605
 
1606
	npath = estrpath(fid->path, elem, 1);
1607
	if(perm & DMDIR){
1608
		if((omode&~ORCLOSE) != OREAD){
1609
			*ep = Eperm;
1610
			free(npath);
1611
			return -1;
1612
		}
1613
		if(stat(npath, &st) >= 0 || errno != ENOENT){
1614
			*ep = Eexist;
1615
			free(npath);
1616
			return -1;
1617
		}
1618
		/* race */
1619
		if(mkdir(npath, (perm|0400)&0777) < 0){
1620
			*ep = strerror(errno);
1621
			free(npath);
1622
			return -1;
1623
		}
1624
		if((fid->dir = opendir(npath)) == nil){
1625
			*ep = strerror(errno);
1626
			remove(npath);		/* race */
1627
			free(npath);
1628
			return -1;
1629
		}
1630
	}else{
1631
		o = O_CREAT|O_EXCL;
1632
		switch(omode&3){
1633
		default:
1634
			*ep = "programmer error";
1635
			return -1;
1636
		case OREAD:
1637
		case OEXEC:
1638
			o |= O_RDONLY;
1639
			break;
1640
		case ORDWR:
1641
			o |= O_RDWR;
1642
			break;
1643
		case OWRITE:
1644
			o |= O_WRONLY;
1645
			break;
1646
		}
1647
		if(omode & OTRUNC)
1648
			o |= O_TRUNC;
1649
		if((fid->fd = open(npath, o, perm&0777)) < 0){
1650
			if(chatty9p)
1651
				fprint(2, "create(%s, 0x%x, 0%o) failed\n", npath, o, perm&0777);
1652
			*ep = strerror(errno);
1653
			free(npath);
1654
			return -1;
1655
		}
1656
	}
1657
 
1658
	opath = fid->path;
1659
	fid->path = npath;
1660
	if(fidstat(fid, ep) < 0){
1661
		fprint(2, "stat after create on %s failed\n", npath);
1662
		remove(npath);	/* race */
1663
		free(npath);
1664
		fid->path = opath;
1665
		if(fid->fd >= 0){
1666
			close(fid->fd);
1667
			fid->fd = -1;
1668
		}else{
1669
			closedir(fid->dir);
1670
			fid->dir = nil;
1671
		}
1672
		return -1;
1673
	}
1674
	fid->omode = omode;
1675
	free(opath);
1676
	return 0;
1677
}
1678
 
1679
int
1680
userremove(Fid *fid, char **ep)
1681
{
1682
	if(remove(fid->path) < 0){
1683
		*ep = strerror(errno);
1684
		return -1;
1685
	}
1686
	return 0;
1687
}
1688
 
1689
void
1690
usage(void)
1691
{
1692
	fprint(2, "usage: u9fs [-Dnz] [-a authmethod] [-m msize] [-u user] [root]\n");
1693
	exit(1);
1694
}
1695
 
1696
int
1697
main(int argc, char **argv)
1698
{
1699
	char *authtype;
1700
	int i;
1701
	int fd;
1702
	int logflag;
1703
 
1704
	auth = authmethods[0];
1705
	logflag = O_WRONLY|O_APPEND|O_CREAT;
1706
	ARGBEGIN{
1707
	case 'D':
1708
		chatty9p = 1;
1709
		break;
1710
	case 'a':
1711
		authtype = EARGF(usage());
1712
		auth = nil;
1713
		for(i=0; i<nelem(authmethods); i++)
1714
			if(strcmp(authmethods[i]->name, authtype)==0)
1715
				auth = authmethods[i];
1716
		if(auth == nil)
1717
			sysfatal("unknown auth type '%s'", authtype);
1718
		break;
1719
	case 'A':
1720
		autharg = EARGF(usage());
1721
		break;
1722
	case 'l':
1723
		logfile = EARGF(usage());
1724
		break;
1725
	case 'm':
1726
		msize = strtol(EARGF(usage()), 0, 0);
1727
		break;
1728
	case 'n':
1729
		network = 0;
1730
		break;
1731
	case 'u':
1732
		defaultuser = EARGF(usage());
1733
		break;
1734
	case 'z':
1735
		logflag |= O_TRUNC;
1736
	}ARGEND
1737
 
1738
	if(argc > 1)
1739
		usage();
1740
 
1741
	fd = open(logfile, logflag, 0666);
1742
	if(fd < 0)
1743
		sysfatal("cannot open log '%s'", logfile);
1744
 
1745
	if(dup2(fd, 2) < 0)
1746
		sysfatal("cannot dup fd onto stderr");
1747
	fprint(2, "u9fs\nkill %d\n", (int)getpid());
1748
 
1749
	fmtinstall('F', fcallconv);
1750
	fmtinstall('D', dirconv);
1751
	fmtinstall('M', dirmodeconv);
1752
 
1753
	rxbuf = emalloc(msize);
1754
	txbuf = emalloc(msize);
1755
	databuf = emalloc(msize);
1756
 
1757
	if(auth->init)
1758
		auth->init();
1759
 
1760
	if(network)
1761
		getremotehostname(remotehostname, sizeof remotehostname);
1762
 
1763
	if(gethostname(hostname, sizeof hostname) < 0)
1764
		strcpy(hostname, "gnot");
1765
 
1766
	umask(0);
1767
 
1768
	if(argc == 1)
1769
		if(chroot(argv[0]) < 0)
1770
			sysfatal("chroot '%s' failed", argv[0]);
1771
 
1772
	none = uname2user("none");
1773
	if(none == nil)
1774
		none = uname2user("nobody");
1775
 
1776
	serve(0, 1);
1777
	return 0;
1778
}