]>
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.
13 #include <h/addrsbr.h>
14 #include <h/aliasbr.h>
15 #include <h/dropsbr.h>
16 #include <zotnet/tws/tws.h>
18 #define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : c)
20 #define MAX_SM_FIELD 1476 /* < largest hdr field sendmail will accept */
21 #define FCCS 10 /* max number of fccs allowed */
23 struct swit switches
[] = {
25 { "filter filterfile", 0 },
49 { "alias aliasfile", 0 },
53 { "width columns", 0 },
61 { "dist", -4 }, /* interface from dist */
63 { "check", -5 }, /* interface from whom */
65 { "nocheck", -7 }, /* interface from whom */
67 { "whom", -4 }, /* interface from whom */
68 #define PUSHSW 22 /* fork to sendmail then exit */
70 #define NPUSHSW 23 /* exec sendmail */
73 { "library directory", -7 },
75 { "idanno number", -6 },
80 /* flags for headers->flags */
81 #define HNOP 0x0000 /* just used to keep .set around */
82 #define HBAD 0x0001 /* bad header - don't let it through */
83 #define HADR 0x0002 /* header has an address field */
84 #define HSUB 0x0004 /* Subject: header */
85 #define HTRY 0x0008 /* try to send to addrs on header */
86 #define HBCC 0x0010 /* don't output this header */
87 #define HMNG 0x0020 /* mung this header */
88 #define HNGR 0x0040 /* no groups allowed in this header */
89 #define HFCC 0x0080 /* FCC: type header */
90 #define HNIL 0x0100 /* okay for this header not to have addrs */
91 #define HIGN 0x0200 /* ignore this header */
93 /* flags for headers->set */
94 #define MFRM 0x0001 /* we've seen a From: */
95 #define MDAT 0x0002 /* we've seen a Date: */
96 #define MRFM 0x0004 /* we've seen a Resent-From: */
97 #define MVIS 0x0008 /* we've seen sighted addrs */
98 #define MINV 0x0010 /* we've seen blind addrs */
99 #define MRDT 0x0020 /* we've seen a Resent-Date: */
108 static struct headers NHeaders
[] = {
109 { "Return-Path", HBAD
, 0 },
110 { "Received", HBAD
, 0 },
111 { "Reply-To", HADR
|HNGR
, 0 },
112 { "From", HADR
|HNGR
, MFRM
},
113 { "Sender", HADR
|HBAD
, 0 },
114 { "Date", HNOP
, MDAT
},
115 { "Subject", HSUB
, 0 },
116 { "To", HADR
|HTRY
, MVIS
},
117 { "cc", HADR
|HTRY
, MVIS
},
118 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, MINV
},
119 { "Message-Id", HBAD
, 0 },
124 static struct headers RHeaders
[] = {
125 { "Resent-Reply-To", HADR
|HNGR
, 0 },
126 { "Resent-From", HADR
|HNGR
, MRFM
},
127 { "Resent-Sender", HADR
|HBAD
, 0 },
128 { "Resent-Date", HNOP
, MRDT
},
129 { "Resent-Subject", HSUB
, 0 },
130 { "Resent-To", HADR
|HTRY
, MVIS
},
131 { "Resent-cc", HADR
|HTRY
, MVIS
},
132 { "Resent-Bcc", HADR
|HTRY
|HBCC
, MINV
},
133 { "Resent-Message-Id", HBAD
, 0 },
134 { "Resent-Fcc", HFCC
, 0 },
135 { "Reply-To", HADR
, 0 },
141 static short fccind
= 0; /* index into fccfold[] */
143 static int badmsg
= 0; /* message has bad semantics */
144 static int verbose
= 0; /* spell it out */
145 static int debug
= 0; /* debugging post */
146 static int rmflg
= 1; /* remove temporary file when done */
147 static int watch
= 0; /* watch the delivery process */
148 static int backflg
= 0; /* rename input file as *.bak when done */
149 static int whomflg
= 0; /* if just checking addresses */
150 static int pushflg
= 0; /* if going to fork to sendmail */
151 static int aliasflg
= -1; /* if going to process aliases */
152 static int outputlinelen
=72;
154 static unsigned msgflags
= 0; /* what we've seen */
160 static char tmpfil
[] = "/tmp/pstXXXXXX";
162 static char from
[BUFSIZ
]; /* my network address */
163 static char signature
[BUFSIZ
]; /* my signature */
164 static char *filter
= NULL
; /* the filter for BCC'ing */
165 static char *subject
= NULL
; /* the subject field for BCC'ing */
166 static char *fccfold
[FCCS
]; /* foldernames for FCC'ing */
168 static struct headers
*hdrtab
; /* table for the message we're doing */
169 static FILE *out
; /* output (temp) file */
171 extern char *sendmail
;
174 * external prototypes
176 extern char *getfullname (void);
177 extern char *getusername (void);
182 static void putfmt (char *, char *, FILE *);
183 static void start_headers (void);
184 static void finish_headers (FILE *);
185 static int get_header (char *, struct headers
*);
186 static void putadr (char *, struct mailname
*);
187 static int putone (char *, int, int);
188 static void insert_fcc (struct headers
*, char *);
189 static void file (char *);
190 static void fcc (char *, char *);
193 static void die (char *, char *, ...);
194 static void make_bcc_file (void);
199 main (int argc
, char **argv
)
201 int state
, i
, pid
, compnum
;
202 char *cp
, *msg
= NULL
, **argp
, **arguments
;
203 char *sargv
[16], buf
[BUFSIZ
], name
[NAMESZ
];
207 setlocale(LC_ALL
, "");
209 invo_name
= r1bindex (argv
[0], '/');
211 /* foil search of user profile/context */
212 if (context_foil (NULL
) == -1)
215 mts_init (invo_name
);
216 arguments
= getarguments (invo_name
, argc
, argv
, 0);
219 while ((cp
= *argp
++)) {
221 switch (smatch (++cp
, switches
)) {
223 ambigsw (cp
, switches
);
226 adios (NULL
, "-%s unknown", cp
);
229 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
230 print_help (buf
, switches
, 1);
233 print_version(invo_name
);
249 if (!(filter
= *argp
++) || *filter
== '-')
250 adios (NULL
, "missing argument to %s", argp
[-2]);
292 if (!(cp
= *argp
++) || *cp
== '-')
293 adios (NULL
, "missing argument to %s", argp
[-2]);
295 alias (AliasFile
);/* load default aka's */
297 if ((state
= alias(cp
)) != AK_OK
)
298 adios (NULL
, "aliasing error in file %s - %s",
299 cp
, akerror(state
) );
306 if (!(cp
= *argp
++) || *cp
== '-')
307 adios (NULL
, "missing argument to %s", argp
[-2]);
308 outputlinelen
= atoi (cp
);
309 if (outputlinelen
<= 10)
314 if (!(cp
= *argp
++) || *cp
== '-')
315 adios (NULL
, "missing argument to %s", argp
[-2]);
316 /* create a minimal context */
317 if (context_foil (cp
) == -1)
322 /* -idanno switch ignored */
323 if (!(cp
= *argp
++) || *cp
== '-')
324 adios (NULL
, "missing argument to %s", argp
[-2]);
329 adios (NULL
, "only one message at a time!");
335 alias (AliasFile
); /* load default aka's */
338 adios (NULL
, "usage: %s [switches] file", invo_name
);
340 if ((in
= fopen (msg
, "r")) == NULL
)
341 adios (msg
, "unable to open");
350 if ((out
= fdopen( mkstemp (tmpfil
), "w" )) == NULL
)
351 adios (tmpfil
, "unable to create");
354 if ((out
= fopen (tmpfil
, "w")) == NULL
)
355 adios (tmpfil
, "unable to create");
356 chmod (tmpfil
, 0600);
360 hdrtab
= (msgstate
== normal
) ? NHeaders
: RHeaders
;
362 for (compnum
= 1, state
= FLD
;;) {
363 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
)) {
366 putfmt (name
, buf
, out
);
372 while (state
== FLDPLUS
) {
373 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
376 putfmt (name
, cp
, out
);
381 finish_headers (out
);
382 fprintf (out
, "\n%s", buf
);
384 while (state
== BODY
) {
385 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
391 finish_headers (out
);
396 adios (NULL
, "message format error in component #%d",
400 adios (NULL
, "getfld() returned %d", state
);
406 if (backflg
&& !whomflg
) {
407 strncpy (buf
, m_backup (msg
), sizeof(buf
));
408 if (rename (msg
, buf
) == NOTOK
)
409 advise (buf
, "unable to rename %s to", msg
);
421 * re-open the temp file, unlink it and exec sendmail, giving it
422 * the msg temp file as std in.
424 if ( freopen( tmpfil
, "r", stdin
) == NULL
)
425 adios (tmpfil
, "can't reopen for sendmail");
430 *argp
++ = "send-mail";
431 *argp
++ = "-m"; /* send to me too */
432 *argp
++ = "-t"; /* read msg for recipients */
433 *argp
++ = "-i"; /* don't stop on "." */
436 if (watch
|| verbose
)
440 if (pushflg
&& !(watch
|| verbose
)) {
441 /* fork to a child to run sendmail */
442 for (i
=0; (pid
= vfork()) == NOTOK
&& i
< 5; i
++)
446 fprintf (verbose
? stdout
: stderr
, "%s: can't fork to %s\n",
447 invo_name
, sendmail
);
450 /* we're the child .. */
456 execv ( sendmail
, sargv
);
457 adios ( sendmail
, "can't exec");
458 return 0; /* dead code to satisfy the compiler */
461 /* DRAFT GENERATION */
464 putfmt (char *name
, char *str
, FILE *out
)
470 while (*str
== ' ' || *str
== '\t')
473 if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
474 fprintf (out
, "%s: %s", name
, str
);
479 if (hdr
->flags
& HIGN
)
481 if (hdr
->flags
& HBAD
) {
482 advise (NULL
, "illegal header line -- %s:", name
);
486 msgflags
|= hdr
->set
;
488 if (hdr
->flags
& HSUB
)
489 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
491 if (hdr
->flags
& HFCC
) {
492 if ((cp
= strrchr(str
, '\n')))
494 for (cp
= pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
496 insert_fcc (hdr
, pp
);
498 insert_fcc (hdr
, pp
);
503 if (hdr
->flags
& HBCC
) {
509 if (*str
!= '\n' && *str
!= '\0') {
510 if (aliasflg
&& hdr
->flags
& HTRY
) {
511 /* this header contains address(es) that we have to do
512 * alias expansion on. Because of the saved state in
513 * getname we have to put all the addresses into a list.
514 * We then let putadr munch on that list, possibly
517 register struct mailname
*f
= 0;
518 register struct mailname
*mp
= 0;
520 while ((cp
= getname(str
))) {
521 mp
= getm( cp
, NULL
, 0, AD_HOST
, NULL
);
526 mp
->m_next
= f
->m_next
;
531 f
= mp
->m_next
; mp
->m_next
= 0;
534 fprintf (out
, "%s: %s", name
, str
);
546 strncpy(from
, getusername(), sizeof(from
));
548 if ((cp
= getfullname ()) && *cp
) {
549 strncpy (sigbuf
, cp
, sizeof(sigbuf
));
550 snprintf (signature
, sizeof(signature
), "%s <%s>", sigbuf
, from
);
553 snprintf (signature
, sizeof(signature
), "%s", from
);
558 finish_headers (FILE *out
)
562 if (!(msgflags
& MDAT
))
563 fprintf (out
, "Date: %s\n", dtimenow (0));
565 fprintf (out
, "Sender: %s\n", from
);
567 fprintf (out
, "From: %s\n", signature
);
569 if (!(msgflags
& MVIS
))
570 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
575 if (!(msgflags
& MRDT
))
576 fprintf (out
, "Resent-Date: %s\n", dtimenow(0));
578 fprintf (out
, "Resent-Sender: %s\n", from
);
580 fprintf (out
, "Resent-From: %s\n", signature
);
582 if (!(msgflags
& MVIS
))
583 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
589 adios (NULL
, "re-format message and try again");
594 get_header (char *header
, struct headers
*table
)
598 for (h
= table
; h
->value
; h
++)
599 if (!strcasecmp (header
, h
->value
))
607 * output the address list for header "name". The address list
608 * is a linked list of mailname structs. "nl" points to the head
609 * of the list. Alias substitution should be done on nl.
612 putadr (char *name
, struct mailname
*nl
)
614 register struct mailname
*mp
, *mp2
;
615 register int linepos
;
619 fprintf (out
, "%s: ", name
);
620 namelen
= strlen(name
) + 2;
623 for (mp
= nl
; mp
; ) {
624 if (linepos
> MAX_SM_FIELD
) {
625 fprintf (out
, "\n%s: ", name
);
629 /* a local name - see if it's an alias */
630 cp
= akvalue(mp
->m_mbox
);
631 if (cp
== mp
->m_mbox
)
632 /* wasn't an alias - use what the user typed */
633 linepos
= putone( mp
->m_text
, linepos
, namelen
);
635 /* an alias - expand it */
636 while ((cp
= getname(cp
))) {
637 if (linepos
> MAX_SM_FIELD
) {
638 fprintf (out
, "\n%s: ", name
);
641 mp2
= getm( cp
, NULL
, 0, AD_HOST
, NULL
);
643 mp2
->m_pers
= getcpy(mp
->m_mbox
);
644 linepos
= putone( adrformat(mp2
), linepos
, namelen
);
646 linepos
= putone( mp2
->m_text
, linepos
, namelen
);
651 /* not a local name - use what the user typed */
652 linepos
= putone( mp
->m_text
, linepos
, namelen
);
662 putone (char *adr
, int pos
, int indent
)
670 else if ( linepos
+len
> outputlinelen
) {
671 fprintf ( out
, ",\n%*s", indent
, "");
688 insert_fcc (struct headers
*hdr
, char *pp
)
692 for (cp
= pp
; isspace (*cp
); cp
++)
694 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace (*pp
); pp
--)
702 adios (NULL
, "too many %ss", hdr
->value
);
703 fccfold
[fccind
++] = getcpy (cp
);
718 if ((out
= fopen (bccfil
, "w")) == NULL
)
719 adios (bccfil
, "unable to create");
720 chmod (bccfil
, 0600);
722 fprintf (out
, "Date: %s\n", dtimenow (0));
723 fprintf (out
, "From: %s\n", signature
);
725 fprintf (out
, "Subject: %s", subject
);
726 fprintf (out
, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
729 if (filter
== NULL
) {
730 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
731 adios (NULL
, "unable to re-open");
732 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
736 vec
[0] = r1bindex (mhlproc
, '/');
738 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
742 adios ("vfork", "unable to");
745 dup2 (fileno (out
), 1);
748 vec
[i
++] = "-forward";
754 execvp (mhlproc
, vec
);
755 adios (mhlproc
, "unable to exec");
758 if (status
= pidwait(child_id
, OK
))
759 admonish (NULL
, "%s lost (status=0%o)", vec
[0], status
);
764 fseek (out
, 0L, SEEK_END
);
765 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
770 /* FCC INTERACTION */
780 for (i
= 0; i
< fccind
; i
++)
782 printf ("Fcc: %s\n", fccfold
[i
]);
784 fcc (path
, fccfold
[i
]);
789 fcc (char *file
, char *folder
)
796 printf ("%sFcc: %s\n", msgstate
== resent
? "Resent-" : "", folder
);
799 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
804 fprintf (stderr
, " %sFcc %s: ",
805 msgstate
== resent
? "Resent-" : "", folder
);
806 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
810 snprintf (fold
, sizeof(fold
), "%s%s",
811 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
812 execlp (fileproc
, r1bindex (fileproc
, '/'),
813 "-link", "-file", file
, fold
, NULL
);
817 if ((status
= pidwait(child_id
, OK
))) {
819 fprintf (stderr
, " %sFcc %s: ",
820 msgstate
== resent
? "Resent-" : "", folder
);
821 fprintf (verbose
? stdout
: stderr
,
822 " errored (0%o)\n", status
);
837 die (char *what
, char *fmt
, ...)
842 advertise (what
, NULL
, fmt
, ap
);