]>
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");
354 if ((out
= fopen (tmpfil
, "w")) == NULL
)
355 adios (tmpfil
, "unable to create");
356 chmod (tmpfil
, 0600);
359 hdrtab
= (msgstate
== normal
) ? NHeaders
: RHeaders
;
361 for (compnum
= 1, state
= FLD
;;) {
362 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
)) {
365 putfmt (name
, buf
, out
);
371 while (state
== FLDPLUS
) {
372 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
375 putfmt (name
, cp
, out
);
380 finish_headers (out
);
381 fprintf (out
, "\n%s", buf
);
383 while (state
== BODY
) {
384 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
390 finish_headers (out
);
395 adios (NULL
, "message format error in component #%d",
399 adios (NULL
, "getfld() returned %d", state
);
405 if (backflg
&& !whomflg
) {
406 strncpy (buf
, m_backup (msg
), sizeof(buf
));
407 if (rename (msg
, buf
) == NOTOK
)
408 advise (buf
, "unable to rename %s to", msg
);
420 * re-open the temp file, unlink it and exec sendmail, giving it
421 * the msg temp file as std in.
423 if ( freopen( tmpfil
, "r", stdin
) == NULL
)
424 adios (tmpfil
, "can't reopen for sendmail");
429 *argp
++ = "send-mail";
430 *argp
++ = "-m"; /* send to me too */
431 *argp
++ = "-t"; /* read msg for recipients */
432 *argp
++ = "-i"; /* don't stop on "." */
435 if (watch
|| verbose
)
439 if (pushflg
&& !(watch
|| verbose
)) {
440 /* fork to a child to run sendmail */
441 for (i
=0; (pid
= vfork()) == NOTOK
&& i
< 5; i
++)
445 fprintf (verbose
? stdout
: stderr
, "%s: can't fork to %s\n",
446 invo_name
, sendmail
);
449 /* we're the child .. */
455 execv ( sendmail
, sargv
);
456 adios ( sendmail
, "can't exec");
457 return 0; /* dead code to satisfy the compiler */
460 /* DRAFT GENERATION */
463 putfmt (char *name
, char *str
, FILE *out
)
469 while (*str
== ' ' || *str
== '\t')
472 if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
473 fprintf (out
, "%s: %s", name
, str
);
478 if (hdr
->flags
& HIGN
)
480 if (hdr
->flags
& HBAD
) {
481 advise (NULL
, "illegal header line -- %s:", name
);
485 msgflags
|= hdr
->set
;
487 if (hdr
->flags
& HSUB
)
488 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
490 if (hdr
->flags
& HFCC
) {
491 if ((cp
= strrchr(str
, '\n')))
493 for (cp
= pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
495 insert_fcc (hdr
, pp
);
497 insert_fcc (hdr
, pp
);
502 if (hdr
->flags
& HBCC
) {
508 if (*str
!= '\n' && *str
!= '\0') {
509 if (aliasflg
&& hdr
->flags
& HTRY
) {
510 /* this header contains address(es) that we have to do
511 * alias expansion on. Because of the saved state in
512 * getname we have to put all the addresses into a list.
513 * We then let putadr munch on that list, possibly
516 register struct mailname
*f
= 0;
517 register struct mailname
*mp
= 0;
519 while ((cp
= getname(str
))) {
520 mp
= getm( cp
, NULL
, 0, AD_HOST
, NULL
);
525 mp
->m_next
= f
->m_next
;
530 f
= mp
->m_next
; mp
->m_next
= 0;
533 fprintf (out
, "%s: %s", name
, str
);
545 strncpy(from
, getusername(), sizeof(from
));
547 if ((cp
= getfullname ()) && *cp
) {
548 strncpy (sigbuf
, cp
, sizeof(sigbuf
));
549 snprintf (signature
, sizeof(signature
), "%s <%s>", sigbuf
, from
);
552 snprintf (signature
, sizeof(signature
), "%s", from
);
557 finish_headers (FILE *out
)
561 if (!(msgflags
& MDAT
))
562 fprintf (out
, "Date: %s\n", dtimenow (0));
564 fprintf (out
, "Sender: %s\n", from
);
566 fprintf (out
, "From: %s\n", signature
);
568 if (!(msgflags
& MVIS
))
569 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
574 if (!(msgflags
& MRDT
))
575 fprintf (out
, "Resent-Date: %s\n", dtimenow(0));
577 fprintf (out
, "Resent-Sender: %s\n", from
);
579 fprintf (out
, "Resent-From: %s\n", signature
);
581 if (!(msgflags
& MVIS
))
582 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
588 adios (NULL
, "re-format message and try again");
593 get_header (char *header
, struct headers
*table
)
597 for (h
= table
; h
->value
; h
++)
598 if (!strcasecmp (header
, h
->value
))
606 * output the address list for header "name". The address list
607 * is a linked list of mailname structs. "nl" points to the head
608 * of the list. Alias substitution should be done on nl.
611 putadr (char *name
, struct mailname
*nl
)
613 register struct mailname
*mp
, *mp2
;
614 register int linepos
;
618 fprintf (out
, "%s: ", name
);
619 namelen
= strlen(name
) + 2;
622 for (mp
= nl
; mp
; ) {
623 if (linepos
> MAX_SM_FIELD
) {
624 fprintf (out
, "\n%s: ", name
);
628 /* a local name - see if it's an alias */
629 cp
= akvalue(mp
->m_mbox
);
630 if (cp
== mp
->m_mbox
)
631 /* wasn't an alias - use what the user typed */
632 linepos
= putone( mp
->m_text
, linepos
, namelen
);
634 /* an alias - expand it */
635 while ((cp
= getname(cp
))) {
636 if (linepos
> MAX_SM_FIELD
) {
637 fprintf (out
, "\n%s: ", name
);
640 mp2
= getm( cp
, NULL
, 0, AD_HOST
, NULL
);
642 mp2
->m_pers
= getcpy(mp
->m_mbox
);
643 linepos
= putone( adrformat(mp2
), linepos
, namelen
);
645 linepos
= putone( mp2
->m_text
, linepos
, namelen
);
650 /* not a local name - use what the user typed */
651 linepos
= putone( mp
->m_text
, linepos
, namelen
);
661 putone (char *adr
, int pos
, int indent
)
669 else if ( linepos
+len
> outputlinelen
) {
670 fprintf ( out
, ",\n%*s", indent
, "");
687 insert_fcc (struct headers
*hdr
, char *pp
)
691 for (cp
= pp
; isspace (*cp
); cp
++)
693 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace (*pp
); pp
--)
701 adios (NULL
, "too many %ss", hdr
->value
);
702 fccfold
[fccind
++] = getcpy (cp
);
717 if ((out
= fopen (bccfil
, "w")) == NULL
)
718 adios (bccfil
, "unable to create");
719 chmod (bccfil
, 0600);
721 fprintf (out
, "Date: %s\n", dtimenow (0));
722 fprintf (out
, "From: %s\n", signature
);
724 fprintf (out
, "Subject: %s", subject
);
725 fprintf (out
, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
728 if (filter
== NULL
) {
729 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
730 adios (NULL
, "unable to re-open");
731 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
735 vec
[0] = r1bindex (mhlproc
, '/');
737 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
741 adios ("vfork", "unable to");
744 dup2 (fileno (out
), 1);
747 vec
[i
++] = "-forward";
753 execvp (mhlproc
, vec
);
754 adios (mhlproc
, "unable to exec");
757 if (status
= pidwait(child_id
, OK
))
758 admonish (NULL
, "%s lost (status=0%o)", vec
[0], status
);
763 fseek (out
, 0L, SEEK_END
);
764 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
769 /* FCC INTERACTION */
779 for (i
= 0; i
< fccind
; i
++)
781 printf ("Fcc: %s\n", fccfold
[i
]);
783 fcc (path
, fccfold
[i
]);
788 fcc (char *file
, char *folder
)
795 printf ("%sFcc: %s\n", msgstate
== resent
? "Resent-" : "", folder
);
798 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
803 fprintf (stderr
, " %sFcc %s: ",
804 msgstate
== resent
? "Resent-" : "", folder
);
805 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
809 snprintf (fold
, sizeof(fold
), "%s%s",
810 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
811 execlp (fileproc
, r1bindex (fileproc
, '/'),
812 "-link", "-file", file
, fold
, NULL
);
816 if ((status
= pidwait(child_id
, OK
))) {
818 fprintf (stderr
, " %sFcc %s: ",
819 msgstate
== resent
? "Resent-" : "", folder
);
820 fprintf (verbose
? stdout
: stderr
,
821 " errored (0%o)\n", status
);
836 die (char *what
, char *fmt
, ...)
841 advertise (what
, NULL
, fmt
, ap
);