BASE64: we don't remove \0's anymore, we need to treat auth basic strings different.
[citadel.git] / citadel / modules / imap / serv_imap.c
index 46a6ba7d94591183cc231dfade9ee22db3183649..4b3e146cef6f40ef696311400cf4627811fe8f28 100644 (file)
@@ -293,7 +293,7 @@ void imap_add_single_msgid(long msgnum, void *userdata)
        if (Imap->num_msgs > Imap->num_alloc) {
                Imap->num_alloc += REALLOC_INCREMENT;
                Imap->msgids = realloc(Imap->msgids, (Imap->num_alloc * sizeof(long)) );
-               Imap->flags = realloc(Imap->flags, (Imap->num_alloc * sizeof(long)) );
+               Imap->flags = realloc(Imap->flags, (Imap->num_alloc * sizeof(unsigned int)) );
        }
        Imap->msgids[Imap->num_msgs - 1] = msgnum;
        Imap->flags[Imap->num_msgs - 1] = 0;
@@ -320,16 +320,17 @@ void imap_load_msgids(void)
        /* Load the message list */
        cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
        if (cdbfr != NULL) {
-               Imap->msgids = malloc(cdbfr->len);
-               memcpy(Imap->msgids, cdbfr->ptr, cdbfr->len);
+               Imap->msgids = (long*)cdbfr->ptr;
                Imap->num_msgs = cdbfr->len / sizeof(long);
                Imap->num_alloc = cdbfr->len / sizeof(long);
+               cdbfr->ptr = NULL;
+               cdbfr->len = 0;
                cdb_free(cdbfr);
        }
 
        if (Imap->num_msgs) {
-               Imap->flags = malloc(Imap->num_alloc * sizeof(long));
-               memset(Imap->flags, 0, (Imap->num_alloc * sizeof(long)) );
+               Imap->flags = malloc(Imap->num_alloc * sizeof(unsigned int));
+               memset(Imap->flags, 0, (Imap->num_alloc * sizeof(unsigned int)) );
        }
 
        imap_set_seen_flags(0);
@@ -371,14 +372,10 @@ void imap_rescan_msgids(void)
         */
        cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
        if (cdbfr != NULL) {
-               msglist = malloc(cdbfr->len + 1);
-               if (msglist == NULL) {
-                       IMAPM_syslog(LOG_CRIT, "malloc() failed");
-                       CC->kill_me = KILLME_MALLOC_FAILED;
-                       return;
-               }
-               memcpy(msglist, cdbfr->ptr, (size_t)cdbfr->len);
+               msglist = (long*)cdbfr->ptr;
+               cdbfr->ptr = NULL;
                num_msgs = cdbfr->len / sizeof(long);
+               cdbfr->len = 0;
                cdb_free(cdbfr);
        } else {
                num_msgs = 0;
@@ -417,9 +414,8 @@ void imap_rescan_msgids(void)
                                         (Imap->num_msgs - i)));
                                memmove(&Imap->flags[i],
                                        &Imap->flags[i + 1],
-                                       (sizeof(long) *
+                                       (sizeof(unsigned int) *
                                         (Imap->num_msgs - i)));
-
                                --i;
                        }
 
@@ -708,22 +704,31 @@ void imap_auth_plain(void)
 {
        citimap *Imap = IMAP;
        const char *decoded_authstring;
-       char ident[256];
-       char user[256];
-       char pass[256];
+       char ident[256] = "";
+       char user[256] = "";
+       char pass[256] = "";
        int result;
        long len;
 
        memset(pass, 0, sizeof(pass));
-       StrBufDecodeBase64(Imap->Cmd.CmdBuf);
+       len = StrBufDecodeBase64(Imap->Cmd.CmdBuf);
+
+       if (len > 0)
+       {
+               decoded_authstring = ChrPtr(Imap->Cmd.CmdBuf);
 
-       decoded_authstring = ChrPtr(Imap->Cmd.CmdBuf);
-       safestrncpy(ident, decoded_authstring, sizeof ident);
-       safestrncpy(user, &decoded_authstring[strlen(ident) + 1], sizeof user);
-       len = safestrncpy(pass, &decoded_authstring[strlen(ident) + strlen(user) + 2], sizeof pass);
-       if (len < 0)
-               len = sizeof(pass) - 1;
+               len = safestrncpy(ident, decoded_authstring, sizeof ident);
 
+               decoded_authstring += len + 1;
+
+               len = safestrncpy(user, decoded_authstring, sizeof user);
+
+               decoded_authstring += len + 1;
+
+               len = safestrncpy(pass, decoded_authstring, sizeof pass);
+               if (len < 0)
+                       len = sizeof(pass) - 1;
+       }
        Imap->authstate = imap_as_normal;
 
        if (!IsEmptyStr(ident)) {
@@ -985,6 +990,7 @@ void imap_close(int num_parms, ConstStr *Params)
  */
 void imap_namespace(int num_parms, ConstStr *Params)
 {
+       long len;
        int i;
        struct floor *fl;
        int floors = 0;
@@ -1005,8 +1011,8 @@ void imap_namespace(int num_parms, ConstStr *Params)
                if (fl->f_flags & F_INUSE) {
                        /* if (floors > 0) IAPuts(" "); samjam says this confuses javamail */
                        IAPuts("(");
-                       snprintf(Namespace, sizeof(Namespace), "%s/", fl->f_name);
-                       plain_imap_strout(Namespace);
+                       len = snprintf(Namespace, sizeof(Namespace), "%s/", fl->f_name);
+                       IPutStr(Namespace, len);
                        IAPuts(" \"/\")");
                        ++floors;
                }
@@ -1158,6 +1164,7 @@ int imap_grabroom(char *returned_roomname, const char *foldername, int zapped_ok
  */
 void imap_status(int num_parms, ConstStr *Params)
 {
+       long len;
        int ret;
        char roomname[ROOMNAMELEN];
        char imaproomname[SIZ];
@@ -1186,9 +1193,9 @@ void imap_status(int num_parms, ConstStr *Params)
         * names and simply spew all possible data items.  It's far easier to
         * code and probably saves us some processing time too.
         */
-       imap_mailboxname(imaproomname, sizeof imaproomname, &CC->room);
+       len = imap_mailboxname(imaproomname, sizeof imaproomname, &CC->room);
        IAPuts("* STATUS ");
-              plain_imap_strout(imaproomname);
+       IPutStr(imaproomname, len);
        IAPrintf(" (MESSAGES %d ", msgs);
        IAPrintf("RECENT %d ", new);    /* Initially, new==recent */
        IAPrintf("UIDNEXT %ld ", CitControl.MMhighest + 1);
@@ -1721,7 +1728,7 @@ CTDL_MODULE_INIT(imap)
                CtdlRegisterServiceHook(config.c_imaps_port,
                                        NULL, imaps_greeting, imap_command_loop, NULL, CitadelServiceIMAPS);
 #endif
-               CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);
+               CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP, PRIO_STOP + 30);
                CtdlRegisterCleanupHook(imap_cleanup);
        }