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\n", pd.pw_uid);
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;
480 * Populate CC->cs_inet_email with a default address. This will be
481 * overwritten with the user's directory address, if one exists, when
482 * the vCard module's login hook runs.
484 snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
485 CC->user.fullname, config.c_fqdn);
486 for (i=0; i<strlen(CC->cs_inet_email); ++i) {
487 if (isspace(CC->cs_inet_email[i])) {
488 CC->cs_inet_email[i] = '_';
492 /* Create any personal rooms required by the system.
493 * (Technically, MAILROOM should be there already, but just in case...)
495 create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
496 create_room(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
497 create_room(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
499 /* Run any startup routines registered by loadable modules */
500 PerformSessionHooks(EVT_LOGIN);
502 /* Enter the lobby */
503 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
507 void logged_in_response(void)
509 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
510 CIT_OK, CC->user.fullname, CC->user.axlevel,
511 CC->user.timescalled, CC->user.posted,
512 CC->user.flags, CC->user.usernum,
519 * misc things to be taken care of when a user is logged out
521 void logout(struct CitContext *who)
524 * Clear out some session data. Most likely, the CitContext for this
525 * session is about to get nuked when the session disconnects, but
526 * since it's possible to log in again without reconnecting, we cannot
527 * make that assumption.
529 strcpy(who->fake_username, "");
530 strcpy(who->fake_postname, "");
531 strcpy(who->fake_hostname, "");
532 strcpy(who->fake_roomname, "");
536 * If there is a download in progress, abort it.
538 if (who->download_fp != NULL) {
539 fclose(who->download_fp);
540 who->download_fp = NULL;
544 * If there is an upload in progress, abort it.
546 if (who->upload_fp != NULL) {
551 * If we were talking to a network node, we're not anymore...
553 if (strlen(who->net_node) > 0) {
554 network_talking_to(who->net_node, NTT_REMOVE);
557 /* Do modular stuff... */
558 PerformSessionHooks(EVT_LOGOUT);
560 /* Free any output buffers */
561 if (who->output_buffer != NULL) {
568 * an alternate version of validpw() which executes `chkpwd' instead of
569 * verifying the password directly
571 static int validpw(uid_t uid, const char *pass)
574 int status, pipev[2];
578 lprintf(CTDL_ERR, "pipe failed (%s): denying autologin access for "
579 "uid %ld\n", strerror(errno), (long)uid);
582 switch (pid = fork()) {
584 lprintf(CTDL_ERR, "fork failed (%s): denying autologin access for "
585 "uid %ld\n", strerror(errno), (long)uid);
592 if (dup2(pipev[0], 0) == -1) {
598 execl(CTDLDIR "/chkpwd", CTDLDIR "/chkpwd", NULL);
599 perror(CTDLDIR "/chkpwd");
605 snprintf(buf, sizeof buf, "%lu\n", (unsigned long) uid));
606 write(pipev[1], pass, strlen(pass));
607 write(pipev[1], "\n", 1);
610 while (waitpid(pid, &status, 0) == -1)
611 if (errno != EINTR) {
612 lprintf(CTDL_ERR, "waitpid failed (%s): denying autologin "
613 "access for uid %ld\n",
614 strerror(errno), (long)uid);
617 if (WIFEXITED(status) && !WEXITSTATUS(status))
631 int CtdlTryPassword(char *password)
635 if ((CC->logged_in)) {
636 lprintf(CTDL_WARNING, "CtdlTryPassword: already logged in\n");
637 return pass_already_logged_in;
639 if (!strcmp(CC->curr_user, NLI)) {
640 lprintf(CTDL_WARNING, "CtdlTryPassword: no user selected\n");
643 if (getuser(&CC->user, CC->curr_user)) {
644 lprintf(CTDL_ERR, "CtdlTryPassword: internal error\n");
645 return pass_internal_error;
647 if (password == NULL) {
648 lprintf(CTDL_INFO, "CtdlTryPassword: NULL password string supplied\n");
649 return pass_wrong_password;
654 #ifdef ENABLE_AUTOLOGIN
656 if (validpw(CC->user.uid, password)) {
658 /* we could get rid of this */
659 lgetuser(&CC->user, CC->curr_user);
660 safestrncpy(CC->user.password, password, sizeof CC->user.password);
668 #else /* ENABLE_AUTOLOGIN */
671 strproc(CC->user.password);
672 code = strcasecmp(CC->user.password, password);
674 strproc(CC->user.password);
675 code = strcasecmp(CC->user.password, password);
677 #endif /* ENABLE_AUTOLOGIN */
683 lprintf(CTDL_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
684 return pass_wrong_password;
689 void cmd_pass(char *buf)
694 extract_token(password, buf, 0, '|', sizeof password);
695 a = CtdlTryPassword(password);
698 case pass_already_logged_in:
699 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
702 cprintf("%d You must send a name with USER first.\n",
703 ERROR + USERNAME_REQUIRED);
705 case pass_wrong_password:
706 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
709 logged_in_response();
717 * Delete a user record *and* all of its related resources.
719 int purge_user(char pname[])
722 struct ctdluser usbuf;
723 char usernamekey[USERNAME_SIZE];
724 struct CitContext *ccptr;
725 int user_is_logged_in = 0;
727 makeuserkey(usernamekey, pname);
729 if (getuser(&usbuf, pname) != 0) {
730 lprintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
731 return (ERROR + NO_SUCH_USER);
733 /* Don't delete a user who is currently logged in. Instead, just
734 * set the access level to 0, and let the account get swept up
735 * during the next purge.
737 user_is_logged_in = 0;
738 begin_critical_section(S_SESSION_TABLE);
739 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
740 if (ccptr->user.usernum == usbuf.usernum) {
741 user_is_logged_in = 1;
744 end_critical_section(S_SESSION_TABLE);
745 if (user_is_logged_in == 1) {
746 lprintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
751 lprintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
753 /* Perform any purge functions registered by server extensions */
754 PerformUserHooks(&usbuf, EVT_PURGEUSER);
756 /* delete any existing user/room relationships */
757 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
759 /* delete the userlog entry */
760 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
762 /* remove the user's bio file */
770 /* remove the user's picture */
783 * create_user() - back end processing to create a new user
785 * Set 'newusername' to the desired account name.
786 * Set 'become_user' to nonzero if this is self-service account creation and we want
787 * to actually log in as the user we just created, otherwise set it to 0.
789 int create_user(char *newusername, int become_user)
791 struct ctdluser usbuf;
792 struct ctdlroom qrbuf;
794 char mailboxname[ROOMNAMELEN];
797 safestrncpy(username, newusername, sizeof username);
800 #ifdef ENABLE_AUTOLOGIN
802 struct passwd *p = (struct passwd *) getpwnam(username);
805 extract_token(username, p->pw_gecos, 0, ',', sizeof username);
817 if (!getuser(&usbuf, username)) {
818 return (ERROR + ALREADY_EXISTS);
821 /* Go ahead and initialize a new user record */
822 memset(&usbuf, 0, sizeof(struct ctdluser));
823 safestrncpy(usbuf.fullname, username, sizeof usbuf.fullname);
824 strcpy(usbuf.password, "");
827 /* These are the default flags on new accounts */
828 usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
830 usbuf.timescalled = 0;
832 usbuf.axlevel = config.c_initax;
833 usbuf.USscreenwidth = 80;
834 usbuf.USscreenheight = 24;
835 usbuf.lastcall = time(NULL);
837 /* fetch a new user number */
838 usbuf.usernum = get_new_user_number();
840 /* The very first user created on the system will always be an Aide */
841 if (usbuf.usernum == 1L) {
845 /* add user to userlog */
849 * Give the user a private mailbox and a configuration room.
850 * Make the latter an invisible system room.
852 MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
853 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
855 MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
856 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
857 if (lgetroom(&qrbuf, mailboxname) == 0) {
858 qrbuf.QRflags2 |= QR2_SYSTEM;
862 /* Perform any create functions registered by server extensions */
863 PerformUserHooks(&usbuf, EVT_NEWUSER);
865 /* Everything below this line can be bypassed if administratively
866 * creating a user, instead of doing self-service account creation
870 /* Now become the user we just created */
871 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
872 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
875 /* Check to make sure we're still who we think we are */
876 if (getuser(&CC->user, CC->curr_user)) {
877 return (ERROR + INTERNAL_ERROR);
881 lprintf(CTDL_NOTICE, "New user <%s> created\n", username);
889 * cmd_newu() - create a new user account and log in as that user
891 void cmd_newu(char *cmdbuf)
896 #ifdef ENABLE_AUTOLOGIN
897 cprintf("%d This system does not use native mode authentication.\n",
900 #endif /* ENABLE_AUTOLOGIN */
902 if (config.c_disable_newu) {
903 cprintf("%d Self-service user account creation "
904 "is disabled on this system.\n", ERROR + NOT_HERE);
909 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
913 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
914 ERROR + MAX_SESSIONS_EXCEEDED,
915 config.c_nodename, config.c_maxsessions);
917 extract_token(username, cmdbuf, 0, '|', sizeof username);
921 if (strlen(username) == 0) {
922 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
926 if ((!strcasecmp(username, "bbs")) ||
927 (!strcasecmp(username, "new")) ||
928 (!strcasecmp(username, "."))) {
929 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
933 a = create_user(username, 1);
937 logged_in_response();
938 } else if (a == ERROR + ALREADY_EXISTS) {
939 cprintf("%d '%s' already exists.\n",
940 ERROR + ALREADY_EXISTS, username);
942 } else if (a == ERROR + INTERNAL_ERROR) {
943 cprintf("%d Internal error - user record disappeared?\n",
944 ERROR + INTERNAL_ERROR);
947 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
956 void cmd_setp(char *new_pw)
958 if (CtdlAccessCheck(ac_logged_in)) {
961 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
962 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
966 if (strlen(new_pw) == 0) {
967 cprintf("%d Password unchanged.\n", CIT_OK);
970 lgetuser(&CC->user, CC->curr_user);
971 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
973 cprintf("%d Password changed.\n", CIT_OK);
974 lprintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
975 PerformSessionHooks(EVT_SETPASS);
980 * cmd_creu() - administratively create a new user account (do not log in to it)
982 void cmd_creu(char *cmdbuf)
989 if (CtdlAccessCheck(ac_aide)) {
993 extract_token(username, cmdbuf, 0, '|', sizeof username);
994 extract_token(password, cmdbuf, 1, '|', sizeof password);
1000 if (strlen(username) == 0) {
1001 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1005 a = create_user(username, 0);
1008 if (strlen(password) > 0) {
1009 lgetuser(&tmp, username);
1010 safestrncpy(tmp.password, password, sizeof(tmp.password));
1013 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1014 (strlen(password) > 0) ? "and password set" :
1015 "with no password");
1017 } else if (a == ERROR + ALREADY_EXISTS) {
1018 cprintf("%d '%s' already exists.\n",
1019 ERROR + ALREADY_EXISTS, username);
1022 cprintf("%d An error occured creating the user account.\n", ERROR + INTERNAL_ERROR);
1029 * get user parameters
1034 if (CtdlAccessCheck(ac_logged_in))
1037 getuser(&CC->user, CC->curr_user);
1038 cprintf("%d %d|%d|%d|\n",
1040 CC->user.USscreenwidth,
1041 CC->user.USscreenheight,
1042 (CC->user.flags & US_USER_SET)
1047 * set user parameters
1049 void cmd_setu(char *new_parms)
1051 if (CtdlAccessCheck(ac_logged_in))
1054 if (num_parms(new_parms) < 3) {
1055 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1058 lgetuser(&CC->user, CC->curr_user);
1059 CC->user.USscreenwidth = extract_int(new_parms, 0);
1060 CC->user.USscreenheight = extract_int(new_parms, 1);
1061 CC->user.flags = CC->user.flags & (~US_USER_SET);
1062 CC->user.flags = CC->user.flags |
1063 (extract_int(new_parms, 2) & US_USER_SET);
1065 lputuser(&CC->user);
1066 cprintf("%d Ok\n", CIT_OK);
1070 * set last read pointer
1072 void cmd_slrp(char *new_ptr)
1076 struct visit original_vbuf;
1078 if (CtdlAccessCheck(ac_logged_in)) {
1082 if (!strncasecmp(new_ptr, "highest", 7)) {
1083 newlr = CC->room.QRhighest;
1085 newlr = atol(new_ptr);
1088 lgetuser(&CC->user, CC->curr_user);
1090 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1091 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1092 vbuf.v_lastseen = newlr;
1093 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1095 /* Only rewrite the record if it changed */
1096 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1097 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1098 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1101 lputuser(&CC->user);
1102 cprintf("%d %ld\n", CIT_OK, newlr);
1106 void cmd_seen(char *argbuf) {
1107 long target_msgnum = 0L;
1108 int target_setting = 0;
1110 if (CtdlAccessCheck(ac_logged_in)) {
1114 if (num_parms(argbuf) != 2) {
1115 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1119 target_msgnum = extract_long(argbuf, 0);
1120 target_setting = extract_int(argbuf, 1);
1122 CtdlSetSeen(&target_msgnum, 1, target_setting,
1123 ctdlsetseen_seen, NULL, NULL);
1124 cprintf("%d OK\n", CIT_OK);
1128 void cmd_gtsn(char *argbuf) {
1131 if (CtdlAccessCheck(ac_logged_in)) {
1135 CtdlGetSeen(buf, ctdlsetseen_seen);
1136 cprintf("%d %s\n", CIT_OK, buf);
1141 * API function for cmd_invt_kick() and anything else that needs to
1142 * invite or kick out a user to/from a room.
1144 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1146 int CtdlInvtKick(char *iuser, int op) {
1147 struct ctdluser USscratch;
1151 if (getuser(&USscratch, iuser) != 0) {
1155 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1157 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1158 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1161 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1162 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1164 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1166 /* post a message in Aide> saying what we just did */
1167 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1169 ((op == 1) ? "invited to" : "kicked out of"),
1179 * INVT and KICK commands
1181 void cmd_invt_kick(char *iuser, int op) {
1184 * These commands are only allowed by aides, room aides,
1185 * and room namespace owners
1188 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1189 /* access granted */
1192 cprintf("%d Higher access or room ownership required.\n",
1193 ERROR + HIGHER_ACCESS_REQUIRED);
1197 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1199 cprintf("%d Can't add/remove users from this room.\n",
1204 if (CtdlInvtKick(iuser, op) != 0) {
1205 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1209 cprintf("%d %s %s %s.\n",
1211 ((op == 1) ? "invited to" : "kicked out of"),
1218 * Forget (Zap) the current room (API call)
1219 * Returns 0 on success
1221 int CtdlForgetThisRoom(void) {
1224 /* On some systems, Aides are not allowed to forget rooms */
1225 if (is_aide() && (config.c_aide_zap == 0)
1226 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1230 lgetuser(&CC->user, CC->curr_user);
1231 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1233 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1234 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1236 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1237 lputuser(&CC->user);
1239 /* Return to the Lobby, so we don't end up in an undefined room */
1240 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1247 * forget (Zap) the current room
1252 if (CtdlAccessCheck(ac_logged_in)) {
1256 if (CtdlForgetThisRoom() == 0) {
1257 cprintf("%d Ok\n", CIT_OK);
1260 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1265 * Get Next Unregistered User
1269 struct cdbdata *cdbus;
1270 struct ctdluser usbuf;
1272 if (CtdlAccessCheck(ac_aide)) {
1276 if ((CitControl.MMflags & MM_VALID) == 0) {
1277 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1281 /* There are unvalidated users. Traverse the user database,
1282 * and return the first user we find that needs validation.
1284 cdb_rewind(CDB_USERS);
1285 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1286 memset(&usbuf, 0, sizeof(struct ctdluser));
1287 memcpy(&usbuf, cdbus->ptr,
1288 ((cdbus->len > sizeof(struct ctdluser)) ?
1289 sizeof(struct ctdluser) : cdbus->len));
1291 if ((usbuf.flags & US_NEEDVALID)
1292 && (usbuf.axlevel > 0)) {
1293 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1294 cdb_close_cursor(CDB_USERS);
1299 /* If we get to this point, there are no more unvalidated users.
1300 * Therefore we clear the "users need validation" flag.
1303 begin_critical_section(S_CONTROL);
1305 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1307 end_critical_section(S_CONTROL);
1308 cprintf("%d *** End of registration.\n", CIT_OK);
1317 void cmd_vali(char *v_args)
1321 struct ctdluser userbuf;
1323 extract_token(user, v_args, 0, '|', sizeof user);
1324 newax = extract_int(v_args, 1);
1326 if (CtdlAccessCheck(ac_aide)) {
1330 if (lgetuser(&userbuf, user) != 0) {
1331 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1335 userbuf.axlevel = newax;
1336 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1340 /* If the access level was set to zero, delete the user */
1342 if (purge_user(user) == 0) {
1343 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1347 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1353 * Traverse the user file...
1355 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1358 struct ctdluser usbuf;
1359 struct cdbdata *cdbus;
1361 cdb_rewind(CDB_USERS);
1363 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1364 memset(&usbuf, 0, sizeof(struct ctdluser));
1365 memcpy(&usbuf, cdbus->ptr,
1366 ((cdbus->len > sizeof(struct ctdluser)) ?
1367 sizeof(struct ctdluser) : cdbus->len));
1369 (*CallBack) (&usbuf, in_data);
1375 * List one user (this works with cmd_list)
1377 void ListThisUser(struct ctdluser *usbuf, void *data)
1381 searchstring = (char *)data;
1382 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1386 if (usbuf->axlevel > 0) {
1387 if ((CC->user.axlevel >= 6)
1388 || ((usbuf->flags & US_UNLISTED) == 0)
1389 || ((CC->internal_pgm))) {
1390 cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1394 (long)usbuf->lastcall,
1397 if (CC->user.axlevel >= 6)
1398 cprintf("%s", usbuf->password);
1405 * List users (searchstring may be empty to list all users)
1407 void cmd_list(char *cmdbuf)
1409 char searchstring[256];
1410 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1411 striplt(searchstring);
1412 cprintf("%d \n", LISTING_FOLLOWS);
1413 ForEachUser(ListThisUser, (void *)searchstring );
1421 * assorted info we need to check at login
1429 if (CtdlAccessCheck(ac_logged_in)) {
1433 getuser(&CC->user, CC->curr_user); /* no lock is needed here */
1434 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1437 if (CC->user.axlevel >= 6) {
1439 if (CitControl.MMflags & MM_VALID)
1443 /* check for mail */
1444 mail = InitialMailCheck();
1446 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1451 * check to see if a user exists
1453 void cmd_qusr(char *who)
1455 struct ctdluser usbuf;
1457 if (getuser(&usbuf, who) == 0) {
1458 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1460 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1466 * Administrative Get User Parameters
1468 void cmd_agup(char *cmdbuf)
1470 struct ctdluser usbuf;
1471 char requested_user[128];
1473 if (CtdlAccessCheck(ac_aide)) {
1477 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1478 if (getuser(&usbuf, requested_user) != 0) {
1479 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1482 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1489 (int) usbuf.axlevel,
1491 (long)usbuf.lastcall,
1498 * Administrative Set User Parameters
1500 void cmd_asup(char *cmdbuf)
1502 struct ctdluser usbuf;
1503 char requested_user[128];
1509 if (CtdlAccessCheck(ac_aide))
1512 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1513 if (lgetuser(&usbuf, requested_user) != 0) {
1514 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1517 np = num_parms(cmdbuf);
1519 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1521 usbuf.flags = extract_int(cmdbuf, 2);
1523 usbuf.timescalled = extract_int(cmdbuf, 3);
1525 usbuf.posted = extract_int(cmdbuf, 4);
1527 newax = extract_int(cmdbuf, 5);
1528 if ((newax >= 0) && (newax <= 6)) {
1529 usbuf.axlevel = extract_int(cmdbuf, 5);
1533 usbuf.lastcall = extract_long(cmdbuf, 7);
1536 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1539 if (usbuf.axlevel == 0) {
1540 if (purge_user(requested_user) == 0) {
1546 sprintf(notify, "User \"%s\" has been deleted by %s.\n",
1547 usbuf.fullname, CC->user.fullname);
1548 aide_message(notify);
1551 cprintf("%d Ok", CIT_OK);
1553 cprintf(" (%s deleted)", requested_user);
1560 * Check to see if the user who we just sent mail to is logged in. If yes,
1561 * bump the 'new mail' counter for their session. That enables them to
1562 * receive a new mail notification without having to hit the database.
1564 void BumpNewMailCounter(long which_user) {
1565 struct CitContext *ptr;
1567 begin_critical_section(S_SESSION_TABLE);
1569 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1570 if (ptr->user.usernum == which_user) {
1575 end_critical_section(S_SESSION_TABLE);
1580 * Count the number of new mail messages the user has
1584 int num_newmsgs = 0;
1586 num_newmsgs = CC->newmail;
1589 return (num_newmsgs);
1594 * Count the number of new mail messages the user has
1596 int InitialMailCheck()
1598 int num_newmsgs = 0;
1600 char mailboxname[ROOMNAMELEN];
1601 struct ctdlroom mailbox;
1603 struct cdbdata *cdbfr;
1604 long *msglist = NULL;
1607 MailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1608 if (getroom(&mailbox, mailboxname) != 0)
1610 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1612 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1614 if (cdbfr != NULL) {
1615 msglist = malloc(cdbfr->len);
1616 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1617 num_msgs = cdbfr->len / sizeof(long);
1621 for (a = 0; a < num_msgs; ++a) {
1622 if (msglist[a] > 0L) {
1623 if (msglist[a] > vbuf.v_lastseen) {
1628 if (msglist != NULL)
1631 return (num_newmsgs);
1637 * Set the preferred view for the current user/room combination
1639 void cmd_view(char *cmdbuf) {
1643 if (CtdlAccessCheck(ac_logged_in)) {
1647 requested_view = extract_int(cmdbuf, 0);
1649 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1650 vbuf.v_view = requested_view;
1651 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1653 cprintf("%d ok\n", CIT_OK);