Fixed a bug in username key usage that was causing email aliases to disappear
[citadel.git] / citadel / modules / ctdlproto / serv_user.c
index 10921b48dc41723513c60145c288ea4159f83c77..437f59633514c36f80e206537745fdb66a132726 100644 (file)
@@ -1,7 +1,7 @@
 /* 
  * Server functions which perform operations on user objects.
  *
- * Copyright (c) 1987-2017 by the citadel.org team
+ * Copyright (c) 1987-2020 by the citadel.org team
  *
  * This program is open source software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License, version 3.
@@ -34,7 +34,7 @@ void cmd_user(char *cmdbuf)
        striplt(username);
        syslog(LOG_DEBUG, "user_ops: cmd_user(%s)", username);
 
-       a = CtdlLoginExistingUser(NULL, username);
+       a = CtdlLoginExistingUser(username);
        switch (a) {
        case login_already_logged_in:
                cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
@@ -93,7 +93,6 @@ void cmd_pass(char *buf)
 void cmd_newu(char *cmdbuf)
 {
        int a;
-       long len;
        char username[SIZ];
 
        if (CtdlGetConfigInt("c_auth_mode") != AUTHMODE_NATIVE) {
@@ -120,7 +119,6 @@ void cmd_newu(char *cmdbuf)
        }
        extract_token(username, cmdbuf, 0, '|', sizeof username);
        strproc(username);
-       len = cutuserkey(username);
 
        if (IsEmptyStr(username)) {
                cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
@@ -134,7 +132,7 @@ void cmd_newu(char *cmdbuf)
                return;
        }
 
-       a = create_user(username, len, 1);
+       a = create_user(username, CREATE_USER_BECOME_USER, NATIVE_AUTH_UID);
 
        if (a == 0) {
                logged_in_response();
@@ -164,11 +162,6 @@ void cmd_setp(char *new_pw)
                cprintf("%d Not allowed.  Use the 'passwd' command.\n", ERROR + NOT_HERE);
                return;
        }
-       if (CC->is_master) {
-               cprintf("%d The master prefix password cannot be changed with this command.\n",
-                       ERROR + NOT_HERE);
-               return;
-       }
 
        if (!strcasecmp(new_pw, "GENERATE_RANDOM_PASSWORD")) {
                char random_password[17];
@@ -194,7 +187,6 @@ void cmd_setp(char *new_pw)
 void cmd_creu(char *cmdbuf)
 {
        int a;
-       long len;
        char username[SIZ];
        char password[SIZ];
        struct ctdluser tmp;
@@ -210,12 +202,10 @@ void cmd_creu(char *cmdbuf)
                cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
                return;
        }
-       len = cutuserkey(username);
-
 
        extract_token(password, cmdbuf, 1, '|', sizeof password);
 
-       a = create_user(username, len, 0);
+       a = create_user(username, CREATE_USER_DO_NOT_BECOME_USER, NATIVE_AUTH_UID);
 
        if (a == 0) {
                if (!IsEmptyStr(password)) {
@@ -494,6 +484,40 @@ void cmd_vali(char *v_args)
        cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
 }
 
+
+/*
+ * List one user (this works with cmd_list)
+ */
+void ListThisUser(char *username, void *data)
+{
+       char *searchstring;
+       struct ctdluser usbuf;
+
+       if (CtdlGetUser(&usbuf, username) != 0) {
+               return;
+       }
+
+       searchstring = (char *)data;
+       if (bmstrcasestr(usbuf.fullname, searchstring) == NULL) {
+               return;
+       }
+
+       if (usbuf.axlevel > AxDeleted) {
+               if ((CC->user.axlevel >= AxAideU)
+                   || ((usbuf.flags & US_UNLISTED) == 0)
+                   || ((CC->internal_pgm))) {
+                       cprintf("%s|%d|%ld|%ld|%ld|%ld||\n",
+                               usbuf.fullname,
+                               usbuf.axlevel,
+                               usbuf.usernum,
+                               (long)usbuf.lastcall,
+                               usbuf.timescalled,
+                               usbuf.posted);
+               }
+       }
+}
+
+
 /* 
  *  List users (searchstring may be empty to list all users)
  */
@@ -667,6 +691,81 @@ void cmd_isme(char *argbuf) {
 }
 
 
+/*
+ * Administrative Get Email Addresses
+ */
+void cmd_agea(char *cmdbuf)
+{
+       struct ctdluser usbuf;
+       char requested_user[128];
+       int i, num_e;
+       char e[512];
+
+       if (CtdlAccessCheck(ac_aide)) {
+               return;
+       }
+
+       extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
+       if (CtdlGetUser(&usbuf, requested_user) != 0) {
+               cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
+               return;
+       }
+       cprintf("%d internet email addresses for %s\n", LISTING_FOLLOWS, usbuf.fullname);
+       num_e = num_tokens(usbuf.emailaddrs, '|');
+       for (i=0; i<num_e; ++i) {
+               extract_token(e, usbuf.emailaddrs, i, '|', sizeof e);
+               cprintf("%s\n", e);
+       }
+       cprintf("000\n");
+}
+
+
+/*
+ * Administrative Set Email Addresses
+ */
+void cmd_asea(char *cmdbuf)
+{
+       struct ctdluser usbuf;
+       char requested_user[128];
+       char buf[SIZ];
+       char whodat[64];
+       char new_emailaddrs[512] = { 0 } ;
+
+       if (CtdlAccessCheck(ac_aide)) return;
+
+       extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
+       if (CtdlGetUser(&usbuf, requested_user) != 0) {
+               cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
+               return;
+       }
+
+       cprintf("%d Ok\n", SEND_LISTING);
+       while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
+               if (IsEmptyStr(buf)) {
+                       syslog(LOG_ERR, "user_ops: address <%s> is empty - not using", buf);
+               }
+               else if ((strlen(new_emailaddrs) + strlen(buf) + 2) > sizeof(new_emailaddrs)) {
+                       syslog(LOG_ERR, "user_ops: address <%s> does not fit in buffer - not using", buf);
+               }
+               else if (!IsDirectory(buf, 0)) {
+                       syslog(LOG_ERR, "user_ops: address <%s> is not in one of our domains - not using", buf);
+               }
+               else if ( (CtdlDirectoryLookup(whodat, buf, sizeof whodat) == 0) && (CtdlUserCmp(whodat, requested_user)) ) {
+                       syslog(LOG_ERR, "user_ops: address <%s> already belongs to <%s> - not using", buf, whodat);
+               }
+               else {
+                       syslog(LOG_DEBUG, "user_ops: address <%s> validated", buf);
+                       if (!IsEmptyStr(new_emailaddrs)) {
+                               strcat(new_emailaddrs, "|");
+                       }
+                       strcat(new_emailaddrs, buf);
+               }
+       }
+
+       CtdlSetEmailAddressesForUser(requested_user, new_emailaddrs);
+}
+
+
 /*
  * Set the preferred view for the current user/room combination
  */
@@ -768,6 +867,8 @@ CTDL_MODULE_INIT(serv_user)
                CtdlRegisterProtoHook(cmd_qusr, "QUSR", "check to see if a user exists");
                CtdlRegisterProtoHook(cmd_agup, "AGUP", "Administratively Get User Parameters");
                CtdlRegisterProtoHook(cmd_asup, "ASUP", "Administratively Set User Parameters");
+               CtdlRegisterProtoHook(cmd_agea, "AGEA", "Administratively Get Email Addresses");
+               CtdlRegisterProtoHook(cmd_asea, "ASEA", "Administratively Set Email Addresses");
                CtdlRegisterProtoHook(cmd_seen, "SEEN", "Manipulate seen/unread message flags");
                CtdlRegisterProtoHook(cmd_gtsn, "GTSN", "Fetch seen/unread message flags");
                CtdlRegisterProtoHook(cmd_view, "VIEW", "Set preferred view for user/room combination");