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);
312 * getuserbyuid() - get user by system uid (for PAM mode authentication)
313 * returns 0 if user was found
315 * WARNING: don't use this function unless you absolutely have to. It does
316 * a sequential search and therefore is computationally expensive.
318 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
320 struct cdbdata *cdbus;
322 cdb_rewind(CDB_USERS);
324 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
325 memset(usbuf, 0, sizeof(struct ctdluser));
326 memcpy(usbuf, cdbus->ptr,
327 ((cdbus->len > sizeof(struct ctdluser)) ?
328 sizeof(struct ctdluser) : cdbus->len));
330 if (usbuf->uid == number) {
331 cdb_close_cursor(CDB_USERS);
341 * Back end for cmd_user() and its ilk
343 int CtdlLoginExistingUser(char *trythisname)
348 if ((CC->logged_in)) {
349 return login_already_logged_in;
352 if (trythisname == NULL) return login_not_found;
353 safestrncpy(username, trythisname, USERNAME_SIZE);
356 if (strlen(username) == 0) {
357 return login_not_found;
360 if (config.c_auth_mode == 1) {
365 struct passwd *tempPwdPtr;
368 lprintf(CTDL_DEBUG, "asking host about <%s>\n", username);
369 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
370 if (tempPwdPtr == NULL) {
371 return login_not_found;
373 lprintf(CTDL_DEBUG, "found it! uid=%d, gecos=%s\n", pd.pw_uid, pd.pw_gecos);
375 /* Locate the associated Citadel account.
376 * If not found, make one attempt to create it.
378 found_user = getuserbyuid(&CC->user, pd.pw_uid);
379 if (found_user != 0) {
380 create_user(username, 0);
381 found_user = getuserbyuid(&CC->user, pd.pw_uid);
387 /* native auth mode */
389 struct recptypes *valid = NULL;
391 /* First, try to log in as if the supplied name is a display name */
392 found_user = getuser(&CC->user, username);
394 /* If that didn't work, try to log in as if the supplied name
395 * is an e-mail address
397 if (found_user != 0) {
398 valid = validate_recipients(username);
400 if (valid->num_local == 1) {
401 found_user = getuser(&CC->user, valid->recp_local);
408 /* Did we find something? */
409 if (found_user == 0) {
410 if (((CC->nologin)) && (CC->user.axlevel < 6)) {
411 return login_too_many_users;
413 safestrncpy(CC->curr_user, CC->user.fullname,
414 sizeof CC->curr_user);
418 return login_not_found;
426 void cmd_user(char *cmdbuf)
431 extract_token(username, cmdbuf, 0, '|', sizeof username);
434 a = CtdlLoginExistingUser(username);
436 case login_already_logged_in:
437 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
439 case login_too_many_users:
441 "Too many users are already online "
443 ERROR + MAX_SESSIONS_EXCEEDED,
444 config.c_nodename, config.c_maxsessions);
447 cprintf("%d Password required for %s\n",
448 MORE_DATA, CC->curr_user);
450 case login_not_found:
451 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
454 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
461 * session startup code which is common to both cmd_pass() and cmd_newu()
463 void session_startup(void)
467 lprintf(CTDL_NOTICE, "<%s> logged in\n", CC->curr_user);
469 lgetuser(&CC->user, CC->curr_user);
470 ++(CC->user.timescalled);
471 CC->previous_login = CC->user.lastcall;
472 time(&CC->user.lastcall);
474 /* If this user's name is the name of the system administrator
475 * (as specified in setup), automatically assign access level 6.
477 if (!strcasecmp(CC->user.fullname, config.c_sysadm)) {
478 CC->user.axlevel = 6;
481 /* If we're authenticating off the host system, automatically give
482 * root the highest level of access.
484 if (config.c_auth_mode == 1) {
485 if (CC->user.uid == 0) {
486 CC->user.axlevel = 6;
493 * Populate CC->cs_inet_email with a default address. This will be
494 * overwritten with the user's directory address, if one exists, when
495 * the vCard module's login hook runs.
497 snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
498 CC->user.fullname, config.c_fqdn);
499 for (i=0; i<strlen(CC->cs_inet_email); ++i) {
500 if (isspace(CC->cs_inet_email[i])) {
501 CC->cs_inet_email[i] = '_';
505 /* Create any personal rooms required by the system.
506 * (Technically, MAILROOM should be there already, but just in case...)
508 create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
509 create_room(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
510 create_room(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
512 /* Run any startup routines registered by loadable modules */
513 PerformSessionHooks(EVT_LOGIN);
515 /* Enter the lobby */
516 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
520 void logged_in_response(void)
522 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
523 CIT_OK, CC->user.fullname, CC->user.axlevel,
524 CC->user.timescalled, CC->user.posted,
525 CC->user.flags, CC->user.usernum,
532 * misc things to be taken care of when a user is logged out
534 void logout(struct CitContext *who)
537 * Clear out some session data. Most likely, the CitContext for this
538 * session is about to get nuked when the session disconnects, but
539 * since it's possible to log in again without reconnecting, we cannot
540 * make that assumption.
542 strcpy(who->fake_username, "");
543 strcpy(who->fake_postname, "");
544 strcpy(who->fake_hostname, "");
545 strcpy(who->fake_roomname, "");
549 * If there is a download in progress, abort it.
551 if (who->download_fp != NULL) {
552 fclose(who->download_fp);
553 who->download_fp = NULL;
557 * If there is an upload in progress, abort it.
559 if (who->upload_fp != NULL) {
564 * If we were talking to a network node, we're not anymore...
566 if (strlen(who->net_node) > 0) {
567 network_talking_to(who->net_node, NTT_REMOVE);
570 /* Do modular stuff... */
571 PerformSessionHooks(EVT_LOGOUT);
573 /* Free any output buffers */
574 if (who->output_buffer != NULL) {
581 * an alternate version of validpw() which executes `chkpwd' instead of
582 * verifying the password directly
584 static int validpw(uid_t uid, const char *pass)
587 int status, pipev[2];
591 lprintf(CTDL_ERR, "pipe failed (%s): denying host auth access for "
592 "uid %ld\n", strerror(errno), (long)uid);
595 switch (pid = fork()) {
597 lprintf(CTDL_ERR, "fork failed (%s): denying host auth access for "
598 "uid %ld\n", strerror(errno), (long)uid);
605 if (dup2(pipev[0], 0) == -1) {
611 execl(CTDLDIR "/chkpwd", CTDLDIR "/chkpwd", NULL);
612 perror(CTDLDIR "/chkpwd");
618 snprintf(buf, sizeof buf, "%lu\n", (unsigned long) uid));
619 write(pipev[1], pass, strlen(pass));
620 write(pipev[1], "\n", 1);
623 while (waitpid(pid, &status, 0) == -1)
624 if (errno != EINTR) {
625 lprintf(CTDL_ERR, "waitpid failed (%s): denying host auth "
626 "access for uid %ld\n",
627 strerror(errno), (long)uid);
630 if (WIFEXITED(status) && !WEXITSTATUS(status))
644 int CtdlTryPassword(char *password)
648 if ((CC->logged_in)) {
649 lprintf(CTDL_WARNING, "CtdlTryPassword: already logged in\n");
650 return pass_already_logged_in;
652 if (!strcmp(CC->curr_user, NLI)) {
653 lprintf(CTDL_WARNING, "CtdlTryPassword: no user selected\n");
656 if (getuser(&CC->user, CC->curr_user)) {
657 lprintf(CTDL_ERR, "CtdlTryPassword: internal error\n");
658 return pass_internal_error;
660 if (password == NULL) {
661 lprintf(CTDL_INFO, "CtdlTryPassword: NULL password string supplied\n");
662 return pass_wrong_password;
666 if (config.c_auth_mode == 1) {
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);
685 /* native auth mode */
688 strproc(CC->user.password);
689 code = strcasecmp(CC->user.password, password);
691 strproc(CC->user.password);
692 code = strcasecmp(CC->user.password, password);
699 lprintf(CTDL_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
700 return pass_wrong_password;
705 void cmd_pass(char *buf)
710 extract_token(password, buf, 0, '|', sizeof password);
711 a = CtdlTryPassword(password);
714 case pass_already_logged_in:
715 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
718 cprintf("%d You must send a name with USER first.\n",
719 ERROR + USERNAME_REQUIRED);
721 case pass_wrong_password:
722 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
725 logged_in_response();
733 * Delete a user record *and* all of its related resources.
735 int purge_user(char pname[])
738 struct ctdluser usbuf;
739 char usernamekey[USERNAME_SIZE];
740 struct CitContext *ccptr;
741 int user_is_logged_in = 0;
743 makeuserkey(usernamekey, pname);
745 if (getuser(&usbuf, pname) != 0) {
746 lprintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
747 return (ERROR + NO_SUCH_USER);
749 /* Don't delete a user who is currently logged in. Instead, just
750 * set the access level to 0, and let the account get swept up
751 * during the next purge.
753 user_is_logged_in = 0;
754 begin_critical_section(S_SESSION_TABLE);
755 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
756 if (ccptr->user.usernum == usbuf.usernum) {
757 user_is_logged_in = 1;
760 end_critical_section(S_SESSION_TABLE);
761 if (user_is_logged_in == 1) {
762 lprintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
767 lprintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
769 /* Perform any purge functions registered by server extensions */
770 PerformUserHooks(&usbuf, EVT_PURGEUSER);
772 /* delete any existing user/room relationships */
773 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
775 /* delete the userlog entry */
776 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
778 /* remove the user's bio file */
786 /* remove the user's picture */
799 * create_user() - back end processing to create a new user
801 * Set 'newusername' to the desired account name.
802 * Set 'become_user' to nonzero if this is self-service account creation and we want
803 * to actually log in as the user we just created, otherwise set it to 0.
805 int create_user(char *newusername, int become_user)
807 struct ctdluser usbuf;
808 struct ctdlroom qrbuf;
810 char mailboxname[ROOMNAMELEN];
814 safestrncpy(username, newusername, sizeof username);
817 if (config.c_auth_mode == 1) {
822 struct passwd *tempPwdPtr;
825 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
826 if (tempPwdPtr != NULL) {
827 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
831 return (ERROR + NO_SUCH_USER);
835 if (!getuser(&usbuf, username)) {
836 return (ERROR + ALREADY_EXISTS);
839 /* Go ahead and initialize a new user record */
840 memset(&usbuf, 0, sizeof(struct ctdluser));
841 safestrncpy(usbuf.fullname, username, sizeof usbuf.fullname);
842 strcpy(usbuf.password, "");
845 /* These are the default flags on new accounts */
846 usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
848 usbuf.timescalled = 0;
850 usbuf.axlevel = config.c_initax;
851 usbuf.USscreenwidth = 80;
852 usbuf.USscreenheight = 24;
853 usbuf.lastcall = time(NULL);
855 /* fetch a new user number */
856 usbuf.usernum = get_new_user_number();
858 /* The very first user created on the system will always be an Aide */
859 if (usbuf.usernum == 1L) {
863 /* add user to userlog */
867 * Give the user a private mailbox and a configuration room.
868 * Make the latter an invisible system room.
870 MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
871 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
873 MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
874 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
875 if (lgetroom(&qrbuf, mailboxname) == 0) {
876 qrbuf.QRflags2 |= QR2_SYSTEM;
880 /* Perform any create functions registered by server extensions */
881 PerformUserHooks(&usbuf, EVT_NEWUSER);
883 /* Everything below this line can be bypassed if administratively
884 * creating a user, instead of doing self-service account creation
888 /* Now become the user we just created */
889 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
890 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
893 /* Check to make sure we're still who we think we are */
894 if (getuser(&CC->user, CC->curr_user)) {
895 return (ERROR + INTERNAL_ERROR);
900 "New user account <%s> has been created, from host %s [%s].\n",
905 aide_message(buf, "User Creation Notice");
906 lprintf(CTDL_NOTICE, "New user <%s> created\n", username);
914 * cmd_newu() - create a new user account and log in as that user
916 void cmd_newu(char *cmdbuf)
921 if (config.c_auth_mode == 1) {
922 cprintf("%d This system does not use native mode authentication.\n",
927 if (config.c_disable_newu) {
928 cprintf("%d Self-service user account creation "
929 "is disabled on this system.\n", ERROR + NOT_HERE);
934 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
938 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
939 ERROR + MAX_SESSIONS_EXCEEDED,
940 config.c_nodename, config.c_maxsessions);
942 extract_token(username, cmdbuf, 0, '|', sizeof username);
946 if (strlen(username) == 0) {
947 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
951 if ((!strcasecmp(username, "bbs")) ||
952 (!strcasecmp(username, "new")) ||
953 (!strcasecmp(username, "."))) {
954 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
958 a = create_user(username, 1);
962 logged_in_response();
963 } else if (a == ERROR + ALREADY_EXISTS) {
964 cprintf("%d '%s' already exists.\n",
965 ERROR + ALREADY_EXISTS, username);
967 } else if (a == ERROR + INTERNAL_ERROR) {
968 cprintf("%d Internal error - user record disappeared?\n",
969 ERROR + INTERNAL_ERROR);
972 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
981 void cmd_setp(char *new_pw)
983 if (CtdlAccessCheck(ac_logged_in)) {
986 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
987 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
991 if (strlen(new_pw) == 0) {
992 cprintf("%d Password unchanged.\n", CIT_OK);
995 lgetuser(&CC->user, CC->curr_user);
996 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
998 cprintf("%d Password changed.\n", CIT_OK);
999 lprintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
1000 PerformSessionHooks(EVT_SETPASS);
1005 * cmd_creu() - administratively create a new user account (do not log in to it)
1007 void cmd_creu(char *cmdbuf)
1012 struct ctdluser tmp;
1014 if (CtdlAccessCheck(ac_aide)) {
1018 extract_token(username, cmdbuf, 0, '|', sizeof username);
1019 extract_token(password, cmdbuf, 1, '|', sizeof password);
1025 if (strlen(username) == 0) {
1026 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1030 a = create_user(username, 0);
1033 if (strlen(password) > 0) {
1034 lgetuser(&tmp, username);
1035 safestrncpy(tmp.password, password, sizeof(tmp.password));
1038 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1039 (strlen(password) > 0) ? "and password set" :
1040 "with no password");
1042 } else if (a == ERROR + ALREADY_EXISTS) {
1043 cprintf("%d '%s' already exists.\n",
1044 ERROR + ALREADY_EXISTS, username);
1047 cprintf("%d An error occured creating the user account.\n", ERROR + INTERNAL_ERROR);
1054 * get user parameters
1059 if (CtdlAccessCheck(ac_logged_in))
1062 getuser(&CC->user, CC->curr_user);
1063 cprintf("%d %d|%d|%d|\n",
1065 CC->user.USscreenwidth,
1066 CC->user.USscreenheight,
1067 (CC->user.flags & US_USER_SET)
1072 * set user parameters
1074 void cmd_setu(char *new_parms)
1076 if (CtdlAccessCheck(ac_logged_in))
1079 if (num_parms(new_parms) < 3) {
1080 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1083 lgetuser(&CC->user, CC->curr_user);
1084 CC->user.USscreenwidth = extract_int(new_parms, 0);
1085 CC->user.USscreenheight = extract_int(new_parms, 1);
1086 CC->user.flags = CC->user.flags & (~US_USER_SET);
1087 CC->user.flags = CC->user.flags |
1088 (extract_int(new_parms, 2) & US_USER_SET);
1090 lputuser(&CC->user);
1091 cprintf("%d Ok\n", CIT_OK);
1095 * set last read pointer
1097 void cmd_slrp(char *new_ptr)
1101 struct visit original_vbuf;
1103 if (CtdlAccessCheck(ac_logged_in)) {
1107 if (!strncasecmp(new_ptr, "highest", 7)) {
1108 newlr = CC->room.QRhighest;
1110 newlr = atol(new_ptr);
1113 lgetuser(&CC->user, CC->curr_user);
1115 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1116 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1117 vbuf.v_lastseen = newlr;
1118 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1120 /* Only rewrite the record if it changed */
1121 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1122 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1123 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1126 lputuser(&CC->user);
1127 cprintf("%d %ld\n", CIT_OK, newlr);
1131 void cmd_seen(char *argbuf) {
1132 long target_msgnum = 0L;
1133 int target_setting = 0;
1135 if (CtdlAccessCheck(ac_logged_in)) {
1139 if (num_parms(argbuf) != 2) {
1140 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1144 target_msgnum = extract_long(argbuf, 0);
1145 target_setting = extract_int(argbuf, 1);
1147 CtdlSetSeen(&target_msgnum, 1, target_setting,
1148 ctdlsetseen_seen, NULL, NULL);
1149 cprintf("%d OK\n", CIT_OK);
1153 void cmd_gtsn(char *argbuf) {
1156 if (CtdlAccessCheck(ac_logged_in)) {
1160 CtdlGetSeen(buf, ctdlsetseen_seen);
1161 cprintf("%d %s\n", CIT_OK, buf);
1166 * API function for cmd_invt_kick() and anything else that needs to
1167 * invite or kick out a user to/from a room.
1169 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1171 int CtdlInvtKick(char *iuser, int op) {
1172 struct ctdluser USscratch;
1176 if (getuser(&USscratch, iuser) != 0) {
1180 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1182 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1183 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1186 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1187 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1189 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1191 /* post a message in Aide> saying what we just did */
1192 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1194 ((op == 1) ? "invited to" : "kicked out of"),
1197 aide_message(bbb,"User Admin Message");
1204 * INVT and KICK commands
1206 void cmd_invt_kick(char *iuser, int op) {
1209 * These commands are only allowed by aides, room aides,
1210 * and room namespace owners
1213 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1214 /* access granted */
1217 cprintf("%d Higher access or room ownership required.\n",
1218 ERROR + HIGHER_ACCESS_REQUIRED);
1222 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1224 cprintf("%d Can't add/remove users from this room.\n",
1229 if (CtdlInvtKick(iuser, op) != 0) {
1230 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1234 cprintf("%d %s %s %s.\n",
1236 ((op == 1) ? "invited to" : "kicked out of"),
1243 * Forget (Zap) the current room (API call)
1244 * Returns 0 on success
1246 int CtdlForgetThisRoom(void) {
1249 /* On some systems, Aides are not allowed to forget rooms */
1250 if (is_aide() && (config.c_aide_zap == 0)
1251 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1255 lgetuser(&CC->user, CC->curr_user);
1256 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1258 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1259 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1261 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1262 lputuser(&CC->user);
1264 /* Return to the Lobby, so we don't end up in an undefined room */
1265 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1272 * forget (Zap) the current room
1277 if (CtdlAccessCheck(ac_logged_in)) {
1281 if (CtdlForgetThisRoom() == 0) {
1282 cprintf("%d Ok\n", CIT_OK);
1285 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1290 * Get Next Unregistered User
1294 struct cdbdata *cdbus;
1295 struct ctdluser usbuf;
1297 if (CtdlAccessCheck(ac_aide)) {
1301 if ((CitControl.MMflags & MM_VALID) == 0) {
1302 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1306 /* There are unvalidated users. Traverse the user database,
1307 * and return the first user we find that needs validation.
1309 cdb_rewind(CDB_USERS);
1310 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1311 memset(&usbuf, 0, sizeof(struct ctdluser));
1312 memcpy(&usbuf, cdbus->ptr,
1313 ((cdbus->len > sizeof(struct ctdluser)) ?
1314 sizeof(struct ctdluser) : cdbus->len));
1316 if ((usbuf.flags & US_NEEDVALID)
1317 && (usbuf.axlevel > 0)) {
1318 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1319 cdb_close_cursor(CDB_USERS);
1324 /* If we get to this point, there are no more unvalidated users.
1325 * Therefore we clear the "users need validation" flag.
1328 begin_critical_section(S_CONTROL);
1330 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1332 end_critical_section(S_CONTROL);
1333 cprintf("%d *** End of registration.\n", CIT_OK);
1342 void cmd_vali(char *v_args)
1346 struct ctdluser userbuf;
1348 extract_token(user, v_args, 0, '|', sizeof user);
1349 newax = extract_int(v_args, 1);
1351 if (CtdlAccessCheck(ac_aide)) {
1355 if (lgetuser(&userbuf, user) != 0) {
1356 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1360 userbuf.axlevel = newax;
1361 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1365 /* If the access level was set to zero, delete the user */
1367 if (purge_user(user) == 0) {
1368 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1372 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1378 * Traverse the user file...
1380 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1383 struct ctdluser usbuf;
1384 struct cdbdata *cdbus;
1386 cdb_rewind(CDB_USERS);
1388 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1389 memset(&usbuf, 0, sizeof(struct ctdluser));
1390 memcpy(&usbuf, cdbus->ptr,
1391 ((cdbus->len > sizeof(struct ctdluser)) ?
1392 sizeof(struct ctdluser) : cdbus->len));
1394 (*CallBack) (&usbuf, in_data);
1400 * List one user (this works with cmd_list)
1402 void ListThisUser(struct ctdluser *usbuf, void *data)
1406 searchstring = (char *)data;
1407 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1411 if (usbuf->axlevel > 0) {
1412 if ((CC->user.axlevel >= 6)
1413 || ((usbuf->flags & US_UNLISTED) == 0)
1414 || ((CC->internal_pgm))) {
1415 cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1419 (long)usbuf->lastcall,
1422 if (CC->user.axlevel >= 6)
1423 cprintf("%s", usbuf->password);
1430 * List users (searchstring may be empty to list all users)
1432 void cmd_list(char *cmdbuf)
1434 char searchstring[256];
1435 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1436 striplt(searchstring);
1437 cprintf("%d \n", LISTING_FOLLOWS);
1438 ForEachUser(ListThisUser, (void *)searchstring );
1446 * assorted info we need to check at login
1454 if (CtdlAccessCheck(ac_logged_in)) {
1458 getuser(&CC->user, CC->curr_user); /* no lock is needed here */
1459 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1462 if (CC->user.axlevel >= 6) {
1464 if (CitControl.MMflags & MM_VALID)
1468 /* check for mail */
1469 mail = InitialMailCheck();
1471 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1476 * check to see if a user exists
1478 void cmd_qusr(char *who)
1480 struct ctdluser usbuf;
1482 if (getuser(&usbuf, who) == 0) {
1483 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1485 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1491 * Administrative Get User Parameters
1493 void cmd_agup(char *cmdbuf)
1495 struct ctdluser usbuf;
1496 char requested_user[128];
1498 if (CtdlAccessCheck(ac_aide)) {
1502 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1503 if (getuser(&usbuf, requested_user) != 0) {
1504 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1507 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1514 (int) usbuf.axlevel,
1516 (long)usbuf.lastcall,
1523 * Administrative Set User Parameters
1525 void cmd_asup(char *cmdbuf)
1527 struct ctdluser usbuf;
1528 char requested_user[128];
1534 if (CtdlAccessCheck(ac_aide))
1537 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1538 if (lgetuser(&usbuf, requested_user) != 0) {
1539 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1542 np = num_parms(cmdbuf);
1544 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1546 usbuf.flags = extract_int(cmdbuf, 2);
1548 usbuf.timescalled = extract_int(cmdbuf, 3);
1550 usbuf.posted = extract_int(cmdbuf, 4);
1552 newax = extract_int(cmdbuf, 5);
1553 if ((newax >= 0) && (newax <= 6)) {
1554 usbuf.axlevel = extract_int(cmdbuf, 5);
1558 usbuf.lastcall = extract_long(cmdbuf, 7);
1561 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1564 if (usbuf.axlevel == 0) {
1565 if (purge_user(requested_user) == 0) {
1571 sprintf(notify, "User \"%s\" has been deleted by %s.\n",
1572 usbuf.fullname, CC->user.fullname);
1573 aide_message(notify, "User Deletion Message");
1576 cprintf("%d Ok", CIT_OK);
1578 cprintf(" (%s deleted)", requested_user);
1585 * Check to see if the user who we just sent mail to is logged in. If yes,
1586 * bump the 'new mail' counter for their session. That enables them to
1587 * receive a new mail notification without having to hit the database.
1589 void BumpNewMailCounter(long which_user) {
1590 struct CitContext *ptr;
1592 begin_critical_section(S_SESSION_TABLE);
1594 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1595 if (ptr->user.usernum == which_user) {
1600 end_critical_section(S_SESSION_TABLE);
1605 * Count the number of new mail messages the user has
1609 int num_newmsgs = 0;
1611 num_newmsgs = CC->newmail;
1614 return (num_newmsgs);
1619 * Count the number of new mail messages the user has
1621 int InitialMailCheck()
1623 int num_newmsgs = 0;
1625 char mailboxname[ROOMNAMELEN];
1626 struct ctdlroom mailbox;
1628 struct cdbdata *cdbfr;
1629 long *msglist = NULL;
1632 MailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1633 if (getroom(&mailbox, mailboxname) != 0)
1635 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1637 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1639 if (cdbfr != NULL) {
1640 msglist = malloc(cdbfr->len);
1641 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1642 num_msgs = cdbfr->len / sizeof(long);
1646 for (a = 0; a < num_msgs; ++a) {
1647 if (msglist[a] > 0L) {
1648 if (msglist[a] > vbuf.v_lastseen) {
1653 if (msglist != NULL)
1656 return (num_newmsgs);
1662 * Set the preferred view for the current user/room combination
1664 void cmd_view(char *cmdbuf) {
1668 if (CtdlAccessCheck(ac_logged_in)) {
1672 requested_view = extract_int(cmdbuf, 0);
1674 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1675 vbuf.v_view = requested_view;
1676 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1678 cprintf("%d ok\n", CIT_OK);