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 /* Did we find something? */
382 if (found_user == 0) {
383 if (((CC->nologin)) && (CC->user.axlevel < 6)) {
384 return login_too_many_users;
386 safestrncpy(CC->curr_user, CC->user.fullname,
387 sizeof CC->curr_user);
391 return login_not_found;
399 void cmd_user(char *cmdbuf)
404 extract(username, cmdbuf, 0);
407 a = CtdlLoginExistingUser(username);
409 case login_already_logged_in:
410 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
412 case login_too_many_users:
414 "Too many users are already online "
416 ERROR + MAX_SESSIONS_EXCEEDED,
417 config.c_nodename, config.c_maxsessions);
420 cprintf("%d Password required for %s\n",
421 MORE_DATA, CC->curr_user);
423 case login_not_found:
424 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
426 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
433 * session startup code which is common to both cmd_pass() and cmd_newu()
435 void session_startup(void)
439 lprintf(3, "<%s> logged in\n", CC->curr_user);
441 lgetuser(&CC->user, CC->curr_user);
442 ++(CC->user.timescalled);
443 CC->previous_login = CC->user.lastcall;
444 time(&CC->user.lastcall);
446 /* If this user's name is the name of the system administrator
447 * (as specified in setup), automatically assign access level 6.
449 if (!strcasecmp(CC->user.fullname, config.c_sysadm)) {
450 CC->user.axlevel = 6;
455 * Populate CC->cs_inet_email with a default address. This will be
456 * overwritten with the user's directory address, if one exists, when
457 * the vCard module's login hook runs.
459 snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
460 CC->user.fullname, config.c_fqdn);
461 for (i=0; i<strlen(CC->cs_inet_email); ++i) {
462 if (isspace(CC->cs_inet_email[i])) {
463 CC->cs_inet_email[i] = '_';
467 /* Create any personal rooms required by the system.
468 * (Technically, MAILROOM should be there already, but just in case...)
470 create_room(MAILROOM, 4, "", 0, 1, 0);
471 create_room(SENTITEMS, 4, "", 0, 1, 0);
473 /* Run any startup routines registered by loadable modules */
474 PerformSessionHooks(EVT_LOGIN);
476 /* Enter the lobby */
477 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
481 void logged_in_response(void)
483 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
484 CIT_OK, CC->user.fullname, CC->user.axlevel,
485 CC->user.timescalled, CC->user.posted,
486 CC->user.flags, CC->user.usernum,
493 * misc things to be taken care of when a user is logged out
495 void logout(struct CitContext *who)
498 * Clear out some session data. Most likely, the CitContext for this
499 * session is about to get nuked when the session disconnects, but
500 * since it's possible to log in again without reconnecting, we cannot
501 * make that assumption.
503 strcpy(who->fake_username, "");
504 strcpy(who->fake_postname, "");
505 strcpy(who->fake_hostname, "");
506 strcpy(who->fake_roomname, "");
510 * If there is a download in progress, abort it.
512 if (who->download_fp != NULL) {
513 fclose(who->download_fp);
514 who->download_fp = NULL;
518 * If there is an upload in progress, abort it.
520 if (who->upload_fp != NULL) {
525 * If we were talking to a network node, we're not anymore...
527 if (strlen(who->net_node) > 0) {
528 network_talking_to(who->net_node, NTT_REMOVE);
531 /* Do modular stuff... */
532 PerformSessionHooks(EVT_LOGOUT);
534 /* Free any output buffers */
535 if (who->output_buffer != NULL) {
542 * an alternate version of validpw() which executes `chkpwd' instead of
543 * verifying the password directly
545 static int validpw(uid_t uid, const char *pass)
548 int status, pipev[2];
552 lprintf(1, "pipe failed (%s): denying autologin access for "
553 "uid %ld\n", strerror(errno), (long)uid);
556 switch (pid = fork()) {
558 lprintf(1, "fork failed (%s): denying autologin access for "
559 "uid %ld\n", strerror(errno), (long)uid);
566 if (dup2(pipev[0], 0) == -1) {
572 execl(BBSDIR "/chkpwd", BBSDIR "/chkpwd", NULL);
573 perror(BBSDIR "/chkpwd");
579 snprintf(buf, sizeof buf, "%lu\n", (unsigned long) uid));
580 write(pipev[1], pass, strlen(pass));
581 write(pipev[1], "\n", 1);
584 while (waitpid(pid, &status, 0) == -1)
585 if (errno != EINTR) {
586 lprintf(1, "waitpid failed (%s): denying autologin "
587 "access for uid %ld\n",
588 strerror(errno), (long)uid);
591 if (WIFEXITED(status) && !WEXITSTATUS(status))
605 int CtdlTryPassword(char *password)
609 if ((CC->logged_in)) {
610 lprintf(5, "CtdlTryPassword: already logged in\n");
611 return pass_already_logged_in;
613 if (!strcmp(CC->curr_user, NLI)) {
614 lprintf(5, "CtdlTryPassword: no user selected\n");
617 if (getuser(&CC->user, CC->curr_user)) {
618 lprintf(5, "CtdlTryPassword: internal error\n");
619 return pass_internal_error;
621 if (password == NULL) {
622 lprintf(5, "CtdlTryPassword: NULL password string supplied\n");
623 return pass_wrong_password;
628 #ifdef ENABLE_AUTOLOGIN
629 /* A uid of BBSUID or -1 indicates that this user exists only in
630 * Citadel, not in the underlying operating system.
632 if ( (CC->user.uid == BBSUID) || (CC->user.uid == (-1)) ) {
634 strproc(CC->user.password);
635 code = strcasecmp(CC->user.password, password);
637 /* Any other uid means we have to check the system password database */
639 if (validpw(CC->user.uid, password)) {
641 lgetuser(&CC->user, CC->curr_user);
642 safestrncpy(CC->user.password, password,
643 sizeof CC->user.password);
648 #else /* ENABLE_AUTOLOGIN */
650 strproc(CC->user.password);
651 code = strcasecmp(CC->user.password, password);
653 #endif /* ENABLE_AUTOLOGIN */
659 lprintf(3, "Bad password specified for <%s>\n", CC->curr_user);
660 return pass_wrong_password;
665 void cmd_pass(char *buf)
670 extract(password, buf, 0);
671 a = CtdlTryPassword(password);
674 case pass_already_logged_in:
675 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
678 cprintf("%d You must send a name with USER first.\n",
679 ERROR + USERNAME_REQUIRED);
681 case pass_wrong_password:
682 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
685 logged_in_response();
687 cprintf("%d Can't find user record!\n",
688 ERROR + INTERNAL_ERROR);
695 * Delete a user record *and* all of its related resources.
697 int purge_user(char pname[])
700 struct ctdluser usbuf;
701 char usernamekey[USERNAME_SIZE];
702 struct CitContext *ccptr;
703 int user_is_logged_in = 0;
705 makeuserkey(usernamekey, pname);
707 if (getuser(&usbuf, pname) != 0) {
708 lprintf(5, "Cannot purge user <%s> - not found\n", pname);
709 return (ERROR + NO_SUCH_USER);
711 /* Don't delete a user who is currently logged in. Instead, just
712 * set the access level to 0, and let the account get swept up
713 * during the next purge.
715 user_is_logged_in = 0;
716 begin_critical_section(S_SESSION_TABLE);
717 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
718 if (ccptr->user.usernum == usbuf.usernum) {
719 user_is_logged_in = 1;
722 end_critical_section(S_SESSION_TABLE);
723 if (user_is_logged_in == 1) {
724 lprintf(5, "User <%s> is logged in; not deleting.\n", pname);
729 lprintf(5, "Deleting user <%s>\n", pname);
731 /* Perform any purge functions registered by server extensions */
732 PerformUserHooks(usbuf.fullname, usbuf.usernum, EVT_PURGEUSER);
734 /* delete any existing user/room relationships */
735 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
737 /* delete the userlog entry */
738 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
740 /* remove the user's bio file */
741 snprintf(filename, sizeof filename, "./bio/%ld", usbuf.usernum);
744 /* remove the user's picture */
745 snprintf(filename, sizeof filename, "./userpics/%ld.gif", usbuf.usernum);
753 * create_user() - back end processing to create a new user
755 * Set 'newusername' to the desired account name.
756 * Set 'become_user' to nonzero if this is self-service account creation and we want
757 * to actually log in as the user we just created, otherwise set it to 0.
759 int create_user(char *newusername, int become_user)
761 struct ctdluser usbuf;
762 struct ctdlroom qrbuf;
763 struct passwd *p = NULL;
765 char mailboxname[ROOMNAMELEN];
768 safestrncpy(username, newusername, sizeof username);
771 #ifdef ENABLE_AUTOLOGIN
772 p = (struct passwd *) getpwnam(username);
774 extract_token(username, p->pw_gecos, 0, ',');
783 if (!getuser(&usbuf, username)) {
784 return (ERROR + ALREADY_EXISTS);
787 /* Go ahead and initialize a new user record */
788 memset(&usbuf, 0, sizeof(struct ctdluser));
789 safestrncpy(usbuf.fullname, username, sizeof usbuf.fullname);
790 strcpy(usbuf.password, "");
793 /* These are the default flags on new accounts */
794 usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
796 usbuf.timescalled = 0;
798 usbuf.axlevel = config.c_initax;
799 usbuf.USscreenwidth = 80;
800 usbuf.USscreenheight = 24;
801 usbuf.lastcall = time(NULL);
803 /* fetch a new user number */
804 usbuf.usernum = get_new_user_number();
806 /* The very first user created on the system will always be an Aide */
807 if (usbuf.usernum == 1L) {
811 /* add user to userlog */
815 * Give the user a private mailbox and a configuration room.
816 * Make the latter an invisible system room.
818 MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
819 create_room(mailboxname, 5, "", 0, 1, 1);
821 MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
822 create_room(mailboxname, 5, "", 0, 1, 1);
823 if (lgetroom(&qrbuf, mailboxname) == 0) {
824 qrbuf.QRflags2 |= QR2_SYSTEM;
828 /* Everything below this line can be bypassed if administratively
829 creating a user, instead of doing self-service account creation
833 /* Now become the user we just created */
834 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
835 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
838 /* Check to make sure we're still who we think we are */
839 if (getuser(&CC->user, CC->curr_user)) {
840 return (ERROR + INTERNAL_ERROR);
844 lprintf(3, "New user <%s> created\n", username);
852 * cmd_newu() - create a new user account and log in as that user
854 void cmd_newu(char *cmdbuf)
859 if (config.c_disable_newu) {
860 cprintf("%d Self-service user account creation "
861 "is disabled on this system.\n", ERROR + NOT_HERE);
866 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
870 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
871 ERROR + MAX_SESSIONS_EXCEEDED,
872 config.c_nodename, config.c_maxsessions);
874 extract(username, cmdbuf, 0);
878 if (strlen(username) == 0) {
879 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
883 if ((!strcasecmp(username, "bbs")) ||
884 (!strcasecmp(username, "new")) ||
885 (!strcasecmp(username, "."))) {
886 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
890 a = create_user(username, 1);
894 logged_in_response();
895 } else if (a == ERROR + ALREADY_EXISTS) {
896 cprintf("%d '%s' already exists.\n",
897 ERROR + ALREADY_EXISTS, username);
899 } else if (a == ERROR + INTERNAL_ERROR) {
900 cprintf("%d Internal error - user record disappeared?\n",
901 ERROR + INTERNAL_ERROR);
904 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
913 void cmd_setp(char *new_pw)
915 if (CtdlAccessCheck(ac_logged_in)) {
918 if ( (CC->user.uid != BBSUID) && (CC->user.uid != (-1)) ) {
919 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
923 if (strlen(new_pw) == 0) {
924 cprintf("%d Password unchanged.\n", CIT_OK);
927 lgetuser(&CC->user, CC->curr_user);
928 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
930 cprintf("%d Password changed.\n", CIT_OK);
931 lprintf(3, "Password changed for user <%s>\n", CC->curr_user);
932 PerformSessionHooks(EVT_SETPASS);
937 * cmd_creu() - administratively create a new user account (do not log in to it)
939 void cmd_creu(char *cmdbuf)
946 if (CtdlAccessCheck(ac_aide)) {
950 extract(username, cmdbuf, 0);
951 extract(password, cmdbuf, 1);
957 if (strlen(username) == 0) {
958 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
962 a = create_user(username, 0);
965 if (strlen(password) > 0) {
966 lgetuser(&tmp, username);
967 safestrncpy(tmp.password, password, sizeof(tmp.password));
970 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
971 (strlen(password) > 0) ? "and password set" :
974 } else if (a == ERROR + ALREADY_EXISTS) {
975 cprintf("%d '%s' already exists.\n",
976 ERROR + ALREADY_EXISTS, username);
979 cprintf("%d An error occured creating the user account.\n", ERROR + INTERNAL_ERROR);
986 * get user parameters
991 if (CtdlAccessCheck(ac_logged_in))
994 getuser(&CC->user, CC->curr_user);
995 cprintf("%d %d|%d|%d|\n",
997 CC->user.USscreenwidth,
998 CC->user.USscreenheight,
999 (CC->user.flags & US_USER_SET)
1004 * set user parameters
1006 void cmd_setu(char *new_parms)
1008 if (CtdlAccessCheck(ac_logged_in))
1011 if (num_parms(new_parms) < 3) {
1012 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1015 lgetuser(&CC->user, CC->curr_user);
1016 CC->user.USscreenwidth = extract_int(new_parms, 0);
1017 CC->user.USscreenheight = extract_int(new_parms, 1);
1018 CC->user.flags = CC->user.flags & (~US_USER_SET);
1019 CC->user.flags = CC->user.flags |
1020 (extract_int(new_parms, 2) & US_USER_SET);
1022 lputuser(&CC->user);
1023 cprintf("%d Ok\n", CIT_OK);
1027 * set last read pointer
1029 void cmd_slrp(char *new_ptr)
1033 struct visit original_vbuf;
1035 if (CtdlAccessCheck(ac_logged_in)) {
1039 if (!strncasecmp(new_ptr, "highest", 7)) {
1040 newlr = CC->room.QRhighest;
1042 newlr = atol(new_ptr);
1045 lgetuser(&CC->user, CC->curr_user);
1047 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1048 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1049 vbuf.v_lastseen = newlr;
1050 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1052 /* Only rewrite the record if it changed */
1053 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1054 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1055 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1058 lputuser(&CC->user);
1059 cprintf("%d %ld\n", CIT_OK, newlr);
1063 void cmd_seen(char *argbuf) {
1064 long target_msgnum = 0L;
1065 int target_setting = 0;
1067 if (CtdlAccessCheck(ac_logged_in)) {
1071 if (num_parms(argbuf) != 2) {
1072 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1076 target_msgnum = extract_long(argbuf, 0);
1077 target_setting = extract_int(argbuf, 1);
1079 CtdlSetSeen(target_msgnum, target_setting, ctdlsetseen_seen);
1080 cprintf("%d OK\n", CIT_OK);
1084 void cmd_gtsn(char *argbuf) {
1087 if (CtdlAccessCheck(ac_logged_in)) {
1091 CtdlGetSeen(buf, ctdlsetseen_seen);
1092 cprintf("%d %s\n", CIT_OK, buf);
1098 * INVT and KICK commands
1100 void cmd_invt_kick(char *iuser, int op)
1102 { /* 1 = invite, 0 = kick out */
1103 struct ctdluser USscratch;
1108 * These commands are only allowed by aides, room aides,
1109 * and room namespace owners
1112 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1113 /* access granted */
1116 cprintf("%d Higher access or room ownership required.\n",
1117 ERROR + HIGHER_ACCESS_REQUIRED);
1121 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1123 cprintf("%d Can't add/remove users from this room.\n",
1128 if (lgetuser(&USscratch, iuser) != 0) {
1129 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1132 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1135 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1136 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1139 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1140 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1142 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1144 lputuser(&USscratch);
1146 /* post a message in Aide> saying what we just did */
1147 snprintf(bbb, sizeof bbb, "%s %s %s> by %s\n",
1149 ((op == 1) ? "invited to" : "kicked out of"),
1154 cprintf("%d %s %s %s.\n",
1156 ((op == 1) ? "invited to" : "kicked out of"),
1163 * Forget (Zap) the current room (API call)
1164 * Returns 0 on success
1166 int CtdlForgetThisRoom(void) {
1169 /* On some systems, Aides are not allowed to forget rooms */
1170 if (is_aide() && (config.c_aide_zap == 0)
1171 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1175 lgetuser(&CC->user, CC->curr_user);
1176 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1178 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1179 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1181 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1182 lputuser(&CC->user);
1184 /* Return to the Lobby, so we don't end up in an undefined room */
1185 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1192 * forget (Zap) the current room
1197 if (CtdlAccessCheck(ac_logged_in)) {
1201 if (CtdlForgetThisRoom() == 0) {
1202 cprintf("%d Ok\n", CIT_OK);
1205 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1210 * Get Next Unregistered User
1214 struct cdbdata *cdbus;
1215 struct ctdluser usbuf;
1217 if (CtdlAccessCheck(ac_aide)) {
1221 if ((CitControl.MMflags & MM_VALID) == 0) {
1222 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1226 /* There are unvalidated users. Traverse the user database,
1227 * and return the first user we find that needs validation.
1229 cdb_rewind(CDB_USERS);
1230 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1231 memset(&usbuf, 0, sizeof(struct ctdluser));
1232 memcpy(&usbuf, cdbus->ptr,
1233 ((cdbus->len > sizeof(struct ctdluser)) ?
1234 sizeof(struct ctdluser) : cdbus->len));
1236 if ((usbuf.flags & US_NEEDVALID)
1237 && (usbuf.axlevel > 0)) {
1238 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1239 cdb_close_cursor(CDB_USERS);
1244 /* If we get to this point, there are no more unvalidated users.
1245 * Therefore we clear the "users need validation" flag.
1248 begin_critical_section(S_CONTROL);
1250 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1252 end_critical_section(S_CONTROL);
1253 cprintf("%d *** End of registration.\n", CIT_OK);
1262 void cmd_vali(char *v_args)
1266 struct ctdluser userbuf;
1268 extract(user, v_args, 0);
1269 newax = extract_int(v_args, 1);
1271 if (CtdlAccessCheck(ac_aide)) {
1275 if (lgetuser(&userbuf, user) != 0) {
1276 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1280 userbuf.axlevel = newax;
1281 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1285 /* If the access level was set to zero, delete the user */
1287 if (purge_user(user) == 0) {
1288 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1292 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1298 * Traverse the user file...
1300 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1303 struct ctdluser usbuf;
1304 struct cdbdata *cdbus;
1306 cdb_rewind(CDB_USERS);
1308 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1309 memset(&usbuf, 0, sizeof(struct ctdluser));
1310 memcpy(&usbuf, cdbus->ptr,
1311 ((cdbus->len > sizeof(struct ctdluser)) ?
1312 sizeof(struct ctdluser) : cdbus->len));
1314 (*CallBack) (&usbuf, in_data);
1320 * List one user (this works with cmd_list)
1322 void ListThisUser(struct ctdluser *usbuf, void *data)
1324 if (usbuf->axlevel > 0) {
1325 if ((CC->user.axlevel >= 6)
1326 || ((usbuf->flags & US_UNLISTED) == 0)
1327 || ((CC->internal_pgm))) {
1328 cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1332 (long)usbuf->lastcall,
1335 if (CC->user.axlevel >= 6)
1336 cprintf("%s", usbuf->password);
1347 cprintf("%d \n", LISTING_FOLLOWS);
1348 ForEachUser(ListThisUser, NULL);
1356 * assorted info we need to check at login
1364 if (CtdlAccessCheck(ac_logged_in)) {
1368 getuser(&CC->user, CC->curr_user); /* no lock is needed here */
1369 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1372 if (CC->user.axlevel >= 6) {
1374 if (CitControl.MMflags & MM_VALID)
1378 /* check for mail */
1379 mail = InitialMailCheck();
1381 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1386 * check to see if a user exists
1388 void cmd_qusr(char *who)
1390 struct ctdluser usbuf;
1392 if (getuser(&usbuf, who) == 0) {
1393 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1395 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1401 * Administrative Get User Parameters
1403 void cmd_agup(char *cmdbuf)
1405 struct ctdluser usbuf;
1406 char requested_user[SIZ];
1408 if (CtdlAccessCheck(ac_aide)) {
1412 extract(requested_user, cmdbuf, 0);
1413 if (getuser(&usbuf, requested_user) != 0) {
1414 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1417 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1424 (int) usbuf.axlevel,
1426 (long)usbuf.lastcall,
1433 * Administrative Set User Parameters
1435 void cmd_asup(char *cmdbuf)
1437 struct ctdluser usbuf;
1438 char requested_user[SIZ];
1444 if (CtdlAccessCheck(ac_aide))
1447 extract(requested_user, cmdbuf, 0);
1448 if (lgetuser(&usbuf, requested_user) != 0) {
1449 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1452 np = num_parms(cmdbuf);
1454 extract(usbuf.password, cmdbuf, 1);
1456 usbuf.flags = extract_int(cmdbuf, 2);
1458 usbuf.timescalled = extract_int(cmdbuf, 3);
1460 usbuf.posted = extract_int(cmdbuf, 4);
1462 newax = extract_int(cmdbuf, 5);
1463 if ((newax >= 0) && (newax <= 6)) {
1464 usbuf.axlevel = extract_int(cmdbuf, 5);
1468 usbuf.lastcall = extract_long(cmdbuf, 7);
1471 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1474 if (usbuf.axlevel == 0) {
1475 if (purge_user(requested_user) == 0) {
1481 sprintf(notify, "User <%s> deleted by %s\n",
1482 usbuf.fullname, CC->user.fullname);
1483 aide_message(notify);
1486 cprintf("%d Ok", CIT_OK);
1488 cprintf(" (%s deleted)", requested_user);
1495 * Check to see if the user who we just sent mail to is logged in. If yes,
1496 * bump the 'new mail' counter for their session. That enables them to
1497 * receive a new mail notification without having to hit the database.
1499 void BumpNewMailCounter(long which_user) {
1500 struct CitContext *ptr;
1502 begin_critical_section(S_SESSION_TABLE);
1504 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1505 if (ptr->user.usernum == which_user) {
1510 end_critical_section(S_SESSION_TABLE);
1515 * Count the number of new mail messages the user has
1519 int num_newmsgs = 0;
1521 num_newmsgs = CC->newmail;
1524 return (num_newmsgs);
1529 * Count the number of new mail messages the user has
1531 int InitialMailCheck()
1533 int num_newmsgs = 0;
1535 char mailboxname[ROOMNAMELEN];
1536 struct ctdlroom mailbox;
1538 struct cdbdata *cdbfr;
1539 long *msglist = NULL;
1542 MailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1543 if (getroom(&mailbox, mailboxname) != 0)
1545 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1547 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1549 if (cdbfr != NULL) {
1550 msglist = mallok(cdbfr->len);
1551 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1552 num_msgs = cdbfr->len / sizeof(long);
1556 for (a = 0; a < num_msgs; ++a) {
1557 if (msglist[a] > 0L) {
1558 if (msglist[a] > vbuf.v_lastseen) {
1563 if (msglist != NULL)
1566 return (num_newmsgs);
1572 * Set the preferred view for the current user/room combination
1574 void cmd_view(char *cmdbuf) {
1578 if (CtdlAccessCheck(ac_logged_in)) {
1582 requested_view = extract_int(cmdbuf, 0);
1584 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1585 vbuf.v_view = requested_view;
1586 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1588 cprintf("%d ok\n", CIT_OK);