]> diplodocus.org Git - nmh/blob - docs/historical/mh-jun-1982/progs/deliver.c
Fixed inc(1) and %(me) to not obey Local-Mailbox profile component.
[nmh] / docs / historical / mh-jun-1982 / progs / deliver.c
1 #ifdef COMMENT
2 Proprietary Rand Corporation, 1981.
3 Further distribution of this software
4 subject to the terms of the Rand
5 license agreement.
6 #endif
7
8 #include <whoami.h>
9 #include "../mh.h"
10 #include "../adrparse.h"
11 #include <stdio.h>
12 #include <ctype.h>
13 #include <pwd.h>
14 #include <sys/types.h>
15 #include <sys/timeb.h>
16 #include <sys/stat.h>
17 #include <signal.h>
18 #include <strings.h>
19 #include <mailsys.h>
20 #include <time.h>
21
22
23 time_t time();
24 char *malloc(), *calloc(), *getncpy(), *sprintf(), *strcpy(), *strncop();
25 char *ctime();
26 long lseek();
27
28 #define OUTPUTLINELEN 72
29 #define EVERYONE 10
30 #define RECVPROG "/.mh_receive"
31 #define DAEMON_GRP 2
32 #define FCCS 10 /* Max number of fcc's allowed */
33
34 /* Alias */
35 #define STAR 2
36 int alitype; /*** 0 = nomatch, 2 = *match, 1 = stringmatch ***/
37
38 struct shome { /* Internal name/uid/home database */
39 struct shome *h_next;
40 char *h_name;
41 int h_uid;
42 int h_gid;
43 char *h_home;
44 } *homes, *home();
45
46 struct swit switches[] = {
47 "debug", -5, /* 0 */
48 "deliver", -1, /* 1 */
49 "format", 0, /* 2 */
50 "noformat", 0, /* 3 */
51 "msgid", 0, /* 4 */
52 "nomsgid", 0, /* 5 */
53 "remove", 0, /* 6 */
54 "noremove", 0, /* 7 */
55 "verbose", 0, /* 8 */
56 "noverbose", 0, /* 9 */
57 "width", 0, /*10 */
58 "help", 4, /*11 */
59 0, 0
60 };
61
62 int verbose, format=1, msgid, debug, myuid, rmflg;
63 short lockwait; /* Secs to wait for mail lock (From strings/lockdir.c) */
64 #define LOCKWAIT (lockwait * 5) /* Ignore lock if older than this */
65 short donecd;
66 short outputlinelen = OUTPUTLINELEN;
67 long now;
68 char tmpfil[32], uutmpfil[32], fccfold[FCCS][128];
69 short fccind;
70 char *deliverto;
71 char *head_alias = ""; /* 1st match in a local mailbox's alias chain */
72 /* (If a:b, b:c, c:d then head_alias == "a") */
73
74 struct mailname localaddrs, netaddrs, uuaddrs;
75 struct mailname *mn_from;
76 char *hdrptr;
77
78 /*ARGSUSED*/
79 main(argc, argv)
80 int argc;
81 char *argv[];
82 {
83 register char *cp, **argp;
84 register struct mailname *lp;
85 char buf[BUFSIZ], name[NAMESZ];
86 char *msg;
87 int state, compnum, fd;
88 FILE *in, *out, *uuout;
89
90 invo_name = argv[0];
91 #ifndef ARPANET
92 format=0; /* default to noformat if no Arpanet */
93 #endif
94 msg = 0;
95 argp = argv + 1;
96 while(cp = *argp++) {
97 if(*cp == '-')
98 switch(smatch(++cp, switches)) {
99 case -2:ambigsw(cp, switches); /* ambiguous */
100 done(1);
101 /* unknown */
102 case -1:fprintf(stderr, "deliver: -%s unknown switch.\n", cp);
103 done(1);
104 case 0: verbose++; debug++; continue; /* -debug */
105 case 1: if(!(deliverto = *argp++) || *deliverto == '-'){
106 missing: fprintf(stderr, "deliver: missing arg to %s\n",
107 argp[-2]);
108 done(1);
109 }
110 continue;
111 case 2: format = 1; continue; /* -format */
112 case 3: format = 0; continue; /* -noformat */
113 case 4: msgid = 1; continue; /* -msgid */
114 case 5: msgid = 0; continue; /* -nomsgid */
115 case 6: rmflg = 1; continue; /* -remove */
116 case 7: rmflg = -1; continue; /* -noremove */
117 case 8: verbose = 1; continue; /* -verbose */
118 case 9: verbose = 0; continue; /* -noverbose */
119 case 10: if(!(cp = *argp++) || *cp == '-')
120 goto missing;
121 outputlinelen = atoi(cp);
122 continue;
123 case 11: help("deliver [switches] file",
124 switches);
125 done(1);
126 }
127 if(msg) {
128 fprintf(stderr, "Deliver: Only one message at a time!\n");
129 done(1);
130 } else
131 msg = cp;
132 }
133 if(!msg) {
134 fprintf(stderr, "Deliver: No Message specified.\n");
135 fprintf(stderr, "Deliver: Usage: deliver [switches] file\n");
136 done(1);
137 }
138 if(outputlinelen < 10) {
139 fprintf(stderr, "deliver: Impossible width: %d\n",
140 outputlinelen);
141 done(1);
142 }
143 gethomes();
144 myuid = getuid();
145 if(deliverto) {
146 int uid;
147 if((uid = geteuid()) && uid != 1 && getegid() != DAEMON_GRP) {
148 /* if(myuid != 0 && getgid() != DAEMON_GRP) { */
149 fprintf(stderr, "Deliver: -deliver switch su only.\n");
150 done(1);
151 }
152 #ifdef VMUNIX
153 setpgrp(0, getpid()); /* So we don't blow away our parent */
154 #endif
155 strcpy(tmpfil, msg);
156 strcpy(uutmpfil, msg);
157 if(access(tmpfil, 4) == -1) {
158 fprintf(stderr, "Deliver: Can't access ");
159 perror(tmpfil);
160 done(1);
161 }
162 if(!rmflg) rmflg = -1;
163 goto del1;
164 }
165 if(!rmflg) rmflg = 1;
166 if((in = fopen(msg, "r")) == NULL) {
167 fprintf(stderr, "Deliver: Can't open ");
168 perror(msg);
169 done(1);
170 }
171 VOID copy(makename("locs", ".tmp"), copy("/usr/tmp/mh/", tmpfil));
172 if(!debug) {
173 if((out = fopen(tmpfil, "w")) == NULL) {
174 fprintf(stderr, "Can't create %s\n", tmpfil);
175 done(1);
176 }
177 VOID chmod(tmpfil, 0744);
178 } else
179 out = stdout;
180
181 VOID copy(makename("uulc", ".tmp"), copy("/usr/tmp/mh/", uutmpfil));
182 if(!debug) {
183 if((uuout = fopen(uutmpfil, "w")) == NULL) {
184 fprintf(stderr, "Can't create %s\n", uutmpfil);
185 done(1);
186 }
187 VOID chmod(uutmpfil, 0600);
188 } else
189 uuout = fopen("/dev/null","w");;
190
191 putdate(out); /* Tack on the date */
192 putdate(uuout); /* Tack on the date */
193 for(compnum = 1, state = FLD;;) {
194 state = m_getfld(state, name, buf, sizeof buf, in);
195 switch(state) {
196
197 case FLD:
198 case FLDEOF:
199 case FLDPLUS:
200 hdrptr = 0;
201 compnum++;
202 if(uleq(name, "fcc")) {
203 cp = buf;
204 while(*cp == ' ' || *cp == '\t')
205 cp++;
206 if(fccind >= FCCS) {
207 fprintf(stderr, "Deliver: too many fcc's.\n");
208 done(1);
209 }
210 VOID copy(cp, fccfold[fccind]);
211 if(cp = rindex(fccfold[fccind], '\n'))
212 *cp = 0;
213 fccind++;
214 break;
215 }
216 hdrptr = add(buf, hdrptr);
217 while(state == FLDPLUS) {
218 state = m_getfld(state, name, buf, sizeof buf, in);
219 hdrptr = add(buf, hdrptr);
220 }
221 putfmt(name, hdrptr, out);
222 { /* Do not format uucp mail */
223 int tmpformat;
224 tmpformat = format; format = 0;
225 putfmt (name, hdrptr, uuout);
226 format = tmpformat;
227 }
228 if(state == FLDEOF)
229 goto process;
230 break;
231
232 case BODY:
233 case BODYEOF:
234 putfrom(out); /* Tack on the from */
235 /* But NOT on the UUCP mail */
236 putmsgid(out); /* and msg id if desired */
237 putmsgid(uuout); /* (why not?) */
238
239 fprintf(out, "\n%s", buf);
240 fprintf(uuout, "\n%s", buf);
241 while(state == BODY) {
242 state=m_getfld(state,name,buf,sizeof buf,in);
243 fputs(buf, out);
244 fputs(buf, uuout);
245 }
246
247 case FILEEOF:
248 goto process;
249
250 case LENERR:
251 case FMTERR:
252 fprintf(stderr, "??Message Format Error ");
253 fprintf(stderr, "in Component #%d.\n", compnum);
254 done(1);
255
256 default:
257 fprintf(stderr, "Getfld returned %d\n", state);
258 done(1);
259 }
260 }
261 process:
262 if(!debug) {
263 VOID fclose(out);
264 VOID fclose(uuout);
265 }
266 else
267 printf("-----\n");
268 VOID fclose(in);
269 del1: if(deliverto) {
270 cinsert(deliverto);
271 }
272 if(debug) {
273 printf("Before alias():\n"); pl();
274 }
275
276 if(localaddrs.m_next)
277 alias(); /* Map names if needed */
278
279 for(lp = localaddrs.m_next; lp; lp = lp->m_next)
280 if(home(lp->m_mbox) == NULL) {
281 fprintf(stderr, "Deliver: Unknown local user: %s.\n", lp->m_mbox);
282 fprintf(stderr, "Deliver: Message not delivered.\n");
283 if(!debug)
284 unlinktmp();
285 done(1);
286 }
287 if(debug) {
288 printf("Addrs:\n"); pl();
289 }
290
291 VOID signal(SIGINT, SIG_IGN);
292 VOID signal(SIGQUIT, SIG_IGN);
293 if(!debug) { /* Send the mail */
294 if(localaddrs.m_next) {
295 fd = open(tmpfil, 0);
296 for(lp = localaddrs.m_next; lp; lp = lp->m_next)
297 sendmail(lp->m_mbox, fd, lp->m_headali);
298 VOID close(fd);
299 }
300 if(fccind)
301 for(state = 0; state < fccind; state++)
302 fcc(tmpfil, fccfold[state]);
303 if(uuaddrs.m_next)
304 uumail();
305 if(netaddrs.m_next)
306 netmail();
307 unlinktmp();
308 }
309 done(donecd);
310 }
311
312
313 unlinktmp()
314 {
315 if (!deliverto) /* "Deliverto" makes it same as tmpfil */
316 VOID unlink (uutmpfil); /* Even if rmflg < 0? */
317 if(rmflg > 0)
318 VOID unlink(tmpfil);
319 }
320
321
322
323 putfmt(name, str, out)
324 char *name, *str;
325 FILE *out;
326 {
327 register char *cp;
328 register struct mailname *mp;
329 int nameoutput = 0;
330 int linepos = 0;
331 int len;
332
333 while(*str == ' ' || *str == '\t') str++;
334 if(uleq(name, "to") ||
335 uleq(name, "cc") ||
336 uleq(name, "bcc") ||
337 uleq(name, "reply-to") ||
338 uleq(name, "from")) {
339 while(cp = getname(str)) {
340 if(!(mp = getm(cp, HOSTNAME)))
341 done(1);
342 if(uleq(name, "from") || uleq(name, "reply-to") ||
343 insert(mp,
344 mp->m_hnum == HOSTNUM ?
345 &localaddrs :
346 (*mp->m_at == '!') ? &uuaddrs :
347 &netaddrs)) {
348 if(!uleq(name, "bcc") && format) {
349 if(!nameoutput) {
350 fprintf(out, "%s: ", name);
351 linepos += (nameoutput = strlen(name)+2);
352 }
353 cp = adrformat(mp,HOSTNAME);
354 /* Needs default name in uumail case */
355 len = strlen(cp);
356 if(linepos != nameoutput)
357 if(len + linepos + 2 > outputlinelen) {
358 fprintf(out, ",\n%*s", nameoutput, "");
359 linepos = nameoutput;
360 } else {
361 fputs(", ", out);
362 linepos += 2;
363 }
364 fputs(cp, out);
365 linepos += len;
366 }
367 if(uleq(name, "from"))
368 mn_from = mp;
369 } else
370 mnfree(mp);
371 }
372 if(linepos) putc('\n', out);
373 if(!format && !uleq(name, "bcc"))
374 goto nofmt;
375 } else {
376 nofmt:
377 fprintf(out, "%s: %s", name, str);
378 }
379 }
380
381 gethomes()
382 {
383 register struct passwd *pw;
384 register struct shome *h, *ph;
385 struct passwd *getpwent();
386
387 ph = (struct shome *) &homes;
388 while((pw = getpwent()) != NULL) {
389 h = (struct shome *) malloc(sizeof *h);
390 h->h_next = NULL;
391 h->h_name = (char *) malloc((unsigned)strlen(pw->pw_name)+1);
392 strcpy(h->h_name, pw->pw_name);
393 h->h_uid = pw->pw_uid;
394 h->h_gid = pw->pw_gid;
395 h->h_home = (char *) malloc((unsigned)strlen(pw->pw_dir)+1);
396 strcpy(h->h_home, pw->pw_dir);
397 ph->h_next = h;
398 ph = h;
399 }
400 }
401
402
403 struct shome *
404 home(name)
405 register char *name;
406 {
407 register struct shome *h;
408
409 for(h = homes; h; h = h->h_next)
410 if(uleq(name, h->h_name))
411 return(h);
412 return(NULL);
413 }
414
415 char *bracket = "\1\1\1\1\n";
416
417 sendmail(name, fd, head_alias) /* Runs with signals ignored! */
418 char *name;
419 register int fd;
420 char *head_alias;
421 {
422 char buf[BUFSIZ];
423 register int i, m, c;
424 register char *mail, *receive, *lock = 0;
425 register struct shome *h;
426 struct stat stbuf;
427
428 if(verbose) {
429 printf("%s: ", name); fflush(stdout);
430 }
431 VOID lseek(fd, 0l, 0);
432 h = home(name);
433
434 mail = concat(mailboxes, "/", h->h_name, NULLCP);
435 /*** mail = concat(h->h_home, mailbox, NULLCP); ***/
436 receive = concat(h->h_home, RECVPROG, NULLCP);
437 if(access(receive, 1) == 0) { /* User has a receive prog */
438
439 calluserprog(receive, mail, fd, h, head_alias);
440
441 } else {
442
443 if((m = open(mail, 2)) < 0) {
444 if((m = creat(mail, 0600)) < 0) {
445 fprintf(stderr, "Deliver: Can't write ");
446 perror(mail);
447 donecd = 1; goto out;
448 }
449 VOID chown(mail, h->h_uid, h->h_gid);
450 if(verbose) {
451 printf("Creating %s ", mail); fflush(stdout);
452 }
453 }
454 lock = concat(lockdir, "/", h->h_name, NULLCP);
455 for(i = 0; i < lockwait; i += 2) {
456 if(link(mail, lock) >= 0)
457 break;
458 if(i == 0 && stat(mail, &stbuf) >= 0 &&
459 stbuf.st_ctime + LOCKWAIT < time((long *) 0)) {
460 i = lockwait;
461 break;
462 }
463 if(verbose) {
464 printf("Busy "); fflush(stdout);
465 }
466 sleep(2);
467 }
468 if(i >= lockwait) {
469 VOID unlink(lock);
470 if(verbose) {
471 printf("Removing lock. "); fflush(stdout);
472 }
473 if(link(mail, lock) < 0) {
474 fprintf(stderr, "Can't lock %s to %s\n",
475 mail, lock);
476 donecd = 1;
477 goto out1;
478 }
479 }
480 VOID lseek(m, 0l, 2);
481 if(write(m, bracket, 5) != 5)
482 goto wrerr;
483 do
484 if((c = read(fd, buf, sizeof buf)) > 0)
485 if(write(m, buf, c) != c) {
486 wrerr: fprintf(stderr, "Write error on ");
487 perror(mail);
488 donecd = 1;
489 goto out1;
490 }
491 while(c == sizeof buf);
492 if(write(m, bracket, 5) != 5)
493 goto wrerr;
494 out1: VOID close(m);
495 out: cndfree(mail);
496 if(lock) {
497 VOID unlink(lock);
498 cndfree(lock);
499 }
500 }
501 if(verbose && !donecd)
502 printf("Sent.\n");
503 }
504
505 netmail()
506 {
507 register struct mailname *mp;
508 register struct shome *h;
509 register struct hosts *hp;
510 register FILE *md;
511 int fd, count;
512 char namebuf[15], tmpname[40], queuename[40];
513 char buf[BUFSIZ];
514
515 crname(namebuf);
516 VOID sprintf(tmpname, "%s/%s", TMAILQDIR, namebuf);
517 VOID sprintf(queuename, "%s/%s", MAILQDIR, namebuf);
518 if((md = fopen(tmpname, "w")) == NULL) {
519 fprintf(stderr, "Deliver: Can't create netmail tmp: ");
520 perror(tmpname);
521 done(1);
522 }
523 for(h = homes; ; h = h->h_next) {
524 if(!h) {
525 fprintf(stderr, "Deliver: Who are you?\n");
526 done(1);
527 }
528 if(h->h_uid == myuid)
529 break;
530 }
531 if(!now)
532 now = time((long *)0);
533 fprintf(md, "%s %s\n", h->h_name, cdate(&now));
534 if(debug) {
535 printf("Netmail..."); fflush(stdout);
536 }
537 for(mp = netaddrs.m_next; mp; mp = mp->m_next) {
538 if(debug) printf("%s at %s\n",mp->m_mbox, mp->m_host);
539 for(hp = hosts.nh_next; hp; hp = hp->nh_next)
540 if(mp->m_hnum == hp->nh_num)
541 break;
542 if(!hp) {
543 fprintf(stderr, "Deliver: hnum->name botch.\n");
544 done(1);
545 }
546 fprintf(md, "/%s %s\n", hp->nh_name, mp->m_mbox);
547 if(verbose)
548 printf("%s at %s: queued\n", mp->m_mbox, mp->m_host);
549 }
550 putc('\n', md);
551 if(fflush(md) == EOF) goto wrerr;
552 if((fd = open(tmpfil, 0)) == -1) {
553 fprintf(stderr, "Deliver: HuH? Can't reopen ");
554 perror(tmpfil);
555 done(1);
556 }
557 do {
558 if((count = read(fd, buf, sizeof buf)) > 0) {
559 if(write(fileno(md), buf, count) != count) {
560 wrerr: fprintf(stderr, "Deliver: write error on ");
561 perror(tmpname);
562 VOID unlink(tmpname);
563 done(1);
564 }
565 } else if(count < 0) {
566 fprintf(stderr, "deliver: Read error on ");
567 perror(tmpfil);
568 VOID unlink(tmpname);
569 done(1);
570 }
571 } while(count == sizeof buf);
572 if(close(fd) == -1 || fclose(md) == EOF) goto wrerr;
573 if(link(tmpname, queuename) == -1 ||
574 unlink(tmpname) == -1) {
575 fprintf(stderr, "Deliver: Trouble linking tmpfile into queue ");
576 perror(queuename);
577 }
578 VOID chmod (queuename, 0600);
579 }
580
581 uumail()
582 {
583 register struct mailname *mp;
584 register struct shome *h;
585 register FILE *rmf;
586 FILE *popen();
587 int fd, count;
588 char cmd[64];
589 char buf[BUFSIZ];
590
591 for(h = homes; h; h = h->h_next)
592 if(h->h_uid == myuid)
593 break;
594 if(!h) {
595 fprintf(stderr, "Deliver: Who are you?\n");
596 done(1);
597 }
598 if(!now)
599 now = time((long *)0);
600 for(mp = uuaddrs.m_next; mp; mp = mp->m_next) {
601 VOID sprintf(cmd, index(mp->m_mbox, '!') ?
602 "uux - %s!rmail \\(%s\\)" :
603 "uux - %s!rmail %s",
604 mp->m_host, mp->m_mbox);
605 if ((rmf=popen (cmd, "w")) == NULL) {
606 fprintf(stderr, "Deliver: Could not open uux pipe.\n");
607 done(1);
608 }
609
610 if((fd = open(uutmpfil, 0)) == -1) {
611 fprintf(stderr, "Deliver: HuH? Can't reopen ");
612 perror(uutmpfil);
613 done(1);
614 }
615 /* UUCP mail insists on beginning with a "From" line. */
616 /* No other concessions (">" before From) will be provided now */
617 fprintf(rmf, "From %s %.24s remote from %s\n", h->h_name,
618 ctime(&now),
619 sysname);
620 VOID fflush(rmf); /* Lest the "write"s get in ahead */
621
622 do {
623 if((count = read(fd, buf, sizeof buf)) > 0) {
624 if(write(fileno(rmf), buf, count) != count) {
625 wrerr: fprintf(stderr, "Deliver: write error on pipe\n");
626 done(1);
627 }
628 } else if(count < 0) {
629 fprintf(stderr, "deliver: Read error on ");
630 perror(uutmpfil);
631 done(1);
632 }
633 } while(count == sizeof buf);
634 if(close(fd) == -1 ) goto wrerr;
635 pclose(rmf);
636 if(verbose)
637 printf("%s At %s (via uux): queued.\n",
638 mp->m_mbox, mp->m_host);
639
640 }
641 }
642
643 char hex[] = "0123456789ABCDEF"; /* Hexadecimal */
644
645 crname(ptr) /* Create unique file name in /usr/tmp */
646 char *ptr;
647 {
648 int i;
649 short tvec[4];
650 static int filecnt;
651 register char *p, *q;
652
653 q = ptr;
654 p = (char *)&tvec[0];
655 tvec[2] = getpid();
656 tvec[3] = filecnt++;
657 if (filecnt==256) {
658 filecnt = 0;
659 sleep(1);
660 }
661 VOID time((time_t *)tvec);
662 for (i=7; i; --i) {
663 *q++ = hex[(*p>>4)&017];
664 *q++ = hex[ *p++ &017];
665 }
666 *q = '\0';
667 }
668
669
670 putfrom(out)
671 register FILE *out;
672 {
673 register struct shome *h;
674 register struct mailname *mp;
675 register char *cp;
676
677 for(h = homes; h; h = h->h_next)
678 if(h->h_uid == myuid) {
679 if(format) {
680 if(!(mp = getm(h->h_name, HOSTNAME)))
681 done(1);
682 cp = adrformat(mp,HOSTNAME);
683 mnfree(mp);
684 } else
685 cp = h->h_name;
686 if(mn_from) {
687 if(mn_from->m_hnum != HOSTNUM ||
688 !uleq(mn_from->m_mbox, h->h_name))
689 fprintf(out, "Sender: %s\n", cp);
690 } else
691 fprintf(out, "From: %s\n", cp);
692 return;
693 }
694 fprintf(stderr, "Deliver: WHO ARE YOU?\n");
695 done(1);
696 }
697
698
699 putdate(out)
700 register FILE *out;
701 {
702 register char *t, *p;
703 char *timezone();
704 struct timeb tb;
705 struct tm *tmp, *localtime();
706 static char *wday[] = {
707 "Sun",
708 "Mon",
709 "Tues",
710 "Wednes",
711 "Thurs",
712 "Fri",
713 "Satur"
714 };
715
716 if(!now)
717 now = time((long *)0);
718 t = ctime(&now);
719 ftime(&tb);
720 tmp = localtime(&now);
721 p = timezone(tb.timezone, tmp->tm_isdst);
722
723 /* day 13 Apr 1981 20 :38 -PST */
724 fprintf(out, "Date: %sday, %.2s %.3s %.4s %.2s:%.2s-%.3s\n",
725 wday[tmp->tm_wday], t+8, t+4, t+20, t+11, t+14, p);
726 }
727
728
729 putmsgid(sp)
730 FILE *sp;
731 {
732 if(!msgid)
733 return;
734 if(!now)
735 now = time((long *)0);
736 fprintf(sp, "Message-ID: <%u.%ld@%s>\n", myuid, now, HOSTNAME);
737 }
738
739
740 /***/
741 pl()
742 {
743 register struct mailname *mp;
744
745 printf("local: ");
746 for(mp = localaddrs.m_next; mp; mp=mp->m_next)
747 printf("%s (%s)%s",
748 mp->m_mbox, mp->m_headali, mp->m_next?", ":"");
749 printf("\n");
750 printf("net: ");
751 for(mp = netaddrs.m_next; mp; mp=mp->m_next)
752 printf("%s@%s (%s)%s",
753 mp->m_mbox, mp->m_host, mp->m_headali, mp->m_next?", ":"");
754 printf("\n");
755 printf("uucp: ");
756 for(mp = uuaddrs.m_next; mp; mp=mp->m_next)
757 printf("%s!%s (%s)%s",
758 mp->m_host, mp->m_mbox, mp->m_headali, mp->m_next?", ":"");
759 printf("\n");
760 }
761 /***/
762
763 insert(np, queue)
764 register struct mailname *np, *queue;
765 {
766 register struct mailname *mp;
767
768 /*** printf("insert(%s@%d)=>", np->m_mbox, np->m_hnum); /***/
769 for(mp = queue; mp->m_next; mp = mp->m_next)
770 if(duplicate(np, mp)) { /* Don't insert existing name! */
771 /*** printf("0\n"); /***/
772 return 0;
773 }
774 mp->m_next = np;
775 /*** printf("1\n"); /***/
776 return 1;
777 }
778
779 duplicate(np, mp)
780 struct mailname *np, *mp;
781 {
782 if (*np->m_at == '!')
783 /* If uucp, compare host names */
784 if(uleq(np->m_host, mp->m_next->m_host) &&
785 uleq(np->m_mbox, mp->m_next->m_mbox))
786 return(1);
787 else
788 return(0);
789
790 /* Otherwise, compare host numbers */
791 if(np->m_hnum == mp->m_next->m_hnum &&
792 uleq(np->m_mbox, mp->m_next->m_mbox))
793 /* Special consideration, eg: news.topic1 != news.topic2 */
794 return( uleq(np->m_headali, mp->m_next->m_headali) ? 1
795 : alitype == STAR ? 0 : 1);
796 else
797 return(0);
798 }
799
800
801
802 cinsert(str)
803 char *str;
804 {
805 register struct mailname *mp;
806
807 if(!(mp = getm(str, HOSTNAME)))
808 done(1);
809 mp->m_headali = getcpy(head_alias);
810 if(!insert(mp, mp->m_hnum == HOSTNUM ?
811 &localaddrs :
812 (*mp->m_at == '!')? &uuaddrs :
813 &netaddrs)) {
814 mnfree(mp);
815 }
816 }
817
818
819 /* alias implementation below...
820 */
821
822 #define SAVE 0
823 #define RESET 1
824 #define NEXT 2
825 #define GROUP "/etc/group"
826 char *AliasFile = "/etc/MailAliases";
827
828 char *termptr, *listsave;
829
830 char *
831 parse(ptr, buf)
832 register char *ptr;
833 char *buf;
834 {
835 register char *cp;
836
837 cp = buf;
838 while(isspace(*ptr) || *ptr == ',' || *ptr == ':')
839 ptr++;
840 while(isalnum(*ptr) || *ptr == '/' || *ptr == '-' ||
841 *ptr == '!' || *ptr == '@' || *ptr == ' ' ||
842 *ptr == '.' || *ptr == '*')
843 *cp++ = *ptr++;
844 if(cp == buf) {
845 switch(*ptr) {
846 case '<':
847 case '=':
848 *cp++ = *ptr++;
849 }
850 }
851 *cp = 0;
852 if(cp == buf)
853 return 0;
854 termptr = ptr;
855 return buf;
856 }
857
858 char *
859 setptr(type)
860 int type;
861 {
862 switch(type) {
863
864 case SAVE:
865 listsave = termptr;
866 break;
867 case RESET: /* Reread the current alias replacement-list */
868 termptr = listsave;
869 break;
870 case NEXT:
871 break;
872 }
873 return(termptr);
874 }
875
876 alias()
877 {
878 register char *cp, *pp;
879 char *parsep = 0;
880 struct mailname ptrsave;
881 register struct mailname *lp;
882 char line[256], pbuf[64];
883 FILE *a;
884
885 if((a = fopen(AliasFile, "r")) == NULL) {
886 fprintf(stderr, "Can't open alias file ");
887 perror(AliasFile);
888 done(1);
889 }
890 while(fgets(line, sizeof line, a)) {
891 if(line[0] == ';' || line[0] == '\n') /* Comment Line */
892 continue;
893 cndfree(parsep);
894 if((parsep = getcpy(parse(line, pbuf))) == NULL) {
895 oops: fprintf(stderr, "Bad alias file %s\n", AliasFile);
896 fprintf(stderr, "Line: %s", line);
897 done(1);
898 }
899 VOID setptr(SAVE);
900 for(lp = &localaddrs; lp->m_next; lp = lp->m_next) {
901 if(alitype = aleq(lp->m_next->m_mbox, parsep)) {
902 setali(lp->m_next);
903
904 ptrsave.m_next = lp; /* Maintain ptr */
905 remove(lp); /* continuity after */
906 lp = &ptrsave; /* lp->next is removed. */
907
908 if(!(cp = setptr(NEXT)) ||
909 !(pp = parse(cp, pbuf)))
910 goto oops;
911 switch(*pp) {
912 case '<': /* From file */
913 cp = setptr(NEXT);
914 if((pp = parse(cp, pbuf)) == NULL)
915 goto oops;
916 addfile(pp);
917 break;
918 case '=': /* UNIX group */
919 cp = setptr(NEXT);
920 if((pp = parse(cp, pbuf)) == NULL)
921 goto oops;
922 addgroup(pp);
923 break;
924 case '*': /* ALL Users */
925 addall();
926 break;
927 default: /* Simple list */
928 for(;;) {
929 cinsert(pp);
930 if(!(cp = setptr(NEXT)) ||
931 !(pp = parse(cp, pbuf)))
932 break;
933 }
934 VOID setptr(RESET);
935 }
936 /* May be more news.*<topics> in hdr */
937 if(alitype == STAR) continue;
938 else break;
939 }
940 }
941 }
942 alitype = 0;
943 }
944
945 setali(mp)
946 struct mailname *mp;
947 {
948 /* propogate original alias match */
949
950 cndfree(head_alias);
951 head_alias = (*mp->m_headali ? getcpy(mp->m_headali) :
952 getcpy(mp->m_mbox));
953 }
954
955
956 addfile(file)
957 char *file;
958 {
959 register char *cp, *pp;
960 char line[128], pbuf[64];
961 FILE *f;
962
963 /*** printf("addfile(%s)\n", file); ***/
964 if((f = fopen(file, "r")) == NULL) {
965 fprintf(stderr, "Can't open ");
966 perror(file);
967 done(1);
968 }
969 while(fgets(line, sizeof line, f)) {
970 cp = line;
971 while(pp = parse(cp, pbuf)) {
972 cinsert(pp);
973 cp = setptr(NEXT);
974 }
975 }
976 VOID fclose(f);
977 }
978
979 addgroup(group)
980 char *group;
981 {
982 register char *cp, *pp;
983 int found = 0;
984 char line[128], pbuf[64], *rindex();
985 FILE *f;
986
987 /*** printf("addgroup(%s)\n", group); ***/
988 if((f = fopen(GROUP, "r")) == NULL) {
989 fprintf(stderr, "Can't open ");
990 perror(GROUP);
991 done(1);
992 }
993 while(fgets(line, sizeof line, f)) {
994 pp = parse(line, pbuf);
995 if(strcmp(pp, group) == 0) {
996 cp = rindex(line, ':');
997 while(pp = parse(cp, pbuf)) {
998 cinsert(pp);
999 cp = setptr(NEXT);
1000 }
1001 found++;
1002 }
1003 }
1004 if(!found) {
1005 fprintf(stderr, "Group: %s non-existent\n", group);
1006 done(1);
1007 }
1008 VOID fclose(f);
1009 }
1010
1011 addall()
1012 {
1013 register struct shome *h;
1014
1015 /*** printf("addall()\n"); ***/
1016 for(h = homes; h; h = h->h_next)
1017 if(h->h_uid >= EVERYONE)
1018 cinsert(h->h_name);
1019 }
1020
1021 remove(mp) /* Remove NEXT from argument node! */
1022 register struct mailname *mp;
1023 {
1024 register struct mailname *rp;
1025
1026 rp = mp->m_next;
1027 mp->m_next = rp->m_next;
1028 cndfree(rp->m_mbox);
1029 cndfree(rp->m_host);
1030 cndfree(rp->m_text);
1031 cndfree(rp->m_headali);
1032 cndfree( (char *)rp);
1033 }
1034
1035 int alarmed;
1036
1037 alarmclock()
1038 {
1039 alarmed++;
1040 }
1041
1042 char **environ;
1043 char *empty[] = {0};
1044
1045 calluserprog(prog, mail, fd, h, head_alias)
1046 char *prog, *mail, *head_alias;
1047 int fd;
1048 register struct shome *h;
1049 {
1050 register int pid, child, i;
1051 int status;
1052
1053 if(verbose) {
1054 printf("Invoking %s ", prog); fflush(stdout);
1055 }
1056 i = 0;
1057 while((child = fork()) == -1)
1058 if(++i > 10) {
1059 fprintf(stderr, "Can't get a fork to invoke %s!\n",
1060 prog);
1061 donecd = 1;
1062 return;
1063 } else
1064 sleep(2);
1065 if(child == 0) { /* In child... */
1066 if(fd != 3)
1067 dup2(fd, 3);
1068 for(i = 4; i < 15; i++)
1069 VOID close(i);
1070 environ = empty;
1071 putenv("USER", h->h_name);
1072 putenv("HOME", h->h_home);
1073 VOID setgid(h->h_gid);
1074 VOID setuid(h->h_uid);
1075 execlp(prog, prog, tmpfil, mail, h->h_home, head_alias, 0);
1076 perror(prog);
1077 done(-1);
1078 }
1079 VOID signal(SIGALRM, alarmclock);
1080 alarmed = 0;
1081 VOID alarm(120); /* Give receive proc 120 secs */
1082 status = 0;
1083 while((pid = wait(&status)) != -1 && pid != child && !alarmed) ;
1084 if(alarmed) {
1085 VOID kill(0, SIGINT);
1086 VOID signal(SIGALRM, alarmclock);
1087 alarmed = 0;
1088 VOID alarm(120);
1089 while((pid = wait(&status)) != -1 && pid != child && !alarmed) ;
1090 if(alarmed) {
1091 VOID kill(child, SIGKILL);
1092 VOID signal(SIGALRM, alarmclock);
1093 alarmed = 0;
1094 VOID alarm(120);
1095 while((pid = wait(&status)) != -1 && pid != child && !alarmed) ;
1096 }
1097 fprintf(stderr, "Deliver: Killed %s--Took more than 120 seconds!\n",
1098 prog);
1099 donecd = 1;
1100 status = 0;
1101 } else
1102 VOID alarm(0);
1103 if(status) {
1104 printf("Deliver: %s error %d from %s on delivery to %s\n",
1105 status&0377? "System" : "User",
1106 status&0377? status&0377 : status>>8,
1107 prog, h->h_name);
1108 donecd = 1;
1109 }
1110 if(pid == -1) {
1111 fprintf(stderr, "Deliver: wait on receive process returned -1\n");
1112 perror("");
1113 donecd = 1;
1114 }
1115 }
1116
1117 aleq(string, aliasent)
1118 register char *string, *aliasent;
1119 {
1120 register int c;
1121
1122 while(c = *string++)
1123 if(*aliasent == '*')
1124 return(STAR);
1125 else if((c|040) != (*aliasent|040))
1126 return(0);
1127 else
1128 aliasent++;
1129 /*** return(*aliasent == 0 | *aliasent == '*'); ***/
1130 return(*aliasent == '*' ? STAR : *aliasent == 0 ? 1 : 0);
1131 }
1132
1133 fcc(file, folder)
1134 char *file, *folder;
1135 {
1136 int child, pid, status;
1137 char fold[128];
1138
1139 if(verbose) {
1140 printf("Fcc: %s...", folder); fflush(stdout);
1141 }
1142 while((child = fork()) == -1) sleep(5);
1143 if(child == 0) {
1144 if(*file != '+')
1145 strcpy(fold, "+");
1146 strcat(fold, folder);
1147 VOID setuid(myuid);
1148 execl(fileproc, "file", "-link", "-file", file, fold, 0);
1149 exit(-1);
1150 } else while((pid = wait(&status)) != -1 && pid != child) ;
1151 if(status)
1152 fprintf(stderr, "Deliver: Error on fcc to %s\n", folder);
1153 else if(verbose)
1154 p