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>
41 #include "sysdep_decls.h"
49 #include "citserver.h"
50 #include "citadel_dirs.h"
53 /* These pipes are used to talk to the chkpwd daemon, which is forked during startup */
54 int chkpwd_write_pipe[2];
55 int chkpwd_read_pipe[2];
58 * makeuserkey() - convert a username into the format used as a database key
59 * (it's just the username converted into lower case)
61 static INLINE void makeuserkey(char *key, char *username) {
64 len = strlen(username);
65 if (len >= USERNAME_SIZE)
67 lprintf (CTDL_EMERG, "Username to long: %s", username);
69 len = USERNAME_SIZE - 1;
70 username[USERNAME_SIZE - 1]='\0';
72 for (i=0; i<=len; ++i) {
73 key[i] = tolower(username[i]);
79 * getuser() - retrieve named user into supplied buffer.
80 * returns 0 on success
82 int getuser(struct ctdluser *usbuf, char name[])
85 char usernamekey[USERNAME_SIZE];
86 struct cdbdata *cdbus;
89 memset(usbuf, 0, sizeof(struct ctdluser));
92 makeuserkey(usernamekey, name);
93 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
95 if (cdbus == NULL) { /* user not found */
99 memcpy(usbuf, cdbus->ptr,
100 ((cdbus->len > sizeof(struct ctdluser)) ?
101 sizeof(struct ctdluser) : cdbus->len));
110 * lgetuser() - same as getuser() but locks the record
112 int lgetuser(struct ctdluser *usbuf, char *name)
116 retcode = getuser(usbuf, name);
118 begin_critical_section(S_USERS);
125 * putuser() - write user buffer into the correct place on disk
127 void putuser(struct ctdluser *usbuf)
129 char usernamekey[USERNAME_SIZE];
131 makeuserkey(usernamekey, usbuf->fullname);
133 usbuf->version = REV_LEVEL;
135 usernamekey, strlen(usernamekey),
136 usbuf, sizeof(struct ctdluser));
142 * lputuser() - same as putuser() but locks the record
144 void lputuser(struct ctdluser *usbuf)
147 end_critical_section(S_USERS);
151 * Index-generating function used by Ctdl[Get|Set]Relationship
153 int GenerateRelationshipIndex(char *IndexBuf,
165 TheIndex.iRoomID = RoomID;
166 TheIndex.iRoomGen = RoomGen;
167 TheIndex.iUserID = UserID;
169 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
170 return (sizeof(TheIndex));
176 * Back end for CtdlSetRelationship()
178 void put_visit(struct visit *newvisit)
183 /* Generate an index */
184 IndexLen = GenerateRelationshipIndex(IndexBuf,
187 newvisit->v_usernum);
189 /* Store the record */
190 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
191 newvisit, sizeof(struct visit)
199 * Define a relationship between a user and a room
201 void CtdlSetRelationship(struct visit *newvisit,
202 struct ctdluser *rel_user,
203 struct ctdlroom *rel_room)
207 /* We don't use these in Citadel because they're implicit by the
208 * index, but they must be present if the database is exported.
210 newvisit->v_roomnum = rel_room->QRnumber;
211 newvisit->v_roomgen = rel_room->QRgen;
212 newvisit->v_usernum = rel_user->usernum;
218 * Locate a relationship between a user and a room
220 void CtdlGetRelationship(struct visit *vbuf,
221 struct ctdluser *rel_user,
222 struct ctdlroom *rel_room)
227 struct cdbdata *cdbvisit;
229 /* Generate an index */
230 IndexLen = GenerateRelationshipIndex(IndexBuf,
235 /* Clear out the buffer */
236 memset(vbuf, 0, sizeof(struct visit));
238 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
239 if (cdbvisit != NULL) {
240 memcpy(vbuf, cdbvisit->ptr,
241 ((cdbvisit->len > sizeof(struct visit)) ?
242 sizeof(struct visit) : cdbvisit->len));
246 /* If this is the first time the user has seen this room,
247 * set the view to be the default for the room.
249 vbuf->v_view = rel_room->QRdefaultview;
252 /* Set v_seen if necessary */
253 if (vbuf->v_seen[0] == 0) {
254 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
259 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
261 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
266 * Is the user currently logged in an Aide?
270 if (CC->user.axlevel >= 6)
278 * Is the user currently logged in an Aide *or* the room aide for this room?
280 int is_room_aide(void)
283 if (!CC->logged_in) {
287 if ((CC->user.axlevel >= 6)
288 || (CC->room.QRroomaide == CC->user.usernum)) {
296 * getuserbynumber() - get user by number
297 * returns 0 if user was found
299 * WARNING: don't use this function unless you absolutely have to. It does
300 * a sequential search and therefore is computationally expensive.
302 int getuserbynumber(struct ctdluser *usbuf, long int number)
304 struct cdbdata *cdbus;
306 cdb_rewind(CDB_USERS);
308 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
309 memset(usbuf, 0, sizeof(struct ctdluser));
310 memcpy(usbuf, cdbus->ptr,
311 ((cdbus->len > sizeof(struct ctdluser)) ?
312 sizeof(struct ctdluser) : cdbus->len));
314 if (usbuf->usernum == number) {
315 cdb_close_cursor(CDB_USERS);
324 * getuserbyuid() - get user by system uid (for PAM mode authentication)
325 * returns 0 if user was found
327 * WARNING: don't use this function unless you absolutely have to. It does
328 * a sequential search and therefore is computationally expensive.
330 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
332 struct cdbdata *cdbus;
334 cdb_rewind(CDB_USERS);
336 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
337 memset(usbuf, 0, sizeof(struct ctdluser));
338 memcpy(usbuf, cdbus->ptr,
339 ((cdbus->len > sizeof(struct ctdluser)) ?
340 sizeof(struct ctdluser) : cdbus->len));
342 if (usbuf->uid == number) {
343 cdb_close_cursor(CDB_USERS);
351 * Back end for cmd_user() and its ilk
353 * NOTE: "authname" should only be used if we are attempting to use the "master user" feature
355 int CtdlLoginExistingUser(char *authname, char *trythisname)
360 if ((CC->logged_in)) {
361 return login_already_logged_in;
364 if (trythisname == NULL) return login_not_found;
366 /* If a "master user" is defined, handle its authentication if specified */
368 if (strlen(config.c_master_user) > 0) if (strlen(config.c_master_pass) > 0) if (authname) {
369 if (!strcasecmp(authname, config.c_master_user)) {
374 /* Continue attempting user validation... */
375 safestrncpy(username, trythisname, USERNAME_SIZE);
378 if (IsEmptyStr(username)) {
379 return login_not_found;
382 if (config.c_auth_mode == 1) {
387 struct passwd *tempPwdPtr;
390 lprintf(CTDL_DEBUG, "asking host about <%s>\n", username);
391 #ifdef SOLARIS_GETPWUID
392 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
394 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
396 if (tempPwdPtr == NULL) {
397 return login_not_found;
399 lprintf(CTDL_DEBUG, "found it! uid=%ld, gecos=%s\n", (long)pd.pw_uid, pd.pw_gecos);
401 /* Locate the associated Citadel account.
402 * If not found, make one attempt to create it.
404 found_user = getuserbyuid(&CC->user, pd.pw_uid);
405 if (found_user != 0) {
406 create_user(username, 0);
407 found_user = getuserbyuid(&CC->user, pd.pw_uid);
413 /* native auth mode */
415 struct recptypes *valid = NULL;
417 /* First, try to log in as if the supplied name is a display name */
418 found_user = getuser(&CC->user, username);
420 /* If that didn't work, try to log in as if the supplied name
421 * is an e-mail address
423 if (found_user != 0) {
424 valid = validate_recipients(username);
426 if (valid->num_local == 1) {
427 found_user = getuser(&CC->user, valid->recp_local);
429 free_recipients(valid);
434 /* Did we find something? */
435 if (found_user == 0) {
436 if (((CC->nologin)) && (CC->user.axlevel < 6)) {
437 return login_too_many_users;
439 safestrncpy(CC->curr_user, CC->user.fullname,
440 sizeof CC->curr_user);
444 return login_not_found;
452 void cmd_user(char *cmdbuf)
457 extract_token(username, cmdbuf, 0, '|', sizeof username);
460 a = CtdlLoginExistingUser(NULL, username);
462 case login_already_logged_in:
463 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
465 case login_too_many_users:
467 "Too many users are already online "
469 ERROR + MAX_SESSIONS_EXCEEDED,
470 config.c_nodename, config.c_maxsessions);
473 cprintf("%d Password required for %s\n",
474 MORE_DATA, CC->curr_user);
476 case login_not_found:
477 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
480 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
487 * session startup code which is common to both cmd_pass() and cmd_newu()
489 void session_startup(void)
493 lprintf(CTDL_NOTICE, "<%s> logged in\n", CC->curr_user);
495 lgetuser(&CC->user, CC->curr_user);
496 ++(CC->user.timescalled);
497 CC->previous_login = CC->user.lastcall;
498 time(&CC->user.lastcall);
500 /* If this user's name is the name of the system administrator
501 * (as specified in setup), automatically assign access level 6.
503 if (!strcasecmp(CC->user.fullname, config.c_sysadm)) {
504 CC->user.axlevel = 6;
507 /* If we're authenticating off the host system, automatically give
508 * root the highest level of access.
510 if (config.c_auth_mode == 1) {
511 if (CC->user.uid == 0) {
512 CC->user.axlevel = 6;
519 * Populate CC->cs_inet_email with a default address. This will be
520 * overwritten with the user's directory address, if one exists, when
521 * the vCard module's login hook runs.
523 snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
524 CC->user.fullname, config.c_fqdn);
525 for (i=0; !IsEmptyStr(&CC->cs_inet_email[i]); ++i) {
526 if (isspace(CC->cs_inet_email[i])) {
527 CC->cs_inet_email[i] = '_';
531 /* Create any personal rooms required by the system.
532 * (Technically, MAILROOM should be there already, but just in case...)
534 create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
535 create_room(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
536 create_room(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
538 /* Run any startup routines registered by loadable modules */
539 PerformSessionHooks(EVT_LOGIN);
541 /* Enter the lobby */
542 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
546 void logged_in_response(void)
548 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
549 CIT_OK, CC->user.fullname, CC->user.axlevel,
550 CC->user.timescalled, CC->user.posted,
551 CC->user.flags, CC->user.usernum,
558 * misc things to be taken care of when a user is logged out
560 void logout(struct CitContext *who)
563 * Clear out some session data. Most likely, the CitContext for this
564 * session is about to get nuked when the session disconnects, but
565 * since it's possible to log in again without reconnecting, we cannot
566 * make that assumption.
568 strcpy(who->fake_username, "");
569 strcpy(who->fake_hostname, "");
570 strcpy(who->fake_roomname, "");
574 * If there is a download in progress, abort it.
576 if (who->download_fp != NULL) {
577 fclose(who->download_fp);
578 who->download_fp = NULL;
582 * If there is an upload in progress, abort it.
584 if (who->upload_fp != NULL) {
589 * If we were talking to a network node, we're not anymore...
591 if (!IsEmptyStr(who->net_node)) {
592 network_talking_to(who->net_node, NTT_REMOVE);
595 /* Do modular stuff... */
596 PerformSessionHooks(EVT_LOGOUT);
598 /* Free any output buffers */
599 if (who->output_buffer != NULL) {
605 * Validate a password on the host unix system by talking to the chkpwd daemon
607 static int validpw(uid_t uid, const char *pass)
611 lprintf(CTDL_DEBUG, "Validating password for uid=%d using chkpwd...\n", uid);
613 begin_critical_section(S_CHKPWD);
614 write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
615 write(chkpwd_write_pipe[1], pass, 256);
616 read(chkpwd_read_pipe[0], buf, 4);
617 end_critical_section(S_CHKPWD);
619 if (!strncmp(buf, "PASS", 4)) {
620 lprintf(CTDL_DEBUG, "...pass\n");
624 lprintf(CTDL_DEBUG, "...fail\n");
629 * Start up the chkpwd daemon so validpw() has something to talk to
631 void start_chkpwd_daemon(void) {
633 struct stat filestats;
636 lprintf(CTDL_DEBUG, "Starting chkpwd daemon for host authentication mode\n");
638 if ((stat(file_chkpwd, &filestats)==-1) ||
639 (filestats.st_size==0)){
640 printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
643 if (pipe(chkpwd_write_pipe) != 0) {
644 lprintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
647 if (pipe(chkpwd_read_pipe) != 0) {
648 lprintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
653 if (chkpwd_pid < 0) {
654 lprintf(CTDL_EMERG, "Unable to fork chkpwd daemon: %s\n", strerror(errno));
657 if (chkpwd_pid == 0) {
658 lprintf(CTDL_DEBUG, "Now calling dup2() write\n");
659 dup2(chkpwd_write_pipe[0], 0);
660 lprintf(CTDL_DEBUG, "Now calling dup2() write\n");
661 dup2(chkpwd_read_pipe[1], 1);
662 lprintf(CTDL_DEBUG, "Now closing stuff\n");
663 for (i=2; i<256; ++i) close(i);
664 lprintf(CTDL_DEBUG, "Now calling execl(%s)\n", file_chkpwd);
665 execl(file_chkpwd, file_chkpwd, NULL);
666 lprintf(CTDL_EMERG, "Unable to exec chkpwd daemon: %s\n", strerror(errno));
680 int CtdlTryPassword(char *password)
684 if ((CC->logged_in)) {
685 lprintf(CTDL_WARNING, "CtdlTryPassword: already logged in\n");
686 return pass_already_logged_in;
688 if (!strcmp(CC->curr_user, NLI)) {
689 lprintf(CTDL_WARNING, "CtdlTryPassword: no user selected\n");
692 if (getuser(&CC->user, CC->curr_user)) {
693 lprintf(CTDL_ERR, "CtdlTryPassword: internal error\n");
694 return pass_internal_error;
696 if (password == NULL) {
697 lprintf(CTDL_INFO, "CtdlTryPassword: NULL password string supplied\n");
698 return pass_wrong_password;
703 code = strcmp(password, config.c_master_pass);
706 else if (config.c_auth_mode == 1) {
710 if (validpw(CC->user.uid, password)) {
714 * sooper-seekrit hack: populate the password field in the
715 * citadel database with the password that the user typed,
716 * if it's correct. This allows most sites to convert from
717 * host auth to native auth if they want to. If you think
718 * this is a security hazard, comment it out.
721 lgetuser(&CC->user, CC->curr_user);
722 safestrncpy(CC->user.password, password, sizeof CC->user.password);
726 * (sooper-seekrit hack ends here)
737 /* native auth mode */
740 strproc(CC->user.password);
741 code = strcasecmp(CC->user.password, password);
743 strproc(CC->user.password);
744 code = strcasecmp(CC->user.password, password);
751 lprintf(CTDL_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
752 return pass_wrong_password;
757 void cmd_pass(char *buf)
762 extract_token(password, buf, 0, '|', sizeof password);
763 a = CtdlTryPassword(password);
766 case pass_already_logged_in:
767 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
770 cprintf("%d You must send a name with USER first.\n",
771 ERROR + USERNAME_REQUIRED);
773 case pass_wrong_password:
774 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
777 logged_in_response();
785 * Delete a user record *and* all of its related resources.
787 int purge_user(char pname[])
790 struct ctdluser usbuf;
791 char usernamekey[USERNAME_SIZE];
792 struct CitContext *ccptr;
793 int user_is_logged_in = 0;
795 makeuserkey(usernamekey, pname);
797 if (getuser(&usbuf, pname) != 0) {
798 lprintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
799 return (ERROR + NO_SUCH_USER);
801 /* Don't delete a user who is currently logged in. Instead, just
802 * set the access level to 0, and let the account get swept up
803 * during the next purge.
805 user_is_logged_in = 0;
806 begin_critical_section(S_SESSION_TABLE);
807 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
808 if (ccptr->user.usernum == usbuf.usernum) {
809 user_is_logged_in = 1;
812 end_critical_section(S_SESSION_TABLE);
813 if (user_is_logged_in == 1) {
814 lprintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
819 lprintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
821 /* Perform any purge functions registered by server extensions */
822 PerformUserHooks(&usbuf, EVT_PURGEUSER);
824 /* delete any existing user/room relationships */
825 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
827 /* delete the userlog entry */
828 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
830 /* remove the user's bio file */
838 /* remove the user's picture */
851 * create_user() - back end processing to create a new user
853 * Set 'newusername' to the desired account name.
854 * Set 'become_user' to nonzero if this is self-service account creation and we want
855 * to actually log in as the user we just created, otherwise set it to 0.
857 int create_user(char *newusername, int become_user)
859 struct ctdluser usbuf;
860 struct ctdlroom qrbuf;
862 char mailboxname[ROOMNAMELEN];
866 safestrncpy(username, newusername, sizeof username);
869 if (config.c_auth_mode == 1) {
874 struct passwd *tempPwdPtr;
877 #ifdef SOLARIS_GETPWUID
878 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof(pwdbuffer));
880 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
882 if (tempPwdPtr != NULL) {
883 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
887 return (ERROR + NO_SUCH_USER);
891 if (!getuser(&usbuf, username)) {
892 return (ERROR + ALREADY_EXISTS);
895 /* Go ahead and initialize a new user record */
896 memset(&usbuf, 0, sizeof(struct ctdluser));
897 safestrncpy(usbuf.fullname, username, sizeof usbuf.fullname);
898 strcpy(usbuf.password, "");
901 /* These are the default flags on new accounts */
902 usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
904 usbuf.timescalled = 0;
906 usbuf.axlevel = config.c_initax;
907 usbuf.USscreenwidth = 80;
908 usbuf.USscreenheight = 24;
909 usbuf.lastcall = time(NULL);
911 /* fetch a new user number */
912 usbuf.usernum = get_new_user_number();
914 /* The very first user created on the system will always be an Aide */
915 if (usbuf.usernum == 1L) {
919 /* add user to userlog */
923 * Give the user a private mailbox and a configuration room.
924 * Make the latter an invisible system room.
926 MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
927 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
929 MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
930 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
931 if (lgetroom(&qrbuf, mailboxname) == 0) {
932 qrbuf.QRflags2 |= QR2_SYSTEM;
936 /* Perform any create functions registered by server extensions */
937 PerformUserHooks(&usbuf, EVT_NEWUSER);
939 /* Everything below this line can be bypassed if administratively
940 * creating a user, instead of doing self-service account creation
944 /* Now become the user we just created */
945 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
946 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
949 /* Check to make sure we're still who we think we are */
950 if (getuser(&CC->user, CC->curr_user)) {
951 return (ERROR + INTERNAL_ERROR);
956 "New user account <%s> has been created, from host %s [%s].\n",
961 aide_message(buf, "User Creation Notice");
962 lprintf(CTDL_NOTICE, "New user <%s> created\n", username);
970 * cmd_newu() - create a new user account and log in as that user
972 void cmd_newu(char *cmdbuf)
977 if (config.c_auth_mode == 1) {
978 cprintf("%d This system does not use native mode authentication.\n",
983 if (config.c_disable_newu) {
984 cprintf("%d Self-service user account creation "
985 "is disabled on this system.\n", ERROR + NOT_HERE);
990 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
994 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
995 ERROR + MAX_SESSIONS_EXCEEDED,
996 config.c_nodename, config.c_maxsessions);
998 extract_token(username, cmdbuf, 0, '|', sizeof username);
1002 if (IsEmptyStr(username)) {
1003 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1007 if ((!strcasecmp(username, "bbs")) ||
1008 (!strcasecmp(username, "new")) ||
1009 (!strcasecmp(username, "."))) {
1010 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
1014 a = create_user(username, 1);
1018 logged_in_response();
1019 } else if (a == ERROR + ALREADY_EXISTS) {
1020 cprintf("%d '%s' already exists.\n",
1021 ERROR + ALREADY_EXISTS, username);
1023 } else if (a == ERROR + INTERNAL_ERROR) {
1024 cprintf("%d Internal error - user record disappeared?\n",
1025 ERROR + INTERNAL_ERROR);
1028 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
1037 void cmd_setp(char *new_pw)
1039 if (CtdlAccessCheck(ac_logged_in)) {
1042 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
1043 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
1046 if (CC->is_master) {
1047 cprintf("%d The master prefix password cannot be changed with this command.\n",
1052 if (IsEmptyStr(new_pw)) {
1053 cprintf("%d Password unchanged.\n", CIT_OK);
1056 lgetuser(&CC->user, CC->curr_user);
1057 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1058 lputuser(&CC->user);
1059 cprintf("%d Password changed.\n", CIT_OK);
1060 lprintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
1061 PerformSessionHooks(EVT_SETPASS);
1066 * cmd_creu() - administratively create a new user account (do not log in to it)
1068 void cmd_creu(char *cmdbuf)
1073 struct ctdluser tmp;
1075 if (CtdlAccessCheck(ac_aide)) {
1079 extract_token(username, cmdbuf, 0, '|', sizeof username);
1080 extract_token(password, cmdbuf, 1, '|', sizeof password);
1086 if (IsEmptyStr(username)) {
1087 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1091 a = create_user(username, 0);
1094 if (!IsEmptyStr(password)) {
1095 lgetuser(&tmp, username);
1096 safestrncpy(tmp.password, password, sizeof(tmp.password));
1099 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1100 (!IsEmptyStr(password)) ? "and password set" :
1101 "with no password");
1103 } else if (a == ERROR + ALREADY_EXISTS) {
1104 cprintf("%d '%s' already exists.\n",
1105 ERROR + ALREADY_EXISTS, username);
1108 cprintf("%d An error occured creating the user account.\n", ERROR + INTERNAL_ERROR);
1115 * get user parameters
1120 if (CtdlAccessCheck(ac_logged_in))
1123 getuser(&CC->user, CC->curr_user);
1124 cprintf("%d %d|%d|%d|\n",
1126 CC->user.USscreenwidth,
1127 CC->user.USscreenheight,
1128 (CC->user.flags & US_USER_SET)
1133 * set user parameters
1135 void cmd_setu(char *new_parms)
1137 if (CtdlAccessCheck(ac_logged_in))
1140 if (num_parms(new_parms) < 3) {
1141 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1144 lgetuser(&CC->user, CC->curr_user);
1145 CC->user.USscreenwidth = extract_int(new_parms, 0);
1146 CC->user.USscreenheight = extract_int(new_parms, 1);
1147 CC->user.flags = CC->user.flags & (~US_USER_SET);
1148 CC->user.flags = CC->user.flags |
1149 (extract_int(new_parms, 2) & US_USER_SET);
1151 lputuser(&CC->user);
1152 cprintf("%d Ok\n", CIT_OK);
1156 * set last read pointer
1158 void cmd_slrp(char *new_ptr)
1162 struct visit original_vbuf;
1164 if (CtdlAccessCheck(ac_logged_in)) {
1168 if (!strncasecmp(new_ptr, "highest", 7)) {
1169 newlr = CC->room.QRhighest;
1171 newlr = atol(new_ptr);
1174 lgetuser(&CC->user, CC->curr_user);
1176 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1177 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1178 vbuf.v_lastseen = newlr;
1179 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1181 /* Only rewrite the record if it changed */
1182 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1183 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1184 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1187 lputuser(&CC->user);
1188 cprintf("%d %ld\n", CIT_OK, newlr);
1192 void cmd_seen(char *argbuf) {
1193 long target_msgnum = 0L;
1194 int target_setting = 0;
1196 if (CtdlAccessCheck(ac_logged_in)) {
1200 if (num_parms(argbuf) != 2) {
1201 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1205 target_msgnum = extract_long(argbuf, 0);
1206 target_setting = extract_int(argbuf, 1);
1208 CtdlSetSeen(&target_msgnum, 1, target_setting,
1209 ctdlsetseen_seen, NULL, NULL);
1210 cprintf("%d OK\n", CIT_OK);
1214 void cmd_gtsn(char *argbuf) {
1217 if (CtdlAccessCheck(ac_logged_in)) {
1221 CtdlGetSeen(buf, ctdlsetseen_seen);
1222 cprintf("%d %s\n", CIT_OK, buf);
1227 * API function for cmd_invt_kick() and anything else that needs to
1228 * invite or kick out a user to/from a room.
1230 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1232 int CtdlInvtKick(char *iuser, int op) {
1233 struct ctdluser USscratch;
1237 if (getuser(&USscratch, iuser) != 0) {
1241 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1243 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1244 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1247 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1248 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1250 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1252 /* post a message in Aide> saying what we just did */
1253 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1255 ((op == 1) ? "invited to" : "kicked out of"),
1258 aide_message(bbb,"User Admin Message");
1265 * INVT and KICK commands
1267 void cmd_invt_kick(char *iuser, int op) {
1270 * These commands are only allowed by aides, room aides,
1271 * and room namespace owners
1274 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1275 /* access granted */
1278 cprintf("%d Higher access or room ownership required.\n",
1279 ERROR + HIGHER_ACCESS_REQUIRED);
1283 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1285 cprintf("%d Can't add/remove users from this room.\n",
1290 if (CtdlInvtKick(iuser, op) != 0) {
1291 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1295 cprintf("%d %s %s %s.\n",
1297 ((op == 1) ? "invited to" : "kicked out of"),
1304 * Forget (Zap) the current room (API call)
1305 * Returns 0 on success
1307 int CtdlForgetThisRoom(void) {
1310 /* On some systems, Aides are not allowed to forget rooms */
1311 if (is_aide() && (config.c_aide_zap == 0)
1312 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1316 lgetuser(&CC->user, CC->curr_user);
1317 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1319 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1320 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1322 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1323 lputuser(&CC->user);
1325 /* Return to the Lobby, so we don't end up in an undefined room */
1326 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1333 * forget (Zap) the current room
1338 if (CtdlAccessCheck(ac_logged_in)) {
1342 if (CtdlForgetThisRoom() == 0) {
1343 cprintf("%d Ok\n", CIT_OK);
1346 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1351 * Get Next Unregistered User
1355 struct cdbdata *cdbus;
1356 struct ctdluser usbuf;
1358 if (CtdlAccessCheck(ac_aide)) {
1362 if ((CitControl.MMflags & MM_VALID) == 0) {
1363 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1367 /* There are unvalidated users. Traverse the user database,
1368 * and return the first user we find that needs validation.
1370 cdb_rewind(CDB_USERS);
1371 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1372 memset(&usbuf, 0, sizeof(struct ctdluser));
1373 memcpy(&usbuf, cdbus->ptr,
1374 ((cdbus->len > sizeof(struct ctdluser)) ?
1375 sizeof(struct ctdluser) : cdbus->len));
1377 if ((usbuf.flags & US_NEEDVALID)
1378 && (usbuf.axlevel > 0)) {
1379 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1380 cdb_close_cursor(CDB_USERS);
1385 /* If we get to this point, there are no more unvalidated users.
1386 * Therefore we clear the "users need validation" flag.
1389 begin_critical_section(S_CONTROL);
1391 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1393 end_critical_section(S_CONTROL);
1394 cprintf("%d *** End of registration.\n", CIT_OK);
1403 void cmd_vali(char *v_args)
1407 struct ctdluser userbuf;
1409 extract_token(user, v_args, 0, '|', sizeof user);
1410 newax = extract_int(v_args, 1);
1412 if (CtdlAccessCheck(ac_aide)) {
1416 if (lgetuser(&userbuf, user) != 0) {
1417 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1421 userbuf.axlevel = newax;
1422 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1426 /* If the access level was set to zero, delete the user */
1428 if (purge_user(user) == 0) {
1429 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1433 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1439 * Traverse the user file...
1441 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1444 struct ctdluser usbuf;
1445 struct cdbdata *cdbus;
1447 cdb_rewind(CDB_USERS);
1449 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1450 memset(&usbuf, 0, sizeof(struct ctdluser));
1451 memcpy(&usbuf, cdbus->ptr,
1452 ((cdbus->len > sizeof(struct ctdluser)) ?
1453 sizeof(struct ctdluser) : cdbus->len));
1455 (*CallBack) (&usbuf, in_data);
1461 * List one user (this works with cmd_list)
1463 void ListThisUser(struct ctdluser *usbuf, void *data)
1467 searchstring = (char *)data;
1468 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1472 if (usbuf->axlevel > 0) {
1473 if ((CC->user.axlevel >= 6)
1474 || ((usbuf->flags & US_UNLISTED) == 0)
1475 || ((CC->internal_pgm))) {
1476 cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1480 (long)usbuf->lastcall,
1483 if (CC->user.axlevel >= 6)
1484 cprintf("%s", usbuf->password);
1491 * List users (searchstring may be empty to list all users)
1493 void cmd_list(char *cmdbuf)
1495 char searchstring[256];
1496 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1497 striplt(searchstring);
1498 cprintf("%d \n", LISTING_FOLLOWS);
1499 ForEachUser(ListThisUser, (void *)searchstring );
1507 * assorted info we need to check at login
1515 if (CtdlAccessCheck(ac_logged_in)) {
1519 getuser(&CC->user, CC->curr_user); /* no lock is needed here */
1520 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1523 if (CC->user.axlevel >= 6) {
1525 if (CitControl.MMflags & MM_VALID)
1529 /* check for mail */
1530 mail = InitialMailCheck();
1532 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1537 * check to see if a user exists
1539 void cmd_qusr(char *who)
1541 struct ctdluser usbuf;
1543 if (getuser(&usbuf, who) == 0) {
1544 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1546 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1552 * Administrative Get User Parameters
1554 void cmd_agup(char *cmdbuf)
1556 struct ctdluser usbuf;
1557 char requested_user[128];
1559 if (CtdlAccessCheck(ac_aide)) {
1563 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1564 if (getuser(&usbuf, requested_user) != 0) {
1565 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1568 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1575 (int) usbuf.axlevel,
1577 (long)usbuf.lastcall,
1584 * Administrative Set User Parameters
1586 void cmd_asup(char *cmdbuf)
1588 struct ctdluser usbuf;
1589 char requested_user[128];
1595 if (CtdlAccessCheck(ac_aide))
1598 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1599 if (lgetuser(&usbuf, requested_user) != 0) {
1600 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1603 np = num_parms(cmdbuf);
1605 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1607 usbuf.flags = extract_int(cmdbuf, 2);
1609 usbuf.timescalled = extract_int(cmdbuf, 3);
1611 usbuf.posted = extract_int(cmdbuf, 4);
1613 newax = extract_int(cmdbuf, 5);
1614 if ((newax >= 0) && (newax <= 6)) {
1615 usbuf.axlevel = extract_int(cmdbuf, 5);
1619 usbuf.lastcall = extract_long(cmdbuf, 7);
1622 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1625 if (usbuf.axlevel == 0) {
1626 if (purge_user(requested_user) == 0) {
1632 sprintf(notify, "User \"%s\" has been deleted by %s.\n",
1633 usbuf.fullname, CC->user.fullname);
1634 aide_message(notify, "User Deletion Message");
1637 cprintf("%d Ok", CIT_OK);
1639 cprintf(" (%s deleted)", requested_user);
1646 * Check to see if the user who we just sent mail to is logged in. If yes,
1647 * bump the 'new mail' counter for their session. That enables them to
1648 * receive a new mail notification without having to hit the database.
1650 void BumpNewMailCounter(long which_user) {
1651 struct CitContext *ptr;
1653 begin_critical_section(S_SESSION_TABLE);
1655 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1656 if (ptr->user.usernum == which_user) {
1661 end_critical_section(S_SESSION_TABLE);
1666 * Count the number of new mail messages the user has
1670 int num_newmsgs = 0;
1672 num_newmsgs = CC->newmail;
1675 return (num_newmsgs);
1680 * Count the number of new mail messages the user has
1682 int InitialMailCheck()
1684 int num_newmsgs = 0;
1686 char mailboxname[ROOMNAMELEN];
1687 struct ctdlroom mailbox;
1689 struct cdbdata *cdbfr;
1690 long *msglist = NULL;
1693 MailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1694 if (getroom(&mailbox, mailboxname) != 0)
1696 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1698 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1700 if (cdbfr != NULL) {
1701 msglist = malloc(cdbfr->len);
1702 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1703 num_msgs = cdbfr->len / sizeof(long);
1707 for (a = 0; a < num_msgs; ++a) {
1708 if (msglist[a] > 0L) {
1709 if (msglist[a] > vbuf.v_lastseen) {
1714 if (msglist != NULL)
1717 return (num_newmsgs);
1723 * Set the preferred view for the current user/room combination
1725 void cmd_view(char *cmdbuf) {
1729 if (CtdlAccessCheck(ac_logged_in)) {
1733 requested_view = extract_int(cmdbuf, 0);
1735 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1736 vbuf.v_view = requested_view;
1737 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1739 cprintf("%d ok\n", CIT_OK);