started working on code for extracting email addresses from ldap
[citadel.git] / citadel / user_ops.c
index 5c3379b13c9a5b91c000a9c1fee30f73335d3cda..87dfa5a2515eea16776664d26640f1f8094526cb 100644 (file)
@@ -38,7 +38,6 @@ int chkpwd_read_pipe[2];
  */
 int CtdlGetUserLen(struct ctdluser *usbuf, const char *name, long len)
 {
-
        char usernamekey[USERNAME_SIZE];
        struct cdbdata *cdbus;
 
@@ -68,13 +67,14 @@ int CtdlGetUser(struct ctdluser *usbuf, char *name)
        return CtdlGetUserLen(usbuf, name, cutuserkey(name));
 }
 
+
 int CtdlLockGetCurrentUser(void)
 {
        CitContext *CCC = CC;
-
        return CtdlGetUserLen(&CCC->user, CCC->curr_user, cutuserkey(CCC->curr_user));
 }
 
+
 /*
  * CtdlGetUserLock()  -  same as getuser() but locks the record
  */
@@ -97,17 +97,12 @@ void CtdlPutUser(struct ctdluser *usbuf)
 {
        char usernamekey[USERNAME_SIZE];
 
-       makeuserkey(usernamekey, 
-                   usbuf->fullname, 
-                   cutuserkey(usbuf->fullname));
-
+       makeuserkey(usernamekey, usbuf->fullname, cutuserkey(usbuf->fullname));
        usbuf->version = REV_LEVEL;
-       cdb_store(CDB_USERS,
-                 usernamekey, strlen(usernamekey),
-                 usbuf, sizeof(struct ctdluser));
-
+       cdb_store(CDB_USERS, usernamekey, strlen(usernamekey), usbuf, sizeof(struct ctdluser));
 }
 
+
 void CtdlPutCurrentUserLock()
 {
        CtdlPutUser(&CC->user);
@@ -124,7 +119,6 @@ void CtdlPutUserLock(struct ctdluser *usbuf)
 }
 
 
-
 /*
  * rename_user()  -  this is tricky because the user's display name is the database key
  *
@@ -166,7 +160,7 @@ int rename_user(char *oldname, char *newname) {
                                syslog(LOG_DEBUG, "user_ops: can not rename user \"Citadel\".");
                                retcode = RENAMEUSER_NOT_FOUND;
                        } else {
-                               syslog(LOG_DEBUG, "user_ops: renaming <%s> to <%s>\n", oldname, newname);
+                               syslog(LOG_DEBUG, "user_ops: renaming <%s> to <%s>", oldname, newname);
                                cdb_delete(CDB_USERS, oldnamekey, strlen(oldnamekey));
                                safestrncpy(usbuf.fullname, newname, sizeof usbuf.fullname);
                                CtdlPutUser(&usbuf);
@@ -182,7 +176,6 @@ int rename_user(char *oldname, char *newname) {
 }
 
 
-
 /*
  * Index-generating function used by Ctdl[Get|Set]Relationship
  */
@@ -233,8 +226,6 @@ void CtdlSetRelationship(visit *newvisit,
                         struct ctdluser *rel_user,
                         struct ctdlroom *rel_room)
 {
-
-
        /* We don't use these in Citadel because they're implicit by the
         * index, but they must be present if the database is exported.
         */
@@ -245,6 +236,7 @@ void CtdlSetRelationship(visit *newvisit,
        put_visit(newvisit);
 }
 
+
 /*
  * Locate a relationship between a user and a room
  */
@@ -252,7 +244,6 @@ void CtdlGetRelationship(visit *vbuf,
                         struct ctdluser *rel_user,
                         struct ctdlroom *rel_room)
 {
-
        char IndexBuf[32];
        int IndexLen;
        struct cdbdata *cdbvisit;
@@ -321,6 +312,7 @@ int CtdlCheckInternetMailPermission(struct ctdluser *who) {
        return(0);
 }
 
+
 /*
  * Convenience function.
  */
@@ -328,8 +320,7 @@ int CtdlAccessCheck(int required_level)
 {
        if (CC->internal_pgm) return(0);
        if (required_level >= ac_internal) {
-               cprintf("%d This is not a user-level command.\n",
-                       ERROR + HIGHER_ACCESS_REQUIRED);
+               cprintf("%d This is not a user-level command.\n", ERROR + HIGHER_ACCESS_REQUIRED);
                return(-1);
        }
 
@@ -362,7 +353,6 @@ int CtdlAccessCheck(int required_level)
 }
 
 
-
 /*
  * Is the user currently logged in an Admin?
  */
@@ -633,8 +623,7 @@ int CtdlLoginExistingUser(char *authname, const char *trythisname)
                if (((CC->nologin)) && (CC->user.axlevel < AxAideU)) {
                        return login_too_many_users;
                } else {
-                       safestrncpy(CC->curr_user, CC->user.fullname,
-                                       sizeof CC->curr_user);
+                       safestrncpy(CC->curr_user, CC->user.fullname, sizeof CC->curr_user);
                        return login_ok;
                }
        }
@@ -675,14 +664,32 @@ void do_login(void)
 
        CtdlPutUserLock(&CCC->user);
 
+
+       // PUT THE EMAIL EXTRACT HERE AJC FIXME
+       //#ifdef HAVE_LDAP
+               //if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
+               //int extract_email_addresses_from_ldap(char *ldap_dn, char *emailaddrs)
+       //#endif
+
        /*
-        * Populate CCC->cs_inet_email with a default address.  This will be
-        * overwritten with the user's directory address, if one exists, when
-        * the vCard module's login hook runs.
+        * No email address for user?  Make one up.
         */
-       snprintf(CCC->cs_inet_email, sizeof CCC->cs_inet_email, "%s@%s",
-               CCC->user.fullname, CtdlGetConfigStr("c_fqdn"));
-       convert_spaces_to_underscores(CCC->cs_inet_email);
+       if (IsEmptyStr(CCC->user.emailaddrs)) {
+               sprintf(CCC->user.emailaddrs, "cit%ld@%s", CCC->user.usernum, CtdlGetConfigStr("c_fqdn"));
+       }
+       
+       /*
+        * Populate cs_inet_email and cs_inet_other_emails with valid email addresses from the user record
+        */
+       strcpy(CCC->cs_inet_email, CCC->user.emailaddrs);
+       char *firstsep = strstr(CCC->cs_inet_email, "|");
+       if (firstsep) {
+               strcpy(CCC->cs_inet_other_emails, firstsep+1);
+               *firstsep = 0;
+       }
+       else {
+               CCC->cs_inet_other_emails[0] = 0;
+       }
 
        /* Create any personal rooms required by the system.
         * (Technically, MAILROOM should be there already, but just in case...)
@@ -770,19 +777,19 @@ static int validpw(uid_t uid, const char *pass)
        begin_critical_section(S_CHKPWD);
        rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
        if (rv == -1) {
-               syslog(LOG_EMERG, "user_ops: communication with chkpwd broken: %s", strerror(errno));
+               syslog(LOG_ERR, "user_ops: communication with chkpwd broken: %m");
                end_critical_section(S_CHKPWD);
                return 0;
        }
        rv = write(chkpwd_write_pipe[1], pass, 256);
        if (rv == -1) {
-               syslog(LOG_EMERG, "user_ops: communication with chkpwd broken: %s", strerror(errno));
+               syslog(LOG_ERR, "user_ops: communication with chkpwd broken: %m");
                end_critical_section(S_CHKPWD);
                return 0;
        }
        rv = read(chkpwd_read_pipe[0], buf, 4);
        if (rv == -1) {
-               syslog(LOG_EMERG, "user_ops: ommunication with chkpwd broken: %s", strerror(errno));
+               syslog(LOG_ERR, "user_ops: ommunication with chkpwd broken: %m");
                end_critical_section(S_CHKPWD);
                return 0;
        }
@@ -809,21 +816,21 @@ void start_chkpwd_daemon(void) {
        syslog(LOG_DEBUG, "user_ops: starting chkpwd daemon for host authentication mode");
 
        if ((stat(file_chkpwd, &filestats)==-1) || (filestats.st_size==0)) {
-               printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
+               syslog(LOG_ERR, "user_ops: %s: %m", file_chkpwd);
                abort();
        }
        if (pipe(chkpwd_write_pipe) != 0) {
-               syslog(LOG_ERR, "user_ops: unable to create pipe for chkpwd daemon: %s", strerror(errno));
+               syslog(LOG_ERR, "user_ops: unable to create pipe for chkpwd daemon: %m");
                abort();
        }
        if (pipe(chkpwd_read_pipe) != 0) {
-               syslog(LOG_ERR, "user_ops: unable to create pipe for chkpwd daemon: %s", strerror(errno));
+               syslog(LOG_ERR, "user_ops: unable to create pipe for chkpwd daemon: %m");
                abort();
        }
 
        chkpwd_pid = fork();
        if (chkpwd_pid < 0) {
-               syslog(LOG_ERR, "user_ops: unable to fork chkpwd daemon: %s", strerror(errno));
+               syslog(LOG_ERR, "user_ops: unable to fork chkpwd daemon: %m");
                abort();
        }
        if (chkpwd_pid == 0) {
@@ -831,7 +838,7 @@ void start_chkpwd_daemon(void) {
                dup2(chkpwd_read_pipe[1], 1);
                for (i=2; i<256; ++i) close(i);
                execl(file_chkpwd, file_chkpwd, NULL);
-               syslog(LOG_ERR, "user_ops: unable to exec chkpwd daemon: %s", strerror(errno));
+               syslog(LOG_ERR, "user_ops: unable to exec chkpwd daemon: %m");
                abort();
                exit(errno);
        }
@@ -1044,11 +1051,9 @@ int create_user(const char *newusername, long len, int become_user)
        char buf[SIZ];
        int retval;
        uid_t uid = (-1);
-       
 
        safestrncpy(username, newusername, sizeof username);
        strproc(username);
-
        
        if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {