From 3061bf4d898bdfb0e0dfe294676b2ac6274d0cb4 Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Sun, 13 Mar 2011 13:32:47 -0400 Subject: [PATCH] CC->kill_me now contains an enum indicating the REASON session was killed --- citadel/citserver.c | 10 +++---- citadel/context.c | 27 ++++--------------- citadel/modules/imap/serv_imap.c | 8 +++--- .../modules/managesieve/serv_managesieve.c | 24 ++++++++--------- citadel/modules/migrate/serv_migrate.c | 16 +++++------ citadel/modules/pop3/serv_pop3.c | 10 +++---- citadel/modules/smtp/serv_smtp.c | 10 +++---- citadel/modules/vcard/serv_vcard.c | 2 +- citadel/modules/xmpp/serv_xmpp.c | 10 +++---- citadel/modules/xmpp/xmpp_presence.c | 2 +- citadel/server.h | 27 +++++++++++++++++++ citadel/sysdep.c | 14 +++++----- citadel/threads.c | 2 +- 13 files changed, 86 insertions(+), 76 deletions(-) diff --git a/citadel/citserver.c b/citadel/citserver.c index 86bd0687c..42fb045a4 100644 --- a/citadel/citserver.c +++ b/citadel/citserver.c @@ -791,7 +791,7 @@ void cmd_ipgm(char *argbuf) sleep(5); cprintf("%d Authentication failed.\n", ERROR + PASSWORD_REQUIRED); syslog(LOG_ERR, "Warning: ipgm authentication failed.\n"); - CC->kill_me = 1; + CC->kill_me = KILLME_AUTHFAILED; } } @@ -825,7 +825,7 @@ void cmd_down(char *argbuf) { { cprintf(Reply, CIT_OK + SERVER_SHUTTING_DOWN); } - CC->kill_me = 1; /* Even the DOWN command has to follow correct proceedure when disconecting */ + CC->kill_me = KILLME_SERVER_SHUTTING_DOWN; CtdlThreadStopAll(); } @@ -994,7 +994,7 @@ void citproto_begin_session() { ERROR + MAX_SESSIONS_EXCEEDED, config.c_nodename, config.c_maxsessions ); - CC->kill_me = 1; + CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED; } else { cprintf("%d %s Citadel server ready.\n", CIT_OK, config.c_nodename); @@ -1018,7 +1018,7 @@ void cmd_qnop(char *argbuf) void cmd_quit(char *argbuf) { cprintf("%d Goodbye.\n", CIT_OK); - CC->kill_me = 1; + CC->kill_me = KILLME_CLIENT_LOGGED_OUT; } @@ -1043,7 +1043,7 @@ void do_command_loop(void) { memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */ if (client_getln(cmdbuf, sizeof cmdbuf) < 1) { syslog(LOG_ERR, "Client disconnected: ending session.\n"); - CC->kill_me = 1; + CC->kill_me = KILLME_CLIENT_DISCONNECTED; CtdlThreadName(old_name); return; } diff --git a/citadel/context.c b/citadel/context.c index 9801c2362..cfe30334e 100644 --- a/citadel/context.c +++ b/citadel/context.c @@ -4,7 +4,7 @@ * * Copyright (c) 1987-2010 by the citadel.org team * - * This program is free software; you can redistribute it and/or modify + * This program is open source software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. @@ -16,7 +16,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "sysdep.h" @@ -167,7 +167,7 @@ int CtdlTerminateOtherSession (int session_num) if ((ccptr->user.usernum == CC->user.usernum) || (CC->user.axlevel >= AxAideU)) { ret |= TERM_ALLOWED; - ccptr->kill_me = 1; + ccptr->kill_me = KILLME_ADMIN_TERMINATE; } } } @@ -292,7 +292,7 @@ void terminate_idle_sessions(void) && (config.c_sleeping > 0) && (now - (ccptr->lastcmd) > config.c_sleeping) ) { if (!ccptr->dont_term) { - ccptr->kill_me = 1; + ccptr->kill_me = KILLME_IDLE; ++killed; } else @@ -592,23 +592,6 @@ void context_cleanup(void) -/* - * Terminate another session. - * (This could justifiably be moved out of sysdep.c because it - * no longer does anything that is system-dependent.) - */ -void kill_session(int session_to_kill) { - CitContext *ptr; - - begin_critical_section(S_SESSION_TABLE); - for (ptr = ContextList; ptr != NULL; ptr = ptr->next) { - if (ptr->cs_pid == session_to_kill) { - ptr->kill_me = 1; - } - } - end_critical_section(S_SESSION_TABLE); -} - /* * Purge all sessions which have the 'kill_me' flag set. * This function has code to prevent it from running more than once every @@ -660,7 +643,7 @@ void dead_session_purge(int force) { * is allocated privately on this thread's stack. */ while (rem != NULL) { - syslog(LOG_DEBUG, "dead_session_purge(): purging session %d\n", rem->cs_pid); + syslog(LOG_DEBUG, "dead_session_purge(): purging session %d, reason=%d\n", rem->cs_pid, rem->kill_me); RemoveContext(rem); ptr = rem; rem = rem->next; diff --git a/citadel/modules/imap/serv_imap.c b/citadel/modules/imap/serv_imap.c index 21f71a2b0..29ab6536b 100644 --- a/citadel/modules/imap/serv_imap.c +++ b/citadel/modules/imap/serv_imap.c @@ -589,7 +589,7 @@ void imap_greeting(void) if (CCC->nologin) { IAPuts("* BYE; Server busy, try later\r\n"); - CCC->kill_me = 1; + CCC->kill_me = KILLME_NOLOGIN; IUnbuffer(); return; } @@ -607,7 +607,7 @@ void imap_greeting(void) void imaps_greeting(void) { CtdlModuleStartCryptoMsgs(NULL, NULL, NULL); #ifdef HAVE_OPENSSL - if (!CC->redirect_ssl) CC->kill_me = 1; /* kill session if no crypto */ + if (!CC->redirect_ssl) CC->kill_me = KILLME_NO_CRYPTO; /* kill session if no crypto */ #endif imap_greeting(); } @@ -1493,7 +1493,7 @@ void imap_command_loop(void) if (CtdlClientGetLine(Imap->Cmd.CmdBuf) < 1) { syslog(LOG_ERR, "Client disconnected: ending session.\r\n"); - CC->kill_me = 1; + CC->kill_me = KILLME_CLIENT_DISCONNECTED; return; } @@ -1645,7 +1645,7 @@ void imap_logout(int num_parms, ConstStr *Params) } IAPrintf("* BYE %s logging out\r\n", config.c_fqdn); IReply("OK Citadel IMAP session ended."); - CC->kill_me = 1; + CC->kill_me = KILLME_CLIENT_LOGGED_OUT; return; } diff --git a/citadel/modules/managesieve/serv_managesieve.c b/citadel/modules/managesieve/serv_managesieve.c index 0bdc0f100..db1d92ce5 100644 --- a/citadel/modules/managesieve/serv_managesieve.c +++ b/citadel/modules/managesieve/serv_managesieve.c @@ -251,13 +251,13 @@ char *ReadString(long size, char *command) if (size < 1) { cprintf("NO %s: %ld BAD Message length must be at least 1.\r\n", command, size); - CC->kill_me = 1; + CC->kill_me = KILLME_READSTRING_FAILED; return NULL; } MGSVE->transmitted_message = malloc(size + 2); if (MGSVE->transmitted_message == NULL) { cprintf("NO %s Cannot allocate memory.\r\n", command); - CC->kill_me = 1; + CC->kill_me = KILLME_MALLOC_FAILED; return NULL; } MGSVE->transmitted_length = size; @@ -314,7 +314,7 @@ void cmd_mgsve_auth(int num_parms, char **parms, struct sdm_userdata *u) } } cprintf("NO \"Authentication Failure.\"\r\n");/* we just support auth plain. */ - CC->kill_me = 1; + CC->kill_me = KILLME_AUTHFAILED; } @@ -330,18 +330,18 @@ void cmd_mgsve_starttls(void) -/** - *LOGOUT command, see chapter 2.3 +/* + * LOGOUT command, see chapter 2.3 */ void cmd_mgsve_logout(struct sdm_userdata *u) { cprintf("OK\r\n"); syslog(LOG_NOTICE, "MgSve bye."); - CC->kill_me = 1; + CC->kill_me = KILLME_CLIENT_LOGGED_OUT; } -/** +/* * HAVESPACE command. see chapter 2.5 */ void cmd_mgsve_havespace(void) @@ -352,7 +352,7 @@ void cmd_mgsve_havespace(void) if (MGSVE->command_state != mgsve_password) { cprintf("NO\r\n"); - CC->kill_me = 1; + CC->kill_me = KILLME_QUOTA; } else { @@ -362,7 +362,7 @@ void cmd_mgsve_havespace(void) } } -/** +/* * PUTSCRIPT command, see chapter 2.6 */ void cmd_mgsve_putscript(int num_parms, char **parms, struct sdm_userdata *u) @@ -396,7 +396,7 @@ void cmd_mgsve_putscript(int num_parms, char **parms, struct sdm_userdata *u) } else { cprintf("%s NO Read failed.\r\n", parms[0]); - CC->kill_me = 1; + CC->kill_me = KILLME_READ_FAILED; return; } @@ -586,7 +586,7 @@ void managesieve_command_loop(void) { } if (length < 1) { syslog(LOG_CRIT, "Client disconnected: ending session.\n"); - CC->kill_me = 1; + CC->kill_me = KILLME_CLIENT_DISCONNECTED; return; } syslog(LOG_INFO, "MANAGESIEVE: %s\n", cmdbuf); @@ -635,7 +635,7 @@ void managesieve_command_loop(void) { else { cprintf("No Invalid access or command.\r\n"); syslog(LOG_INFO, "illegal Managesieve command: %s", parms[0]); - CC->kill_me = 1; + CC->kill_me = KILLME_ILLEGAL_MANAGESIEVE_COMMAND; } diff --git a/citadel/modules/migrate/serv_migrate.c b/citadel/modules/migrate/serv_migrate.c index 0a69a1a82..10404772b 100644 --- a/citadel/modules/migrate/serv_migrate.c +++ b/citadel/modules/migrate/serv_migrate.c @@ -373,7 +373,7 @@ void migr_export_messages(void) { migr_global_message_list = fopen(migr_tempfilename1, "r"); if (migr_global_message_list != NULL) { syslog(LOG_INFO, "Opened %s\n", migr_tempfilename1); - while ((Ctx->kill_me != 1) && + while ((Ctx->kill_me == 0) && (fgets(buf, sizeof(buf), migr_global_message_list) != NULL)) { msgnum = atol(buf); if (msgnum > 0L) { @@ -383,7 +383,7 @@ void migr_export_messages(void) { } fclose(migr_global_message_list); } - if (Ctx->kill_me != 1) + if (Ctx->kill_me == 0) syslog(LOG_INFO, "Exported %d messages.\n", count); else syslog(LOG_ERR, "Export aborted due to client disconnect! \n"); @@ -494,12 +494,12 @@ void migr_do_export(void) { cprintf("%d\n", CitControl.version); client_write("\n", 11); - if (Ctx->kill_me != 1) migr_export_users(); - if (Ctx->kill_me != 1) migr_export_openids(); - if (Ctx->kill_me != 1) migr_export_rooms(); - if (Ctx->kill_me != 1) migr_export_floors(); - if (Ctx->kill_me != 1) migr_export_visits(); - if (Ctx->kill_me != 1) migr_export_messages(); + if (Ctx->kill_me == 0) migr_export_users(); + if (Ctx->kill_me == 0) migr_export_openids(); + if (Ctx->kill_me == 0) migr_export_rooms(); + if (Ctx->kill_me == 0) migr_export_floors(); + if (Ctx->kill_me == 0) migr_export_visits(); + if (Ctx->kill_me == 0) migr_export_messages(); client_write("\n", 24); client_write("000\n", 4); Ctx->dont_term = 0; diff --git a/citadel/modules/pop3/serv_pop3.c b/citadel/modules/pop3/serv_pop3.c index ec98569c2..b8f3cb073 100644 --- a/citadel/modules/pop3/serv_pop3.c +++ b/citadel/modules/pop3/serv_pop3.c @@ -110,9 +110,9 @@ void pop3s_greeting(void) { /* kill session if no crypto */ #ifdef HAVE_OPENSSL - if (!CC->redirect_ssl) CC->kill_me = 1; + if (!CC->redirect_ssl) CC->kill_me = KILLME_NO_CRYPTO; #else - CC->kill_me = 1; + CC->kill_me = KILLME_NO_CRYPTO; #endif pop3_greeting(); @@ -570,7 +570,7 @@ void pop3_command_loop(void) { memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */ if (client_getln(cmdbuf, sizeof cmdbuf) < 1) { syslog(LOG_ERR, "Client disconnected: ending session."); - CC->kill_me = 1; + CC->kill_me = KILLME_CLIENT_DISCONNECTED; return; } if (!strncasecmp(cmdbuf, "PASS", 4)) { @@ -592,7 +592,7 @@ void pop3_command_loop(void) { else if (!strncasecmp(cmdbuf, "QUIT", 4)) { cprintf("+OK Goodbye...\r\n"); pop3_update(); - CC->kill_me = 1; + CC->kill_me = KILLME_CLIENT_LOGGED_OUT; return; } @@ -616,7 +616,7 @@ void pop3_command_loop(void) { else if (CC->nologin) { cprintf("-ERR System busy, try later.\r\n"); - CC->kill_me = 1; + CC->kill_me = KILLME_NOLOGIN; } else if (!strncasecmp(cmdbuf, "LIST", 4)) { diff --git a/citadel/modules/smtp/serv_smtp.c b/citadel/modules/smtp/serv_smtp.c index d2fbe6384..6c7480adf 100644 --- a/citadel/modules/smtp/serv_smtp.c +++ b/citadel/modules/smtp/serv_smtp.c @@ -135,7 +135,7 @@ void smtp_greeting(int is_msa) cprintf("421 %s\r\n", message_to_spammer); else cprintf("550 %s\r\n", message_to_spammer); - CC->kill_me = 1; + CC->kill_me = KILLME_SPAMMER; /* no need to free_recipients(valid), it's not allocated yet */ return; } @@ -147,7 +147,7 @@ void smtp_greeting(int is_msa) cprintf("500 Too many users are already online (maximum is %d)\r\n", config.c_maxsessions ); - CC->kill_me = 1; + CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED; /* no need to free_recipients(valid), it's not allocated yet */ return; } @@ -165,7 +165,7 @@ void smtp_greeting(int is_msa) void smtps_greeting(void) { CtdlModuleStartCryptoMsgs(NULL, NULL, NULL); #ifdef HAVE_OPENSSL - if (!CC->redirect_ssl) CC->kill_me = 1; /* kill session if no crypto */ + if (!CC->redirect_ssl) CC->kill_me = KILLME_NO_CRYPTO; /* kill session if no crypto */ #endif smtp_greeting(0); } @@ -841,7 +841,7 @@ void smtp_command_loop(void) { memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */ if (client_getln(cmdbuf, sizeof cmdbuf) < 1) { syslog(LOG_CRIT, "Client disconnected: ending session.\n"); - CC->kill_me = 1; + CC->kill_me = KILLME_CLIENT_DISCONNECTED; return; } syslog(LOG_INFO, "SMTP server: %s\n", cmdbuf); @@ -893,7 +893,7 @@ void smtp_command_loop(void) { else if (!strncasecmp(cmdbuf, "QUIT", 4)) { cprintf("221 Goodbye...\r\n"); - CC->kill_me = 1; + CC->kill_me = KILLME_CLIENT_LOGGED_OUT; return; } diff --git a/citadel/modules/vcard/serv_vcard.c b/citadel/modules/vcard/serv_vcard.c index 0aba02889..39c401786 100644 --- a/citadel/modules/vcard/serv_vcard.c +++ b/citadel/modules/vcard/serv_vcard.c @@ -1117,7 +1117,7 @@ void check_get(void) { memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */ if (client_getln(cmdbuf, sizeof cmdbuf) < 1) { syslog(LOG_CRIT, "Client disconnected: ending session.\n"); - CC->kill_me = 1; + CC->kill_me = KILLME_CLIENT_DISCONNECTED; return; } syslog(LOG_INFO, ": %s\n", cmdbuf); diff --git a/citadel/modules/xmpp/serv_xmpp.c b/citadel/modules/xmpp/serv_xmpp.c index aa3017e49..d158b819d 100644 --- a/citadel/modules/xmpp/serv_xmpp.c +++ b/citadel/modules/xmpp/serv_xmpp.c @@ -439,10 +439,10 @@ void xmpp_xml_end(void *data, const char *supplied_el) { #ifdef HAVE_OPENSSL cprintf(""); CtdlModuleStartCryptoMsgs(NULL, NULL, NULL); - if (!CC->redirect_ssl) CC->kill_me = 1; + if (!CC->redirect_ssl) CC->kill_me = KILLME_NO_CRYPTO; #else cprintf(""); - CC->kill_me = 1; + CC->kill_me = KILLME_NO_CRYPTO; #endif } @@ -454,7 +454,7 @@ void xmpp_xml_end(void *data, const char *supplied_el) { syslog(LOG_DEBUG, "XMPP client shut down their stream\n"); xmpp_massacre_roster(); cprintf("\n"); - CC->kill_me = 1; + CC->kill_me = KILLME_CLIENT_LOGGED_OUT; } else { @@ -524,7 +524,7 @@ void xmpp_greeting(void) { XMPP->xp = XML_ParserCreateNS("UTF-8", ':'); if (XMPP->xp == NULL) { syslog(LOG_ALERT, "Cannot create XML parser!\n"); - CC->kill_me = 1; + CC->kill_me = KILLME_XML_PARSER; return; } @@ -550,7 +550,7 @@ void xmpp_command_loop(void) { } else { syslog(LOG_ERR, "Client disconnected: ending session.\n"); - CC->kill_me = 1; + CC->kill_me = KILLME_CLIENT_DISCONNECTED; } FreeStrBuf(&stream_input); } diff --git a/citadel/modules/xmpp/xmpp_presence.c b/citadel/modules/xmpp/xmpp_presence.c index 2ef90858c..4175771c4 100644 --- a/citadel/modules/xmpp/xmpp_presence.c +++ b/citadel/modules/xmpp/xmpp_presence.c @@ -3,7 +3,7 @@ * * Copyright (c) 2007-2010 by Art Cancro * - * This program is free software; you can redistribute it and/or modify + * This program is open source software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. diff --git a/citadel/server.h b/citadel/server.h index 70770a15a..d3b89f215 100644 --- a/citadel/server.h +++ b/citadel/server.h @@ -46,6 +46,33 @@ struct CtdlMessage { #define CTDLEXIT_LIBCITADEL 106 /* Incorrect version of libcitadel */ #define CTDL_EXIT_UNSUP_AUTH 107 /* Unsupported auth mode configured */ +/* + * Reasons why a session would be terminated (set CC->kill_me to these values) + */ +enum { + KILLME_NOT, + KILLME_UNKNOWN, + KILLME_CLIENT_LOGGED_OUT, + KILLME_IDLE, + KILLME_CLIENT_DISCONNECTED, + KILLME_AUTHFAILED, + KILLME_SERVER_SHUTTING_DOWN, + KILLME_MAX_SESSIONS_EXCEEDED, + KILLME_ADMIN_TERMINATE, + KILLME_SELECT_INTERRUPTED, + KILLME_SELECT_FAILED, + KILLME_WRITE_FAILED, + KILLME_SIMULATION_WORKER, + KILLME_NOLOGIN, + KILLME_NO_CRYPTO, + KILLME_READSTRING_FAILED, + KILLME_MALLOC_FAILED, + KILLME_QUOTA, + KILLME_READ_FAILED, + KILLME_ILLEGAL_MANAGESIEVE_COMMAND, + KILLME_SPAMMER, + KILLME_XML_PARSER +}; #define CS_STEALTH 1 /* stealth mode */ diff --git a/citadel/sysdep.c b/citadel/sysdep.c index 5468d24ca..628e955b3 100644 --- a/citadel/sysdep.c +++ b/citadel/sysdep.c @@ -496,7 +496,7 @@ int client_write(const char *buf, int nbytes) { syslog(LOG_DEBUG, "client_write(%d bytes) select() interrupted.\n", nbytes-bytes_written); if (CtdlThreadCheckStop()) { - CC->kill_me = 1; + CC->kill_me = KILLME_SELECT_INTERRUPTED; return (-1); } else { /* can't trust fd's and stuff so we need to re-create them */ @@ -508,7 +508,7 @@ int client_write(const char *buf, int nbytes) nbytes - bytes_written, strerror(errno), errno); cit_backtrace(); - Ctx->kill_me = 1; + Ctx->kill_me = KILLME_SELECT_FAILED; return -1; } } @@ -523,7 +523,7 @@ int client_write(const char *buf, int nbytes) strerror(errno), errno); cit_backtrace(); // syslog(LOG_DEBUG, "Tried to send: %s", &buf[bytes_written]); - Ctx->kill_me = 1; + Ctx->kill_me = KILLME_WRITE_FAILED; return -1; } bytes_written = bytes_written + retval; @@ -744,10 +744,10 @@ int client_read_to(char *buf, int bytes, int timeout) int HaveMoreLinesWaiting(CitContext *CCC) { - if ((CCC->kill_me == 1) || ( - (CCC->RecvBuf.ReadWritePointer == NULL) && - (StrLength(CCC->RecvBuf.Buf) == 0) && - (CCC->client_socket != -1)) ) + if ((CCC->kill_me != 0) || + ( (CCC->RecvBuf.ReadWritePointer == NULL) && + (StrLength(CCC->RecvBuf.Buf) == 0) && + (CCC->client_socket != -1)) ) return 0; else return 1; diff --git a/citadel/threads.c b/citadel/threads.c index 3f7aa7ee2..1b605d057 100644 --- a/citadel/threads.c +++ b/citadel/threads.c @@ -1209,7 +1209,7 @@ void *simulation_worker (void*arg) { this = CreateNewContext(); CtdlThreadSleep(1); - this->kill_me = 1; + this->kill_me = KILLME_SIMULATION_WORKER; this->state = CON_IDLE; dead_session_purge(1); begin_critical_section(S_SESSION_TABLE); -- 2.30.2