Did away with lprintf all together now its called CtdlLogPrintf()
[citadel.git] / citadel / citserver.c
index 24b76c1dba9f1e8a991256b421ade5950604397f..f334f5cfd8d94f26426431794251aa848bf2a34a 100644 (file)
@@ -58,6 +58,7 @@
 #include "policy.h"
 #include "control.h"
 #include "euidindex.h"
+#include "svn_revision.h"
 
 #ifndef HAVE_SNPRINTF
 #include "snprintf.h"
@@ -70,7 +71,6 @@ struct CitContext *ContextList = NULL;
 struct CitContext* next_session = NULL;
 char *unique_session_numbers;
 int ScheduledShutdown = 0;
-int do_defrag = 0;
 time_t server_startup_time;
 int panic_fd;
 
@@ -89,9 +89,9 @@ void cit_backtrace(void)
        strings = backtrace_symbols(stack_frames, size);
        for (i = 0; i < size; i++) {
                if (strings != NULL)
-                       lprintf(1, "%s\n", strings[i]);
+                       CtdlLogPrintf(1, "%s\n", strings[i]);
                else
-                       lprintf(1, "%p\n", stack_frames[i]);
+                       CtdlLogPrintf(1, "%p\n", stack_frames[i]);
        }
        free(strings);
 #endif
@@ -112,9 +112,9 @@ void cit_panic_backtrace(int SigNum)
        strings = backtrace_symbols(stack_frames, size);
        for (i = 0; i < size; i++) {
                if (strings != NULL)
-                       lprintf(1, "%s\n", strings[i]);
+                       CtdlLogPrintf(1, "%s\n", strings[i]);
                else
-                       lprintf(1, "%p\n", stack_frames[i]);
+                       CtdlLogPrintf(1, "%p\n", stack_frames[i]);
        }
        free(strings);
 #endif
@@ -130,23 +130,19 @@ void master_startup(void) {
        FILE *urandom;
        struct ctdlroom qrbuf;
        
-       lprintf(CTDL_DEBUG, "master_startup() started\n");
+       CtdlLogPrintf(CTDL_DEBUG, "master_startup() started\n");
        time(&server_startup_time);
 
-       lprintf(CTDL_INFO, "Opening databases\n");
+       CtdlLogPrintf(CTDL_INFO, "Opening databases\n");
        open_databases();
 
        ctdl_thread_internal_init_tsd();
        
        CtdlThreadAllocTSD();
        
-       if (do_defrag) {
-               defrag_databases();
-       }
-
        check_ref_counts();
 
-       lprintf(CTDL_INFO, "Creating base rooms (if necessary)\n");
+       CtdlLogPrintf(CTDL_INFO, "Creating base rooms (if necessary)\n");
        create_room(config.c_baseroom,  0, "", 0, 1, 0, VIEW_BBS);
        create_room(AIDEROOM,           3, "", 0, 1, 0, VIEW_BBS);
        create_room(SYSCONFIGROOM,      3, "", 0, 1, 0, VIEW_BBS);
@@ -158,7 +154,13 @@ void master_startup(void) {
                 lputroom(&qrbuf);
         }
 
-       lprintf(CTDL_INFO, "Seeding the pseudo-random number generator...\n");
+       /* Aide needs to be public postable, else we're not RFC conformant. */
+        if (lgetroom(&qrbuf, AIDEROOM) == 0) {
+                qrbuf.QRflags2 |= QR2_SMTP_PUBLIC;
+                lputroom(&qrbuf);
+        }
+
+       CtdlLogPrintf(CTDL_INFO, "Seeding the pseudo-random number generator...\n");
        urandom = fopen("/dev/urandom", "r");
        if (urandom != NULL) {
                fread(&seed, sizeof seed, 1, urandom);
@@ -170,12 +172,12 @@ void master_startup(void) {
        }
        srandom(seed);
 
-       lprintf(CTDL_INFO, "Initializing ipgm secret\n");
+       CtdlLogPrintf(CTDL_INFO, "Initializing ipgm secret\n");
        get_config();
        config.c_ipgm_secret = rand();
        put_config();
 
-       lprintf(CTDL_DEBUG, "master_startup() finished\n");
+       CtdlLogPrintf(CTDL_DEBUG, "master_startup() finished\n");
 }
 
 
@@ -201,7 +203,7 @@ void master_cleanup(int exitcode) {
        sysdep_master_cleanup();
        
        /* Close databases */
-       lprintf(CTDL_INFO, "Closing databases\n");
+       CtdlLogPrintf(CTDL_INFO, "Closing databases\n");
        close_databases();
 
 #ifdef DEBUG_MEMORY_LEAKS
@@ -210,7 +212,7 @@ void master_cleanup(int exitcode) {
 
        /* If the operator requested a halt but not an exit, halt here. */
        if (shutdown_and_halt) {
-               lprintf(CTDL_NOTICE, "citserver: Halting server without exiting.\n");
+               CtdlLogPrintf(CTDL_NOTICE, "citserver: Halting server without exiting.\n");
                fflush(stdout); fflush(stderr);
                while(1) {
                        sleep(32767);
@@ -220,7 +222,7 @@ void master_cleanup(int exitcode) {
        release_control();
 
        /* Now go away. */
-       lprintf(CTDL_NOTICE, "citserver: Exiting with status %d\n", exitcode);
+       CtdlLogPrintf(CTDL_NOTICE, "citserver: Exiting with status %d\n", exitcode);
        fflush(stdout); fflush(stderr);
        
        if (restart_server != 0)
@@ -236,31 +238,28 @@ void master_cleanup(int exitcode) {
 void RemoveContext (struct CitContext *con)
 {
        if (con==NULL) {
-               lprintf(CTDL_ERR,
+               CtdlLogPrintf(CTDL_ERR,
                        "WARNING: RemoveContext() called with NULL!\n");
                return;
        }
-       lprintf(CTDL_DEBUG, "RemoveContext() session %d\n", con->cs_pid);
+       CtdlLogPrintf(CTDL_DEBUG, "RemoveContext() session %d\n", con->cs_pid);
 
        /* Run any cleanup routines registered by loadable modules.
         * Note: We have to "become_session()" because the cleanup functions
         *       might make references to "CC" assuming it's the right one.
         */
        become_session(con);
+       logout();
        PerformSessionHooks(EVT_STOP);
        become_session(NULL);
 
-       /* Now handle all of the administrivia. */
-       lprintf(CTDL_DEBUG, "Calling logout(%d)\n", con->cs_pid);
-       logout(con);
-
-       lprintf(CTDL_NOTICE, "[%3d] Session ended.\n", con->cs_pid);
+       CtdlLogPrintf(CTDL_NOTICE, "[%3d] Session ended.\n", con->cs_pid);
 
        /* If the client is still connected, blow 'em away. */
-       lprintf(CTDL_DEBUG, "Closing socket %d\n", con->client_socket);
+       CtdlLogPrintf(CTDL_DEBUG, "Closing socket %d\n", con->client_socket);
        close(con->client_socket);
 
-       lprintf(CTDL_DEBUG, "Done with RemoveContext()\n");
+       CtdlLogPrintf(CTDL_DEBUG, "Done with RemoveContext()\n");
 }
 
 
@@ -293,18 +292,23 @@ void cmd_info(void) {
        cprintf("0\n"); /* 1 = no, this server is not LDAP-enabled */
 #endif
 
-       if (config.c_auth_mode == 1) {
-               cprintf("1\n"); /* "create new user" never works with host auth */
+       if (config.c_auth_mode == AUTHMODE_NATIVE) {
+               cprintf("%d\n", config.c_disable_newu);
        }
        else {
-               cprintf("%d\n", config.c_disable_newu); /* otherwise, site defined */
+               cprintf("1\n"); /* "create new user" does not work with non-native auth modes */
        }
 
        cprintf("%s\n", config.c_default_cal_zone);
+
        /* Output load averages */
        cprintf("%f\n", CtdlThreadLoadAvg);
        cprintf("%f\n", CtdlThreadWorkerAvg);
        cprintf("%d\n", CtdlThreadGetCount());
+
+       cprintf("1\n");         /* yes, Sieve mail filtering is supported */
+       cprintf("%d\n", config.c_enable_fulltext);
+       cprintf("%s\n", svn_revision());
        
        cprintf("000\n");
 }
@@ -352,11 +356,16 @@ int is_public_client(void)
        char addrbuf[1024];
        FILE *fp;
        int i;
+       char *public_clientspos;
+       char *public_clientsend;
+       char *paddr = NULL;
        struct stat statbuf;
        static time_t pc_timestamp = 0;
        static char public_clients[SIZ];
        static char public_clients_file[SIZ];
 
+#define LOCALHOSTSTR "127.0.0.1"
+
        snprintf(public_clients_file, 
                         sizeof public_clients_file,
                         "%s/public_clients",
@@ -370,62 +379,71 @@ int is_public_client(void)
         */
        if (stat(public_clients_file, &statbuf) != 0) {
                /* No public_clients file exists, so bail out */
-               lprintf(CTDL_WARNING, "Warning: '%s' does not exist\n", 
+               CtdlLogPrintf(CTDL_WARNING, "Warning: '%s' does not exist\n", 
                                public_clients_file);
                return(0);
        }
 
        if (statbuf.st_mtime > pc_timestamp) {
                begin_critical_section(S_PUBLIC_CLIENTS);
-               lprintf(CTDL_INFO, "Loading %s\n", public_clients_file);
+               CtdlLogPrintf(CTDL_INFO, "Loading %s\n", public_clients_file);
 
-               safestrncpy(public_clients, "127.0.0.1", sizeof public_clients);
+               public_clientspos = &public_clients[0];
+               public_clientsend = public_clientspos + SIZ;
+               safestrncpy(public_clientspos, LOCALHOSTSTR, sizeof public_clients);
+               public_clientspos += sizeof(LOCALHOSTSTR) - 1;
+               
                if (hostname_to_dotted_quad(addrbuf, config.c_fqdn) == 0) {
-                       strcat(public_clients, "|");
-                       strcat(public_clients, addrbuf);
+                       *(public_clientspos++) = '|';
+                       paddr = &addrbuf[0];
+                       while (!IsEmptyStr (paddr) && 
+                              (public_clientspos < public_clientsend))
+                               *(public_clientspos++) = *(paddr++);
                }
 
                fp = fopen(public_clients_file, "r");
-               if (fp != NULL) while (fgets(buf, sizeof buf, fp)!=NULL) {
-                       char *ptr;
-                       ptr = buf;
-                       while (!IsEmptyStr(ptr)) {
-                               if (*ptr == '#') {
-                                       *ptr = 0;
-                                       break;
-                               }
+               if (fp != NULL) 
+                       while ((fgets(buf, sizeof buf, fp)!=NULL) &&
+                              (public_clientspos < public_clientsend)){
+                               char *ptr;
+                               ptr = buf;
+                               while (!IsEmptyStr(ptr)) {
+                                       if (*ptr == '#') {
+                                               *ptr = 0;
+                                               break;
+                                       }
                                else ptr++;
-                       }
-                       ptr--;
-                       while (ptr>buf && isspace(*ptr)) {
-                               *(ptr--) = 0;
-                       }
-                       if (hostname_to_dotted_quad(addrbuf, buf) == 0) {
-                               if ((strlen(public_clients) +
-                                  strlen(addrbuf) + 2)
-                                  < sizeof(public_clients)) {
-                                       strcat(public_clients, "|");
-                                       strcat(public_clients, addrbuf);
+                               }
+                               ptr--;
+                               while (ptr>buf && isspace(*ptr)) {
+                                       *(ptr--) = 0;
+                               }
+                               if (hostname_to_dotted_quad(addrbuf, buf) == 0) {
+                                       *(public_clientspos++) = '|';
+                                       paddr = addrbuf;
+                                       while (!IsEmptyStr(paddr) && 
+                                              (public_clientspos < public_clientsend)){
+                                               *(public_clientspos++) = *(paddr++);
+                                       }
                                }
                        }
-               }
                fclose(fp);
                pc_timestamp = time(NULL);
                end_critical_section(S_PUBLIC_CLIENTS);
        }
 
-       lprintf(CTDL_DEBUG, "Checking whether %s is a local or public client\n",
+       CtdlLogPrintf(CTDL_DEBUG, "Checking whether %s is a local or public client\n",
                CC->cs_addr);
        for (i=0; i<num_parms(public_clients); ++i) {
                extract_token(addrbuf, public_clients, i, '|', sizeof addrbuf);
                if (!strcasecmp(CC->cs_addr, addrbuf)) {
-                       lprintf(CTDL_DEBUG, "... yes it is.\n");
+                       CtdlLogPrintf(CTDL_DEBUG, "... yes it is.\n");
                        return(1);
                }
        }
 
        /* No hits.  This is not a public client. */
-       lprintf(CTDL_DEBUG, "... no it isn't.\n");
+       CtdlLogPrintf(CTDL_DEBUG, "... no it isn't.\n");
        return(0);
 }
 
@@ -469,7 +487,7 @@ void cmd_iden(char *argbuf)
        }
 
        if (do_lookup) {
-               lprintf(CTDL_DEBUG, "Looking up hostname '%s'\n", from_host);
+               CtdlLogPrintf(CTDL_DEBUG, "Looking up hostname '%s'\n", from_host);
                if ((addr.s_addr = inet_addr(from_host)) != -1) {
                        locate_host(CC->cs_host, sizeof CC->cs_host,
                                CC->cs_addr, sizeof CC->cs_addr,
@@ -481,7 +499,7 @@ void cmd_iden(char *argbuf)
                }
        }
 
-       lprintf(CTDL_NOTICE, "Client %d/%d/%01d.%02d (%s) from %s\n",
+       CtdlLogPrintf(CTDL_NOTICE, "Client %d/%d/%01d.%02d (%s) from %s\n",
                dev_code,
                cli_code,
                (rev_level / 100),
@@ -648,9 +666,9 @@ void GenerateRoomDisplay(char *real_room,
        }
 
        if (viewed->cs_flags & CS_CHAT) {
-               while (strlen(real_room) < 14)
+               while (strlen(real_room) < 14) {
                        strcat(real_room, " ");
-
+               }
                strcpy(&real_room[14], "<chat>");
        }
 
@@ -709,7 +727,7 @@ void cmd_term(char *cmdbuf)
                return;
        }
 
-       lprintf(CTDL_DEBUG, "Locating session to kill\n");
+       CtdlLogPrintf(CTDL_DEBUG, "Locating session to kill\n");
        begin_critical_section(S_SESSION_TABLE);
        for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
                if (session_num == ccptr->cs_pid) {
@@ -788,7 +806,7 @@ void cmd_ipgm(char *argbuf)
                sleep(5);
                cprintf("%d Authentication failed.\n",
                        ERROR + PASSWORD_REQUIRED);
-               lprintf(CTDL_ERR, "Warning: ipgm authentication failed.\n");
+               CtdlLogPrintf(CTDL_ERR, "Warning: ipgm authentication failed.\n");
                CC->kill_me = 1;
        }
 
@@ -821,7 +839,7 @@ void cmd_down(char *argbuf) {
                        Reply = "%d Restarting server.  See you soon.\n";
                if ((restart_server > 0) && !running_as_daemon)
                {
-                       lprintf(CTDL_ERR, "The user requested restart, but not running as deamon! Geronimooooooo!\n");
+                       CtdlLogPrintf(CTDL_ERR, "The user requested restart, but not running as deamon! Geronimooooooo!\n");
                        Reply = "%d Warning, not running in deamon mode. maybe we will come up again, but don't lean on it.\n";
                        state = ERROR;
                }
@@ -863,7 +881,7 @@ void cmd_scdn(char *argbuf)
                restart_server = 1;
                if (!running_as_daemon)
                {
-                       lprintf(CTDL_ERR, "The user requested restart, but not running as deamon! Geronimooooooo!\n");
+                       CtdlLogPrintf(CTDL_ERR, "The user requested restart, but not running as deamon! Geronimooooooo!\n");
                        Reply = "%d %d Warning, not running in deamon mode. maybe we will come up again, but don't lean on it.\n";
                        state = ERROR;
                }
@@ -962,7 +980,12 @@ void begin_session(struct CitContext *con)
                con->nologin = 1;
        }
 
-       lprintf(CTDL_NOTICE, "Session started.\n");
+       if (!CC->is_local_socket) {
+               CtdlLogPrintf(CTDL_NOTICE, "Session started from %s [%s].\n", con->cs_host, con->cs_addr);
+       }
+       else {
+               CtdlLogPrintf(CTDL_NOTICE, "Session started via local socket.\n");
+       }
 
        /* Run any session startup routines registered by loadable modules */
        PerformSessionHooks(EVT_START);
@@ -998,7 +1021,7 @@ void do_command_loop(void) {
        time(&CC->lastcmd);
        memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
        if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
-               lprintf(CTDL_ERR, "Client disconnected: ending session.\n");
+               CtdlLogPrintf(CTDL_ERR, "Client disconnected: ending session.\n");
                CC->kill_me = 1;
                CtdlThreadName(old_name);
                return;
@@ -1007,10 +1030,10 @@ void do_command_loop(void) {
        /* Log the server command, but don't show passwords... */
        if ( (strncasecmp(cmdbuf, "PASS", 4))
           && (strncasecmp(cmdbuf, "SETP", 4)) ) {
-               lprintf(CTDL_INFO, "%s\n", cmdbuf);
+               CtdlLogPrintf(CTDL_INFO, "%s\n", cmdbuf);
        }
        else {
-               lprintf(CTDL_INFO, "<password command sent>\n");
+               CtdlLogPrintf(CTDL_INFO, "<password command sent>\n");
        }
 
        buffer_output();
@@ -1043,6 +1066,10 @@ void do_command_loop(void) {
                cprintf("%d%cok\n", CIT_OK, CtdlCheckExpress() );
        }
        
+       else if (!strncasecmp(cmdbuf, "XYZZY", 5)) {
+               cprintf("%d Nothing happens.\n", CIT_OK);
+       }
+       
        else if (!strncasecmp(cmdbuf, "QNOP", 4)) {
                /* do nothing, this command returns no response */
        }
@@ -1057,7 +1084,7 @@ void do_command_loop(void) {
        }
 
        else if (!strncasecmp(cmdbuf,"LOUT",4)) {
-               if (CC->logged_in) logout(CC);
+               if (CC->logged_in) logout();
                cprintf("%d logged out.\n", CIT_OK);
        }
 
@@ -1393,10 +1420,13 @@ void do_command_loop(void) {
                cmd_isme(&cmdbuf[5]);
        }
 
+       else if (!strncasecmp(cmdbuf, "RENU", 4)) {
+               cmd_renu(&cmdbuf[5]);
+       }
+
        else if (!DLoader_Exec_Cmd(cmdbuf)) {
-               cprintf("%d Unrecognized or unsupported command.\n",
-                       ERROR + CMD_NOT_SUPPORTED);
-              }
+               cprintf("%d Unrecognized or unsupported command.\n", ERROR + CMD_NOT_SUPPORTED);
+       }
 
        unbuffer_output();