Upsie, usleep is 10^6 times smaller than sleep
[citadel.git] / citadel / citserver.c
index 3f9ac42ab5acd01ca8ac6a4b6bb7e726fa760b10..0c8955a8a2b3fb10386b589818e6a3618aff6088 100644 (file)
@@ -4,18 +4,12 @@
  * Copyright (c) 1987-2011 by the citadel.org team
  *
  * 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.
+ * it under the terms of the GNU General Public License, version 3.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- *
- * 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
  */
 
 #include "sysdep.h"
@@ -169,14 +163,10 @@ void master_startup(void) {
        
        syslog(LOG_DEBUG, "master_startup() started\n");
        time(&server_startup_time);
+       get_config();
 
        syslog(LOG_INFO, "Opening databases\n");
        open_databases();
-
-       ctdl_thread_internal_init_tsd();
-       
-       CtdlThreadAllocTSD();
-       
        check_ref_counts();
 
        syslog(LOG_INFO, "Creating base rooms (if necessary)\n");
@@ -201,6 +191,9 @@ void master_startup(void) {
        urandom = fopen("/dev/urandom", "r");
        if (urandom != NULL) {
                rv = fread(&seed, sizeof seed, 1, urandom);
+               if (rv == -1)
+                       syslog(LOG_EMERG, "failed to read random seed: %s\n", 
+                              strerror(errno));
                fclose(urandom);
        }
        else {
@@ -210,9 +203,6 @@ void master_startup(void) {
        srand(seed);
        srandom(seed);
 
-       syslog(LOG_INFO, "Initializing ipgm secret\n");
-       get_config();
-       config.c_ipgm_secret = rand();
        put_config();
 
        syslog(LOG_DEBUG, "master_startup() finished\n");
@@ -226,7 +216,7 @@ void master_cleanup(int exitcode) {
        struct CleanupFunctionHook *fcn;
        static int already_cleaning_up = 0;
 
-       if (already_cleaning_up) while(1) sleep(1);
+       if (already_cleaning_up) while(1) usleep(1000000);
        already_cleaning_up = 1;
 
        /* Run any cleanup routines registered by loadable modules */
@@ -244,10 +234,6 @@ void master_cleanup(int exitcode) {
        syslog(LOG_INFO, "Closing databases\n");
        close_databases();
 
-#ifdef DEBUG_MEMORY_LEAKS
-       dump_heap();
-#endif
-
        /* If the operator requested a halt but not an exit, halt here. */
        if (shutdown_and_halt) {
                syslog(LOG_NOTICE, "citserver: Halting server without exiting.\n");
@@ -307,10 +293,10 @@ void cmd_info(char *cmdbuf) {
 
        cprintf("%s\n", config.c_default_cal_zone);
 
-       /* Output load averages */
-       cprintf("%f\n", CtdlThreadLoadAvg);
-       cprintf("%f\n", CtdlThreadWorkerAvg);
-       cprintf("%d\n", CtdlThreadGetCount());
+       /* thread load averages -- temporarily disabled during refactoring of this code */
+       cprintf("0\n");         /* load average */
+       cprintf("0\n");         /* worker average */
+       cprintf("0\n");         /* thread count */
 
        cprintf("1\n");         /* yes, Sieve mail filtering is supported */
        cprintf("%d\n", config.c_enable_fulltext);
@@ -352,9 +338,9 @@ void cmd_time(char *argbuf)
    
    /* timezone and daylight global variables are not portable. */
 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
-   cprintf("%d %ld|%ld|%d\n", CIT_OK, (long)tv, tmp.tm_gmtoff, tmp.tm_isdst);
+   cprintf("%d %ld|%ld|%d|%ld\n", CIT_OK, (long)tv, tmp.tm_gmtoff, tmp.tm_isdst, server_startup_time);
 #else
-   cprintf("%d %ld|%ld|%d\n", CIT_OK, (long)tv, timezone, tmp.tm_isdst);
+   cprintf("%d %ld|%ld|%d|%ld\n", CIT_OK, (long)tv, timezone, tmp.tm_isdst, server_startup_time);
 #endif
 }
 
@@ -439,7 +425,7 @@ int is_public_client(void)
                                        }
                                }
                        }
-               fclose(fp);
+               if (fp != NULL) fclose(fp);
                pc_timestamp = time(NULL);
                end_critical_section(S_PUBLIC_CLIENTS);
        }
@@ -449,7 +435,7 @@ int is_public_client(void)
        for (i=0; i<num_parms(public_clients); ++i) {
                extract_token(addrbuf, public_clients, i, '|', sizeof addrbuf);
                if (!strcasecmp(CC->cs_addr, addrbuf)) {
-                       syslog(LOG_DEBUG, "... yes it is.\n");
+                       syslog(LOG_DEBUG, "... yes its local.\n");
                        return(1);
                }
        }
@@ -661,7 +647,7 @@ void GenerateRoomDisplay(char *real_room,
        if (viewed->room.QRflags & QR_PRIVATE) {
                CtdlRoomAccess(&viewed->room, &viewer->user, &ra, NULL);
                if ( (ra & UA_KNOWN) == 0) {
-                       strcpy(real_room, "<private room>");
+                       strcpy(real_room, " ");
                }
        }
 
@@ -698,14 +684,14 @@ int CtdlAccessCheck(int required_level) {
 
        if (CC->user.axlevel >= AxAideU) return(0);
        if (required_level >= ac_aide) {
-               cprintf("%d This command requires Aide access.\n",
+               cprintf("%d This command requires Admin access.\n",
                        ERROR + HIGHER_ACCESS_REQUIRED);
                return(-1);
        }
 
        if (is_room_aide()) return(0);
        if (required_level >= ac_room_aide) {
-               cprintf("%d This command requires Aide or Room Aide access.\n",
+               cprintf("%d This command requires Admin or Room Admin access.\n",
                        ERROR + HIGHER_ACCESS_REQUIRED);
                return(-1);
        }
@@ -765,36 +751,6 @@ void cmd_echo(char *etext)
 }
 
 
-/* 
- * Perform privilege escalation for an internal program
- */
-void cmd_ipgm(char *argbuf)
-{
-       int secret;
-
-       secret = extract_int(argbuf, 0);
-
-       /* For security reasons, we do NOT allow this command to run
-        * over the network.  Local sockets only.
-        */
-       if (!CC->is_local_socket) {
-               sleep(5);
-               cprintf("%d Authentication failed.\n", ERROR + PASSWORD_REQUIRED);
-       }
-       else if (secret == config.c_ipgm_secret) {
-               CC->internal_pgm = 1;
-               strcpy(CC->curr_user, "<internal program>");
-               CC->cs_flags = CC->cs_flags|CS_STEALTH;
-               cprintf("%d Authenticated as an internal program.\n", CIT_OK);
-       }
-       else {
-               sleep(5);
-               cprintf("%d Authentication failed.\n", ERROR + PASSWORD_REQUIRED);
-               syslog(LOG_ERR, "Warning: ipgm authentication failed.\n");
-               CC->kill_me = 1;
-       }
-}
-
 
 /*
  * Shut down the server
@@ -825,8 +781,8 @@ 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 */
-       CtdlThreadStopAll();
+       CC->kill_me = KILLME_SERVER_SHUTTING_DOWN;
+       server_shutting_down = 1;
 }
 
 
@@ -838,7 +794,7 @@ void cmd_halt(char *argbuf) {
        if (CtdlAccessCheck(ac_aide)) return;
 
        cprintf("%d Halting server.  Goodbye.\n", CIT_OK);
-       CtdlThreadStopAll();
+       server_shutting_down = 1;
        shutdown_and_halt = 1;
 }
 
@@ -896,9 +852,6 @@ void cmd_asyn(char *argbuf)
  */
 void begin_session(CitContext *con)
 {
-       socklen_t len;
-       struct sockaddr_in sin;
-
        /* 
         * Initialize some variables specific to our context.
         */
@@ -907,6 +860,7 @@ void begin_session(CitContext *con)
        con->download_fp = NULL;
        con->upload_fp = NULL;
        con->cached_msglist = NULL;
+       con->cached_num_msgs = 0;
        con->FirstExpressMessage = NULL;
        time(&con->lastcmd);
        time(&con->lastidle);
@@ -922,7 +876,6 @@ void begin_session(CitContext *con)
        safestrncpy(con->cs_addr, "", sizeof con->cs_addr);
        con->cs_UDSclientUID = -1;
        con->cs_host[sizeof con->cs_host - 1] = 0;
-       len = sizeof sin;
        if (!CC->is_local_socket) {
                locate_host(con->cs_host, sizeof con->cs_host,
                        con->cs_addr, sizeof con->cs_addr,
@@ -993,7 +946,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);
@@ -1002,6 +955,12 @@ void citproto_begin_session() {
 }
 
 
+void citproto_begin_admin_session() {
+       CC->internal_pgm = 1;
+       cprintf("%d %s Citadel server ADMIN CONNECTION ready.\n", CIT_OK, config.c_nodename);
+}
+
+
 void cmd_noop(char *argbuf)
 {
        cprintf("%d%cok\n", CIT_OK, CtdlCheckExpress() );
@@ -1017,7 +976,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;
 }
 
 
@@ -1034,25 +993,25 @@ void cmd_lout(char *argbuf)
  */
 void do_command_loop(void) {
        char cmdbuf[SIZ];
-       const char *old_name = NULL;
-       
-       old_name = CtdlThreadName("do_command_loop");
        
        time(&CC->lastcmd);
        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;
-               CtdlThreadName(old_name);
+               syslog(LOG_ERR, "Citadel client disconnected: ending session.\n");
+               CC->kill_me = KILLME_CLIENT_DISCONNECTED;
                return;
        }
 
        /* Log the server command, but don't show passwords... */
        if ( (strncasecmp(cmdbuf, "PASS", 4)) && (strncasecmp(cmdbuf, "SETP", 4)) ) {
-               syslog(LOG_INFO, "CtdlCommand [%s] [%s] %s\n", CTDLUSERIP, CC->curr_user, cmdbuf);
+               syslog(LOG_INFO, "[%d][%s(%ld)] %s",
+                       CC->cs_pid, CC->curr_user, CC->user.usernum, cmdbuf
+               );
        }
        else {
-               syslog(LOG_INFO, "CtdlCommand [%s] [%s] <password command hidden from log>\n", CTDLUSERIP, CC->curr_user);
+               syslog(LOG_INFO, "[%d][%s(%ld)] <password command hidden from log>",
+                       CC->cs_pid, CC->curr_user, CC->user.usernum
+               );
        }
 
        buffer_output();
@@ -1072,8 +1031,6 @@ void do_command_loop(void) {
                time(&CC->lastidle);
        }
        
-       CtdlThreadName(cmdbuf);
-               
        if ((strncasecmp(cmdbuf, "ENT0", 4))
           && (strncasecmp(cmdbuf, "MESG", 4))
           && (strncasecmp(cmdbuf, "MSGS", 4)))
@@ -1089,7 +1046,6 @@ void do_command_loop(void) {
 
        /* Run any after-each-command routines registered by modules */
        PerformSessionHooks(EVT_CMD);
-       CtdlThreadName(old_name);
 }
 
 
@@ -1119,7 +1075,6 @@ CTDL_MODULE_INIT(citserver)
                CtdlRegisterProtoHook(cmd_echo, "ECHO", "echo text back to the client");
                CtdlRegisterProtoHook(cmd_more, "MORE", "fetch the paginator prompt");
                CtdlRegisterProtoHook(cmd_iden, "IDEN", "identify the client software and location");
-               CtdlRegisterProtoHook(cmd_ipgm, "IPGM", "perform privilege escalation for internal programs");
                CtdlRegisterProtoHook(cmd_term, "TERM", "terminate another running session");
                CtdlRegisterProtoHook(cmd_down, "DOWN", "perform a server shutdown");
                CtdlRegisterProtoHook(cmd_halt, "HALT", "halt the server without exiting the server process");