aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/upas/common/libsys.c
blob: ced00033acdfd398e40634f3eb15e1f18691fcac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
#include <u.h>
#include <sys/types.h>
#include <pwd.h>
#include <netdb.h>
#include "common.h"
#include <auth.h>
#include <ndb.h>

/*
 *  number of predefined fd's
 */
int nsysfile=3;

static char err[Errlen];

/*
 *  return the date
 */
extern char *
thedate(void)
{
	static char now[64];
	char *cp;

	strcpy(now, ctime(time(0)));
	cp = strchr(now, '\n');
	if(cp)
		*cp = 0;
	return now;
}

/*
 *  return the user id of the current user
 */
extern char *
getlog(void)
{
	return getuser();
}

/*
 *  return the lock name (we use one lock per directory)
 */
static String *
lockname(char *path)
{
	String *lp;
	char *cp;

	/*
	 *  get the name of the lock file
	 */
	lp = s_new();
	cp = strrchr(path, '/');
	if(cp)
		s_nappend(lp, path, cp - path + 1);
	s_append(lp, "L.mbox");

	return lp;
}

int
syscreatelocked(char *path, int mode, int perm)
{
	return create(path, mode, DMEXCL|perm);
}

int
sysopenlocked(char *path, int mode)
{
/*	return open(path, OEXCL|mode);/**/
	return open(path, mode);		/* until system call is fixed */
}

int
sysunlockfile(int fd)
{
	return close(fd);
}

/*
 *  try opening a lock file.  If it doesn't exist try creating it.
 */
static int
openlockfile(Mlock *l)
{
	int fd;
	Dir *d;
	Dir nd;
	char *p;

	fd = open(s_to_c(l->name), OREAD);
	if(fd >= 0){
		l->fd = fd;
		return 0;
	}

	d = dirstat(s_to_c(l->name));
	if(d == nil){
		/* file doesn't exist */
		/* try creating it */
		fd = create(s_to_c(l->name), OREAD, DMEXCL|0666);
		if(fd >= 0){
			nulldir(&nd);
			nd.mode = DMEXCL|0666;
			if(dirfwstat(fd, &nd) < 0){
				/* if we can't chmod, don't bother */
				/* live without the lock but log it */
				syslog(0, "mail", "lock error: %s: %r", s_to_c(l->name));
				remove(s_to_c(l->name));
			}
			l->fd = fd;
			return 0;
		}

		/* couldn't create */
		/* do we have write access to the directory? */
		p = strrchr(s_to_c(l->name), '/');
		if(p != 0){
			*p = 0;
			fd = access(s_to_c(l->name), 2);
			*p = '/';
			if(fd < 0){
				/* live without the lock but log it */
				syslog(0, "mail", "lock error: %s: %r", s_to_c(l->name));
				return 0;
			}
		} else {
			fd = access(".", 2);
			if(fd < 0){
				/* live without the lock but log it */
				syslog(0, "mail", "lock error: %s: %r", s_to_c(l->name));
				return 0;
			}
		}
	} else
		free(d);

	return 1; /* try again later */
}

#define LSECS 5*60

/*
 *  Set a lock for a particular file.  The lock is a file in the same directory
 *  and has L. prepended to the name of the last element of the file name.
 */
extern Mlock *
syslock(char *path)
{
	Mlock *l;
	int tries;

	l = mallocz(sizeof(Mlock), 1);
	if(l == 0)
		return nil;

	l->name = lockname(path);

	/*
	 *  wait LSECS seconds for it to unlock
	 */
	for(tries = 0; tries < LSECS*2; tries++){
		switch(openlockfile(l)){
		case 0:
			return l;
		case 1:
			sleep(500);
			break;
		default:
			goto noway;
		}
	}

noway:
	s_free(l->name);
	free(l);
	return nil;
}

/*
 *  like lock except don't wait
 */
extern Mlock *
trylock(char *path)
{
	Mlock *l;
	char buf[1];
	int fd;

	l = malloc(sizeof(Mlock));
	if(l == 0)
		return 0;

	l->name = lockname(path);
	if(openlockfile(l) != 0){
		s_free(l->name);
		free(l);
		return 0;
	}
	
	/* fork process to keep lock alive */
	switch(l->pid = rfork(RFPROC)){
	default:
		break;
	case 0:
		fd = l->fd;
		for(;;){
			sleep(1000*60);
			if(pread(fd, buf, 1, 0) < 0)
				break;
		}
		_exits(0);
	}
	return l;
}

extern void
syslockrefresh(Mlock *l)
{
	char buf[1];

	pread(l->fd, buf, 1, 0);
}

extern void
sysunlock(Mlock *l)
{
	if(l == 0)
		return;
	if(l->name){
		s_free(l->name);
	}
	if(l->fd >= 0)
		close(l->fd);
	if(l->pid > 0)
		postnote(PNPROC, l->pid, "time to die");
	free(l);
}

/*
 *  Open a file.  The modes are:
 *
 *	l	- locked
 *	a	- set append permissions
 *	r	- readable
 *	w	- writable
 *	A	- append only (doesn't exist in Bio)
 */
extern Biobuf *
sysopen(char *path, char *mode, ulong perm)
{
	int sysperm;
	int sysmode;
	int fd;
	int docreate;
	int append;
	int truncate;
	Dir *d, nd;
	Biobuf *bp;

	/*
	 *  decode the request
	 */
	sysperm = 0;
	sysmode = -1;
	docreate = 0;
	append = 0;
	truncate = 0;
 	for(; mode && *mode; mode++)
		switch(*mode){
		case 'A':
			sysmode = OWRITE;
			append = 1;
			break;
		case 'c':
			docreate = 1;
			break;
		case 'l':
			sysperm |= DMEXCL;
			break;
		case 'a':
			sysperm |= DMAPPEND;
			break;
		case 'w':
			if(sysmode == -1)
				sysmode = OWRITE;
			else
				sysmode = ORDWR;
			break;
		case 'r':
			if(sysmode == -1)
				sysmode = OREAD;
			else
				sysmode = ORDWR;
			break;
		case 't':
			truncate = 1;
			break;
		default:
			break;
		}
	switch(sysmode){
	case OREAD:
	case OWRITE:
	case ORDWR:
		break;
	default:
		if(sysperm&DMAPPEND)
			sysmode = OWRITE;
		else
			sysmode = OREAD;
		break;
	}

	/*
	 *  create file if we need to
	 */
	if(truncate)
		sysmode |= OTRUNC;
	fd = open(path, sysmode);
	if(fd < 0){
		d = dirstat(path);
		if(d == nil){
			if(docreate == 0)
				return 0;

			fd = create(path, sysmode, sysperm|perm);
			if(fd < 0)
				return 0;
			nulldir(&nd);
			nd.mode = sysperm|perm;
			dirfwstat(fd, &nd);
		} else {
			free(d);
			return 0;
		}
	}

	bp = (Biobuf*)malloc(sizeof(Biobuf));
	if(bp == 0){
		close(fd);
		return 0;
	}
	memset(bp, 0, sizeof(Biobuf));
	Binit(bp, fd, sysmode&~OTRUNC);

	if(append)
		Bseek(bp, 0, 2);
	return bp;
}

/*
 *  close the file, etc.
 */
int
sysclose(Biobuf *bp)
{
	int rv;

	rv = Bterm(bp);
	close(Bfildes(bp));
	free(bp);
	return rv;
}

/*
 *  create a file
 */
int
syscreate(char *file, int mode, ulong perm)
{
	return create(file, mode, perm);
}

/*
 *  make a directory
 */
int
sysmkdir(char *file, ulong perm)
{
	int fd;

	if((fd = create(file, OREAD, DMDIR|perm)) < 0)
		return -1;
	close(fd);
	return 0;
}

/*
 *  change the group of a file
 */
int
syschgrp(char *file, char *group)
{
	Dir nd;

	if(group == 0)
		return -1;
	nulldir(&nd);
	nd.gid = group;
	return dirwstat(file, &nd);
}

extern int
sysdirreadall(int fd, Dir **d)
{
	return dirreadall(fd, d);
}

/*
 *  read in the system name
 */
static char *unix_hostname_read(void);
extern char *
sysname_read(void)
{
	static char name[128];
	char *cp;

	cp = getenv("site");
	if(cp == 0 || *cp == 0)
		cp = alt_sysname_read();
	if(cp == 0 || *cp == 0)
		cp = "kremvax";
	strecpy(name, name+sizeof name, cp);
	return name;
}
extern char *
alt_sysname_read(void)
{
	char *cp;
	static char name[128];

	cp = getenv("sysname");
	if(cp == 0 || *cp == 0)
		cp = unix_hostname_read();
	if(cp == 0 || *cp == 0)
		return 0;
	strecpy(name, name+sizeof name, cp);
	return name;
}
static char *
unix_hostname_read(void)
{
	static char hostname[256];

	if(gethostname(hostname, sizeof hostname) < 0)
		return nil;
	return hostname;
}

/*
 *  get all names
 */
extern char**
sysnames_read(void)
{
	static char **namev;
	struct hostent *h;
	char **p, **a;

	if(namev)
		return namev;

	h = gethostbyname(alt_sysname_read());
	for(p=h->h_aliases; *p; p++)
		;
	
	namev = malloc((2+p-h->h_aliases)*sizeof namev[0]);
	if(namev == 0)
		return 0;

	a = namev;
	*a++ = strdup(h->h_name);
	for(p=h->h_aliases; *p; p++)
		*a++ = strdup(*p);
	*a = 0;

	return namev;
}

/*
 *  read in the domain name.
 *  chop off beginning pieces until we find one with an mx record.
 */
extern char *
domainname_read(void)
{
	char **namev, *p;
	Ndbtuple *t;

	for(namev = sysnames_read(); *namev; namev++){
		if(strchr(*namev, '.')){
			for(p=*namev-1; p && *++p; p=strchr(p, '.')){
				if((t = dnsquery(nil, p, "mx")) != nil){
					ndbfree(t);
					return p;
				}
			}
		}
	}
	return 0;
}

/*
 *  return true if the last error message meant file
 *  did not exist.
 */
extern int
e_nonexistent(void)
{
	rerrstr(err, sizeof(err));
	return strcmp(err, "file does not exist") == 0;
}

/*
 *  return true if the last error message meant file
 *  was locked.
 */
extern int
e_locked(void)
{
	rerrstr(err, sizeof(err));
	return strcmp(err, "open/create -- file is locked") == 0;
}

/*
 *  return the length of a file
 */
extern long
sysfilelen(Biobuf *fp)
{
	Dir *d;
	long rv;

	d = dirfstat(Bfildes(fp));
	if(d == nil)
		return -1;
	rv = d->length;
	free(d);
	return rv;
}

/*
 *  remove a file
 */
extern int
sysremove(char *path)
{
	return remove(path);
}

/*
 *  rename a file, fails unless both are in the same directory
 */
extern int
sysrename(char *old, char *new)
{
	Dir d;
	char *obase;
	char *nbase;

	obase = strrchr(old, '/');
	nbase = strrchr(new, '/');
	if(obase){
		if(nbase == 0)
			return -1;
		if(strncmp(old, new, obase-old) != 0)
			return -1;
		nbase++;
	} else {
		if(nbase)
			return -1;
		nbase = new;
	}
	nulldir(&d);
	d.name = nbase;
	return dirwstat(old, &d);
}

/*
 *  see if a file exists
 */
extern int
sysexist(char *file)
{
	Dir	*d;

	d = dirstat(file);
	if(d == nil)
		return 0;
	free(d);
	return 1;
}

/*
 *  return nonzero if file is a directory
 */
extern int
sysisdir(char *file)
{
	Dir	*d;
	int	rv;

	d = dirstat(file);
	if(d == nil)
		return 0;
	rv = d->mode & DMDIR;
	free(d);
	return rv;
}

/*
 *  kill a process
 */
extern int
syskill(int pid)
{
	return postnote(PNPROC, pid, "kill");
}

/*
 *  kill a process group
 */
extern int
syskillpg(int pid)
{
	return postnote(PNGROUP, pid, "kill");
}

extern int
sysdetach(void)
{
	if(rfork(RFENVG|RFNAMEG|RFNOTEG) < 0) {
		werrstr("rfork failed");
		return -1;
	}
	return 0;
}

/*
 *  catch a write on a closed pipe
 */
static int *closedflag;
static int
catchpipe(void *a, char *msg)
{
	static char *foo = "sys: write on closed pipe";

	USED(a);
	if(strncmp(msg, foo, strlen(foo)) == 0){
		if(closedflag)
			*closedflag = 1;
		return 1;
	}
	return 0;
}
void
pipesig(int *flagp)
{
	closedflag = flagp;
	atnotify(catchpipe, 1);
}
void
pipesigoff(void)
{
	atnotify(catchpipe, 0);
}

extern int
holdon(void)
{
	/* XXX talk to 9term? */
	return -1;
}

extern int
sysopentty(void)
{
	return open("/dev/tty", ORDWR);
}

extern void
holdoff(int fd)
{
	write(fd, "holdoff", 7);
	close(fd);
}

extern int
sysfiles(void)
{
	return 128;
}

/*
 *  expand a path relative to the user's mailbox directory
 *
 *  if the path starts with / or ./, don't change it
 *
 */
extern String *
mboxpath(char *path, char *user, String *to, int dot)
{
	char *dir;
	String *s;
	
	if (dot || *path=='/' || strncmp(path, "./", 2) == 0
			      || strncmp(path, "../", 3) == 0) {
		to = s_append(to, path);
	} else {
		if ((dir = homedir(user)) != nil) {
			s = s_copy(dir);
			s_append(s, "/mail/");
			if(access(s_to_c(s), AEXIST) >= 0){
				to = s_append(to, s_to_c(s));
				s_free(s);
				to = s_append(to, path);
				return to;
			}
			s_free(s);
		}
		to = s_append(to, MAILROOT);
		to = s_append(to, "/box/");
		to = s_append(to, user);
		to = s_append(to, "/");
		to = s_append(to, path);
	}
	return to;
}

extern String *
mboxname(char *user, String *to)
{
	return mboxpath("mbox", user, to, 0);
}

extern String *
deadletter(String *to)		/* pass in sender??? */
{
	char *cp;

	cp = getlog();
	if(cp == 0)
		return 0;
	return mboxpath("dead.letter", cp, to, 0);
}

String *
readlock(String *file)
{
	char *cp;

	cp = getlog();
	if(cp == 0)
		return 0;
	return mboxpath("reading", cp, file, 0);
}

String *
username(String *from)
{
	String* s;
	struct passwd* pw;

	setpwent();
	while((pw = getpwent()) != nil){
		if(strcmp(s_to_c(from), pw->pw_name) == 0){
			s = s_new();
			s_append(s, "\"");
			s_append(s, pw->pw_gecos);
			s_append(s, "\"");
			return s;
		}
	}
	return nil;
}

char *
homedir(char *user)
{
	static char buf[1024];
	struct passwd* pw;

	setpwent();
	while((pw = getpwent()) != nil)
		if(strcmp(user, pw->pw_name) == 0){
			strecpy(buf, buf+sizeof buf, pw->pw_dir);
			return buf;
		}
	return nil;
}

char *
remoteaddr(int fd, char *dir)
{
	char *raddr;
	NetConnInfo *nci;
	
	if((nci = getnetconninfo(dir, fd)) == nil)
		return nil;
	raddr = strdup(nci->raddr);
	freenetconninfo(nci);
	return raddr;
}

/*  create a file and  */
/*	1) ensure the modes we asked for */
/*	2) make gid == uid */
static int
docreate(char *file, int perm)
{
	int fd;
	Dir ndir;
	Dir *d;

	/*  create the mbox */
	fd = create(file, OREAD, perm);
	if(fd < 0){
		fprint(2, "couldn't create %s\n", file);
		return -1;
	}
	d = dirfstat(fd);
	if(d == nil){
		fprint(2, "couldn't stat %s\n", file);
		return -1;
	}
	nulldir(&ndir);
	ndir.mode = perm;
	ndir.gid = d->uid;
	if(dirfwstat(fd, &ndir) < 0)
		fprint(2, "couldn't chmod %s: %r\n", file);
	close(fd);
	return 0;
}

/*  create a mailbox */
int
creatembox(char *user, char *folder)
{
	char *p;
	String *mailfile;
	char buf[512];
	Mlock *ml;

	mailfile = s_new();
	if(folder == 0)
		mboxname(user, mailfile);
	else {
		snprint(buf, sizeof(buf), "%s/mbox", folder);
		mboxpath(buf, user, mailfile, 0);
	}

	/* don't destroy existing mailbox */
	if(access(s_to_c(mailfile), 0) == 0){
		fprint(2, "mailbox already exists\n");
		return -1;
	}
	fprint(2, "creating new mbox: %s\n", s_to_c(mailfile));

	/*  make sure preceding levels exist */
	for(p = s_to_c(mailfile); p; p++) {
		if(*p == '/')	/* skip leading or consecutive slashes */
			continue;
		p = strchr(p, '/');
		if(p == 0)
			break;
		*p = 0;
		if(access(s_to_c(mailfile), 0) != 0){
			if(docreate(s_to_c(mailfile), DMDIR|0711) < 0)
				return -1;
		}
		*p = '/';
	}

	/*  create the mbox */
	if(docreate(s_to_c(mailfile), 0622|DMAPPEND|DMEXCL) < 0)
		return -1;

	/*
	 *  create the lock file if it doesn't exist
	 */
	ml = trylock(s_to_c(mailfile));
	if(ml != nil)
		sysunlock(ml);

	return 0;
}