* make the write -1 aware here too.
authorWilfried Göesgens <willi@citadel.org>
Fri, 12 Feb 2010 20:45:14 +0000 (20:45 +0000)
committerWilfried Göesgens <willi@citadel.org>
Fri, 12 Feb 2010 20:45:14 +0000 (20:45 +0000)
* don't close -1

citadel/clientsocket.c
citadel/clientsocket.h
citadel/modules/clamav/serv_virus.c
citadel/modules/network/serv_network.c
citadel/modules/pop3client/serv_pop3client.c
citadel/modules/smtp/serv_smtp.c
citadel/modules/spam/serv_spam.c
citadel/msgbase.c

index 7c69fe67333c93d3d82698f0291c3e078e667fb0..3a3ac483eb7309ce4fa595f623b60ff8022ff0b5 100644 (file)
@@ -274,14 +274,19 @@ INLINE int sock_read(int *sock, char *buf, int bytes, int keep_reading_until_ful
  * sock_write() - send binary to server.
  * Returns the number of bytes written, or -1 for error.
  */
-int sock_write(int sock, char *buf, int nbytes)
+int sock_write(int *sock, char *buf, int nbytes)
 {
        int bytes_written = 0;
        int retval;
-       while (bytes_written < nbytes) {
-               retval = write(sock, &buf[bytes_written],
+
+       while ((*sock != -1)  && 
+              (bytes_written < nbytes))
+       {
+               retval = write(*sock, &buf[bytes_written],
                               nbytes - bytes_written);
                if (retval < 1) {
+                       sock_close(*sock);
+                       *sock = -1;
                        return (-1);
                }
                bytes_written = bytes_written + retval;
@@ -316,7 +321,7 @@ int ml_sock_gets(int *sock, char *buf) {
  * sock_puts() - send line to server - implemented in terms of serv_write()
  * Returns the number of bytes written, or -1 for error.
  */
-int sock_puts(int sock, char *buf)
+int sock_puts(int *sock, char *buf)
 {
        int i, j;
 
index fcaf034b6944375365fc22ccbaf6e03a5bfb9495..6d5189d4a5f9bbd64a3e762213122ce125b7aca8 100644 (file)
 int sock_connect(char *host, char *service, char *protocol);
 int sock_read_to(int *sock, char *buf, int bytes, int timeout, int keep_reading_until_full);
 int sock_read(int *sock, char *buf, int bytes, int keep_reading_until_full);
-int sock_write(int sock, char *buf, int nbytes);
+int sock_write(int *sock, char *buf, int nbytes);
 int ml_sock_gets(int *sock, char *buf);
 int sock_getln(int *sock, char *buf, int bufsize);
 int CtdlSockGetLine(int *sock, StrBuf *Target);
-int sock_puts(int sock, char *buf);
+int sock_puts(int *sock, char *buf);
 
 
 /* 
index 6aeeaa9bf3819a2654ee9085b890e634018ede97..42cfee005465f40594aa9bda1937b1288738e029 100644 (file)
@@ -121,7 +121,7 @@ int clamd(struct CtdlMessage *msg) {
        /* Command */
        CtdlLogPrintf(CTDL_DEBUG, "Transmitting STREAM command\n");
        sprintf(buf, "STREAM\r\n");
-       sock_write(sock, buf, strlen(buf));
+       sock_write(&sock, buf, strlen(buf));
 
        CtdlLogPrintf(CTDL_DEBUG, "Waiting for PORT number\n");
         if (sock_getln(&sock, buf, sizeof buf) < 0) {
@@ -162,13 +162,14 @@ int clamd(struct CtdlMessage *msg) {
        CC->redirect_len = 0;
        CC->redirect_alloc = 0;
 
-       sock_write(streamsock, msgtext, msglen);
+       sock_write(&streamsock, msgtext, msglen);
        free(msgtext);
 
        /* Close the streamsocket connection; this tells clamd
         * that we're done.
         */
-       close(streamsock);
+       if (streamsock != -1)
+               close(streamsock);
        
        /* Response */
        CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n");
index e2d16dd9aa08e521799e10ba0cbfa84cdbf15c23..2ac84e3afa3159e38b2be0e2bd6e1d130f6400df 100644 (file)
@@ -1799,7 +1799,7 @@ void receive_spool(int *sock, char *remote_nodename) {
        FILE *fp, *newfp;
 
        CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
-       if (sock_puts(*sock, "NDOP") < 0) return;
+       if (sock_puts(sock, "NDOP") < 0) return;
        if (sock_getln(sock, buf, sizeof buf) < 0) return;
        CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
        if (buf[0] != '2') {
@@ -1830,7 +1830,7 @@ void receive_spool(int *sock, char *remote_nodename) {
                        bytes_received,
                     ((download_len - bytes_received > IGNET_PACKET_SIZE)
                 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
-               if (sock_puts(*sock, buf) < 0) {
+               if (sock_puts(sock, buf) < 0) {
                        fclose(fp);
                        unlink(tempfilename);
                        return;
@@ -1859,7 +1859,7 @@ void receive_spool(int *sock, char *remote_nodename) {
                unlink(tempfilename);
                return;
        }
-       if (sock_puts(*sock, "CLOS") < 0) {
+       if (sock_puts(sock, "CLOS") < 0) {
                unlink(tempfilename);
                return;
        }
@@ -1922,7 +1922,7 @@ void transmit_spool(int *sock, char *remote_nodename)
        int fd;
        char sfname[128];
 
-       if (sock_puts(*sock, "NUOP") < 0) return;
+       if (sock_puts(sock, "NUOP") < 0) return;
        if (sock_getln(sock, buf, sizeof buf) < 0) return;
        CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
        if (buf[0] != '2') {
@@ -1953,7 +1953,7 @@ void transmit_spool(int *sock, char *remote_nodename)
                        }
                        
                        snprintf(buf, sizeof buf, "WRIT %ld", bytes_to_write);
-                       if (sock_puts(*sock, buf) < 0) {
+                       if (sock_puts(sock, buf) < 0) {
                                close(fd);
                                return;
                        }
@@ -1963,7 +1963,7 @@ void transmit_spool(int *sock, char *remote_nodename)
                        }
                        thisblock = atol(&buf[4]);
                        if (buf[0] == '7') {
-                               if (sock_write(*sock, pbuf,
+                               if (sock_write(sock, pbuf,
                                   (int) thisblock) < 0) {
                                        close(fd);
                                        return;
@@ -1982,7 +1982,7 @@ ABORTUPL:
        if(CtdlThreadCheckStop())
                return;
                
-       if (sock_puts(*sock, "UCLS 1") < 0) return;
+       if (sock_puts(sock, "UCLS 1") < 0) return;
        /**
         * From here on we must complete or messages will get lost
         */
@@ -2036,7 +2036,7 @@ void network_poll_node(char *node, char *secret, char *host, char *port) {
                /* We're talking to the correct node.  Now identify ourselves. */
                snprintf(buf, sizeof buf, "NETP %s|%s", config.c_nodename, secret);
                CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
-               if (sock_puts(sock, buf) <0) goto bail;
+               if (sock_puts(&sock, buf) <0) goto bail;
                if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
                CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
                if (buf[0] != '2') goto bail;
@@ -2048,8 +2048,10 @@ void network_poll_node(char *node, char *secret, char *host, char *port) {
                        transmit_spool(&sock, node);
        }
 
-       sock_puts(sock, "QUIT");
-bail:  sock_close(sock);
+       sock_puts(&sock, "QUIT");
+bail:  
+       if (sock != -1)
+               sock_close(sock);
        network_talking_to(node, NTT_REMOVE);
 }
 
index 7cc41a1778f42b6e020353cd2dc1269d8a43dcf5..775a892ce3c19c46f3eed38c5c57484164d0403e 100644 (file)
@@ -115,7 +115,7 @@ void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3
         */
        snprintf(buf, sizeof buf, "USER %s\r", pop3user);
        CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
-       if (sock_puts(sock, buf) <0) goto bail;
+       if (sock_puts(&sock, buf) <0) goto bail;
        if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
        CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
        if (strncasecmp(buf, "+OK", 3)) goto bail;
@@ -126,7 +126,7 @@ void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3
        /* Password */
        snprintf(buf, sizeof buf, "PASS %s\r", pop3pass);
        CtdlLogPrintf(CTDL_DEBUG, "<PASS <password>\n");
-       if (sock_puts(sock, buf) <0) goto bail;
+       if (sock_puts(&sock, buf) <0) goto bail;
        if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
        CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
        if (strncasecmp(buf, "+OK", 3)) goto bail;
@@ -137,7 +137,7 @@ void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3
        /* Get the list of messages */
        snprintf(buf, sizeof buf, "LIST\r");
        CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
-       if (sock_puts(sock, buf) <0) goto bail;
+       if (sock_puts(&sock, buf) <0) goto bail;
        if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
        CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
        if (strncasecmp(buf, "+OK", 3)) goto bail;
@@ -171,7 +171,7 @@ void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3
                /* Find out the UIDL of the message, to determine whether we've already downloaded it */
                snprintf(buf, sizeof buf, "UIDL %d\r", msglist[i]);
                CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
-               if (sock_puts(sock, buf) <0) goto bail;
+               if (sock_puts(&sock, buf) <0) goto bail;
                if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
                CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
                if (strncasecmp(buf, "+OK", 3)) goto bail;
@@ -197,7 +197,7 @@ void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3
                        /* Message has not been seen. Tell the server to fetch the message... */
                        snprintf(buf, sizeof buf, "RETR %d\r", msglist[i]);
                        CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
-                       if (sock_puts(sock, buf) <0) goto bail;
+                       if (sock_puts(&sock, buf) <0) goto bail;
                        if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
                        CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
                        if (strncasecmp(buf, "+OK", 3)) goto bail;
@@ -221,7 +221,7 @@ void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3
                                if (!keep) {
                                        snprintf(buf, sizeof buf, "DELE %d\r", msglist[i]);
                                        CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
-                                       if (sock_puts(sock, buf) <0) goto bail;
+                                       if (sock_puts(&sock, buf) <0) goto bail;
                                        if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
                                        CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf); /* errors here are non-fatal */
                                }
@@ -239,10 +239,12 @@ void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3
        /* Log out */
        snprintf(buf, sizeof buf, "QUIT\r");
        CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
-       if (sock_puts(sock, buf) <0) goto bail;
+       if (sock_puts(&sock, buf) <0) goto bail;
        if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
        CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
-bail:  sock_close(sock);
+bail:  
+       if (sock != -1)
+               sock_close(sock);
        if (msglist) free(msglist);
 }
 
index f673165ad2316997999c4d428e48602411663108..152c1b26a461747fd88958bf37c859eb439b5d5e 100644 (file)
@@ -1127,7 +1127,7 @@ void smtp_try(const char *key, const char *addr, int *status,
        /* Do a EHLO command.  If it fails, try the HELO command. */
        snprintf(buf, sizeof buf, "EHLO %s\r\n", config.c_fqdn);
        CtdlLogPrintf(CTDL_DEBUG, ">%s", buf);
-       sock_write(sock, buf, strlen(buf));
+       sock_write(&sock, buf, strlen(buf));
        if (ml_sock_gets(&sock, buf) < 0) {
                *status = 4;
                strcpy(dsn, "Connection broken during SMTP HELO");
@@ -1137,7 +1137,7 @@ void smtp_try(const char *key, const char *addr, int *status,
        if (buf[0] != '2') {
                snprintf(buf, sizeof buf, "HELO %s\r\n", config.c_fqdn);
                CtdlLogPrintf(CTDL_DEBUG, ">%s", buf);
-               sock_write(sock, buf, strlen(buf));
+               sock_write(&sock, buf, strlen(buf));
                if (ml_sock_gets(&sock, buf) < 0) {
                        *status = 4;
                        strcpy(dsn, "Connection broken during SMTP HELO");
@@ -1164,7 +1164,7 @@ void smtp_try(const char *key, const char *addr, int *status,
                CtdlEncodeBase64(encoded, buf, strlen(mx_user) + strlen(mx_user) + strlen(mx_pass) + 2, 0);
                snprintf(buf, sizeof buf, "AUTH PLAIN %s\r\n", encoded);
                CtdlLogPrintf(CTDL_DEBUG, ">%s", buf);
-               sock_write(sock, buf, strlen(buf));
+               sock_write(&sock, buf, strlen(buf));
                if (ml_sock_gets(&sock, buf) < 0) {
                        *status = 4;
                        strcpy(dsn, "Connection broken during SMTP AUTH");
@@ -1188,7 +1188,7 @@ void smtp_try(const char *key, const char *addr, int *status,
        /* previous command succeeded, now try the MAIL FROM: command */
        snprintf(buf, sizeof buf, "MAIL FROM:<%s>\r\n", envelope_from);
        CtdlLogPrintf(CTDL_DEBUG, ">%s", buf);
-       sock_write(sock, buf, strlen(buf));
+       sock_write(&sock, buf, strlen(buf));
        if (ml_sock_gets(&sock, buf) < 0) {
                *status = 4;
                strcpy(dsn, "Connection broken during SMTP MAIL");
@@ -1211,7 +1211,7 @@ void smtp_try(const char *key, const char *addr, int *status,
        /* MAIL succeeded, now try the RCPT To: command */
        snprintf(buf, sizeof buf, "RCPT TO:<%s@%s>\r\n", user, node);
        CtdlLogPrintf(CTDL_DEBUG, ">%s", buf);
-       sock_write(sock, buf, strlen(buf));
+       sock_write(&sock, buf, strlen(buf));
        if (ml_sock_gets(&sock, buf) < 0) {
                *status = 4;
                strcpy(dsn, "Connection broken during SMTP RCPT");
@@ -1233,7 +1233,7 @@ void smtp_try(const char *key, const char *addr, int *status,
 
        /* RCPT succeeded, now try the DATA command */
        CtdlLogPrintf(CTDL_DEBUG, ">DATA\n");
-       sock_write(sock, "DATA\r\n", 6);
+       sock_write(&sock, "DATA\r\n", 6);
        if (ml_sock_gets(&sock, buf) < 0) {
                *status = 4;
                strcpy(dsn, "Connection broken during SMTP DATA");
@@ -1254,15 +1254,15 @@ void smtp_try(const char *key, const char *addr, int *status,
        }
 
        /* If we reach this point, the server is expecting data.*/
-       sock_write(sock, msgtext, msg_size);
+       sock_write(&sock, msgtext, msg_size);
        if (msgtext[msg_size-1] != 10) {
                CtdlLogPrintf(CTDL_WARNING, "Possible problem: message did not "
                        "correctly terminate. (expecting 0x10, got 0x%02x)\n",
                                buf[msg_size-1]);
-               sock_write(sock, "\r\n", 2);
+               sock_write(&sock, "\r\n", 2);
        }
 
-       sock_write(sock, ".\r\n", 3);
+       sock_write(&sock, ".\r\n", 3);
        if (ml_sock_gets(&sock, buf) < 0) {
                *status = 4;
                strcpy(dsn, "Connection broken during SMTP message transmit");
@@ -1287,14 +1287,15 @@ void smtp_try(const char *key, const char *addr, int *status,
        *status = 2;
 
        CtdlLogPrintf(CTDL_DEBUG, ">QUIT\n");
-       sock_write(sock, "QUIT\r\n", 6);
+       sock_write(&sock, "QUIT\r\n", 6);
        ml_sock_gets(&sock, buf);
        CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
        CtdlLogPrintf(CTDL_INFO, "SMTP client: delivery to <%s> @ <%s> (%s) succeeded\n",
                user, node, name);
 
 bail:  free(msgtext);
-       sock_close(sock);
+       if (sock != -1)
+               sock_close(sock);
 
        /* Write something to the syslog (which may or may not be where the
         * rest of the Citadel logs are going; some sysadmins want LOG_MAIL).
index 7ff266dea300b71a38d91f492a2b81849f042004..840fd749f330b03fb635c2b9484d53cb39f5e084 100644 (file)
@@ -110,7 +110,7 @@ int spam_assassin(struct CtdlMessage *msg) {
        /* Command */
        CtdlLogPrintf(CTDL_DEBUG, "Transmitting command\n");
        sprintf(buf, "CHECK SPAMC/1.2\r\n\r\n");
-       sock_write(sock, buf, strlen(buf));
+       sock_write(&sock, buf, strlen(buf));
 
        /* Message */
        CC->redirect_buffer = malloc(SIZ);
@@ -123,13 +123,14 @@ int spam_assassin(struct CtdlMessage *msg) {
        CC->redirect_len = 0;
        CC->redirect_alloc = 0;
 
-       sock_write(sock, msgtext, msglen);
+       sock_write(&sock, msgtext, msglen);
        free(msgtext);
 
        /* Close one end of the socket connection; this tells SpamAssassin
         * that we're done.
         */
-       sock_shutdown(sock, SHUT_WR);
+       if (sock != -1)
+               sock_shutdown(sock, SHUT_WR);
        
        /* Response */
        CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n");
index c31e3615462d725381eb051c840b1dbefb550d98..fc19974db36c778b190dc4af18f8befa56dfdcd8 100644 (file)
@@ -3234,7 +3234,6 @@ StrBuf *CtdlReadMessageBodyBuf(char *terminator,  /* token signalling EOT */
 {
        StrBuf *Message;
        StrBuf *LineBuf;
-       char buf[1024];
        int flushing = 0;
        int finished = 0;
        int dotdot = 0;