]> code.citadel.org Git - citadel.git/blobdiff - citadel/citserver.c
More session table stability nonsense
[citadel.git] / citadel / citserver.c
index 6b013926a11fcbe1ee3d51a7946159666653c256..15c6d0c6c1239c439e51b32146844a796d317796 100644 (file)
@@ -1,3 +1,4 @@
+/* $Id$ */
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdio.h>
@@ -26,6 +27,9 @@
 #include "room_ops.h"
 #include "file_ops.h"
 #include "dynloader.h"
+#include "policy.h"
+#include "control.h"
+#include "tools.h"
 
 struct CitContext *ContextList = NULL;
 int ScheduledShutdown = 0;
@@ -72,6 +76,7 @@ void master_cleanup(void) {
 
        /* Now go away. */
        lprintf(3, "citserver: exiting.\n");
+       fflush(stdout); fflush(stderr);
        exit(0);
        }
 
@@ -113,9 +118,6 @@ void cleanup_stuff(void *arg)
        /* Deallocate any message list we might have in memory */
        if (CC->msglist != NULL) free(CC->msglist);
 
-       /* Purge any stale user/room relationships */
-       PurgeStaleRelationships();
-
        /* Now get rid of the session and context */
        lprintf(7, "cleanup_stuff() calling RemoveContext(%d)\n", CC->cs_pid);
        RemoveContext(CC);
@@ -132,8 +134,8 @@ void cleanup_stuff(void *arg)
  */
 void set_wtmpsupp(char *newtext)
 {
-       strncpy(CC->cs_room,newtext,ROOMNAMELEN-1);
-       CC->cs_room[ROOMNAMELEN-1] = 0;
+       strncpy(CC->cs_room,newtext,sizeof CC->cs_room);
+       CC->cs_room[sizeof CC->cs_room - 1] = 0;
        time(&CC->cs_lastupdt);
 
        /* Run any routines registered by loadable modules */
@@ -141,6 +143,23 @@ void set_wtmpsupp(char *newtext)
        }
 
 
+/*
+ * call set_wtmpsupp() with the name of the current room, modified a bit...
+ */
+void set_wtmpsupp_to_current_room() {
+       if (CC->quickroom.QRflags & QR_PRIVATE) {
+               set_wtmpsupp("<private room>");
+               }
+       else if (CC->quickroom.QRflags & QR_MAILBOX) {
+               set_wtmpsupp(&CC->quickroom.QRname[11]);
+               }
+       else {
+               set_wtmpsupp(CC->quickroom.QRname);
+               }
+       }
+
+
+
 /*
  * cmd_info()  -  tell the client about this server
  */
@@ -160,16 +179,19 @@ void cmd_info(void) {
        cprintf("000\n");
        }
 
-void cmd_rchg(char *newroomname)
+void cmd_rchg(char *argbuf)
 {
-   if ((newroomname) && (newroomname[0]))
-   {
-      bzero(CC->fake_roomname, ROOMNAMELEN);
-      strncpy(CC->fake_roomname, newroomname, ROOMNAMELEN-1);
-   }
-   else
-      CC->fake_roomname[0] = '\0';
-   cprintf("%d\n",OK);
+       char newroomname[ROOMNAMELEN];
+
+       extract(newroomname, argbuf, 0);
+       if (strlen(newroomname) > 0) {
+               strncpy(CC->fake_roomname, newroomname, ROOMNAMELEN);
+               CC->fake_roomname[ROOMNAMELEN - 1] = 0;
+               }
+       else {
+               strcpy(CC->fake_roomname, "");
+               }
+       cprintf("%d OK\n", OK);
 }
 
 void cmd_hchg(char *newhostname)
@@ -180,8 +202,8 @@ void cmd_hchg(char *newhostname)
       strncpy(CC->fake_hostname, newhostname, 24);
    }
    else
-      CC->fake_hostname[0] = '\0';
-   cprintf("%d\n",OK);
+      strcpy(CC->fake_hostname, "");
+   cprintf("%d OK\n",OK);
 }
 
 void cmd_uchg(char *newusername)
@@ -278,7 +300,8 @@ void cmd_iden(char *argbuf)
        rev_level = extract_int(argbuf,2);
        extract(desc,argbuf,3);
 
-       strcpy(from_host,config.c_fqdn);
+       strncpy(from_host,config.c_fqdn,sizeof from_host);
+       from_host[sizeof from_host - 1] = 0;
        if (num_parms(argbuf)>=5) extract(from_host,argbuf,4);
 
        CC->cs_clientdev = dev_code;
@@ -292,7 +315,7 @@ void cmd_iden(char *argbuf)
                strncpy(CC->cs_host,from_host,24);
                CC->cs_host[24] = 0;
                }
-       set_wtmpsupp(CC->quickroom.QRname);
+       set_wtmpsupp_to_current_room();
 
        syslog(LOG_NOTICE,"client %d/%d/%01d.%02d (%s)\n",
                dev_code,
@@ -334,7 +357,7 @@ void cmd_stel(char *cmdbuf)
                        CC->cs_flags = CC->cs_flags|CS_STEALTH;
                }
 
-       set_wtmpsupp(CC->quickroom.QRname);
+       set_wtmpsupp_to_current_room();
        cprintf("%d Ok\n",OK);
        }
 
@@ -418,7 +441,7 @@ void cmd_emsg(char *mname)
        free(dirs[1]);
 
        if (strlen(targ)==0) {
-               sprintf(targ, "./help/%s", buf);
+               snprintf(targ, sizeof targ, "./help/%s", buf);
                }
 
        mfp = fopen(targ,"w");
@@ -519,6 +542,7 @@ void cmd_term(char *cmdbuf)
 {
        int session_num;
        struct CitContext *ccptr;
+       int session_to_kill = 0;
 
        if (!CC->logged_in) {
                cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
@@ -537,15 +561,23 @@ void cmd_term(char *cmdbuf)
                return;
                }
 
+       lprintf(9, "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) {
-                       kill_session(ccptr->cs_pid);
-                       cprintf("%d Session terminated.\n", OK);
-                       return;
+                       session_to_kill = ccptr->cs_pid;
                        }
                }
+       end_critical_section(S_SESSION_TABLE);
+       lprintf(9, "session_to_kill == %d\n", session_to_kill);
 
-       cprintf("%d No such session.\n", ERROR);
+       if (session_to_kill > 0) {
+               kill_session(ccptr->cs_pid);
+               cprintf("%d Session terminated.\n", OK);
+               }
+       else {
+               cprintf("%d No such session.\n", ERROR);
+               }
        }
 
 
@@ -640,7 +672,7 @@ void cmd_scdn(char *argbuf)
 void *context_loop(struct CitContext *con)
 {
        char cmdbuf[256];
-       int session_num;
+       int num_sessions;
 
        /*
         * Wedge our way into the context table.
@@ -664,7 +696,7 @@ void *context_loop(struct CitContext *con)
        strcpy(CC->cs_clientname, "(unknown)");
        strcpy(CC->curr_user,"");
        strcpy(CC->net_node,"");
-       sprintf(CC->temp,"/tmp/CitServer.%d.%d", getpid(), CC->cs_pid);
+       snprintf(CC->temp, sizeof CC->temp, "/tmp/CitServer.%d.%d", getpid(), CC->cs_pid);
        strcpy(CC->cs_room, "");
        strncpy(CC->cs_host, config.c_fqdn, sizeof CC->cs_host);
        CC->cs_host[sizeof CC->cs_host - 1] = 0;
@@ -673,9 +705,9 @@ void *context_loop(struct CitContext *con)
        CC->upload_type = UPL_FILE;
        CC->dl_is_net = 0;
 
-       session_num = session_count();
+       num_sessions = session_count();
        CC->nologin = 0;
-       if ((config.c_maxsessions > 0)&&(session_num >= config.c_maxsessions))
+       if ((config.c_maxsessions > 0)&&(num_sessions > config.c_maxsessions))
                CC->nologin = 1;
 
        if (CC->nologin==1) {
@@ -696,9 +728,9 @@ void *context_loop(struct CitContext *con)
 
        do {
                time(&CC->lastcmd);
+               bzero(cmdbuf, sizeof cmdbuf); /* Clear it, just in case */
                if (client_gets(cmdbuf) < 1) cleanup(EXIT_NULL);
                lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
-               /* hook_command_received(CC->cs_pid, cmdbuf); FIX */
 
                /*
                 * Let other clients see the last command we executed, but
@@ -1056,6 +1088,18 @@ void *context_loop(struct CitContext *con)
                        cmd_asup(&cmdbuf[5]);
                        }
 
+               else if (!strncasecmp(cmdbuf, "GPEX", 4)) {
+                       cmd_gpex(&cmdbuf[5]);
+                       }
+
+               else if (!strncasecmp(cmdbuf, "SPEX", 4)) {
+                       cmd_spex(&cmdbuf[5]);
+                       }
+
+               else if (!strncasecmp(cmdbuf, "CONF", 4)) {
+                       cmd_conf(&cmdbuf[5]);
+                       }
+
                else if (!DLoader_Exec_Cmd(cmdbuf))
                        {
                           cprintf("%d Unrecognized or unsupported command.\n",