X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/cff8de7654cf393a494b01a587bd4d503d15a5e3..ec173fd2c:/uip/rcvdist.c?ds=sidebyside diff --git a/uip/rcvdist.c b/uip/rcvdist.c index 8ff0365b..fcf9dd77 100644 --- a/uip/rcvdist.c +++ b/uip/rcvdist.c @@ -1,28 +1,92 @@ - -/* - * rcvdist.c -- asynchronously redistribute messages +/* rcvdist.c -- asynchronously redistribute messages * * This code is Copyright (c) 2002, by the authors of nmh. See the * COPYRIGHT file in the root directory of the nmh distribution for * complete copyright information. */ -#include -#include -#include -#include -#include -#include - -static struct swit switches[] = { -#define FORMSW 0 - { "form formfile", 4 }, -#define VERSIONSW 1 - { "version", 0 }, -#define HELPSW 2 - { "help", 0 }, - { NULL, 0 } -}; +#include "h/mh.h" +#include "sbr/fmt_new.h" +#include "distsbr.h" +#include "sbr/m_gmprot.h" +#include "sbr/m_getfld.h" +#include "sbr/getarguments.h" +#include "sbr/smatch.h" +#include "sbr/cpydata.h" +#include "sbr/context_find.h" +#include "sbr/ambigsw.h" +#include "sbr/pidstatus.h" +#include "sbr/print_version.h" +#include "sbr/print_help.h" +#include "sbr/arglist.h" +#include "sbr/error.h" +#include "h/fmt_scan.h" +#include "h/tws.h" +#include "h/mts.h" +#include "h/done.h" +#include "h/utils.h" +#include "sbr/m_mktemp.h" + +#define RCVDIST_SWITCHES \ + X("form formfile", 4, FORMSW) \ + X("version", 0, VERSIONSW) \ + X("help", 0, HELPSW) \ + +#define X(sw, minchars, id) id, +DEFINE_SWITCH_ENUM(RCVDIST); +#undef X + +#define X(sw, minchars, id) { sw, minchars, id }, +DEFINE_SWITCH_ARRAY(RCVDIST, switches); +#undef X + +#ifndef CYRUS_SASL +# define SASLminc(a) (a) +#else /* CYRUS_SASL */ +# define SASLminc(a) 0 +#endif /* CYRUS_SASL */ + +#ifndef OAUTH_SUPPORT +# define OAUTHminc(a) (a) +#else /* OAUTH_SUPPORT */ +# define OAUTHminc(a) 0 +#endif /* OAUTH_SUPPORT */ + +/* These are just the post(1) switches that take an argument. */ +#define POST_SWITCHES \ + X("alias aliasfile", 0, ALIASW) \ + X("filter filterfile", 0, FILTSW) \ + X("library directory", -7, LIBSW) /* interface from send, whom */ \ + X("width columns", 0, WIDTHSW) \ + X("idanno number", -6, ANNOSW) /* interface from send */ \ + X("client host", -6, CLIESW) \ + X("server host", 6, SERVSW) /* specify alternate SMTP server */ \ + X("partno", -6, PARTSW) \ + X("saslmech", SASLminc(5), SASLMECHSW) \ + X("user", SASLminc(-4), USERSW) \ + X("port server submission port name/number", 4, PORTSW) \ + X("fileproc", -4, FILEPROCSW) \ + X("mhlproc", -3, MHLPROCSW) \ + X("sendmail program", 0, MTSSM) \ + X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \ + X("credentials legacy|file:filename", 0, CREDENTIALSSW) \ + X("messageid localname|random", 2, MESSAGEIDSW) \ + X("authservice auth-service-name", OAUTHminc(-11), AUTHSERVICESW) \ + X("oauthcredfile credential-file", OAUTHminc(-7), OAUTHCREDFILESW) \ + X("oauthclientid client-id", OAUTHminc(-12), OAUTHCLIDSW) \ + X("oauthclientsecret client-secret", OAUTHminc(-12), OAUTHCLSECSW) \ + X("oauthauthendpoint authentication-endpoint", OAUTHminc(-6), OAUTHAUTHENDSW) \ + X("oauthredirect redirect-uri", OAUTHminc(-6), OAUTHREDIRSW) \ + X("oauthtokenendpoint token-endpoint", OAUTHminc(-6), OAUTHTOKENDSW) \ + X("oauthscope scope", OAUTHminc(-6), OAUTHSCOPESW) \ + +#define X(sw, minchars, id) id, +DEFINE_SWITCH_ENUM(POST); +#undef X + +#define X(sw, minchars, id) { sw, minchars, id }, +DEFINE_SWITCH_ARRAY(POST, post_switches_with_args); +#undef X static char backup[BUFSIZ] = ""; static char drft[BUFSIZ] = ""; @@ -39,23 +103,24 @@ int main (int argc, char **argv) { pid_t child_id; - int i, vecp = 1; - char *addrs = NULL, *cp, *form = NULL, buf[BUFSIZ]; - char **argp, **arguments, *vec[MAXARGS]; + int vecp; + char *addrs = NULL, *cp, *form = NULL, buf[BUFSIZ], *program; + char **argp, **arguments, **vec; FILE *fp; char *tfile = NULL; - done=unlink_done; + if (nmh_init(argv[0], true, false)) { return 1; } + + set_done(unlink_done); -#ifdef LOCALE - setlocale(LC_ALL, ""); -#endif - invo_name = r1bindex (argv[0], '/'); + /* + * Configure this now, since any unknown switches to rcvdist get + * sent to postproc + */ - /* read user profile/context */ - context_read(); + vec = argsplit(postproc, &program, &vecp); - mts_init (invo_name); + mts_init (); arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; @@ -65,10 +130,19 @@ main (int argc, char **argv) case AMBIGSW: ambigsw (cp, switches); done (1); - case UNKWNSW: + case UNKWNSW: { + const int argno = smatch(cp, post_switches_with_args); vec[vecp++] = --cp; + if (argno != AMBIGSW && argno != UNKWNSW) { + /* It's a post switch that does take an argument. */ + if (!(cp = *argp) || *cp == '-') { + die("missing argument to %s", argp[-1]); + } + vec[vecp++] = cp; + ++argp; + } continue; - + } case HELPSW: snprintf (buf, sizeof(buf), "%s [switches] [switches for postproc] address ...", @@ -81,28 +155,30 @@ main (int argc, char **argv) case FORMSW: if (!(form = *argp++) || *form == '-') - adios (NULL, "missing argument to %s", argp[-2]); + die("missing argument to %s", argp[-2]); continue; } } - addrs = addrs ? add (cp, add (", ", addrs)) : getcpy (cp); + addrs = addrs ? add (cp, add (", ", addrs)) : mh_xstrdup(cp); } if (addrs == NULL) - adios (NULL, "usage: %s [switches] [switches for postproc] address ...", + die("usage: %s [switches] [switches for postproc] address ...", invo_name); umask (~m_gmprot ()); - tfile = m_mktemp2(NULL, invo_name, NULL, &fp); - if (tfile == NULL) adios("rcvdist", "unable to create temporary file"); + if ((tfile = m_mktemp2(NULL, invo_name, NULL, &fp)) == NULL) { + die("unable to create temporary file in %s", get_temp_dir()); + } strncpy (tmpfil, tfile, sizeof(tmpfil)); cpydata (fileno (stdin), fileno (fp), "message", tmpfil); fseek (fp, 0L, SEEK_SET); - tfile = m_mktemp2(NULL, invo_name, NULL, NULL); - if (tfile == NULL) adios("forw", "unable to create temporary file"); + if ((tfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) { + die("unable to create temporary file in %s", get_temp_dir()); + } strncpy (drft, tfile, sizeof(tmpfil)); rcvdistout (fp, form, addrs); @@ -111,22 +187,21 @@ main (int argc, char **argv) if (distout (drft, tmpfil, backup) == NOTOK) done (1); - vec[0] = r1bindex (postproc, '/'); vec[vecp++] = "-dist"; - vec[vecp++] = drft; if ((cp = context_find ("mhlproc"))) { vec[vecp++] = "-mhlproc"; vec[vecp++] = cp; } + vec[vecp++] = drft; vec[vecp] = NULL; - for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++) - sleep (5); + child_id = fork(); switch (child_id) { case NOTOK: - admonish (NULL, "unable to fork");/* fall */ + adios("fork", "failed:"); + case OK: - execvp (postproc, vec); + execvp (program, vec); fprintf (stderr, "unable to exec "); perror (postproc); _exit (1); @@ -140,16 +215,10 @@ main (int argc, char **argv) /* very similar to routine in replsbr.c */ -#define SBUFSIZ 256 - static int outputlinelen = OUTPUTLINELEN; static struct format *fmt; -static int ncomps = 0; -static char **compbuffers = 0; -static struct comp **used_buf = 0; - static int dat[5]; static char *addrcomps[] = { @@ -172,11 +241,13 @@ static char *addrcomps[] = { static void rcvdistout (FILE *inb, char *form, char *addrs) { - register int char_read = 0, format_len, i, state; - register char *tmpbuf, **nxtbuf, **ap; - char *cp, *scanl, name[NAMESZ]; - register struct comp *cptr, **savecomp; + int char_read = 0, format_len, i, state; + char **ap; + char *cp, name[NAMESZ], tmpbuf[NMH_BUFSIZ]; + charstring_t scanl; + struct comp *cptr; FILE *out; + m_getfld_state_t gstate; if (!(out = fopen (drft, "w"))) adios (drft, "unable to create"); @@ -184,66 +255,39 @@ rcvdistout (FILE *inb, char *form, char *addrs) /* get new format string */ cp = new_fs (form ? form : rcvdistcomps, NULL, NULL); format_len = strlen (cp); - ncomps = fmt_compile (cp, &fmt) + 1; - if (!(nxtbuf = compbuffers = (char **) calloc ((size_t) ncomps, sizeof(char *)))) - adios (NULL, "unable to allocate component buffers"); - if (!(savecomp = used_buf = (struct comp **) calloc ((size_t) (ncomps + 1), sizeof(struct comp *)))) - adios (NULL, "unable to allocate component buffer stack"); - savecomp += ncomps + 1; - *--savecomp = 0; - - for (i = ncomps; i--;) - *nxtbuf++ = mh_xmalloc (SBUFSIZ); - nxtbuf = compbuffers; - tmpbuf = *nxtbuf++; + fmt_compile (cp, &fmt, 1); for (ap = addrcomps; *ap; ap++) { - FINDCOMP (cptr, *ap); + cptr = fmt_findcomp (*ap); if (cptr) cptr->c_type |= CT_ADDR; } - FINDCOMP (cptr, "addresses"); + cptr = fmt_findcomp ("addresses"); if (cptr) cptr->c_text = addrs; - for (state = FLD;;) { - switch (state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb)) { + gstate = m_getfld_state_init(inb); + for (;;) { + int msg_count = sizeof tmpbuf; + switch (state = m_getfld2(&gstate, name, tmpbuf, &msg_count)) { case FLD: case FLDPLUS: - if ((cptr = wantcomp[CHASH (name)])) - do { - if (!mh_strcasecmp (name, cptr->c_name)) { - char_read += msg_count; - if (!cptr->c_text) { - cptr->c_text = tmpbuf; - *--savecomp = cptr; - tmpbuf = *nxtbuf++; - } - else { - i = strlen (cp = cptr->c_text) - 1; - if (cp[i] == '\n') { - if (cptr->c_type & CT_ADDR) { - cp[i] = 0; - cp = add (",\n\t", cp); - } - else - cp = add ("\t", cp); - } - cptr->c_text = add (tmpbuf, cp); - } - while (state == FLDPLUS) { - state = m_getfld (state, name, tmpbuf, - SBUFSIZ, inb); - cptr->c_text = add (tmpbuf, cptr->c_text); - char_read += msg_count; - } - break; - } - } while ((cptr = cptr->c_next)); - - while (state == FLDPLUS) - state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb); + i = fmt_addcomptext(name, tmpbuf); + if (i != -1) { + char_read += msg_count; + while (state == FLDPLUS) { + msg_count = sizeof tmpbuf; + state = m_getfld2(&gstate, name, tmpbuf, &msg_count); + fmt_appendcomp(i, name, tmpbuf); + char_read += msg_count; + } + } + + while (state == FLDPLUS) { + msg_count = sizeof tmpbuf; + state = m_getfld2(&gstate, name, tmpbuf, &msg_count); + } break; case LENERR: @@ -253,41 +297,37 @@ rcvdistout (FILE *inb, char *form, char *addrs) goto finished; default: - adios (NULL, "m_getfld() returned %d", state); + die("m_getfld2() returned %d", state); } } finished: ; + m_getfld_state_destroy (&gstate); i = format_len + char_read + 256; - scanl = mh_xmalloc ((size_t) i + 2); + scanl = charstring_create (i + 2); dat[0] = dat[1] = dat[2] = dat[4] = 0; dat[3] = outputlinelen; - fmt_scan (fmt, scanl, i, dat); - fputs (scanl, out); + fmt_scan (fmt, scanl, i, dat, NULL); + fputs (charstring_buffer (scanl), out); if (ferror (out)) adios (drft, "error writing"); fclose (out); - free (scanl); - for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++); nxtbuf++, i--) - free (cptr->c_text); - while (i-- > 0) - free (*nxtbuf++); - free ((char *) compbuffers); - free ((char *) used_buf); + charstring_free (scanl); + fmt_free(fmt, 1); } -static void +static void NORETURN unlink_done (int status) { if (backup[0]) - unlink (backup); + (void) m_unlink (backup); if (drft[0]) - unlink (drft); + (void) m_unlink (drft); if (tmpfil[0]) - unlink (tmpfil); + (void) m_unlink (tmpfil); - exit (status ? RCV_MBX : RCV_MOK); + exit(status ? 1 : 0); }