2 * Server functions which perform operations on user objects.
4 * Copyright (c) 1987-2011 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.
17 #include <libcitadel.h>
21 #include "citserver.h"
23 #include "citadel_ldap.h"
24 #include "ctdl_module.h"
26 #include "internet_addressing.h"
28 /* These pipes are used to talk to the chkpwd daemon, which is forked during startup */
29 int chkpwd_write_pipe[2];
30 int chkpwd_read_pipe[2];
34 * CtdlGetUser() - retrieve named user into supplied buffer.
35 * returns 0 on success
37 int CtdlGetUserLen(struct ctdluser *usbuf, const char *name, long len)
40 char usernamekey[USERNAME_SIZE];
41 struct cdbdata *cdbus;
44 memset(usbuf, 0, sizeof(struct ctdluser));
47 makeuserkey(usernamekey, name, len);
48 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
50 if (cdbus == NULL) { /* user not found */
54 memcpy(usbuf, cdbus->ptr,
55 ((cdbus->len > sizeof(struct ctdluser)) ?
56 sizeof(struct ctdluser) : cdbus->len));
64 int CtdlGetUser(struct ctdluser *usbuf, char *name)
66 return CtdlGetUserLen(usbuf, name, cutuserkey(name));
69 int CtdlLockGetCurrentUser(void)
73 return CtdlGetUserLen(&CCC->user, CCC->curr_user, cutuserkey(CCC->curr_user));
77 * CtdlGetUserLock() - same as getuser() but locks the record
79 int CtdlGetUserLock(struct ctdluser *usbuf, char *name)
83 retcode = CtdlGetUser(usbuf, name);
85 begin_critical_section(S_USERS);
92 * CtdlPutUser() - write user buffer into the correct place on disk
94 void CtdlPutUser(struct ctdluser *usbuf)
96 char usernamekey[USERNAME_SIZE];
98 makeuserkey(usernamekey,
100 cutuserkey(usbuf->fullname));
102 usbuf->version = REV_LEVEL;
104 usernamekey, strlen(usernamekey),
105 usbuf, sizeof(struct ctdluser));
109 void CtdlPutCurrentUserLock()
111 CtdlPutUser(&CC->user);
116 * CtdlPutUserLock() - same as putuser() but locks the record
118 void CtdlPutUserLock(struct ctdluser *usbuf)
121 end_critical_section(S_USERS);
127 * rename_user() - this is tricky because the user's display name is the database key
129 * Returns 0 on success or nonzero if there was an error...
132 int rename_user(char *oldname, char *newname) {
133 int retcode = RENAMEUSER_OK;
134 struct ctdluser usbuf;
136 char oldnamekey[USERNAME_SIZE];
137 char newnamekey[USERNAME_SIZE];
139 /* Create the database keys... */
140 makeuserkey(oldnamekey, oldname, cutuserkey(oldname));
141 makeuserkey(newnamekey, newname, cutuserkey(newname));
143 /* Lock up and get going */
144 begin_critical_section(S_USERS);
146 /* We cannot rename a user who is currently logged in */
147 if (CtdlIsUserLoggedIn(oldname)) {
148 end_critical_section(S_USERS);
149 return RENAMEUSER_LOGGED_IN;
152 if (CtdlGetUser(&usbuf, newname) == 0) {
153 retcode = RENAMEUSER_ALREADY_EXISTS;
157 if (CtdlGetUser(&usbuf, oldname) != 0) {
158 retcode = RENAMEUSER_NOT_FOUND;
161 else { /* Sanity checks succeeded. Now rename the user. */
162 if (usbuf.usernum == 0)
164 CONM_syslog(LOG_DEBUG, "Can not rename user \"Citadel\".\n");
165 retcode = RENAMEUSER_NOT_FOUND;
167 CON_syslog(LOG_DEBUG, "Renaming <%s> to <%s>\n", oldname, newname);
168 cdb_delete(CDB_USERS, oldnamekey, strlen(oldnamekey));
169 safestrncpy(usbuf.fullname, newname, sizeof usbuf.fullname);
171 cdb_store(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long),
172 usbuf.fullname, strlen(usbuf.fullname)+1 );
174 retcode = RENAMEUSER_OK;
180 end_critical_section(S_USERS);
187 * Index-generating function used by Ctdl[Get|Set]Relationship
189 int GenerateRelationshipIndex(char *IndexBuf,
201 TheIndex.iRoomID = RoomID;
202 TheIndex.iRoomGen = RoomGen;
203 TheIndex.iUserID = UserID;
205 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
206 return (sizeof(TheIndex));
212 * Back end for CtdlSetRelationship()
214 void put_visit(visit *newvisit)
219 memset (IndexBuf, 0, sizeof (IndexBuf));
220 /* Generate an index */
221 IndexLen = GenerateRelationshipIndex(IndexBuf,
224 newvisit->v_usernum);
226 /* Store the record */
227 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
228 newvisit, sizeof(visit)
236 * Define a relationship between a user and a room
238 void CtdlSetRelationship(visit *newvisit,
239 struct ctdluser *rel_user,
240 struct ctdlroom *rel_room)
244 /* We don't use these in Citadel because they're implicit by the
245 * index, but they must be present if the database is exported.
247 newvisit->v_roomnum = rel_room->QRnumber;
248 newvisit->v_roomgen = rel_room->QRgen;
249 newvisit->v_usernum = rel_user->usernum;
255 * Locate a relationship between a user and a room
257 void CtdlGetRelationship(visit *vbuf,
258 struct ctdluser *rel_user,
259 struct ctdlroom *rel_room)
264 struct cdbdata *cdbvisit;
266 /* Generate an index */
267 IndexLen = GenerateRelationshipIndex(IndexBuf,
272 /* Clear out the buffer */
273 memset(vbuf, 0, sizeof(visit));
275 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
276 if (cdbvisit != NULL) {
277 memcpy(vbuf, cdbvisit->ptr,
278 ((cdbvisit->len > sizeof(visit)) ?
279 sizeof(visit) : cdbvisit->len));
283 /* If this is the first time the user has seen this room,
284 * set the view to be the default for the room.
286 vbuf->v_view = rel_room->QRdefaultview;
289 /* Set v_seen if necessary */
290 if (vbuf->v_seen[0] == 0) {
291 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
296 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
298 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
302 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
304 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
309 * Check to see if the specified user has Internet mail permission
310 * (returns nonzero if permission is granted)
312 int CtdlCheckInternetMailPermission(struct ctdluser *who) {
314 /* Do not allow twits to send Internet mail */
315 if (who->axlevel <= AxProbU) return(0);
317 /* Globally enabled? */
318 if (CtdlGetConfigInt("c_restrict") == 0) return(1);
320 /* User flagged ok? */
321 if (who->flags & US_INTERNET) return(2);
323 /* Admin level access? */
324 if (who->axlevel >= AxAideU) return(3);
326 /* No mail for you! */
331 * Convenience function.
333 int CtdlAccessCheck(int required_level)
335 if (CC->internal_pgm) return(0);
336 if (required_level >= ac_internal) {
337 cprintf("%d This is not a user-level command.\n",
338 ERROR + HIGHER_ACCESS_REQUIRED);
342 if ((required_level >= ac_logged_in_or_guest) && (CC->logged_in == 0) && (CtdlGetConfigInt("c_guest_logins") == 0)) {
343 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
347 if ((required_level >= ac_logged_in) && (CC->logged_in == 0)) {
348 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
352 if (CC->user.axlevel >= AxAideU) return(0);
353 if (required_level >= ac_aide) {
354 cprintf("%d This command requires Admin access.\n",
355 ERROR + HIGHER_ACCESS_REQUIRED);
359 if (is_room_aide()) return(0);
360 if (required_level >= ac_room_aide) {
361 cprintf("%d This command requires Admin or Room Admin access.\n",
362 ERROR + HIGHER_ACCESS_REQUIRED);
366 /* shhh ... succeed quietly */
373 * Is the user currently logged in an Admin?
377 if (CC->user.axlevel >= AxAideU)
385 * Is the user currently logged in an Admin *or* the room Admin for this room?
387 int is_room_aide(void)
390 if (!CC->logged_in) {
394 if ((CC->user.axlevel >= AxAideU)
395 || (CC->room.QRroomaide == CC->user.usernum)) {
403 * CtdlGetUserByNumber() - get user by number
404 * returns 0 if user was found
406 * Note: fetching a user this way requires one additional database operation.
408 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number)
410 struct cdbdata *cdbun;
413 cdbun = cdb_fetch(CDB_USERSBYNUMBER, &number, sizeof(long));
415 CON_syslog(LOG_INFO, "User %ld not found\n", number);
419 CON_syslog(LOG_INFO, "User %ld maps to %s\n", number, cdbun->ptr);
420 r = CtdlGetUser(usbuf, cdbun->ptr);
426 * Helper function for rebuild_usersbynumber()
428 void rebuild_ubn_for_user(struct ctdluser *usbuf, void *data) {
431 struct ubnlist *next;
432 char username[USERNAME_SIZE];
436 static struct ubnlist *u = NULL;
437 struct ubnlist *ptr = NULL;
439 /* Lazy programming here. Call this function as a ForEachUser backend
440 * in order to queue up the room names, or call it with a null user
441 * to make it do the processing.
444 ptr = (struct ubnlist *) malloc(sizeof (struct ubnlist));
445 if (ptr == NULL) return;
447 ptr->usernum = usbuf->usernum;
448 safestrncpy(ptr->username, usbuf->fullname, sizeof ptr->username);
455 CON_syslog(LOG_DEBUG, "Rebuilding usersbynumber index %10ld : %s\n",
456 u->usernum, u->username);
457 cdb_store(CDB_USERSBYNUMBER, &u->usernum, sizeof(long), u->username, strlen(u->username)+1);
468 * Rebuild the users-by-number index
470 void rebuild_usersbynumber(void) {
471 cdb_trunc(CDB_USERSBYNUMBER); /* delete the old indices */
472 ForEachUser(rebuild_ubn_for_user, NULL); /* enumerate the users */
473 rebuild_ubn_for_user(NULL, NULL); /* and index them */
479 * getuserbyuid() - get user by system uid (for PAM mode authentication)
480 * returns 0 if user was found
482 * WARNING: don't use this function unless you absolutely have to. It does
483 * a sequential search and therefore is computationally expensive.
485 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
487 struct cdbdata *cdbus;
489 cdb_rewind(CDB_USERS);
491 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
492 memset(usbuf, 0, sizeof(struct ctdluser));
493 memcpy(usbuf, cdbus->ptr,
494 ((cdbus->len > sizeof(struct ctdluser)) ?
495 sizeof(struct ctdluser) : cdbus->len));
497 if (usbuf->uid == number) {
498 cdb_close_cursor(CDB_USERS);
506 * Back end for cmd_user() and its ilk
508 * NOTE: "authname" should only be used if we are attempting to use the "master user" feature
510 int CtdlLoginExistingUser(char *authname, const char *trythisname)
516 CON_syslog(LOG_DEBUG, "CtdlLoginExistingUser(%s, %s)\n", authname, trythisname);
518 if ((CC->logged_in)) {
519 return login_already_logged_in;
522 if (trythisname == NULL) return login_not_found;
524 if (!strncasecmp(trythisname, "SYS_", 4))
526 CON_syslog(LOG_DEBUG, "System user \"%s\" is not allowed to log in.\n", trythisname);
527 return login_not_found;
530 /* If a "master user" is defined, handle its authentication if specified */
532 if ( (!IsEmptyStr(CtdlGetConfigStr("c_master_user"))) &&
533 (!IsEmptyStr(CtdlGetConfigStr("c_master_pass"))) &&
534 (authname != NULL) &&
535 (!strcasecmp(authname, CtdlGetConfigStr("c_master_user"))) )
540 /* Continue attempting user validation... */
541 safestrncpy(username, trythisname, sizeof (username));
543 len = cutuserkey(username);
545 if (IsEmptyStr(username)) {
546 return login_not_found;
549 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
554 struct passwd *tempPwdPtr;
557 CON_syslog(LOG_DEBUG, "asking host about <%s>\n", username);
558 #ifdef HAVE_GETPWNAM_R
559 #ifdef SOLARIS_GETPWUID
560 CON_syslog(LOG_DEBUG, "Calling getpwnam_r()\n");
561 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
562 #else // SOLARIS_GETPWUID
563 CONM_syslog(LOG_DEBUG, "Calling getpwnam_r()\n");
564 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
565 #endif // SOLARIS_GETPWUID
566 #else // HAVE_GETPWNAM_R
567 CON_syslog(LOG_DEBUG, "SHOULD NEVER GET HERE!!!\n");
569 #endif // HAVE_GETPWNAM_R
570 if (tempPwdPtr == NULL) {
571 CON_syslog(LOG_DEBUG, "no such user <%s>\n", username);
572 return login_not_found;
575 /* Locate the associated Citadel account.
576 * If not found, make one attempt to create it.
578 found_user = getuserbyuid(&CC->user, pd.pw_uid);
579 CON_syslog(LOG_DEBUG, "found it: uid=%ld, gecos=%s here: %d\n",
580 (long)pd.pw_uid, pd.pw_gecos, found_user);
581 if (found_user != 0) {
582 len = cutuserkey(username);
583 create_user(username, len, 0);
584 found_user = getuserbyuid(&CC->user, pd.pw_uid);
590 else if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
598 found_user = CtdlTryUserLDAP(username, ldap_dn, sizeof ldap_dn, ldap_cn, sizeof ldap_cn, &ldap_uid, 0);
599 if (found_user != 0) {
600 return login_not_found;
603 found_user = getuserbyuid(&CC->user, ldap_uid);
604 if (found_user != 0) {
605 create_user(username, len, 0);
606 found_user = getuserbyuid(&CC->user, ldap_uid);
609 if (found_user == 0) {
610 if (CC->ldap_dn != NULL) free(CC->ldap_dn);
611 CC->ldap_dn = strdup(ldap_dn);
618 /* native auth mode */
620 recptypes *valid = NULL;
622 /* First, try to log in as if the supplied name is a display name */
623 found_user = CtdlGetUser(&CC->user, username);
625 /* If that didn't work, try to log in as if the supplied name
626 * is an e-mail address
628 if (found_user != 0) {
629 valid = validate_recipients(username, NULL, 0);
631 if (valid->num_local == 1) {
632 found_user = CtdlGetUser(&CC->user, valid->recp_local);
634 free_recipients(valid);
639 /* Did we find something? */
640 if (found_user == 0) {
641 if (((CC->nologin)) && (CC->user.axlevel < AxAideU)) {
642 return login_too_many_users;
644 safestrncpy(CC->curr_user, CC->user.fullname,
645 sizeof CC->curr_user);
649 return login_not_found;
657 * session startup code which is common to both cmd_pass() and cmd_newu()
661 struct CitContext *CCC = CC;
664 CON_syslog(LOG_NOTICE, "<%s> logged in\n", CCC->curr_user);
666 CtdlGetUserLock(&CCC->user, CCC->curr_user);
667 ++(CCC->user.timescalled);
668 CCC->previous_login = CCC->user.lastcall;
669 time(&CCC->user.lastcall);
671 /* If this user's name is the name of the system administrator
672 * (as specified in setup), automatically assign access level 6.
674 if (!strcasecmp(CCC->user.fullname, CtdlGetConfigStr("c_sysadm"))) {
675 CCC->user.axlevel = AxAideU;
678 /* If we're authenticating off the host system, automatically give
679 * root the highest level of access.
681 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
682 if (CCC->user.uid == 0) {
683 CCC->user.axlevel = AxAideU;
687 CtdlPutUserLock(&CCC->user);
690 * Populate CCC->cs_inet_email with a default address. This will be
691 * overwritten with the user's directory address, if one exists, when
692 * the vCard module's login hook runs.
694 snprintf(CCC->cs_inet_email, sizeof CCC->cs_inet_email, "%s@%s",
695 CCC->user.fullname, CtdlGetConfigStr("c_fqdn"));
696 convert_spaces_to_underscores(CCC->cs_inet_email);
698 /* Create any personal rooms required by the system.
699 * (Technically, MAILROOM should be there already, but just in case...)
701 CtdlCreateRoom(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
702 CtdlCreateRoom(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
703 CtdlCreateRoom(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
704 CtdlCreateRoom(USERDRAFTROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
706 /* Run any startup routines registered by loadable modules */
707 PerformSessionHooks(EVT_LOGIN);
709 /* Enter the lobby */
710 CtdlUserGoto(CtdlGetConfigStr("c_baseroom"), 0, 0, NULL, NULL, NULL, NULL);
714 void logged_in_response(void)
716 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
717 CIT_OK, CC->user.fullname, CC->user.axlevel,
718 CC->user.timescalled, CC->user.posted,
719 CC->user.flags, CC->user.usernum,
725 void CtdlUserLogout(void)
727 CitContext *CCC = MyContext();
729 CON_syslog(LOG_DEBUG, "CtdlUserLogout() logging out <%s> from session %d",
730 CCC->curr_user, CCC->cs_pid
733 /* Run any hooks registered by modules... */
734 PerformSessionHooks(EVT_LOGOUT);
737 * Clear out some session data. Most likely, the CitContext for this
738 * session is about to get nuked when the session disconnects, but
739 * since it's possible to log in again without reconnecting, we cannot
740 * make that assumption.
742 strcpy(CCC->fake_username, "");
743 strcpy(CCC->fake_hostname, "");
744 strcpy(CCC->fake_roomname, "");
747 /* Check to see if the user was deleted whilst logged in and purge them if necessary */
748 if ((CCC->user.axlevel == AxDeleted) && (CCC->user.usernum))
749 purge_user(CCC->user.fullname);
751 /* Clear out the user record in memory so we don't behave like a ghost */
752 memset(&CCC->user, 0, sizeof(struct ctdluser));
753 CCC->curr_user[0] = 0;
755 CCC->cs_inet_email[0] = 0;
756 CCC->cs_inet_other_emails[0] = 0;
757 CCC->cs_inet_fn[0] = 0;
758 CCC->fake_username[0] = 0;
759 CCC->fake_hostname[0] = 0;
760 CCC->fake_roomname[0] = 0;
763 /* Free any output buffers */
769 * Validate a password on the host unix system by talking to the chkpwd daemon
771 static int validpw(uid_t uid, const char *pass)
776 if (IsEmptyStr(pass)) {
777 CON_syslog(LOG_DEBUG, "Refusing to chkpwd for uid=%d with empty password.\n", uid);
781 CON_syslog(LOG_DEBUG, "Validating password for uid=%d using chkpwd...\n", uid);
783 begin_critical_section(S_CHKPWD);
784 rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
786 CON_syslog(LOG_EMERG, "Communicatino with chkpwd broken: %s\n", strerror(errno));
787 end_critical_section(S_CHKPWD);
790 rv = write(chkpwd_write_pipe[1], pass, 256);
792 CON_syslog(LOG_EMERG, "Communicatino with chkpwd broken: %s\n", strerror(errno));
793 end_critical_section(S_CHKPWD);
796 rv = read(chkpwd_read_pipe[0], buf, 4);
798 CON_syslog(LOG_EMERG, "Communicatino with chkpwd broken: %s\n", strerror(errno));
799 end_critical_section(S_CHKPWD);
802 end_critical_section(S_CHKPWD);
804 if (!strncmp(buf, "PASS", 4)) {
805 CONM_syslog(LOG_DEBUG, "...pass\n");
809 CONM_syslog(LOG_DEBUG, "...fail\n");
814 * Start up the chkpwd daemon so validpw() has something to talk to
816 void start_chkpwd_daemon(void) {
818 struct stat filestats;
821 CONM_syslog(LOG_DEBUG, "Starting chkpwd daemon for host authentication mode\n");
823 if ((stat(file_chkpwd, &filestats)==-1) ||
824 (filestats.st_size==0)){
825 printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
828 if (pipe(chkpwd_write_pipe) != 0) {
829 CON_syslog(LOG_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
832 if (pipe(chkpwd_read_pipe) != 0) {
833 CON_syslog(LOG_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
838 if (chkpwd_pid < 0) {
839 CON_syslog(LOG_EMERG, "Unable to fork chkpwd daemon: %s\n", strerror(errno));
842 if (chkpwd_pid == 0) {
843 CONM_syslog(LOG_DEBUG, "Now calling dup2() write\n");
844 dup2(chkpwd_write_pipe[0], 0);
845 CONM_syslog(LOG_DEBUG, "Now calling dup2() write\n");
846 dup2(chkpwd_read_pipe[1], 1);
847 CONM_syslog(LOG_DEBUG, "Now closing stuff\n");
848 for (i=2; i<256; ++i) close(i);
849 CON_syslog(LOG_DEBUG, "Now calling execl(%s)\n", file_chkpwd);
850 execl(file_chkpwd, file_chkpwd, NULL);
851 CON_syslog(LOG_EMERG, "Unable to exec chkpwd daemon: %s\n", strerror(errno));
858 int CtdlTryPassword(const char *password, long len)
861 CitContext *CCC = CC;
863 if ((CCC->logged_in)) {
864 CONM_syslog(LOG_WARNING, "CtdlTryPassword: already logged in\n");
865 return pass_already_logged_in;
867 if (!strcmp(CCC->curr_user, NLI)) {
868 CONM_syslog(LOG_WARNING, "CtdlTryPassword: no user selected\n");
871 if (CtdlGetUser(&CCC->user, CCC->curr_user)) {
872 CONM_syslog(LOG_ERR, "CtdlTryPassword: internal error\n");
873 return pass_internal_error;
875 if (password == NULL) {
876 CONM_syslog(LOG_INFO, "CtdlTryPassword: NULL password string supplied\n");
877 return pass_wrong_password;
880 if (CCC->is_master) {
881 code = strcmp(password, CtdlGetConfigStr("c_master_pass"));
884 else if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
888 if (validpw(CCC->user.uid, password)) {
892 * sooper-seekrit hack: populate the password field in the
893 * citadel database with the password that the user typed,
894 * if it's correct. This allows most sites to convert from
895 * host auth to native auth if they want to. If you think
896 * this is a security hazard, comment it out.
899 CtdlGetUserLock(&CCC->user, CCC->curr_user);
900 safestrncpy(CCC->user.password, password, sizeof CCC->user.password);
901 CtdlPutUserLock(&CCC->user);
904 * (sooper-seekrit hack ends here)
914 else if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
918 if ((CCC->ldap_dn) && (!CtdlTryPasswordLDAP(CCC->ldap_dn, password))) {
929 /* native auth mode */
932 pw = (char*) malloc(len + 1);
933 memcpy(pw, password, len + 1);
935 strproc(CCC->user.password);
936 code = strcasecmp(CCC->user.password, pw);
939 strproc(CCC->user.password);
940 code = strcasecmp(CCC->user.password, pw);
949 CON_syslog(LOG_WARNING, "Bad password specified for <%s> Service <%s> Port <%ld> Remote <%s / %s>\n",
957 //citserver[5610]: Bad password specified for <willi> Service <citadel-TCP> Remote <PotzBlitz / >
959 return pass_wrong_password;
967 * Delete a user record *and* all of its related resources.
969 int purge_user(char pname[])
972 struct ctdluser usbuf;
973 char usernamekey[USERNAME_SIZE];
975 makeuserkey(usernamekey, pname, cutuserkey(pname));
977 /* If the name is empty we can't find them in the DB any way so just return */
978 if (IsEmptyStr(pname))
979 return (ERROR + NO_SUCH_USER);
981 if (CtdlGetUser(&usbuf, pname) != 0) {
982 CON_syslog(LOG_ERR, "Cannot purge user <%s> - not found\n", pname);
983 return (ERROR + NO_SUCH_USER);
985 /* Don't delete a user who is currently logged in. Instead, just
986 * set the access level to 0, and let the account get swept up
987 * during the next purge.
989 if (CtdlIsUserLoggedInByNum(usbuf.usernum)) {
990 CON_syslog(LOG_WARNING, "User <%s> is logged in; not deleting.\n", pname);
991 usbuf.axlevel = AxDeleted;
995 CON_syslog(LOG_NOTICE, "Deleting user <%s>\n", pname);
999 * This should all be wrapped in a S_USERS mutex.
1000 * Without the mutex the user could log in before we get to the next function
1001 * That would truly mess things up :-(
1002 * I would like to see the S_USERS start before the CtdlIsUserLoggedInByNum() above
1003 * and end after the user has been deleted from the database, below.
1004 * Question is should we enter the EVT_PURGEUSER whilst S_USERS is active?
1007 /* Perform any purge functions registered by server extensions */
1008 PerformUserHooks(&usbuf, EVT_PURGEUSER);
1010 /* delete any existing user/room relationships */
1011 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
1013 /* delete the users-by-number index record */
1014 cdb_delete(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long));
1016 /* delete the userlog entry */
1017 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
1019 /* remove the user's bio file */
1027 /* remove the user's picture */
1039 int internal_create_user (const char *username, long len, struct ctdluser *usbuf, uid_t uid)
1041 if (!CtdlGetUserLen(usbuf, username, len)) {
1042 return (ERROR + ALREADY_EXISTS);
1045 /* Go ahead and initialize a new user record */
1046 memset(usbuf, 0, sizeof(struct ctdluser));
1047 safestrncpy(usbuf->fullname, username, sizeof usbuf->fullname);
1048 strcpy(usbuf->password, "");
1051 /* These are the default flags on new accounts */
1052 usbuf->flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
1054 usbuf->timescalled = 0;
1056 usbuf->axlevel = CtdlGetConfigInt("c_initax");
1057 usbuf->lastcall = time(NULL);
1059 /* fetch a new user number */
1060 usbuf->usernum = get_new_user_number();
1062 /* add user to the database */
1064 cdb_store(CDB_USERSBYNUMBER, &usbuf->usernum, sizeof(long), usbuf->fullname, strlen(usbuf->fullname)+1);
1072 * create_user() - back end processing to create a new user
1074 * Set 'newusername' to the desired account name.
1075 * Set 'become_user' to nonzero if this is self-service account creation and we want
1076 * to actually log in as the user we just created, otherwise set it to 0.
1078 int create_user(const char *newusername, long len, int become_user)
1080 struct ctdluser usbuf;
1081 struct ctdlroom qrbuf;
1083 char mailboxname[ROOMNAMELEN];
1089 safestrncpy(username, newusername, sizeof username);
1093 if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_HOST) {
1095 /* host auth mode */
1098 struct passwd *tempPwdPtr;
1099 char pwdbuffer[SIZ];
1101 #ifdef HAVE_GETPWNAM_R
1102 #ifdef SOLARIS_GETPWUID
1103 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof(pwdbuffer));
1104 #else // SOLARIS_GETPWUID
1105 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
1106 #endif // SOLARIS_GETPWUID
1107 #else // HAVE_GETPWNAM_R
1109 #endif // HAVE_GETPWNAM_R
1110 if (tempPwdPtr != NULL) {
1111 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
1113 if (IsEmptyStr (username))
1115 safestrncpy(username, pd.pw_name, sizeof username);
1116 len = cutuserkey(username);
1120 return (ERROR + NO_SUCH_USER);
1125 if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
1126 if (CtdlTryUserLDAP(username, NULL, 0, username, sizeof username, &uid, 0) != 0) {
1127 return(ERROR + NO_SUCH_USER);
1130 #endif /* HAVE_LDAP */
1132 if ((retval = internal_create_user(username, len, &usbuf, uid)) != 0)
1136 * Give the user a private mailbox and a configuration room.
1137 * Make the latter an invisible system room.
1139 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
1140 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
1142 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
1143 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
1144 if (CtdlGetRoomLock(&qrbuf, mailboxname) == 0) {
1145 qrbuf.QRflags2 |= QR2_SYSTEM;
1146 CtdlPutRoomLock(&qrbuf);
1149 /* Perform any create functions registered by server extensions */
1150 PerformUserHooks(&usbuf, EVT_NEWUSER);
1152 /* Everything below this line can be bypassed if administratively
1153 * creating a user, instead of doing self-service account creation
1157 /* Now become the user we just created */
1158 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
1159 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
1162 /* Check to make sure we're still who we think we are */
1163 if (CtdlGetUser(&CC->user, CC->curr_user)) {
1164 return (ERROR + INTERNAL_ERROR);
1169 "New user account <%s> has been created, from host %s [%s].\n",
1174 CtdlAideMessage(buf, "User Creation Notice");
1175 CON_syslog(LOG_NOTICE, "New user <%s> created\n", username);
1182 * set password - back end api code
1184 void CtdlSetPassword(char *new_pw)
1186 CtdlGetUserLock(&CC->user, CC->curr_user);
1187 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1188 CtdlPutUserLock(&CC->user);
1189 CON_syslog(LOG_INFO, "Password changed for user <%s>\n", CC->curr_user);
1190 PerformSessionHooks(EVT_SETPASS);
1197 * API function for cmd_invt_kick() and anything else that needs to
1198 * invite or kick out a user to/from a room.
1200 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1202 int CtdlInvtKick(char *iuser, int op) {
1203 struct ctdluser USscratch;
1207 if (CtdlGetUser(&USscratch, iuser) != 0) {
1211 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1213 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1214 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1217 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1218 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1220 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1222 /* post a message in Aide> saying what we just did */
1223 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1225 ((op == 1) ? "invited to" : "kicked out of"),
1227 (CC->logged_in ? CC->user.fullname : "an administrator")
1229 CtdlAideMessage(bbb,"User Admin Message");
1236 * Forget (Zap) the current room (API call)
1237 * Returns 0 on success
1239 int CtdlForgetThisRoom(void) {
1242 /* On some systems, Admins are not allowed to forget rooms */
1243 if (is_aide() && (CtdlGetConfigInt("c_aide_zap") == 0)
1244 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1248 CtdlGetUserLock(&CC->user, CC->curr_user);
1249 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1251 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1252 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1254 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1255 CtdlPutUserLock(&CC->user);
1257 /* Return to the Lobby, so we don't end up in an undefined room */
1258 CtdlUserGoto(CtdlGetConfigStr("c_baseroom"), 0, 0, NULL, NULL, NULL, NULL);
1267 * Traverse the user file...
1269 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1272 struct ctdluser usbuf;
1273 struct cdbdata *cdbus;
1275 cdb_rewind(CDB_USERS);
1277 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1278 memset(&usbuf, 0, sizeof(struct ctdluser));
1279 memcpy(&usbuf, cdbus->ptr,
1280 ((cdbus->len > sizeof(struct ctdluser)) ?
1281 sizeof(struct ctdluser) : cdbus->len));
1283 (*CallBack) (&usbuf, in_data);
1289 * List one user (this works with cmd_list)
1291 void ListThisUser(struct ctdluser *usbuf, void *data)
1295 searchstring = (char *)data;
1296 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1300 if (usbuf->axlevel > AxDeleted) {
1301 if ((CC->user.axlevel >= AxAideU)
1302 || ((usbuf->flags & US_UNLISTED) == 0)
1303 || ((CC->internal_pgm))) {
1304 cprintf("%s|%d|%ld|%ld|%ld|%ld||\n",
1308 (long)usbuf->lastcall,
1319 * Count the number of new mail messages the user has
1323 int num_newmsgs = 0;
1325 num_newmsgs = CC->newmail;
1328 return (num_newmsgs);
1333 * Count the number of new mail messages the user has
1335 int InitialMailCheck()
1337 int num_newmsgs = 0;
1339 char mailboxname[ROOMNAMELEN];
1340 struct ctdlroom mailbox;
1342 struct cdbdata *cdbfr;
1343 long *msglist = NULL;
1346 CtdlMailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1347 if (CtdlGetRoom(&mailbox, mailboxname) != 0)
1349 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1351 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1353 if (cdbfr != NULL) {
1354 msglist = malloc(cdbfr->len);
1355 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1356 num_msgs = cdbfr->len / sizeof(long);
1360 for (a = 0; a < num_msgs; ++a) {
1361 if (msglist[a] > 0L) {
1362 if (msglist[a] > vbuf.v_lastseen) {
1367 if (msglist != NULL)
1370 return (num_newmsgs);