CtdlEncodeBase64() - only add linebreaks if told to by the caller.
authorArt Cancro <ajc@citadel.org>
Tue, 14 Jun 2022 19:06:16 +0000 (15:06 -0400)
committerArt Cancro <ajc@citadel.org>
Tue, 14 Jun 2022 19:06:16 +0000 (15:06 -0400)
Also changed this parameter to an enum.

16 files changed:
citadel/server/modules/bio/serv_bio.c
citadel/server/modules/image/serv_image.c
citadel/server/modules/imap/serv_imap.c
citadel/server/modules/migrate/serv_migrate.c
citadel/server/modules/smtp/serv_smtp.c
citadel/server/modules/upgrade/serv_upgrade.c
citadel/server/modules/wiki/serv_wiki.c
citadel/server/netconfig.c
libcitadel/lib/base64.c
libcitadel/lib/libcitadel.h
libcitadel/lib/tools.c
textclient/client_passwords.c
webcit-ng/admin_functions.c
webcit-ng/ctdlclient.c
webcit/messages.c
webcit/sieve.c

index 6ccce90c1eddb0ebbb3b46d4a568cd664be3a055..738180893febab266d4933eba7005941341924ae 100644 (file)
@@ -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;
index dfdec40fa3afcd7791ef60a8a73233d8b7568afc..ff71507a82eeffd7ec0fca2222739c2392d9b5e7 100644 (file)
@@ -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;
index d9cbab2822474620346b2c97563b4bd72108d0fe..43d1eff7ac8fa9ce23fb633f77dacf803c013ffb 100644 (file)
@@ -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';
                }
index 994d908752ed323a4b5a660669d1d59123adbf12..a45cc017da4a6c06be0e86441b1e10e7e717e807 100644 (file)
@@ -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.
index 8707406bfab899b46195560b84cd685910cde182..b7e269d86b6739749f3db79e1fa6014956d38379 100644 (file)
@@ -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';
                        }
index 722ba5752865e8f99a2947f1aa2b0ced515a9cbd..c3fc9618fff09f99a71112936889404d3c0271cd 100644 (file)
@@ -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);
index 427c8a2c45abb71fcb53ac354fd299872ebe2714..75238f8524fb97ffcc7b738a35304398cdb12e74 100644 (file)
@@ -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);
index e71d262dab45c5f89969e3f3c95bab87683da11e..0969c6769d118d1e007a4555fce4a345bf160edd 100644 (file)
@@ -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;
index 1134588b38045b17072565c15887e2e62df17752..a7be64d9a2834a146076de9dbddda2786c8deb2e 100644 (file)
@@ -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;
+                       }
                }
 
        }
index d8e70b276c9ca0d3597f6f17a94cbe605759dea2..3bde17364aa148ad8f379565f3872b4d466ea3e8 100644 (file)
@@ -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);
index 41151b2cbc03075bdc54c8c4c90847b56042e1fe..20dd1e926dc0887989a5b08c93192c7e194b6867 100644 (file)
@@ -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]='=';
index 6187a7cd3c4ddd3ef3aa5bf6ae75610d24bff1ac..040e1783f52cf92875963dffcfb4775936072238 100644 (file)
@@ -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);
index 4865b8c661d87663eeb04b0f1056b17e9435df35..1cd8b482c922f63dc0e32e7fd69b9107dcc02db9 100644 (file)
@@ -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));
 
index 24b3780501889c3e3c9cc27d1d4b263c692a6e66..15eeed19a65d45ef2d900905b3616dfb9a036939 100644 (file)
@@ -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);
        }
index ab3602f2a6c76dc55a3a373c2184d4359223a6b6..d0ad4d67c9451f927af3835e41af3ba421def87c 100644 (file)
@@ -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);
index 735cffc0c7729b59df9618a338dbb6ee73e56abf..2e3d615853b4f09c68964856ed41ba3b2c358b03 100644 (file)
@@ -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';
                        }