2 * Server functions which perform operations on user objects.
4 * Copyright (c) 1987-2019 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License, version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
20 #include <libcitadel.h>
23 #include "citserver.h"
25 #include "citadel_ldap.h"
26 #include "ctdl_module.h"
28 #include "internet_addressing.h"
30 /* These pipes are used to talk to the chkpwd daemon, which is forked during startup */
31 int chkpwd_write_pipe[2];
32 int chkpwd_read_pipe[2];
36 * Trim a string down to the maximum username size and return the new length
38 long cutusername(char *username) {
40 len = strlen(username);
41 if (len >= USERNAME_SIZE)
43 syslog(LOG_INFO, "Username too long: %s", username);
44 len = USERNAME_SIZE - 1;
52 * makeuserkey() - convert a username into the format used as a database key
53 * (Key format is the username with all non-alphanumeric characters removed, and converted to lower case.)
55 void makeuserkey(char *key, const char *username, long len) {
59 if (len >= USERNAME_SIZE) {
60 syslog(LOG_INFO, "Username too long: %s", username);
61 len = USERNAME_SIZE - 1;
63 for (i=0; i<=len; ++i) {
64 if (isalnum((username[i]))) {
65 key[keylen++] = tolower(username[i]);
73 * CtdlGetUser() retrieve named user into supplied buffer.
74 * returns 0 on success
76 int CtdlGetUser(struct ctdluser *usbuf, char *name)
78 char usernamekey[USERNAME_SIZE];
79 struct cdbdata *cdbus;
80 long len = cutusername(name);
83 memset(usbuf, 0, sizeof(struct ctdluser));
86 makeuserkey(usernamekey, name, len);
87 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
89 if (cdbus == NULL) { /* user not found */
93 memcpy(usbuf, cdbus->ptr, ((cdbus->len > sizeof(struct ctdluser)) ? sizeof(struct ctdluser) : cdbus->len));
100 int CtdlLockGetCurrentUser(void)
102 CitContext *CCC = CC;
103 return CtdlGetUser(&CCC->user, CCC->curr_user);
108 * CtdlGetUserLock() - same as getuser() but locks the record
110 int CtdlGetUserLock(struct ctdluser *usbuf, char *name)
114 retcode = CtdlGetUser(usbuf, name);
116 begin_critical_section(S_USERS);
123 * CtdlPutUser() - write user buffer into the correct place on disk
125 void CtdlPutUser(struct ctdluser *usbuf)
127 char usernamekey[USERNAME_SIZE];
129 makeuserkey(usernamekey, usbuf->fullname, cutusername(usbuf->fullname));
130 usbuf->version = REV_LEVEL;
131 cdb_store(CDB_USERS, usernamekey, strlen(usernamekey), usbuf, sizeof(struct ctdluser));
135 void CtdlPutCurrentUserLock()
137 CtdlPutUser(&CC->user);
142 * CtdlPutUserLock() - same as putuser() but locks the record
144 void CtdlPutUserLock(struct ctdluser *usbuf)
147 end_critical_section(S_USERS);
152 * rename_user() - this is tricky because the user's display name is the database key
154 * Returns 0 on success or nonzero if there was an error...
157 int rename_user(char *oldname, char *newname) {
158 int retcode = RENAMEUSER_OK;
159 struct ctdluser usbuf;
161 char oldnamekey[USERNAME_SIZE];
162 char newnamekey[USERNAME_SIZE];
164 /* Create the database keys... */
165 makeuserkey(oldnamekey, oldname, cutusername(oldname));
166 makeuserkey(newnamekey, newname, cutusername(newname));
168 /* Lock up and get going */
169 begin_critical_section(S_USERS);
171 /* We cannot rename a user who is currently logged in */
172 if (CtdlIsUserLoggedIn(oldname)) {
173 end_critical_section(S_USERS);
174 return RENAMEUSER_LOGGED_IN;
177 if (CtdlGetUser(&usbuf, newname) == 0) {
178 retcode = RENAMEUSER_ALREADY_EXISTS;
182 if (CtdlGetUser(&usbuf, oldname) != 0) {
183 retcode = RENAMEUSER_NOT_FOUND;
185 else { /* Sanity checks succeeded. Now rename the user. */
186 if (usbuf.usernum == 0) {
187 syslog(LOG_DEBUG, "user_ops: can not rename user \"Citadel\".");
188 retcode = RENAMEUSER_NOT_FOUND;
190 syslog(LOG_DEBUG, "user_ops: renaming <%s> to <%s>", oldname, newname);
191 cdb_delete(CDB_USERS, oldnamekey, strlen(oldnamekey));
192 safestrncpy(usbuf.fullname, newname, sizeof usbuf.fullname);
194 cdb_store(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long), usbuf.fullname, strlen(usbuf.fullname)+1 );
195 retcode = RENAMEUSER_OK;
201 end_critical_section(S_USERS);
207 * Convert a username into the format used as a database key prior to version 928
208 * This only gets called by reindex_user_928()
210 void makeuserkey_pre928(char *key, const char *username, long len) {
213 if (len >= USERNAME_SIZE) {
214 syslog(LOG_INFO, "Username too long: %s", username);
215 len = USERNAME_SIZE - 1;
217 for (i=0; i<=len; ++i) {
218 key[i] = tolower(username[i]);
224 * Read a user record using the pre-v928 index format, and write it back using the v928-and-higher index format.
225 * This ONLY gets called during an upgrade from version <928 to version >=928.
227 void reindex_user_928(char *username, void *out_data) {
229 char oldkey[USERNAME_SIZE];
230 char newkey[USERNAME_SIZE];
231 struct cdbdata *cdbus;
232 long len = cutusername(username);
233 struct ctdluser usbuf;
235 makeuserkey_pre928(oldkey, username, len);
236 makeuserkey(newkey, username, len);
238 syslog(LOG_DEBUG, "user_ops: reindex_user_928: %s <%s> --> <%s>", username, oldkey, newkey);
240 // Fetch the user record using the old index format
241 cdbus = cdb_fetch(CDB_USERS, oldkey, strlen(oldkey));
243 syslog(LOG_INFO, "user_ops: <%s> not found, were they already reindexed?", username);
246 memcpy(&usbuf, cdbus->ptr, ((cdbus->len > sizeof(struct ctdluser)) ? sizeof(struct ctdluser) : cdbus->len));
249 // delete the old record
250 cdb_delete(CDB_USERS, oldkey, strlen(oldkey));
252 // write the new record
253 cdb_store(CDB_USERS, newkey, strlen(newkey), &usbuf, sizeof(struct ctdluser));
258 * Index-generating function used by Ctdl[Get|Set]Relationship
260 int GenerateRelationshipIndex(char *IndexBuf,
271 TheIndex.iRoomID = RoomID;
272 TheIndex.iRoomGen = RoomGen;
273 TheIndex.iUserID = UserID;
275 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
276 return(sizeof(TheIndex));
281 * Back end for CtdlSetRelationship()
283 void put_visit(visit *newvisit)
288 memset (IndexBuf, 0, sizeof (IndexBuf));
289 /* Generate an index */
290 IndexLen = GenerateRelationshipIndex(IndexBuf, newvisit->v_roomnum, newvisit->v_roomgen, newvisit->v_usernum);
292 /* Store the record */
293 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
294 newvisit, sizeof(visit)
300 * Define a relationship between a user and a room
302 void CtdlSetRelationship(visit *newvisit, struct ctdluser *rel_user, struct ctdlroom *rel_room) {
303 /* We don't use these in Citadel because they're implicit by the
304 * index, but they must be present if the database is exported.
306 newvisit->v_roomnum = rel_room->QRnumber;
307 newvisit->v_roomgen = rel_room->QRgen;
308 newvisit->v_usernum = rel_user->usernum;
315 * Locate a relationship between a user and a room
317 void CtdlGetRelationship(visit *vbuf, struct ctdluser *rel_user, struct ctdlroom *rel_room) {
320 struct cdbdata *cdbvisit;
322 /* Generate an index */
323 IndexLen = GenerateRelationshipIndex(IndexBuf, rel_room->QRnumber, rel_room->QRgen, rel_user->usernum);
325 /* Clear out the buffer */
326 memset(vbuf, 0, sizeof(visit));
328 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
329 if (cdbvisit != NULL) {
330 memcpy(vbuf, cdbvisit->ptr, ((cdbvisit->len > sizeof(visit)) ? sizeof(visit) : cdbvisit->len));
334 /* If this is the first time the user has seen this room,
335 * set the view to be the default for the room.
337 vbuf->v_view = rel_room->QRdefaultview;
340 /* Set v_seen if necessary */
341 if (vbuf->v_seen[0] == 0) {
342 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
347 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
349 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
353 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
355 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
360 * Check to see if the specified user has Internet mail permission
361 * (returns nonzero if permission is granted)
363 int CtdlCheckInternetMailPermission(struct ctdluser *who) {
365 /* Do not allow twits to send Internet mail */
366 if (who->axlevel <= AxProbU) return(0);
368 /* Globally enabled? */
369 if (CtdlGetConfigInt("c_restrict") == 0) return(1);
371 /* User flagged ok? */
372 if (who->flags & US_INTERNET) return(2);
374 /* Admin level access? */
375 if (who->axlevel >= AxAideU) return(3);
377 /* No mail for you! */
383 * Convenience function.
385 int CtdlAccessCheck(int required_level)
387 if (CC->internal_pgm) return(0);
388 if (required_level >= ac_internal) {
389 cprintf("%d This is not a user-level command.\n", ERROR + HIGHER_ACCESS_REQUIRED);
393 if ((required_level >= ac_logged_in_or_guest) && (CC->logged_in == 0) && (CtdlGetConfigInt("c_guest_logins") == 0)) {
394 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
398 if ((required_level >= ac_logged_in) && (CC->logged_in == 0)) {
399 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
403 if (CC->user.axlevel >= AxAideU) return(0);
404 if (required_level >= ac_aide) {
405 cprintf("%d This command requires Admin access.\n",
406 ERROR + HIGHER_ACCESS_REQUIRED);
410 if (is_room_aide()) return(0);
411 if (required_level >= ac_room_aide) {
412 cprintf("%d This command requires Admin or Room Admin access.\n",
413 ERROR + HIGHER_ACCESS_REQUIRED);
417 /* shhh ... succeed quietly */
423 * Is the user currently logged in an Admin?
427 if (CC->user.axlevel >= AxAideU)
435 * Is the user currently logged in an Admin *or* the room Admin for this room?
437 int is_room_aide(void)
440 if (!CC->logged_in) {
444 if ((CC->user.axlevel >= AxAideU) || (CC->room.QRroomaide == CC->user.usernum)) {
453 * CtdlGetUserByNumber() - get user by number
454 * returns 0 if user was found
456 * Note: fetching a user this way requires one additional database operation.
458 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number)
460 struct cdbdata *cdbun;
463 cdbun = cdb_fetch(CDB_USERSBYNUMBER, &number, sizeof(long));
465 syslog(LOG_INFO, "user_ops: %ld not found", number);
469 syslog(LOG_INFO, "user_ops: %ld maps to %s", number, cdbun->ptr);
470 r = CtdlGetUser(usbuf, cdbun->ptr);
477 * Helper function for rebuild_usersbynumber()
479 void rebuild_ubn_for_user(char *username, void *data) {
482 syslog(LOG_DEBUG, "user_ops: rebuilding usersbynumber index for %s", username);
483 if (CtdlGetUser(&u, username) == 0) {
484 cdb_store(CDB_USERSBYNUMBER, &(u.usernum), sizeof(long), u.fullname, strlen(u.fullname)+1);
490 * Rebuild the users-by-number index
492 void rebuild_usersbynumber(void) {
493 cdb_trunc(CDB_USERSBYNUMBER); /* delete the old indices */
494 ForEachUser(rebuild_ubn_for_user, NULL); /* enumerate the users */
499 * getuserbyuid() Get user by system uid (for PAM mode authentication)
500 * Returns 0 if user was found
501 * This now uses an extauth index.
503 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
505 struct cdbdata *cdbextauth;
509 claimed_id = NewStrBuf();
510 StrBufPrintf(claimed_id, "uid:%d", number);
511 cdbextauth = cdb_fetch(CDB_EXTAUTH, ChrPtr(claimed_id), StrLength(claimed_id));
512 FreeStrBuf(&claimed_id);
513 if (cdbextauth == NULL) {
517 memcpy(&usernum, cdbextauth->ptr, sizeof(long));
518 cdb_free(cdbextauth);
520 if (!CtdlGetUserByNumber(usbuf, usernum)) {
529 * Back end for cmd_user() and its ilk
531 int CtdlLoginExistingUser(const char *trythisname)
536 syslog(LOG_DEBUG, "user_ops: CtdlLoginExistingUser(%s)", trythisname);
538 if ((CC->logged_in)) {
539 return login_already_logged_in;
542 if (trythisname == NULL) return login_not_found;
544 if (!strncasecmp(trythisname, "SYS_", 4))
546 syslog(LOG_DEBUG, "user_ops: system user \"%s\" is not allowed to log in.", trythisname);
547 return login_not_found;
550 /* Continue attempting user validation... */
551 safestrncpy(username, trythisname, sizeof (username));
554 if (IsEmptyStr(username)) {
555 return login_not_found;
558 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
563 struct passwd *tempPwdPtr;
566 syslog(LOG_DEBUG, "user_ops: asking host about <%s>", username);
567 #ifdef HAVE_GETPWNAM_R
568 #ifdef SOLARIS_GETPWUID
569 syslog(LOG_DEBUG, "user_ops: calling getpwnam_r()");
570 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
571 #else // SOLARIS_GETPWUID
572 syslog(LOG_DEBUG, "user_ops: calling getpwnam_r()");
573 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
574 #endif // SOLARIS_GETPWUID
575 #else // HAVE_GETPWNAM_R
576 syslog(LOG_DEBUG, "user_ops: SHOULD NEVER GET HERE!!!");
578 #endif // HAVE_GETPWNAM_R
579 if (tempPwdPtr == NULL) {
580 syslog(LOG_DEBUG, "user_ops: no such user <%s>", username);
581 return login_not_found;
584 /* Locate the associated Citadel account. If not found, make one attempt to create it. */
585 found_user = getuserbyuid(&CC->user, pd.pw_uid);
586 if (found_user != 0) {
587 create_user(username, CREATE_USER_DO_NOT_BECOME_USER, pd.pw_uid);
588 found_user = getuserbyuid(&CC->user, pd.pw_uid);
590 syslog(LOG_DEBUG, "user_ops: found it: uid=%ld, gecos=%s here: %d", (long)pd.pw_uid, pd.pw_gecos, found_user);
595 else if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
603 found_user = CtdlTryUserLDAP(username, ldap_dn, sizeof ldap_dn, ldap_cn, sizeof ldap_cn, &ldap_uid);
604 if (found_user != 0) {
605 return login_not_found;
608 found_user = getuserbyuid(&CC->user, ldap_uid);
609 if (found_user != 0) {
610 create_user(ldap_cn, CREATE_USER_DO_NOT_BECOME_USER, ldap_uid);
611 found_user = getuserbyuid(&CC->user, ldap_uid);
614 if (found_user == 0) {
615 if (CC->ldap_dn != NULL) free(CC->ldap_dn);
616 CC->ldap_dn = strdup(ldap_dn);
623 /* native auth mode */
625 recptypes *valid = NULL;
627 /* First, try to log in as if the supplied name is a display name */
628 found_user = CtdlGetUser(&CC->user, username);
630 /* If that didn't work, try to log in as if the supplied name * is an e-mail address */
631 if (found_user != 0) {
632 valid = validate_recipients(username, NULL, 0);
634 if (valid->num_local == 1) {
635 found_user = CtdlGetUser(&CC->user, valid->recp_local);
637 free_recipients(valid);
642 /* Did we find something? */
643 if (found_user == 0) {
644 if (((CC->nologin)) && (CC->user.axlevel < AxAideU)) {
645 return login_too_many_users;
647 safestrncpy(CC->curr_user, CC->user.fullname, sizeof CC->curr_user);
651 return login_not_found;
656 * session startup code which is common to both cmd_pass() and cmd_newu()
661 syslog(LOG_NOTICE, "user_ops: <%s> logged in", CC->curr_user);
663 CtdlGetUserLock(&CC->user, CC->curr_user);
664 ++(CC->user.timescalled);
665 CC->previous_login = CC->user.lastcall;
666 time(&CC->user.lastcall);
668 /* If this user's name is the name of the system administrator
669 * (as specified in setup), automatically assign access level 6.
671 if ( (!IsEmptyStr(CtdlGetConfigStr("c_sysadm"))) && (!strcasecmp(CC->user.fullname, CtdlGetConfigStr("c_sysadm"))) ) {
672 CC->user.axlevel = AxAideU;
675 /* If we're authenticating off the host system, automatically give root the highest level of access. */
676 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
677 if (CC->user.uid == 0) {
678 CC->user.axlevel = AxAideU;
681 CtdlPutUserLock(&CC->user);
683 /* If we are using LDAP authentication, extract the user's email addresses from the directory. */
685 if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
686 char new_emailaddrs[512];
687 if (CtdlGetConfigInt("c_ldap_sync_email_addrs") > 0) {
688 if (extract_email_addresses_from_ldap(CC->ldap_dn, new_emailaddrs) == 0) {
689 CtdlSetEmailAddressesForUser(CC->user.fullname, new_emailaddrs);
695 /* If the user does not have any email addresses assigned, generate one. */
696 if (IsEmptyStr(CC->user.emailaddrs)) {
697 AutoGenerateEmailAddressForUser(&CC->user);
700 /* Populate the user principal identity, which is consistent and never aliased */
701 strcpy(CC->cs_principal_id, "wowowowow");
702 makeuserkey(CC->cs_principal_id, CC->user.fullname, sizeof CC->user.fullname);
703 strcat(CC->cs_principal_id, "@");
704 strcat(CC->cs_principal_id, CtdlGetConfigStr("c_fqdn"));
707 * Populate cs_inet_email and cs_inet_other_emails with valid email addresses from the user record
709 strcpy(CC->cs_inet_email, CC->user.emailaddrs);
710 char *firstsep = strstr(CC->cs_inet_email, "|");
712 strcpy(CC->cs_inet_other_emails, firstsep+1);
716 CC->cs_inet_other_emails[0] = 0;
719 /* Create any personal rooms required by the system.
720 * (Technically, MAILROOM should be there already, but just in case...)
722 CtdlCreateRoom(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
723 CtdlCreateRoom(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
724 CtdlCreateRoom(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
725 CtdlCreateRoom(USERDRAFTROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
727 /* Run any startup routines registered by loadable modules */
728 PerformSessionHooks(EVT_LOGIN);
730 /* Enter the lobby */
731 CtdlUserGoto(CtdlGetConfigStr("c_baseroom"), 0, 0, NULL, NULL, NULL, NULL);
735 void logged_in_response(void)
737 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
738 CIT_OK, CC->user.fullname, CC->user.axlevel,
739 CC->user.timescalled, CC->user.posted,
740 CC->user.flags, CC->user.usernum,
746 void CtdlUserLogout(void)
748 CitContext *CCC = MyContext();
750 syslog(LOG_DEBUG, "user_ops: CtdlUserLogout() logging out <%s> from session %d", CCC->curr_user, CCC->cs_pid);
752 /* Run any hooks registered by modules... */
753 PerformSessionHooks(EVT_LOGOUT);
756 * Clear out some session data. Most likely, the CitContext for this
757 * session is about to get nuked when the session disconnects, but
758 * since it's possible to log in again without reconnecting, we cannot
759 * make that assumption.
763 /* Check to see if the user was deleted while logged in and purge them if necessary */
764 if ((CCC->user.axlevel == AxDeleted) && (CCC->user.usernum)) {
765 purge_user(CCC->user.fullname);
768 /* Clear out the user record in memory so we don't behave like a ghost */
769 memset(&CCC->user, 0, sizeof(struct ctdluser));
770 CCC->curr_user[0] = 0;
771 CCC->cs_inet_email[0] = 0;
772 CCC->cs_inet_other_emails[0] = 0;
773 CCC->cs_inet_fn[0] = 0;
775 /* Free any output buffers */
781 * Validate a password on the host unix system by talking to the chkpwd daemon
783 static int validpw(uid_t uid, const char *pass)
788 if (IsEmptyStr(pass)) {
789 syslog(LOG_DEBUG, "user_ops: refusing to chkpwd for uid=%d with empty password", uid);
793 syslog(LOG_DEBUG, "user_ops: validating password for uid=%d using chkpwd...", uid);
795 begin_critical_section(S_CHKPWD);
796 rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
798 syslog(LOG_ERR, "user_ops: communication with chkpwd broken: %m");
799 end_critical_section(S_CHKPWD);
802 rv = write(chkpwd_write_pipe[1], pass, 256);
804 syslog(LOG_ERR, "user_ops: communication with chkpwd broken: %m");
805 end_critical_section(S_CHKPWD);
808 rv = read(chkpwd_read_pipe[0], buf, 4);
810 syslog(LOG_ERR, "user_ops: ommunication with chkpwd broken: %m");
811 end_critical_section(S_CHKPWD);
814 end_critical_section(S_CHKPWD);
816 if (!strncmp(buf, "PASS", 4)) {
817 syslog(LOG_DEBUG, "user_ops: chkpwd pass");
821 syslog(LOG_DEBUG, "user_ops: chkpwd fail");
827 * Start up the chkpwd daemon so validpw() has something to talk to
829 void start_chkpwd_daemon(void) {
831 struct stat filestats;
834 syslog(LOG_DEBUG, "user_ops: starting chkpwd daemon for host authentication mode");
836 if ((stat(file_chkpwd, &filestats)==-1) || (filestats.st_size==0)) {
837 syslog(LOG_ERR, "user_ops: %s: %m", file_chkpwd);
840 if (pipe(chkpwd_write_pipe) != 0) {
841 syslog(LOG_ERR, "user_ops: unable to create pipe for chkpwd daemon: %m");
844 if (pipe(chkpwd_read_pipe) != 0) {
845 syslog(LOG_ERR, "user_ops: unable to create pipe for chkpwd daemon: %m");
850 if (chkpwd_pid < 0) {
851 syslog(LOG_ERR, "user_ops: unable to fork chkpwd daemon: %m");
854 if (chkpwd_pid == 0) {
855 dup2(chkpwd_write_pipe[0], 0);
856 dup2(chkpwd_read_pipe[1], 1);
857 for (i=2; i<256; ++i) close(i);
858 execl(file_chkpwd, file_chkpwd, NULL);
859 syslog(LOG_ERR, "user_ops: unable to exec chkpwd daemon: %m");
866 int CtdlTryPassword(const char *password, long len)
869 CitContext *CCC = CC;
871 if ((CCC->logged_in)) {
872 syslog(LOG_WARNING, "user_ops: CtdlTryPassword: already logged in");
873 return pass_already_logged_in;
875 if (!strcmp(CCC->curr_user, NLI)) {
876 syslog(LOG_WARNING, "user_ops: CtdlTryPassword: no user selected");
879 if (CtdlGetUser(&CCC->user, CCC->curr_user)) {
880 syslog(LOG_ERR, "user_ops: CtdlTryPassword: internal error");
881 return pass_internal_error;
883 if (password == NULL) {
884 syslog(LOG_INFO, "user_ops: CtdlTryPassword: NULL password string supplied");
885 return pass_wrong_password;
888 else if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
892 if (validpw(CCC->user.uid, password)) {
896 * sooper-seekrit hack: populate the password field in the
897 * citadel database with the password that the user typed,
898 * if it's correct. This allows most sites to convert from
899 * host auth to native auth if they want to. If you think
900 * this is a security hazard, comment it out.
903 CtdlGetUserLock(&CCC->user, CCC->curr_user);
904 safestrncpy(CCC->user.password, password, sizeof CCC->user.password);
905 CtdlPutUserLock(&CCC->user);
908 * (sooper-seekrit hack ends here)
917 else if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
921 if ((CCC->ldap_dn) && (!CtdlTryPasswordLDAP(CCC->ldap_dn, password))) {
932 /* native auth mode */
935 pw = (char*) malloc(len + 1);
936 memcpy(pw, password, len + 1);
938 strproc(CCC->user.password);
939 code = strcasecmp(CCC->user.password, pw);
942 strproc(CCC->user.password);
943 code = strcasecmp(CCC->user.password, pw);
953 syslog(LOG_WARNING, "user_ops: bad password specified for <%s> Service <%s> Port <%ld> Remote <%s / %s>",
960 return pass_wrong_password;
966 * Delete a user record *and* all of its related resources.
968 int purge_user(char pname[])
970 struct ctdluser usbuf;
971 char usernamekey[USERNAME_SIZE];
973 makeuserkey(usernamekey, pname, cutusername(pname));
975 /* If the name is empty we can't find them in the DB any way so just return */
976 if (IsEmptyStr(pname)) {
977 return(ERROR + NO_SUCH_USER);
980 if (CtdlGetUser(&usbuf, pname) != 0) {
981 syslog(LOG_ERR, "user_ops: cannot purge user <%s> - not found", pname);
982 return(ERROR + NO_SUCH_USER);
985 /* Don't delete a user who is currently logged in. Instead, just
986 * set the access level to 0, and let the account get swept up
987 * during the next purge.
989 if (CtdlIsUserLoggedInByNum(usbuf.usernum)) {
990 syslog(LOG_WARNING, "user_ops: <%s> is logged in; not deleting", pname);
991 usbuf.axlevel = AxDeleted;
996 syslog(LOG_NOTICE, "user_ops: deleting <%s>", pname);
998 /* Perform any purge functions registered by server extensions */
999 PerformUserHooks(&usbuf, EVT_PURGEUSER);
1001 /* delete any existing user/room relationships */
1002 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
1004 /* delete the users-by-number index record */
1005 cdb_delete(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long));
1007 /* delete the userlog entry */
1008 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
1014 int internal_create_user(char *username, struct ctdluser *usbuf, uid_t uid)
1016 if (!CtdlGetUser(usbuf, username)) {
1017 return(ERROR + ALREADY_EXISTS);
1020 /* Go ahead and initialize a new user record */
1021 memset(usbuf, 0, sizeof(struct ctdluser));
1022 safestrncpy(usbuf->fullname, username, sizeof usbuf->fullname);
1023 strcpy(usbuf->password, "");
1026 /* These are the default flags on new accounts */
1027 usbuf->flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
1029 usbuf->timescalled = 0;
1031 usbuf->axlevel = CtdlGetConfigInt("c_initax");
1032 usbuf->lastcall = time(NULL);
1034 /* fetch a new user number */
1035 usbuf->usernum = get_new_user_number();
1037 /* add user to the database */
1039 cdb_store(CDB_USERSBYNUMBER, &usbuf->usernum, sizeof(long), usbuf->fullname, strlen(usbuf->fullname)+1);
1041 /* If non-native auth, index by uid */
1042 if ((usbuf->uid > 0) && (usbuf->uid != NATIVE_AUTH_UID)) {
1043 StrBuf *claimed_id = NewStrBuf();
1044 StrBufPrintf(claimed_id, "uid:%d", usbuf->uid);
1045 attach_extauth(usbuf, claimed_id);
1046 FreeStrBuf(&claimed_id);
1054 * create_user() - back end processing to create a new user
1056 * Set 'newusername' to the desired account name.
1057 * Set 'become_user' to CREATE_USER_BECOME_USER if this is self-service account creation and we want to
1058 * actually log in as the user we just created, otherwise set it to CREATE_USER_DO_NOT_BECOME_USER
1059 * Set 'uid' to some uid_t value to associate the account with an external auth user, or (-1) for native auth
1061 int create_user(char *username, int become_user, uid_t uid)
1063 struct ctdluser usbuf;
1064 struct ctdlroom qrbuf;
1065 char mailboxname[ROOMNAMELEN];
1070 if ((retval = internal_create_user(username, &usbuf, uid)) != 0) {
1075 * Give the user a private mailbox and a configuration room.
1076 * Make the latter an invisible system room.
1078 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
1079 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
1081 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
1082 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
1083 if (CtdlGetRoomLock(&qrbuf, mailboxname) == 0) {
1084 qrbuf.QRflags2 |= QR2_SYSTEM;
1085 CtdlPutRoomLock(&qrbuf);
1088 /* Perform any create functions registered by server extensions */
1089 PerformUserHooks(&usbuf, EVT_NEWUSER);
1091 /* Everything below this line can be bypassed if administratively
1092 * creating a user, instead of doing self-service account creation
1095 if (become_user == CREATE_USER_BECOME_USER) {
1096 /* Now become the user we just created */
1097 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
1098 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
1101 /* Check to make sure we're still who we think we are */
1102 if (CtdlGetUser(&CC->user, CC->curr_user)) {
1103 return(ERROR + INTERNAL_ERROR);
1108 "New user account <%s> has been created, from host %s [%s].\n",
1113 CtdlAideMessage(buf, "User Creation Notice");
1114 syslog(LOG_NOTICE, "user_ops: <%s> created", username);
1120 * set password - back end api code
1122 void CtdlSetPassword(char *new_pw)
1124 CtdlGetUserLock(&CC->user, CC->curr_user);
1125 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1126 CtdlPutUserLock(&CC->user);
1127 syslog(LOG_INFO, "user_ops: password changed for <%s>", CC->curr_user);
1128 PerformSessionHooks(EVT_SETPASS);
1133 * API function for cmd_invt_kick() and anything else that needs to
1134 * invite or kick out a user to/from a room.
1136 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1138 int CtdlInvtKick(char *iuser, int op) {
1139 struct ctdluser USscratch;
1143 if (CtdlGetUser(&USscratch, iuser) != 0) {
1147 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1149 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1150 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1153 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1154 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1156 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1158 /* post a message in Aide> saying what we just did */
1159 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1161 ((op == 1) ? "invited to" : "kicked out of"),
1163 (CC->logged_in ? CC->user.fullname : "an administrator")
1165 CtdlAideMessage(bbb,"User Admin Message");
1171 * Forget (Zap) the current room (API call)
1172 * Returns 0 on success
1174 int CtdlForgetThisRoom(void) {
1177 /* On some systems, Admins are not allowed to forget rooms */
1178 if (is_aide() && (CtdlGetConfigInt("c_aide_zap") == 0)
1179 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1183 CtdlGetUserLock(&CC->user, CC->curr_user);
1184 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1186 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1187 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1189 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1190 CtdlPutUserLock(&CC->user);
1192 /* Return to the Lobby, so we don't end up in an undefined room */
1193 CtdlUserGoto(CtdlGetConfigStr("c_baseroom"), 0, 0, NULL, NULL, NULL, NULL);
1199 * Traverse the user file and perform a callback for each user record.
1200 * (New improved version that runs in two phases so that callbacks can perform writes without having a r/o cursor open)
1202 void ForEachUser(void (*CallBack) (char *, void *out_data), void *in_data)
1204 struct cdbdata *cdbus;
1205 struct ctdluser *usptr;
1209 char username[USERNAME_SIZE];
1211 struct feu *ufirst = NULL;
1212 struct feu *ulast = NULL;
1213 struct feu *f = NULL;
1215 cdb_rewind(CDB_USERS);
1217 // Phase 1 : build a linked list of all our user account names
1218 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1219 usptr = (struct ctdluser *) cdbus->ptr;
1221 if (strlen(usptr->fullname) > 0) {
1222 f = malloc(sizeof(struct feu));
1224 strncpy(f->username, usptr->fullname, USERNAME_SIZE);
1226 if (ufirst == NULL) {
1237 // Phase 2 : perform the callback for each user while de-allocating the list
1238 while (ufirst != NULL) {
1239 (*CallBack) (ufirst->username, in_data);
1241 ufirst = ufirst->next;
1248 * Count the number of new mail messages the user has
1252 int num_newmsgs = 0;
1253 num_newmsgs = CC->newmail;
1255 return(num_newmsgs);
1260 * Count the number of new mail messages the user has
1262 int InitialMailCheck()
1264 int num_newmsgs = 0;
1266 char mailboxname[ROOMNAMELEN];
1267 struct ctdlroom mailbox;
1269 struct cdbdata *cdbfr;
1270 long *msglist = NULL;
1273 CtdlMailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1274 if (CtdlGetRoom(&mailbox, mailboxname) != 0)
1276 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1278 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1280 if (cdbfr != NULL) {
1281 msglist = malloc(cdbfr->len);
1282 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1283 num_msgs = cdbfr->len / sizeof(long);
1287 for (a = 0; a < num_msgs; ++a) {
1288 if (msglist[a] > 0L) {
1289 if (msglist[a] > vbuf.v_lastseen) {
1294 if (msglist != NULL)
1297 return(num_newmsgs);