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"
53 #include "citadel_ldap.h"
56 #include "ctdl_module.h"
58 /* These pipes are used to talk to the chkpwd daemon, which is forked during startup */
59 int chkpwd_write_pipe[2];
60 int chkpwd_read_pipe[2];
63 * makeuserkey() - convert a username into the format used as a database key
64 * (it's just the username converted into lower case)
66 INLINE void makeuserkey(char *key, char *username) {
69 len = strlen(username);
70 if (len >= USERNAME_SIZE)
72 CtdlLogPrintf (CTDL_EMERG, "Username to long: %s", username);
74 len = USERNAME_SIZE - 1;
75 username[USERNAME_SIZE - 1]='\0';
77 for (i=0; i<=len; ++i) {
78 key[i] = tolower(username[i]);
84 * getuser() - retrieve named user into supplied buffer.
85 * returns 0 on success
87 int getuser(struct ctdluser *usbuf, char name[])
89 return CtdlGetUser(usbuf, name);
94 * CtdlGetUser() - retrieve named user into supplied buffer.
95 * returns 0 on success
97 int CtdlGetUser(struct ctdluser *usbuf, char name[])
100 char usernamekey[USERNAME_SIZE];
101 struct cdbdata *cdbus;
104 memset(usbuf, 0, sizeof(struct ctdluser));
107 makeuserkey(usernamekey, name);
108 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
110 if (cdbus == NULL) { /* user not found */
114 memcpy(usbuf, cdbus->ptr,
115 ((cdbus->len > sizeof(struct ctdluser)) ?
116 sizeof(struct ctdluser) : cdbus->len));
125 * CtdlGetUserLock() - same as getuser() but locks the record
127 int CtdlGetUserLock(struct ctdluser *usbuf, char *name)
131 retcode = CtdlGetUser(usbuf, name);
133 begin_critical_section(S_USERS);
140 * lgetuser() - same as getuser() but locks the record
142 int lgetuser(struct ctdluser *usbuf, char *name)
144 return CtdlGetUserLock(usbuf, name);
149 * CtdlPutUser() - write user buffer into the correct place on disk
151 void CtdlPutUser(struct ctdluser *usbuf)
153 char usernamekey[USERNAME_SIZE];
155 makeuserkey(usernamekey, usbuf->fullname);
157 usbuf->version = REV_LEVEL;
159 usernamekey, strlen(usernamekey),
160 usbuf, sizeof(struct ctdluser));
166 * putuser() - write user buffer into the correct place on disk
168 void putuser(struct ctdluser *usbuf)
175 * CtdlPutUserLock() - same as putuser() but locks the record
177 void CtdlPutUserLock(struct ctdluser *usbuf)
180 end_critical_section(S_USERS);
185 * lputuser() - same as putuser() but locks the record
187 void lputuser(struct ctdluser *usbuf)
189 CtdlPutUserLock(usbuf);
194 * rename_user() - this is tricky because the user's display name is the database key
196 * Returns 0 on success or nonzero if there was an error...
199 int rename_user(char *oldname, char *newname) {
201 int retcode = RENAMEUSER_OK;
202 struct ctdluser usbuf;
204 char oldnamekey[USERNAME_SIZE];
205 char newnamekey[USERNAME_SIZE];
207 /* We cannot rename a user who is currently logged in */
208 /* FIXME: This is very broken!!!!
209 * We check that the user is not already logged in because we can't rename them
210 * if they are logged in.
211 * BUT THEN WE LEAVE A HUGE WINDOW FOR THEM TO LOG IN BEFORE WE LOCK TO RENAME THEM!!!!!
212 * We are also traversing an un-locked context list which is a very bad thing to do.
214 for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
215 if (!strcasecmp(cptr->user.fullname, oldname)) {
216 return(RENAMEUSER_LOGGED_IN);
220 /* Create the database keys... */
221 makeuserkey(oldnamekey, oldname);
222 makeuserkey(newnamekey, newname);
224 /* Lock up and get going */
225 begin_critical_section(S_USERS);
227 if (CtdlGetUser(&usbuf, newname) == 0) {
228 retcode = RENAMEUSER_ALREADY_EXISTS;
232 if (CtdlGetUser(&usbuf, oldname) != 0) {
233 retcode = RENAMEUSER_NOT_FOUND;
236 else { /* Sanity checks succeeded. Now rename the user. */
237 if (usbuf.usernum == 0)
239 CtdlLogPrintf (CTDL_DEBUG, "Can not rename user \"Citadel\".\n");
240 retcode = RENAMEUSER_NOT_FOUND;
242 CtdlLogPrintf(CTDL_DEBUG, "Renaming <%s> to <%s>\n", oldname, newname);
243 cdb_delete(CDB_USERS, oldnamekey, strlen(oldnamekey));
244 safestrncpy(usbuf.fullname, newname, sizeof usbuf.fullname);
246 cdb_store(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long),
247 usbuf.fullname, strlen(usbuf.fullname)+1 );
249 retcode = RENAMEUSER_OK;
255 end_critical_section(S_USERS);
262 * Index-generating function used by Ctdl[Get|Set]Relationship
264 int GenerateRelationshipIndex(char *IndexBuf,
276 TheIndex.iRoomID = RoomID;
277 TheIndex.iRoomGen = RoomGen;
278 TheIndex.iUserID = UserID;
280 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
281 return (sizeof(TheIndex));
287 * Back end for CtdlSetRelationship()
289 void put_visit(struct visit *newvisit)
294 memset (IndexBuf, 0, sizeof (IndexBuf));
295 /* Generate an index */
296 IndexLen = GenerateRelationshipIndex(IndexBuf,
299 newvisit->v_usernum);
301 /* Store the record */
302 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
303 newvisit, sizeof(struct visit)
311 * Define a relationship between a user and a room
313 void CtdlSetRelationship(struct visit *newvisit,
314 struct ctdluser *rel_user,
315 struct ctdlroom *rel_room)
319 /* We don't use these in Citadel because they're implicit by the
320 * index, but they must be present if the database is exported.
322 newvisit->v_roomnum = rel_room->QRnumber;
323 newvisit->v_roomgen = rel_room->QRgen;
324 newvisit->v_usernum = rel_user->usernum;
330 * Locate a relationship between a user and a room
332 void CtdlGetRelationship(struct visit *vbuf,
333 struct ctdluser *rel_user,
334 struct ctdlroom *rel_room)
339 struct cdbdata *cdbvisit;
341 /* Generate an index */
342 IndexLen = GenerateRelationshipIndex(IndexBuf,
347 /* Clear out the buffer */
348 memset(vbuf, 0, sizeof(struct visit));
350 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
351 if (cdbvisit != NULL) {
352 memcpy(vbuf, cdbvisit->ptr,
353 ((cdbvisit->len > sizeof(struct visit)) ?
354 sizeof(struct visit) : cdbvisit->len));
358 /* If this is the first time the user has seen this room,
359 * set the view to be the default for the room.
361 vbuf->v_view = rel_room->QRdefaultview;
364 /* Set v_seen if necessary */
365 if (vbuf->v_seen[0] == 0) {
366 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
371 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
373 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
377 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
379 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
384 * Is the user currently logged in an Aide?
388 if (CC->user.axlevel >= 6)
396 * Is the user currently logged in an Aide *or* the room aide for this room?
398 int is_room_aide(void)
401 if (!CC->logged_in) {
405 if ((CC->user.axlevel >= 6)
406 || (CC->room.QRroomaide == CC->user.usernum)) {
414 * CtdlGetUserByNumber() - get user by number
415 * returns 0 if user was found
417 * Note: fetching a user this way requires one additional database operation.
419 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number)
421 struct cdbdata *cdbun;
424 cdbun = cdb_fetch(CDB_USERSBYNUMBER, &number, sizeof(long));
426 CtdlLogPrintf(CTDL_INFO, "User %ld not found\n", number);
430 CtdlLogPrintf(CTDL_INFO, "User %ld maps to %s\n", number, cdbun->ptr);
431 r = CtdlGetUser(usbuf, cdbun->ptr);
437 * getuserbynumber() - get user by number
438 * returns 0 if user was found
440 * Note: fetching a user this way requires one additional database operation.
442 int getuserbynumber(struct ctdluser *usbuf, long number)
444 return CtdlGetUserByNumber(usbuf, number);
450 * Helper function for rebuild_usersbynumber()
452 void rebuild_ubn_for_user(struct ctdluser *usbuf, void *data) {
455 struct ubnlist *next;
456 char username[USERNAME_SIZE];
460 static struct ubnlist *u = NULL;
461 struct ubnlist *ptr = NULL;
463 /* Lazy programming here. Call this function as a ForEachUser backend
464 * in order to queue up the room names, or call it with a null user
465 * to make it do the processing.
468 ptr = (struct ubnlist *) malloc(sizeof (struct ubnlist));
469 if (ptr == NULL) return;
471 ptr->usernum = usbuf->usernum;
472 safestrncpy(ptr->username, usbuf->fullname, sizeof ptr->username);
479 CtdlLogPrintf(CTDL_DEBUG, "Rebuilding usersbynumber index %10ld : %s\n",
480 u->usernum, u->username);
481 cdb_store(CDB_USERSBYNUMBER, &u->usernum, sizeof(long), u->username, strlen(u->username)+1);
492 * Rebuild the users-by-number index
494 void rebuild_usersbynumber(void) {
495 cdb_trunc(CDB_USERSBYNUMBER); /* delete the old indices */
496 ForEachUser(rebuild_ubn_for_user, NULL); /* enumerate the users */
497 rebuild_ubn_for_user(NULL, NULL); /* and index them */
503 * getuserbyuid() - get user by system uid (for PAM mode authentication)
504 * returns 0 if user was found
506 * WARNING: don't use this function unless you absolutely have to. It does
507 * a sequential search and therefore is computationally expensive.
509 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
511 struct cdbdata *cdbus;
513 cdb_rewind(CDB_USERS);
515 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
516 memset(usbuf, 0, sizeof(struct ctdluser));
517 memcpy(usbuf, cdbus->ptr,
518 ((cdbus->len > sizeof(struct ctdluser)) ?
519 sizeof(struct ctdluser) : cdbus->len));
521 if (usbuf->uid == number) {
522 cdb_close_cursor(CDB_USERS);
530 * Back end for cmd_user() and its ilk
532 * NOTE: "authname" should only be used if we are attempting to use the "master user" feature
534 int CtdlLoginExistingUser(char *authname, char *trythisname)
539 CtdlLogPrintf(9, "CtdlLoginExistingUser(%s, %s)\n", authname, trythisname);
541 if ((CC->logged_in)) {
542 return login_already_logged_in;
545 if (trythisname == NULL) return login_not_found;
547 if (!strncasecmp(trythisname, "SYS_", 4))
549 CtdlLogPrintf(CTDL_DEBUG, "System user \"%s\" is not allowed to log in.\n", trythisname);
550 return login_not_found;
553 /* If a "master user" is defined, handle its authentication if specified */
555 if (strlen(config.c_master_user) > 0) if (strlen(config.c_master_pass) > 0) if (authname) {
556 if (!strcasecmp(authname, config.c_master_user)) {
561 /* Continue attempting user validation... */
562 safestrncpy(username, trythisname, USERNAME_SIZE);
565 if (IsEmptyStr(username)) {
566 return login_not_found;
569 if (config.c_auth_mode == AUTHMODE_HOST) {
574 struct passwd *tempPwdPtr;
577 CtdlLogPrintf(CTDL_DEBUG, "asking host about <%s>\n", username);
578 #ifdef HAVE_GETPWNAM_R
579 #ifdef SOLARIS_GETPWUID
580 CtdlLogPrintf(CTDL_DEBUG, "Calling getpwnam_r()\n");
581 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
582 #else // SOLARIS_GETPWUID
583 CtdlLogPrintf(CTDL_DEBUG, "Calling getpwnam_r()\n");
584 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
585 #endif // SOLARIS_GETPWUID
586 #else // HAVE_GETPWNAM_R
587 CtdlLogPrintf(CTDL_DEBUG, "SHOULD NEVER GET HERE!!!\n");
589 #endif // HAVE_GETPWNAM_R
590 if (tempPwdPtr == NULL) {
591 CtdlLogPrintf(CTDL_DEBUG, "no such user <%s>\n", username);
592 return login_not_found;
595 /* Locate the associated Citadel account.
596 * If not found, make one attempt to create it.
598 found_user = getuserbyuid(&CC->user, pd.pw_uid);
599 CtdlLogPrintf(CTDL_DEBUG, "found it: uid=%ld, gecos=%s here: %d\n",
600 (long)pd.pw_uid, pd.pw_gecos, found_user);
601 if (found_user != 0) {
602 create_user(username, 0);
603 found_user = getuserbyuid(&CC->user, pd.pw_uid);
609 else if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
617 found_user = CtdlTryUserLDAP(username, ldap_dn, sizeof ldap_dn, ldap_cn, sizeof ldap_cn, &ldap_uid);
618 if (found_user != 0) {
619 return login_not_found;
622 found_user = getuserbyuid(&CC->user, ldap_uid);
623 if (found_user != 0) {
624 create_user(trythisname, 0);
625 found_user = getuserbyuid(&CC->user, ldap_uid);
628 if (found_user == 0) {
629 if (CC->ldap_dn != NULL) free(CC->ldap_dn);
630 CC->ldap_dn = strdup(ldap_dn);
637 /* native auth mode */
639 struct recptypes *valid = NULL;
641 /* First, try to log in as if the supplied name is a display name */
642 found_user = CtdlGetUser(&CC->user, username);
644 /* If that didn't work, try to log in as if the supplied name
645 * is an e-mail address
647 if (found_user != 0) {
648 valid = validate_recipients(username, NULL, 0);
650 if (valid->num_local == 1) {
651 found_user = CtdlGetUser(&CC->user, valid->recp_local);
653 free_recipients(valid);
658 /* Did we find something? */
659 if (found_user == 0) {
660 if (((CC->nologin)) && (CC->user.axlevel < 6)) {
661 return login_too_many_users;
663 safestrncpy(CC->curr_user, CC->user.fullname,
664 sizeof CC->curr_user);
668 return login_not_found;
676 void cmd_user(char *cmdbuf)
681 CtdlLogPrintf(CTDL_DEBUG, "cmd_user(%s)\n", cmdbuf);
682 extract_token(username, cmdbuf, 0, '|', sizeof username);
683 CtdlLogPrintf(CTDL_DEBUG, "username: %s\n", username);
685 CtdlLogPrintf(CTDL_DEBUG, "username: %s\n", username);
687 a = CtdlLoginExistingUser(NULL, username);
689 case login_already_logged_in:
690 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
692 case login_too_many_users:
694 "Too many users are already online "
696 ERROR + MAX_SESSIONS_EXCEEDED,
697 config.c_nodename, config.c_maxsessions);
700 cprintf("%d Password required for %s\n",
701 MORE_DATA, CC->curr_user);
703 case login_not_found:
704 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
707 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
714 * session startup code which is common to both cmd_pass() and cmd_newu()
719 CtdlLogPrintf(CTDL_NOTICE, "<%s> logged in\n", CC->curr_user);
721 CtdlGetUserLock(&CC->user, CC->curr_user);
722 ++(CC->user.timescalled);
723 CC->previous_login = CC->user.lastcall;
724 time(&CC->user.lastcall);
726 /* If this user's name is the name of the system administrator
727 * (as specified in setup), automatically assign access level 6.
729 if (!strcasecmp(CC->user.fullname, config.c_sysadm)) {
730 CC->user.axlevel = 6;
733 /* If we're authenticating off the host system, automatically give
734 * root the highest level of access.
736 if (config.c_auth_mode == AUTHMODE_HOST) {
737 if (CC->user.uid == 0) {
738 CC->user.axlevel = 6;
742 CtdlPutUserLock(&CC->user);
745 * Populate CC->cs_inet_email with a default address. This will be
746 * overwritten with the user's directory address, if one exists, when
747 * the vCard module's login hook runs.
749 snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
750 CC->user.fullname, config.c_fqdn);
751 convert_spaces_to_underscores(CC->cs_inet_email);
753 /* Create any personal rooms required by the system.
754 * (Technically, MAILROOM should be there already, but just in case...)
756 CtdlCreateRoom(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
757 CtdlCreateRoom(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
758 CtdlCreateRoom(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
759 /* CtdlCreateRoom(USERDRAFTROOM, 4, "", 0, 1, 0, VIEW_MAILBOX); temporarily disabled for 7.60 */
761 /* Run any startup routines registered by loadable modules */
762 PerformSessionHooks(EVT_LOGIN);
764 /* Enter the lobby */
765 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
769 void logged_in_response(void)
771 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
772 CIT_OK, CC->user.fullname, CC->user.axlevel,
773 CC->user.timescalled, CC->user.posted,
774 CC->user.flags, CC->user.usernum,
781 * misc things to be taken care of when a user is logged out
789 void CtdlUserLogout(void)
791 CitContext *CCC = CC; /* CachedCitContext - performance boost */
793 * If there is a download in progress, abort it.
795 if (CCC->download_fp != NULL) {
796 fclose(CCC->download_fp);
797 CCC->download_fp = NULL;
801 * If there is an upload in progress, abort it.
803 if (CCC->upload_fp != NULL) {
808 * If we were talking to a network node, we're not anymore...
810 if (!IsEmptyStr(CCC->net_node)) {
811 network_talking_to(CCC->net_node, NTT_REMOVE);
814 /* Run any hooks registered by modules... */
815 PerformSessionHooks(EVT_LOGOUT);
818 * Clear out some session data. Most likely, the CitContext for this
819 * session is about to get nuked when the session disconnects, but
820 * since it's possible to log in again without reconnecting, we cannot
821 * make that assumption.
823 strcpy(CCC->fake_username, "");
824 strcpy(CCC->fake_hostname, "");
825 strcpy(CCC->fake_roomname, "");
828 /* Check to see if the user was deleted whilst logged in and purge them if necessary */
829 if ((CCC->user.axlevel == 0) && (CCC->user.usernum))
830 purge_user(CCC->user.fullname);
832 /* Free any output buffers */
837 * Validate a password on the host unix system by talking to the chkpwd daemon
839 static int validpw(uid_t uid, const char *pass)
844 if (IsEmptyStr(pass)) {
845 CtdlLogPrintf(CTDL_DEBUG, "refusing to check empty password for uid=%d using chkpwd...\n", uid);
849 CtdlLogPrintf(CTDL_DEBUG, "Validating password for uid=%d using chkpwd...\n", uid);
851 begin_critical_section(S_CHKPWD);
852 rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
853 rv = write(chkpwd_write_pipe[1], pass, 256);
854 rv = read(chkpwd_read_pipe[0], buf, 4);
855 end_critical_section(S_CHKPWD);
857 if (!strncmp(buf, "PASS", 4)) {
858 CtdlLogPrintf(CTDL_DEBUG, "...pass\n");
862 CtdlLogPrintf(CTDL_DEBUG, "...fail\n");
867 * Start up the chkpwd daemon so validpw() has something to talk to
869 void start_chkpwd_daemon(void) {
871 struct stat filestats;
874 CtdlLogPrintf(CTDL_DEBUG, "Starting chkpwd daemon for host authentication mode\n");
876 if ((stat(file_chkpwd, &filestats)==-1) ||
877 (filestats.st_size==0)){
878 printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
881 if (pipe(chkpwd_write_pipe) != 0) {
882 CtdlLogPrintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
885 if (pipe(chkpwd_read_pipe) != 0) {
886 CtdlLogPrintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
891 if (chkpwd_pid < 0) {
892 CtdlLogPrintf(CTDL_EMERG, "Unable to fork chkpwd daemon: %s\n", strerror(errno));
895 if (chkpwd_pid == 0) {
896 CtdlLogPrintf(CTDL_DEBUG, "Now calling dup2() write\n");
897 dup2(chkpwd_write_pipe[0], 0);
898 CtdlLogPrintf(CTDL_DEBUG, "Now calling dup2() write\n");
899 dup2(chkpwd_read_pipe[1], 1);
900 CtdlLogPrintf(CTDL_DEBUG, "Now closing stuff\n");
901 for (i=2; i<256; ++i) close(i);
902 CtdlLogPrintf(CTDL_DEBUG, "Now calling execl(%s)\n", file_chkpwd);
903 execl(file_chkpwd, file_chkpwd, NULL);
904 CtdlLogPrintf(CTDL_EMERG, "Unable to exec chkpwd daemon: %s\n", strerror(errno));
911 int CtdlTryPassword(char *password)
915 if ((CC->logged_in)) {
916 CtdlLogPrintf(CTDL_WARNING, "CtdlTryPassword: already logged in\n");
917 return pass_already_logged_in;
919 if (!strcmp(CC->curr_user, NLI)) {
920 CtdlLogPrintf(CTDL_WARNING, "CtdlTryPassword: no user selected\n");
923 if (CtdlGetUser(&CC->user, CC->curr_user)) {
924 CtdlLogPrintf(CTDL_ERR, "CtdlTryPassword: internal error\n");
925 return pass_internal_error;
927 if (password == NULL) {
928 CtdlLogPrintf(CTDL_INFO, "CtdlTryPassword: NULL password string supplied\n");
929 return pass_wrong_password;
934 code = strcmp(password, config.c_master_pass);
937 else if (config.c_auth_mode == AUTHMODE_HOST) {
941 if (validpw(CC->user.uid, password)) {
945 * sooper-seekrit hack: populate the password field in the
946 * citadel database with the password that the user typed,
947 * if it's correct. This allows most sites to convert from
948 * host auth to native auth if they want to. If you think
949 * this is a security hazard, comment it out.
952 CtdlGetUserLock(&CC->user, CC->curr_user);
953 safestrncpy(CC->user.password, password, sizeof CC->user.password);
954 CtdlPutUserLock(&CC->user);
957 * (sooper-seekrit hack ends here)
967 else if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
971 if ((CC->ldap_dn) && (!CtdlTryPasswordLDAP(CC->ldap_dn, password))) {
982 /* native auth mode */
985 strproc(CC->user.password);
986 code = strcasecmp(CC->user.password, password);
988 strproc(CC->user.password);
989 code = strcasecmp(CC->user.password, password);
996 CtdlLogPrintf(CTDL_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
997 return pass_wrong_password;
1002 void cmd_pass(char *buf)
1007 memset(password, 0, sizeof(password));
1008 extract_token(password, buf, 0, '|', sizeof password);
1009 a = CtdlTryPassword(password);
1012 case pass_already_logged_in:
1013 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
1016 cprintf("%d You must send a name with USER first.\n",
1017 ERROR + USERNAME_REQUIRED);
1019 case pass_wrong_password:
1020 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
1023 logged_in_response();
1031 * Delete a user record *and* all of its related resources.
1033 int purge_user(char pname[])
1036 struct ctdluser usbuf;
1037 char usernamekey[USERNAME_SIZE];
1039 int user_is_logged_in = 0;
1041 makeuserkey(usernamekey, pname);
1043 /* If the name is empty we can't find them in the DB any way so just return */
1044 if (IsEmptyStr(pname))
1045 return (ERROR + NO_SUCH_USER);
1047 if (CtdlGetUser(&usbuf, pname) != 0) {
1048 CtdlLogPrintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
1049 return (ERROR + NO_SUCH_USER);
1051 /* Don't delete a user who is currently logged in. Instead, just
1052 * set the access level to 0, and let the account get swept up
1053 * during the next purge.
1055 user_is_logged_in = 0;
1056 begin_critical_section(S_SESSION_TABLE);
1057 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
1058 if (ccptr->user.usernum == usbuf.usernum) {
1059 user_is_logged_in = 1;
1062 end_critical_section(S_SESSION_TABLE);
1063 if (user_is_logged_in == 1) {
1064 CtdlLogPrintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
1066 CtdlPutUser(&usbuf);
1069 CtdlLogPrintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
1071 /* Perform any purge functions registered by server extensions */
1072 PerformUserHooks(&usbuf, EVT_PURGEUSER);
1074 /* delete any existing user/room relationships */
1075 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
1077 /* delete the users-by-number index record */
1078 cdb_delete(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long));
1080 /* delete the userlog entry */
1081 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
1083 /* remove the user's bio file */
1091 /* remove the user's picture */
1103 int internal_create_user (char *username, struct ctdluser *usbuf, uid_t uid)
1105 if (!CtdlGetUser(usbuf, username)) {
1106 return (ERROR + ALREADY_EXISTS);
1109 /* Go ahead and initialize a new user record */
1110 memset(usbuf, 0, sizeof(struct ctdluser));
1111 safestrncpy(usbuf->fullname, username, sizeof usbuf->fullname);
1112 strcpy(usbuf->password, "");
1115 /* These are the default flags on new accounts */
1116 usbuf->flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
1118 usbuf->timescalled = 0;
1120 usbuf->axlevel = config.c_initax;
1121 usbuf->USscreenwidth = 80;
1122 usbuf->USscreenheight = 24;
1123 usbuf->lastcall = time(NULL);
1125 /* fetch a new user number */
1126 usbuf->usernum = get_new_user_number();
1128 /* add user to the database */
1130 cdb_store(CDB_USERSBYNUMBER, &usbuf->usernum, sizeof(long), usbuf->fullname, strlen(usbuf->fullname)+1);
1138 * create_user() - back end processing to create a new user
1140 * Set 'newusername' to the desired account name.
1141 * Set 'become_user' to nonzero if this is self-service account creation and we want
1142 * to actually log in as the user we just created, otherwise set it to 0.
1144 int create_user(char *newusername, int become_user)
1146 struct ctdluser usbuf;
1147 struct ctdlroom qrbuf;
1149 char mailboxname[ROOMNAMELEN];
1155 safestrncpy(username, newusername, sizeof username);
1159 if (config.c_auth_mode == AUTHMODE_HOST) {
1161 /* host auth mode */
1164 struct passwd *tempPwdPtr;
1165 char pwdbuffer[256];
1167 #ifdef HAVE_GETPWNAM_R
1168 #ifdef SOLARIS_GETPWUID
1169 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof(pwdbuffer));
1170 #else // SOLARIS_GETPWUID
1171 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
1172 #endif // SOLARIS_GETPWUID
1173 #else // HAVE_GETPWNAM_R
1175 #endif // HAVE_GETPWNAM_R
1176 if (tempPwdPtr != NULL) {
1177 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
1179 if (IsEmptyStr (username))
1181 safestrncpy(username, pd.pw_name, sizeof username);
1185 return (ERROR + NO_SUCH_USER);
1190 if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
1191 if (CtdlTryUserLDAP(username, NULL, 0, username, sizeof username, &uid) != 0) {
1192 return(ERROR + NO_SUCH_USER);
1195 #endif /* HAVE_LDAP */
1197 if ((retval = internal_create_user(username, &usbuf, uid)) != 0)
1201 * Give the user a private mailbox and a configuration room.
1202 * Make the latter an invisible system room.
1204 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
1205 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
1207 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
1208 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
1209 if (CtdlGetRoomLock(&qrbuf, mailboxname) == 0) {
1210 qrbuf.QRflags2 |= QR2_SYSTEM;
1211 CtdlPutRoomLock(&qrbuf);
1214 /* Perform any create functions registered by server extensions */
1215 PerformUserHooks(&usbuf, EVT_NEWUSER);
1217 /* Everything below this line can be bypassed if administratively
1218 * creating a user, instead of doing self-service account creation
1222 /* Now become the user we just created */
1223 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
1224 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
1227 /* Check to make sure we're still who we think we are */
1228 if (CtdlGetUser(&CC->user, CC->curr_user)) {
1229 return (ERROR + INTERNAL_ERROR);
1234 "New user account <%s> has been created, from host %s [%s].\n",
1239 CtdlAideMessage(buf, "User Creation Notice");
1240 CtdlLogPrintf(CTDL_NOTICE, "New user <%s> created\n", username);
1247 * cmd_newu() - create a new user account and log in as that user
1249 void cmd_newu(char *cmdbuf)
1254 if (config.c_auth_mode != AUTHMODE_NATIVE) {
1255 cprintf("%d This system does not use native mode authentication.\n",
1260 if (config.c_disable_newu) {
1261 cprintf("%d Self-service user account creation "
1262 "is disabled on this system.\n", ERROR + NOT_HERE);
1266 if (CC->logged_in) {
1267 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
1271 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
1272 ERROR + MAX_SESSIONS_EXCEEDED,
1273 config.c_nodename, config.c_maxsessions);
1275 extract_token(username, cmdbuf, 0, '|', sizeof username);
1279 if (IsEmptyStr(username)) {
1280 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1284 if ((!strcasecmp(username, "bbs")) ||
1285 (!strcasecmp(username, "new")) ||
1286 (!strcasecmp(username, "."))) {
1287 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
1291 a = create_user(username, 1);
1294 logged_in_response();
1295 } else if (a == ERROR + ALREADY_EXISTS) {
1296 cprintf("%d '%s' already exists.\n",
1297 ERROR + ALREADY_EXISTS, username);
1299 } else if (a == ERROR + INTERNAL_ERROR) {
1300 cprintf("%d Internal error - user record disappeared?\n",
1301 ERROR + INTERNAL_ERROR);
1304 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
1310 * set password - back end api code
1312 void CtdlSetPassword(char *new_pw)
1314 CtdlGetUserLock(&CC->user, CC->curr_user);
1315 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1316 CtdlPutUserLock(&CC->user);
1317 CtdlLogPrintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
1318 PerformSessionHooks(EVT_SETPASS);
1323 * set password - citadel protocol implementation
1325 void cmd_setp(char *new_pw)
1327 int generate_random_password = 0;
1329 if (CtdlAccessCheck(ac_logged_in)) {
1332 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
1333 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
1336 if (CC->is_master) {
1337 cprintf("%d The master prefix password cannot be changed with this command.\n",
1342 if (!strcasecmp(new_pw, "GENERATE_RANDOM_PASSWORD")) {
1343 char random_password[17];
1344 generate_random_password = 1;
1345 snprintf(random_password, sizeof random_password, "%08lx%08lx", random(), random());
1346 CtdlSetPassword(random_password);
1347 cprintf("%d %s\n", CIT_OK, random_password);
1351 if (IsEmptyStr(new_pw)) {
1352 cprintf("%d Password unchanged.\n", CIT_OK);
1355 CtdlSetPassword(new_pw);
1356 cprintf("%d Password changed.\n", CIT_OK);
1362 * cmd_creu() - administratively create a new user account (do not log in to it)
1364 void cmd_creu(char *cmdbuf)
1369 struct ctdluser tmp;
1371 if (CtdlAccessCheck(ac_aide)) {
1375 extract_token(username, cmdbuf, 0, '|', sizeof username);
1376 extract_token(password, cmdbuf, 1, '|', sizeof password);
1382 if (IsEmptyStr(username)) {
1383 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1387 a = create_user(username, 0);
1390 if (!IsEmptyStr(password)) {
1391 CtdlGetUserLock(&tmp, username);
1392 safestrncpy(tmp.password, password, sizeof(tmp.password));
1393 CtdlPutUserLock(&tmp);
1395 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1396 (!IsEmptyStr(password)) ? "and password set" :
1397 "with no password");
1399 } else if (a == ERROR + ALREADY_EXISTS) {
1400 cprintf("%d '%s' already exists.\n", ERROR + ALREADY_EXISTS, username);
1402 } else if ( (config.c_auth_mode != AUTHMODE_NATIVE) && (a == ERROR + NO_SUCH_USER) ) {
1403 cprintf("%d User accounts are not created within Citadel in host authentication mode.\n",
1404 ERROR + NO_SUCH_USER);
1407 cprintf("%d An error occurred creating the user account.\n", ERROR + INTERNAL_ERROR);
1414 * get user parameters
1416 void cmd_getu(char *cmdbuf)
1419 if (CtdlAccessCheck(ac_logged_in))
1422 CtdlGetUser(&CC->user, CC->curr_user);
1423 cprintf("%d %d|%d|%d|\n",
1425 CC->user.USscreenwidth,
1426 CC->user.USscreenheight,
1427 (CC->user.flags & US_USER_SET)
1432 * set user parameters
1434 void cmd_setu(char *new_parms)
1436 if (CtdlAccessCheck(ac_logged_in))
1439 if (num_parms(new_parms) < 3) {
1440 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1443 CtdlGetUserLock(&CC->user, CC->curr_user);
1444 CC->user.USscreenwidth = extract_int(new_parms, 0);
1445 CC->user.USscreenheight = extract_int(new_parms, 1);
1446 CC->user.flags = CC->user.flags & (~US_USER_SET);
1447 CC->user.flags = CC->user.flags |
1448 (extract_int(new_parms, 2) & US_USER_SET);
1450 CtdlPutUserLock(&CC->user);
1451 cprintf("%d Ok\n", CIT_OK);
1455 * set last read pointer
1457 void cmd_slrp(char *new_ptr)
1461 struct visit original_vbuf;
1463 if (CtdlAccessCheck(ac_logged_in)) {
1467 if (!strncasecmp(new_ptr, "highest", 7)) {
1468 newlr = CC->room.QRhighest;
1470 newlr = atol(new_ptr);
1473 CtdlGetUserLock(&CC->user, CC->curr_user);
1475 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1476 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1477 vbuf.v_lastseen = newlr;
1478 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1480 /* Only rewrite the record if it changed */
1481 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1482 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1483 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1486 CtdlPutUserLock(&CC->user);
1487 cprintf("%d %ld\n", CIT_OK, newlr);
1491 void cmd_seen(char *argbuf) {
1492 long target_msgnum = 0L;
1493 int target_setting = 0;
1495 if (CtdlAccessCheck(ac_logged_in)) {
1499 if (num_parms(argbuf) != 2) {
1500 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1504 target_msgnum = extract_long(argbuf, 0);
1505 target_setting = extract_int(argbuf, 1);
1507 CtdlSetSeen(&target_msgnum, 1, target_setting,
1508 ctdlsetseen_seen, NULL, NULL);
1509 cprintf("%d OK\n", CIT_OK);
1513 void cmd_gtsn(char *argbuf) {
1516 if (CtdlAccessCheck(ac_logged_in)) {
1520 CtdlGetSeen(buf, ctdlsetseen_seen);
1521 cprintf("%d %s\n", CIT_OK, buf);
1526 * API function for cmd_invt_kick() and anything else that needs to
1527 * invite or kick out a user to/from a room.
1529 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1531 int CtdlInvtKick(char *iuser, int op) {
1532 struct ctdluser USscratch;
1536 if (CtdlGetUser(&USscratch, iuser) != 0) {
1540 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1542 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1543 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1546 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1547 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1549 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1551 /* post a message in Aide> saying what we just did */
1552 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1554 ((op == 1) ? "invited to" : "kicked out of"),
1557 CtdlAideMessage(bbb,"User Admin Message");
1564 * INVT and KICK commands
1566 void cmd_invt_kick(char *iuser, int op) {
1569 * These commands are only allowed by aides, room aides,
1570 * and room namespace owners
1573 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1574 /* access granted */
1577 cprintf("%d Higher access or room ownership required.\n",
1578 ERROR + HIGHER_ACCESS_REQUIRED);
1582 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1584 cprintf("%d Can't add/remove users from this room.\n",
1589 if (CtdlInvtKick(iuser, op) != 0) {
1590 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1594 cprintf("%d %s %s %s.\n",
1596 ((op == 1) ? "invited to" : "kicked out of"),
1601 void cmd_invt(char *iuser) {cmd_invt_kick(iuser, 1);}
1602 void cmd_kick(char *iuser) {cmd_invt_kick(iuser, 0);}
1605 * Forget (Zap) the current room (API call)
1606 * Returns 0 on success
1608 int CtdlForgetThisRoom(void) {
1611 /* On some systems, Aides are not allowed to forget rooms */
1612 if (is_aide() && (config.c_aide_zap == 0)
1613 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1617 CtdlGetUserLock(&CC->user, CC->curr_user);
1618 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1620 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1621 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1623 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1624 CtdlPutUserLock(&CC->user);
1626 /* Return to the Lobby, so we don't end up in an undefined room */
1627 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
1634 * forget (Zap) the current room
1636 void cmd_forg(char *argbuf)
1639 if (CtdlAccessCheck(ac_logged_in)) {
1643 if (CtdlForgetThisRoom() == 0) {
1644 cprintf("%d Ok\n", CIT_OK);
1647 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1652 * Get Next Unregistered User
1654 void cmd_gnur(char *argbuf)
1656 struct cdbdata *cdbus;
1657 struct ctdluser usbuf;
1659 if (CtdlAccessCheck(ac_aide)) {
1663 if ((CitControl.MMflags & MM_VALID) == 0) {
1664 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1668 /* There are unvalidated users. Traverse the user database,
1669 * and return the first user we find that needs validation.
1671 cdb_rewind(CDB_USERS);
1672 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1673 memset(&usbuf, 0, sizeof(struct ctdluser));
1674 memcpy(&usbuf, cdbus->ptr,
1675 ((cdbus->len > sizeof(struct ctdluser)) ?
1676 sizeof(struct ctdluser) : cdbus->len));
1678 if ((usbuf.flags & US_NEEDVALID)
1679 && (usbuf.axlevel > 0)) {
1680 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1681 cdb_close_cursor(CDB_USERS);
1686 /* If we get to this point, there are no more unvalidated users.
1687 * Therefore we clear the "users need validation" flag.
1690 begin_critical_section(S_CONTROL);
1692 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1694 end_critical_section(S_CONTROL);
1695 cprintf("%d *** End of registration.\n", CIT_OK);
1704 void cmd_vali(char *v_args)
1708 struct ctdluser userbuf;
1710 extract_token(user, v_args, 0, '|', sizeof user);
1711 newax = extract_int(v_args, 1);
1713 if (CtdlAccessCheck(ac_aide)) {
1717 if (CtdlGetUserLock(&userbuf, user) != 0) {
1718 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1722 userbuf.axlevel = newax;
1723 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1725 CtdlPutUserLock(&userbuf);
1727 /* If the access level was set to zero, delete the user */
1729 if (purge_user(user) == 0) {
1730 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1734 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1740 * Traverse the user file...
1742 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1745 struct ctdluser usbuf;
1746 struct cdbdata *cdbus;
1748 cdb_rewind(CDB_USERS);
1750 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1751 memset(&usbuf, 0, sizeof(struct ctdluser));
1752 memcpy(&usbuf, cdbus->ptr,
1753 ((cdbus->len > sizeof(struct ctdluser)) ?
1754 sizeof(struct ctdluser) : cdbus->len));
1756 (*CallBack) (&usbuf, in_data);
1762 * List one user (this works with cmd_list)
1764 void ListThisUser(struct ctdluser *usbuf, void *data)
1768 searchstring = (char *)data;
1769 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1773 if (usbuf->axlevel > 0) {
1774 if ((CC->user.axlevel >= 6)
1775 || ((usbuf->flags & US_UNLISTED) == 0)
1776 || ((CC->internal_pgm))) {
1777 cprintf("%s|%d|%ld|%ld|%ld|%ld||\n",
1781 (long)usbuf->lastcall,
1789 * List users (searchstring may be empty to list all users)
1791 void cmd_list(char *cmdbuf)
1793 char searchstring[256];
1794 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1795 striplt(searchstring);
1796 cprintf("%d \n", LISTING_FOLLOWS);
1797 ForEachUser(ListThisUser, (void *)searchstring );
1805 * assorted info we need to check at login
1807 void cmd_chek(char *argbuf)
1813 if (CtdlAccessCheck(ac_logged_in)) {
1817 CtdlGetUser(&CC->user, CC->curr_user); /* no lock is needed here */
1818 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1821 if (CC->user.axlevel >= 6) {
1823 if (CitControl.MMflags & MM_VALID)
1827 /* check for mail */
1828 mail = InitialMailCheck();
1830 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1835 * check to see if a user exists
1837 void cmd_qusr(char *who)
1839 struct ctdluser usbuf;
1841 if (CtdlGetUser(&usbuf, who) == 0) {
1842 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1844 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1850 * Administrative Get User Parameters
1852 void cmd_agup(char *cmdbuf)
1854 struct ctdluser usbuf;
1855 char requested_user[128];
1857 if (CtdlAccessCheck(ac_aide)) {
1861 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1862 if (CtdlGetUser(&usbuf, requested_user) != 0) {
1863 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1866 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1873 (int) usbuf.axlevel,
1875 (long)usbuf.lastcall,
1882 * Administrative Set User Parameters
1884 void cmd_asup(char *cmdbuf)
1886 struct ctdluser usbuf;
1887 char requested_user[128];
1893 if (CtdlAccessCheck(ac_aide))
1896 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1897 if (CtdlGetUserLock(&usbuf, requested_user) != 0) {
1898 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1901 np = num_parms(cmdbuf);
1903 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1905 usbuf.flags = extract_int(cmdbuf, 2);
1907 usbuf.timescalled = extract_int(cmdbuf, 3);
1909 usbuf.posted = extract_int(cmdbuf, 4);
1911 newax = extract_int(cmdbuf, 5);
1912 if ((newax >= 0) && (newax <= 6)) {
1913 usbuf.axlevel = extract_int(cmdbuf, 5);
1917 usbuf.lastcall = extract_long(cmdbuf, 7);
1920 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1922 CtdlPutUserLock(&usbuf);
1923 if (usbuf.axlevel == 0) {
1924 if (purge_user(requested_user) == 0) {
1930 snprintf(notify, SIZ,
1931 "User \"%s\" has been deleted by %s.\n",
1932 usbuf.fullname, CC->user.fullname);
1933 CtdlAideMessage(notify, "User Deletion Message");
1936 cprintf("%d Ok", CIT_OK);
1938 cprintf(" (%s deleted)", requested_user);
1945 * Check to see if the user who we just sent mail to is logged in. If yes,
1946 * bump the 'new mail' counter for their session. That enables them to
1947 * receive a new mail notification without having to hit the database.
1949 void BumpNewMailCounter(long which_user) {
1952 begin_critical_section(S_SESSION_TABLE);
1954 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1955 if (ptr->user.usernum == which_user) {
1960 end_critical_section(S_SESSION_TABLE);
1965 * Count the number of new mail messages the user has
1969 int num_newmsgs = 0;
1971 num_newmsgs = CC->newmail;
1974 return (num_newmsgs);
1979 * Count the number of new mail messages the user has
1981 int InitialMailCheck()
1983 int num_newmsgs = 0;
1985 char mailboxname[ROOMNAMELEN];
1986 struct ctdlroom mailbox;
1988 struct cdbdata *cdbfr;
1989 long *msglist = NULL;
1992 CtdlMailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1993 if (CtdlGetRoom(&mailbox, mailboxname) != 0)
1995 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1997 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1999 if (cdbfr != NULL) {
2000 msglist = malloc(cdbfr->len);
2001 memcpy(msglist, cdbfr->ptr, cdbfr->len);
2002 num_msgs = cdbfr->len / sizeof(long);
2006 for (a = 0; a < num_msgs; ++a) {
2007 if (msglist[a] > 0L) {
2008 if (msglist[a] > vbuf.v_lastseen) {
2013 if (msglist != NULL)
2016 return (num_newmsgs);
2022 * Set the preferred view for the current user/room combination
2024 void cmd_view(char *cmdbuf) {
2028 if (CtdlAccessCheck(ac_logged_in)) {
2032 requested_view = extract_int(cmdbuf, 0);
2034 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
2035 vbuf.v_view = requested_view;
2036 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
2038 cprintf("%d ok\n", CIT_OK);
2045 void cmd_renu(char *cmdbuf)
2048 char oldname[USERNAME_SIZE];
2049 char newname[USERNAME_SIZE];
2051 if (CtdlAccessCheck(ac_aide)) {
2055 extract_token(oldname, cmdbuf, 0, '|', sizeof oldname);
2056 extract_token(newname, cmdbuf, 1, '|', sizeof newname);
2058 retcode = rename_user(oldname, newname);
2061 cprintf("%d '%s' has been renamed to '%s'.\n", CIT_OK, oldname, newname);
2063 case RENAMEUSER_LOGGED_IN:
2064 cprintf("%d '%s' is currently logged in and cannot be renamed.\n",
2065 ERROR + ALREADY_LOGGED_IN , oldname);
2067 case RENAMEUSER_NOT_FOUND:
2068 cprintf("%d '%s' does not exist.\n", ERROR + NO_SUCH_USER, oldname);
2070 case RENAMEUSER_ALREADY_EXISTS:
2071 cprintf("%d A user named '%s' already exists.\n", ERROR + ALREADY_EXISTS, newname);
2075 cprintf("%d An unknown error occurred.\n", ERROR);
2080 /*****************************************************************************/
2081 /* MODULE INITIALIZATION STUFF */
2082 /*****************************************************************************/
2085 CTDL_MODULE_INIT(user_ops)
2088 CtdlRegisterProtoHook(cmd_user, "USER", "Autoconverted. TODO: document me.");
2089 CtdlRegisterProtoHook(cmd_pass, "PASS", "Autoconverted. TODO: document me.");
2090 CtdlRegisterProtoHook(cmd_creu, "CREU", "Autoconverted. TODO: document me.");
2091 CtdlRegisterProtoHook(cmd_setp, "SETP", "Autoconverted. TODO: document me.");
2092 CtdlRegisterProtoHook(cmd_getu, "GETU", "Autoconverted. TODO: document me.");
2093 CtdlRegisterProtoHook(cmd_setu, "SETU", "Autoconverted. TODO: document me.");
2094 CtdlRegisterProtoHook(cmd_slrp, "SLRP", "Autoconverted. TODO: document me.");
2095 CtdlRegisterProtoHook(cmd_invt, "INVT", "Autoconverted. TODO: document me.");
2096 CtdlRegisterProtoHook(cmd_kick, "KICK", "Autoconverted. TODO: document me.");
2097 CtdlRegisterProtoHook(cmd_forg, "FORG", "Autoconverted. TODO: document me.");
2098 CtdlRegisterProtoHook(cmd_gnur, "GNUR", "Autoconverted. TODO: document me.");
2099 CtdlRegisterProtoHook(cmd_vali, "VALI", "Autoconverted. TODO: document me.");
2100 CtdlRegisterProtoHook(cmd_list, "LIST", "Autoconverted. TODO: document me.");
2101 CtdlRegisterProtoHook(cmd_chek, "CHEK", "Autoconverted. TODO: document me.");
2102 CtdlRegisterProtoHook(cmd_qusr, "QUSR", "Autoconverted. TODO: document me.");
2103 CtdlRegisterProtoHook(cmd_agup, "AGUP", "Autoconverted. TODO: document me.");
2104 CtdlRegisterProtoHook(cmd_asup, "ASUP", "Autoconverted. TODO: document me.");
2105 CtdlRegisterProtoHook(cmd_seen, "SEEN", "Autoconverted. TODO: document me.");
2106 CtdlRegisterProtoHook(cmd_gtsn, "GTSN", "Autoconverted. TODO: document me.");
2107 CtdlRegisterProtoHook(cmd_view, "VIEW", "Autoconverted. TODO: document me.");
2108 CtdlRegisterProtoHook(cmd_renu, "RENU", "Autoconverted. TODO: document me.");
2109 CtdlRegisterProtoHook(cmd_newu, "NEWU", "Autoconverted. TODO: document me.");
2111 /* return our Subversion id for the Log */