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 if (strlen(username) == 0) {
359 return login_not_found;
362 #ifdef ENABLE_AUTOLOGIN
364 /* If this is an autologin build, the only valid auth source is the
365 * host operating system.
368 struct passwd *tempPwdPtr;
371 lprintf(CTDL_DEBUG, "asking host about <%s>\n", username);
372 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
373 if (tempPwdPtr == NULL) {
374 return login_not_found;
376 lprintf(CTDL_DEBUG, "found it! uid=%d, gecos=%s\n", pd.pw_uid, pd.pw_gecos);
378 /* Locate the associated Citadel account.
379 * If not found, make one attempt to create it.
381 found_user = getuserbyuid(&CC->user, pd.pw_uid);
382 if (found_user != 0) {
383 create_user(username, 0);
384 found_user = getuserbyuid(&CC->user, pd.pw_uid);
387 #else /* ENABLE_AUTOLOGIN */
388 struct recptypes *valid = NULL;
390 /* First, try to log in as if the supplied name is a display name */
391 found_user = getuser(&CC->user, username);
393 /* If that didn't work, try to log in as if the supplied name
394 * is an e-mail address
396 if (found_user != 0) {
397 valid = validate_recipients(username);
399 if (valid->num_local == 1) {
400 found_user = getuser(&CC->user,
407 #endif /* ENABLE_AUTOLOGIN */
409 /* Did we find something? */
410 if (found_user == 0) {
411 if (((CC->nologin)) && (CC->user.axlevel < 6)) {
412 return login_too_many_users;
414 safestrncpy(CC->curr_user, CC->user.fullname,
415 sizeof CC->curr_user);
419 return login_not_found;
427 void cmd_user(char *cmdbuf)
432 extract_token(username, cmdbuf, 0, '|', sizeof username);
435 a = CtdlLoginExistingUser(username);
437 case login_already_logged_in:
438 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
440 case login_too_many_users:
442 "Too many users are already online "
444 ERROR + MAX_SESSIONS_EXCEEDED,
445 config.c_nodename, config.c_maxsessions);
448 cprintf("%d Password required for %s\n",
449 MORE_DATA, CC->curr_user);
451 case login_not_found:
452 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
455 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
462 * session startup code which is common to both cmd_pass() and cmd_newu()
464 void session_startup(void)
468 lprintf(CTDL_NOTICE, "<%s> logged in\n", CC->curr_user);
470 lgetuser(&CC->user, CC->curr_user);
471 ++(CC->user.timescalled);
472 CC->previous_login = CC->user.lastcall;
473 time(&CC->user.lastcall);
475 /* If this user's name is the name of the system administrator
476 * (as specified in setup), automatically assign access level 6.
478 if (!strcasecmp(CC->user.fullname, config.c_sysadm)) {
479 CC->user.axlevel = 6;
482 #ifdef ENABLE_AUTOLOGIN
483 /* If we're authenticating off the host system, automatically give
484 * root the highest level of access.
486 if (CC->user.uid == 0) {
487 CC->user.axlevel = 6;
494 * Populate CC->cs_inet_email with a default address. This will be
495 * overwritten with the user's directory address, if one exists, when
496 * the vCard module's login hook runs.
498 snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
499 CC->user.fullname, config.c_fqdn);
500 for (i=0; i<strlen(CC->cs_inet_email); ++i) {
501 if (isspace(CC->cs_inet_email[i])) {
502 CC->cs_inet_email[i] = '_';
506 /* Create any personal rooms required by the system.
507 * (Technically, MAILROOM should be there already, but just in case...)
509 create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
510 create_room(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
511 create_room(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
513 /* Run any startup routines registered by loadable modules */
514 PerformSessionHooks(EVT_LOGIN);
516 /* Enter the lobby */
517 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
521 void logged_in_response(void)
523 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
524 CIT_OK, CC->user.fullname, CC->user.axlevel,
525 CC->user.timescalled, CC->user.posted,
526 CC->user.flags, CC->user.usernum,
533 * misc things to be taken care of when a user is logged out
535 void logout(struct CitContext *who)
538 * Clear out some session data. Most likely, the CitContext for this
539 * session is about to get nuked when the session disconnects, but
540 * since it's possible to log in again without reconnecting, we cannot
541 * make that assumption.
543 strcpy(who->fake_username, "");
544 strcpy(who->fake_postname, "");
545 strcpy(who->fake_hostname, "");
546 strcpy(who->fake_roomname, "");
550 * If there is a download in progress, abort it.
552 if (who->download_fp != NULL) {
553 fclose(who->download_fp);
554 who->download_fp = NULL;
558 * If there is an upload in progress, abort it.
560 if (who->upload_fp != NULL) {
565 * If we were talking to a network node, we're not anymore...
567 if (strlen(who->net_node) > 0) {
568 network_talking_to(who->net_node, NTT_REMOVE);
571 /* Do modular stuff... */
572 PerformSessionHooks(EVT_LOGOUT);
574 /* Free any output buffers */
575 if (who->output_buffer != NULL) {
582 * an alternate version of validpw() which executes `chkpwd' instead of
583 * verifying the password directly
585 static int validpw(uid_t uid, const char *pass)
588 int status, pipev[2];
592 lprintf(CTDL_ERR, "pipe failed (%s): denying autologin access for "
593 "uid %ld\n", strerror(errno), (long)uid);
596 switch (pid = fork()) {
598 lprintf(CTDL_ERR, "fork failed (%s): denying autologin access for "
599 "uid %ld\n", strerror(errno), (long)uid);
606 if (dup2(pipev[0], 0) == -1) {
612 execl(CTDLDIR "/chkpwd", CTDLDIR "/chkpwd", NULL);
613 perror(CTDLDIR "/chkpwd");
619 snprintf(buf, sizeof buf, "%lu\n", (unsigned long) uid));
620 write(pipev[1], pass, strlen(pass));
621 write(pipev[1], "\n", 1);
624 while (waitpid(pid, &status, 0) == -1)
625 if (errno != EINTR) {
626 lprintf(CTDL_ERR, "waitpid failed (%s): denying autologin "
627 "access for uid %ld\n",
628 strerror(errno), (long)uid);
631 if (WIFEXITED(status) && !WEXITSTATUS(status))
645 int CtdlTryPassword(char *password)
649 if ((CC->logged_in)) {
650 lprintf(CTDL_WARNING, "CtdlTryPassword: already logged in\n");
651 return pass_already_logged_in;
653 if (!strcmp(CC->curr_user, NLI)) {
654 lprintf(CTDL_WARNING, "CtdlTryPassword: no user selected\n");
657 if (getuser(&CC->user, CC->curr_user)) {
658 lprintf(CTDL_ERR, "CtdlTryPassword: internal error\n");
659 return pass_internal_error;
661 if (password == NULL) {
662 lprintf(CTDL_INFO, "CtdlTryPassword: NULL password string supplied\n");
663 return pass_wrong_password;
668 #ifdef ENABLE_AUTOLOGIN
670 if (validpw(CC->user.uid, password)) {
672 /* we could get rid of this */
673 lgetuser(&CC->user, CC->curr_user);
674 safestrncpy(CC->user.password, password, sizeof CC->user.password);
682 #else /* ENABLE_AUTOLOGIN */
685 strproc(CC->user.password);
686 code = strcasecmp(CC->user.password, password);
688 strproc(CC->user.password);
689 code = strcasecmp(CC->user.password, password);
691 #endif /* ENABLE_AUTOLOGIN */
697 lprintf(CTDL_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
698 return pass_wrong_password;
703 void cmd_pass(char *buf)
708 extract_token(password, buf, 0, '|', sizeof password);
709 a = CtdlTryPassword(password);
712 case pass_already_logged_in:
713 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
716 cprintf("%d You must send a name with USER first.\n",
717 ERROR + USERNAME_REQUIRED);
719 case pass_wrong_password:
720 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
723 logged_in_response();
731 * Delete a user record *and* all of its related resources.
733 int purge_user(char pname[])
736 struct ctdluser usbuf;
737 char usernamekey[USERNAME_SIZE];
738 struct CitContext *ccptr;
739 int user_is_logged_in = 0;
741 makeuserkey(usernamekey, pname);
743 if (getuser(&usbuf, pname) != 0) {
744 lprintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
745 return (ERROR + NO_SUCH_USER);
747 /* Don't delete a user who is currently logged in. Instead, just
748 * set the access level to 0, and let the account get swept up
749 * during the next purge.
751 user_is_logged_in = 0;
752 begin_critical_section(S_SESSION_TABLE);
753 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
754 if (ccptr->user.usernum == usbuf.usernum) {
755 user_is_logged_in = 1;
758 end_critical_section(S_SESSION_TABLE);
759 if (user_is_logged_in == 1) {
760 lprintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
765 lprintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
767 /* Perform any purge functions registered by server extensions */
768 PerformUserHooks(&usbuf, EVT_PURGEUSER);
770 /* delete any existing user/room relationships */
771 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
773 /* delete the userlog entry */
774 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
776 /* remove the user's bio file */
784 /* remove the user's picture */
797 * create_user() - back end processing to create a new user
799 * Set 'newusername' to the desired account name.
800 * Set 'become_user' to nonzero if this is self-service account creation and we want
801 * to actually log in as the user we just created, otherwise set it to 0.
803 int create_user(char *newusername, int become_user)
805 struct ctdluser usbuf;
806 struct ctdlroom qrbuf;
808 char mailboxname[ROOMNAMELEN];
811 safestrncpy(username, newusername, sizeof username);
814 #ifdef ENABLE_AUTOLOGIN
816 struct passwd *tempPwdPtr;
819 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
820 if (tempPwdPtr != NULL) {
821 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
825 return (ERROR + NO_SUCH_USER);
829 if (!getuser(&usbuf, username)) {
830 return (ERROR + ALREADY_EXISTS);
833 /* Go ahead and initialize a new user record */
834 memset(&usbuf, 0, sizeof(struct ctdluser));
835 safestrncpy(usbuf.fullname, username, sizeof usbuf.fullname);
836 strcpy(usbuf.password, "");
839 /* These are the default flags on new accounts */
840 usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
842 usbuf.timescalled = 0;
844 usbuf.axlevel = config.c_initax;
845 usbuf.USscreenwidth = 80;
846 usbuf.USscreenheight = 24;
847 usbuf.lastcall = time(NULL);
849 /* fetch a new user number */
850 usbuf.usernum = get_new_user_number();
852 /* The very first user created on the system will always be an Aide */
853 if (usbuf.usernum == 1L) {
857 /* add user to userlog */
861 * Give the user a private mailbox and a configuration room.
862 * Make the latter an invisible system room.
864 MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
865 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
867 MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
868 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
869 if (lgetroom(&qrbuf, mailboxname) == 0) {
870 qrbuf.QRflags2 |= QR2_SYSTEM;
874 /* Perform any create functions registered by server extensions */
875 PerformUserHooks(&usbuf, EVT_NEWUSER);
877 /* Everything below this line can be bypassed if administratively
878 * creating a user, instead of doing self-service account creation
882 /* Now become the user we just created */
883 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
884 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
887 /* Check to make sure we're still who we think we are */
888 if (getuser(&CC->user, CC->curr_user)) {
889 return (ERROR + INTERNAL_ERROR);
893 lprintf(CTDL_NOTICE, "New user <%s> created\n", username);
901 * cmd_newu() - create a new user account and log in as that user
903 void cmd_newu(char *cmdbuf)
908 #ifdef ENABLE_AUTOLOGIN
909 cprintf("%d This system does not use native mode authentication.\n",
912 #endif /* ENABLE_AUTOLOGIN */
914 if (config.c_disable_newu) {
915 cprintf("%d Self-service user account creation "
916 "is disabled on this system.\n", ERROR + NOT_HERE);
921 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
925 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
926 ERROR + MAX_SESSIONS_EXCEEDED,
927 config.c_nodename, config.c_maxsessions);
929 extract_token(username, cmdbuf, 0, '|', sizeof username);
933 if (strlen(username) == 0) {
934 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
938 if ((!strcasecmp(username, "bbs")) ||
939 (!strcasecmp(username, "new")) ||
940 (!strcasecmp(username, "."))) {
941 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
945 a = create_user(username, 1);
949 logged_in_response();
950 } else if (a == ERROR + ALREADY_EXISTS) {
951 cprintf("%d '%s' already exists.\n",
952 ERROR + ALREADY_EXISTS, username);
954 } else if (a == ERROR + INTERNAL_ERROR) {
955 cprintf("%d Internal error - user record disappeared?\n",
956 ERROR + INTERNAL_ERROR);
959 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
968 void cmd_setp(char *new_pw)
970 if (CtdlAccessCheck(ac_logged_in)) {
973 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
974 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
978 if (strlen(new_pw) == 0) {
979 cprintf("%d Password unchanged.\n", CIT_OK);
982 lgetuser(&CC->user, CC->curr_user);
983 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
985 cprintf("%d Password changed.\n", CIT_OK);
986 lprintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
987 PerformSessionHooks(EVT_SETPASS);
992 * cmd_creu() - administratively create a new user account (do not log in to it)
994 void cmd_creu(char *cmdbuf)
1001 if (CtdlAccessCheck(ac_aide)) {
1005 extract_token(username, cmdbuf, 0, '|', sizeof username);
1006 extract_token(password, cmdbuf, 1, '|', sizeof password);
1012 if (strlen(username) == 0) {
1013 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1017 a = create_user(username, 0);
1020 if (strlen(password) > 0) {
1021 lgetuser(&tmp, username);
1022 safestrncpy(tmp.password, password, sizeof(tmp.password));
1025 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1026 (strlen(password) > 0) ? "and password set" :
1027 "with no password");
1029 } else if (a == ERROR + ALREADY_EXISTS) {
1030 cprintf("%d '%s' already exists.\n",
1031 ERROR + ALREADY_EXISTS, username);
1034 cprintf("%d An error occured creating the user account.\n", ERROR + INTERNAL_ERROR);
1041 * get user parameters
1046 if (CtdlAccessCheck(ac_logged_in))
1049 getuser(&CC->user, CC->curr_user);
1050 cprintf("%d %d|%d|%d|\n",
1052 CC->user.USscreenwidth,
1053 CC->user.USscreenheight,
1054 (CC->user.flags & US_USER_SET)
1059 * set user parameters
1061 void cmd_setu(char *new_parms)
1063 if (CtdlAccessCheck(ac_logged_in))
1066 if (num_parms(new_parms) < 3) {
1067 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1070 lgetuser(&CC->user, CC->curr_user);
1071 CC->user.USscreenwidth = extract_int(new_parms, 0);
1072 CC->user.USscreenheight = extract_int(new_parms, 1);
1073 CC->user.flags = CC->user.flags & (~US_USER_SET);
1074 CC->user.flags = CC->user.flags |
1075 (extract_int(new_parms, 2) & US_USER_SET);
1077 lputuser(&CC->user);
1078 cprintf("%d Ok\n", CIT_OK);
1082 * set last read pointer
1084 void cmd_slrp(char *new_ptr)
1088 struct visit original_vbuf;
1090 if (CtdlAccessCheck(ac_logged_in)) {
1094 if (!strncasecmp(new_ptr, "highest", 7)) {
1095 newlr = CC->room.QRhighest;
1097 newlr = atol(new_ptr);
1100 lgetuser(&CC->user, CC->curr_user);
1102 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1103 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1104 vbuf.v_lastseen = newlr;
1105 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1107 /* Only rewrite the record if it changed */
1108 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1109 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1110 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1113 lputuser(&CC->user);
1114 cprintf("%d %ld\n", CIT_OK, newlr);
1118 void cmd_seen(char *argbuf) {
1119 long target_msgnum = 0L;
1120 int target_setting = 0;
1122 if (CtdlAccessCheck(ac_logged_in)) {
1126 if (num_parms(argbuf) != 2) {
1127 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1131 target_msgnum = extract_long(argbuf, 0);
1132 target_setting = extract_int(argbuf, 1);
1134 CtdlSetSeen(&target_msgnum, 1, target_setting,
1135 ctdlsetseen_seen, NULL, NULL);
1136 cprintf("%d OK\n", CIT_OK);
1140 void cmd_gtsn(char *argbuf) {
1143 if (CtdlAccessCheck(ac_logged_in)) {
1147 CtdlGetSeen(buf, ctdlsetseen_seen);
1148 cprintf("%d %s\n", CIT_OK, buf);
1153 * API function for cmd_invt_kick() and anything else that needs to
1154 * invite or kick out a user to/from a room.
1156 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1158 int CtdlInvtKick(char *iuser, int op) {
1159 struct ctdluser USscratch;
1163 if (getuser(&USscratch, iuser) != 0) {
1167 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1169 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1170 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1173 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1174 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1176 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1178 /* post a message in Aide> saying what we just did */
1179 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1181 ((op == 1) ? "invited to" : "kicked out of"),
1184 aide_message(bbb,"User Admin Message");
1191 * INVT and KICK commands
1193 void cmd_invt_kick(char *iuser, int op) {
1196 * These commands are only allowed by aides, room aides,
1197 * and room namespace owners
1200 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1201 /* access granted */
1204 cprintf("%d Higher access or room ownership required.\n",
1205 ERROR + HIGHER_ACCESS_REQUIRED);
1209 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1211 cprintf("%d Can't add/remove users from this room.\n",
1216 if (CtdlInvtKick(iuser, op) != 0) {
1217 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1221 cprintf("%d %s %s %s.\n",
1223 ((op == 1) ? "invited to" : "kicked out of"),
1230 * Forget (Zap) the current room (API call)
1231 * Returns 0 on success
1233 int CtdlForgetThisRoom(void) {
1236 /* On some systems, Aides are not allowed to forget rooms */
1237 if (is_aide() && (config.c_aide_zap == 0)
1238 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1242 lgetuser(&CC->user, CC->curr_user);
1243 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1245 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1246 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1248 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1249 lputuser(&CC->user);
1251 /* Return to the Lobby, so we don't end up in an undefined room */
1252 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1259 * forget (Zap) the current room
1264 if (CtdlAccessCheck(ac_logged_in)) {
1268 if (CtdlForgetThisRoom() == 0) {
1269 cprintf("%d Ok\n", CIT_OK);
1272 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1277 * Get Next Unregistered User
1281 struct cdbdata *cdbus;
1282 struct ctdluser usbuf;
1284 if (CtdlAccessCheck(ac_aide)) {
1288 if ((CitControl.MMflags & MM_VALID) == 0) {
1289 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1293 /* There are unvalidated users. Traverse the user database,
1294 * and return the first user we find that needs validation.
1296 cdb_rewind(CDB_USERS);
1297 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1298 memset(&usbuf, 0, sizeof(struct ctdluser));
1299 memcpy(&usbuf, cdbus->ptr,
1300 ((cdbus->len > sizeof(struct ctdluser)) ?
1301 sizeof(struct ctdluser) : cdbus->len));
1303 if ((usbuf.flags & US_NEEDVALID)
1304 && (usbuf.axlevel > 0)) {
1305 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1306 cdb_close_cursor(CDB_USERS);
1311 /* If we get to this point, there are no more unvalidated users.
1312 * Therefore we clear the "users need validation" flag.
1315 begin_critical_section(S_CONTROL);
1317 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1319 end_critical_section(S_CONTROL);
1320 cprintf("%d *** End of registration.\n", CIT_OK);
1329 void cmd_vali(char *v_args)
1333 struct ctdluser userbuf;
1335 extract_token(user, v_args, 0, '|', sizeof user);
1336 newax = extract_int(v_args, 1);
1338 if (CtdlAccessCheck(ac_aide)) {
1342 if (lgetuser(&userbuf, user) != 0) {
1343 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1347 userbuf.axlevel = newax;
1348 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1352 /* If the access level was set to zero, delete the user */
1354 if (purge_user(user) == 0) {
1355 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1359 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1365 * Traverse the user file...
1367 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1370 struct ctdluser usbuf;
1371 struct cdbdata *cdbus;
1373 cdb_rewind(CDB_USERS);
1375 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1376 memset(&usbuf, 0, sizeof(struct ctdluser));
1377 memcpy(&usbuf, cdbus->ptr,
1378 ((cdbus->len > sizeof(struct ctdluser)) ?
1379 sizeof(struct ctdluser) : cdbus->len));
1381 (*CallBack) (&usbuf, in_data);
1387 * List one user (this works with cmd_list)
1389 void ListThisUser(struct ctdluser *usbuf, void *data)
1393 searchstring = (char *)data;
1394 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1398 if (usbuf->axlevel > 0) {
1399 if ((CC->user.axlevel >= 6)
1400 || ((usbuf->flags & US_UNLISTED) == 0)
1401 || ((CC->internal_pgm))) {
1402 cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1406 (long)usbuf->lastcall,
1409 if (CC->user.axlevel >= 6)
1410 cprintf("%s", usbuf->password);
1417 * List users (searchstring may be empty to list all users)
1419 void cmd_list(char *cmdbuf)
1421 char searchstring[256];
1422 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1423 striplt(searchstring);
1424 cprintf("%d \n", LISTING_FOLLOWS);
1425 ForEachUser(ListThisUser, (void *)searchstring );
1433 * assorted info we need to check at login
1441 if (CtdlAccessCheck(ac_logged_in)) {
1445 getuser(&CC->user, CC->curr_user); /* no lock is needed here */
1446 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1449 if (CC->user.axlevel >= 6) {
1451 if (CitControl.MMflags & MM_VALID)
1455 /* check for mail */
1456 mail = InitialMailCheck();
1458 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1463 * check to see if a user exists
1465 void cmd_qusr(char *who)
1467 struct ctdluser usbuf;
1469 if (getuser(&usbuf, who) == 0) {
1470 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1472 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1478 * Administrative Get User Parameters
1480 void cmd_agup(char *cmdbuf)
1482 struct ctdluser usbuf;
1483 char requested_user[128];
1485 if (CtdlAccessCheck(ac_aide)) {
1489 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1490 if (getuser(&usbuf, requested_user) != 0) {
1491 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1494 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1501 (int) usbuf.axlevel,
1503 (long)usbuf.lastcall,
1510 * Administrative Set User Parameters
1512 void cmd_asup(char *cmdbuf)
1514 struct ctdluser usbuf;
1515 char requested_user[128];
1521 if (CtdlAccessCheck(ac_aide))
1524 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1525 if (lgetuser(&usbuf, requested_user) != 0) {
1526 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1529 np = num_parms(cmdbuf);
1531 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1533 usbuf.flags = extract_int(cmdbuf, 2);
1535 usbuf.timescalled = extract_int(cmdbuf, 3);
1537 usbuf.posted = extract_int(cmdbuf, 4);
1539 newax = extract_int(cmdbuf, 5);
1540 if ((newax >= 0) && (newax <= 6)) {
1541 usbuf.axlevel = extract_int(cmdbuf, 5);
1545 usbuf.lastcall = extract_long(cmdbuf, 7);
1548 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1551 if (usbuf.axlevel == 0) {
1552 if (purge_user(requested_user) == 0) {
1558 sprintf(notify, "User \"%s\" has been deleted by %s.\n",
1559 usbuf.fullname, CC->user.fullname);
1560 aide_message(notify, "User Deletion Message");
1563 cprintf("%d Ok", CIT_OK);
1565 cprintf(" (%s deleted)", requested_user);
1572 * Check to see if the user who we just sent mail to is logged in. If yes,
1573 * bump the 'new mail' counter for their session. That enables them to
1574 * receive a new mail notification without having to hit the database.
1576 void BumpNewMailCounter(long which_user) {
1577 struct CitContext *ptr;
1579 begin_critical_section(S_SESSION_TABLE);
1581 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1582 if (ptr->user.usernum == which_user) {
1587 end_critical_section(S_SESSION_TABLE);
1592 * Count the number of new mail messages the user has
1596 int num_newmsgs = 0;
1598 num_newmsgs = CC->newmail;
1601 return (num_newmsgs);
1606 * Count the number of new mail messages the user has
1608 int InitialMailCheck()
1610 int num_newmsgs = 0;
1612 char mailboxname[ROOMNAMELEN];
1613 struct ctdlroom mailbox;
1615 struct cdbdata *cdbfr;
1616 long *msglist = NULL;
1619 MailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1620 if (getroom(&mailbox, mailboxname) != 0)
1622 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1624 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1626 if (cdbfr != NULL) {
1627 msglist = malloc(cdbfr->len);
1628 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1629 num_msgs = cdbfr->len / sizeof(long);
1633 for (a = 0; a < num_msgs; ++a) {
1634 if (msglist[a] > 0L) {
1635 if (msglist[a] > vbuf.v_lastseen) {
1640 if (msglist != NULL)
1643 return (num_newmsgs);
1649 * Set the preferred view for the current user/room combination
1651 void cmd_view(char *cmdbuf) {
1655 if (CtdlAccessCheck(ac_logged_in)) {
1659 requested_view = extract_int(cmdbuf, 0);
1661 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1662 vbuf.v_view = requested_view;
1663 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1665 cprintf("%d ok\n", CIT_OK);