4 * Server functions which perform operations on user objects.
17 #include <sys/types.h>
19 #ifdef HAVE_SYS_STAT_H
23 #if TIME_WITH_SYS_TIME
24 # include <sys/time.h>
28 # include <sys/time.h>
36 #include <libcitadel.h>
42 #include "sysdep_decls.h"
49 #include "citserver.h"
50 #include "citadel_dirs.h"
54 /* These pipes are used to talk to the chkpwd daemon, which is forked during startup */
55 int chkpwd_write_pipe[2];
56 int chkpwd_read_pipe[2];
59 * makeuserkey() - convert a username into the format used as a database key
60 * (it's just the username converted into lower case)
62 static INLINE void makeuserkey(char *key, char *username) {
65 len = strlen(username);
66 if (len >= USERNAME_SIZE)
68 lprintf (CTDL_EMERG, "Username to long: %s", username);
70 len = USERNAME_SIZE - 1;
71 username[USERNAME_SIZE - 1]='\0';
73 for (i=0; i<=len; ++i) {
74 key[i] = tolower(username[i]);
80 * getuser() - retrieve named user into supplied buffer.
81 * returns 0 on success
83 int getuser(struct ctdluser *usbuf, char name[])
86 char usernamekey[USERNAME_SIZE];
87 struct cdbdata *cdbus;
90 memset(usbuf, 0, sizeof(struct ctdluser));
93 makeuserkey(usernamekey, name);
94 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
96 if (cdbus == NULL) { /* user not found */
100 memcpy(usbuf, cdbus->ptr,
101 ((cdbus->len > sizeof(struct ctdluser)) ?
102 sizeof(struct ctdluser) : cdbus->len));
111 * lgetuser() - same as getuser() but locks the record
113 int lgetuser(struct ctdluser *usbuf, char *name)
117 retcode = getuser(usbuf, name);
119 begin_critical_section(S_USERS);
126 * putuser() - write user buffer into the correct place on disk
128 void putuser(struct ctdluser *usbuf)
130 char usernamekey[USERNAME_SIZE];
132 makeuserkey(usernamekey, usbuf->fullname);
134 usbuf->version = REV_LEVEL;
136 usernamekey, strlen(usernamekey),
137 usbuf, sizeof(struct ctdluser));
143 * lputuser() - same as putuser() but locks the record
145 void lputuser(struct ctdluser *usbuf)
148 end_critical_section(S_USERS);
152 * Index-generating function used by Ctdl[Get|Set]Relationship
154 int GenerateRelationshipIndex(char *IndexBuf,
166 TheIndex.iRoomID = RoomID;
167 TheIndex.iRoomGen = RoomGen;
168 TheIndex.iUserID = UserID;
170 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
171 return (sizeof(TheIndex));
177 * Back end for CtdlSetRelationship()
179 void put_visit(struct visit *newvisit)
184 memset (IndexBuf, 0, sizeof (IndexBuf));
185 /* Generate an index */
186 IndexLen = GenerateRelationshipIndex(IndexBuf,
189 newvisit->v_usernum);
191 /* Store the record */
192 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
193 newvisit, sizeof(struct visit)
201 * Define a relationship between a user and a room
203 void CtdlSetRelationship(struct visit *newvisit,
204 struct ctdluser *rel_user,
205 struct ctdlroom *rel_room)
209 /* We don't use these in Citadel because they're implicit by the
210 * index, but they must be present if the database is exported.
212 newvisit->v_roomnum = rel_room->QRnumber;
213 newvisit->v_roomgen = rel_room->QRgen;
214 newvisit->v_usernum = rel_user->usernum;
220 * Locate a relationship between a user and a room
222 void CtdlGetRelationship(struct visit *vbuf,
223 struct ctdluser *rel_user,
224 struct ctdlroom *rel_room)
229 struct cdbdata *cdbvisit;
231 /* Generate an index */
232 IndexLen = GenerateRelationshipIndex(IndexBuf,
237 /* Clear out the buffer */
238 memset(vbuf, 0, sizeof(struct visit));
240 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
241 if (cdbvisit != NULL) {
242 memcpy(vbuf, cdbvisit->ptr,
243 ((cdbvisit->len > sizeof(struct visit)) ?
244 sizeof(struct visit) : cdbvisit->len));
248 /* If this is the first time the user has seen this room,
249 * set the view to be the default for the room.
251 vbuf->v_view = rel_room->QRdefaultview;
254 /* Set v_seen if necessary */
255 if (vbuf->v_seen[0] == 0) {
256 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
261 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
263 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
268 * Is the user currently logged in an Aide?
272 if (CC->user.axlevel >= 6)
280 * Is the user currently logged in an Aide *or* the room aide for this room?
282 int is_room_aide(void)
285 if (!CC->logged_in) {
289 if ((CC->user.axlevel >= 6)
290 || (CC->room.QRroomaide == CC->user.usernum)) {
298 * getuserbynumber() - get user by number
299 * returns 0 if user was found
301 * WARNING: don't use this function unless you absolutely have to. It does
302 * a sequential search and therefore is computationally expensive.
304 int getuserbynumber(struct ctdluser *usbuf, long int number)
306 struct cdbdata *cdbus;
308 cdb_rewind(CDB_USERS);
310 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
311 memset(usbuf, 0, sizeof(struct ctdluser));
312 memcpy(usbuf, cdbus->ptr,
313 ((cdbus->len > sizeof(struct ctdluser)) ?
314 sizeof(struct ctdluser) : cdbus->len));
316 if (usbuf->usernum == number) {
317 cdb_close_cursor(CDB_USERS);
326 * getuserbyuid() - get user by system uid (for PAM mode authentication)
327 * returns 0 if user was found
329 * WARNING: don't use this function unless you absolutely have to. It does
330 * a sequential search and therefore is computationally expensive.
332 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
334 struct cdbdata *cdbus;
336 cdb_rewind(CDB_USERS);
338 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
339 memset(usbuf, 0, sizeof(struct ctdluser));
340 memcpy(usbuf, cdbus->ptr,
341 ((cdbus->len > sizeof(struct ctdluser)) ?
342 sizeof(struct ctdluser) : cdbus->len));
344 if (usbuf->uid == number) {
345 cdb_close_cursor(CDB_USERS);
353 * Back end for cmd_user() and its ilk
355 * NOTE: "authname" should only be used if we are attempting to use the "master user" feature
357 int CtdlLoginExistingUser(char *authname, char *trythisname)
362 lprintf(9, "CtdlLoginExistingUser(%s, %s)\n", authname, trythisname);
364 if ((CC->logged_in)) {
365 return login_already_logged_in;
368 if (trythisname == NULL) return login_not_found;
370 /* If a "master user" is defined, handle its authentication if specified */
372 if (strlen(config.c_master_user) > 0) if (strlen(config.c_master_pass) > 0) if (authname) {
373 if (!strcasecmp(authname, config.c_master_user)) {
378 /* Continue attempting user validation... */
379 safestrncpy(username, trythisname, USERNAME_SIZE);
382 if (IsEmptyStr(username)) {
383 return login_not_found;
386 if (config.c_auth_mode == AUTHMODE_HOST) {
391 struct passwd *tempPwdPtr;
394 lprintf(CTDL_DEBUG, "asking host about <%s>\n", username);
395 #ifdef HAVE_GETPWNAM_R
396 #ifdef SOLARIS_GETPWUID
397 lprintf(CTDL_DEBUG, "Calling getpwnam_r()\n");
398 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
399 #else // SOLARIS_GETPWUID
400 lprintf(CTDL_DEBUG, "Calling getpwnam_r()\n");
401 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
402 #endif // SOLARIS_GETPWUID
403 #else // HAVE_GETPWNAM_R
404 lprintf(CTDL_DEBUG, "SHOULD NEVER GET HERE!!!\n");
406 #endif // HAVE_GETPWNAM_R
407 if (tempPwdPtr == NULL) {
408 lprintf(CTDL_DEBUG, "no such user <%s>\n", username);
409 return login_not_found;
412 /* Locate the associated Citadel account.
413 * If not found, make one attempt to create it.
415 found_user = getuserbyuid(&CC->user, pd.pw_uid);
416 lprintf(CTDL_DEBUG, "found it: uid=%ld, gecos=%s here: %d\n",
417 (long)pd.pw_uid, pd.pw_gecos, found_user);
418 if (found_user != 0) {
419 create_user(username, 0);
420 found_user = getuserbyuid(&CC->user, pd.pw_uid);
426 /* native auth mode */
428 struct recptypes *valid = NULL;
430 /* First, try to log in as if the supplied name is a display name */
431 found_user = getuser(&CC->user, username);
433 /* If that didn't work, try to log in as if the supplied name
434 * is an e-mail address
436 if (found_user != 0) {
437 valid = validate_recipients(username, NULL, 0);
439 if (valid->num_local == 1) {
440 found_user = getuser(&CC->user, valid->recp_local);
442 free_recipients(valid);
447 /* Did we find something? */
448 if (found_user == 0) {
449 if (((CC->nologin)) && (CC->user.axlevel < 6)) {
450 return login_too_many_users;
452 safestrncpy(CC->curr_user, CC->user.fullname,
453 sizeof CC->curr_user);
457 return login_not_found;
465 void cmd_user(char *cmdbuf)
470 extract_token(username, cmdbuf, 0, '|', sizeof username);
473 a = CtdlLoginExistingUser(NULL, username);
475 case login_already_logged_in:
476 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
478 case login_too_many_users:
480 "Too many users are already online "
482 ERROR + MAX_SESSIONS_EXCEEDED,
483 config.c_nodename, config.c_maxsessions);
486 cprintf("%d Password required for %s\n",
487 MORE_DATA, CC->curr_user);
489 case login_not_found:
490 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
493 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
500 * session startup code which is common to both cmd_pass() and cmd_newu()
502 void session_startup(void)
506 lprintf(CTDL_NOTICE, "<%s> logged in\n", CC->curr_user);
508 lgetuser(&CC->user, CC->curr_user);
509 ++(CC->user.timescalled);
510 CC->previous_login = CC->user.lastcall;
511 time(&CC->user.lastcall);
513 /* If this user's name is the name of the system administrator
514 * (as specified in setup), automatically assign access level 6.
516 if (!strcasecmp(CC->user.fullname, config.c_sysadm)) {
517 CC->user.axlevel = 6;
520 /* If we're authenticating off the host system, automatically give
521 * root the highest level of access.
523 if (config.c_auth_mode == AUTHMODE_HOST) {
524 if (CC->user.uid == 0) {
525 CC->user.axlevel = 6;
532 * Populate CC->cs_inet_email with a default address. This will be
533 * overwritten with the user's directory address, if one exists, when
534 * the vCard module's login hook runs.
536 snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
537 CC->user.fullname, config.c_fqdn);
538 for (i=0; !IsEmptyStr(&CC->cs_inet_email[i]); ++i) {
539 if (isspace(CC->cs_inet_email[i])) {
540 CC->cs_inet_email[i] = '_';
544 /* Create any personal rooms required by the system.
545 * (Technically, MAILROOM should be there already, but just in case...)
547 create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
548 create_room(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
549 create_room(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
551 /* Run any startup routines registered by loadable modules */
552 PerformSessionHooks(EVT_LOGIN);
554 /* Enter the lobby */
555 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
559 void logged_in_response(void)
561 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
562 CIT_OK, CC->user.fullname, CC->user.axlevel,
563 CC->user.timescalled, CC->user.posted,
564 CC->user.flags, CC->user.usernum,
571 * misc things to be taken care of when a user is logged out
573 void logout(struct CitContext *who)
576 * Clear out some session data. Most likely, the CitContext for this
577 * session is about to get nuked when the session disconnects, but
578 * since it's possible to log in again without reconnecting, we cannot
579 * make that assumption.
581 strcpy(who->fake_username, "");
582 strcpy(who->fake_hostname, "");
583 strcpy(who->fake_roomname, "");
587 * If there is a download in progress, abort it.
589 if (who->download_fp != NULL) {
590 fclose(who->download_fp);
591 who->download_fp = NULL;
595 * If there is an upload in progress, abort it.
597 if (who->upload_fp != NULL) {
602 * If we were talking to a network node, we're not anymore...
604 if (!IsEmptyStr(who->net_node)) {
605 network_talking_to(who->net_node, NTT_REMOVE);
608 /* Do modular stuff... */
609 PerformSessionHooks(EVT_LOGOUT);
611 /* Check to see if the user was deleted whilst logged in and purge them if necessary */
612 if (who->user.axlevel == 0)
613 purge_user(who->user.fullname);
615 /* Free any output buffers */
616 if (who->output_buffer != NULL) {
622 * Validate a password on the host unix system by talking to the chkpwd daemon
624 static int validpw(uid_t uid, const char *pass)
628 lprintf(CTDL_DEBUG, "Validating password for uid=%d using chkpwd...\n", uid);
630 begin_critical_section(S_CHKPWD);
631 write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
632 write(chkpwd_write_pipe[1], pass, 256);
633 read(chkpwd_read_pipe[0], buf, 4);
634 end_critical_section(S_CHKPWD);
636 if (!strncmp(buf, "PASS", 4)) {
637 lprintf(CTDL_DEBUG, "...pass\n");
641 lprintf(CTDL_DEBUG, "...fail\n");
646 * Start up the chkpwd daemon so validpw() has something to talk to
648 void start_chkpwd_daemon(void) {
650 struct stat filestats;
653 lprintf(CTDL_DEBUG, "Starting chkpwd daemon for host authentication mode\n");
655 if ((stat(file_chkpwd, &filestats)==-1) ||
656 (filestats.st_size==0)){
657 printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
660 if (pipe(chkpwd_write_pipe) != 0) {
661 lprintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
664 if (pipe(chkpwd_read_pipe) != 0) {
665 lprintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
670 if (chkpwd_pid < 0) {
671 lprintf(CTDL_EMERG, "Unable to fork chkpwd daemon: %s\n", strerror(errno));
674 if (chkpwd_pid == 0) {
675 lprintf(CTDL_DEBUG, "Now calling dup2() write\n");
676 dup2(chkpwd_write_pipe[0], 0);
677 lprintf(CTDL_DEBUG, "Now calling dup2() write\n");
678 dup2(chkpwd_read_pipe[1], 1);
679 lprintf(CTDL_DEBUG, "Now closing stuff\n");
680 for (i=2; i<256; ++i) close(i);
681 lprintf(CTDL_DEBUG, "Now calling execl(%s)\n", file_chkpwd);
682 execl(file_chkpwd, file_chkpwd, NULL);
683 lprintf(CTDL_EMERG, "Unable to exec chkpwd daemon: %s\n", strerror(errno));
697 int CtdlTryPassword(char *password)
701 if ((CC->logged_in)) {
702 lprintf(CTDL_WARNING, "CtdlTryPassword: already logged in\n");
703 return pass_already_logged_in;
705 if (!strcmp(CC->curr_user, NLI)) {
706 lprintf(CTDL_WARNING, "CtdlTryPassword: no user selected\n");
709 if (getuser(&CC->user, CC->curr_user)) {
710 lprintf(CTDL_ERR, "CtdlTryPassword: internal error\n");
711 return pass_internal_error;
713 if (password == NULL) {
714 lprintf(CTDL_INFO, "CtdlTryPassword: NULL password string supplied\n");
715 return pass_wrong_password;
720 code = strcmp(password, config.c_master_pass);
723 else if (config.c_auth_mode == AUTHMODE_HOST) {
727 if (validpw(CC->user.uid, password)) {
731 * sooper-seekrit hack: populate the password field in the
732 * citadel database with the password that the user typed,
733 * if it's correct. This allows most sites to convert from
734 * host auth to native auth if they want to. If you think
735 * this is a security hazard, comment it out.
738 lgetuser(&CC->user, CC->curr_user);
739 safestrncpy(CC->user.password, password, sizeof CC->user.password);
743 * (sooper-seekrit hack ends here)
754 /* native auth mode */
757 strproc(CC->user.password);
758 code = strcasecmp(CC->user.password, password);
760 strproc(CC->user.password);
761 code = strcasecmp(CC->user.password, password);
768 lprintf(CTDL_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
769 return pass_wrong_password;
774 void cmd_pass(char *buf)
779 extract_token(password, buf, 0, '|', sizeof password);
780 a = CtdlTryPassword(password);
783 case pass_already_logged_in:
784 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
787 cprintf("%d You must send a name with USER first.\n",
788 ERROR + USERNAME_REQUIRED);
790 case pass_wrong_password:
791 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
794 logged_in_response();
802 * Delete a user record *and* all of its related resources.
804 int purge_user(char pname[])
807 struct ctdluser usbuf;
808 char usernamekey[USERNAME_SIZE];
809 struct CitContext *ccptr;
810 int user_is_logged_in = 0;
812 makeuserkey(usernamekey, pname);
814 /* If the name is empty we can't find them in the DB any way so just return */
815 if (IsEmptyStr(pname))
816 return (ERROR + NO_SUCH_USER);
818 if (getuser(&usbuf, pname) != 0) {
819 lprintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
820 return (ERROR + NO_SUCH_USER);
822 /* Don't delete a user who is currently logged in. Instead, just
823 * set the access level to 0, and let the account get swept up
824 * during the next purge.
826 user_is_logged_in = 0;
827 begin_critical_section(S_SESSION_TABLE);
828 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
829 if (ccptr->user.usernum == usbuf.usernum) {
830 user_is_logged_in = 1;
833 end_critical_section(S_SESSION_TABLE);
834 if (user_is_logged_in == 1) {
835 lprintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
840 lprintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
842 /* Perform any purge functions registered by server extensions */
843 PerformUserHooks(&usbuf, EVT_PURGEUSER);
845 /* delete any existing user/room relationships */
846 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
848 /* delete the userlog entry */
849 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
851 /* remove the user's bio file */
859 /* remove the user's picture */
872 * create_user() - back end processing to create a new user
874 * Set 'newusername' to the desired account name.
875 * Set 'become_user' to nonzero if this is self-service account creation and we want
876 * to actually log in as the user we just created, otherwise set it to 0.
878 int create_user(char *newusername, int become_user)
880 struct ctdluser usbuf;
881 struct ctdlroom qrbuf;
883 char mailboxname[ROOMNAMELEN];
887 safestrncpy(username, newusername, sizeof username);
890 if (config.c_auth_mode == AUTHMODE_HOST) {
895 struct passwd *tempPwdPtr;
898 #ifdef HAVE_GETPWNAM_R
899 #ifdef SOLARIS_GETPWUID
900 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof(pwdbuffer));
901 #else // SOLARIS_GETPWUID
902 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
903 #endif // SOLARIS_GETPWUID
904 #else // HAVE_GETPWNAM_R
906 #endif // HAVE_GETPWNAM_R
907 if (tempPwdPtr != NULL) {
908 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
910 if (IsEmptyStr (username))
913 "Can't find Realname for user %s [%d] in the Host Auth Database; giving up.\n",
914 newusername, pd.pw_uid);
916 "Can't find Realname for user %s [%d] in the Host Auth Database; giving up.\n",
917 newusername, pd.pw_uid);
918 aide_message(buf, "User Creation Failure Notice");
923 return (ERROR + NO_SUCH_USER);
927 if (!getuser(&usbuf, username)) {
928 return (ERROR + ALREADY_EXISTS);
931 /* Go ahead and initialize a new user record */
932 memset(&usbuf, 0, sizeof(struct ctdluser));
933 safestrncpy(usbuf.fullname, username, sizeof usbuf.fullname);
934 strcpy(usbuf.password, "");
937 /* These are the default flags on new accounts */
938 usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
940 usbuf.timescalled = 0;
942 usbuf.axlevel = config.c_initax;
943 usbuf.USscreenwidth = 80;
944 usbuf.USscreenheight = 24;
945 usbuf.lastcall = time(NULL);
947 /* fetch a new user number */
948 usbuf.usernum = get_new_user_number();
950 /* The very first user created on the system will always be an Aide */
951 if (usbuf.usernum == 1L) {
955 /* add user to userlog */
959 * Give the user a private mailbox and a configuration room.
960 * Make the latter an invisible system room.
962 MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
963 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
965 MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
966 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
967 if (lgetroom(&qrbuf, mailboxname) == 0) {
968 qrbuf.QRflags2 |= QR2_SYSTEM;
972 /* Perform any create functions registered by server extensions */
973 PerformUserHooks(&usbuf, EVT_NEWUSER);
975 /* Everything below this line can be bypassed if administratively
976 * creating a user, instead of doing self-service account creation
980 /* Now become the user we just created */
981 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
982 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
985 /* Check to make sure we're still who we think we are */
986 if (getuser(&CC->user, CC->curr_user)) {
987 return (ERROR + INTERNAL_ERROR);
992 "New user account <%s> has been created, from host %s [%s].\n",
997 aide_message(buf, "User Creation Notice");
998 lprintf(CTDL_NOTICE, "New user <%s> created\n", username);
1006 * cmd_newu() - create a new user account and log in as that user
1008 void cmd_newu(char *cmdbuf)
1013 if (config.c_auth_mode != AUTHMODE_NATIVE) {
1014 cprintf("%d This system does not use native mode authentication.\n",
1019 if (config.c_disable_newu) {
1020 cprintf("%d Self-service user account creation "
1021 "is disabled on this system.\n", ERROR + NOT_HERE);
1025 if (CC->logged_in) {
1026 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
1030 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
1031 ERROR + MAX_SESSIONS_EXCEEDED,
1032 config.c_nodename, config.c_maxsessions);
1034 extract_token(username, cmdbuf, 0, '|', sizeof username);
1038 if (IsEmptyStr(username)) {
1039 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1043 if ((!strcasecmp(username, "bbs")) ||
1044 (!strcasecmp(username, "new")) ||
1045 (!strcasecmp(username, "."))) {
1046 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
1050 a = create_user(username, 1);
1054 logged_in_response();
1055 } else if (a == ERROR + ALREADY_EXISTS) {
1056 cprintf("%d '%s' already exists.\n",
1057 ERROR + ALREADY_EXISTS, username);
1059 } else if (a == ERROR + INTERNAL_ERROR) {
1060 cprintf("%d Internal error - user record disappeared?\n",
1061 ERROR + INTERNAL_ERROR);
1064 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
1073 void cmd_setp(char *new_pw)
1075 if (CtdlAccessCheck(ac_logged_in)) {
1078 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
1079 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
1082 if (CC->is_master) {
1083 cprintf("%d The master prefix password cannot be changed with this command.\n",
1088 if (IsEmptyStr(new_pw)) {
1089 cprintf("%d Password unchanged.\n", CIT_OK);
1092 lgetuser(&CC->user, CC->curr_user);
1093 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1094 lputuser(&CC->user);
1095 cprintf("%d Password changed.\n", CIT_OK);
1096 lprintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
1097 PerformSessionHooks(EVT_SETPASS);
1102 * cmd_creu() - administratively create a new user account (do not log in to it)
1104 void cmd_creu(char *cmdbuf)
1109 struct ctdluser tmp;
1111 if (CtdlAccessCheck(ac_aide)) {
1115 extract_token(username, cmdbuf, 0, '|', sizeof username);
1116 extract_token(password, cmdbuf, 1, '|', sizeof password);
1122 if (IsEmptyStr(username)) {
1123 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1127 a = create_user(username, 0);
1130 if (!IsEmptyStr(password)) {
1131 lgetuser(&tmp, username);
1132 safestrncpy(tmp.password, password, sizeof(tmp.password));
1135 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1136 (!IsEmptyStr(password)) ? "and password set" :
1137 "with no password");
1139 } else if (a == ERROR + ALREADY_EXISTS) {
1140 cprintf("%d '%s' already exists.\n", ERROR + ALREADY_EXISTS, username);
1142 } else if ( (config.c_auth_mode != AUTHMODE_NATIVE) && (a == ERROR + NO_SUCH_USER) ) {
1143 cprintf("%d User accounts are not created within Citadel in host authentication mode.\n",
1144 ERROR + NO_SUCH_USER);
1147 cprintf("%d An error occurred creating the user account.\n", ERROR + INTERNAL_ERROR);
1154 * get user parameters
1159 if (CtdlAccessCheck(ac_logged_in))
1162 getuser(&CC->user, CC->curr_user);
1163 cprintf("%d %d|%d|%d|\n",
1165 CC->user.USscreenwidth,
1166 CC->user.USscreenheight,
1167 (CC->user.flags & US_USER_SET)
1172 * set user parameters
1174 void cmd_setu(char *new_parms)
1176 if (CtdlAccessCheck(ac_logged_in))
1179 if (num_parms(new_parms) < 3) {
1180 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1183 lgetuser(&CC->user, CC->curr_user);
1184 CC->user.USscreenwidth = extract_int(new_parms, 0);
1185 CC->user.USscreenheight = extract_int(new_parms, 1);
1186 CC->user.flags = CC->user.flags & (~US_USER_SET);
1187 CC->user.flags = CC->user.flags |
1188 (extract_int(new_parms, 2) & US_USER_SET);
1190 lputuser(&CC->user);
1191 cprintf("%d Ok\n", CIT_OK);
1195 * set last read pointer
1197 void cmd_slrp(char *new_ptr)
1201 struct visit original_vbuf;
1203 if (CtdlAccessCheck(ac_logged_in)) {
1207 if (!strncasecmp(new_ptr, "highest", 7)) {
1208 newlr = CC->room.QRhighest;
1210 newlr = atol(new_ptr);
1213 lgetuser(&CC->user, CC->curr_user);
1215 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1216 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1217 vbuf.v_lastseen = newlr;
1218 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1220 /* Only rewrite the record if it changed */
1221 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1222 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1223 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1226 lputuser(&CC->user);
1227 cprintf("%d %ld\n", CIT_OK, newlr);
1231 void cmd_seen(char *argbuf) {
1232 long target_msgnum = 0L;
1233 int target_setting = 0;
1235 if (CtdlAccessCheck(ac_logged_in)) {
1239 if (num_parms(argbuf) != 2) {
1240 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1244 target_msgnum = extract_long(argbuf, 0);
1245 target_setting = extract_int(argbuf, 1);
1247 CtdlSetSeen(&target_msgnum, 1, target_setting,
1248 ctdlsetseen_seen, NULL, NULL);
1249 cprintf("%d OK\n", CIT_OK);
1253 void cmd_gtsn(char *argbuf) {
1256 if (CtdlAccessCheck(ac_logged_in)) {
1260 CtdlGetSeen(buf, ctdlsetseen_seen);
1261 cprintf("%d %s\n", CIT_OK, buf);
1266 * API function for cmd_invt_kick() and anything else that needs to
1267 * invite or kick out a user to/from a room.
1269 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1271 int CtdlInvtKick(char *iuser, int op) {
1272 struct ctdluser USscratch;
1276 if (getuser(&USscratch, iuser) != 0) {
1280 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1282 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1283 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1286 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1287 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1289 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1291 /* post a message in Aide> saying what we just did */
1292 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1294 ((op == 1) ? "invited to" : "kicked out of"),
1297 aide_message(bbb,"User Admin Message");
1304 * INVT and KICK commands
1306 void cmd_invt_kick(char *iuser, int op) {
1309 * These commands are only allowed by aides, room aides,
1310 * and room namespace owners
1313 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1314 /* access granted */
1317 cprintf("%d Higher access or room ownership required.\n",
1318 ERROR + HIGHER_ACCESS_REQUIRED);
1322 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1324 cprintf("%d Can't add/remove users from this room.\n",
1329 if (CtdlInvtKick(iuser, op) != 0) {
1330 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1334 cprintf("%d %s %s %s.\n",
1336 ((op == 1) ? "invited to" : "kicked out of"),
1343 * Forget (Zap) the current room (API call)
1344 * Returns 0 on success
1346 int CtdlForgetThisRoom(void) {
1349 /* On some systems, Aides are not allowed to forget rooms */
1350 if (is_aide() && (config.c_aide_zap == 0)
1351 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1355 lgetuser(&CC->user, CC->curr_user);
1356 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1358 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1359 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1361 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1362 lputuser(&CC->user);
1364 /* Return to the Lobby, so we don't end up in an undefined room */
1365 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1372 * forget (Zap) the current room
1377 if (CtdlAccessCheck(ac_logged_in)) {
1381 if (CtdlForgetThisRoom() == 0) {
1382 cprintf("%d Ok\n", CIT_OK);
1385 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1390 * Get Next Unregistered User
1394 struct cdbdata *cdbus;
1395 struct ctdluser usbuf;
1397 if (CtdlAccessCheck(ac_aide)) {
1401 if ((CitControl.MMflags & MM_VALID) == 0) {
1402 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1406 /* There are unvalidated users. Traverse the user database,
1407 * and return the first user we find that needs validation.
1409 cdb_rewind(CDB_USERS);
1410 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1411 memset(&usbuf, 0, sizeof(struct ctdluser));
1412 memcpy(&usbuf, cdbus->ptr,
1413 ((cdbus->len > sizeof(struct ctdluser)) ?
1414 sizeof(struct ctdluser) : cdbus->len));
1416 if ((usbuf.flags & US_NEEDVALID)
1417 && (usbuf.axlevel > 0)) {
1418 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1419 cdb_close_cursor(CDB_USERS);
1424 /* If we get to this point, there are no more unvalidated users.
1425 * Therefore we clear the "users need validation" flag.
1428 begin_critical_section(S_CONTROL);
1430 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1432 end_critical_section(S_CONTROL);
1433 cprintf("%d *** End of registration.\n", CIT_OK);
1442 void cmd_vali(char *v_args)
1446 struct ctdluser userbuf;
1448 extract_token(user, v_args, 0, '|', sizeof user);
1449 newax = extract_int(v_args, 1);
1451 if (CtdlAccessCheck(ac_aide)) {
1455 if (lgetuser(&userbuf, user) != 0) {
1456 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1460 userbuf.axlevel = newax;
1461 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1465 /* If the access level was set to zero, delete the user */
1467 if (purge_user(user) == 0) {
1468 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1472 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1478 * Traverse the user file...
1480 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1483 struct ctdluser usbuf;
1484 struct cdbdata *cdbus;
1486 cdb_rewind(CDB_USERS);
1488 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1489 memset(&usbuf, 0, sizeof(struct ctdluser));
1490 memcpy(&usbuf, cdbus->ptr,
1491 ((cdbus->len > sizeof(struct ctdluser)) ?
1492 sizeof(struct ctdluser) : cdbus->len));
1494 (*CallBack) (&usbuf, in_data);
1500 * List one user (this works with cmd_list)
1502 void ListThisUser(struct ctdluser *usbuf, void *data)
1506 searchstring = (char *)data;
1507 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1511 if (usbuf->axlevel > 0) {
1512 if ((CC->user.axlevel >= 6)
1513 || ((usbuf->flags & US_UNLISTED) == 0)
1514 || ((CC->internal_pgm))) {
1515 cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1519 (long)usbuf->lastcall,
1522 if (CC->user.axlevel >= 6)
1523 cprintf("%s", usbuf->password);
1530 * List users (searchstring may be empty to list all users)
1532 void cmd_list(char *cmdbuf)
1534 char searchstring[256];
1535 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1536 striplt(searchstring);
1537 cprintf("%d \n", LISTING_FOLLOWS);
1538 ForEachUser(ListThisUser, (void *)searchstring );
1546 * assorted info we need to check at login
1554 if (CtdlAccessCheck(ac_logged_in)) {
1558 getuser(&CC->user, CC->curr_user); /* no lock is needed here */
1559 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1562 if (CC->user.axlevel >= 6) {
1564 if (CitControl.MMflags & MM_VALID)
1568 /* check for mail */
1569 mail = InitialMailCheck();
1571 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1576 * check to see if a user exists
1578 void cmd_qusr(char *who)
1580 struct ctdluser usbuf;
1582 if (getuser(&usbuf, who) == 0) {
1583 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1585 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1591 * Administrative Get User Parameters
1593 void cmd_agup(char *cmdbuf)
1595 struct ctdluser usbuf;
1596 char requested_user[128];
1598 if (CtdlAccessCheck(ac_aide)) {
1602 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1603 if (getuser(&usbuf, requested_user) != 0) {
1604 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1607 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1614 (int) usbuf.axlevel,
1616 (long)usbuf.lastcall,
1623 * Administrative Set User Parameters
1625 void cmd_asup(char *cmdbuf)
1627 struct ctdluser usbuf;
1628 char requested_user[128];
1634 if (CtdlAccessCheck(ac_aide))
1637 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1638 if (lgetuser(&usbuf, requested_user) != 0) {
1639 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1642 np = num_parms(cmdbuf);
1644 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1646 usbuf.flags = extract_int(cmdbuf, 2);
1648 usbuf.timescalled = extract_int(cmdbuf, 3);
1650 usbuf.posted = extract_int(cmdbuf, 4);
1652 newax = extract_int(cmdbuf, 5);
1653 if ((newax >= 0) && (newax <= 6)) {
1654 usbuf.axlevel = extract_int(cmdbuf, 5);
1658 usbuf.lastcall = extract_long(cmdbuf, 7);
1661 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1664 if (usbuf.axlevel == 0) {
1665 if (purge_user(requested_user) == 0) {
1671 snprintf(notify, SIZ,
1672 "User \"%s\" has been deleted by %s.\n",
1673 usbuf.fullname, CC->user.fullname);
1674 aide_message(notify, "User Deletion Message");
1677 cprintf("%d Ok", CIT_OK);
1679 cprintf(" (%s deleted)", requested_user);
1686 * Check to see if the user who we just sent mail to is logged in. If yes,
1687 * bump the 'new mail' counter for their session. That enables them to
1688 * receive a new mail notification without having to hit the database.
1690 void BumpNewMailCounter(long which_user) {
1691 struct CitContext *ptr;
1693 begin_critical_section(S_SESSION_TABLE);
1695 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1696 if (ptr->user.usernum == which_user) {
1701 end_critical_section(S_SESSION_TABLE);
1706 * Count the number of new mail messages the user has
1710 int num_newmsgs = 0;
1712 num_newmsgs = CC->newmail;
1715 return (num_newmsgs);
1720 * Count the number of new mail messages the user has
1722 int InitialMailCheck()
1724 int num_newmsgs = 0;
1726 char mailboxname[ROOMNAMELEN];
1727 struct ctdlroom mailbox;
1729 struct cdbdata *cdbfr;
1730 long *msglist = NULL;
1733 MailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1734 if (getroom(&mailbox, mailboxname) != 0)
1736 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1738 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1740 if (cdbfr != NULL) {
1741 msglist = malloc(cdbfr->len);
1742 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1743 num_msgs = cdbfr->len / sizeof(long);
1747 for (a = 0; a < num_msgs; ++a) {
1748 if (msglist[a] > 0L) {
1749 if (msglist[a] > vbuf.v_lastseen) {
1754 if (msglist != NULL)
1757 return (num_newmsgs);
1763 * Set the preferred view for the current user/room combination
1765 void cmd_view(char *cmdbuf) {
1769 if (CtdlAccessCheck(ac_logged_in)) {
1773 requested_view = extract_int(cmdbuf, 0);
1775 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1776 vbuf.v_view = requested_view;
1777 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1779 cprintf("%d ok\n", CIT_OK);