From 02862316ec5cf51e9153aef6c2e3622494735d3b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Wilfried=20G=C3=B6esgens?= Date: Fri, 12 Feb 2010 20:45:14 +0000 Subject: [PATCH] * make the write -1 aware here too. * don't close -1 --- citadel/clientsocket.c | 13 +++++++---- citadel/clientsocket.h | 4 ++-- citadel/modules/clamav/serv_virus.c | 7 +++--- citadel/modules/network/serv_network.c | 22 ++++++++++--------- citadel/modules/pop3client/serv_pop3client.c | 18 ++++++++------- citadel/modules/smtp/serv_smtp.c | 23 ++++++++++---------- citadel/modules/spam/serv_spam.c | 7 +++--- citadel/msgbase.c | 1 - 8 files changed, 53 insertions(+), 42 deletions(-) diff --git a/citadel/clientsocket.c b/citadel/clientsocket.c index 7c69fe673..3a3ac483e 100644 --- a/citadel/clientsocket.c +++ b/citadel/clientsocket.c @@ -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; diff --git a/citadel/clientsocket.h b/citadel/clientsocket.h index fcaf034b6..6d5189d4a 100644 --- a/citadel/clientsocket.h +++ b/citadel/clientsocket.h @@ -23,11 +23,11 @@ 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); /* diff --git a/citadel/modules/clamav/serv_virus.c b/citadel/modules/clamav/serv_virus.c index 6aeeaa9bf..42cfee005 100644 --- a/citadel/modules/clamav/serv_virus.c +++ b/citadel/modules/clamav/serv_virus.c @@ -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"); diff --git a/citadel/modules/network/serv_network.c b/citadel/modules/network/serv_network.c index e2d16dd9a..2ac84e3af 100644 --- a/citadel/modules/network/serv_network.c +++ b/citadel/modules/network/serv_network.c @@ -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); } diff --git a/citadel/modules/pop3client/serv_pop3client.c b/citadel/modules/pop3client/serv_pop3client.c index 7cc41a177..775a892ce 100644 --- a/citadel/modules/pop3client/serv_pop3client.c +++ b/citadel/modules/pop3client/serv_pop3client.c @@ -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, "\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); } diff --git a/citadel/modules/smtp/serv_smtp.c b/citadel/modules/smtp/serv_smtp.c index f673165ad..152c1b26a 100644 --- a/citadel/modules/smtp/serv_smtp.c +++ b/citadel/modules/smtp/serv_smtp.c @@ -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). diff --git a/citadel/modules/spam/serv_spam.c b/citadel/modules/spam/serv_spam.c index 7ff266dea..840fd749f 100644 --- a/citadel/modules/spam/serv_spam.c +++ b/citadel/modules/spam/serv_spam.c @@ -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"); diff --git a/citadel/msgbase.c b/citadel/msgbase.c index c31e36154..fc19974db 100644 --- a/citadel/msgbase.c +++ b/citadel/msgbase.c @@ -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; -- 2.30.2