From: Art Cancro Date: Tue, 4 Dec 2001 05:16:19 +0000 (+0000) Subject: * mime_parser.c: change to memory allocation algorithm ... some badly done X-Git-Tag: v7.86~6705 X-Git-Url: https://code.citadel.org/?a=commitdiff_plain;h=a953deac53d1052bcd8657f68d73c1fc80ba1cef;p=citadel.git * mime_parser.c: change to memory allocation algorithm ... some badly done messages were crashing the server --- diff --git a/citadel/ChangeLog b/citadel/ChangeLog index 3e880e455..77c5f538d 100644 --- a/citadel/ChangeLog +++ b/citadel/ChangeLog @@ -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 Fri Jul 10 1998 Art Cancro * Initial CVS import + diff --git a/citadel/client_passwords.c b/citadel/client_passwords.c index a70bcc4fa..75076af09 100644 --- a/citadel/client_passwords.c +++ b/citadel/client_passwords.c @@ -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); diff --git a/citadel/mime_parser.c b/citadel/mime_parser.c index 1dfd751d1..0f8cc93f8 100644 --- a/citadel/mime_parser.c +++ b/citadel/mime_parser.c @@ -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), diff --git a/citadel/serv_imap.c b/citadel/serv_imap.c index b51141b39..c1489d5e0 100644 --- a/citadel/serv_imap.c +++ b/citadel/serv_imap.c @@ -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); } diff --git a/citadel/serv_smtp.c b/citadel/serv_smtp.c index 6bc34c94f..03e336f65 100644 --- a/citadel/serv_smtp.c +++ b/citadel/serv_smtp.c @@ -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"); diff --git a/citadel/server.h b/citadel/server.h index 4b24b9e64..cf2dc329a 100644 --- a/citadel/server.h +++ b/citadel/server.h @@ -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 diff --git a/citadel/tools.c b/citadel/tools.c index c37b52142..a62fe39d9 100644 --- a/citadel/tools.c +++ b/citadel/tools.c @@ -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) { diff --git a/citadel/tools.h b/citadel/tools.h index d2fc6cf7f..b36e17558 100644 --- a/citadel/tools.h +++ b/citadel/tools.h @@ -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);