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)
371 || (CC->room.QRroomaide == CC->user.usernum)) {
380 * CtdlGetUserByNumber() - get user by number
381 * returns 0 if user was found
383 * Note: fetching a user this way requires one additional database operation.
385 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number)
387 struct cdbdata *cdbun;
390 cdbun = cdb_fetch(CDB_USERSBYNUMBER, &number, sizeof(long));
392 syslog(LOG_INFO, "user_ops: %ld not found", number);
396 syslog(LOG_INFO, "user_ops: %ld maps to %s", number, cdbun->ptr);
397 r = CtdlGetUser(usbuf, cdbun->ptr);
404 * Helper function for rebuild_usersbynumber()
406 void rebuild_ubn_for_user(struct ctdluser *usbuf, void *data) {
409 struct ubnlist *next;
410 char username[USERNAME_SIZE];
414 static struct ubnlist *u = NULL;
415 struct ubnlist *ptr = NULL;
417 /* Lazy programming here. Call this function as a ForEachUser backend
418 * in order to queue up the room names, or call it with a null user
419 * to make it do the processing.
422 ptr = (struct ubnlist *) malloc(sizeof (struct ubnlist));
423 if (ptr == NULL) return;
425 ptr->usernum = usbuf->usernum;
426 safestrncpy(ptr->username, usbuf->fullname, sizeof ptr->username);
433 syslog(LOG_DEBUG, "user_ops: rebuilding usersbynumber index %10ld : %s", u->usernum, u->username);
434 cdb_store(CDB_USERSBYNUMBER, &u->usernum, sizeof(long), u->username, strlen(u->username)+1);
443 * Rebuild the users-by-number index
445 void rebuild_usersbynumber(void) {
446 cdb_trunc(CDB_USERSBYNUMBER); /* delete the old indices */
447 ForEachUser(rebuild_ubn_for_user, NULL); /* enumerate the users */
448 rebuild_ubn_for_user(NULL, NULL); /* and index them */
453 * getuserbyuid() - get user by system uid (for PAM mode authentication)
454 * returns 0 if user was found
456 * WARNING: don't use this function unless you absolutely have to. It does
457 * a sequential search and therefore is computationally expensive.
459 * FIXME: build an index, dummy.
461 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
463 struct cdbdata *cdbus;
465 cdb_rewind(CDB_USERS);
467 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
468 memset(usbuf, 0, sizeof(struct ctdluser));
469 memcpy(usbuf, cdbus->ptr,
470 ((cdbus->len > sizeof(struct ctdluser)) ?
471 sizeof(struct ctdluser) : cdbus->len));
473 if (usbuf->uid == number) {
474 cdb_close_cursor(CDB_USERS);
483 * Back end for cmd_user() and its ilk
485 * NOTE: "authname" should only be used if we are attempting to use the "master user" feature
487 int CtdlLoginExistingUser(char *authname, const char *trythisname)
492 syslog(LOG_DEBUG, "user_ops: CtdlLoginExistingUser(%s, %s)", authname, trythisname);
494 if ((CC->logged_in)) {
495 return login_already_logged_in;
498 if (trythisname == NULL) return login_not_found;
500 if (!strncasecmp(trythisname, "SYS_", 4))
502 syslog(LOG_DEBUG, "user_ops: system user \"%s\" is not allowed to log in.", trythisname);
503 return login_not_found;
506 /* If a "master user" is defined, handle its authentication if specified */
508 if ( (!IsEmptyStr(CtdlGetConfigStr("c_master_user"))) &&
509 (!IsEmptyStr(CtdlGetConfigStr("c_master_pass"))) &&
510 (authname != NULL) &&
511 (!strcasecmp(authname, CtdlGetConfigStr("c_master_user"))) )
516 /* Continue attempting user validation... */
517 safestrncpy(username, trythisname, sizeof (username));
520 if (IsEmptyStr(username)) {
521 return login_not_found;
524 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
529 struct passwd *tempPwdPtr;
532 syslog(LOG_DEBUG, "user_ops: asking host about <%s>", username);
533 #ifdef HAVE_GETPWNAM_R
534 #ifdef SOLARIS_GETPWUID
535 syslog(LOG_DEBUG, "user_ops: calling getpwnam_r()");
536 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
537 #else // SOLARIS_GETPWUID
538 syslog(LOG_DEBUG, "user_ops: calling getpwnam_r()");
539 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
540 #endif // SOLARIS_GETPWUID
541 #else // HAVE_GETPWNAM_R
542 syslog(LOG_DEBUG, "user_ops: SHOULD NEVER GET HERE!!!");
544 #endif // HAVE_GETPWNAM_R
545 if (tempPwdPtr == NULL) {
546 syslog(LOG_DEBUG, "user_ops: no such user <%s>", username);
547 return login_not_found;
550 /* Locate the associated Citadel account.
551 * If not found, make one attempt to create it.
553 found_user = getuserbyuid(&CC->user, pd.pw_uid);
554 syslog(LOG_DEBUG, "user_ops: found it: uid=%ld, gecos=%s here: %d", (long)pd.pw_uid, pd.pw_gecos, found_user);
555 if (found_user != 0) {
556 create_user(username, 0);
557 found_user = getuserbyuid(&CC->user, pd.pw_uid);
563 else if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
571 found_user = CtdlTryUserLDAP(username, ldap_dn, sizeof ldap_dn, ldap_cn, sizeof ldap_cn, &ldap_uid, 0);
572 if (found_user != 0) {
573 return login_not_found;
576 found_user = getuserbyuid(&CC->user, ldap_uid);
577 if (found_user != 0) {
578 create_user(username, 0);
579 found_user = getuserbyuid(&CC->user, ldap_uid);
582 if (found_user == 0) {
583 if (CC->ldap_dn != NULL) free(CC->ldap_dn);
584 CC->ldap_dn = strdup(ldap_dn);
591 /* native auth mode */
593 recptypes *valid = NULL;
595 /* First, try to log in as if the supplied name is a display name */
596 found_user = CtdlGetUser(&CC->user, username);
598 /* If that didn't work, try to log in as if the supplied name
599 * is an e-mail address
601 if (found_user != 0) {
602 valid = validate_recipients(username, NULL, 0);
604 if (valid->num_local == 1) {
605 found_user = CtdlGetUser(&CC->user, valid->recp_local);
607 free_recipients(valid);
612 /* Did we find something? */
613 if (found_user == 0) {
614 if (((CC->nologin)) && (CC->user.axlevel < AxAideU)) {
615 return login_too_many_users;
617 safestrncpy(CC->curr_user, CC->user.fullname, sizeof CC->curr_user);
621 return login_not_found;
626 * session startup code which is common to both cmd_pass() and cmd_newu()
630 struct CitContext *CCC = CC;
633 syslog(LOG_NOTICE, "user_ops: <%s> logged in", CCC->curr_user);
635 CtdlGetUserLock(&CCC->user, CCC->curr_user);
636 ++(CCC->user.timescalled);
637 CCC->previous_login = CCC->user.lastcall;
638 time(&CCC->user.lastcall);
640 /* If this user's name is the name of the system administrator
641 * (as specified in setup), automatically assign access level 6.
643 if (!strcasecmp(CCC->user.fullname, CtdlGetConfigStr("c_sysadm"))) {
644 CCC->user.axlevel = AxAideU;
647 /* If we're authenticating off the host system, automatically give
648 * root the highest level of access.
650 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
651 if (CCC->user.uid == 0) {
652 CCC->user.axlevel = AxAideU;
657 * If we are using LDAP authentication, extract the user's email addresses from the directory.
658 * FIXME make this a site configurable setting
661 if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
662 char new_emailaddrs[512];
663 if (extract_email_addresses_from_ldap(CCC->ldap_dn, new_emailaddrs) == 0) {
664 strcpy(CCC->user.emailaddrs, new_emailaddrs);
670 * No email address for user? Make one up.
672 if (IsEmptyStr(CCC->user.emailaddrs)) {
673 sprintf(CCC->user.emailaddrs, "cit%ld@%s", CCC->user.usernum, CtdlGetConfigStr("c_fqdn"));
676 CtdlPutUserLock(&CCC->user);
679 * Populate cs_inet_email and cs_inet_other_emails with valid email addresses from the user record
681 strcpy(CCC->cs_inet_email, CCC->user.emailaddrs);
682 char *firstsep = strstr(CCC->cs_inet_email, "|");
684 strcpy(CCC->cs_inet_other_emails, firstsep+1);
688 CCC->cs_inet_other_emails[0] = 0;
691 /* Create any personal rooms required by the system.
692 * (Technically, MAILROOM should be there already, but just in case...)
694 CtdlCreateRoom(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
695 CtdlCreateRoom(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
696 CtdlCreateRoom(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
697 CtdlCreateRoom(USERDRAFTROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
699 /* Run any startup routines registered by loadable modules */
700 PerformSessionHooks(EVT_LOGIN);
702 /* Enter the lobby */
703 CtdlUserGoto(CtdlGetConfigStr("c_baseroom"), 0, 0, NULL, NULL, NULL, NULL);
707 void logged_in_response(void)
709 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
710 CIT_OK, CC->user.fullname, CC->user.axlevel,
711 CC->user.timescalled, CC->user.posted,
712 CC->user.flags, CC->user.usernum,
718 void CtdlUserLogout(void)
720 CitContext *CCC = MyContext();
722 syslog(LOG_DEBUG, "user_ops: CtdlUserLogout() logging out <%s> from session %d", CCC->curr_user, CCC->cs_pid);
724 /* Run any hooks registered by modules... */
725 PerformSessionHooks(EVT_LOGOUT);
728 * Clear out some session data. Most likely, the CitContext for this
729 * session is about to get nuked when the session disconnects, but
730 * since it's possible to log in again without reconnecting, we cannot
731 * make that assumption.
733 strcpy(CCC->fake_username, "");
734 strcpy(CCC->fake_hostname, "");
735 strcpy(CCC->fake_roomname, "");
738 /* Check to see if the user was deleted whilst logged in and purge them if necessary */
739 if ((CCC->user.axlevel == AxDeleted) && (CCC->user.usernum)) {
740 purge_user(CCC->user.fullname);
743 /* Clear out the user record in memory so we don't behave like a ghost */
744 memset(&CCC->user, 0, sizeof(struct ctdluser));
745 CCC->curr_user[0] = 0;
747 CCC->cs_inet_email[0] = 0;
748 CCC->cs_inet_other_emails[0] = 0;
749 CCC->cs_inet_fn[0] = 0;
750 CCC->fake_username[0] = 0;
751 CCC->fake_hostname[0] = 0;
752 CCC->fake_roomname[0] = 0;
754 /* Free any output buffers */
760 * Validate a password on the host unix system by talking to the chkpwd daemon
762 static int validpw(uid_t uid, const char *pass)
767 if (IsEmptyStr(pass)) {
768 syslog(LOG_DEBUG, "user_ops: refusing to chkpwd for uid=%d with empty password", uid);
772 syslog(LOG_DEBUG, "user_ops: validating password for uid=%d using chkpwd...", uid);
774 begin_critical_section(S_CHKPWD);
775 rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
777 syslog(LOG_ERR, "user_ops: communication with chkpwd broken: %m");
778 end_critical_section(S_CHKPWD);
781 rv = write(chkpwd_write_pipe[1], pass, 256);
783 syslog(LOG_ERR, "user_ops: communication with chkpwd broken: %m");
784 end_critical_section(S_CHKPWD);
787 rv = read(chkpwd_read_pipe[0], buf, 4);
789 syslog(LOG_ERR, "user_ops: ommunication with chkpwd broken: %m");
790 end_critical_section(S_CHKPWD);
793 end_critical_section(S_CHKPWD);
795 if (!strncmp(buf, "PASS", 4)) {
796 syslog(LOG_DEBUG, "user_ops: chkpwd pass");
800 syslog(LOG_DEBUG, "user_ops: chkpwd fail");
806 * Start up the chkpwd daemon so validpw() has something to talk to
808 void start_chkpwd_daemon(void) {
810 struct stat filestats;
813 syslog(LOG_DEBUG, "user_ops: starting chkpwd daemon for host authentication mode");
815 if ((stat(file_chkpwd, &filestats)==-1) || (filestats.st_size==0)) {
816 syslog(LOG_ERR, "user_ops: %s: %m", file_chkpwd);
819 if (pipe(chkpwd_write_pipe) != 0) {
820 syslog(LOG_ERR, "user_ops: unable to create pipe for chkpwd daemon: %m");
823 if (pipe(chkpwd_read_pipe) != 0) {
824 syslog(LOG_ERR, "user_ops: unable to create pipe for chkpwd daemon: %m");
829 if (chkpwd_pid < 0) {
830 syslog(LOG_ERR, "user_ops: unable to fork chkpwd daemon: %m");
833 if (chkpwd_pid == 0) {
834 dup2(chkpwd_write_pipe[0], 0);
835 dup2(chkpwd_read_pipe[1], 1);
836 for (i=2; i<256; ++i) close(i);
837 execl(file_chkpwd, file_chkpwd, NULL);
838 syslog(LOG_ERR, "user_ops: unable to exec chkpwd daemon: %m");
845 int CtdlTryPassword(const char *password, long len)
848 CitContext *CCC = CC;
850 if ((CCC->logged_in)) {
851 syslog(LOG_WARNING, "user_ops: CtdlTryPassword: already logged in");
852 return pass_already_logged_in;
854 if (!strcmp(CCC->curr_user, NLI)) {
855 syslog(LOG_WARNING, "user_ops: CtdlTryPassword: no user selected");
858 if (CtdlGetUser(&CCC->user, CCC->curr_user)) {
859 syslog(LOG_ERR, "user_ops: CtdlTryPassword: internal error");
860 return pass_internal_error;
862 if (password == NULL) {
863 syslog(LOG_INFO, "user_ops: CtdlTryPassword: NULL password string supplied");
864 return pass_wrong_password;
867 if (CCC->is_master) {
868 code = strcmp(password, CtdlGetConfigStr("c_master_pass"));
871 else if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
875 if (validpw(CCC->user.uid, password)) {
879 * sooper-seekrit hack: populate the password field in the
880 * citadel database with the password that the user typed,
881 * if it's correct. This allows most sites to convert from
882 * host auth to native auth if they want to. If you think
883 * this is a security hazard, comment it out.
886 CtdlGetUserLock(&CCC->user, CCC->curr_user);
887 safestrncpy(CCC->user.password, password, sizeof CCC->user.password);
888 CtdlPutUserLock(&CCC->user);
891 * (sooper-seekrit hack ends here)
900 else if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
904 if ((CCC->ldap_dn) && (!CtdlTryPasswordLDAP(CCC->ldap_dn, password))) {
915 /* native auth mode */
918 pw = (char*) malloc(len + 1);
919 memcpy(pw, password, len + 1);
921 strproc(CCC->user.password);
922 code = strcasecmp(CCC->user.password, pw);
925 strproc(CCC->user.password);
926 code = strcasecmp(CCC->user.password, pw);
936 syslog(LOG_WARNING, "user_ops: bad password specified for <%s> Service <%s> Port <%ld> Remote <%s / %s>",
943 return pass_wrong_password;
949 * Delete a user record *and* all of its related resources.
951 int purge_user(char pname[])
953 struct ctdluser usbuf;
954 char usernamekey[USERNAME_SIZE];
956 makeuserkey(usernamekey, pname, cutuserkey(pname));
958 /* If the name is empty we can't find them in the DB any way so just return */
959 if (IsEmptyStr(pname))
960 return (ERROR + NO_SUCH_USER);
962 if (CtdlGetUser(&usbuf, pname) != 0) {
963 syslog(LOG_ERR, "user_ops: cannot purge user <%s> - not found", pname);
964 return (ERROR + NO_SUCH_USER);
966 /* Don't delete a user who is currently logged in. Instead, just
967 * set the access level to 0, and let the account get swept up
968 * during the next purge.
970 if (CtdlIsUserLoggedInByNum(usbuf.usernum)) {
971 syslog(LOG_WARNING, "user_ops: <%s> is logged in; not deleting", pname);
972 usbuf.axlevel = AxDeleted;
976 syslog(LOG_NOTICE, "user_ops: deleting <%s>", pname);
980 * This should all be wrapped in a S_USERS mutex.
981 * Without the mutex the user could log in before we get to the next function
982 * That would truly mess things up :-(
983 * I would like to see the S_USERS start before the CtdlIsUserLoggedInByNum() above
984 * and end after the user has been deleted from the database, below.
985 * Question is should we enter the EVT_PURGEUSER while S_USERS is active?
988 /* Perform any purge functions registered by server extensions */
989 PerformUserHooks(&usbuf, EVT_PURGEUSER);
991 /* delete any existing user/room relationships */
992 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
994 /* delete the users-by-number index record */
995 cdb_delete(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long));
997 /* delete the userlog entry */
998 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
1004 int internal_create_user(char *username, struct ctdluser *usbuf, uid_t uid)
1006 if (!CtdlGetUser(usbuf, username)) {
1007 return (ERROR + ALREADY_EXISTS);
1010 /* Go ahead and initialize a new user record */
1011 memset(usbuf, 0, sizeof(struct ctdluser));
1012 safestrncpy(usbuf->fullname, username, sizeof usbuf->fullname);
1013 strcpy(usbuf->password, "");
1016 /* These are the default flags on new accounts */
1017 usbuf->flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
1019 usbuf->timescalled = 0;
1021 usbuf->axlevel = CtdlGetConfigInt("c_initax");
1022 usbuf->lastcall = time(NULL);
1024 /* fetch a new user number */
1025 usbuf->usernum = get_new_user_number();
1027 /* add user to the database */
1029 cdb_store(CDB_USERSBYNUMBER, &usbuf->usernum, sizeof(long), usbuf->fullname, strlen(usbuf->fullname)+1);
1036 * create_user() - back end processing to create a new user
1038 * Set 'newusername' to the desired account name.
1039 * Set 'become_user' to nonzero if this is self-service account creation and we want
1040 * to actually log in as the user we just created, otherwise set it to 0.
1042 int create_user(const char *newusername, int become_user)
1044 struct ctdluser usbuf;
1045 struct ctdlroom qrbuf;
1047 char mailboxname[ROOMNAMELEN];
1052 safestrncpy(username, newusername, sizeof username);
1055 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
1057 /* host auth mode */
1060 struct passwd *tempPwdPtr;
1061 char pwdbuffer[SIZ];
1063 #ifdef HAVE_GETPWNAM_R
1064 #ifdef SOLARIS_GETPWUID
1065 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof(pwdbuffer));
1066 #else // SOLARIS_GETPWUID
1067 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
1068 #endif // SOLARIS_GETPWUID
1069 #else // HAVE_GETPWNAM_R
1071 #endif // HAVE_GETPWNAM_R
1072 if (tempPwdPtr != NULL) {
1073 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
1075 if (IsEmptyStr (username))
1077 safestrncpy(username, pd.pw_name, sizeof username);
1081 return (ERROR + NO_SUCH_USER);
1086 if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
1087 if (CtdlTryUserLDAP(username, NULL, 0, username, sizeof username, &uid, 0) != 0) {
1088 return(ERROR + NO_SUCH_USER);
1091 #endif /* HAVE_LDAP */
1093 if ((retval = internal_create_user(username, &usbuf, uid)) != 0)
1097 * Give the user a private mailbox and a configuration room.
1098 * Make the latter an invisible system room.
1100 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
1101 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
1103 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
1104 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
1105 if (CtdlGetRoomLock(&qrbuf, mailboxname) == 0) {
1106 qrbuf.QRflags2 |= QR2_SYSTEM;
1107 CtdlPutRoomLock(&qrbuf);
1110 /* Perform any create functions registered by server extensions */
1111 PerformUserHooks(&usbuf, EVT_NEWUSER);
1113 /* Everything below this line can be bypassed if administratively
1114 * creating a user, instead of doing self-service account creation
1118 /* Now become the user we just created */
1119 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
1120 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
1123 /* Check to make sure we're still who we think we are */
1124 if (CtdlGetUser(&CC->user, CC->curr_user)) {
1125 return (ERROR + INTERNAL_ERROR);
1130 "New user account <%s> has been created, from host %s [%s].\n",
1135 CtdlAideMessage(buf, "User Creation Notice");
1136 syslog(LOG_NOTICE, "user_ops: <%s> created", username);
1142 * set password - back end api code
1144 void CtdlSetPassword(char *new_pw)
1146 CtdlGetUserLock(&CC->user, CC->curr_user);
1147 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1148 CtdlPutUserLock(&CC->user);
1149 syslog(LOG_INFO, "user_ops: password changed for <%s>", CC->curr_user);
1150 PerformSessionHooks(EVT_SETPASS);
1155 * API function for cmd_invt_kick() and anything else that needs to
1156 * invite or kick out a user to/from a room.
1158 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1160 int CtdlInvtKick(char *iuser, int op) {
1161 struct ctdluser USscratch;
1165 if (CtdlGetUser(&USscratch, iuser) != 0) {
1169 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1171 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1172 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1175 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1176 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1178 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1180 /* post a message in Aide> saying what we just did */
1181 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1183 ((op == 1) ? "invited to" : "kicked out of"),
1185 (CC->logged_in ? CC->user.fullname : "an administrator")
1187 CtdlAideMessage(bbb,"User Admin Message");
1194 * Forget (Zap) the current room (API call)
1195 * Returns 0 on success
1197 int CtdlForgetThisRoom(void) {
1200 /* On some systems, Admins are not allowed to forget rooms */
1201 if (is_aide() && (CtdlGetConfigInt("c_aide_zap") == 0)
1202 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1206 CtdlGetUserLock(&CC->user, CC->curr_user);
1207 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1209 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1210 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1212 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1213 CtdlPutUserLock(&CC->user);
1215 /* Return to the Lobby, so we don't end up in an undefined room */
1216 CtdlUserGoto(CtdlGetConfigStr("c_baseroom"), 0, 0, NULL, NULL, NULL, NULL);
1223 * Traverse the user file...
1225 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1228 struct ctdluser usbuf;
1229 struct cdbdata *cdbus;
1231 cdb_rewind(CDB_USERS);
1233 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1234 memset(&usbuf, 0, sizeof(struct ctdluser));
1235 memcpy(&usbuf, cdbus->ptr,
1236 ((cdbus->len > sizeof(struct ctdluser)) ?
1237 sizeof(struct ctdluser) : cdbus->len));
1239 (*CallBack) (&usbuf, in_data);
1245 * List one user (this works with cmd_list)
1247 void ListThisUser(struct ctdluser *usbuf, void *data)
1251 searchstring = (char *)data;
1252 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1256 if (usbuf->axlevel > AxDeleted) {
1257 if ((CC->user.axlevel >= AxAideU)
1258 || ((usbuf->flags & US_UNLISTED) == 0)
1259 || ((CC->internal_pgm))) {
1260 cprintf("%s|%d|%ld|%ld|%ld|%ld||\n",
1264 (long)usbuf->lastcall,
1273 * Count the number of new mail messages the user has
1277 int num_newmsgs = 0;
1279 num_newmsgs = CC->newmail;
1282 return (num_newmsgs);
1287 * Count the number of new mail messages the user has
1289 int InitialMailCheck()
1291 int num_newmsgs = 0;
1293 char mailboxname[ROOMNAMELEN];
1294 struct ctdlroom mailbox;
1296 struct cdbdata *cdbfr;
1297 long *msglist = NULL;
1300 CtdlMailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1301 if (CtdlGetRoom(&mailbox, mailboxname) != 0)
1303 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1305 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1307 if (cdbfr != NULL) {
1308 msglist = malloc(cdbfr->len);
1309 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1310 num_msgs = cdbfr->len / sizeof(long);
1314 for (a = 0; a < num_msgs; ++a) {
1315 if (msglist[a] > 0L) {
1316 if (msglist[a] > vbuf.v_lastseen) {
1321 if (msglist != NULL)
1324 return (num_newmsgs);