4 * Server functions which perform operations on user objects.
21 #include <sys/types.h>
24 #if TIME_WITH_SYS_TIME
25 # include <sys/time.h>
29 # include <sys/time.h>
44 #include "serv_extensions.h"
45 #include "sysdep_decls.h"
53 #include "citserver.h"
57 * makeuserkey() - convert a username into the format used as a database key
58 * (it's just the username converted into lower case)
60 static INLINE void makeuserkey(char *key, char *username) {
63 len = strlen(username);
64 for (i=0; i<=len; ++i) {
65 key[i] = tolower(username[i]);
71 * getuser() - retrieve named user into supplied buffer.
72 * returns 0 on success
74 int getuser(struct ctdluser *usbuf, char name[])
77 char usernamekey[USERNAME_SIZE];
78 char sysuser_name[USERNAME_SIZE];
79 struct cdbdata *cdbus;
80 int using_sysuser = 0;
82 memset(usbuf, 0, sizeof(struct ctdluser));
84 #ifdef ENABLE_AUTOLOGIN
85 if (CtdlAssociateSystemUser(sysuser_name, name) == 0) {
91 makeuserkey(usernamekey, sysuser_name);
94 makeuserkey(usernamekey, name);
97 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
98 if (cdbus == NULL) { /* user not found */
101 memcpy(usbuf, cdbus->ptr,
102 ((cdbus->len > sizeof(struct ctdluser)) ?
103 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 /* Generate an index */
185 IndexLen = GenerateRelationshipIndex(IndexBuf,
188 newvisit->v_usernum);
190 /* Store the record */
191 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
192 newvisit, sizeof(struct visit)
200 * Define a relationship between a user and a room
202 void CtdlSetRelationship(struct visit *newvisit,
203 struct ctdluser *rel_user,
204 struct ctdlroom *rel_room)
208 /* We don't use these in Citadel because they're implicit by the
209 * index, but they must be present if the database is exported.
211 newvisit->v_roomnum = rel_room->QRnumber;
212 newvisit->v_roomgen = rel_room->QRgen;
213 newvisit->v_usernum = rel_user->usernum;
219 * Locate a relationship between a user and a room
221 void CtdlGetRelationship(struct visit *vbuf,
222 struct ctdluser *rel_user,
223 struct ctdlroom *rel_room)
228 struct cdbdata *cdbvisit;
230 /* Generate an index */
231 IndexLen = GenerateRelationshipIndex(IndexBuf,
236 /* Clear out the buffer */
237 memset(vbuf, 0, sizeof(struct visit));
239 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
240 if (cdbvisit != NULL) {
241 memcpy(vbuf, cdbvisit->ptr,
242 ((cdbvisit->len > sizeof(struct visit)) ?
243 sizeof(struct visit) : cdbvisit->len));
247 /* If this is the first time the user has seen this room,
248 * set the view to be the default for the room.
250 vbuf->v_view = rel_room->QRdefaultview;
253 /* Set v_seen if necessary */
254 if (vbuf->v_seen[0] == 0) {
255 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
260 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
262 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
267 * Is the user currently logged in an Aide?
271 if (CC->user.axlevel >= 6)
279 * Is the user currently logged in an Aide *or* the room aide for this room?
281 int is_room_aide(void)
284 if (!CC->logged_in) {
288 if ((CC->user.axlevel >= 6)
289 || (CC->room.QRroomaide == CC->user.usernum)) {
297 * getuserbynumber() - get user by number
298 * returns 0 if user was found
300 * WARNING: don't use this function unless you absolutely have to. It does
301 * a sequential search and therefore is computationally expensive.
303 int getuserbynumber(struct ctdluser *usbuf, long int number)
305 struct cdbdata *cdbus;
307 cdb_rewind(CDB_USERS);
309 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
310 memset(usbuf, 0, sizeof(struct ctdluser));
311 memcpy(usbuf, cdbus->ptr,
312 ((cdbus->len > sizeof(struct ctdluser)) ?
313 sizeof(struct ctdluser) : cdbus->len));
315 if (usbuf->usernum == number) {
316 cdb_close_cursor(CDB_USERS);
325 * See if we can translate a system login name (i.e. from /etc/passwd)
326 * to a Citadel screen name. Returns 0 if one is found.
328 int CtdlAssociateSystemUser(char *screenname, char *loginname) {
332 p = (struct passwd *) getpwnam(loginname);
334 strcpy(screenname, p->pw_gecos);
335 for (a = 0; a < strlen(screenname); ++a) {
336 if (screenname[a] == ',') {
348 * Back end for cmd_user() and its ilk
350 int CtdlLoginExistingUser(char *trythisname)
354 struct recptypes *valid = NULL;
355 struct passwd *p = NULL;
357 if (trythisname == NULL) return login_not_found;
358 safestrncpy(username, trythisname, sizeof username);
361 if ((CC->logged_in)) {
362 return login_already_logged_in;
365 /* First, try to log in as if the supplied name is a display name */
366 found_user = getuser(&CC->user, username);
368 /* If that didn't work, try to log in as if the supplied name
369 * is an e-mail address
371 if (found_user != 0) {
372 valid = validate_recipients(trythisname);
374 if (valid->num_local == 1) {
375 found_user = getuser(&CC->user,
382 #ifdef ENABLE_AUTOLOGIN
383 /* If we haven't found the account yet, and the supplied name
384 * is a login name on the underlying host system, create the
387 if (found_user != 0) {
388 p = (struct passwd *) getpwnam(username);
390 create_user(username, 0);
391 found_user = getuser(&CC->user, username);
394 #endif /* ENABLE_AUTOLOGIN */
396 /* Did we find something? */
397 if (found_user == 0) {
398 if (((CC->nologin)) && (CC->user.axlevel < 6)) {
399 return login_too_many_users;
401 safestrncpy(CC->curr_user, CC->user.fullname,
402 sizeof CC->curr_user);
406 return login_not_found;
414 void cmd_user(char *cmdbuf)
419 extract(username, cmdbuf, 0);
422 a = CtdlLoginExistingUser(username);
424 case login_already_logged_in:
425 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
427 case login_too_many_users:
429 "Too many users are already online "
431 ERROR + MAX_SESSIONS_EXCEEDED,
432 config.c_nodename, config.c_maxsessions);
435 cprintf("%d Password required for %s\n",
436 MORE_DATA, CC->curr_user);
438 case login_not_found:
439 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
442 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
449 * session startup code which is common to both cmd_pass() and cmd_newu()
451 void session_startup(void)
455 lprintf(CTDL_NOTICE, "<%s> logged in\n", CC->curr_user);
457 lgetuser(&CC->user, CC->curr_user);
458 ++(CC->user.timescalled);
459 CC->previous_login = CC->user.lastcall;
460 time(&CC->user.lastcall);
462 /* If this user's name is the name of the system administrator
463 * (as specified in setup), automatically assign access level 6.
465 if (!strcasecmp(CC->user.fullname, config.c_sysadm)) {
466 CC->user.axlevel = 6;
471 * Populate CC->cs_inet_email with a default address. This will be
472 * overwritten with the user's directory address, if one exists, when
473 * the vCard module's login hook runs.
475 snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
476 CC->user.fullname, config.c_fqdn);
477 for (i=0; i<strlen(CC->cs_inet_email); ++i) {
478 if (isspace(CC->cs_inet_email[i])) {
479 CC->cs_inet_email[i] = '_';
483 /* Create any personal rooms required by the system.
484 * (Technically, MAILROOM should be there already, but just in case...)
486 create_room(MAILROOM, 4, "", 0, 1, 0);
487 create_room(SENTITEMS, 4, "", 0, 1, 0);
489 /* Run any startup routines registered by loadable modules */
490 PerformSessionHooks(EVT_LOGIN);
492 /* Enter the lobby */
493 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
497 void logged_in_response(void)
499 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
500 CIT_OK, CC->user.fullname, CC->user.axlevel,
501 CC->user.timescalled, CC->user.posted,
502 CC->user.flags, CC->user.usernum,
509 * misc things to be taken care of when a user is logged out
511 void logout(struct CitContext *who)
514 * Clear out some session data. Most likely, the CitContext for this
515 * session is about to get nuked when the session disconnects, but
516 * since it's possible to log in again without reconnecting, we cannot
517 * make that assumption.
519 strcpy(who->fake_username, "");
520 strcpy(who->fake_postname, "");
521 strcpy(who->fake_hostname, "");
522 strcpy(who->fake_roomname, "");
526 * If there is a download in progress, abort it.
528 if (who->download_fp != NULL) {
529 fclose(who->download_fp);
530 who->download_fp = NULL;
534 * If there is an upload in progress, abort it.
536 if (who->upload_fp != NULL) {
541 * If we were talking to a network node, we're not anymore...
543 if (strlen(who->net_node) > 0) {
544 network_talking_to(who->net_node, NTT_REMOVE);
547 /* Do modular stuff... */
548 PerformSessionHooks(EVT_LOGOUT);
550 /* Free any output buffers */
551 if (who->output_buffer != NULL) {
558 * an alternate version of validpw() which executes `chkpwd' instead of
559 * verifying the password directly
561 static int validpw(uid_t uid, const char *pass)
564 int status, pipev[2];
568 lprintf(CTDL_ERR, "pipe failed (%s): denying autologin access for "
569 "uid %ld\n", strerror(errno), (long)uid);
572 switch (pid = fork()) {
574 lprintf(CTDL_ERR, "fork failed (%s): denying autologin access for "
575 "uid %ld\n", strerror(errno), (long)uid);
582 if (dup2(pipev[0], 0) == -1) {
588 execl(BBSDIR "/chkpwd", BBSDIR "/chkpwd", NULL);
589 perror(BBSDIR "/chkpwd");
595 snprintf(buf, sizeof buf, "%lu\n", (unsigned long) uid));
596 write(pipev[1], pass, strlen(pass));
597 write(pipev[1], "\n", 1);
600 while (waitpid(pid, &status, 0) == -1)
601 if (errno != EINTR) {
602 lprintf(CTDL_ERR, "waitpid failed (%s): denying autologin "
603 "access for uid %ld\n",
604 strerror(errno), (long)uid);
607 if (WIFEXITED(status) && !WEXITSTATUS(status))
621 int CtdlTryPassword(char *password)
625 if ((CC->logged_in)) {
626 lprintf(CTDL_WARNING, "CtdlTryPassword: already logged in\n");
627 return pass_already_logged_in;
629 if (!strcmp(CC->curr_user, NLI)) {
630 lprintf(CTDL_WARNING, "CtdlTryPassword: no user selected\n");
633 if (getuser(&CC->user, CC->curr_user)) {
634 lprintf(CTDL_ERR, "CtdlTryPassword: internal error\n");
635 return pass_internal_error;
637 if (password == NULL) {
638 lprintf(CTDL_INFO, "CtdlTryPassword: NULL password string supplied\n");
639 return pass_wrong_password;
644 #ifdef ENABLE_AUTOLOGIN
645 /* A uid of BBSUID or -1 indicates that this user exists only in
646 * Citadel, not in the underlying operating system.
648 if ( (CC->user.uid == BBSUID) || (CC->user.uid == (-1)) ) {
650 strproc(CC->user.password);
651 code = strcasecmp(CC->user.password, password);
653 /* Any other uid means we have to check the system password database */
655 if (validpw(CC->user.uid, password)) {
657 lgetuser(&CC->user, CC->curr_user);
658 safestrncpy(CC->user.password, password,
659 sizeof CC->user.password);
664 #else /* ENABLE_AUTOLOGIN */
666 strproc(CC->user.password);
667 code = strcasecmp(CC->user.password, password);
669 #endif /* ENABLE_AUTOLOGIN */
675 lprintf(CTDL_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
676 return pass_wrong_password;
681 void cmd_pass(char *buf)
686 extract(password, buf, 0);
687 a = CtdlTryPassword(password);
690 case pass_already_logged_in:
691 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
694 cprintf("%d You must send a name with USER first.\n",
695 ERROR + USERNAME_REQUIRED);
697 case pass_wrong_password:
698 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
701 logged_in_response();
703 cprintf("%d Can't find user record!\n",
704 ERROR + INTERNAL_ERROR);
711 * Delete a user record *and* all of its related resources.
713 int purge_user(char pname[])
716 struct ctdluser usbuf;
717 char usernamekey[USERNAME_SIZE];
718 struct CitContext *ccptr;
719 int user_is_logged_in = 0;
721 makeuserkey(usernamekey, pname);
723 if (getuser(&usbuf, pname) != 0) {
724 lprintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
725 return (ERROR + NO_SUCH_USER);
727 /* Don't delete a user who is currently logged in. Instead, just
728 * set the access level to 0, and let the account get swept up
729 * during the next purge.
731 user_is_logged_in = 0;
732 begin_critical_section(S_SESSION_TABLE);
733 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
734 if (ccptr->user.usernum == usbuf.usernum) {
735 user_is_logged_in = 1;
738 end_critical_section(S_SESSION_TABLE);
739 if (user_is_logged_in == 1) {
740 lprintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
745 lprintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
747 /* Perform any purge functions registered by server extensions */
748 PerformUserHooks(&usbuf, EVT_PURGEUSER);
750 /* delete any existing user/room relationships */
751 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
753 /* delete the userlog entry */
754 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
756 /* remove the user's bio file */
757 snprintf(filename, sizeof filename, "./bio/%ld", usbuf.usernum);
760 /* remove the user's picture */
761 snprintf(filename, sizeof filename, "./userpics/%ld.gif", usbuf.usernum);
769 * create_user() - back end processing to create a new user
771 * Set 'newusername' to the desired account name.
772 * Set 'become_user' to nonzero if this is self-service account creation and we want
773 * to actually log in as the user we just created, otherwise set it to 0.
775 int create_user(char *newusername, int become_user)
777 struct ctdluser usbuf;
778 struct ctdlroom qrbuf;
779 struct passwd *p = NULL;
781 char mailboxname[ROOMNAMELEN];
784 safestrncpy(username, newusername, sizeof username);
787 #ifdef ENABLE_AUTOLOGIN
788 p = (struct passwd *) getpwnam(username);
790 extract_token(username, p->pw_gecos, 0, ',');
799 if (!getuser(&usbuf, username)) {
800 return (ERROR + ALREADY_EXISTS);
803 /* Go ahead and initialize a new user record */
804 memset(&usbuf, 0, sizeof(struct ctdluser));
805 safestrncpy(usbuf.fullname, username, sizeof usbuf.fullname);
806 strcpy(usbuf.password, "");
809 /* These are the default flags on new accounts */
810 usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
812 usbuf.timescalled = 0;
814 usbuf.axlevel = config.c_initax;
815 usbuf.USscreenwidth = 80;
816 usbuf.USscreenheight = 24;
817 usbuf.lastcall = time(NULL);
819 /* fetch a new user number */
820 usbuf.usernum = get_new_user_number();
822 /* The very first user created on the system will always be an Aide */
823 if (usbuf.usernum == 1L) {
827 /* add user to userlog */
831 * Give the user a private mailbox and a configuration room.
832 * Make the latter an invisible system room.
834 MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
835 create_room(mailboxname, 5, "", 0, 1, 1);
837 MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
838 create_room(mailboxname, 5, "", 0, 1, 1);
839 if (lgetroom(&qrbuf, mailboxname) == 0) {
840 qrbuf.QRflags2 |= QR2_SYSTEM;
844 /* Perform any create functions registered by server extensions */
845 PerformUserHooks(&usbuf, EVT_NEWUSER);
847 /* Everything below this line can be bypassed if administratively
848 * creating a user, instead of doing self-service account creation
852 /* Now become the user we just created */
853 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
854 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
857 /* Check to make sure we're still who we think we are */
858 if (getuser(&CC->user, CC->curr_user)) {
859 return (ERROR + INTERNAL_ERROR);
863 lprintf(CTDL_NOTICE, "New user <%s> created\n", username);
871 * cmd_newu() - create a new user account and log in as that user
873 void cmd_newu(char *cmdbuf)
878 if (config.c_disable_newu) {
879 cprintf("%d Self-service user account creation "
880 "is disabled on this system.\n", ERROR + NOT_HERE);
885 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
889 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
890 ERROR + MAX_SESSIONS_EXCEEDED,
891 config.c_nodename, config.c_maxsessions);
893 extract(username, cmdbuf, 0);
897 if (strlen(username) == 0) {
898 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
902 if ((!strcasecmp(username, "bbs")) ||
903 (!strcasecmp(username, "new")) ||
904 (!strcasecmp(username, "."))) {
905 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
909 a = create_user(username, 1);
913 logged_in_response();
914 } else if (a == ERROR + ALREADY_EXISTS) {
915 cprintf("%d '%s' already exists.\n",
916 ERROR + ALREADY_EXISTS, username);
918 } else if (a == ERROR + INTERNAL_ERROR) {
919 cprintf("%d Internal error - user record disappeared?\n",
920 ERROR + INTERNAL_ERROR);
923 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
932 void cmd_setp(char *new_pw)
934 if (CtdlAccessCheck(ac_logged_in)) {
937 if ( (CC->user.uid != BBSUID) && (CC->user.uid != (-1)) ) {
938 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
942 if (strlen(new_pw) == 0) {
943 cprintf("%d Password unchanged.\n", CIT_OK);
946 lgetuser(&CC->user, CC->curr_user);
947 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
949 cprintf("%d Password changed.\n", CIT_OK);
950 lprintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
951 PerformSessionHooks(EVT_SETPASS);
956 * cmd_creu() - administratively create a new user account (do not log in to it)
958 void cmd_creu(char *cmdbuf)
965 if (CtdlAccessCheck(ac_aide)) {
969 extract(username, cmdbuf, 0);
970 extract(password, cmdbuf, 1);
976 if (strlen(username) == 0) {
977 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
981 a = create_user(username, 0);
984 if (strlen(password) > 0) {
985 lgetuser(&tmp, username);
986 safestrncpy(tmp.password, password, sizeof(tmp.password));
989 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
990 (strlen(password) > 0) ? "and password set" :
993 } else if (a == ERROR + ALREADY_EXISTS) {
994 cprintf("%d '%s' already exists.\n",
995 ERROR + ALREADY_EXISTS, username);
998 cprintf("%d An error occured creating the user account.\n", ERROR + INTERNAL_ERROR);
1005 * get user parameters
1010 if (CtdlAccessCheck(ac_logged_in))
1013 getuser(&CC->user, CC->curr_user);
1014 cprintf("%d %d|%d|%d|\n",
1016 CC->user.USscreenwidth,
1017 CC->user.USscreenheight,
1018 (CC->user.flags & US_USER_SET)
1023 * set user parameters
1025 void cmd_setu(char *new_parms)
1027 if (CtdlAccessCheck(ac_logged_in))
1030 if (num_parms(new_parms) < 3) {
1031 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1034 lgetuser(&CC->user, CC->curr_user);
1035 CC->user.USscreenwidth = extract_int(new_parms, 0);
1036 CC->user.USscreenheight = extract_int(new_parms, 1);
1037 CC->user.flags = CC->user.flags & (~US_USER_SET);
1038 CC->user.flags = CC->user.flags |
1039 (extract_int(new_parms, 2) & US_USER_SET);
1041 lputuser(&CC->user);
1042 cprintf("%d Ok\n", CIT_OK);
1046 * set last read pointer
1048 void cmd_slrp(char *new_ptr)
1052 struct visit original_vbuf;
1054 if (CtdlAccessCheck(ac_logged_in)) {
1058 if (!strncasecmp(new_ptr, "highest", 7)) {
1059 newlr = CC->room.QRhighest;
1061 newlr = atol(new_ptr);
1064 lgetuser(&CC->user, CC->curr_user);
1066 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1067 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1068 vbuf.v_lastseen = newlr;
1069 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1071 /* Only rewrite the record if it changed */
1072 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1073 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1074 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1077 lputuser(&CC->user);
1078 cprintf("%d %ld\n", CIT_OK, newlr);
1082 void cmd_seen(char *argbuf) {
1083 long target_msgnum = 0L;
1084 int target_setting = 0;
1086 if (CtdlAccessCheck(ac_logged_in)) {
1090 if (num_parms(argbuf) != 2) {
1091 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1095 target_msgnum = extract_long(argbuf, 0);
1096 target_setting = extract_int(argbuf, 1);
1098 CtdlSetSeen(target_msgnum, target_setting, ctdlsetseen_seen);
1099 cprintf("%d OK\n", CIT_OK);
1103 void cmd_gtsn(char *argbuf) {
1106 if (CtdlAccessCheck(ac_logged_in)) {
1110 CtdlGetSeen(buf, ctdlsetseen_seen);
1111 cprintf("%d %s\n", CIT_OK, buf);
1116 * API function for cmd_invt_kick() and anything else that needs to
1117 * invite or kick out a user to/from a room.
1119 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1121 int CtdlInvtKick(char *iuser, int op) {
1122 struct ctdluser USscratch;
1126 if (getuser(&USscratch, iuser) != 0) {
1130 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1132 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1133 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1136 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1137 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1139 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1141 /* post a message in Aide> saying what we just did */
1142 snprintf(bbb, sizeof bbb, "%s %s %s> by %s\n",
1144 ((op == 1) ? "invited to" : "kicked out of"),
1154 * INVT and KICK commands
1156 void cmd_invt_kick(char *iuser, int op) {
1159 * These commands are only allowed by aides, room aides,
1160 * and room namespace owners
1163 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1164 /* access granted */
1167 cprintf("%d Higher access or room ownership required.\n",
1168 ERROR + HIGHER_ACCESS_REQUIRED);
1172 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1174 cprintf("%d Can't add/remove users from this room.\n",
1179 if (CtdlInvtKick(iuser, op) != 0) {
1180 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1184 cprintf("%d %s %s %s.\n",
1186 ((op == 1) ? "invited to" : "kicked out of"),
1193 * Forget (Zap) the current room (API call)
1194 * Returns 0 on success
1196 int CtdlForgetThisRoom(void) {
1199 /* On some systems, Aides are not allowed to forget rooms */
1200 if (is_aide() && (config.c_aide_zap == 0)
1201 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1205 lgetuser(&CC->user, CC->curr_user);
1206 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1208 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1209 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1211 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1212 lputuser(&CC->user);
1214 /* Return to the Lobby, so we don't end up in an undefined room */
1215 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1222 * forget (Zap) the current room
1227 if (CtdlAccessCheck(ac_logged_in)) {
1231 if (CtdlForgetThisRoom() == 0) {
1232 cprintf("%d Ok\n", CIT_OK);
1235 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1240 * Get Next Unregistered User
1244 struct cdbdata *cdbus;
1245 struct ctdluser usbuf;
1247 if (CtdlAccessCheck(ac_aide)) {
1251 if ((CitControl.MMflags & MM_VALID) == 0) {
1252 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1256 /* There are unvalidated users. Traverse the user database,
1257 * and return the first user we find that needs validation.
1259 cdb_rewind(CDB_USERS);
1260 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1261 memset(&usbuf, 0, sizeof(struct ctdluser));
1262 memcpy(&usbuf, cdbus->ptr,
1263 ((cdbus->len > sizeof(struct ctdluser)) ?
1264 sizeof(struct ctdluser) : cdbus->len));
1266 if ((usbuf.flags & US_NEEDVALID)
1267 && (usbuf.axlevel > 0)) {
1268 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1269 cdb_close_cursor(CDB_USERS);
1274 /* If we get to this point, there are no more unvalidated users.
1275 * Therefore we clear the "users need validation" flag.
1278 begin_critical_section(S_CONTROL);
1280 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1282 end_critical_section(S_CONTROL);
1283 cprintf("%d *** End of registration.\n", CIT_OK);
1292 void cmd_vali(char *v_args)
1296 struct ctdluser userbuf;
1298 extract(user, v_args, 0);
1299 newax = extract_int(v_args, 1);
1301 if (CtdlAccessCheck(ac_aide)) {
1305 if (lgetuser(&userbuf, user) != 0) {
1306 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1310 userbuf.axlevel = newax;
1311 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1315 /* If the access level was set to zero, delete the user */
1317 if (purge_user(user) == 0) {
1318 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1322 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1328 * Traverse the user file...
1330 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1333 struct ctdluser usbuf;
1334 struct cdbdata *cdbus;
1336 cdb_rewind(CDB_USERS);
1338 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1339 memset(&usbuf, 0, sizeof(struct ctdluser));
1340 memcpy(&usbuf, cdbus->ptr,
1341 ((cdbus->len > sizeof(struct ctdluser)) ?
1342 sizeof(struct ctdluser) : cdbus->len));
1344 (*CallBack) (&usbuf, in_data);
1350 * List one user (this works with cmd_list)
1352 void ListThisUser(struct ctdluser *usbuf, void *data)
1354 if (usbuf->axlevel > 0) {
1355 if ((CC->user.axlevel >= 6)
1356 || ((usbuf->flags & US_UNLISTED) == 0)
1357 || ((CC->internal_pgm))) {
1358 cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1362 (long)usbuf->lastcall,
1365 if (CC->user.axlevel >= 6)
1366 cprintf("%s", usbuf->password);
1377 cprintf("%d \n", LISTING_FOLLOWS);
1378 ForEachUser(ListThisUser, NULL);
1386 * assorted info we need to check at login
1394 if (CtdlAccessCheck(ac_logged_in)) {
1398 getuser(&CC->user, CC->curr_user); /* no lock is needed here */
1399 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1402 if (CC->user.axlevel >= 6) {
1404 if (CitControl.MMflags & MM_VALID)
1408 /* check for mail */
1409 mail = InitialMailCheck();
1411 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1416 * check to see if a user exists
1418 void cmd_qusr(char *who)
1420 struct ctdluser usbuf;
1422 if (getuser(&usbuf, who) == 0) {
1423 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1425 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1431 * Administrative Get User Parameters
1433 void cmd_agup(char *cmdbuf)
1435 struct ctdluser usbuf;
1436 char requested_user[SIZ];
1438 if (CtdlAccessCheck(ac_aide)) {
1442 extract(requested_user, cmdbuf, 0);
1443 if (getuser(&usbuf, requested_user) != 0) {
1444 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1447 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1454 (int) usbuf.axlevel,
1456 (long)usbuf.lastcall,
1463 * Administrative Set User Parameters
1465 void cmd_asup(char *cmdbuf)
1467 struct ctdluser usbuf;
1468 char requested_user[SIZ];
1474 if (CtdlAccessCheck(ac_aide))
1477 extract(requested_user, cmdbuf, 0);
1478 if (lgetuser(&usbuf, requested_user) != 0) {
1479 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1482 np = num_parms(cmdbuf);
1484 extract(usbuf.password, cmdbuf, 1);
1486 usbuf.flags = extract_int(cmdbuf, 2);
1488 usbuf.timescalled = extract_int(cmdbuf, 3);
1490 usbuf.posted = extract_int(cmdbuf, 4);
1492 newax = extract_int(cmdbuf, 5);
1493 if ((newax >= 0) && (newax <= 6)) {
1494 usbuf.axlevel = extract_int(cmdbuf, 5);
1498 usbuf.lastcall = extract_long(cmdbuf, 7);
1501 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1504 if (usbuf.axlevel == 0) {
1505 if (purge_user(requested_user) == 0) {
1511 sprintf(notify, "User <%s> deleted by %s\n",
1512 usbuf.fullname, CC->user.fullname);
1513 aide_message(notify);
1516 cprintf("%d Ok", CIT_OK);
1518 cprintf(" (%s deleted)", requested_user);
1525 * Check to see if the user who we just sent mail to is logged in. If yes,
1526 * bump the 'new mail' counter for their session. That enables them to
1527 * receive a new mail notification without having to hit the database.
1529 void BumpNewMailCounter(long which_user) {
1530 struct CitContext *ptr;
1532 begin_critical_section(S_SESSION_TABLE);
1534 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1535 if (ptr->user.usernum == which_user) {
1540 end_critical_section(S_SESSION_TABLE);
1545 * Count the number of new mail messages the user has
1549 int num_newmsgs = 0;
1551 num_newmsgs = CC->newmail;
1554 return (num_newmsgs);
1559 * Count the number of new mail messages the user has
1561 int InitialMailCheck()
1563 int num_newmsgs = 0;
1565 char mailboxname[ROOMNAMELEN];
1566 struct ctdlroom mailbox;
1568 struct cdbdata *cdbfr;
1569 long *msglist = NULL;
1572 MailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1573 if (getroom(&mailbox, mailboxname) != 0)
1575 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1577 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1579 if (cdbfr != NULL) {
1580 msglist = malloc(cdbfr->len);
1581 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1582 num_msgs = cdbfr->len / sizeof(long);
1586 for (a = 0; a < num_msgs; ++a) {
1587 if (msglist[a] > 0L) {
1588 if (msglist[a] > vbuf.v_lastseen) {
1593 if (msglist != NULL)
1596 return (num_newmsgs);
1602 * Set the preferred view for the current user/room combination
1604 void cmd_view(char *cmdbuf) {
1608 if (CtdlAccessCheck(ac_logged_in)) {
1612 requested_view = extract_int(cmdbuf, 0);
1614 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1615 vbuf.v_view = requested_view;
1616 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1618 cprintf("%d ok\n", CIT_OK);