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"
51 /* These pipes are used to talk to the chkpwd daemon, which is forked during startup */
52 int chkpwd_write_pipe[2];
53 int chkpwd_read_pipe[2];
56 * makeuserkey() - convert a username into the format used as a database key
57 * (it's just the username converted into lower case)
59 static INLINE void makeuserkey(char *key, char *username) {
62 len = strlen(username);
63 for (i=0; i<=len; ++i) {
64 key[i] = tolower(username[i]);
70 * getuser() - retrieve named user into supplied buffer.
71 * returns 0 on success
73 int getuser(struct ctdluser *usbuf, char name[])
76 char usernamekey[USERNAME_SIZE];
77 struct cdbdata *cdbus;
80 memset(usbuf, 0, sizeof(struct ctdluser));
83 makeuserkey(usernamekey, name);
84 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
86 if (cdbus == NULL) { /* user not found */
90 memcpy(usbuf, cdbus->ptr,
91 ((cdbus->len > sizeof(struct ctdluser)) ?
92 sizeof(struct ctdluser) : cdbus->len));
101 * lgetuser() - same as getuser() but locks the record
103 int lgetuser(struct ctdluser *usbuf, char *name)
107 retcode = getuser(usbuf, name);
109 begin_critical_section(S_USERS);
116 * putuser() - write user buffer into the correct place on disk
118 void putuser(struct ctdluser *usbuf)
120 char usernamekey[USERNAME_SIZE];
122 makeuserkey(usernamekey, usbuf->fullname);
124 usbuf->version = REV_LEVEL;
126 usernamekey, strlen(usernamekey),
127 usbuf, sizeof(struct ctdluser));
133 * lputuser() - same as putuser() but locks the record
135 void lputuser(struct ctdluser *usbuf)
138 end_critical_section(S_USERS);
142 * Index-generating function used by Ctdl[Get|Set]Relationship
144 int GenerateRelationshipIndex(char *IndexBuf,
156 TheIndex.iRoomID = RoomID;
157 TheIndex.iRoomGen = RoomGen;
158 TheIndex.iUserID = UserID;
160 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
161 return (sizeof(TheIndex));
167 * Back end for CtdlSetRelationship()
169 void put_visit(struct visit *newvisit)
174 /* Generate an index */
175 IndexLen = GenerateRelationshipIndex(IndexBuf,
178 newvisit->v_usernum);
180 /* Store the record */
181 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
182 newvisit, sizeof(struct visit)
190 * Define a relationship between a user and a room
192 void CtdlSetRelationship(struct visit *newvisit,
193 struct ctdluser *rel_user,
194 struct ctdlroom *rel_room)
198 /* We don't use these in Citadel because they're implicit by the
199 * index, but they must be present if the database is exported.
201 newvisit->v_roomnum = rel_room->QRnumber;
202 newvisit->v_roomgen = rel_room->QRgen;
203 newvisit->v_usernum = rel_user->usernum;
209 * Locate a relationship between a user and a room
211 void CtdlGetRelationship(struct visit *vbuf,
212 struct ctdluser *rel_user,
213 struct ctdlroom *rel_room)
218 struct cdbdata *cdbvisit;
220 /* Generate an index */
221 IndexLen = GenerateRelationshipIndex(IndexBuf,
226 /* Clear out the buffer */
227 memset(vbuf, 0, sizeof(struct visit));
229 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
230 if (cdbvisit != NULL) {
231 memcpy(vbuf, cdbvisit->ptr,
232 ((cdbvisit->len > sizeof(struct visit)) ?
233 sizeof(struct visit) : cdbvisit->len));
237 /* If this is the first time the user has seen this room,
238 * set the view to be the default for the room.
240 vbuf->v_view = rel_room->QRdefaultview;
243 /* Set v_seen if necessary */
244 if (vbuf->v_seen[0] == 0) {
245 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
250 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
252 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
257 * Is the user currently logged in an Aide?
261 if (CC->user.axlevel >= 6)
269 * Is the user currently logged in an Aide *or* the room aide for this room?
271 int is_room_aide(void)
274 if (!CC->logged_in) {
278 if ((CC->user.axlevel >= 6)
279 || (CC->room.QRroomaide == CC->user.usernum)) {
287 * getuserbynumber() - get user by number
288 * returns 0 if user was found
290 * WARNING: don't use this function unless you absolutely have to. It does
291 * a sequential search and therefore is computationally expensive.
293 int getuserbynumber(struct ctdluser *usbuf, long int number)
295 struct cdbdata *cdbus;
297 cdb_rewind(CDB_USERS);
299 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
300 memset(usbuf, 0, sizeof(struct ctdluser));
301 memcpy(usbuf, cdbus->ptr,
302 ((cdbus->len > sizeof(struct ctdluser)) ?
303 sizeof(struct ctdluser) : cdbus->len));
305 if (usbuf->usernum == number) {
306 cdb_close_cursor(CDB_USERS);
315 * getuserbyuid() - get user by system uid (for PAM mode authentication)
316 * returns 0 if user was found
318 * WARNING: don't use this function unless you absolutely have to. It does
319 * a sequential search and therefore is computationally expensive.
321 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
323 struct cdbdata *cdbus;
325 cdb_rewind(CDB_USERS);
327 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
328 memset(usbuf, 0, sizeof(struct ctdluser));
329 memcpy(usbuf, cdbus->ptr,
330 ((cdbus->len > sizeof(struct ctdluser)) ?
331 sizeof(struct ctdluser) : cdbus->len));
333 if (usbuf->uid == number) {
334 cdb_close_cursor(CDB_USERS);
344 * Back end for cmd_user() and its ilk
346 int CtdlLoginExistingUser(char *trythisname)
351 if ((CC->logged_in)) {
352 return login_already_logged_in;
355 if (trythisname == NULL) return login_not_found;
356 safestrncpy(username, trythisname, USERNAME_SIZE);
359 if (strlen(username) == 0) {
360 return login_not_found;
363 if (config.c_auth_mode == 1) {
368 struct passwd *tempPwdPtr;
371 lprintf(CTDL_DEBUG, "asking host about <%s>\n", username);
372 #ifdef SOLARIS_GETPWUID
373 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
375 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
377 if (tempPwdPtr == NULL) {
378 return login_not_found;
380 lprintf(CTDL_DEBUG, "found it! uid=%ld, gecos=%s\n", (long)pd.pw_uid, pd.pw_gecos);
382 /* Locate the associated Citadel account.
383 * If not found, make one attempt to create it.
385 found_user = getuserbyuid(&CC->user, pd.pw_uid);
386 if (found_user != 0) {
387 create_user(username, 0);
388 found_user = getuserbyuid(&CC->user, pd.pw_uid);
394 /* native auth mode */
396 struct recptypes *valid = NULL;
398 /* First, try to log in as if the supplied name is a display name */
399 found_user = getuser(&CC->user, username);
401 /* If that didn't work, try to log in as if the supplied name
402 * is an e-mail address
404 if (found_user != 0) {
405 valid = validate_recipients(username);
407 if (valid->num_local == 1) {
408 found_user = getuser(&CC->user, valid->recp_local);
410 free_recipients(valid);
415 /* Did we find something? */
416 if (found_user == 0) {
417 if (((CC->nologin)) && (CC->user.axlevel < 6)) {
418 return login_too_many_users;
420 safestrncpy(CC->curr_user, CC->user.fullname,
421 sizeof CC->curr_user);
425 return login_not_found;
433 void cmd_user(char *cmdbuf)
438 extract_token(username, cmdbuf, 0, '|', sizeof username);
441 a = CtdlLoginExistingUser(username);
443 case login_already_logged_in:
444 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
446 case login_too_many_users:
448 "Too many users are already online "
450 ERROR + MAX_SESSIONS_EXCEEDED,
451 config.c_nodename, config.c_maxsessions);
454 cprintf("%d Password required for %s\n",
455 MORE_DATA, CC->curr_user);
457 case login_not_found:
458 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
461 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
468 * session startup code which is common to both cmd_pass() and cmd_newu()
470 void session_startup(void)
474 lprintf(CTDL_NOTICE, "<%s> logged in\n", CC->curr_user);
476 lgetuser(&CC->user, CC->curr_user);
477 ++(CC->user.timescalled);
478 CC->previous_login = CC->user.lastcall;
479 time(&CC->user.lastcall);
481 /* If this user's name is the name of the system administrator
482 * (as specified in setup), automatically assign access level 6.
484 if (!strcasecmp(CC->user.fullname, config.c_sysadm)) {
485 CC->user.axlevel = 6;
488 /* If we're authenticating off the host system, automatically give
489 * root the highest level of access.
491 if (config.c_auth_mode == 1) {
492 if (CC->user.uid == 0) {
493 CC->user.axlevel = 6;
500 * Populate CC->cs_inet_email with a default address. This will be
501 * overwritten with the user's directory address, if one exists, when
502 * the vCard module's login hook runs.
504 snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
505 CC->user.fullname, config.c_fqdn);
506 for (i=0; i<strlen(CC->cs_inet_email); ++i) {
507 if (isspace(CC->cs_inet_email[i])) {
508 CC->cs_inet_email[i] = '_';
512 /* Create any personal rooms required by the system.
513 * (Technically, MAILROOM should be there already, but just in case...)
515 create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
516 create_room(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
517 create_room(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
519 /* Run any startup routines registered by loadable modules */
520 PerformSessionHooks(EVT_LOGIN);
522 /* Enter the lobby */
523 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
527 void logged_in_response(void)
529 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
530 CIT_OK, CC->user.fullname, CC->user.axlevel,
531 CC->user.timescalled, CC->user.posted,
532 CC->user.flags, CC->user.usernum,
539 * misc things to be taken care of when a user is logged out
541 void logout(struct CitContext *who)
544 * Clear out some session data. Most likely, the CitContext for this
545 * session is about to get nuked when the session disconnects, but
546 * since it's possible to log in again without reconnecting, we cannot
547 * make that assumption.
549 strcpy(who->fake_username, "");
550 strcpy(who->fake_hostname, "");
551 strcpy(who->fake_roomname, "");
555 * If there is a download in progress, abort it.
557 if (who->download_fp != NULL) {
558 fclose(who->download_fp);
559 who->download_fp = NULL;
563 * If there is an upload in progress, abort it.
565 if (who->upload_fp != NULL) {
570 * If we were talking to a network node, we're not anymore...
572 if (strlen(who->net_node) > 0) {
573 network_talking_to(who->net_node, NTT_REMOVE);
576 /* Do modular stuff... */
577 PerformSessionHooks(EVT_LOGOUT);
579 /* Free any output buffers */
580 if (who->output_buffer != NULL) {
586 * Validate a password on the host unix system by talking to the chkpwd daemon
588 static int validpw(uid_t uid, const char *pass)
592 lprintf(CTDL_DEBUG, "Validating password for uid=%d using chkpwd...\n", uid);
594 begin_critical_section(S_CHKPWD);
595 snprintf(buf, sizeof buf, "%016d", uid);
596 write(chkpwd_write_pipe[1], buf, 16);
597 write(chkpwd_write_pipe[1], pass, 256);
598 read(chkpwd_read_pipe[0], buf, 4);
599 end_critical_section(S_CHKPWD);
601 if (!strncmp(buf, "PASS", 4)) {
602 lprintf(CTDL_DEBUG, "...pass\n");
606 lprintf(CTDL_DEBUG, "...fail\n");
611 * Start up the chkpwd daemon so validpw() has something to talk to
613 void start_chkpwd_daemon(void) {
617 lprintf(CTDL_DEBUG, "Starting chkpwd daemon for host authentication mode\n");
619 if (pipe(chkpwd_write_pipe) != 0) {
620 lprintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
623 if (pipe(chkpwd_read_pipe) != 0) {
624 lprintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
629 if (chkpwd_pid < 0) {
630 lprintf(CTDL_EMERG, "Unable to fork chkpwd daemon: %s\n", strerror(errno));
633 if (chkpwd_pid == 0) {
634 lprintf(CTDL_DEBUG, "Now calling dup2() write\n");
635 dup2(chkpwd_write_pipe[0], 0);
636 lprintf(CTDL_DEBUG, "Now calling dup2() write\n");
637 dup2(chkpwd_read_pipe[1], 1);
638 lprintf(CTDL_DEBUG, "Now closing stuff\n");
639 for (i=2; i<256; ++i) close(i);
640 lprintf(CTDL_DEBUG, "Now calling execl(%s)\n", file_chkpwd);
641 execl(file_chkpwd, file_chkpwd, NULL);
642 lprintf(CTDL_EMERG, "Unable to exec chkpwd daemon: %s\n", strerror(errno));
656 int CtdlTryPassword(char *password)
660 if ((CC->logged_in)) {
661 lprintf(CTDL_WARNING, "CtdlTryPassword: already logged in\n");
662 return pass_already_logged_in;
664 if (!strcmp(CC->curr_user, NLI)) {
665 lprintf(CTDL_WARNING, "CtdlTryPassword: no user selected\n");
668 if (getuser(&CC->user, CC->curr_user)) {
669 lprintf(CTDL_ERR, "CtdlTryPassword: internal error\n");
670 return pass_internal_error;
672 if (password == NULL) {
673 lprintf(CTDL_INFO, "CtdlTryPassword: NULL password string supplied\n");
674 return pass_wrong_password;
678 if (config.c_auth_mode == 1) {
682 if (validpw(CC->user.uid, password)) {
686 * sooper-seekrit hack: populate the password field in the
687 * citadel database with the password that the user typed,
688 * if it's correct. This allows most sites to convert from
689 * host auth to native auth if they want to. If you think
690 * this is a security hazard, comment it out.
693 lgetuser(&CC->user, CC->curr_user);
694 safestrncpy(CC->user.password, password, sizeof CC->user.password);
698 * (sooper-seekrit hack ends here)
709 /* native auth mode */
712 strproc(CC->user.password);
713 code = strcasecmp(CC->user.password, password);
715 strproc(CC->user.password);
716 code = strcasecmp(CC->user.password, password);
723 lprintf(CTDL_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
724 return pass_wrong_password;
729 void cmd_pass(char *buf)
734 extract_token(password, buf, 0, '|', sizeof password);
735 a = CtdlTryPassword(password);
738 case pass_already_logged_in:
739 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
742 cprintf("%d You must send a name with USER first.\n",
743 ERROR + USERNAME_REQUIRED);
745 case pass_wrong_password:
746 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
749 logged_in_response();
757 * Delete a user record *and* all of its related resources.
759 int purge_user(char pname[])
762 struct ctdluser usbuf;
763 char usernamekey[USERNAME_SIZE];
764 struct CitContext *ccptr;
765 int user_is_logged_in = 0;
767 makeuserkey(usernamekey, pname);
769 if (getuser(&usbuf, pname) != 0) {
770 lprintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
771 return (ERROR + NO_SUCH_USER);
773 /* Don't delete a user who is currently logged in. Instead, just
774 * set the access level to 0, and let the account get swept up
775 * during the next purge.
777 user_is_logged_in = 0;
778 begin_critical_section(S_SESSION_TABLE);
779 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
780 if (ccptr->user.usernum == usbuf.usernum) {
781 user_is_logged_in = 1;
784 end_critical_section(S_SESSION_TABLE);
785 if (user_is_logged_in == 1) {
786 lprintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
791 lprintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
793 /* Perform any purge functions registered by server extensions */
794 PerformUserHooks(&usbuf, EVT_PURGEUSER);
796 /* delete any existing user/room relationships */
797 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
799 /* delete the userlog entry */
800 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
802 /* remove the user's bio file */
810 /* remove the user's picture */
823 * create_user() - back end processing to create a new user
825 * Set 'newusername' to the desired account name.
826 * Set 'become_user' to nonzero if this is self-service account creation and we want
827 * to actually log in as the user we just created, otherwise set it to 0.
829 int create_user(char *newusername, int become_user)
831 struct ctdluser usbuf;
832 struct ctdlroom qrbuf;
834 char mailboxname[ROOMNAMELEN];
838 safestrncpy(username, newusername, sizeof username);
841 if (config.c_auth_mode == 1) {
846 struct passwd *tempPwdPtr;
849 #ifdef SOLARIS_GETPWUID
850 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof(pwdbuffer));
852 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
854 if (tempPwdPtr != NULL) {
855 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
859 return (ERROR + NO_SUCH_USER);
863 if (!getuser(&usbuf, username)) {
864 return (ERROR + ALREADY_EXISTS);
867 /* Go ahead and initialize a new user record */
868 memset(&usbuf, 0, sizeof(struct ctdluser));
869 safestrncpy(usbuf.fullname, username, sizeof usbuf.fullname);
870 strcpy(usbuf.password, "");
873 /* These are the default flags on new accounts */
874 usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
876 usbuf.timescalled = 0;
878 usbuf.axlevel = config.c_initax;
879 usbuf.USscreenwidth = 80;
880 usbuf.USscreenheight = 24;
881 usbuf.lastcall = time(NULL);
883 /* fetch a new user number */
884 usbuf.usernum = get_new_user_number();
886 /* The very first user created on the system will always be an Aide */
887 if (usbuf.usernum == 1L) {
891 /* add user to userlog */
895 * Give the user a private mailbox and a configuration room.
896 * Make the latter an invisible system room.
898 MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
899 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
901 MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
902 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
903 if (lgetroom(&qrbuf, mailboxname) == 0) {
904 qrbuf.QRflags2 |= QR2_SYSTEM;
908 /* Perform any create functions registered by server extensions */
909 PerformUserHooks(&usbuf, EVT_NEWUSER);
911 /* Everything below this line can be bypassed if administratively
912 * creating a user, instead of doing self-service account creation
916 /* Now become the user we just created */
917 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
918 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
921 /* Check to make sure we're still who we think we are */
922 if (getuser(&CC->user, CC->curr_user)) {
923 return (ERROR + INTERNAL_ERROR);
928 "New user account <%s> has been created, from host %s [%s].\n",
933 aide_message(buf, "User Creation Notice");
934 lprintf(CTDL_NOTICE, "New user <%s> created\n", username);
942 * cmd_newu() - create a new user account and log in as that user
944 void cmd_newu(char *cmdbuf)
949 if (config.c_auth_mode == 1) {
950 cprintf("%d This system does not use native mode authentication.\n",
955 if (config.c_disable_newu) {
956 cprintf("%d Self-service user account creation "
957 "is disabled on this system.\n", ERROR + NOT_HERE);
962 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
966 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
967 ERROR + MAX_SESSIONS_EXCEEDED,
968 config.c_nodename, config.c_maxsessions);
970 extract_token(username, cmdbuf, 0, '|', sizeof username);
974 if (strlen(username) == 0) {
975 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
979 if ((!strcasecmp(username, "bbs")) ||
980 (!strcasecmp(username, "new")) ||
981 (!strcasecmp(username, "."))) {
982 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
986 a = create_user(username, 1);
990 logged_in_response();
991 } else if (a == ERROR + ALREADY_EXISTS) {
992 cprintf("%d '%s' already exists.\n",
993 ERROR + ALREADY_EXISTS, username);
995 } else if (a == ERROR + INTERNAL_ERROR) {
996 cprintf("%d Internal error - user record disappeared?\n",
997 ERROR + INTERNAL_ERROR);
1000 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
1009 void cmd_setp(char *new_pw)
1011 if (CtdlAccessCheck(ac_logged_in)) {
1014 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
1015 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
1019 if (strlen(new_pw) == 0) {
1020 cprintf("%d Password unchanged.\n", CIT_OK);
1023 lgetuser(&CC->user, CC->curr_user);
1024 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1025 lputuser(&CC->user);
1026 cprintf("%d Password changed.\n", CIT_OK);
1027 lprintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
1028 PerformSessionHooks(EVT_SETPASS);
1033 * cmd_creu() - administratively create a new user account (do not log in to it)
1035 void cmd_creu(char *cmdbuf)
1040 struct ctdluser tmp;
1042 if (CtdlAccessCheck(ac_aide)) {
1046 extract_token(username, cmdbuf, 0, '|', sizeof username);
1047 extract_token(password, cmdbuf, 1, '|', sizeof password);
1053 if (strlen(username) == 0) {
1054 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1058 a = create_user(username, 0);
1061 if (strlen(password) > 0) {
1062 lgetuser(&tmp, username);
1063 safestrncpy(tmp.password, password, sizeof(tmp.password));
1066 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1067 (strlen(password) > 0) ? "and password set" :
1068 "with no password");
1070 } else if (a == ERROR + ALREADY_EXISTS) {
1071 cprintf("%d '%s' already exists.\n",
1072 ERROR + ALREADY_EXISTS, username);
1075 cprintf("%d An error occured creating the user account.\n", ERROR + INTERNAL_ERROR);
1082 * get user parameters
1087 if (CtdlAccessCheck(ac_logged_in))
1090 getuser(&CC->user, CC->curr_user);
1091 cprintf("%d %d|%d|%d|\n",
1093 CC->user.USscreenwidth,
1094 CC->user.USscreenheight,
1095 (CC->user.flags & US_USER_SET)
1100 * set user parameters
1102 void cmd_setu(char *new_parms)
1104 if (CtdlAccessCheck(ac_logged_in))
1107 if (num_parms(new_parms) < 3) {
1108 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1111 lgetuser(&CC->user, CC->curr_user);
1112 CC->user.USscreenwidth = extract_int(new_parms, 0);
1113 CC->user.USscreenheight = extract_int(new_parms, 1);
1114 CC->user.flags = CC->user.flags & (~US_USER_SET);
1115 CC->user.flags = CC->user.flags |
1116 (extract_int(new_parms, 2) & US_USER_SET);
1118 lputuser(&CC->user);
1119 cprintf("%d Ok\n", CIT_OK);
1123 * set last read pointer
1125 void cmd_slrp(char *new_ptr)
1129 struct visit original_vbuf;
1131 if (CtdlAccessCheck(ac_logged_in)) {
1135 if (!strncasecmp(new_ptr, "highest", 7)) {
1136 newlr = CC->room.QRhighest;
1138 newlr = atol(new_ptr);
1141 lgetuser(&CC->user, CC->curr_user);
1143 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1144 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1145 vbuf.v_lastseen = newlr;
1146 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1148 /* Only rewrite the record if it changed */
1149 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1150 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1151 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1154 lputuser(&CC->user);
1155 cprintf("%d %ld\n", CIT_OK, newlr);
1159 void cmd_seen(char *argbuf) {
1160 long target_msgnum = 0L;
1161 int target_setting = 0;
1163 if (CtdlAccessCheck(ac_logged_in)) {
1167 if (num_parms(argbuf) != 2) {
1168 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1172 target_msgnum = extract_long(argbuf, 0);
1173 target_setting = extract_int(argbuf, 1);
1175 CtdlSetSeen(&target_msgnum, 1, target_setting,
1176 ctdlsetseen_seen, NULL, NULL);
1177 cprintf("%d OK\n", CIT_OK);
1181 void cmd_gtsn(char *argbuf) {
1184 if (CtdlAccessCheck(ac_logged_in)) {
1188 CtdlGetSeen(buf, ctdlsetseen_seen);
1189 cprintf("%d %s\n", CIT_OK, buf);
1194 * API function for cmd_invt_kick() and anything else that needs to
1195 * invite or kick out a user to/from a room.
1197 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1199 int CtdlInvtKick(char *iuser, int op) {
1200 struct ctdluser USscratch;
1204 if (getuser(&USscratch, iuser) != 0) {
1208 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1210 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1211 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1214 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1215 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1217 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1219 /* post a message in Aide> saying what we just did */
1220 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1222 ((op == 1) ? "invited to" : "kicked out of"),
1225 aide_message(bbb,"User Admin Message");
1232 * INVT and KICK commands
1234 void cmd_invt_kick(char *iuser, int op) {
1237 * These commands are only allowed by aides, room aides,
1238 * and room namespace owners
1241 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1242 /* access granted */
1245 cprintf("%d Higher access or room ownership required.\n",
1246 ERROR + HIGHER_ACCESS_REQUIRED);
1250 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1252 cprintf("%d Can't add/remove users from this room.\n",
1257 if (CtdlInvtKick(iuser, op) != 0) {
1258 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1262 cprintf("%d %s %s %s.\n",
1264 ((op == 1) ? "invited to" : "kicked out of"),
1271 * Forget (Zap) the current room (API call)
1272 * Returns 0 on success
1274 int CtdlForgetThisRoom(void) {
1277 /* On some systems, Aides are not allowed to forget rooms */
1278 if (is_aide() && (config.c_aide_zap == 0)
1279 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1283 lgetuser(&CC->user, CC->curr_user);
1284 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1286 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1287 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1289 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1290 lputuser(&CC->user);
1292 /* Return to the Lobby, so we don't end up in an undefined room */
1293 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1300 * forget (Zap) the current room
1305 if (CtdlAccessCheck(ac_logged_in)) {
1309 if (CtdlForgetThisRoom() == 0) {
1310 cprintf("%d Ok\n", CIT_OK);
1313 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1318 * Get Next Unregistered User
1322 struct cdbdata *cdbus;
1323 struct ctdluser usbuf;
1325 if (CtdlAccessCheck(ac_aide)) {
1329 if ((CitControl.MMflags & MM_VALID) == 0) {
1330 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1334 /* There are unvalidated users. Traverse the user database,
1335 * and return the first user we find that needs validation.
1337 cdb_rewind(CDB_USERS);
1338 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1339 memset(&usbuf, 0, sizeof(struct ctdluser));
1340 memcpy(&usbuf, cdbus->ptr,
1341 ((cdbus->len > sizeof(struct ctdluser)) ?
1342 sizeof(struct ctdluser) : cdbus->len));
1344 if ((usbuf.flags & US_NEEDVALID)
1345 && (usbuf.axlevel > 0)) {
1346 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1347 cdb_close_cursor(CDB_USERS);
1352 /* If we get to this point, there are no more unvalidated users.
1353 * Therefore we clear the "users need validation" flag.
1356 begin_critical_section(S_CONTROL);
1358 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1360 end_critical_section(S_CONTROL);
1361 cprintf("%d *** End of registration.\n", CIT_OK);
1370 void cmd_vali(char *v_args)
1374 struct ctdluser userbuf;
1376 extract_token(user, v_args, 0, '|', sizeof user);
1377 newax = extract_int(v_args, 1);
1379 if (CtdlAccessCheck(ac_aide)) {
1383 if (lgetuser(&userbuf, user) != 0) {
1384 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1388 userbuf.axlevel = newax;
1389 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1393 /* If the access level was set to zero, delete the user */
1395 if (purge_user(user) == 0) {
1396 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1400 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1406 * Traverse the user file...
1408 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1411 struct ctdluser usbuf;
1412 struct cdbdata *cdbus;
1414 cdb_rewind(CDB_USERS);
1416 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1417 memset(&usbuf, 0, sizeof(struct ctdluser));
1418 memcpy(&usbuf, cdbus->ptr,
1419 ((cdbus->len > sizeof(struct ctdluser)) ?
1420 sizeof(struct ctdluser) : cdbus->len));
1422 (*CallBack) (&usbuf, in_data);
1428 * List one user (this works with cmd_list)
1430 void ListThisUser(struct ctdluser *usbuf, void *data)
1434 searchstring = (char *)data;
1435 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1439 if (usbuf->axlevel > 0) {
1440 if ((CC->user.axlevel >= 6)
1441 || ((usbuf->flags & US_UNLISTED) == 0)
1442 || ((CC->internal_pgm))) {
1443 cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1447 (long)usbuf->lastcall,
1450 if (CC->user.axlevel >= 6)
1451 cprintf("%s", usbuf->password);
1458 * List users (searchstring may be empty to list all users)
1460 void cmd_list(char *cmdbuf)
1462 char searchstring[256];
1463 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1464 striplt(searchstring);
1465 cprintf("%d \n", LISTING_FOLLOWS);
1466 ForEachUser(ListThisUser, (void *)searchstring );
1474 * assorted info we need to check at login
1482 if (CtdlAccessCheck(ac_logged_in)) {
1486 getuser(&CC->user, CC->curr_user); /* no lock is needed here */
1487 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1490 if (CC->user.axlevel >= 6) {
1492 if (CitControl.MMflags & MM_VALID)
1496 /* check for mail */
1497 mail = InitialMailCheck();
1499 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1504 * check to see if a user exists
1506 void cmd_qusr(char *who)
1508 struct ctdluser usbuf;
1510 if (getuser(&usbuf, who) == 0) {
1511 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1513 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1519 * Administrative Get User Parameters
1521 void cmd_agup(char *cmdbuf)
1523 struct ctdluser usbuf;
1524 char requested_user[128];
1526 if (CtdlAccessCheck(ac_aide)) {
1530 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1531 if (getuser(&usbuf, requested_user) != 0) {
1532 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1535 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1542 (int) usbuf.axlevel,
1544 (long)usbuf.lastcall,
1551 * Administrative Set User Parameters
1553 void cmd_asup(char *cmdbuf)
1555 struct ctdluser usbuf;
1556 char requested_user[128];
1562 if (CtdlAccessCheck(ac_aide))
1565 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1566 if (lgetuser(&usbuf, requested_user) != 0) {
1567 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1570 np = num_parms(cmdbuf);
1572 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1574 usbuf.flags = extract_int(cmdbuf, 2);
1576 usbuf.timescalled = extract_int(cmdbuf, 3);
1578 usbuf.posted = extract_int(cmdbuf, 4);
1580 newax = extract_int(cmdbuf, 5);
1581 if ((newax >= 0) && (newax <= 6)) {
1582 usbuf.axlevel = extract_int(cmdbuf, 5);
1586 usbuf.lastcall = extract_long(cmdbuf, 7);
1589 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1592 if (usbuf.axlevel == 0) {
1593 if (purge_user(requested_user) == 0) {
1599 sprintf(notify, "User \"%s\" has been deleted by %s.\n",
1600 usbuf.fullname, CC->user.fullname);
1601 aide_message(notify, "User Deletion Message");
1604 cprintf("%d Ok", CIT_OK);
1606 cprintf(" (%s deleted)", requested_user);
1613 * Check to see if the user who we just sent mail to is logged in. If yes,
1614 * bump the 'new mail' counter for their session. That enables them to
1615 * receive a new mail notification without having to hit the database.
1617 void BumpNewMailCounter(long which_user) {
1618 struct CitContext *ptr;
1620 begin_critical_section(S_SESSION_TABLE);
1622 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1623 if (ptr->user.usernum == which_user) {
1628 end_critical_section(S_SESSION_TABLE);
1633 * Count the number of new mail messages the user has
1637 int num_newmsgs = 0;
1639 num_newmsgs = CC->newmail;
1642 return (num_newmsgs);
1647 * Count the number of new mail messages the user has
1649 int InitialMailCheck()
1651 int num_newmsgs = 0;
1653 char mailboxname[ROOMNAMELEN];
1654 struct ctdlroom mailbox;
1656 struct cdbdata *cdbfr;
1657 long *msglist = NULL;
1660 MailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1661 if (getroom(&mailbox, mailboxname) != 0)
1663 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1665 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1667 if (cdbfr != NULL) {
1668 msglist = malloc(cdbfr->len);
1669 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1670 num_msgs = cdbfr->len / sizeof(long);
1674 for (a = 0; a < num_msgs; ++a) {
1675 if (msglist[a] > 0L) {
1676 if (msglist[a] > vbuf.v_lastseen) {
1681 if (msglist != NULL)
1684 return (num_newmsgs);
1690 * Set the preferred view for the current user/room combination
1692 void cmd_view(char *cmdbuf) {
1696 if (CtdlAccessCheck(ac_logged_in)) {
1700 requested_view = extract_int(cmdbuf, 0);
1702 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1703 vbuf.v_view = requested_view;
1704 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1706 cprintf("%d ok\n", CIT_OK);