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"
22 #include "citadel_ldap.h"
23 #include "ctdl_module.h"
25 #include "internet_addressing.h"
27 /* These pipes are used to talk to the chkpwd daemon, which is forked during startup */
28 int chkpwd_write_pipe[2];
29 int chkpwd_read_pipe[2];
34 * getuser() - retrieve named user into supplied buffer.
35 * returns 0 on success
37 int getuser(struct ctdluser *usbuf, char name[])
39 return CtdlGetUser(usbuf, name);
44 * CtdlGetUser() - retrieve named user into supplied buffer.
45 * returns 0 on success
47 int CtdlGetUserLen(struct ctdluser *usbuf, const char *name, long len)
50 char usernamekey[USERNAME_SIZE];
51 struct cdbdata *cdbus;
54 memset(usbuf, 0, sizeof(struct ctdluser));
57 makeuserkey(usernamekey, name, len);
58 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
60 if (cdbus == NULL) { /* user not found */
64 memcpy(usbuf, cdbus->ptr,
65 ((cdbus->len > sizeof(struct ctdluser)) ?
66 sizeof(struct ctdluser) : cdbus->len));
74 int CtdlGetUser(struct ctdluser *usbuf, char *name)
76 return CtdlGetUserLen(usbuf, name, cutuserkey(name));
81 * CtdlGetUserLock() - same as getuser() but locks the record
83 int CtdlGetUserLock(struct ctdluser *usbuf, char *name)
87 retcode = CtdlGetUser(usbuf, name);
89 begin_critical_section(S_USERS);
96 * lgetuser() - same as getuser() but locks the record
98 int lgetuser(struct ctdluser *usbuf, char *name)
100 return CtdlGetUserLock(usbuf, name);
105 * CtdlPutUser() - write user buffer into the correct place on disk
107 void CtdlPutUser(struct ctdluser *usbuf)
109 char usernamekey[USERNAME_SIZE];
111 makeuserkey(usernamekey,
113 cutuserkey(usbuf->fullname));
115 usbuf->version = REV_LEVEL;
117 usernamekey, strlen(usernamekey),
118 usbuf, sizeof(struct ctdluser));
124 * putuser() - write user buffer into the correct place on disk
126 void putuser(struct ctdluser *usbuf)
133 * CtdlPutUserLock() - same as putuser() but locks the record
135 void CtdlPutUserLock(struct ctdluser *usbuf)
138 end_critical_section(S_USERS);
143 * lputuser() - same as putuser() but locks the record
145 void lputuser(struct ctdluser *usbuf)
147 CtdlPutUserLock(usbuf);
152 * rename_user() - this is tricky because the user's display name is the database key
154 * Returns 0 on success or nonzero if there was an error...
157 int rename_user(char *oldname, char *newname) {
158 int retcode = RENAMEUSER_OK;
159 struct ctdluser usbuf;
161 char oldnamekey[USERNAME_SIZE];
162 char newnamekey[USERNAME_SIZE];
164 /* Create the database keys... */
165 makeuserkey(oldnamekey, oldname, cutuserkey(oldname));
166 makeuserkey(newnamekey, newname, cutuserkey(newname));
168 /* Lock up and get going */
169 begin_critical_section(S_USERS);
171 /* We cannot rename a user who is currently logged in */
172 if (CtdlIsUserLoggedIn(oldname)) {
173 end_critical_section(S_USERS);
174 return RENAMEUSER_LOGGED_IN;
177 if (CtdlGetUser(&usbuf, newname) == 0) {
178 retcode = RENAMEUSER_ALREADY_EXISTS;
182 if (CtdlGetUser(&usbuf, oldname) != 0) {
183 retcode = RENAMEUSER_NOT_FOUND;
186 else { /* Sanity checks succeeded. Now rename the user. */
187 if (usbuf.usernum == 0)
189 CONM_syslog(LOG_DEBUG, "Can not rename user \"Citadel\".\n");
190 retcode = RENAMEUSER_NOT_FOUND;
192 CON_syslog(LOG_DEBUG, "Renaming <%s> to <%s>\n", oldname, newname);
193 cdb_delete(CDB_USERS, oldnamekey, strlen(oldnamekey));
194 safestrncpy(usbuf.fullname, newname, sizeof usbuf.fullname);
196 cdb_store(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long),
197 usbuf.fullname, strlen(usbuf.fullname)+1 );
199 retcode = RENAMEUSER_OK;
205 end_critical_section(S_USERS);
212 * Index-generating function used by Ctdl[Get|Set]Relationship
214 int GenerateRelationshipIndex(char *IndexBuf,
226 TheIndex.iRoomID = RoomID;
227 TheIndex.iRoomGen = RoomGen;
228 TheIndex.iUserID = UserID;
230 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
231 return (sizeof(TheIndex));
237 * Back end for CtdlSetRelationship()
239 void put_visit(visit *newvisit)
244 memset (IndexBuf, 0, sizeof (IndexBuf));
245 /* Generate an index */
246 IndexLen = GenerateRelationshipIndex(IndexBuf,
249 newvisit->v_usernum);
251 /* Store the record */
252 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
253 newvisit, sizeof(visit)
261 * Define a relationship between a user and a room
263 void CtdlSetRelationship(visit *newvisit,
264 struct ctdluser *rel_user,
265 struct ctdlroom *rel_room)
269 /* We don't use these in Citadel because they're implicit by the
270 * index, but they must be present if the database is exported.
272 newvisit->v_roomnum = rel_room->QRnumber;
273 newvisit->v_roomgen = rel_room->QRgen;
274 newvisit->v_usernum = rel_user->usernum;
280 * Locate a relationship between a user and a room
282 void CtdlGetRelationship(visit *vbuf,
283 struct ctdluser *rel_user,
284 struct ctdlroom *rel_room)
289 struct cdbdata *cdbvisit;
291 /* Generate an index */
292 IndexLen = GenerateRelationshipIndex(IndexBuf,
297 /* Clear out the buffer */
298 memset(vbuf, 0, sizeof(visit));
300 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
301 if (cdbvisit != NULL) {
302 memcpy(vbuf, cdbvisit->ptr,
303 ((cdbvisit->len > sizeof(visit)) ?
304 sizeof(visit) : cdbvisit->len));
308 /* If this is the first time the user has seen this room,
309 * set the view to be the default for the room.
311 vbuf->v_view = rel_room->QRdefaultview;
314 /* Set v_seen if necessary */
315 if (vbuf->v_seen[0] == 0) {
316 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
321 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
323 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
327 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
329 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
334 * Check to see if the specified user has Internet mail permission
335 * (returns nonzero if permission is granted)
337 int CtdlCheckInternetMailPermission(struct ctdluser *who) {
339 /* Do not allow twits to send Internet mail */
340 if (who->axlevel <= AxProbU) return(0);
342 /* Globally enabled? */
343 if (config.c_restrict == 0) return(1);
345 /* User flagged ok? */
346 if (who->flags & US_INTERNET) return(2);
348 /* Admin level access? */
349 if (who->axlevel >= AxAideU) return(3);
351 /* No mail for you! */
356 * Convenience function.
358 int CtdlAccessCheck(int required_level)
360 if (CC->internal_pgm) return(0);
361 if (required_level >= ac_internal) {
362 cprintf("%d This is not a user-level command.\n",
363 ERROR + HIGHER_ACCESS_REQUIRED);
367 if ((required_level >= ac_logged_in_or_guest) && (CC->logged_in == 0) && (!config.c_guest_logins)) {
368 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
372 if ((required_level >= ac_logged_in) && (CC->logged_in == 0)) {
373 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
377 if (CC->user.axlevel >= AxAideU) return(0);
378 if (required_level >= ac_aide) {
379 cprintf("%d This command requires Admin access.\n",
380 ERROR + HIGHER_ACCESS_REQUIRED);
384 if (is_room_aide()) return(0);
385 if (required_level >= ac_room_aide) {
386 cprintf("%d This command requires Admin or Room Admin access.\n",
387 ERROR + HIGHER_ACCESS_REQUIRED);
391 /* shhh ... succeed quietly */
398 * Is the user currently logged in an Admin?
402 if (CC->user.axlevel >= AxAideU)
410 * Is the user currently logged in an Admin *or* the room Admin for this room?
412 int is_room_aide(void)
415 if (!CC->logged_in) {
419 if ((CC->user.axlevel >= AxAideU)
420 || (CC->room.QRroomaide == CC->user.usernum)) {
428 * CtdlGetUserByNumber() - get user by number
429 * returns 0 if user was found
431 * Note: fetching a user this way requires one additional database operation.
433 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number)
435 struct cdbdata *cdbun;
438 cdbun = cdb_fetch(CDB_USERSBYNUMBER, &number, sizeof(long));
440 CON_syslog(LOG_INFO, "User %ld not found\n", number);
444 CON_syslog(LOG_INFO, "User %ld maps to %s\n", number, cdbun->ptr);
445 r = CtdlGetUser(usbuf, cdbun->ptr);
451 * getuserbynumber() - get user by number
452 * returns 0 if user was found
454 * Note: fetching a user this way requires one additional database operation.
456 int getuserbynumber(struct ctdluser *usbuf, long number)
458 return CtdlGetUserByNumber(usbuf, number);
464 * Helper function for rebuild_usersbynumber()
466 void rebuild_ubn_for_user(struct ctdluser *usbuf, void *data) {
469 struct ubnlist *next;
470 char username[USERNAME_SIZE];
474 static struct ubnlist *u = NULL;
475 struct ubnlist *ptr = NULL;
477 /* Lazy programming here. Call this function as a ForEachUser backend
478 * in order to queue up the room names, or call it with a null user
479 * to make it do the processing.
482 ptr = (struct ubnlist *) malloc(sizeof (struct ubnlist));
483 if (ptr == NULL) return;
485 ptr->usernum = usbuf->usernum;
486 safestrncpy(ptr->username, usbuf->fullname, sizeof ptr->username);
493 CON_syslog(LOG_DEBUG, "Rebuilding usersbynumber index %10ld : %s\n",
494 u->usernum, u->username);
495 cdb_store(CDB_USERSBYNUMBER, &u->usernum, sizeof(long), u->username, strlen(u->username)+1);
506 * Rebuild the users-by-number index
508 void rebuild_usersbynumber(void) {
509 cdb_trunc(CDB_USERSBYNUMBER); /* delete the old indices */
510 ForEachUser(rebuild_ubn_for_user, NULL); /* enumerate the users */
511 rebuild_ubn_for_user(NULL, NULL); /* and index them */
517 * getuserbyuid() - get user by system uid (for PAM mode authentication)
518 * returns 0 if user was found
520 * WARNING: don't use this function unless you absolutely have to. It does
521 * a sequential search and therefore is computationally expensive.
523 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
525 struct cdbdata *cdbus;
527 cdb_rewind(CDB_USERS);
529 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
530 memset(usbuf, 0, sizeof(struct ctdluser));
531 memcpy(usbuf, cdbus->ptr,
532 ((cdbus->len > sizeof(struct ctdluser)) ?
533 sizeof(struct ctdluser) : cdbus->len));
535 if (usbuf->uid == number) {
536 cdb_close_cursor(CDB_USERS);
544 * Back end for cmd_user() and its ilk
546 * NOTE: "authname" should only be used if we are attempting to use the "master user" feature
548 int CtdlLoginExistingUser(char *authname, const char *trythisname)
554 CON_syslog(LOG_DEBUG, "CtdlLoginExistingUser(%s, %s)\n", authname, trythisname);
556 if ((CC->logged_in)) {
557 return login_already_logged_in;
560 if (trythisname == NULL) return login_not_found;
562 if (!strncasecmp(trythisname, "SYS_", 4))
564 CON_syslog(LOG_DEBUG, "System user \"%s\" is not allowed to log in.\n", trythisname);
565 return login_not_found;
568 /* If a "master user" is defined, handle its authentication if specified */
570 if (strlen(config.c_master_user) > 0) if (strlen(config.c_master_pass) > 0) if (authname) {
571 if (!strcasecmp(authname, config.c_master_user)) {
576 /* Continue attempting user validation... */
577 safestrncpy(username, trythisname, sizeof (username));
579 len = cutuserkey(username);
581 if (IsEmptyStr(username)) {
582 return login_not_found;
585 if (config.c_auth_mode == AUTHMODE_HOST) {
590 struct passwd *tempPwdPtr;
593 CON_syslog(LOG_DEBUG, "asking host about <%s>\n", username);
594 #ifdef HAVE_GETPWNAM_R
595 #ifdef SOLARIS_GETPWUID
596 CON_syslog(LOG_DEBUG, "Calling getpwnam_r()\n");
597 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
598 #else // SOLARIS_GETPWUID
599 CONM_syslog(LOG_DEBUG, "Calling getpwnam_r()\n");
600 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
601 #endif // SOLARIS_GETPWUID
602 #else // HAVE_GETPWNAM_R
603 CON_syslog(LOG_DEBUG, "SHOULD NEVER GET HERE!!!\n");
605 #endif // HAVE_GETPWNAM_R
606 if (tempPwdPtr == NULL) {
607 CON_syslog(LOG_DEBUG, "no such user <%s>\n", username);
608 return login_not_found;
611 /* Locate the associated Citadel account.
612 * If not found, make one attempt to create it.
614 found_user = getuserbyuid(&CC->user, pd.pw_uid);
615 CON_syslog(LOG_DEBUG, "found it: uid=%ld, gecos=%s here: %d\n",
616 (long)pd.pw_uid, pd.pw_gecos, found_user);
617 if (found_user != 0) {
618 len = cutuserkey(username);
619 create_user(username, len, 0);
620 found_user = getuserbyuid(&CC->user, pd.pw_uid);
626 else if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
634 found_user = CtdlTryUserLDAP(username, ldap_dn, sizeof ldap_dn, ldap_cn, sizeof ldap_cn, &ldap_uid);
635 if (found_user != 0) {
636 return login_not_found;
639 found_user = getuserbyuid(&CC->user, ldap_uid);
640 if (found_user != 0) {
641 create_user(username, len, 0);
642 found_user = getuserbyuid(&CC->user, ldap_uid);
645 if (found_user == 0) {
646 if (CC->ldap_dn != NULL) free(CC->ldap_dn);
647 CC->ldap_dn = strdup(ldap_dn);
654 /* native auth mode */
656 recptypes *valid = NULL;
658 /* First, try to log in as if the supplied name is a display name */
659 found_user = CtdlGetUser(&CC->user, username);
661 /* If that didn't work, try to log in as if the supplied name
662 * is an e-mail address
664 if (found_user != 0) {
665 valid = validate_recipients(username, NULL, 0);
667 if (valid->num_local == 1) {
668 found_user = CtdlGetUser(&CC->user, valid->recp_local);
670 free_recipients(valid);
675 /* Did we find something? */
676 if (found_user == 0) {
677 if (((CC->nologin)) && (CC->user.axlevel < AxAideU)) {
678 return login_too_many_users;
680 safestrncpy(CC->curr_user, CC->user.fullname,
681 sizeof CC->curr_user);
685 return login_not_found;
693 * session startup code which is common to both cmd_pass() and cmd_newu()
697 struct CitContext *CCC = CC;
700 CON_syslog(LOG_NOTICE, "<%s> logged in\n", CCC->curr_user);
702 CtdlGetUserLock(&CCC->user, CCC->curr_user);
703 ++(CCC->user.timescalled);
704 CCC->previous_login = CCC->user.lastcall;
705 time(&CCC->user.lastcall);
707 /* If this user's name is the name of the system administrator
708 * (as specified in setup), automatically assign access level 6.
710 if (!strcasecmp(CCC->user.fullname, config.c_sysadm)) {
711 CCC->user.axlevel = AxAideU;
714 /* If we're authenticating off the host system, automatically give
715 * root the highest level of access.
717 if (config.c_auth_mode == AUTHMODE_HOST) {
718 if (CCC->user.uid == 0) {
719 CCC->user.axlevel = AxAideU;
723 CtdlPutUserLock(&CCC->user);
726 * Populate CCC->cs_inet_email with a default address. This will be
727 * overwritten with the user's directory address, if one exists, when
728 * the vCard module's login hook runs.
730 snprintf(CCC->cs_inet_email, sizeof CCC->cs_inet_email, "%s@%s",
731 CCC->user.fullname, config.c_fqdn);
732 convert_spaces_to_underscores(CCC->cs_inet_email);
734 /* Create any personal rooms required by the system.
735 * (Technically, MAILROOM should be there already, but just in case...)
737 CtdlCreateRoom(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
738 CtdlCreateRoom(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
739 CtdlCreateRoom(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
740 CtdlCreateRoom(USERDRAFTROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
742 /* Run any startup routines registered by loadable modules */
743 PerformSessionHooks(EVT_LOGIN);
745 /* Enter the lobby */
746 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
750 void logged_in_response(void)
752 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
753 CIT_OK, CC->user.fullname, CC->user.axlevel,
754 CC->user.timescalled, CC->user.posted,
755 CC->user.flags, CC->user.usernum,
761 void CtdlUserLogout(void)
763 CitContext *CCC = MyContext();
765 CON_syslog(LOG_DEBUG, "CtdlUserLogout() logging out <%s> from session %d",
766 CCC->curr_user, CCC->cs_pid
769 /* Run any hooks registered by modules... */
770 PerformSessionHooks(EVT_LOGOUT);
773 * Clear out some session data. Most likely, the CitContext for this
774 * session is about to get nuked when the session disconnects, but
775 * since it's possible to log in again without reconnecting, we cannot
776 * make that assumption.
778 strcpy(CCC->fake_username, "");
779 strcpy(CCC->fake_hostname, "");
780 strcpy(CCC->fake_roomname, "");
783 /* Check to see if the user was deleted whilst logged in and purge them if necessary */
784 if ((CCC->user.axlevel == AxDeleted) && (CCC->user.usernum))
785 purge_user(CCC->user.fullname);
787 /* Clear out the user record in memory so we don't behave like a ghost */
788 memset(&CCC->user, 0, sizeof(struct ctdluser));
789 CCC->curr_user[0] = 0;
791 CCC->cs_inet_email[0] = 0;
792 CCC->cs_inet_other_emails[0] = 0;
793 CCC->cs_inet_fn[0] = 0;
794 CCC->fake_username[0] = 0;
795 CCC->fake_hostname[0] = 0;
796 CCC->fake_roomname[0] = 0;
799 /* Free any output buffers */
805 * Validate a password on the host unix system by talking to the chkpwd daemon
807 static int validpw(uid_t uid, const char *pass)
812 if (IsEmptyStr(pass)) {
813 CON_syslog(LOG_DEBUG, "Refusing to chkpwd for uid=%d with empty password.\n", uid);
817 CON_syslog(LOG_DEBUG, "Validating password for uid=%d using chkpwd...\n", uid);
819 begin_critical_section(S_CHKPWD);
820 rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
822 CON_syslog(LOG_EMERG, "Communicatino with chkpwd broken: %s\n", strerror(errno));
823 end_critical_section(S_CHKPWD);
826 rv = write(chkpwd_write_pipe[1], pass, 256);
828 CON_syslog(LOG_EMERG, "Communicatino with chkpwd broken: %s\n", strerror(errno));
829 end_critical_section(S_CHKPWD);
832 rv = read(chkpwd_read_pipe[0], buf, 4);
834 CON_syslog(LOG_EMERG, "Communicatino with chkpwd broken: %s\n", strerror(errno));
835 end_critical_section(S_CHKPWD);
838 end_critical_section(S_CHKPWD);
840 if (!strncmp(buf, "PASS", 4)) {
841 CONM_syslog(LOG_DEBUG, "...pass\n");
845 CONM_syslog(LOG_DEBUG, "...fail\n");
850 * Start up the chkpwd daemon so validpw() has something to talk to
852 void start_chkpwd_daemon(void) {
854 struct stat filestats;
857 CONM_syslog(LOG_DEBUG, "Starting chkpwd daemon for host authentication mode\n");
859 if ((stat(file_chkpwd, &filestats)==-1) ||
860 (filestats.st_size==0)){
861 printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
864 if (pipe(chkpwd_write_pipe) != 0) {
865 CON_syslog(LOG_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
868 if (pipe(chkpwd_read_pipe) != 0) {
869 CON_syslog(LOG_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
874 if (chkpwd_pid < 0) {
875 CON_syslog(LOG_EMERG, "Unable to fork chkpwd daemon: %s\n", strerror(errno));
878 if (chkpwd_pid == 0) {
879 CONM_syslog(LOG_DEBUG, "Now calling dup2() write\n");
880 dup2(chkpwd_write_pipe[0], 0);
881 CONM_syslog(LOG_DEBUG, "Now calling dup2() write\n");
882 dup2(chkpwd_read_pipe[1], 1);
883 CONM_syslog(LOG_DEBUG, "Now closing stuff\n");
884 for (i=2; i<256; ++i) close(i);
885 CON_syslog(LOG_DEBUG, "Now calling execl(%s)\n", file_chkpwd);
886 execl(file_chkpwd, file_chkpwd, NULL);
887 CON_syslog(LOG_EMERG, "Unable to exec chkpwd daemon: %s\n", strerror(errno));
894 int CtdlTryPassword(const char *password, long len)
897 CitContext *CCC = CC;
899 if ((CCC->logged_in)) {
900 CONM_syslog(LOG_WARNING, "CtdlTryPassword: already logged in\n");
901 return pass_already_logged_in;
903 if (!strcmp(CCC->curr_user, NLI)) {
904 CONM_syslog(LOG_WARNING, "CtdlTryPassword: no user selected\n");
907 if (CtdlGetUser(&CCC->user, CCC->curr_user)) {
908 CONM_syslog(LOG_ERR, "CtdlTryPassword: internal error\n");
909 return pass_internal_error;
911 if (password == NULL) {
912 CONM_syslog(LOG_INFO, "CtdlTryPassword: NULL password string supplied\n");
913 return pass_wrong_password;
916 if (CCC->is_master) {
917 code = strcmp(password, config.c_master_pass);
920 else if (config.c_auth_mode == AUTHMODE_HOST) {
924 if (validpw(CCC->user.uid, password)) {
928 * sooper-seekrit hack: populate the password field in the
929 * citadel database with the password that the user typed,
930 * if it's correct. This allows most sites to convert from
931 * host auth to native auth if they want to. If you think
932 * this is a security hazard, comment it out.
935 CtdlGetUserLock(&CCC->user, CCC->curr_user);
936 safestrncpy(CCC->user.password, password, sizeof CCC->user.password);
937 CtdlPutUserLock(&CCC->user);
940 * (sooper-seekrit hack ends here)
950 else if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
954 if ((CCC->ldap_dn) && (!CtdlTryPasswordLDAP(CCC->ldap_dn, password))) {
965 /* native auth mode */
968 pw = (char*) malloc(len + 1);
969 memcpy(pw, password, len + 1);
971 strproc(CCC->user.password);
972 code = strcasecmp(CCC->user.password, pw);
975 strproc(CCC->user.password);
976 code = strcasecmp(CCC->user.password, pw);
985 CON_syslog(LOG_WARNING, "Bad password specified for <%s> Service <%s> Port <%ld> Remote <%s / %s>\n",
993 //citserver[5610]: Bad password specified for <willi> Service <citadel-TCP> Remote <PotzBlitz / >
995 return pass_wrong_password;
1003 * Delete a user record *and* all of its related resources.
1005 int purge_user(char pname[])
1008 struct ctdluser usbuf;
1009 char usernamekey[USERNAME_SIZE];
1011 makeuserkey(usernamekey, pname, cutuserkey(pname));
1013 /* If the name is empty we can't find them in the DB any way so just return */
1014 if (IsEmptyStr(pname))
1015 return (ERROR + NO_SUCH_USER);
1017 if (CtdlGetUser(&usbuf, pname) != 0) {
1018 CON_syslog(LOG_ERR, "Cannot purge user <%s> - not found\n", pname);
1019 return (ERROR + NO_SUCH_USER);
1021 /* Don't delete a user who is currently logged in. Instead, just
1022 * set the access level to 0, and let the account get swept up
1023 * during the next purge.
1025 if (CtdlIsUserLoggedInByNum(usbuf.usernum)) {
1026 CON_syslog(LOG_WARNING, "User <%s> is logged in; not deleting.\n", pname);
1027 usbuf.axlevel = AxDeleted;
1028 CtdlPutUser(&usbuf);
1031 CON_syslog(LOG_NOTICE, "Deleting user <%s>\n", pname);
1035 * This should all be wrapped in a S_USERS mutex.
1036 * Without the mutex the user could log in before we get to the next function
1037 * That would truly mess things up :-(
1038 * I would like to see the S_USERS start before the CtdlIsUserLoggedInByNum() above
1039 * and end after the user has been deleted from the database, below.
1040 * Question is should we enter the EVT_PURGEUSER whilst S_USERS is active?
1043 /* Perform any purge functions registered by server extensions */
1044 PerformUserHooks(&usbuf, EVT_PURGEUSER);
1046 /* delete any existing user/room relationships */
1047 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
1049 /* delete the users-by-number index record */
1050 cdb_delete(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long));
1052 /* delete the userlog entry */
1053 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
1055 /* remove the user's bio file */
1063 /* remove the user's picture */
1075 int internal_create_user (const char *username, long len, struct ctdluser *usbuf, uid_t uid)
1077 if (!CtdlGetUserLen(usbuf, username, len)) {
1078 return (ERROR + ALREADY_EXISTS);
1081 /* Go ahead and initialize a new user record */
1082 memset(usbuf, 0, sizeof(struct ctdluser));
1083 safestrncpy(usbuf->fullname, username, sizeof usbuf->fullname);
1084 strcpy(usbuf->password, "");
1087 /* These are the default flags on new accounts */
1088 usbuf->flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
1090 usbuf->timescalled = 0;
1092 usbuf->axlevel = config.c_initax;
1093 usbuf->lastcall = time(NULL);
1095 /* fetch a new user number */
1096 usbuf->usernum = get_new_user_number();
1098 /* add user to the database */
1100 cdb_store(CDB_USERSBYNUMBER, &usbuf->usernum, sizeof(long), usbuf->fullname, strlen(usbuf->fullname)+1);
1108 * create_user() - back end processing to create a new user
1110 * Set 'newusername' to the desired account name.
1111 * Set 'become_user' to nonzero if this is self-service account creation and we want
1112 * to actually log in as the user we just created, otherwise set it to 0.
1114 int create_user(const char *newusername, long len, int become_user)
1116 struct ctdluser usbuf;
1117 struct ctdlroom qrbuf;
1119 char mailboxname[ROOMNAMELEN];
1125 safestrncpy(username, newusername, sizeof username);
1129 if (config.c_auth_mode == AUTHMODE_HOST) {
1131 /* host auth mode */
1134 struct passwd *tempPwdPtr;
1135 char pwdbuffer[SIZ];
1137 #ifdef HAVE_GETPWNAM_R
1138 #ifdef SOLARIS_GETPWUID
1139 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof(pwdbuffer));
1140 #else // SOLARIS_GETPWUID
1141 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
1142 #endif // SOLARIS_GETPWUID
1143 #else // HAVE_GETPWNAM_R
1145 #endif // HAVE_GETPWNAM_R
1146 if (tempPwdPtr != NULL) {
1147 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
1149 if (IsEmptyStr (username))
1151 safestrncpy(username, pd.pw_name, sizeof username);
1152 len = cutuserkey(username);
1156 return (ERROR + NO_SUCH_USER);
1161 if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
1162 if (CtdlTryUserLDAP(username, NULL, 0, username, sizeof username, &uid) != 0) {
1163 return(ERROR + NO_SUCH_USER);
1166 #endif /* HAVE_LDAP */
1168 if ((retval = internal_create_user(username, len, &usbuf, uid)) != 0)
1172 * Give the user a private mailbox and a configuration room.
1173 * Make the latter an invisible system room.
1175 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
1176 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
1178 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
1179 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
1180 if (CtdlGetRoomLock(&qrbuf, mailboxname) == 0) {
1181 qrbuf.QRflags2 |= QR2_SYSTEM;
1182 CtdlPutRoomLock(&qrbuf);
1185 /* Perform any create functions registered by server extensions */
1186 PerformUserHooks(&usbuf, EVT_NEWUSER);
1188 /* Everything below this line can be bypassed if administratively
1189 * creating a user, instead of doing self-service account creation
1193 /* Now become the user we just created */
1194 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
1195 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
1198 /* Check to make sure we're still who we think we are */
1199 if (CtdlGetUser(&CC->user, CC->curr_user)) {
1200 return (ERROR + INTERNAL_ERROR);
1205 "New user account <%s> has been created, from host %s [%s].\n",
1210 CtdlAideMessage(buf, "User Creation Notice");
1211 CON_syslog(LOG_NOTICE, "New user <%s> created\n", username);
1218 * set password - back end api code
1220 void CtdlSetPassword(char *new_pw)
1222 CtdlGetUserLock(&CC->user, CC->curr_user);
1223 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1224 CtdlPutUserLock(&CC->user);
1225 CON_syslog(LOG_INFO, "Password changed for user <%s>\n", CC->curr_user);
1226 PerformSessionHooks(EVT_SETPASS);
1233 * API function for cmd_invt_kick() and anything else that needs to
1234 * invite or kick out a user to/from a room.
1236 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1238 int CtdlInvtKick(char *iuser, int op) {
1239 struct ctdluser USscratch;
1243 if (CtdlGetUser(&USscratch, iuser) != 0) {
1247 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1249 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1250 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1253 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1254 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1256 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1258 /* post a message in Aide> saying what we just did */
1259 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1261 ((op == 1) ? "invited to" : "kicked out of"),
1263 (CC->logged_in ? CC->user.fullname : "an administrator")
1265 CtdlAideMessage(bbb,"User Admin Message");
1272 * Forget (Zap) the current room (API call)
1273 * Returns 0 on success
1275 int CtdlForgetThisRoom(void) {
1278 /* On some systems, Admins are not allowed to forget rooms */
1279 if (is_aide() && (config.c_aide_zap == 0)
1280 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1284 CtdlGetUserLock(&CC->user, CC->curr_user);
1285 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1287 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1288 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1290 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1291 CtdlPutUserLock(&CC->user);
1293 /* Return to the Lobby, so we don't end up in an undefined room */
1294 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
1303 * Traverse the user file...
1305 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1308 struct ctdluser usbuf;
1309 struct cdbdata *cdbus;
1311 cdb_rewind(CDB_USERS);
1313 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1314 memset(&usbuf, 0, sizeof(struct ctdluser));
1315 memcpy(&usbuf, cdbus->ptr,
1316 ((cdbus->len > sizeof(struct ctdluser)) ?
1317 sizeof(struct ctdluser) : cdbus->len));
1319 (*CallBack) (&usbuf, in_data);
1325 * List one user (this works with cmd_list)
1327 void ListThisUser(struct ctdluser *usbuf, void *data)
1331 searchstring = (char *)data;
1332 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1336 if (usbuf->axlevel > AxDeleted) {
1337 if ((CC->user.axlevel >= AxAideU)
1338 || ((usbuf->flags & US_UNLISTED) == 0)
1339 || ((CC->internal_pgm))) {
1340 cprintf("%s|%d|%ld|%ld|%ld|%ld||\n",
1344 (long)usbuf->lastcall,
1355 * Count the number of new mail messages the user has
1359 int num_newmsgs = 0;
1361 num_newmsgs = CC->newmail;
1364 return (num_newmsgs);
1369 * Count the number of new mail messages the user has
1371 int InitialMailCheck()
1373 int num_newmsgs = 0;
1375 char mailboxname[ROOMNAMELEN];
1376 struct ctdlroom mailbox;
1378 struct cdbdata *cdbfr;
1379 long *msglist = NULL;
1382 CtdlMailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1383 if (CtdlGetRoom(&mailbox, mailboxname) != 0)
1385 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1387 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1389 if (cdbfr != NULL) {
1390 msglist = malloc(cdbfr->len);
1391 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1392 num_msgs = cdbfr->len / sizeof(long);
1396 for (a = 0; a < num_msgs; ++a) {
1397 if (msglist[a] > 0L) {
1398 if (msglist[a] > vbuf.v_lastseen) {
1403 if (msglist != NULL)
1406 return (num_newmsgs);