]> code.citadel.org Git - citadel.git/blobdiff - citadel/netproc.c
* serv_smtp.c: implement RFC2920 ESMTP "pipelining" extension on the server
[citadel.git] / citadel / netproc.c
index 5b2a6f53061b22ae940fe9cc2fdf48b79e947d84..d88898be7213f8090ed15dff64dbb28dcd63325f 100644 (file)
 #include <fcntl.h>
 #include <stdio.h>
 #include <ctype.h>
-#include <time.h>
+
+#if TIME_WITH_SYS_TIME
+# include <sys/time.h>
+# include <time.h>
+#else
+# if HAVE_SYS_TIME_H
+#  include <sys/time.h>
+# else
+#  include <time.h>
+# endif
+#endif
+
 #include <signal.h>
 #include <errno.h>
 #include <syslog.h>
@@ -408,7 +419,7 @@ void load_filterlist(void)
        fp = fopen("./network/filterlist", "r");
        if (fp == NULL)
                return;
-       while (fgets(sbuf, 256, fp) != NULL) {
+       while (fgets(sbuf, sizeof sbuf, fp) != NULL) {
                if (sbuf[0] != '#') {
                        sbuf[strlen(sbuf) - 1] = 0;
                        fbuf = (struct filterlist *)
@@ -484,7 +495,7 @@ GETSN:      for (stemp = slist; stemp != NULL; stemp = stemp->next) {
 }
 
 
-void fpgetfield(FILE * fp, char *string)
+void fpgetfield(FILE *fp, char *string, int limit)
 {
        int a, b;
 
@@ -492,7 +503,7 @@ void fpgetfield(FILE * fp, char *string)
        a = 0;
        do {
                b = getc(fp);
-               if (b < 1) {
+               if ((b < 1) || (a >= limit)) {
                        string[a] = 0;
                        return;
                }
@@ -529,7 +540,7 @@ BONFGM:     b = getc(fp);
                goto END;
        if (b == 'M')
                goto END;
-       fpgetfield(fp, bbb);
+       fpgetfield(fp, bbb, sizeof bbb);
        while ((bbb[0] == ' ') && (strlen(bbb) > 1))
                strcpy(bbb, &bbb[1]);
        if (b == 'A') {
@@ -585,7 +596,7 @@ BONFGM:     b = getc(fp);
                strcpy(buffer->Z, bbb);
        goto BONFGM;
 
-END:
+END:;
 
 }
 
@@ -655,7 +666,7 @@ void proc_file_transfer(char *tname)
        do {
                a = getc(tfp);
                if (a != 'M') {
-                       fpgetfield(tfp, buf);
+                       fpgetfield(tfp, buf, sizeof buf);
                        if (a == 'O') {
                                strcpy(dest_room, buf);
                        }
@@ -871,8 +882,8 @@ void inprocess(void)
        int valid_msg;
 
        /* temp file names */
-       strcpy(tname, tmpnam(NULL));
-       strcpy(iname, tmpnam(NULL));
+       sprintf(tname, "%s.netproc.%d", tmpnam(NULL), __LINE__);
+       sprintf(iname, "%s.netproc.%d", tmpnam(NULL), __LINE__);
 
        load_filterlist();
 
@@ -1196,7 +1207,7 @@ int ismsgok(FILE *mmfp, char *sysname)
        getc(mmfp);
 
        while (a = getc(mmfp), ((a != 'M') && (a != 0))) {
-               fpgetfield(mmfp, fbuf);
+               fpgetfield(mmfp, fbuf, sizeof fbuf);
                if (a == 'P') {
                        ok = checkpath(fbuf, sysname);
                }
@@ -1274,7 +1285,7 @@ int spool_out(struct msglist *cmlist, FILE * destfp, char *sysname)
 {
        struct msglist *cmptr;
        FILE *mmfp;
-       char fbuf[128];
+       char fbuf[1024];
        int a;
        int msgs_spooled = 0;
        long msg_len;
@@ -1302,6 +1313,10 @@ int spool_out(struct msglist *cmlist, FILE * destfp, char *sysname)
                }
                /* download the message from the server... */
                mmfp = tmpfile();
+               if (mmfp == NULL) {
+                       syslog(LOG_NOTICE, "tmpfile() failed: %s\n",
+                               strerror(errno) );
+               }
                sprintf(buf, "MSG3 %ld", cmptr->m_num);
                serv_puts(buf);
                serv_gets(buf);
@@ -1326,13 +1341,16 @@ int spool_out(struct msglist *cmlist, FILE * destfp, char *sysname)
                        fread(fbuf, 3, 1, mmfp);
                        fwrite(fbuf, 3, 1, destfp);
                        while (a = getc(mmfp), ((a != 0) && (a != 'M'))) {
-                               if (a != 'C')
+                               if (a != 'C') {
                                        putc(a, destfp);
-                               fpgetfield(mmfp, fbuf);
-                               if (a == 'P')
+                               }
+                               fpgetfield(mmfp, fbuf, sizeof fbuf);
+                               if (a == 'P') {
                                        fprintf(destfp, "%s!", NODENAME);
-                               if (a != 'C')
+                               }
+                               if (a != 'C') {
                                        fwrite(fbuf, strlen(fbuf) + 1, 1, destfp);
+                               }
                                if (a == 'S') if (!strcasecmp(fbuf, "CANCEL")) {
                                        delete_locally(cmptr->m_num, cmptr->m_rmname);
                                }
@@ -1370,14 +1388,15 @@ void outprocess(char *sysname)
        char tempflnm[64];
        char buf[256];
        struct msglist *cmlist = NULL;
+       struct msglist *cmlast = NULL;
        struct rmlist *crmlist = NULL;
        struct rmlist *rmptr, *rmptr2;
-       struct msglist *cmptr, *cmptr2;
+       struct msglist *cmptr;
        FILE *sysflfp, *tempflfp;
-       int outgoing_msgs;
+       int outgoing_msgs = 0;
        long thismsg;
 
-       strcpy(tempflnm, tmpnam(NULL));
+       sprintf(tempflnm, "%s.netproc.%d", tmpnam(NULL), __LINE__);
        tempflfp = fopen(tempflnm, "w");
        if (tempflfp == NULL)
                return;
@@ -1443,14 +1462,14 @@ void outprocess(char *sysname)
                                                cmptr->m_num = thismsg;
                                                strcpy(cmptr->m_rmname, rmptr->rm_name);
 
-                                               if (cmlist == NULL)
+                                               if (cmlist == NULL) {
                                                        cmlist = cmptr;
+                                               }
                                                else {
-                                                       cmptr2 = cmlist;
-                                                       while (cmptr2->next != NULL)
-                                                               cmptr2 = cmptr2->next;
-                                                       cmptr2->next = cmptr;
+                                                       cmlast->next = cmptr;
                                                }
+                                               cmlast = cmptr;
+                                               ++outgoing_msgs;
                                        }
                        } else {        /* print error from "msgs all" */
                                syslog(LOG_ERR, "%s", buf);
@@ -1458,22 +1477,17 @@ void outprocess(char *sysname)
                }
        }
 
-       outgoing_msgs = 0;
-       cmptr2 = cmlist;        /* this loop counts the messages */
-       while (cmptr2 != NULL) {
-               ++outgoing_msgs;
-               cmptr2 = cmptr2->next;
-       }
        syslog(LOG_NOTICE, "%d messages to be spooled to %s",
               outgoing_msgs, sysname);
 
 /*
  * Spool out the messages, but only if there are any.
  */
-       if (outgoing_msgs != 0)
+       if (outgoing_msgs != 0) {
                outgoing_msgs = spool_out(cmlist, tempflfp, sysname);
-       syslog(LOG_NOTICE, "%d messages actually spooled",
-              outgoing_msgs);
+       }
+
+       syslog(LOG_NOTICE, "%d messages actually spooled", outgoing_msgs);
 
 /*
  * Deallocate list of spooled messages.