]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/network/serv_network.c
if we tell the client we're in error state, return.
[citadel.git] / citadel / modules / network / serv_network.c
index 4a6dac4c56eadba8616ac901451bbd184ee005ce..a5ddd83e02ddf467c86f89f68c6729b5bfa571a5 100644 (file)
 #  include <time.h>
 # endif
 #endif
+#ifdef HAVE_SYSCALL_H
+# include <syscall.h>
+#else 
+# if HAVE_SYS_SYSCALL_H
+#  include <sys/syscall.h>
+# endif
+#endif
 
 #include <sys/wait.h>
 #include <string.h>
@@ -405,8 +412,11 @@ void cmd_gnet(char *argbuf) {
 void cmd_snet(char *argbuf) {
        char tempfilename[PATH_MAX];
        char filename[PATH_MAX];
-       char buf[SIZ];
-       FILE *fp, *newfp;
+       int TmpFD;
+       StrBuf *Line;
+       struct stat StatBuf;
+       long len;
+       int rc;
 
        unbuffer_output();
 
@@ -415,40 +425,67 @@ void cmd_snet(char *argbuf) {
        }
        else if (CtdlAccessCheck(ac_room_aide)) return;
 
-       CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
-       assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
+       len = assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
+       memcpy(tempfilename, filename, len + 1);
 
-       fp = fopen(tempfilename, "w");
-       if (fp == NULL) {
-               cprintf("%d Cannot open %s: %s\n",
+       memset(&StatBuf, 0, sizeof(struct stat));
+       if ((stat(filename, &StatBuf)  == -1) || (StatBuf.st_size == 0))
+               StatBuf.st_size = 80; /* Not there or empty? guess 80 chars line. */
+
+       sprintf(tempfilename + len, ".%d", CC->cs_pid);
+       errno = 0;
+       TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
+
+       if ((TmpFD > 0) && (errno == 0))
+       {
+               char *tmp = malloc(StatBuf.st_size * 2);
+               memset(tmp, ' ', StatBuf.st_size * 2);
+               rc = write(TmpFD, tmp, StatBuf.st_size * 2);
+               free(tmp);
+               if ((rc <= 0) || (rc != StatBuf.st_size * 2))
+               {
+                       close(TmpFD);
+                       cprintf("%d Unable to allocate the space required for %s: %s\n",
+                               ERROR + INTERNAL_ERROR,
+                               tempfilename,
+                               strerror(errno));
+                       unlink(tempfilename);
+                       return;
+               }       
+               lseek(TmpFD, SEEK_SET, 0);
+       }
+       else {
+               cprintf("%d Unable to allocate the space required for %s: %s\n",
                        ERROR + INTERNAL_ERROR,
                        tempfilename,
                        strerror(errno));
+               unlink(tempfilename);
+               return;
        }
+       Line = NewStrBuf();
 
        cprintf("%d %s\n", SEND_LISTING, tempfilename);
-       while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
-               fprintf(fp, "%s\n", buf);
+
+       len = 0;
+       while (rc = CtdlClientGetLine(Line), 
+              (rc >= 0))
+       {
+               if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
+                       break;
+               StrBufAppendBufPlain(Line, HKEY("\n"), 0);
+               write(TmpFD, ChrPtr(Line), StrLength(Line));
+               len += StrLength(Line);
        }
-       fclose(fp);
+       FreeStrBuf(&Line);
+       ftruncate(TmpFD, len);
+       close(TmpFD);
 
        /* Now copy the temp file to its permanent location.
         * (We copy instead of link because they may be on different filesystems)
         */
        begin_critical_section(S_NETCONFIGS);
-       fp = fopen(tempfilename, "r");
-       if (fp != NULL) {
-               newfp = fopen(filename, "w");
-               if (newfp != NULL) {
-                       while (fgets(buf, sizeof buf, fp) != NULL) {
-                               fprintf(newfp, "%s", buf);
-                       }
-                       fclose(newfp);
-               }
-               fclose(fp);
-       }
+       rename(tempfilename, filename);
        end_critical_section(S_NETCONFIGS);
-       unlink(tempfilename);
 }
 
 
@@ -647,7 +684,19 @@ void network_spool_msg(long msgnum, void *userdata) {
                 */
                msg = CtdlFetchMessage(msgnum, 1);
                if (msg != NULL) {
-
+                       if (msg->cm_fields['V'] == NULL){
+                               /* local message, no enVelope */
+                               StrBuf *Buf;
+                               Buf = NewStrBuf();
+                               StrBufAppendBufPlain(Buf, msg->cm_fields['O'], -1, 0);
+                               StrBufAppendBufPlain(Buf, HKEY("@"), 0);
+                               StrBufAppendBufPlain(Buf, config.c_fqdn, -1, 0);
+                               
+                               msg->cm_fields['K'] = SmashStrBuf(&Buf);
+                       }
+                       else {
+                               msg->cm_fields['K'] = strdup (msg->cm_fields['V']);
+                       }
                        /* Set the 'List-ID' header */
                        if (msg->cm_fields['L'] != NULL) {
                                free(msg->cm_fields['L']);
@@ -874,10 +923,13 @@ void network_spool_msg(long msgnum, void *userdata) {
                                serialize_message(&sermsg, msg);
                                if (sermsg.len > 0) {
 
-                                       /* write it to the spool file */
-                                       snprintf(filename, sizeof filename,"%s/%s",
-                                                       ctdl_netout_dir,
-                                                       mptr->remote_nodename);
+                                       /* write it to a spool file */
+                                       snprintf(filename, sizeof filename,"%s/%s@%lx%x",
+                                               ctdl_netout_dir,
+                                               mptr->remote_nodename,
+                                               time(NULL),
+                                               rand()
+                                       );
                                        CtdlLogPrintf(CTDL_DEBUG, "Appending to %s\n", filename);
                                        fp = fopen(filename, "ab");
                                        if (fp != NULL) {
@@ -1036,60 +1088,96 @@ void free_spoolcontrol_struct(SpoolControl **scc)
 
 int writenfree_spoolcontrol_file(SpoolControl **scc, char *filename)
 {
-       FILE *fp;
+       char tempfilename[PATH_MAX];
+       int TmpFD;
        SpoolControl *sc;
        namelist *nptr = NULL;
        maplist *mptr = NULL;
+       long len;
+       time_t unixtime;
+       struct timeval tv;
+       long reltid; /* if we don't have SYS_gettid, use "random" value */
+       StrBuf *Cfg;
+       int rc;
+
+       len = strlen(filename);
+       memcpy(tempfilename, filename, len + 1);
 
+
+#if defined(HAVE_SYSCALL_H) && defined (SYS_gettid)
+       reltid = syscall(SYS_gettid);
+#endif
+       gettimeofday(&tv, NULL);
+       /* Promote to time_t; types differ on some OSes (like darwin) */
+       unixtime = tv.tv_sec;
+
+       sprintf(tempfilename + len, ".%ld-%ld", reltid, unixtime);
        sc = *scc;
-       fp = fopen(filename, "w");
-       if (fp == NULL) {
+       errno = 0;
+       TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
+       Cfg = NewStrBuf();
+       if ((TmpFD < 0) || (errno != 0)) {
                CtdlLogPrintf(CTDL_CRIT, "ERROR: cannot open %s: %s\n",
                        filename, strerror(errno));
                free_spoolcontrol_struct(scc);
+               unlink(tempfilename);
        }
        else {
-               fprintf(fp, "lastsent|%ld\n", sc->lastsent);
+               StrBufAppendPrintf(Cfg, "lastsent|%ld\n", sc->lastsent);
 
                /* Write out the listrecps while freeing from memory at the
                 * same time.  Am I clever or what?  :)
                 */
                while (sc->listrecps != NULL) {
-                       fprintf(fp, "listrecp|%s\n", sc->listrecps->name);
+                       StrBufAppendPrintf(Cfg, "listrecp|%s\n", sc->listrecps->name);
                        nptr = sc->listrecps->next;
                        free(sc->listrecps);
                        sc->listrecps = nptr;
                }
                /* Do the same for digestrecps */
                while (sc->digestrecps != NULL) {
-                       fprintf(fp, "digestrecp|%s\n", sc->digestrecps->name);
+                       StrBufAppendPrintf(Cfg, "digestrecp|%s\n", sc->digestrecps->name);
                        nptr = sc->digestrecps->next;
                        free(sc->digestrecps);
                        sc->digestrecps = nptr;
                }
                /* Do the same for participates */
                while (sc->participates != NULL) {
-                       fprintf(fp, "participate|%s\n", sc->participates->name);
+                       StrBufAppendPrintf(Cfg, "participate|%s\n", sc->participates->name);
                        nptr = sc->participates->next;
                        free(sc->participates);
                        sc->participates = nptr;
                }
                while (sc->ignet_push_shares != NULL) {
-                       fprintf(fp, "ignet_push_share|%s", sc->ignet_push_shares->remote_nodename);
+                       StrBufAppendPrintf(Cfg, "ignet_push_share|%s", sc->ignet_push_shares->remote_nodename);
                        if (!IsEmptyStr(sc->ignet_push_shares->remote_roomname)) {
-                               fprintf(fp, "|%s", sc->ignet_push_shares->remote_roomname);
+                               StrBufAppendPrintf(Cfg, "|%s", sc->ignet_push_shares->remote_roomname);
                        }
-                       fprintf(fp, "\n");
+                       StrBufAppendPrintf(Cfg, "\n");
                        mptr = sc->ignet_push_shares->next;
                        free(sc->ignet_push_shares);
                        sc->ignet_push_shares = mptr;
                }
                if (sc->misc != NULL) {
-                       fwrite(sc->misc, strlen(sc->misc), 1, fp);
+                       StrBufAppendBufPlain(Cfg, sc->misc, -1, 0);
                }
                free(sc->misc);
 
-               fclose(fp);
+               rc = write(TmpFD, ChrPtr(Cfg), StrLength(Cfg));
+               if ((rc >=0 ) && (rc == StrLength(Cfg))) 
+               {
+                       close(TmpFD);
+                       rename(tempfilename, filename);
+               }
+               else {
+                       CtdlLogPrintf(CTDL_EMERG, 
+                                     "unable to write %s; [%s]; not enough space on the disk?\n", 
+                                     tempfilename, 
+                                     strerror(errno));
+                       close(TmpFD);
+                       unlink(tempfilename);
+               }
+               FreeStrBuf(&Cfg);
                free(sc);
                *scc=NULL;
        }
@@ -1537,15 +1625,17 @@ void network_process_buffer(char *buffer, long size) {
                                        strcpy(nexthop, msg->cm_fields['D']);
                                }
                                snprintf(filename, 
-                                                sizeof filename,
-                                                "%s/%s",
-                                                ctdl_netout_dir,
-                                                nexthop);
+                                       sizeof filename,
+                                       "%s/%s@%lx%x",
+                                       ctdl_netout_dir,
+                                       nexthop,
+                                       time(NULL),
+                                       rand()
+                               );
                                CtdlLogPrintf(CTDL_DEBUG, "Appending to %s\n", filename);
                                fp = fopen(filename, "ab");
                                if (fp != NULL) {
-                                       fwrite(sermsg.ser,
-                                               sermsg.len, 1, fp);
+                                       fwrite(sermsg.ser, sermsg.len, 1, fp);
                                        fclose(fp);
                                }
                                else {
@@ -1732,10 +1822,11 @@ void network_do_spoolin(void) {
        while (d = readdir(dp), d != NULL) {
                if ((strcmp(d->d_name, ".")) && (strcmp(d->d_name, ".."))) {
                        snprintf(filename, 
-                                        sizeof filename,
-                                        "%s/%s",
-                                        ctdl_netin_dir,
-                                        d->d_name);
+                               sizeof filename,
+                               "%s/%s",
+                               ctdl_netin_dir,
+                               d->d_name
+                       );
                        network_process_file(filename);
                }
        }
@@ -1744,15 +1835,60 @@ void network_do_spoolin(void) {
 }
 
 /*
- * Delete any files in the outbound queue that were intended
- * to be sent to nodes which no longer exist.
+ * Step 1: consolidate files in the outbound queue into one file per neighbor node
+ * Step 2: delete any files in the outbound queue that were for neighbors who no longer exist.
  */
-void network_purge_spoolout(void) {
+void network_consolidate_spoolout(void) {
        DIR *dp;
        struct dirent *d;
        char filename[PATH_MAX];
+       char cmd[PATH_MAX];
        char nexthop[256];
        int i;
+       char *ptr;
+
+       /* Step 1: consolidate files in the outbound queue into one file per neighbor node */
+       dp = opendir(ctdl_netout_dir);
+       if (dp == NULL) return;
+       while (d = readdir(dp), d != NULL) {
+               if (
+                       (strcmp(d->d_name, "."))
+                       && (strcmp(d->d_name, ".."))
+                       && (strchr(d->d_name, '@') != NULL)
+               ) {
+                       safestrncpy(nexthop, d->d_name, sizeof nexthop);
+                       ptr = strchr(nexthop, '@');
+                       if (ptr) *ptr = 0;
+       
+                       snprintf(filename, 
+                               sizeof filename,
+                               "%s/%s",
+                               ctdl_netout_dir,
+                               d->d_name
+                       );
+       
+                       CtdlLogPrintf(CTDL_DEBUG, "Consolidate %s to %s\n", filename, nexthop);
+                       if (network_talking_to(nexthop, NTT_CHECK)) {
+                               CtdlLogPrintf(CTDL_DEBUG,
+                                       "Currently online with %s - skipping for now\n",
+                                       nexthop
+                               );
+                       }
+                       else {
+                               network_talking_to(nexthop, NTT_ADD);
+                               snprintf(cmd, sizeof cmd, "/bin/cat %s >>%s/%s && /bin/rm -f %s",
+                                       filename,
+                                       ctdl_netout_dir, nexthop,
+                                       filename
+                               );
+                               system(cmd);
+                               network_talking_to(nexthop, NTT_REMOVE);
+                       }
+               }
+       }
+       closedir(dp);
+
+       /* Step 2: delete any files in the outbound queue that were for neighbors who no longer exist */
 
        dp = opendir(ctdl_netout_dir);
        if (dp == NULL) return;
@@ -1760,11 +1896,15 @@ void network_purge_spoolout(void) {
        while (d = readdir(dp), d != NULL) {
                if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
                        continue;
+               ptr = strchr(d->d_name, '@');
+               if (d != NULL)
+                       continue;
                snprintf(filename, 
-                                sizeof filename,
-                                "%s/%s",
-                                ctdl_netout_dir,
-                                d->d_name);
+                       sizeof filename,
+                       "%s/%s",
+                       ctdl_netout_dir,
+                       d->d_name
+               );
 
                strcpy(nexthop, "");
                i = is_valid_node(nexthop, NULL, d->d_name);
@@ -1783,18 +1923,33 @@ void network_purge_spoolout(void) {
  * receive network spool from the remote system
  */
 void receive_spool(int *sock, char *remote_nodename) {
-       size_t siz;
        long download_len = 0L;
        long bytes_received = 0L;
-       long bytes_copied = 0L;
        char buf[SIZ];
        static char pbuf[IGNET_PACKET_SIZE];
        char tempfilename[PATH_MAX];
-       char filename[PATH_MAX];
+       char permfilename[PATH_MAX];
        long plen;
-       FILE *fp, *newfp;
+       FILE *fp;
+
+       snprintf(tempfilename, 
+               sizeof tempfilename, 
+               "%s/%s.%lx%x",
+               ctdl_nettmp_dir,
+               remote_nodename, 
+               time(NULL),
+               rand()
+       );
+
+       snprintf(permfilename, 
+               sizeof permfilename, 
+               "%s/%s.%lx%x",
+               ctdl_netin_dir,
+               remote_nodename, 
+               time(NULL),
+               rand()
+       );
 
-       CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
        if (sock_puts(sock, "NDOP") < 0) return;
        if (sock_getln(sock, buf, sizeof buf) < 0) return;
        CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
@@ -1803,63 +1958,70 @@ void receive_spool(int *sock, char *remote_nodename) {
        }
        download_len = extract_long(&buf[4], 0);
 
-       bytes_received = 0L;
-       fp = fopen(tempfilename, "w");
-       if (fp == NULL) {
-               CtdlLogPrintf(CTDL_CRIT, "cannot open download file locally: %s\n",
-                       strerror(errno));
-               return;
-       }
-
-       while (bytes_received < download_len) {
-               /**
-                * If shutting down we can exit here and unlink the temp file.
-                * this shouldn't loose us any messages.
-                */
-               if (CtdlThreadCheckStop())
-               {
-                       fclose(fp);
-                       unlink(tempfilename);
-                       return;
-               }
-               snprintf(buf, sizeof buf, "READ %ld|%ld",
-                       bytes_received,
-                    ((download_len - bytes_received > IGNET_PACKET_SIZE)
-                ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
-               if (sock_puts(sock, buf) < 0) {
-                       fclose(fp);
-                       unlink(tempfilename);
+       if (download_len>0) {
+               bytes_received = 0L;
+               fp = fopen(tempfilename, "w");
+               if (fp == NULL) {
+                       CtdlLogPrintf(CTDL_CRIT, "cannot open download file locally: %s\n",
+                                     strerror(errno));
                        return;
                }
-               if (sock_getln(sock, buf, sizeof buf) < 0) {
-                       fclose(fp);
-                       unlink(tempfilename);
-                       return;
-               }
-               if (buf[0] == '6') {
-                       plen = extract_long(&buf[4], 0);
-                       if (sock_read(sock, pbuf, plen, 1) < 0) {
+
+               CtdlLogPrintf(CTDL_DEBUG, "For this download we are expecting %d bytes\n", download_len);
+               while (bytes_received < download_len) {
+                       /*
+                        * If shutting down we can exit here and unlink the temp file.
+                        * this shouldn't loose us any messages.
+                        */
+                       if (CtdlThreadCheckStop())
+                       {
+                               fclose(fp);
+                               unlink(tempfilename);
+                               return;
+                       }
+                       snprintf(buf, sizeof buf, "READ %ld|%ld",
+                                bytes_received,
+                                ((download_len - bytes_received > IGNET_PACKET_SIZE)
+                                 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
+                       
+                       if (sock_puts(sock, buf) < 0) {
+                               fclose(fp);
+                               unlink(tempfilename);
+                               return;
+                       }
+                       if (sock_getln(sock, buf, sizeof buf) < 0) {
                                fclose(fp);
                                unlink(tempfilename);
                                return;
                        }
-                       fwrite((char *) pbuf, plen, 1, fp);
-                       bytes_received = bytes_received + plen;
+                       
+                       if (buf[0] == '6') {
+                               plen = extract_long(&buf[4], 0);
+                               if (sock_read(sock, pbuf, plen, 1) < 0) {
+                                       fclose(fp);
+                                       unlink(tempfilename);
+                                       return;
+                               }
+                               fwrite((char *) pbuf, plen, 1, fp);
+                               bytes_received = bytes_received + plen;
+                       }
                }
-       }
 
-       fclose(fp);
-       /** Last chance for shutdown exit */
+               fclose(fp);
+       }
+       /* Last chance for shutdown exit */
        if (CtdlThreadCheckStop())
        {
                unlink(tempfilename);
                return;
        }
+
        if (sock_puts(sock, "CLOS") < 0) {
                unlink(tempfilename);
                return;
        }
-       /**
+
+       /*
         * From here on we must complete or messages will get lost
         */
        if (sock_getln(sock, buf, sizeof buf) < 0) {
@@ -1871,36 +2033,13 @@ void receive_spool(int *sock, char *remote_nodename) {
        }
        CtdlLogPrintf(CTDL_DEBUG, "%s\n", buf);
        
-       /* Now copy the temp file to its permanent location.
-        * (We copy instead of link because they may be on different filesystems)
+       /* Now move the temp file to its permanent location.
         */
-       begin_critical_section(S_NETSPOOL);
-       snprintf(filename, 
-                        sizeof filename, 
-                        "%s/%s.%ld",
-                        ctdl_netin_dir,
-                        remote_nodename, 
-                        (long) getpid()
-       );
-       fp = fopen(tempfilename, "r");
-       if (fp != NULL) {
-               newfp = fopen(filename, "w");
-               if (newfp != NULL) {
-                       bytes_copied = 0L;
-                       while (bytes_copied < download_len) {
-                               plen = download_len - bytes_copied;
-                               if (plen > sizeof buf) {
-                                       plen = sizeof buf;
-                               }
-                               siz = fread(buf, plen, 1, fp);
-                               fwrite(buf, plen, 1, newfp);
-                               bytes_copied += plen;
-                       }
-                       fclose(newfp);
-               }
-               fclose(fp);
+       if (link(tempfilename, permfilename) != 0) {
+               CtdlLogPrintf(CTDL_ALERT, "Could not link %s to %s: %s\n",
+                       tempfilename, permfilename, strerror(errno)
+               );
        }
-       end_critical_section(S_NETSPOOL);
        unlink(tempfilename);
 }
 
@@ -1926,14 +2065,14 @@ void transmit_spool(int *sock, char *remote_nodename)
        }
 
        snprintf(sfname, sizeof sfname, 
-                        "%s/%s",
-                        ctdl_netout_dir,
-                        remote_nodename);
+               "%s/%s",
+               ctdl_netout_dir,
+               remote_nodename
+       );
        fd = open(sfname, O_RDONLY);
        if (fd < 0) {
                if (errno != ENOENT) {
-                       CtdlLogPrintf(CTDL_CRIT, "cannot open upload file locally: %s\n",
-                               strerror(errno));
+                       CtdlLogPrintf(CTDL_CRIT, "cannot open %s: %s\n", sfname, strerror(errno));
                }
                return;
        }
@@ -1941,7 +2080,7 @@ void transmit_spool(int *sock, char *remote_nodename)
        while (plen = (long) read(fd, pbuf, IGNET_PACKET_SIZE), plen > 0L) {
                bytes_to_write = plen;
                while (bytes_to_write > 0L) {
-                       /** Exit if shutting down */
+                       /* Exit if shutting down */
                        if (CtdlThreadCheckStop())
                        {
                                close(fd);
@@ -1959,8 +2098,7 @@ void transmit_spool(int *sock, char *remote_nodename)
                        }
                        thisblock = atol(&buf[4]);
                        if (buf[0] == '7') {
-                               if (sock_write(sock, pbuf,
-                                  (int) thisblock) < 0) {
+                               if (sock_write(sock, pbuf, (int) thisblock) < 0) {
                                        close(fd);
                                        return;
                                }
@@ -1974,17 +2112,18 @@ void transmit_spool(int *sock, char *remote_nodename)
 
 ABORTUPL:
        close(fd);
-       /** Last chance for shutdown exit */
+
+       /* Last chance for shutdown exit */
        if(CtdlThreadCheckStop())
                return;
                
        if (sock_puts(sock, "UCLS 1") < 0) return;
-       /**
+
+       /*
         * From here on we must complete or messages will get lost
         */
        if (sock_getln(sock, buf, sizeof buf) < 0) return;
-       CtdlLogPrintf(CTDL_NOTICE, "Sent %ld octets to <%s>\n",
-                       bytes_written, remote_nodename);
+       CtdlLogPrintf(CTDL_NOTICE, "Sent %ld octets to <%s>\n", bytes_written, remote_nodename);
        CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
        if (buf[0] == '2') {
                CtdlLogPrintf(CTDL_DEBUG, "Removing <%s>\n", sfname);
@@ -2131,6 +2270,10 @@ void create_spool_dirs(void) {
                CtdlLogPrintf(CTDL_EMERG, "unable to create directory [%s]: %s", ctdl_netin_dir, strerror(errno));
        if (chown(ctdl_netin_dir, CTDLUID, (-1)) != 0)
                CtdlLogPrintf(CTDL_EMERG, "unable to set the access rights for [%s]: %s", ctdl_netin_dir, strerror(errno));
+       if ((mkdir(ctdl_nettmp_dir, 0700) != 0) && (errno != EEXIST))
+               CtdlLogPrintf(CTDL_EMERG, "unable to create directory [%s]: %s", ctdl_nettmp_dir, strerror(errno));
+       if (chown(ctdl_nettmp_dir, CTDLUID, (-1)) != 0)
+               CtdlLogPrintf(CTDL_EMERG, "unable to set the access rights for [%s]: %s", ctdl_nettmp_dir, strerror(errno));
        if ((mkdir(ctdl_netout_dir, 0700) != 0) && (errno != EEXIST))
                CtdlLogPrintf(CTDL_EMERG, "unable to create directory [%s]: %s", ctdl_netout_dir, strerror(errno));
        if (chown(ctdl_netout_dir, CTDLUID, (-1)) != 0)
@@ -2174,6 +2317,7 @@ void *network_do_queue(void *args) {
         * with a static variable instead.
         */
        if (doing_queue) {
+               CtdlClearSystemContext();
                return NULL;
        }
        doing_queue = 1;
@@ -2240,7 +2384,7 @@ void *network_do_queue(void *args) {
        free_filter_list(filterlist);
        filterlist = NULL;
 
-       network_purge_spoolout();
+       network_consolidate_spoolout();
 
        CtdlLogPrintf(CTDL_DEBUG, "network: queue run completed\n");
 
@@ -2261,6 +2405,7 @@ void *network_do_queue(void *args) {
        else {
                CtdlLogPrintf(CTDL_DEBUG, "network: Task STOPPED.\n");
        }
+       CtdlClearSystemContext();
        return NULL;
 }
 
@@ -2283,12 +2428,6 @@ void cmd_netp(char *cmdbuf)
        extract_token(node, cmdbuf, 0, '|', sizeof node);
        extract_token(pass, cmdbuf, 1, '|', sizeof pass);
 
-       if (doing_queue) {
-               CtdlLogPrintf(CTDL_WARNING, "Network node <%s> refused - spooling\n", node);
-               cprintf("%d spooling - try again in a few minutes\n", ERROR + RESOURCE_BUSY);
-               return;
-       }
-
        /* load the IGnet Configuration to check node validity */
        load_working_ignetcfg();
        v = is_valid_node(nexthop, secret, node);