2 * Server functions which perform operations on user objects.
4 * Copyright (c) 1987-2018 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, rel_room->QRnumber, rel_room->QRgen, rel_user->usernum);
246 /* Clear out the buffer */
247 memset(vbuf, 0, sizeof(visit));
249 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
250 if (cdbvisit != NULL) {
251 memcpy(vbuf, cdbvisit->ptr, ((cdbvisit->len > sizeof(visit)) ? sizeof(visit) : cdbvisit->len));
255 /* If this is the first time the user has seen this room,
256 * set the view to be the default for the room.
258 vbuf->v_view = rel_room->QRdefaultview;
261 /* Set v_seen if necessary */
262 if (vbuf->v_seen[0] == 0) {
263 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
268 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
270 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
274 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
276 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
281 * Check to see if the specified user has Internet mail permission
282 * (returns nonzero if permission is granted)
284 int CtdlCheckInternetMailPermission(struct ctdluser *who) {
286 /* Do not allow twits to send Internet mail */
287 if (who->axlevel <= AxProbU) return(0);
289 /* Globally enabled? */
290 if (CtdlGetConfigInt("c_restrict") == 0) return(1);
292 /* User flagged ok? */
293 if (who->flags & US_INTERNET) return(2);
295 /* Admin level access? */
296 if (who->axlevel >= AxAideU) return(3);
298 /* No mail for you! */
304 * Convenience function.
306 int CtdlAccessCheck(int required_level)
308 if (CC->internal_pgm) return(0);
309 if (required_level >= ac_internal) {
310 cprintf("%d This is not a user-level command.\n", ERROR + HIGHER_ACCESS_REQUIRED);
314 if ((required_level >= ac_logged_in_or_guest) && (CC->logged_in == 0) && (CtdlGetConfigInt("c_guest_logins") == 0)) {
315 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
319 if ((required_level >= ac_logged_in) && (CC->logged_in == 0)) {
320 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
324 if (CC->user.axlevel >= AxAideU) return(0);
325 if (required_level >= ac_aide) {
326 cprintf("%d This command requires Admin access.\n",
327 ERROR + HIGHER_ACCESS_REQUIRED);
331 if (is_room_aide()) return(0);
332 if (required_level >= ac_room_aide) {
333 cprintf("%d This command requires Admin or Room Admin access.\n",
334 ERROR + HIGHER_ACCESS_REQUIRED);
338 /* shhh ... succeed quietly */
344 * Is the user currently logged in an Admin?
348 if (CC->user.axlevel >= AxAideU)
356 * Is the user currently logged in an Admin *or* the room Admin for this room?
358 int is_room_aide(void)
361 if (!CC->logged_in) {
365 if ((CC->user.axlevel >= AxAideU) || (CC->room.QRroomaide == CC->user.usernum)) {
374 * CtdlGetUserByNumber() - get user by number
375 * returns 0 if user was found
377 * Note: fetching a user this way requires one additional database operation.
379 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number)
381 struct cdbdata *cdbun;
384 cdbun = cdb_fetch(CDB_USERSBYNUMBER, &number, sizeof(long));
386 syslog(LOG_INFO, "user_ops: %ld not found", number);
390 syslog(LOG_INFO, "user_ops: %ld maps to %s", number, cdbun->ptr);
391 r = CtdlGetUser(usbuf, cdbun->ptr);
398 * Helper function for rebuild_usersbynumber()
400 void rebuild_ubn_for_user(struct ctdluser *usbuf, void *data) {
403 struct ubnlist *next;
404 char username[USERNAME_SIZE];
408 static struct ubnlist *u = NULL;
409 struct ubnlist *ptr = NULL;
411 /* Lazy programming here. Call this function as a ForEachUser backend
412 * in order to queue up the room names, or call it with a null user
413 * to make it do the processing.
416 ptr = (struct ubnlist *) malloc(sizeof (struct ubnlist));
417 if (ptr == NULL) return;
419 ptr->usernum = usbuf->usernum;
420 safestrncpy(ptr->username, usbuf->fullname, sizeof ptr->username);
427 syslog(LOG_DEBUG, "user_ops: rebuilding usersbynumber index %10ld : %s", u->usernum, u->username);
428 cdb_store(CDB_USERSBYNUMBER, &u->usernum, sizeof(long), u->username, strlen(u->username)+1);
437 * Rebuild the users-by-number index
439 void rebuild_usersbynumber(void) {
440 cdb_trunc(CDB_USERSBYNUMBER); /* delete the old indices */
441 ForEachUser(rebuild_ubn_for_user, NULL); /* enumerate the users */
442 rebuild_ubn_for_user(NULL, NULL); /* and index them */
447 * getuserbyuid() Get user by system uid (for PAM mode authentication)
448 * Returns 0 if user was found
449 * This now uses an extauth index.
451 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
453 struct cdbdata *cdbextauth;
457 claimed_id = NewStrBuf();
458 StrBufPrintf(claimed_id, "uid:%d", number);
459 cdbextauth = cdb_fetch(CDB_EXTAUTH, ChrPtr(claimed_id), StrLength(claimed_id));
460 FreeStrBuf(&claimed_id);
461 if (cdbextauth == NULL) {
465 memcpy(&usernum, cdbextauth->ptr, sizeof(long));
466 cdb_free(cdbextauth);
468 if (!CtdlGetUserByNumber(usbuf, usernum)) {
477 * Back end for cmd_user() and its ilk
479 * NOTE: "authname" should only be used if we are attempting to use the "master user" feature
481 int CtdlLoginExistingUser(char *authname, const char *trythisname)
486 syslog(LOG_DEBUG, "user_ops: CtdlLoginExistingUser(%s, %s)", authname, trythisname);
488 if ((CC->logged_in)) {
489 return login_already_logged_in;
492 if (trythisname == NULL) return login_not_found;
494 if (!strncasecmp(trythisname, "SYS_", 4))
496 syslog(LOG_DEBUG, "user_ops: system user \"%s\" is not allowed to log in.", trythisname);
497 return login_not_found;
500 /* If a "master user" is defined, handle its authentication if specified */
502 if ( (!IsEmptyStr(CtdlGetConfigStr("c_master_user"))) &&
503 (!IsEmptyStr(CtdlGetConfigStr("c_master_pass"))) &&
504 (authname != NULL) &&
505 (!strcasecmp(authname, CtdlGetConfigStr("c_master_user"))) )
510 /* Continue attempting user validation... */
511 safestrncpy(username, trythisname, sizeof (username));
514 if (IsEmptyStr(username)) {
515 return login_not_found;
518 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
523 struct passwd *tempPwdPtr;
526 syslog(LOG_DEBUG, "user_ops: asking host about <%s>", username);
527 #ifdef HAVE_GETPWNAM_R
528 #ifdef SOLARIS_GETPWUID
529 syslog(LOG_DEBUG, "user_ops: calling getpwnam_r()");
530 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
531 #else // SOLARIS_GETPWUID
532 syslog(LOG_DEBUG, "user_ops: calling getpwnam_r()");
533 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
534 #endif // SOLARIS_GETPWUID
535 #else // HAVE_GETPWNAM_R
536 syslog(LOG_DEBUG, "user_ops: SHOULD NEVER GET HERE!!!");
538 #endif // HAVE_GETPWNAM_R
539 if (tempPwdPtr == NULL) {
540 syslog(LOG_DEBUG, "user_ops: no such user <%s>", username);
541 return login_not_found;
544 /* Locate the associated Citadel account. If not found, make one attempt to create it. */
545 found_user = getuserbyuid(&CC->user, pd.pw_uid);
546 if (found_user != 0) {
547 create_user(username, CREATE_USER_DO_NOT_BECOME_USER, pd.pw_uid);
548 found_user = getuserbyuid(&CC->user, pd.pw_uid);
550 syslog(LOG_DEBUG, "user_ops: found it: uid=%ld, gecos=%s here: %d", (long)pd.pw_uid, pd.pw_gecos, found_user);
555 else if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
563 found_user = CtdlTryUserLDAP(username, ldap_dn, sizeof ldap_dn, ldap_cn, sizeof ldap_cn, &ldap_uid);
564 if (found_user != 0) {
565 return login_not_found;
568 found_user = getuserbyuid(&CC->user, ldap_uid);
569 if (found_user != 0) {
570 create_user(ldap_cn, CREATE_USER_DO_NOT_BECOME_USER, ldap_uid);
571 found_user = getuserbyuid(&CC->user, ldap_uid);
574 if (found_user == 0) {
575 if (CC->ldap_dn != NULL) free(CC->ldap_dn);
576 CC->ldap_dn = strdup(ldap_dn);
583 /* native auth mode */
585 recptypes *valid = NULL;
587 /* First, try to log in as if the supplied name is a display name */
588 found_user = CtdlGetUser(&CC->user, username);
590 /* If that didn't work, try to log in as if the supplied name * is an e-mail address */
591 if (found_user != 0) {
592 valid = validate_recipients(username, NULL, 0);
594 if (valid->num_local == 1) {
595 found_user = CtdlGetUser(&CC->user, valid->recp_local);
597 free_recipients(valid);
602 /* Did we find something? */
603 if (found_user == 0) {
604 if (((CC->nologin)) && (CC->user.axlevel < AxAideU)) {
605 return login_too_many_users;
607 safestrncpy(CC->curr_user, CC->user.fullname, sizeof CC->curr_user);
611 return login_not_found;
616 * session startup code which is common to both cmd_pass() and cmd_newu()
621 syslog(LOG_NOTICE, "user_ops: <%s> logged in", CC->curr_user);
623 CtdlGetUserLock(&CC->user, CC->curr_user);
624 ++(CC->user.timescalled);
625 CC->previous_login = CC->user.lastcall;
626 time(&CC->user.lastcall);
628 /* If this user's name is the name of the system administrator
629 * (as specified in setup), automatically assign access level 6.
631 if ( (!IsEmptyStr(CtdlGetConfigStr("c_sysadm"))) && (!strcasecmp(CC->user.fullname, CtdlGetConfigStr("c_sysadm"))) ) {
632 CC->user.axlevel = AxAideU;
635 /* If we're authenticating off the host system, automatically give root the highest level of access. */
636 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
637 if (CC->user.uid == 0) {
638 CC->user.axlevel = AxAideU;
641 CtdlPutUserLock(&CC->user);
643 /* If we are using LDAP authentication, extract the user's email addresses from the directory. */
645 if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
646 char new_emailaddrs[512];
647 if (CtdlGetConfigInt("c_ldap_sync_email_addrs") > 0) {
648 if (extract_email_addresses_from_ldap(CC->ldap_dn, new_emailaddrs) == 0) {
649 CtdlSetEmailAddressesForUser(CC->user.fullname, new_emailaddrs);
655 /* If the user does not have any email addresses assigned, generate one. */
656 if (IsEmptyStr(CC->user.emailaddrs)) {
657 char synthetic_email_addr[1024];
658 snprintf(synthetic_email_addr, sizeof synthetic_email_addr, "ctdl%08lx@%s", CC->user.usernum, CtdlGetConfigStr("c_fqdn"));
659 CtdlSetEmailAddressesForUser(CC->user.fullname, synthetic_email_addr);
660 strncpy(CC->user.emailaddrs, synthetic_email_addr, sizeof(CC->user.emailaddrs));
661 syslog(LOG_DEBUG, "user_ops: auto-generated email address <%s> for <%s>", synthetic_email_addr, CC->user.fullname);
665 * Populate cs_inet_email and cs_inet_other_emails with valid email addresses from the user record
667 strcpy(CC->cs_inet_email, CC->user.emailaddrs);
668 char *firstsep = strstr(CC->cs_inet_email, "|");
670 strcpy(CC->cs_inet_other_emails, firstsep+1);
674 CC->cs_inet_other_emails[0] = 0;
677 /* Create any personal rooms required by the system.
678 * (Technically, MAILROOM should be there already, but just in case...)
680 CtdlCreateRoom(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
681 CtdlCreateRoom(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
682 CtdlCreateRoom(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
683 CtdlCreateRoom(USERDRAFTROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
685 /* Run any startup routines registered by loadable modules */
686 PerformSessionHooks(EVT_LOGIN);
688 /* Enter the lobby */
689 CtdlUserGoto(CtdlGetConfigStr("c_baseroom"), 0, 0, NULL, NULL, NULL, NULL);
693 void logged_in_response(void)
695 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
696 CIT_OK, CC->user.fullname, CC->user.axlevel,
697 CC->user.timescalled, CC->user.posted,
698 CC->user.flags, CC->user.usernum,
704 void CtdlUserLogout(void)
706 CitContext *CCC = MyContext();
708 syslog(LOG_DEBUG, "user_ops: CtdlUserLogout() logging out <%s> from session %d", CCC->curr_user, CCC->cs_pid);
710 /* Run any hooks registered by modules... */
711 PerformSessionHooks(EVT_LOGOUT);
714 * Clear out some session data. Most likely, the CitContext for this
715 * session is about to get nuked when the session disconnects, but
716 * since it's possible to log in again without reconnecting, we cannot
717 * make that assumption.
719 strcpy(CCC->fake_username, "");
720 strcpy(CCC->fake_hostname, "");
721 strcpy(CCC->fake_roomname, "");
724 /* Check to see if the user was deleted whilst logged in and purge them if necessary */
725 if ((CCC->user.axlevel == AxDeleted) && (CCC->user.usernum)) {
726 purge_user(CCC->user.fullname);
729 /* Clear out the user record in memory so we don't behave like a ghost */
730 memset(&CCC->user, 0, sizeof(struct ctdluser));
731 CCC->curr_user[0] = 0;
733 CCC->cs_inet_email[0] = 0;
734 CCC->cs_inet_other_emails[0] = 0;
735 CCC->cs_inet_fn[0] = 0;
736 CCC->fake_username[0] = 0;
737 CCC->fake_hostname[0] = 0;
738 CCC->fake_roomname[0] = 0;
740 /* Free any output buffers */
746 * Validate a password on the host unix system by talking to the chkpwd daemon
748 static int validpw(uid_t uid, const char *pass)
753 if (IsEmptyStr(pass)) {
754 syslog(LOG_DEBUG, "user_ops: refusing to chkpwd for uid=%d with empty password", uid);
758 syslog(LOG_DEBUG, "user_ops: validating password for uid=%d using chkpwd...", uid);
760 begin_critical_section(S_CHKPWD);
761 rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
763 syslog(LOG_ERR, "user_ops: communication with chkpwd broken: %m");
764 end_critical_section(S_CHKPWD);
767 rv = write(chkpwd_write_pipe[1], pass, 256);
769 syslog(LOG_ERR, "user_ops: communication with chkpwd broken: %m");
770 end_critical_section(S_CHKPWD);
773 rv = read(chkpwd_read_pipe[0], buf, 4);
775 syslog(LOG_ERR, "user_ops: ommunication with chkpwd broken: %m");
776 end_critical_section(S_CHKPWD);
779 end_critical_section(S_CHKPWD);
781 if (!strncmp(buf, "PASS", 4)) {
782 syslog(LOG_DEBUG, "user_ops: chkpwd pass");
786 syslog(LOG_DEBUG, "user_ops: chkpwd fail");
792 * Start up the chkpwd daemon so validpw() has something to talk to
794 void start_chkpwd_daemon(void) {
796 struct stat filestats;
799 syslog(LOG_DEBUG, "user_ops: starting chkpwd daemon for host authentication mode");
801 if ((stat(file_chkpwd, &filestats)==-1) || (filestats.st_size==0)) {
802 syslog(LOG_ERR, "user_ops: %s: %m", file_chkpwd);
805 if (pipe(chkpwd_write_pipe) != 0) {
806 syslog(LOG_ERR, "user_ops: unable to create pipe for chkpwd daemon: %m");
809 if (pipe(chkpwd_read_pipe) != 0) {
810 syslog(LOG_ERR, "user_ops: unable to create pipe for chkpwd daemon: %m");
815 if (chkpwd_pid < 0) {
816 syslog(LOG_ERR, "user_ops: unable to fork chkpwd daemon: %m");
819 if (chkpwd_pid == 0) {
820 dup2(chkpwd_write_pipe[0], 0);
821 dup2(chkpwd_read_pipe[1], 1);
822 for (i=2; i<256; ++i) close(i);
823 execl(file_chkpwd, file_chkpwd, NULL);
824 syslog(LOG_ERR, "user_ops: unable to exec chkpwd daemon: %m");
831 int CtdlTryPassword(const char *password, long len)
834 CitContext *CCC = CC;
836 if ((CCC->logged_in)) {
837 syslog(LOG_WARNING, "user_ops: CtdlTryPassword: already logged in");
838 return pass_already_logged_in;
840 if (!strcmp(CCC->curr_user, NLI)) {
841 syslog(LOG_WARNING, "user_ops: CtdlTryPassword: no user selected");
844 if (CtdlGetUser(&CCC->user, CCC->curr_user)) {
845 syslog(LOG_ERR, "user_ops: CtdlTryPassword: internal error");
846 return pass_internal_error;
848 if (password == NULL) {
849 syslog(LOG_INFO, "user_ops: CtdlTryPassword: NULL password string supplied");
850 return pass_wrong_password;
853 if (CCC->is_master) {
854 code = strcmp(password, CtdlGetConfigStr("c_master_pass"));
857 else if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
861 if (validpw(CCC->user.uid, password)) {
865 * sooper-seekrit hack: populate the password field in the
866 * citadel database with the password that the user typed,
867 * if it's correct. This allows most sites to convert from
868 * host auth to native auth if they want to. If you think
869 * this is a security hazard, comment it out.
872 CtdlGetUserLock(&CCC->user, CCC->curr_user);
873 safestrncpy(CCC->user.password, password, sizeof CCC->user.password);
874 CtdlPutUserLock(&CCC->user);
877 * (sooper-seekrit hack ends here)
886 else if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
890 if ((CCC->ldap_dn) && (!CtdlTryPasswordLDAP(CCC->ldap_dn, password))) {
901 /* native auth mode */
904 pw = (char*) malloc(len + 1);
905 memcpy(pw, password, len + 1);
907 strproc(CCC->user.password);
908 code = strcasecmp(CCC->user.password, pw);
911 strproc(CCC->user.password);
912 code = strcasecmp(CCC->user.password, pw);
922 syslog(LOG_WARNING, "user_ops: bad password specified for <%s> Service <%s> Port <%ld> Remote <%s / %s>",
929 return pass_wrong_password;
935 * Delete a user record *and* all of its related resources.
937 int purge_user(char pname[])
939 struct ctdluser usbuf;
940 char usernamekey[USERNAME_SIZE];
942 makeuserkey(usernamekey, pname, cutuserkey(pname));
944 /* If the name is empty we can't find them in the DB any way so just return */
945 if (IsEmptyStr(pname))
946 return(ERROR + NO_SUCH_USER);
948 if (CtdlGetUser(&usbuf, pname) != 0) {
949 syslog(LOG_ERR, "user_ops: cannot purge user <%s> - not found", pname);
950 return(ERROR + NO_SUCH_USER);
953 /* Don't delete a user who is currently logged in. Instead, just
954 * set the access level to 0, and let the account get swept up
955 * during the next purge.
957 if (CtdlIsUserLoggedInByNum(usbuf.usernum)) {
958 syslog(LOG_WARNING, "user_ops: <%s> is logged in; not deleting", pname);
959 usbuf.axlevel = AxDeleted;
964 syslog(LOG_NOTICE, "user_ops: deleting <%s>", pname);
966 /* Perform any purge functions registered by server extensions */
967 PerformUserHooks(&usbuf, EVT_PURGEUSER);
969 /* delete any existing user/room relationships */
970 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
972 /* delete the users-by-number index record */
973 cdb_delete(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long));
975 /* delete the userlog entry */
976 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
982 int internal_create_user(char *username, struct ctdluser *usbuf, uid_t uid)
984 if (!CtdlGetUser(usbuf, username)) {
985 return(ERROR + ALREADY_EXISTS);
988 /* Go ahead and initialize a new user record */
989 memset(usbuf, 0, sizeof(struct ctdluser));
990 safestrncpy(usbuf->fullname, username, sizeof usbuf->fullname);
991 strcpy(usbuf->password, "");
994 /* These are the default flags on new accounts */
995 usbuf->flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
997 usbuf->timescalled = 0;
999 usbuf->axlevel = CtdlGetConfigInt("c_initax");
1000 usbuf->lastcall = time(NULL);
1002 /* fetch a new user number */
1003 usbuf->usernum = get_new_user_number();
1005 /* add user to the database */
1007 cdb_store(CDB_USERSBYNUMBER, &usbuf->usernum, sizeof(long), usbuf->fullname, strlen(usbuf->fullname)+1);
1009 /* If non-native auth, index by uid */
1010 if ((usbuf->uid > 0) && (usbuf->uid != NATIVE_AUTH_UID)) {
1011 StrBuf *claimed_id = NewStrBuf();
1012 StrBufPrintf(claimed_id, "uid:%d", usbuf->uid);
1013 attach_extauth(usbuf, claimed_id);
1014 FreeStrBuf(&claimed_id);
1022 * create_user() - back end processing to create a new user
1024 * Set 'newusername' to the desired account name.
1025 * Set 'become_user' to CREATE_USER_BECOME_USER if this is self-service account creation and we want to
1026 * actually log in as the user we just created, otherwise set it to CREATE_USER_DO_NOT_BECOME_USER
1027 * Set 'uid' to some uid_t value to associate the account with an external auth user, or (-1) for native auth
1029 int create_user(char *username, int become_user, uid_t uid)
1031 struct ctdluser usbuf;
1032 struct ctdlroom qrbuf;
1033 char mailboxname[ROOMNAMELEN];
1038 if ((retval = internal_create_user(username, &usbuf, uid)) != 0) {
1043 * Give the user a private mailbox and a configuration room.
1044 * Make the latter an invisible system room.
1046 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
1047 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
1049 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
1050 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
1051 if (CtdlGetRoomLock(&qrbuf, mailboxname) == 0) {
1052 qrbuf.QRflags2 |= QR2_SYSTEM;
1053 CtdlPutRoomLock(&qrbuf);
1056 /* Perform any create functions registered by server extensions */
1057 PerformUserHooks(&usbuf, EVT_NEWUSER);
1059 /* Everything below this line can be bypassed if administratively
1060 * creating a user, instead of doing self-service account creation
1063 if (become_user == CREATE_USER_BECOME_USER) {
1064 /* Now become the user we just created */
1065 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
1066 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
1069 /* Check to make sure we're still who we think we are */
1070 if (CtdlGetUser(&CC->user, CC->curr_user)) {
1071 return(ERROR + INTERNAL_ERROR);
1076 "New user account <%s> has been created, from host %s [%s].\n",
1081 CtdlAideMessage(buf, "User Creation Notice");
1082 syslog(LOG_NOTICE, "user_ops: <%s> created", username);
1088 * set password - back end api code
1090 void CtdlSetPassword(char *new_pw)
1092 CtdlGetUserLock(&CC->user, CC->curr_user);
1093 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1094 CtdlPutUserLock(&CC->user);
1095 syslog(LOG_INFO, "user_ops: password changed for <%s>", CC->curr_user);
1096 PerformSessionHooks(EVT_SETPASS);
1101 * API function for cmd_invt_kick() and anything else that needs to
1102 * invite or kick out a user to/from a room.
1104 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1106 int CtdlInvtKick(char *iuser, int op) {
1107 struct ctdluser USscratch;
1111 if (CtdlGetUser(&USscratch, iuser) != 0) {
1115 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1117 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1118 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1121 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1122 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1124 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1126 /* post a message in Aide> saying what we just did */
1127 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1129 ((op == 1) ? "invited to" : "kicked out of"),
1131 (CC->logged_in ? CC->user.fullname : "an administrator")
1133 CtdlAideMessage(bbb,"User Admin Message");
1139 * Forget (Zap) the current room (API call)
1140 * Returns 0 on success
1142 int CtdlForgetThisRoom(void) {
1145 /* On some systems, Admins are not allowed to forget rooms */
1146 if (is_aide() && (CtdlGetConfigInt("c_aide_zap") == 0)
1147 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1151 CtdlGetUserLock(&CC->user, CC->curr_user);
1152 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1154 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1155 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1157 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1158 CtdlPutUserLock(&CC->user);
1160 /* Return to the Lobby, so we don't end up in an undefined room */
1161 CtdlUserGoto(CtdlGetConfigStr("c_baseroom"), 0, 0, NULL, NULL, NULL, NULL);
1167 * Traverse the user file...
1169 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1172 struct ctdluser usbuf;
1173 struct cdbdata *cdbus;
1175 cdb_rewind(CDB_USERS);
1177 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1178 memset(&usbuf, 0, sizeof(struct ctdluser));
1179 memcpy(&usbuf, cdbus->ptr,
1180 ((cdbus->len > sizeof(struct ctdluser)) ?
1181 sizeof(struct ctdluser) : cdbus->len));
1183 (*CallBack) (&usbuf, in_data);
1189 * List one user (this works with cmd_list)
1191 void ListThisUser(struct ctdluser *usbuf, void *data)
1195 searchstring = (char *)data;
1196 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1200 if (usbuf->axlevel > AxDeleted) {
1201 if ((CC->user.axlevel >= AxAideU)
1202 || ((usbuf->flags & US_UNLISTED) == 0)
1203 || ((CC->internal_pgm))) {
1204 cprintf("%s|%d|%ld|%ld|%ld|%ld||\n",
1208 (long)usbuf->lastcall,
1217 * Count the number of new mail messages the user has
1221 int num_newmsgs = 0;
1222 num_newmsgs = CC->newmail;
1224 return(num_newmsgs);
1229 * Count the number of new mail messages the user has
1231 int InitialMailCheck()
1233 int num_newmsgs = 0;
1235 char mailboxname[ROOMNAMELEN];
1236 struct ctdlroom mailbox;
1238 struct cdbdata *cdbfr;
1239 long *msglist = NULL;
1242 CtdlMailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1243 if (CtdlGetRoom(&mailbox, mailboxname) != 0)
1245 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1247 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1249 if (cdbfr != NULL) {
1250 msglist = malloc(cdbfr->len);
1251 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1252 num_msgs = cdbfr->len / sizeof(long);
1256 for (a = 0; a < num_msgs; ++a) {
1257 if (msglist[a] > 0L) {
1258 if (msglist[a] > vbuf.v_lastseen) {
1263 if (msglist != NULL)
1266 return(num_newmsgs);