From aa9b5f5703df6306f22168227edbfa6e13d9f9cf Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Tue, 14 Jun 2022 15:06:16 -0400 Subject: [PATCH] CtdlEncodeBase64() - only add linebreaks if told to by the caller. Also changed this parameter to an enum. --- citadel/server/modules/bio/serv_bio.c | 2 +- citadel/server/modules/image/serv_image.c | 6 +++--- citadel/server/modules/imap/serv_imap.c | 6 +++--- citadel/server/modules/migrate/serv_migrate.c | 2 +- citadel/server/modules/smtp/serv_smtp.c | 4 ++-- citadel/server/modules/upgrade/serv_upgrade.c | 4 ++-- citadel/server/modules/wiki/serv_wiki.c | 2 +- citadel/server/netconfig.c | 2 +- libcitadel/lib/base64.c | 6 ++++-- libcitadel/lib/libcitadel.h | 4 ++++ libcitadel/lib/tools.c | 2 +- textclient/client_passwords.c | 4 ++-- webcit-ng/admin_functions.c | 2 +- webcit-ng/ctdlclient.c | 2 +- webcit/messages.c | 2 +- webcit/sieve.c | 2 +- 16 files changed, 29 insertions(+), 23 deletions(-) diff --git a/citadel/server/modules/bio/serv_bio.c b/citadel/server/modules/bio/serv_bio.c index 6ccce90c1..738180893 100644 --- a/citadel/server/modules/bio/serv_bio.c +++ b/citadel/server/modules/bio/serv_bio.c @@ -96,7 +96,7 @@ void import_one_bio_file(char *username, long usernum, char *path) { char *encoded_data = malloc((data_length * 2) + 100); if (encoded_data) { sprintf(encoded_data, "Content-type: text/plain; charset=UTF-8\nContent-transfer-encoding: base64\n\n"); - CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1); + CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, BASE64_YES_LINEBREAKS); char userconfigroomname[ROOMNAMELEN]; struct ctdluser usbuf; diff --git a/citadel/server/modules/image/serv_image.c b/citadel/server/modules/image/serv_image.c index dfdec40fa..ff71507a8 100644 --- a/citadel/server/modules/image/serv_image.c +++ b/citadel/server/modules/image/serv_image.c @@ -81,7 +81,7 @@ void cmd_ulri(char *cmdbuf) { char *encoded_data = malloc((data_length * 2) + 100); if (encoded_data) { sprintf(encoded_data, "Content-type: %s\nContent-transfer-encoding: base64\n\n", mimetype); - CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1); + CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, BASE64_YES_LINEBREAKS); long new_msgnum = quickie_message("Citadel", NULL, NULL, SYSCONFIGROOM, encoded_data, FMT_RFC822, "Image uploaded by admin user"); if (CtdlGetRoomLock(&CC->room, CC->room.QRname) == 0) { @@ -196,7 +196,7 @@ void cmd_ului(char *cmdbuf) { char *encoded_data = malloc((data_length * 2) + 100); if (encoded_data) { sprintf(encoded_data, "Content-type: %s\nContent-transfer-encoding: base64\n\n", mimetype); - CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1); + CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, BASE64_YES_LINEBREAKS); long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, encoded_data, FMT_RFC822, "Photo uploaded by user"); if (CtdlGetUserLock(&usbuf, username) == 0) { // lock it this time @@ -237,7 +237,7 @@ void import_one_userpic_file(char *username, long usernum, char *path) { char *encoded_data = malloc((data_length * 2) + 100); if (encoded_data) { sprintf(encoded_data, "Content-type: %s\nContent-transfer-encoding: base64\n\n", GuessMimeByFilename(path, strlen(path))); - CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1); + CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, BASE64_YES_LINEBREAKS); char userconfigroomname[ROOMNAMELEN]; struct ctdluser usbuf; diff --git a/citadel/server/modules/imap/serv_imap.c b/citadel/server/modules/imap/serv_imap.c index d9cbab282..43d1eff7a 100644 --- a/citadel/server/modules/imap/serv_imap.c +++ b/citadel/server/modules/imap/serv_imap.c @@ -629,7 +629,7 @@ void imap_authenticate(int num_parms, ConstStr *Params) { } if (!strcasecmp(Params[2].Key, "LOGIN")) { - size_t len = CtdlEncodeBase64(UsrBuf, "Username:", 9, 0); + size_t len = CtdlEncodeBase64(UsrBuf, "Username:", 9, BASE64_NO_LINEBREAKS); if (UsrBuf[len - 1] == '\n') { UsrBuf[len - 1] = '\0'; } @@ -641,7 +641,7 @@ void imap_authenticate(int num_parms, ConstStr *Params) { } if (!strcasecmp(Params[2].Key, "PLAIN")) { - // size_t len = CtdlEncodeBase64(UsrBuf, "Username:", 9, 0); + // size_t len = CtdlEncodeBase64(UsrBuf, "Username:", 9, BASE64_NO_LINEBREAKS); // if (UsrBuf[len - 1] == '\n') { // UsrBuf[len - 1] = '\0'; // } @@ -722,7 +722,7 @@ void imap_auth_login_user(long state) { case imap_as_expecting_username: StrBufDecodeBase64(Imap->Cmd.CmdBuf); CtdlLoginExistingUser(ChrPtr(Imap->Cmd.CmdBuf)); - size_t len = CtdlEncodeBase64(PWBuf, "Password:", 9, 0); + size_t len = CtdlEncodeBase64(PWBuf, "Password:", 9, BASE64_NO_LINEBREAKS); if (PWBuf[len - 1] == '\n') { PWBuf[len - 1] = '\0'; } diff --git a/citadel/server/modules/migrate/serv_migrate.c b/citadel/server/modules/migrate/serv_migrate.c index 994d90875..a45cc017d 100644 --- a/citadel/server/modules/migrate/serv_migrate.c +++ b/citadel/server/modules/migrate/serv_migrate.c @@ -344,7 +344,7 @@ void migr_export_message(long msgnum) { } else { // Once we do the encoding we know the exact size - encoded_len = CtdlEncodeBase64(encoded_msg, (char *)smr.ser, smr.len, 1); + encoded_len = CtdlEncodeBase64(encoded_msg, (char *)smr.ser, smr.len, BASE64_YES_LINEBREAKS); // Careful now. If the message is gargantuan, trying to write multiple gigamegs in one // big write operation can make our transport unhappy. So we'll chunk it up 10 KB at a time. diff --git a/citadel/server/modules/smtp/serv_smtp.c b/citadel/server/modules/smtp/serv_smtp.c index 8707406bf..b7e269d86 100644 --- a/citadel/server/modules/smtp/serv_smtp.c +++ b/citadel/server/modules/smtp/serv_smtp.c @@ -312,7 +312,7 @@ void smtp_get_user(int offset) { StrBufDecodeBase64(UserName); if (CtdlLoginExistingUser(ChrPtr(UserName)) == login_ok) { - size_t len = CtdlEncodeBase64(buf, "Password:", 9, 0); + size_t len = CtdlEncodeBase64(buf, "Password:", 9, BASE64_NO_LINEBREAKS); if (buf[len - 1] == '\n') { buf[len - 1] = '\0'; @@ -426,7 +426,7 @@ void smtp_auth(void) { smtp_get_user(11); } else { - size_t len = CtdlEncodeBase64(username_prompt, "Username:", 9, 0); + size_t len = CtdlEncodeBase64(username_prompt, "Username:", 9, BASE64_NO_LINEBREAKS); if (username_prompt[len - 1] == '\n') { username_prompt[len - 1] = '\0'; } diff --git a/citadel/server/modules/upgrade/serv_upgrade.c b/citadel/server/modules/upgrade/serv_upgrade.c index 722ba5752..c3fc9618f 100644 --- a/citadel/server/modules/upgrade/serv_upgrade.c +++ b/citadel/server/modules/upgrade/serv_upgrade.c @@ -182,7 +182,7 @@ void iorarf_oneroom(char *roomname, char *infofile, char *picfile) { encoded_data = malloc((data_length * 2) + 100); if (encoded_data) { sprintf(encoded_data, "Content-type: text/plain\nContent-transfer-encoding: base64\n\n"); - CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1); + CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, BASE64_YES_LINEBREAKS); snprintf(subject, sizeof subject, "Imported room banner for %s", roomname); info_msgnum = quickie_message("Citadel", NULL, NULL, SYSCONFIGROOM, encoded_data, FMT_RFC822, subject); free(encoded_data); @@ -213,7 +213,7 @@ void iorarf_oneroom(char *roomname, char *infofile, char *picfile) { encoded_data = malloc((data_length * 2) + 100); if (encoded_data) { sprintf(encoded_data, "Content-type: image/gif\nContent-transfer-encoding: base64\n\n"); - CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1); + CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, BASE64_YES_LINEBREAKS); snprintf(subject, sizeof subject, "Imported room icon for %s", roomname); pic_msgnum = quickie_message("Citadel", NULL, NULL, SYSCONFIGROOM, encoded_data, FMT_RFC822, subject); free(encoded_data); diff --git a/citadel/server/modules/wiki/serv_wiki.c b/citadel/server/modules/wiki/serv_wiki.c index 427c8a2c4..75238f852 100644 --- a/citadel/server/modules/wiki/serv_wiki.c +++ b/citadel/server/modules/wiki/serv_wiki.c @@ -307,7 +307,7 @@ int wiki_upload_beforesave(struct CtdlMessage *msg, struct recptypes *recp) { CC->user.fullname, CtdlGetConfigStr("c_nodename")); - memolen = CtdlEncodeBase64(encoded_memo, memo, memolen, 0); + memolen = CtdlEncodeBase64(encoded_memo, memo, memolen, BASE64_YES_LINEBREAKS); StrBufAppendBufPlain(NewMsgText, HKEY("--"), 0); StrBufAppendBufPlain(NewMsgText, boundary, -1, 0); diff --git a/citadel/server/netconfig.c b/citadel/server/netconfig.c index e71d262da..0969c6769 100644 --- a/citadel/server/netconfig.c +++ b/citadel/server/netconfig.c @@ -49,7 +49,7 @@ void SaveRoomNetConfigFile(long roomnum, const char *raw_netconfig) { enc = malloc(len * 2); if (enc) { - enc_len = CtdlEncodeBase64(enc, raw_netconfig, len, 0); + enc_len = CtdlEncodeBase64(enc, raw_netconfig, len, BASE64_NO_LINEBREAKS); if ((enc_len > 1) && (enc[enc_len-2] == 13)) enc[enc_len-2] = 0; if ((enc_len > 0) && (enc[enc_len-1] == 10)) enc[enc_len-1] = 0; enc[enc_len] = 0; diff --git a/libcitadel/lib/base64.c b/libcitadel/lib/base64.c index 1134588b3..a7be64d9a 100644 --- a/libcitadel/lib/base64.c +++ b/libcitadel/lib/base64.c @@ -88,8 +88,10 @@ size_t CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen, int li bytes_output += 4; } if ( ((bytes_processed % 57) == 0) || (bytes_processed >= sourcelen) ) { - sprintf(&dest[bytes_output], "\r\n"); - bytes_output += 2; + if (linebreaks) { + sprintf(&dest[bytes_output], "\r\n"); + bytes_output += 2; + } } } diff --git a/libcitadel/lib/libcitadel.h b/libcitadel/lib/libcitadel.h index d8e70b276..3bde17364 100644 --- a/libcitadel/lib/libcitadel.h +++ b/libcitadel/lib/libcitadel.h @@ -405,6 +405,10 @@ long extract_long (const char *source, int parmnum); unsigned long extract_unsigned_long(const char *source, int parmnum); void CtdlInitBase64Table(void); size_t CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen, int linebreaks); +enum { + BASE64_NO_LINEBREAKS = 0, + BASE64_YES_LINEBREAKS = 1 +}; size_t CtdlDecodeBase64(char *dest, const char *source, size_t length); unsigned int decode_hex(char *Source); int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen); diff --git a/libcitadel/lib/tools.c b/libcitadel/lib/tools.c index 41151b2cb..20dd1e926 100644 --- a/libcitadel/lib/tools.c +++ b/libcitadel/lib/tools.c @@ -220,7 +220,7 @@ char *rfc2047encode(const char *line, long length) { result = (char*) malloc(sizeof(UTF8_HEADER) + 4 + length * 2); strncpy (result, UTF8_HEADER, strlen (UTF8_HEADER)); - CtdlEncodeBase64(result + strlen(UTF8_HEADER), line, length, 0); + CtdlEncodeBase64(result + strlen(UTF8_HEADER), line, length, BASE64_NO_LINEBREAKS); end = strlen (result); result[end]='?'; result[end+1]='='; diff --git a/textclient/client_passwords.c b/textclient/client_passwords.c index 6187a7cd3..040e1783f 100644 --- a/textclient/client_passwords.c +++ b/textclient/client_passwords.c @@ -100,13 +100,13 @@ void set_stored_password(char *host, char *port, char *username, char *password) if ((strcasecmp(hostbuf, host)) || (strcasecmp(portbuf, port))) { snprintf(buf, sizeof buf, "%s|%s|%s|%s|", hostbuf, portbuf, ubuf, pbuf); - CtdlEncodeBase64(buf64, buf, strlen(buf), 0); + CtdlEncodeBase64(buf64, buf, strlen(buf), BASE64_NO_LINEBREAKS); fprintf(fp, "%s\n", buf64); } } if (!IsEmptyStr(username)) { snprintf(buf, sizeof buf, "%s|%s|%s|%s|", host, port, username, password); - CtdlEncodeBase64(buf64, buf, strlen(buf), 0); + CtdlEncodeBase64(buf64, buf, strlen(buf), BASE64_NO_LINEBREAKS); fprintf(fp, "%s\n", buf64); } fclose(oldfp); diff --git a/webcit-ng/admin_functions.c b/webcit-ng/admin_functions.c index 4865b8c66..1cd8b482c 100644 --- a/webcit-ng/admin_functions.c +++ b/webcit-ng/admin_functions.c @@ -27,7 +27,7 @@ void try_login(struct http_transaction *h, struct ctdlsession *c) { extract_token(password, h->request_body, 1, '|', sizeof password); snprintf(buf, sizeof buf, "%s:%s", username, password); - CtdlEncodeBase64(auth, buf, strlen(buf), 0); + CtdlEncodeBase64(auth, buf, strlen(buf), BASE64_NO_LINEBREAKS); syslog(LOG_DEBUG, "try_login(username='%s',password=(%d bytes))", username, (int) strlen(password)); diff --git a/webcit-ng/ctdlclient.c b/webcit-ng/ctdlclient.c index 24b378050..15eeed19a 100644 --- a/webcit-ng/ctdlclient.c +++ b/webcit-ng/ctdlclient.c @@ -210,7 +210,7 @@ int login_to_citadel(struct ctdlsession *c, char *auth, char *resultbuf) { // Re-encode the auth string so it contains the properly formatted username char new_auth_string[1024]; snprintf(new_auth_string, sizeof(new_auth_string), "%s:%s", c->whoami, supplied_password); - CtdlEncodeBase64(c->auth, new_auth_string, strlen(new_auth_string), 0); + CtdlEncodeBase64(c->auth, new_auth_string, strlen(new_auth_string), BASE64_NO_LINEBREAKS); return(0); } diff --git a/webcit/messages.c b/webcit/messages.c index ab3602f2a..d0ad4d67c 100644 --- a/webcit/messages.c +++ b/webcit/messages.c @@ -945,7 +945,7 @@ void post_mime_to_server(void) { break; } syslog(LOG_DEBUG, "Attachment: raw len %d", StrLength(att->Data)); - encoded_strlen = CtdlEncodeBase64(encoded, ChrPtr(att->Data), StrLength(att->Data), 1); + encoded_strlen = CtdlEncodeBase64(encoded, ChrPtr(att->Data), StrLength(att->Data), BASE64_YES_LINEBREAKS); syslog(LOG_DEBUG, "Attachment: encoded len %d", encoded_strlen); serv_printf("--%s", top_boundary); diff --git a/webcit/sieve.c b/webcit/sieve.c index 735cffc0c..2e3d61585 100644 --- a/webcit/sieve.c +++ b/webcit/sieve.c @@ -113,7 +113,7 @@ void parse_fields_from_rule_editor(void) { redirect, automsg, final ); - size_t len = CtdlEncodeBase64(encoded_rule, rule, strlen(rule)+1, 0); + size_t len = CtdlEncodeBase64(encoded_rule, rule, strlen(rule)+1, BASE64_NO_LINEBREAKS); if (encoded_rule[len - 1] == '\n') { encoded_rule[len - 1] = '\0'; } -- 2.30.2