* mime_parser.c: change to memory allocation algorithm ... some badly done
authorArt Cancro <ajc@citadel.org>
Tue, 4 Dec 2001 05:16:19 +0000 (05:16 +0000)
committerArt Cancro <ajc@citadel.org>
Tue, 4 Dec 2001 05:16:19 +0000 (05:16 +0000)
  messages were crashing the server

citadel/ChangeLog
citadel/client_passwords.c
citadel/mime_parser.c
citadel/serv_imap.c
citadel/serv_smtp.c
citadel/server.h
citadel/tools.c
citadel/tools.h

index 3e880e45504845eaef4f4dc11a849c42f3fc84c3..77c5f538de08fca75198d7b7c53a576d8702881e 100644 (file)
@@ -1,4 +1,8 @@
  $Log$
+ Revision 580.92  2001/12/04 05:16:19  ajc
+ * mime_parser.c: change to memory allocation algorithm ... some badly done
+   messages were crashing the server
+
  Revision 580.91  2001/12/03 22:48:16  ajc
  * ooops.  Look for the QR2_SYSTEM flag in QRflags2, not QRflags.
 
@@ -2922,3 +2926,4 @@ Sat Jul 11 00:20:48 EDT 1998 Nathan Bryant <bryant@cs.usm.maine.edu>
 
 Fri Jul 10 1998 Art Cancro <ajc@uncensored.citadel.org>
        * Initial CVS import 
+
index a70bcc4faf41de7e4b1390b1e1f497da1c694aa0..75076af09c8db29d99472e2f51d3efaaf6b07604 100644 (file)
@@ -56,7 +56,7 @@ void get_stored_password(
        fp = fopen(pwfile, "r");
        if (fp == NULL) return;
        while (fgets(buf64, sizeof buf64, fp) != NULL) {
-               decode_base64(buf, buf64);
+               decode_base64(buf, buf64, sizeof(buf64));
                extract(hostbuf, buf, 0);
                extract(portbuf, buf, 1);
                extract(ubuf, buf, 2);
@@ -97,7 +97,7 @@ void set_stored_password(
        fp = fopen(pwfile, "w");
        if (fp == NULL) fp = fopen("/dev/null", "w");
        while (fgets(buf64, sizeof buf64, oldfp) != NULL) {
-               decode_base64(buf, buf64);
+               decode_base64(buf, buf64, sizeof(buf64));
                extract(hostbuf, buf, 0);
                extract(portbuf, buf, 1);
                extract(ubuf, buf, 2);
index 1dfd751d166f1221d394d012bc6ad8e15914fb06..0f8cc93f8de618cdc437bbd332ba2c27c1bda3a2 100644 (file)
@@ -192,6 +192,7 @@ void mime_decode(char *partnum,
                        }
                return;
        }
+       
        if ((strcasecmp(encoding, "base64"))
            && (strcasecmp(encoding, "quoted-printable"))) {
                lprintf(9, "ERROR: unknown MIME encoding '%s'\n", encoding);
@@ -201,22 +202,26 @@ void mime_decode(char *partnum,
         * Allocate a buffer for the decoded data.  The output buffer is the
         * same size as the input buffer; this assumes that the decoded data
         * will never be larger than the encoded data.  This is a safe
-        * assumption with base64, uuencode, and quoted-printable.  Just to
-        * be safe, we still pad the buffer a bit.
+        * assumption with base64, uuencode, and quoted-printable.
         */
-       decoded = mallok(length + 1024);
+       lprintf(9, "About to allocate %d bytes for decoded part\n",
+               length+2048);
+       decoded = mallok(length+2048);
        if (decoded == NULL) {
                lprintf(9, "ERROR: cannot allocate memory.\n");
                return;
        }
+       lprintf(9, "Got it!\n");
 
+       lprintf(9, "Decoding %s\n", encoding);
        if (!strcasecmp(encoding, "base64")) {
-               bytes_decoded = decode_base64(decoded, part_start);
+               bytes_decoded = decode_base64(decoded, part_start, length);
        }
        else if (!strcasecmp(encoding, "quoted-printable")) {
                bytes_decoded = decode_quoted_printable(decoded,
                                                        part_start, length);
        }
+       lprintf(9, "Bytes decoded: %d\n", bytes_decoded);
 
        if (bytes_decoded > 0) if (CallBack != NULL) {
                CallBack(name, filename, fixed_partnum(partnum),
index b51141b393959aadffa862a6ed36afa8cbb0718c..c1489d5e057bdff88ad0217685ad75950b99159c 100644 (file)
@@ -316,7 +316,7 @@ void imap_authenticate(int num_parms, char *parms[]) {
 void imap_auth_login_user(char *cmd) {
        char buf[SIZ];
 
-       decode_base64(buf, cmd);
+       decode_base64(buf, cmd, SIZ);
        CtdlLoginExistingUser(buf);
        encode_base64(buf, "Password:");
        cprintf("+ %s\r\n", buf);
@@ -327,7 +327,7 @@ void imap_auth_login_user(char *cmd) {
 void imap_auth_login_pass(char *cmd) {
        char buf[SIZ];
 
-       decode_base64(buf, cmd);
+       decode_base64(buf, cmd, SIZ);
        if (CtdlTryPassword(buf) == pass_ok) {
                cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
        }
index 6bc34c94fbd0fcd89ddd49cec1901df930ffda6d..03e336f65fca5f1dea0e0cb70e2dff98a18a8b72 100644 (file)
@@ -158,7 +158,7 @@ void smtp_get_user(char *argbuf) {
        char buf[SIZ];
        char username[SIZ];
 
-       decode_base64(username, argbuf);
+       decode_base64(username, argbuf, SIZ);
        lprintf(9, "Trying <%s>\n", username);
        if (CtdlLoginExistingUser(username) == login_ok) {
                encode_base64(buf, "Password:");
@@ -178,7 +178,7 @@ void smtp_get_user(char *argbuf) {
 void smtp_get_pass(char *argbuf) {
        char password[SIZ];
 
-       decode_base64(password, argbuf);
+       decode_base64(password, argbuf, SIZ);
        lprintf(9, "Trying <%s>\n", password);
        if (CtdlTryPassword(password) == pass_ok) {
                cprintf("235 Authentication successful.\r\n");
index 4b24b9e64d89cb9016d8187b7c038a6be25d965a..cf2dc329a87067c99bbf64af0a252a46be83e82c 100644 (file)
@@ -3,7 +3,7 @@
 /* Uncomment this if you want to track memory leaks.
  * This incurs some overhead, so don't use it unless you're debugging the code!
  */
-#define DEBUG_MEMORY_LEAKS
+/* #define DEBUG_MEMORY_LEAKS */
 
 /*
  * New format for a message in memory
index c37b521429dff7e8d36995f254e61b14dc7a5280..a62fe39d9f514f78bafa9f9ae00e80b686443ddb 100644 (file)
@@ -245,10 +245,11 @@ void encode_base64(char *dest, char *source)
 
 /* 
  * Convert base64-encoded to binary.  Returns the length of the decoded data.
+ * It will stop after reading 'length' bytes.
  */
-int decode_base64(char *dest, char *source)
+int decode_base64(char *dest, char *source, size_t length)
 {
-    int i;
+    int i, c;
     int dpos = 0;
     int spos = 0;
 
@@ -273,7 +274,10 @@ int decode_base64(char *dest, char *source)
        byte a[4], b[4], o[3];
 
        for (i = 0; i < 4; i++) {
-           int c = source[spos++];
+           if (spos >= length) {
+               return(dpos);
+           }
+           c = source[spos++];
 
            if (c == 0) {
                if (i > 0) {
index d2fc6cf7fd3be244cafbae96b11d00a28d4893dd..b36e175586fc4075546881af8457571db646d713 100644 (file)
@@ -5,7 +5,7 @@ void extract_token(char *dest, char *source, int parmnum, char separator);
 int extract_int (char *source, int parmnum);
 long int extract_long (char *source, long int parmnum);
 void encode_base64(char *dest, char *source);
-int decode_base64(char *dest, char *source);
+int decode_base64(char *dest, char *source, size_t length);
 void striplt(char *);
 int haschar(char *st, int ch);
 int collapsed_strcmp(char *s1, char *s2);