libical, expat, and libsieve are now *required*.
[citadel.git] / citadel / user_ops.c
index 4c29bd692bd81f725591663e476b11ce6b3108c5..8b2b9d19a0980beb8dabdabd548ed4a696ceaabd 100644 (file)
 
 #include <string.h>
 #include <limits.h>
+#include <libcitadel.h>
 #include "auth.h"
 #include "citadel.h"
 #include "server.h"
 #include "database.h"
 #include "user_ops.h"
-#include "serv_extensions.h"
 #include "sysdep_decls.h"
 #include "support.h"
 #include "room_ops.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];
@@ -63,6 +63,13 @@ static INLINE void makeuserkey(char *key, char *username) {
        int i, len;
 
        len = strlen(username);
+       if (len >= USERNAME_SIZE)
+       {
+               lprintf (CTDL_EMERG, "Username to long: %s", username);
+               cit_backtrace ();
+               len = USERNAME_SIZE - 1; 
+               username[USERNAME_SIZE - 1]='\0';
+       }
        for (i=0; i<=len; ++i) {
                key[i] = tolower(username[i]);
        }
@@ -141,6 +148,61 @@ void lputuser(struct ctdluser *usbuf)
        end_critical_section(S_USERS);
 }
 
+
+/*
+ * rename_user()  -  this is tricky because the user's display name is the database key
+ *
+ * Returns 0 on success or nonzero if there was an error...
+ *
+ */
+int rename_user(char *oldname, char *newname) {
+       struct CitContext *cptr;
+       int retcode = RENAMEUSER_OK;
+       struct ctdluser usbuf;
+
+       char oldnamekey[USERNAME_SIZE];
+       char newnamekey[USERNAME_SIZE];
+
+       /* We cannot rename a user who is currently logged in */
+       for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
+               if (!strcasecmp(cptr->user.fullname, oldname)) {
+                       return(RENAMEUSER_LOGGED_IN);
+               }
+       }
+
+       /* Create the database keys... */
+       makeuserkey(oldnamekey, oldname);
+       makeuserkey(newnamekey, newname);
+
+       /* Lock up and get going */
+       begin_critical_section(S_USERS);
+
+       if (getuser(&usbuf, newname) == 0) {
+               retcode = RENAMEUSER_ALREADY_EXISTS;
+       }
+       else {
+
+               if (getuser(&usbuf, oldname) != 0) {
+                       retcode = RENAMEUSER_NOT_FOUND;
+               }
+
+               else {          /* Sanity checks succeeded.  Now rename the user. */
+
+                       lprintf(CTDL_DEBUG, "Renaming <%s> to <%s>\n", oldname, newname);
+                       cdb_delete(CDB_USERS, oldnamekey, strlen(oldnamekey));
+                       safestrncpy(usbuf.fullname, newname, sizeof usbuf.fullname);
+                       putuser(&usbuf);
+                       retcode = RENAMEUSER_OK;
+               }
+       
+       }
+
+       end_critical_section(S_USERS);
+       return(retcode);
+}
+
+
+
 /*
  * Index-generating function used by Ctdl[Get|Set]Relationship
  */
@@ -174,6 +236,7 @@ void put_visit(struct visit *newvisit)
        char IndexBuf[32];
        int IndexLen = 0;
 
+       memset (IndexBuf, 0, sizeof (IndexBuf));
        /* Generate an index */
        IndexLen = GenerateRelationshipIndex(IndexBuf,
                                             newvisit->v_roomnum,
@@ -341,9 +404,6 @@ int getuserbyuid(struct ctdluser *usbuf, uid_t number)
        return (-1);
 }
 
-#define MASTER_USER            "master"
-#define MASTER_PASSWORD                "d0nuts"
-
 /*
  * Back end for cmd_user() and its ilk
  *
@@ -354,30 +414,31 @@ 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;
        }
 
        if (trythisname == NULL) return login_not_found;
 
+       /* If a "master user" is defined, handle its authentication if specified */
        CC->is_master = 0;
-#ifdef MASTER_USER_HACK
-       /* This lives inside an ifdef for now, because it isn't yet secure enough for general deployment */
-       if (authname) {
-               if (!strcasecmp(authname, MASTER_USER)) {
+       if (strlen(config.c_master_user) > 0) if (strlen(config.c_master_pass) > 0) if (authname) {
+               if (!strcasecmp(authname, config.c_master_user)) {
                        CC->is_master = 1;
                }
        }
-#endif
 
+       /* Continue attempting user validation... */
        safestrncpy(username, trythisname, USERNAME_SIZE);
        striplt(username);
 
-       if (strlen(username) == 0) {
+       if (IsEmptyStr(username)) {
                return login_not_found;
        }
 
-       if (config.c_auth_mode == 1) {
+       if (config.c_auth_mode == AUTHMODE_HOST) {
 
                /* host auth mode */
 
@@ -386,20 +447,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);
@@ -419,7 +489,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);
@@ -486,8 +556,6 @@ void cmd_user(char *cmdbuf)
  */
 void session_startup(void)
 {
-       int i = 0;
-
        lprintf(CTDL_NOTICE, "<%s> logged in\n", CC->curr_user);
 
        lgetuser(&CC->user, CC->curr_user);
@@ -505,7 +573,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;
                }
@@ -520,11 +588,7 @@ void session_startup(void)
         */
        snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
                CC->user.fullname, config.c_fqdn);
-       for (i=0; i<strlen(CC->cs_inet_email); ++i) {
-               if (isspace(CC->cs_inet_email[i])) {
-                       CC->cs_inet_email[i] = '_';
-               }
-       }
+       convert_spaces_to_underscores(CC->cs_inet_email);
 
        /* Create any personal rooms required by the system.
         * (Technically, MAILROOM should be there already, but just in case...)
@@ -586,12 +650,16 @@ void logout(struct CitContext *who)
        /*
         * If we were talking to a network node, we're not anymore...
         */
-       if (strlen(who->net_node) > 0) {
+       if (!IsEmptyStr(who->net_node)) {
                network_talking_to(who->net_node, NTT_REMOVE);
        }
 
        /* 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) {
@@ -606,6 +674,11 @@ static int validpw(uid_t uid, const char *pass)
 {
        char buf[256];
 
+       if (IsEmptyStr(pass)) {
+               lprintf(CTDL_DEBUG, "refusing to check empty password for uid=%d using chkpwd...\n", uid);
+               return 0;
+       }
+
        lprintf(CTDL_DEBUG, "Validating password for uid=%d using chkpwd...\n", uid);
 
        begin_critical_section(S_CHKPWD);
@@ -698,10 +771,10 @@ int CtdlTryPassword(char *password)
        code = (-1);
 
        if (CC->is_master) {
-               code = strcmp(password, MASTER_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 */
 
@@ -792,6 +865,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);
@@ -864,7 +941,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 */
 
@@ -872,14 +949,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);
@@ -972,7 +1064,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;
@@ -997,7 +1089,7 @@ void cmd_newu(char *cmdbuf)
        username[25] = 0;
        strproc(username);
 
-       if (strlen(username) == 0) {
+       if (IsEmptyStr(username)) {
                cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
                return;
        }
@@ -1047,7 +1139,7 @@ void cmd_setp(char *new_pw)
                return;
        }
        strproc(new_pw);
-       if (strlen(new_pw) == 0) {
+       if (IsEmptyStr(new_pw)) {
                cprintf("%d Password unchanged.\n", CIT_OK);
                return;
        }
@@ -1081,7 +1173,7 @@ void cmd_creu(char *cmdbuf)
        strproc(username);
        strproc(password);
 
-       if (strlen(username) == 0) {
+       if (IsEmptyStr(username)) {
                cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
                return;
        }
@@ -1089,21 +1181,24 @@ void cmd_creu(char *cmdbuf)
        a = create_user(username, 0);
 
        if (a == 0) {
-               if (strlen(password) > 0) {
+               if (!IsEmptyStr(password)) {
                        lgetuser(&tmp, username);
                        safestrncpy(tmp.password, password, sizeof(tmp.password));
                        lputuser(&tmp);
                }
                cprintf("%d User '%s' created %s.\n", CIT_OK, username,
-                               (strlen(password) > 0) ? "and password set" :
+                               (!IsEmptyStr(password)) ? "and password set" :
                                "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);
        }
 }
 
@@ -1627,8 +1722,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");
        }
 
@@ -1736,3 +1832,40 @@ void cmd_view(char *cmdbuf) {
        
        cprintf("%d ok\n", CIT_OK);
 }
+
+
+/*
+ * Rename a user
+ */
+void cmd_renu(char *cmdbuf)
+{
+       int retcode;
+       char oldname[USERNAME_SIZE];
+       char newname[USERNAME_SIZE];
+
+       if (CtdlAccessCheck(ac_aide)) {
+               return;
+       }
+
+       extract_token(oldname, cmdbuf, 0, '|', sizeof oldname);
+       extract_token(newname, cmdbuf, 1, '|', sizeof newname);
+
+       retcode = rename_user(oldname, newname);
+       switch(retcode) {
+               case RENAMEUSER_OK:
+                       cprintf("%d '%s' has been renamed to '%s'.\n", CIT_OK, oldname, newname);
+                       return;
+               case RENAMEUSER_LOGGED_IN:
+                       cprintf("%d '%s' is currently logged in and cannot be renamed.\n",
+                               ERROR + ALREADY_LOGGED_IN , oldname);
+                       return;
+               case RENAMEUSER_NOT_FOUND:
+                       cprintf("%d '%s' does not exist.\n", ERROR + NO_SUCH_USER, oldname);
+                       return;
+               case RENAMEUSER_ALREADY_EXISTS:
+                       cprintf("%d A user named '%s' already exists.\n", ERROR + ALREADY_EXISTS, newname);
+                       return;
+       }
+
+       cprintf("%d An unknown error occurred.\n", ERROR);
+}