]>
diplodocus.org Git - nmh/blob - docs/historical/2.9BSD/cmds/deliver.c
2 static char sccsid
[] = "@(#)deliver.c 4.3 3/5/83";
10 #include <sys/timeb.h>
15 /*#define DELIVERMAIL "/etc/delivermail"*/
17 #define RECVPROG "/.mh_receive"
18 #define FCCS 10 /* Max number of fcc's allowed */
20 struct shome
{ /* Internal name/uid/home database */
28 struct swit switches
[] = {
31 "noformat", 0, /* 2 */
35 "noverbose", 0, /* 6 */
40 int verbose
, format
, msgid
, debug
, myuid
, addrp
;
41 int lockwait
; /* Secs to wait for mail lock (From strings/lockdir.c) */
42 #define LOCKWAIT (lockwait * 5) /* Ignore lock if older than this */
45 char tmpfil
[32], fccfold
[FCCS
][128];
51 struct mailname
*m_next
;
59 register char *cp
, *addrs
, **argp
;
60 register struct mailname
*lp
;
61 char buf
[BUFSIZ
], name
[NAMESZ
];
63 int state
, compnum
, fd
;
71 switch(smatch(++cp
, switches
)) {
72 case -2:ambigsw(cp
, switches
); /* ambiguous */
75 case -1:fprintf(stderr
, "sndmsg: -%s unknown switch.\n", cp
);
77 case 0: verbose
++; debug
++; continue; /* -debug */
78 case 1: fprintf(stderr
, "Deliver: -format not yet.\n");
80 /*** case 1: format = 1; continue; /* -format */
81 case 2: format
= 0; continue; /* -noformat */
82 case 3: msgid
= 1; continue; /* -msgid */
83 case 4: msgid
= 0; continue; /* -nomsgid */
84 case 5: verbose
= 1; continue; /* -verbose */
85 case 6: verbose
= 0; continue; /* -noverbose */
86 case 7: help("deliver [switches] file",
91 fprintf(stderr
, "Deliver: Only one message at a time!\n");
97 fprintf(stderr
, "Deliver: No Message specified.\n");
98 fprintf(stderr
, "Deliver: Usage: deliver [switches] file\n");
101 if((in
= fopen(msg
, "r")) == NULL
) {
102 fprintf(stderr
, "Deliver: Can't open ");
106 copy(makename("locs", ".tmp"), copy("/tmp/", tmpfil
));
108 if((out
= fopen(tmpfil
, "w")) == NULL
) {
109 fprintf(stderr
, "Can't create %s\n", tmpfil
);
119 putdate(0, out
); /* Tack on the date */
120 putfrom(out
); /* Tack on the from */
122 putmsgid(out
); /* and msg id if desired */
123 for(addrs
= 0, compnum
= 1, state
= FLD
;;) {
124 state
= m_getfld(state
, name
, buf
, sizeof buf
, in
);
131 if(uleq(name
, "to") || uleq(name
, "cc") ||
133 addrs
= add(buf
, addrs
);
134 if(!uleq(name
, "bcc")) {
136 while(*cp
== ' ' || *cp
== '\t')
138 fprintf(out
, "%s: %s", name
, cp
);
139 if(uleq(name
, "fcc")) {
141 fprintf(stderr
, "Deliver: too many fcc's.\n");
144 copy(cp
, fccfold
[fccind
]);
145 if(cp
= rindex(fccfold
[fccind
], '\n'))
150 while(state
== FLDPLUS
|| state
== FLDEOF
) {
151 state
= m_getfld(state
, name
, buf
, sizeof buf
, in
);
152 addrs
= add(buf
, addrs
);
161 fprintf(out
, "\n%s", buf
);
162 while(state
== BODY
) {
163 state
=m_getfld(state
,name
,buf
,sizeof buf
,in
);
172 fprintf(stderr
, "??Message Format Error ");
173 fprintf(stderr
, "in Component #%d.\n", compnum
);
177 fprintf(stderr
, "Getfld returned %d\n", state
);
187 while(cp
= getname(addrs
)) /* Put addrs in chain */
190 printf("Before alias(): "); pl();
193 alias(); /* Map names if needed */
195 for(lp
= addrlist
.m_next
; lp
; lp
= lp
->m_next
) {
197 if (netname(lp
->m_name
))
201 if(home(lp
->m_name
) == NULL
) {
202 fprintf(stderr
, "Deliver: Unknown User: %s.\n", lp
->m_name
);
203 fprintf(stderr
, "Deliver: Message Not Delivered.\n");
211 printf("Addrs: "); pl();
214 signal(SIGINT
, SIG_IGN
);
215 signal(SIGQUIT
, SIG_IGN
);
216 if(!debug
) { /* Send the mail */
217 fd
= open(tmpfil
, 0);
218 for(lp
= addrlist
.m_next
; lp
; lp
= lp
->m_next
)
219 sendmail(lp
->m_name
, fd
);
226 strcpy(tmp2fil
, "/tmp/mhFXXXXXX");
228 out
= fopen(tmp2fil
, "w");
229 putdate(0, out
); /* Tack on the date */
230 putfrom(out
); /* Tack on the from */
231 appendfile(tmpfil
, out
);
233 for(state
= 0; state
< fccind
; state
++)
234 fcc(tmp2fil
, fccfold
[state
]);
237 for(state
= 0; state
< fccind
; state
++)
238 fcc(tmp2fil
, fccfold
[state
]);
248 appendfile(name
, fout
)
255 if ((fin
= fopen(name
, "r")) == NULL
) {
259 while ((c
= getc(fin
)) != EOF
)
271 if (index(cp
, ':') || index(cp
, '!') ||
272 index(cp
, '@') || index(cp
, '^'))
281 register struct passwd
*pw
;
282 register struct shome
*h
, *ph
;
283 struct passwd
*getpwent();
285 ph
= (struct shome
*) &homes
;
286 while((pw
= getpwent()) != NULL
) {
287 h
= (struct shome
*) malloc(sizeof *h
);
289 h
->h_name
= (char *) malloc(strlen(pw
->pw_name
)+1);
290 strcpy(h
->h_name
, pw
->pw_name
);
291 h
->h_uid
= pw
->pw_uid
;
292 h
->h_gid
= pw
->pw_gid
;
293 h
->h_home
= (char *) malloc(strlen(pw
->pw_dir
)+1);
294 strcpy(h
->h_home
, pw
->pw_dir
);
305 register struct shome
*h
;
307 for(h
= homes
; h
; h
= h
->h_next
)
308 if(uleq(name
, h
->h_name
))
319 static char name
[32];
320 static char *getaddrs
;
326 while(*getaddrs
&& !isalpha(*getaddrs
))
333 while(!isspace(*getaddrs
))
336 while(isalnum(*getaddrs
) || index(".:@/-", *getaddrs
))
345 char *bracket
= "\1\1\1\1\n";
347 sendmail(name
, fd
) /* Runs with signals ignored! */
351 char buf
[BUFSIZ
], *myname
;
352 register int i
, m
, c
;
353 register char *mail
, *receive
, *lock
= 0;
354 register struct shome
*h
;
359 printf("%s: ", name
);
366 * If no home for this user, as in network address
367 * being forced down this path, just assume normal
374 mail
= concat(mailboxes
, h
->h_name
, 0);
375 /*** mail = concat(h->h_home, mailbox, 0); ***/
376 receive
= concat(h
->h_home
, RECVPROG
, 0);
377 if(access(receive
, 1) == 0) { /* User has a receive prog */
379 calluserprog(receive
, mail
, fd
, h
);
393 myname
= getenv("USER");
394 if (myname
== (char *) 0)
399 /* fiddle with files, then . . . */
404 execl(Mailprog
, "Mail", "-r", myname
, name
, 0);
405 execl("/bin/mail", "mail", "-r", myname
, name
, 0);
414 while (wait(&status
) != pid
)
420 if((m
= open(mail
, 2)) < 0) {
421 if((m
= creat(mail
, 0600)) < 0) {
422 fprintf(stderr
, "Deliver: Can't write ");
426 chown(mail
, h
->h_uid
, h
->h_gid
);
428 fprintf(stderr
, "Creating %s ", mail
);
432 lock
= concat(lockdir
, h
->h_name
, 0);
433 for(i
= 0; i
< lockwait
; i
+= 2) {
434 if(link(mail
, lock
) >= 0)
436 if(i
== 0 && stat(mail
, &stbuf
) >= 0 &&
437 stbuf
.st_ctime
+ LOCKWAIT
< time((long *) 0)) {
450 printf("Removing lock. ");
453 if(link(mail
, lock
) < 0) {
454 fprintf(stderr
, "Can't lock %s to %s\n",
461 write(m
, bracket
, 5);
463 if((c
= read(fd
, buf
, sizeof buf
)) > 0)
464 if(write(m
, buf
, c
) != c
) {
465 fprintf(stderr
, "Write error on ");
470 while(c
== sizeof buf
);
471 write(m
, bracket
, 5);
480 if(verbose
&& !donecd
)
487 * Salt away a name to send to, ultimately.
490 char *stashname
[100];
498 cp
= (char *) calloc(1, strlen(name
) + 1);
502 *stashp
++ = "delivermail";
514 while ((pid
= fork()) == -1)
517 signal(SIGHUP
, SIG_IGN
);
518 signal(SIGINT
, SIG_IGN
);
519 signal(SIGQUIT
, SIG_IGN
);
523 execv(DELIVERMAIL
, stashname
);
533 register struct shome
*h
;
535 for(h
= homes
; h
; h
= h
->h_next
)
536 if(h
->h_uid
== myuid
) {
537 fprintf(out
, "From: %s\n", h
->h_name
);
540 fprintf(stderr
, "Deliver: WHO ARE YOU?\n");
553 fprintf(sp
, "Message-ID: <%u.%u.%ld.%s@%s>\n", getpid(), myuid
,
554 now
, localname
, hostname
);
560 register struct mailname
*mp
;
562 for(mp
= addrlist
.m_next
; mp
; mp
=mp
->m_next
)
563 printf("%s%s", mp
->m_name
, mp
->m_next
?", ":"");
571 register struct mailname
*mp
;
574 /*** printf("insert(%s)\n", name); ***/
576 for(mp
= &addrlist
; mp
->m_next
; mp
= mp
->m_next
)
577 if(uleq(name
, mp
->m_next
->m_name
))
578 return; /* Don't insert existing name! */
579 mp
->m_next
= (struct mailname
*) malloc(sizeof *mp
->m_next
);
582 mp
->m_name
= getcpy(name
);
586 /* alias implementation below...
591 #define GROUP "/etc/group"
592 char *AliasFile
= "/usr/local/lib/MailAliases";
604 while(isspace(*ptr
) || *ptr
== ',' || *ptr
== ':')
606 while(isalnum(*ptr
) || *ptr
== '/' || *ptr
== '-' ||
607 *ptr
== '.' || *ptr
== '*')
632 register char *cp
, *pp
;
633 register struct mailname
*lp
;
634 char line
[256], pbuf
[64];
637 if((a
= fopen(AliasFile
, "r")) == NULL
)
639 while(fgets(line
, sizeof line
, a
)) {
640 if(line
[0] == ';' || line
[0] == '\n') /* Comment Line */
642 if((pp
= parse(line
, pbuf
)) == NULL
) {
643 oops
: fprintf(stderr
, "Bad alias file %s\n", AliasFile
);
644 fprintf(stderr
, "Line: %s", line
);
647 for(lp
= &addrlist
; lp
->m_next
; lp
= lp
->m_next
) {
648 if(aleq(lp
->m_next
->m_name
, pp
)) {
650 if(!(cp
= advance(line
)) ||
651 !(pp
= parse(cp
, pbuf
)))
654 case '<': /* From file */
656 if((pp
= parse(cp
, pbuf
)) == NULL
)
660 case '=': /* UNIX group */
662 if((pp
= parse(cp
, pbuf
)) == NULL
)
666 case '*': /* ALL Users */
669 default: /* Simple list */
672 if(!(cp
= advance(line
)) ||
673 !(pp
= parse(cp
, pbuf
)))
687 register char *cp
, *pp
;
688 char line
[128], pbuf
[64];
691 /*** printf("addfile(%s)\n", file); ***/
692 if((f
= fopen(file
, "r")) == NULL
) {
693 fprintf(stderr
, "Can't open ");
697 while(fgets(line
, sizeof line
, f
)) {
699 while(pp
= parse(cp
, pbuf
)) {
710 register char *cp
, *pp
;
712 char line
[128], pbuf
[64], *rindex();
715 /*** printf("addgroup(%s)\n", group); ***/
716 if((f
= fopen(GROUP
, "r")) == NULL
) {
717 fprintf(stderr
, "Can't open ");
721 while(fgets(line
, sizeof line
, f
)) {
722 pp
= parse(line
, pbuf
);
723 if(strcmp(pp
, group
) == 0) {
724 cp
= rindex(line
, ':');
725 while(pp
= parse(cp
, pbuf
)) {
733 fprintf(stderr
, "Group: %s non-existent\n", group
);
741 register struct shome
*h
;
743 /*** printf("addall()\n"); ***/
744 for(h
= homes
; h
; h
= h
->h_next
)
745 if(h
->h_uid
>= EVERYONE
)
749 remove(mp
) /* Remove NEXT from argument node! */
750 register struct mailname
*mp
;
752 register struct mailname
*rp
;
755 mp
->m_next
= rp
->m_next
;
767 extern char **environ
;
768 char envhome
[60] = "HOME=";
769 char envname
[60] = "NAME=";
770 char *envinit
[] = {envhome
, envname
, (char *) 0};
772 calluserprog(prog
, mail
, fd
, h
)
775 register struct shome
*h
;
777 register int pid
, child
, i
;
781 printf("Invoking %s ", prog
);
785 while((child
= fork()) == -1)
787 fprintf(stderr
, "Can't get a fork to invoke %s!\n",
793 if(child
== 0) { /* In child... */
796 for(i
= 4; i
< 15; i
++)
798 strcat(envname
, h
->h_name
);
799 strcat(envhome
, h
->h_home
);
803 execlp(prog
, prog
, tmpfil
, mail
, h
->h_home
, 0);
807 signal(SIGALRM
, alarmclock
);
809 alarm(30); /* Give receive proc 30 secs */
811 while((pid
= wait(&status
)) != -1 && pid
!= child
&& !alarmed
) ;
814 signal(SIGALRM
, alarmclock
);
817 while((pid
= wait(&status
)) != -1 && pid
!= child
&& !alarmed
) ;
819 kill(child
, SIGKILL
);
820 signal(SIGALRM
, alarmclock
);
823 while((pid
= wait(&status
)) != -1 && pid
!= child
&& !alarmed
) ;
825 fprintf(stderr
, "Deliver: Killed %s--Took more than 30 seconds!\n",
832 printf("Deliver: %s error %d from %s on delivery to %s\n",
833 status
&0377? "System" : "User",
834 status
&0377? status
&0377 : status
>>8,
839 fprintf(stderr
, "Deliver: wait on receive process returned -1\n");
845 aleq(string
, aliasent
)
846 register char *string
, *aliasent
;
853 else if((c
|040) != (*aliasent
|040))
857 return(*aliasent
== 0 | *aliasent
== '*');
863 int child
, pid
, status
;
867 printf("Fcc: %s...", folder
);
870 while((child
= fork()) == -1) sleep(5);
874 strcat(fold
, folder
);
876 execl(fileproc
, "file", "-link", "-file", file
, fold
, 0);
878 } else while((pid
= wait(&status
)) != -1 && pid
!= child
) ;
880 fprintf(stderr
, "Deliver: Error on fcc to %s\n", folder
);