]> 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 c362cf238f54272d2fec2be80cdf3a5cf6e440d3..d88898be7213f8090ed15dff64dbb28dcd63325f 100644 (file)
@@ -1,7 +1,9 @@
 /*
+ * $Id$
+ *
  * Citadel/UX Intelligent Network Processor for IGnet/Open networks
  * See copyright.txt for copyright information
- * $Id$
+ *
  */
 
 /* How long it takes for an old node to drop off the network map */
 #define USE_TIME       (604800L)
 
 /* Where do we keep our lock file? */
-#define LOCKFILE       "/var/lock/LCK.netproc"
+#define LOCKFILE       "/tmp/netproc.LCK"
 
 /* Path to the 'uudecode' utility (needed for network file transfers) */
 #define UUDECODE       "/usr/bin/uudecode"
 
+/* Files used by the networker */
+#define MAILSYSINFO    "./network/mail.sysinfo"
+
 /* Uncomment the DEBUG def to see noisy traces */
-/* #define DEBUG 1 */
+#define DEBUG 1
 
 
 #include "sysdep.h"
 #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>
-#ifdef HAVE_GDBM_H
-#include <gdbm.h>
-#endif
 #include "citadel.h"
 #include "tools.h"
+#include "ipc.h"
 
 /* A list of users you wish to filter out of incoming traffic can be kept
  * in ./network/filterlist -- messages from these users will be automatically
@@ -80,13 +94,49 @@ struct syslist {
 };
 
 
-void attach_to_server(int argc, char **argv);
+
+/*
+ * This structure is used to hold all of the fields of a message
+ * during conversion, processing, or whatever.
+ */
+struct minfo {
+       char A[512];
+       char B[512];
+       char C[512];
+       char D[512];
+       char E[512];
+       char G[512];
+       char H[512];
+       char I[512];
+       char N[512];
+       char O[512];
+       char P[512];
+       char R[512];
+       char S[512];
+       long T;
+       char U[512];
+       char Z[512];
+       char nexthop[512];
+       };
+
+
+struct usetable {
+       struct usetable *next;
+       char msgid[256];
+       time_t timestamp;
+};
+
+
+
+
 void serv_read(char *buf, int bytes);
 void serv_write(char *buf, int nbytes);
 void get_config(void);
 
 struct filterlist *filter = NULL;
 struct syslist *slist = NULL;
+struct msglist *purgelist = NULL;
+struct usetable *usetable = NULL;
 
 struct config config;
 extern char bbs_home_directory[];
@@ -115,7 +165,7 @@ void strip_trailing_whitespace(char *buf)
 
 
 /*
- * we also load the network/mail.sysinfo table into memory, make changes
+ * we also load the mail.sysinfo table into memory, make changes
  * as we learn more about the network from incoming messages, and write
  * the table back to disk when we're done.
  */
@@ -126,7 +176,7 @@ int load_syslist(void)
        char insys = 0;
        char buf[128];
 
-       fp = fopen("network/mail.sysinfo", "r");
+       fp = fopen(MAILSYSINFO, "r");
        if (fp == NULL)
                return (1);
 
@@ -231,7 +281,7 @@ void rewrite_syslist(void)
        time_t now;
 
        time(&now);
-       newfp = fopen("network/mail.sysinfo", "w");
+       newfp = fopen(MAILSYSINFO, "w");
        for (stemp = slist; stemp != NULL; stemp = stemp->next) {
                if (!strcasecmp(stemp->s_name, config.c_nodename)) {
                        time(&stemp->s_lastcontact);
@@ -307,6 +357,11 @@ int set_lockfile(void)
                        return 1;
        }
        lfp = fopen(LOCKFILE, "w");
+       if (lfp == NULL) {
+               syslog(LOG_NOTICE, "Cannot create %s: %s", LOCKFILE,
+                       strerror(errno));
+               return(1);
+       }
        fprintf(lfp, "%ld\n", (long) getpid());
        fclose(lfp);
        return (0);
@@ -364,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 *)
@@ -440,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;
 
@@ -448,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;
                }
@@ -463,40 +518,29 @@ void fpgetfield(FILE * fp, char *string)
  * Load all of the fields of a message, except the actual text, into a
  * table in memory (so we know how to process the message).
  */
-void msgfind(char *msgfile, struct minfo *buffer)
+void fpmsgfind(FILE *fp, struct minfo *buffer)
 {
        int b, e, mtype, aflag;
        char bbb[1024];
        char userid[1024];
-       FILE *fp;
 
        strcpy(userid, "");
-       fp = fopen(msgfile, "rb");
-       if (fp == NULL) {
-               syslog(LOG_ERR, "can't open message file: %s", strerror(errno));
-               return;
-       }
        e = getc(fp);
        if (e != 255) {
-               syslog(LOG_ERR, "incorrect message format");
+               syslog(LOG_ERR, "Magic number check failed for this message");
                goto END;
        }
+
+       memset(buffer, 0, sizeof(struct minfo));
        mtype = getc(fp);
        aflag = getc(fp);
-       buffer->I = 0L;
-       buffer->R[0] = 0;
-       buffer->E[0] = 0;
-       buffer->H[0] = 0;
-       buffer->S[0] = 0;
-       buffer->B[0] = 0;
-       buffer->G[0] = 0;
 
 BONFGM:        b = getc(fp);
        if (b < 0)
                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') {
@@ -539,7 +583,7 @@ BONFGM:     b = getc(fp);
        if (b == 'T')
                buffer->T = atol(bbb);
        if (b == 'I')
-               buffer->I = atol(bbb);
+               strcpy(buffer->I, bbb);
        if (b == 'H')
                strcpy(buffer->H, bbb);
        if (b == 'B')
@@ -548,18 +592,30 @@ BONFGM:   b = getc(fp);
                strcpy(buffer->G, bbb);
        if (b == 'E')
                strcpy(buffer->E, bbb);
+       if (b == 'Z')
+               strcpy(buffer->Z, bbb);
        goto BONFGM;
 
-END:   fclose(fp);
+END:;
 
-       /* NOTE: we used to use the following two lines of code to assign
-        * the timestamp as a message-ID if there was no message-ID already
-        * in the message.  We don't do this anymore because it screws up
-        * the loopzapper.
-        *
-       if (buffer->I == 0L)
-               buffer->I = buffer->T;
-        */
+}
+
+
+/*
+ * msgfind() is the same as fpmsgfind() except it accepts a filename
+ * instead of a file handle.
+ */
+void msgfind(char *msgfile, struct minfo *buffer) {
+       FILE *fp;
+
+       fp = fopen(msgfile, "rb");
+       if (fp == NULL) {
+               syslog(LOG_ERR, "can't open %s: %s", msgfile, strerror(errno));
+               return;
+       }
+
+       fpmsgfind(fp, buffer);
+       fclose(fp);
 }
 
 
@@ -610,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);
                        }
@@ -706,7 +762,12 @@ void bounce(struct minfo *bminfo)
 void strmsgid(char *buf, struct minfo *msginfo) {
        int i;
 
-       sprintf(buf, "%ld@%s", msginfo->I, msginfo->N);
+       strcpy(buf, msginfo->I);
+       if (strchr(buf, '@') == NULL) {
+               strcat(buf, "@");
+               strcat(buf, msginfo->N);
+       }
+
        for (i=0; i<strlen(buf); ++i) {
                if (isspace(buf[i])) {
                        strcpy(&buf[i], &buf[i+1]);
@@ -722,80 +783,87 @@ void strmsgid(char *buf, struct minfo *msginfo) {
  * Returns 1 if the message is a duplicate; otherwise, it returns
  * 0 and the message ID is added to the use table.
  */
-int already_received(GDBM_FILE ut, struct minfo *msginfo) {
+int already_received(struct minfo *msginfo) {
        char buf[256];
+       struct usetable *u;
        time_t now;
-       datum mkey, newrec;
-       int retval = 0;
 
        /* We can't check for dups on a zero msgid, so just pass them through */
-       if ((msginfo->I)==0L) {
+       if (strlen(msginfo->I)==0) {
                return 0;
        }
 
        strmsgid(buf, msginfo);
        now = time(NULL);
 
-       mkey.dptr = buf;
-       mkey.dsize = strlen(buf);
-
        /* Set return value to 1 if message exists */
-       if (gdbm_exists(ut, mkey)) {
-               retval = 1;
+       for (u=usetable; u!=NULL; u=u->next) {
+               if (!strcasecmp(buf, u->msgid)) {
+                       u->timestamp = time(NULL);      /* keep it fresh */
+                       return(1);
+               }
        }
 
-       /* Write a record into the use table for this message.
-        * Replace existing records; this keeps the timestamp fresh.
-        */
-       newrec.dptr = (char *)&now;
-       newrec.dsize = sizeof(now);
-       gdbm_store(ut, mkey, newrec, GDBM_REPLACE);
+       /* Not found, so we're ok, but add it to the use table now */
+       u = (struct usetable *) malloc(sizeof (struct usetable));
+       u->next = usetable;
+       u->timestamp = time(NULL);
+       strncpy(u->msgid, buf, 255);
+       usetable = u;
+
+       return(0);
+}
+
+
+/*
+ * Load the use table from disk
+ */
+void read_use_table(void) {
+       struct usetable *u;
+       struct usetable ubuf;
+       FILE *fp;
+
+       unlink("data/usetable.gdbm");   /* we don't use this anymore */
+
+       fp = fopen("usetable", "rb");
+       if (fp == NULL) return;
 
-       return(retval);
+       while (fread(&ubuf, sizeof (struct usetable), 1, fp) > 0) {
+               u = (struct usetable *) malloc(sizeof (struct usetable));
+               memcpy(u, &ubuf, sizeof (struct usetable));
+               u->next = usetable;
+               usetable = u;
+       }
+
+       fclose(fp);
 }
 
 
 
 /*
- * Purge any old entries out of the use table.
+ * Purge any old entries out of the use table as we write them back to disk.
  * 
- * Yes, you're reading this correctly: it keeps traversing the table until
- * it manages to do a complete pass without deleting any records.  Read the
- * gdbm man page to find out why.
- *
  */
-void purge_use_table(GDBM_FILE ut) {
-       datum mkey, nextkey, therec;
-       int purged_anything = 0;
-       time_t rec_timestamp, now;
+void write_use_table(void) {
+       struct usetable *u;
+       time_t now;
+       FILE *fp;
 
        now = time(NULL);
-
-       do {
-               purged_anything = 0;
-               mkey = gdbm_firstkey(ut);
-               while (mkey.dptr != NULL) {
-                       therec = gdbm_fetch(ut, mkey);
-                       if (therec.dptr != NULL) {
-                               memcpy(&rec_timestamp, therec.dptr,
-                                       sizeof(time_t));
-                               free(therec.dptr);
-
-                               if ((now - rec_timestamp) > USE_TIME) {
-                                       gdbm_delete(ut, mkey);
-                                       purged_anything = 1;
-                               }
-
-                       }
-                       nextkey = gdbm_nextkey(ut, mkey);
-                       free(mkey.dptr);
-                       mkey = nextkey;
+       fp = fopen("usetable", "wb");
+       if (fp == NULL) return;
+       for (u=usetable; u!=NULL; u=u->next) {
+               if ((now - u->timestamp) <= USE_TIME) {
+                       fwrite(u, sizeof(struct usetable), 1, fp);
                }
-       } while (purged_anything != 0);
+       }
+       fclose(fp);
 }
 
 
 
+
+
 /*
  * process incoming files in ./network/spoolin
  */
@@ -803,7 +871,6 @@ void inprocess(void)
 {
        FILE *fp, *message, *testfp, *ls, *duplist;
        static struct minfo minfo;
-       struct recentmsg recentmsg;
        char tname[128], aaa[1024], iname[256], sfilename[256], pfilename[256];
        int a, b;
        int FieldID;
@@ -812,26 +879,17 @@ void inprocess(void)
        char buf[256];
        long msglen;
        int bloklen;
-       GDBM_FILE use_table;
+       int valid_msg;
 
        /* temp file names */
-       sprintf(tname, tmpnam(NULL));
-       sprintf(iname, tmpnam(NULL));
+       sprintf(tname, "%s.netproc.%d", tmpnam(NULL), __LINE__);
+       sprintf(iname, "%s.netproc.%d", tmpnam(NULL), __LINE__);
 
        load_filterlist();
 
        /* Make sure we're in the right directory */
        chdir(bbs_home_directory);
 
-       /* Open the use table */
-       use_table = gdbm_open("./data/usetable.gdbm", 512,
-                             GDBM_WRCREAT, 0600, 0);
-       if (use_table == NULL) {
-               syslog(LOG_ERR, "could not open use table: %s",
-                      strerror(errno));
-       }
-
-
        /* temporary file to contain a log of rejected dups */
        duplist = tmpfile();
 
@@ -847,6 +905,10 @@ void inprocess(void)
 SKIP:                          ptr = fgets(sfilename, sizeof sfilename, ls);
                                if (ptr != NULL) {
                                        sfilename[strlen(sfilename) - 1] = 0;
+#ifdef DEBUG
+                                       syslog(LOG_DEBUG,
+                                               "Trying <%s>", sfilename);
+#endif
                                        if (!strcmp(sfilename, ".")) goto SKIP;
                                        if (!strcmp(sfilename, "..")) goto SKIP;
                                        if (!strcmp(sfilename, "CVS")) goto SKIP;
@@ -872,6 +934,7 @@ NXMSG:      /* Seek to the beginning of the next message */
                                goto ENDSTR;
 
                        /* This crates the temporary file. */
+                       valid_msg = 1;
                        message = fopen(tname, "wb");
                        if (message == NULL) {
                                syslog(LOG_ERR, "error creating %s: %s",
@@ -884,28 +947,35 @@ NXMSG:    /* Seek to the beginning of the next message */
                        a = getc(fp);
                        putc(a, message);       /* mode */
                        do {
-                               FieldID = getc(fp);     /* Header field ID */
-                               putc(FieldID, message);
-                               do {
-                                       a = getc(fp);
-                                       putc(a, message);
-                               } while (a > 0);
+                               FieldID = getc(fp); /* Header field ID */
+                               if (isalpha(FieldID)) {
+                                       putc(FieldID, message);
+                                       do {
+                                               a = getc(fp);
+                                               if (a < 127) putc(a, message);
+                                       } while (a > 0);
+                                       if (a != 0) putc(0, message);
+                               }
+                               else {  /* Invalid field ID; flush it */
+                                       do {
+                                               a = getc(fp);
+                                       } while (a > 0);
+                                       valid_msg = 0;
+                               }
                        } while ((FieldID != 'M') && (a >= 0));
                        /* M is always last */
+                       if (FieldID != 'M') valid_msg = 0;
 
                        msglen = ftell(message);
                        fclose(message);
 
+                       if (!valid_msg) {
+                               unlink(tname);
+                               goto NXMSG;
+                       }
+
                        /* process the individual mesage */
-                       minfo.D[0] = 0;
-                       minfo.C[0] = 0;
-                       minfo.B[0] = 0;
-                       minfo.G[0] = 0;
-                       minfo.R[0] = 0;
                        msgfind(tname, &minfo);
-                       strncpy(recentmsg.RMnodename, minfo.N, 9);
-                       recentmsg.RMnodename[9] = 0;
-                       recentmsg.RMnum = minfo.I;
                        syslog(LOG_NOTICE, "#%ld fm <%s> in <%s> @ <%s>",
                               minfo.I, minfo.A, minfo.O, minfo.N);
                        if (strlen(minfo.R) > 0) {
@@ -961,9 +1031,9 @@ NXMSG:     /* Seek to the beginning of the next message */
                        }
 
                        /* Check the use table; reject message if it's been here before */
-                       if (already_received(use_table, &minfo)) {
+                       if (already_received(&minfo)) {
                                syslog(LOG_NOTICE, "rejected duplicate message");
-                               fprintf(duplist, "#%ld fm <%s> in <%s> @ <%s>\n",
+                               fprintf(duplist, "#<%s> fm <%s> in <%s> @ <%s>\n",
                                        minfo.I, minfo.A, minfo.O, minfo.N);
                        }
 
@@ -998,7 +1068,6 @@ NXMSG:     /* Seek to the beginning of the next message */
 
                        /* otherwise process it as a normal message */
                        else {
-
                                if (!strcasecmp(minfo.R, "postmaster")) {
                                        strcpy(minfo.R, "");
                                        strcpy(minfo.C, "Aide");
@@ -1060,6 +1129,7 @@ NXMSG:    /* Seek to the beginning of the next message */
                                }
 
                                fclose(message);
+                               
                        }
 
                        unlink(tname);
@@ -1071,9 +1141,6 @@ ENDSTR:                   fclose(fp);
        } while (ptr != NULL);
        unlink(iname);
 
-       purge_use_table(use_table);
-       gdbm_close(use_table);
-
 
        /*
         * If dups were rejected, post a message saying so
@@ -1123,22 +1190,24 @@ int checkpath(char *path, char *sys)
 }
 
 /*
- * implement split horizon algorithm
+ * Implement split horizon algorithm (prevent infinite spooling loops
+ * by refusing to send any node a message which already contains its
+ * nodename in the path).
  */
-int ismsgok(long int mpos, FILE * mmfp, char *sysname)
+int ismsgok(FILE *mmfp, char *sysname)
 {
        int a;
        int ok = 0;             /* fail safe - no path, don't send it */
        char fbuf[256];
 
-       fseek(mmfp, mpos, 0);
+       fseek(mmfp, 0L, 0);
        if (getc(mmfp) != 255)
                return (0);
        getc(mmfp);
        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);
                }
@@ -1151,17 +1220,77 @@ int ismsgok(long int mpos, FILE * mmfp, char *sysname)
 
 
 
+
+/*
+ * Add a message to the list of messages to be deleted off the local server
+ * at the end of this run.
+ */
+void delete_locally(long msgid, char *roomname) {
+       struct msglist *mptr;
+
+       mptr = (struct msglist *) malloc(sizeof(struct msglist));
+       mptr->next = purgelist;
+       mptr->m_num = msgid;
+       strcpy(mptr->m_rmname, roomname);
+       purgelist = mptr;
+}
+
+
+
+/*
+ * Delete all messages on the purge list from the local server.
+ */
+void process_purgelist(void) {
+       char curr_rm[ROOMNAMELEN];
+       char buf[256];
+       struct msglist *mptr;
+
+
+       strcpy(curr_rm, "__nothing__");
+       while (purgelist != NULL) {
+               if (strcasecmp(curr_rm, purgelist->m_rmname)) {
+                       sprintf(buf, "GOTO %s", purgelist->m_rmname);
+                       serv_puts(buf);
+                       serv_gets(buf);
+                       if (buf[0] == '2') {
+                               extract(curr_rm, &buf[4], 0);
+                       }
+                       else {
+                               syslog(LOG_ERR, "%s", buf);
+                       }
+               }
+               if (!strcasecmp(curr_rm, purgelist->m_rmname)) {
+                       syslog(LOG_NOTICE, "Purging <%ld> in <%s>",
+                               purgelist->m_num, purgelist->m_rmname);
+                       sprintf(buf, "DELE %ld", purgelist->m_num);
+                       serv_puts(buf);
+                       serv_gets(buf);
+                       if (buf[0] != '2') {
+                               syslog(LOG_ERR, "%s", buf);
+                       }
+
+               }
+               mptr = purgelist->next;
+               free(purgelist);
+               purgelist = mptr;
+       }
+}
+
+
+
+
 /* spool list of messages to a file */
 /* returns # of msgs spooled */
 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;
        int blok_len;
+       static struct minfo minfo;
 
        char buf[256];
        char curr_rm[256];
@@ -1184,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);
@@ -1201,20 +1334,26 @@ int spool_out(struct msglist *cmlist, FILE * destfp, char *sysname)
 
                rewind(mmfp);
 
-               if (ismsgok(0L, mmfp, sysname)) {
+               if (ismsgok(mmfp, sysname)) {
                        ++msgs_spooled;
                        fflush(stdout);
                        fseek(mmfp, 0L, 0);
                        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);
+                               }
                        }
                        if (a == 'M') {
                                fprintf(destfp, "C%s%c",
@@ -1225,6 +1364,14 @@ int spool_out(struct msglist *cmlist, FILE * destfp, char *sysname)
                                        putc(a, destfp);
                                } while (a > 0);
                        }
+
+               /* Get this message into the use table, so we can reject it
+                * if a misconfigured remote system sends it back to us.
+                */
+               fseek(mmfp, 0L, 0);
+               fpmsgfind(mmfp, &minfo);
+               already_received(&minfo);
+
                }
                fclose(mmfp);
        }
@@ -1241,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;
 
-       sprintf(tempflnm, tmpnam(NULL));
+       sprintf(tempflnm, "%s.netproc.%d", tmpnam(NULL), __LINE__);
        tempflfp = fopen(tempflnm, "w");
        if (tempflfp == NULL)
                return;
@@ -1314,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);
@@ -1329,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.
@@ -1386,14 +1529,11 @@ void outprocess(char *sysname)
 void np_attach_to_server(void)
 {
        char buf[256];
-       char portname[8];
        char *args[] =
-       {"netproc", "localhost", NULL, NULL};
+       { "netproc", NULL };
 
        syslog(LOG_NOTICE, "Attaching to server...");
-       sprintf(portname, "%d", config.c_port_number);
-       args[2] = portname;
-       attach_to_server(3, args);
+       attach_to_server(1, args, NULL, NULL);
        serv_gets(buf);
        syslog(LOG_NOTICE, "%s", &buf[4]);
        sprintf(buf, "IPGM %d", config.c_ipgm_secret);
@@ -1437,8 +1577,14 @@ int main(int argc, char **argv)
                }
        }
 
+#ifdef DEBUG
+       syslog(LOG_DEBUG, "Calling get_config()");
+#endif
        get_config();
 
+#ifdef DEBUG
+       syslog(LOG_DEBUG, "Creating lock file");
+#endif
        if (set_lockfile() != 0) {
                syslog(LOG_NOTICE, "lock file exists: already running");
                cleanup(1);
@@ -1457,21 +1603,40 @@ int main(int argc, char **argv)
                syslog(LOG_ERR, "cannot load sysinfo");
        setup_special_nodes();
 
-       inprocess();            /* first collect incoming stuff */
+       /* Open the use table */
+       read_use_table();
+
+       /* first collect incoming stuff */
+       inprocess();
 
+       /* Now process outbound messages, but NOT if this is just a
+        * quick import-only run (i.e. the -i command-line option
+        * was specified)
+        */
        if (import_only != 1) {
                allfp = (FILE *) popen("cd ./network/systems; ls", "r");
                if (allfp != NULL) {
                        while (fgets(allst, 32, allfp) != NULL) {
                                allst[strlen(allst) - 1] = 0;
-                               outprocess(allst);
+                               if (strcmp(allst, "CVS"))
+                                       outprocess(allst);
                        }
                        pclose(allfp);
                }
                /* import again in case anything new was generated */
                inprocess();
        }
+
+       /* Update mail.sysinfo with new information we learned */
        rewrite_syslist();
+
+       /* Delete any messages which need to be purged locally */
+       syslog(LOG_NOTICE, "calling process_purgelist()");
+       process_purgelist();
+
+       /* Close the use table */
+       write_use_table();
+
        syslog(LOG_NOTICE, "processing ended.");
        cleanup(0);
        return 0;