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 #ifdef SOLARIS_GETPWUID
371 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
373 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
375 if (tempPwdPtr == NULL) {
376 return login_not_found;
378 lprintf(CTDL_DEBUG, "found it! uid=%ld, gecos=%s\n", (long)pd.pw_uid, pd.pw_gecos);
380 /* Locate the associated Citadel account.
381 * If not found, make one attempt to create it.
383 found_user = getuserbyuid(&CC->user, pd.pw_uid);
384 if (found_user != 0) {
385 create_user(username, 0);
386 found_user = getuserbyuid(&CC->user, pd.pw_uid);
392 /* native auth mode */
394 struct recptypes *valid = NULL;
396 /* First, try to log in as if the supplied name is a display name */
397 found_user = getuser(&CC->user, username);
399 /* If that didn't work, try to log in as if the supplied name
400 * is an e-mail address
402 if (found_user != 0) {
403 valid = validate_recipients(username);
405 if (valid->num_local == 1) {
406 found_user = getuser(&CC->user, valid->recp_local);
413 /* Did we find something? */
414 if (found_user == 0) {
415 if (((CC->nologin)) && (CC->user.axlevel < 6)) {
416 return login_too_many_users;
418 safestrncpy(CC->curr_user, CC->user.fullname,
419 sizeof CC->curr_user);
423 return login_not_found;
431 void cmd_user(char *cmdbuf)
436 extract_token(username, cmdbuf, 0, '|', sizeof username);
439 a = CtdlLoginExistingUser(username);
441 case login_already_logged_in:
442 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
444 case login_too_many_users:
446 "Too many users are already online "
448 ERROR + MAX_SESSIONS_EXCEEDED,
449 config.c_nodename, config.c_maxsessions);
452 cprintf("%d Password required for %s\n",
453 MORE_DATA, CC->curr_user);
455 case login_not_found:
456 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
459 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
466 * session startup code which is common to both cmd_pass() and cmd_newu()
468 void session_startup(void)
472 lprintf(CTDL_NOTICE, "<%s> logged in\n", CC->curr_user);
474 lgetuser(&CC->user, CC->curr_user);
475 ++(CC->user.timescalled);
476 CC->previous_login = CC->user.lastcall;
477 time(&CC->user.lastcall);
479 /* If this user's name is the name of the system administrator
480 * (as specified in setup), automatically assign access level 6.
482 if (!strcasecmp(CC->user.fullname, config.c_sysadm)) {
483 CC->user.axlevel = 6;
486 /* If we're authenticating off the host system, automatically give
487 * root the highest level of access.
489 if (config.c_auth_mode == 1) {
490 if (CC->user.uid == 0) {
491 CC->user.axlevel = 6;
498 * Populate CC->cs_inet_email with a default address. This will be
499 * overwritten with the user's directory address, if one exists, when
500 * the vCard module's login hook runs.
502 snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
503 CC->user.fullname, config.c_fqdn);
504 for (i=0; i<strlen(CC->cs_inet_email); ++i) {
505 if (isspace(CC->cs_inet_email[i])) {
506 CC->cs_inet_email[i] = '_';
510 /* Create any personal rooms required by the system.
511 * (Technically, MAILROOM should be there already, but just in case...)
513 create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
514 create_room(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
515 create_room(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
517 /* Run any startup routines registered by loadable modules */
518 PerformSessionHooks(EVT_LOGIN);
520 /* Enter the lobby */
521 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
525 void logged_in_response(void)
527 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
528 CIT_OK, CC->user.fullname, CC->user.axlevel,
529 CC->user.timescalled, CC->user.posted,
530 CC->user.flags, CC->user.usernum,
537 * misc things to be taken care of when a user is logged out
539 void logout(struct CitContext *who)
542 * Clear out some session data. Most likely, the CitContext for this
543 * session is about to get nuked when the session disconnects, but
544 * since it's possible to log in again without reconnecting, we cannot
545 * make that assumption.
547 strcpy(who->fake_username, "");
548 strcpy(who->fake_hostname, "");
549 strcpy(who->fake_roomname, "");
553 * If there is a download in progress, abort it.
555 if (who->download_fp != NULL) {
556 fclose(who->download_fp);
557 who->download_fp = NULL;
561 * If there is an upload in progress, abort it.
563 if (who->upload_fp != NULL) {
568 * If we were talking to a network node, we're not anymore...
570 if (strlen(who->net_node) > 0) {
571 network_talking_to(who->net_node, NTT_REMOVE);
574 /* Do modular stuff... */
575 PerformSessionHooks(EVT_LOGOUT);
577 /* Free any output buffers */
578 if (who->output_buffer != NULL) {
585 * an alternate version of validpw() which executes `chkpwd' instead of
586 * verifying the password directly
588 static int validpw(uid_t uid, const char *pass)
591 int status, pipev[2];
595 lprintf(CTDL_ERR, "pipe failed (%s): denying host auth access for "
596 "uid %ld\n", strerror(errno), (long)uid);
599 switch (pid = fork()) {
601 lprintf(CTDL_ERR, "fork failed (%s): denying host auth access for "
602 "uid %ld\n", strerror(errno), (long)uid);
609 if (dup2(pipev[0], 0) == -1) {
615 execl(file_chkpwd, file_chkpwd, NULL);
622 snprintf(buf, sizeof buf, "%lu\n", (unsigned long) uid));
623 write(pipev[1], pass, strlen(pass));
624 write(pipev[1], "\n", 1);
627 while (waitpid(pid, &status, 0) == -1)
628 if (errno != EINTR) {
629 lprintf(CTDL_ERR, "waitpid failed (%s): denying host auth "
630 "access for uid %ld\n",
631 strerror(errno), (long)uid);
634 if (WIFEXITED(status) && !WEXITSTATUS(status))
648 int CtdlTryPassword(char *password)
652 if ((CC->logged_in)) {
653 lprintf(CTDL_WARNING, "CtdlTryPassword: already logged in\n");
654 return pass_already_logged_in;
656 if (!strcmp(CC->curr_user, NLI)) {
657 lprintf(CTDL_WARNING, "CtdlTryPassword: no user selected\n");
660 if (getuser(&CC->user, CC->curr_user)) {
661 lprintf(CTDL_ERR, "CtdlTryPassword: internal error\n");
662 return pass_internal_error;
664 if (password == NULL) {
665 lprintf(CTDL_INFO, "CtdlTryPassword: NULL password string supplied\n");
666 return pass_wrong_password;
670 if (config.c_auth_mode == 1) {
674 if (validpw(CC->user.uid, password)) {
678 * sooper-seekrit hack: populate the password field in the
679 * citadel database with the password that the user typed,
680 * if it's correct. This allows most sites to convert from
681 * host auth to native auth if they want to. If you think
682 * this is a security hazard, comment it out.
685 lgetuser(&CC->user, CC->curr_user);
686 safestrncpy(CC->user.password, password, sizeof CC->user.password);
690 * (sooper-seekrit hack ends here)
701 /* native auth mode */
704 strproc(CC->user.password);
705 code = strcasecmp(CC->user.password, password);
707 strproc(CC->user.password);
708 code = strcasecmp(CC->user.password, password);
715 lprintf(CTDL_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
716 return pass_wrong_password;
721 void cmd_pass(char *buf)
726 extract_token(password, buf, 0, '|', sizeof password);
727 a = CtdlTryPassword(password);
730 case pass_already_logged_in:
731 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
734 cprintf("%d You must send a name with USER first.\n",
735 ERROR + USERNAME_REQUIRED);
737 case pass_wrong_password:
738 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
741 logged_in_response();
749 * Delete a user record *and* all of its related resources.
751 int purge_user(char pname[])
754 struct ctdluser usbuf;
755 char usernamekey[USERNAME_SIZE];
756 struct CitContext *ccptr;
757 int user_is_logged_in = 0;
759 makeuserkey(usernamekey, pname);
761 if (getuser(&usbuf, pname) != 0) {
762 lprintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
763 return (ERROR + NO_SUCH_USER);
765 /* Don't delete a user who is currently logged in. Instead, just
766 * set the access level to 0, and let the account get swept up
767 * during the next purge.
769 user_is_logged_in = 0;
770 begin_critical_section(S_SESSION_TABLE);
771 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
772 if (ccptr->user.usernum == usbuf.usernum) {
773 user_is_logged_in = 1;
776 end_critical_section(S_SESSION_TABLE);
777 if (user_is_logged_in == 1) {
778 lprintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
783 lprintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
785 /* Perform any purge functions registered by server extensions */
786 PerformUserHooks(&usbuf, EVT_PURGEUSER);
788 /* delete any existing user/room relationships */
789 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
791 /* delete the userlog entry */
792 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
794 /* remove the user's bio file */
802 /* remove the user's picture */
815 * create_user() - back end processing to create a new user
817 * Set 'newusername' to the desired account name.
818 * Set 'become_user' to nonzero if this is self-service account creation and we want
819 * to actually log in as the user we just created, otherwise set it to 0.
821 int create_user(char *newusername, int become_user)
823 struct ctdluser usbuf;
824 struct ctdlroom qrbuf;
826 char mailboxname[ROOMNAMELEN];
830 safestrncpy(username, newusername, sizeof username);
833 if (config.c_auth_mode == 1) {
838 struct passwd *tempPwdPtr;
841 #ifdef SOLARIS_GETPWUID
842 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof(pwdbuffer));
844 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
846 if (tempPwdPtr != NULL) {
847 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
851 return (ERROR + NO_SUCH_USER);
855 if (!getuser(&usbuf, username)) {
856 return (ERROR + ALREADY_EXISTS);
859 /* Go ahead and initialize a new user record */
860 memset(&usbuf, 0, sizeof(struct ctdluser));
861 safestrncpy(usbuf.fullname, username, sizeof usbuf.fullname);
862 strcpy(usbuf.password, "");
865 /* These are the default flags on new accounts */
866 usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
868 usbuf.timescalled = 0;
870 usbuf.axlevel = config.c_initax;
871 usbuf.USscreenwidth = 80;
872 usbuf.USscreenheight = 24;
873 usbuf.lastcall = time(NULL);
875 /* fetch a new user number */
876 usbuf.usernum = get_new_user_number();
878 /* The very first user created on the system will always be an Aide */
879 if (usbuf.usernum == 1L) {
883 /* add user to userlog */
887 * Give the user a private mailbox and a configuration room.
888 * Make the latter an invisible system room.
890 MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
891 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
893 MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
894 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
895 if (lgetroom(&qrbuf, mailboxname) == 0) {
896 qrbuf.QRflags2 |= QR2_SYSTEM;
900 /* Perform any create functions registered by server extensions */
901 PerformUserHooks(&usbuf, EVT_NEWUSER);
903 /* Everything below this line can be bypassed if administratively
904 * creating a user, instead of doing self-service account creation
908 /* Now become the user we just created */
909 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
910 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
913 /* Check to make sure we're still who we think we are */
914 if (getuser(&CC->user, CC->curr_user)) {
915 return (ERROR + INTERNAL_ERROR);
920 "New user account <%s> has been created, from host %s [%s].\n",
925 aide_message(buf, "User Creation Notice");
926 lprintf(CTDL_NOTICE, "New user <%s> created\n", username);
934 * cmd_newu() - create a new user account and log in as that user
936 void cmd_newu(char *cmdbuf)
941 if (config.c_auth_mode == 1) {
942 cprintf("%d This system does not use native mode authentication.\n",
947 if (config.c_disable_newu) {
948 cprintf("%d Self-service user account creation "
949 "is disabled on this system.\n", ERROR + NOT_HERE);
954 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
958 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
959 ERROR + MAX_SESSIONS_EXCEEDED,
960 config.c_nodename, config.c_maxsessions);
962 extract_token(username, cmdbuf, 0, '|', sizeof username);
966 if (strlen(username) == 0) {
967 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
971 if ((!strcasecmp(username, "bbs")) ||
972 (!strcasecmp(username, "new")) ||
973 (!strcasecmp(username, "."))) {
974 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
978 a = create_user(username, 1);
982 logged_in_response();
983 } else if (a == ERROR + ALREADY_EXISTS) {
984 cprintf("%d '%s' already exists.\n",
985 ERROR + ALREADY_EXISTS, username);
987 } else if (a == ERROR + INTERNAL_ERROR) {
988 cprintf("%d Internal error - user record disappeared?\n",
989 ERROR + INTERNAL_ERROR);
992 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
1001 void cmd_setp(char *new_pw)
1003 if (CtdlAccessCheck(ac_logged_in)) {
1006 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
1007 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
1011 if (strlen(new_pw) == 0) {
1012 cprintf("%d Password unchanged.\n", CIT_OK);
1015 lgetuser(&CC->user, CC->curr_user);
1016 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1017 lputuser(&CC->user);
1018 cprintf("%d Password changed.\n", CIT_OK);
1019 lprintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
1020 PerformSessionHooks(EVT_SETPASS);
1025 * cmd_creu() - administratively create a new user account (do not log in to it)
1027 void cmd_creu(char *cmdbuf)
1032 struct ctdluser tmp;
1034 if (CtdlAccessCheck(ac_aide)) {
1038 extract_token(username, cmdbuf, 0, '|', sizeof username);
1039 extract_token(password, cmdbuf, 1, '|', sizeof password);
1045 if (strlen(username) == 0) {
1046 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1050 a = create_user(username, 0);
1053 if (strlen(password) > 0) {
1054 lgetuser(&tmp, username);
1055 safestrncpy(tmp.password, password, sizeof(tmp.password));
1058 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1059 (strlen(password) > 0) ? "and password set" :
1060 "with no password");
1062 } else if (a == ERROR + ALREADY_EXISTS) {
1063 cprintf("%d '%s' already exists.\n",
1064 ERROR + ALREADY_EXISTS, username);
1067 cprintf("%d An error occured creating the user account.\n", ERROR + INTERNAL_ERROR);
1074 * get user parameters
1079 if (CtdlAccessCheck(ac_logged_in))
1082 getuser(&CC->user, CC->curr_user);
1083 cprintf("%d %d|%d|%d|\n",
1085 CC->user.USscreenwidth,
1086 CC->user.USscreenheight,
1087 (CC->user.flags & US_USER_SET)
1092 * set user parameters
1094 void cmd_setu(char *new_parms)
1096 if (CtdlAccessCheck(ac_logged_in))
1099 if (num_parms(new_parms) < 3) {
1100 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1103 lgetuser(&CC->user, CC->curr_user);
1104 CC->user.USscreenwidth = extract_int(new_parms, 0);
1105 CC->user.USscreenheight = extract_int(new_parms, 1);
1106 CC->user.flags = CC->user.flags & (~US_USER_SET);
1107 CC->user.flags = CC->user.flags |
1108 (extract_int(new_parms, 2) & US_USER_SET);
1110 lputuser(&CC->user);
1111 cprintf("%d Ok\n", CIT_OK);
1115 * set last read pointer
1117 void cmd_slrp(char *new_ptr)
1121 struct visit original_vbuf;
1123 if (CtdlAccessCheck(ac_logged_in)) {
1127 if (!strncasecmp(new_ptr, "highest", 7)) {
1128 newlr = CC->room.QRhighest;
1130 newlr = atol(new_ptr);
1133 lgetuser(&CC->user, CC->curr_user);
1135 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1136 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1137 vbuf.v_lastseen = newlr;
1138 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1140 /* Only rewrite the record if it changed */
1141 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1142 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1143 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1146 lputuser(&CC->user);
1147 cprintf("%d %ld\n", CIT_OK, newlr);
1151 void cmd_seen(char *argbuf) {
1152 long target_msgnum = 0L;
1153 int target_setting = 0;
1155 if (CtdlAccessCheck(ac_logged_in)) {
1159 if (num_parms(argbuf) != 2) {
1160 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1164 target_msgnum = extract_long(argbuf, 0);
1165 target_setting = extract_int(argbuf, 1);
1167 CtdlSetSeen(&target_msgnum, 1, target_setting,
1168 ctdlsetseen_seen, NULL, NULL);
1169 cprintf("%d OK\n", CIT_OK);
1173 void cmd_gtsn(char *argbuf) {
1176 if (CtdlAccessCheck(ac_logged_in)) {
1180 CtdlGetSeen(buf, ctdlsetseen_seen);
1181 cprintf("%d %s\n", CIT_OK, buf);
1186 * API function for cmd_invt_kick() and anything else that needs to
1187 * invite or kick out a user to/from a room.
1189 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1191 int CtdlInvtKick(char *iuser, int op) {
1192 struct ctdluser USscratch;
1196 if (getuser(&USscratch, iuser) != 0) {
1200 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1202 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1203 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1206 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1207 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1209 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1211 /* post a message in Aide> saying what we just did */
1212 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1214 ((op == 1) ? "invited to" : "kicked out of"),
1217 aide_message(bbb,"User Admin Message");
1224 * INVT and KICK commands
1226 void cmd_invt_kick(char *iuser, int op) {
1229 * These commands are only allowed by aides, room aides,
1230 * and room namespace owners
1233 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1234 /* access granted */
1237 cprintf("%d Higher access or room ownership required.\n",
1238 ERROR + HIGHER_ACCESS_REQUIRED);
1242 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1244 cprintf("%d Can't add/remove users from this room.\n",
1249 if (CtdlInvtKick(iuser, op) != 0) {
1250 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1254 cprintf("%d %s %s %s.\n",
1256 ((op == 1) ? "invited to" : "kicked out of"),
1263 * Forget (Zap) the current room (API call)
1264 * Returns 0 on success
1266 int CtdlForgetThisRoom(void) {
1269 /* On some systems, Aides are not allowed to forget rooms */
1270 if (is_aide() && (config.c_aide_zap == 0)
1271 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1275 lgetuser(&CC->user, CC->curr_user);
1276 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1278 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1279 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1281 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1282 lputuser(&CC->user);
1284 /* Return to the Lobby, so we don't end up in an undefined room */
1285 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1292 * forget (Zap) the current room
1297 if (CtdlAccessCheck(ac_logged_in)) {
1301 if (CtdlForgetThisRoom() == 0) {
1302 cprintf("%d Ok\n", CIT_OK);
1305 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1310 * Get Next Unregistered User
1314 struct cdbdata *cdbus;
1315 struct ctdluser usbuf;
1317 if (CtdlAccessCheck(ac_aide)) {
1321 if ((CitControl.MMflags & MM_VALID) == 0) {
1322 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1326 /* There are unvalidated users. Traverse the user database,
1327 * and return the first user we find that needs validation.
1329 cdb_rewind(CDB_USERS);
1330 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1331 memset(&usbuf, 0, sizeof(struct ctdluser));
1332 memcpy(&usbuf, cdbus->ptr,
1333 ((cdbus->len > sizeof(struct ctdluser)) ?
1334 sizeof(struct ctdluser) : cdbus->len));
1336 if ((usbuf.flags & US_NEEDVALID)
1337 && (usbuf.axlevel > 0)) {
1338 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1339 cdb_close_cursor(CDB_USERS);
1344 /* If we get to this point, there are no more unvalidated users.
1345 * Therefore we clear the "users need validation" flag.
1348 begin_critical_section(S_CONTROL);
1350 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1352 end_critical_section(S_CONTROL);
1353 cprintf("%d *** End of registration.\n", CIT_OK);
1362 void cmd_vali(char *v_args)
1366 struct ctdluser userbuf;
1368 extract_token(user, v_args, 0, '|', sizeof user);
1369 newax = extract_int(v_args, 1);
1371 if (CtdlAccessCheck(ac_aide)) {
1375 if (lgetuser(&userbuf, user) != 0) {
1376 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1380 userbuf.axlevel = newax;
1381 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1385 /* If the access level was set to zero, delete the user */
1387 if (purge_user(user) == 0) {
1388 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1392 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1398 * Traverse the user file...
1400 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1403 struct ctdluser usbuf;
1404 struct cdbdata *cdbus;
1406 cdb_rewind(CDB_USERS);
1408 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1409 memset(&usbuf, 0, sizeof(struct ctdluser));
1410 memcpy(&usbuf, cdbus->ptr,
1411 ((cdbus->len > sizeof(struct ctdluser)) ?
1412 sizeof(struct ctdluser) : cdbus->len));
1414 (*CallBack) (&usbuf, in_data);
1420 * List one user (this works with cmd_list)
1422 void ListThisUser(struct ctdluser *usbuf, void *data)
1426 searchstring = (char *)data;
1427 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1431 if (usbuf->axlevel > 0) {
1432 if ((CC->user.axlevel >= 6)
1433 || ((usbuf->flags & US_UNLISTED) == 0)
1434 || ((CC->internal_pgm))) {
1435 cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1439 (long)usbuf->lastcall,
1442 if (CC->user.axlevel >= 6)
1443 cprintf("%s", usbuf->password);
1450 * List users (searchstring may be empty to list all users)
1452 void cmd_list(char *cmdbuf)
1454 char searchstring[256];
1455 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1456 striplt(searchstring);
1457 cprintf("%d \n", LISTING_FOLLOWS);
1458 ForEachUser(ListThisUser, (void *)searchstring );
1466 * assorted info we need to check at login
1474 if (CtdlAccessCheck(ac_logged_in)) {
1478 getuser(&CC->user, CC->curr_user); /* no lock is needed here */
1479 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1482 if (CC->user.axlevel >= 6) {
1484 if (CitControl.MMflags & MM_VALID)
1488 /* check for mail */
1489 mail = InitialMailCheck();
1491 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1496 * check to see if a user exists
1498 void cmd_qusr(char *who)
1500 struct ctdluser usbuf;
1502 if (getuser(&usbuf, who) == 0) {
1503 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1505 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1511 * Administrative Get User Parameters
1513 void cmd_agup(char *cmdbuf)
1515 struct ctdluser usbuf;
1516 char requested_user[128];
1518 if (CtdlAccessCheck(ac_aide)) {
1522 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1523 if (getuser(&usbuf, requested_user) != 0) {
1524 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1527 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1534 (int) usbuf.axlevel,
1536 (long)usbuf.lastcall,
1543 * Administrative Set User Parameters
1545 void cmd_asup(char *cmdbuf)
1547 struct ctdluser usbuf;
1548 char requested_user[128];
1554 if (CtdlAccessCheck(ac_aide))
1557 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1558 if (lgetuser(&usbuf, requested_user) != 0) {
1559 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1562 np = num_parms(cmdbuf);
1564 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1566 usbuf.flags = extract_int(cmdbuf, 2);
1568 usbuf.timescalled = extract_int(cmdbuf, 3);
1570 usbuf.posted = extract_int(cmdbuf, 4);
1572 newax = extract_int(cmdbuf, 5);
1573 if ((newax >= 0) && (newax <= 6)) {
1574 usbuf.axlevel = extract_int(cmdbuf, 5);
1578 usbuf.lastcall = extract_long(cmdbuf, 7);
1581 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1584 if (usbuf.axlevel == 0) {
1585 if (purge_user(requested_user) == 0) {
1591 sprintf(notify, "User \"%s\" has been deleted by %s.\n",
1592 usbuf.fullname, CC->user.fullname);
1593 aide_message(notify, "User Deletion Message");
1596 cprintf("%d Ok", CIT_OK);
1598 cprintf(" (%s deleted)", requested_user);
1605 * Check to see if the user who we just sent mail to is logged in. If yes,
1606 * bump the 'new mail' counter for their session. That enables them to
1607 * receive a new mail notification without having to hit the database.
1609 void BumpNewMailCounter(long which_user) {
1610 struct CitContext *ptr;
1612 begin_critical_section(S_SESSION_TABLE);
1614 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1615 if (ptr->user.usernum == which_user) {
1620 end_critical_section(S_SESSION_TABLE);
1625 * Count the number of new mail messages the user has
1629 int num_newmsgs = 0;
1631 num_newmsgs = CC->newmail;
1634 return (num_newmsgs);
1639 * Count the number of new mail messages the user has
1641 int InitialMailCheck()
1643 int num_newmsgs = 0;
1645 char mailboxname[ROOMNAMELEN];
1646 struct ctdlroom mailbox;
1648 struct cdbdata *cdbfr;
1649 long *msglist = NULL;
1652 MailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1653 if (getroom(&mailbox, mailboxname) != 0)
1655 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1657 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1659 if (cdbfr != NULL) {
1660 msglist = malloc(cdbfr->len);
1661 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1662 num_msgs = cdbfr->len / sizeof(long);
1666 for (a = 0; a < num_msgs; ++a) {
1667 if (msglist[a] > 0L) {
1668 if (msglist[a] > vbuf.v_lastseen) {
1673 if (msglist != NULL)
1676 return (num_newmsgs);
1682 * Set the preferred view for the current user/room combination
1684 void cmd_view(char *cmdbuf) {
1688 if (CtdlAccessCheck(ac_logged_in)) {
1692 requested_view = extract_int(cmdbuf, 0);
1694 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1695 vbuf.v_view = requested_view;
1696 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1698 cprintf("%d ok\n", CIT_OK);