Subversion Repositories planix.SVN

Rev

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