]> diplodocus.org Git - nmh/blobdiff - uip/sendsbr.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[nmh] / uip / sendsbr.c
index 66ce65d15abaa781ce821d7a9b08e0f7823ae56c..0af6f63a3fe72c8b2e8026369e002a3c061930f7 100644 (file)
@@ -1,6 +1,4 @@
-
-/*
- * sendsbr.c -- routines to help WhatNow/Send along
+/* sendsbr.c -- routines to help WhatNow/Send along
  *
  * This code is Copyright (c) 2002, by the authors of nmh.  See the
  * COPYRIGHT file in the root directory of the nmh distribution for
 
 #ifdef OAUTH_SUPPORT
 #include <h/oauth.h>
+#endif
+#include "sbr/m_maildir.h"
+#include "sbr/m_mktemp.h"
+#include "sbr/message_id.h"
 
+#ifdef OAUTH_SUPPORT
 static int setup_oauth_params(char *[], int *, const char *, const char **);
 #endif /* OAUTH_SUPPORT */
 
@@ -132,7 +135,7 @@ sendsbr (char **vec, int vecp, char *program, char *draft, struct stat *st,
 #ifdef OAUTH_SUPPORT
                const char *errmsg;
                if (setup_oauth_params(vec, nvecsp, auth_svc, &errmsg) != OK) {
-                       adios(NULL, errmsg);
+                        adios(NULL, "%s", errmsg);
                }
 #else
                 adios(NULL, "send built without OAUTH_SUPPORT, "
@@ -191,11 +194,11 @@ splitmsg (char **vec, int vecp, char *program, char *drft,
     int        compnum, nparts, partno, state, status;
     long pos, start;
     time_t clock;
-    char *cp, *dp, buffer[BUFSIZ], msgid[BUFSIZ];
+    char *cp, *dp, buffer[NMH_BUFSIZ], msgid[BUFSIZ];
     char subject[BUFSIZ];
     char name[NAMESZ], partnum[BUFSIZ];
     FILE *in;
-    m_getfld_state_t gstate = 0;
+    m_getfld_state_t gstate;
 
     if ((in = fopen (drft, "r")) == NULL)
        adios (drft, "unable to open for reading");
@@ -207,10 +210,11 @@ splitmsg (char **vec, int vecp, char *program, char *drft,
      * Scan through the message and examine the various header fields,
      * as well as locate the beginning of the message body.
      */
-    m_getfld_track_filepos (&gstate, in);
+    gstate = m_getfld_state_init(in);
+    m_getfld_track_filepos2(&gstate);
     for (compnum = 1;;) {
        int bufsz = sizeof buffer;
-       switch (state = m_getfld (&gstate, name, buffer, &bufsz, in)) {
+       switch (state = m_getfld2(&gstate, name, buffer, &bufsz)) {
            case FLD:
            case FLDPLUS:
                compnum++;
@@ -221,7 +225,7 @@ splitmsg (char **vec, int vecp, char *program, char *drft,
                if (!strcasecmp (name, "Message-ID")) {
                    while (state == FLDPLUS) {
                        bufsz = sizeof buffer;
-                       state = m_getfld (&gstate, name, buffer, &bufsz, in);
+                       state = m_getfld2(&gstate, name, buffer, &bufsz);
                    }
                } else if (uprf (name, XXX_FIELD_PRF)
                        || !strcasecmp (name, VRSN_FIELD)
@@ -247,7 +251,7 @@ splitmsg (char **vec, int vecp, char *program, char *drft,
                    dp = add (concat (name, ":", buffer, NULL), dp);
                    while (state == FLDPLUS) {
                        bufsz = sizeof buffer;
-                       state = m_getfld (&gstate, name, buffer, &bufsz, in);
+                       state = m_getfld2(&gstate, name, buffer, &bufsz);
                        dp = add (buffer, dp);
                    }
                } else {
@@ -258,7 +262,7 @@ splitmsg (char **vec, int vecp, char *program, char *drft,
                    cp = add (concat (name, ":", buffer, NULL), cp);
                    while (state == FLDPLUS) {
                        bufsz = sizeof buffer;
-                       state = m_getfld (&gstate, name, buffer, &bufsz, in);
+                       state = m_getfld2(&gstate, name, buffer, &bufsz);
                        cp = add (buffer, cp);
                    }
                }
@@ -286,7 +290,7 @@ splitmsg (char **vec, int vecp, char *program, char *drft,
 
     nparts = 1;
     pos = start;
-    while (fgets (buffer, sizeof(buffer) - 1, in)) {
+    while (fgets (buffer, sizeof buffer, in)) {
        long len;
 
        if ((pos += (len = strlen (buffer))) > CPERMSG) {
@@ -298,8 +302,7 @@ splitmsg (char **vec, int vecp, char *program, char *drft,
     /* Only one part, nothing to split */
     if (nparts == 1) {
        free (cp);
-       if (dp)
-           free (dp);
+        free(dp);
 
        fclose (in);
        return sendaux (vec, vecp, program, drft, st);
@@ -357,7 +360,7 @@ splitmsg (char **vec, int vecp, char *program, char *drft,
        for (;;) {
            long len;
 
-           if (!fgets (buffer, sizeof(buffer) - 1, in)) {
+           if (!fgets (buffer, sizeof buffer, in)) {
                if (partno == nparts)
                    break;
                adios (NULL, "premature eof");
@@ -405,8 +408,7 @@ splitmsg (char **vec, int vecp, char *program, char *drft,
     }
 
     free (cp);
-    if (dp)
-       free (dp);
+    free(dp);
 
     fclose (in);       /* close the draft */
     return status;
@@ -422,7 +424,7 @@ static int
 sendaux (char **vec, int vecp, char *program, char *drft, struct stat *st)
 {
     pid_t child_id;
-    int i, status, fd, fd2;
+    int status, fd, fd2;
     char backup[BUFSIZ], buf[BUFSIZ];
 
     fd = pushsw ? tmp_fd () : NOTOK;
@@ -434,8 +436,8 @@ sendaux (char **vec, int vecp, char *program, char *drft, struct stat *st)
            snprintf (buf, sizeof(buf), "%d", fd2);
            vec[vecp++] = buf;
        } else {
-           admonish (NULL, "unable to create temporary file in %s "
-                      "for annotation list", get_temp_dir());
+           inform("unable to create temporary file in %s for "
+               "annotation list, continuing...", get_temp_dir());
        }
     }
     vec[vecp++] = drft;
@@ -443,9 +445,7 @@ sendaux (char **vec, int vecp, char *program, char *drft, struct stat *st)
        done (1);
     vec[vecp] = NULL;
 
-    for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
-       sleep (5);
-
+    child_id = fork();
     switch (child_id) {
     case -1:
        /* oops -- fork error */
@@ -456,7 +456,7 @@ sendaux (char **vec, int vecp, char *program, char *drft, struct stat *st)
        /*
         * child process -- send it
         *
-        * If fd is ok, then we are pushing and fd points to temp
+        * If fd is OK, then we are pushing and fd points to temp
         * file, so capture anything on stdout and stderr there.
         */
        if (fd != NOTOK) {
@@ -486,7 +486,7 @@ sendaux (char **vec, int vecp, char *program, char *drft, struct stat *st)
                alert (drft, fd);
                close (fd);
            } else {
-               advise (NULL, "message not delivered to anyone");
+               inform("message not delivered to anyone");
            }
            if (annotext && fd2 != NOTOK)
                close (fd2);
@@ -512,18 +512,17 @@ static void
 alert (char *file, int out)
 {
     pid_t child_id;
-    int i, in, argp;
+    int in, argp;
     char buf[BUFSIZ];
     char *program;
     char **arglist;
 
-    for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
-       sleep (5);
-
+    child_id = fork();
     switch (child_id) {
        case NOTOK:
            /* oops -- fork error */
            advise ("fork", "unable to");
+           /* FALLTHRU */
 
        case OK:
            /* child process -- send it */
@@ -535,7 +534,7 @@ alert (char *file, int out)
                if ((in = open (file, O_RDONLY)) == NOTOK) {
                    admonish (file, "unable to re-open");
                } else {
-                   lseek (out, (off_t) 0, SEEK_END);
+                   lseek(out, 0, SEEK_END);
                    strncpy (buf, "\nMessage not delivered to anyone.\n", sizeof(buf));
                    if (write (out, buf, strlen (buf)) < 0) {
                        advise (file, "write");
@@ -554,7 +553,7 @@ alert (char *file, int out)
                        admonish (buf, "unable to rename %s to", file);
                }
            }
-           lseek (out, (off_t) 0, SEEK_SET);
+           lseek(out, 0, SEEK_SET);
            dup2 (out, fileno (stdin));
            close (out);
            /* create subject for error notification */
@@ -588,10 +587,9 @@ tmp_fd (void)
     if ((tfile = m_mktemp2(NULL, invo_name, &fd, NULL)) == NULL) return NOTOK;
 
     if (debugsw)
-       advise (NULL, "temporary file %s selected", tfile);
-    else
-       if (m_unlink (tfile) == NOTOK)
-           advise (tfile, "unable to remove");
+       inform("temporary file %s selected", tfile);
+    else if (m_unlink (tfile) == NOTOK)
+        advise (tfile, "unable to remove");
 
     return fd;
 }
@@ -611,7 +609,7 @@ anno (int fd, struct stat *st)
                || st->st_dev != st2.st_dev
                || st->st_ino != st2.st_ino)) {
        if (debugsw)
-           admonish (NULL, "$mhaltmsg mismatch");
+           inform("$mhaltmsg mismatch, continuing...");
        return;
     }
 
@@ -619,10 +617,10 @@ anno (int fd, struct stat *st)
     switch (child_id) {
        case NOTOK:             /* oops */
            if (!debugsw)
-               advise (NULL,
-                           "unable to fork, so doing annotations by hand...");
+               inform("unable to fork, so doing annotations by hand...");
            if (cwd == NULL)
                cwd = mh_xstrdup(pwd ());
+           /* FALLTHRU */
 
        case OK:
            /* block a few signals */
@@ -665,7 +663,7 @@ annoaux (int fd)
 
     if ((folder = getenv ("mhfolder")) == NULL || *folder == 0) {
        if (debugsw)
-           admonish (NULL, "$mhfolder not set");
+           inform("$mhfolder not set, continuing...");
        return;
     }
     maildir = m_maildir (folder);
@@ -676,20 +674,20 @@ annoaux (int fd)
     }
     if (!(mp = folder_read (folder, 0))) {
        if (debugsw)
-           admonish (NULL, "unable to read folder %s", folder);
+           inform("unable to read folder %s, continuing...", folder);
        return;
     }
 
     /* check for empty folder */
     if (mp->nummsg == 0) {
        if (debugsw)
-           admonish (NULL, "no messages in %s", folder);
+           inform("no messages in %s, continuing...", folder);
        goto oops;
     }
 
     if ((cp = getenv ("mhmessages")) == NULL || *cp == 0) {
        if (debugsw)
-           admonish (NULL, "$mhmessages not set");
+           inform("$mhmessages not set, continuing...");
        goto oops;
     }
     if (!debugsw                       /* MOBY HACK... */
@@ -708,14 +706,14 @@ annoaux (int fd)
        dup2 (fd2, fileno (stderr));
     if (mp->numsel == 0) {
        if (debugsw)
-           admonish (NULL, "no messages to annotate");
+           inform("no messages to annotate, continuing...");
        goto oops;
     }
 
-    lseek (fd, (off_t) 0, SEEK_SET);
+    lseek(fd, 0, SEEK_SET);
     if ((fp = fdopen (fd, "r")) == NULL) {
        if (debugsw)
-           admonish (NULL, "unable to fdopen annotation list");
+           inform("unable to fdopen annotation list, continuing...");
        goto oops;
     }
     cp = NULL;
@@ -724,12 +722,12 @@ annoaux (int fd)
     fclose (fp);
 
     if (debugsw)
-       advise (NULL, "annotate%s with %s: \"%s\"",
+       inform("annotate%s with %s: \"%s\"",
                inplace ? " inplace" : "", annotext, cp);
     for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
        if (is_selected(mp, msgnum)) {
            if (debugsw)
-               advise (NULL, "annotate message %d", msgnum);
+               inform("annotate message %d", msgnum);
             annotate (m_name (msgnum), annotext, cp, inplace, 1, -2, 0);
        }
     }
@@ -749,7 +747,7 @@ handle_sendfrom(char **vec, int *vecp, char *draft, const char *auth_svc) {
 
     /* Extract address and host from From: header line in draft. */
     if (get_from_header_info(draft, &addr, &host, &message) != OK) {
-        adios(draft, message);
+        adios(draft, "%s", message);
     }
 
     /* Merge in any address or host specific switches to post(1) from profile. */
@@ -766,7 +764,7 @@ handle_sendfrom(char **vec, int *vecp, char *draft, const char *auth_svc) {
             if (strcmp(*vp, "xoauth2") == 0) {
 #ifdef OAUTH_SUPPORT
                 if (setup_oauth_params(vec, vecp, auth_svc, &message) != OK) {
-                    adios(NULL, message);
+                    adios(NULL, "%s", message);
                 }
                 break;
 #else
@@ -885,18 +883,17 @@ get_from_header_info(const char *filename, const char **addr, const char **host,
 
         /* Use the hostformat that corresponds to the successful addrformat. */
         if ((*host = get_message_header_info(in, hostformat)) == NULL) {
-            fclose(in);
-            *message = "unable to find sender host in";
+            *message = "unable to find sender host";
             fclose(in);
             return NOTOK;
         }
         fclose(in);
 
         return OK;
-    } else {
-        *message = "unable to open";
-        return NOTOK;
     }
+
+    *message = "unable to open";
+    return NOTOK;
 }
 
 
@@ -911,7 +908,7 @@ get_message_header_info(FILE *in, char *format) {
     struct format *fmt;
     struct stat st;
     int parsing_header;
-    m_getfld_state_t gstate = 0;
+    m_getfld_state_t gstate;
     charstring_t buffer = charstring_create(0);
     char *retval;
 
@@ -927,10 +924,11 @@ get_message_header_info(FILE *in, char *format) {
      */
     rewind (in);
     parsing_header = 1;
+    gstate = m_getfld_state_init(in);
     do {
         char name[NAMESZ], rbuf[NMH_BUFSIZ];
         int bufsz = sizeof rbuf;
-        int state = m_getfld(&gstate, name, rbuf, &bufsz, in);
+        int state = m_getfld2(&gstate, name, rbuf, &bufsz);
 
         switch (state) {
         case FLD:
@@ -940,14 +938,14 @@ get_message_header_info(FILE *in, char *format) {
             if (bucket != -1) {
                 while (state == FLDPLUS) {
                     bufsz = sizeof rbuf;
-                    state = m_getfld(&gstate, name, rbuf, &bufsz, in);
+                    state = m_getfld2(&gstate, name, rbuf, &bufsz);
                     fmt_appendcomp(bucket, name, rbuf);
                 }
             }
 
             while (state == FLDPLUS) {
                 bufsz = sizeof rbuf;
-                state = m_getfld(&gstate, name, rbuf, &bufsz, in);
+                state = m_getfld2(&gstate, name, rbuf, &bufsz);
             }
             break;
         }
@@ -1012,7 +1010,7 @@ merge_profile_entry(const char *addr, const char *host, char *vec[], int *vecp)
 }
 
 
-static void
+static void NORETURN
 armed_done (int status)
 {
     longjmp (env, status ? status : NOTOK);