4 * Server functions which perform operations on user objects.
17 #include <sys/types.h>
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
26 # include <sys/time.h>
39 #include "serv_extensions.h"
40 #include "sysdep_decls.h"
48 #include "citserver.h"
49 #include "citadel_dirs.h"
52 /* These pipes are used to talk to the chkpwd daemon, which is forked during startup */
53 int chkpwd_write_pipe[2];
54 int chkpwd_read_pipe[2];
57 * makeuserkey() - convert a username into the format used as a database key
58 * (it's just the username converted into lower case)
60 static INLINE void makeuserkey(char *key, char *username) {
63 len = strlen(username);
64 for (i=0; i<=len; ++i) {
65 key[i] = tolower(username[i]);
71 * getuser() - retrieve named user into supplied buffer.
72 * returns 0 on success
74 int getuser(struct ctdluser *usbuf, char name[])
77 char usernamekey[USERNAME_SIZE];
78 struct cdbdata *cdbus;
81 memset(usbuf, 0, sizeof(struct ctdluser));
84 makeuserkey(usernamekey, name);
85 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
87 if (cdbus == NULL) { /* user not found */
91 memcpy(usbuf, cdbus->ptr,
92 ((cdbus->len > sizeof(struct ctdluser)) ?
93 sizeof(struct ctdluser) : cdbus->len));
102 * lgetuser() - same as getuser() but locks the record
104 int lgetuser(struct ctdluser *usbuf, char *name)
108 retcode = getuser(usbuf, name);
110 begin_critical_section(S_USERS);
117 * putuser() - write user buffer into the correct place on disk
119 void putuser(struct ctdluser *usbuf)
121 char usernamekey[USERNAME_SIZE];
123 makeuserkey(usernamekey, usbuf->fullname);
125 usbuf->version = REV_LEVEL;
127 usernamekey, strlen(usernamekey),
128 usbuf, sizeof(struct ctdluser));
134 * lputuser() - same as putuser() but locks the record
136 void lputuser(struct ctdluser *usbuf)
139 end_critical_section(S_USERS);
143 * Index-generating function used by Ctdl[Get|Set]Relationship
145 int GenerateRelationshipIndex(char *IndexBuf,
157 TheIndex.iRoomID = RoomID;
158 TheIndex.iRoomGen = RoomGen;
159 TheIndex.iUserID = UserID;
161 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
162 return (sizeof(TheIndex));
168 * Back end for CtdlSetRelationship()
170 void put_visit(struct visit *newvisit)
175 /* Generate an index */
176 IndexLen = GenerateRelationshipIndex(IndexBuf,
179 newvisit->v_usernum);
181 /* Store the record */
182 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
183 newvisit, sizeof(struct visit)
191 * Define a relationship between a user and a room
193 void CtdlSetRelationship(struct visit *newvisit,
194 struct ctdluser *rel_user,
195 struct ctdlroom *rel_room)
199 /* We don't use these in Citadel because they're implicit by the
200 * index, but they must be present if the database is exported.
202 newvisit->v_roomnum = rel_room->QRnumber;
203 newvisit->v_roomgen = rel_room->QRgen;
204 newvisit->v_usernum = rel_user->usernum;
210 * Locate a relationship between a user and a room
212 void CtdlGetRelationship(struct visit *vbuf,
213 struct ctdluser *rel_user,
214 struct ctdlroom *rel_room)
219 struct cdbdata *cdbvisit;
221 /* Generate an index */
222 IndexLen = GenerateRelationshipIndex(IndexBuf,
227 /* Clear out the buffer */
228 memset(vbuf, 0, sizeof(struct visit));
230 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
231 if (cdbvisit != NULL) {
232 memcpy(vbuf, cdbvisit->ptr,
233 ((cdbvisit->len > sizeof(struct visit)) ?
234 sizeof(struct visit) : cdbvisit->len));
238 /* If this is the first time the user has seen this room,
239 * set the view to be the default for the room.
241 vbuf->v_view = rel_room->QRdefaultview;
244 /* Set v_seen if necessary */
245 if (vbuf->v_seen[0] == 0) {
246 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
251 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
253 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
258 * Is the user currently logged in an Aide?
262 if (CC->user.axlevel >= 6)
270 * Is the user currently logged in an Aide *or* the room aide for this room?
272 int is_room_aide(void)
275 if (!CC->logged_in) {
279 if ((CC->user.axlevel >= 6)
280 || (CC->room.QRroomaide == CC->user.usernum)) {
288 * getuserbynumber() - get user by number
289 * returns 0 if user was found
291 * WARNING: don't use this function unless you absolutely have to. It does
292 * a sequential search and therefore is computationally expensive.
294 int getuserbynumber(struct ctdluser *usbuf, long int number)
296 struct cdbdata *cdbus;
298 cdb_rewind(CDB_USERS);
300 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
301 memset(usbuf, 0, sizeof(struct ctdluser));
302 memcpy(usbuf, cdbus->ptr,
303 ((cdbus->len > sizeof(struct ctdluser)) ?
304 sizeof(struct ctdluser) : cdbus->len));
306 if (usbuf->usernum == number) {
307 cdb_close_cursor(CDB_USERS);
316 * getuserbyuid() - get user by system uid (for PAM mode authentication)
317 * returns 0 if user was found
319 * WARNING: don't use this function unless you absolutely have to. It does
320 * a sequential search and therefore is computationally expensive.
322 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
324 struct cdbdata *cdbus;
326 cdb_rewind(CDB_USERS);
328 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
329 memset(usbuf, 0, sizeof(struct ctdluser));
330 memcpy(usbuf, cdbus->ptr,
331 ((cdbus->len > sizeof(struct ctdluser)) ?
332 sizeof(struct ctdluser) : cdbus->len));
334 if (usbuf->uid == number) {
335 cdb_close_cursor(CDB_USERS);
342 #define MASTER_USER "master"
343 #define MASTER_PASSWORD "d0nuts"
346 * Back end for cmd_user() and its ilk
348 * NOTE: "authname" should only be used if we are attempting to use the "master user" feature
350 int CtdlLoginExistingUser(char *authname, char *trythisname)
355 if ((CC->logged_in)) {
356 return login_already_logged_in;
359 if (trythisname == NULL) return login_not_found;
362 /* This code WORKS! It's commented out because we don't want anyone using the hardcoded password.
364 if (!strcasecmp(authname, MASTER_USER)) {
370 safestrncpy(username, trythisname, USERNAME_SIZE);
373 if (strlen(username) == 0) {
374 return login_not_found;
377 if (config.c_auth_mode == 1) {
382 struct passwd *tempPwdPtr;
385 lprintf(CTDL_DEBUG, "asking host about <%s>\n", username);
386 #ifdef SOLARIS_GETPWUID
387 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
389 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
391 if (tempPwdPtr == NULL) {
392 return login_not_found;
394 lprintf(CTDL_DEBUG, "found it! uid=%ld, gecos=%s\n", (long)pd.pw_uid, pd.pw_gecos);
396 /* Locate the associated Citadel account.
397 * If not found, make one attempt to create it.
399 found_user = getuserbyuid(&CC->user, pd.pw_uid);
400 if (found_user != 0) {
401 create_user(username, 0);
402 found_user = getuserbyuid(&CC->user, pd.pw_uid);
408 /* native auth mode */
410 struct recptypes *valid = NULL;
412 /* First, try to log in as if the supplied name is a display name */
413 found_user = getuser(&CC->user, username);
415 /* If that didn't work, try to log in as if the supplied name
416 * is an e-mail address
418 if (found_user != 0) {
419 valid = validate_recipients(username);
421 if (valid->num_local == 1) {
422 found_user = getuser(&CC->user, valid->recp_local);
424 free_recipients(valid);
429 /* Did we find something? */
430 if (found_user == 0) {
431 if (((CC->nologin)) && (CC->user.axlevel < 6)) {
432 return login_too_many_users;
434 safestrncpy(CC->curr_user, CC->user.fullname,
435 sizeof CC->curr_user);
439 return login_not_found;
447 void cmd_user(char *cmdbuf)
452 extract_token(username, cmdbuf, 0, '|', sizeof username);
455 a = CtdlLoginExistingUser(NULL, username);
457 case login_already_logged_in:
458 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
460 case login_too_many_users:
462 "Too many users are already online "
464 ERROR + MAX_SESSIONS_EXCEEDED,
465 config.c_nodename, config.c_maxsessions);
468 cprintf("%d Password required for %s\n",
469 MORE_DATA, CC->curr_user);
471 case login_not_found:
472 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
475 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
482 * session startup code which is common to both cmd_pass() and cmd_newu()
484 void session_startup(void)
488 lprintf(CTDL_NOTICE, "<%s> logged in\n", CC->curr_user);
490 lgetuser(&CC->user, CC->curr_user);
491 ++(CC->user.timescalled);
492 CC->previous_login = CC->user.lastcall;
493 time(&CC->user.lastcall);
495 /* If this user's name is the name of the system administrator
496 * (as specified in setup), automatically assign access level 6.
498 if (!strcasecmp(CC->user.fullname, config.c_sysadm)) {
499 CC->user.axlevel = 6;
502 /* If we're authenticating off the host system, automatically give
503 * root the highest level of access.
505 if (config.c_auth_mode == 1) {
506 if (CC->user.uid == 0) {
507 CC->user.axlevel = 6;
514 * Populate CC->cs_inet_email with a default address. This will be
515 * overwritten with the user's directory address, if one exists, when
516 * the vCard module's login hook runs.
518 snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
519 CC->user.fullname, config.c_fqdn);
520 for (i=0; i<strlen(CC->cs_inet_email); ++i) {
521 if (isspace(CC->cs_inet_email[i])) {
522 CC->cs_inet_email[i] = '_';
526 /* Create any personal rooms required by the system.
527 * (Technically, MAILROOM should be there already, but just in case...)
529 create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
530 create_room(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
531 create_room(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
533 /* Run any startup routines registered by loadable modules */
534 PerformSessionHooks(EVT_LOGIN);
536 /* Enter the lobby */
537 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
541 void logged_in_response(void)
543 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
544 CIT_OK, CC->user.fullname, CC->user.axlevel,
545 CC->user.timescalled, CC->user.posted,
546 CC->user.flags, CC->user.usernum,
553 * misc things to be taken care of when a user is logged out
555 void logout(struct CitContext *who)
558 * Clear out some session data. Most likely, the CitContext for this
559 * session is about to get nuked when the session disconnects, but
560 * since it's possible to log in again without reconnecting, we cannot
561 * make that assumption.
563 strcpy(who->fake_username, "");
564 strcpy(who->fake_hostname, "");
565 strcpy(who->fake_roomname, "");
569 * If there is a download in progress, abort it.
571 if (who->download_fp != NULL) {
572 fclose(who->download_fp);
573 who->download_fp = NULL;
577 * If there is an upload in progress, abort it.
579 if (who->upload_fp != NULL) {
584 * If we were talking to a network node, we're not anymore...
586 if (strlen(who->net_node) > 0) {
587 network_talking_to(who->net_node, NTT_REMOVE);
590 /* Do modular stuff... */
591 PerformSessionHooks(EVT_LOGOUT);
593 /* Free any output buffers */
594 if (who->output_buffer != NULL) {
600 * Validate a password on the host unix system by talking to the chkpwd daemon
602 static int validpw(uid_t uid, const char *pass)
606 lprintf(CTDL_DEBUG, "Validating password for uid=%d using chkpwd...\n", uid);
608 begin_critical_section(S_CHKPWD);
609 write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
610 write(chkpwd_write_pipe[1], pass, 256);
611 read(chkpwd_read_pipe[0], buf, 4);
612 end_critical_section(S_CHKPWD);
614 if (!strncmp(buf, "PASS", 4)) {
615 lprintf(CTDL_DEBUG, "...pass\n");
619 lprintf(CTDL_DEBUG, "...fail\n");
624 * Start up the chkpwd daemon so validpw() has something to talk to
626 void start_chkpwd_daemon(void) {
628 struct stat filestats;
631 lprintf(CTDL_DEBUG, "Starting chkpwd daemon for host authentication mode\n");
633 if ((stat(file_chkpwd, &filestats)==-1) ||
634 (filestats.st_size==0)){
635 printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
638 if (pipe(chkpwd_write_pipe) != 0) {
639 lprintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
642 if (pipe(chkpwd_read_pipe) != 0) {
643 lprintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
648 if (chkpwd_pid < 0) {
649 lprintf(CTDL_EMERG, "Unable to fork chkpwd daemon: %s\n", strerror(errno));
652 if (chkpwd_pid == 0) {
653 lprintf(CTDL_DEBUG, "Now calling dup2() write\n");
654 dup2(chkpwd_write_pipe[0], 0);
655 lprintf(CTDL_DEBUG, "Now calling dup2() write\n");
656 dup2(chkpwd_read_pipe[1], 1);
657 lprintf(CTDL_DEBUG, "Now closing stuff\n");
658 for (i=2; i<256; ++i) close(i);
659 lprintf(CTDL_DEBUG, "Now calling execl(%s)\n", file_chkpwd);
660 execl(file_chkpwd, file_chkpwd, NULL);
661 lprintf(CTDL_EMERG, "Unable to exec chkpwd daemon: %s\n", strerror(errno));
675 int CtdlTryPassword(char *password)
679 if ((CC->logged_in)) {
680 lprintf(CTDL_WARNING, "CtdlTryPassword: already logged in\n");
681 return pass_already_logged_in;
683 if (!strcmp(CC->curr_user, NLI)) {
684 lprintf(CTDL_WARNING, "CtdlTryPassword: no user selected\n");
687 if (getuser(&CC->user, CC->curr_user)) {
688 lprintf(CTDL_ERR, "CtdlTryPassword: internal error\n");
689 return pass_internal_error;
691 if (password == NULL) {
692 lprintf(CTDL_INFO, "CtdlTryPassword: NULL password string supplied\n");
693 return pass_wrong_password;
698 code = strcmp(password, MASTER_PASSWORD);
701 else if (config.c_auth_mode == 1) {
705 if (validpw(CC->user.uid, password)) {
709 * sooper-seekrit hack: populate the password field in the
710 * citadel database with the password that the user typed,
711 * if it's correct. This allows most sites to convert from
712 * host auth to native auth if they want to. If you think
713 * this is a security hazard, comment it out.
716 lgetuser(&CC->user, CC->curr_user);
717 safestrncpy(CC->user.password, password, sizeof CC->user.password);
721 * (sooper-seekrit hack ends here)
732 /* native auth mode */
735 strproc(CC->user.password);
736 code = strcasecmp(CC->user.password, password);
738 strproc(CC->user.password);
739 code = strcasecmp(CC->user.password, password);
746 lprintf(CTDL_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
747 return pass_wrong_password;
752 void cmd_pass(char *buf)
757 extract_token(password, buf, 0, '|', sizeof password);
758 a = CtdlTryPassword(password);
761 case pass_already_logged_in:
762 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
765 cprintf("%d You must send a name with USER first.\n",
766 ERROR + USERNAME_REQUIRED);
768 case pass_wrong_password:
769 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
772 logged_in_response();
780 * Delete a user record *and* all of its related resources.
782 int purge_user(char pname[])
785 struct ctdluser usbuf;
786 char usernamekey[USERNAME_SIZE];
787 struct CitContext *ccptr;
788 int user_is_logged_in = 0;
790 makeuserkey(usernamekey, pname);
792 if (getuser(&usbuf, pname) != 0) {
793 lprintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
794 return (ERROR + NO_SUCH_USER);
796 /* Don't delete a user who is currently logged in. Instead, just
797 * set the access level to 0, and let the account get swept up
798 * during the next purge.
800 user_is_logged_in = 0;
801 begin_critical_section(S_SESSION_TABLE);
802 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
803 if (ccptr->user.usernum == usbuf.usernum) {
804 user_is_logged_in = 1;
807 end_critical_section(S_SESSION_TABLE);
808 if (user_is_logged_in == 1) {
809 lprintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
814 lprintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
816 /* Perform any purge functions registered by server extensions */
817 PerformUserHooks(&usbuf, EVT_PURGEUSER);
819 /* delete any existing user/room relationships */
820 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
822 /* delete the userlog entry */
823 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
825 /* remove the user's bio file */
833 /* remove the user's picture */
846 * create_user() - back end processing to create a new user
848 * Set 'newusername' to the desired account name.
849 * Set 'become_user' to nonzero if this is self-service account creation and we want
850 * to actually log in as the user we just created, otherwise set it to 0.
852 int create_user(char *newusername, int become_user)
854 struct ctdluser usbuf;
855 struct ctdlroom qrbuf;
857 char mailboxname[ROOMNAMELEN];
861 safestrncpy(username, newusername, sizeof username);
864 if (config.c_auth_mode == 1) {
869 struct passwd *tempPwdPtr;
872 #ifdef SOLARIS_GETPWUID
873 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof(pwdbuffer));
875 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
877 if (tempPwdPtr != NULL) {
878 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
882 return (ERROR + NO_SUCH_USER);
886 if (!getuser(&usbuf, username)) {
887 return (ERROR + ALREADY_EXISTS);
890 /* Go ahead and initialize a new user record */
891 memset(&usbuf, 0, sizeof(struct ctdluser));
892 safestrncpy(usbuf.fullname, username, sizeof usbuf.fullname);
893 strcpy(usbuf.password, "");
896 /* These are the default flags on new accounts */
897 usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
899 usbuf.timescalled = 0;
901 usbuf.axlevel = config.c_initax;
902 usbuf.USscreenwidth = 80;
903 usbuf.USscreenheight = 24;
904 usbuf.lastcall = time(NULL);
906 /* fetch a new user number */
907 usbuf.usernum = get_new_user_number();
909 /* The very first user created on the system will always be an Aide */
910 if (usbuf.usernum == 1L) {
914 /* add user to userlog */
918 * Give the user a private mailbox and a configuration room.
919 * Make the latter an invisible system room.
921 MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
922 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
924 MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
925 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
926 if (lgetroom(&qrbuf, mailboxname) == 0) {
927 qrbuf.QRflags2 |= QR2_SYSTEM;
931 /* Perform any create functions registered by server extensions */
932 PerformUserHooks(&usbuf, EVT_NEWUSER);
934 /* Everything below this line can be bypassed if administratively
935 * creating a user, instead of doing self-service account creation
939 /* Now become the user we just created */
940 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
941 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
944 /* Check to make sure we're still who we think we are */
945 if (getuser(&CC->user, CC->curr_user)) {
946 return (ERROR + INTERNAL_ERROR);
951 "New user account <%s> has been created, from host %s [%s].\n",
956 aide_message(buf, "User Creation Notice");
957 lprintf(CTDL_NOTICE, "New user <%s> created\n", username);
965 * cmd_newu() - create a new user account and log in as that user
967 void cmd_newu(char *cmdbuf)
972 if (config.c_auth_mode == 1) {
973 cprintf("%d This system does not use native mode authentication.\n",
978 if (config.c_disable_newu) {
979 cprintf("%d Self-service user account creation "
980 "is disabled on this system.\n", ERROR + NOT_HERE);
985 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
989 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
990 ERROR + MAX_SESSIONS_EXCEEDED,
991 config.c_nodename, config.c_maxsessions);
993 extract_token(username, cmdbuf, 0, '|', sizeof username);
997 if (strlen(username) == 0) {
998 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1002 if ((!strcasecmp(username, "bbs")) ||
1003 (!strcasecmp(username, "new")) ||
1004 (!strcasecmp(username, "."))) {
1005 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
1009 a = create_user(username, 1);
1013 logged_in_response();
1014 } else if (a == ERROR + ALREADY_EXISTS) {
1015 cprintf("%d '%s' already exists.\n",
1016 ERROR + ALREADY_EXISTS, username);
1018 } else if (a == ERROR + INTERNAL_ERROR) {
1019 cprintf("%d Internal error - user record disappeared?\n",
1020 ERROR + INTERNAL_ERROR);
1023 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
1032 void cmd_setp(char *new_pw)
1034 if (CtdlAccessCheck(ac_logged_in)) {
1037 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
1038 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
1041 if (CC->is_master) {
1042 cprintf("%d The master prefix password cannot be changed with this command.\n",
1047 if (strlen(new_pw) == 0) {
1048 cprintf("%d Password unchanged.\n", CIT_OK);
1051 lgetuser(&CC->user, CC->curr_user);
1052 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1053 lputuser(&CC->user);
1054 cprintf("%d Password changed.\n", CIT_OK);
1055 lprintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
1056 PerformSessionHooks(EVT_SETPASS);
1061 * cmd_creu() - administratively create a new user account (do not log in to it)
1063 void cmd_creu(char *cmdbuf)
1068 struct ctdluser tmp;
1070 if (CtdlAccessCheck(ac_aide)) {
1074 extract_token(username, cmdbuf, 0, '|', sizeof username);
1075 extract_token(password, cmdbuf, 1, '|', sizeof password);
1081 if (strlen(username) == 0) {
1082 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1086 a = create_user(username, 0);
1089 if (strlen(password) > 0) {
1090 lgetuser(&tmp, username);
1091 safestrncpy(tmp.password, password, sizeof(tmp.password));
1094 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1095 (strlen(password) > 0) ? "and password set" :
1096 "with no password");
1098 } else if (a == ERROR + ALREADY_EXISTS) {
1099 cprintf("%d '%s' already exists.\n",
1100 ERROR + ALREADY_EXISTS, username);
1103 cprintf("%d An error occured creating the user account.\n", ERROR + INTERNAL_ERROR);
1110 * get user parameters
1115 if (CtdlAccessCheck(ac_logged_in))
1118 getuser(&CC->user, CC->curr_user);
1119 cprintf("%d %d|%d|%d|\n",
1121 CC->user.USscreenwidth,
1122 CC->user.USscreenheight,
1123 (CC->user.flags & US_USER_SET)
1128 * set user parameters
1130 void cmd_setu(char *new_parms)
1132 if (CtdlAccessCheck(ac_logged_in))
1135 if (num_parms(new_parms) < 3) {
1136 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1139 lgetuser(&CC->user, CC->curr_user);
1140 CC->user.USscreenwidth = extract_int(new_parms, 0);
1141 CC->user.USscreenheight = extract_int(new_parms, 1);
1142 CC->user.flags = CC->user.flags & (~US_USER_SET);
1143 CC->user.flags = CC->user.flags |
1144 (extract_int(new_parms, 2) & US_USER_SET);
1146 lputuser(&CC->user);
1147 cprintf("%d Ok\n", CIT_OK);
1151 * set last read pointer
1153 void cmd_slrp(char *new_ptr)
1157 struct visit original_vbuf;
1159 if (CtdlAccessCheck(ac_logged_in)) {
1163 if (!strncasecmp(new_ptr, "highest", 7)) {
1164 newlr = CC->room.QRhighest;
1166 newlr = atol(new_ptr);
1169 lgetuser(&CC->user, CC->curr_user);
1171 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1172 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1173 vbuf.v_lastseen = newlr;
1174 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1176 /* Only rewrite the record if it changed */
1177 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1178 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1179 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1182 lputuser(&CC->user);
1183 cprintf("%d %ld\n", CIT_OK, newlr);
1187 void cmd_seen(char *argbuf) {
1188 long target_msgnum = 0L;
1189 int target_setting = 0;
1191 if (CtdlAccessCheck(ac_logged_in)) {
1195 if (num_parms(argbuf) != 2) {
1196 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1200 target_msgnum = extract_long(argbuf, 0);
1201 target_setting = extract_int(argbuf, 1);
1203 CtdlSetSeen(&target_msgnum, 1, target_setting,
1204 ctdlsetseen_seen, NULL, NULL);
1205 cprintf("%d OK\n", CIT_OK);
1209 void cmd_gtsn(char *argbuf) {
1212 if (CtdlAccessCheck(ac_logged_in)) {
1216 CtdlGetSeen(buf, ctdlsetseen_seen);
1217 cprintf("%d %s\n", CIT_OK, buf);
1222 * API function for cmd_invt_kick() and anything else that needs to
1223 * invite or kick out a user to/from a room.
1225 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1227 int CtdlInvtKick(char *iuser, int op) {
1228 struct ctdluser USscratch;
1232 if (getuser(&USscratch, iuser) != 0) {
1236 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1238 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1239 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1242 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1243 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1245 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1247 /* post a message in Aide> saying what we just did */
1248 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1250 ((op == 1) ? "invited to" : "kicked out of"),
1253 aide_message(bbb,"User Admin Message");
1260 * INVT and KICK commands
1262 void cmd_invt_kick(char *iuser, int op) {
1265 * These commands are only allowed by aides, room aides,
1266 * and room namespace owners
1269 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1270 /* access granted */
1273 cprintf("%d Higher access or room ownership required.\n",
1274 ERROR + HIGHER_ACCESS_REQUIRED);
1278 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1280 cprintf("%d Can't add/remove users from this room.\n",
1285 if (CtdlInvtKick(iuser, op) != 0) {
1286 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1290 cprintf("%d %s %s %s.\n",
1292 ((op == 1) ? "invited to" : "kicked out of"),
1299 * Forget (Zap) the current room (API call)
1300 * Returns 0 on success
1302 int CtdlForgetThisRoom(void) {
1305 /* On some systems, Aides are not allowed to forget rooms */
1306 if (is_aide() && (config.c_aide_zap == 0)
1307 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1311 lgetuser(&CC->user, CC->curr_user);
1312 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1314 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1315 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1317 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1318 lputuser(&CC->user);
1320 /* Return to the Lobby, so we don't end up in an undefined room */
1321 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1328 * forget (Zap) the current room
1333 if (CtdlAccessCheck(ac_logged_in)) {
1337 if (CtdlForgetThisRoom() == 0) {
1338 cprintf("%d Ok\n", CIT_OK);
1341 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1346 * Get Next Unregistered User
1350 struct cdbdata *cdbus;
1351 struct ctdluser usbuf;
1353 if (CtdlAccessCheck(ac_aide)) {
1357 if ((CitControl.MMflags & MM_VALID) == 0) {
1358 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1362 /* There are unvalidated users. Traverse the user database,
1363 * and return the first user we find that needs validation.
1365 cdb_rewind(CDB_USERS);
1366 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1367 memset(&usbuf, 0, sizeof(struct ctdluser));
1368 memcpy(&usbuf, cdbus->ptr,
1369 ((cdbus->len > sizeof(struct ctdluser)) ?
1370 sizeof(struct ctdluser) : cdbus->len));
1372 if ((usbuf.flags & US_NEEDVALID)
1373 && (usbuf.axlevel > 0)) {
1374 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1375 cdb_close_cursor(CDB_USERS);
1380 /* If we get to this point, there are no more unvalidated users.
1381 * Therefore we clear the "users need validation" flag.
1384 begin_critical_section(S_CONTROL);
1386 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1388 end_critical_section(S_CONTROL);
1389 cprintf("%d *** End of registration.\n", CIT_OK);
1398 void cmd_vali(char *v_args)
1402 struct ctdluser userbuf;
1404 extract_token(user, v_args, 0, '|', sizeof user);
1405 newax = extract_int(v_args, 1);
1407 if (CtdlAccessCheck(ac_aide)) {
1411 if (lgetuser(&userbuf, user) != 0) {
1412 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1416 userbuf.axlevel = newax;
1417 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1421 /* If the access level was set to zero, delete the user */
1423 if (purge_user(user) == 0) {
1424 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1428 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1434 * Traverse the user file...
1436 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1439 struct ctdluser usbuf;
1440 struct cdbdata *cdbus;
1442 cdb_rewind(CDB_USERS);
1444 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1445 memset(&usbuf, 0, sizeof(struct ctdluser));
1446 memcpy(&usbuf, cdbus->ptr,
1447 ((cdbus->len > sizeof(struct ctdluser)) ?
1448 sizeof(struct ctdluser) : cdbus->len));
1450 (*CallBack) (&usbuf, in_data);
1456 * List one user (this works with cmd_list)
1458 void ListThisUser(struct ctdluser *usbuf, void *data)
1462 searchstring = (char *)data;
1463 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1467 if (usbuf->axlevel > 0) {
1468 if ((CC->user.axlevel >= 6)
1469 || ((usbuf->flags & US_UNLISTED) == 0)
1470 || ((CC->internal_pgm))) {
1471 cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1475 (long)usbuf->lastcall,
1478 if (CC->user.axlevel >= 6)
1479 cprintf("%s", usbuf->password);
1486 * List users (searchstring may be empty to list all users)
1488 void cmd_list(char *cmdbuf)
1490 char searchstring[256];
1491 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1492 striplt(searchstring);
1493 cprintf("%d \n", LISTING_FOLLOWS);
1494 ForEachUser(ListThisUser, (void *)searchstring );
1502 * assorted info we need to check at login
1510 if (CtdlAccessCheck(ac_logged_in)) {
1514 getuser(&CC->user, CC->curr_user); /* no lock is needed here */
1515 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1518 if (CC->user.axlevel >= 6) {
1520 if (CitControl.MMflags & MM_VALID)
1524 /* check for mail */
1525 mail = InitialMailCheck();
1527 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1532 * check to see if a user exists
1534 void cmd_qusr(char *who)
1536 struct ctdluser usbuf;
1538 if (getuser(&usbuf, who) == 0) {
1539 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1541 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1547 * Administrative Get User Parameters
1549 void cmd_agup(char *cmdbuf)
1551 struct ctdluser usbuf;
1552 char requested_user[128];
1554 if (CtdlAccessCheck(ac_aide)) {
1558 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1559 if (getuser(&usbuf, requested_user) != 0) {
1560 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1563 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1570 (int) usbuf.axlevel,
1572 (long)usbuf.lastcall,
1579 * Administrative Set User Parameters
1581 void cmd_asup(char *cmdbuf)
1583 struct ctdluser usbuf;
1584 char requested_user[128];
1590 if (CtdlAccessCheck(ac_aide))
1593 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1594 if (lgetuser(&usbuf, requested_user) != 0) {
1595 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1598 np = num_parms(cmdbuf);
1600 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1602 usbuf.flags = extract_int(cmdbuf, 2);
1604 usbuf.timescalled = extract_int(cmdbuf, 3);
1606 usbuf.posted = extract_int(cmdbuf, 4);
1608 newax = extract_int(cmdbuf, 5);
1609 if ((newax >= 0) && (newax <= 6)) {
1610 usbuf.axlevel = extract_int(cmdbuf, 5);
1614 usbuf.lastcall = extract_long(cmdbuf, 7);
1617 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1620 if (usbuf.axlevel == 0) {
1621 if (purge_user(requested_user) == 0) {
1627 sprintf(notify, "User \"%s\" has been deleted by %s.\n",
1628 usbuf.fullname, CC->user.fullname);
1629 aide_message(notify, "User Deletion Message");
1632 cprintf("%d Ok", CIT_OK);
1634 cprintf(" (%s deleted)", requested_user);
1641 * Check to see if the user who we just sent mail to is logged in. If yes,
1642 * bump the 'new mail' counter for their session. That enables them to
1643 * receive a new mail notification without having to hit the database.
1645 void BumpNewMailCounter(long which_user) {
1646 struct CitContext *ptr;
1648 begin_critical_section(S_SESSION_TABLE);
1650 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1651 if (ptr->user.usernum == which_user) {
1656 end_critical_section(S_SESSION_TABLE);
1661 * Count the number of new mail messages the user has
1665 int num_newmsgs = 0;
1667 num_newmsgs = CC->newmail;
1670 return (num_newmsgs);
1675 * Count the number of new mail messages the user has
1677 int InitialMailCheck()
1679 int num_newmsgs = 0;
1681 char mailboxname[ROOMNAMELEN];
1682 struct ctdlroom mailbox;
1684 struct cdbdata *cdbfr;
1685 long *msglist = NULL;
1688 MailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1689 if (getroom(&mailbox, mailboxname) != 0)
1691 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1693 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1695 if (cdbfr != NULL) {
1696 msglist = malloc(cdbfr->len);
1697 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1698 num_msgs = cdbfr->len / sizeof(long);
1702 for (a = 0; a < num_msgs; ++a) {
1703 if (msglist[a] > 0L) {
1704 if (msglist[a] > vbuf.v_lastseen) {
1709 if (msglist != NULL)
1712 return (num_newmsgs);
1718 * Set the preferred view for the current user/room combination
1720 void cmd_view(char *cmdbuf) {
1724 if (CtdlAccessCheck(ac_logged_in)) {
1728 requested_view = extract_int(cmdbuf, 0);
1730 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1731 vbuf.v_view = requested_view;
1732 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1734 cprintf("%d ok\n", CIT_OK);