4 /* globals to all files */
6 char *thissys, *altthissys;
14 /* global to this file */
15 static String *errstring;
21 static int add822headers = 1;
22 static String *arglist;
25 static int send(dest *, message *, int);
26 static void lesstedious(void);
27 static void save_mail(message *);
28 static int complain_mail(dest *, message *);
29 static int pipe_mail(dest *, message *);
30 static void appaddr(String *, dest *);
31 static void mkerrstring(String *, message *, dest *, dest *, char *, int);
32 static int replymsg(String *, message *, dest *);
33 static int catchint(void*, char*);
38 fprint(2, "usage: mail [-birtx] list-of-addresses\n");
43 main(int argc, char *argv[])
76 if(shellchars(*argv)){
77 fprint(2, "illegal characters in destination\n");
80 d_insert(&dp, d_new(s_copy(*argv++)));
89 * - whether we're rmail or mail
91 base = basename(argv0);
92 checkforward = rmail = (strcmp(base, "rmail")==0) | rflg;
93 thissys = sysname_read();
94 altthissys = alt_sysname_read();
99 * read the mail. If an interrupt occurs while reading, save in
103 Binit(&in, 0, OREAD);
105 atnotify(catchint, 1);
106 mp = m_read(&in, rmail, !iflg);
109 if (interrupt != 0) {
115 if(default_from(mp) < 0){
116 fprint(2, "%s: can't determine login name\n", argv0);
124 * If this is a gateway, translate the sender address into a local
125 * address. This only happens if mail to the local address is
126 * forwarded to the sender.
131 * Protect against shell characters in the sender name for
134 mp->sender = escapespecial(mp->sender);
135 if (shellchars(s_to_c(mp->sender)))
136 mp->replyaddr = s_copy("postmaster");
138 mp->replyaddr = s_clone(mp->sender);
141 * reject messages that have been looping for too long
143 if(mp->received > 32)
144 exit(refuse(dp, mp, "possible forward loop", 0, 0));
147 * reject messages that are too long. We don't do it earlier
148 * in m_read since we haven't set up enough things yet.
151 exit(refuse(dp, mp, "message too long", 0, 0));
153 rv = send(dp, mp, checkforward);
161 /* send a message to a list of sites */
163 send(dest *destp, message *mp, int checkforward)
165 dest *dp; /* destination being acted upon */
166 dest *bound; /* bound destinations */
169 /* bind the destinations to actions */
170 bound = up_bind(destp, mp, checkforward);
171 if(add822headers && mp->haveto == 0){
173 mp->to = d_to(bound);
178 /* loop through and execute commands */
179 for (dp = d_rm(&bound); dp != 0; dp = d_rm(&bound)) {
180 switch (dp->status) {
182 errors += cat_mail(dp, mp);
186 if (!rmail && !nflg && !forked) {
190 errors += pipe_mail(dp, mp);
193 errors += complain_mail(dp, mp);
201 /* avoid user tedium (as Mike Lesk said in a previous version) */
227 save_mail(message *mp)
234 fp = sysopen(s_to_c(file), "cAt", 0660);
239 fprint(2, "saved in %s\n", s_to_c(file));
243 /* remember the interrupt happened */
246 catchint(void *a, char *msg)
249 if(strstr(msg, "interrupt") || strstr(msg, "hangup")) {
256 /* dispose of incorrect addresses */
258 complain_mail(dest *dp, message *mp)
262 switch (dp->status) {
264 msg = "Invalid address"; /* a little different, for debugging */
267 msg = "invalid address";
270 msg = "unknown user";
274 msg = "forwarding loop";
277 if(dp->pstat && *s_to_c(dp->repl2))
278 return refuse(dp, mp, s_to_c(dp->repl2), dp->pstat, 0);
280 msg = "destination unknown or forwarding disallowed";
289 if(dp->pstat && *s_to_c(dp->repl2))
290 return refuse(dp, mp, s_to_c(dp->repl2), dp->pstat, 0);
292 msg = "name translation failed";
295 msg = "broken alias";
298 msg = "corrupted mailbox";
301 return refuse(dp, mp, "out of some resource. Try again later.", 0, 1);
307 print("%s: %s\n", msg, s_to_c(dp->addr));
310 return refuse(dp, mp, msg, 0, 0);
313 /* dispose of remote addresses */
315 pipe_mail(dest *dp, message *mp)
322 String *errstring=s_new();
324 if (dp->status == d_pipeto)
329 * collect the arguments
331 next = d_rm_same(&dp);
335 cmd = s_clone(next->repl1);
336 for(; next != 0; next = d_rm_same(&dp)){
338 s_append(cmd, s_to_c(next->addr));
341 if (next->repl2 != 0) {
343 s_append(cmd, s_to_c(next->repl2));
346 d_insert(&list, next);
351 print("%s", s_to_c(cmd));
353 print("%s\n", s_to_c(cmd));
361 pp = proc_start(s_to_c(cmd), instream(), 0, outstream(), 1, none);
362 if(pp==0 || pp->std[0]==0 || pp->std[2]==0)
363 return refuse(list, mp, "out of processes, pipes, or memory", 0, 1);
365 m_print(mp, pp->std[0]->fp, thissys, 0);
367 stream_free(pp->std[0]);
369 while(s_read_line(pp->std[2]->fp, errstring))
371 status = proc_wait(pp);
379 return refuse(list, mp, s_to_c(errstring), status, 0);
380 loglist(list, mp, "remote");
385 appaddr(String *sp, dest *dp)
390 if (dp->parent != 0) {
391 for(parent=dp->parent; parent->parent!=0; parent=parent->parent)
393 s = unescapespecial(s_clone(parent->addr));
394 s_append(sp, s_to_c(s));
396 s_append(sp, "' alias `");
398 s = unescapespecial(s_clone(dp->addr));
399 s_append(sp, s_to_c(s));
406 * returns 0 - if mail has been disposed of
407 * other - if mail has not been disposed
410 refuse(dest *list, message *mp, char *cp, int status, int outofresources)
412 String *errstring=s_new();
417 mkerrstring(errstring, mp, dp, list, cp, status);
420 * log first in case we get into trouble
422 logrefusal(dp, mp, s_to_c(errstring));
425 * bulk mail is never replied to, if we're out of resources,
426 * let the sender try again
429 /* accept it or request a retry */
431 fprint(2, "Mail %s\n", s_to_c(errstring));
432 rv = 1; /* try again later */
434 rv = 0; /* silently discard bulk */
436 rv = replymsg(errstring, mp, dp); /* try later if we can't reply */
438 /* aysnchronous delivery only happens if !rmail */
441 * if spun off for asynchronous delivery, we own the mail now.
442 * return it or dump it on the floor. rv really doesn't matter.
445 if(!outofresources && !mp->bulk)
446 replymsg(errstring, mp, dp);
448 fprint(2, "Mail %s\n", s_to_c(errstring));
458 /* make the error message */
460 mkerrstring(String *errstring, message *mp, dest *dp, dest *list, char *cp, int status)
466 sender = unescapespecial(s_clone(mp->sender));
468 /* list all aliases */
469 s_append(errstring, " from '");
470 s_append(errstring, s_to_c(sender));
471 s_append(errstring, "'\nto '");
472 appaddr(errstring, dp);
473 for(next = d_rm(&list); next != 0; next = d_rm(&list)) {
474 s_append(errstring, "'\nand '");
475 appaddr(errstring, next);
478 s_append(errstring, "'\nfailed with error '");
479 s_append(errstring, cp);
480 s_append(errstring, "'.\n");
482 /* >> and | deserve different flavored messages */
485 s_append(errstring, "The mailer `");
486 s_append(errstring, s_to_c(dp->repl1));
487 sprint(smsg, "' returned error status %x.\n\n", status);
488 s_append(errstring, smsg);
496 * create a new boundary
506 srand((time(0)<<16)|getpid());
509 strcpy(buf, "upas-");
510 for(i = 5; i < sizeof(buf)-1; i++)
511 buf[i] = 'a' + nrand(26);
517 * reply with up to 1024 characters of the
521 replymsg(String *errstring, message *mp, dest *dp)
523 message *refp = m_new();
529 boundary = mkboundary();
532 refp->rfc822headers = 1;
533 rcvr = dp->status==d_eloop ? "postmaster" : s_to_c(mp->replyaddr);
534 ndp = d_new(s_copy(rcvr));
535 s_append(refp->sender, "postmaster");
536 s_append(refp->replyaddr, "/dev/null");
537 s_append(refp->date, thedate());
539 s_append(refp->body, "To: ");
540 s_append(refp->body, rcvr);
541 s_append(refp->body, "\n");
542 s_append(refp->body, "Subject: bounced mail\n");
543 s_append(refp->body, "MIME-Version: 1.0\n");
544 s_append(refp->body, "Content-Type: multipart/mixed;\n");
545 s_append(refp->body, "\tboundary=\"");
546 s_append(refp->body, s_to_c(boundary));
547 s_append(refp->body, "\"\n");
548 s_append(refp->body, "Content-Disposition: inline\n");
549 s_append(refp->body, "\n");
550 s_append(refp->body, "This is a multi-part message in MIME format.\n");
551 s_append(refp->body, "--");
552 s_append(refp->body, s_to_c(boundary));
553 s_append(refp->body, "\n");
554 s_append(refp->body, "Content-Disposition: inline\n");
555 s_append(refp->body, "Content-Type: text/plain; charset=\"US-ASCII\"\n");
556 s_append(refp->body, "Content-Transfer-Encoding: 7bit\n");
557 s_append(refp->body, "\n");
558 s_append(refp->body, "The attached mail");
559 s_append(refp->body, s_to_c(errstring));
560 s_append(refp->body, "--");
561 s_append(refp->body, s_to_c(boundary));
562 s_append(refp->body, "\n");
563 s_append(refp->body, "Content-Type: message/rfc822\n");
564 s_append(refp->body, "Content-Disposition: inline\n\n");
565 s_append(refp->body, s_to_c(mp->body));
566 s_append(refp->body, "--");
567 s_append(refp->body, s_to_c(boundary));
568 s_append(refp->body, "--\n");
570 refp->size = s_len(refp->body);
571 rv = send(ndp, refp, 0);