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;
356 if (trythisname == NULL) return login_not_found;
357 safestrncpy(username, trythisname, sizeof username);
360 if ((CC->logged_in)) {
361 return login_already_logged_in;
364 /* First, try to log in as if the supplied name is a display name */
365 found_user = getuser(&CC->user, username);
367 /* If that didn't work, try to log in as if the supplied name
368 * is an e-mail address
370 if (found_user != 0) {
371 valid = validate_recipients(trythisname);
373 if (valid->num_local == 1) {
374 found_user = getuser(&CC->user,
381 #ifdef ENABLE_AUTOLOGIN
382 /* If we haven't found the account yet, and the supplied name
383 * is a login name on the underlying host system, create the
386 if (found_user != 0) {
387 struct passwd *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;
780 char mailboxname[ROOMNAMELEN];
783 safestrncpy(username, newusername, sizeof username);
786 #ifdef ENABLE_AUTOLOGIN
788 struct passwd *p = (struct passwd *) getpwnam(username);
791 extract_token(username, p->pw_gecos, 0, ',');
801 if (!getuser(&usbuf, username)) {
802 return (ERROR + ALREADY_EXISTS);
805 /* Go ahead and initialize a new user record */
806 memset(&usbuf, 0, sizeof(struct ctdluser));
807 safestrncpy(usbuf.fullname, username, sizeof usbuf.fullname);
808 strcpy(usbuf.password, "");
811 /* These are the default flags on new accounts */
812 usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
814 usbuf.timescalled = 0;
816 usbuf.axlevel = config.c_initax;
817 usbuf.USscreenwidth = 80;
818 usbuf.USscreenheight = 24;
819 usbuf.lastcall = time(NULL);
821 /* fetch a new user number */
822 usbuf.usernum = get_new_user_number();
824 /* The very first user created on the system will always be an Aide */
825 if (usbuf.usernum == 1L) {
829 /* add user to userlog */
833 * Give the user a private mailbox and a configuration room.
834 * Make the latter an invisible system room.
836 MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
837 create_room(mailboxname, 5, "", 0, 1, 1);
839 MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
840 create_room(mailboxname, 5, "", 0, 1, 1);
841 if (lgetroom(&qrbuf, mailboxname) == 0) {
842 qrbuf.QRflags2 |= QR2_SYSTEM;
846 /* Perform any create functions registered by server extensions */
847 PerformUserHooks(&usbuf, EVT_NEWUSER);
849 /* Everything below this line can be bypassed if administratively
850 * creating a user, instead of doing self-service account creation
854 /* Now become the user we just created */
855 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
856 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
859 /* Check to make sure we're still who we think we are */
860 if (getuser(&CC->user, CC->curr_user)) {
861 return (ERROR + INTERNAL_ERROR);
865 lprintf(CTDL_NOTICE, "New user <%s> created\n", username);
873 * cmd_newu() - create a new user account and log in as that user
875 void cmd_newu(char *cmdbuf)
880 if (config.c_disable_newu) {
881 cprintf("%d Self-service user account creation "
882 "is disabled on this system.\n", ERROR + NOT_HERE);
887 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
891 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
892 ERROR + MAX_SESSIONS_EXCEEDED,
893 config.c_nodename, config.c_maxsessions);
895 extract(username, cmdbuf, 0);
899 if (strlen(username) == 0) {
900 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
904 if ((!strcasecmp(username, "bbs")) ||
905 (!strcasecmp(username, "new")) ||
906 (!strcasecmp(username, "."))) {
907 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
911 a = create_user(username, 1);
915 logged_in_response();
916 } else if (a == ERROR + ALREADY_EXISTS) {
917 cprintf("%d '%s' already exists.\n",
918 ERROR + ALREADY_EXISTS, username);
920 } else if (a == ERROR + INTERNAL_ERROR) {
921 cprintf("%d Internal error - user record disappeared?\n",
922 ERROR + INTERNAL_ERROR);
925 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
934 void cmd_setp(char *new_pw)
936 if (CtdlAccessCheck(ac_logged_in)) {
939 if ( (CC->user.uid != BBSUID) && (CC->user.uid != (-1)) ) {
940 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
944 if (strlen(new_pw) == 0) {
945 cprintf("%d Password unchanged.\n", CIT_OK);
948 lgetuser(&CC->user, CC->curr_user);
949 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
951 cprintf("%d Password changed.\n", CIT_OK);
952 lprintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
953 PerformSessionHooks(EVT_SETPASS);
958 * cmd_creu() - administratively create a new user account (do not log in to it)
960 void cmd_creu(char *cmdbuf)
967 if (CtdlAccessCheck(ac_aide)) {
971 extract(username, cmdbuf, 0);
972 extract(password, cmdbuf, 1);
978 if (strlen(username) == 0) {
979 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
983 a = create_user(username, 0);
986 if (strlen(password) > 0) {
987 lgetuser(&tmp, username);
988 safestrncpy(tmp.password, password, sizeof(tmp.password));
991 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
992 (strlen(password) > 0) ? "and password set" :
995 } else if (a == ERROR + ALREADY_EXISTS) {
996 cprintf("%d '%s' already exists.\n",
997 ERROR + ALREADY_EXISTS, username);
1000 cprintf("%d An error occured creating the user account.\n", ERROR + INTERNAL_ERROR);
1007 * get user parameters
1012 if (CtdlAccessCheck(ac_logged_in))
1015 getuser(&CC->user, CC->curr_user);
1016 cprintf("%d %d|%d|%d|\n",
1018 CC->user.USscreenwidth,
1019 CC->user.USscreenheight,
1020 (CC->user.flags & US_USER_SET)
1025 * set user parameters
1027 void cmd_setu(char *new_parms)
1029 if (CtdlAccessCheck(ac_logged_in))
1032 if (num_parms(new_parms) < 3) {
1033 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1036 lgetuser(&CC->user, CC->curr_user);
1037 CC->user.USscreenwidth = extract_int(new_parms, 0);
1038 CC->user.USscreenheight = extract_int(new_parms, 1);
1039 CC->user.flags = CC->user.flags & (~US_USER_SET);
1040 CC->user.flags = CC->user.flags |
1041 (extract_int(new_parms, 2) & US_USER_SET);
1043 lputuser(&CC->user);
1044 cprintf("%d Ok\n", CIT_OK);
1048 * set last read pointer
1050 void cmd_slrp(char *new_ptr)
1054 struct visit original_vbuf;
1056 if (CtdlAccessCheck(ac_logged_in)) {
1060 if (!strncasecmp(new_ptr, "highest", 7)) {
1061 newlr = CC->room.QRhighest;
1063 newlr = atol(new_ptr);
1066 lgetuser(&CC->user, CC->curr_user);
1068 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1069 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1070 vbuf.v_lastseen = newlr;
1071 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1073 /* Only rewrite the record if it changed */
1074 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1075 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1076 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1079 lputuser(&CC->user);
1080 cprintf("%d %ld\n", CIT_OK, newlr);
1084 void cmd_seen(char *argbuf) {
1085 long target_msgnum = 0L;
1086 int target_setting = 0;
1088 if (CtdlAccessCheck(ac_logged_in)) {
1092 if (num_parms(argbuf) != 2) {
1093 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1097 target_msgnum = extract_long(argbuf, 0);
1098 target_setting = extract_int(argbuf, 1);
1100 CtdlSetSeen(target_msgnum, target_setting, ctdlsetseen_seen);
1101 cprintf("%d OK\n", CIT_OK);
1105 void cmd_gtsn(char *argbuf) {
1108 if (CtdlAccessCheck(ac_logged_in)) {
1112 CtdlGetSeen(buf, ctdlsetseen_seen);
1113 cprintf("%d %s\n", CIT_OK, buf);
1118 * API function for cmd_invt_kick() and anything else that needs to
1119 * invite or kick out a user to/from a room.
1121 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1123 int CtdlInvtKick(char *iuser, int op) {
1124 struct ctdluser USscratch;
1128 if (getuser(&USscratch, iuser) != 0) {
1132 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1134 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1135 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1138 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1139 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1141 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1143 /* post a message in Aide> saying what we just did */
1144 snprintf(bbb, sizeof bbb, "%s %s %s> by %s\n",
1146 ((op == 1) ? "invited to" : "kicked out of"),
1156 * INVT and KICK commands
1158 void cmd_invt_kick(char *iuser, int op) {
1161 * These commands are only allowed by aides, room aides,
1162 * and room namespace owners
1165 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1166 /* access granted */
1169 cprintf("%d Higher access or room ownership required.\n",
1170 ERROR + HIGHER_ACCESS_REQUIRED);
1174 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1176 cprintf("%d Can't add/remove users from this room.\n",
1181 if (CtdlInvtKick(iuser, op) != 0) {
1182 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1186 cprintf("%d %s %s %s.\n",
1188 ((op == 1) ? "invited to" : "kicked out of"),
1195 * Forget (Zap) the current room (API call)
1196 * Returns 0 on success
1198 int CtdlForgetThisRoom(void) {
1201 /* On some systems, Aides are not allowed to forget rooms */
1202 if (is_aide() && (config.c_aide_zap == 0)
1203 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1207 lgetuser(&CC->user, CC->curr_user);
1208 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1210 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1211 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1213 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1214 lputuser(&CC->user);
1216 /* Return to the Lobby, so we don't end up in an undefined room */
1217 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1224 * forget (Zap) the current room
1229 if (CtdlAccessCheck(ac_logged_in)) {
1233 if (CtdlForgetThisRoom() == 0) {
1234 cprintf("%d Ok\n", CIT_OK);
1237 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1242 * Get Next Unregistered User
1246 struct cdbdata *cdbus;
1247 struct ctdluser usbuf;
1249 if (CtdlAccessCheck(ac_aide)) {
1253 if ((CitControl.MMflags & MM_VALID) == 0) {
1254 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1258 /* There are unvalidated users. Traverse the user database,
1259 * and return the first user we find that needs validation.
1261 cdb_rewind(CDB_USERS);
1262 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1263 memset(&usbuf, 0, sizeof(struct ctdluser));
1264 memcpy(&usbuf, cdbus->ptr,
1265 ((cdbus->len > sizeof(struct ctdluser)) ?
1266 sizeof(struct ctdluser) : cdbus->len));
1268 if ((usbuf.flags & US_NEEDVALID)
1269 && (usbuf.axlevel > 0)) {
1270 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1271 cdb_close_cursor(CDB_USERS);
1276 /* If we get to this point, there are no more unvalidated users.
1277 * Therefore we clear the "users need validation" flag.
1280 begin_critical_section(S_CONTROL);
1282 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1284 end_critical_section(S_CONTROL);
1285 cprintf("%d *** End of registration.\n", CIT_OK);
1294 void cmd_vali(char *v_args)
1298 struct ctdluser userbuf;
1300 extract(user, v_args, 0);
1301 newax = extract_int(v_args, 1);
1303 if (CtdlAccessCheck(ac_aide)) {
1307 if (lgetuser(&userbuf, user) != 0) {
1308 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1312 userbuf.axlevel = newax;
1313 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1317 /* If the access level was set to zero, delete the user */
1319 if (purge_user(user) == 0) {
1320 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1324 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1330 * Traverse the user file...
1332 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1335 struct ctdluser usbuf;
1336 struct cdbdata *cdbus;
1338 cdb_rewind(CDB_USERS);
1340 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1341 memset(&usbuf, 0, sizeof(struct ctdluser));
1342 memcpy(&usbuf, cdbus->ptr,
1343 ((cdbus->len > sizeof(struct ctdluser)) ?
1344 sizeof(struct ctdluser) : cdbus->len));
1346 (*CallBack) (&usbuf, in_data);
1352 * List one user (this works with cmd_list)
1354 void ListThisUser(struct ctdluser *usbuf, void *data)
1356 if (usbuf->axlevel > 0) {
1357 if ((CC->user.axlevel >= 6)
1358 || ((usbuf->flags & US_UNLISTED) == 0)
1359 || ((CC->internal_pgm))) {
1360 cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1364 (long)usbuf->lastcall,
1367 if (CC->user.axlevel >= 6)
1368 cprintf("%s", usbuf->password);
1379 cprintf("%d \n", LISTING_FOLLOWS);
1380 ForEachUser(ListThisUser, NULL);
1388 * assorted info we need to check at login
1396 if (CtdlAccessCheck(ac_logged_in)) {
1400 getuser(&CC->user, CC->curr_user); /* no lock is needed here */
1401 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1404 if (CC->user.axlevel >= 6) {
1406 if (CitControl.MMflags & MM_VALID)
1410 /* check for mail */
1411 mail = InitialMailCheck();
1413 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1418 * check to see if a user exists
1420 void cmd_qusr(char *who)
1422 struct ctdluser usbuf;
1424 if (getuser(&usbuf, who) == 0) {
1425 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1427 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1433 * Administrative Get User Parameters
1435 void cmd_agup(char *cmdbuf)
1437 struct ctdluser usbuf;
1438 char requested_user[SIZ];
1440 if (CtdlAccessCheck(ac_aide)) {
1444 extract(requested_user, cmdbuf, 0);
1445 if (getuser(&usbuf, requested_user) != 0) {
1446 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1449 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1456 (int) usbuf.axlevel,
1458 (long)usbuf.lastcall,
1465 * Administrative Set User Parameters
1467 void cmd_asup(char *cmdbuf)
1469 struct ctdluser usbuf;
1470 char requested_user[SIZ];
1476 if (CtdlAccessCheck(ac_aide))
1479 extract(requested_user, cmdbuf, 0);
1480 if (lgetuser(&usbuf, requested_user) != 0) {
1481 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1484 np = num_parms(cmdbuf);
1486 extract(usbuf.password, cmdbuf, 1);
1488 usbuf.flags = extract_int(cmdbuf, 2);
1490 usbuf.timescalled = extract_int(cmdbuf, 3);
1492 usbuf.posted = extract_int(cmdbuf, 4);
1494 newax = extract_int(cmdbuf, 5);
1495 if ((newax >= 0) && (newax <= 6)) {
1496 usbuf.axlevel = extract_int(cmdbuf, 5);
1500 usbuf.lastcall = extract_long(cmdbuf, 7);
1503 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1506 if (usbuf.axlevel == 0) {
1507 if (purge_user(requested_user) == 0) {
1513 sprintf(notify, "User <%s> deleted by %s\n",
1514 usbuf.fullname, CC->user.fullname);
1515 aide_message(notify);
1518 cprintf("%d Ok", CIT_OK);
1520 cprintf(" (%s deleted)", requested_user);
1527 * Check to see if the user who we just sent mail to is logged in. If yes,
1528 * bump the 'new mail' counter for their session. That enables them to
1529 * receive a new mail notification without having to hit the database.
1531 void BumpNewMailCounter(long which_user) {
1532 struct CitContext *ptr;
1534 begin_critical_section(S_SESSION_TABLE);
1536 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1537 if (ptr->user.usernum == which_user) {
1542 end_critical_section(S_SESSION_TABLE);
1547 * Count the number of new mail messages the user has
1551 int num_newmsgs = 0;
1553 num_newmsgs = CC->newmail;
1556 return (num_newmsgs);
1561 * Count the number of new mail messages the user has
1563 int InitialMailCheck()
1565 int num_newmsgs = 0;
1567 char mailboxname[ROOMNAMELEN];
1568 struct ctdlroom mailbox;
1570 struct cdbdata *cdbfr;
1571 long *msglist = NULL;
1574 MailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1575 if (getroom(&mailbox, mailboxname) != 0)
1577 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1579 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1581 if (cdbfr != NULL) {
1582 msglist = malloc(cdbfr->len);
1583 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1584 num_msgs = cdbfr->len / sizeof(long);
1588 for (a = 0; a < num_msgs; ++a) {
1589 if (msglist[a] > 0L) {
1590 if (msglist[a] > vbuf.v_lastseen) {
1595 if (msglist != NULL)
1598 return (num_newmsgs);
1604 * Set the preferred view for the current user/room combination
1606 void cmd_view(char *cmdbuf) {
1610 if (CtdlAccessCheck(ac_logged_in)) {
1614 requested_view = extract_int(cmdbuf, 0);
1616 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1617 vbuf.v_view = requested_view;
1618 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1620 cprintf("%d ok\n", CIT_OK);