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 CtdlLogPrintf (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. */
190 if (usbuf.usernum == 0)
192 CtdlLogPrintf (CTDL_DEBUG, "Can not rename user \"Citadel\".\n");
193 retcode = RENAMEUSER_NOT_FOUND;
195 CtdlLogPrintf(CTDL_DEBUG, "Renaming <%s> to <%s>\n", oldname, newname);
196 cdb_delete(CDB_USERS, oldnamekey, strlen(oldnamekey));
197 safestrncpy(usbuf.fullname, newname, sizeof usbuf.fullname);
199 retcode = RENAMEUSER_OK;
205 end_critical_section(S_USERS);
212 * Index-generating function used by Ctdl[Get|Set]Relationship
214 int GenerateRelationshipIndex(char *IndexBuf,
226 TheIndex.iRoomID = RoomID;
227 TheIndex.iRoomGen = RoomGen;
228 TheIndex.iUserID = UserID;
230 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
231 return (sizeof(TheIndex));
237 * Back end for CtdlSetRelationship()
239 void put_visit(struct visit *newvisit)
244 memset (IndexBuf, 0, sizeof (IndexBuf));
245 /* Generate an index */
246 IndexLen = GenerateRelationshipIndex(IndexBuf,
249 newvisit->v_usernum);
251 /* Store the record */
252 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
253 newvisit, sizeof(struct visit)
261 * Define a relationship between a user and a room
263 void CtdlSetRelationship(struct visit *newvisit,
264 struct ctdluser *rel_user,
265 struct ctdlroom *rel_room)
269 /* We don't use these in Citadel because they're implicit by the
270 * index, but they must be present if the database is exported.
272 newvisit->v_roomnum = rel_room->QRnumber;
273 newvisit->v_roomgen = rel_room->QRgen;
274 newvisit->v_usernum = rel_user->usernum;
280 * Locate a relationship between a user and a room
282 void CtdlGetRelationship(struct visit *vbuf,
283 struct ctdluser *rel_user,
284 struct ctdlroom *rel_room)
289 struct cdbdata *cdbvisit;
291 /* Generate an index */
292 IndexLen = GenerateRelationshipIndex(IndexBuf,
297 /* Clear out the buffer */
298 memset(vbuf, 0, sizeof(struct visit));
300 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
301 if (cdbvisit != NULL) {
302 memcpy(vbuf, cdbvisit->ptr,
303 ((cdbvisit->len > sizeof(struct visit)) ?
304 sizeof(struct visit) : cdbvisit->len));
308 /* If this is the first time the user has seen this room,
309 * set the view to be the default for the room.
311 vbuf->v_view = rel_room->QRdefaultview;
314 /* Set v_seen if necessary */
315 if (vbuf->v_seen[0] == 0) {
316 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
321 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
323 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
328 * Is the user currently logged in an Aide?
332 if (CC->user.axlevel >= 6)
340 * Is the user currently logged in an Aide *or* the room aide for this room?
342 int is_room_aide(void)
345 if (!CC->logged_in) {
349 if ((CC->user.axlevel >= 6)
350 || (CC->room.QRroomaide == CC->user.usernum)) {
358 * getuserbynumber() - get user by number
359 * returns 0 if user was found
361 * WARNING: don't use this function unless you absolutely have to. It does
362 * a sequential search and therefore is computationally expensive.
364 int getuserbynumber(struct ctdluser *usbuf, long int number)
366 struct cdbdata *cdbus;
368 cdb_rewind(CDB_USERS);
370 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
371 memset(usbuf, 0, sizeof(struct ctdluser));
372 memcpy(usbuf, cdbus->ptr,
373 ((cdbus->len > sizeof(struct ctdluser)) ?
374 sizeof(struct ctdluser) : cdbus->len));
376 if (usbuf->usernum == number) {
377 cdb_close_cursor(CDB_USERS);
386 * getuserbyuid() - get user by system uid (for PAM mode authentication)
387 * returns 0 if user was found
389 * WARNING: don't use this function unless you absolutely have to. It does
390 * a sequential search and therefore is computationally expensive.
392 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
394 struct cdbdata *cdbus;
396 cdb_rewind(CDB_USERS);
398 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
399 memset(usbuf, 0, sizeof(struct ctdluser));
400 memcpy(usbuf, cdbus->ptr,
401 ((cdbus->len > sizeof(struct ctdluser)) ?
402 sizeof(struct ctdluser) : cdbus->len));
404 if (usbuf->uid == number) {
405 cdb_close_cursor(CDB_USERS);
413 * Back end for cmd_user() and its ilk
415 * NOTE: "authname" should only be used if we are attempting to use the "master user" feature
417 int CtdlLoginExistingUser(char *authname, char *trythisname)
422 CtdlLogPrintf(9, "CtdlLoginExistingUser(%s, %s)\n", authname, trythisname);
424 if ((CC->logged_in)) {
425 return login_already_logged_in;
428 if (trythisname == NULL) return login_not_found;
430 if (!strcasecmp(trythisname, "Citadel"))
432 CtdlLogPrintf(CTDL_DEBUG, "System user \"Citadel\" is not allowed to log in.\n");
433 return login_not_found;
436 /* If a "master user" is defined, handle its authentication if specified */
438 if (strlen(config.c_master_user) > 0) if (strlen(config.c_master_pass) > 0) if (authname) {
439 if (!strcasecmp(authname, config.c_master_user)) {
444 /* Continue attempting user validation... */
445 safestrncpy(username, trythisname, USERNAME_SIZE);
448 if (IsEmptyStr(username)) {
449 return login_not_found;
452 if (config.c_auth_mode == AUTHMODE_HOST) {
457 struct passwd *tempPwdPtr;
460 CtdlLogPrintf(CTDL_DEBUG, "asking host about <%s>\n", username);
461 #ifdef HAVE_GETPWNAM_R
462 #ifdef SOLARIS_GETPWUID
463 CtdlLogPrintf(CTDL_DEBUG, "Calling getpwnam_r()\n");
464 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
465 #else // SOLARIS_GETPWUID
466 CtdlLogPrintf(CTDL_DEBUG, "Calling getpwnam_r()\n");
467 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
468 #endif // SOLARIS_GETPWUID
469 #else // HAVE_GETPWNAM_R
470 CtdlLogPrintf(CTDL_DEBUG, "SHOULD NEVER GET HERE!!!\n");
472 #endif // HAVE_GETPWNAM_R
473 if (tempPwdPtr == NULL) {
474 CtdlLogPrintf(CTDL_DEBUG, "no such user <%s>\n", username);
475 return login_not_found;
478 /* Locate the associated Citadel account.
479 * If not found, make one attempt to create it.
481 found_user = getuserbyuid(&CC->user, pd.pw_uid);
482 CtdlLogPrintf(CTDL_DEBUG, "found it: uid=%ld, gecos=%s here: %d\n",
483 (long)pd.pw_uid, pd.pw_gecos, found_user);
484 if (found_user != 0) {
485 create_user(username, 0);
486 found_user = getuserbyuid(&CC->user, pd.pw_uid);
492 /* native auth mode */
494 struct recptypes *valid = NULL;
496 /* First, try to log in as if the supplied name is a display name */
497 found_user = getuser(&CC->user, username);
499 /* If that didn't work, try to log in as if the supplied name
500 * is an e-mail address
502 if (found_user != 0) {
503 valid = validate_recipients(username, NULL, 0);
505 if (valid->num_local == 1) {
506 found_user = getuser(&CC->user, valid->recp_local);
508 free_recipients(valid);
513 /* Did we find something? */
514 if (found_user == 0) {
515 if (((CC->nologin)) && (CC->user.axlevel < 6)) {
516 return login_too_many_users;
518 safestrncpy(CC->curr_user, CC->user.fullname,
519 sizeof CC->curr_user);
523 return login_not_found;
531 void cmd_user(char *cmdbuf)
536 extract_token(username, cmdbuf, 0, '|', sizeof username);
539 a = CtdlLoginExistingUser(NULL, username);
541 case login_already_logged_in:
542 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
544 case login_too_many_users:
546 "Too many users are already online "
548 ERROR + MAX_SESSIONS_EXCEEDED,
549 config.c_nodename, config.c_maxsessions);
552 cprintf("%d Password required for %s\n",
553 MORE_DATA, CC->curr_user);
555 case login_not_found:
556 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
559 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
566 * session startup code which is common to both cmd_pass() and cmd_newu()
568 void session_startup(void)
570 CtdlLogPrintf(CTDL_NOTICE, "<%s> logged in\n", CC->curr_user);
572 lgetuser(&CC->user, CC->curr_user);
573 ++(CC->user.timescalled);
574 CC->previous_login = CC->user.lastcall;
575 time(&CC->user.lastcall);
577 /* If this user's name is the name of the system administrator
578 * (as specified in setup), automatically assign access level 6.
580 if (!strcasecmp(CC->user.fullname, config.c_sysadm)) {
581 CC->user.axlevel = 6;
584 /* If we're authenticating off the host system, automatically give
585 * root the highest level of access.
587 if (config.c_auth_mode == AUTHMODE_HOST) {
588 if (CC->user.uid == 0) {
589 CC->user.axlevel = 6;
596 * Populate CC->cs_inet_email with a default address. This will be
597 * overwritten with the user's directory address, if one exists, when
598 * the vCard module's login hook runs.
600 snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
601 CC->user.fullname, config.c_fqdn);
602 convert_spaces_to_underscores(CC->cs_inet_email);
604 /* Create any personal rooms required by the system.
605 * (Technically, MAILROOM should be there already, but just in case...)
607 create_room(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
608 create_room(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
609 create_room(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
611 /* Run any startup routines registered by loadable modules */
612 PerformSessionHooks(EVT_LOGIN);
614 /* Enter the lobby */
615 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
619 void logged_in_response(void)
621 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
622 CIT_OK, CC->user.fullname, CC->user.axlevel,
623 CC->user.timescalled, CC->user.posted,
624 CC->user.flags, CC->user.usernum,
631 * misc things to be taken care of when a user is logged out
635 struct CitContext *CCC = CC; /* CachedCitContext - performance boost */
637 * If there is a download in progress, abort it.
639 if (CCC->download_fp != NULL) {
640 fclose(CCC->download_fp);
641 CCC->download_fp = NULL;
645 * If there is an upload in progress, abort it.
647 if (CCC->upload_fp != NULL) {
652 * If we were talking to a network node, we're not anymore...
654 if (!IsEmptyStr(CCC->net_node)) {
655 network_talking_to(CCC->net_node, NTT_REMOVE);
658 /* Run any hooks registered by modules... */
659 PerformSessionHooks(EVT_LOGOUT);
662 * Clear out some session data. Most likely, the CitContext for this
663 * session is about to get nuked when the session disconnects, but
664 * since it's possible to log in again without reconnecting, we cannot
665 * make that assumption.
667 strcpy(CCC->fake_username, "");
668 strcpy(CCC->fake_hostname, "");
669 strcpy(CCC->fake_roomname, "");
672 /* Check to see if the user was deleted whilst logged in and purge them if necessary */
673 if (CCC->user.axlevel == 0)
674 purge_user(CCC->user.fullname);
676 /* Free any output buffers */
677 if (CCC->output_buffer != NULL) {
683 * Validate a password on the host unix system by talking to the chkpwd daemon
685 static int validpw(uid_t uid, const char *pass)
689 if (IsEmptyStr(pass)) {
690 CtdlLogPrintf(CTDL_DEBUG, "refusing to check empty password for uid=%d using chkpwd...\n", uid);
694 CtdlLogPrintf(CTDL_DEBUG, "Validating password for uid=%d using chkpwd...\n", uid);
696 begin_critical_section(S_CHKPWD);
697 write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
698 write(chkpwd_write_pipe[1], pass, 256);
699 read(chkpwd_read_pipe[0], buf, 4);
700 end_critical_section(S_CHKPWD);
702 if (!strncmp(buf, "PASS", 4)) {
703 CtdlLogPrintf(CTDL_DEBUG, "...pass\n");
707 CtdlLogPrintf(CTDL_DEBUG, "...fail\n");
712 * Start up the chkpwd daemon so validpw() has something to talk to
714 void start_chkpwd_daemon(void) {
716 struct stat filestats;
719 CtdlLogPrintf(CTDL_DEBUG, "Starting chkpwd daemon for host authentication mode\n");
721 if ((stat(file_chkpwd, &filestats)==-1) ||
722 (filestats.st_size==0)){
723 printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
726 if (pipe(chkpwd_write_pipe) != 0) {
727 CtdlLogPrintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
730 if (pipe(chkpwd_read_pipe) != 0) {
731 CtdlLogPrintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
736 if (chkpwd_pid < 0) {
737 CtdlLogPrintf(CTDL_EMERG, "Unable to fork chkpwd daemon: %s\n", strerror(errno));
740 if (chkpwd_pid == 0) {
741 CtdlLogPrintf(CTDL_DEBUG, "Now calling dup2() write\n");
742 dup2(chkpwd_write_pipe[0], 0);
743 CtdlLogPrintf(CTDL_DEBUG, "Now calling dup2() write\n");
744 dup2(chkpwd_read_pipe[1], 1);
745 CtdlLogPrintf(CTDL_DEBUG, "Now closing stuff\n");
746 for (i=2; i<256; ++i) close(i);
747 CtdlLogPrintf(CTDL_DEBUG, "Now calling execl(%s)\n", file_chkpwd);
748 execl(file_chkpwd, file_chkpwd, NULL);
749 CtdlLogPrintf(CTDL_EMERG, "Unable to exec chkpwd daemon: %s\n", strerror(errno));
763 int CtdlTryPassword(char *password)
767 if ((CC->logged_in)) {
768 CtdlLogPrintf(CTDL_WARNING, "CtdlTryPassword: already logged in\n");
769 return pass_already_logged_in;
771 if (!strcmp(CC->curr_user, NLI)) {
772 CtdlLogPrintf(CTDL_WARNING, "CtdlTryPassword: no user selected\n");
775 if (getuser(&CC->user, CC->curr_user)) {
776 CtdlLogPrintf(CTDL_ERR, "CtdlTryPassword: internal error\n");
777 return pass_internal_error;
779 if (password == NULL) {
780 CtdlLogPrintf(CTDL_INFO, "CtdlTryPassword: NULL password string supplied\n");
781 return pass_wrong_password;
786 code = strcmp(password, config.c_master_pass);
789 else if (config.c_auth_mode == AUTHMODE_HOST) {
793 if (validpw(CC->user.uid, password)) {
797 * sooper-seekrit hack: populate the password field in the
798 * citadel database with the password that the user typed,
799 * if it's correct. This allows most sites to convert from
800 * host auth to native auth if they want to. If you think
801 * this is a security hazard, comment it out.
804 lgetuser(&CC->user, CC->curr_user);
805 safestrncpy(CC->user.password, password, sizeof CC->user.password);
809 * (sooper-seekrit hack ends here)
820 /* native auth mode */
823 strproc(CC->user.password);
824 code = strcasecmp(CC->user.password, password);
826 strproc(CC->user.password);
827 code = strcasecmp(CC->user.password, password);
834 CtdlLogPrintf(CTDL_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
835 return pass_wrong_password;
840 void cmd_pass(char *buf)
845 extract_token(password, buf, 0, '|', sizeof password);
846 a = CtdlTryPassword(password);
849 case pass_already_logged_in:
850 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
853 cprintf("%d You must send a name with USER first.\n",
854 ERROR + USERNAME_REQUIRED);
856 case pass_wrong_password:
857 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
860 logged_in_response();
868 * Delete a user record *and* all of its related resources.
870 int purge_user(char pname[])
873 struct ctdluser usbuf;
874 char usernamekey[USERNAME_SIZE];
875 struct CitContext *ccptr;
876 int user_is_logged_in = 0;
878 makeuserkey(usernamekey, pname);
880 /* If the name is empty we can't find them in the DB any way so just return */
881 if (IsEmptyStr(pname))
882 return (ERROR + NO_SUCH_USER);
884 if (getuser(&usbuf, pname) != 0) {
885 CtdlLogPrintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
886 return (ERROR + NO_SUCH_USER);
888 /* Don't delete a user who is currently logged in. Instead, just
889 * set the access level to 0, and let the account get swept up
890 * during the next purge.
892 user_is_logged_in = 0;
893 begin_critical_section(S_SESSION_TABLE);
894 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
895 if (ccptr->user.usernum == usbuf.usernum) {
896 user_is_logged_in = 1;
899 end_critical_section(S_SESSION_TABLE);
900 if (user_is_logged_in == 1) {
901 CtdlLogPrintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
906 CtdlLogPrintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
908 /* Perform any purge functions registered by server extensions */
909 PerformUserHooks(&usbuf, EVT_PURGEUSER);
911 /* delete any existing user/room relationships */
912 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
914 /* delete the userlog entry */
915 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
917 /* remove the user's bio file */
925 /* remove the user's picture */
938 * create_user() - back end processing to create a new user
940 * Set 'newusername' to the desired account name.
941 * Set 'become_user' to nonzero if this is self-service account creation and we want
942 * to actually log in as the user we just created, otherwise set it to 0.
944 int create_user(char *newusername, int become_user)
946 struct ctdluser usbuf;
947 struct ctdlroom qrbuf;
949 char mailboxname[ROOMNAMELEN];
953 safestrncpy(username, newusername, sizeof username);
956 if (config.c_auth_mode == AUTHMODE_HOST) {
961 struct passwd *tempPwdPtr;
964 #ifdef HAVE_GETPWNAM_R
965 #ifdef SOLARIS_GETPWUID
966 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof(pwdbuffer));
967 #else // SOLARIS_GETPWUID
968 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
969 #endif // SOLARIS_GETPWUID
970 #else // HAVE_GETPWNAM_R
972 #endif // HAVE_GETPWNAM_R
973 if (tempPwdPtr != NULL) {
974 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
976 if (IsEmptyStr (username))
978 CtdlLogPrintf (CTDL_EMERG,
979 "Can't find Realname for user %s [%d] in the Host Auth Database; giving up.\n",
980 newusername, pd.pw_uid);
982 "Can't find Realname for user %s [%d] in the Host Auth Database; giving up.\n",
983 newusername, pd.pw_uid);
984 aide_message(buf, "User Creation Failure Notice");
989 return (ERROR + NO_SUCH_USER);
993 if (!getuser(&usbuf, username)) {
994 return (ERROR + ALREADY_EXISTS);
997 /* Go ahead and initialize a new user record */
998 memset(&usbuf, 0, sizeof(struct ctdluser));
999 safestrncpy(usbuf.fullname, username, sizeof usbuf.fullname);
1000 strcpy(usbuf.password, "");
1003 /* These are the default flags on new accounts */
1004 usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
1006 usbuf.timescalled = 0;
1008 usbuf.axlevel = config.c_initax;
1009 usbuf.USscreenwidth = 80;
1010 usbuf.USscreenheight = 24;
1011 usbuf.lastcall = time(NULL);
1013 /* fetch a new user number */
1014 usbuf.usernum = get_new_user_number();
1016 /* The very first user created on the system will always be an Aide */
1017 if (usbuf.usernum == 1L) {
1021 /* add user to userlog */
1025 * Give the user a private mailbox and a configuration room.
1026 * Make the latter an invisible system room.
1028 MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
1029 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
1031 MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
1032 create_room(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
1033 if (lgetroom(&qrbuf, mailboxname) == 0) {
1034 qrbuf.QRflags2 |= QR2_SYSTEM;
1038 /* Perform any create functions registered by server extensions */
1039 PerformUserHooks(&usbuf, EVT_NEWUSER);
1041 /* Everything below this line can be bypassed if administratively
1042 * creating a user, instead of doing self-service account creation
1046 /* Now become the user we just created */
1047 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
1048 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
1051 /* Check to make sure we're still who we think we are */
1052 if (getuser(&CC->user, CC->curr_user)) {
1053 return (ERROR + INTERNAL_ERROR);
1058 "New user account <%s> has been created, from host %s [%s].\n",
1063 aide_message(buf, "User Creation Notice");
1064 CtdlLogPrintf(CTDL_NOTICE, "New user <%s> created\n", username);
1072 * cmd_newu() - create a new user account and log in as that user
1074 void cmd_newu(char *cmdbuf)
1079 if (config.c_auth_mode != AUTHMODE_NATIVE) {
1080 cprintf("%d This system does not use native mode authentication.\n",
1085 if (config.c_disable_newu) {
1086 cprintf("%d Self-service user account creation "
1087 "is disabled on this system.\n", ERROR + NOT_HERE);
1091 if (CC->logged_in) {
1092 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
1096 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
1097 ERROR + MAX_SESSIONS_EXCEEDED,
1098 config.c_nodename, config.c_maxsessions);
1100 extract_token(username, cmdbuf, 0, '|', sizeof username);
1104 if (IsEmptyStr(username)) {
1105 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1109 if ((!strcasecmp(username, "bbs")) ||
1110 (!strcasecmp(username, "new")) ||
1111 (!strcasecmp(username, "."))) {
1112 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
1116 a = create_user(username, 1);
1120 logged_in_response();
1121 } else if (a == ERROR + ALREADY_EXISTS) {
1122 cprintf("%d '%s' already exists.\n",
1123 ERROR + ALREADY_EXISTS, username);
1125 } else if (a == ERROR + INTERNAL_ERROR) {
1126 cprintf("%d Internal error - user record disappeared?\n",
1127 ERROR + INTERNAL_ERROR);
1130 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
1139 void cmd_setp(char *new_pw)
1141 if (CtdlAccessCheck(ac_logged_in)) {
1144 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
1145 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
1148 if (CC->is_master) {
1149 cprintf("%d The master prefix password cannot be changed with this command.\n",
1154 if (IsEmptyStr(new_pw)) {
1155 cprintf("%d Password unchanged.\n", CIT_OK);
1158 lgetuser(&CC->user, CC->curr_user);
1159 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1160 lputuser(&CC->user);
1161 cprintf("%d Password changed.\n", CIT_OK);
1162 CtdlLogPrintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
1163 PerformSessionHooks(EVT_SETPASS);
1168 * cmd_creu() - administratively create a new user account (do not log in to it)
1170 void cmd_creu(char *cmdbuf)
1175 struct ctdluser tmp;
1177 if (CtdlAccessCheck(ac_aide)) {
1181 extract_token(username, cmdbuf, 0, '|', sizeof username);
1182 extract_token(password, cmdbuf, 1, '|', sizeof password);
1188 if (IsEmptyStr(username)) {
1189 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1193 a = create_user(username, 0);
1196 if (!IsEmptyStr(password)) {
1197 lgetuser(&tmp, username);
1198 safestrncpy(tmp.password, password, sizeof(tmp.password));
1201 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1202 (!IsEmptyStr(password)) ? "and password set" :
1203 "with no password");
1205 } else if (a == ERROR + ALREADY_EXISTS) {
1206 cprintf("%d '%s' already exists.\n", ERROR + ALREADY_EXISTS, username);
1208 } else if ( (config.c_auth_mode != AUTHMODE_NATIVE) && (a == ERROR + NO_SUCH_USER) ) {
1209 cprintf("%d User accounts are not created within Citadel in host authentication mode.\n",
1210 ERROR + NO_SUCH_USER);
1213 cprintf("%d An error occurred creating the user account.\n", ERROR + INTERNAL_ERROR);
1220 * get user parameters
1225 if (CtdlAccessCheck(ac_logged_in))
1228 getuser(&CC->user, CC->curr_user);
1229 cprintf("%d %d|%d|%d|\n",
1231 CC->user.USscreenwidth,
1232 CC->user.USscreenheight,
1233 (CC->user.flags & US_USER_SET)
1238 * set user parameters
1240 void cmd_setu(char *new_parms)
1242 if (CtdlAccessCheck(ac_logged_in))
1245 if (num_parms(new_parms) < 3) {
1246 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1249 lgetuser(&CC->user, CC->curr_user);
1250 CC->user.USscreenwidth = extract_int(new_parms, 0);
1251 CC->user.USscreenheight = extract_int(new_parms, 1);
1252 CC->user.flags = CC->user.flags & (~US_USER_SET);
1253 CC->user.flags = CC->user.flags |
1254 (extract_int(new_parms, 2) & US_USER_SET);
1256 lputuser(&CC->user);
1257 cprintf("%d Ok\n", CIT_OK);
1261 * set last read pointer
1263 void cmd_slrp(char *new_ptr)
1267 struct visit original_vbuf;
1269 if (CtdlAccessCheck(ac_logged_in)) {
1273 if (!strncasecmp(new_ptr, "highest", 7)) {
1274 newlr = CC->room.QRhighest;
1276 newlr = atol(new_ptr);
1279 lgetuser(&CC->user, CC->curr_user);
1281 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1282 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1283 vbuf.v_lastseen = newlr;
1284 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1286 /* Only rewrite the record if it changed */
1287 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1288 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1289 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1292 lputuser(&CC->user);
1293 cprintf("%d %ld\n", CIT_OK, newlr);
1297 void cmd_seen(char *argbuf) {
1298 long target_msgnum = 0L;
1299 int target_setting = 0;
1301 if (CtdlAccessCheck(ac_logged_in)) {
1305 if (num_parms(argbuf) != 2) {
1306 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1310 target_msgnum = extract_long(argbuf, 0);
1311 target_setting = extract_int(argbuf, 1);
1313 CtdlSetSeen(&target_msgnum, 1, target_setting,
1314 ctdlsetseen_seen, NULL, NULL);
1315 cprintf("%d OK\n", CIT_OK);
1319 void cmd_gtsn(char *argbuf) {
1322 if (CtdlAccessCheck(ac_logged_in)) {
1326 CtdlGetSeen(buf, ctdlsetseen_seen);
1327 cprintf("%d %s\n", CIT_OK, buf);
1332 * API function for cmd_invt_kick() and anything else that needs to
1333 * invite or kick out a user to/from a room.
1335 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1337 int CtdlInvtKick(char *iuser, int op) {
1338 struct ctdluser USscratch;
1342 if (getuser(&USscratch, iuser) != 0) {
1346 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1348 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1349 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1352 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1353 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1355 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1357 /* post a message in Aide> saying what we just did */
1358 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1360 ((op == 1) ? "invited to" : "kicked out of"),
1363 aide_message(bbb,"User Admin Message");
1370 * INVT and KICK commands
1372 void cmd_invt_kick(char *iuser, int op) {
1375 * These commands are only allowed by aides, room aides,
1376 * and room namespace owners
1379 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1380 /* access granted */
1383 cprintf("%d Higher access or room ownership required.\n",
1384 ERROR + HIGHER_ACCESS_REQUIRED);
1388 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1390 cprintf("%d Can't add/remove users from this room.\n",
1395 if (CtdlInvtKick(iuser, op) != 0) {
1396 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1400 cprintf("%d %s %s %s.\n",
1402 ((op == 1) ? "invited to" : "kicked out of"),
1409 * Forget (Zap) the current room (API call)
1410 * Returns 0 on success
1412 int CtdlForgetThisRoom(void) {
1415 /* On some systems, Aides are not allowed to forget rooms */
1416 if (is_aide() && (config.c_aide_zap == 0)
1417 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1421 lgetuser(&CC->user, CC->curr_user);
1422 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1424 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1425 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1427 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1428 lputuser(&CC->user);
1430 /* Return to the Lobby, so we don't end up in an undefined room */
1431 usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1438 * forget (Zap) the current room
1443 if (CtdlAccessCheck(ac_logged_in)) {
1447 if (CtdlForgetThisRoom() == 0) {
1448 cprintf("%d Ok\n", CIT_OK);
1451 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1456 * Get Next Unregistered User
1460 struct cdbdata *cdbus;
1461 struct ctdluser usbuf;
1463 if (CtdlAccessCheck(ac_aide)) {
1467 if ((CitControl.MMflags & MM_VALID) == 0) {
1468 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1472 /* There are unvalidated users. Traverse the user database,
1473 * and return the first user we find that needs validation.
1475 cdb_rewind(CDB_USERS);
1476 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1477 memset(&usbuf, 0, sizeof(struct ctdluser));
1478 memcpy(&usbuf, cdbus->ptr,
1479 ((cdbus->len > sizeof(struct ctdluser)) ?
1480 sizeof(struct ctdluser) : cdbus->len));
1482 if ((usbuf.flags & US_NEEDVALID)
1483 && (usbuf.axlevel > 0)) {
1484 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1485 cdb_close_cursor(CDB_USERS);
1490 /* If we get to this point, there are no more unvalidated users.
1491 * Therefore we clear the "users need validation" flag.
1494 begin_critical_section(S_CONTROL);
1496 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1498 end_critical_section(S_CONTROL);
1499 cprintf("%d *** End of registration.\n", CIT_OK);
1508 void cmd_vali(char *v_args)
1512 struct ctdluser userbuf;
1514 extract_token(user, v_args, 0, '|', sizeof user);
1515 newax = extract_int(v_args, 1);
1517 if (CtdlAccessCheck(ac_aide)) {
1521 if (lgetuser(&userbuf, user) != 0) {
1522 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1526 userbuf.axlevel = newax;
1527 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1531 /* If the access level was set to zero, delete the user */
1533 if (purge_user(user) == 0) {
1534 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1538 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1544 * Traverse the user file...
1546 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1549 struct ctdluser usbuf;
1550 struct cdbdata *cdbus;
1552 cdb_rewind(CDB_USERS);
1554 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1555 memset(&usbuf, 0, sizeof(struct ctdluser));
1556 memcpy(&usbuf, cdbus->ptr,
1557 ((cdbus->len > sizeof(struct ctdluser)) ?
1558 sizeof(struct ctdluser) : cdbus->len));
1560 (*CallBack) (&usbuf, in_data);
1566 * List one user (this works with cmd_list)
1568 void ListThisUser(struct ctdluser *usbuf, void *data)
1572 searchstring = (char *)data;
1573 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1577 if (usbuf->axlevel > 0) {
1578 if ((CC->user.axlevel >= 6)
1579 || ((usbuf->flags & US_UNLISTED) == 0)
1580 || ((CC->internal_pgm))) {
1581 cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1585 (long)usbuf->lastcall,
1588 if (CC->user.axlevel >= 6)
1589 cprintf("%s", usbuf->password);
1596 * List users (searchstring may be empty to list all users)
1598 void cmd_list(char *cmdbuf)
1600 char searchstring[256];
1601 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1602 striplt(searchstring);
1603 cprintf("%d \n", LISTING_FOLLOWS);
1604 ForEachUser(ListThisUser, (void *)searchstring );
1612 * assorted info we need to check at login
1620 if (CtdlAccessCheck(ac_logged_in)) {
1624 getuser(&CC->user, CC->curr_user); /* no lock is needed here */
1625 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1628 if (CC->user.axlevel >= 6) {
1630 if (CitControl.MMflags & MM_VALID)
1634 /* check for mail */
1635 mail = InitialMailCheck();
1637 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1642 * check to see if a user exists
1644 void cmd_qusr(char *who)
1646 struct ctdluser usbuf;
1648 if (getuser(&usbuf, who) == 0) {
1649 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1651 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1657 * Administrative Get User Parameters
1659 void cmd_agup(char *cmdbuf)
1661 struct ctdluser usbuf;
1662 char requested_user[128];
1664 if (CtdlAccessCheck(ac_aide)) {
1668 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1669 if (getuser(&usbuf, requested_user) != 0) {
1670 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1673 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1680 (int) usbuf.axlevel,
1682 (long)usbuf.lastcall,
1689 * Administrative Set User Parameters
1691 void cmd_asup(char *cmdbuf)
1693 struct ctdluser usbuf;
1694 char requested_user[128];
1700 if (CtdlAccessCheck(ac_aide))
1703 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1704 if (lgetuser(&usbuf, requested_user) != 0) {
1705 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1708 np = num_parms(cmdbuf);
1710 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1712 usbuf.flags = extract_int(cmdbuf, 2);
1714 usbuf.timescalled = extract_int(cmdbuf, 3);
1716 usbuf.posted = extract_int(cmdbuf, 4);
1718 newax = extract_int(cmdbuf, 5);
1719 if ((newax >= 0) && (newax <= 6)) {
1720 usbuf.axlevel = extract_int(cmdbuf, 5);
1724 usbuf.lastcall = extract_long(cmdbuf, 7);
1727 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1730 if (usbuf.axlevel == 0) {
1731 if (purge_user(requested_user) == 0) {
1737 snprintf(notify, SIZ,
1738 "User \"%s\" has been deleted by %s.\n",
1739 usbuf.fullname, CC->user.fullname);
1740 aide_message(notify, "User Deletion Message");
1743 cprintf("%d Ok", CIT_OK);
1745 cprintf(" (%s deleted)", requested_user);
1752 * Check to see if the user who we just sent mail to is logged in. If yes,
1753 * bump the 'new mail' counter for their session. That enables them to
1754 * receive a new mail notification without having to hit the database.
1756 void BumpNewMailCounter(long which_user) {
1757 struct CitContext *ptr;
1759 begin_critical_section(S_SESSION_TABLE);
1761 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1762 if (ptr->user.usernum == which_user) {
1767 end_critical_section(S_SESSION_TABLE);
1772 * Count the number of new mail messages the user has
1776 int num_newmsgs = 0;
1778 num_newmsgs = CC->newmail;
1781 return (num_newmsgs);
1786 * Count the number of new mail messages the user has
1788 int InitialMailCheck()
1790 int num_newmsgs = 0;
1792 char mailboxname[ROOMNAMELEN];
1793 struct ctdlroom mailbox;
1795 struct cdbdata *cdbfr;
1796 long *msglist = NULL;
1799 MailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1800 if (getroom(&mailbox, mailboxname) != 0)
1802 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1804 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1806 if (cdbfr != NULL) {
1807 msglist = malloc(cdbfr->len);
1808 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1809 num_msgs = cdbfr->len / sizeof(long);
1813 for (a = 0; a < num_msgs; ++a) {
1814 if (msglist[a] > 0L) {
1815 if (msglist[a] > vbuf.v_lastseen) {
1820 if (msglist != NULL)
1823 return (num_newmsgs);
1829 * Set the preferred view for the current user/room combination
1831 void cmd_view(char *cmdbuf) {
1835 if (CtdlAccessCheck(ac_logged_in)) {
1839 requested_view = extract_int(cmdbuf, 0);
1841 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1842 vbuf.v_view = requested_view;
1843 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1845 cprintf("%d ok\n", CIT_OK);
1852 void cmd_renu(char *cmdbuf)
1855 char oldname[USERNAME_SIZE];
1856 char newname[USERNAME_SIZE];
1858 if (CtdlAccessCheck(ac_aide)) {
1862 extract_token(oldname, cmdbuf, 0, '|', sizeof oldname);
1863 extract_token(newname, cmdbuf, 1, '|', sizeof newname);
1865 retcode = rename_user(oldname, newname);
1868 cprintf("%d '%s' has been renamed to '%s'.\n", CIT_OK, oldname, newname);
1870 case RENAMEUSER_LOGGED_IN:
1871 cprintf("%d '%s' is currently logged in and cannot be renamed.\n",
1872 ERROR + ALREADY_LOGGED_IN , oldname);
1874 case RENAMEUSER_NOT_FOUND:
1875 cprintf("%d '%s' does not exist.\n", ERROR + NO_SUCH_USER, oldname);
1877 case RENAMEUSER_ALREADY_EXISTS:
1878 cprintf("%d A user named '%s' already exists.\n", ERROR + ALREADY_EXISTS, newname);
1882 cprintf("%d An unknown error occurred.\n", ERROR);