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);
152 * Index-generating function used by Ctdl[Get|Set]Relationship
154 int GenerateRelationshipIndex(char *IndexBuf,
166 TheIndex.iRoomID = RoomID;
167 TheIndex.iRoomGen = RoomGen;
168 TheIndex.iUserID = UserID;
170 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
171 return (sizeof(TheIndex));
177 * Back end for CtdlSetRelationship()
179 void put_visit(struct visit *newvisit)
184 memset (IndexBuf, 0, sizeof (IndexBuf));
185 /* Generate an index */
186 IndexLen = GenerateRelationshipIndex(IndexBuf,
189 newvisit->v_usernum);
191 /* Store the record */
192 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
193 newvisit, sizeof(struct visit)
201 * Define a relationship between a user and a room
203 void CtdlSetRelationship(struct visit *newvisit,
204 struct ctdluser *rel_user,
205 struct ctdlroom *rel_room)
209 /* We don't use these in Citadel because they're implicit by the
210 * index, but they must be present if the database is exported.
212 newvisit->v_roomnum = rel_room->QRnumber;
213 newvisit->v_roomgen = rel_room->QRgen;
214 newvisit->v_usernum = rel_user->usernum;
220 * Locate a relationship between a user and a room
222 void CtdlGetRelationship(struct visit *vbuf,
223 struct ctdluser *rel_user,
224 struct ctdlroom *rel_room)
229 struct cdbdata *cdbvisit;
231 /* Generate an index */
232 IndexLen = GenerateRelationshipIndex(IndexBuf,
237 /* Clear out the buffer */
238 memset(vbuf, 0, sizeof(struct visit));
240 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
241 if (cdbvisit != NULL) {
242 memcpy(vbuf, cdbvisit->ptr,
243 ((cdbvisit->len > sizeof(struct visit)) ?
244 sizeof(struct visit) : cdbvisit->len));
248 /* If this is the first time the user has seen this room,
249 * set the view to be the default for the room.
251 vbuf->v_view = rel_room->QRdefaultview;
254 /* Set v_seen if necessary */
255 if (vbuf->v_seen[0] == 0) {
256 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
261 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
263 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
268 * Is the user currently logged in an Aide?
272 if (CC->user.axlevel >= 6)
280 * Is the user currently logged in an Aide *or* the room aide for this room?
282 int is_room_aide(void)
285 if (!CC->logged_in) {
289 if ((CC->user.axlevel >= 6)
290 || (CC->room.QRroomaide == CC->user.usernum)) {
298 * getuserbynumber() - get user by number
299 * returns 0 if user was found
301 * WARNING: don't use this function unless you absolutely have to. It does
302 * a sequential search and therefore is computationally expensive.
304 int getuserbynumber(struct ctdluser *usbuf, long int number)
306 struct cdbdata *cdbus;
308 cdb_rewind(CDB_USERS);
310 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
311 memset(usbuf, 0, sizeof(struct ctdluser));
312 memcpy(usbuf, cdbus->ptr,
313 ((cdbus->len > sizeof(struct ctdluser)) ?
314 sizeof(struct ctdluser) : cdbus->len));
316 if (usbuf->usernum == number) {
317 cdb_close_cursor(CDB_USERS);
326 * getuserbyuid() - get user by system uid (for PAM mode authentication)
327 * returns 0 if user was found
329 * WARNING: don't use this function unless you absolutely have to. It does
330 * a sequential search and therefore is computationally expensive.
332 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
334 struct cdbdata *cdbus;
336 cdb_rewind(CDB_USERS);
338 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
339 memset(usbuf, 0, sizeof(struct ctdluser));
340 memcpy(usbuf, cdbus->ptr,
341 ((cdbus->len > sizeof(struct ctdluser)) ?
342 sizeof(struct ctdluser) : cdbus->len));
344 if (usbuf->uid == number) {
345 cdb_close_cursor(CDB_USERS);
353 * Back end for cmd_user() and its ilk
355 * NOTE: "authname" should only be used if we are attempting to use the "master user" feature
357 int CtdlLoginExistingUser(char *authname, char *trythisname)
362 if ((CC->logged_in)) {
363 return login_already_logged_in;
366 if (trythisname == NULL) return login_not_found;
368 /* If a "master user" is defined, handle its authentication if specified */
370 if (strlen(config.c_master_user) > 0) if (strlen(config.c_master_pass) > 0) if (authname) {
371 if (!strcasecmp(authname, config.c_master_user)) {
376 /* Continue attempting user validation... */
377 safestrncpy(username, trythisname, USERNAME_SIZE);
380 if (IsEmptyStr(username)) {
381 return login_not_found;
384 if (config.c_auth_mode == AUTHMODE_HOST) {
389 struct passwd *tempPwdPtr;
392 lprintf(CTDL_DEBUG, "asking host about <%s>\n", username);
393 #ifdef HAVE_GETPWNAM_R
394 #ifdef SOLARIS_GETPWUID
395 lprintf(CTDL_DEBUG, "Calling getpwnam_r()\n");
396 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
397 #else // SOLARIS_GETPWUID
398 lprintf(CTDL_DEBUG, "Calling getpwnam_r()\n");
399 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
400 #endif // SOLARIS_GETPWUID
401 #else // HAVE_GETPWNAM_R
402 lprintf(CTDL_DEBUG, "SHOULD NEVER GET HERE!!!\n");
404 #endif // HAVE_GETPWNAM_R
405 if (tempPwdPtr == NULL) {
406 lprintf(CTDL_DEBUG, "no such user <%s>\n", username);
407 return login_not_found;
410 /* Locate the associated Citadel account.
411 * If not found, make one attempt to create it.
413 found_user = getuserbyuid(&CC->user, pd.pw_uid);
414 lprintf(CTDL_DEBUG, "found it: uid=%ld, gecos=%s here: %ld\n", (long)pd.pw_uid, pd.pw_gecos, found_user);
415 if (found_user != 0) {
416 create_user(username, 0);
417 found_user = getuserbyuid(&CC->user, pd.pw_uid);
423 /* native auth mode */
425 struct recptypes *valid = NULL;
427 /* First, try to log in as if the supplied name is a display name */
428 found_user = getuser(&CC->user, username);
430 /* If that didn't work, try to log in as if the supplied name
431 * is an e-mail address
433 if (found_user != 0) {
434 valid = validate_recipients(username, NULL, 0);
436 if (valid->num_local == 1) {
437 found_user = getuser(&CC->user, valid->recp_local);
439 free_recipients(valid);
444 /* Did we find something? */
445 if (found_user == 0) {
446 if (((CC->nologin)) && (CC->user.axlevel < 6)) {
447 return login_too_many_users;
449 safestrncpy(CC->curr_user, CC->user.fullname,
450 sizeof CC->curr_user);
454 return login_not_found;
462 void cmd_user(char *cmdbuf)
467 extract_token(username, cmdbuf, 0, '|', sizeof username);
470 a = CtdlLoginExistingUser(NULL, username);
472 case login_already_logged_in:
473 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
475 case login_too_many_users:
477 "Too many users are already online "
479 ERROR + MAX_SESSIONS_EXCEEDED,
480 config.c_nodename, config.c_maxsessions);
483 cprintf("%d Password required for %s\n",
484 MORE_DATA, CC->curr_user);
486 case login_not_found:
487 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
490 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
497 * session startup code which is common to both cmd_pass() and cmd_newu()
499 void session_startup(void)
503 lprintf(CTDL_NOTICE, "<%s> logged in\n", CC->curr_user);
505 lgetuser(&CC->user, CC->curr_user);
506 ++(CC->user.timescalled);
507 CC->previous_login = CC->user.lastcall;
508 time(&CC->user.lastcall);
510 /* If this user's name is the name of the system administrator
511 * (as specified in setup), automatically assign access level 6.
513 if (!strcasecmp(CC->user.fullname, config.c_sysadm)) {
514 CC->user.axlevel = 6;
517 /* If we're authenticating off the host system, automatically give
518 * root the highest level of access.
520 if (config.c_auth_mode == AUTHMODE_HOST) {
521 if (CC->user.uid == 0) {
522 CC->user.axlevel = 6;
529 * Populate CC->cs_inet_email with a default address. This will be
530 * overwritten with the user's directory address, if one exists, when
531 * the vCard module's login hook runs.
533 snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
534 CC->user.fullname, config.c_fqdn);
535 for (i=0; !IsEmptyStr(&CC->cs_inet_email[i]); ++i) {
536 if (isspace(CC->cs_inet_email[i])) {
537 CC->cs_inet_email[i] = '_';
541 /* Create any personal rooms required by the system.
542 * (Technically, MAILROOM should be there already, but just in case...)
544 create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
545 create_room(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
546 create_room(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
548 /* Run any startup routines registered by loadable modules */
549 PerformSessionHooks(EVT_LOGIN);
551 /* Enter the lobby */
552 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
556 void logged_in_response(void)
558 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
559 CIT_OK, CC->user.fullname, CC->user.axlevel,
560 CC->user.timescalled, CC->user.posted,
561 CC->user.flags, CC->user.usernum,
568 * misc things to be taken care of when a user is logged out
570 void logout(struct CitContext *who)
573 * Clear out some session data. Most likely, the CitContext for this
574 * session is about to get nuked when the session disconnects, but
575 * since it's possible to log in again without reconnecting, we cannot
576 * make that assumption.
578 strcpy(who->fake_username, "");
579 strcpy(who->fake_hostname, "");
580 strcpy(who->fake_roomname, "");
584 * If there is a download in progress, abort it.
586 if (who->download_fp != NULL) {
587 fclose(who->download_fp);
588 who->download_fp = NULL;
592 * If there is an upload in progress, abort it.
594 if (who->upload_fp != NULL) {
599 * If we were talking to a network node, we're not anymore...
601 if (!IsEmptyStr(who->net_node)) {
602 network_talking_to(who->net_node, NTT_REMOVE);
605 /* Do modular stuff... */
606 PerformSessionHooks(EVT_LOGOUT);
608 /* Free any output buffers */
609 if (who->output_buffer != NULL) {
615 * Validate a password on the host unix system by talking to the chkpwd daemon
617 static int validpw(uid_t uid, const char *pass)
621 lprintf(CTDL_DEBUG, "Validating password for uid=%d using chkpwd...\n", uid);
623 begin_critical_section(S_CHKPWD);
624 write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
625 write(chkpwd_write_pipe[1], pass, 256);
626 read(chkpwd_read_pipe[0], buf, 4);
627 end_critical_section(S_CHKPWD);
629 if (!strncmp(buf, "PASS", 4)) {
630 lprintf(CTDL_DEBUG, "...pass\n");
634 lprintf(CTDL_DEBUG, "...fail\n");
639 * Start up the chkpwd daemon so validpw() has something to talk to
641 void start_chkpwd_daemon(void) {
643 struct stat filestats;
646 lprintf(CTDL_DEBUG, "Starting chkpwd daemon for host authentication mode\n");
648 if ((stat(file_chkpwd, &filestats)==-1) ||
649 (filestats.st_size==0)){
650 printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
653 if (pipe(chkpwd_write_pipe) != 0) {
654 lprintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
657 if (pipe(chkpwd_read_pipe) != 0) {
658 lprintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
663 if (chkpwd_pid < 0) {
664 lprintf(CTDL_EMERG, "Unable to fork chkpwd daemon: %s\n", strerror(errno));
667 if (chkpwd_pid == 0) {
668 lprintf(CTDL_DEBUG, "Now calling dup2() write\n");
669 dup2(chkpwd_write_pipe[0], 0);
670 lprintf(CTDL_DEBUG, "Now calling dup2() write\n");
671 dup2(chkpwd_read_pipe[1], 1);
672 lprintf(CTDL_DEBUG, "Now closing stuff\n");
673 for (i=2; i<256; ++i) close(i);
674 lprintf(CTDL_DEBUG, "Now calling execl(%s)\n", file_chkpwd);
675 execl(file_chkpwd, file_chkpwd, NULL);
676 lprintf(CTDL_EMERG, "Unable to exec chkpwd daemon: %s\n", strerror(errno));
690 int CtdlTryPassword(char *password)
694 if ((CC->logged_in)) {
695 lprintf(CTDL_WARNING, "CtdlTryPassword: already logged in\n");
696 return pass_already_logged_in;
698 if (!strcmp(CC->curr_user, NLI)) {
699 lprintf(CTDL_WARNING, "CtdlTryPassword: no user selected\n");
702 if (getuser(&CC->user, CC->curr_user)) {
703 lprintf(CTDL_ERR, "CtdlTryPassword: internal error\n");
704 return pass_internal_error;
706 if (password == NULL) {
707 lprintf(CTDL_INFO, "CtdlTryPassword: NULL password string supplied\n");
708 return pass_wrong_password;
713 code = strcmp(password, config.c_master_pass);
716 else if (config.c_auth_mode == AUTHMODE_HOST) {
720 if (validpw(CC->user.uid, password)) {
724 * sooper-seekrit hack: populate the password field in the
725 * citadel database with the password that the user typed,
726 * if it's correct. This allows most sites to convert from
727 * host auth to native auth if they want to. If you think
728 * this is a security hazard, comment it out.
731 lgetuser(&CC->user, CC->curr_user);
732 safestrncpy(CC->user.password, password, sizeof CC->user.password);
736 * (sooper-seekrit hack ends here)
747 /* native auth mode */
750 strproc(CC->user.password);
751 code = strcasecmp(CC->user.password, password);
753 strproc(CC->user.password);
754 code = strcasecmp(CC->user.password, password);
761 lprintf(CTDL_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
762 return pass_wrong_password;
767 void cmd_pass(char *buf)
772 extract_token(password, buf, 0, '|', sizeof password);
773 a = CtdlTryPassword(password);
776 case pass_already_logged_in:
777 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
780 cprintf("%d You must send a name with USER first.\n",
781 ERROR + USERNAME_REQUIRED);
783 case pass_wrong_password:
784 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
787 logged_in_response();
795 * Delete a user record *and* all of its related resources.
797 int purge_user(char pname[])
800 struct ctdluser usbuf;
801 char usernamekey[USERNAME_SIZE];
802 struct CitContext *ccptr;
803 int user_is_logged_in = 0;
805 makeuserkey(usernamekey, pname);
807 if (getuser(&usbuf, pname) != 0) {
808 lprintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
809 return (ERROR + NO_SUCH_USER);
811 /* Don't delete a user who is currently logged in. Instead, just
812 * set the access level to 0, and let the account get swept up
813 * during the next purge.
815 user_is_logged_in = 0;
816 begin_critical_section(S_SESSION_TABLE);
817 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
818 if (ccptr->user.usernum == usbuf.usernum) {
819 user_is_logged_in = 1;
822 end_critical_section(S_SESSION_TABLE);
823 if (user_is_logged_in == 1) {
824 lprintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
829 lprintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
831 /* Perform any purge functions registered by server extensions */
832 PerformUserHooks(&usbuf, EVT_PURGEUSER);
834 /* delete any existing user/room relationships */
835 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
837 /* delete the userlog entry */
838 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
840 /* remove the user's bio file */
848 /* remove the user's picture */
861 * create_user() - back end processing to create a new user
863 * Set 'newusername' to the desired account name.
864 * Set 'become_user' to nonzero if this is self-service account creation and we want
865 * to actually log in as the user we just created, otherwise set it to 0.
867 int create_user(char *newusername, int become_user)
869 struct ctdluser usbuf;
870 struct ctdlroom qrbuf;
872 char mailboxname[ROOMNAMELEN];
876 safestrncpy(username, newusername, sizeof username);
879 if (config.c_auth_mode == AUTHMODE_HOST) {
884 struct passwd *tempPwdPtr;
887 #ifdef HAVE_GETPWNAM_R
888 #ifdef SOLARIS_GETPWUID
889 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof(pwdbuffer));
890 #else // SOLARIS_GETPWUID
891 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
892 #endif // SOLARIS_GETPWUID
893 #else // HAVE_GETPWNAM_R
895 #endif // HAVE_GETPWNAM_R
896 if (tempPwdPtr != NULL) {
897 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
899 if (IsEmptyStr (username))
902 "Can't find Realname for user %s [%d] in the Host Auth Database; giving up.\n",
903 newusername, pd.pw_uid);
905 "Can't find Realname for user %s [%d] in the Host Auth Database; giving up.\n",
906 newusername, pd.pw_uid);
907 aide_message(buf, "User Creation Failure Notice");
912 return (ERROR + NO_SUCH_USER);
916 if (!getuser(&usbuf, username)) {
917 return (ERROR + ALREADY_EXISTS);
920 /* Go ahead and initialize a new user record */
921 memset(&usbuf, 0, sizeof(struct ctdluser));
922 safestrncpy(usbuf.fullname, username, sizeof usbuf.fullname);
923 strcpy(usbuf.password, "");
926 /* These are the default flags on new accounts */
927 usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
929 usbuf.timescalled = 0;
931 usbuf.axlevel = config.c_initax;
932 usbuf.USscreenwidth = 80;
933 usbuf.USscreenheight = 24;
934 usbuf.lastcall = time(NULL);
936 /* fetch a new user number */
937 usbuf.usernum = get_new_user_number();
939 /* The very first user created on the system will always be an Aide */
940 if (usbuf.usernum == 1L) {
944 /* add user to userlog */
948 * Give the user a private mailbox and a configuration room.
949 * Make the latter an invisible system room.
951 MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
952 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
954 MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
955 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
956 if (lgetroom(&qrbuf, mailboxname) == 0) {
957 qrbuf.QRflags2 |= QR2_SYSTEM;
961 /* Perform any create functions registered by server extensions */
962 PerformUserHooks(&usbuf, EVT_NEWUSER);
964 /* Everything below this line can be bypassed if administratively
965 * creating a user, instead of doing self-service account creation
969 /* Now become the user we just created */
970 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
971 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
974 /* Check to make sure we're still who we think we are */
975 if (getuser(&CC->user, CC->curr_user)) {
976 return (ERROR + INTERNAL_ERROR);
981 "New user account <%s> has been created, from host %s [%s].\n",
986 aide_message(buf, "User Creation Notice");
987 lprintf(CTDL_NOTICE, "New user <%s> created\n", username);
995 * cmd_newu() - create a new user account and log in as that user
997 void cmd_newu(char *cmdbuf)
1002 if (config.c_auth_mode != AUTHMODE_NATIVE) {
1003 cprintf("%d This system does not use native mode authentication.\n",
1008 if (config.c_disable_newu) {
1009 cprintf("%d Self-service user account creation "
1010 "is disabled on this system.\n", ERROR + NOT_HERE);
1014 if (CC->logged_in) {
1015 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
1019 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
1020 ERROR + MAX_SESSIONS_EXCEEDED,
1021 config.c_nodename, config.c_maxsessions);
1023 extract_token(username, cmdbuf, 0, '|', sizeof username);
1027 if (IsEmptyStr(username)) {
1028 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1032 if ((!strcasecmp(username, "bbs")) ||
1033 (!strcasecmp(username, "new")) ||
1034 (!strcasecmp(username, "."))) {
1035 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
1039 a = create_user(username, 1);
1043 logged_in_response();
1044 } else if (a == ERROR + ALREADY_EXISTS) {
1045 cprintf("%d '%s' already exists.\n",
1046 ERROR + ALREADY_EXISTS, username);
1048 } else if (a == ERROR + INTERNAL_ERROR) {
1049 cprintf("%d Internal error - user record disappeared?\n",
1050 ERROR + INTERNAL_ERROR);
1053 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
1062 void cmd_setp(char *new_pw)
1064 if (CtdlAccessCheck(ac_logged_in)) {
1067 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
1068 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
1071 if (CC->is_master) {
1072 cprintf("%d The master prefix password cannot be changed with this command.\n",
1077 if (IsEmptyStr(new_pw)) {
1078 cprintf("%d Password unchanged.\n", CIT_OK);
1081 lgetuser(&CC->user, CC->curr_user);
1082 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1083 lputuser(&CC->user);
1084 cprintf("%d Password changed.\n", CIT_OK);
1085 lprintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
1086 PerformSessionHooks(EVT_SETPASS);
1091 * cmd_creu() - administratively create a new user account (do not log in to it)
1093 void cmd_creu(char *cmdbuf)
1098 struct ctdluser tmp;
1100 if (CtdlAccessCheck(ac_aide)) {
1104 extract_token(username, cmdbuf, 0, '|', sizeof username);
1105 extract_token(password, cmdbuf, 1, '|', sizeof password);
1111 if (IsEmptyStr(username)) {
1112 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1116 a = create_user(username, 0);
1119 if (!IsEmptyStr(password)) {
1120 lgetuser(&tmp, username);
1121 safestrncpy(tmp.password, password, sizeof(tmp.password));
1124 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1125 (!IsEmptyStr(password)) ? "and password set" :
1126 "with no password");
1128 } else if (a == ERROR + ALREADY_EXISTS) {
1129 cprintf("%d '%s' already exists.\n", ERROR + ALREADY_EXISTS, username);
1131 } else if ( (config.c_auth_mode != AUTHMODE_NATIVE) && (a == ERROR + NO_SUCH_USER) ) {
1132 cprintf("%d User accounts are not created within Citadel in host authentication mode.\n",
1133 ERROR + NO_SUCH_USER);
1136 cprintf("%d An error occurred creating the user account.\n", ERROR + INTERNAL_ERROR);
1143 * get user parameters
1148 if (CtdlAccessCheck(ac_logged_in))
1151 getuser(&CC->user, CC->curr_user);
1152 cprintf("%d %d|%d|%d|\n",
1154 CC->user.USscreenwidth,
1155 CC->user.USscreenheight,
1156 (CC->user.flags & US_USER_SET)
1161 * set user parameters
1163 void cmd_setu(char *new_parms)
1165 if (CtdlAccessCheck(ac_logged_in))
1168 if (num_parms(new_parms) < 3) {
1169 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1172 lgetuser(&CC->user, CC->curr_user);
1173 CC->user.USscreenwidth = extract_int(new_parms, 0);
1174 CC->user.USscreenheight = extract_int(new_parms, 1);
1175 CC->user.flags = CC->user.flags & (~US_USER_SET);
1176 CC->user.flags = CC->user.flags |
1177 (extract_int(new_parms, 2) & US_USER_SET);
1179 lputuser(&CC->user);
1180 cprintf("%d Ok\n", CIT_OK);
1184 * set last read pointer
1186 void cmd_slrp(char *new_ptr)
1190 struct visit original_vbuf;
1192 if (CtdlAccessCheck(ac_logged_in)) {
1196 if (!strncasecmp(new_ptr, "highest", 7)) {
1197 newlr = CC->room.QRhighest;
1199 newlr = atol(new_ptr);
1202 lgetuser(&CC->user, CC->curr_user);
1204 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1205 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1206 vbuf.v_lastseen = newlr;
1207 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1209 /* Only rewrite the record if it changed */
1210 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1211 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1212 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1215 lputuser(&CC->user);
1216 cprintf("%d %ld\n", CIT_OK, newlr);
1220 void cmd_seen(char *argbuf) {
1221 long target_msgnum = 0L;
1222 int target_setting = 0;
1224 if (CtdlAccessCheck(ac_logged_in)) {
1228 if (num_parms(argbuf) != 2) {
1229 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1233 target_msgnum = extract_long(argbuf, 0);
1234 target_setting = extract_int(argbuf, 1);
1236 CtdlSetSeen(&target_msgnum, 1, target_setting,
1237 ctdlsetseen_seen, NULL, NULL);
1238 cprintf("%d OK\n", CIT_OK);
1242 void cmd_gtsn(char *argbuf) {
1245 if (CtdlAccessCheck(ac_logged_in)) {
1249 CtdlGetSeen(buf, ctdlsetseen_seen);
1250 cprintf("%d %s\n", CIT_OK, buf);
1255 * API function for cmd_invt_kick() and anything else that needs to
1256 * invite or kick out a user to/from a room.
1258 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1260 int CtdlInvtKick(char *iuser, int op) {
1261 struct ctdluser USscratch;
1265 if (getuser(&USscratch, iuser) != 0) {
1269 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1271 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1272 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1275 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1276 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1278 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1280 /* post a message in Aide> saying what we just did */
1281 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1283 ((op == 1) ? "invited to" : "kicked out of"),
1286 aide_message(bbb,"User Admin Message");
1293 * INVT and KICK commands
1295 void cmd_invt_kick(char *iuser, int op) {
1298 * These commands are only allowed by aides, room aides,
1299 * and room namespace owners
1302 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1303 /* access granted */
1306 cprintf("%d Higher access or room ownership required.\n",
1307 ERROR + HIGHER_ACCESS_REQUIRED);
1311 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1313 cprintf("%d Can't add/remove users from this room.\n",
1318 if (CtdlInvtKick(iuser, op) != 0) {
1319 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1323 cprintf("%d %s %s %s.\n",
1325 ((op == 1) ? "invited to" : "kicked out of"),
1332 * Forget (Zap) the current room (API call)
1333 * Returns 0 on success
1335 int CtdlForgetThisRoom(void) {
1338 /* On some systems, Aides are not allowed to forget rooms */
1339 if (is_aide() && (config.c_aide_zap == 0)
1340 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1344 lgetuser(&CC->user, CC->curr_user);
1345 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1347 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1348 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1350 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1351 lputuser(&CC->user);
1353 /* Return to the Lobby, so we don't end up in an undefined room */
1354 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1361 * forget (Zap) the current room
1366 if (CtdlAccessCheck(ac_logged_in)) {
1370 if (CtdlForgetThisRoom() == 0) {
1371 cprintf("%d Ok\n", CIT_OK);
1374 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1379 * Get Next Unregistered User
1383 struct cdbdata *cdbus;
1384 struct ctdluser usbuf;
1386 if (CtdlAccessCheck(ac_aide)) {
1390 if ((CitControl.MMflags & MM_VALID) == 0) {
1391 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1395 /* There are unvalidated users. Traverse the user database,
1396 * and return the first user we find that needs validation.
1398 cdb_rewind(CDB_USERS);
1399 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1400 memset(&usbuf, 0, sizeof(struct ctdluser));
1401 memcpy(&usbuf, cdbus->ptr,
1402 ((cdbus->len > sizeof(struct ctdluser)) ?
1403 sizeof(struct ctdluser) : cdbus->len));
1405 if ((usbuf.flags & US_NEEDVALID)
1406 && (usbuf.axlevel > 0)) {
1407 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1408 cdb_close_cursor(CDB_USERS);
1413 /* If we get to this point, there are no more unvalidated users.
1414 * Therefore we clear the "users need validation" flag.
1417 begin_critical_section(S_CONTROL);
1419 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1421 end_critical_section(S_CONTROL);
1422 cprintf("%d *** End of registration.\n", CIT_OK);
1431 void cmd_vali(char *v_args)
1435 struct ctdluser userbuf;
1437 extract_token(user, v_args, 0, '|', sizeof user);
1438 newax = extract_int(v_args, 1);
1440 if (CtdlAccessCheck(ac_aide)) {
1444 if (lgetuser(&userbuf, user) != 0) {
1445 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1449 userbuf.axlevel = newax;
1450 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1454 /* If the access level was set to zero, delete the user */
1456 if (purge_user(user) == 0) {
1457 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1461 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1467 * Traverse the user file...
1469 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1472 struct ctdluser usbuf;
1473 struct cdbdata *cdbus;
1475 cdb_rewind(CDB_USERS);
1477 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1478 memset(&usbuf, 0, sizeof(struct ctdluser));
1479 memcpy(&usbuf, cdbus->ptr,
1480 ((cdbus->len > sizeof(struct ctdluser)) ?
1481 sizeof(struct ctdluser) : cdbus->len));
1483 (*CallBack) (&usbuf, in_data);
1489 * List one user (this works with cmd_list)
1491 void ListThisUser(struct ctdluser *usbuf, void *data)
1495 searchstring = (char *)data;
1496 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1500 if (usbuf->axlevel > 0) {
1501 if ((CC->user.axlevel >= 6)
1502 || ((usbuf->flags & US_UNLISTED) == 0)
1503 || ((CC->internal_pgm))) {
1504 cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1508 (long)usbuf->lastcall,
1511 if (CC->user.axlevel >= 6)
1512 cprintf("%s", usbuf->password);
1519 * List users (searchstring may be empty to list all users)
1521 void cmd_list(char *cmdbuf)
1523 char searchstring[256];
1524 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1525 striplt(searchstring);
1526 cprintf("%d \n", LISTING_FOLLOWS);
1527 ForEachUser(ListThisUser, (void *)searchstring );
1535 * assorted info we need to check at login
1543 if (CtdlAccessCheck(ac_logged_in)) {
1547 getuser(&CC->user, CC->curr_user); /* no lock is needed here */
1548 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1551 if (CC->user.axlevel >= 6) {
1553 if (CitControl.MMflags & MM_VALID)
1557 /* check for mail */
1558 mail = InitialMailCheck();
1560 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1565 * check to see if a user exists
1567 void cmd_qusr(char *who)
1569 struct ctdluser usbuf;
1571 if (getuser(&usbuf, who) == 0) {
1572 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1574 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1580 * Administrative Get User Parameters
1582 void cmd_agup(char *cmdbuf)
1584 struct ctdluser usbuf;
1585 char requested_user[128];
1587 if (CtdlAccessCheck(ac_aide)) {
1591 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1592 if (getuser(&usbuf, requested_user) != 0) {
1593 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1596 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1603 (int) usbuf.axlevel,
1605 (long)usbuf.lastcall,
1612 * Administrative Set User Parameters
1614 void cmd_asup(char *cmdbuf)
1616 struct ctdluser usbuf;
1617 char requested_user[128];
1623 if (CtdlAccessCheck(ac_aide))
1626 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1627 if (lgetuser(&usbuf, requested_user) != 0) {
1628 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1631 np = num_parms(cmdbuf);
1633 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1635 usbuf.flags = extract_int(cmdbuf, 2);
1637 usbuf.timescalled = extract_int(cmdbuf, 3);
1639 usbuf.posted = extract_int(cmdbuf, 4);
1641 newax = extract_int(cmdbuf, 5);
1642 if ((newax >= 0) && (newax <= 6)) {
1643 usbuf.axlevel = extract_int(cmdbuf, 5);
1647 usbuf.lastcall = extract_long(cmdbuf, 7);
1650 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1653 if (usbuf.axlevel == 0) {
1654 if (purge_user(requested_user) == 0) {
1660 sprintf(notify, "User \"%s\" has been deleted by %s.\n",
1661 usbuf.fullname, CC->user.fullname);
1662 aide_message(notify, "User Deletion Message");
1665 cprintf("%d Ok", CIT_OK);
1667 cprintf(" (%s deleted)", requested_user);
1674 * Check to see if the user who we just sent mail to is logged in. If yes,
1675 * bump the 'new mail' counter for their session. That enables them to
1676 * receive a new mail notification without having to hit the database.
1678 void BumpNewMailCounter(long which_user) {
1679 struct CitContext *ptr;
1681 begin_critical_section(S_SESSION_TABLE);
1683 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1684 if (ptr->user.usernum == which_user) {
1689 end_critical_section(S_SESSION_TABLE);
1694 * Count the number of new mail messages the user has
1698 int num_newmsgs = 0;
1700 num_newmsgs = CC->newmail;
1703 return (num_newmsgs);
1708 * Count the number of new mail messages the user has
1710 int InitialMailCheck()
1712 int num_newmsgs = 0;
1714 char mailboxname[ROOMNAMELEN];
1715 struct ctdlroom mailbox;
1717 struct cdbdata *cdbfr;
1718 long *msglist = NULL;
1721 MailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1722 if (getroom(&mailbox, mailboxname) != 0)
1724 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1726 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1728 if (cdbfr != NULL) {
1729 msglist = malloc(cdbfr->len);
1730 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1731 num_msgs = cdbfr->len / sizeof(long);
1735 for (a = 0; a < num_msgs; ++a) {
1736 if (msglist[a] > 0L) {
1737 if (msglist[a] > vbuf.v_lastseen) {
1742 if (msglist != NULL)
1745 return (num_newmsgs);
1751 * Set the preferred view for the current user/room combination
1753 void cmd_view(char *cmdbuf) {
1757 if (CtdlAccessCheck(ac_logged_in)) {
1761 requested_view = extract_int(cmdbuf, 0);
1763 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1764 vbuf.v_view = requested_view;
1765 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1767 cprintf("%d ok\n", CIT_OK);