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);
701 * Populate cs_inet_email and cs_inet_other_emails with valid email addresses from the user record
703 strcpy(CC->cs_inet_email, CC->user.emailaddrs);
704 char *firstsep = strstr(CC->cs_inet_email, "|");
706 strcpy(CC->cs_inet_other_emails, firstsep+1);
710 CC->cs_inet_other_emails[0] = 0;
713 /* Create any personal rooms required by the system.
714 * (Technically, MAILROOM should be there already, but just in case...)
716 CtdlCreateRoom(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
717 CtdlCreateRoom(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
718 CtdlCreateRoom(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
719 CtdlCreateRoom(USERDRAFTROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
721 /* Run any startup routines registered by loadable modules */
722 PerformSessionHooks(EVT_LOGIN);
724 /* Enter the lobby */
725 CtdlUserGoto(CtdlGetConfigStr("c_baseroom"), 0, 0, NULL, NULL, NULL, NULL);
729 void logged_in_response(void)
731 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
732 CIT_OK, CC->user.fullname, CC->user.axlevel,
733 CC->user.timescalled, CC->user.posted,
734 CC->user.flags, CC->user.usernum,
740 void CtdlUserLogout(void)
742 CitContext *CCC = MyContext();
744 syslog(LOG_DEBUG, "user_ops: CtdlUserLogout() logging out <%s> from session %d", CCC->curr_user, CCC->cs_pid);
746 /* Run any hooks registered by modules... */
747 PerformSessionHooks(EVT_LOGOUT);
750 * Clear out some session data. Most likely, the CitContext for this
751 * session is about to get nuked when the session disconnects, but
752 * since it's possible to log in again without reconnecting, we cannot
753 * make that assumption.
757 /* Check to see if the user was deleted while logged in and purge them if necessary */
758 if ((CCC->user.axlevel == AxDeleted) && (CCC->user.usernum)) {
759 purge_user(CCC->user.fullname);
762 /* Clear out the user record in memory so we don't behave like a ghost */
763 memset(&CCC->user, 0, sizeof(struct ctdluser));
764 CCC->curr_user[0] = 0;
765 CCC->cs_inet_email[0] = 0;
766 CCC->cs_inet_other_emails[0] = 0;
767 CCC->cs_inet_fn[0] = 0;
769 /* Free any output buffers */
775 * Validate a password on the host unix system by talking to the chkpwd daemon
777 static int validpw(uid_t uid, const char *pass)
782 if (IsEmptyStr(pass)) {
783 syslog(LOG_DEBUG, "user_ops: refusing to chkpwd for uid=%d with empty password", uid);
787 syslog(LOG_DEBUG, "user_ops: validating password for uid=%d using chkpwd...", uid);
789 begin_critical_section(S_CHKPWD);
790 rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
792 syslog(LOG_ERR, "user_ops: communication with chkpwd broken: %m");
793 end_critical_section(S_CHKPWD);
796 rv = write(chkpwd_write_pipe[1], pass, 256);
798 syslog(LOG_ERR, "user_ops: communication with chkpwd broken: %m");
799 end_critical_section(S_CHKPWD);
802 rv = read(chkpwd_read_pipe[0], buf, 4);
804 syslog(LOG_ERR, "user_ops: ommunication with chkpwd broken: %m");
805 end_critical_section(S_CHKPWD);
808 end_critical_section(S_CHKPWD);
810 if (!strncmp(buf, "PASS", 4)) {
811 syslog(LOG_DEBUG, "user_ops: chkpwd pass");
815 syslog(LOG_DEBUG, "user_ops: chkpwd fail");
821 * Start up the chkpwd daemon so validpw() has something to talk to
823 void start_chkpwd_daemon(void) {
825 struct stat filestats;
828 syslog(LOG_DEBUG, "user_ops: starting chkpwd daemon for host authentication mode");
830 if ((stat(file_chkpwd, &filestats)==-1) || (filestats.st_size==0)) {
831 syslog(LOG_ERR, "user_ops: %s: %m", file_chkpwd);
834 if (pipe(chkpwd_write_pipe) != 0) {
835 syslog(LOG_ERR, "user_ops: unable to create pipe for chkpwd daemon: %m");
838 if (pipe(chkpwd_read_pipe) != 0) {
839 syslog(LOG_ERR, "user_ops: unable to create pipe for chkpwd daemon: %m");
844 if (chkpwd_pid < 0) {
845 syslog(LOG_ERR, "user_ops: unable to fork chkpwd daemon: %m");
848 if (chkpwd_pid == 0) {
849 dup2(chkpwd_write_pipe[0], 0);
850 dup2(chkpwd_read_pipe[1], 1);
851 for (i=2; i<256; ++i) close(i);
852 execl(file_chkpwd, file_chkpwd, NULL);
853 syslog(LOG_ERR, "user_ops: unable to exec chkpwd daemon: %m");
860 int CtdlTryPassword(const char *password, long len)
863 CitContext *CCC = CC;
865 if ((CCC->logged_in)) {
866 syslog(LOG_WARNING, "user_ops: CtdlTryPassword: already logged in");
867 return pass_already_logged_in;
869 if (!strcmp(CCC->curr_user, NLI)) {
870 syslog(LOG_WARNING, "user_ops: CtdlTryPassword: no user selected");
873 if (CtdlGetUser(&CCC->user, CCC->curr_user)) {
874 syslog(LOG_ERR, "user_ops: CtdlTryPassword: internal error");
875 return pass_internal_error;
877 if (password == NULL) {
878 syslog(LOG_INFO, "user_ops: CtdlTryPassword: NULL password string supplied");
879 return pass_wrong_password;
882 else if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
886 if (validpw(CCC->user.uid, password)) {
890 * sooper-seekrit hack: populate the password field in the
891 * citadel database with the password that the user typed,
892 * if it's correct. This allows most sites to convert from
893 * host auth to native auth if they want to. If you think
894 * this is a security hazard, comment it out.
897 CtdlGetUserLock(&CCC->user, CCC->curr_user);
898 safestrncpy(CCC->user.password, password, sizeof CCC->user.password);
899 CtdlPutUserLock(&CCC->user);
902 * (sooper-seekrit hack ends here)
911 else if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
915 if ((CCC->ldap_dn) && (!CtdlTryPasswordLDAP(CCC->ldap_dn, password))) {
926 /* native auth mode */
929 pw = (char*) malloc(len + 1);
930 memcpy(pw, password, len + 1);
932 strproc(CCC->user.password);
933 code = strcasecmp(CCC->user.password, pw);
936 strproc(CCC->user.password);
937 code = strcasecmp(CCC->user.password, pw);
947 syslog(LOG_WARNING, "user_ops: bad password specified for <%s> Service <%s> Port <%ld> Remote <%s / %s>",
954 return pass_wrong_password;
960 * Delete a user record *and* all of its related resources.
962 int purge_user(char pname[])
964 struct ctdluser usbuf;
965 char usernamekey[USERNAME_SIZE];
967 makeuserkey(usernamekey, pname, cutusername(pname));
969 /* If the name is empty we can't find them in the DB any way so just return */
970 if (IsEmptyStr(pname)) {
971 return(ERROR + NO_SUCH_USER);
974 if (CtdlGetUser(&usbuf, pname) != 0) {
975 syslog(LOG_ERR, "user_ops: cannot purge user <%s> - not found", pname);
976 return(ERROR + NO_SUCH_USER);
979 /* Don't delete a user who is currently logged in. Instead, just
980 * set the access level to 0, and let the account get swept up
981 * during the next purge.
983 if (CtdlIsUserLoggedInByNum(usbuf.usernum)) {
984 syslog(LOG_WARNING, "user_ops: <%s> is logged in; not deleting", pname);
985 usbuf.axlevel = AxDeleted;
990 syslog(LOG_NOTICE, "user_ops: deleting <%s>", pname);
992 /* Perform any purge functions registered by server extensions */
993 PerformUserHooks(&usbuf, EVT_PURGEUSER);
995 /* delete any existing user/room relationships */
996 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
998 /* delete the users-by-number index record */
999 cdb_delete(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long));
1001 /* delete the userlog entry */
1002 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
1008 int internal_create_user(char *username, struct ctdluser *usbuf, uid_t uid)
1010 if (!CtdlGetUser(usbuf, username)) {
1011 return(ERROR + ALREADY_EXISTS);
1014 /* Go ahead and initialize a new user record */
1015 memset(usbuf, 0, sizeof(struct ctdluser));
1016 safestrncpy(usbuf->fullname, username, sizeof usbuf->fullname);
1017 strcpy(usbuf->password, "");
1020 /* These are the default flags on new accounts */
1021 usbuf->flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
1023 usbuf->timescalled = 0;
1025 usbuf->axlevel = CtdlGetConfigInt("c_initax");
1026 usbuf->lastcall = time(NULL);
1028 /* fetch a new user number */
1029 usbuf->usernum = get_new_user_number();
1031 /* add user to the database */
1033 cdb_store(CDB_USERSBYNUMBER, &usbuf->usernum, sizeof(long), usbuf->fullname, strlen(usbuf->fullname)+1);
1035 /* If non-native auth, index by uid */
1036 if ((usbuf->uid > 0) && (usbuf->uid != NATIVE_AUTH_UID)) {
1037 StrBuf *claimed_id = NewStrBuf();
1038 StrBufPrintf(claimed_id, "uid:%d", usbuf->uid);
1039 attach_extauth(usbuf, claimed_id);
1040 FreeStrBuf(&claimed_id);
1048 * create_user() - back end processing to create a new user
1050 * Set 'newusername' to the desired account name.
1051 * Set 'become_user' to CREATE_USER_BECOME_USER if this is self-service account creation and we want to
1052 * actually log in as the user we just created, otherwise set it to CREATE_USER_DO_NOT_BECOME_USER
1053 * Set 'uid' to some uid_t value to associate the account with an external auth user, or (-1) for native auth
1055 int create_user(char *username, int become_user, uid_t uid)
1057 struct ctdluser usbuf;
1058 struct ctdlroom qrbuf;
1059 char mailboxname[ROOMNAMELEN];
1064 if ((retval = internal_create_user(username, &usbuf, uid)) != 0) {
1069 * Give the user a private mailbox and a configuration room.
1070 * Make the latter an invisible system room.
1072 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
1073 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
1075 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
1076 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
1077 if (CtdlGetRoomLock(&qrbuf, mailboxname) == 0) {
1078 qrbuf.QRflags2 |= QR2_SYSTEM;
1079 CtdlPutRoomLock(&qrbuf);
1082 /* Perform any create functions registered by server extensions */
1083 PerformUserHooks(&usbuf, EVT_NEWUSER);
1085 /* Everything below this line can be bypassed if administratively
1086 * creating a user, instead of doing self-service account creation
1089 if (become_user == CREATE_USER_BECOME_USER) {
1090 /* Now become the user we just created */
1091 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
1092 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
1095 /* Check to make sure we're still who we think we are */
1096 if (CtdlGetUser(&CC->user, CC->curr_user)) {
1097 return(ERROR + INTERNAL_ERROR);
1102 "New user account <%s> has been created, from host %s [%s].\n",
1107 CtdlAideMessage(buf, "User Creation Notice");
1108 syslog(LOG_NOTICE, "user_ops: <%s> created", username);
1114 * set password - back end api code
1116 void CtdlSetPassword(char *new_pw)
1118 CtdlGetUserLock(&CC->user, CC->curr_user);
1119 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1120 CtdlPutUserLock(&CC->user);
1121 syslog(LOG_INFO, "user_ops: password changed for <%s>", CC->curr_user);
1122 PerformSessionHooks(EVT_SETPASS);
1127 * API function for cmd_invt_kick() and anything else that needs to
1128 * invite or kick out a user to/from a room.
1130 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1132 int CtdlInvtKick(char *iuser, int op) {
1133 struct ctdluser USscratch;
1137 if (CtdlGetUser(&USscratch, iuser) != 0) {
1141 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1143 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1144 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1147 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1148 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1150 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1152 /* post a message in Aide> saying what we just did */
1153 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1155 ((op == 1) ? "invited to" : "kicked out of"),
1157 (CC->logged_in ? CC->user.fullname : "an administrator")
1159 CtdlAideMessage(bbb,"User Admin Message");
1165 * Forget (Zap) the current room (API call)
1166 * Returns 0 on success
1168 int CtdlForgetThisRoom(void) {
1171 /* On some systems, Admins are not allowed to forget rooms */
1172 if (is_aide() && (CtdlGetConfigInt("c_aide_zap") == 0)
1173 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1177 CtdlGetUserLock(&CC->user, CC->curr_user);
1178 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1180 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1181 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1183 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1184 CtdlPutUserLock(&CC->user);
1186 /* Return to the Lobby, so we don't end up in an undefined room */
1187 CtdlUserGoto(CtdlGetConfigStr("c_baseroom"), 0, 0, NULL, NULL, NULL, NULL);
1193 * Traverse the user file and perform a callback for each user record.
1194 * (New improved version that runs in two phases so that callbacks can perform writes without having a r/o cursor open)
1196 void ForEachUser(void (*CallBack) (char *, void *out_data), void *in_data)
1198 struct cdbdata *cdbus;
1199 struct ctdluser *usptr;
1203 char username[USERNAME_SIZE];
1205 struct feu *usernames = NULL;
1206 struct feu *f = NULL;
1208 cdb_rewind(CDB_USERS);
1210 // Phase 1 : load list of usernames
1211 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1212 usptr = (struct ctdluser *) cdbus->ptr;
1214 if (strlen(usptr->fullname) > 0) {
1215 f = malloc(sizeof(struct feu));
1216 f->next = usernames;
1217 strncpy(f->username, usptr->fullname, USERNAME_SIZE);
1222 // Phase 2 : perform the callback for each username
1223 while (usernames != NULL) {
1224 (*CallBack) (usernames->username, in_data);
1226 usernames = usernames->next;
1235 * Count the number of new mail messages the user has
1239 int num_newmsgs = 0;
1240 num_newmsgs = CC->newmail;
1242 return(num_newmsgs);
1247 * Count the number of new mail messages the user has
1249 int InitialMailCheck()
1251 int num_newmsgs = 0;
1253 char mailboxname[ROOMNAMELEN];
1254 struct ctdlroom mailbox;
1256 struct cdbdata *cdbfr;
1257 long *msglist = NULL;
1260 CtdlMailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1261 if (CtdlGetRoom(&mailbox, mailboxname) != 0)
1263 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1265 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1267 if (cdbfr != NULL) {
1268 msglist = malloc(cdbfr->len);
1269 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1270 num_msgs = cdbfr->len / sizeof(long);
1274 for (a = 0; a < num_msgs; ++a) {
1275 if (msglist[a] > 0L) {
1276 if (msglist[a] > vbuf.v_lastseen) {
1281 if (msglist != NULL)
1284 return(num_newmsgs);