4 * Server functions which perform operations on user objects.
17 #include <sys/types.h>
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
25 # include <sys/time.h>
40 #include "serv_extensions.h"
41 #include "sysdep_decls.h"
49 #include "citserver.h"
53 * makeuserkey() - convert a username into the format used as a database key
54 * (it's just the username converted into lower case)
56 static INLINE void makeuserkey(char *key, char *username) {
59 len = strlen(username);
60 for (i=0; i<=len; ++i) {
61 key[i] = tolower(username[i]);
67 * getuser() - retrieve named user into supplied buffer.
68 * returns 0 on success
70 int getuser(struct ctdluser *usbuf, char name[])
73 char usernamekey[USERNAME_SIZE];
74 struct cdbdata *cdbus;
77 memset(usbuf, 0, sizeof(struct ctdluser));
80 makeuserkey(usernamekey, name);
81 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
83 if (cdbus == NULL) { /* user not found */
87 memcpy(usbuf, cdbus->ptr,
88 ((cdbus->len > sizeof(struct ctdluser)) ?
89 sizeof(struct ctdluser) : cdbus->len));
98 * lgetuser() - same as getuser() but locks the record
100 int lgetuser(struct ctdluser *usbuf, char *name)
104 retcode = getuser(usbuf, name);
106 begin_critical_section(S_USERS);
113 * putuser() - write user buffer into the correct place on disk
115 void putuser(struct ctdluser *usbuf)
117 char usernamekey[USERNAME_SIZE];
119 makeuserkey(usernamekey, usbuf->fullname);
121 usbuf->version = REV_LEVEL;
123 usernamekey, strlen(usernamekey),
124 usbuf, sizeof(struct ctdluser));
130 * lputuser() - same as putuser() but locks the record
132 void lputuser(struct ctdluser *usbuf)
135 end_critical_section(S_USERS);
139 * Index-generating function used by Ctdl[Get|Set]Relationship
141 int GenerateRelationshipIndex(char *IndexBuf,
153 TheIndex.iRoomID = RoomID;
154 TheIndex.iRoomGen = RoomGen;
155 TheIndex.iUserID = UserID;
157 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
158 return (sizeof(TheIndex));
164 * Back end for CtdlSetRelationship()
166 void put_visit(struct visit *newvisit)
171 /* Generate an index */
172 IndexLen = GenerateRelationshipIndex(IndexBuf,
175 newvisit->v_usernum);
177 /* Store the record */
178 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
179 newvisit, sizeof(struct visit)
187 * Define a relationship between a user and a room
189 void CtdlSetRelationship(struct visit *newvisit,
190 struct ctdluser *rel_user,
191 struct ctdlroom *rel_room)
195 /* We don't use these in Citadel because they're implicit by the
196 * index, but they must be present if the database is exported.
198 newvisit->v_roomnum = rel_room->QRnumber;
199 newvisit->v_roomgen = rel_room->QRgen;
200 newvisit->v_usernum = rel_user->usernum;
206 * Locate a relationship between a user and a room
208 void CtdlGetRelationship(struct visit *vbuf,
209 struct ctdluser *rel_user,
210 struct ctdlroom *rel_room)
215 struct cdbdata *cdbvisit;
217 /* Generate an index */
218 IndexLen = GenerateRelationshipIndex(IndexBuf,
223 /* Clear out the buffer */
224 memset(vbuf, 0, sizeof(struct visit));
226 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
227 if (cdbvisit != NULL) {
228 memcpy(vbuf, cdbvisit->ptr,
229 ((cdbvisit->len > sizeof(struct visit)) ?
230 sizeof(struct visit) : cdbvisit->len));
234 /* If this is the first time the user has seen this room,
235 * set the view to be the default for the room.
237 vbuf->v_view = rel_room->QRdefaultview;
240 /* Set v_seen if necessary */
241 if (vbuf->v_seen[0] == 0) {
242 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
247 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
249 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
254 * Is the user currently logged in an Aide?
258 if (CC->user.axlevel >= 6)
266 * Is the user currently logged in an Aide *or* the room aide for this room?
268 int is_room_aide(void)
271 if (!CC->logged_in) {
275 if ((CC->user.axlevel >= 6)
276 || (CC->room.QRroomaide == CC->user.usernum)) {
284 * getuserbynumber() - get user by number
285 * returns 0 if user was found
287 * WARNING: don't use this function unless you absolutely have to. It does
288 * a sequential search and therefore is computationally expensive.
290 int getuserbynumber(struct ctdluser *usbuf, long int number)
292 struct cdbdata *cdbus;
294 cdb_rewind(CDB_USERS);
296 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
297 memset(usbuf, 0, sizeof(struct ctdluser));
298 memcpy(usbuf, cdbus->ptr,
299 ((cdbus->len > sizeof(struct ctdluser)) ?
300 sizeof(struct ctdluser) : cdbus->len));
302 if (usbuf->usernum == number) {
303 cdb_close_cursor(CDB_USERS);
311 #ifdef ENABLE_AUTOLOGIN
313 * getuserbyuid() - get user by system uid (for PAM mode authentication)
314 * returns 0 if user was found
316 * WARNING: don't use this function unless you absolutely have to. It does
317 * a sequential search and therefore is computationally expensive.
319 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
321 struct cdbdata *cdbus;
323 cdb_rewind(CDB_USERS);
325 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
326 memset(usbuf, 0, sizeof(struct ctdluser));
327 memcpy(usbuf, cdbus->ptr,
328 ((cdbus->len > sizeof(struct ctdluser)) ?
329 sizeof(struct ctdluser) : cdbus->len));
331 if (usbuf->uid == number) {
332 cdb_close_cursor(CDB_USERS);
338 #endif /* ENABLE_AUTOLOGIN */
343 * Back end for cmd_user() and its ilk
345 int CtdlLoginExistingUser(char *trythisname)
350 if ((CC->logged_in)) {
351 return login_already_logged_in;
354 if (trythisname == NULL) return login_not_found;
355 safestrncpy(username, trythisname, USERNAME_SIZE);
358 #ifdef ENABLE_AUTOLOGIN
360 /* If this is an autologin build, the only valid auth source is the
361 * host operating system.
364 struct passwd *tempPwdPtr;
367 lprintf(CTDL_DEBUG, "asking host about <%s>\n", username);
368 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
369 if (tempPwdPtr == NULL) {
370 return login_not_found;
372 lprintf(CTDL_DEBUG, "found it! uid=%d, gecos=%s\n", pd.pw_uid, pd.pw_gecos);
374 /* Locate the associated Citadel account.
375 * If not found, make one attempt to create it.
377 found_user = getuserbyuid(&CC->user, pd.pw_uid);
378 if (found_user != 0) {
379 create_user(username, 0);
380 found_user = getuserbyuid(&CC->user, pd.pw_uid);
383 #else /* ENABLE_AUTOLOGIN */
384 struct recptypes *valid = NULL;
386 /* First, try to log in as if the supplied name is a display name */
387 found_user = getuser(&CC->user, username);
389 /* If that didn't work, try to log in as if the supplied name
390 * is an e-mail address
392 if (found_user != 0) {
393 valid = validate_recipients(username);
395 if (valid->num_local == 1) {
396 found_user = getuser(&CC->user,
403 #endif /* ENABLE_AUTOLOGIN */
405 /* Did we find something? */
406 if (found_user == 0) {
407 if (((CC->nologin)) && (CC->user.axlevel < 6)) {
408 return login_too_many_users;
410 safestrncpy(CC->curr_user, CC->user.fullname,
411 sizeof CC->curr_user);
415 return login_not_found;
423 void cmd_user(char *cmdbuf)
428 extract_token(username, cmdbuf, 0, '|', sizeof username);
431 a = CtdlLoginExistingUser(username);
433 case login_already_logged_in:
434 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
436 case login_too_many_users:
438 "Too many users are already online "
440 ERROR + MAX_SESSIONS_EXCEEDED,
441 config.c_nodename, config.c_maxsessions);
444 cprintf("%d Password required for %s\n",
445 MORE_DATA, CC->curr_user);
447 case login_not_found:
448 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
451 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
458 * session startup code which is common to both cmd_pass() and cmd_newu()
460 void session_startup(void)
464 lprintf(CTDL_NOTICE, "<%s> logged in\n", CC->curr_user);
466 lgetuser(&CC->user, CC->curr_user);
467 ++(CC->user.timescalled);
468 CC->previous_login = CC->user.lastcall;
469 time(&CC->user.lastcall);
471 /* If this user's name is the name of the system administrator
472 * (as specified in setup), automatically assign access level 6.
474 if (!strcasecmp(CC->user.fullname, config.c_sysadm)) {
475 CC->user.axlevel = 6;
478 #ifdef ENABLE_AUTOLOGIN
479 /* If we're authenticating off the host system, automatically give
480 * root the highest level of access.
482 if (CC->user.uid == 0) {
483 CC->user.axlevel = 6;
490 * Populate CC->cs_inet_email with a default address. This will be
491 * overwritten with the user's directory address, if one exists, when
492 * the vCard module's login hook runs.
494 snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
495 CC->user.fullname, config.c_fqdn);
496 for (i=0; i<strlen(CC->cs_inet_email); ++i) {
497 if (isspace(CC->cs_inet_email[i])) {
498 CC->cs_inet_email[i] = '_';
502 /* Create any personal rooms required by the system.
503 * (Technically, MAILROOM should be there already, but just in case...)
505 create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
506 create_room(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
507 create_room(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
509 /* Run any startup routines registered by loadable modules */
510 PerformSessionHooks(EVT_LOGIN);
512 /* Enter the lobby */
513 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
517 void logged_in_response(void)
519 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
520 CIT_OK, CC->user.fullname, CC->user.axlevel,
521 CC->user.timescalled, CC->user.posted,
522 CC->user.flags, CC->user.usernum,
529 * misc things to be taken care of when a user is logged out
531 void logout(struct CitContext *who)
534 * Clear out some session data. Most likely, the CitContext for this
535 * session is about to get nuked when the session disconnects, but
536 * since it's possible to log in again without reconnecting, we cannot
537 * make that assumption.
539 strcpy(who->fake_username, "");
540 strcpy(who->fake_postname, "");
541 strcpy(who->fake_hostname, "");
542 strcpy(who->fake_roomname, "");
546 * If there is a download in progress, abort it.
548 if (who->download_fp != NULL) {
549 fclose(who->download_fp);
550 who->download_fp = NULL;
554 * If there is an upload in progress, abort it.
556 if (who->upload_fp != NULL) {
561 * If we were talking to a network node, we're not anymore...
563 if (strlen(who->net_node) > 0) {
564 network_talking_to(who->net_node, NTT_REMOVE);
567 /* Do modular stuff... */
568 PerformSessionHooks(EVT_LOGOUT);
570 /* Free any output buffers */
571 if (who->output_buffer != NULL) {
578 * an alternate version of validpw() which executes `chkpwd' instead of
579 * verifying the password directly
581 static int validpw(uid_t uid, const char *pass)
584 int status, pipev[2];
588 lprintf(CTDL_ERR, "pipe failed (%s): denying autologin access for "
589 "uid %ld\n", strerror(errno), (long)uid);
592 switch (pid = fork()) {
594 lprintf(CTDL_ERR, "fork failed (%s): denying autologin access for "
595 "uid %ld\n", strerror(errno), (long)uid);
602 if (dup2(pipev[0], 0) == -1) {
608 execl(CTDLDIR "/chkpwd", CTDLDIR "/chkpwd", NULL);
609 perror(CTDLDIR "/chkpwd");
615 snprintf(buf, sizeof buf, "%lu\n", (unsigned long) uid));
616 write(pipev[1], pass, strlen(pass));
617 write(pipev[1], "\n", 1);
620 while (waitpid(pid, &status, 0) == -1)
621 if (errno != EINTR) {
622 lprintf(CTDL_ERR, "waitpid failed (%s): denying autologin "
623 "access for uid %ld\n",
624 strerror(errno), (long)uid);
627 if (WIFEXITED(status) && !WEXITSTATUS(status))
641 int CtdlTryPassword(char *password)
645 if ((CC->logged_in)) {
646 lprintf(CTDL_WARNING, "CtdlTryPassword: already logged in\n");
647 return pass_already_logged_in;
649 if (!strcmp(CC->curr_user, NLI)) {
650 lprintf(CTDL_WARNING, "CtdlTryPassword: no user selected\n");
653 if (getuser(&CC->user, CC->curr_user)) {
654 lprintf(CTDL_ERR, "CtdlTryPassword: internal error\n");
655 return pass_internal_error;
657 if (password == NULL) {
658 lprintf(CTDL_INFO, "CtdlTryPassword: NULL password string supplied\n");
659 return pass_wrong_password;
664 #ifdef ENABLE_AUTOLOGIN
666 if (validpw(CC->user.uid, password)) {
668 /* we could get rid of this */
669 lgetuser(&CC->user, CC->curr_user);
670 safestrncpy(CC->user.password, password, sizeof CC->user.password);
678 #else /* ENABLE_AUTOLOGIN */
681 strproc(CC->user.password);
682 code = strcasecmp(CC->user.password, password);
684 strproc(CC->user.password);
685 code = strcasecmp(CC->user.password, password);
687 #endif /* ENABLE_AUTOLOGIN */
693 lprintf(CTDL_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
694 return pass_wrong_password;
699 void cmd_pass(char *buf)
704 extract_token(password, buf, 0, '|', sizeof password);
705 a = CtdlTryPassword(password);
708 case pass_already_logged_in:
709 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
712 cprintf("%d You must send a name with USER first.\n",
713 ERROR + USERNAME_REQUIRED);
715 case pass_wrong_password:
716 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
719 logged_in_response();
727 * Delete a user record *and* all of its related resources.
729 int purge_user(char pname[])
732 struct ctdluser usbuf;
733 char usernamekey[USERNAME_SIZE];
734 struct CitContext *ccptr;
735 int user_is_logged_in = 0;
737 makeuserkey(usernamekey, pname);
739 if (getuser(&usbuf, pname) != 0) {
740 lprintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
741 return (ERROR + NO_SUCH_USER);
743 /* Don't delete a user who is currently logged in. Instead, just
744 * set the access level to 0, and let the account get swept up
745 * during the next purge.
747 user_is_logged_in = 0;
748 begin_critical_section(S_SESSION_TABLE);
749 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
750 if (ccptr->user.usernum == usbuf.usernum) {
751 user_is_logged_in = 1;
754 end_critical_section(S_SESSION_TABLE);
755 if (user_is_logged_in == 1) {
756 lprintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
761 lprintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
763 /* Perform any purge functions registered by server extensions */
764 PerformUserHooks(&usbuf, EVT_PURGEUSER);
766 /* delete any existing user/room relationships */
767 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
769 /* delete the userlog entry */
770 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
772 /* remove the user's bio file */
780 /* remove the user's picture */
793 * create_user() - back end processing to create a new user
795 * Set 'newusername' to the desired account name.
796 * Set 'become_user' to nonzero if this is self-service account creation and we want
797 * to actually log in as the user we just created, otherwise set it to 0.
799 int create_user(char *newusername, int become_user)
801 struct ctdluser usbuf;
802 struct ctdlroom qrbuf;
804 char mailboxname[ROOMNAMELEN];
807 safestrncpy(username, newusername, sizeof username);
810 #ifdef ENABLE_AUTOLOGIN
812 struct passwd *tempPwdPtr;
815 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
816 if (tempPwdPtr != NULL) {
817 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
821 return (ERROR + NO_SUCH_USER);
825 if (!getuser(&usbuf, username)) {
826 return (ERROR + ALREADY_EXISTS);
829 /* Go ahead and initialize a new user record */
830 memset(&usbuf, 0, sizeof(struct ctdluser));
831 safestrncpy(usbuf.fullname, username, sizeof usbuf.fullname);
832 strcpy(usbuf.password, "");
835 /* These are the default flags on new accounts */
836 usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
838 usbuf.timescalled = 0;
840 usbuf.axlevel = config.c_initax;
841 usbuf.USscreenwidth = 80;
842 usbuf.USscreenheight = 24;
843 usbuf.lastcall = time(NULL);
845 /* fetch a new user number */
846 usbuf.usernum = get_new_user_number();
848 /* The very first user created on the system will always be an Aide */
849 if (usbuf.usernum == 1L) {
853 /* add user to userlog */
857 * Give the user a private mailbox and a configuration room.
858 * Make the latter an invisible system room.
860 MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
861 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
863 MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
864 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
865 if (lgetroom(&qrbuf, mailboxname) == 0) {
866 qrbuf.QRflags2 |= QR2_SYSTEM;
870 /* Perform any create functions registered by server extensions */
871 PerformUserHooks(&usbuf, EVT_NEWUSER);
873 /* Everything below this line can be bypassed if administratively
874 * creating a user, instead of doing self-service account creation
878 /* Now become the user we just created */
879 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
880 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
883 /* Check to make sure we're still who we think we are */
884 if (getuser(&CC->user, CC->curr_user)) {
885 return (ERROR + INTERNAL_ERROR);
889 lprintf(CTDL_NOTICE, "New user <%s> created\n", username);
897 * cmd_newu() - create a new user account and log in as that user
899 void cmd_newu(char *cmdbuf)
904 #ifdef ENABLE_AUTOLOGIN
905 cprintf("%d This system does not use native mode authentication.\n",
908 #endif /* ENABLE_AUTOLOGIN */
910 if (config.c_disable_newu) {
911 cprintf("%d Self-service user account creation "
912 "is disabled on this system.\n", ERROR + NOT_HERE);
917 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
921 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
922 ERROR + MAX_SESSIONS_EXCEEDED,
923 config.c_nodename, config.c_maxsessions);
925 extract_token(username, cmdbuf, 0, '|', sizeof username);
929 if (strlen(username) == 0) {
930 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
934 if ((!strcasecmp(username, "bbs")) ||
935 (!strcasecmp(username, "new")) ||
936 (!strcasecmp(username, "."))) {
937 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
941 a = create_user(username, 1);
945 logged_in_response();
946 } else if (a == ERROR + ALREADY_EXISTS) {
947 cprintf("%d '%s' already exists.\n",
948 ERROR + ALREADY_EXISTS, username);
950 } else if (a == ERROR + INTERNAL_ERROR) {
951 cprintf("%d Internal error - user record disappeared?\n",
952 ERROR + INTERNAL_ERROR);
955 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
964 void cmd_setp(char *new_pw)
966 if (CtdlAccessCheck(ac_logged_in)) {
969 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
970 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
974 if (strlen(new_pw) == 0) {
975 cprintf("%d Password unchanged.\n", CIT_OK);
978 lgetuser(&CC->user, CC->curr_user);
979 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
981 cprintf("%d Password changed.\n", CIT_OK);
982 lprintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
983 PerformSessionHooks(EVT_SETPASS);
988 * cmd_creu() - administratively create a new user account (do not log in to it)
990 void cmd_creu(char *cmdbuf)
997 if (CtdlAccessCheck(ac_aide)) {
1001 extract_token(username, cmdbuf, 0, '|', sizeof username);
1002 extract_token(password, cmdbuf, 1, '|', sizeof password);
1008 if (strlen(username) == 0) {
1009 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1013 a = create_user(username, 0);
1016 if (strlen(password) > 0) {
1017 lgetuser(&tmp, username);
1018 safestrncpy(tmp.password, password, sizeof(tmp.password));
1021 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1022 (strlen(password) > 0) ? "and password set" :
1023 "with no password");
1025 } else if (a == ERROR + ALREADY_EXISTS) {
1026 cprintf("%d '%s' already exists.\n",
1027 ERROR + ALREADY_EXISTS, username);
1030 cprintf("%d An error occured creating the user account.\n", ERROR + INTERNAL_ERROR);
1037 * get user parameters
1042 if (CtdlAccessCheck(ac_logged_in))
1045 getuser(&CC->user, CC->curr_user);
1046 cprintf("%d %d|%d|%d|\n",
1048 CC->user.USscreenwidth,
1049 CC->user.USscreenheight,
1050 (CC->user.flags & US_USER_SET)
1055 * set user parameters
1057 void cmd_setu(char *new_parms)
1059 if (CtdlAccessCheck(ac_logged_in))
1062 if (num_parms(new_parms) < 3) {
1063 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1066 lgetuser(&CC->user, CC->curr_user);
1067 CC->user.USscreenwidth = extract_int(new_parms, 0);
1068 CC->user.USscreenheight = extract_int(new_parms, 1);
1069 CC->user.flags = CC->user.flags & (~US_USER_SET);
1070 CC->user.flags = CC->user.flags |
1071 (extract_int(new_parms, 2) & US_USER_SET);
1073 lputuser(&CC->user);
1074 cprintf("%d Ok\n", CIT_OK);
1078 * set last read pointer
1080 void cmd_slrp(char *new_ptr)
1084 struct visit original_vbuf;
1086 if (CtdlAccessCheck(ac_logged_in)) {
1090 if (!strncasecmp(new_ptr, "highest", 7)) {
1091 newlr = CC->room.QRhighest;
1093 newlr = atol(new_ptr);
1096 lgetuser(&CC->user, CC->curr_user);
1098 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1099 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1100 vbuf.v_lastseen = newlr;
1101 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1103 /* Only rewrite the record if it changed */
1104 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1105 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1106 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1109 lputuser(&CC->user);
1110 cprintf("%d %ld\n", CIT_OK, newlr);
1114 void cmd_seen(char *argbuf) {
1115 long target_msgnum = 0L;
1116 int target_setting = 0;
1118 if (CtdlAccessCheck(ac_logged_in)) {
1122 if (num_parms(argbuf) != 2) {
1123 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1127 target_msgnum = extract_long(argbuf, 0);
1128 target_setting = extract_int(argbuf, 1);
1130 CtdlSetSeen(&target_msgnum, 1, target_setting,
1131 ctdlsetseen_seen, NULL, NULL);
1132 cprintf("%d OK\n", CIT_OK);
1136 void cmd_gtsn(char *argbuf) {
1139 if (CtdlAccessCheck(ac_logged_in)) {
1143 CtdlGetSeen(buf, ctdlsetseen_seen);
1144 cprintf("%d %s\n", CIT_OK, buf);
1149 * API function for cmd_invt_kick() and anything else that needs to
1150 * invite or kick out a user to/from a room.
1152 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1154 int CtdlInvtKick(char *iuser, int op) {
1155 struct ctdluser USscratch;
1159 if (getuser(&USscratch, iuser) != 0) {
1163 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1165 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1166 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1169 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1170 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1172 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1174 /* post a message in Aide> saying what we just did */
1175 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1177 ((op == 1) ? "invited to" : "kicked out of"),
1187 * INVT and KICK commands
1189 void cmd_invt_kick(char *iuser, int op) {
1192 * These commands are only allowed by aides, room aides,
1193 * and room namespace owners
1196 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1197 /* access granted */
1200 cprintf("%d Higher access or room ownership required.\n",
1201 ERROR + HIGHER_ACCESS_REQUIRED);
1205 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1207 cprintf("%d Can't add/remove users from this room.\n",
1212 if (CtdlInvtKick(iuser, op) != 0) {
1213 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1217 cprintf("%d %s %s %s.\n",
1219 ((op == 1) ? "invited to" : "kicked out of"),
1226 * Forget (Zap) the current room (API call)
1227 * Returns 0 on success
1229 int CtdlForgetThisRoom(void) {
1232 /* On some systems, Aides are not allowed to forget rooms */
1233 if (is_aide() && (config.c_aide_zap == 0)
1234 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1238 lgetuser(&CC->user, CC->curr_user);
1239 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1241 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1242 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1244 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1245 lputuser(&CC->user);
1247 /* Return to the Lobby, so we don't end up in an undefined room */
1248 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1255 * forget (Zap) the current room
1260 if (CtdlAccessCheck(ac_logged_in)) {
1264 if (CtdlForgetThisRoom() == 0) {
1265 cprintf("%d Ok\n", CIT_OK);
1268 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1273 * Get Next Unregistered User
1277 struct cdbdata *cdbus;
1278 struct ctdluser usbuf;
1280 if (CtdlAccessCheck(ac_aide)) {
1284 if ((CitControl.MMflags & MM_VALID) == 0) {
1285 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1289 /* There are unvalidated users. Traverse the user database,
1290 * and return the first user we find that needs validation.
1292 cdb_rewind(CDB_USERS);
1293 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1294 memset(&usbuf, 0, sizeof(struct ctdluser));
1295 memcpy(&usbuf, cdbus->ptr,
1296 ((cdbus->len > sizeof(struct ctdluser)) ?
1297 sizeof(struct ctdluser) : cdbus->len));
1299 if ((usbuf.flags & US_NEEDVALID)
1300 && (usbuf.axlevel > 0)) {
1301 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1302 cdb_close_cursor(CDB_USERS);
1307 /* If we get to this point, there are no more unvalidated users.
1308 * Therefore we clear the "users need validation" flag.
1311 begin_critical_section(S_CONTROL);
1313 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1315 end_critical_section(S_CONTROL);
1316 cprintf("%d *** End of registration.\n", CIT_OK);
1325 void cmd_vali(char *v_args)
1329 struct ctdluser userbuf;
1331 extract_token(user, v_args, 0, '|', sizeof user);
1332 newax = extract_int(v_args, 1);
1334 if (CtdlAccessCheck(ac_aide)) {
1338 if (lgetuser(&userbuf, user) != 0) {
1339 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1343 userbuf.axlevel = newax;
1344 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1348 /* If the access level was set to zero, delete the user */
1350 if (purge_user(user) == 0) {
1351 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1355 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1361 * Traverse the user file...
1363 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1366 struct ctdluser usbuf;
1367 struct cdbdata *cdbus;
1369 cdb_rewind(CDB_USERS);
1371 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1372 memset(&usbuf, 0, sizeof(struct ctdluser));
1373 memcpy(&usbuf, cdbus->ptr,
1374 ((cdbus->len > sizeof(struct ctdluser)) ?
1375 sizeof(struct ctdluser) : cdbus->len));
1377 (*CallBack) (&usbuf, in_data);
1383 * List one user (this works with cmd_list)
1385 void ListThisUser(struct ctdluser *usbuf, void *data)
1389 searchstring = (char *)data;
1390 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1394 if (usbuf->axlevel > 0) {
1395 if ((CC->user.axlevel >= 6)
1396 || ((usbuf->flags & US_UNLISTED) == 0)
1397 || ((CC->internal_pgm))) {
1398 cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1402 (long)usbuf->lastcall,
1405 if (CC->user.axlevel >= 6)
1406 cprintf("%s", usbuf->password);
1413 * List users (searchstring may be empty to list all users)
1415 void cmd_list(char *cmdbuf)
1417 char searchstring[256];
1418 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1419 striplt(searchstring);
1420 cprintf("%d \n", LISTING_FOLLOWS);
1421 ForEachUser(ListThisUser, (void *)searchstring );
1429 * assorted info we need to check at login
1437 if (CtdlAccessCheck(ac_logged_in)) {
1441 getuser(&CC->user, CC->curr_user); /* no lock is needed here */
1442 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1445 if (CC->user.axlevel >= 6) {
1447 if (CitControl.MMflags & MM_VALID)
1451 /* check for mail */
1452 mail = InitialMailCheck();
1454 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1459 * check to see if a user exists
1461 void cmd_qusr(char *who)
1463 struct ctdluser usbuf;
1465 if (getuser(&usbuf, who) == 0) {
1466 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1468 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1474 * Administrative Get User Parameters
1476 void cmd_agup(char *cmdbuf)
1478 struct ctdluser usbuf;
1479 char requested_user[128];
1481 if (CtdlAccessCheck(ac_aide)) {
1485 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1486 if (getuser(&usbuf, requested_user) != 0) {
1487 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1490 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1497 (int) usbuf.axlevel,
1499 (long)usbuf.lastcall,
1506 * Administrative Set User Parameters
1508 void cmd_asup(char *cmdbuf)
1510 struct ctdluser usbuf;
1511 char requested_user[128];
1517 if (CtdlAccessCheck(ac_aide))
1520 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1521 if (lgetuser(&usbuf, requested_user) != 0) {
1522 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1525 np = num_parms(cmdbuf);
1527 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1529 usbuf.flags = extract_int(cmdbuf, 2);
1531 usbuf.timescalled = extract_int(cmdbuf, 3);
1533 usbuf.posted = extract_int(cmdbuf, 4);
1535 newax = extract_int(cmdbuf, 5);
1536 if ((newax >= 0) && (newax <= 6)) {
1537 usbuf.axlevel = extract_int(cmdbuf, 5);
1541 usbuf.lastcall = extract_long(cmdbuf, 7);
1544 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1547 if (usbuf.axlevel == 0) {
1548 if (purge_user(requested_user) == 0) {
1554 sprintf(notify, "User \"%s\" has been deleted by %s.\n",
1555 usbuf.fullname, CC->user.fullname);
1556 aide_message(notify);
1559 cprintf("%d Ok", CIT_OK);
1561 cprintf(" (%s deleted)", requested_user);
1568 * Check to see if the user who we just sent mail to is logged in. If yes,
1569 * bump the 'new mail' counter for their session. That enables them to
1570 * receive a new mail notification without having to hit the database.
1572 void BumpNewMailCounter(long which_user) {
1573 struct CitContext *ptr;
1575 begin_critical_section(S_SESSION_TABLE);
1577 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1578 if (ptr->user.usernum == which_user) {
1583 end_critical_section(S_SESSION_TABLE);
1588 * Count the number of new mail messages the user has
1592 int num_newmsgs = 0;
1594 num_newmsgs = CC->newmail;
1597 return (num_newmsgs);
1602 * Count the number of new mail messages the user has
1604 int InitialMailCheck()
1606 int num_newmsgs = 0;
1608 char mailboxname[ROOMNAMELEN];
1609 struct ctdlroom mailbox;
1611 struct cdbdata *cdbfr;
1612 long *msglist = NULL;
1615 MailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1616 if (getroom(&mailbox, mailboxname) != 0)
1618 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1620 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1622 if (cdbfr != NULL) {
1623 msglist = malloc(cdbfr->len);
1624 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1625 num_msgs = cdbfr->len / sizeof(long);
1629 for (a = 0; a < num_msgs; ++a) {
1630 if (msglist[a] > 0L) {
1631 if (msglist[a] > vbuf.v_lastseen) {
1636 if (msglist != NULL)
1639 return (num_newmsgs);
1645 * Set the preferred view for the current user/room combination
1647 void cmd_view(char *cmdbuf) {
1651 if (CtdlAccessCheck(ac_logged_in)) {
1655 requested_view = extract_int(cmdbuf, 0);
1657 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1658 vbuf.v_view = requested_view;
1659 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1661 cprintf("%d ok\n", CIT_OK);