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 char sysuser_name[USERNAME_SIZE];
75 struct cdbdata *cdbus;
76 int using_sysuser = 0;
79 memset(usbuf, 0, sizeof(struct ctdluser));
82 #ifdef ENABLE_AUTOLOGIN
83 if (CtdlAssociateSystemUser(sysuser_name, name) == 0) {
89 makeuserkey(usernamekey, sysuser_name);
92 makeuserkey(usernamekey, name);
95 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
96 if (cdbus == NULL) { /* user not found */
100 memcpy(usbuf, cdbus->ptr,
101 ((cdbus->len > sizeof(struct ctdluser)) ?
102 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, USERNAME_SIZE);
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(username);
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_token(username, cmdbuf, 0, '|', sizeof username);
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, VIEW_MAILBOX);
487 create_room(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
488 create_room(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
490 /* Run any startup routines registered by loadable modules */
491 PerformSessionHooks(EVT_LOGIN);
493 /* Enter the lobby */
494 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
498 void logged_in_response(void)
500 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
501 CIT_OK, CC->user.fullname, CC->user.axlevel,
502 CC->user.timescalled, CC->user.posted,
503 CC->user.flags, CC->user.usernum,
510 * misc things to be taken care of when a user is logged out
512 void logout(struct CitContext *who)
515 * Clear out some session data. Most likely, the CitContext for this
516 * session is about to get nuked when the session disconnects, but
517 * since it's possible to log in again without reconnecting, we cannot
518 * make that assumption.
520 strcpy(who->fake_username, "");
521 strcpy(who->fake_postname, "");
522 strcpy(who->fake_hostname, "");
523 strcpy(who->fake_roomname, "");
527 * If there is a download in progress, abort it.
529 if (who->download_fp != NULL) {
530 fclose(who->download_fp);
531 who->download_fp = NULL;
535 * If there is an upload in progress, abort it.
537 if (who->upload_fp != NULL) {
542 * If we were talking to a network node, we're not anymore...
544 if (strlen(who->net_node) > 0) {
545 network_talking_to(who->net_node, NTT_REMOVE);
548 /* Do modular stuff... */
549 PerformSessionHooks(EVT_LOGOUT);
551 /* Free any output buffers */
552 if (who->output_buffer != NULL) {
559 * an alternate version of validpw() which executes `chkpwd' instead of
560 * verifying the password directly
562 static int validpw(uid_t uid, const char *pass)
565 int status, pipev[2];
569 lprintf(CTDL_ERR, "pipe failed (%s): denying autologin access for "
570 "uid %ld\n", strerror(errno), (long)uid);
573 switch (pid = fork()) {
575 lprintf(CTDL_ERR, "fork failed (%s): denying autologin access for "
576 "uid %ld\n", strerror(errno), (long)uid);
583 if (dup2(pipev[0], 0) == -1) {
589 execl(CTDLDIR "/chkpwd", CTDLDIR "/chkpwd", NULL);
590 perror(CTDLDIR "/chkpwd");
596 snprintf(buf, sizeof buf, "%lu\n", (unsigned long) uid));
597 write(pipev[1], pass, strlen(pass));
598 write(pipev[1], "\n", 1);
601 while (waitpid(pid, &status, 0) == -1)
602 if (errno != EINTR) {
603 lprintf(CTDL_ERR, "waitpid failed (%s): denying autologin "
604 "access for uid %ld\n",
605 strerror(errno), (long)uid);
608 if (WIFEXITED(status) && !WEXITSTATUS(status))
622 int CtdlTryPassword(char *password)
626 if ((CC->logged_in)) {
627 lprintf(CTDL_WARNING, "CtdlTryPassword: already logged in\n");
628 return pass_already_logged_in;
630 if (!strcmp(CC->curr_user, NLI)) {
631 lprintf(CTDL_WARNING, "CtdlTryPassword: no user selected\n");
634 if (getuser(&CC->user, CC->curr_user)) {
635 lprintf(CTDL_ERR, "CtdlTryPassword: internal error\n");
636 return pass_internal_error;
638 if (password == NULL) {
639 lprintf(CTDL_INFO, "CtdlTryPassword: NULL password string supplied\n");
640 return pass_wrong_password;
645 #ifdef ENABLE_AUTOLOGIN
646 /* A uid of CTDLUID or -1 indicates that this user exists only in
647 * Citadel, not in the underlying operating system.
649 if ( (CC->user.uid == CTDLUID) || (CC->user.uid == (-1)) ) {
651 strproc(CC->user.password);
652 code = strcasecmp(CC->user.password, password);
654 /* Any other uid means we have to check the system password database */
656 if (validpw(CC->user.uid, password)) {
658 lgetuser(&CC->user, CC->curr_user);
659 safestrncpy(CC->user.password, password,
660 sizeof CC->user.password);
665 #else /* ENABLE_AUTOLOGIN */
667 strproc(CC->user.password);
668 code = strcasecmp(CC->user.password, password);
670 #endif /* ENABLE_AUTOLOGIN */
676 lprintf(CTDL_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
677 return pass_wrong_password;
682 void cmd_pass(char *buf)
687 extract_token(password, buf, 0, '|', sizeof password);
688 a = CtdlTryPassword(password);
691 case pass_already_logged_in:
692 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
695 cprintf("%d You must send a name with USER first.\n",
696 ERROR + USERNAME_REQUIRED);
698 case pass_wrong_password:
699 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
702 logged_in_response();
710 * Delete a user record *and* all of its related resources.
712 int purge_user(char pname[])
715 struct ctdluser usbuf;
716 char usernamekey[USERNAME_SIZE];
717 struct CitContext *ccptr;
718 int user_is_logged_in = 0;
720 makeuserkey(usernamekey, pname);
722 if (getuser(&usbuf, pname) != 0) {
723 lprintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
724 return (ERROR + NO_SUCH_USER);
726 /* Don't delete a user who is currently logged in. Instead, just
727 * set the access level to 0, and let the account get swept up
728 * during the next purge.
730 user_is_logged_in = 0;
731 begin_critical_section(S_SESSION_TABLE);
732 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
733 if (ccptr->user.usernum == usbuf.usernum) {
734 user_is_logged_in = 1;
737 end_critical_section(S_SESSION_TABLE);
738 if (user_is_logged_in == 1) {
739 lprintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
744 lprintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
746 /* Perform any purge functions registered by server extensions */
747 PerformUserHooks(&usbuf, EVT_PURGEUSER);
749 /* delete any existing user/room relationships */
750 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
752 /* delete the userlog entry */
753 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
755 /* remove the user's bio file */
763 /* remove the user's picture */
776 * create_user() - back end processing to create a new user
778 * Set 'newusername' to the desired account name.
779 * Set 'become_user' to nonzero if this is self-service account creation and we want
780 * to actually log in as the user we just created, otherwise set it to 0.
782 int create_user(char *newusername, int become_user)
784 struct ctdluser usbuf;
785 struct ctdlroom qrbuf;
787 char mailboxname[ROOMNAMELEN];
790 safestrncpy(username, newusername, sizeof username);
793 #ifdef ENABLE_AUTOLOGIN
795 struct passwd *p = (struct passwd *) getpwnam(username);
798 extract_token(username, p->pw_gecos, 0, ',', sizeof username);
808 if (!getuser(&usbuf, username)) {
809 return (ERROR + ALREADY_EXISTS);
812 /* Go ahead and initialize a new user record */
813 memset(&usbuf, 0, sizeof(struct ctdluser));
814 safestrncpy(usbuf.fullname, username, sizeof usbuf.fullname);
815 strcpy(usbuf.password, "");
818 /* These are the default flags on new accounts */
819 usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
821 usbuf.timescalled = 0;
823 usbuf.axlevel = config.c_initax;
824 usbuf.USscreenwidth = 80;
825 usbuf.USscreenheight = 24;
826 usbuf.lastcall = time(NULL);
828 /* fetch a new user number */
829 usbuf.usernum = get_new_user_number();
831 /* The very first user created on the system will always be an Aide */
832 if (usbuf.usernum == 1L) {
836 /* add user to userlog */
840 * Give the user a private mailbox and a configuration room.
841 * Make the latter an invisible system room.
843 MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
844 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
846 MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
847 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
848 if (lgetroom(&qrbuf, mailboxname) == 0) {
849 qrbuf.QRflags2 |= QR2_SYSTEM;
853 /* Perform any create functions registered by server extensions */
854 PerformUserHooks(&usbuf, EVT_NEWUSER);
856 /* Everything below this line can be bypassed if administratively
857 * creating a user, instead of doing self-service account creation
861 /* Now become the user we just created */
862 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
863 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
866 /* Check to make sure we're still who we think we are */
867 if (getuser(&CC->user, CC->curr_user)) {
868 return (ERROR + INTERNAL_ERROR);
872 lprintf(CTDL_NOTICE, "New user <%s> created\n", username);
880 * cmd_newu() - create a new user account and log in as that user
882 void cmd_newu(char *cmdbuf)
887 if (config.c_disable_newu) {
888 cprintf("%d Self-service user account creation "
889 "is disabled on this system.\n", ERROR + NOT_HERE);
894 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
898 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
899 ERROR + MAX_SESSIONS_EXCEEDED,
900 config.c_nodename, config.c_maxsessions);
902 extract_token(username, cmdbuf, 0, '|', sizeof username);
906 if (strlen(username) == 0) {
907 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
911 if ((!strcasecmp(username, "bbs")) ||
912 (!strcasecmp(username, "new")) ||
913 (!strcasecmp(username, "."))) {
914 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
918 a = create_user(username, 1);
922 logged_in_response();
923 } else if (a == ERROR + ALREADY_EXISTS) {
924 cprintf("%d '%s' already exists.\n",
925 ERROR + ALREADY_EXISTS, username);
927 } else if (a == ERROR + INTERNAL_ERROR) {
928 cprintf("%d Internal error - user record disappeared?\n",
929 ERROR + INTERNAL_ERROR);
932 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
941 void cmd_setp(char *new_pw)
943 if (CtdlAccessCheck(ac_logged_in)) {
946 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
947 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
951 if (strlen(new_pw) == 0) {
952 cprintf("%d Password unchanged.\n", CIT_OK);
955 lgetuser(&CC->user, CC->curr_user);
956 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
958 cprintf("%d Password changed.\n", CIT_OK);
959 lprintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
960 PerformSessionHooks(EVT_SETPASS);
965 * cmd_creu() - administratively create a new user account (do not log in to it)
967 void cmd_creu(char *cmdbuf)
974 if (CtdlAccessCheck(ac_aide)) {
978 extract_token(username, cmdbuf, 0, '|', sizeof username);
979 extract_token(password, cmdbuf, 1, '|', sizeof password);
985 if (strlen(username) == 0) {
986 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
990 a = create_user(username, 0);
993 if (strlen(password) > 0) {
994 lgetuser(&tmp, username);
995 safestrncpy(tmp.password, password, sizeof(tmp.password));
998 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
999 (strlen(password) > 0) ? "and password set" :
1000 "with no password");
1002 } else if (a == ERROR + ALREADY_EXISTS) {
1003 cprintf("%d '%s' already exists.\n",
1004 ERROR + ALREADY_EXISTS, username);
1007 cprintf("%d An error occured creating the user account.\n", ERROR + INTERNAL_ERROR);
1014 * get user parameters
1019 if (CtdlAccessCheck(ac_logged_in))
1022 getuser(&CC->user, CC->curr_user);
1023 cprintf("%d %d|%d|%d|\n",
1025 CC->user.USscreenwidth,
1026 CC->user.USscreenheight,
1027 (CC->user.flags & US_USER_SET)
1032 * set user parameters
1034 void cmd_setu(char *new_parms)
1036 if (CtdlAccessCheck(ac_logged_in))
1039 if (num_parms(new_parms) < 3) {
1040 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1043 lgetuser(&CC->user, CC->curr_user);
1044 CC->user.USscreenwidth = extract_int(new_parms, 0);
1045 CC->user.USscreenheight = extract_int(new_parms, 1);
1046 CC->user.flags = CC->user.flags & (~US_USER_SET);
1047 CC->user.flags = CC->user.flags |
1048 (extract_int(new_parms, 2) & US_USER_SET);
1050 lputuser(&CC->user);
1051 cprintf("%d Ok\n", CIT_OK);
1055 * set last read pointer
1057 void cmd_slrp(char *new_ptr)
1061 struct visit original_vbuf;
1063 if (CtdlAccessCheck(ac_logged_in)) {
1067 if (!strncasecmp(new_ptr, "highest", 7)) {
1068 newlr = CC->room.QRhighest;
1070 newlr = atol(new_ptr);
1073 lgetuser(&CC->user, CC->curr_user);
1075 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1076 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1077 vbuf.v_lastseen = newlr;
1078 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1080 /* Only rewrite the record if it changed */
1081 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1082 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1083 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1086 lputuser(&CC->user);
1087 cprintf("%d %ld\n", CIT_OK, newlr);
1091 void cmd_seen(char *argbuf) {
1092 long target_msgnum = 0L;
1093 int target_setting = 0;
1095 if (CtdlAccessCheck(ac_logged_in)) {
1099 if (num_parms(argbuf) != 2) {
1100 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1104 target_msgnum = extract_long(argbuf, 0);
1105 target_setting = extract_int(argbuf, 1);
1107 CtdlSetSeen(&target_msgnum, 1, target_setting,
1108 ctdlsetseen_seen, NULL, NULL);
1109 cprintf("%d OK\n", CIT_OK);
1113 void cmd_gtsn(char *argbuf) {
1116 if (CtdlAccessCheck(ac_logged_in)) {
1120 CtdlGetSeen(buf, ctdlsetseen_seen);
1121 cprintf("%d %s\n", CIT_OK, buf);
1126 * API function for cmd_invt_kick() and anything else that needs to
1127 * invite or kick out a user to/from a room.
1129 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1131 int CtdlInvtKick(char *iuser, int op) {
1132 struct ctdluser USscratch;
1136 if (getuser(&USscratch, iuser) != 0) {
1140 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1142 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1143 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1146 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1147 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1149 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1151 /* post a message in Aide> saying what we just did */
1152 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1154 ((op == 1) ? "invited to" : "kicked out of"),
1164 * INVT and KICK commands
1166 void cmd_invt_kick(char *iuser, int op) {
1169 * These commands are only allowed by aides, room aides,
1170 * and room namespace owners
1173 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1174 /* access granted */
1177 cprintf("%d Higher access or room ownership required.\n",
1178 ERROR + HIGHER_ACCESS_REQUIRED);
1182 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1184 cprintf("%d Can't add/remove users from this room.\n",
1189 if (CtdlInvtKick(iuser, op) != 0) {
1190 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1194 cprintf("%d %s %s %s.\n",
1196 ((op == 1) ? "invited to" : "kicked out of"),
1203 * Forget (Zap) the current room (API call)
1204 * Returns 0 on success
1206 int CtdlForgetThisRoom(void) {
1209 /* On some systems, Aides are not allowed to forget rooms */
1210 if (is_aide() && (config.c_aide_zap == 0)
1211 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1215 lgetuser(&CC->user, CC->curr_user);
1216 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1218 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1219 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1221 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1222 lputuser(&CC->user);
1224 /* Return to the Lobby, so we don't end up in an undefined room */
1225 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1232 * forget (Zap) the current room
1237 if (CtdlAccessCheck(ac_logged_in)) {
1241 if (CtdlForgetThisRoom() == 0) {
1242 cprintf("%d Ok\n", CIT_OK);
1245 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1250 * Get Next Unregistered User
1254 struct cdbdata *cdbus;
1255 struct ctdluser usbuf;
1257 if (CtdlAccessCheck(ac_aide)) {
1261 if ((CitControl.MMflags & MM_VALID) == 0) {
1262 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1266 /* There are unvalidated users. Traverse the user database,
1267 * and return the first user we find that needs validation.
1269 cdb_rewind(CDB_USERS);
1270 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1271 memset(&usbuf, 0, sizeof(struct ctdluser));
1272 memcpy(&usbuf, cdbus->ptr,
1273 ((cdbus->len > sizeof(struct ctdluser)) ?
1274 sizeof(struct ctdluser) : cdbus->len));
1276 if ((usbuf.flags & US_NEEDVALID)
1277 && (usbuf.axlevel > 0)) {
1278 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1279 cdb_close_cursor(CDB_USERS);
1284 /* If we get to this point, there are no more unvalidated users.
1285 * Therefore we clear the "users need validation" flag.
1288 begin_critical_section(S_CONTROL);
1290 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1292 end_critical_section(S_CONTROL);
1293 cprintf("%d *** End of registration.\n", CIT_OK);
1302 void cmd_vali(char *v_args)
1306 struct ctdluser userbuf;
1308 extract_token(user, v_args, 0, '|', sizeof user);
1309 newax = extract_int(v_args, 1);
1311 if (CtdlAccessCheck(ac_aide)) {
1315 if (lgetuser(&userbuf, user) != 0) {
1316 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1320 userbuf.axlevel = newax;
1321 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1325 /* If the access level was set to zero, delete the user */
1327 if (purge_user(user) == 0) {
1328 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1332 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1338 * Traverse the user file...
1340 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1343 struct ctdluser usbuf;
1344 struct cdbdata *cdbus;
1346 cdb_rewind(CDB_USERS);
1348 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1349 memset(&usbuf, 0, sizeof(struct ctdluser));
1350 memcpy(&usbuf, cdbus->ptr,
1351 ((cdbus->len > sizeof(struct ctdluser)) ?
1352 sizeof(struct ctdluser) : cdbus->len));
1354 (*CallBack) (&usbuf, in_data);
1360 * List one user (this works with cmd_list)
1362 void ListThisUser(struct ctdluser *usbuf, void *data)
1366 searchstring = (char *)data;
1367 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1371 if (usbuf->axlevel > 0) {
1372 if ((CC->user.axlevel >= 6)
1373 || ((usbuf->flags & US_UNLISTED) == 0)
1374 || ((CC->internal_pgm))) {
1375 cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1379 (long)usbuf->lastcall,
1382 if (CC->user.axlevel >= 6)
1383 cprintf("%s", usbuf->password);
1390 * List users (searchstring may be empty to list all users)
1392 void cmd_list(char *cmdbuf)
1394 char searchstring[256];
1395 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1396 striplt(searchstring);
1397 cprintf("%d \n", LISTING_FOLLOWS);
1398 ForEachUser(ListThisUser, (void *)searchstring );
1406 * assorted info we need to check at login
1414 if (CtdlAccessCheck(ac_logged_in)) {
1418 getuser(&CC->user, CC->curr_user); /* no lock is needed here */
1419 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1422 if (CC->user.axlevel >= 6) {
1424 if (CitControl.MMflags & MM_VALID)
1428 /* check for mail */
1429 mail = InitialMailCheck();
1431 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1436 * check to see if a user exists
1438 void cmd_qusr(char *who)
1440 struct ctdluser usbuf;
1442 if (getuser(&usbuf, who) == 0) {
1443 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1445 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1451 * Administrative Get User Parameters
1453 void cmd_agup(char *cmdbuf)
1455 struct ctdluser usbuf;
1456 char requested_user[128];
1458 if (CtdlAccessCheck(ac_aide)) {
1462 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1463 if (getuser(&usbuf, requested_user) != 0) {
1464 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1467 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1474 (int) usbuf.axlevel,
1476 (long)usbuf.lastcall,
1483 * Administrative Set User Parameters
1485 void cmd_asup(char *cmdbuf)
1487 struct ctdluser usbuf;
1488 char requested_user[128];
1494 if (CtdlAccessCheck(ac_aide))
1497 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1498 if (lgetuser(&usbuf, requested_user) != 0) {
1499 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1502 np = num_parms(cmdbuf);
1504 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1506 usbuf.flags = extract_int(cmdbuf, 2);
1508 usbuf.timescalled = extract_int(cmdbuf, 3);
1510 usbuf.posted = extract_int(cmdbuf, 4);
1512 newax = extract_int(cmdbuf, 5);
1513 if ((newax >= 0) && (newax <= 6)) {
1514 usbuf.axlevel = extract_int(cmdbuf, 5);
1518 usbuf.lastcall = extract_long(cmdbuf, 7);
1521 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1524 if (usbuf.axlevel == 0) {
1525 if (purge_user(requested_user) == 0) {
1531 sprintf(notify, "User \"%s\" has been deleted by %s.\n",
1532 usbuf.fullname, CC->user.fullname);
1533 aide_message(notify);
1536 cprintf("%d Ok", CIT_OK);
1538 cprintf(" (%s deleted)", requested_user);
1545 * Check to see if the user who we just sent mail to is logged in. If yes,
1546 * bump the 'new mail' counter for their session. That enables them to
1547 * receive a new mail notification without having to hit the database.
1549 void BumpNewMailCounter(long which_user) {
1550 struct CitContext *ptr;
1552 begin_critical_section(S_SESSION_TABLE);
1554 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1555 if (ptr->user.usernum == which_user) {
1560 end_critical_section(S_SESSION_TABLE);
1565 * Count the number of new mail messages the user has
1569 int num_newmsgs = 0;
1571 num_newmsgs = CC->newmail;
1574 return (num_newmsgs);
1579 * Count the number of new mail messages the user has
1581 int InitialMailCheck()
1583 int num_newmsgs = 0;
1585 char mailboxname[ROOMNAMELEN];
1586 struct ctdlroom mailbox;
1588 struct cdbdata *cdbfr;
1589 long *msglist = NULL;
1592 MailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1593 if (getroom(&mailbox, mailboxname) != 0)
1595 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1597 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1599 if (cdbfr != NULL) {
1600 msglist = malloc(cdbfr->len);
1601 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1602 num_msgs = cdbfr->len / sizeof(long);
1606 for (a = 0; a < num_msgs; ++a) {
1607 if (msglist[a] > 0L) {
1608 if (msglist[a] > vbuf.v_lastseen) {
1613 if (msglist != NULL)
1616 return (num_newmsgs);
1622 * Set the preferred view for the current user/room combination
1624 void cmd_view(char *cmdbuf) {
1628 if (CtdlAccessCheck(ac_logged_in)) {
1632 requested_view = extract_int(cmdbuf, 0);
1634 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1635 vbuf.v_view = requested_view;
1636 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1638 cprintf("%d ok\n", CIT_OK);