static char sccsid
[] = "@(#)spost.c 1.6 (Berkeley) 11/2/85";
static char ident
[] = "@(#)$Id: spost.c,v 1.8 1992/12/15 00:20:22 jromine Exp $";
/* spost.c - feed messages to sendmail */
* (This is a simpler, faster, replacement for "post" for use when "sendmail"
* is the transport system)
#include "../h/addrsbr.h"
#include "../h/aliasbr.h"
#include "../h/dropsbr.h"
#include "../zotnet/tws.h"
extern char *getfullname (), *getusr ();
#define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : c)
#define MAX_SM_FIELD 1476 /* < largest hdr field sendmail will accept */
#define FCCS 10 /* max number of fccs allowed */
struct swit switches
[] = {
"dist", -4, /* interface from dist */
"check", -5, /* interface from whom */
"nocheck", -7, /* interface from whom */
"whom", -4, /* interface from whom */
#define PUSHSW 18 /* fork to sendmail then exit */
#define NPUSHSW 19 /* exec sendmail */
#define HNOP 0x0000 /* just used to keep .set around */
#define HBAD 0x0001 /* bad header - don't let it through */
#define HADR 0x0002 /* header has an address field */
#define HSUB 0x0004 /* Subject: header */
#define HTRY 0x0008 /* try to send to addrs on header */
#define HBCC 0x0010 /* don't output this header */
#define HMNG 0x0020 /* mung this header */
#define HNGR 0x0040 /* no groups allowed in this header */
#define HFCC 0x0080 /* FCC: type header */
#define HNIL 0x0100 /* okay for this header not to have addrs */
#define HIGN 0x0200 /* ignore this header */
#define MFRM 0x0001 /* we've seen a From: */
#define MDAT 0x0002 /* we've seen a Date: */
#define MRFM 0x0004 /* we've seen a Resent-From: */
#define MVIS 0x0008 /* we've seen sighted addrs */
#define MINV 0x0010 /* we've seen blind addrs */
#define MRDT 0x0020 /* we've seen a Resent-Date: */
static struct headers NHeaders
[] = {
"Reply-To", HADR
| HNGR
, 0,
"From", HADR
| HNGR
, MFRM
,
"Sender", HADR
| HBAD
, 0,
"Bcc", HADR
| HTRY
| HBCC
| HNIL
, MINV
,
static struct headers RHeaders
[] = {
"Resent-Reply-To", HADR
| HNGR
, 0,
"Resent-From", HADR
| HNGR
, MRFM
,
"Resent-Sender", HADR
| HBAD
, 0,
"Resent-Date", HNOP
, MRDT
,
"Resent-Subject", HSUB
, 0,
"Resent-To", HADR
| HTRY
, MVIS
,
"Resent-cc", HADR
| HTRY
, MVIS
,
"Resent-Bcc", HADR
| HTRY
| HBCC
, MINV
,
"Resent-Message-Id", HBAD
, 0,
static short fccind
= 0; /* index into fccfold[] */
static int badmsg
= 0; /* message has bad semantics */
static int verbose
= 0; /* spell it out */
static int debug
= 0; /* debugging post */
static int rmflg
= 1; /* remove temporary file when done */
static int watch
= 0; /* watch the delivery process */
static int backflg
= 0; /* rename input file as *.bak when done */
static int whomflg
= 0; /* if just checking addresses */
static int pushflg
= 0; /* if going to fork to sendmail */
static int aliasflg
= -1; /* if going to process aliases */
static int outputlinelen
=72;
static unsigned msgflags
= 0; /* what we've seen */
static char tmpfil
[] = "/tmp/pstXXXXXX";
static char from
[BUFSIZ
]; /* my network address */
static char signature
[BUFSIZ
]; /* my signature */
static char *filter
= NULL
; /* the filter for BCC'ing */
static char *subject
= NULL
; /* the subject field for BCC'ing */
static char *fccfold
[FCCS
]; /* foldernames for FCC'ing */
static struct headers
*hdrtab
; /* table for the message we're doing */
static FILE *out
; /* output (temp) file */
static putfmt(), start_headers(), finish_headers(), putadr(),
static int get_header(), putone();
invo_name
= r1bindex (argv
[0], '/');
if ((cp
= m_find (invo_name
)) != NULL
) {
argp
= copyip (brkstring (cp
, " ", "\n"), arguments
);
(void) copyip (argv
+1, argp
);
switch (smatch (++cp
, switches
)) {
adios (NULLCP
, "-%s unknown", cp
);
(void)sprintf (buf
, "%s [switches] file", invo_name
);
if (!(filter
= *argp
++) || *filter
== '-')
adios (NULLCP
, "missing argument to %s", argp
[-2]);
if (!(cp
= *argp
++) || *cp
== '-')
adios (NULLCP
, "missing argument to %s", argp
[-2]);
(void) alias (AliasFile
);/* load default aka's */
if ((state
= alias(cp
)) != AK_OK
)
adios (NULLCP
, "aliasing error in file %s - %s",
if (!(cp
= *argp
++) || *cp
== '-')
adios (NULLCP
, "missing argument to %s", argp
[-2]);
outputlinelen
= atoi (cp
);
/* -library & -idanno switch ignored */
if (!(cp
= *argp
++) || *cp
== '-')
adios (NULLCP
, "missing argument to %s", argp
[-2]);
adios (NULLCP
, "only one message at a time!");
(void) alias (AliasFile
); /* load default aka's */
adios (NULLCP
, "usage: %s [switches] file", invo_name
);
if ((in
= fopen (msg
, "r")) == NULL
)
adios (msg
, "unable to open");
if ((out
= fopen (tmpfil
, "w")) == NULL
)
adios (tmpfil
, "unable to create");
(void)chmod (tmpfil
, 0600);
hdrtab
= (msgstate
== normal
) ? NHeaders
: RHeaders
;
for (compnum
= 1, state
= FLD
;;) {
switch (state
= m_getfld (state
, name
, buf
, sizeof buf
, in
)) {
while (state
== FLDPLUS
) {
state
= m_getfld (state
, name
, buf
, sizeof buf
, in
);
fprintf (out
, "\n%s", buf
);
state
= m_getfld (state
, name
, buf
, sizeof buf
, in
);
adios (NULLCP
, "message format error in component #%d",
adios (NULLCP
, "getfld() returned %d", state
);
if (backflg
&& !whomflg
) {
(void) strcpy (buf
, m_backup (msg
));
if (rename (msg
, buf
) == NOTOK
)
advise (buf
, "unable to rename %s to", msg
);
* re-open the temp file, unlink it and exec sendmail, giving it
* the msg temp file as std in.
if ( freopen( tmpfil
, "r", stdin
) == NULL
)
adios (tmpfil
, "can't reopen for sendmail");
*argp
++ = "-m"; /* send to me too */
*argp
++ = "-t"; /* read msg for recipients */
*argp
++ = "-i"; /* don't stop on "." */
if (pushflg
&& !(watch
|| verbose
)) {
/* fork to a child to run sendmail */
for (i
=0; (pid
= vfork()) == NOTOK
&& i
< 5; i
++)
fprintf (verbose
? stdout
: stderr
, "%s: can't fork to %s\n",
execv ( sendmail
, sargv
);
adios ( sendmail
, "can't exec");
/* \f DRAFT GENERATION */
static putfmt (name
, str
, out
)
while (*str
== ' ' || *str
== '\t')
if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
fprintf (out
, "%s: %s", name
, str
);
if (hdr
-> flags
& HBAD
) {
advise (NULLCP
, "illegal header line -- %s:", name
);
subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
if (hdr
-> flags
& HFCC
) {
if (cp
= rindex (str
, '\n'))
for (cp
= pp
= str
; cp
= index (pp
, ','); pp
= cp
) {
if (hdr
-> flags
& HBCC
) {
if (*str
!= '\n' && *str
!= '\0')
if (aliasflg
&& hdr
->flags
& HTRY
) {
/* this header contains address(es) that we have to do
* alias expansion on. Because of the saved state in
* getname we have to put all the addresses into a list.
* We then let putadr munch on that list, possibly
register struct mailname
*f
= 0;
register struct mailname
*mp
= 0;
while (cp
= getname( str
) ) {
mp
= getm( cp
, NULLCP
, 0, AD_HOST
, NULLCP
);
f
= mp
->m_next
; mp
->m_next
= 0;
fprintf (out
, "%s: %s", name
, str
);
(void)strcpy( from
, getusr() );
if ((cp
= getfullname ()) && *cp
) {
(void)strcpy (sigbuf
, cp
);
(void)sprintf (signature
, "%s <%s>", sigbuf
, from
);
(void)sprintf (signature
, "%s", from
);
fprintf (out
, "Date: %s\n", dtimenow ());
fprintf (out
, "Sender: %s\n", from
);
fprintf (out
, "From: %s\n", signature
);
fprintf (out
, "Bcc: Blind Distribution List: ;\n");
fprintf (out
, "Resent-Date: %s\n", dtimenow());
fprintf (out
, "Resent-Sender: %s\n", from
);
fprintf (out
, "Resent-From: %s\n", signature
);
fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
adios (NULLCP
, "re-format message and try again");
get_header (header
, table
)
for (h
= table
; h
-> value
; h
++)
if (uleq (header
, h
-> value
))
/* output the address list for header "name". The address list
* is a linked list of mailname structs. "nl" points to the head
* of the list. Alias substitution should be done on nl.
register struct mailname
*mp
, *mp2
;
fprintf (out
, "%s: ", name
);
namelen
= strlen(name
) + 2;
if (linepos
> MAX_SM_FIELD
) {
fprintf (out
, "\n%s: ", name
);
/* a local name - see if it's an alias */
cp
= akvalue(mp
->m_mbox
);
/* wasn't an alias - use what the user typed */
linepos
= putone( mp
->m_text
, linepos
, namelen
);
/* an alias - expand it */
while (cp
= getname(cp
) ) {
if (linepos
> MAX_SM_FIELD
) {
fprintf (out
, "\n%s: ", name
);
mp2
= getm( cp
, NULLCP
, 0, AD_HOST
, NULLCP
);
mp2
->m_pers
= getcpy(mp
->m_mbox
);
linepos
= putone( adrformat(mp2
), linepos
, namelen
);
linepos
= putone( mp2
->m_text
, linepos
, namelen
);
/* not a local name - use what the user typed */
linepos
= putone( mp
->m_text
, linepos
, namelen
);
static int putone ( adr
, pos
, indent
)
else if ( linepos
+len
> outputlinelen
) {
fprintf ( out
, ",\n%*s", indent
, "");
static insert_fcc (hdr
, pp
)
for (cp
= pp
; isspace (*cp
); cp
++)
for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace (*pp
); pp
--)
adios (NULLCP
, "too many %ss", hdr
-> value
);
fccfold
[fccind
++] = getcpy (cp
);
static make_bcc_file () {
if ((out
= fopen (bccfil
, "w")) == NULL
)
adios (bccfil
, "unable to create");
(void)chmod (bccfil
, 0600);
fprintf (out
, "Date: %s\n", dtimenow ());
fprintf (out
, "From: %s\n", signature
);
fprintf (out
, "Subject: %s", subject
);
fprintf (out
, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
if ((fd
= open (tmpfil
, 0)) == NOTOK
)
adios (NULLCP
, "unable to re-open");
cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
vec
[0] = r1bindex (mhlproc
, '/');
for (i
= 0; (child_id
= vfork ()) == NOTOK
&& i
< 5; i
++)
adios ("vfork", "unable to");
adios (mhlproc
, "unable to exec");
if (status
= pidwait (child_id
, OK
))
admonish (NULL
, "%s lost (status=0%o)", vec
[0], status
);
fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
for (i
= 0; i
< fccind
; i
++)
printf ("Fcc: %s\n", fccfold
[i
]);
static fcc (file
, folder
)
printf ("%sFcc: %s\n", msgstate
== resent
? "Resent-" : "", folder
);
for (i
= 0; (child_id
= vfork ()) == NOTOK
&& i
< 5; i
++)
fprintf (stderr
, " %sFcc %s: ",
msgstate
== resent
? "Resent-" : "", folder
);
fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
(void)sprintf (fold
, "%s%s",
*folder
== '+' || *folder
== '@' ? "" : "+", folder
);
execlp (fileproc
, r1bindex (fileproc
, '/'),
"-link", "-file", file
, fold
, NULL
);
if (status
= pidwait (child_id
, OK
)) {
fprintf (stderr
, " %sFcc %s: ",
msgstate
== resent
? "Resent-" : "", folder
);
fprintf (verbose
? stdout
: stderr
,
" errored (0%o)\n", status
);
static die (what
, fmt
, a
, b
, c
, d
)
adios (what
, fmt
, a
, b
, c
, d
);