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>
38 #include "serv_extensions.h"
39 #include "sysdep_decls.h"
47 #include "citserver.h"
48 #include "citadel_dirs.h"
52 * makeuserkey() - convert a username into the format used as a database key
53 * (it's just the username converted into lower case)
55 static INLINE void makeuserkey(char *key, char *username) {
58 len = strlen(username);
59 for (i=0; i<=len; ++i) {
60 key[i] = tolower(username[i]);
66 * getuser() - retrieve named user into supplied buffer.
67 * returns 0 on success
69 int getuser(struct ctdluser *usbuf, char name[])
72 char usernamekey[USERNAME_SIZE];
73 struct cdbdata *cdbus;
76 memset(usbuf, 0, sizeof(struct ctdluser));
79 makeuserkey(usernamekey, name);
80 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
82 if (cdbus == NULL) { /* user not found */
86 memcpy(usbuf, cdbus->ptr,
87 ((cdbus->len > sizeof(struct ctdluser)) ?
88 sizeof(struct ctdluser) : cdbus->len));
97 * lgetuser() - same as getuser() but locks the record
99 int lgetuser(struct ctdluser *usbuf, char *name)
103 retcode = getuser(usbuf, name);
105 begin_critical_section(S_USERS);
112 * putuser() - write user buffer into the correct place on disk
114 void putuser(struct ctdluser *usbuf)
116 char usernamekey[USERNAME_SIZE];
118 makeuserkey(usernamekey, usbuf->fullname);
120 usbuf->version = REV_LEVEL;
122 usernamekey, strlen(usernamekey),
123 usbuf, sizeof(struct ctdluser));
129 * lputuser() - same as putuser() but locks the record
131 void lputuser(struct ctdluser *usbuf)
134 end_critical_section(S_USERS);
138 * Index-generating function used by Ctdl[Get|Set]Relationship
140 int GenerateRelationshipIndex(char *IndexBuf,
152 TheIndex.iRoomID = RoomID;
153 TheIndex.iRoomGen = RoomGen;
154 TheIndex.iUserID = UserID;
156 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
157 return (sizeof(TheIndex));
163 * Back end for CtdlSetRelationship()
165 void put_visit(struct visit *newvisit)
170 /* Generate an index */
171 IndexLen = GenerateRelationshipIndex(IndexBuf,
174 newvisit->v_usernum);
176 /* Store the record */
177 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
178 newvisit, sizeof(struct visit)
186 * Define a relationship between a user and a room
188 void CtdlSetRelationship(struct visit *newvisit,
189 struct ctdluser *rel_user,
190 struct ctdlroom *rel_room)
194 /* We don't use these in Citadel because they're implicit by the
195 * index, but they must be present if the database is exported.
197 newvisit->v_roomnum = rel_room->QRnumber;
198 newvisit->v_roomgen = rel_room->QRgen;
199 newvisit->v_usernum = rel_user->usernum;
205 * Locate a relationship between a user and a room
207 void CtdlGetRelationship(struct visit *vbuf,
208 struct ctdluser *rel_user,
209 struct ctdlroom *rel_room)
214 struct cdbdata *cdbvisit;
216 /* Generate an index */
217 IndexLen = GenerateRelationshipIndex(IndexBuf,
222 /* Clear out the buffer */
223 memset(vbuf, 0, sizeof(struct visit));
225 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
226 if (cdbvisit != NULL) {
227 memcpy(vbuf, cdbvisit->ptr,
228 ((cdbvisit->len > sizeof(struct visit)) ?
229 sizeof(struct visit) : cdbvisit->len));
233 /* If this is the first time the user has seen this room,
234 * set the view to be the default for the room.
236 vbuf->v_view = rel_room->QRdefaultview;
239 /* Set v_seen if necessary */
240 if (vbuf->v_seen[0] == 0) {
241 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
246 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
248 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
253 * Is the user currently logged in an Aide?
257 if (CC->user.axlevel >= 6)
265 * Is the user currently logged in an Aide *or* the room aide for this room?
267 int is_room_aide(void)
270 if (!CC->logged_in) {
274 if ((CC->user.axlevel >= 6)
275 || (CC->room.QRroomaide == CC->user.usernum)) {
283 * getuserbynumber() - get user by number
284 * returns 0 if user was found
286 * WARNING: don't use this function unless you absolutely have to. It does
287 * a sequential search and therefore is computationally expensive.
289 int getuserbynumber(struct ctdluser *usbuf, long int number)
291 struct cdbdata *cdbus;
293 cdb_rewind(CDB_USERS);
295 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
296 memset(usbuf, 0, sizeof(struct ctdluser));
297 memcpy(usbuf, cdbus->ptr,
298 ((cdbus->len > sizeof(struct ctdluser)) ?
299 sizeof(struct ctdluser) : cdbus->len));
301 if (usbuf->usernum == number) {
302 cdb_close_cursor(CDB_USERS);
311 * getuserbyuid() - get user by system uid (for PAM mode authentication)
312 * returns 0 if user was found
314 * WARNING: don't use this function unless you absolutely have to. It does
315 * a sequential search and therefore is computationally expensive.
317 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
319 struct cdbdata *cdbus;
321 cdb_rewind(CDB_USERS);
323 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
324 memset(usbuf, 0, sizeof(struct ctdluser));
325 memcpy(usbuf, cdbus->ptr,
326 ((cdbus->len > sizeof(struct ctdluser)) ?
327 sizeof(struct ctdluser) : cdbus->len));
329 if (usbuf->uid == number) {
330 cdb_close_cursor(CDB_USERS);
340 * Back end for cmd_user() and its ilk
342 int CtdlLoginExistingUser(char *trythisname)
347 if ((CC->logged_in)) {
348 return login_already_logged_in;
351 if (trythisname == NULL) return login_not_found;
352 safestrncpy(username, trythisname, USERNAME_SIZE);
355 if (strlen(username) == 0) {
356 return login_not_found;
359 if (config.c_auth_mode == 1) {
364 struct passwd *tempPwdPtr;
367 lprintf(CTDL_DEBUG, "asking host about <%s>\n", username);
368 #ifdef SOLARIS_GETPWUID
369 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
371 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
373 if (tempPwdPtr == NULL) {
374 return login_not_found;
376 lprintf(CTDL_DEBUG, "found it! uid=%ld, gecos=%s\n", (long)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);
390 /* native auth mode */
392 struct recptypes *valid = NULL;
394 /* First, try to log in as if the supplied name is a display name */
395 found_user = getuser(&CC->user, username);
397 /* If that didn't work, try to log in as if the supplied name
398 * is an e-mail address
400 if (found_user != 0) {
401 valid = validate_recipients(username);
403 if (valid->num_local == 1) {
404 found_user = getuser(&CC->user, valid->recp_local);
406 free_recipients(valid);
411 /* Did we find something? */
412 if (found_user == 0) {
413 if (((CC->nologin)) && (CC->user.axlevel < 6)) {
414 return login_too_many_users;
416 safestrncpy(CC->curr_user, CC->user.fullname,
417 sizeof CC->curr_user);
421 return login_not_found;
429 void cmd_user(char *cmdbuf)
434 extract_token(username, cmdbuf, 0, '|', sizeof username);
437 a = CtdlLoginExistingUser(username);
439 case login_already_logged_in:
440 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
442 case login_too_many_users:
444 "Too many users are already online "
446 ERROR + MAX_SESSIONS_EXCEEDED,
447 config.c_nodename, config.c_maxsessions);
450 cprintf("%d Password required for %s\n",
451 MORE_DATA, CC->curr_user);
453 case login_not_found:
454 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
457 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
464 * session startup code which is common to both cmd_pass() and cmd_newu()
466 void session_startup(void)
470 lprintf(CTDL_NOTICE, "<%s> logged in\n", CC->curr_user);
472 lgetuser(&CC->user, CC->curr_user);
473 ++(CC->user.timescalled);
474 CC->previous_login = CC->user.lastcall;
475 time(&CC->user.lastcall);
477 /* If this user's name is the name of the system administrator
478 * (as specified in setup), automatically assign access level 6.
480 if (!strcasecmp(CC->user.fullname, config.c_sysadm)) {
481 CC->user.axlevel = 6;
484 /* If we're authenticating off the host system, automatically give
485 * root the highest level of access.
487 if (config.c_auth_mode == 1) {
488 if (CC->user.uid == 0) {
489 CC->user.axlevel = 6;
496 * Populate CC->cs_inet_email with a default address. This will be
497 * overwritten with the user's directory address, if one exists, when
498 * the vCard module's login hook runs.
500 snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
501 CC->user.fullname, config.c_fqdn);
502 for (i=0; i<strlen(CC->cs_inet_email); ++i) {
503 if (isspace(CC->cs_inet_email[i])) {
504 CC->cs_inet_email[i] = '_';
508 /* Create any personal rooms required by the system.
509 * (Technically, MAILROOM should be there already, but just in case...)
511 create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
512 create_room(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
513 create_room(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
515 /* Run any startup routines registered by loadable modules */
516 PerformSessionHooks(EVT_LOGIN);
518 /* Enter the lobby */
519 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
523 void logged_in_response(void)
525 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
526 CIT_OK, CC->user.fullname, CC->user.axlevel,
527 CC->user.timescalled, CC->user.posted,
528 CC->user.flags, CC->user.usernum,
535 * misc things to be taken care of when a user is logged out
537 void logout(struct CitContext *who)
540 * Clear out some session data. Most likely, the CitContext for this
541 * session is about to get nuked when the session disconnects, but
542 * since it's possible to log in again without reconnecting, we cannot
543 * make that assumption.
545 strcpy(who->fake_username, "");
546 strcpy(who->fake_hostname, "");
547 strcpy(who->fake_roomname, "");
551 * If there is a download in progress, abort it.
553 if (who->download_fp != NULL) {
554 fclose(who->download_fp);
555 who->download_fp = NULL;
559 * If there is an upload in progress, abort it.
561 if (who->upload_fp != NULL) {
566 * If we were talking to a network node, we're not anymore...
568 if (strlen(who->net_node) > 0) {
569 network_talking_to(who->net_node, NTT_REMOVE);
572 /* Do modular stuff... */
573 PerformSessionHooks(EVT_LOGOUT);
575 /* Free any output buffers */
576 if (who->output_buffer != NULL) {
582 * Validate a password on the host unix system by calling the 'chkpwd' utility
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))
643 int CtdlTryPassword(char *password)
647 if ((CC->logged_in)) {
648 lprintf(CTDL_WARNING, "CtdlTryPassword: already logged in\n");
649 return pass_already_logged_in;
651 if (!strcmp(CC->curr_user, NLI)) {
652 lprintf(CTDL_WARNING, "CtdlTryPassword: no user selected\n");
655 if (getuser(&CC->user, CC->curr_user)) {
656 lprintf(CTDL_ERR, "CtdlTryPassword: internal error\n");
657 return pass_internal_error;
659 if (password == NULL) {
660 lprintf(CTDL_INFO, "CtdlTryPassword: NULL password string supplied\n");
661 return pass_wrong_password;
665 if (config.c_auth_mode == 1) {
669 if (validpw(CC->user.uid, password)) {
673 * sooper-seekrit hack: populate the password field in the
674 * citadel database with the password that the user typed,
675 * if it's correct. This allows most sites to convert from
676 * host auth to native auth if they want to. If you think
677 * this is a security hazard, comment it out.
680 lgetuser(&CC->user, CC->curr_user);
681 safestrncpy(CC->user.password, password, sizeof CC->user.password);
685 * (sooper-seekrit hack ends here)
696 /* native auth mode */
699 strproc(CC->user.password);
700 code = strcasecmp(CC->user.password, password);
702 strproc(CC->user.password);
703 code = strcasecmp(CC->user.password, password);
710 lprintf(CTDL_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
711 return pass_wrong_password;
716 void cmd_pass(char *buf)
721 extract_token(password, buf, 0, '|', sizeof password);
722 a = CtdlTryPassword(password);
725 case pass_already_logged_in:
726 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
729 cprintf("%d You must send a name with USER first.\n",
730 ERROR + USERNAME_REQUIRED);
732 case pass_wrong_password:
733 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
736 logged_in_response();
744 * Delete a user record *and* all of its related resources.
746 int purge_user(char pname[])
749 struct ctdluser usbuf;
750 char usernamekey[USERNAME_SIZE];
751 struct CitContext *ccptr;
752 int user_is_logged_in = 0;
754 makeuserkey(usernamekey, pname);
756 if (getuser(&usbuf, pname) != 0) {
757 lprintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
758 return (ERROR + NO_SUCH_USER);
760 /* Don't delete a user who is currently logged in. Instead, just
761 * set the access level to 0, and let the account get swept up
762 * during the next purge.
764 user_is_logged_in = 0;
765 begin_critical_section(S_SESSION_TABLE);
766 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
767 if (ccptr->user.usernum == usbuf.usernum) {
768 user_is_logged_in = 1;
771 end_critical_section(S_SESSION_TABLE);
772 if (user_is_logged_in == 1) {
773 lprintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
778 lprintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
780 /* Perform any purge functions registered by server extensions */
781 PerformUserHooks(&usbuf, EVT_PURGEUSER);
783 /* delete any existing user/room relationships */
784 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
786 /* delete the userlog entry */
787 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
789 /* remove the user's bio file */
797 /* remove the user's picture */
810 * create_user() - back end processing to create a new user
812 * Set 'newusername' to the desired account name.
813 * Set 'become_user' to nonzero if this is self-service account creation and we want
814 * to actually log in as the user we just created, otherwise set it to 0.
816 int create_user(char *newusername, int become_user)
818 struct ctdluser usbuf;
819 struct ctdlroom qrbuf;
821 char mailboxname[ROOMNAMELEN];
825 safestrncpy(username, newusername, sizeof username);
828 if (config.c_auth_mode == 1) {
833 struct passwd *tempPwdPtr;
836 #ifdef SOLARIS_GETPWUID
837 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof(pwdbuffer));
839 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
841 if (tempPwdPtr != NULL) {
842 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
846 return (ERROR + NO_SUCH_USER);
850 if (!getuser(&usbuf, username)) {
851 return (ERROR + ALREADY_EXISTS);
854 /* Go ahead and initialize a new user record */
855 memset(&usbuf, 0, sizeof(struct ctdluser));
856 safestrncpy(usbuf.fullname, username, sizeof usbuf.fullname);
857 strcpy(usbuf.password, "");
860 /* These are the default flags on new accounts */
861 usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
863 usbuf.timescalled = 0;
865 usbuf.axlevel = config.c_initax;
866 usbuf.USscreenwidth = 80;
867 usbuf.USscreenheight = 24;
868 usbuf.lastcall = time(NULL);
870 /* fetch a new user number */
871 usbuf.usernum = get_new_user_number();
873 /* The very first user created on the system will always be an Aide */
874 if (usbuf.usernum == 1L) {
878 /* add user to userlog */
882 * Give the user a private mailbox and a configuration room.
883 * Make the latter an invisible system room.
885 MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
886 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
888 MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
889 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
890 if (lgetroom(&qrbuf, mailboxname) == 0) {
891 qrbuf.QRflags2 |= QR2_SYSTEM;
895 /* Perform any create functions registered by server extensions */
896 PerformUserHooks(&usbuf, EVT_NEWUSER);
898 /* Everything below this line can be bypassed if administratively
899 * creating a user, instead of doing self-service account creation
903 /* Now become the user we just created */
904 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
905 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
908 /* Check to make sure we're still who we think we are */
909 if (getuser(&CC->user, CC->curr_user)) {
910 return (ERROR + INTERNAL_ERROR);
915 "New user account <%s> has been created, from host %s [%s].\n",
920 aide_message(buf, "User Creation Notice");
921 lprintf(CTDL_NOTICE, "New user <%s> created\n", username);
929 * cmd_newu() - create a new user account and log in as that user
931 void cmd_newu(char *cmdbuf)
936 if (config.c_auth_mode == 1) {
937 cprintf("%d This system does not use native mode authentication.\n",
942 if (config.c_disable_newu) {
943 cprintf("%d Self-service user account creation "
944 "is disabled on this system.\n", ERROR + NOT_HERE);
949 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
953 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
954 ERROR + MAX_SESSIONS_EXCEEDED,
955 config.c_nodename, config.c_maxsessions);
957 extract_token(username, cmdbuf, 0, '|', sizeof username);
961 if (strlen(username) == 0) {
962 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
966 if ((!strcasecmp(username, "bbs")) ||
967 (!strcasecmp(username, "new")) ||
968 (!strcasecmp(username, "."))) {
969 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
973 a = create_user(username, 1);
977 logged_in_response();
978 } else if (a == ERROR + ALREADY_EXISTS) {
979 cprintf("%d '%s' already exists.\n",
980 ERROR + ALREADY_EXISTS, username);
982 } else if (a == ERROR + INTERNAL_ERROR) {
983 cprintf("%d Internal error - user record disappeared?\n",
984 ERROR + INTERNAL_ERROR);
987 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
996 void cmd_setp(char *new_pw)
998 if (CtdlAccessCheck(ac_logged_in)) {
1001 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
1002 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
1006 if (strlen(new_pw) == 0) {
1007 cprintf("%d Password unchanged.\n", CIT_OK);
1010 lgetuser(&CC->user, CC->curr_user);
1011 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1012 lputuser(&CC->user);
1013 cprintf("%d Password changed.\n", CIT_OK);
1014 lprintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
1015 PerformSessionHooks(EVT_SETPASS);
1020 * cmd_creu() - administratively create a new user account (do not log in to it)
1022 void cmd_creu(char *cmdbuf)
1027 struct ctdluser tmp;
1029 if (CtdlAccessCheck(ac_aide)) {
1033 extract_token(username, cmdbuf, 0, '|', sizeof username);
1034 extract_token(password, cmdbuf, 1, '|', sizeof password);
1040 if (strlen(username) == 0) {
1041 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1045 a = create_user(username, 0);
1048 if (strlen(password) > 0) {
1049 lgetuser(&tmp, username);
1050 safestrncpy(tmp.password, password, sizeof(tmp.password));
1053 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1054 (strlen(password) > 0) ? "and password set" :
1055 "with no password");
1057 } else if (a == ERROR + ALREADY_EXISTS) {
1058 cprintf("%d '%s' already exists.\n",
1059 ERROR + ALREADY_EXISTS, username);
1062 cprintf("%d An error occured creating the user account.\n", ERROR + INTERNAL_ERROR);
1069 * get user parameters
1074 if (CtdlAccessCheck(ac_logged_in))
1077 getuser(&CC->user, CC->curr_user);
1078 cprintf("%d %d|%d|%d|\n",
1080 CC->user.USscreenwidth,
1081 CC->user.USscreenheight,
1082 (CC->user.flags & US_USER_SET)
1087 * set user parameters
1089 void cmd_setu(char *new_parms)
1091 if (CtdlAccessCheck(ac_logged_in))
1094 if (num_parms(new_parms) < 3) {
1095 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1098 lgetuser(&CC->user, CC->curr_user);
1099 CC->user.USscreenwidth = extract_int(new_parms, 0);
1100 CC->user.USscreenheight = extract_int(new_parms, 1);
1101 CC->user.flags = CC->user.flags & (~US_USER_SET);
1102 CC->user.flags = CC->user.flags |
1103 (extract_int(new_parms, 2) & US_USER_SET);
1105 lputuser(&CC->user);
1106 cprintf("%d Ok\n", CIT_OK);
1110 * set last read pointer
1112 void cmd_slrp(char *new_ptr)
1116 struct visit original_vbuf;
1118 if (CtdlAccessCheck(ac_logged_in)) {
1122 if (!strncasecmp(new_ptr, "highest", 7)) {
1123 newlr = CC->room.QRhighest;
1125 newlr = atol(new_ptr);
1128 lgetuser(&CC->user, CC->curr_user);
1130 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1131 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1132 vbuf.v_lastseen = newlr;
1133 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1135 /* Only rewrite the record if it changed */
1136 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1137 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1138 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1141 lputuser(&CC->user);
1142 cprintf("%d %ld\n", CIT_OK, newlr);
1146 void cmd_seen(char *argbuf) {
1147 long target_msgnum = 0L;
1148 int target_setting = 0;
1150 if (CtdlAccessCheck(ac_logged_in)) {
1154 if (num_parms(argbuf) != 2) {
1155 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1159 target_msgnum = extract_long(argbuf, 0);
1160 target_setting = extract_int(argbuf, 1);
1162 CtdlSetSeen(&target_msgnum, 1, target_setting,
1163 ctdlsetseen_seen, NULL, NULL);
1164 cprintf("%d OK\n", CIT_OK);
1168 void cmd_gtsn(char *argbuf) {
1171 if (CtdlAccessCheck(ac_logged_in)) {
1175 CtdlGetSeen(buf, ctdlsetseen_seen);
1176 cprintf("%d %s\n", CIT_OK, buf);
1181 * API function for cmd_invt_kick() and anything else that needs to
1182 * invite or kick out a user to/from a room.
1184 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1186 int CtdlInvtKick(char *iuser, int op) {
1187 struct ctdluser USscratch;
1191 if (getuser(&USscratch, iuser) != 0) {
1195 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1197 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1198 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1201 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1202 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1204 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1206 /* post a message in Aide> saying what we just did */
1207 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1209 ((op == 1) ? "invited to" : "kicked out of"),
1212 aide_message(bbb,"User Admin Message");
1219 * INVT and KICK commands
1221 void cmd_invt_kick(char *iuser, int op) {
1224 * These commands are only allowed by aides, room aides,
1225 * and room namespace owners
1228 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1229 /* access granted */
1232 cprintf("%d Higher access or room ownership required.\n",
1233 ERROR + HIGHER_ACCESS_REQUIRED);
1237 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1239 cprintf("%d Can't add/remove users from this room.\n",
1244 if (CtdlInvtKick(iuser, op) != 0) {
1245 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1249 cprintf("%d %s %s %s.\n",
1251 ((op == 1) ? "invited to" : "kicked out of"),
1258 * Forget (Zap) the current room (API call)
1259 * Returns 0 on success
1261 int CtdlForgetThisRoom(void) {
1264 /* On some systems, Aides are not allowed to forget rooms */
1265 if (is_aide() && (config.c_aide_zap == 0)
1266 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1270 lgetuser(&CC->user, CC->curr_user);
1271 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1273 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1274 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1276 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1277 lputuser(&CC->user);
1279 /* Return to the Lobby, so we don't end up in an undefined room */
1280 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1287 * forget (Zap) the current room
1292 if (CtdlAccessCheck(ac_logged_in)) {
1296 if (CtdlForgetThisRoom() == 0) {
1297 cprintf("%d Ok\n", CIT_OK);
1300 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1305 * Get Next Unregistered User
1309 struct cdbdata *cdbus;
1310 struct ctdluser usbuf;
1312 if (CtdlAccessCheck(ac_aide)) {
1316 if ((CitControl.MMflags & MM_VALID) == 0) {
1317 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1321 /* There are unvalidated users. Traverse the user database,
1322 * and return the first user we find that needs validation.
1324 cdb_rewind(CDB_USERS);
1325 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1326 memset(&usbuf, 0, sizeof(struct ctdluser));
1327 memcpy(&usbuf, cdbus->ptr,
1328 ((cdbus->len > sizeof(struct ctdluser)) ?
1329 sizeof(struct ctdluser) : cdbus->len));
1331 if ((usbuf.flags & US_NEEDVALID)
1332 && (usbuf.axlevel > 0)) {
1333 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1334 cdb_close_cursor(CDB_USERS);
1339 /* If we get to this point, there are no more unvalidated users.
1340 * Therefore we clear the "users need validation" flag.
1343 begin_critical_section(S_CONTROL);
1345 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1347 end_critical_section(S_CONTROL);
1348 cprintf("%d *** End of registration.\n", CIT_OK);
1357 void cmd_vali(char *v_args)
1361 struct ctdluser userbuf;
1363 extract_token(user, v_args, 0, '|', sizeof user);
1364 newax = extract_int(v_args, 1);
1366 if (CtdlAccessCheck(ac_aide)) {
1370 if (lgetuser(&userbuf, user) != 0) {
1371 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1375 userbuf.axlevel = newax;
1376 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1380 /* If the access level was set to zero, delete the user */
1382 if (purge_user(user) == 0) {
1383 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1387 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1393 * Traverse the user file...
1395 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1398 struct ctdluser usbuf;
1399 struct cdbdata *cdbus;
1401 cdb_rewind(CDB_USERS);
1403 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1404 memset(&usbuf, 0, sizeof(struct ctdluser));
1405 memcpy(&usbuf, cdbus->ptr,
1406 ((cdbus->len > sizeof(struct ctdluser)) ?
1407 sizeof(struct ctdluser) : cdbus->len));
1409 (*CallBack) (&usbuf, in_data);
1415 * List one user (this works with cmd_list)
1417 void ListThisUser(struct ctdluser *usbuf, void *data)
1421 searchstring = (char *)data;
1422 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1426 if (usbuf->axlevel > 0) {
1427 if ((CC->user.axlevel >= 6)
1428 || ((usbuf->flags & US_UNLISTED) == 0)
1429 || ((CC->internal_pgm))) {
1430 cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1434 (long)usbuf->lastcall,
1437 if (CC->user.axlevel >= 6)
1438 cprintf("%s", usbuf->password);
1445 * List users (searchstring may be empty to list all users)
1447 void cmd_list(char *cmdbuf)
1449 char searchstring[256];
1450 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1451 striplt(searchstring);
1452 cprintf("%d \n", LISTING_FOLLOWS);
1453 ForEachUser(ListThisUser, (void *)searchstring );
1461 * assorted info we need to check at login
1469 if (CtdlAccessCheck(ac_logged_in)) {
1473 getuser(&CC->user, CC->curr_user); /* no lock is needed here */
1474 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1477 if (CC->user.axlevel >= 6) {
1479 if (CitControl.MMflags & MM_VALID)
1483 /* check for mail */
1484 mail = InitialMailCheck();
1486 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1491 * check to see if a user exists
1493 void cmd_qusr(char *who)
1495 struct ctdluser usbuf;
1497 if (getuser(&usbuf, who) == 0) {
1498 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1500 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1506 * Administrative Get User Parameters
1508 void cmd_agup(char *cmdbuf)
1510 struct ctdluser usbuf;
1511 char requested_user[128];
1513 if (CtdlAccessCheck(ac_aide)) {
1517 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1518 if (getuser(&usbuf, requested_user) != 0) {
1519 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1522 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1529 (int) usbuf.axlevel,
1531 (long)usbuf.lastcall,
1538 * Administrative Set User Parameters
1540 void cmd_asup(char *cmdbuf)
1542 struct ctdluser usbuf;
1543 char requested_user[128];
1549 if (CtdlAccessCheck(ac_aide))
1552 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1553 if (lgetuser(&usbuf, requested_user) != 0) {
1554 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1557 np = num_parms(cmdbuf);
1559 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1561 usbuf.flags = extract_int(cmdbuf, 2);
1563 usbuf.timescalled = extract_int(cmdbuf, 3);
1565 usbuf.posted = extract_int(cmdbuf, 4);
1567 newax = extract_int(cmdbuf, 5);
1568 if ((newax >= 0) && (newax <= 6)) {
1569 usbuf.axlevel = extract_int(cmdbuf, 5);
1573 usbuf.lastcall = extract_long(cmdbuf, 7);
1576 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1579 if (usbuf.axlevel == 0) {
1580 if (purge_user(requested_user) == 0) {
1586 sprintf(notify, "User \"%s\" has been deleted by %s.\n",
1587 usbuf.fullname, CC->user.fullname);
1588 aide_message(notify, "User Deletion Message");
1591 cprintf("%d Ok", CIT_OK);
1593 cprintf(" (%s deleted)", requested_user);
1600 * Check to see if the user who we just sent mail to is logged in. If yes,
1601 * bump the 'new mail' counter for their session. That enables them to
1602 * receive a new mail notification without having to hit the database.
1604 void BumpNewMailCounter(long which_user) {
1605 struct CitContext *ptr;
1607 begin_critical_section(S_SESSION_TABLE);
1609 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1610 if (ptr->user.usernum == which_user) {
1615 end_critical_section(S_SESSION_TABLE);
1620 * Count the number of new mail messages the user has
1624 int num_newmsgs = 0;
1626 num_newmsgs = CC->newmail;
1629 return (num_newmsgs);
1634 * Count the number of new mail messages the user has
1636 int InitialMailCheck()
1638 int num_newmsgs = 0;
1640 char mailboxname[ROOMNAMELEN];
1641 struct ctdlroom mailbox;
1643 struct cdbdata *cdbfr;
1644 long *msglist = NULL;
1647 MailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1648 if (getroom(&mailbox, mailboxname) != 0)
1650 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1652 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1654 if (cdbfr != NULL) {
1655 msglist = malloc(cdbfr->len);
1656 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1657 num_msgs = cdbfr->len / sizeof(long);
1661 for (a = 0; a < num_msgs; ++a) {
1662 if (msglist[a] > 0L) {
1663 if (msglist[a] > vbuf.v_lastseen) {
1668 if (msglist != NULL)
1671 return (num_newmsgs);
1677 * Set the preferred view for the current user/room combination
1679 void cmd_view(char *cmdbuf) {
1683 if (CtdlAccessCheck(ac_logged_in)) {
1687 requested_view = extract_int(cmdbuf, 0);
1689 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1690 vbuf.v_view = requested_view;
1691 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1693 cprintf("%d ok\n", CIT_OK);