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];
35 * Trim a string down to the maximum username size and return the new length
37 long cutusername(char *username) {
39 len = strlen(username);
40 if (len >= USERNAME_SIZE)
42 syslog(LOG_INFO, "Username too long: %s", username);
43 len = USERNAME_SIZE - 1;
51 * makeuserkey() - convert a username into the format used as a database key
52 * (it's just the username converted into lower case)
54 void makeuserkey(char *key, const char *username, long len) {
57 if (len >= USERNAME_SIZE) {
58 syslog(LOG_INFO, "Username too long: %s", username);
59 len = USERNAME_SIZE - 1;
61 for (i=0; i<=len; ++i) {
62 key[i] = tolower(username[i]);
68 * CtdlGetUser() retrieve named user into supplied buffer.
69 * returns 0 on success
71 int CtdlGetUser(struct ctdluser *usbuf, char *name)
73 char usernamekey[USERNAME_SIZE];
74 struct cdbdata *cdbus;
75 long len = cutusername(name);
78 memset(usbuf, 0, sizeof(struct ctdluser));
81 makeuserkey(usernamekey, name, len);
82 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
84 if (cdbus == NULL) { /* user not found */
88 memcpy(usbuf, cdbus->ptr, ((cdbus->len > sizeof(struct ctdluser)) ? sizeof(struct ctdluser) : cdbus->len));
95 int CtdlLockGetCurrentUser(void)
98 return CtdlGetUser(&CCC->user, CCC->curr_user);
103 * CtdlGetUserLock() - same as getuser() but locks the record
105 int CtdlGetUserLock(struct ctdluser *usbuf, char *name)
109 retcode = CtdlGetUser(usbuf, name);
111 begin_critical_section(S_USERS);
118 * CtdlPutUser() - write user buffer into the correct place on disk
120 void CtdlPutUser(struct ctdluser *usbuf)
122 char usernamekey[USERNAME_SIZE];
124 makeuserkey(usernamekey, usbuf->fullname, cutusername(usbuf->fullname));
125 usbuf->version = REV_LEVEL;
126 cdb_store(CDB_USERS, usernamekey, strlen(usernamekey), usbuf, sizeof(struct ctdluser));
130 void CtdlPutCurrentUserLock()
132 CtdlPutUser(&CC->user);
137 * CtdlPutUserLock() - same as putuser() but locks the record
139 void CtdlPutUserLock(struct ctdluser *usbuf)
142 end_critical_section(S_USERS);
147 * rename_user() - this is tricky because the user's display name is the database key
149 * Returns 0 on success or nonzero if there was an error...
152 int rename_user(char *oldname, char *newname) {
153 int retcode = RENAMEUSER_OK;
154 struct ctdluser usbuf;
156 char oldnamekey[USERNAME_SIZE];
157 char newnamekey[USERNAME_SIZE];
159 /* Create the database keys... */
160 makeuserkey(oldnamekey, oldname, cutusername(oldname));
161 makeuserkey(newnamekey, newname, cutusername(newname));
163 /* Lock up and get going */
164 begin_critical_section(S_USERS);
166 /* We cannot rename a user who is currently logged in */
167 if (CtdlIsUserLoggedIn(oldname)) {
168 end_critical_section(S_USERS);
169 return RENAMEUSER_LOGGED_IN;
172 if (CtdlGetUser(&usbuf, newname) == 0) {
173 retcode = RENAMEUSER_ALREADY_EXISTS;
177 if (CtdlGetUser(&usbuf, oldname) != 0) {
178 retcode = RENAMEUSER_NOT_FOUND;
180 else { /* Sanity checks succeeded. Now rename the user. */
181 if (usbuf.usernum == 0) {
182 syslog(LOG_DEBUG, "user_ops: can not rename user \"Citadel\".");
183 retcode = RENAMEUSER_NOT_FOUND;
185 syslog(LOG_DEBUG, "user_ops: renaming <%s> to <%s>", oldname, newname);
186 cdb_delete(CDB_USERS, oldnamekey, strlen(oldnamekey));
187 safestrncpy(usbuf.fullname, newname, sizeof usbuf.fullname);
189 cdb_store(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long), usbuf.fullname, strlen(usbuf.fullname)+1 );
190 retcode = RENAMEUSER_OK;
196 end_critical_section(S_USERS);
202 * Index-generating function used by Ctdl[Get|Set]Relationship
204 int GenerateRelationshipIndex(char *IndexBuf,
215 TheIndex.iRoomID = RoomID;
216 TheIndex.iRoomGen = RoomGen;
217 TheIndex.iUserID = UserID;
219 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
220 return(sizeof(TheIndex));
225 * Back end for CtdlSetRelationship()
227 void put_visit(visit *newvisit)
232 memset (IndexBuf, 0, sizeof (IndexBuf));
233 /* Generate an index */
234 IndexLen = GenerateRelationshipIndex(IndexBuf, newvisit->v_roomnum, newvisit->v_roomgen, newvisit->v_usernum);
236 /* Store the record */
237 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
238 newvisit, sizeof(visit)
244 * Define a relationship between a user and a room
246 void CtdlSetRelationship(visit *newvisit, struct ctdluser *rel_user, struct ctdlroom *rel_room) {
247 /* We don't use these in Citadel because they're implicit by the
248 * index, but they must be present if the database is exported.
250 newvisit->v_roomnum = rel_room->QRnumber;
251 newvisit->v_roomgen = rel_room->QRgen;
252 newvisit->v_usernum = rel_user->usernum;
259 * Locate a relationship between a user and a room
261 void CtdlGetRelationship(visit *vbuf, struct ctdluser *rel_user, struct ctdlroom *rel_room) {
264 struct cdbdata *cdbvisit;
266 /* Generate an index */
267 IndexLen = GenerateRelationshipIndex(IndexBuf, rel_room->QRnumber, rel_room->QRgen, rel_user->usernum);
269 /* Clear out the buffer */
270 memset(vbuf, 0, sizeof(visit));
272 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
273 if (cdbvisit != NULL) {
274 memcpy(vbuf, cdbvisit->ptr, ((cdbvisit->len > sizeof(visit)) ? sizeof(visit) : cdbvisit->len));
278 /* If this is the first time the user has seen this room,
279 * set the view to be the default for the room.
281 vbuf->v_view = rel_room->QRdefaultview;
284 /* Set v_seen if necessary */
285 if (vbuf->v_seen[0] == 0) {
286 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
291 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
293 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
297 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
299 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
304 * Check to see if the specified user has Internet mail permission
305 * (returns nonzero if permission is granted)
307 int CtdlCheckInternetMailPermission(struct ctdluser *who) {
309 /* Do not allow twits to send Internet mail */
310 if (who->axlevel <= AxProbU) return(0);
312 /* Globally enabled? */
313 if (CtdlGetConfigInt("c_restrict") == 0) return(1);
315 /* User flagged ok? */
316 if (who->flags & US_INTERNET) return(2);
318 /* Admin level access? */
319 if (who->axlevel >= AxAideU) return(3);
321 /* No mail for you! */
327 * Convenience function.
329 int CtdlAccessCheck(int required_level)
331 if (CC->internal_pgm) return(0);
332 if (required_level >= ac_internal) {
333 cprintf("%d This is not a user-level command.\n", ERROR + HIGHER_ACCESS_REQUIRED);
337 if ((required_level >= ac_logged_in_or_guest) && (CC->logged_in == 0) && (CtdlGetConfigInt("c_guest_logins") == 0)) {
338 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
342 if ((required_level >= ac_logged_in) && (CC->logged_in == 0)) {
343 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
347 if (CC->user.axlevel >= AxAideU) return(0);
348 if (required_level >= ac_aide) {
349 cprintf("%d This command requires Admin access.\n",
350 ERROR + HIGHER_ACCESS_REQUIRED);
354 if (is_room_aide()) return(0);
355 if (required_level >= ac_room_aide) {
356 cprintf("%d This command requires Admin or Room Admin access.\n",
357 ERROR + HIGHER_ACCESS_REQUIRED);
361 /* shhh ... succeed quietly */
367 * Is the user currently logged in an Admin?
371 if (CC->user.axlevel >= AxAideU)
379 * Is the user currently logged in an Admin *or* the room Admin for this room?
381 int is_room_aide(void)
384 if (!CC->logged_in) {
388 if ((CC->user.axlevel >= AxAideU) || (CC->room.QRroomaide == CC->user.usernum)) {
397 * CtdlGetUserByNumber() - get user by number
398 * returns 0 if user was found
400 * Note: fetching a user this way requires one additional database operation.
402 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);
421 * Helper function for rebuild_usersbynumber()
423 void rebuild_ubn_for_user(char *username, void *data) {
426 syslog(LOG_DEBUG, "user_ops: rebuilding usersbynumber index for %s", username);
427 if (CtdlGetUser(&u, username) == 0) {
428 cdb_store(CDB_USERSBYNUMBER, &(u.usernum), sizeof(long), u.fullname, strlen(u.fullname)+1);
434 * Rebuild the users-by-number index
436 void rebuild_usersbynumber(void) {
437 cdb_trunc(CDB_USERSBYNUMBER); /* delete the old indices */
438 ForEachUser(rebuild_ubn_for_user, NULL); /* enumerate the users */
443 * getuserbyuid() Get user by system uid (for PAM mode authentication)
444 * Returns 0 if user was found
445 * This now uses an extauth index.
447 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
449 struct cdbdata *cdbextauth;
453 claimed_id = NewStrBuf();
454 StrBufPrintf(claimed_id, "uid:%d", number);
455 cdbextauth = cdb_fetch(CDB_EXTAUTH, ChrPtr(claimed_id), StrLength(claimed_id));
456 FreeStrBuf(&claimed_id);
457 if (cdbextauth == NULL) {
461 memcpy(&usernum, cdbextauth->ptr, sizeof(long));
462 cdb_free(cdbextauth);
464 if (!CtdlGetUserByNumber(usbuf, usernum)) {
473 * Back end for cmd_user() and its ilk
475 int CtdlLoginExistingUser(const char *trythisname)
480 syslog(LOG_DEBUG, "user_ops: CtdlLoginExistingUser(%s)", trythisname);
482 if ((CC->logged_in)) {
483 return login_already_logged_in;
486 if (trythisname == NULL) return login_not_found;
488 if (!strncasecmp(trythisname, "SYS_", 4))
490 syslog(LOG_DEBUG, "user_ops: system user \"%s\" is not allowed to log in.", trythisname);
491 return login_not_found;
494 /* Continue attempting user validation... */
495 safestrncpy(username, trythisname, sizeof (username));
498 if (IsEmptyStr(username)) {
499 return login_not_found;
502 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
507 struct passwd *tempPwdPtr;
510 syslog(LOG_DEBUG, "user_ops: asking host about <%s>", username);
511 #ifdef HAVE_GETPWNAM_R
512 #ifdef SOLARIS_GETPWUID
513 syslog(LOG_DEBUG, "user_ops: calling getpwnam_r()");
514 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
515 #else // SOLARIS_GETPWUID
516 syslog(LOG_DEBUG, "user_ops: calling getpwnam_r()");
517 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
518 #endif // SOLARIS_GETPWUID
519 #else // HAVE_GETPWNAM_R
520 syslog(LOG_DEBUG, "user_ops: SHOULD NEVER GET HERE!!!");
522 #endif // HAVE_GETPWNAM_R
523 if (tempPwdPtr == NULL) {
524 syslog(LOG_DEBUG, "user_ops: no such user <%s>", username);
525 return login_not_found;
528 /* Locate the associated Citadel account. If not found, make one attempt to create it. */
529 found_user = getuserbyuid(&CC->user, pd.pw_uid);
530 if (found_user != 0) {
531 create_user(username, CREATE_USER_DO_NOT_BECOME_USER, pd.pw_uid);
532 found_user = getuserbyuid(&CC->user, pd.pw_uid);
534 syslog(LOG_DEBUG, "user_ops: found it: uid=%ld, gecos=%s here: %d", (long)pd.pw_uid, pd.pw_gecos, found_user);
539 else if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
547 found_user = CtdlTryUserLDAP(username, ldap_dn, sizeof ldap_dn, ldap_cn, sizeof ldap_cn, &ldap_uid);
548 if (found_user != 0) {
549 return login_not_found;
552 found_user = getuserbyuid(&CC->user, ldap_uid);
553 if (found_user != 0) {
554 create_user(ldap_cn, CREATE_USER_DO_NOT_BECOME_USER, ldap_uid);
555 found_user = getuserbyuid(&CC->user, ldap_uid);
558 if (found_user == 0) {
559 if (CC->ldap_dn != NULL) free(CC->ldap_dn);
560 CC->ldap_dn = strdup(ldap_dn);
567 /* native auth mode */
569 recptypes *valid = NULL;
571 /* First, try to log in as if the supplied name is a display name */
572 found_user = CtdlGetUser(&CC->user, username);
574 /* If that didn't work, try to log in as if the supplied name * is an e-mail address */
575 if (found_user != 0) {
576 valid = validate_recipients(username, NULL, 0);
578 if (valid->num_local == 1) {
579 found_user = CtdlGetUser(&CC->user, valid->recp_local);
581 free_recipients(valid);
586 /* Did we find something? */
587 if (found_user == 0) {
588 if (((CC->nologin)) && (CC->user.axlevel < AxAideU)) {
589 return login_too_many_users;
591 safestrncpy(CC->curr_user, CC->user.fullname, sizeof CC->curr_user);
595 return login_not_found;
600 * session startup code which is common to both cmd_pass() and cmd_newu()
605 syslog(LOG_NOTICE, "user_ops: <%s> logged in", CC->curr_user);
607 CtdlGetUserLock(&CC->user, CC->curr_user);
608 ++(CC->user.timescalled);
609 CC->previous_login = CC->user.lastcall;
610 time(&CC->user.lastcall);
612 /* If this user's name is the name of the system administrator
613 * (as specified in setup), automatically assign access level 6.
615 if ( (!IsEmptyStr(CtdlGetConfigStr("c_sysadm"))) && (!strcasecmp(CC->user.fullname, CtdlGetConfigStr("c_sysadm"))) ) {
616 CC->user.axlevel = AxAideU;
619 /* If we're authenticating off the host system, automatically give root the highest level of access. */
620 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
621 if (CC->user.uid == 0) {
622 CC->user.axlevel = AxAideU;
625 CtdlPutUserLock(&CC->user);
627 /* If we are using LDAP authentication, extract the user's email addresses from the directory. */
629 if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
630 char new_emailaddrs[512];
631 if (CtdlGetConfigInt("c_ldap_sync_email_addrs") > 0) {
632 if (extract_email_addresses_from_ldap(CC->ldap_dn, new_emailaddrs) == 0) {
633 CtdlSetEmailAddressesForUser(CC->user.fullname, new_emailaddrs);
639 /* If the user does not have any email addresses assigned, generate one. */
640 if (IsEmptyStr(CC->user.emailaddrs)) {
641 AutoGenerateEmailAddressForUser(&CC->user);
645 * Populate cs_inet_email and cs_inet_other_emails with valid email addresses from the user record
647 strcpy(CC->cs_inet_email, CC->user.emailaddrs);
648 char *firstsep = strstr(CC->cs_inet_email, "|");
650 strcpy(CC->cs_inet_other_emails, firstsep+1);
654 CC->cs_inet_other_emails[0] = 0;
657 /* Create any personal rooms required by the system.
658 * (Technically, MAILROOM should be there already, but just in case...)
660 CtdlCreateRoom(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
661 CtdlCreateRoom(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
662 CtdlCreateRoom(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
663 CtdlCreateRoom(USERDRAFTROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
665 /* Run any startup routines registered by loadable modules */
666 PerformSessionHooks(EVT_LOGIN);
668 /* Enter the lobby */
669 CtdlUserGoto(CtdlGetConfigStr("c_baseroom"), 0, 0, NULL, NULL, NULL, NULL);
673 void logged_in_response(void)
675 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
676 CIT_OK, CC->user.fullname, CC->user.axlevel,
677 CC->user.timescalled, CC->user.posted,
678 CC->user.flags, CC->user.usernum,
684 void CtdlUserLogout(void)
686 CitContext *CCC = MyContext();
688 syslog(LOG_DEBUG, "user_ops: CtdlUserLogout() logging out <%s> from session %d", CCC->curr_user, CCC->cs_pid);
690 /* Run any hooks registered by modules... */
691 PerformSessionHooks(EVT_LOGOUT);
694 * Clear out some session data. Most likely, the CitContext for this
695 * session is about to get nuked when the session disconnects, but
696 * since it's possible to log in again without reconnecting, we cannot
697 * make that assumption.
701 /* Check to see if the user was deleted while logged in and purge them if necessary */
702 if ((CCC->user.axlevel == AxDeleted) && (CCC->user.usernum)) {
703 purge_user(CCC->user.fullname);
706 /* Clear out the user record in memory so we don't behave like a ghost */
707 memset(&CCC->user, 0, sizeof(struct ctdluser));
708 CCC->curr_user[0] = 0;
709 CCC->cs_inet_email[0] = 0;
710 CCC->cs_inet_other_emails[0] = 0;
711 CCC->cs_inet_fn[0] = 0;
713 /* Free any output buffers */
719 * Validate a password on the host unix system by talking to the chkpwd daemon
721 static int validpw(uid_t uid, const char *pass)
726 if (IsEmptyStr(pass)) {
727 syslog(LOG_DEBUG, "user_ops: refusing to chkpwd for uid=%d with empty password", uid);
731 syslog(LOG_DEBUG, "user_ops: validating password for uid=%d using chkpwd...", uid);
733 begin_critical_section(S_CHKPWD);
734 rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
736 syslog(LOG_ERR, "user_ops: communication with chkpwd broken: %m");
737 end_critical_section(S_CHKPWD);
740 rv = write(chkpwd_write_pipe[1], pass, 256);
742 syslog(LOG_ERR, "user_ops: communication with chkpwd broken: %m");
743 end_critical_section(S_CHKPWD);
746 rv = read(chkpwd_read_pipe[0], buf, 4);
748 syslog(LOG_ERR, "user_ops: ommunication with chkpwd broken: %m");
749 end_critical_section(S_CHKPWD);
752 end_critical_section(S_CHKPWD);
754 if (!strncmp(buf, "PASS", 4)) {
755 syslog(LOG_DEBUG, "user_ops: chkpwd pass");
759 syslog(LOG_DEBUG, "user_ops: chkpwd fail");
765 * Start up the chkpwd daemon so validpw() has something to talk to
767 void start_chkpwd_daemon(void) {
769 struct stat filestats;
772 syslog(LOG_DEBUG, "user_ops: starting chkpwd daemon for host authentication mode");
774 if ((stat(file_chkpwd, &filestats)==-1) || (filestats.st_size==0)) {
775 syslog(LOG_ERR, "user_ops: %s: %m", file_chkpwd);
778 if (pipe(chkpwd_write_pipe) != 0) {
779 syslog(LOG_ERR, "user_ops: unable to create pipe for chkpwd daemon: %m");
782 if (pipe(chkpwd_read_pipe) != 0) {
783 syslog(LOG_ERR, "user_ops: unable to create pipe for chkpwd daemon: %m");
788 if (chkpwd_pid < 0) {
789 syslog(LOG_ERR, "user_ops: unable to fork chkpwd daemon: %m");
792 if (chkpwd_pid == 0) {
793 dup2(chkpwd_write_pipe[0], 0);
794 dup2(chkpwd_read_pipe[1], 1);
795 for (i=2; i<256; ++i) close(i);
796 execl(file_chkpwd, file_chkpwd, NULL);
797 syslog(LOG_ERR, "user_ops: unable to exec chkpwd daemon: %m");
804 int CtdlTryPassword(const char *password, long len)
807 CitContext *CCC = CC;
809 if ((CCC->logged_in)) {
810 syslog(LOG_WARNING, "user_ops: CtdlTryPassword: already logged in");
811 return pass_already_logged_in;
813 if (!strcmp(CCC->curr_user, NLI)) {
814 syslog(LOG_WARNING, "user_ops: CtdlTryPassword: no user selected");
817 if (CtdlGetUser(&CCC->user, CCC->curr_user)) {
818 syslog(LOG_ERR, "user_ops: CtdlTryPassword: internal error");
819 return pass_internal_error;
821 if (password == NULL) {
822 syslog(LOG_INFO, "user_ops: CtdlTryPassword: NULL password string supplied");
823 return pass_wrong_password;
826 else if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
830 if (validpw(CCC->user.uid, password)) {
834 * sooper-seekrit hack: populate the password field in the
835 * citadel database with the password that the user typed,
836 * if it's correct. This allows most sites to convert from
837 * host auth to native auth if they want to. If you think
838 * this is a security hazard, comment it out.
841 CtdlGetUserLock(&CCC->user, CCC->curr_user);
842 safestrncpy(CCC->user.password, password, sizeof CCC->user.password);
843 CtdlPutUserLock(&CCC->user);
846 * (sooper-seekrit hack ends here)
855 else if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
859 if ((CCC->ldap_dn) && (!CtdlTryPasswordLDAP(CCC->ldap_dn, password))) {
870 /* native auth mode */
873 pw = (char*) malloc(len + 1);
874 memcpy(pw, password, len + 1);
876 strproc(CCC->user.password);
877 code = strcasecmp(CCC->user.password, pw);
880 strproc(CCC->user.password);
881 code = strcasecmp(CCC->user.password, pw);
891 syslog(LOG_WARNING, "user_ops: bad password specified for <%s> Service <%s> Port <%ld> Remote <%s / %s>",
898 return pass_wrong_password;
904 * Delete a user record *and* all of its related resources.
906 int purge_user(char pname[])
908 struct ctdluser usbuf;
909 char usernamekey[USERNAME_SIZE];
911 makeuserkey(usernamekey, pname, cutusername(pname));
913 /* If the name is empty we can't find them in the DB any way so just return */
914 if (IsEmptyStr(pname)) {
915 return(ERROR + NO_SUCH_USER);
918 if (CtdlGetUser(&usbuf, pname) != 0) {
919 syslog(LOG_ERR, "user_ops: cannot purge user <%s> - not found", pname);
920 return(ERROR + NO_SUCH_USER);
923 /* Don't delete a user who is currently logged in. Instead, just
924 * set the access level to 0, and let the account get swept up
925 * during the next purge.
927 if (CtdlIsUserLoggedInByNum(usbuf.usernum)) {
928 syslog(LOG_WARNING, "user_ops: <%s> is logged in; not deleting", pname);
929 usbuf.axlevel = AxDeleted;
934 syslog(LOG_NOTICE, "user_ops: deleting <%s>", pname);
936 /* Perform any purge functions registered by server extensions */
937 PerformUserHooks(&usbuf, EVT_PURGEUSER);
939 /* delete any existing user/room relationships */
940 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
942 /* delete the users-by-number index record */
943 cdb_delete(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long));
945 /* delete the userlog entry */
946 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
952 int internal_create_user(char *username, struct ctdluser *usbuf, uid_t uid)
954 if (!CtdlGetUser(usbuf, username)) {
955 return(ERROR + ALREADY_EXISTS);
958 /* Go ahead and initialize a new user record */
959 memset(usbuf, 0, sizeof(struct ctdluser));
960 safestrncpy(usbuf->fullname, username, sizeof usbuf->fullname);
961 strcpy(usbuf->password, "");
964 /* These are the default flags on new accounts */
965 usbuf->flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
967 usbuf->timescalled = 0;
969 usbuf->axlevel = CtdlGetConfigInt("c_initax");
970 usbuf->lastcall = time(NULL);
972 /* fetch a new user number */
973 usbuf->usernum = get_new_user_number();
975 /* add user to the database */
977 cdb_store(CDB_USERSBYNUMBER, &usbuf->usernum, sizeof(long), usbuf->fullname, strlen(usbuf->fullname)+1);
979 /* If non-native auth, index by uid */
980 if ((usbuf->uid > 0) && (usbuf->uid != NATIVE_AUTH_UID)) {
981 StrBuf *claimed_id = NewStrBuf();
982 StrBufPrintf(claimed_id, "uid:%d", usbuf->uid);
983 attach_extauth(usbuf, claimed_id);
984 FreeStrBuf(&claimed_id);
992 * create_user() - back end processing to create a new user
994 * Set 'newusername' to the desired account name.
995 * Set 'become_user' to CREATE_USER_BECOME_USER if this is self-service account creation and we want to
996 * actually log in as the user we just created, otherwise set it to CREATE_USER_DO_NOT_BECOME_USER
997 * Set 'uid' to some uid_t value to associate the account with an external auth user, or (-1) for native auth
999 int create_user(char *username, int become_user, uid_t uid)
1001 struct ctdluser usbuf;
1002 struct ctdlroom qrbuf;
1003 char mailboxname[ROOMNAMELEN];
1008 if ((retval = internal_create_user(username, &usbuf, uid)) != 0) {
1013 * Give the user a private mailbox and a configuration room.
1014 * Make the latter an invisible system room.
1016 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
1017 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
1019 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
1020 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
1021 if (CtdlGetRoomLock(&qrbuf, mailboxname) == 0) {
1022 qrbuf.QRflags2 |= QR2_SYSTEM;
1023 CtdlPutRoomLock(&qrbuf);
1026 /* Perform any create functions registered by server extensions */
1027 PerformUserHooks(&usbuf, EVT_NEWUSER);
1029 /* Everything below this line can be bypassed if administratively
1030 * creating a user, instead of doing self-service account creation
1033 if (become_user == CREATE_USER_BECOME_USER) {
1034 /* Now become the user we just created */
1035 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
1036 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
1039 /* Check to make sure we're still who we think we are */
1040 if (CtdlGetUser(&CC->user, CC->curr_user)) {
1041 return(ERROR + INTERNAL_ERROR);
1046 "New user account <%s> has been created, from host %s [%s].\n",
1051 CtdlAideMessage(buf, "User Creation Notice");
1052 syslog(LOG_NOTICE, "user_ops: <%s> created", username);
1058 * set password - back end api code
1060 void CtdlSetPassword(char *new_pw)
1062 CtdlGetUserLock(&CC->user, CC->curr_user);
1063 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1064 CtdlPutUserLock(&CC->user);
1065 syslog(LOG_INFO, "user_ops: password changed for <%s>", CC->curr_user);
1066 PerformSessionHooks(EVT_SETPASS);
1071 * API function for cmd_invt_kick() and anything else that needs to
1072 * invite or kick out a user to/from a room.
1074 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1076 int CtdlInvtKick(char *iuser, int op) {
1077 struct ctdluser USscratch;
1081 if (CtdlGetUser(&USscratch, iuser) != 0) {
1085 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1087 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1088 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1091 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1092 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1094 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1096 /* post a message in Aide> saying what we just did */
1097 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1099 ((op == 1) ? "invited to" : "kicked out of"),
1101 (CC->logged_in ? CC->user.fullname : "an administrator")
1103 CtdlAideMessage(bbb,"User Admin Message");
1109 * Forget (Zap) the current room (API call)
1110 * Returns 0 on success
1112 int CtdlForgetThisRoom(void) {
1115 /* On some systems, Admins are not allowed to forget rooms */
1116 if (is_aide() && (CtdlGetConfigInt("c_aide_zap") == 0)
1117 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1121 CtdlGetUserLock(&CC->user, CC->curr_user);
1122 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1124 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1125 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1127 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1128 CtdlPutUserLock(&CC->user);
1130 /* Return to the Lobby, so we don't end up in an undefined room */
1131 CtdlUserGoto(CtdlGetConfigStr("c_baseroom"), 0, 0, NULL, NULL, NULL, NULL);
1137 * Traverse the user file and perform a callback for each user record.
1138 * (New improved version that runs in two phases so that callbacks can perform writes without having a r/o cursor open)
1140 void ForEachUser(void (*CallBack) (char *, void *out_data), void *in_data)
1143 char username[USERNAME_SIZE];
1147 struct cdbdata *cdbus;
1148 struct ctdluser *usptr;
1150 struct feu *usernames = NULL;
1152 int num_users_alloc = 0;
1154 cdb_rewind(CDB_USERS);
1156 // Phase 1 : load list of usernames
1157 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1158 usptr = (struct ctdluser *) cdbus->ptr;
1160 if (strlen(usptr->fullname) > 0) {
1162 if (num_users > num_users_alloc) {
1163 num_users_alloc = ((num_users_alloc == 0) ? 1 : (num_users_alloc * 2));
1164 usernames = realloc(usernames, num_users_alloc * sizeof(struct feu));
1166 strcpy(usernames[num_users-1].username, usptr->fullname);
1167 usernames[num_users-1].version = usptr->version;
1171 // Phase 2 : perform the callback for each username
1172 for (i=0; i<num_users; ++i) {
1173 //if (usernames[i].version < 927) {
1174 // FIXME This is where we will do the reindexing stuff
1176 (*CallBack) (usernames[i].username, in_data);
1184 * Count the number of new mail messages the user has
1188 int num_newmsgs = 0;
1189 num_newmsgs = CC->newmail;
1191 return(num_newmsgs);
1196 * Count the number of new mail messages the user has
1198 int InitialMailCheck()
1200 int num_newmsgs = 0;
1202 char mailboxname[ROOMNAMELEN];
1203 struct ctdlroom mailbox;
1205 struct cdbdata *cdbfr;
1206 long *msglist = NULL;
1209 CtdlMailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1210 if (CtdlGetRoom(&mailbox, mailboxname) != 0)
1212 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1214 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1216 if (cdbfr != NULL) {
1217 msglist = malloc(cdbfr->len);
1218 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1219 num_msgs = cdbfr->len / sizeof(long);
1223 for (a = 0; a < num_msgs; ++a) {
1224 if (msglist[a] > 0L) {
1225 if (msglist[a] > vbuf.v_lastseen) {
1230 if (msglist != NULL)
1233 return(num_newmsgs);