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 */
756 snprintf(filename, sizeof filename,
757 #ifndef HAVE_DATA_DIR
762 "/bio/%ld", usbuf.usernum);
765 /* remove the user's picture */
766 snprintf(filename, sizeof filename,
767 #ifndef HAVE_DATA_DIR
772 "/userpics/%ld.gif", usbuf.usernum);
780 * create_user() - back end processing to create a new user
782 * Set 'newusername' to the desired account name.
783 * Set 'become_user' to nonzero if this is self-service account creation and we want
784 * to actually log in as the user we just created, otherwise set it to 0.
786 int create_user(char *newusername, int become_user)
788 struct ctdluser usbuf;
789 struct ctdlroom qrbuf;
791 char mailboxname[ROOMNAMELEN];
794 safestrncpy(username, newusername, sizeof username);
797 #ifdef ENABLE_AUTOLOGIN
799 struct passwd *p = (struct passwd *) getpwnam(username);
802 extract_token(username, p->pw_gecos, 0, ',', sizeof username);
812 if (!getuser(&usbuf, username)) {
813 return (ERROR + ALREADY_EXISTS);
816 /* Go ahead and initialize a new user record */
817 memset(&usbuf, 0, sizeof(struct ctdluser));
818 safestrncpy(usbuf.fullname, username, sizeof usbuf.fullname);
819 strcpy(usbuf.password, "");
822 /* These are the default flags on new accounts */
823 usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
825 usbuf.timescalled = 0;
827 usbuf.axlevel = config.c_initax;
828 usbuf.USscreenwidth = 80;
829 usbuf.USscreenheight = 24;
830 usbuf.lastcall = time(NULL);
832 /* fetch a new user number */
833 usbuf.usernum = get_new_user_number();
835 /* The very first user created on the system will always be an Aide */
836 if (usbuf.usernum == 1L) {
840 /* add user to userlog */
844 * Give the user a private mailbox and a configuration room.
845 * Make the latter an invisible system room.
847 MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
848 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
850 MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
851 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
852 if (lgetroom(&qrbuf, mailboxname) == 0) {
853 qrbuf.QRflags2 |= QR2_SYSTEM;
857 /* Perform any create functions registered by server extensions */
858 PerformUserHooks(&usbuf, EVT_NEWUSER);
860 /* Everything below this line can be bypassed if administratively
861 * creating a user, instead of doing self-service account creation
865 /* Now become the user we just created */
866 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
867 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
870 /* Check to make sure we're still who we think we are */
871 if (getuser(&CC->user, CC->curr_user)) {
872 return (ERROR + INTERNAL_ERROR);
876 lprintf(CTDL_NOTICE, "New user <%s> created\n", username);
884 * cmd_newu() - create a new user account and log in as that user
886 void cmd_newu(char *cmdbuf)
891 if (config.c_disable_newu) {
892 cprintf("%d Self-service user account creation "
893 "is disabled on this system.\n", ERROR + NOT_HERE);
898 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
902 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
903 ERROR + MAX_SESSIONS_EXCEEDED,
904 config.c_nodename, config.c_maxsessions);
906 extract_token(username, cmdbuf, 0, '|', sizeof username);
910 if (strlen(username) == 0) {
911 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
915 if ((!strcasecmp(username, "bbs")) ||
916 (!strcasecmp(username, "new")) ||
917 (!strcasecmp(username, "."))) {
918 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
922 a = create_user(username, 1);
926 logged_in_response();
927 } else if (a == ERROR + ALREADY_EXISTS) {
928 cprintf("%d '%s' already exists.\n",
929 ERROR + ALREADY_EXISTS, username);
931 } else if (a == ERROR + INTERNAL_ERROR) {
932 cprintf("%d Internal error - user record disappeared?\n",
933 ERROR + INTERNAL_ERROR);
936 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
945 void cmd_setp(char *new_pw)
947 if (CtdlAccessCheck(ac_logged_in)) {
950 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
951 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
955 if (strlen(new_pw) == 0) {
956 cprintf("%d Password unchanged.\n", CIT_OK);
959 lgetuser(&CC->user, CC->curr_user);
960 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
962 cprintf("%d Password changed.\n", CIT_OK);
963 lprintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
964 PerformSessionHooks(EVT_SETPASS);
969 * cmd_creu() - administratively create a new user account (do not log in to it)
971 void cmd_creu(char *cmdbuf)
978 if (CtdlAccessCheck(ac_aide)) {
982 extract_token(username, cmdbuf, 0, '|', sizeof username);
983 extract_token(password, cmdbuf, 1, '|', sizeof password);
989 if (strlen(username) == 0) {
990 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
994 a = create_user(username, 0);
997 if (strlen(password) > 0) {
998 lgetuser(&tmp, username);
999 safestrncpy(tmp.password, password, sizeof(tmp.password));
1002 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1003 (strlen(password) > 0) ? "and password set" :
1004 "with no password");
1006 } else if (a == ERROR + ALREADY_EXISTS) {
1007 cprintf("%d '%s' already exists.\n",
1008 ERROR + ALREADY_EXISTS, username);
1011 cprintf("%d An error occured creating the user account.\n", ERROR + INTERNAL_ERROR);
1018 * get user parameters
1023 if (CtdlAccessCheck(ac_logged_in))
1026 getuser(&CC->user, CC->curr_user);
1027 cprintf("%d %d|%d|%d|\n",
1029 CC->user.USscreenwidth,
1030 CC->user.USscreenheight,
1031 (CC->user.flags & US_USER_SET)
1036 * set user parameters
1038 void cmd_setu(char *new_parms)
1040 if (CtdlAccessCheck(ac_logged_in))
1043 if (num_parms(new_parms) < 3) {
1044 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1047 lgetuser(&CC->user, CC->curr_user);
1048 CC->user.USscreenwidth = extract_int(new_parms, 0);
1049 CC->user.USscreenheight = extract_int(new_parms, 1);
1050 CC->user.flags = CC->user.flags & (~US_USER_SET);
1051 CC->user.flags = CC->user.flags |
1052 (extract_int(new_parms, 2) & US_USER_SET);
1054 lputuser(&CC->user);
1055 cprintf("%d Ok\n", CIT_OK);
1059 * set last read pointer
1061 void cmd_slrp(char *new_ptr)
1065 struct visit original_vbuf;
1067 if (CtdlAccessCheck(ac_logged_in)) {
1071 if (!strncasecmp(new_ptr, "highest", 7)) {
1072 newlr = CC->room.QRhighest;
1074 newlr = atol(new_ptr);
1077 lgetuser(&CC->user, CC->curr_user);
1079 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1080 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1081 vbuf.v_lastseen = newlr;
1082 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1084 /* Only rewrite the record if it changed */
1085 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1086 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1087 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1090 lputuser(&CC->user);
1091 cprintf("%d %ld\n", CIT_OK, newlr);
1095 void cmd_seen(char *argbuf) {
1096 long target_msgnum = 0L;
1097 int target_setting = 0;
1099 if (CtdlAccessCheck(ac_logged_in)) {
1103 if (num_parms(argbuf) != 2) {
1104 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1108 target_msgnum = extract_long(argbuf, 0);
1109 target_setting = extract_int(argbuf, 1);
1111 CtdlSetSeen(&target_msgnum, 1, target_setting,
1112 ctdlsetseen_seen, NULL, NULL);
1113 cprintf("%d OK\n", CIT_OK);
1117 void cmd_gtsn(char *argbuf) {
1120 if (CtdlAccessCheck(ac_logged_in)) {
1124 CtdlGetSeen(buf, ctdlsetseen_seen);
1125 cprintf("%d %s\n", CIT_OK, buf);
1130 * API function for cmd_invt_kick() and anything else that needs to
1131 * invite or kick out a user to/from a room.
1133 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1135 int CtdlInvtKick(char *iuser, int op) {
1136 struct ctdluser USscratch;
1140 if (getuser(&USscratch, iuser) != 0) {
1144 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1146 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1147 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1150 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1151 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1153 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1155 /* post a message in Aide> saying what we just did */
1156 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1158 ((op == 1) ? "invited to" : "kicked out of"),
1168 * INVT and KICK commands
1170 void cmd_invt_kick(char *iuser, int op) {
1173 * These commands are only allowed by aides, room aides,
1174 * and room namespace owners
1177 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1178 /* access granted */
1181 cprintf("%d Higher access or room ownership required.\n",
1182 ERROR + HIGHER_ACCESS_REQUIRED);
1186 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1188 cprintf("%d Can't add/remove users from this room.\n",
1193 if (CtdlInvtKick(iuser, op) != 0) {
1194 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1198 cprintf("%d %s %s %s.\n",
1200 ((op == 1) ? "invited to" : "kicked out of"),
1207 * Forget (Zap) the current room (API call)
1208 * Returns 0 on success
1210 int CtdlForgetThisRoom(void) {
1213 /* On some systems, Aides are not allowed to forget rooms */
1214 if (is_aide() && (config.c_aide_zap == 0)
1215 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1219 lgetuser(&CC->user, CC->curr_user);
1220 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1222 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1223 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1225 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1226 lputuser(&CC->user);
1228 /* Return to the Lobby, so we don't end up in an undefined room */
1229 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1236 * forget (Zap) the current room
1241 if (CtdlAccessCheck(ac_logged_in)) {
1245 if (CtdlForgetThisRoom() == 0) {
1246 cprintf("%d Ok\n", CIT_OK);
1249 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1254 * Get Next Unregistered User
1258 struct cdbdata *cdbus;
1259 struct ctdluser usbuf;
1261 if (CtdlAccessCheck(ac_aide)) {
1265 if ((CitControl.MMflags & MM_VALID) == 0) {
1266 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1270 /* There are unvalidated users. Traverse the user database,
1271 * and return the first user we find that needs validation.
1273 cdb_rewind(CDB_USERS);
1274 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1275 memset(&usbuf, 0, sizeof(struct ctdluser));
1276 memcpy(&usbuf, cdbus->ptr,
1277 ((cdbus->len > sizeof(struct ctdluser)) ?
1278 sizeof(struct ctdluser) : cdbus->len));
1280 if ((usbuf.flags & US_NEEDVALID)
1281 && (usbuf.axlevel > 0)) {
1282 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1283 cdb_close_cursor(CDB_USERS);
1288 /* If we get to this point, there are no more unvalidated users.
1289 * Therefore we clear the "users need validation" flag.
1292 begin_critical_section(S_CONTROL);
1294 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1296 end_critical_section(S_CONTROL);
1297 cprintf("%d *** End of registration.\n", CIT_OK);
1306 void cmd_vali(char *v_args)
1310 struct ctdluser userbuf;
1312 extract_token(user, v_args, 0, '|', sizeof user);
1313 newax = extract_int(v_args, 1);
1315 if (CtdlAccessCheck(ac_aide)) {
1319 if (lgetuser(&userbuf, user) != 0) {
1320 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1324 userbuf.axlevel = newax;
1325 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1329 /* If the access level was set to zero, delete the user */
1331 if (purge_user(user) == 0) {
1332 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1336 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1342 * Traverse the user file...
1344 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1347 struct ctdluser usbuf;
1348 struct cdbdata *cdbus;
1350 cdb_rewind(CDB_USERS);
1352 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1353 memset(&usbuf, 0, sizeof(struct ctdluser));
1354 memcpy(&usbuf, cdbus->ptr,
1355 ((cdbus->len > sizeof(struct ctdluser)) ?
1356 sizeof(struct ctdluser) : cdbus->len));
1358 (*CallBack) (&usbuf, in_data);
1364 * List one user (this works with cmd_list)
1366 void ListThisUser(struct ctdluser *usbuf, void *data)
1370 searchstring = (char *)data;
1371 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1375 if (usbuf->axlevel > 0) {
1376 if ((CC->user.axlevel >= 6)
1377 || ((usbuf->flags & US_UNLISTED) == 0)
1378 || ((CC->internal_pgm))) {
1379 cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1383 (long)usbuf->lastcall,
1386 if (CC->user.axlevel >= 6)
1387 cprintf("%s", usbuf->password);
1394 * List users (searchstring may be empty to list all users)
1396 void cmd_list(char *cmdbuf)
1398 char searchstring[256];
1399 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1400 striplt(searchstring);
1401 cprintf("%d \n", LISTING_FOLLOWS);
1402 ForEachUser(ListThisUser, (void *)searchstring );
1410 * assorted info we need to check at login
1418 if (CtdlAccessCheck(ac_logged_in)) {
1422 getuser(&CC->user, CC->curr_user); /* no lock is needed here */
1423 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1426 if (CC->user.axlevel >= 6) {
1428 if (CitControl.MMflags & MM_VALID)
1432 /* check for mail */
1433 mail = InitialMailCheck();
1435 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1440 * check to see if a user exists
1442 void cmd_qusr(char *who)
1444 struct ctdluser usbuf;
1446 if (getuser(&usbuf, who) == 0) {
1447 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1449 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1455 * Administrative Get User Parameters
1457 void cmd_agup(char *cmdbuf)
1459 struct ctdluser usbuf;
1460 char requested_user[128];
1462 if (CtdlAccessCheck(ac_aide)) {
1466 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1467 if (getuser(&usbuf, requested_user) != 0) {
1468 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1471 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1478 (int) usbuf.axlevel,
1480 (long)usbuf.lastcall,
1487 * Administrative Set User Parameters
1489 void cmd_asup(char *cmdbuf)
1491 struct ctdluser usbuf;
1492 char requested_user[128];
1498 if (CtdlAccessCheck(ac_aide))
1501 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1502 if (lgetuser(&usbuf, requested_user) != 0) {
1503 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1506 np = num_parms(cmdbuf);
1508 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1510 usbuf.flags = extract_int(cmdbuf, 2);
1512 usbuf.timescalled = extract_int(cmdbuf, 3);
1514 usbuf.posted = extract_int(cmdbuf, 4);
1516 newax = extract_int(cmdbuf, 5);
1517 if ((newax >= 0) && (newax <= 6)) {
1518 usbuf.axlevel = extract_int(cmdbuf, 5);
1522 usbuf.lastcall = extract_long(cmdbuf, 7);
1525 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1528 if (usbuf.axlevel == 0) {
1529 if (purge_user(requested_user) == 0) {
1535 sprintf(notify, "User \"%s\" has been deleted by %s.\n",
1536 usbuf.fullname, CC->user.fullname);
1537 aide_message(notify);
1540 cprintf("%d Ok", CIT_OK);
1542 cprintf(" (%s deleted)", requested_user);
1549 * Check to see if the user who we just sent mail to is logged in. If yes,
1550 * bump the 'new mail' counter for their session. That enables them to
1551 * receive a new mail notification without having to hit the database.
1553 void BumpNewMailCounter(long which_user) {
1554 struct CitContext *ptr;
1556 begin_critical_section(S_SESSION_TABLE);
1558 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1559 if (ptr->user.usernum == which_user) {
1564 end_critical_section(S_SESSION_TABLE);
1569 * Count the number of new mail messages the user has
1573 int num_newmsgs = 0;
1575 num_newmsgs = CC->newmail;
1578 return (num_newmsgs);
1583 * Count the number of new mail messages the user has
1585 int InitialMailCheck()
1587 int num_newmsgs = 0;
1589 char mailboxname[ROOMNAMELEN];
1590 struct ctdlroom mailbox;
1592 struct cdbdata *cdbfr;
1593 long *msglist = NULL;
1596 MailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1597 if (getroom(&mailbox, mailboxname) != 0)
1599 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1601 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1603 if (cdbfr != NULL) {
1604 msglist = malloc(cdbfr->len);
1605 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1606 num_msgs = cdbfr->len / sizeof(long);
1610 for (a = 0; a < num_msgs; ++a) {
1611 if (msglist[a] > 0L) {
1612 if (msglist[a] > vbuf.v_lastseen) {
1617 if (msglist != NULL)
1620 return (num_newmsgs);
1626 * Set the preferred view for the current user/room combination
1628 void cmd_view(char *cmdbuf) {
1632 if (CtdlAccessCheck(ac_logged_in)) {
1636 requested_view = extract_int(cmdbuf, 0);
1638 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1639 vbuf.v_view = requested_view;
1640 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1642 cprintf("%d ok\n", CIT_OK);