* Unfinished code is now disabled.
[citadel.git] / citadel / user_ops.c
index f457c60894f7795e1a40fef116a81ce957dd5e98..10fcb3f47d1fc605cf9e07674734e8354679bc52 100644 (file)
@@ -33,6 +33,7 @@
 
 #include <string.h>
 #include <limits.h>
+#include <libcitadel.h>
 #include "auth.h"
 #include "citadel.h"
 #include "server.h"
 #include "control.h"
 #include "msgbase.h"
 #include "config.h"
-#include "tools.h"
 #include "citserver.h"
 #include "citadel_dirs.h"
 #include "genstamp.h"
+#include "threads.h"
 
 /* These pipes are used to talk to the chkpwd daemon, which is forked during startup */
 int chkpwd_write_pipe[2];
@@ -358,6 +359,8 @@ int CtdlLoginExistingUser(char *authname, char *trythisname)
        char username[SIZ];
        int found_user;
 
+       lprintf(9, "CtdlLoginExistingUser(%s, %s)\n", authname, trythisname);
+
        if ((CC->logged_in)) {
                return login_already_logged_in;
        }
@@ -380,7 +383,7 @@ int CtdlLoginExistingUser(char *authname, char *trythisname)
                return login_not_found;
        }
 
-       if (config.c_auth_mode == 1) {
+       if (config.c_auth_mode == AUTHMODE_HOST) {
 
                /* host auth mode */
 
@@ -389,20 +392,29 @@ int CtdlLoginExistingUser(char *authname, char *trythisname)
                char pwdbuffer[256];
        
                lprintf(CTDL_DEBUG, "asking host about <%s>\n", username);
+#ifdef HAVE_GETPWNAM_R
 #ifdef SOLARIS_GETPWUID
+               lprintf(CTDL_DEBUG, "Calling getpwnam_r()\n");
                tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
-#else
+#else // SOLARIS_GETPWUID
+               lprintf(CTDL_DEBUG, "Calling getpwnam_r()\n");
                getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
-#endif
+#endif // SOLARIS_GETPWUID
+#else // HAVE_GETPWNAM_R
+               lprintf(CTDL_DEBUG, "SHOULD NEVER GET HERE!!!\n");
+               tempPwdPtr = NULL;
+#endif // HAVE_GETPWNAM_R
                if (tempPwdPtr == NULL) {
+                       lprintf(CTDL_DEBUG, "no such user <%s>\n", username);
                        return login_not_found;
                }
-               lprintf(CTDL_DEBUG, "found it! uid=%ld, gecos=%s\n", (long)pd.pw_uid, pd.pw_gecos);
        
                /* Locate the associated Citadel account.
                 * If not found, make one attempt to create it.
                 */
                found_user = getuserbyuid(&CC->user, pd.pw_uid);
+               lprintf(CTDL_DEBUG, "found it: uid=%ld, gecos=%s here: %d\n",
+                       (long)pd.pw_uid, pd.pw_gecos, found_user);
                if (found_user != 0) {
                        create_user(username, 0);
                        found_user = getuserbyuid(&CC->user, pd.pw_uid);
@@ -422,7 +434,7 @@ int CtdlLoginExistingUser(char *authname, char *trythisname)
                * is an e-mail address
                */
                if (found_user != 0) {
-                       valid = validate_recipients(username);
+                       valid = validate_recipients(username, NULL, 0);
                        if (valid != NULL) {
                                if (valid->num_local == 1) {
                                        found_user = getuser(&CC->user, valid->recp_local);
@@ -508,7 +520,7 @@ void session_startup(void)
        /* If we're authenticating off the host system, automatically give
         * root the highest level of access.
         */
-       if (config.c_auth_mode == 1) {
+       if (config.c_auth_mode == AUTHMODE_HOST) {
                if (CC->user.uid == 0) {
                        CC->user.axlevel = 6;
                }
@@ -595,6 +607,10 @@ void logout(struct CitContext *who)
 
        /* Do modular stuff... */
        PerformSessionHooks(EVT_LOGOUT);
+       
+       /* Check to see if the user was deleted whilst logged in and purge them if necessary */
+       if (who->user.axlevel == 0)
+               purge_user(who->user.fullname);
 
        /* Free any output buffers */
        if (who->output_buffer != NULL) {
@@ -704,7 +720,7 @@ int CtdlTryPassword(char *password)
                code = strcmp(password, config.c_master_pass);
        }
 
-       else if (config.c_auth_mode == 1) {
+       else if (config.c_auth_mode == AUTHMODE_HOST) {
 
                /* host auth mode */
 
@@ -795,6 +811,10 @@ int purge_user(char pname[])
 
        makeuserkey(usernamekey, pname);
 
+       /* If the name is empty we can't find them in the DB any way so just return */
+       if (IsEmptyStr(pname))
+               return (ERROR + NO_SUCH_USER);
+
        if (getuser(&usbuf, pname) != 0) {
                lprintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
                return (ERROR + NO_SUCH_USER);
@@ -867,7 +887,7 @@ int create_user(char *newusername, int become_user)
        safestrncpy(username, newusername, sizeof username);
        strproc(username);
 
-       if (config.c_auth_mode == 1) {
+       if (config.c_auth_mode == AUTHMODE_HOST) {
 
                /* host auth mode */
 
@@ -875,14 +895,29 @@ int create_user(char *newusername, int become_user)
                struct passwd *tempPwdPtr;
                char pwdbuffer[256];
        
+#ifdef HAVE_GETPWNAM_R
 #ifdef SOLARIS_GETPWUID
                tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof(pwdbuffer));
-#else
+#else // SOLARIS_GETPWUID
                getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
-#endif
+#endif // SOLARIS_GETPWUID
+#else // HAVE_GETPWNAM_R
+               tempPwdPtr = NULL;
+#endif // HAVE_GETPWNAM_R
                if (tempPwdPtr != NULL) {
                        extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
                        uid = pd.pw_uid;
+                       if (IsEmptyStr (username))
+                       {
+                               lprintf (CTDL_EMERG, 
+                                        "Can't find Realname for user %s [%d] in the Host Auth Database; giving up.\n", 
+                                        newusername, pd.pw_uid);
+                               snprintf(buf, SIZ, 
+                                        "Can't find Realname for user %s [%d] in the Host Auth Database; giving up.\n",
+                                        newusername, pd.pw_uid);
+                               aide_message(buf, "User Creation Failure Notice");
+
+                       }
                }
                else {
                        return (ERROR + NO_SUCH_USER);
@@ -975,7 +1010,7 @@ void cmd_newu(char *cmdbuf)
        int a;
        char username[26];
 
-       if (config.c_auth_mode == 1) {
+       if (config.c_auth_mode != AUTHMODE_NATIVE) {
                cprintf("%d This system does not use native mode authentication.\n",
                        ERROR + NOT_HERE);
                return;
@@ -1102,11 +1137,14 @@ void cmd_creu(char *cmdbuf)
                                "with no password");
                return;
        } else if (a == ERROR + ALREADY_EXISTS) {
-               cprintf("%d '%s' already exists.\n",
-                       ERROR + ALREADY_EXISTS, username);
+               cprintf("%d '%s' already exists.\n", ERROR + ALREADY_EXISTS, username);
+               return;
+       } else if ( (config.c_auth_mode != AUTHMODE_NATIVE) && (a == ERROR + NO_SUCH_USER) ) {
+               cprintf("%d User accounts are not created within Citadel in host authentication mode.\n",
+                       ERROR + NO_SUCH_USER);
                return;
        } else {
-               cprintf("%d An error occured creating the user account.\n", ERROR + INTERNAL_ERROR);
+               cprintf("%d An error occurred creating the user account.\n", ERROR + INTERNAL_ERROR);
        }
 }
 
@@ -1630,8 +1668,9 @@ void cmd_asup(char *cmdbuf)
        }
 
        if (deleted) {
-               sprintf(notify, "User \"%s\" has been deleted by %s.\n",
-                       usbuf.fullname, CC->user.fullname);
+               snprintf(notify, SIZ, 
+                        "User \"%s\" has been deleted by %s.\n",
+                        usbuf.fullname, CC->user.fullname);
                aide_message(notify, "User Deletion Message");
        }