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.
24 #include <sys/types.h>
27 #ifdef HAVE_SYS_STAT_H
31 #if TIME_WITH_SYS_TIME
32 # include <sys/time.h>
36 # include <sys/time.h>
44 #include <libcitadel.h>
49 #include "sysdep_decls.h"
55 #include "citserver.h"
56 #include "citadel_dirs.h"
59 #include "citadel_ldap.h"
61 #include "ctdl_module.h"
63 #include "internet_addressing.h"
65 /* These pipes are used to talk to the chkpwd daemon, which is forked during startup */
66 int chkpwd_write_pipe[2];
67 int chkpwd_read_pipe[2];
72 * getuser() - retrieve named user into supplied buffer.
73 * returns 0 on success
75 int getuser(struct ctdluser *usbuf, char name[])
77 return CtdlGetUser(usbuf, name);
82 * CtdlGetUser() - retrieve named user into supplied buffer.
83 * returns 0 on success
85 int CtdlGetUserLen(struct ctdluser *usbuf, const char *name, long len)
88 char usernamekey[USERNAME_SIZE];
89 struct cdbdata *cdbus;
92 memset(usbuf, 0, sizeof(struct ctdluser));
95 makeuserkey(usernamekey, name, len);
96 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
98 if (cdbus == NULL) { /* user not found */
102 memcpy(usbuf, cdbus->ptr,
103 ((cdbus->len > sizeof(struct ctdluser)) ?
104 sizeof(struct ctdluser) : cdbus->len));
112 int CtdlGetUser(struct ctdluser *usbuf, char *name)
114 return CtdlGetUserLen(usbuf, name, cutuserkey(name));
119 * CtdlGetUserLock() - same as getuser() but locks the record
121 int CtdlGetUserLock(struct ctdluser *usbuf, char *name)
125 retcode = CtdlGetUser(usbuf, name);
127 begin_critical_section(S_USERS);
134 * lgetuser() - same as getuser() but locks the record
136 int lgetuser(struct ctdluser *usbuf, char *name)
138 return CtdlGetUserLock(usbuf, name);
143 * CtdlPutUser() - write user buffer into the correct place on disk
145 void CtdlPutUser(struct ctdluser *usbuf)
147 char usernamekey[USERNAME_SIZE];
149 makeuserkey(usernamekey,
151 cutuserkey(usbuf->fullname));
153 usbuf->version = REV_LEVEL;
155 usernamekey, strlen(usernamekey),
156 usbuf, sizeof(struct ctdluser));
162 * putuser() - write user buffer into the correct place on disk
164 void putuser(struct ctdluser *usbuf)
171 * CtdlPutUserLock() - same as putuser() but locks the record
173 void CtdlPutUserLock(struct ctdluser *usbuf)
176 end_critical_section(S_USERS);
181 * lputuser() - same as putuser() but locks the record
183 void lputuser(struct ctdluser *usbuf)
185 CtdlPutUserLock(usbuf);
190 * rename_user() - this is tricky because the user's display name is the database key
192 * Returns 0 on success or nonzero if there was an error...
195 int rename_user(char *oldname, char *newname) {
196 int retcode = RENAMEUSER_OK;
197 struct ctdluser usbuf;
199 char oldnamekey[USERNAME_SIZE];
200 char newnamekey[USERNAME_SIZE];
202 /* Create the database keys... */
203 makeuserkey(oldnamekey, oldname, cutuserkey(oldname));
204 makeuserkey(newnamekey, newname, cutuserkey(newname));
206 /* Lock up and get going */
207 begin_critical_section(S_USERS);
209 /* We cannot rename a user who is currently logged in */
210 if (CtdlIsUserLoggedIn(oldname)) {
211 end_critical_section(S_USERS);
212 return RENAMEUSER_LOGGED_IN;
215 if (CtdlGetUser(&usbuf, newname) == 0) {
216 retcode = RENAMEUSER_ALREADY_EXISTS;
220 if (CtdlGetUser(&usbuf, oldname) != 0) {
221 retcode = RENAMEUSER_NOT_FOUND;
224 else { /* Sanity checks succeeded. Now rename the user. */
225 if (usbuf.usernum == 0)
227 CONM_syslog(LOG_DEBUG, "Can not rename user \"Citadel\".\n");
228 retcode = RENAMEUSER_NOT_FOUND;
230 CON_syslog(LOG_DEBUG, "Renaming <%s> to <%s>\n", oldname, newname);
231 cdb_delete(CDB_USERS, oldnamekey, strlen(oldnamekey));
232 safestrncpy(usbuf.fullname, newname, sizeof usbuf.fullname);
234 cdb_store(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long),
235 usbuf.fullname, strlen(usbuf.fullname)+1 );
237 retcode = RENAMEUSER_OK;
243 end_critical_section(S_USERS);
250 * Index-generating function used by Ctdl[Get|Set]Relationship
252 int GenerateRelationshipIndex(char *IndexBuf,
264 TheIndex.iRoomID = RoomID;
265 TheIndex.iRoomGen = RoomGen;
266 TheIndex.iUserID = UserID;
268 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
269 return (sizeof(TheIndex));
275 * Back end for CtdlSetRelationship()
277 void put_visit(visit *newvisit)
282 memset (IndexBuf, 0, sizeof (IndexBuf));
283 /* Generate an index */
284 IndexLen = GenerateRelationshipIndex(IndexBuf,
287 newvisit->v_usernum);
289 /* Store the record */
290 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
291 newvisit, sizeof(visit)
299 * Define a relationship between a user and a room
301 void CtdlSetRelationship(visit *newvisit,
302 struct ctdluser *rel_user,
303 struct ctdlroom *rel_room)
307 /* We don't use these in Citadel because they're implicit by the
308 * index, but they must be present if the database is exported.
310 newvisit->v_roomnum = rel_room->QRnumber;
311 newvisit->v_roomgen = rel_room->QRgen;
312 newvisit->v_usernum = rel_user->usernum;
318 * Locate a relationship between a user and a room
320 void CtdlGetRelationship(visit *vbuf,
321 struct ctdluser *rel_user,
322 struct ctdlroom *rel_room)
327 struct cdbdata *cdbvisit;
329 /* Generate an index */
330 IndexLen = GenerateRelationshipIndex(IndexBuf,
335 /* Clear out the buffer */
336 memset(vbuf, 0, sizeof(visit));
338 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
339 if (cdbvisit != NULL) {
340 memcpy(vbuf, cdbvisit->ptr,
341 ((cdbvisit->len > sizeof(visit)) ?
342 sizeof(visit) : cdbvisit->len));
346 /* If this is the first time the user has seen this room,
347 * set the view to be the default for the room.
349 vbuf->v_view = rel_room->QRdefaultview;
352 /* Set v_seen if necessary */
353 if (vbuf->v_seen[0] == 0) {
354 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
359 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
361 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
365 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
367 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
372 * Check to see if the specified user has Internet mail permission
373 * (returns nonzero if permission is granted)
375 int CtdlCheckInternetMailPermission(struct ctdluser *who) {
377 /* Do not allow twits to send Internet mail */
378 if (who->axlevel <= AxProbU) return(0);
380 /* Globally enabled? */
381 if (config.c_restrict == 0) return(1);
383 /* User flagged ok? */
384 if (who->flags & US_INTERNET) return(2);
386 /* Admin level access? */
387 if (who->axlevel >= AxAideU) return(3);
389 /* No mail for you! */
395 * Is the user currently logged in an Admin?
399 if (CC->user.axlevel >= AxAideU)
407 * Is the user currently logged in an Admin *or* the room Admin for this room?
409 int is_room_aide(void)
412 if (!CC->logged_in) {
416 if ((CC->user.axlevel >= AxAideU)
417 || (CC->room.QRroomaide == CC->user.usernum)) {
425 * CtdlGetUserByNumber() - get user by number
426 * returns 0 if user was found
428 * Note: fetching a user this way requires one additional database operation.
430 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number)
432 struct cdbdata *cdbun;
435 cdbun = cdb_fetch(CDB_USERSBYNUMBER, &number, sizeof(long));
437 CON_syslog(LOG_INFO, "User %ld not found\n", number);
441 CON_syslog(LOG_INFO, "User %ld maps to %s\n", number, cdbun->ptr);
442 r = CtdlGetUser(usbuf, cdbun->ptr);
448 * getuserbynumber() - get user by number
449 * returns 0 if user was found
451 * Note: fetching a user this way requires one additional database operation.
453 int getuserbynumber(struct ctdluser *usbuf, long number)
455 return CtdlGetUserByNumber(usbuf, number);
461 * Helper function for rebuild_usersbynumber()
463 void rebuild_ubn_for_user(struct ctdluser *usbuf, void *data) {
466 struct ubnlist *next;
467 char username[USERNAME_SIZE];
471 static struct ubnlist *u = NULL;
472 struct ubnlist *ptr = NULL;
474 /* Lazy programming here. Call this function as a ForEachUser backend
475 * in order to queue up the room names, or call it with a null user
476 * to make it do the processing.
479 ptr = (struct ubnlist *) malloc(sizeof (struct ubnlist));
480 if (ptr == NULL) return;
482 ptr->usernum = usbuf->usernum;
483 safestrncpy(ptr->username, usbuf->fullname, sizeof ptr->username);
490 CON_syslog(LOG_DEBUG, "Rebuilding usersbynumber index %10ld : %s\n",
491 u->usernum, u->username);
492 cdb_store(CDB_USERSBYNUMBER, &u->usernum, sizeof(long), u->username, strlen(u->username)+1);
503 * Rebuild the users-by-number index
505 void rebuild_usersbynumber(void) {
506 cdb_trunc(CDB_USERSBYNUMBER); /* delete the old indices */
507 ForEachUser(rebuild_ubn_for_user, NULL); /* enumerate the users */
508 rebuild_ubn_for_user(NULL, NULL); /* and index them */
514 * getuserbyuid() - get user by system uid (for PAM mode authentication)
515 * returns 0 if user was found
517 * WARNING: don't use this function unless you absolutely have to. It does
518 * a sequential search and therefore is computationally expensive.
520 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
522 struct cdbdata *cdbus;
524 cdb_rewind(CDB_USERS);
526 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
527 memset(usbuf, 0, sizeof(struct ctdluser));
528 memcpy(usbuf, cdbus->ptr,
529 ((cdbus->len > sizeof(struct ctdluser)) ?
530 sizeof(struct ctdluser) : cdbus->len));
532 if (usbuf->uid == number) {
533 cdb_close_cursor(CDB_USERS);
541 * Back end for cmd_user() and its ilk
543 * NOTE: "authname" should only be used if we are attempting to use the "master user" feature
545 int CtdlLoginExistingUser(char *authname, const char *trythisname)
551 CON_syslog(LOG_DEBUG, "CtdlLoginExistingUser(%s, %s)\n", authname, trythisname);
553 if ((CC->logged_in)) {
554 return login_already_logged_in;
557 if (trythisname == NULL) return login_not_found;
559 if (!strncasecmp(trythisname, "SYS_", 4))
561 CON_syslog(LOG_DEBUG, "System user \"%s\" is not allowed to log in.\n", trythisname);
562 return login_not_found;
565 /* If a "master user" is defined, handle its authentication if specified */
567 if (strlen(config.c_master_user) > 0) if (strlen(config.c_master_pass) > 0) if (authname) {
568 if (!strcasecmp(authname, config.c_master_user)) {
573 /* Continue attempting user validation... */
574 safestrncpy(username, trythisname, sizeof (username));
576 len = cutuserkey(username);
578 if (IsEmptyStr(username)) {
579 return login_not_found;
582 if (config.c_auth_mode == AUTHMODE_HOST) {
587 struct passwd *tempPwdPtr;
590 CON_syslog(LOG_DEBUG, "asking host about <%s>\n", username);
591 #ifdef HAVE_GETPWNAM_R
592 #ifdef SOLARIS_GETPWUID
593 CON_syslog(LOG_DEBUG, "Calling getpwnam_r()\n");
594 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
595 #else // SOLARIS_GETPWUID
596 CONM_syslog(LOG_DEBUG, "Calling getpwnam_r()\n");
597 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
598 #endif // SOLARIS_GETPWUID
599 #else // HAVE_GETPWNAM_R
600 CON_syslog(LOG_DEBUG, "SHOULD NEVER GET HERE!!!\n");
602 #endif // HAVE_GETPWNAM_R
603 if (tempPwdPtr == NULL) {
604 CON_syslog(LOG_DEBUG, "no such user <%s>\n", username);
605 return login_not_found;
608 /* Locate the associated Citadel account.
609 * If not found, make one attempt to create it.
611 found_user = getuserbyuid(&CC->user, pd.pw_uid);
612 CON_syslog(LOG_DEBUG, "found it: uid=%ld, gecos=%s here: %d\n",
613 (long)pd.pw_uid, pd.pw_gecos, found_user);
614 if (found_user != 0) {
615 len = cutuserkey(username);
616 create_user(username, len, 0);
617 found_user = getuserbyuid(&CC->user, pd.pw_uid);
623 else if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
631 found_user = CtdlTryUserLDAP(username, ldap_dn, sizeof ldap_dn, ldap_cn, sizeof ldap_cn, &ldap_uid);
632 if (found_user != 0) {
633 return login_not_found;
636 found_user = getuserbyuid(&CC->user, ldap_uid);
637 if (found_user != 0) {
638 create_user(username, len, 0);
639 found_user = getuserbyuid(&CC->user, ldap_uid);
642 if (found_user == 0) {
643 if (CC->ldap_dn != NULL) free(CC->ldap_dn);
644 CC->ldap_dn = strdup(ldap_dn);
651 /* native auth mode */
653 struct recptypes *valid = NULL;
655 /* First, try to log in as if the supplied name is a display name */
656 found_user = CtdlGetUser(&CC->user, username);
658 /* If that didn't work, try to log in as if the supplied name
659 * is an e-mail address
661 if (found_user != 0) {
662 valid = validate_recipients(username, NULL, 0);
664 if (valid->num_local == 1) {
665 found_user = CtdlGetUser(&CC->user, valid->recp_local);
667 free_recipients(valid);
672 /* Did we find something? */
673 if (found_user == 0) {
674 if (((CC->nologin)) && (CC->user.axlevel < AxAideU)) {
675 return login_too_many_users;
677 safestrncpy(CC->curr_user, CC->user.fullname,
678 sizeof CC->curr_user);
682 return login_not_found;
690 * session startup code which is common to both cmd_pass() and cmd_newu()
694 struct CitContext *CCC = CC;
697 CON_syslog(LOG_NOTICE, "<%s> logged in\n", CCC->curr_user);
699 CtdlGetUserLock(&CCC->user, CCC->curr_user);
700 ++(CCC->user.timescalled);
701 CCC->previous_login = CCC->user.lastcall;
702 time(&CCC->user.lastcall);
704 /* If this user's name is the name of the system administrator
705 * (as specified in setup), automatically assign access level 6.
707 if (!strcasecmp(CCC->user.fullname, config.c_sysadm)) {
708 CCC->user.axlevel = AxAideU;
711 /* If we're authenticating off the host system, automatically give
712 * root the highest level of access.
714 if (config.c_auth_mode == AUTHMODE_HOST) {
715 if (CCC->user.uid == 0) {
716 CCC->user.axlevel = AxAideU;
720 CtdlPutUserLock(&CCC->user);
723 * Populate CCC->cs_inet_email with a default address. This will be
724 * overwritten with the user's directory address, if one exists, when
725 * the vCard module's login hook runs.
727 snprintf(CCC->cs_inet_email, sizeof CCC->cs_inet_email, "%s@%s",
728 CCC->user.fullname, config.c_fqdn);
729 convert_spaces_to_underscores(CCC->cs_inet_email);
731 /* Create any personal rooms required by the system.
732 * (Technically, MAILROOM should be there already, but just in case...)
734 CtdlCreateRoom(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
735 CtdlCreateRoom(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
736 CtdlCreateRoom(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
737 CtdlCreateRoom(USERDRAFTROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
739 /* Run any startup routines registered by loadable modules */
740 PerformSessionHooks(EVT_LOGIN);
742 /* Enter the lobby */
743 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
747 void logged_in_response(void)
749 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
750 CIT_OK, CC->user.fullname, CC->user.axlevel,
751 CC->user.timescalled, CC->user.posted,
752 CC->user.flags, CC->user.usernum,
758 void CtdlUserLogout(void)
760 CitContext *CCC = MyContext();
762 CON_syslog(LOG_DEBUG, "CtdlUserLogout() logging out <%s> from session %d",
763 CCC->curr_user, CCC->cs_pid
766 /* Run any hooks registered by modules... */
767 PerformSessionHooks(EVT_LOGOUT);
770 * Clear out some session data. Most likely, the CitContext for this
771 * session is about to get nuked when the session disconnects, but
772 * since it's possible to log in again without reconnecting, we cannot
773 * make that assumption.
775 strcpy(CCC->fake_username, "");
776 strcpy(CCC->fake_hostname, "");
777 strcpy(CCC->fake_roomname, "");
780 /* Check to see if the user was deleted whilst logged in and purge them if necessary */
781 if ((CCC->user.axlevel == AxDeleted) && (CCC->user.usernum))
782 purge_user(CCC->user.fullname);
784 /* Clear out the user record in memory so we don't behave like a ghost */
785 memset(&CCC->user, 0, sizeof(struct ctdluser));
786 CCC->curr_user[0] = 0;
788 CCC->cs_inet_email[0] = 0;
789 CCC->cs_inet_other_emails[0] = 0;
790 CCC->cs_inet_fn[0] = 0;
791 CCC->fake_username[0] = 0;
792 CCC->fake_hostname[0] = 0;
793 CCC->fake_roomname[0] = 0;
796 /* Free any output buffers */
802 * Validate a password on the host unix system by talking to the chkpwd daemon
804 static int validpw(uid_t uid, const char *pass)
809 if (IsEmptyStr(pass)) {
810 CON_syslog(LOG_DEBUG, "Refusing to chkpwd for uid=%d with empty password.\n", uid);
814 CON_syslog(LOG_DEBUG, "Validating password for uid=%d using chkpwd...\n", uid);
816 begin_critical_section(S_CHKPWD);
817 rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
819 CON_syslog(LOG_EMERG, "Communicatino with chkpwd broken: %s\n", strerror(errno));
820 end_critical_section(S_CHKPWD);
823 rv = write(chkpwd_write_pipe[1], pass, 256);
825 CON_syslog(LOG_EMERG, "Communicatino with chkpwd broken: %s\n", strerror(errno));
826 end_critical_section(S_CHKPWD);
829 rv = read(chkpwd_read_pipe[0], buf, 4);
831 CON_syslog(LOG_EMERG, "Communicatino with chkpwd broken: %s\n", strerror(errno));
832 end_critical_section(S_CHKPWD);
835 end_critical_section(S_CHKPWD);
837 if (!strncmp(buf, "PASS", 4)) {
838 CONM_syslog(LOG_DEBUG, "...pass\n");
842 CONM_syslog(LOG_DEBUG, "...fail\n");
847 * Start up the chkpwd daemon so validpw() has something to talk to
849 void start_chkpwd_daemon(void) {
851 struct stat filestats;
854 CONM_syslog(LOG_DEBUG, "Starting chkpwd daemon for host authentication mode\n");
856 if ((stat(file_chkpwd, &filestats)==-1) ||
857 (filestats.st_size==0)){
858 printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
861 if (pipe(chkpwd_write_pipe) != 0) {
862 CON_syslog(LOG_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
865 if (pipe(chkpwd_read_pipe) != 0) {
866 CON_syslog(LOG_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
871 if (chkpwd_pid < 0) {
872 CON_syslog(LOG_EMERG, "Unable to fork chkpwd daemon: %s\n", strerror(errno));
875 if (chkpwd_pid == 0) {
876 CONM_syslog(LOG_DEBUG, "Now calling dup2() write\n");
877 dup2(chkpwd_write_pipe[0], 0);
878 CONM_syslog(LOG_DEBUG, "Now calling dup2() write\n");
879 dup2(chkpwd_read_pipe[1], 1);
880 CONM_syslog(LOG_DEBUG, "Now closing stuff\n");
881 for (i=2; i<256; ++i) close(i);
882 CON_syslog(LOG_DEBUG, "Now calling execl(%s)\n", file_chkpwd);
883 execl(file_chkpwd, file_chkpwd, NULL);
884 CON_syslog(LOG_EMERG, "Unable to exec chkpwd daemon: %s\n", strerror(errno));
891 int CtdlTryPassword(const char *password, long len)
894 CitContext *CCC = CC;
896 if ((CCC->logged_in)) {
897 CONM_syslog(LOG_WARNING, "CtdlTryPassword: already logged in\n");
898 return pass_already_logged_in;
900 if (!strcmp(CCC->curr_user, NLI)) {
901 CONM_syslog(LOG_WARNING, "CtdlTryPassword: no user selected\n");
904 if (CtdlGetUser(&CCC->user, CCC->curr_user)) {
905 CONM_syslog(LOG_ERR, "CtdlTryPassword: internal error\n");
906 return pass_internal_error;
908 if (password == NULL) {
909 CONM_syslog(LOG_INFO, "CtdlTryPassword: NULL password string supplied\n");
910 return pass_wrong_password;
913 if (CCC->is_master) {
914 code = strcmp(password, config.c_master_pass);
917 else if (config.c_auth_mode == AUTHMODE_HOST) {
921 if (validpw(CCC->user.uid, password)) {
925 * sooper-seekrit hack: populate the password field in the
926 * citadel database with the password that the user typed,
927 * if it's correct. This allows most sites to convert from
928 * host auth to native auth if they want to. If you think
929 * this is a security hazard, comment it out.
932 CtdlGetUserLock(&CCC->user, CCC->curr_user);
933 safestrncpy(CCC->user.password, password, sizeof CCC->user.password);
934 CtdlPutUserLock(&CCC->user);
937 * (sooper-seekrit hack ends here)
947 else if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
951 if ((CCC->ldap_dn) && (!CtdlTryPasswordLDAP(CCC->ldap_dn, password))) {
962 /* native auth mode */
965 pw = (char*) malloc(len + 1);
966 memcpy(pw, password, len + 1);
968 strproc(CCC->user.password);
969 code = strcasecmp(CCC->user.password, pw);
972 strproc(CCC->user.password);
973 code = strcasecmp(CCC->user.password, pw);
982 CON_syslog(LOG_WARNING, "Bad password specified for <%s> Service <%s> Port <%ld> Remote <%s / %s>\n",
990 //citserver[5610]: Bad password specified for <willi> Service <citadel-TCP> Remote <PotzBlitz / >
992 return pass_wrong_password;
1000 * Delete a user record *and* all of its related resources.
1002 int purge_user(char pname[])
1005 struct ctdluser usbuf;
1006 char usernamekey[USERNAME_SIZE];
1008 makeuserkey(usernamekey, pname, cutuserkey(pname));
1010 /* If the name is empty we can't find them in the DB any way so just return */
1011 if (IsEmptyStr(pname))
1012 return (ERROR + NO_SUCH_USER);
1014 if (CtdlGetUser(&usbuf, pname) != 0) {
1015 CON_syslog(LOG_ERR, "Cannot purge user <%s> - not found\n", pname);
1016 return (ERROR + NO_SUCH_USER);
1018 /* Don't delete a user who is currently logged in. Instead, just
1019 * set the access level to 0, and let the account get swept up
1020 * during the next purge.
1022 if (CtdlIsUserLoggedInByNum(usbuf.usernum)) {
1023 CON_syslog(LOG_WARNING, "User <%s> is logged in; not deleting.\n", pname);
1024 usbuf.axlevel = AxDeleted;
1025 CtdlPutUser(&usbuf);
1028 CON_syslog(LOG_NOTICE, "Deleting user <%s>\n", pname);
1032 * This should all be wrapped in a S_USERS mutex.
1033 * Without the mutex the user could log in before we get to the next function
1034 * That would truly mess things up :-(
1035 * I would like to see the S_USERS start before the CtdlIsUserLoggedInByNum() above
1036 * and end after the user has been deleted from the database, below.
1037 * Question is should we enter the EVT_PURGEUSER whilst S_USERS is active?
1040 /* Perform any purge functions registered by server extensions */
1041 PerformUserHooks(&usbuf, EVT_PURGEUSER);
1043 /* delete any existing user/room relationships */
1044 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
1046 /* delete the users-by-number index record */
1047 cdb_delete(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long));
1049 /* delete the userlog entry */
1050 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
1052 /* remove the user's bio file */
1060 /* remove the user's picture */
1072 int internal_create_user (const char *username, long len, struct ctdluser *usbuf, uid_t uid)
1074 if (!CtdlGetUserLen(usbuf, username, len)) {
1075 return (ERROR + ALREADY_EXISTS);
1078 /* Go ahead and initialize a new user record */
1079 memset(usbuf, 0, sizeof(struct ctdluser));
1080 safestrncpy(usbuf->fullname, username, sizeof usbuf->fullname);
1081 strcpy(usbuf->password, "");
1084 /* These are the default flags on new accounts */
1085 usbuf->flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
1087 usbuf->timescalled = 0;
1089 usbuf->axlevel = config.c_initax;
1090 usbuf->lastcall = time(NULL);
1092 /* fetch a new user number */
1093 usbuf->usernum = get_new_user_number();
1095 /* add user to the database */
1097 cdb_store(CDB_USERSBYNUMBER, &usbuf->usernum, sizeof(long), usbuf->fullname, strlen(usbuf->fullname)+1);
1105 * create_user() - back end processing to create a new user
1107 * Set 'newusername' to the desired account name.
1108 * Set 'become_user' to nonzero if this is self-service account creation and we want
1109 * to actually log in as the user we just created, otherwise set it to 0.
1111 int create_user(const char *newusername, long len, int become_user)
1113 struct ctdluser usbuf;
1114 struct ctdlroom qrbuf;
1116 char mailboxname[ROOMNAMELEN];
1122 safestrncpy(username, newusername, sizeof username);
1126 if (config.c_auth_mode == AUTHMODE_HOST) {
1128 /* host auth mode */
1131 struct passwd *tempPwdPtr;
1132 char pwdbuffer[SIZ];
1134 #ifdef HAVE_GETPWNAM_R
1135 #ifdef SOLARIS_GETPWUID
1136 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof(pwdbuffer));
1137 #else // SOLARIS_GETPWUID
1138 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
1139 #endif // SOLARIS_GETPWUID
1140 #else // HAVE_GETPWNAM_R
1142 #endif // HAVE_GETPWNAM_R
1143 if (tempPwdPtr != NULL) {
1144 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
1146 if (IsEmptyStr (username))
1148 safestrncpy(username, pd.pw_name, sizeof username);
1149 len = cutuserkey(username);
1153 return (ERROR + NO_SUCH_USER);
1158 if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
1159 if (CtdlTryUserLDAP(username, NULL, 0, username, sizeof username, &uid) != 0) {
1160 return(ERROR + NO_SUCH_USER);
1163 #endif /* HAVE_LDAP */
1165 if ((retval = internal_create_user(username, len, &usbuf, uid)) != 0)
1169 * Give the user a private mailbox and a configuration room.
1170 * Make the latter an invisible system room.
1172 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
1173 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
1175 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
1176 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
1177 if (CtdlGetRoomLock(&qrbuf, mailboxname) == 0) {
1178 qrbuf.QRflags2 |= QR2_SYSTEM;
1179 CtdlPutRoomLock(&qrbuf);
1182 /* Perform any create functions registered by server extensions */
1183 PerformUserHooks(&usbuf, EVT_NEWUSER);
1185 /* Everything below this line can be bypassed if administratively
1186 * creating a user, instead of doing self-service account creation
1190 /* Now become the user we just created */
1191 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
1192 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
1195 /* Check to make sure we're still who we think we are */
1196 if (CtdlGetUser(&CC->user, CC->curr_user)) {
1197 return (ERROR + INTERNAL_ERROR);
1202 "New user account <%s> has been created, from host %s [%s].\n",
1207 CtdlAideMessage(buf, "User Creation Notice");
1208 CON_syslog(LOG_NOTICE, "New user <%s> created\n", username);
1215 * set password - back end api code
1217 void CtdlSetPassword(char *new_pw)
1219 CtdlGetUserLock(&CC->user, CC->curr_user);
1220 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1221 CtdlPutUserLock(&CC->user);
1222 CON_syslog(LOG_INFO, "Password changed for user <%s>\n", CC->curr_user);
1223 PerformSessionHooks(EVT_SETPASS);
1230 * API function for cmd_invt_kick() and anything else that needs to
1231 * invite or kick out a user to/from a room.
1233 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1235 int CtdlInvtKick(char *iuser, int op) {
1236 struct ctdluser USscratch;
1240 if (CtdlGetUser(&USscratch, iuser) != 0) {
1244 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1246 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1247 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1250 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1251 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1253 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1255 /* post a message in Aide> saying what we just did */
1256 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1258 ((op == 1) ? "invited to" : "kicked out of"),
1260 (CC->logged_in ? CC->user.fullname : "an administrator")
1262 CtdlAideMessage(bbb,"User Admin Message");
1269 * Forget (Zap) the current room (API call)
1270 * Returns 0 on success
1272 int CtdlForgetThisRoom(void) {
1275 /* On some systems, Admins are not allowed to forget rooms */
1276 if (is_aide() && (config.c_aide_zap == 0)
1277 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1281 CtdlGetUserLock(&CC->user, CC->curr_user);
1282 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1284 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1285 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1287 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1288 CtdlPutUserLock(&CC->user);
1290 /* Return to the Lobby, so we don't end up in an undefined room */
1291 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
1300 * Traverse the user file...
1302 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1305 struct ctdluser usbuf;
1306 struct cdbdata *cdbus;
1308 cdb_rewind(CDB_USERS);
1310 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1311 memset(&usbuf, 0, sizeof(struct ctdluser));
1312 memcpy(&usbuf, cdbus->ptr,
1313 ((cdbus->len > sizeof(struct ctdluser)) ?
1314 sizeof(struct ctdluser) : cdbus->len));
1316 (*CallBack) (&usbuf, in_data);
1322 * List one user (this works with cmd_list)
1324 void ListThisUser(struct ctdluser *usbuf, void *data)
1328 searchstring = (char *)data;
1329 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1333 if (usbuf->axlevel > AxDeleted) {
1334 if ((CC->user.axlevel >= AxAideU)
1335 || ((usbuf->flags & US_UNLISTED) == 0)
1336 || ((CC->internal_pgm))) {
1337 cprintf("%s|%d|%ld|%ld|%ld|%ld||\n",
1341 (long)usbuf->lastcall,
1352 * Count the number of new mail messages the user has
1356 int num_newmsgs = 0;
1358 num_newmsgs = CC->newmail;
1361 return (num_newmsgs);
1366 * Count the number of new mail messages the user has
1368 int InitialMailCheck()
1370 int num_newmsgs = 0;
1372 char mailboxname[ROOMNAMELEN];
1373 struct ctdlroom mailbox;
1375 struct cdbdata *cdbfr;
1376 long *msglist = NULL;
1379 CtdlMailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1380 if (CtdlGetRoom(&mailbox, mailboxname) != 0)
1382 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1384 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1386 if (cdbfr != NULL) {
1387 msglist = malloc(cdbfr->len);
1388 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1389 num_msgs = cdbfr->len / sizeof(long);
1393 for (a = 0; a < num_msgs; ++a) {
1394 if (msglist[a] > 0L) {
1395 if (msglist[a] > vbuf.v_lastseen) {
1400 if (msglist != NULL)
1403 return (num_newmsgs);