1 // Server functions which perform operations on user objects.
3 // Copyright (c) 1987-2022 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];
33 // makeuserkey() - convert a username into the format used as a database key
34 // "key" must be a buffer of at least USERNAME_SIZE
35 // (Key format is the username with all non-alphanumeric characters removed, and converted to lower case.)
36 void makeuserkey(char *key, const char *username) {
40 if (IsEmptyStr(username)) {
45 int len = strlen(username);
46 for (i=0; ((i<=len) && (i<USERNAME_SIZE-1)); ++i) {
47 if (isalnum((username[i]))) {
48 key[keylen++] = tolower(username[i]);
55 // Compare two usernames to see if they are the same user after being keyed for the database
56 // Usage is identical to strcmp()
57 int CtdlUserCmp(char *s1, char *s2) {
58 char k1[USERNAME_SIZE];
59 char k2[USERNAME_SIZE];
63 return(strcmp(k1,k2));
67 // CtdlGetUser() retrieve named user into supplied buffer.
68 // returns 0 on success
69 int CtdlGetUser(struct ctdluser *usbuf, char *name) {
70 char usernamekey[USERNAME_SIZE];
71 struct cdbdata *cdbus;
74 memset(usbuf, 0, sizeof(struct ctdluser));
77 makeuserkey(usernamekey, name);
78 if (IsEmptyStr(usernamekey)) {
79 return(1); // empty user name
81 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
83 if (cdbus == NULL) { // user not found
87 memcpy(usbuf, cdbus->ptr, ((cdbus->len > sizeof(struct ctdluser)) ? sizeof(struct ctdluser) : cdbus->len));
94 int CtdlLockGetCurrentUser(void) {
95 return CtdlGetUser(&CC->user, CC->curr_user);
99 // CtdlGetUserLock() - same as getuser() but locks the record
100 int CtdlGetUserLock(struct ctdluser *usbuf, char *name) {
103 retcode = CtdlGetUser(usbuf, name);
105 begin_critical_section(S_USERS);
111 // CtdlPutUser() - write user buffer into the correct place on disk
112 void CtdlPutUser(struct ctdluser *usbuf) {
113 char usernamekey[USERNAME_SIZE];
114 makeuserkey(usernamekey, usbuf->fullname);
115 usbuf->version = REV_LEVEL;
116 cdb_store(CDB_USERS, usernamekey, strlen(usernamekey), usbuf, sizeof(struct ctdluser));
120 void CtdlPutCurrentUserLock() {
121 CtdlPutUser(&CC->user);
125 // CtdlPutUserLock() - same as putuser() but locks the record
126 void CtdlPutUserLock(struct ctdluser *usbuf) {
128 end_critical_section(S_USERS);
132 // rename_user() - this is tricky because the user's display name is the database key
133 // Returns 0 on success or nonzero if there was an error...
134 int rename_user(char *oldname, char *newname) {
135 int retcode = RENAMEUSER_OK;
136 struct ctdluser usbuf;
138 char oldnamekey[USERNAME_SIZE];
139 char newnamekey[USERNAME_SIZE];
141 // Create the database keys...
142 makeuserkey(oldnamekey, oldname);
143 makeuserkey(newnamekey, newname);
145 // Lock up and get going
146 begin_critical_section(S_USERS);
148 // We cannot rename a user who is currently logged in
149 if (CtdlIsUserLoggedIn(oldname)) {
150 end_critical_section(S_USERS);
151 return RENAMEUSER_LOGGED_IN;
154 if (CtdlGetUser(&usbuf, newname) == 0) {
155 retcode = RENAMEUSER_ALREADY_EXISTS;
159 if (CtdlGetUser(&usbuf, oldname) != 0) {
160 retcode = RENAMEUSER_NOT_FOUND;
162 else { // Sanity checks succeeded. Now rename the user.
163 if (usbuf.usernum == 0) {
164 syslog(LOG_DEBUG, "user_ops: can not rename user \"Citadel\".");
165 retcode = RENAMEUSER_NOT_FOUND;
168 syslog(LOG_DEBUG, "user_ops: renaming <%s> to <%s>", oldname, newname);
169 cdb_delete(CDB_USERS, oldnamekey, strlen(oldnamekey));
170 safestrncpy(usbuf.fullname, newname, sizeof usbuf.fullname);
172 cdb_store(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long), usbuf.fullname, strlen(usbuf.fullname)+1 );
173 retcode = RENAMEUSER_OK;
179 end_critical_section(S_USERS);
184 // Convert a username into the format used as a database key prior to version 928
185 // This only gets called by reindex_user_928()
186 void makeuserkey_pre928(char *key, const char *username) {
189 int len = strlen(username);
191 if (len >= USERNAME_SIZE) {
192 syslog(LOG_INFO, "Username too long: %s", username);
193 len = USERNAME_SIZE - 1;
195 for (i=0; i<=len; ++i) {
196 key[i] = tolower(username[i]);
201 // Read a user record using the pre-v928 index format, and write it back using the v928-and-higher index format.
202 // This ONLY gets called during an upgrade from version <928 to version >=928.
203 void reindex_user_928(char *username, void *out_data) {
205 char oldkey[USERNAME_SIZE];
206 char newkey[USERNAME_SIZE];
207 struct cdbdata *cdbus;
208 struct ctdluser usbuf;
210 makeuserkey_pre928(oldkey, username);
211 makeuserkey(newkey, username);
213 syslog(LOG_DEBUG, "user_ops: reindex_user_928: %s <%s> --> <%s>", username, oldkey, newkey);
215 // Fetch the user record using the old index format
216 cdbus = cdb_fetch(CDB_USERS, oldkey, strlen(oldkey));
218 syslog(LOG_INFO, "user_ops: <%s> not found, were they already reindexed?", username);
221 memcpy(&usbuf, cdbus->ptr, ((cdbus->len > sizeof(struct ctdluser)) ? sizeof(struct ctdluser) : cdbus->len));
224 // delete the old record
225 cdb_delete(CDB_USERS, oldkey, strlen(oldkey));
227 // write the new record
228 cdb_store(CDB_USERS, newkey, strlen(newkey), &usbuf, sizeof(struct ctdluser));
232 // Index-generating function used by Ctdl[Get|Set]Relationship
233 int GenerateRelationshipIndex(char *IndexBuf,
244 TheIndex.iRoomID = RoomID;
245 TheIndex.iRoomGen = RoomGen;
246 TheIndex.iUserID = UserID;
248 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
249 return(sizeof(TheIndex));
253 // Back end for CtdlSetRelationship()
254 void put_visit(visit *newvisit) {
258 memset (IndexBuf, 0, sizeof (IndexBuf));
260 IndexLen = GenerateRelationshipIndex(IndexBuf, newvisit->v_roomnum, newvisit->v_roomgen, newvisit->v_usernum);
263 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
264 newvisit, sizeof(visit)
269 // Define a relationship between a user and a room
270 void CtdlSetRelationship(visit *newvisit, struct ctdluser *rel_user, struct ctdlroom *rel_room) {
271 // We don't use these in Citadel because they're implicit by the
272 // index, but they must be present if the database is exported.
273 newvisit->v_roomnum = rel_room->QRnumber;
274 newvisit->v_roomgen = rel_room->QRgen;
275 newvisit->v_usernum = rel_user->usernum;
281 // Locate a relationship between a user and a room
282 void CtdlGetRelationship(visit *vbuf, struct ctdluser *rel_user, struct ctdlroom *rel_room) {
285 struct cdbdata *cdbvisit;
288 IndexLen = GenerateRelationshipIndex(IndexBuf, rel_room->QRnumber, rel_room->QRgen, rel_user->usernum);
290 // Clear out the buffer
291 memset(vbuf, 0, sizeof(visit));
293 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
294 if (cdbvisit != NULL) {
295 memcpy(vbuf, cdbvisit->ptr, ((cdbvisit->len > sizeof(visit)) ? sizeof(visit) : cdbvisit->len));
299 // If this is the first time the user has seen this room, set the view to be the default for the room.
300 vbuf->v_view = rel_room->QRdefaultview;
303 // Set v_seen if necessary
304 if (vbuf->v_seen[0] == 0) {
305 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
310 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix) {
311 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
315 // Check to see if the specified user has Internet mail permission
316 // (returns nonzero if permission is granted)
317 int CtdlCheckInternetMailPermission(struct ctdluser *who) {
319 // Do not allow twits to send Internet mail
320 if (who->axlevel <= AxProbU) return(0);
323 if (CtdlGetConfigInt("c_restrict") == 0) return(1);
326 if (who->flags & US_INTERNET) return(2);
328 // Admin level access?
329 if (who->axlevel >= AxAideU) return(3);
336 // This is a convenience function which follows the Citadel protocol semantics for most commands.
337 // If the current user does not have the requested access level, it outputs a protocol-friendly error message
338 // and then returns (-1). This allows calling functions to complete an access level check in one line of code.
339 int CtdlAccessCheck(int required_level) {
340 if (CC->internal_pgm) return(0);
341 if (required_level >= ac_internal) {
342 cprintf("%d This is not a user-level command.\n", ERROR + HIGHER_ACCESS_REQUIRED);
346 if ((required_level >= ac_logged_in_or_guest) && (CC->logged_in == 0) && (CtdlGetConfigInt("c_guest_logins") == 0)) {
347 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
351 if ((required_level >= ac_logged_in) && (CC->logged_in == 0)) {
352 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
356 if (CC->user.axlevel >= AxAideU) return(0);
357 if (required_level >= ac_aide) {
358 cprintf("%d This command requires Admin access.\n",
359 ERROR + HIGHER_ACCESS_REQUIRED);
363 if (is_room_aide()) return(0);
364 if (required_level >= ac_room_aide) {
365 cprintf("%d This command requires Admin or Room Admin access.\n",
366 ERROR + HIGHER_ACCESS_REQUIRED);
370 // Do not generate any output if we succeeded -- the calling function will handle that.
375 // Is the user currently logged in an Admin?
377 if (CC->user.axlevel >= AxAideU) {
386 // Is the user currently logged in an Admin *or* the room Admin for this room?
387 int is_room_aide(void) {
388 if (!CC->logged_in) {
392 if ((CC->user.axlevel >= AxAideU) || (CC->room.QRroomaide == CC->user.usernum)) {
401 // CtdlGetUserByNumber() - get user by number, returns 0 if user was found
402 // Note: fetching a user this way requires one additional database operation.
403 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number) {
404 struct cdbdata *cdbun;
407 cdbun = cdb_fetch(CDB_USERSBYNUMBER, &number, sizeof(long));
409 syslog(LOG_INFO, "user_ops: %ld not found", number);
413 syslog(LOG_INFO, "user_ops: %ld maps to %s", number, cdbun->ptr);
414 r = CtdlGetUser(usbuf, cdbun->ptr);
420 // Helper function for rebuild_usersbynumber()
421 void rebuild_ubn_for_user(char *username, void *data) {
424 syslog(LOG_DEBUG, "user_ops: rebuilding usersbynumber index for %s", username);
425 if (CtdlGetUser(&u, username) == 0) {
426 cdb_store(CDB_USERSBYNUMBER, &(u.usernum), sizeof(long), u.fullname, strlen(u.fullname)+1);
431 // Rebuild the users-by-number index
432 void rebuild_usersbynumber(void) {
433 cdb_trunc(CDB_USERSBYNUMBER); // delete the old indices
434 ForEachUser(rebuild_ubn_for_user, NULL); // enumerate the users
438 // getuserbyuid() Get user by system uid (for PAM mode authentication)
439 // Returns 0 if user was found
440 // This now uses an extauth index.
441 int getuserbyuid(struct ctdluser *usbuf, uid_t number) {
442 struct cdbdata *cdbextauth;
446 claimed_id = NewStrBuf();
447 StrBufPrintf(claimed_id, "uid:%d", number);
448 cdbextauth = cdb_fetch(CDB_EXTAUTH, ChrPtr(claimed_id), StrLength(claimed_id));
449 FreeStrBuf(&claimed_id);
450 if (cdbextauth == NULL) {
454 memcpy(&usernum, cdbextauth->ptr, sizeof(long));
455 cdb_free(cdbextauth);
457 if (!CtdlGetUserByNumber(usbuf, usernum)) {
465 // Back end for cmd_user() and its ilk
466 int CtdlLoginExistingUser(const char *trythisname) {
470 syslog(LOG_DEBUG, "user_ops: CtdlLoginExistingUser(%s)", trythisname);
472 if ((CC->logged_in)) {
473 return login_already_logged_in;
476 if (trythisname == NULL) return login_not_found;
478 if (!strncasecmp(trythisname, "SYS_", 4)) {
479 syslog(LOG_DEBUG, "user_ops: system user \"%s\" is not allowed to log in.", trythisname);
480 return login_not_found;
483 // Continue attempting user validation...
484 safestrncpy(username, trythisname, sizeof (username));
487 if (IsEmptyStr(username)) {
488 return login_not_found;
492 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
494 struct passwd *tempPwdPtr;
497 syslog(LOG_DEBUG, "user_ops: asking host about <%s>", username);
498 #ifdef HAVE_GETPWNAM_R
499 #ifdef SOLARIS_GETPWUID
500 syslog(LOG_DEBUG, "user_ops: calling getpwnam_r()");
501 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
502 #else // SOLARIS_GETPWUID
503 syslog(LOG_DEBUG, "user_ops: calling getpwnam_r()");
504 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
505 #endif // SOLARIS_GETPWUID
506 #else // HAVE_GETPWNAM_R
507 syslog(LOG_DEBUG, "user_ops: SHOULD NEVER GET HERE!!!");
509 #endif // HAVE_GETPWNAM_R
510 if (tempPwdPtr == NULL) {
511 syslog(LOG_DEBUG, "user_ops: no such user <%s>", username);
512 return login_not_found;
515 // Locate the associated Citadel account. If not found, make one attempt to create it.
516 found_user = getuserbyuid(&CC->user, pd.pw_uid);
517 if (found_user != 0) {
518 create_user(username, CREATE_USER_DO_NOT_BECOME_USER, pd.pw_uid);
519 found_user = getuserbyuid(&CC->user, pd.pw_uid);
521 syslog(LOG_DEBUG, "user_ops: found it: uid=%ld, gecos=%s here: %d", (long)pd.pw_uid, pd.pw_gecos, found_user);
526 else if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
532 found_user = CtdlTryUserLDAP(username, ldap_dn, sizeof ldap_dn, ldap_cn, sizeof ldap_cn, &ldap_uid);
533 if (found_user != 0) {
534 return login_not_found;
537 found_user = getuserbyuid(&CC->user, ldap_uid);
538 if (found_user != 0) {
539 create_user(ldap_cn, CREATE_USER_DO_NOT_BECOME_USER, ldap_uid);
540 found_user = getuserbyuid(&CC->user, ldap_uid);
543 if (found_user == 0) {
544 if (CC->ldap_dn != NULL) free(CC->ldap_dn);
545 CC->ldap_dn = strdup(ldap_dn);
550 // native auth mode...
552 struct recptypes *valid = NULL;
554 // First, try to log in as if the supplied name is a display name
555 found_user = CtdlGetUser(&CC->user, username);
557 // If that didn't work, try to log in as if the supplied name * is an e-mail address
558 if (found_user != 0) {
559 valid = validate_recipients(username, NULL, 0);
561 if (valid->num_local == 1) {
562 found_user = CtdlGetUser(&CC->user, valid->recp_local);
564 free_recipients(valid);
569 // User 0 is a system account and must not be used by a real user
570 if (&CC->user.usernum <= 0) {
571 return login_not_found;
574 // Did we find something?
575 if (found_user == 0) {
576 if (((CC->nologin)) && (CC->user.axlevel < AxAideU)) {
577 return login_too_many_users;
580 safestrncpy(CC->curr_user, CC->user.fullname, sizeof CC->curr_user);
584 return login_not_found;
588 // session startup code which is common to both cmd_pass() and cmd_newu()
589 void do_login(void) {
591 syslog(LOG_NOTICE, "user_ops: <%s> logged in", CC->curr_user);
593 CtdlGetUserLock(&CC->user, CC->curr_user);
594 ++(CC->user.timescalled);
595 CC->previous_login = CC->user.lastcall;
596 time(&CC->user.lastcall);
598 // If this user's name is the name of the system administrator
599 // (as specified in setup), automatically assign access level 6.
600 if ( (!IsEmptyStr(CtdlGetConfigStr("c_sysadm"))) && (!strcasecmp(CC->user.fullname, CtdlGetConfigStr("c_sysadm"))) ) {
601 CC->user.axlevel = AxAideU;
604 // If we're authenticating off the host system, automatically give root the highest level of access.
605 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
606 if (CC->user.uid == 0) {
607 CC->user.axlevel = AxAideU;
610 CtdlPutUserLock(&CC->user);
612 // If we are using LDAP authentication, extract the user's email addresses from the directory.
613 if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
614 char new_emailaddrs[512];
615 if (CtdlGetConfigInt("c_ldap_sync_email_addrs") > 0) {
616 if (extract_email_addresses_from_ldap(CC->ldap_dn, new_emailaddrs) == 0) {
617 CtdlSetEmailAddressesForUser(CC->user.fullname, new_emailaddrs);
622 // If the user does not have any email addresses assigned, generate one.
623 if (IsEmptyStr(CC->user.emailaddrs)) {
624 AutoGenerateEmailAddressForUser(&CC->user);
627 // Populate the user principal identity, which is consistent and never aliased
628 strcpy(CC->cs_principal_id, "");
629 makeuserkey(CC->cs_principal_id, CC->user.fullname);
630 strcat(CC->cs_principal_id, "@");
631 strcat(CC->cs_principal_id, CtdlGetConfigStr("c_fqdn"));
633 // Populate cs_inet_email and cs_inet_other_emails with valid email addresses from the user record
634 strcpy(CC->cs_inet_email, CC->user.emailaddrs);
635 char *firstsep = strstr(CC->cs_inet_email, "|");
637 strcpy(CC->cs_inet_other_emails, firstsep+1);
641 CC->cs_inet_other_emails[0] = 0;
644 // Create any personal rooms required by the system.
645 // (Technically, MAILROOM should be there already, but just in case...)
646 CtdlCreateRoom(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
647 CtdlCreateRoom(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
648 CtdlCreateRoom(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
649 CtdlCreateRoom(USERDRAFTROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
651 // Run any startup routines registered by loadable modules
652 PerformSessionHooks(EVT_LOGIN);
655 CtdlUserGoto(CtdlGetConfigStr("c_baseroom"), 0, 0, NULL, NULL, NULL, NULL);
659 void logged_in_response(void) {
660 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
661 CIT_OK, CC->user.fullname, CC->user.axlevel,
662 CC->user.timescalled, CC->user.posted,
663 CC->user.flags, CC->user.usernum,
669 void CtdlUserLogout(void) {
671 syslog(LOG_DEBUG, "user_ops: CtdlUserLogout() logging out <%s> from session %d", CC->curr_user, CC->cs_pid);
673 // Run any hooks registered by modules...
674 PerformSessionHooks(EVT_LOGOUT);
676 // Clear out some session data. Most likely, the CitContext for this
677 // session is about to get nuked when the session disconnects, but
678 // since it's possible to log in again without reconnecting, we cannot
679 // make that assumption.
682 // Check to see if the user was deleted while logged in and purge them if necessary
683 if ((CC->user.axlevel == AxDeleted) && (CC->user.usernum)) {
684 purge_user(CC->user.fullname);
687 // Clear out the user record in memory so we don't behave like a ghost
688 memset(&CC->user, 0, sizeof(struct ctdluser));
689 CC->curr_user[0] = 0;
690 CC->cs_inet_email[0] = 0;
691 CC->cs_inet_other_emails[0] = 0;
692 CC->cs_inet_fn[0] = 0;
694 // Free any output buffers
699 // Validate a password on the host unix system by talking to the chkpwd daemon
700 static int validpw(uid_t uid, const char *pass) {
704 if (IsEmptyStr(pass)) {
705 syslog(LOG_DEBUG, "user_ops: refusing to chkpwd for uid=%d with empty password", uid);
709 syslog(LOG_DEBUG, "user_ops: validating password for uid=%d using chkpwd...", uid);
711 begin_critical_section(S_CHKPWD);
712 rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
714 syslog(LOG_ERR, "user_ops: communication with chkpwd broken: %m");
715 end_critical_section(S_CHKPWD);
718 rv = write(chkpwd_write_pipe[1], pass, 256);
720 syslog(LOG_ERR, "user_ops: communication with chkpwd broken: %m");
721 end_critical_section(S_CHKPWD);
724 rv = read(chkpwd_read_pipe[0], buf, 4);
726 syslog(LOG_ERR, "user_ops: ommunication with chkpwd broken: %m");
727 end_critical_section(S_CHKPWD);
730 end_critical_section(S_CHKPWD);
732 if (!strncmp(buf, "PASS", 4)) {
733 syslog(LOG_DEBUG, "user_ops: chkpwd pass");
737 syslog(LOG_DEBUG, "user_ops: chkpwd fail");
742 // Start up the chkpwd daemon so validpw() has something to talk to
743 void start_chkpwd_daemon(void) {
745 struct stat filestats;
748 syslog(LOG_DEBUG, "user_ops: starting chkpwd daemon for host authentication mode");
750 if ((stat(file_chkpwd, &filestats)==-1) || (filestats.st_size==0)) {
751 syslog(LOG_ERR, "user_ops: %s: %m", file_chkpwd);
754 if (pipe(chkpwd_write_pipe) != 0) {
755 syslog(LOG_ERR, "user_ops: unable to create pipe for chkpwd daemon: %m");
758 if (pipe(chkpwd_read_pipe) != 0) {
759 syslog(LOG_ERR, "user_ops: unable to create pipe for chkpwd daemon: %m");
764 if (chkpwd_pid < 0) {
765 syslog(LOG_ERR, "user_ops: unable to fork chkpwd daemon: %m");
768 if (chkpwd_pid == 0) {
769 dup2(chkpwd_write_pipe[0], 0);
770 dup2(chkpwd_read_pipe[1], 1);
771 for (i=2; i<256; ++i) close(i);
772 execl(file_chkpwd, file_chkpwd, NULL);
773 syslog(LOG_ERR, "user_ops: unable to exec chkpwd daemon: %m");
780 int CtdlTryPassword(const char *password, long len) {
783 if ((CC->logged_in)) {
784 syslog(LOG_WARNING, "user_ops: CtdlTryPassword: already logged in");
785 return pass_already_logged_in;
787 if (!strcmp(CC->curr_user, NLI)) {
788 syslog(LOG_WARNING, "user_ops: CtdlTryPassword: no user selected");
791 if (CtdlGetUser(&CC->user, CC->curr_user)) {
792 syslog(LOG_ERR, "user_ops: CtdlTryPassword: internal error");
793 return pass_internal_error;
795 if (password == NULL) {
796 syslog(LOG_INFO, "user_ops: CtdlTryPassword: NULL password string supplied");
797 return pass_wrong_password;
801 else if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
802 if (validpw(CC->user.uid, password)) {
805 // sooper-seekrit hack: populate the password field in the
806 // citadel database with the password that the user typed,
807 // if it's correct. This allows most sites to convert from
808 // host auth to native auth if they want to. If you think
809 // this is a security hazard, comment it out.
811 CtdlGetUserLock(&CC->user, CC->curr_user);
812 safestrncpy(CC->user.password, password, sizeof CC->user.password);
813 CtdlPutUserLock(&CC->user);
815 // (sooper-seekrit hack ends here)
823 else if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
825 if ((CC->ldap_dn) && (!CtdlTryPasswordLDAP(CC->ldap_dn, password))) {
833 // native auth mode...
837 pw = (char*) malloc(len + 1);
838 memcpy(pw, password, len + 1);
840 strproc(CC->user.password);
841 code = strcasecmp(CC->user.password, pw);
844 strproc(CC->user.password);
845 code = strcasecmp(CC->user.password, pw);
855 syslog(LOG_WARNING, "user_ops: bad password specified for <%s> Service <%s> Port <%ld> Remote <%s / %s>",
862 return pass_wrong_password;
867 // Delete a user record *and* all of its related resources.
868 int purge_user(char pname[]) {
869 struct ctdluser usbuf;
870 char usernamekey[USERNAME_SIZE];
872 makeuserkey(usernamekey, pname);
874 // If the name is empty we can't find them in the DB any way so just return
875 if (IsEmptyStr(pname)) {
876 return(ERROR + NO_SUCH_USER);
879 if (CtdlGetUser(&usbuf, pname) != 0) {
880 syslog(LOG_ERR, "user_ops: cannot purge user <%s> - not found", pname);
881 return(ERROR + NO_SUCH_USER);
884 // Don't delete a user who is currently logged in. Instead, just
885 // set the access level to 0, and let the account get swept up
886 // during the next purge.
887 if (CtdlIsUserLoggedInByNum(usbuf.usernum)) {
888 syslog(LOG_WARNING, "user_ops: <%s> is logged in; not deleting", pname);
889 usbuf.axlevel = AxDeleted;
894 syslog(LOG_NOTICE, "user_ops: deleting <%s>", pname);
896 // Perform any purge functions registered by server extensions
897 PerformUserHooks(&usbuf, EVT_PURGEUSER);
899 // delete any existing user/room relationships
900 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
902 // delete the users-by-number index record
903 cdb_delete(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long));
905 // delete the user entry
906 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
912 // This is the back end processing that happens when we create a new user account.
913 int internal_create_user(char *username, struct ctdluser *usbuf, uid_t uid) {
914 if (!CtdlGetUser(usbuf, username)) {
915 return(ERROR + ALREADY_EXISTS);
918 // Go ahead and initialize a new user record
919 memset(usbuf, 0, sizeof(struct ctdluser));
920 safestrncpy(usbuf->fullname, username, sizeof usbuf->fullname);
921 strcpy(usbuf->password, "");
924 // These are the default flags on new accounts
925 usbuf->flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
927 usbuf->timescalled = 0;
929 usbuf->axlevel = CtdlGetConfigInt("c_initax");
930 usbuf->lastcall = time(NULL);
932 // fetch a new user number
933 usbuf->usernum = get_new_user_number();
935 // add user to the database
937 cdb_store(CDB_USERSBYNUMBER, &usbuf->usernum, sizeof(long), usbuf->fullname, strlen(usbuf->fullname)+1);
939 // If non-native auth, index by uid
940 if ((usbuf->uid > 0) && (usbuf->uid != NATIVE_AUTH_UID)) {
941 StrBuf *claimed_id = NewStrBuf();
942 StrBufPrintf(claimed_id, "uid:%d", usbuf->uid);
943 attach_extauth(usbuf, claimed_id);
944 FreeStrBuf(&claimed_id);
951 // create_user() - back end processing to create a new user
953 // Set 'newusername' to the desired account name.
954 // Set 'become_user' to CREATE_USER_BECOME_USER if this is self-service account creation and we want to
955 // actually log in as the user we just created, otherwise set it to CREATE_USER_DO_NOT_BECOME_USER
956 // Set 'uid' to some uid_t value to associate the account with an external auth user, or (-1) for native auth
957 int create_user(char *username, int become_user, uid_t uid) {
958 struct ctdluser usbuf;
959 struct ctdlroom qrbuf;
960 char mailboxname[ROOMNAMELEN];
965 if ((retval = internal_create_user(username, &usbuf, uid)) != 0) {
969 // Give the user a private mailbox and a configuration room.
970 // Make the latter an invisible system room.
971 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
972 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
974 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
975 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
976 if (CtdlGetRoomLock(&qrbuf, mailboxname) == 0) {
977 qrbuf.QRflags2 |= QR2_SYSTEM;
978 CtdlPutRoomLock(&qrbuf);
981 // Perform any create functions registered by server extensions
982 PerformUserHooks(&usbuf, EVT_NEWUSER);
984 // Everything below this line can be bypassed if administratively
985 // creating a user, instead of doing self-service account creation
987 if (become_user == CREATE_USER_BECOME_USER) {
988 // Now become the user we just created
989 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
990 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
993 // Check to make sure we're still who we think we are
994 if (CtdlGetUser(&CC->user, CC->curr_user)) {
995 return(ERROR + INTERNAL_ERROR);
1000 "New user account <%s> has been created, from host %s [%s].\n",
1005 CtdlAideMessage(buf, "User Creation Notice");
1006 syslog(LOG_NOTICE, "user_ops: <%s> created", username);
1011 // set password - back end api code
1012 void CtdlSetPassword(char *new_pw) {
1013 CtdlGetUserLock(&CC->user, CC->curr_user);
1014 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1015 CtdlPutUserLock(&CC->user);
1016 syslog(LOG_INFO, "user_ops: password changed for <%s>", CC->curr_user);
1017 PerformSessionHooks(EVT_SETPASS);
1021 // API function for cmd_invt_kick() and anything else that needs to
1022 // invite or kick out a user to/from a room.
1024 // Set iuser to the name of the user, and op to 1=invite or 0=kick
1025 int CtdlInvtKick(char *iuser, int op) {
1026 struct ctdluser USscratch;
1030 if (CtdlGetUser(&USscratch, iuser) != 0) {
1034 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1036 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1037 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1040 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1041 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1043 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1045 // post a message in Aide> saying what we just did
1046 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1048 ((op == 1) ? "invited to" : "kicked out of"),
1050 (CC->logged_in ? CC->user.fullname : "an administrator")
1052 CtdlAideMessage(bbb,"User Admin Message");
1057 // Forget (Zap) the current room (API call)
1058 // Returns 0 on success
1059 int CtdlForgetThisRoom(void) {
1062 // On some systems, Admins are not allowed to forget rooms
1063 if (is_aide() && (CtdlGetConfigInt("c_aide_zap") == 0)
1064 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1068 CtdlGetUserLock(&CC->user, CC->curr_user);
1069 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1071 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1072 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1074 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1075 CtdlPutUserLock(&CC->user);
1077 // Return to the Lobby, so we don't end up in an undefined room
1078 CtdlUserGoto(CtdlGetConfigStr("c_baseroom"), 0, 0, NULL, NULL, NULL, NULL);
1083 // Traverse the user file and perform a callback for each user record.
1084 // (New improved version that runs in two phases so that callbacks can perform writes without having a r/o cursor open)
1085 void ForEachUser(void (*CallBack) (char *, void *out_data), void *in_data) {
1086 struct cdbdata *cdbus;
1087 struct ctdluser *usptr;
1091 char username[USERNAME_SIZE];
1093 struct feu *ufirst = NULL;
1094 struct feu *ulast = NULL;
1095 struct feu *f = NULL;
1097 cdb_rewind(CDB_USERS);
1099 // Phase 1 : build a linked list of all our user account names
1100 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1101 usptr = (struct ctdluser *) cdbus->ptr;
1103 if (strlen(usptr->fullname) > 0) {
1104 f = malloc(sizeof(struct feu));
1106 strncpy(f->username, usptr->fullname, USERNAME_SIZE);
1108 if (ufirst == NULL) {
1119 // Phase 2 : perform the callback for each user while de-allocating the list
1120 while (ufirst != NULL) {
1121 (*CallBack) (ufirst->username, in_data);
1123 ufirst = ufirst->next;
1129 // Count the number of new mail messages the user has
1130 int NewMailCount() {
1131 int num_newmsgs = 0;
1132 num_newmsgs = CC->newmail;
1134 return(num_newmsgs);
1138 // Count the number of new mail messages the user has
1139 int InitialMailCheck() {
1140 int num_newmsgs = 0;
1142 char mailboxname[ROOMNAMELEN];
1143 struct ctdlroom mailbox;
1145 struct cdbdata *cdbfr;
1146 long *msglist = NULL;
1149 CtdlMailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1150 if (CtdlGetRoom(&mailbox, mailboxname) != 0)
1152 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1154 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1156 if (cdbfr != NULL) {
1157 msglist = malloc(cdbfr->len);
1158 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1159 num_msgs = cdbfr->len / sizeof(long);
1163 for (a = 0; a < num_msgs; ++a) {
1164 if (msglist[a] > 0L) {
1165 if (msglist[a] > vbuf.v_lastseen) {
1170 if (msglist != NULL)
1173 return(num_newmsgs);