4 * Server functions which perform operations on user objects.
17 #include <sys/types.h>
19 #ifdef HAVE_SYS_STAT_H
23 #if TIME_WITH_SYS_TIME
24 # include <sys/time.h>
28 # include <sys/time.h>
36 #include <libcitadel.h>
42 #include "sysdep_decls.h"
49 #include "citserver.h"
50 #include "citadel_dirs.h"
54 /* These pipes are used to talk to the chkpwd daemon, which is forked during startup */
55 int chkpwd_write_pipe[2];
56 int chkpwd_read_pipe[2];
59 * makeuserkey() - convert a username into the format used as a database key
60 * (it's just the username converted into lower case)
62 static INLINE void makeuserkey(char *key, char *username) {
65 len = strlen(username);
66 if (len >= USERNAME_SIZE)
68 lprintf (CTDL_EMERG, "Username to long: %s", username);
70 len = USERNAME_SIZE - 1;
71 username[USERNAME_SIZE - 1]='\0';
73 for (i=0; i<=len; ++i) {
74 key[i] = tolower(username[i]);
80 * getuser() - retrieve named user into supplied buffer.
81 * returns 0 on success
83 int getuser(struct ctdluser *usbuf, char name[])
86 char usernamekey[USERNAME_SIZE];
87 struct cdbdata *cdbus;
90 memset(usbuf, 0, sizeof(struct ctdluser));
93 makeuserkey(usernamekey, name);
94 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
96 if (cdbus == NULL) { /* user not found */
100 memcpy(usbuf, cdbus->ptr,
101 ((cdbus->len > sizeof(struct ctdluser)) ?
102 sizeof(struct ctdluser) : cdbus->len));
111 * lgetuser() - same as getuser() but locks the record
113 int lgetuser(struct ctdluser *usbuf, char *name)
117 retcode = getuser(usbuf, name);
119 begin_critical_section(S_USERS);
126 * putuser() - write user buffer into the correct place on disk
128 void putuser(struct ctdluser *usbuf)
130 char usernamekey[USERNAME_SIZE];
132 makeuserkey(usernamekey, usbuf->fullname);
134 usbuf->version = REV_LEVEL;
136 usernamekey, strlen(usernamekey),
137 usbuf, sizeof(struct ctdluser));
143 * lputuser() - same as putuser() but locks the record
145 void lputuser(struct ctdluser *usbuf)
148 end_critical_section(S_USERS);
153 * rename_user() - this is tricky because the user's display name is the database key
155 * Returns 0 on success or nonzero if there was an error...
158 int rename_user(char *oldname, char *newname) {
159 struct CitContext *cptr;
160 int retcode = RENAMEUSER_OK;
161 struct ctdluser usbuf;
163 char oldnamekey[USERNAME_SIZE];
164 char newnamekey[USERNAME_SIZE];
166 /* We cannot rename a user who is currently logged in */
167 for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
168 if (!strcasecmp(cptr->user.fullname, oldname)) {
169 return(RENAMEUSER_LOGGED_IN);
173 /* Create the database keys... */
174 makeuserkey(oldnamekey, oldname);
175 makeuserkey(newnamekey, newname);
177 /* Lock up and get going */
178 begin_critical_section(S_USERS);
180 if (getuser(&usbuf, newname) == 0) {
181 retcode = RENAMEUSER_ALREADY_EXISTS;
185 if (getuser(&usbuf, oldname) != 0) {
186 retcode = RENAMEUSER_NOT_FOUND;
189 else { /* Sanity checks succeeded. Now rename the user. */
191 lprintf(CTDL_DEBUG, "Renaming <%s> to <%s>\n", oldname, newname);
192 cdb_delete(CDB_USERS, oldnamekey, strlen(oldnamekey));
193 safestrncpy(usbuf.fullname, newname, sizeof usbuf.fullname);
195 retcode = RENAMEUSER_OK;
200 end_critical_section(S_USERS);
207 * Index-generating function used by Ctdl[Get|Set]Relationship
209 int GenerateRelationshipIndex(char *IndexBuf,
221 TheIndex.iRoomID = RoomID;
222 TheIndex.iRoomGen = RoomGen;
223 TheIndex.iUserID = UserID;
225 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
226 return (sizeof(TheIndex));
232 * Back end for CtdlSetRelationship()
234 void put_visit(struct visit *newvisit)
239 memset (IndexBuf, 0, sizeof (IndexBuf));
240 /* Generate an index */
241 IndexLen = GenerateRelationshipIndex(IndexBuf,
244 newvisit->v_usernum);
246 /* Store the record */
247 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
248 newvisit, sizeof(struct visit)
256 * Define a relationship between a user and a room
258 void CtdlSetRelationship(struct visit *newvisit,
259 struct ctdluser *rel_user,
260 struct ctdlroom *rel_room)
264 /* We don't use these in Citadel because they're implicit by the
265 * index, but they must be present if the database is exported.
267 newvisit->v_roomnum = rel_room->QRnumber;
268 newvisit->v_roomgen = rel_room->QRgen;
269 newvisit->v_usernum = rel_user->usernum;
275 * Locate a relationship between a user and a room
277 void CtdlGetRelationship(struct visit *vbuf,
278 struct ctdluser *rel_user,
279 struct ctdlroom *rel_room)
284 struct cdbdata *cdbvisit;
286 /* Generate an index */
287 IndexLen = GenerateRelationshipIndex(IndexBuf,
292 /* Clear out the buffer */
293 memset(vbuf, 0, sizeof(struct visit));
295 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
296 if (cdbvisit != NULL) {
297 memcpy(vbuf, cdbvisit->ptr,
298 ((cdbvisit->len > sizeof(struct visit)) ?
299 sizeof(struct visit) : cdbvisit->len));
303 /* If this is the first time the user has seen this room,
304 * set the view to be the default for the room.
306 vbuf->v_view = rel_room->QRdefaultview;
309 /* Set v_seen if necessary */
310 if (vbuf->v_seen[0] == 0) {
311 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
316 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
318 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
323 * Is the user currently logged in an Aide?
327 if (CC->user.axlevel >= 6)
335 * Is the user currently logged in an Aide *or* the room aide for this room?
337 int is_room_aide(void)
340 if (!CC->logged_in) {
344 if ((CC->user.axlevel >= 6)
345 || (CC->room.QRroomaide == CC->user.usernum)) {
353 * getuserbynumber() - get user by number
354 * returns 0 if user was found
356 * WARNING: don't use this function unless you absolutely have to. It does
357 * a sequential search and therefore is computationally expensive.
359 int getuserbynumber(struct ctdluser *usbuf, long int number)
361 struct cdbdata *cdbus;
363 cdb_rewind(CDB_USERS);
365 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
366 memset(usbuf, 0, sizeof(struct ctdluser));
367 memcpy(usbuf, cdbus->ptr,
368 ((cdbus->len > sizeof(struct ctdluser)) ?
369 sizeof(struct ctdluser) : cdbus->len));
371 if (usbuf->usernum == number) {
372 cdb_close_cursor(CDB_USERS);
381 * getuserbyuid() - get user by system uid (for PAM mode authentication)
382 * returns 0 if user was found
384 * WARNING: don't use this function unless you absolutely have to. It does
385 * a sequential search and therefore is computationally expensive.
387 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
389 struct cdbdata *cdbus;
391 cdb_rewind(CDB_USERS);
393 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
394 memset(usbuf, 0, sizeof(struct ctdluser));
395 memcpy(usbuf, cdbus->ptr,
396 ((cdbus->len > sizeof(struct ctdluser)) ?
397 sizeof(struct ctdluser) : cdbus->len));
399 if (usbuf->uid == number) {
400 cdb_close_cursor(CDB_USERS);
408 * Back end for cmd_user() and its ilk
410 * NOTE: "authname" should only be used if we are attempting to use the "master user" feature
412 int CtdlLoginExistingUser(char *authname, char *trythisname)
417 lprintf(9, "CtdlLoginExistingUser(%s, %s)\n", authname, trythisname);
419 if ((CC->logged_in)) {
420 return login_already_logged_in;
423 if (trythisname == NULL) return login_not_found;
425 /* If a "master user" is defined, handle its authentication if specified */
427 if (strlen(config.c_master_user) > 0) if (strlen(config.c_master_pass) > 0) if (authname) {
428 if (!strcasecmp(authname, config.c_master_user)) {
433 /* Continue attempting user validation... */
434 safestrncpy(username, trythisname, USERNAME_SIZE);
437 if (IsEmptyStr(username)) {
438 return login_not_found;
441 if (config.c_auth_mode == AUTHMODE_HOST) {
446 struct passwd *tempPwdPtr;
449 lprintf(CTDL_DEBUG, "asking host about <%s>\n", username);
450 #ifdef HAVE_GETPWNAM_R
451 #ifdef SOLARIS_GETPWUID
452 lprintf(CTDL_DEBUG, "Calling getpwnam_r()\n");
453 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
454 #else // SOLARIS_GETPWUID
455 lprintf(CTDL_DEBUG, "Calling getpwnam_r()\n");
456 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
457 #endif // SOLARIS_GETPWUID
458 #else // HAVE_GETPWNAM_R
459 lprintf(CTDL_DEBUG, "SHOULD NEVER GET HERE!!!\n");
461 #endif // HAVE_GETPWNAM_R
462 if (tempPwdPtr == NULL) {
463 lprintf(CTDL_DEBUG, "no such user <%s>\n", username);
464 return login_not_found;
467 /* Locate the associated Citadel account.
468 * If not found, make one attempt to create it.
470 found_user = getuserbyuid(&CC->user, pd.pw_uid);
471 lprintf(CTDL_DEBUG, "found it: uid=%ld, gecos=%s here: %d\n",
472 (long)pd.pw_uid, pd.pw_gecos, found_user);
473 if (found_user != 0) {
474 create_user(username, 0);
475 found_user = getuserbyuid(&CC->user, pd.pw_uid);
481 /* native auth mode */
483 struct recptypes *valid = NULL;
485 /* First, try to log in as if the supplied name is a display name */
486 found_user = getuser(&CC->user, username);
488 /* If that didn't work, try to log in as if the supplied name
489 * is an e-mail address
491 if (found_user != 0) {
492 valid = validate_recipients(username, NULL, 0);
494 if (valid->num_local == 1) {
495 found_user = getuser(&CC->user, valid->recp_local);
497 free_recipients(valid);
502 /* Did we find something? */
503 if (found_user == 0) {
504 if (((CC->nologin)) && (CC->user.axlevel < 6)) {
505 return login_too_many_users;
507 safestrncpy(CC->curr_user, CC->user.fullname,
508 sizeof CC->curr_user);
512 return login_not_found;
520 void cmd_user(char *cmdbuf)
525 extract_token(username, cmdbuf, 0, '|', sizeof username);
528 a = CtdlLoginExistingUser(NULL, username);
530 case login_already_logged_in:
531 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
533 case login_too_many_users:
535 "Too many users are already online "
537 ERROR + MAX_SESSIONS_EXCEEDED,
538 config.c_nodename, config.c_maxsessions);
541 cprintf("%d Password required for %s\n",
542 MORE_DATA, CC->curr_user);
544 case login_not_found:
545 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
548 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
555 * session startup code which is common to both cmd_pass() and cmd_newu()
557 void session_startup(void)
559 lprintf(CTDL_NOTICE, "<%s> logged in\n", CC->curr_user);
561 lgetuser(&CC->user, CC->curr_user);
562 ++(CC->user.timescalled);
563 CC->previous_login = CC->user.lastcall;
564 time(&CC->user.lastcall);
566 /* If this user's name is the name of the system administrator
567 * (as specified in setup), automatically assign access level 6.
569 if (!strcasecmp(CC->user.fullname, config.c_sysadm)) {
570 CC->user.axlevel = 6;
573 /* If we're authenticating off the host system, automatically give
574 * root the highest level of access.
576 if (config.c_auth_mode == AUTHMODE_HOST) {
577 if (CC->user.uid == 0) {
578 CC->user.axlevel = 6;
585 * Populate CC->cs_inet_email with a default address. This will be
586 * overwritten with the user's directory address, if one exists, when
587 * the vCard module's login hook runs.
589 snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
590 CC->user.fullname, config.c_fqdn);
591 convert_spaces_to_underscores(CC->cs_inet_email);
593 /* Create any personal rooms required by the system.
594 * (Technically, MAILROOM should be there already, but just in case...)
596 create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
597 create_room(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
598 create_room(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
600 /* Run any startup routines registered by loadable modules */
601 PerformSessionHooks(EVT_LOGIN);
603 /* Enter the lobby */
604 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
608 void logged_in_response(void)
610 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
611 CIT_OK, CC->user.fullname, CC->user.axlevel,
612 CC->user.timescalled, CC->user.posted,
613 CC->user.flags, CC->user.usernum,
620 * misc things to be taken care of when a user is logged out
624 struct CitContext *CCC = CC; /* CachedCitContext - performance boost */
626 * If there is a download in progress, abort it.
628 if (CCC->download_fp != NULL) {
629 fclose(CCC->download_fp);
630 CCC->download_fp = NULL;
634 * If there is an upload in progress, abort it.
636 if (CCC->upload_fp != NULL) {
641 * If we were talking to a network node, we're not anymore...
643 if (!IsEmptyStr(CCC->net_node)) {
644 network_talking_to(CCC->net_node, NTT_REMOVE);
647 /* Run any hooks registered by modules... */
648 PerformSessionHooks(EVT_LOGOUT);
651 * Clear out some session data. Most likely, the CitContext for this
652 * session is about to get nuked when the session disconnects, but
653 * since it's possible to log in again without reconnecting, we cannot
654 * make that assumption.
656 strcpy(CCC->fake_username, "");
657 strcpy(CCC->fake_hostname, "");
658 strcpy(CCC->fake_roomname, "");
661 /* Check to see if the user was deleted whilst logged in and purge them if necessary */
662 if (CCC->user.axlevel == 0)
663 purge_user(CCC->user.fullname);
665 /* Free any output buffers */
666 if (CCC->output_buffer != NULL) {
672 * Validate a password on the host unix system by talking to the chkpwd daemon
674 static int validpw(uid_t uid, const char *pass)
678 if (IsEmptyStr(pass)) {
679 lprintf(CTDL_DEBUG, "refusing to check empty password for uid=%d using chkpwd...\n", uid);
683 lprintf(CTDL_DEBUG, "Validating password for uid=%d using chkpwd...\n", uid);
685 begin_critical_section(S_CHKPWD);
686 write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
687 write(chkpwd_write_pipe[1], pass, 256);
688 read(chkpwd_read_pipe[0], buf, 4);
689 end_critical_section(S_CHKPWD);
691 if (!strncmp(buf, "PASS", 4)) {
692 lprintf(CTDL_DEBUG, "...pass\n");
696 lprintf(CTDL_DEBUG, "...fail\n");
701 * Start up the chkpwd daemon so validpw() has something to talk to
703 void start_chkpwd_daemon(void) {
705 struct stat filestats;
708 lprintf(CTDL_DEBUG, "Starting chkpwd daemon for host authentication mode\n");
710 if ((stat(file_chkpwd, &filestats)==-1) ||
711 (filestats.st_size==0)){
712 printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
715 if (pipe(chkpwd_write_pipe) != 0) {
716 lprintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
719 if (pipe(chkpwd_read_pipe) != 0) {
720 lprintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
725 if (chkpwd_pid < 0) {
726 lprintf(CTDL_EMERG, "Unable to fork chkpwd daemon: %s\n", strerror(errno));
729 if (chkpwd_pid == 0) {
730 lprintf(CTDL_DEBUG, "Now calling dup2() write\n");
731 dup2(chkpwd_write_pipe[0], 0);
732 lprintf(CTDL_DEBUG, "Now calling dup2() write\n");
733 dup2(chkpwd_read_pipe[1], 1);
734 lprintf(CTDL_DEBUG, "Now closing stuff\n");
735 for (i=2; i<256; ++i) close(i);
736 lprintf(CTDL_DEBUG, "Now calling execl(%s)\n", file_chkpwd);
737 execl(file_chkpwd, file_chkpwd, NULL);
738 lprintf(CTDL_EMERG, "Unable to exec chkpwd daemon: %s\n", strerror(errno));
752 int CtdlTryPassword(char *password)
756 if ((CC->logged_in)) {
757 lprintf(CTDL_WARNING, "CtdlTryPassword: already logged in\n");
758 return pass_already_logged_in;
760 if (!strcmp(CC->curr_user, NLI)) {
761 lprintf(CTDL_WARNING, "CtdlTryPassword: no user selected\n");
764 if (getuser(&CC->user, CC->curr_user)) {
765 lprintf(CTDL_ERR, "CtdlTryPassword: internal error\n");
766 return pass_internal_error;
768 if (password == NULL) {
769 lprintf(CTDL_INFO, "CtdlTryPassword: NULL password string supplied\n");
770 return pass_wrong_password;
775 code = strcmp(password, config.c_master_pass);
778 else if (config.c_auth_mode == AUTHMODE_HOST) {
782 if (validpw(CC->user.uid, password)) {
786 * sooper-seekrit hack: populate the password field in the
787 * citadel database with the password that the user typed,
788 * if it's correct. This allows most sites to convert from
789 * host auth to native auth if they want to. If you think
790 * this is a security hazard, comment it out.
793 lgetuser(&CC->user, CC->curr_user);
794 safestrncpy(CC->user.password, password, sizeof CC->user.password);
798 * (sooper-seekrit hack ends here)
809 /* native auth mode */
812 strproc(CC->user.password);
813 code = strcasecmp(CC->user.password, password);
815 strproc(CC->user.password);
816 code = strcasecmp(CC->user.password, password);
823 lprintf(CTDL_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
824 return pass_wrong_password;
829 void cmd_pass(char *buf)
834 extract_token(password, buf, 0, '|', sizeof password);
835 a = CtdlTryPassword(password);
838 case pass_already_logged_in:
839 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
842 cprintf("%d You must send a name with USER first.\n",
843 ERROR + USERNAME_REQUIRED);
845 case pass_wrong_password:
846 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
849 logged_in_response();
857 * Delete a user record *and* all of its related resources.
859 int purge_user(char pname[])
862 struct ctdluser usbuf;
863 char usernamekey[USERNAME_SIZE];
864 struct CitContext *ccptr;
865 int user_is_logged_in = 0;
867 makeuserkey(usernamekey, pname);
869 /* If the name is empty we can't find them in the DB any way so just return */
870 if (IsEmptyStr(pname))
871 return (ERROR + NO_SUCH_USER);
873 if (getuser(&usbuf, pname) != 0) {
874 lprintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
875 return (ERROR + NO_SUCH_USER);
877 /* Don't delete a user who is currently logged in. Instead, just
878 * set the access level to 0, and let the account get swept up
879 * during the next purge.
881 user_is_logged_in = 0;
882 begin_critical_section(S_SESSION_TABLE);
883 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
884 if (ccptr->user.usernum == usbuf.usernum) {
885 user_is_logged_in = 1;
888 end_critical_section(S_SESSION_TABLE);
889 if (user_is_logged_in == 1) {
890 lprintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
895 lprintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
897 /* Perform any purge functions registered by server extensions */
898 PerformUserHooks(&usbuf, EVT_PURGEUSER);
900 /* delete any existing user/room relationships */
901 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
903 /* delete the userlog entry */
904 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
906 /* remove the user's bio file */
914 /* remove the user's picture */
927 * create_user() - back end processing to create a new user
929 * Set 'newusername' to the desired account name.
930 * Set 'become_user' to nonzero if this is self-service account creation and we want
931 * to actually log in as the user we just created, otherwise set it to 0.
933 int create_user(char *newusername, int become_user)
935 struct ctdluser usbuf;
936 struct ctdlroom qrbuf;
938 char mailboxname[ROOMNAMELEN];
942 safestrncpy(username, newusername, sizeof username);
945 if (config.c_auth_mode == AUTHMODE_HOST) {
950 struct passwd *tempPwdPtr;
953 #ifdef HAVE_GETPWNAM_R
954 #ifdef SOLARIS_GETPWUID
955 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof(pwdbuffer));
956 #else // SOLARIS_GETPWUID
957 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
958 #endif // SOLARIS_GETPWUID
959 #else // HAVE_GETPWNAM_R
961 #endif // HAVE_GETPWNAM_R
962 if (tempPwdPtr != NULL) {
963 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
965 if (IsEmptyStr (username))
968 "Can't find Realname for user %s [%d] in the Host Auth Database; giving up.\n",
969 newusername, pd.pw_uid);
971 "Can't find Realname for user %s [%d] in the Host Auth Database; giving up.\n",
972 newusername, pd.pw_uid);
973 aide_message(buf, "User Creation Failure Notice");
978 return (ERROR + NO_SUCH_USER);
982 if (!getuser(&usbuf, username)) {
983 return (ERROR + ALREADY_EXISTS);
986 /* Go ahead and initialize a new user record */
987 memset(&usbuf, 0, sizeof(struct ctdluser));
988 safestrncpy(usbuf.fullname, username, sizeof usbuf.fullname);
989 strcpy(usbuf.password, "");
992 /* These are the default flags on new accounts */
993 usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
995 usbuf.timescalled = 0;
997 usbuf.axlevel = config.c_initax;
998 usbuf.USscreenwidth = 80;
999 usbuf.USscreenheight = 24;
1000 usbuf.lastcall = time(NULL);
1002 /* fetch a new user number */
1003 usbuf.usernum = get_new_user_number();
1005 /* The very first user created on the system will always be an Aide */
1006 if (usbuf.usernum == 1L) {
1010 /* add user to userlog */
1014 * Give the user a private mailbox and a configuration room.
1015 * Make the latter an invisible system room.
1017 MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
1018 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
1020 MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
1021 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
1022 if (lgetroom(&qrbuf, mailboxname) == 0) {
1023 qrbuf.QRflags2 |= QR2_SYSTEM;
1027 /* Perform any create functions registered by server extensions */
1028 PerformUserHooks(&usbuf, EVT_NEWUSER);
1030 /* Everything below this line can be bypassed if administratively
1031 * creating a user, instead of doing self-service account creation
1035 /* Now become the user we just created */
1036 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
1037 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
1040 /* Check to make sure we're still who we think we are */
1041 if (getuser(&CC->user, CC->curr_user)) {
1042 return (ERROR + INTERNAL_ERROR);
1047 "New user account <%s> has been created, from host %s [%s].\n",
1052 aide_message(buf, "User Creation Notice");
1053 lprintf(CTDL_NOTICE, "New user <%s> created\n", username);
1061 * cmd_newu() - create a new user account and log in as that user
1063 void cmd_newu(char *cmdbuf)
1068 if (config.c_auth_mode != AUTHMODE_NATIVE) {
1069 cprintf("%d This system does not use native mode authentication.\n",
1074 if (config.c_disable_newu) {
1075 cprintf("%d Self-service user account creation "
1076 "is disabled on this system.\n", ERROR + NOT_HERE);
1080 if (CC->logged_in) {
1081 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
1085 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
1086 ERROR + MAX_SESSIONS_EXCEEDED,
1087 config.c_nodename, config.c_maxsessions);
1089 extract_token(username, cmdbuf, 0, '|', sizeof username);
1093 if (IsEmptyStr(username)) {
1094 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1098 if ((!strcasecmp(username, "bbs")) ||
1099 (!strcasecmp(username, "new")) ||
1100 (!strcasecmp(username, "."))) {
1101 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
1105 a = create_user(username, 1);
1109 logged_in_response();
1110 } else if (a == ERROR + ALREADY_EXISTS) {
1111 cprintf("%d '%s' already exists.\n",
1112 ERROR + ALREADY_EXISTS, username);
1114 } else if (a == ERROR + INTERNAL_ERROR) {
1115 cprintf("%d Internal error - user record disappeared?\n",
1116 ERROR + INTERNAL_ERROR);
1119 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
1128 void cmd_setp(char *new_pw)
1130 if (CtdlAccessCheck(ac_logged_in)) {
1133 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
1134 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
1137 if (CC->is_master) {
1138 cprintf("%d The master prefix password cannot be changed with this command.\n",
1143 if (IsEmptyStr(new_pw)) {
1144 cprintf("%d Password unchanged.\n", CIT_OK);
1147 lgetuser(&CC->user, CC->curr_user);
1148 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1149 lputuser(&CC->user);
1150 cprintf("%d Password changed.\n", CIT_OK);
1151 lprintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
1152 PerformSessionHooks(EVT_SETPASS);
1157 * cmd_creu() - administratively create a new user account (do not log in to it)
1159 void cmd_creu(char *cmdbuf)
1164 struct ctdluser tmp;
1166 if (CtdlAccessCheck(ac_aide)) {
1170 extract_token(username, cmdbuf, 0, '|', sizeof username);
1171 extract_token(password, cmdbuf, 1, '|', sizeof password);
1177 if (IsEmptyStr(username)) {
1178 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1182 a = create_user(username, 0);
1185 if (!IsEmptyStr(password)) {
1186 lgetuser(&tmp, username);
1187 safestrncpy(tmp.password, password, sizeof(tmp.password));
1190 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1191 (!IsEmptyStr(password)) ? "and password set" :
1192 "with no password");
1194 } else if (a == ERROR + ALREADY_EXISTS) {
1195 cprintf("%d '%s' already exists.\n", ERROR + ALREADY_EXISTS, username);
1197 } else if ( (config.c_auth_mode != AUTHMODE_NATIVE) && (a == ERROR + NO_SUCH_USER) ) {
1198 cprintf("%d User accounts are not created within Citadel in host authentication mode.\n",
1199 ERROR + NO_SUCH_USER);
1202 cprintf("%d An error occurred creating the user account.\n", ERROR + INTERNAL_ERROR);
1209 * get user parameters
1214 if (CtdlAccessCheck(ac_logged_in))
1217 getuser(&CC->user, CC->curr_user);
1218 cprintf("%d %d|%d|%d|\n",
1220 CC->user.USscreenwidth,
1221 CC->user.USscreenheight,
1222 (CC->user.flags & US_USER_SET)
1227 * set user parameters
1229 void cmd_setu(char *new_parms)
1231 if (CtdlAccessCheck(ac_logged_in))
1234 if (num_parms(new_parms) < 3) {
1235 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1238 lgetuser(&CC->user, CC->curr_user);
1239 CC->user.USscreenwidth = extract_int(new_parms, 0);
1240 CC->user.USscreenheight = extract_int(new_parms, 1);
1241 CC->user.flags = CC->user.flags & (~US_USER_SET);
1242 CC->user.flags = CC->user.flags |
1243 (extract_int(new_parms, 2) & US_USER_SET);
1245 lputuser(&CC->user);
1246 cprintf("%d Ok\n", CIT_OK);
1250 * set last read pointer
1252 void cmd_slrp(char *new_ptr)
1256 struct visit original_vbuf;
1258 if (CtdlAccessCheck(ac_logged_in)) {
1262 if (!strncasecmp(new_ptr, "highest", 7)) {
1263 newlr = CC->room.QRhighest;
1265 newlr = atol(new_ptr);
1268 lgetuser(&CC->user, CC->curr_user);
1270 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1271 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1272 vbuf.v_lastseen = newlr;
1273 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1275 /* Only rewrite the record if it changed */
1276 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1277 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1278 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1281 lputuser(&CC->user);
1282 cprintf("%d %ld\n", CIT_OK, newlr);
1286 void cmd_seen(char *argbuf) {
1287 long target_msgnum = 0L;
1288 int target_setting = 0;
1290 if (CtdlAccessCheck(ac_logged_in)) {
1294 if (num_parms(argbuf) != 2) {
1295 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1299 target_msgnum = extract_long(argbuf, 0);
1300 target_setting = extract_int(argbuf, 1);
1302 CtdlSetSeen(&target_msgnum, 1, target_setting,
1303 ctdlsetseen_seen, NULL, NULL);
1304 cprintf("%d OK\n", CIT_OK);
1308 void cmd_gtsn(char *argbuf) {
1311 if (CtdlAccessCheck(ac_logged_in)) {
1315 CtdlGetSeen(buf, ctdlsetseen_seen);
1316 cprintf("%d %s\n", CIT_OK, buf);
1321 * API function for cmd_invt_kick() and anything else that needs to
1322 * invite or kick out a user to/from a room.
1324 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1326 int CtdlInvtKick(char *iuser, int op) {
1327 struct ctdluser USscratch;
1331 if (getuser(&USscratch, iuser) != 0) {
1335 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1337 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1338 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1341 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1342 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1344 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1346 /* post a message in Aide> saying what we just did */
1347 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1349 ((op == 1) ? "invited to" : "kicked out of"),
1352 aide_message(bbb,"User Admin Message");
1359 * INVT and KICK commands
1361 void cmd_invt_kick(char *iuser, int op) {
1364 * These commands are only allowed by aides, room aides,
1365 * and room namespace owners
1368 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1369 /* access granted */
1372 cprintf("%d Higher access or room ownership required.\n",
1373 ERROR + HIGHER_ACCESS_REQUIRED);
1377 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1379 cprintf("%d Can't add/remove users from this room.\n",
1384 if (CtdlInvtKick(iuser, op) != 0) {
1385 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1389 cprintf("%d %s %s %s.\n",
1391 ((op == 1) ? "invited to" : "kicked out of"),
1398 * Forget (Zap) the current room (API call)
1399 * Returns 0 on success
1401 int CtdlForgetThisRoom(void) {
1404 /* On some systems, Aides are not allowed to forget rooms */
1405 if (is_aide() && (config.c_aide_zap == 0)
1406 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1410 lgetuser(&CC->user, CC->curr_user);
1411 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1413 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1414 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1416 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1417 lputuser(&CC->user);
1419 /* Return to the Lobby, so we don't end up in an undefined room */
1420 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1427 * forget (Zap) the current room
1432 if (CtdlAccessCheck(ac_logged_in)) {
1436 if (CtdlForgetThisRoom() == 0) {
1437 cprintf("%d Ok\n", CIT_OK);
1440 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1445 * Get Next Unregistered User
1449 struct cdbdata *cdbus;
1450 struct ctdluser usbuf;
1452 if (CtdlAccessCheck(ac_aide)) {
1456 if ((CitControl.MMflags & MM_VALID) == 0) {
1457 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1461 /* There are unvalidated users. Traverse the user database,
1462 * and return the first user we find that needs validation.
1464 cdb_rewind(CDB_USERS);
1465 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1466 memset(&usbuf, 0, sizeof(struct ctdluser));
1467 memcpy(&usbuf, cdbus->ptr,
1468 ((cdbus->len > sizeof(struct ctdluser)) ?
1469 sizeof(struct ctdluser) : cdbus->len));
1471 if ((usbuf.flags & US_NEEDVALID)
1472 && (usbuf.axlevel > 0)) {
1473 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1474 cdb_close_cursor(CDB_USERS);
1479 /* If we get to this point, there are no more unvalidated users.
1480 * Therefore we clear the "users need validation" flag.
1483 begin_critical_section(S_CONTROL);
1485 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1487 end_critical_section(S_CONTROL);
1488 cprintf("%d *** End of registration.\n", CIT_OK);
1497 void cmd_vali(char *v_args)
1501 struct ctdluser userbuf;
1503 extract_token(user, v_args, 0, '|', sizeof user);
1504 newax = extract_int(v_args, 1);
1506 if (CtdlAccessCheck(ac_aide)) {
1510 if (lgetuser(&userbuf, user) != 0) {
1511 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1515 userbuf.axlevel = newax;
1516 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1520 /* If the access level was set to zero, delete the user */
1522 if (purge_user(user) == 0) {
1523 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1527 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1533 * Traverse the user file...
1535 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1538 struct ctdluser usbuf;
1539 struct cdbdata *cdbus;
1541 cdb_rewind(CDB_USERS);
1543 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1544 memset(&usbuf, 0, sizeof(struct ctdluser));
1545 memcpy(&usbuf, cdbus->ptr,
1546 ((cdbus->len > sizeof(struct ctdluser)) ?
1547 sizeof(struct ctdluser) : cdbus->len));
1549 (*CallBack) (&usbuf, in_data);
1555 * List one user (this works with cmd_list)
1557 void ListThisUser(struct ctdluser *usbuf, void *data)
1561 searchstring = (char *)data;
1562 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1566 if (usbuf->axlevel > 0) {
1567 if ((CC->user.axlevel >= 6)
1568 || ((usbuf->flags & US_UNLISTED) == 0)
1569 || ((CC->internal_pgm))) {
1570 cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1574 (long)usbuf->lastcall,
1577 if (CC->user.axlevel >= 6)
1578 cprintf("%s", usbuf->password);
1585 * List users (searchstring may be empty to list all users)
1587 void cmd_list(char *cmdbuf)
1589 char searchstring[256];
1590 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1591 striplt(searchstring);
1592 cprintf("%d \n", LISTING_FOLLOWS);
1593 ForEachUser(ListThisUser, (void *)searchstring );
1601 * assorted info we need to check at login
1609 if (CtdlAccessCheck(ac_logged_in)) {
1613 getuser(&CC->user, CC->curr_user); /* no lock is needed here */
1614 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1617 if (CC->user.axlevel >= 6) {
1619 if (CitControl.MMflags & MM_VALID)
1623 /* check for mail */
1624 mail = InitialMailCheck();
1626 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1631 * check to see if a user exists
1633 void cmd_qusr(char *who)
1635 struct ctdluser usbuf;
1637 if (getuser(&usbuf, who) == 0) {
1638 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1640 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1646 * Administrative Get User Parameters
1648 void cmd_agup(char *cmdbuf)
1650 struct ctdluser usbuf;
1651 char requested_user[128];
1653 if (CtdlAccessCheck(ac_aide)) {
1657 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1658 if (getuser(&usbuf, requested_user) != 0) {
1659 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1662 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1669 (int) usbuf.axlevel,
1671 (long)usbuf.lastcall,
1678 * Administrative Set User Parameters
1680 void cmd_asup(char *cmdbuf)
1682 struct ctdluser usbuf;
1683 char requested_user[128];
1689 if (CtdlAccessCheck(ac_aide))
1692 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1693 if (lgetuser(&usbuf, requested_user) != 0) {
1694 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1697 np = num_parms(cmdbuf);
1699 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1701 usbuf.flags = extract_int(cmdbuf, 2);
1703 usbuf.timescalled = extract_int(cmdbuf, 3);
1705 usbuf.posted = extract_int(cmdbuf, 4);
1707 newax = extract_int(cmdbuf, 5);
1708 if ((newax >= 0) && (newax <= 6)) {
1709 usbuf.axlevel = extract_int(cmdbuf, 5);
1713 usbuf.lastcall = extract_long(cmdbuf, 7);
1716 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1719 if (usbuf.axlevel == 0) {
1720 if (purge_user(requested_user) == 0) {
1726 snprintf(notify, SIZ,
1727 "User \"%s\" has been deleted by %s.\n",
1728 usbuf.fullname, CC->user.fullname);
1729 aide_message(notify, "User Deletion Message");
1732 cprintf("%d Ok", CIT_OK);
1734 cprintf(" (%s deleted)", requested_user);
1741 * Check to see if the user who we just sent mail to is logged in. If yes,
1742 * bump the 'new mail' counter for their session. That enables them to
1743 * receive a new mail notification without having to hit the database.
1745 void BumpNewMailCounter(long which_user) {
1746 struct CitContext *ptr;
1748 begin_critical_section(S_SESSION_TABLE);
1750 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1751 if (ptr->user.usernum == which_user) {
1756 end_critical_section(S_SESSION_TABLE);
1761 * Count the number of new mail messages the user has
1765 int num_newmsgs = 0;
1767 num_newmsgs = CC->newmail;
1770 return (num_newmsgs);
1775 * Count the number of new mail messages the user has
1777 int InitialMailCheck()
1779 int num_newmsgs = 0;
1781 char mailboxname[ROOMNAMELEN];
1782 struct ctdlroom mailbox;
1784 struct cdbdata *cdbfr;
1785 long *msglist = NULL;
1788 MailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1789 if (getroom(&mailbox, mailboxname) != 0)
1791 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1793 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1795 if (cdbfr != NULL) {
1796 msglist = malloc(cdbfr->len);
1797 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1798 num_msgs = cdbfr->len / sizeof(long);
1802 for (a = 0; a < num_msgs; ++a) {
1803 if (msglist[a] > 0L) {
1804 if (msglist[a] > vbuf.v_lastseen) {
1809 if (msglist != NULL)
1812 return (num_newmsgs);
1818 * Set the preferred view for the current user/room combination
1820 void cmd_view(char *cmdbuf) {
1824 if (CtdlAccessCheck(ac_logged_in)) {
1828 requested_view = extract_int(cmdbuf, 0);
1830 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1831 vbuf.v_view = requested_view;
1832 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1834 cprintf("%d ok\n", CIT_OK);
1841 void cmd_renu(char *cmdbuf)
1844 char oldname[USERNAME_SIZE];
1845 char newname[USERNAME_SIZE];
1847 if (CtdlAccessCheck(ac_aide)) {
1851 extract_token(oldname, cmdbuf, 0, '|', sizeof oldname);
1852 extract_token(newname, cmdbuf, 1, '|', sizeof newname);
1854 retcode = rename_user(oldname, newname);
1857 cprintf("%d '%s' has been renamed to '%s'.\n", CIT_OK, oldname, newname);
1859 case RENAMEUSER_LOGGED_IN:
1860 cprintf("%d '%s' is currently logged in and cannot be renamed.\n",
1861 ERROR + ALREADY_LOGGED_IN , oldname);
1863 case RENAMEUSER_NOT_FOUND:
1864 cprintf("%d '%s' does not exist.\n", ERROR + NO_SUCH_USER, oldname);
1866 case RENAMEUSER_ALREADY_EXISTS:
1867 cprintf("%d A user named '%s' already exists.\n", ERROR + ALREADY_EXISTS, newname);
1871 cprintf("%d An unknown error occurred.\n", ERROR);