1 // Server functions which perform operations on user objects.
3 // Copyright (c) 1987-2021 by the citadel.org team
5 // This program is open source software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License, version 3.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
18 #include <libcitadel.h>
21 #include "citserver.h"
23 #include "citadel_ldap.h"
24 #include "ctdl_module.h"
26 #include "internet_addressing.h"
28 /* These pipes are used to talk to the chkpwd daemon, which is forked during startup */
29 int chkpwd_write_pipe[2];
30 int chkpwd_read_pipe[2];
34 * makeuserkey() - convert a username into the format used as a database key
35 * "key" must be a buffer of at least USERNAME_SIZE
36 * (Key format is the username with all non-alphanumeric characters removed, and converted to lower case.)
38 void makeuserkey(char *key, const char *username) {
42 if (IsEmptyStr(username)) {
47 int len = strlen(username);
48 for (i=0; ((i<=len) && (i<USERNAME_SIZE-1)); ++i) {
49 if (isalnum((username[i]))) {
50 key[keylen++] = tolower(username[i]);
58 * Compare two usernames to see if they are the same user after being keyed for the database
59 * Usage is identical to strcmp()
61 int CtdlUserCmp(char *s1, char *s2) {
62 char k1[USERNAME_SIZE];
63 char k2[USERNAME_SIZE];
67 return(strcmp(k1,k2));
72 * CtdlGetUser() retrieve named user into supplied buffer.
73 * returns 0 on success
75 int CtdlGetUser(struct ctdluser *usbuf, char *name)
77 char usernamekey[USERNAME_SIZE];
78 struct cdbdata *cdbus;
81 memset(usbuf, 0, sizeof(struct ctdluser));
84 makeuserkey(usernamekey, name);
85 if (IsEmptyStr(usernamekey)) {
86 return(1); // empty user name
88 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
90 if (cdbus == NULL) { // user not found
94 memcpy(usbuf, cdbus->ptr, ((cdbus->len > sizeof(struct ctdluser)) ? sizeof(struct ctdluser) : cdbus->len));
101 int CtdlLockGetCurrentUser(void) {
102 CitContext *CCC = CC;
103 return CtdlGetUser(&CCC->user, CCC->curr_user);
107 // CtdlGetUserLock() - same as getuser() but locks the record
108 int CtdlGetUserLock(struct ctdluser *usbuf, char *name) {
111 retcode = CtdlGetUser(usbuf, name);
113 begin_critical_section(S_USERS);
119 // CtdlPutUser() - write user buffer into the correct place on disk
120 void CtdlPutUser(struct ctdluser *usbuf) {
121 char usernamekey[USERNAME_SIZE];
122 makeuserkey(usernamekey, usbuf->fullname);
123 usbuf->version = REV_LEVEL;
124 cdb_store(CDB_USERS, usernamekey, strlen(usernamekey), usbuf, sizeof(struct ctdluser));
128 void CtdlPutCurrentUserLock() {
129 CtdlPutUser(&CC->user);
133 // CtdlPutUserLock() - same as putuser() but locks the record
134 void CtdlPutUserLock(struct ctdluser *usbuf) {
136 end_critical_section(S_USERS);
140 // rename_user() - this is tricky because the user's display name is the database key
141 // Returns 0 on success or nonzero if there was an error...
142 int rename_user(char *oldname, char *newname) {
143 int retcode = RENAMEUSER_OK;
144 struct ctdluser usbuf;
146 char oldnamekey[USERNAME_SIZE];
147 char newnamekey[USERNAME_SIZE];
149 // Create the database keys...
150 makeuserkey(oldnamekey, oldname);
151 makeuserkey(newnamekey, newname);
153 // Lock up and get going
154 begin_critical_section(S_USERS);
156 // We cannot rename a user who is currently logged in
157 if (CtdlIsUserLoggedIn(oldname)) {
158 end_critical_section(S_USERS);
159 return RENAMEUSER_LOGGED_IN;
162 if (CtdlGetUser(&usbuf, newname) == 0) {
163 retcode = RENAMEUSER_ALREADY_EXISTS;
167 if (CtdlGetUser(&usbuf, oldname) != 0) {
168 retcode = RENAMEUSER_NOT_FOUND;
170 else { // Sanity checks succeeded. Now rename the user.
171 if (usbuf.usernum == 0) {
172 syslog(LOG_DEBUG, "user_ops: can not rename user \"Citadel\".");
173 retcode = RENAMEUSER_NOT_FOUND;
176 syslog(LOG_DEBUG, "user_ops: renaming <%s> to <%s>", oldname, newname);
177 cdb_delete(CDB_USERS, oldnamekey, strlen(oldnamekey));
178 safestrncpy(usbuf.fullname, newname, sizeof usbuf.fullname);
180 cdb_store(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long), usbuf.fullname, strlen(usbuf.fullname)+1 );
181 retcode = RENAMEUSER_OK;
187 end_critical_section(S_USERS);
192 // Convert a username into the format used as a database key prior to version 928
193 // This only gets called by reindex_user_928()
194 void makeuserkey_pre928(char *key, const char *username) {
197 int len = strlen(username);
199 if (len >= USERNAME_SIZE) {
200 syslog(LOG_INFO, "Username too long: %s", username);
201 len = USERNAME_SIZE - 1;
203 for (i=0; i<=len; ++i) {
204 key[i] = tolower(username[i]);
209 // Read a user record using the pre-v928 index format, and write it back using the v928-and-higher index format.
210 // This ONLY gets called during an upgrade from version <928 to version >=928.
211 void reindex_user_928(char *username, void *out_data) {
213 char oldkey[USERNAME_SIZE];
214 char newkey[USERNAME_SIZE];
215 struct cdbdata *cdbus;
216 struct ctdluser usbuf;
218 makeuserkey_pre928(oldkey, username);
219 makeuserkey(newkey, username);
221 syslog(LOG_DEBUG, "user_ops: reindex_user_928: %s <%s> --> <%s>", username, oldkey, newkey);
223 // Fetch the user record using the old index format
224 cdbus = cdb_fetch(CDB_USERS, oldkey, strlen(oldkey));
226 syslog(LOG_INFO, "user_ops: <%s> not found, were they already reindexed?", username);
229 memcpy(&usbuf, cdbus->ptr, ((cdbus->len > sizeof(struct ctdluser)) ? sizeof(struct ctdluser) : cdbus->len));
232 // delete the old record
233 cdb_delete(CDB_USERS, oldkey, strlen(oldkey));
235 // write the new record
236 cdb_store(CDB_USERS, newkey, strlen(newkey), &usbuf, sizeof(struct ctdluser));
240 // Index-generating function used by Ctdl[Get|Set]Relationship
241 int GenerateRelationshipIndex(char *IndexBuf,
252 TheIndex.iRoomID = RoomID;
253 TheIndex.iRoomGen = RoomGen;
254 TheIndex.iUserID = UserID;
256 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
257 return(sizeof(TheIndex));
261 // Back end for CtdlSetRelationship()
262 void put_visit(visit *newvisit) {
266 memset (IndexBuf, 0, sizeof (IndexBuf));
268 IndexLen = GenerateRelationshipIndex(IndexBuf, newvisit->v_roomnum, newvisit->v_roomgen, newvisit->v_usernum);
271 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
272 newvisit, sizeof(visit)
277 // Define a relationship between a user and a room
278 void CtdlSetRelationship(visit *newvisit, struct ctdluser *rel_user, struct ctdlroom *rel_room) {
279 // We don't use these in Citadel because they're implicit by the
280 // index, but they must be present if the database is exported.
281 newvisit->v_roomnum = rel_room->QRnumber;
282 newvisit->v_roomgen = rel_room->QRgen;
283 newvisit->v_usernum = rel_user->usernum;
290 * Locate a relationship between a user and a room
292 void CtdlGetRelationship(visit *vbuf, struct ctdluser *rel_user, struct ctdlroom *rel_room) {
295 struct cdbdata *cdbvisit;
297 /* Generate an index */
298 IndexLen = GenerateRelationshipIndex(IndexBuf, rel_room->QRnumber, rel_room->QRgen, rel_user->usernum);
300 /* Clear out the buffer */
301 memset(vbuf, 0, sizeof(visit));
303 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
304 if (cdbvisit != NULL) {
305 memcpy(vbuf, cdbvisit->ptr, ((cdbvisit->len > sizeof(visit)) ? sizeof(visit) : cdbvisit->len));
309 /* If this is the first time the user has seen this room,
310 * set the view to be the default for the room.
312 vbuf->v_view = rel_room->QRdefaultview;
315 /* Set v_seen if necessary */
316 if (vbuf->v_seen[0] == 0) {
317 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
322 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
324 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
328 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
330 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
335 * Check to see if the specified user has Internet mail permission
336 * (returns nonzero if permission is granted)
338 int CtdlCheckInternetMailPermission(struct ctdluser *who) {
340 /* Do not allow twits to send Internet mail */
341 if (who->axlevel <= AxProbU) return(0);
343 /* Globally enabled? */
344 if (CtdlGetConfigInt("c_restrict") == 0) return(1);
346 /* User flagged ok? */
347 if (who->flags & US_INTERNET) return(2);
349 /* Admin level access? */
350 if (who->axlevel >= AxAideU) return(3);
352 /* No mail for you! */
358 * Convenience function.
360 int CtdlAccessCheck(int required_level)
362 if (CC->internal_pgm) return(0);
363 if (required_level >= ac_internal) {
364 cprintf("%d This is not a user-level command.\n", ERROR + HIGHER_ACCESS_REQUIRED);
368 if ((required_level >= ac_logged_in_or_guest) && (CC->logged_in == 0) && (CtdlGetConfigInt("c_guest_logins") == 0)) {
369 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
373 if ((required_level >= ac_logged_in) && (CC->logged_in == 0)) {
374 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
378 if (CC->user.axlevel >= AxAideU) return(0);
379 if (required_level >= ac_aide) {
380 cprintf("%d This command requires Admin access.\n",
381 ERROR + HIGHER_ACCESS_REQUIRED);
385 if (is_room_aide()) return(0);
386 if (required_level >= ac_room_aide) {
387 cprintf("%d This command requires Admin or Room Admin access.\n",
388 ERROR + HIGHER_ACCESS_REQUIRED);
392 /* shhh ... succeed quietly */
398 * Is the user currently logged in an Admin?
401 if (CC->user.axlevel >= AxAideU)
409 * Is the user currently logged in an Admin *or* the room Admin for this room?
411 int is_room_aide(void) {
413 if (!CC->logged_in) {
417 if ((CC->user.axlevel >= AxAideU) || (CC->room.QRroomaide == CC->user.usernum)) {
426 * CtdlGetUserByNumber() - get user by number
427 * returns 0 if user was found
429 * Note: fetching a user this way requires one additional database operation.
431 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number) {
432 struct cdbdata *cdbun;
435 cdbun = cdb_fetch(CDB_USERSBYNUMBER, &number, sizeof(long));
437 syslog(LOG_INFO, "user_ops: %ld not found", number);
441 syslog(LOG_INFO, "user_ops: %ld maps to %s", number, cdbun->ptr);
442 r = CtdlGetUser(usbuf, cdbun->ptr);
449 * Helper function for rebuild_usersbynumber()
451 void rebuild_ubn_for_user(char *username, void *data) {
454 syslog(LOG_DEBUG, "user_ops: rebuilding usersbynumber index for %s", username);
455 if (CtdlGetUser(&u, username) == 0) {
456 cdb_store(CDB_USERSBYNUMBER, &(u.usernum), sizeof(long), u.fullname, strlen(u.fullname)+1);
462 * Rebuild the users-by-number index
464 void rebuild_usersbynumber(void) {
465 cdb_trunc(CDB_USERSBYNUMBER); /* delete the old indices */
466 ForEachUser(rebuild_ubn_for_user, NULL); /* enumerate the users */
471 * getuserbyuid() Get user by system uid (for PAM mode authentication)
472 * Returns 0 if user was found
473 * This now uses an extauth index.
475 int getuserbyuid(struct ctdluser *usbuf, uid_t number) {
476 struct cdbdata *cdbextauth;
480 claimed_id = NewStrBuf();
481 StrBufPrintf(claimed_id, "uid:%d", number);
482 cdbextauth = cdb_fetch(CDB_EXTAUTH, ChrPtr(claimed_id), StrLength(claimed_id));
483 FreeStrBuf(&claimed_id);
484 if (cdbextauth == NULL) {
488 memcpy(&usernum, cdbextauth->ptr, sizeof(long));
489 cdb_free(cdbextauth);
491 if (!CtdlGetUserByNumber(usbuf, usernum)) {
500 * Back end for cmd_user() and its ilk
502 int CtdlLoginExistingUser(const char *trythisname) {
506 syslog(LOG_DEBUG, "user_ops: CtdlLoginExistingUser(%s)", trythisname);
508 if ((CC->logged_in)) {
509 return login_already_logged_in;
512 if (trythisname == NULL) return login_not_found;
514 if (!strncasecmp(trythisname, "SYS_", 4))
516 syslog(LOG_DEBUG, "user_ops: system user \"%s\" is not allowed to log in.", trythisname);
517 return login_not_found;
520 /* Continue attempting user validation... */
521 safestrncpy(username, trythisname, sizeof (username));
524 if (IsEmptyStr(username)) {
525 return login_not_found;
528 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
533 struct passwd *tempPwdPtr;
536 syslog(LOG_DEBUG, "user_ops: asking host about <%s>", username);
537 #ifdef HAVE_GETPWNAM_R
538 #ifdef SOLARIS_GETPWUID
539 syslog(LOG_DEBUG, "user_ops: calling getpwnam_r()");
540 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
541 #else // SOLARIS_GETPWUID
542 syslog(LOG_DEBUG, "user_ops: calling getpwnam_r()");
543 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
544 #endif // SOLARIS_GETPWUID
545 #else // HAVE_GETPWNAM_R
546 syslog(LOG_DEBUG, "user_ops: SHOULD NEVER GET HERE!!!");
548 #endif // HAVE_GETPWNAM_R
549 if (tempPwdPtr == NULL) {
550 syslog(LOG_DEBUG, "user_ops: no such user <%s>", username);
551 return login_not_found;
554 /* Locate the associated Citadel account. If not found, make one attempt to create it. */
555 found_user = getuserbyuid(&CC->user, pd.pw_uid);
556 if (found_user != 0) {
557 create_user(username, CREATE_USER_DO_NOT_BECOME_USER, pd.pw_uid);
558 found_user = getuserbyuid(&CC->user, pd.pw_uid);
560 syslog(LOG_DEBUG, "user_ops: found it: uid=%ld, gecos=%s here: %d", (long)pd.pw_uid, pd.pw_gecos, found_user);
564 else if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
572 found_user = CtdlTryUserLDAP(username, ldap_dn, sizeof ldap_dn, ldap_cn, sizeof ldap_cn, &ldap_uid);
573 if (found_user != 0) {
574 return login_not_found;
577 found_user = getuserbyuid(&CC->user, ldap_uid);
578 if (found_user != 0) {
579 create_user(ldap_cn, CREATE_USER_DO_NOT_BECOME_USER, ldap_uid);
580 found_user = getuserbyuid(&CC->user, ldap_uid);
583 if (found_user == 0) {
584 if (CC->ldap_dn != NULL) free(CC->ldap_dn);
585 CC->ldap_dn = strdup(ldap_dn);
591 /* native auth mode */
593 struct recptypes *valid = NULL;
595 /* First, try to log in as if the supplied name is a display name */
596 found_user = CtdlGetUser(&CC->user, username);
598 /* If that didn't work, try to log in as if the supplied name * is an e-mail address */
599 if (found_user != 0) {
600 valid = validate_recipients(username, NULL, 0);
602 if (valid->num_local == 1) {
603 found_user = CtdlGetUser(&CC->user, valid->recp_local);
605 free_recipients(valid);
610 /* Did we find something? */
611 if (found_user == 0) {
612 if (((CC->nologin)) && (CC->user.axlevel < AxAideU)) {
613 return login_too_many_users;
615 safestrncpy(CC->curr_user, CC->user.fullname, sizeof CC->curr_user);
619 return login_not_found;
623 // session startup code which is common to both cmd_pass() and cmd_newu()
624 void do_login(void) {
626 syslog(LOG_NOTICE, "user_ops: <%s> logged in", CC->curr_user);
628 CtdlGetUserLock(&CC->user, CC->curr_user);
629 ++(CC->user.timescalled);
630 CC->previous_login = CC->user.lastcall;
631 time(&CC->user.lastcall);
633 // If this user's name is the name of the system administrator
634 // (as specified in setup), automatically assign access level 6.
635 if ( (!IsEmptyStr(CtdlGetConfigStr("c_sysadm"))) && (!strcasecmp(CC->user.fullname, CtdlGetConfigStr("c_sysadm"))) ) {
636 CC->user.axlevel = AxAideU;
639 // If we're authenticating off the host system, automatically give root the highest level of access.
640 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
641 if (CC->user.uid == 0) {
642 CC->user.axlevel = AxAideU;
645 CtdlPutUserLock(&CC->user);
647 // If we are using LDAP authentication, extract the user's email addresses from the directory.
648 if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
649 char new_emailaddrs[512];
650 if (CtdlGetConfigInt("c_ldap_sync_email_addrs") > 0) {
651 if (extract_email_addresses_from_ldap(CC->ldap_dn, new_emailaddrs) == 0) {
652 CtdlSetEmailAddressesForUser(CC->user.fullname, new_emailaddrs);
657 // If the user does not have any email addresses assigned, generate one.
658 if (IsEmptyStr(CC->user.emailaddrs)) {
659 AutoGenerateEmailAddressForUser(&CC->user);
662 // Populate the user principal identity, which is consistent and never aliased
663 strcpy(CC->cs_principal_id, "");
664 makeuserkey(CC->cs_principal_id, CC->user.fullname);
665 strcat(CC->cs_principal_id, "@");
666 strcat(CC->cs_principal_id, CtdlGetConfigStr("c_fqdn"));
668 // Populate cs_inet_email and cs_inet_other_emails with valid email addresses from the user record
669 strcpy(CC->cs_inet_email, CC->user.emailaddrs);
670 char *firstsep = strstr(CC->cs_inet_email, "|");
672 strcpy(CC->cs_inet_other_emails, firstsep+1);
676 CC->cs_inet_other_emails[0] = 0;
679 // Create any personal rooms required by the system.
680 // (Technically, MAILROOM should be there already, but just in case...)
681 CtdlCreateRoom(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
682 CtdlCreateRoom(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
683 CtdlCreateRoom(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
684 CtdlCreateRoom(USERDRAFTROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
686 // Run any startup routines registered by loadable modules
687 PerformSessionHooks(EVT_LOGIN);
690 CtdlUserGoto(CtdlGetConfigStr("c_baseroom"), 0, 0, NULL, NULL, NULL, NULL);
694 void logged_in_response(void) {
695 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
696 CIT_OK, CC->user.fullname, CC->user.axlevel,
697 CC->user.timescalled, CC->user.posted,
698 CC->user.flags, CC->user.usernum,
704 void CtdlUserLogout(void) {
705 CitContext *CCC = MyContext();
707 syslog(LOG_DEBUG, "user_ops: CtdlUserLogout() logging out <%s> from session %d", CCC->curr_user, CCC->cs_pid);
709 // Run any hooks registered by modules...
710 PerformSessionHooks(EVT_LOGOUT);
712 // Clear out some session data. Most likely, the CitContext for this
713 // session is about to get nuked when the session disconnects, but
714 // since it's possible to log in again without reconnecting, we cannot
715 // make that assumption.
718 // Check to see if the user was deleted while logged in and purge them if necessary
719 if ((CCC->user.axlevel == AxDeleted) && (CCC->user.usernum)) {
720 purge_user(CCC->user.fullname);
723 // Clear out the user record in memory so we don't behave like a ghost
724 memset(&CCC->user, 0, sizeof(struct ctdluser));
725 CCC->curr_user[0] = 0;
726 CCC->cs_inet_email[0] = 0;
727 CCC->cs_inet_other_emails[0] = 0;
728 CCC->cs_inet_fn[0] = 0;
730 // Free any output buffers
736 * Validate a password on the host unix system by talking to the chkpwd daemon
738 static int validpw(uid_t uid, const char *pass)
743 if (IsEmptyStr(pass)) {
744 syslog(LOG_DEBUG, "user_ops: refusing to chkpwd for uid=%d with empty password", uid);
748 syslog(LOG_DEBUG, "user_ops: validating password for uid=%d using chkpwd...", uid);
750 begin_critical_section(S_CHKPWD);
751 rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
753 syslog(LOG_ERR, "user_ops: communication with chkpwd broken: %m");
754 end_critical_section(S_CHKPWD);
757 rv = write(chkpwd_write_pipe[1], pass, 256);
759 syslog(LOG_ERR, "user_ops: communication with chkpwd broken: %m");
760 end_critical_section(S_CHKPWD);
763 rv = read(chkpwd_read_pipe[0], buf, 4);
765 syslog(LOG_ERR, "user_ops: ommunication with chkpwd broken: %m");
766 end_critical_section(S_CHKPWD);
769 end_critical_section(S_CHKPWD);
771 if (!strncmp(buf, "PASS", 4)) {
772 syslog(LOG_DEBUG, "user_ops: chkpwd pass");
776 syslog(LOG_DEBUG, "user_ops: chkpwd fail");
782 * Start up the chkpwd daemon so validpw() has something to talk to
784 void start_chkpwd_daemon(void) {
786 struct stat filestats;
789 syslog(LOG_DEBUG, "user_ops: starting chkpwd daemon for host authentication mode");
791 if ((stat(file_chkpwd, &filestats)==-1) || (filestats.st_size==0)) {
792 syslog(LOG_ERR, "user_ops: %s: %m", file_chkpwd);
795 if (pipe(chkpwd_write_pipe) != 0) {
796 syslog(LOG_ERR, "user_ops: unable to create pipe for chkpwd daemon: %m");
799 if (pipe(chkpwd_read_pipe) != 0) {
800 syslog(LOG_ERR, "user_ops: unable to create pipe for chkpwd daemon: %m");
805 if (chkpwd_pid < 0) {
806 syslog(LOG_ERR, "user_ops: unable to fork chkpwd daemon: %m");
809 if (chkpwd_pid == 0) {
810 dup2(chkpwd_write_pipe[0], 0);
811 dup2(chkpwd_read_pipe[1], 1);
812 for (i=2; i<256; ++i) close(i);
813 execl(file_chkpwd, file_chkpwd, NULL);
814 syslog(LOG_ERR, "user_ops: unable to exec chkpwd daemon: %m");
821 int CtdlTryPassword(const char *password, long len)
824 CitContext *CCC = CC;
826 if ((CCC->logged_in)) {
827 syslog(LOG_WARNING, "user_ops: CtdlTryPassword: already logged in");
828 return pass_already_logged_in;
830 if (!strcmp(CCC->curr_user, NLI)) {
831 syslog(LOG_WARNING, "user_ops: CtdlTryPassword: no user selected");
834 if (CtdlGetUser(&CCC->user, CCC->curr_user)) {
835 syslog(LOG_ERR, "user_ops: CtdlTryPassword: internal error");
836 return pass_internal_error;
838 if (password == NULL) {
839 syslog(LOG_INFO, "user_ops: CtdlTryPassword: NULL password string supplied");
840 return pass_wrong_password;
843 else if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
847 if (validpw(CCC->user.uid, password)) {
851 * sooper-seekrit hack: populate the password field in the
852 * citadel database with the password that the user typed,
853 * if it's correct. This allows most sites to convert from
854 * host auth to native auth if they want to. If you think
855 * this is a security hazard, comment it out.
858 CtdlGetUserLock(&CCC->user, CCC->curr_user);
859 safestrncpy(CCC->user.password, password, sizeof CCC->user.password);
860 CtdlPutUserLock(&CCC->user);
863 * (sooper-seekrit hack ends here)
871 else if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
875 if ((CCC->ldap_dn) && (!CtdlTryPasswordLDAP(CCC->ldap_dn, password))) {
885 /* native auth mode */
888 pw = (char*) malloc(len + 1);
889 memcpy(pw, password, len + 1);
891 strproc(CCC->user.password);
892 code = strcasecmp(CCC->user.password, pw);
895 strproc(CCC->user.password);
896 code = strcasecmp(CCC->user.password, pw);
906 syslog(LOG_WARNING, "user_ops: bad password specified for <%s> Service <%s> Port <%ld> Remote <%s / %s>",
913 return pass_wrong_password;
919 * Delete a user record *and* all of its related resources.
921 int purge_user(char pname[])
923 struct ctdluser usbuf;
924 char usernamekey[USERNAME_SIZE];
926 makeuserkey(usernamekey, pname);
928 /* If the name is empty we can't find them in the DB any way so just return */
929 if (IsEmptyStr(pname)) {
930 return(ERROR + NO_SUCH_USER);
933 if (CtdlGetUser(&usbuf, pname) != 0) {
934 syslog(LOG_ERR, "user_ops: cannot purge user <%s> - not found", pname);
935 return(ERROR + NO_SUCH_USER);
938 /* Don't delete a user who is currently logged in. Instead, just
939 * set the access level to 0, and let the account get swept up
940 * during the next purge.
942 if (CtdlIsUserLoggedInByNum(usbuf.usernum)) {
943 syslog(LOG_WARNING, "user_ops: <%s> is logged in; not deleting", pname);
944 usbuf.axlevel = AxDeleted;
949 syslog(LOG_NOTICE, "user_ops: deleting <%s>", pname);
951 /* Perform any purge functions registered by server extensions */
952 PerformUserHooks(&usbuf, EVT_PURGEUSER);
954 /* delete any existing user/room relationships */
955 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
957 /* delete the users-by-number index record */
958 cdb_delete(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long));
960 /* delete the userlog entry */
961 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
967 int internal_create_user(char *username, struct ctdluser *usbuf, uid_t uid)
969 if (!CtdlGetUser(usbuf, username)) {
970 return(ERROR + ALREADY_EXISTS);
973 /* Go ahead and initialize a new user record */
974 memset(usbuf, 0, sizeof(struct ctdluser));
975 safestrncpy(usbuf->fullname, username, sizeof usbuf->fullname);
976 strcpy(usbuf->password, "");
979 /* These are the default flags on new accounts */
980 usbuf->flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
982 usbuf->timescalled = 0;
984 usbuf->axlevel = CtdlGetConfigInt("c_initax");
985 usbuf->lastcall = time(NULL);
987 /* fetch a new user number */
988 usbuf->usernum = get_new_user_number();
990 /* add user to the database */
992 cdb_store(CDB_USERSBYNUMBER, &usbuf->usernum, sizeof(long), usbuf->fullname, strlen(usbuf->fullname)+1);
994 /* If non-native auth, index by uid */
995 if ((usbuf->uid > 0) && (usbuf->uid != NATIVE_AUTH_UID)) {
996 StrBuf *claimed_id = NewStrBuf();
997 StrBufPrintf(claimed_id, "uid:%d", usbuf->uid);
998 attach_extauth(usbuf, claimed_id);
999 FreeStrBuf(&claimed_id);
1007 * create_user() - back end processing to create a new user
1009 * Set 'newusername' to the desired account name.
1010 * Set 'become_user' to CREATE_USER_BECOME_USER if this is self-service account creation and we want to
1011 * actually log in as the user we just created, otherwise set it to CREATE_USER_DO_NOT_BECOME_USER
1012 * Set 'uid' to some uid_t value to associate the account with an external auth user, or (-1) for native auth
1014 int create_user(char *username, int become_user, uid_t uid)
1016 struct ctdluser usbuf;
1017 struct ctdlroom qrbuf;
1018 char mailboxname[ROOMNAMELEN];
1023 if ((retval = internal_create_user(username, &usbuf, uid)) != 0) {
1028 * Give the user a private mailbox and a configuration room.
1029 * Make the latter an invisible system room.
1031 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
1032 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
1034 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
1035 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
1036 if (CtdlGetRoomLock(&qrbuf, mailboxname) == 0) {
1037 qrbuf.QRflags2 |= QR2_SYSTEM;
1038 CtdlPutRoomLock(&qrbuf);
1041 /* Perform any create functions registered by server extensions */
1042 PerformUserHooks(&usbuf, EVT_NEWUSER);
1044 /* Everything below this line can be bypassed if administratively
1045 * creating a user, instead of doing self-service account creation
1048 if (become_user == CREATE_USER_BECOME_USER) {
1049 /* Now become the user we just created */
1050 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
1051 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
1054 /* Check to make sure we're still who we think we are */
1055 if (CtdlGetUser(&CC->user, CC->curr_user)) {
1056 return(ERROR + INTERNAL_ERROR);
1061 "New user account <%s> has been created, from host %s [%s].\n",
1066 CtdlAideMessage(buf, "User Creation Notice");
1067 syslog(LOG_NOTICE, "user_ops: <%s> created", username);
1073 * set password - back end api code
1075 void CtdlSetPassword(char *new_pw)
1077 CtdlGetUserLock(&CC->user, CC->curr_user);
1078 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1079 CtdlPutUserLock(&CC->user);
1080 syslog(LOG_INFO, "user_ops: password changed for <%s>", CC->curr_user);
1081 PerformSessionHooks(EVT_SETPASS);
1086 * API function for cmd_invt_kick() and anything else that needs to
1087 * invite or kick out a user to/from a room.
1089 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1091 int CtdlInvtKick(char *iuser, int op) {
1092 struct ctdluser USscratch;
1096 if (CtdlGetUser(&USscratch, iuser) != 0) {
1100 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1102 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1103 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1106 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1107 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1109 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1111 /* post a message in Aide> saying what we just did */
1112 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1114 ((op == 1) ? "invited to" : "kicked out of"),
1116 (CC->logged_in ? CC->user.fullname : "an administrator")
1118 CtdlAideMessage(bbb,"User Admin Message");
1124 * Forget (Zap) the current room (API call)
1125 * Returns 0 on success
1127 int CtdlForgetThisRoom(void) {
1130 /* On some systems, Admins are not allowed to forget rooms */
1131 if (is_aide() && (CtdlGetConfigInt("c_aide_zap") == 0)
1132 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1136 CtdlGetUserLock(&CC->user, CC->curr_user);
1137 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1139 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1140 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1142 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1143 CtdlPutUserLock(&CC->user);
1145 /* Return to the Lobby, so we don't end up in an undefined room */
1146 CtdlUserGoto(CtdlGetConfigStr("c_baseroom"), 0, 0, NULL, NULL, NULL, NULL);
1152 * Traverse the user file and perform a callback for each user record.
1153 * (New improved version that runs in two phases so that callbacks can perform writes without having a r/o cursor open)
1155 void ForEachUser(void (*CallBack) (char *, void *out_data), void *in_data)
1157 struct cdbdata *cdbus;
1158 struct ctdluser *usptr;
1162 char username[USERNAME_SIZE];
1164 struct feu *ufirst = NULL;
1165 struct feu *ulast = NULL;
1166 struct feu *f = NULL;
1168 cdb_rewind(CDB_USERS);
1170 // Phase 1 : build a linked list of all our user account names
1171 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1172 usptr = (struct ctdluser *) cdbus->ptr;
1174 if (strlen(usptr->fullname) > 0) {
1175 f = malloc(sizeof(struct feu));
1177 strncpy(f->username, usptr->fullname, USERNAME_SIZE);
1179 if (ufirst == NULL) {
1190 // Phase 2 : perform the callback for each user while de-allocating the list
1191 while (ufirst != NULL) {
1192 (*CallBack) (ufirst->username, in_data);
1194 ufirst = ufirst->next;
1201 * Count the number of new mail messages the user has
1205 int num_newmsgs = 0;
1206 num_newmsgs = CC->newmail;
1208 return(num_newmsgs);
1213 * Count the number of new mail messages the user has
1215 int InitialMailCheck()
1217 int num_newmsgs = 0;
1219 char mailboxname[ROOMNAMELEN];
1220 struct ctdlroom mailbox;
1222 struct cdbdata *cdbfr;
1223 long *msglist = NULL;
1226 CtdlMailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1227 if (CtdlGetRoom(&mailbox, mailboxname) != 0)
1229 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1231 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1233 if (cdbfr != NULL) {
1234 msglist = malloc(cdbfr->len);
1235 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1236 num_msgs = cdbfr->len / sizeof(long);
1240 for (a = 0; a < num_msgs; ++a) {
1241 if (msglist[a] > 0L) {
1242 if (msglist[a] > vbuf.v_lastseen) {
1247 if (msglist != NULL)
1250 return(num_newmsgs);