]> code.citadel.org Git - citadel.git/blobdiff - citadel/user_ops.c
* Split cmd_user() and cmd_pass() into frontend/backend functions
[citadel.git] / citadel / user_ops.c
index 608d4a584cb2dfa5f7771a2a66a20edca76fd585..3ed2d286894123533c0f9b283a42fbc2bcb5df34 100644 (file)
@@ -14,9 +14,6 @@
 #include <string.h>
 #include <syslog.h>
 #include <limits.h>
-#ifdef HAVE_PTHREAD_H
-#include <pthread.h>
-#endif
 #ifndef ENABLE_CHKPWD
 #include "auth.h"
 #endif
@@ -245,24 +242,21 @@ int getuserbynumber(struct usersupp *usbuf, long int number)
 
 
 /*
- * USER cmd
+ * Back end for cmd_user() and its ilk
  */
-void cmd_user(char *cmdbuf)
+int CtdlLoginExistingUser(char *username)
 {
-       char username[256];
        char autoname[256];
        int found_user = 0;
        struct passwd *p;
        int a;
 
-       extract(username,cmdbuf,0);
        username[25] = 0;
        strproc(username);
 
        if ((CC->logged_in)) {
-               cprintf("%d Already logged in.\n",ERROR);
-               return;
-               }
+               return login_already_logged_in;
+       }
 
        found_user = getuser(&CC->usersupp,username);
        if (found_user != 0) {
@@ -272,24 +266,56 @@ void cmd_user(char *cmdbuf)
                        for (a=0; a<strlen(autoname); ++a)
                                if (autoname[a]==',') autoname[a]=0;
                        found_user = getuser(&CC->usersupp,autoname);
-                       }
                }
+       }
        if (found_user == 0) {
                if (((CC->nologin)) && (CC->usersupp.axlevel < 6)) {
-                       cprintf("%d %s: Too many users are already online (maximum is %d)\n",
-                       ERROR+MAX_SESSIONS_EXCEEDED,
-                       config.c_nodename,config.c_maxsessions);
-                       }
+                       return login_too_many_users;
+               }
                else {
                        strcpy(CC->curr_user,CC->usersupp.fullname);
+                       return login_ok;
+               }
+       }
+       return login_not_found;
+}
+
+
+
+/*
+ * USER cmd
+ */
+void cmd_user(char *cmdbuf)
+{
+       char username[256];
+       int a;
+
+       extract(username,cmdbuf,0);
+       username[25] = 0;
+       strproc(username);
+
+       a = CtdlLoginExistingUser(username);
+       switch(a) {
+               case login_already_logged_in:
+                       cprintf("%d Already logged in.\n",ERROR);
+                       return;
+               case login_too_many_users:
+                       cprintf("%d %s: "
+                               "Too many users are already online "
+                               "(maximum is %d)\n",
+                               ERROR+MAX_SESSIONS_EXCEEDED,
+                               config.c_nodename,config.c_maxsessions);
+                       return;
+               case login_ok:
                        cprintf("%d Password required for %s\n",
                                MORE_DATA,CC->curr_user);
-                       }
-               }
-       else {
-               cprintf("%d %s not found.\n",ERROR,username);
-               }
+                       return;
+               case login_not_found:
+                       cprintf("%d %s not found.\n", ERROR, username);
+                       return;
+               cprintf("%d Internal error\n", ERROR);
        }
+}
 
 
 
@@ -319,14 +345,21 @@ void session_startup(void) {
         /* Run any cleanup routines registered by loadable modules */
        PerformSessionHooks(EVT_LOGIN);
 
-       cprintf("%d %s|%d|%d|%d|%u|%ld\n",OK,CC->usersupp.fullname,CC->usersupp.axlevel,
-               CC->usersupp.timescalled,CC->usersupp.posted,CC->usersupp.flags,
-               CC->usersupp.usernum);
        usergoto(BASEROOM,0);           /* Enter the lobby */   
        rec_log(CL_LOGIN,CC->curr_user);
        }
 
 
+void logged_in_response(void) {
+       cprintf("%d %s|%d|%d|%d|%u|%ld\n",
+               OK, CC->usersupp.fullname, CC->usersupp.axlevel,
+               CC->usersupp.timescalled, CC->usersupp.posted,
+               CC->usersupp.flags,
+               CC->usersupp.usernum);
+}
+
+
+
 /* 
  * misc things to be taken care of when a user is logged out
  */
@@ -404,24 +437,20 @@ static int validpw(uid_t uid, const char *pass)
        }
 #endif
 
-void cmd_pass(char *buf)
+
+
+int CtdlTryPassword(char *password)
 {
-       char password[256];
        int code;
 
-       extract(password,buf,0);
-
        if ((CC->logged_in)) {
-               cprintf("%d Already logged in.\n",ERROR);
-               return;
+               return pass_already_logged_in;
                }
-       if (!strcmp(CC->curr_user,"")) {
-               cprintf("%d You must send a name with USER first.\n",ERROR);
-               return;
+       if (!strcmp(CC->curr_user, NLI)) {
+               return pass_no_user;
                }
-       if (getuser(&CC->usersupp,CC->curr_user)) {
-               cprintf("%d Can't find user record!\n",ERROR+INTERNAL_ERROR);
-               return;
+       if (getuser(&CC->usersupp, CC->curr_user)) {
+               return pass_internal_error;
                }
 
        code = (-1);
@@ -445,14 +474,44 @@ void cmd_pass(char *buf)
        if (!code) {
                (CC->logged_in) = 1;
                session_startup();
+               return pass_ok;
                }
        else {
-               cprintf("%d Wrong password.\n",ERROR);
                rec_log(CL_BADPW,CC->curr_user);
+               return pass_wrong_password;
                }
        }
 
 
+void cmd_pass(char *buf)
+{
+       char password[256];
+       int a;
+
+       extract(password, buf, 0);
+       a = CtdlTryPassword(password);
+
+       switch (a) {
+               case pass_already_logged_in:
+                       cprintf("%d Already logged in.\n",ERROR);
+                       return;
+               case pass_no_user:
+                       cprintf("%d You must send a name with USER first.\n",
+                               ERROR);
+                       return;
+               case pass_wrong_password:
+                       cprintf("%d Wrong password.\n", ERROR);
+                       return;
+               case pass_ok:
+                       logged_in_response();
+                       return;
+               cprintf("%d Can't find user record!\n",
+                       ERROR+INTERNAL_ERROR);
+       }
+}
+
+
+
 /*
  * Delete a user record *and* all of its related resources.
  */
@@ -635,6 +694,7 @@ void cmd_newu(char *cmdbuf)
                }
        else if (a==0) {
                session_startup();
+               logged_in_response();
                }
        else {
                cprintf("%d unknown error\n",ERROR);
@@ -1147,6 +1207,7 @@ void cmd_asup(char *cmdbuf) {
        char requested_user[256];
        int np;
        int newax;
+       int deleted = 0;
        
        if ( (CC->internal_pgm==0)
           && ( (CC->logged_in == 0) || (is_aide()==0) ) ) {
@@ -1182,10 +1243,12 @@ void cmd_asup(char *cmdbuf) {
        lputuser(&usbuf);
        if (usbuf.axlevel == 0) {
                if (purge_user(requested_user)==0) {
-                       cprintf("%d %s deleted.\n", OK, requested_user);
+                       deleted = 1;
                        }
                }
-       cprintf("%d Ok\n", OK);
+       cprintf("%d Ok", OK);
+       if (deleted) cprintf(" (%s deleted)", requested_user);
+       cprintf("\n");
        }