]>
diplodocus.org Git - nmh/blob - uip/spost.c
3 * spost.c -- feed messages to sendmail
5 * This is a simpler, faster, replacement for "post" for use
6 * when "sendmail" is the transport system.
10 * This code is Copyright (c) 2002, by the authors of nmh. See the
11 * COPYRIGHT file in the root directory of the nmh distribution for
12 * complete copyright information.
17 #include <h/addrsbr.h>
18 #include <h/aliasbr.h>
19 #include <h/dropsbr.h>
23 #define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : c)
25 #define MAX_SM_FIELD 1476 /* < largest hdr field sendmail will accept */
26 #define FCCS 10 /* max number of fccs allowed */
28 struct swit switches
[] = {
30 { "filter filterfile", 0 },
54 { "alias aliasfile", 0 },
58 { "width columns", 0 },
66 { "dist", -4 }, /* interface from dist */
68 { "check", -5 }, /* interface from whom */
70 { "nocheck", -7 }, /* interface from whom */
72 { "whom", -4 }, /* interface from whom */
73 #define PUSHSW 22 /* fork to sendmail then exit */
75 #define NPUSHSW 23 /* exec sendmail */
78 { "library directory", -7 },
80 { "idanno number", -6 },
85 /* flags for headers->flags */
86 #define HNOP 0x0000 /* just used to keep .set around */
87 #define HBAD 0x0001 /* bad header - don't let it through */
88 #define HADR 0x0002 /* header has an address field */
89 #define HSUB 0x0004 /* Subject: header */
90 #define HTRY 0x0008 /* try to send to addrs on header */
91 #define HBCC 0x0010 /* don't output this header */
92 #define HMNG 0x0020 /* mung this header */
93 #define HNGR 0x0040 /* no groups allowed in this header */
94 #define HFCC 0x0080 /* FCC: type header */
95 #define HNIL 0x0100 /* okay for this header not to have addrs */
96 #define HIGN 0x0200 /* ignore this header */
98 /* flags for headers->set */
99 #define MFRM 0x0001 /* we've seen a From: */
100 #define MDAT 0x0002 /* we've seen a Date: */
101 #define MRFM 0x0004 /* we've seen a Resent-From: */
102 #define MVIS 0x0008 /* we've seen sighted addrs */
103 #define MINV 0x0010 /* we've seen blind addrs */
104 #define MRDT 0x0020 /* we've seen a Resent-Date: */
113 static struct headers NHeaders
[] = {
114 { "Return-Path", HBAD
, 0 },
115 { "Received", HBAD
, 0 },
116 { "Reply-To", HADR
|HNGR
, 0 },
117 { "From", HADR
|HNGR
, MFRM
},
118 { "Sender", HADR
|HBAD
, 0 },
119 { "Date", HNOP
, MDAT
},
120 { "Subject", HSUB
, 0 },
121 { "To", HADR
|HTRY
, MVIS
},
122 { "cc", HADR
|HTRY
, MVIS
},
123 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, MINV
},
124 { "Message-Id", HBAD
, 0 },
129 static struct headers RHeaders
[] = {
130 { "Resent-Reply-To", HADR
|HNGR
, 0 },
131 { "Resent-From", HADR
|HNGR
, MRFM
},
132 { "Resent-Sender", HADR
|HBAD
, 0 },
133 { "Resent-Date", HNOP
, MRDT
},
134 { "Resent-Subject", HSUB
, 0 },
135 { "Resent-To", HADR
|HTRY
, MVIS
},
136 { "Resent-cc", HADR
|HTRY
, MVIS
},
137 { "Resent-Bcc", HADR
|HTRY
|HBCC
, MINV
},
138 { "Resent-Message-Id", HBAD
, 0 },
139 { "Resent-Fcc", HFCC
, 0 },
140 { "Reply-To", HADR
, 0 },
146 static short fccind
= 0; /* index into fccfold[] */
148 static int badmsg
= 0; /* message has bad semantics */
149 static int verbose
= 0; /* spell it out */
150 static int debug
= 0; /* debugging post */
151 static int rmflg
= 1; /* remove temporary file when done */
152 static int watch
= 0; /* watch the delivery process */
153 static int backflg
= 0; /* rename input file as *.bak when done */
154 static int whomflg
= 0; /* if just checking addresses */
155 static int pushflg
= 0; /* if going to fork to sendmail */
156 static int aliasflg
= -1; /* if going to process aliases */
157 static int outputlinelen
=72;
159 static unsigned msgflags
= 0; /* what we've seen */
165 static char tmpfil
[] = "/tmp/pstXXXXXX";
167 static char from
[BUFSIZ
]; /* my network address */
168 static char signature
[BUFSIZ
]; /* my signature */
169 static char *filter
= NULL
; /* the filter for BCC'ing */
170 static char *subject
= NULL
; /* the subject field for BCC'ing */
171 static char *fccfold
[FCCS
]; /* foldernames for FCC'ing */
173 static struct headers
*hdrtab
; /* table for the message we're doing */
174 static FILE *out
; /* output (temp) file */
176 extern char *sendmail
;
179 * external prototypes
181 extern char *getfullname (void);
182 extern char *getusername (void);
187 static void putfmt (char *, char *, FILE *);
188 static void start_headers (void);
189 static void finish_headers (FILE *);
190 static int get_header (char *, struct headers
*);
191 static void putadr (char *, struct mailname
*);
192 static int putone (char *, int, int);
193 static void insert_fcc (struct headers
*, char *);
194 static void file (char *);
195 static void fcc (char *, char *);
198 static void die (char *, char *, ...);
199 static void make_bcc_file (void);
204 main (int argc
, char **argv
)
206 int state
, i
, pid
, compnum
;
207 char *cp
, *msg
= NULL
, **argp
, **arguments
;
208 char *sargv
[16], buf
[BUFSIZ
], name
[NAMESZ
];
212 setlocale(LC_ALL
, "");
214 invo_name
= r1bindex (argv
[0], '/');
216 /* foil search of user profile/context */
217 if (context_foil (NULL
) == -1)
220 mts_init (invo_name
);
221 arguments
= getarguments (invo_name
, argc
, argv
, 0);
224 while ((cp
= *argp
++)) {
226 switch (smatch (++cp
, switches
)) {
228 ambigsw (cp
, switches
);
231 adios (NULL
, "-%s unknown", cp
);
234 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
235 print_help (buf
, switches
, 1);
238 print_version(invo_name
);
254 if (!(filter
= *argp
++) || *filter
== '-')
255 adios (NULL
, "missing argument to %s", argp
[-2]);
297 if (!(cp
= *argp
++) || *cp
== '-')
298 adios (NULL
, "missing argument to %s", argp
[-2]);
300 alias (AliasFile
);/* load default aka's */
302 if ((state
= alias(cp
)) != AK_OK
)
303 adios (NULL
, "aliasing error in file %s - %s",
304 cp
, akerror(state
) );
311 if (!(cp
= *argp
++) || *cp
== '-')
312 adios (NULL
, "missing argument to %s", argp
[-2]);
313 outputlinelen
= atoi (cp
);
314 if (outputlinelen
<= 10)
319 if (!(cp
= *argp
++) || *cp
== '-')
320 adios (NULL
, "missing argument to %s", argp
[-2]);
321 /* create a minimal context */
322 if (context_foil (cp
) == -1)
327 /* -idanno switch ignored */
328 if (!(cp
= *argp
++) || *cp
== '-')
329 adios (NULL
, "missing argument to %s", argp
[-2]);
334 adios (NULL
, "only one message at a time!");
340 alias (AliasFile
); /* load default aka's */
343 adios (NULL
, "usage: %s [switches] file", invo_name
);
345 if ((in
= fopen (msg
, "r")) == NULL
)
346 adios (msg
, "unable to open");
355 if ((out
= fdopen( mkstemp (tmpfil
), "w" )) == NULL
)
356 adios (tmpfil
, "unable to create");
359 if ((out
= fopen (tmpfil
, "w")) == NULL
)
360 adios (tmpfil
, "unable to create");
361 chmod (tmpfil
, 0600);
365 hdrtab
= (msgstate
== normal
) ? NHeaders
: RHeaders
;
367 for (compnum
= 1, state
= FLD
;;) {
368 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
)) {
371 putfmt (name
, buf
, out
);
377 while (state
== FLDPLUS
) {
378 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
381 putfmt (name
, cp
, out
);
386 finish_headers (out
);
387 fprintf (out
, "\n%s", buf
);
389 while (state
== BODY
) {
390 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
396 finish_headers (out
);
401 adios (NULL
, "message format error in component #%d",
405 adios (NULL
, "getfld() returned %d", state
);
411 if (backflg
&& !whomflg
) {
412 strncpy (buf
, m_backup (msg
), sizeof(buf
));
413 if (rename (msg
, buf
) == NOTOK
)
414 advise (buf
, "unable to rename %s to", msg
);
426 * re-open the temp file, unlink it and exec sendmail, giving it
427 * the msg temp file as std in.
429 if ( freopen( tmpfil
, "r", stdin
) == NULL
)
430 adios (tmpfil
, "can't reopen for sendmail");
435 *argp
++ = "send-mail";
436 *argp
++ = "-m"; /* send to me too */
437 *argp
++ = "-t"; /* read msg for recipients */
438 *argp
++ = "-i"; /* don't stop on "." */
441 if (watch
|| verbose
)
445 if (pushflg
&& !(watch
|| verbose
)) {
446 /* fork to a child to run sendmail */
447 for (i
=0; (pid
= vfork()) == NOTOK
&& i
< 5; i
++)
451 fprintf (verbose
? stdout
: stderr
, "%s: can't fork to %s\n",
452 invo_name
, sendmail
);
455 /* we're the child .. */
461 execv ( sendmail
, sargv
);
462 adios ( sendmail
, "can't exec");
463 return 0; /* dead code to satisfy the compiler */
466 /* DRAFT GENERATION */
469 putfmt (char *name
, char *str
, FILE *out
)
475 while (*str
== ' ' || *str
== '\t')
478 if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
479 fprintf (out
, "%s: %s", name
, str
);
484 if (hdr
->flags
& HIGN
)
486 if (hdr
->flags
& HBAD
) {
487 advise (NULL
, "illegal header line -- %s:", name
);
491 msgflags
|= hdr
->set
;
493 if (hdr
->flags
& HSUB
)
494 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
496 if (hdr
->flags
& HFCC
) {
497 if ((cp
= strrchr(str
, '\n')))
499 for (cp
= pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
501 insert_fcc (hdr
, pp
);
503 insert_fcc (hdr
, pp
);
508 if (hdr
->flags
& HBCC
) {
514 if (*str
!= '\n' && *str
!= '\0') {
515 if (aliasflg
&& hdr
->flags
& HTRY
) {
516 /* this header contains address(es) that we have to do
517 * alias expansion on. Because of the saved state in
518 * getname we have to put all the addresses into a list.
519 * We then let putadr munch on that list, possibly
522 register struct mailname
*f
= 0;
523 register struct mailname
*mp
= 0;
525 while ((cp
= getname(str
))) {
526 mp
= getm( cp
, NULL
, 0, AD_HOST
, NULL
);
531 mp
->m_next
= f
->m_next
;
536 f
= mp
->m_next
; mp
->m_next
= 0;
539 /* The author(s) of spost decided that alias substitution wasn't
540 necessary for the non-HTRY headers. Unfortunately, one of those
541 headers is "From:", and having alias substitution work on that is
542 extremely useful for someone with a lot of POP3 email accounts or
543 aliases. post supports aliasing of "From:"...
545 Since "From:"-processing is incompletely implemented in this
546 unsupported and undocumented spost backend, I'm not going to take
547 the time to implement my new draft-From:-based email address
548 masquerading. If I do ever implement it here, I'd almost
549 certainly want to implement "From:" line alias processing as
550 well. -- Dan Harkless <dan-nmh@dilvish.speed.net> */
551 fprintf (out
, "%s: %s", name
, str
);
563 strncpy(from
, getusername(), sizeof(from
));
565 if ((cp
= getfullname ()) && *cp
) {
566 strncpy (sigbuf
, cp
, sizeof(sigbuf
));
567 snprintf (signature
, sizeof(signature
), "%s <%s>", sigbuf
, from
);
570 snprintf (signature
, sizeof(signature
), "%s", from
);
575 finish_headers (FILE *out
)
579 if (!(msgflags
& MDAT
))
580 fprintf (out
, "Date: %s\n", dtimenow (0));
582 fprintf (out
, "Sender: %s\n", from
);
584 fprintf (out
, "From: %s\n", signature
);
586 if (!(msgflags
& MVIS
))
587 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
592 if (!(msgflags
& MRDT
))
593 fprintf (out
, "Resent-Date: %s\n", dtimenow(0));
595 fprintf (out
, "Resent-Sender: %s\n", from
);
597 fprintf (out
, "Resent-From: %s\n", signature
);
599 if (!(msgflags
& MVIS
))
600 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
606 adios (NULL
, "re-format message and try again");
611 get_header (char *header
, struct headers
*table
)
615 for (h
= table
; h
->value
; h
++)
616 if (!strcasecmp (header
, h
->value
))
624 * output the address list for header "name". The address list
625 * is a linked list of mailname structs. "nl" points to the head
626 * of the list. Alias substitution should be done on nl.
629 putadr (char *name
, struct mailname
*nl
)
631 register struct mailname
*mp
, *mp2
;
632 register int linepos
;
636 fprintf (out
, "%s: ", name
);
637 namelen
= strlen(name
) + 2;
640 for (mp
= nl
; mp
; ) {
641 if (linepos
> MAX_SM_FIELD
) {
642 fprintf (out
, "\n%s: ", name
);
646 /* a local name - see if it's an alias */
647 cp
= akvalue(mp
->m_mbox
);
648 if (cp
== mp
->m_mbox
)
649 /* wasn't an alias - use what the user typed */
650 linepos
= putone( mp
->m_text
, linepos
, namelen
);
652 /* an alias - expand it */
653 while ((cp
= getname(cp
))) {
654 if (linepos
> MAX_SM_FIELD
) {
655 fprintf (out
, "\n%s: ", name
);
658 mp2
= getm( cp
, NULL
, 0, AD_HOST
, NULL
);
660 mp2
->m_pers
= getcpy(mp
->m_mbox
);
661 linepos
= putone( adrformat(mp2
), linepos
, namelen
);
663 linepos
= putone( mp2
->m_text
, linepos
, namelen
);
668 /* not a local name - use what the user typed */
669 linepos
= putone( mp
->m_text
, linepos
, namelen
);
679 putone (char *adr
, int pos
, int indent
)
687 else if ( linepos
+len
> outputlinelen
) {
688 fprintf ( out
, ",\n%*s", indent
, "");
705 insert_fcc (struct headers
*hdr
, char *pp
)
709 for (cp
= pp
; isspace (*cp
); cp
++)
711 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace (*pp
); pp
--)
719 adios (NULL
, "too many %ss", hdr
->value
);
720 fccfold
[fccind
++] = getcpy (cp
);
735 fd
= mkstemp(bccfil
);
736 if (fd
== -1 || (out
= fdopen(fd
, "w")) == NULL
)
737 adios (bccfil
, "unable to create");
740 if ((out
= fopen (bccfil
, "w")) == NULL
)
741 adios (bccfil
, "unable to create");
743 chmod (bccfil
, 0600);
745 fprintf (out
, "Date: %s\n", dtimenow (0));
746 fprintf (out
, "From: %s\n", signature
);
748 fprintf (out
, "Subject: %s", subject
);
749 fprintf (out
, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
752 if (filter
== NULL
) {
753 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
754 adios (NULL
, "unable to re-open");
755 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
759 vec
[0] = r1bindex (mhlproc
, '/');
761 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
765 adios ("vfork", "unable to");
768 dup2 (fileno (out
), 1);
771 vec
[i
++] = "-forward";
777 execvp (mhlproc
, vec
);
778 adios (mhlproc
, "unable to exec");
781 if (status
= pidwait(child_id
, OK
))
782 admonish (NULL
, "%s lost (status=0%o)", vec
[0], status
);
787 fseek (out
, 0L, SEEK_END
);
788 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
793 /* FCC INTERACTION */
803 for (i
= 0; i
< fccind
; i
++)
805 printf ("Fcc: %s\n", fccfold
[i
]);
807 fcc (path
, fccfold
[i
]);
812 fcc (char *file
, char *folder
)
819 printf ("%sFcc: %s\n", msgstate
== resent
? "Resent-" : "", folder
);
822 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
827 fprintf (stderr
, " %sFcc %s: ",
828 msgstate
== resent
? "Resent-" : "", folder
);
829 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
833 snprintf (fold
, sizeof(fold
), "%s%s",
834 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
835 execlp (fileproc
, r1bindex (fileproc
, '/'),
836 "-link", "-file", file
, fold
, NULL
);
840 if ((status
= pidwait(child_id
, OK
))) {
842 fprintf (stderr
, " %sFcc %s: ",
843 msgstate
== resent
? "Resent-" : "", folder
);
844 fprintf (verbose
? stdout
: stderr
,
845 " errored (0%o)\n", status
);
860 die (char *what
, char *fmt
, ...)
865 advertise (what
, NULL
, fmt
, ap
);