2 * Server functions which perform operations on user objects.
4 * Copyright (c) 1987-2017 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 * CtdlGetUser() retrieve named user into supplied buffer.
37 * returns 0 on success
39 int CtdlGetUser(struct ctdluser *usbuf, char *name)
41 char usernamekey[USERNAME_SIZE];
42 struct cdbdata *cdbus;
43 long len = cutuserkey(name);
46 memset(usbuf, 0, sizeof(struct ctdluser));
49 makeuserkey(usernamekey, name, len);
50 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
52 if (cdbus == NULL) { /* user not found */
56 memcpy(usbuf, cdbus->ptr, ((cdbus->len > sizeof(struct ctdluser)) ? sizeof(struct ctdluser) : cdbus->len));
63 int CtdlLockGetCurrentUser(void)
66 return CtdlGetUser(&CCC->user, CCC->curr_user);
71 * CtdlGetUserLock() - same as getuser() but locks the record
73 int CtdlGetUserLock(struct ctdluser *usbuf, char *name)
77 retcode = CtdlGetUser(usbuf, name);
79 begin_critical_section(S_USERS);
86 * CtdlPutUser() - write user buffer into the correct place on disk
88 void CtdlPutUser(struct ctdluser *usbuf)
90 char usernamekey[USERNAME_SIZE];
92 makeuserkey(usernamekey, usbuf->fullname, cutuserkey(usbuf->fullname));
93 usbuf->version = REV_LEVEL;
94 cdb_store(CDB_USERS, usernamekey, strlen(usernamekey), usbuf, sizeof(struct ctdluser));
98 void CtdlPutCurrentUserLock()
100 CtdlPutUser(&CC->user);
105 * CtdlPutUserLock() - same as putuser() but locks the record
107 void CtdlPutUserLock(struct ctdluser *usbuf)
110 end_critical_section(S_USERS);
115 * rename_user() - this is tricky because the user's display name is the database key
117 * Returns 0 on success or nonzero if there was an error...
120 int rename_user(char *oldname, char *newname) {
121 int retcode = RENAMEUSER_OK;
122 struct ctdluser usbuf;
124 char oldnamekey[USERNAME_SIZE];
125 char newnamekey[USERNAME_SIZE];
127 /* Create the database keys... */
128 makeuserkey(oldnamekey, oldname, cutuserkey(oldname));
129 makeuserkey(newnamekey, newname, cutuserkey(newname));
131 /* Lock up and get going */
132 begin_critical_section(S_USERS);
134 /* We cannot rename a user who is currently logged in */
135 if (CtdlIsUserLoggedIn(oldname)) {
136 end_critical_section(S_USERS);
137 return RENAMEUSER_LOGGED_IN;
140 if (CtdlGetUser(&usbuf, newname) == 0) {
141 retcode = RENAMEUSER_ALREADY_EXISTS;
145 if (CtdlGetUser(&usbuf, oldname) != 0) {
146 retcode = RENAMEUSER_NOT_FOUND;
149 else { /* Sanity checks succeeded. Now rename the user. */
150 if (usbuf.usernum == 0)
152 syslog(LOG_DEBUG, "user_ops: can not rename user \"Citadel\".");
153 retcode = RENAMEUSER_NOT_FOUND;
155 syslog(LOG_DEBUG, "user_ops: renaming <%s> to <%s>", oldname, newname);
156 cdb_delete(CDB_USERS, oldnamekey, strlen(oldnamekey));
157 safestrncpy(usbuf.fullname, newname, sizeof usbuf.fullname);
159 cdb_store(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long), usbuf.fullname, strlen(usbuf.fullname)+1 );
160 retcode = RENAMEUSER_OK;
166 end_critical_section(S_USERS);
172 * Index-generating function used by Ctdl[Get|Set]Relationship
174 int GenerateRelationshipIndex(char *IndexBuf,
186 TheIndex.iRoomID = RoomID;
187 TheIndex.iRoomGen = RoomGen;
188 TheIndex.iUserID = UserID;
190 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
191 return (sizeof(TheIndex));
196 * Back end for CtdlSetRelationship()
198 void put_visit(visit *newvisit)
203 memset (IndexBuf, 0, sizeof (IndexBuf));
204 /* Generate an index */
205 IndexLen = GenerateRelationshipIndex(IndexBuf, newvisit->v_roomnum, newvisit->v_roomgen, newvisit->v_usernum);
207 /* Store the record */
208 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
209 newvisit, sizeof(visit)
215 * Define a relationship between a user and a room
217 void CtdlSetRelationship(visit *newvisit,
218 struct ctdluser *rel_user,
219 struct ctdlroom *rel_room)
221 /* We don't use these in Citadel because they're implicit by the
222 * index, but they must be present if the database is exported.
224 newvisit->v_roomnum = rel_room->QRnumber;
225 newvisit->v_roomgen = rel_room->QRgen;
226 newvisit->v_usernum = rel_user->usernum;
233 * Locate a relationship between a user and a room
235 void CtdlGetRelationship(visit *vbuf,
236 struct ctdluser *rel_user,
237 struct ctdlroom *rel_room)
241 struct cdbdata *cdbvisit;
243 /* Generate an index */
244 IndexLen = GenerateRelationshipIndex(IndexBuf,
249 /* Clear out the buffer */
250 memset(vbuf, 0, sizeof(visit));
252 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
253 if (cdbvisit != NULL) {
254 memcpy(vbuf, cdbvisit->ptr,
255 ((cdbvisit->len > sizeof(visit)) ?
256 sizeof(visit) : cdbvisit->len));
260 /* If this is the first time the user has seen this room,
261 * set the view to be the default for the room.
263 vbuf->v_view = rel_room->QRdefaultview;
266 /* Set v_seen if necessary */
267 if (vbuf->v_seen[0] == 0) {
268 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
273 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
275 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
279 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
281 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
286 * Check to see if the specified user has Internet mail permission
287 * (returns nonzero if permission is granted)
289 int CtdlCheckInternetMailPermission(struct ctdluser *who) {
291 /* Do not allow twits to send Internet mail */
292 if (who->axlevel <= AxProbU) return(0);
294 /* Globally enabled? */
295 if (CtdlGetConfigInt("c_restrict") == 0) return(1);
297 /* User flagged ok? */
298 if (who->flags & US_INTERNET) return(2);
300 /* Admin level access? */
301 if (who->axlevel >= AxAideU) return(3);
303 /* No mail for you! */
309 * Convenience function.
311 int CtdlAccessCheck(int required_level)
313 if (CC->internal_pgm) return(0);
314 if (required_level >= ac_internal) {
315 cprintf("%d This is not a user-level command.\n", ERROR + HIGHER_ACCESS_REQUIRED);
319 if ((required_level >= ac_logged_in_or_guest) && (CC->logged_in == 0) && (CtdlGetConfigInt("c_guest_logins") == 0)) {
320 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
324 if ((required_level >= ac_logged_in) && (CC->logged_in == 0)) {
325 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
329 if (CC->user.axlevel >= AxAideU) return(0);
330 if (required_level >= ac_aide) {
331 cprintf("%d This command requires Admin access.\n",
332 ERROR + HIGHER_ACCESS_REQUIRED);
336 if (is_room_aide()) return(0);
337 if (required_level >= ac_room_aide) {
338 cprintf("%d This command requires Admin or Room Admin access.\n",
339 ERROR + HIGHER_ACCESS_REQUIRED);
343 /* shhh ... succeed quietly */
349 * Is the user currently logged in an Admin?
353 if (CC->user.axlevel >= AxAideU)
361 * Is the user currently logged in an Admin *or* the room Admin for this room?
363 int is_room_aide(void)
366 if (!CC->logged_in) {
370 if ((CC->user.axlevel >= AxAideU) || (CC->room.QRroomaide == CC->user.usernum)) {
379 * CtdlGetUserByNumber() - get user by number
380 * returns 0 if user was found
382 * Note: fetching a user this way requires one additional database operation.
384 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number)
386 struct cdbdata *cdbun;
389 cdbun = cdb_fetch(CDB_USERSBYNUMBER, &number, sizeof(long));
391 syslog(LOG_INFO, "user_ops: %ld not found", number);
395 syslog(LOG_INFO, "user_ops: %ld maps to %s", number, cdbun->ptr);
396 r = CtdlGetUser(usbuf, cdbun->ptr);
403 * Helper function for rebuild_usersbynumber()
405 void rebuild_ubn_for_user(struct ctdluser *usbuf, void *data) {
408 struct ubnlist *next;
409 char username[USERNAME_SIZE];
413 static struct ubnlist *u = NULL;
414 struct ubnlist *ptr = NULL;
416 /* Lazy programming here. Call this function as a ForEachUser backend
417 * in order to queue up the room names, or call it with a null user
418 * to make it do the processing.
421 ptr = (struct ubnlist *) malloc(sizeof (struct ubnlist));
422 if (ptr == NULL) return;
424 ptr->usernum = usbuf->usernum;
425 safestrncpy(ptr->username, usbuf->fullname, sizeof ptr->username);
432 syslog(LOG_DEBUG, "user_ops: rebuilding usersbynumber index %10ld : %s", u->usernum, u->username);
433 cdb_store(CDB_USERSBYNUMBER, &u->usernum, sizeof(long), u->username, strlen(u->username)+1);
442 * Rebuild the users-by-number index
444 void rebuild_usersbynumber(void) {
445 cdb_trunc(CDB_USERSBYNUMBER); /* delete the old indices */
446 ForEachUser(rebuild_ubn_for_user, NULL); /* enumerate the users */
447 rebuild_ubn_for_user(NULL, NULL); /* and index them */
452 * getuserbyuid() Get user by system uid (for PAM mode authentication)
453 * Returns 0 if user was found
454 * This now uses an extauth index.
456 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
458 struct cdbdata *cdbextauth;
462 claimed_id = NewStrBuf();
463 StrBufPrintf(claimed_id, "uid:%d", number);
464 cdbextauth = cdb_fetch(CDB_EXTAUTH, ChrPtr(claimed_id), StrLength(claimed_id));
465 FreeStrBuf(&claimed_id);
466 if (cdbextauth == NULL) {
470 memcpy(&usernum, cdbextauth->ptr, sizeof(long));
471 cdb_free(cdbextauth);
473 if (!CtdlGetUserByNumber(usbuf, usernum)) {
482 * Back end for cmd_user() and its ilk
484 * NOTE: "authname" should only be used if we are attempting to use the "master user" feature
486 int CtdlLoginExistingUser(char *authname, const char *trythisname)
491 syslog(LOG_DEBUG, "user_ops: CtdlLoginExistingUser(%s, %s)", authname, trythisname);
493 if ((CC->logged_in)) {
494 return login_already_logged_in;
497 if (trythisname == NULL) return login_not_found;
499 if (!strncasecmp(trythisname, "SYS_", 4))
501 syslog(LOG_DEBUG, "user_ops: system user \"%s\" is not allowed to log in.", trythisname);
502 return login_not_found;
505 /* If a "master user" is defined, handle its authentication if specified */
507 if ( (!IsEmptyStr(CtdlGetConfigStr("c_master_user"))) &&
508 (!IsEmptyStr(CtdlGetConfigStr("c_master_pass"))) &&
509 (authname != NULL) &&
510 (!strcasecmp(authname, CtdlGetConfigStr("c_master_user"))) )
515 /* Continue attempting user validation... */
516 safestrncpy(username, trythisname, sizeof (username));
519 if (IsEmptyStr(username)) {
520 return login_not_found;
523 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
528 struct passwd *tempPwdPtr;
531 syslog(LOG_DEBUG, "user_ops: asking host about <%s>", username);
532 #ifdef HAVE_GETPWNAM_R
533 #ifdef SOLARIS_GETPWUID
534 syslog(LOG_DEBUG, "user_ops: calling getpwnam_r()");
535 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
536 #else // SOLARIS_GETPWUID
537 syslog(LOG_DEBUG, "user_ops: calling getpwnam_r()");
538 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
539 #endif // SOLARIS_GETPWUID
540 #else // HAVE_GETPWNAM_R
541 syslog(LOG_DEBUG, "user_ops: SHOULD NEVER GET HERE!!!");
543 #endif // HAVE_GETPWNAM_R
544 if (tempPwdPtr == NULL) {
545 syslog(LOG_DEBUG, "user_ops: no such user <%s>", username);
546 return login_not_found;
549 /* Locate the associated Citadel account.
550 * If not found, make one attempt to create it.
552 found_user = getuserbyuid(&CC->user, pd.pw_uid);
553 if (found_user != 0) {
554 create_user(username, CREATE_USER_DO_NOT_BECOME_USER, pd.pw_uid);
555 found_user = getuserbyuid(&CC->user, pd.pw_uid);
557 syslog(LOG_DEBUG, "user_ops: found it: uid=%ld, gecos=%s here: %d", (long)pd.pw_uid, pd.pw_gecos, found_user);
562 else if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
570 found_user = CtdlTryUserLDAP(username, ldap_dn, sizeof ldap_dn, ldap_cn, sizeof ldap_cn, &ldap_uid);
571 if (found_user != 0) {
572 return login_not_found;
575 found_user = getuserbyuid(&CC->user, ldap_uid);
576 if (found_user != 0) {
577 create_user(ldap_cn, CREATE_USER_DO_NOT_BECOME_USER, ldap_uid);
578 found_user = getuserbyuid(&CC->user, ldap_uid);
581 if (found_user == 0) {
582 if (CC->ldap_dn != NULL) free(CC->ldap_dn);
583 CC->ldap_dn = strdup(ldap_dn);
590 /* native auth mode */
592 recptypes *valid = NULL;
594 /* First, try to log in as if the supplied name is a display name */
595 found_user = CtdlGetUser(&CC->user, username);
597 /* If that didn't work, try to log in as if the supplied name * is an e-mail address */
598 if (found_user != 0) {
599 valid = validate_recipients(username, NULL, 0);
601 if (valid->num_local == 1) {
602 found_user = CtdlGetUser(&CC->user, valid->recp_local);
604 free_recipients(valid);
609 /* Did we find something? */
610 if (found_user == 0) {
611 if (((CC->nologin)) && (CC->user.axlevel < AxAideU)) {
612 return login_too_many_users;
614 safestrncpy(CC->curr_user, CC->user.fullname, sizeof CC->curr_user);
618 return login_not_found;
623 * session startup code which is common to both cmd_pass() and cmd_newu()
627 struct CitContext *CCC = CC;
630 syslog(LOG_NOTICE, "user_ops: <%s> logged in", CCC->curr_user);
632 CtdlGetUserLock(&CCC->user, CCC->curr_user);
633 ++(CCC->user.timescalled);
634 CCC->previous_login = CCC->user.lastcall;
635 time(&CCC->user.lastcall);
637 /* If this user's name is the name of the system administrator
638 * (as specified in setup), automatically assign access level 6.
640 if (!strcasecmp(CCC->user.fullname, CtdlGetConfigStr("c_sysadm"))) {
641 CCC->user.axlevel = AxAideU;
644 /* If we're authenticating off the host system, automatically give
645 * root the highest level of access.
647 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
648 if (CCC->user.uid == 0) {
649 CCC->user.axlevel = AxAideU;
654 * If we are using LDAP authentication, extract the user's email addresses from the directory.
657 if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
658 char new_emailaddrs[512];
659 if (CtdlGetConfigInt("c_ldap_sync_email_addrs") > 0) {
660 if (extract_email_addresses_from_ldap(CCC->ldap_dn, new_emailaddrs) == 0) {
661 CtdlSetEmailAddressesForUser(CCC->user.fullname, new_emailaddrs);
668 * No email address for user? Make one up.
670 if (IsEmptyStr(CCC->user.emailaddrs)) {
671 sprintf(CCC->user.emailaddrs, "cit%ld@%s", CCC->user.usernum, CtdlGetConfigStr("c_fqdn"));
674 CtdlPutUserLock(&CCC->user);
677 * Populate cs_inet_email and cs_inet_other_emails with valid email addresses from the user record
679 strcpy(CCC->cs_inet_email, CCC->user.emailaddrs);
680 char *firstsep = strstr(CCC->cs_inet_email, "|");
682 strcpy(CCC->cs_inet_other_emails, firstsep+1);
686 CCC->cs_inet_other_emails[0] = 0;
689 /* Create any personal rooms required by the system.
690 * (Technically, MAILROOM should be there already, but just in case...)
692 CtdlCreateRoom(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
693 CtdlCreateRoom(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
694 CtdlCreateRoom(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
695 CtdlCreateRoom(USERDRAFTROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
697 /* Run any startup routines registered by loadable modules */
698 PerformSessionHooks(EVT_LOGIN);
700 /* Enter the lobby */
701 CtdlUserGoto(CtdlGetConfigStr("c_baseroom"), 0, 0, NULL, NULL, NULL, NULL);
705 void logged_in_response(void)
707 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
708 CIT_OK, CC->user.fullname, CC->user.axlevel,
709 CC->user.timescalled, CC->user.posted,
710 CC->user.flags, CC->user.usernum,
716 void CtdlUserLogout(void)
718 CitContext *CCC = MyContext();
720 syslog(LOG_DEBUG, "user_ops: CtdlUserLogout() logging out <%s> from session %d", CCC->curr_user, CCC->cs_pid);
722 /* Run any hooks registered by modules... */
723 PerformSessionHooks(EVT_LOGOUT);
726 * Clear out some session data. Most likely, the CitContext for this
727 * session is about to get nuked when the session disconnects, but
728 * since it's possible to log in again without reconnecting, we cannot
729 * make that assumption.
731 strcpy(CCC->fake_username, "");
732 strcpy(CCC->fake_hostname, "");
733 strcpy(CCC->fake_roomname, "");
736 /* Check to see if the user was deleted whilst logged in and purge them if necessary */
737 if ((CCC->user.axlevel == AxDeleted) && (CCC->user.usernum)) {
738 purge_user(CCC->user.fullname);
741 /* Clear out the user record in memory so we don't behave like a ghost */
742 memset(&CCC->user, 0, sizeof(struct ctdluser));
743 CCC->curr_user[0] = 0;
745 CCC->cs_inet_email[0] = 0;
746 CCC->cs_inet_other_emails[0] = 0;
747 CCC->cs_inet_fn[0] = 0;
748 CCC->fake_username[0] = 0;
749 CCC->fake_hostname[0] = 0;
750 CCC->fake_roomname[0] = 0;
752 /* Free any output buffers */
758 * Validate a password on the host unix system by talking to the chkpwd daemon
760 static int validpw(uid_t uid, const char *pass)
765 if (IsEmptyStr(pass)) {
766 syslog(LOG_DEBUG, "user_ops: refusing to chkpwd for uid=%d with empty password", uid);
770 syslog(LOG_DEBUG, "user_ops: validating password for uid=%d using chkpwd...", uid);
772 begin_critical_section(S_CHKPWD);
773 rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
775 syslog(LOG_ERR, "user_ops: communication with chkpwd broken: %m");
776 end_critical_section(S_CHKPWD);
779 rv = write(chkpwd_write_pipe[1], pass, 256);
781 syslog(LOG_ERR, "user_ops: communication with chkpwd broken: %m");
782 end_critical_section(S_CHKPWD);
785 rv = read(chkpwd_read_pipe[0], buf, 4);
787 syslog(LOG_ERR, "user_ops: ommunication with chkpwd broken: %m");
788 end_critical_section(S_CHKPWD);
791 end_critical_section(S_CHKPWD);
793 if (!strncmp(buf, "PASS", 4)) {
794 syslog(LOG_DEBUG, "user_ops: chkpwd pass");
798 syslog(LOG_DEBUG, "user_ops: chkpwd fail");
804 * Start up the chkpwd daemon so validpw() has something to talk to
806 void start_chkpwd_daemon(void) {
808 struct stat filestats;
811 syslog(LOG_DEBUG, "user_ops: starting chkpwd daemon for host authentication mode");
813 if ((stat(file_chkpwd, &filestats)==-1) || (filestats.st_size==0)) {
814 syslog(LOG_ERR, "user_ops: %s: %m", file_chkpwd);
817 if (pipe(chkpwd_write_pipe) != 0) {
818 syslog(LOG_ERR, "user_ops: unable to create pipe for chkpwd daemon: %m");
821 if (pipe(chkpwd_read_pipe) != 0) {
822 syslog(LOG_ERR, "user_ops: unable to create pipe for chkpwd daemon: %m");
827 if (chkpwd_pid < 0) {
828 syslog(LOG_ERR, "user_ops: unable to fork chkpwd daemon: %m");
831 if (chkpwd_pid == 0) {
832 dup2(chkpwd_write_pipe[0], 0);
833 dup2(chkpwd_read_pipe[1], 1);
834 for (i=2; i<256; ++i) close(i);
835 execl(file_chkpwd, file_chkpwd, NULL);
836 syslog(LOG_ERR, "user_ops: unable to exec chkpwd daemon: %m");
843 int CtdlTryPassword(const char *password, long len)
846 CitContext *CCC = CC;
848 if ((CCC->logged_in)) {
849 syslog(LOG_WARNING, "user_ops: CtdlTryPassword: already logged in");
850 return pass_already_logged_in;
852 if (!strcmp(CCC->curr_user, NLI)) {
853 syslog(LOG_WARNING, "user_ops: CtdlTryPassword: no user selected");
856 if (CtdlGetUser(&CCC->user, CCC->curr_user)) {
857 syslog(LOG_ERR, "user_ops: CtdlTryPassword: internal error");
858 return pass_internal_error;
860 if (password == NULL) {
861 syslog(LOG_INFO, "user_ops: CtdlTryPassword: NULL password string supplied");
862 return pass_wrong_password;
865 if (CCC->is_master) {
866 code = strcmp(password, CtdlGetConfigStr("c_master_pass"));
869 else if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
873 if (validpw(CCC->user.uid, password)) {
877 * sooper-seekrit hack: populate the password field in the
878 * citadel database with the password that the user typed,
879 * if it's correct. This allows most sites to convert from
880 * host auth to native auth if they want to. If you think
881 * this is a security hazard, comment it out.
884 CtdlGetUserLock(&CCC->user, CCC->curr_user);
885 safestrncpy(CCC->user.password, password, sizeof CCC->user.password);
886 CtdlPutUserLock(&CCC->user);
889 * (sooper-seekrit hack ends here)
898 else if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
902 if ((CCC->ldap_dn) && (!CtdlTryPasswordLDAP(CCC->ldap_dn, password))) {
913 /* native auth mode */
916 pw = (char*) malloc(len + 1);
917 memcpy(pw, password, len + 1);
919 strproc(CCC->user.password);
920 code = strcasecmp(CCC->user.password, pw);
923 strproc(CCC->user.password);
924 code = strcasecmp(CCC->user.password, pw);
934 syslog(LOG_WARNING, "user_ops: bad password specified for <%s> Service <%s> Port <%ld> Remote <%s / %s>",
941 return pass_wrong_password;
947 * Delete a user record *and* all of its related resources.
949 int purge_user(char pname[])
951 struct ctdluser usbuf;
952 char usernamekey[USERNAME_SIZE];
954 makeuserkey(usernamekey, pname, cutuserkey(pname));
956 /* If the name is empty we can't find them in the DB any way so just return */
957 if (IsEmptyStr(pname))
958 return (ERROR + NO_SUCH_USER);
960 if (CtdlGetUser(&usbuf, pname) != 0) {
961 syslog(LOG_ERR, "user_ops: cannot purge user <%s> - not found", pname);
962 return (ERROR + NO_SUCH_USER);
964 /* Don't delete a user who is currently logged in. Instead, just
965 * set the access level to 0, and let the account get swept up
966 * during the next purge.
968 if (CtdlIsUserLoggedInByNum(usbuf.usernum)) {
969 syslog(LOG_WARNING, "user_ops: <%s> is logged in; not deleting", pname);
970 usbuf.axlevel = AxDeleted;
974 syslog(LOG_NOTICE, "user_ops: deleting <%s>", pname);
978 * This should all be wrapped in a S_USERS mutex.
979 * Without the mutex the user could log in before we get to the next function
980 * That would truly mess things up :-(
981 * I would like to see the S_USERS start before the CtdlIsUserLoggedInByNum() above
982 * and end after the user has been deleted from the database, below.
983 * Question is should we enter the EVT_PURGEUSER while S_USERS is active?
986 /* Perform any purge functions registered by server extensions */
987 PerformUserHooks(&usbuf, EVT_PURGEUSER);
989 /* delete any existing user/room relationships */
990 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
992 /* delete the users-by-number index record */
993 cdb_delete(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long));
995 /* delete the userlog entry */
996 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
1002 int internal_create_user(char *username, struct ctdluser *usbuf, uid_t uid)
1004 if (!CtdlGetUser(usbuf, username)) {
1005 return (ERROR + ALREADY_EXISTS);
1008 /* Go ahead and initialize a new user record */
1009 memset(usbuf, 0, sizeof(struct ctdluser));
1010 safestrncpy(usbuf->fullname, username, sizeof usbuf->fullname);
1011 strcpy(usbuf->password, "");
1014 /* These are the default flags on new accounts */
1015 usbuf->flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
1017 usbuf->timescalled = 0;
1019 usbuf->axlevel = CtdlGetConfigInt("c_initax");
1020 usbuf->lastcall = time(NULL);
1022 /* fetch a new user number */
1023 usbuf->usernum = get_new_user_number();
1025 /* add user to the database */
1027 cdb_store(CDB_USERSBYNUMBER, &usbuf->usernum, sizeof(long), usbuf->fullname, strlen(usbuf->fullname)+1);
1029 /* If non-native auth, index by uid */
1030 if ((usbuf->uid > 0) && (usbuf->uid != NATIVE_AUTH_UID)) {
1031 StrBuf *claimed_id = NewStrBuf();
1032 StrBufPrintf(claimed_id, "uid:%d", usbuf->uid);
1033 attach_extauth(usbuf, claimed_id);
1034 FreeStrBuf(&claimed_id);
1042 * create_user() - back end processing to create a new user
1044 * Set 'newusername' to the desired account name.
1045 * Set 'become_user' to CREATE_USER_BECOME_USER if this is self-service account creation and we want to
1046 * actually log in as the user we just created, otherwise set it to CREATE_USER_DO_NOT_BECOME_USER
1047 * Set 'uid' to some uid_t value to associate the account with an external auth user, or (-1) for native auth
1049 int create_user(char *username, int become_user, uid_t uid)
1051 struct ctdluser usbuf;
1052 struct ctdlroom qrbuf;
1053 char mailboxname[ROOMNAMELEN];
1058 if ((retval = internal_create_user(username, &usbuf, uid)) != 0) {
1063 * Give the user a private mailbox and a configuration room.
1064 * Make the latter an invisible system room.
1066 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
1067 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
1069 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
1070 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
1071 if (CtdlGetRoomLock(&qrbuf, mailboxname) == 0) {
1072 qrbuf.QRflags2 |= QR2_SYSTEM;
1073 CtdlPutRoomLock(&qrbuf);
1076 /* Perform any create functions registered by server extensions */
1077 PerformUserHooks(&usbuf, EVT_NEWUSER);
1079 /* Everything below this line can be bypassed if administratively
1080 * creating a user, instead of doing self-service account creation
1083 if (become_user == CREATE_USER_BECOME_USER) {
1084 /* Now become the user we just created */
1085 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
1086 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
1089 /* Check to make sure we're still who we think we are */
1090 if (CtdlGetUser(&CC->user, CC->curr_user)) {
1091 return (ERROR + INTERNAL_ERROR);
1096 "New user account <%s> has been created, from host %s [%s].\n",
1101 CtdlAideMessage(buf, "User Creation Notice");
1102 syslog(LOG_NOTICE, "user_ops: <%s> created", username);
1108 * set password - back end api code
1110 void CtdlSetPassword(char *new_pw)
1112 CtdlGetUserLock(&CC->user, CC->curr_user);
1113 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1114 CtdlPutUserLock(&CC->user);
1115 syslog(LOG_INFO, "user_ops: password changed for <%s>", CC->curr_user);
1116 PerformSessionHooks(EVT_SETPASS);
1121 * API function for cmd_invt_kick() and anything else that needs to
1122 * invite or kick out a user to/from a room.
1124 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1126 int CtdlInvtKick(char *iuser, int op) {
1127 struct ctdluser USscratch;
1131 if (CtdlGetUser(&USscratch, iuser) != 0) {
1135 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1137 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1138 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1141 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1142 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1144 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1146 /* post a message in Aide> saying what we just did */
1147 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1149 ((op == 1) ? "invited to" : "kicked out of"),
1151 (CC->logged_in ? CC->user.fullname : "an administrator")
1153 CtdlAideMessage(bbb,"User Admin Message");
1160 * Forget (Zap) the current room (API call)
1161 * Returns 0 on success
1163 int CtdlForgetThisRoom(void) {
1166 /* On some systems, Admins are not allowed to forget rooms */
1167 if (is_aide() && (CtdlGetConfigInt("c_aide_zap") == 0)
1168 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1172 CtdlGetUserLock(&CC->user, CC->curr_user);
1173 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1175 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1176 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1178 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1179 CtdlPutUserLock(&CC->user);
1181 /* Return to the Lobby, so we don't end up in an undefined room */
1182 CtdlUserGoto(CtdlGetConfigStr("c_baseroom"), 0, 0, NULL, NULL, NULL, NULL);
1189 * Traverse the user file...
1191 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1194 struct ctdluser usbuf;
1195 struct cdbdata *cdbus;
1197 cdb_rewind(CDB_USERS);
1199 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1200 memset(&usbuf, 0, sizeof(struct ctdluser));
1201 memcpy(&usbuf, cdbus->ptr,
1202 ((cdbus->len > sizeof(struct ctdluser)) ?
1203 sizeof(struct ctdluser) : cdbus->len));
1205 (*CallBack) (&usbuf, in_data);
1211 * List one user (this works with cmd_list)
1213 void ListThisUser(struct ctdluser *usbuf, void *data)
1217 searchstring = (char *)data;
1218 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1222 if (usbuf->axlevel > AxDeleted) {
1223 if ((CC->user.axlevel >= AxAideU)
1224 || ((usbuf->flags & US_UNLISTED) == 0)
1225 || ((CC->internal_pgm))) {
1226 cprintf("%s|%d|%ld|%ld|%ld|%ld||\n",
1230 (long)usbuf->lastcall,
1239 * Count the number of new mail messages the user has
1243 int num_newmsgs = 0;
1245 num_newmsgs = CC->newmail;
1248 return (num_newmsgs);
1253 * Count the number of new mail messages the user has
1255 int InitialMailCheck()
1257 int num_newmsgs = 0;
1259 char mailboxname[ROOMNAMELEN];
1260 struct ctdlroom mailbox;
1262 struct cdbdata *cdbfr;
1263 long *msglist = NULL;
1266 CtdlMailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1267 if (CtdlGetRoom(&mailbox, mailboxname) != 0)
1269 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1271 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1273 if (cdbfr != NULL) {
1274 msglist = malloc(cdbfr->len);
1275 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1276 num_msgs = cdbfr->len / sizeof(long);
1280 for (a = 0; a < num_msgs; ++a) {
1281 if (msglist[a] > 0L) {
1282 if (msglist[a] > vbuf.v_lastseen) {
1287 if (msglist != NULL)
1290 return (num_newmsgs);