Make #'require's NOERROR arg do its job properly.
[sxemacs] / lib-src / movemail.c
index 66456ed..f6aa06d 100644 (file)
@@ -1,7 +1,7 @@
 /* movemail foo bar -- move file foo to file bar,
    locking file foo.
    Copyright (C) 1986, 1992, 1993, 1994, 1996 Free Software Foundation, Inc.
-   
+
    Copyright (C) 2005 Johann "Myrkraverk" Oskarsson <johann@myrkraverk.com>
 
 This file is part of SXEmacs.
@@ -45,11 +45,11 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
  * form "po:username".  This will cause movemail to open a connection
  * to a pop server running on $MAILHOST (environment variable).
  * Movemail must be setuid to root in order to work with POP.
- * 
+ *
  * New module: popmail.c
  * Modified routines:
  *     main - added code within #ifdef MAIL_USE_POP; added setuid
- *             (getuid ()) after POP code. 
+ *             (getuid ()) after POP code.
  * New routines in movemail.c:
  *     get_errmsg - return pointer to system error message
  *
@@ -143,6 +143,14 @@ static int pop_search_top(popserver server, int msgno, int lines,
                          struct re_pattern_buffer *regexp);
 #endif
 
+
+#define xstrncpy(d_,s_,l_)                     \
+       do {                                    \
+               char* dst_=d_;                  \
+               dst_[0]='\0';                   \
+               strncat((dst_),(s_),(l_)-1);    \
+       } while(0)
+
 int verbose = 0;
 #ifdef MAIL_USE_POP
 int reverse = 0;
@@ -169,12 +177,12 @@ struct option longopts[] = {
        {"verbose", no_argument, NULL, 'v'},
        {0}
 };
-#endif 
+#endif
 
 #define DOTLOCKING     0
-#define FLOCKING       1
+#define FLOCKING       1
 #define LOCKFING       2
-#define MMDF                   3
+#define MMDF           3
 #define LOCKING         4
 
 #if defined(MAIL_LOCK_FLOCK) && defined(HAVE_FLOCK)
@@ -291,6 +299,7 @@ int main(int argc, char *argv[])
        }
 
        while (optind < argc) {
+               assert(argv[optind] != NULL);
                if (!inname) {
                        inname = argv[optind];
                } else if (!outname) {
@@ -315,11 +324,6 @@ int main(int argc, char *argv[])
        if (*outname == 0)
                fatal("Destination file name is empty", 0);
 
-       VERBOSE(("checking access to output file\n"));
-       /* Check access to output file.  */
-       if (access(outname, F_OK) == 0 && access(outname, W_OK) != 0)
-               pfatal_with_name(outname);
-
        /* Also check that outname's directory is writable to the real uid.  */
        {
                char *buf = (char *)xmalloc(strlen(outname) + 1);
@@ -340,17 +344,18 @@ int main(int argc, char *argv[])
                int retcode = popmail(inname + 3, outname, poppass);
                exit(retcode);
        }
-       setuid(getuid());
+       if (0 != setuid(getuid())) {
+               exit(1);
+       }
 #endif                         /* MAIL_USE_POP */
 
 #ifndef DISABLE_DIRECT_ACCESS
 
-       /* Check access to input file.  */
-       if (access(inname, R_OK | W_OK) != 0)
-               pfatal_with_name(inname);
 
        if (fork() == 0) {
-               setuid(getuid());
+               if (0 != setuid(getuid())) {
+                       exit(1);
+               }
 
                VERBOSE(("opening input file\n"));
 
@@ -429,7 +434,7 @@ int main(int argc, char *argv[])
                        char buf[1024];
                        while (1) {
                                nread = read(indesc, buf, sizeof buf);
-                               if (nread < 0 ||  
+                               if (nread < 0 ||
                                    nread != write(outdesc, buf, nread)) {
                                        int saved_errno = errno;
                                        unlink(outname);
@@ -600,7 +605,7 @@ static void lock_dot(char *filename)
 
 #ifdef HAVE_MKSTEMP
                /* Remove all group and other permissions.. */
-               umask(S_IRWXG|S_IRWXO);
+               umask(S_IRWXG|S_IRWXO);
                desc = mkstemp(tempname);
 #else
                desc = open(tempname, O_WRONLY | O_CREAT | O_EXCL, 0666);
@@ -634,6 +639,7 @@ static void lock_dot(char *filename)
                }
        }
        strcpy(dotlock_filename, filename);
+       free(tempname);
 }
 #endif                         /* not DISABLE_DIRECT_ACCESS */
 
@@ -687,9 +693,9 @@ static char *concat(char *s1, char *s2, char *s3)
        int len1 = strlen(s1), len2 = strlen(s2), len3 = strlen(s3);
        char *result = (char *)xmalloc(len1 + len2 + len3 + 1);
 
-       strncpy(result, s1, len1+1);
-       strncpy(result + len1, s2, len2+1);
-       strncpy(result + len1 + len2, s3, len3+1);
+       xstrncpy(result, s1, len1+1);
+       xstrncpy(result + len1, s2, len2+1);
+       xstrncpy(result + len1 + len2, s3, len3+1);
        *(result + len1 + len2 + len3) = '\0';
 
        return result;
@@ -715,7 +721,7 @@ static long *xmalloc(unsigned int size)
 #include <stdio.h>
 #include "../src/syspwd.h"
 
-#define POP_ERROR      (-1)
+#define POP_ERROR      (-1)
 #define POP_RETRIEVED (0)
 #define POP_DONE (1)
 
@@ -853,7 +859,7 @@ pop_retr(popserver server, int msgno, int (*action) (char *, FILE *),
        int ret;
 
        if (pop_retrieve_first(server, msgno, &line)) {
-               strncpy(Errmsg, pop_error, sizeof(Errmsg));
+               xstrncpy(Errmsg, pop_error, sizeof(Errmsg));
                Errmsg[sizeof(Errmsg) - 1] = '\0';
                return (POP_ERROR);
        }
@@ -870,7 +876,7 @@ pop_retr(popserver server, int msgno, int (*action) (char *, FILE *),
        }
 
        if (ret) {
-               strncpy(Errmsg, pop_error, sizeof(Errmsg));
+               xstrncpy(Errmsg, pop_error, sizeof(Errmsg));
                Errmsg[sizeof(Errmsg) - 1] = '\0';
                return (POP_ERROR);
        }
@@ -888,7 +894,7 @@ pop_search_top(popserver server, int msgno, int lines,
        int match = POP_DONE;
 
        if (pop_top_first(server, msgno, lines, &line)) {
-               strncpy(Errmsg, pop_error, sizeof(Errmsg));
+               xstrncpy(Errmsg, pop_error, sizeof(Errmsg));
                Errmsg[sizeof(Errmsg) - 1] = '\0';
                return (POP_ERROR);
        }
@@ -912,7 +918,7 @@ pop_search_top(popserver server, int msgno, int lines,
        }
 
        if (ret) {
-               strncpy(Errmsg, pop_error, sizeof(Errmsg));
+               xstrncpy(Errmsg, pop_error, sizeof(Errmsg));
                Errmsg[sizeof(Errmsg) - 1] = '\0';
                return (POP_ERROR);
        }