Skip to content

Commit

Permalink
fix more clientsession leakage and some regressions in the imap code …
Browse files Browse the repository at this point in the history
…(fetch and create)
  • Loading branch information
pjstevns committed Dec 11, 2008
1 parent 4ac17d2 commit 232e772
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 47 deletions.
16 changes: 10 additions & 6 deletions src/clientsession.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,20 @@ void client_session_reset_parser(ClientSession_t *session)
}
}

void client_session_bailout(ClientSession_t *session)
void client_session_bailout(ClientSession_t **session)
{
if (! session) return;
TRACE(TRACE_DEBUG,"[%p]", session);
ClientSession_t *c = *session;

if (! c) return;
TRACE(TRACE_DEBUG,"[%p]", c);

// brute force:
if (server_conf->no_daemonize == 1) _exit(0);

client_session_reset(session);
ci_close(session->ci);
client_session_reset(c);
ci_close(c->ci);
g_free(c);
c = NULL;
}

void client_session_set_timeout(ClientSession_t *session, int timeout)
Expand Down Expand Up @@ -140,7 +144,7 @@ void socket_write_cb(int fd UNUSED, short what UNUSED, void *arg)

case IMAPCS_LOGOUT:
case IMAPCS_ERROR:
client_session_bailout(session);
client_session_bailout(&session);
break;

case IMAPCS_INITIAL_CONNECT:
Expand Down
2 changes: 1 addition & 1 deletion src/clientsession.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
ClientSession_t * client_session_new(client_sock *c);
int client_session_reset(ClientSession_t * session);
void client_session_reset_parser(ClientSession_t *session);
void client_session_bailout(ClientSession_t *session);
void client_session_bailout(ClientSession_t **session);
void client_session_set_timeout(ClientSession_t *session, int timeout);

void socket_read_cb(int fd, short what, void *arg);
Expand Down
23 changes: 16 additions & 7 deletions src/dbmail-imapsession.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ static u64_t dbmail_imap_session_message_load(ImapSession *self, int filter)
}
self->message = db_init_fetch(self->msg_idnr, DBMAIL_MESSAGE_FILTER_FULL);
}

if (! self->message) {
TRACE(TRACE_ERR,"message retrieval failed");
return 0;
}

return Cache_update(self->cache, self->message, filter);
}

Expand Down Expand Up @@ -690,7 +696,9 @@ static int _imap_show_body_section(body_fetch_t *bodyfetch, gpointer data)

if (self->fi->msgparse_needed) {

dbmail_imap_session_message_load(self, DBMAIL_MESSAGE_FILTER_FULL);
if (! dbmail_imap_session_message_load(self, DBMAIL_MESSAGE_FILTER_FULL))
return 0;

if (bodyfetch->partspec[0]) {
if (bodyfetch->partspec[0] == '0') {
dbmail_imap_session_buff_printf(self, "\r\n%s BAD protocol error\r\n", self->tag);
Expand Down Expand Up @@ -1022,7 +1030,7 @@ int dbmail_imap_session_fetch_get_items(ImapSession *self)
if (! self->ids)
TRACE(TRACE_INFO, "[%p] self->ids is NULL", self);
else {
dbmail_imap_session_buff_clear(self);
dbmail_imap_session_buff_flush(self);
self->error = FALSE;
g_tree_foreach(self->ids, (GTraverseFunc) _do_fetch, self);
if (self->error) return -1;
Expand Down Expand Up @@ -1206,8 +1214,8 @@ int dbmail_imap_session_mailbox_get_selectable(ImapSession * self, u64_t idnr)
static void notify_fetch(ImapSession *self, DbmailMailbox *newbox, u64_t *uid)
{
u64_t *msn;
char *oldflags, *newflags;
MessageInfo *old, *new;
char *oldflags = NULL, *newflags = NULL;
MessageInfo *old = NULL, *new = NULL;

assert(uid);

Expand All @@ -1222,10 +1230,11 @@ static void notify_fetch(ImapSession *self, DbmailMailbox *newbox, u64_t *uid)
old = g_tree_lookup(self->mailbox->msginfo, uid);

// FETCH
oldflags = imap_flags_as_string(self->mailbox->state, old);
if (old)
oldflags = imap_flags_as_string(self->mailbox->state, old);
newflags = imap_flags_as_string(self->mailbox->state, new);

if (! MATCH(oldflags,newflags)) {
if ((! oldflags) || (! MATCH(oldflags,newflags))) {
char *t = NULL;
if (self->use_uid) t = g_strdup_printf(" UID %llu", *uid);
dbmail_imap_session_buff_printf(self,"* %llu FETCH (FLAGS %s%s)\r\n", *msn, newflags, t?t:"");
Expand All @@ -1235,7 +1244,7 @@ static void notify_fetch(ImapSession *self, DbmailMailbox *newbox, u64_t *uid)
if (t) g_free(t);
}

g_free(oldflags);
if (oldflags) g_free(oldflags);
g_free(newflags);
}

Expand Down
2 changes: 2 additions & 0 deletions src/dbmail-message.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,14 @@ static DbmailMessage * _mime_retrieve(DbmailMessage *self)
if (is_header && ((boundary = find_boundary(str)) != NULL)) {
got_boundary = TRUE;
dprint("<boundary depth=\"%d\">%s</boundary>\n", depth, boundary);
if (blist[depth]) g_free(blist[depth]);
blist[depth] = boundary;
}

if (prevdepth > depth && blist[depth]) {
dprint("\n--%s at %d--\n", blist[depth], depth);
g_string_append_printf(m, "\n--%s--\n", blist[depth]);
g_free(blist[depth]);
blist[depth] = NULL;
finalized=TRUE;
}
Expand Down
4 changes: 2 additions & 2 deletions src/dbmailtypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -535,8 +535,8 @@ typedef struct {
/*
* A struct to hold info about a Sieve script
*/
typedef struct ssinfo {
char *name;
typedef struct {
char name[512];
int active;
} sievescript_info_t;
/*
Expand Down
4 changes: 2 additions & 2 deletions src/dm_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ u64_t Cache_update(T C, DbmailMessage *message, int filter)
g_free(buf);
break;
case DBMAIL_MESSAGE_FILTER_FULL:
outcnt = C->size;
Mem_rewind(C->memdump);
Mem_rewind(C->tmpdump);
/* done */
break;

}


TRACE(TRACE_DEBUG,"C->size[%llu]", C->size);
TRACE(TRACE_DEBUG,"C->size[%llu], outcnt[%llu]", C->size, outcnt);

return outcnt;
}
Expand Down
1 change: 1 addition & 0 deletions src/dm_db.c
Original file line number Diff line number Diff line change
Expand Up @@ -2448,6 +2448,7 @@ int db_mailbox_create_with_parents(const char * mailbox, mailbox_source_t source
*message = "General error while subscribing";
skip_and_free = DM_EGENERAL;
}
MailboxState_setPermission(M, IMAPPERM_READWRITE);
}

/* If the PUBLIC user owns it, then the current user needs ACLs. */
Expand Down
6 changes: 4 additions & 2 deletions src/dm_iconv.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,18 @@ void dbmail_iconv_init(void)
/* convert not encoded field to utf8 */
char * dbmail_iconv_str_to_utf8(const char* str_in, const char *charset)
{
char * subj=NULL;
char * subj=NULL, *t;
iconv_t conv_iconv = (iconv_t)-1;

dbmail_iconv_init();

if (str_in==NULL)
return NULL;

t = str_in;

if (g_utf8_validate((const gchar *)str_in, -1, NULL) || !g_mime_utils_text_is_8bit((unsigned char *)str_in, strlen(str_in)))
return g_strdup(str_in);
return g_strdup(t);

if (charset) {
if ((conv_iconv=g_mime_iconv_open("UTF-8",charset)) != (iconv_t)-1) {
Expand Down
4 changes: 2 additions & 2 deletions src/dm_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ typedef struct {
int drop_privileges(char *newuser, char *newgroup)
{
/* will drop running program's priviledges to newuser and newgroup */
struct passwd *pwd;
struct group *grp;
struct passwd *pwd = NULL;
struct group *grp = NULL;

grp = getgrnam(newgroup);

Expand Down
4 changes: 2 additions & 2 deletions src/dm_sievescript.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ int dm_sievescript_list(u64_t user_idnr, GList **scriptlist)
TRY
r = db_query(c,"SELECT name,active FROM %ssievescripts WHERE owner_idnr = %llu", DBPFX,user_idnr);
while (db_result_next(r)) {
struct ssinfo *info = g_new0(struct ssinfo,1);
info->name = g_strdup(db_result_get(r,0));
sievescript_info_t *info = g_new0(sievescript_info_t,1);
strncpy(info->name, db_result_get(r,0), sizeof(info->name));
info->active = db_result_get_int(r,1);
*(GList **)scriptlist = g_list_prepend(*(GList **)scriptlist, info);
}
Expand Down
11 changes: 4 additions & 7 deletions src/lmtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,19 @@ static void lmtp_cb_read(void *arg)
if (r == -1) return;

if (r == 0) {
client_session_bailout(session);
g_free(session);
client_session_bailout(&session);
return;
}

if ((r = lmtp_tokenizer(session, buffer))) {
if (r == -3) {
client_session_bailout(session);
g_free(session);
client_session_bailout(&session);
return;
}

if (r > 0) {
if (lmtp(session) == -3) {
client_session_bailout(session);
g_free(session);
client_session_bailout(&session);
return;
}
client_session_reset_parser(session);
Expand All @@ -120,7 +117,7 @@ void lmtp_cb_write(void *arg)

switch (session->state) {
case QUIT:
client_session_bailout(session);
client_session_bailout(&session);
break;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/pop3.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ void pop3_cb_read(void *arg)
l = ci_readln(session->ci, buffer);

if (l==0) { // error
client_session_bailout(session);
client_session_bailout(&session);
return;
}

Expand All @@ -265,7 +265,7 @@ void pop3_cb_write(void *arg)
switch (session->state) {
case POP3_QUIT_STATE:
db_session_cleanup(session);
client_session_bailout(session);
client_session_bailout(&session);
break;
}
}
Expand Down Expand Up @@ -310,7 +310,7 @@ int pop3_error(ClientSession_t * session, const char *formatstring, ...)

if (session->error_count >= MAX_ERRORS) {
ci_write(ci, "-ERR too many errors\r\n");
client_session_bailout(session);
client_session_bailout(&session);
return -3;
} else {
va_start(ap, formatstring);
Expand Down
5 changes: 1 addition & 4 deletions src/sievecmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -640,10 +640,7 @@ int do_list(u64_t user_idnr)
printf(" - ");
printf("%s\n", info->name);

g_free(info->name);

if (! g_list_next(scriptlist))
break;
if (! g_list_next(scriptlist)) break;
scriptlist = g_list_next(scriptlist);
}

Expand Down
17 changes: 9 additions & 8 deletions src/timsieve.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,17 @@ void tims_cb_read(void *arg)

l = ci_readln(session->ci, buffer);
if (l == 0) {
client_session_bailout(session);
client_session_bailout(&session);
return;
}

if ((l = tims_tokenizer(session, buffer))) {
if (l == -3) {
client_session_bailout(session);
client_session_bailout(&session);
return;
}
if (tims(session) == -3) {
client_session_bailout(session);
client_session_bailout(&session);
return;
}
client_session_reset_parser(session);
Expand All @@ -119,7 +119,7 @@ void tims_cb_write(void *arg)
ci_write(session->ci, NULL); //flush buffered data
switch(session->state) {
case QUIT:
client_session_bailout(session);
client_session_bailout(&session);
break;
}
}
Expand Down Expand Up @@ -445,14 +445,16 @@ int tims(ClientSession_t *session)
return tims_error(session, "NO \"Script name required.\"\r\n");

ret = dm_sievescript_getbyname(session->useridnr, scriptname, &script);
if (ret == -3) {
if (script == NULL) {
return tims_error(session, "NO \"Script not found.\"\r\n");
} else if (ret != 0 || script == NULL) {
} else if (ret < 0) {
g_free(script);
return tims_error(session, "NO \"Internal error.\"\r\n");
} else {
ci_write(ci, "{%u+}\r\n", (unsigned int)strlen(script));
ci_write(ci, "%s\r\n", script);
ci_write(ci, "OK\r\n");
g_free(script);
}
return 1;

Expand Down Expand Up @@ -511,8 +513,7 @@ int tims(ClientSession_t *session)
while (scriptlist) {
sievescript_info_t *info = (sievescript_info_t *) scriptlist->data;
ci_write(ci, "\"%s\"%s\r\n", info->name, (info-> active == 1 ? " ACTIVE" : ""));
if (! g_list_next(scriptlist))
break;
if (! g_list_next(scriptlist)) break;
scriptlist = g_list_next(scriptlist);
}
ci_write(ci, "OK\r\n");
Expand Down
2 changes: 1 addition & 1 deletion test-scripts/testimap.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def testAppend(self):
self.assertEquals(strip_crlf(result[1][0][1]),TESTMSG['strict822'].get_payload())

result = self.o.fetch(id,"(FLAGS)")
expect = '1 (FLAGS (\\Flagged Userflag))'
expect = '1 (FLAGS (\\Seen \\Flagged Userflag))'
self.assertEquals(result[1][0],expect)

def testCheck(self):
Expand Down

0 comments on commit 232e772

Please sign in to comment.