* added message subject to all those tiny messages
[citadel.git] / citadel / user_ops.c
index b506bd9ff39995709f7eaf91014383235381dc25..ff56484ca016121f1f2fc7bab6be967f0fd3d679 100644 (file)
@@ -5,10 +5,6 @@
  *
  */
 
-#ifdef DLL_EXPORT
-#define IN_LIBCIT
-#endif
-
 #include "sysdep.h"
 #include <errno.h>
 #include <stdlib.h>
@@ -75,28 +71,15 @@ int getuser(struct ctdluser *usbuf, char name[])
 {
 
        char usernamekey[USERNAME_SIZE];
-       char sysuser_name[USERNAME_SIZE];
        struct cdbdata *cdbus;
-       int using_sysuser = 0;
 
        if (usbuf != NULL) {
                memset(usbuf, 0, sizeof(struct ctdluser));
        }
 
-#ifdef ENABLE_AUTOLOGIN
-       if (CtdlAssociateSystemUser(sysuser_name, name) == 0) {
-               ++using_sysuser;
-       }
-#endif
-
-       if (using_sysuser) {
-               makeuserkey(usernamekey, sysuser_name);
-       }
-       else {
-               makeuserkey(usernamekey, name);
-       }
-
+       makeuserkey(usernamekey, name);
        cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
+
        if (cdbus == NULL) {    /* user not found */
                return(1);
        }
@@ -325,26 +308,34 @@ int getuserbynumber(struct ctdluser *usbuf, long int number)
 }
 
 
+#ifdef ENABLE_AUTOLOGIN
 /*
- * See if we can translate a system login name (i.e. from /etc/passwd)
- * to a Citadel screen name.  Returns 0 if one is found.
+ * getuserbyuid()  -     get user by system uid (for PAM mode authentication)
+ *                       returns 0 if user was found
+ *
+ * WARNING: don't use this function unless you absolutely have to.  It does
+ *          a sequential search and therefore is computationally expensive.
  */
-int CtdlAssociateSystemUser(char *screenname, char *loginname) {
-       struct passwd *p;
-       int a;
+int getuserbyuid(struct ctdluser *usbuf, uid_t number)
+{
+       struct cdbdata *cdbus;
 
-       p = (struct passwd *) getpwnam(loginname);
-       if (p != NULL) {
-               strcpy(screenname, p->pw_gecos);
-               for (a = 0; a < strlen(screenname); ++a) {
-                       if (screenname[a] == ',') {
-                               screenname[a] = 0;
-                       }
+       cdb_rewind(CDB_USERS);
+
+       while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
+               memset(usbuf, 0, sizeof(struct ctdluser));
+               memcpy(usbuf, cdbus->ptr,
+                      ((cdbus->len > sizeof(struct ctdluser)) ?
+                       sizeof(struct ctdluser) : cdbus->len));
+               cdb_free(cdbus);
+               if (usbuf->uid == number) {
+                       cdb_close_cursor(CDB_USERS);
+                       return (0);
                }
-               return(0);
        }
-       return(1);
+       return (-1);
 }
+#endif /* ENABLE_AUTOLOGIN */
 
 
 
@@ -355,16 +346,47 @@ int CtdlLoginExistingUser(char *trythisname)
 {
        char username[SIZ];
        int found_user;
-       struct recptypes *valid = NULL;
+
+       if ((CC->logged_in)) {
+               return login_already_logged_in;
+       }
 
        if (trythisname == NULL) return login_not_found;
        safestrncpy(username, trythisname, USERNAME_SIZE);
        striplt(username);
 
-       if ((CC->logged_in)) {
-               return login_already_logged_in;
+       if (strlen(username) == 0) {
+               return login_not_found;
        }
 
+#ifdef ENABLE_AUTOLOGIN
+
+       /* If this is an autologin build, the only valid auth source is the
+        * host operating system.
+        */
+       struct passwd pd;
+       struct passwd *tempPwdPtr;
+       char pwdbuffer[256];
+
+       lprintf(CTDL_DEBUG, "asking host about <%s>\n", username);
+       getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
+       if (tempPwdPtr == NULL) {
+               return login_not_found;
+       }
+       lprintf(CTDL_DEBUG, "found it! uid=%d, gecos=%s\n", 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);
+       if (found_user != 0) {
+               create_user(username, 0);
+               found_user = getuserbyuid(&CC->user, pd.pw_uid);
+       }
+
+#else /* ENABLE_AUTOLOGIN */
+       struct recptypes *valid = NULL;
+
        /* First, try to log in as if the supplied name is a display name */
        found_user = getuser(&CC->user, username);
 
@@ -382,19 +404,6 @@ int CtdlLoginExistingUser(char *trythisname)
                }
        }
 
-#ifdef ENABLE_AUTOLOGIN
-       /* If we haven't found the account yet, and the supplied name
-        * is a login name on the underlying host system, create the
-        * account.
-        */
-       if (found_user != 0) {
-               struct passwd *p = (struct passwd *) getpwnam(username);
-
-               if (p != NULL) {
-                       create_user(username, 0);
-                       found_user = getuser(&CC->user, username);
-               }
-       }
 #endif /* ENABLE_AUTOLOGIN */
 
        /* Did we find something? */
@@ -469,6 +478,16 @@ void session_startup(void)
        if (!strcasecmp(CC->user.fullname, config.c_sysadm)) {
                CC->user.axlevel = 6;
        }
+
+#ifdef ENABLE_AUTOLOGIN
+       /* If we're authenticating off the host system, automatically give
+        * root the highest level of access.
+        */
+       if (CC->user.uid == 0) {
+               CC->user.axlevel = 6;
+       }
+#endif
+
        lputuser(&CC->user);
 
        /*
@@ -489,6 +508,7 @@ void session_startup(void)
         */
        create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
        create_room(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
+       create_room(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
 
        /* Run any startup routines registered by loadable modules */
        PerformSessionHooks(EVT_LOGIN);
@@ -646,26 +666,24 @@ int CtdlTryPassword(char *password)
 
 
 #ifdef ENABLE_AUTOLOGIN
-       /* A uid of CTDLUID or -1 indicates that this user exists only in
-        * Citadel, not in the underlying operating system.
-        */
-       if ( (CC->user.uid == CTDLUID) || (CC->user.uid == (-1)) ) {
-               strproc(password);
-               strproc(CC->user.password);
-               code = strcasecmp(CC->user.password, password);
+
+       if (validpw(CC->user.uid, password)) {
+               code = 0;
+               /* we could get rid of this */
+               lgetuser(&CC->user, CC->curr_user);
+               safestrncpy(CC->user.password, password, sizeof CC->user.password);
+               lputuser(&CC->user);
+               /*                          */
        }
-       /* Any other uid means we have to check the system password database */
        else {
-               if (validpw(CC->user.uid, password)) {
-                       code = 0;
-                       lgetuser(&CC->user, CC->curr_user);
-                       safestrncpy(CC->user.password, password,
-                                   sizeof CC->user.password);
-                       lputuser(&CC->user);
-               }
+               code = (-1);
        }
 
 #else /* ENABLE_AUTOLOGIN */
+
+       strproc(password);
+       strproc(CC->user.password);
+       code = strcasecmp(CC->user.password, password);
        strproc(password);
        strproc(CC->user.password);
        code = strcasecmp(CC->user.password, password);
@@ -756,23 +774,19 @@ int purge_user(char pname[])
        cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
 
        /* remove the user's bio file */
-       snprintf(filename, sizeof filename, 
-#ifndef HAVE_DATA_DIR
-                        "."
-#else
-                        DATA_DIR
-#endif
-                        "/bio/%ld", usbuf.usernum);
+       snprintf(filename, 
+                        sizeof filename, 
+                        "%s/%ld",
+                        ctdl_bio_dir,
+                        usbuf.usernum);
        unlink(filename);
 
        /* remove the user's picture */
-       snprintf(filename, sizeof filename, 
-#ifndef HAVE_DATA_DIR
-                        "."
-#else
-                        DATA_DIR
-#endif
-                        "/userpics/%ld.gif", usbuf.usernum);
+       snprintf(filename, 
+                        sizeof filename, 
+                        "%s/%ld.gif",
+                        ctdl_image_dir,
+                        usbuf.usernum);
        unlink(filename);
 
        return (0);
@@ -792,24 +806,24 @@ int create_user(char *newusername, int become_user)
        struct ctdlroom qrbuf;
        char username[256];
        char mailboxname[ROOMNAMELEN];
-       uid_t uid;
+       uid_t uid = (-1);
 
        safestrncpy(username, newusername, sizeof username);
        strproc(username);
 
 #ifdef ENABLE_AUTOLOGIN
-       {
-               struct passwd *p = (struct passwd *) getpwnam(username);
+       struct passwd pd;
+       struct passwd *tempPwdPtr;
+       char pwdbuffer[256];
 
-               if (p != NULL) {
-                       extract_token(username, p->pw_gecos, 0, ',', sizeof username);
-                       uid = p->pw_uid;
-               } else {
-                       uid = (-1);
-               }
+       getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
+       if (tempPwdPtr != NULL) {
+               extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
+               uid = pd.pw_uid;
+       }
+       else {
+               return (ERROR + NO_SUCH_USER);
        }
-#else
-       uid = (-1);
 #endif
 
        if (!getuser(&usbuf, username)) {
@@ -891,6 +905,12 @@ void cmd_newu(char *cmdbuf)
        int a;
        char username[26];
 
+#ifdef ENABLE_AUTOLOGIN
+       cprintf("%d This system does not use native mode authentication.\n",
+               ERROR + NOT_HERE);
+       return;
+#endif /* ENABLE_AUTOLOGIN */
+
        if (config.c_disable_newu) {
                cprintf("%d Self-service user account creation "
                        "is disabled on this system.\n", ERROR + NOT_HERE);
@@ -1111,7 +1131,8 @@ void cmd_seen(char *argbuf) {
        target_msgnum = extract_long(argbuf, 0);
        target_setting = extract_int(argbuf, 1);
 
-       CtdlSetSeen(target_msgnum, target_setting, ctdlsetseen_seen, NULL, NULL);
+       CtdlSetSeen(&target_msgnum, 1, target_setting,
+                       ctdlsetseen_seen, NULL, NULL);
        cprintf("%d OK\n", CIT_OK);
 }
 
@@ -1155,12 +1176,12 @@ int CtdlInvtKick(char *iuser, int op) {
        CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
 
        /* post a message in Aide> saying what we just did */
-       snprintf(bbb, sizeof bbb, "%s %s %s> by %s\n",
+       snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
                iuser,
                ((op == 1) ? "invited to" : "kicked out of"),
                CC->room.QRname,
                CC->user.fullname);
-       aide_message(bbb);
+       aide_message(bbb,"User Admin Message");
 
        return(0);
 }
@@ -1370,7 +1391,7 @@ void ListThisUser(struct ctdluser *usbuf, void *data)
        char *searchstring;
 
        searchstring = (char *)data;
-       if (bmstrstr(usbuf->fullname, searchstring, strncasecmp) == NULL) {
+       if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
                return;
        }
 
@@ -1534,9 +1555,9 @@ void cmd_asup(char *cmdbuf)
        }
 
        if (deleted) {
-               sprintf(notify, "User <%s> deleted by %s\n",
+               sprintf(notify, "User \"%s\" has been deleted by %s.\n",
                        usbuf.fullname, CC->user.fullname);
-               aide_message(notify);
+               aide_message(notify, "User Deletion Message");
        }
 
        cprintf("%d Ok", CIT_OK);