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>
45 #include "serv_extensions.h"
46 #include "sysdep_decls.h"
54 #include "citserver.h"
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 for (i=0; i<=len; ++i) {
66 key[i] = tolower(username[i]);
72 * getuser() - retrieve named user into supplied buffer.
73 * returns 0 on success
75 int getuser(struct ctdluser *usbuf, char name[])
78 char usernamekey[USERNAME_SIZE];
79 char sysuser_name[USERNAME_SIZE];
80 struct cdbdata *cdbus;
81 int using_sysuser = 0;
83 memset(usbuf, 0, sizeof(struct ctdluser));
85 #ifdef ENABLE_AUTOLOGIN
86 if (CtdlAssociateSystemUser(sysuser_name, name) == 0) {
92 makeuserkey(usernamekey, sysuser_name);
95 makeuserkey(usernamekey, name);
98 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
99 if (cdbus == NULL) { /* user not found */
102 memcpy(usbuf, cdbus->ptr,
103 ((cdbus->len > sizeof(struct ctdluser)) ?
104 sizeof(struct ctdluser) : cdbus->len));
112 * lgetuser() - same as getuser() but locks the record
114 int lgetuser(struct ctdluser *usbuf, char *name)
118 retcode = getuser(usbuf, name);
120 begin_critical_section(S_USERS);
127 * putuser() - write user buffer into the correct place on disk
129 void putuser(struct ctdluser *usbuf)
131 char usernamekey[USERNAME_SIZE];
133 makeuserkey(usernamekey, usbuf->fullname);
135 usbuf->version = REV_LEVEL;
137 usernamekey, strlen(usernamekey),
138 usbuf, sizeof(struct ctdluser));
144 * lputuser() - same as putuser() but locks the record
146 void lputuser(struct ctdluser *usbuf)
149 end_critical_section(S_USERS);
153 * Index-generating function used by Ctdl[Get|Set]Relationship
155 int GenerateRelationshipIndex(char *IndexBuf,
167 TheIndex.iRoomID = RoomID;
168 TheIndex.iRoomGen = RoomGen;
169 TheIndex.iUserID = UserID;
171 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
172 return (sizeof(TheIndex));
178 * Back end for CtdlSetRelationship()
180 void put_visit(struct visit *newvisit)
185 /* Generate an index */
186 IndexLen = GenerateRelationshipIndex(IndexBuf,
189 newvisit->v_usernum);
191 /* Store the record */
192 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
193 newvisit, sizeof(struct visit)
201 * Define a relationship between a user and a room
203 void CtdlSetRelationship(struct visit *newvisit,
204 struct ctdluser *rel_user,
205 struct ctdlroom *rel_room)
209 /* We don't use these in Citadel because they're implicit by the
210 * index, but they must be present if the database is exported.
212 newvisit->v_roomnum = rel_room->QRnumber;
213 newvisit->v_roomgen = rel_room->QRgen;
214 newvisit->v_usernum = rel_user->usernum;
220 * Locate a relationship between a user and a room
222 void CtdlGetRelationship(struct visit *vbuf,
223 struct ctdluser *rel_user,
224 struct ctdlroom *rel_room)
229 struct cdbdata *cdbvisit;
231 /* Generate an index */
232 IndexLen = GenerateRelationshipIndex(IndexBuf,
237 /* Clear out the buffer */
238 memset(vbuf, 0, sizeof(struct visit));
240 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
241 if (cdbvisit != NULL) {
242 memcpy(vbuf, cdbvisit->ptr,
243 ((cdbvisit->len > sizeof(struct visit)) ?
244 sizeof(struct visit) : cdbvisit->len));
248 /* If this is the first time the user has seen this room,
249 * set the view to be the default for the room.
251 vbuf->v_view = rel_room->QRdefaultview;
254 /* Set v_seen if necessary */
255 if (vbuf->v_seen[0] == 0) {
256 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
261 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
263 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
268 * Is the user currently logged in an Aide?
272 if (CC->user.axlevel >= 6)
280 * Is the user currently logged in an Aide *or* the room aide for this room?
282 int is_room_aide(void)
285 if (!CC->logged_in) {
289 if ((CC->user.axlevel >= 6)
290 || (CC->room.QRroomaide == CC->user.usernum)) {
298 * getuserbynumber() - get user by number
299 * returns 0 if user was found
301 * WARNING: don't use this function unless you absolutely have to. It does
302 * a sequential search and therefore is computationally expensive.
304 int getuserbynumber(struct ctdluser *usbuf, long int number)
306 struct cdbdata *cdbus;
308 cdb_rewind(CDB_USERS);
310 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
311 memset(usbuf, 0, sizeof(struct ctdluser));
312 memcpy(usbuf, cdbus->ptr,
313 ((cdbus->len > sizeof(struct ctdluser)) ?
314 sizeof(struct ctdluser) : cdbus->len));
316 if (usbuf->usernum == number) {
317 cdb_close_cursor(CDB_USERS);
326 * See if we can translate a system login name (i.e. from /etc/passwd)
327 * to a Citadel screen name. Returns 0 if one is found.
329 int CtdlAssociateSystemUser(char *screenname, char *loginname) {
333 p = (struct passwd *) getpwnam(loginname);
335 strcpy(screenname, p->pw_gecos);
336 for (a = 0; a < strlen(screenname); ++a) {
337 if (screenname[a] == ',') {
349 * Back end for cmd_user() and its ilk
351 int CtdlLoginExistingUser(char *trythisname)
355 struct recptypes *valid = 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 /* Did we find something? */
383 if (found_user == 0) {
384 if (((CC->nologin)) && (CC->user.axlevel < 6)) {
385 return login_too_many_users;
387 safestrncpy(CC->curr_user, CC->user.fullname,
388 sizeof CC->curr_user);
392 return login_not_found;
400 void cmd_user(char *cmdbuf)
405 extract(username, cmdbuf, 0);
408 a = CtdlLoginExistingUser(username);
410 case login_already_logged_in:
411 cprintf("%d Already logged in.\n", ERROR);
413 case login_too_many_users:
415 "Too many users are already online "
417 ERROR + MAX_SESSIONS_EXCEEDED,
418 config.c_nodename, config.c_maxsessions);
421 cprintf("%d Password required for %s\n",
422 MORE_DATA, CC->curr_user);
424 case login_not_found:
425 cprintf("%d %s not found.\n", ERROR, username);
427 cprintf("%d Internal error\n", ERROR);
434 * session startup code which is common to both cmd_pass() and cmd_newu()
436 void session_startup(void)
440 syslog(LOG_NOTICE, "session %d: user <%s> logged in",
441 CC->cs_pid, CC->curr_user);
443 lgetuser(&CC->user, CC->curr_user);
444 ++(CC->user.timescalled);
445 CC->previous_login = CC->user.lastcall;
446 time(&CC->user.lastcall);
448 /* If this user's name is the name of the system administrator
449 * (as specified in setup), automatically assign access level 6.
451 if (!strcasecmp(CC->user.fullname, config.c_sysadm)) {
452 CC->user.axlevel = 6;
457 * Populate CC->cs_inet_email with a default address. This will be
458 * overwritten with the user's directory address, if one exists, when
459 * the vCard module's login hook runs.
461 snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
462 CC->user.fullname, config.c_fqdn);
463 for (i=0; i<strlen(CC->cs_inet_email); ++i) {
464 if (isspace(CC->cs_inet_email[i])) {
465 CC->cs_inet_email[i] = '_';
469 /* Create any personal rooms required by the system.
470 * (Technically, MAILROOM should be there already, but just in case...)
472 create_room(MAILROOM, 4, "", 0, 1, 0);
473 create_room(SENTITEMS, 4, "", 0, 1, 0);
475 /* Run any startup routines registered by loadable modules */
476 PerformSessionHooks(EVT_LOGIN);
478 /* Enter the lobby */
479 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
483 void logged_in_response(void)
485 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
486 CIT_OK, CC->user.fullname, CC->user.axlevel,
487 CC->user.timescalled, CC->user.posted,
488 CC->user.flags, CC->user.usernum,
495 * misc things to be taken care of when a user is logged out
497 void logout(struct CitContext *who)
500 * Clear out some session data. Most likely, the CitContext for this
501 * session is about to get nuked when the session disconnects, but
502 * since it's possible to log in again without reconnecting, we cannot
503 * make that assumption.
505 strcpy(who->fake_username, "");
506 strcpy(who->fake_postname, "");
507 strcpy(who->fake_hostname, "");
508 strcpy(who->fake_roomname, "");
512 * If there is a download in progress, abort it.
514 if (who->download_fp != NULL) {
515 fclose(who->download_fp);
516 who->download_fp = NULL;
520 * If there is an upload in progress, abort it.
522 if (who->upload_fp != NULL) {
527 * If we were talking to a network node, we're not anymore...
529 if (strlen(who->net_node) > 0) {
530 network_talking_to(who->net_node, NTT_REMOVE);
533 /* Do modular stuff... */
534 PerformSessionHooks(EVT_LOGOUT);
536 /* Free any output buffers */
537 if (who->output_buffer != NULL) {
544 * an alternate version of validpw() which executes `chkpwd' instead of
545 * verifying the password directly
547 static int validpw(uid_t uid, const char *pass)
550 int status, pipev[2];
554 lprintf(1, "pipe failed (%s): denying autologin access for "
555 "uid %ld\n", strerror(errno), (long)uid);
558 switch (pid = fork()) {
560 lprintf(1, "fork failed (%s): denying autologin access for "
561 "uid %ld\n", strerror(errno), (long)uid);
568 if (dup2(pipev[0], 0) == -1) {
574 execl(BBSDIR "/chkpwd", BBSDIR "/chkpwd", NULL);
575 perror(BBSDIR "/chkpwd");
581 snprintf(buf, sizeof buf, "%lu\n", (unsigned long) uid));
582 write(pipev[1], pass, strlen(pass));
583 write(pipev[1], "\n", 1);
586 while (waitpid(pid, &status, 0) == -1)
587 if (errno != EINTR) {
588 lprintf(1, "waitpid failed (%s): denying autologin "
589 "access for uid %ld\n",
590 strerror(errno), (long)uid);
593 if (WIFEXITED(status) && !WEXITSTATUS(status))
607 int CtdlTryPassword(char *password)
611 if ((CC->logged_in)) {
612 lprintf(5, "CtdlTryPassword: already logged in\n");
613 return pass_already_logged_in;
615 if (!strcmp(CC->curr_user, NLI)) {
616 lprintf(5, "CtdlTryPassword: no user selected\n");
619 if (getuser(&CC->user, CC->curr_user)) {
620 lprintf(5, "CtdlTryPassword: internal error\n");
621 return pass_internal_error;
623 if (password == NULL) {
624 lprintf(5, "CtdlTryPassword: NULL password string supplied\n");
625 return pass_wrong_password;
630 #ifdef ENABLE_AUTOLOGIN
631 /* A uid of BBSUID or -1 indicates that this user exists only in
632 * Citadel, not in the underlying operating system.
634 if ( (CC->user.uid == BBSUID) || (CC->user.uid == (-1)) ) {
636 strproc(CC->user.password);
637 code = strcasecmp(CC->user.password, password);
639 /* Any other uid means we have to check the system password database */
641 if (validpw(CC->user.uid, password)) {
643 lgetuser(&CC->user, CC->curr_user);
644 safestrncpy(CC->user.password, password,
645 sizeof CC->user.password);
650 #else /* ENABLE_AUTOLOGIN */
652 strproc(CC->user.password);
653 code = strcasecmp(CC->user.password, password);
655 #endif /* ENABLE_AUTOLOGIN */
661 lprintf(3, "Bad password specified for <%s>\n", CC->curr_user);
662 return pass_wrong_password;
667 void cmd_pass(char *buf)
672 extract(password, buf, 0);
673 a = CtdlTryPassword(password);
676 case pass_already_logged_in:
677 cprintf("%d Already logged in.\n", ERROR);
680 cprintf("%d You must send a name with USER first.\n",
683 case pass_wrong_password:
684 cprintf("%d Wrong password.\n", ERROR);
687 logged_in_response();
689 cprintf("%d Can't find user record!\n",
690 ERROR + INTERNAL_ERROR);
697 * Delete a user record *and* all of its related resources.
699 int purge_user(char pname[])
702 struct ctdluser usbuf;
703 char usernamekey[USERNAME_SIZE];
704 struct CitContext *ccptr;
705 int user_is_logged_in = 0;
707 makeuserkey(usernamekey, pname);
709 if (getuser(&usbuf, pname) != 0) {
710 lprintf(5, "Cannot purge user <%s> - not found\n", pname);
711 return (ERROR + NO_SUCH_USER);
713 /* Don't delete a user who is currently logged in. Instead, just
714 * set the access level to 0, and let the account get swept up
715 * during the next purge.
717 user_is_logged_in = 0;
718 begin_critical_section(S_SESSION_TABLE);
719 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
720 if (ccptr->user.usernum == usbuf.usernum) {
721 user_is_logged_in = 1;
724 end_critical_section(S_SESSION_TABLE);
725 if (user_is_logged_in == 1) {
726 lprintf(5, "User <%s> is logged in; not deleting.\n", pname);
731 lprintf(5, "Deleting user <%s>\n", pname);
733 /* Perform any purge functions registered by server extensions */
734 PerformUserHooks(usbuf.fullname, usbuf.usernum, EVT_PURGEUSER);
736 /* delete any existing user/room relationships */
737 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
739 /* delete the userlog entry */
740 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
742 /* remove the user's bio file */
743 snprintf(filename, sizeof filename, "./bio/%ld", usbuf.usernum);
746 /* remove the user's picture */
747 snprintf(filename, sizeof filename, "./userpics/%ld.gif", usbuf.usernum);
755 * create_user() - back end processing to create a new user
757 * Set 'newusername' to the desired account name.
758 * Set 'become_user' to nonzero if this is self-service account creation and we want
759 * to actually log in as the user we just created, otherwise set it to 0.
761 int create_user(char *newusername, int become_user)
763 struct ctdluser usbuf;
764 struct ctdlroom qrbuf;
765 struct passwd *p = NULL;
767 char mailboxname[ROOMNAMELEN];
770 safestrncpy(username, newusername, sizeof username);
773 #ifdef ENABLE_AUTOLOGIN
774 p = (struct passwd *) getpwnam(username);
776 extract_token(username, p->pw_gecos, 0, ',');
785 if (!getuser(&usbuf, username)) {
786 return (ERROR + ALREADY_EXISTS);
789 /* Go ahead and initialize a new user record */
790 memset(&usbuf, 0, sizeof(struct ctdluser));
791 safestrncpy(usbuf.fullname, username, sizeof usbuf.fullname);
792 strcpy(usbuf.password, "");
795 /* These are the default flags on new accounts */
796 usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
798 usbuf.timescalled = 0;
800 usbuf.axlevel = config.c_initax;
801 usbuf.USscreenwidth = 80;
802 usbuf.USscreenheight = 24;
803 usbuf.lastcall = time(NULL);
805 /* fetch a new user number */
806 usbuf.usernum = get_new_user_number();
808 /* The very first user created on the system will always be an Aide */
809 if (usbuf.usernum == 1L) {
813 /* add user to userlog */
817 * Give the user a private mailbox and a configuration room.
818 * Make the latter an invisible system room.
820 MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
821 create_room(mailboxname, 5, "", 0, 1, 1);
823 MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
824 create_room(mailboxname, 5, "", 0, 1, 1);
825 if (lgetroom(&qrbuf, mailboxname) == 0) {
826 qrbuf.QRflags2 |= QR2_SYSTEM;
830 /* Everything below this line can be bypassed if administratively
831 creating a user, instead of doing self-service account creation
835 /* Now become the user we just created */
836 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
837 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
840 /* Check to make sure we're still who we think we are */
841 if (getuser(&CC->user, CC->curr_user)) {
842 return (ERROR + INTERNAL_ERROR);
846 lprintf(3, "New user <%s> created\n", username);
854 * cmd_newu() - create a new user account and log in as that user
856 void cmd_newu(char *cmdbuf)
861 if (config.c_disable_newu) {
862 cprintf("%d Self-service user account creation "
863 "is disabled on this system.\n", ERROR);
868 cprintf("%d Already logged in.\n", ERROR);
872 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
873 ERROR + MAX_SESSIONS_EXCEEDED,
874 config.c_nodename, config.c_maxsessions);
876 extract(username, cmdbuf, 0);
880 if (strlen(username) == 0) {
881 cprintf("%d You must supply a user name.\n", ERROR);
885 if ((!strcasecmp(username, "bbs")) ||
886 (!strcasecmp(username, "new")) ||
887 (!strcasecmp(username, "."))) {
888 cprintf("%d '%s' is an invalid login name.\n", ERROR, username);
892 a = create_user(username, 1);
896 logged_in_response();
897 } else if (a == ERROR + ALREADY_EXISTS) {
898 cprintf("%d '%s' already exists.\n",
899 ERROR + ALREADY_EXISTS, username);
901 } else if (a == ERROR + INTERNAL_ERROR) {
902 cprintf("%d Internal error - user record disappeared?\n",
903 ERROR + INTERNAL_ERROR);
906 cprintf("%d unknown error\n", ERROR);
915 void cmd_setp(char *new_pw)
917 if (CtdlAccessCheck(ac_logged_in)) {
920 if ( (CC->user.uid != BBSUID) && (CC->user.uid != (-1)) ) {
921 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR);
925 if (strlen(new_pw) == 0) {
926 cprintf("%d Password unchanged.\n", CIT_OK);
929 lgetuser(&CC->user, CC->curr_user);
930 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
932 cprintf("%d Password changed.\n", CIT_OK);
933 lprintf(3, "Password changed for user <%s>\n", CC->curr_user);
934 PerformSessionHooks(EVT_SETPASS);
939 * cmd_creu() - administratively create a new user account (do not log in to it)
941 void cmd_creu(char *cmdbuf)
946 if (CtdlAccessCheck(ac_aide)) {
950 extract(username, cmdbuf, 0);
954 if (strlen(username) == 0) {
955 cprintf("%d You must supply a user name.\n", ERROR);
959 a = create_user(username, 0);
962 cprintf("%d ok\n", CIT_OK);
964 } else if (a == ERROR + ALREADY_EXISTS) {
965 cprintf("%d '%s' already exists.\n",
966 ERROR + ALREADY_EXISTS, username);
969 cprintf("%d An error occured creating the user account.\n", ERROR);
976 * get user parameters
981 if (CtdlAccessCheck(ac_logged_in))
984 getuser(&CC->user, CC->curr_user);
985 cprintf("%d %d|%d|%d|\n",
987 CC->user.USscreenwidth,
988 CC->user.USscreenheight,
989 (CC->user.flags & US_USER_SET)
994 * set user parameters
996 void cmd_setu(char *new_parms)
998 if (CtdlAccessCheck(ac_logged_in))
1001 if (num_parms(new_parms) < 3) {
1002 cprintf("%d Usage error.\n", ERROR);
1005 lgetuser(&CC->user, CC->curr_user);
1006 CC->user.USscreenwidth = extract_int(new_parms, 0);
1007 CC->user.USscreenheight = extract_int(new_parms, 1);
1008 CC->user.flags = CC->user.flags & (~US_USER_SET);
1009 CC->user.flags = CC->user.flags |
1010 (extract_int(new_parms, 2) & US_USER_SET);
1012 lputuser(&CC->user);
1013 cprintf("%d Ok\n", CIT_OK);
1017 * set last read pointer
1019 void cmd_slrp(char *new_ptr)
1023 struct visit original_vbuf;
1025 if (CtdlAccessCheck(ac_logged_in)) {
1029 if (!strncasecmp(new_ptr, "highest", 7)) {
1030 newlr = CC->room.QRhighest;
1032 newlr = atol(new_ptr);
1035 lgetuser(&CC->user, CC->curr_user);
1037 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1038 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1039 vbuf.v_lastseen = newlr;
1040 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1042 /* Only rewrite the record if it changed */
1043 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1044 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1045 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1048 lputuser(&CC->user);
1049 cprintf("%d %ld\n", CIT_OK, newlr);
1053 void cmd_seen(char *argbuf) {
1054 long target_msgnum = 0L;
1055 int target_setting = 0;
1057 if (CtdlAccessCheck(ac_logged_in)) {
1061 if (num_parms(argbuf) != 2) {
1062 cprintf("%d Invalid parameters\n", ERROR);
1066 target_msgnum = extract_long(argbuf, 0);
1067 target_setting = extract_int(argbuf, 1);
1069 CtdlSetSeen(target_msgnum, target_setting, ctdlsetseen_seen);
1070 cprintf("%d OK\n", CIT_OK);
1074 void cmd_gtsn(char *argbuf) {
1077 if (CtdlAccessCheck(ac_logged_in)) {
1081 CtdlGetSeen(buf, ctdlsetseen_seen);
1082 cprintf("%d %s\n", CIT_OK, buf);
1088 * INVT and KICK commands
1090 void cmd_invt_kick(char *iuser, int op)
1092 { /* 1 = invite, 0 = kick out */
1093 struct ctdluser USscratch;
1098 * These commands are only allowed by aides, room aides,
1099 * and room namespace owners
1102 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1103 /* access granted */
1106 cprintf("%d Higher access or room ownership required.\n",
1107 ERROR + HIGHER_ACCESS_REQUIRED);
1111 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1113 cprintf("%d Can't add/remove users from this room.\n",
1118 if (lgetuser(&USscratch, iuser) != 0) {
1119 cprintf("%d No such user.\n", ERROR);
1122 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1125 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1126 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1129 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1130 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1132 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1134 lputuser(&USscratch);
1136 /* post a message in Aide> saying what we just did */
1137 snprintf(bbb, sizeof bbb, "%s %s %s> by %s\n",
1139 ((op == 1) ? "invited to" : "kicked out of"),
1144 cprintf("%d %s %s %s.\n",
1146 ((op == 1) ? "invited to" : "kicked out of"),
1153 * Forget (Zap) the current room (API call)
1154 * Returns 0 on success
1156 int CtdlForgetThisRoom(void) {
1159 /* On some systems, Aides are not allowed to forget rooms */
1160 if (is_aide() && (config.c_aide_zap == 0)
1161 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1165 lgetuser(&CC->user, CC->curr_user);
1166 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1168 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1169 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1171 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1172 lputuser(&CC->user);
1174 /* Return to the Lobby, so we don't end up in an undefined room */
1175 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1182 * forget (Zap) the current room
1187 if (CtdlAccessCheck(ac_logged_in)) {
1191 if (CtdlForgetThisRoom() == 0) {
1192 cprintf("%d Ok\n", CIT_OK);
1195 cprintf("%d You may not forget this room.\n", ERROR);
1200 * Get Next Unregistered User
1204 struct cdbdata *cdbus;
1205 struct ctdluser usbuf;
1207 if (CtdlAccessCheck(ac_aide)) {
1211 if ((CitControl.MMflags & MM_VALID) == 0) {
1212 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1216 /* There are unvalidated users. Traverse the user database,
1217 * and return the first user we find that needs validation.
1219 cdb_rewind(CDB_USERS);
1220 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1221 memset(&usbuf, 0, sizeof(struct ctdluser));
1222 memcpy(&usbuf, cdbus->ptr,
1223 ((cdbus->len > sizeof(struct ctdluser)) ?
1224 sizeof(struct ctdluser) : cdbus->len));
1226 if ((usbuf.flags & US_NEEDVALID)
1227 && (usbuf.axlevel > 0)) {
1228 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1229 cdb_close_cursor(CDB_USERS);
1234 /* If we get to this point, there are no more unvalidated users.
1235 * Therefore we clear the "users need validation" flag.
1238 begin_critical_section(S_CONTROL);
1240 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1242 end_critical_section(S_CONTROL);
1243 cprintf("%d *** End of registration.\n", CIT_OK);
1252 void cmd_vali(char *v_args)
1256 struct ctdluser userbuf;
1258 extract(user, v_args, 0);
1259 newax = extract_int(v_args, 1);
1261 if (CtdlAccessCheck(ac_aide)) {
1265 if (lgetuser(&userbuf, user) != 0) {
1266 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1270 userbuf.axlevel = newax;
1271 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1275 /* If the access level was set to zero, delete the user */
1277 if (purge_user(user) == 0) {
1278 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1282 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1288 * Traverse the user file...
1290 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1293 struct ctdluser usbuf;
1294 struct cdbdata *cdbus;
1296 cdb_rewind(CDB_USERS);
1298 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1299 memset(&usbuf, 0, sizeof(struct ctdluser));
1300 memcpy(&usbuf, cdbus->ptr,
1301 ((cdbus->len > sizeof(struct ctdluser)) ?
1302 sizeof(struct ctdluser) : cdbus->len));
1304 (*CallBack) (&usbuf, in_data);
1310 * List one user (this works with cmd_list)
1312 void ListThisUser(struct ctdluser *usbuf, void *data)
1314 if (usbuf->axlevel > 0) {
1315 if ((CC->user.axlevel >= 6)
1316 || ((usbuf->flags & US_UNLISTED) == 0)
1317 || ((CC->internal_pgm))) {
1318 cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1322 (long)usbuf->lastcall,
1325 if (CC->user.axlevel >= 6)
1326 cprintf("%s", usbuf->password);
1337 cprintf("%d \n", LISTING_FOLLOWS);
1338 ForEachUser(ListThisUser, NULL);
1346 * assorted info we need to check at login
1354 if (CtdlAccessCheck(ac_logged_in)) {
1358 getuser(&CC->user, CC->curr_user); /* no lock is needed here */
1359 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1362 if (CC->user.axlevel >= 6) {
1364 if (CitControl.MMflags & MM_VALID)
1368 /* check for mail */
1369 mail = InitialMailCheck();
1371 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1376 * check to see if a user exists
1378 void cmd_qusr(char *who)
1380 struct ctdluser usbuf;
1382 if (getuser(&usbuf, who) == 0) {
1383 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1385 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1391 * Administrative Get User Parameters
1393 void cmd_agup(char *cmdbuf)
1395 struct ctdluser usbuf;
1396 char requested_user[SIZ];
1398 if (CtdlAccessCheck(ac_aide)) {
1402 extract(requested_user, cmdbuf, 0);
1403 if (getuser(&usbuf, requested_user) != 0) {
1404 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1407 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1414 (int) usbuf.axlevel,
1416 (long)usbuf.lastcall,
1423 * Administrative Set User Parameters
1425 void cmd_asup(char *cmdbuf)
1427 struct ctdluser usbuf;
1428 char requested_user[SIZ];
1434 if (CtdlAccessCheck(ac_aide))
1437 extract(requested_user, cmdbuf, 0);
1438 if (lgetuser(&usbuf, requested_user) != 0) {
1439 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1442 np = num_parms(cmdbuf);
1444 extract(usbuf.password, cmdbuf, 1);
1446 usbuf.flags = extract_int(cmdbuf, 2);
1448 usbuf.timescalled = extract_int(cmdbuf, 3);
1450 usbuf.posted = extract_int(cmdbuf, 4);
1452 newax = extract_int(cmdbuf, 5);
1453 if ((newax >= 0) && (newax <= 6)) {
1454 usbuf.axlevel = extract_int(cmdbuf, 5);
1458 usbuf.lastcall = extract_long(cmdbuf, 7);
1461 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1464 if (usbuf.axlevel == 0) {
1465 if (purge_user(requested_user) == 0) {
1471 sprintf(notify, "User <%s> deleted by %s\n",
1472 usbuf.fullname, CC->user.fullname);
1473 aide_message(notify);
1476 cprintf("%d Ok", CIT_OK);
1478 cprintf(" (%s deleted)", requested_user);
1485 * Check to see if the user who we just sent mail to is logged in. If yes,
1486 * bump the 'new mail' counter for their session. That enables them to
1487 * receive a new mail notification without having to hit the database.
1489 void BumpNewMailCounter(long which_user) {
1490 struct CitContext *ptr;
1492 begin_critical_section(S_SESSION_TABLE);
1494 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1495 if (ptr->user.usernum == which_user) {
1500 end_critical_section(S_SESSION_TABLE);
1505 * Count the number of new mail messages the user has
1509 int num_newmsgs = 0;
1511 num_newmsgs = CC->newmail;
1514 return (num_newmsgs);
1519 * Count the number of new mail messages the user has
1521 int InitialMailCheck()
1523 int num_newmsgs = 0;
1525 char mailboxname[ROOMNAMELEN];
1526 struct ctdlroom mailbox;
1528 struct cdbdata *cdbfr;
1529 long *msglist = NULL;
1532 MailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1533 if (getroom(&mailbox, mailboxname) != 0)
1535 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1537 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1539 if (cdbfr != NULL) {
1540 msglist = mallok(cdbfr->len);
1541 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1542 num_msgs = cdbfr->len / sizeof(long);
1546 for (a = 0; a < num_msgs; ++a) {
1547 if (msglist[a] > 0L) {
1548 if (msglist[a] > vbuf.v_lastseen) {
1553 if (msglist != NULL)
1556 return (num_newmsgs);
1562 * Set the preferred view for the current user/room combination
1564 void cmd_view(char *cmdbuf) {
1568 if (CtdlAccessCheck(ac_logged_in)) {
1572 requested_view = extract_int(cmdbuf, 0);
1574 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1575 vbuf.v_view = requested_view;
1576 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1578 cprintf("%d ok\n", CIT_OK);