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 CtdlGetUserLen(struct ctdluser *usbuf, const char *name, long len)
41 char usernamekey[USERNAME_SIZE];
42 struct cdbdata *cdbus;
45 memset(usbuf, 0, sizeof(struct ctdluser));
48 makeuserkey(usernamekey, name, len);
49 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
51 if (cdbus == NULL) { /* user not found */
55 memcpy(usbuf, cdbus->ptr,
56 ((cdbus->len > sizeof(struct ctdluser)) ?
57 sizeof(struct ctdluser) : cdbus->len));
65 int CtdlGetUser(struct ctdluser *usbuf, char *name)
67 return CtdlGetUserLen(usbuf, name, cutuserkey(name));
71 int CtdlLockGetCurrentUser(void)
74 return CtdlGetUserLen(&CCC->user, CCC->curr_user, cutuserkey(CCC->curr_user));
79 * CtdlGetUserLock() - same as getuser() but locks the record
81 int CtdlGetUserLock(struct ctdluser *usbuf, char *name)
85 retcode = CtdlGetUser(usbuf, name);
87 begin_critical_section(S_USERS);
94 * CtdlPutUser() - write user buffer into the correct place on disk
96 void CtdlPutUser(struct ctdluser *usbuf)
98 char usernamekey[USERNAME_SIZE];
100 makeuserkey(usernamekey, usbuf->fullname, cutuserkey(usbuf->fullname));
101 usbuf->version = REV_LEVEL;
102 cdb_store(CDB_USERS, usernamekey, strlen(usernamekey), usbuf, sizeof(struct ctdluser));
106 void CtdlPutCurrentUserLock()
108 CtdlPutUser(&CC->user);
113 * CtdlPutUserLock() - same as putuser() but locks the record
115 void CtdlPutUserLock(struct ctdluser *usbuf)
118 end_critical_section(S_USERS);
123 * rename_user() - this is tricky because the user's display name is the database key
125 * Returns 0 on success or nonzero if there was an error...
128 int rename_user(char *oldname, char *newname) {
129 int retcode = RENAMEUSER_OK;
130 struct ctdluser usbuf;
132 char oldnamekey[USERNAME_SIZE];
133 char newnamekey[USERNAME_SIZE];
135 /* Create the database keys... */
136 makeuserkey(oldnamekey, oldname, cutuserkey(oldname));
137 makeuserkey(newnamekey, newname, cutuserkey(newname));
139 /* Lock up and get going */
140 begin_critical_section(S_USERS);
142 /* We cannot rename a user who is currently logged in */
143 if (CtdlIsUserLoggedIn(oldname)) {
144 end_critical_section(S_USERS);
145 return RENAMEUSER_LOGGED_IN;
148 if (CtdlGetUser(&usbuf, newname) == 0) {
149 retcode = RENAMEUSER_ALREADY_EXISTS;
153 if (CtdlGetUser(&usbuf, oldname) != 0) {
154 retcode = RENAMEUSER_NOT_FOUND;
157 else { /* Sanity checks succeeded. Now rename the user. */
158 if (usbuf.usernum == 0)
160 syslog(LOG_DEBUG, "user_ops: can not rename user \"Citadel\".");
161 retcode = RENAMEUSER_NOT_FOUND;
163 syslog(LOG_DEBUG, "user_ops: renaming <%s> to <%s>", oldname, newname);
164 cdb_delete(CDB_USERS, oldnamekey, strlen(oldnamekey));
165 safestrncpy(usbuf.fullname, newname, sizeof usbuf.fullname);
167 cdb_store(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long), usbuf.fullname, strlen(usbuf.fullname)+1 );
168 retcode = RENAMEUSER_OK;
174 end_critical_section(S_USERS);
180 * Index-generating function used by Ctdl[Get|Set]Relationship
182 int GenerateRelationshipIndex(char *IndexBuf,
194 TheIndex.iRoomID = RoomID;
195 TheIndex.iRoomGen = RoomGen;
196 TheIndex.iUserID = UserID;
198 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
199 return (sizeof(TheIndex));
204 * Back end for CtdlSetRelationship()
206 void put_visit(visit *newvisit)
211 memset (IndexBuf, 0, sizeof (IndexBuf));
212 /* Generate an index */
213 IndexLen = GenerateRelationshipIndex(IndexBuf, newvisit->v_roomnum, newvisit->v_roomgen, newvisit->v_usernum);
215 /* Store the record */
216 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
217 newvisit, sizeof(visit)
223 * Define a relationship between a user and a room
225 void CtdlSetRelationship(visit *newvisit,
226 struct ctdluser *rel_user,
227 struct ctdlroom *rel_room)
229 /* We don't use these in Citadel because they're implicit by the
230 * index, but they must be present if the database is exported.
232 newvisit->v_roomnum = rel_room->QRnumber;
233 newvisit->v_roomgen = rel_room->QRgen;
234 newvisit->v_usernum = rel_user->usernum;
241 * Locate a relationship between a user and a room
243 void CtdlGetRelationship(visit *vbuf,
244 struct ctdluser *rel_user,
245 struct ctdlroom *rel_room)
249 struct cdbdata *cdbvisit;
251 /* Generate an index */
252 IndexLen = GenerateRelationshipIndex(IndexBuf,
257 /* Clear out the buffer */
258 memset(vbuf, 0, sizeof(visit));
260 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
261 if (cdbvisit != NULL) {
262 memcpy(vbuf, cdbvisit->ptr,
263 ((cdbvisit->len > sizeof(visit)) ?
264 sizeof(visit) : cdbvisit->len));
268 /* If this is the first time the user has seen this room,
269 * set the view to be the default for the room.
271 vbuf->v_view = rel_room->QRdefaultview;
274 /* Set v_seen if necessary */
275 if (vbuf->v_seen[0] == 0) {
276 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
281 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
283 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
287 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
289 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
294 * Check to see if the specified user has Internet mail permission
295 * (returns nonzero if permission is granted)
297 int CtdlCheckInternetMailPermission(struct ctdluser *who) {
299 /* Do not allow twits to send Internet mail */
300 if (who->axlevel <= AxProbU) return(0);
302 /* Globally enabled? */
303 if (CtdlGetConfigInt("c_restrict") == 0) return(1);
305 /* User flagged ok? */
306 if (who->flags & US_INTERNET) return(2);
308 /* Admin level access? */
309 if (who->axlevel >= AxAideU) return(3);
311 /* No mail for you! */
317 * Convenience function.
319 int CtdlAccessCheck(int required_level)
321 if (CC->internal_pgm) return(0);
322 if (required_level >= ac_internal) {
323 cprintf("%d This is not a user-level command.\n", ERROR + HIGHER_ACCESS_REQUIRED);
327 if ((required_level >= ac_logged_in_or_guest) && (CC->logged_in == 0) && (CtdlGetConfigInt("c_guest_logins") == 0)) {
328 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
332 if ((required_level >= ac_logged_in) && (CC->logged_in == 0)) {
333 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
337 if (CC->user.axlevel >= AxAideU) return(0);
338 if (required_level >= ac_aide) {
339 cprintf("%d This command requires Admin access.\n",
340 ERROR + HIGHER_ACCESS_REQUIRED);
344 if (is_room_aide()) return(0);
345 if (required_level >= ac_room_aide) {
346 cprintf("%d This command requires Admin or Room Admin access.\n",
347 ERROR + HIGHER_ACCESS_REQUIRED);
351 /* shhh ... succeed quietly */
357 * Is the user currently logged in an Admin?
361 if (CC->user.axlevel >= AxAideU)
369 * Is the user currently logged in an Admin *or* the room Admin for this room?
371 int is_room_aide(void)
374 if (!CC->logged_in) {
378 if ((CC->user.axlevel >= AxAideU)
379 || (CC->room.QRroomaide == CC->user.usernum)) {
388 * CtdlGetUserByNumber() - get user by number
389 * returns 0 if user was found
391 * Note: fetching a user this way requires one additional database operation.
393 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number)
395 struct cdbdata *cdbun;
398 cdbun = cdb_fetch(CDB_USERSBYNUMBER, &number, sizeof(long));
400 syslog(LOG_INFO, "user_ops: %ld not found", number);
404 syslog(LOG_INFO, "user_ops: %ld maps to %s", number, cdbun->ptr);
405 r = CtdlGetUser(usbuf, cdbun->ptr);
412 * Helper function for rebuild_usersbynumber()
414 void rebuild_ubn_for_user(struct ctdluser *usbuf, void *data) {
417 struct ubnlist *next;
418 char username[USERNAME_SIZE];
422 static struct ubnlist *u = NULL;
423 struct ubnlist *ptr = NULL;
425 /* Lazy programming here. Call this function as a ForEachUser backend
426 * in order to queue up the room names, or call it with a null user
427 * to make it do the processing.
430 ptr = (struct ubnlist *) malloc(sizeof (struct ubnlist));
431 if (ptr == NULL) return;
433 ptr->usernum = usbuf->usernum;
434 safestrncpy(ptr->username, usbuf->fullname, sizeof ptr->username);
441 syslog(LOG_DEBUG, "user_ops: rebuilding usersbynumber index %10ld : %s", u->usernum, u->username);
442 cdb_store(CDB_USERSBYNUMBER, &u->usernum, sizeof(long), u->username, strlen(u->username)+1);
451 * Rebuild the users-by-number index
453 void rebuild_usersbynumber(void) {
454 cdb_trunc(CDB_USERSBYNUMBER); /* delete the old indices */
455 ForEachUser(rebuild_ubn_for_user, NULL); /* enumerate the users */
456 rebuild_ubn_for_user(NULL, NULL); /* and index them */
461 * getuserbyuid() - get user by system uid (for PAM mode authentication)
462 * returns 0 if user was found
464 * WARNING: don't use this function unless you absolutely have to. It does
465 * a sequential search and therefore is computationally expensive.
467 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
469 struct cdbdata *cdbus;
471 cdb_rewind(CDB_USERS);
473 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
474 memset(usbuf, 0, sizeof(struct ctdluser));
475 memcpy(usbuf, cdbus->ptr,
476 ((cdbus->len > sizeof(struct ctdluser)) ?
477 sizeof(struct ctdluser) : cdbus->len));
479 if (usbuf->uid == number) {
480 cdb_close_cursor(CDB_USERS);
489 * Back end for cmd_user() and its ilk
491 * NOTE: "authname" should only be used if we are attempting to use the "master user" feature
493 int CtdlLoginExistingUser(char *authname, const char *trythisname)
499 syslog(LOG_DEBUG, "user_ops: CtdlLoginExistingUser(%s, %s)", authname, trythisname);
501 if ((CC->logged_in)) {
502 return login_already_logged_in;
505 if (trythisname == NULL) return login_not_found;
507 if (!strncasecmp(trythisname, "SYS_", 4))
509 syslog(LOG_DEBUG, "user_ops: system user \"%s\" is not allowed to log in.", trythisname);
510 return login_not_found;
513 /* If a "master user" is defined, handle its authentication if specified */
515 if ( (!IsEmptyStr(CtdlGetConfigStr("c_master_user"))) &&
516 (!IsEmptyStr(CtdlGetConfigStr("c_master_pass"))) &&
517 (authname != NULL) &&
518 (!strcasecmp(authname, CtdlGetConfigStr("c_master_user"))) )
523 /* Continue attempting user validation... */
524 safestrncpy(username, trythisname, sizeof (username));
526 len = cutuserkey(username);
528 if (IsEmptyStr(username)) {
529 return login_not_found;
532 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
537 struct passwd *tempPwdPtr;
540 syslog(LOG_DEBUG, "user_ops: asking host about <%s>", username);
541 #ifdef HAVE_GETPWNAM_R
542 #ifdef SOLARIS_GETPWUID
543 syslog(LOG_DEBUG, "user_ops: calling getpwnam_r()");
544 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
545 #else // SOLARIS_GETPWUID
546 syslog(LOG_DEBUG, "user_ops: calling getpwnam_r()");
547 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
548 #endif // SOLARIS_GETPWUID
549 #else // HAVE_GETPWNAM_R
550 syslog(LOG_DEBUG, "user_ops: SHOULD NEVER GET HERE!!!");
552 #endif // HAVE_GETPWNAM_R
553 if (tempPwdPtr == NULL) {
554 syslog(LOG_DEBUG, "user_ops: no such user <%s>", username);
555 return login_not_found;
558 /* Locate the associated Citadel account.
559 * If not found, make one attempt to create it.
561 found_user = getuserbyuid(&CC->user, pd.pw_uid);
562 syslog(LOG_DEBUG, "user_ops: found it: uid=%ld, gecos=%s here: %d", (long)pd.pw_uid, pd.pw_gecos, found_user);
563 if (found_user != 0) {
564 len = cutuserkey(username);
565 create_user(username, len, 0);
566 found_user = getuserbyuid(&CC->user, pd.pw_uid);
572 else if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
580 found_user = CtdlTryUserLDAP(username, ldap_dn, sizeof ldap_dn, ldap_cn, sizeof ldap_cn, &ldap_uid, 0);
581 if (found_user != 0) {
582 return login_not_found;
585 found_user = getuserbyuid(&CC->user, ldap_uid);
586 if (found_user != 0) {
587 create_user(username, len, 0);
588 found_user = getuserbyuid(&CC->user, ldap_uid);
591 if (found_user == 0) {
592 if (CC->ldap_dn != NULL) free(CC->ldap_dn);
593 CC->ldap_dn = strdup(ldap_dn);
600 /* native auth mode */
602 recptypes *valid = NULL;
604 /* First, try to log in as if the supplied name is a display name */
605 found_user = CtdlGetUser(&CC->user, username);
607 /* If that didn't work, try to log in as if the supplied name
608 * is an e-mail address
610 if (found_user != 0) {
611 valid = validate_recipients(username, NULL, 0);
613 if (valid->num_local == 1) {
614 found_user = CtdlGetUser(&CC->user, valid->recp_local);
616 free_recipients(valid);
621 /* Did we find something? */
622 if (found_user == 0) {
623 if (((CC->nologin)) && (CC->user.axlevel < AxAideU)) {
624 return login_too_many_users;
626 safestrncpy(CC->curr_user, CC->user.fullname,
627 sizeof CC->curr_user);
631 return login_not_found;
636 * session startup code which is common to both cmd_pass() and cmd_newu()
640 struct CitContext *CCC = CC;
643 syslog(LOG_NOTICE, "user_ops: <%s> logged in", CCC->curr_user);
645 CtdlGetUserLock(&CCC->user, CCC->curr_user);
646 ++(CCC->user.timescalled);
647 CCC->previous_login = CCC->user.lastcall;
648 time(&CCC->user.lastcall);
650 /* If this user's name is the name of the system administrator
651 * (as specified in setup), automatically assign access level 6.
653 if (!strcasecmp(CCC->user.fullname, CtdlGetConfigStr("c_sysadm"))) {
654 CCC->user.axlevel = AxAideU;
657 /* If we're authenticating off the host system, automatically give
658 * root the highest level of access.
660 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
661 if (CCC->user.uid == 0) {
662 CCC->user.axlevel = AxAideU;
666 CtdlPutUserLock(&CCC->user);
669 * No email address for user? Make one up.
671 if (IsEmptyStr(CCC->user.emailaddrs)) {
672 sprintf(CCC->user.emailaddrs, "cit%ld@%s", CCC->user.usernum, CtdlGetConfigStr("c_fqdn"));
676 * Populate cs_inet_email and cs_inet_other_emails with valid email addresses from the user record
678 strcpy(CCC->cs_inet_email, CCC->user.emailaddrs);
679 char *firstsep = strstr(CCC->cs_inet_email, "|");
681 strcpy(CCC->cs_inet_other_emails, firstsep+1);
685 CCC->cs_inet_other_emails[0] = 0;
688 /* Create any personal rooms required by the system.
689 * (Technically, MAILROOM should be there already, but just in case...)
691 CtdlCreateRoom(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
692 CtdlCreateRoom(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
693 CtdlCreateRoom(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
694 CtdlCreateRoom(USERDRAFTROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
696 /* Run any startup routines registered by loadable modules */
697 PerformSessionHooks(EVT_LOGIN);
699 /* Enter the lobby */
700 CtdlUserGoto(CtdlGetConfigStr("c_baseroom"), 0, 0, NULL, NULL, NULL, NULL);
704 void logged_in_response(void)
706 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
707 CIT_OK, CC->user.fullname, CC->user.axlevel,
708 CC->user.timescalled, CC->user.posted,
709 CC->user.flags, CC->user.usernum,
715 void CtdlUserLogout(void)
717 CitContext *CCC = MyContext();
719 syslog(LOG_DEBUG, "user_ops: CtdlUserLogout() logging out <%s> from session %d", CCC->curr_user, CCC->cs_pid);
721 /* Run any hooks registered by modules... */
722 PerformSessionHooks(EVT_LOGOUT);
725 * Clear out some session data. Most likely, the CitContext for this
726 * session is about to get nuked when the session disconnects, but
727 * since it's possible to log in again without reconnecting, we cannot
728 * make that assumption.
730 strcpy(CCC->fake_username, "");
731 strcpy(CCC->fake_hostname, "");
732 strcpy(CCC->fake_roomname, "");
735 /* Check to see if the user was deleted whilst logged in and purge them if necessary */
736 if ((CCC->user.axlevel == AxDeleted) && (CCC->user.usernum)) {
737 purge_user(CCC->user.fullname);
740 /* Clear out the user record in memory so we don't behave like a ghost */
741 memset(&CCC->user, 0, sizeof(struct ctdluser));
742 CCC->curr_user[0] = 0;
744 CCC->cs_inet_email[0] = 0;
745 CCC->cs_inet_other_emails[0] = 0;
746 CCC->cs_inet_fn[0] = 0;
747 CCC->fake_username[0] = 0;
748 CCC->fake_hostname[0] = 0;
749 CCC->fake_roomname[0] = 0;
751 /* Free any output buffers */
757 * Validate a password on the host unix system by talking to the chkpwd daemon
759 static int validpw(uid_t uid, const char *pass)
764 if (IsEmptyStr(pass)) {
765 syslog(LOG_DEBUG, "user_ops: refusing to chkpwd for uid=%d with empty password", uid);
769 syslog(LOG_DEBUG, "user_ops: validating password for uid=%d using chkpwd...", uid);
771 begin_critical_section(S_CHKPWD);
772 rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
774 syslog(LOG_EMERG, "user_ops: communication with chkpwd broken: %s", strerror(errno));
775 end_critical_section(S_CHKPWD);
778 rv = write(chkpwd_write_pipe[1], pass, 256);
780 syslog(LOG_EMERG, "user_ops: communication with chkpwd broken: %s", strerror(errno));
781 end_critical_section(S_CHKPWD);
784 rv = read(chkpwd_read_pipe[0], buf, 4);
786 syslog(LOG_EMERG, "user_ops: ommunication with chkpwd broken: %s", strerror(errno));
787 end_critical_section(S_CHKPWD);
790 end_critical_section(S_CHKPWD);
792 if (!strncmp(buf, "PASS", 4)) {
793 syslog(LOG_DEBUG, "user_ops: chkpwd pass");
797 syslog(LOG_DEBUG, "user_ops: chkpwd fail");
803 * Start up the chkpwd daemon so validpw() has something to talk to
805 void start_chkpwd_daemon(void) {
807 struct stat filestats;
810 syslog(LOG_DEBUG, "user_ops: starting chkpwd daemon for host authentication mode");
812 if ((stat(file_chkpwd, &filestats)==-1) || (filestats.st_size==0)) {
813 syslog(LOG_ERR, "user_ops: %s: %s", file_chkpwd, strerror(errno));
816 if (pipe(chkpwd_write_pipe) != 0) {
817 syslog(LOG_ERR, "user_ops: unable to create pipe for chkpwd daemon: %s", strerror(errno));
820 if (pipe(chkpwd_read_pipe) != 0) {
821 syslog(LOG_ERR, "user_ops: unable to create pipe for chkpwd daemon: %s", strerror(errno));
826 if (chkpwd_pid < 0) {
827 syslog(LOG_ERR, "user_ops: unable to fork chkpwd daemon: %s", strerror(errno));
830 if (chkpwd_pid == 0) {
831 dup2(chkpwd_write_pipe[0], 0);
832 dup2(chkpwd_read_pipe[1], 1);
833 for (i=2; i<256; ++i) close(i);
834 execl(file_chkpwd, file_chkpwd, NULL);
835 syslog(LOG_ERR, "user_ops: unable to exec chkpwd daemon: %s", strerror(errno));
842 int CtdlTryPassword(const char *password, long len)
845 CitContext *CCC = CC;
847 if ((CCC->logged_in)) {
848 syslog(LOG_WARNING, "user_ops: CtdlTryPassword: already logged in");
849 return pass_already_logged_in;
851 if (!strcmp(CCC->curr_user, NLI)) {
852 syslog(LOG_WARNING, "user_ops: CtdlTryPassword: no user selected");
855 if (CtdlGetUser(&CCC->user, CCC->curr_user)) {
856 syslog(LOG_ERR, "user_ops: CtdlTryPassword: internal error");
857 return pass_internal_error;
859 if (password == NULL) {
860 syslog(LOG_INFO, "user_ops: CtdlTryPassword: NULL password string supplied");
861 return pass_wrong_password;
864 if (CCC->is_master) {
865 code = strcmp(password, CtdlGetConfigStr("c_master_pass"));
868 else if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
872 if (validpw(CCC->user.uid, password)) {
876 * sooper-seekrit hack: populate the password field in the
877 * citadel database with the password that the user typed,
878 * if it's correct. This allows most sites to convert from
879 * host auth to native auth if they want to. If you think
880 * this is a security hazard, comment it out.
883 CtdlGetUserLock(&CCC->user, CCC->curr_user);
884 safestrncpy(CCC->user.password, password, sizeof CCC->user.password);
885 CtdlPutUserLock(&CCC->user);
888 * (sooper-seekrit hack ends here)
897 else if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
901 if ((CCC->ldap_dn) && (!CtdlTryPasswordLDAP(CCC->ldap_dn, password))) {
912 /* native auth mode */
915 pw = (char*) malloc(len + 1);
916 memcpy(pw, password, len + 1);
918 strproc(CCC->user.password);
919 code = strcasecmp(CCC->user.password, pw);
922 strproc(CCC->user.password);
923 code = strcasecmp(CCC->user.password, pw);
933 syslog(LOG_WARNING, "user_ops: bad password specified for <%s> Service <%s> Port <%ld> Remote <%s / %s>",
940 return pass_wrong_password;
946 * Delete a user record *and* all of its related resources.
948 int purge_user(char pname[])
950 struct ctdluser usbuf;
951 char usernamekey[USERNAME_SIZE];
953 makeuserkey(usernamekey, pname, cutuserkey(pname));
955 /* If the name is empty we can't find them in the DB any way so just return */
956 if (IsEmptyStr(pname))
957 return (ERROR + NO_SUCH_USER);
959 if (CtdlGetUser(&usbuf, pname) != 0) {
960 syslog(LOG_ERR, "user_ops: cannot purge user <%s> - not found", pname);
961 return (ERROR + NO_SUCH_USER);
963 /* Don't delete a user who is currently logged in. Instead, just
964 * set the access level to 0, and let the account get swept up
965 * during the next purge.
967 if (CtdlIsUserLoggedInByNum(usbuf.usernum)) {
968 syslog(LOG_WARNING, "user_ops: <%s> is logged in; not deleting", pname);
969 usbuf.axlevel = AxDeleted;
973 syslog(LOG_NOTICE, "user_ops: deleting <%s>", pname);
977 * This should all be wrapped in a S_USERS mutex.
978 * Without the mutex the user could log in before we get to the next function
979 * That would truly mess things up :-(
980 * I would like to see the S_USERS start before the CtdlIsUserLoggedInByNum() above
981 * and end after the user has been deleted from the database, below.
982 * Question is should we enter the EVT_PURGEUSER while S_USERS is active?
985 /* Perform any purge functions registered by server extensions */
986 PerformUserHooks(&usbuf, EVT_PURGEUSER);
988 /* delete any existing user/room relationships */
989 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
991 /* delete the users-by-number index record */
992 cdb_delete(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long));
994 /* delete the userlog entry */
995 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
1001 int internal_create_user (const char *username, long len, struct ctdluser *usbuf, uid_t uid)
1003 if (!CtdlGetUserLen(usbuf, username, len)) {
1004 return (ERROR + ALREADY_EXISTS);
1007 /* Go ahead and initialize a new user record */
1008 memset(usbuf, 0, sizeof(struct ctdluser));
1009 safestrncpy(usbuf->fullname, username, sizeof usbuf->fullname);
1010 strcpy(usbuf->password, "");
1013 /* These are the default flags on new accounts */
1014 usbuf->flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
1016 usbuf->timescalled = 0;
1018 usbuf->axlevel = CtdlGetConfigInt("c_initax");
1019 usbuf->lastcall = time(NULL);
1021 /* fetch a new user number */
1022 usbuf->usernum = get_new_user_number();
1024 /* add user to the database */
1026 cdb_store(CDB_USERSBYNUMBER, &usbuf->usernum, sizeof(long), usbuf->fullname, strlen(usbuf->fullname)+1);
1033 * create_user() - back end processing to create a new user
1035 * Set 'newusername' to the desired account name.
1036 * Set 'become_user' to nonzero if this is self-service account creation and we want
1037 * to actually log in as the user we just created, otherwise set it to 0.
1039 int create_user(const char *newusername, long len, int become_user)
1041 struct ctdluser usbuf;
1042 struct ctdlroom qrbuf;
1044 char mailboxname[ROOMNAMELEN];
1049 safestrncpy(username, newusername, sizeof username);
1052 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
1054 /* host auth mode */
1057 struct passwd *tempPwdPtr;
1058 char pwdbuffer[SIZ];
1060 #ifdef HAVE_GETPWNAM_R
1061 #ifdef SOLARIS_GETPWUID
1062 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof(pwdbuffer));
1063 #else // SOLARIS_GETPWUID
1064 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
1065 #endif // SOLARIS_GETPWUID
1066 #else // HAVE_GETPWNAM_R
1068 #endif // HAVE_GETPWNAM_R
1069 if (tempPwdPtr != NULL) {
1070 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
1072 if (IsEmptyStr (username))
1074 safestrncpy(username, pd.pw_name, sizeof username);
1075 len = cutuserkey(username);
1079 return (ERROR + NO_SUCH_USER);
1084 if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
1085 if (CtdlTryUserLDAP(username, NULL, 0, username, sizeof username, &uid, 0) != 0) {
1086 return(ERROR + NO_SUCH_USER);
1089 #endif /* HAVE_LDAP */
1091 if ((retval = internal_create_user(username, len, &usbuf, uid)) != 0)
1095 * Give the user a private mailbox and a configuration room.
1096 * Make the latter an invisible system room.
1098 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
1099 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
1101 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
1102 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
1103 if (CtdlGetRoomLock(&qrbuf, mailboxname) == 0) {
1104 qrbuf.QRflags2 |= QR2_SYSTEM;
1105 CtdlPutRoomLock(&qrbuf);
1108 /* Perform any create functions registered by server extensions */
1109 PerformUserHooks(&usbuf, EVT_NEWUSER);
1111 /* Everything below this line can be bypassed if administratively
1112 * creating a user, instead of doing self-service account creation
1116 /* Now become the user we just created */
1117 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
1118 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
1121 /* Check to make sure we're still who we think we are */
1122 if (CtdlGetUser(&CC->user, CC->curr_user)) {
1123 return (ERROR + INTERNAL_ERROR);
1128 "New user account <%s> has been created, from host %s [%s].\n",
1133 CtdlAideMessage(buf, "User Creation Notice");
1134 syslog(LOG_NOTICE, "user_ops: <%s> created", username);
1140 * set password - back end api code
1142 void CtdlSetPassword(char *new_pw)
1144 CtdlGetUserLock(&CC->user, CC->curr_user);
1145 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1146 CtdlPutUserLock(&CC->user);
1147 syslog(LOG_INFO, "user_ops: password changed for <%s>", CC->curr_user);
1148 PerformSessionHooks(EVT_SETPASS);
1153 * API function for cmd_invt_kick() and anything else that needs to
1154 * invite or kick out a user to/from a room.
1156 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1158 int CtdlInvtKick(char *iuser, int op) {
1159 struct ctdluser USscratch;
1163 if (CtdlGetUser(&USscratch, iuser) != 0) {
1167 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1169 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1170 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1173 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1174 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1176 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1178 /* post a message in Aide> saying what we just did */
1179 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1181 ((op == 1) ? "invited to" : "kicked out of"),
1183 (CC->logged_in ? CC->user.fullname : "an administrator")
1185 CtdlAideMessage(bbb,"User Admin Message");
1192 * Forget (Zap) the current room (API call)
1193 * Returns 0 on success
1195 int CtdlForgetThisRoom(void) {
1198 /* On some systems, Admins are not allowed to forget rooms */
1199 if (is_aide() && (CtdlGetConfigInt("c_aide_zap") == 0)
1200 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1204 CtdlGetUserLock(&CC->user, CC->curr_user);
1205 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1207 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1208 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1210 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1211 CtdlPutUserLock(&CC->user);
1213 /* Return to the Lobby, so we don't end up in an undefined room */
1214 CtdlUserGoto(CtdlGetConfigStr("c_baseroom"), 0, 0, NULL, NULL, NULL, NULL);
1221 * Traverse the user file...
1223 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1226 struct ctdluser usbuf;
1227 struct cdbdata *cdbus;
1229 cdb_rewind(CDB_USERS);
1231 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1232 memset(&usbuf, 0, sizeof(struct ctdluser));
1233 memcpy(&usbuf, cdbus->ptr,
1234 ((cdbus->len > sizeof(struct ctdluser)) ?
1235 sizeof(struct ctdluser) : cdbus->len));
1237 (*CallBack) (&usbuf, in_data);
1243 * List one user (this works with cmd_list)
1245 void ListThisUser(struct ctdluser *usbuf, void *data)
1249 searchstring = (char *)data;
1250 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1254 if (usbuf->axlevel > AxDeleted) {
1255 if ((CC->user.axlevel >= AxAideU)
1256 || ((usbuf->flags & US_UNLISTED) == 0)
1257 || ((CC->internal_pgm))) {
1258 cprintf("%s|%d|%ld|%ld|%ld|%ld||\n",
1262 (long)usbuf->lastcall,
1271 * Count the number of new mail messages the user has
1275 int num_newmsgs = 0;
1277 num_newmsgs = CC->newmail;
1280 return (num_newmsgs);
1285 * Count the number of new mail messages the user has
1287 int InitialMailCheck()
1289 int num_newmsgs = 0;
1291 char mailboxname[ROOMNAMELEN];
1292 struct ctdlroom mailbox;
1294 struct cdbdata *cdbfr;
1295 long *msglist = NULL;
1298 CtdlMailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1299 if (CtdlGetRoom(&mailbox, mailboxname) != 0)
1301 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1303 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1305 if (cdbfr != NULL) {
1306 msglist = malloc(cdbfr->len);
1307 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1308 num_msgs = cdbfr->len / sizeof(long);
1312 for (a = 0; a < num_msgs; ++a) {
1313 if (msglist[a] > 0L) {
1314 if (msglist[a] > vbuf.v_lastseen) {
1319 if (msglist != NULL)
1322 return (num_newmsgs);