Grabbed a chunk of code out of citserver.c cmd_term() and used it to create
authorDave West <davew@uncensored.citadel.org>
Tue, 10 Nov 2009 21:07:22 +0000 (21:07 +0000)
committerDave West <davew@uncensored.citadel.org>
Tue, 10 Nov 2009 21:07:22 +0000 (21:07 +0000)
a new function
CtdlTerminateOtherSession()
Placed this function in context.c since it manipulates the contexts.
Exposed this function to the API

Noticed we don't seem to have a client that uses the TERM command.

citadel/citserver.c
citadel/context.c
citadel/context.h

index d0c7007ff37cdd020b21e8c76f7752f7c6a57d5f..e61ed3fd060582e258f6ee15053ab0be6935e925 100644 (file)
@@ -708,35 +708,19 @@ int CtdlAccessCheck(int required_level) {
 void cmd_term(char *cmdbuf)
 {
        int session_num;
-       CitContext *ccptr;
-       int found_it = 0;
-       int allowed = 0;
+       int terminated = 0;
 
        session_num = extract_int(cmdbuf, 0);
-       if (session_num == CC->cs_pid) {
+
+       terminated = CtdlTerminateOtherSession(session_num);
+
+       if (terminated < 0) {
                cprintf("%d You can't kill your own session.\n", ERROR + ILLEGAL_VALUE);
                return;
        }
 
-       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) {
-                       found_it = 1;
-                       if ((ccptr->user.usernum == CC->user.usernum)
-                          || (CC->user.axlevel >= 6)) {
-                               allowed = 1;
-                               ccptr->kill_me = 1;
-                       }
-                       else {
-                               allowed = 0;
-                       }
-               }
-       }
-       end_critical_section(S_SESSION_TABLE);
-
-       if (found_it) {
-               if (allowed) {
+       if (terminated & TERM_FOUND) {
+               if (terminated == TERM_KILLED) {
                        cprintf("%d Session terminated.\n", CIT_OK);
                }
                else {
index c678829149b0179a51be598c7a5151ebe7c73005..70fdd2795051dc4c6caffd453eba4c3e814c7ddf 100644 (file)
@@ -129,6 +129,40 @@ int CtdlIsSingleUser(void)
 
 
 
+
+/*
+ * Locate a context by its session number and terminate it if the user is able.
+ * User can NOT terminate their current session.
+ * User CAN terminate any other session that has them logged in.
+ * Aide CAN terminate any session except the current one.
+ */
+int CtdlTerminateOtherSession (int session_num)
+{
+       int ret = 0;
+       CitContext *ccptr;
+
+       if (session_num == CC->cs_pid) {
+               return TERM_NOTALLOWED;
+       }
+
+       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) {
+                       ret |= TERM_FOUND;
+                       if ((ccptr->user.usernum == CC->user.usernum)
+                          || (CC->user.axlevel >= 6)) {
+                               ret |= TERM_ALLOWED;
+                               ccptr->kill_me = 1;
+                       }
+               }
+       }
+       end_critical_section(S_SESSION_TABLE);
+       return ret;
+}
+
+
+
 /*
  * Check to see if the user who we just sent mail to is logged in.  If yes,
  * bump the 'new mail' counter for their session.  That enables them to
index 3754a20be3d28e2f005cb7fc835a5fcd34d8d660..ecf08a0adb84bb010e7e4307bd97a67195670554 100644 (file)
@@ -150,5 +150,10 @@ void dead_session_purge(int force);
 void BumpNewMailCounter(long) __attribute__ ((deprecated));
 
 void terminate_idle_sessions(void);
-
+int CtdlTerminateOtherSession (int session_num);
+/* bits returned by CtdlTerminateOtherSession */
+#define TERM_FOUND     0x01
+#define TERM_ALLOWED   0x02
+#define TERM_KILLED    0x03
+#define TERM_NOTALLOWED -1
 #endif /* CONTEXT_H */