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"
50 #include "citadel_dirs.h"
54 * makeuserkey() - convert a username into the format used as a database key
55 * (it's just the username converted into lower case)
57 static INLINE void makeuserkey(char *key, char *username) {
60 len = strlen(username);
61 for (i=0; i<=len; ++i) {
62 key[i] = tolower(username[i]);
68 * getuser() - retrieve named user into supplied buffer.
69 * returns 0 on success
71 int getuser(struct ctdluser *usbuf, char name[])
74 char usernamekey[USERNAME_SIZE];
75 struct cdbdata *cdbus;
78 memset(usbuf, 0, sizeof(struct ctdluser));
81 makeuserkey(usernamekey, name);
82 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
84 if (cdbus == NULL) { /* user not found */
88 memcpy(usbuf, cdbus->ptr,
89 ((cdbus->len > sizeof(struct ctdluser)) ?
90 sizeof(struct ctdluser) : cdbus->len));
99 * lgetuser() - same as getuser() but locks the record
101 int lgetuser(struct ctdluser *usbuf, char *name)
105 retcode = getuser(usbuf, name);
107 begin_critical_section(S_USERS);
114 * putuser() - write user buffer into the correct place on disk
116 void putuser(struct ctdluser *usbuf)
118 char usernamekey[USERNAME_SIZE];
120 makeuserkey(usernamekey, usbuf->fullname);
122 usbuf->version = REV_LEVEL;
124 usernamekey, strlen(usernamekey),
125 usbuf, sizeof(struct ctdluser));
131 * lputuser() - same as putuser() but locks the record
133 void lputuser(struct ctdluser *usbuf)
136 end_critical_section(S_USERS);
140 * Index-generating function used by Ctdl[Get|Set]Relationship
142 int GenerateRelationshipIndex(char *IndexBuf,
154 TheIndex.iRoomID = RoomID;
155 TheIndex.iRoomGen = RoomGen;
156 TheIndex.iUserID = UserID;
158 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
159 return (sizeof(TheIndex));
165 * Back end for CtdlSetRelationship()
167 void put_visit(struct visit *newvisit)
172 /* Generate an index */
173 IndexLen = GenerateRelationshipIndex(IndexBuf,
176 newvisit->v_usernum);
178 /* Store the record */
179 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
180 newvisit, sizeof(struct visit)
188 * Define a relationship between a user and a room
190 void CtdlSetRelationship(struct visit *newvisit,
191 struct ctdluser *rel_user,
192 struct ctdlroom *rel_room)
196 /* We don't use these in Citadel because they're implicit by the
197 * index, but they must be present if the database is exported.
199 newvisit->v_roomnum = rel_room->QRnumber;
200 newvisit->v_roomgen = rel_room->QRgen;
201 newvisit->v_usernum = rel_user->usernum;
207 * Locate a relationship between a user and a room
209 void CtdlGetRelationship(struct visit *vbuf,
210 struct ctdluser *rel_user,
211 struct ctdlroom *rel_room)
216 struct cdbdata *cdbvisit;
218 /* Generate an index */
219 IndexLen = GenerateRelationshipIndex(IndexBuf,
224 /* Clear out the buffer */
225 memset(vbuf, 0, sizeof(struct visit));
227 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
228 if (cdbvisit != NULL) {
229 memcpy(vbuf, cdbvisit->ptr,
230 ((cdbvisit->len > sizeof(struct visit)) ?
231 sizeof(struct visit) : cdbvisit->len));
235 /* If this is the first time the user has seen this room,
236 * set the view to be the default for the room.
238 vbuf->v_view = rel_room->QRdefaultview;
241 /* Set v_seen if necessary */
242 if (vbuf->v_seen[0] == 0) {
243 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
248 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
250 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
255 * Is the user currently logged in an Aide?
259 if (CC->user.axlevel >= 6)
267 * Is the user currently logged in an Aide *or* the room aide for this room?
269 int is_room_aide(void)
272 if (!CC->logged_in) {
276 if ((CC->user.axlevel >= 6)
277 || (CC->room.QRroomaide == CC->user.usernum)) {
285 * getuserbynumber() - get user by number
286 * returns 0 if user was found
288 * WARNING: don't use this function unless you absolutely have to. It does
289 * a sequential search and therefore is computationally expensive.
291 int getuserbynumber(struct ctdluser *usbuf, long int number)
293 struct cdbdata *cdbus;
295 cdb_rewind(CDB_USERS);
297 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
298 memset(usbuf, 0, sizeof(struct ctdluser));
299 memcpy(usbuf, cdbus->ptr,
300 ((cdbus->len > sizeof(struct ctdluser)) ?
301 sizeof(struct ctdluser) : cdbus->len));
303 if (usbuf->usernum == number) {
304 cdb_close_cursor(CDB_USERS);
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);
342 * Back end for cmd_user() and its ilk
344 int CtdlLoginExistingUser(char *trythisname)
349 if ((CC->logged_in)) {
350 return login_already_logged_in;
353 if (trythisname == NULL) return login_not_found;
354 safestrncpy(username, trythisname, USERNAME_SIZE);
357 if (strlen(username) == 0) {
358 return login_not_found;
361 if (config.c_auth_mode == 1) {
366 struct passwd *tempPwdPtr;
369 lprintf(CTDL_DEBUG, "asking host about <%s>\n", username);
370 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
371 if (tempPwdPtr == NULL) {
372 return login_not_found;
374 lprintf(CTDL_DEBUG, "found it! uid=%d, gecos=%s\n", pd.pw_uid, pd.pw_gecos);
376 /* Locate the associated Citadel account.
377 * If not found, make one attempt to create it.
379 found_user = getuserbyuid(&CC->user, pd.pw_uid);
380 if (found_user != 0) {
381 create_user(username, 0);
382 found_user = getuserbyuid(&CC->user, pd.pw_uid);
388 /* native auth mode */
390 struct recptypes *valid = NULL;
392 /* First, try to log in as if the supplied name is a display name */
393 found_user = getuser(&CC->user, username);
395 /* If that didn't work, try to log in as if the supplied name
396 * is an e-mail address
398 if (found_user != 0) {
399 valid = validate_recipients(username);
401 if (valid->num_local == 1) {
402 found_user = getuser(&CC->user, valid->recp_local);
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 /* If we're authenticating off the host system, automatically give
483 * root the highest level of access.
485 if (config.c_auth_mode == 1) {
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_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(file_chkpwd, file_chkpwd, NULL);
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)) {
674 * sooper-seekrit hack: populate the password field in the
675 * citadel database with the password that the user typed,
676 * if it's correct. This allows most sites to convert from
677 * host auth to native auth if they want to. If you think
678 * this is a security hazard, comment it out.
681 lgetuser(&CC->user, CC->curr_user);
682 safestrncpy(CC->user.password, password, sizeof CC->user.password);
686 * (sooper-seekrit hack ends here)
697 /* native auth mode */
700 strproc(CC->user.password);
701 code = strcasecmp(CC->user.password, password);
703 strproc(CC->user.password);
704 code = strcasecmp(CC->user.password, password);
711 lprintf(CTDL_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
712 return pass_wrong_password;
717 void cmd_pass(char *buf)
722 extract_token(password, buf, 0, '|', sizeof password);
723 a = CtdlTryPassword(password);
726 case pass_already_logged_in:
727 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
730 cprintf("%d You must send a name with USER first.\n",
731 ERROR + USERNAME_REQUIRED);
733 case pass_wrong_password:
734 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
737 logged_in_response();
745 * Delete a user record *and* all of its related resources.
747 int purge_user(char pname[])
750 struct ctdluser usbuf;
751 char usernamekey[USERNAME_SIZE];
752 struct CitContext *ccptr;
753 int user_is_logged_in = 0;
755 makeuserkey(usernamekey, pname);
757 if (getuser(&usbuf, pname) != 0) {
758 lprintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
759 return (ERROR + NO_SUCH_USER);
761 /* Don't delete a user who is currently logged in. Instead, just
762 * set the access level to 0, and let the account get swept up
763 * during the next purge.
765 user_is_logged_in = 0;
766 begin_critical_section(S_SESSION_TABLE);
767 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
768 if (ccptr->user.usernum == usbuf.usernum) {
769 user_is_logged_in = 1;
772 end_critical_section(S_SESSION_TABLE);
773 if (user_is_logged_in == 1) {
774 lprintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
779 lprintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
781 /* Perform any purge functions registered by server extensions */
782 PerformUserHooks(&usbuf, EVT_PURGEUSER);
784 /* delete any existing user/room relationships */
785 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
787 /* delete the userlog entry */
788 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
790 /* remove the user's bio file */
798 /* remove the user's picture */
811 * create_user() - back end processing to create a new user
813 * Set 'newusername' to the desired account name.
814 * Set 'become_user' to nonzero if this is self-service account creation and we want
815 * to actually log in as the user we just created, otherwise set it to 0.
817 int create_user(char *newusername, int become_user)
819 struct ctdluser usbuf;
820 struct ctdlroom qrbuf;
822 char mailboxname[ROOMNAMELEN];
826 safestrncpy(username, newusername, sizeof username);
829 if (config.c_auth_mode == 1) {
834 struct passwd *tempPwdPtr;
837 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
838 if (tempPwdPtr != NULL) {
839 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
843 return (ERROR + NO_SUCH_USER);
847 if (!getuser(&usbuf, username)) {
848 return (ERROR + ALREADY_EXISTS);
851 /* Go ahead and initialize a new user record */
852 memset(&usbuf, 0, sizeof(struct ctdluser));
853 safestrncpy(usbuf.fullname, username, sizeof usbuf.fullname);
854 strcpy(usbuf.password, "");
857 /* These are the default flags on new accounts */
858 usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
860 usbuf.timescalled = 0;
862 usbuf.axlevel = config.c_initax;
863 usbuf.USscreenwidth = 80;
864 usbuf.USscreenheight = 24;
865 usbuf.lastcall = time(NULL);
867 /* fetch a new user number */
868 usbuf.usernum = get_new_user_number();
870 /* The very first user created on the system will always be an Aide */
871 if (usbuf.usernum == 1L) {
875 /* add user to userlog */
879 * Give the user a private mailbox and a configuration room.
880 * Make the latter an invisible system room.
882 MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
883 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
885 MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
886 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
887 if (lgetroom(&qrbuf, mailboxname) == 0) {
888 qrbuf.QRflags2 |= QR2_SYSTEM;
892 /* Perform any create functions registered by server extensions */
893 PerformUserHooks(&usbuf, EVT_NEWUSER);
895 /* Everything below this line can be bypassed if administratively
896 * creating a user, instead of doing self-service account creation
900 /* Now become the user we just created */
901 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
902 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
905 /* Check to make sure we're still who we think we are */
906 if (getuser(&CC->user, CC->curr_user)) {
907 return (ERROR + INTERNAL_ERROR);
912 "New user account <%s> has been created, from host %s [%s].\n",
917 aide_message(buf, "User Creation Notice");
918 lprintf(CTDL_NOTICE, "New user <%s> created\n", username);
926 * cmd_newu() - create a new user account and log in as that user
928 void cmd_newu(char *cmdbuf)
933 if (config.c_auth_mode == 1) {
934 cprintf("%d This system does not use native mode authentication.\n",
939 if (config.c_disable_newu) {
940 cprintf("%d Self-service user account creation "
941 "is disabled on this system.\n", ERROR + NOT_HERE);
946 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
950 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
951 ERROR + MAX_SESSIONS_EXCEEDED,
952 config.c_nodename, config.c_maxsessions);
954 extract_token(username, cmdbuf, 0, '|', sizeof username);
958 if (strlen(username) == 0) {
959 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
963 if ((!strcasecmp(username, "bbs")) ||
964 (!strcasecmp(username, "new")) ||
965 (!strcasecmp(username, "."))) {
966 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
970 a = create_user(username, 1);
974 logged_in_response();
975 } else if (a == ERROR + ALREADY_EXISTS) {
976 cprintf("%d '%s' already exists.\n",
977 ERROR + ALREADY_EXISTS, username);
979 } else if (a == ERROR + INTERNAL_ERROR) {
980 cprintf("%d Internal error - user record disappeared?\n",
981 ERROR + INTERNAL_ERROR);
984 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
993 void cmd_setp(char *new_pw)
995 if (CtdlAccessCheck(ac_logged_in)) {
998 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
999 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
1003 if (strlen(new_pw) == 0) {
1004 cprintf("%d Password unchanged.\n", CIT_OK);
1007 lgetuser(&CC->user, CC->curr_user);
1008 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1009 lputuser(&CC->user);
1010 cprintf("%d Password changed.\n", CIT_OK);
1011 lprintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
1012 PerformSessionHooks(EVT_SETPASS);
1017 * cmd_creu() - administratively create a new user account (do not log in to it)
1019 void cmd_creu(char *cmdbuf)
1024 struct ctdluser tmp;
1026 if (CtdlAccessCheck(ac_aide)) {
1030 extract_token(username, cmdbuf, 0, '|', sizeof username);
1031 extract_token(password, cmdbuf, 1, '|', sizeof password);
1037 if (strlen(username) == 0) {
1038 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1042 a = create_user(username, 0);
1045 if (strlen(password) > 0) {
1046 lgetuser(&tmp, username);
1047 safestrncpy(tmp.password, password, sizeof(tmp.password));
1050 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1051 (strlen(password) > 0) ? "and password set" :
1052 "with no password");
1054 } else if (a == ERROR + ALREADY_EXISTS) {
1055 cprintf("%d '%s' already exists.\n",
1056 ERROR + ALREADY_EXISTS, username);
1059 cprintf("%d An error occured creating the user account.\n", ERROR + INTERNAL_ERROR);
1066 * get user parameters
1071 if (CtdlAccessCheck(ac_logged_in))
1074 getuser(&CC->user, CC->curr_user);
1075 cprintf("%d %d|%d|%d|\n",
1077 CC->user.USscreenwidth,
1078 CC->user.USscreenheight,
1079 (CC->user.flags & US_USER_SET)
1084 * set user parameters
1086 void cmd_setu(char *new_parms)
1088 if (CtdlAccessCheck(ac_logged_in))
1091 if (num_parms(new_parms) < 3) {
1092 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1095 lgetuser(&CC->user, CC->curr_user);
1096 CC->user.USscreenwidth = extract_int(new_parms, 0);
1097 CC->user.USscreenheight = extract_int(new_parms, 1);
1098 CC->user.flags = CC->user.flags & (~US_USER_SET);
1099 CC->user.flags = CC->user.flags |
1100 (extract_int(new_parms, 2) & US_USER_SET);
1102 lputuser(&CC->user);
1103 cprintf("%d Ok\n", CIT_OK);
1107 * set last read pointer
1109 void cmd_slrp(char *new_ptr)
1113 struct visit original_vbuf;
1115 if (CtdlAccessCheck(ac_logged_in)) {
1119 if (!strncasecmp(new_ptr, "highest", 7)) {
1120 newlr = CC->room.QRhighest;
1122 newlr = atol(new_ptr);
1125 lgetuser(&CC->user, CC->curr_user);
1127 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1128 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1129 vbuf.v_lastseen = newlr;
1130 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1132 /* Only rewrite the record if it changed */
1133 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1134 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1135 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1138 lputuser(&CC->user);
1139 cprintf("%d %ld\n", CIT_OK, newlr);
1143 void cmd_seen(char *argbuf) {
1144 long target_msgnum = 0L;
1145 int target_setting = 0;
1147 if (CtdlAccessCheck(ac_logged_in)) {
1151 if (num_parms(argbuf) != 2) {
1152 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1156 target_msgnum = extract_long(argbuf, 0);
1157 target_setting = extract_int(argbuf, 1);
1159 CtdlSetSeen(&target_msgnum, 1, target_setting,
1160 ctdlsetseen_seen, NULL, NULL);
1161 cprintf("%d OK\n", CIT_OK);
1165 void cmd_gtsn(char *argbuf) {
1168 if (CtdlAccessCheck(ac_logged_in)) {
1172 CtdlGetSeen(buf, ctdlsetseen_seen);
1173 cprintf("%d %s\n", CIT_OK, buf);
1178 * API function for cmd_invt_kick() and anything else that needs to
1179 * invite or kick out a user to/from a room.
1181 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1183 int CtdlInvtKick(char *iuser, int op) {
1184 struct ctdluser USscratch;
1188 if (getuser(&USscratch, iuser) != 0) {
1192 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1194 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1195 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1198 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1199 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1201 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1203 /* post a message in Aide> saying what we just did */
1204 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1206 ((op == 1) ? "invited to" : "kicked out of"),
1209 aide_message(bbb,"User Admin Message");
1216 * INVT and KICK commands
1218 void cmd_invt_kick(char *iuser, int op) {
1221 * These commands are only allowed by aides, room aides,
1222 * and room namespace owners
1225 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1226 /* access granted */
1229 cprintf("%d Higher access or room ownership required.\n",
1230 ERROR + HIGHER_ACCESS_REQUIRED);
1234 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1236 cprintf("%d Can't add/remove users from this room.\n",
1241 if (CtdlInvtKick(iuser, op) != 0) {
1242 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1246 cprintf("%d %s %s %s.\n",
1248 ((op == 1) ? "invited to" : "kicked out of"),
1255 * Forget (Zap) the current room (API call)
1256 * Returns 0 on success
1258 int CtdlForgetThisRoom(void) {
1261 /* On some systems, Aides are not allowed to forget rooms */
1262 if (is_aide() && (config.c_aide_zap == 0)
1263 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1267 lgetuser(&CC->user, CC->curr_user);
1268 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1270 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1271 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1273 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1274 lputuser(&CC->user);
1276 /* Return to the Lobby, so we don't end up in an undefined room */
1277 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1284 * forget (Zap) the current room
1289 if (CtdlAccessCheck(ac_logged_in)) {
1293 if (CtdlForgetThisRoom() == 0) {
1294 cprintf("%d Ok\n", CIT_OK);
1297 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1302 * Get Next Unregistered User
1306 struct cdbdata *cdbus;
1307 struct ctdluser usbuf;
1309 if (CtdlAccessCheck(ac_aide)) {
1313 if ((CitControl.MMflags & MM_VALID) == 0) {
1314 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1318 /* There are unvalidated users. Traverse the user database,
1319 * and return the first user we find that needs validation.
1321 cdb_rewind(CDB_USERS);
1322 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1323 memset(&usbuf, 0, sizeof(struct ctdluser));
1324 memcpy(&usbuf, cdbus->ptr,
1325 ((cdbus->len > sizeof(struct ctdluser)) ?
1326 sizeof(struct ctdluser) : cdbus->len));
1328 if ((usbuf.flags & US_NEEDVALID)
1329 && (usbuf.axlevel > 0)) {
1330 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1331 cdb_close_cursor(CDB_USERS);
1336 /* If we get to this point, there are no more unvalidated users.
1337 * Therefore we clear the "users need validation" flag.
1340 begin_critical_section(S_CONTROL);
1342 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1344 end_critical_section(S_CONTROL);
1345 cprintf("%d *** End of registration.\n", CIT_OK);
1354 void cmd_vali(char *v_args)
1358 struct ctdluser userbuf;
1360 extract_token(user, v_args, 0, '|', sizeof user);
1361 newax = extract_int(v_args, 1);
1363 if (CtdlAccessCheck(ac_aide)) {
1367 if (lgetuser(&userbuf, user) != 0) {
1368 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1372 userbuf.axlevel = newax;
1373 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1377 /* If the access level was set to zero, delete the user */
1379 if (purge_user(user) == 0) {
1380 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1384 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1390 * Traverse the user file...
1392 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1395 struct ctdluser usbuf;
1396 struct cdbdata *cdbus;
1398 cdb_rewind(CDB_USERS);
1400 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1401 memset(&usbuf, 0, sizeof(struct ctdluser));
1402 memcpy(&usbuf, cdbus->ptr,
1403 ((cdbus->len > sizeof(struct ctdluser)) ?
1404 sizeof(struct ctdluser) : cdbus->len));
1406 (*CallBack) (&usbuf, in_data);
1412 * List one user (this works with cmd_list)
1414 void ListThisUser(struct ctdluser *usbuf, void *data)
1418 searchstring = (char *)data;
1419 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1423 if (usbuf->axlevel > 0) {
1424 if ((CC->user.axlevel >= 6)
1425 || ((usbuf->flags & US_UNLISTED) == 0)
1426 || ((CC->internal_pgm))) {
1427 cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1431 (long)usbuf->lastcall,
1434 if (CC->user.axlevel >= 6)
1435 cprintf("%s", usbuf->password);
1442 * List users (searchstring may be empty to list all users)
1444 void cmd_list(char *cmdbuf)
1446 char searchstring[256];
1447 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1448 striplt(searchstring);
1449 cprintf("%d \n", LISTING_FOLLOWS);
1450 ForEachUser(ListThisUser, (void *)searchstring );
1458 * assorted info we need to check at login
1466 if (CtdlAccessCheck(ac_logged_in)) {
1470 getuser(&CC->user, CC->curr_user); /* no lock is needed here */
1471 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1474 if (CC->user.axlevel >= 6) {
1476 if (CitControl.MMflags & MM_VALID)
1480 /* check for mail */
1481 mail = InitialMailCheck();
1483 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1488 * check to see if a user exists
1490 void cmd_qusr(char *who)
1492 struct ctdluser usbuf;
1494 if (getuser(&usbuf, who) == 0) {
1495 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1497 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1503 * Administrative Get User Parameters
1505 void cmd_agup(char *cmdbuf)
1507 struct ctdluser usbuf;
1508 char requested_user[128];
1510 if (CtdlAccessCheck(ac_aide)) {
1514 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1515 if (getuser(&usbuf, requested_user) != 0) {
1516 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1519 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1526 (int) usbuf.axlevel,
1528 (long)usbuf.lastcall,
1535 * Administrative Set User Parameters
1537 void cmd_asup(char *cmdbuf)
1539 struct ctdluser usbuf;
1540 char requested_user[128];
1546 if (CtdlAccessCheck(ac_aide))
1549 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1550 if (lgetuser(&usbuf, requested_user) != 0) {
1551 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1554 np = num_parms(cmdbuf);
1556 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1558 usbuf.flags = extract_int(cmdbuf, 2);
1560 usbuf.timescalled = extract_int(cmdbuf, 3);
1562 usbuf.posted = extract_int(cmdbuf, 4);
1564 newax = extract_int(cmdbuf, 5);
1565 if ((newax >= 0) && (newax <= 6)) {
1566 usbuf.axlevel = extract_int(cmdbuf, 5);
1570 usbuf.lastcall = extract_long(cmdbuf, 7);
1573 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1576 if (usbuf.axlevel == 0) {
1577 if (purge_user(requested_user) == 0) {
1583 sprintf(notify, "User \"%s\" has been deleted by %s.\n",
1584 usbuf.fullname, CC->user.fullname);
1585 aide_message(notify, "User Deletion Message");
1588 cprintf("%d Ok", CIT_OK);
1590 cprintf(" (%s deleted)", requested_user);
1597 * Check to see if the user who we just sent mail to is logged in. If yes,
1598 * bump the 'new mail' counter for their session. That enables them to
1599 * receive a new mail notification without having to hit the database.
1601 void BumpNewMailCounter(long which_user) {
1602 struct CitContext *ptr;
1604 begin_critical_section(S_SESSION_TABLE);
1606 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1607 if (ptr->user.usernum == which_user) {
1612 end_critical_section(S_SESSION_TABLE);
1617 * Count the number of new mail messages the user has
1621 int num_newmsgs = 0;
1623 num_newmsgs = CC->newmail;
1626 return (num_newmsgs);
1631 * Count the number of new mail messages the user has
1633 int InitialMailCheck()
1635 int num_newmsgs = 0;
1637 char mailboxname[ROOMNAMELEN];
1638 struct ctdlroom mailbox;
1640 struct cdbdata *cdbfr;
1641 long *msglist = NULL;
1644 MailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1645 if (getroom(&mailbox, mailboxname) != 0)
1647 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1649 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1651 if (cdbfr != NULL) {
1652 msglist = malloc(cdbfr->len);
1653 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1654 num_msgs = cdbfr->len / sizeof(long);
1658 for (a = 0; a < num_msgs; ++a) {
1659 if (msglist[a] > 0L) {
1660 if (msglist[a] > vbuf.v_lastseen) {
1665 if (msglist != NULL)
1668 return (num_newmsgs);
1674 * Set the preferred view for the current user/room combination
1676 void cmd_view(char *cmdbuf) {
1680 if (CtdlAccessCheck(ac_logged_in)) {
1684 requested_view = extract_int(cmdbuf, 0);
1686 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1687 vbuf.v_view = requested_view;
1688 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1690 cprintf("%d ok\n", CIT_OK);