4 * Server functions which perform operations on user objects.
21 #include <sys/types.h>
24 #if TIME_WITH_SYS_TIME
25 # include <sys/time.h>
29 # include <sys/time.h>
44 #include "serv_extensions.h"
45 #include "sysdep_decls.h"
53 #include "citserver.h"
57 * makeuserkey() - convert a username into the format used as a database key
58 * (it's just the username converted into lower case)
60 static INLINE void makeuserkey(char *key, char *username) {
63 len = strlen(username);
64 for (i=0; i<=len; ++i) {
65 key[i] = tolower(username[i]);
71 * getuser() - retrieve named user into supplied buffer.
72 * returns 0 on success
74 int getuser(struct ctdluser *usbuf, char name[])
77 char usernamekey[USERNAME_SIZE];
78 char sysuser_name[USERNAME_SIZE];
79 struct cdbdata *cdbus;
80 int using_sysuser = 0;
83 memset(usbuf, 0, sizeof(struct ctdluser));
86 #ifdef ENABLE_AUTOLOGIN
87 if (CtdlAssociateSystemUser(sysuser_name, name) == 0) {
93 makeuserkey(usernamekey, sysuser_name);
96 makeuserkey(usernamekey, name);
99 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
100 if (cdbus == NULL) { /* user not found */
104 memcpy(usbuf, cdbus->ptr,
105 ((cdbus->len > sizeof(struct ctdluser)) ?
106 sizeof(struct ctdluser) : cdbus->len));
115 * lgetuser() - same as getuser() but locks the record
117 int lgetuser(struct ctdluser *usbuf, char *name)
121 retcode = getuser(usbuf, name);
123 begin_critical_section(S_USERS);
130 * putuser() - write user buffer into the correct place on disk
132 void putuser(struct ctdluser *usbuf)
134 char usernamekey[USERNAME_SIZE];
136 makeuserkey(usernamekey, usbuf->fullname);
138 usbuf->version = REV_LEVEL;
140 usernamekey, strlen(usernamekey),
141 usbuf, sizeof(struct ctdluser));
147 * lputuser() - same as putuser() but locks the record
149 void lputuser(struct ctdluser *usbuf)
152 end_critical_section(S_USERS);
156 * Index-generating function used by Ctdl[Get|Set]Relationship
158 int GenerateRelationshipIndex(char *IndexBuf,
170 TheIndex.iRoomID = RoomID;
171 TheIndex.iRoomGen = RoomGen;
172 TheIndex.iUserID = UserID;
174 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
175 return (sizeof(TheIndex));
181 * Back end for CtdlSetRelationship()
183 void put_visit(struct visit *newvisit)
188 /* Generate an index */
189 IndexLen = GenerateRelationshipIndex(IndexBuf,
192 newvisit->v_usernum);
194 /* Store the record */
195 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
196 newvisit, sizeof(struct visit)
204 * Define a relationship between a user and a room
206 void CtdlSetRelationship(struct visit *newvisit,
207 struct ctdluser *rel_user,
208 struct ctdlroom *rel_room)
212 /* We don't use these in Citadel because they're implicit by the
213 * index, but they must be present if the database is exported.
215 newvisit->v_roomnum = rel_room->QRnumber;
216 newvisit->v_roomgen = rel_room->QRgen;
217 newvisit->v_usernum = rel_user->usernum;
223 * Locate a relationship between a user and a room
225 void CtdlGetRelationship(struct visit *vbuf,
226 struct ctdluser *rel_user,
227 struct ctdlroom *rel_room)
232 struct cdbdata *cdbvisit;
234 /* Generate an index */
235 IndexLen = GenerateRelationshipIndex(IndexBuf,
240 /* Clear out the buffer */
241 memset(vbuf, 0, sizeof(struct visit));
243 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
244 if (cdbvisit != NULL) {
245 memcpy(vbuf, cdbvisit->ptr,
246 ((cdbvisit->len > sizeof(struct visit)) ?
247 sizeof(struct visit) : cdbvisit->len));
251 /* If this is the first time the user has seen this room,
252 * set the view to be the default for the room.
254 vbuf->v_view = rel_room->QRdefaultview;
257 /* Set v_seen if necessary */
258 if (vbuf->v_seen[0] == 0) {
259 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
264 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
266 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
271 * Is the user currently logged in an Aide?
275 if (CC->user.axlevel >= 6)
283 * Is the user currently logged in an Aide *or* the room aide for this room?
285 int is_room_aide(void)
288 if (!CC->logged_in) {
292 if ((CC->user.axlevel >= 6)
293 || (CC->room.QRroomaide == CC->user.usernum)) {
301 * getuserbynumber() - get user by number
302 * returns 0 if user was found
304 * WARNING: don't use this function unless you absolutely have to. It does
305 * a sequential search and therefore is computationally expensive.
307 int getuserbynumber(struct ctdluser *usbuf, long int number)
309 struct cdbdata *cdbus;
311 cdb_rewind(CDB_USERS);
313 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
314 memset(usbuf, 0, sizeof(struct ctdluser));
315 memcpy(usbuf, cdbus->ptr,
316 ((cdbus->len > sizeof(struct ctdluser)) ?
317 sizeof(struct ctdluser) : cdbus->len));
319 if (usbuf->usernum == number) {
320 cdb_close_cursor(CDB_USERS);
329 * See if we can translate a system login name (i.e. from /etc/passwd)
330 * to a Citadel screen name. Returns 0 if one is found.
332 int CtdlAssociateSystemUser(char *screenname, char *loginname) {
336 p = (struct passwd *) getpwnam(loginname);
338 strcpy(screenname, p->pw_gecos);
339 for (a = 0; a < strlen(screenname); ++a) {
340 if (screenname[a] == ',') {
352 * Back end for cmd_user() and its ilk
354 int CtdlLoginExistingUser(char *trythisname)
358 struct recptypes *valid = NULL;
360 if (trythisname == NULL) return login_not_found;
361 safestrncpy(username, trythisname, USERNAME_SIZE);
364 if ((CC->logged_in)) {
365 return login_already_logged_in;
368 /* First, try to log in as if the supplied name is a display name */
369 found_user = getuser(&CC->user, username);
371 /* If that didn't work, try to log in as if the supplied name
372 * is an e-mail address
374 if (found_user != 0) {
375 valid = validate_recipients(username);
377 if (valid->num_local == 1) {
378 found_user = getuser(&CC->user,
385 #ifdef ENABLE_AUTOLOGIN
386 /* If we haven't found the account yet, and the supplied name
387 * is a login name on the underlying host system, create the
390 if (found_user != 0) {
391 struct passwd *p = (struct passwd *) getpwnam(username);
394 create_user(username, 0);
395 found_user = getuser(&CC->user, username);
398 #endif /* ENABLE_AUTOLOGIN */
400 /* Did we find something? */
401 if (found_user == 0) {
402 if (((CC->nologin)) && (CC->user.axlevel < 6)) {
403 return login_too_many_users;
405 safestrncpy(CC->curr_user, CC->user.fullname,
406 sizeof CC->curr_user);
410 return login_not_found;
418 void cmd_user(char *cmdbuf)
423 extract_token(username, cmdbuf, 0, '|', sizeof username);
426 a = CtdlLoginExistingUser(username);
428 case login_already_logged_in:
429 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
431 case login_too_many_users:
433 "Too many users are already online "
435 ERROR + MAX_SESSIONS_EXCEEDED,
436 config.c_nodename, config.c_maxsessions);
439 cprintf("%d Password required for %s\n",
440 MORE_DATA, CC->curr_user);
442 case login_not_found:
443 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
446 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
453 * session startup code which is common to both cmd_pass() and cmd_newu()
455 void session_startup(void)
459 lprintf(CTDL_NOTICE, "<%s> logged in\n", CC->curr_user);
461 lgetuser(&CC->user, CC->curr_user);
462 ++(CC->user.timescalled);
463 CC->previous_login = CC->user.lastcall;
464 time(&CC->user.lastcall);
466 /* If this user's name is the name of the system administrator
467 * (as specified in setup), automatically assign access level 6.
469 if (!strcasecmp(CC->user.fullname, config.c_sysadm)) {
470 CC->user.axlevel = 6;
475 * Populate CC->cs_inet_email with a default address. This will be
476 * overwritten with the user's directory address, if one exists, when
477 * the vCard module's login hook runs.
479 snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
480 CC->user.fullname, config.c_fqdn);
481 for (i=0; i<strlen(CC->cs_inet_email); ++i) {
482 if (isspace(CC->cs_inet_email[i])) {
483 CC->cs_inet_email[i] = '_';
487 /* Create any personal rooms required by the system.
488 * (Technically, MAILROOM should be there already, but just in case...)
490 create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
491 create_room(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
493 /* Run any startup routines registered by loadable modules */
494 PerformSessionHooks(EVT_LOGIN);
496 /* Enter the lobby */
497 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
501 void logged_in_response(void)
503 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
504 CIT_OK, CC->user.fullname, CC->user.axlevel,
505 CC->user.timescalled, CC->user.posted,
506 CC->user.flags, CC->user.usernum,
513 * misc things to be taken care of when a user is logged out
515 void logout(struct CitContext *who)
518 * Clear out some session data. Most likely, the CitContext for this
519 * session is about to get nuked when the session disconnects, but
520 * since it's possible to log in again without reconnecting, we cannot
521 * make that assumption.
523 strcpy(who->fake_username, "");
524 strcpy(who->fake_postname, "");
525 strcpy(who->fake_hostname, "");
526 strcpy(who->fake_roomname, "");
530 * If there is a download in progress, abort it.
532 if (who->download_fp != NULL) {
533 fclose(who->download_fp);
534 who->download_fp = NULL;
538 * If there is an upload in progress, abort it.
540 if (who->upload_fp != NULL) {
545 * If we were talking to a network node, we're not anymore...
547 if (strlen(who->net_node) > 0) {
548 network_talking_to(who->net_node, NTT_REMOVE);
551 /* Do modular stuff... */
552 PerformSessionHooks(EVT_LOGOUT);
554 /* Free any output buffers */
555 if (who->output_buffer != NULL) {
562 * an alternate version of validpw() which executes `chkpwd' instead of
563 * verifying the password directly
565 static int validpw(uid_t uid, const char *pass)
568 int status, pipev[2];
572 lprintf(CTDL_ERR, "pipe failed (%s): denying autologin access for "
573 "uid %ld\n", strerror(errno), (long)uid);
576 switch (pid = fork()) {
578 lprintf(CTDL_ERR, "fork failed (%s): denying autologin access for "
579 "uid %ld\n", strerror(errno), (long)uid);
586 if (dup2(pipev[0], 0) == -1) {
592 execl(CTDLDIR "/chkpwd", CTDLDIR "/chkpwd", NULL);
593 perror(CTDLDIR "/chkpwd");
599 snprintf(buf, sizeof buf, "%lu\n", (unsigned long) uid));
600 write(pipev[1], pass, strlen(pass));
601 write(pipev[1], "\n", 1);
604 while (waitpid(pid, &status, 0) == -1)
605 if (errno != EINTR) {
606 lprintf(CTDL_ERR, "waitpid failed (%s): denying autologin "
607 "access for uid %ld\n",
608 strerror(errno), (long)uid);
611 if (WIFEXITED(status) && !WEXITSTATUS(status))
625 int CtdlTryPassword(char *password)
629 if ((CC->logged_in)) {
630 lprintf(CTDL_WARNING, "CtdlTryPassword: already logged in\n");
631 return pass_already_logged_in;
633 if (!strcmp(CC->curr_user, NLI)) {
634 lprintf(CTDL_WARNING, "CtdlTryPassword: no user selected\n");
637 if (getuser(&CC->user, CC->curr_user)) {
638 lprintf(CTDL_ERR, "CtdlTryPassword: internal error\n");
639 return pass_internal_error;
641 if (password == NULL) {
642 lprintf(CTDL_INFO, "CtdlTryPassword: NULL password string supplied\n");
643 return pass_wrong_password;
648 #ifdef ENABLE_AUTOLOGIN
649 /* A uid of CTDLUID or -1 indicates that this user exists only in
650 * Citadel, not in the underlying operating system.
652 if ( (CC->user.uid == CTDLUID) || (CC->user.uid == (-1)) ) {
654 strproc(CC->user.password);
655 code = strcasecmp(CC->user.password, password);
657 /* Any other uid means we have to check the system password database */
659 if (validpw(CC->user.uid, password)) {
661 lgetuser(&CC->user, CC->curr_user);
662 safestrncpy(CC->user.password, password,
663 sizeof CC->user.password);
668 #else /* ENABLE_AUTOLOGIN */
670 strproc(CC->user.password);
671 code = strcasecmp(CC->user.password, password);
673 #endif /* ENABLE_AUTOLOGIN */
679 lprintf(CTDL_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
680 return pass_wrong_password;
685 void cmd_pass(char *buf)
690 extract_token(password, buf, 0, '|', sizeof password);
691 a = CtdlTryPassword(password);
694 case pass_already_logged_in:
695 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
698 cprintf("%d You must send a name with USER first.\n",
699 ERROR + USERNAME_REQUIRED);
701 case pass_wrong_password:
702 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
705 logged_in_response();
713 * Delete a user record *and* all of its related resources.
715 int purge_user(char pname[])
718 struct ctdluser usbuf;
719 char usernamekey[USERNAME_SIZE];
720 struct CitContext *ccptr;
721 int user_is_logged_in = 0;
723 makeuserkey(usernamekey, pname);
725 if (getuser(&usbuf, pname) != 0) {
726 lprintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
727 return (ERROR + NO_SUCH_USER);
729 /* Don't delete a user who is currently logged in. Instead, just
730 * set the access level to 0, and let the account get swept up
731 * during the next purge.
733 user_is_logged_in = 0;
734 begin_critical_section(S_SESSION_TABLE);
735 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
736 if (ccptr->user.usernum == usbuf.usernum) {
737 user_is_logged_in = 1;
740 end_critical_section(S_SESSION_TABLE);
741 if (user_is_logged_in == 1) {
742 lprintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
747 lprintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
749 /* Perform any purge functions registered by server extensions */
750 PerformUserHooks(&usbuf, EVT_PURGEUSER);
752 /* delete any existing user/room relationships */
753 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
755 /* delete the userlog entry */
756 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
758 /* remove the user's bio file */
759 snprintf(filename, sizeof filename, "./bio/%ld", usbuf.usernum);
762 /* remove the user's picture */
763 snprintf(filename, sizeof filename, "./userpics/%ld.gif", usbuf.usernum);
771 * create_user() - back end processing to create a new user
773 * Set 'newusername' to the desired account name.
774 * Set 'become_user' to nonzero if this is self-service account creation and we want
775 * to actually log in as the user we just created, otherwise set it to 0.
777 int create_user(char *newusername, int become_user)
779 struct ctdluser usbuf;
780 struct ctdlroom qrbuf;
782 char mailboxname[ROOMNAMELEN];
785 safestrncpy(username, newusername, sizeof username);
788 #ifdef ENABLE_AUTOLOGIN
790 struct passwd *p = (struct passwd *) getpwnam(username);
793 extract_token(username, p->pw_gecos, 0, ',', sizeof username);
803 if (!getuser(&usbuf, username)) {
804 return (ERROR + ALREADY_EXISTS);
807 /* Go ahead and initialize a new user record */
808 memset(&usbuf, 0, sizeof(struct ctdluser));
809 safestrncpy(usbuf.fullname, username, sizeof usbuf.fullname);
810 strcpy(usbuf.password, "");
813 /* These are the default flags on new accounts */
814 usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
816 usbuf.timescalled = 0;
818 usbuf.axlevel = config.c_initax;
819 usbuf.USscreenwidth = 80;
820 usbuf.USscreenheight = 24;
821 usbuf.lastcall = time(NULL);
823 /* fetch a new user number */
824 usbuf.usernum = get_new_user_number();
826 /* The very first user created on the system will always be an Aide */
827 if (usbuf.usernum == 1L) {
831 /* add user to userlog */
835 * Give the user a private mailbox and a configuration room.
836 * Make the latter an invisible system room.
838 MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
839 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
841 MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
842 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
843 if (lgetroom(&qrbuf, mailboxname) == 0) {
844 qrbuf.QRflags2 |= QR2_SYSTEM;
848 /* Perform any create functions registered by server extensions */
849 PerformUserHooks(&usbuf, EVT_NEWUSER);
851 /* Everything below this line can be bypassed if administratively
852 * creating a user, instead of doing self-service account creation
856 /* Now become the user we just created */
857 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
858 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
861 /* Check to make sure we're still who we think we are */
862 if (getuser(&CC->user, CC->curr_user)) {
863 return (ERROR + INTERNAL_ERROR);
867 lprintf(CTDL_NOTICE, "New user <%s> created\n", username);
875 * cmd_newu() - create a new user account and log in as that user
877 void cmd_newu(char *cmdbuf)
882 if (config.c_disable_newu) {
883 cprintf("%d Self-service user account creation "
884 "is disabled on this system.\n", ERROR + NOT_HERE);
889 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
893 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
894 ERROR + MAX_SESSIONS_EXCEEDED,
895 config.c_nodename, config.c_maxsessions);
897 extract_token(username, cmdbuf, 0, '|', sizeof username);
901 if (strlen(username) == 0) {
902 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
906 if ((!strcasecmp(username, "bbs")) ||
907 (!strcasecmp(username, "new")) ||
908 (!strcasecmp(username, "."))) {
909 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
913 a = create_user(username, 1);
917 logged_in_response();
918 } else if (a == ERROR + ALREADY_EXISTS) {
919 cprintf("%d '%s' already exists.\n",
920 ERROR + ALREADY_EXISTS, username);
922 } else if (a == ERROR + INTERNAL_ERROR) {
923 cprintf("%d Internal error - user record disappeared?\n",
924 ERROR + INTERNAL_ERROR);
927 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
936 void cmd_setp(char *new_pw)
938 if (CtdlAccessCheck(ac_logged_in)) {
941 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
942 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
946 if (strlen(new_pw) == 0) {
947 cprintf("%d Password unchanged.\n", CIT_OK);
950 lgetuser(&CC->user, CC->curr_user);
951 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
953 cprintf("%d Password changed.\n", CIT_OK);
954 lprintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
955 PerformSessionHooks(EVT_SETPASS);
960 * cmd_creu() - administratively create a new user account (do not log in to it)
962 void cmd_creu(char *cmdbuf)
969 if (CtdlAccessCheck(ac_aide)) {
973 extract_token(username, cmdbuf, 0, '|', sizeof username);
974 extract_token(password, cmdbuf, 1, '|', sizeof password);
980 if (strlen(username) == 0) {
981 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
985 a = create_user(username, 0);
988 if (strlen(password) > 0) {
989 lgetuser(&tmp, username);
990 safestrncpy(tmp.password, password, sizeof(tmp.password));
993 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
994 (strlen(password) > 0) ? "and password set" :
997 } else if (a == ERROR + ALREADY_EXISTS) {
998 cprintf("%d '%s' already exists.\n",
999 ERROR + ALREADY_EXISTS, username);
1002 cprintf("%d An error occured creating the user account.\n", ERROR + INTERNAL_ERROR);
1009 * get user parameters
1014 if (CtdlAccessCheck(ac_logged_in))
1017 getuser(&CC->user, CC->curr_user);
1018 cprintf("%d %d|%d|%d|\n",
1020 CC->user.USscreenwidth,
1021 CC->user.USscreenheight,
1022 (CC->user.flags & US_USER_SET)
1027 * set user parameters
1029 void cmd_setu(char *new_parms)
1031 if (CtdlAccessCheck(ac_logged_in))
1034 if (num_parms(new_parms) < 3) {
1035 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1038 lgetuser(&CC->user, CC->curr_user);
1039 CC->user.USscreenwidth = extract_int(new_parms, 0);
1040 CC->user.USscreenheight = extract_int(new_parms, 1);
1041 CC->user.flags = CC->user.flags & (~US_USER_SET);
1042 CC->user.flags = CC->user.flags |
1043 (extract_int(new_parms, 2) & US_USER_SET);
1045 lputuser(&CC->user);
1046 cprintf("%d Ok\n", CIT_OK);
1050 * set last read pointer
1052 void cmd_slrp(char *new_ptr)
1056 struct visit original_vbuf;
1058 if (CtdlAccessCheck(ac_logged_in)) {
1062 if (!strncasecmp(new_ptr, "highest", 7)) {
1063 newlr = CC->room.QRhighest;
1065 newlr = atol(new_ptr);
1068 lgetuser(&CC->user, CC->curr_user);
1070 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1071 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1072 vbuf.v_lastseen = newlr;
1073 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1075 /* Only rewrite the record if it changed */
1076 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1077 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1078 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1081 lputuser(&CC->user);
1082 cprintf("%d %ld\n", CIT_OK, newlr);
1086 void cmd_seen(char *argbuf) {
1087 long target_msgnum = 0L;
1088 int target_setting = 0;
1090 if (CtdlAccessCheck(ac_logged_in)) {
1094 if (num_parms(argbuf) != 2) {
1095 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1099 target_msgnum = extract_long(argbuf, 0);
1100 target_setting = extract_int(argbuf, 1);
1102 CtdlSetSeen(target_msgnum, target_setting, ctdlsetseen_seen);
1103 cprintf("%d OK\n", CIT_OK);
1107 void cmd_gtsn(char *argbuf) {
1110 if (CtdlAccessCheck(ac_logged_in)) {
1114 CtdlGetSeen(buf, ctdlsetseen_seen);
1115 cprintf("%d %s\n", CIT_OK, buf);
1120 * API function for cmd_invt_kick() and anything else that needs to
1121 * invite or kick out a user to/from a room.
1123 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1125 int CtdlInvtKick(char *iuser, int op) {
1126 struct ctdluser USscratch;
1130 if (getuser(&USscratch, iuser) != 0) {
1134 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1136 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1137 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1140 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1141 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1143 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1145 /* post a message in Aide> saying what we just did */
1146 snprintf(bbb, sizeof bbb, "%s %s %s> by %s\n",
1148 ((op == 1) ? "invited to" : "kicked out of"),
1158 * INVT and KICK commands
1160 void cmd_invt_kick(char *iuser, int op) {
1163 * These commands are only allowed by aides, room aides,
1164 * and room namespace owners
1167 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1168 /* access granted */
1171 cprintf("%d Higher access or room ownership required.\n",
1172 ERROR + HIGHER_ACCESS_REQUIRED);
1176 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1178 cprintf("%d Can't add/remove users from this room.\n",
1183 if (CtdlInvtKick(iuser, op) != 0) {
1184 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1188 cprintf("%d %s %s %s.\n",
1190 ((op == 1) ? "invited to" : "kicked out of"),
1197 * Forget (Zap) the current room (API call)
1198 * Returns 0 on success
1200 int CtdlForgetThisRoom(void) {
1203 /* On some systems, Aides are not allowed to forget rooms */
1204 if (is_aide() && (config.c_aide_zap == 0)
1205 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1209 lgetuser(&CC->user, CC->curr_user);
1210 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1212 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1213 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1215 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1216 lputuser(&CC->user);
1218 /* Return to the Lobby, so we don't end up in an undefined room */
1219 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1226 * forget (Zap) the current room
1231 if (CtdlAccessCheck(ac_logged_in)) {
1235 if (CtdlForgetThisRoom() == 0) {
1236 cprintf("%d Ok\n", CIT_OK);
1239 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1244 * Get Next Unregistered User
1248 struct cdbdata *cdbus;
1249 struct ctdluser usbuf;
1251 if (CtdlAccessCheck(ac_aide)) {
1255 if ((CitControl.MMflags & MM_VALID) == 0) {
1256 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1260 /* There are unvalidated users. Traverse the user database,
1261 * and return the first user we find that needs validation.
1263 cdb_rewind(CDB_USERS);
1264 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1265 memset(&usbuf, 0, sizeof(struct ctdluser));
1266 memcpy(&usbuf, cdbus->ptr,
1267 ((cdbus->len > sizeof(struct ctdluser)) ?
1268 sizeof(struct ctdluser) : cdbus->len));
1270 if ((usbuf.flags & US_NEEDVALID)
1271 && (usbuf.axlevel > 0)) {
1272 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1273 cdb_close_cursor(CDB_USERS);
1278 /* If we get to this point, there are no more unvalidated users.
1279 * Therefore we clear the "users need validation" flag.
1282 begin_critical_section(S_CONTROL);
1284 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1286 end_critical_section(S_CONTROL);
1287 cprintf("%d *** End of registration.\n", CIT_OK);
1296 void cmd_vali(char *v_args)
1300 struct ctdluser userbuf;
1302 extract_token(user, v_args, 0, '|', sizeof user);
1303 newax = extract_int(v_args, 1);
1305 if (CtdlAccessCheck(ac_aide)) {
1309 if (lgetuser(&userbuf, user) != 0) {
1310 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1314 userbuf.axlevel = newax;
1315 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1319 /* If the access level was set to zero, delete the user */
1321 if (purge_user(user) == 0) {
1322 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1326 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1332 * Traverse the user file...
1334 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1337 struct ctdluser usbuf;
1338 struct cdbdata *cdbus;
1340 cdb_rewind(CDB_USERS);
1342 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1343 memset(&usbuf, 0, sizeof(struct ctdluser));
1344 memcpy(&usbuf, cdbus->ptr,
1345 ((cdbus->len > sizeof(struct ctdluser)) ?
1346 sizeof(struct ctdluser) : cdbus->len));
1348 (*CallBack) (&usbuf, in_data);
1354 * List one user (this works with cmd_list)
1356 void ListThisUser(struct ctdluser *usbuf, void *data)
1358 if (usbuf->axlevel > 0) {
1359 if ((CC->user.axlevel >= 6)
1360 || ((usbuf->flags & US_UNLISTED) == 0)
1361 || ((CC->internal_pgm))) {
1362 cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1366 (long)usbuf->lastcall,
1369 if (CC->user.axlevel >= 6)
1370 cprintf("%s", usbuf->password);
1381 cprintf("%d \n", LISTING_FOLLOWS);
1382 ForEachUser(ListThisUser, NULL);
1390 * assorted info we need to check at login
1398 if (CtdlAccessCheck(ac_logged_in)) {
1402 getuser(&CC->user, CC->curr_user); /* no lock is needed here */
1403 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1406 if (CC->user.axlevel >= 6) {
1408 if (CitControl.MMflags & MM_VALID)
1412 /* check for mail */
1413 mail = InitialMailCheck();
1415 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1420 * check to see if a user exists
1422 void cmd_qusr(char *who)
1424 struct ctdluser usbuf;
1426 if (getuser(&usbuf, who) == 0) {
1427 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1429 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1435 * Administrative Get User Parameters
1437 void cmd_agup(char *cmdbuf)
1439 struct ctdluser usbuf;
1440 char requested_user[128];
1442 if (CtdlAccessCheck(ac_aide)) {
1446 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1447 if (getuser(&usbuf, requested_user) != 0) {
1448 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1451 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1458 (int) usbuf.axlevel,
1460 (long)usbuf.lastcall,
1467 * Administrative Set User Parameters
1469 void cmd_asup(char *cmdbuf)
1471 struct ctdluser usbuf;
1472 char requested_user[128];
1478 if (CtdlAccessCheck(ac_aide))
1481 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1482 if (lgetuser(&usbuf, requested_user) != 0) {
1483 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1486 np = num_parms(cmdbuf);
1488 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1490 usbuf.flags = extract_int(cmdbuf, 2);
1492 usbuf.timescalled = extract_int(cmdbuf, 3);
1494 usbuf.posted = extract_int(cmdbuf, 4);
1496 newax = extract_int(cmdbuf, 5);
1497 if ((newax >= 0) && (newax <= 6)) {
1498 usbuf.axlevel = extract_int(cmdbuf, 5);
1502 usbuf.lastcall = extract_long(cmdbuf, 7);
1505 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1508 if (usbuf.axlevel == 0) {
1509 if (purge_user(requested_user) == 0) {
1515 sprintf(notify, "User <%s> deleted by %s\n",
1516 usbuf.fullname, CC->user.fullname);
1517 aide_message(notify);
1520 cprintf("%d Ok", CIT_OK);
1522 cprintf(" (%s deleted)", requested_user);
1529 * Check to see if the user who we just sent mail to is logged in. If yes,
1530 * bump the 'new mail' counter for their session. That enables them to
1531 * receive a new mail notification without having to hit the database.
1533 void BumpNewMailCounter(long which_user) {
1534 struct CitContext *ptr;
1536 begin_critical_section(S_SESSION_TABLE);
1538 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1539 if (ptr->user.usernum == which_user) {
1544 end_critical_section(S_SESSION_TABLE);
1549 * Count the number of new mail messages the user has
1553 int num_newmsgs = 0;
1555 num_newmsgs = CC->newmail;
1558 return (num_newmsgs);
1563 * Count the number of new mail messages the user has
1565 int InitialMailCheck()
1567 int num_newmsgs = 0;
1569 char mailboxname[ROOMNAMELEN];
1570 struct ctdlroom mailbox;
1572 struct cdbdata *cdbfr;
1573 long *msglist = NULL;
1576 MailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1577 if (getroom(&mailbox, mailboxname) != 0)
1579 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1581 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1583 if (cdbfr != NULL) {
1584 msglist = malloc(cdbfr->len);
1585 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1586 num_msgs = cdbfr->len / sizeof(long);
1590 for (a = 0; a < num_msgs; ++a) {
1591 if (msglist[a] > 0L) {
1592 if (msglist[a] > vbuf.v_lastseen) {
1597 if (msglist != NULL)
1600 return (num_newmsgs);
1606 * Set the preferred view for the current user/room combination
1608 void cmd_view(char *cmdbuf) {
1612 if (CtdlAccessCheck(ac_logged_in)) {
1616 requested_view = extract_int(cmdbuf, 0);
1618 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1619 vbuf.v_view = requested_view;
1620 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1622 cprintf("%d ok\n", CIT_OK);