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
785 CitContext *CCC = CC; /* CachedCitContext - performance boost */
787 * If there is a download in progress, abort it.
789 if (CCC->download_fp != NULL) {
790 fclose(CCC->download_fp);
791 CCC->download_fp = NULL;
795 * If there is an upload in progress, abort it.
797 if (CCC->upload_fp != NULL) {
802 * If we were talking to a network node, we're not anymore...
804 if (!IsEmptyStr(CCC->net_node)) {
805 network_talking_to(CCC->net_node, NTT_REMOVE);
808 /* Run any hooks registered by modules... */
809 PerformSessionHooks(EVT_LOGOUT);
812 * Clear out some session data. Most likely, the CitContext for this
813 * session is about to get nuked when the session disconnects, but
814 * since it's possible to log in again without reconnecting, we cannot
815 * make that assumption.
817 strcpy(CCC->fake_username, "");
818 strcpy(CCC->fake_hostname, "");
819 strcpy(CCC->fake_roomname, "");
822 /* Check to see if the user was deleted whilst logged in and purge them if necessary */
823 if ((CCC->user.axlevel == 0) && (CCC->user.usernum))
824 purge_user(CCC->user.fullname);
826 /* Free any output buffers */
831 * Validate a password on the host unix system by talking to the chkpwd daemon
833 static int validpw(uid_t uid, const char *pass)
838 if (IsEmptyStr(pass)) {
839 CtdlLogPrintf(CTDL_DEBUG, "refusing to check empty password for uid=%d using chkpwd...\n", uid);
843 CtdlLogPrintf(CTDL_DEBUG, "Validating password for uid=%d using chkpwd...\n", uid);
845 begin_critical_section(S_CHKPWD);
846 rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
847 rv = write(chkpwd_write_pipe[1], pass, 256);
848 rv = read(chkpwd_read_pipe[0], buf, 4);
849 end_critical_section(S_CHKPWD);
851 if (!strncmp(buf, "PASS", 4)) {
852 CtdlLogPrintf(CTDL_DEBUG, "...pass\n");
856 CtdlLogPrintf(CTDL_DEBUG, "...fail\n");
861 * Start up the chkpwd daemon so validpw() has something to talk to
863 void start_chkpwd_daemon(void) {
865 struct stat filestats;
868 CtdlLogPrintf(CTDL_DEBUG, "Starting chkpwd daemon for host authentication mode\n");
870 if ((stat(file_chkpwd, &filestats)==-1) ||
871 (filestats.st_size==0)){
872 printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
875 if (pipe(chkpwd_write_pipe) != 0) {
876 CtdlLogPrintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
879 if (pipe(chkpwd_read_pipe) != 0) {
880 CtdlLogPrintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
885 if (chkpwd_pid < 0) {
886 CtdlLogPrintf(CTDL_EMERG, "Unable to fork chkpwd daemon: %s\n", strerror(errno));
889 if (chkpwd_pid == 0) {
890 CtdlLogPrintf(CTDL_DEBUG, "Now calling dup2() write\n");
891 dup2(chkpwd_write_pipe[0], 0);
892 CtdlLogPrintf(CTDL_DEBUG, "Now calling dup2() write\n");
893 dup2(chkpwd_read_pipe[1], 1);
894 CtdlLogPrintf(CTDL_DEBUG, "Now closing stuff\n");
895 for (i=2; i<256; ++i) close(i);
896 CtdlLogPrintf(CTDL_DEBUG, "Now calling execl(%s)\n", file_chkpwd);
897 execl(file_chkpwd, file_chkpwd, NULL);
898 CtdlLogPrintf(CTDL_EMERG, "Unable to exec chkpwd daemon: %s\n", strerror(errno));
905 int CtdlTryPassword(char *password)
909 if ((CC->logged_in)) {
910 CtdlLogPrintf(CTDL_WARNING, "CtdlTryPassword: already logged in\n");
911 return pass_already_logged_in;
913 if (!strcmp(CC->curr_user, NLI)) {
914 CtdlLogPrintf(CTDL_WARNING, "CtdlTryPassword: no user selected\n");
917 if (CtdlGetUser(&CC->user, CC->curr_user)) {
918 CtdlLogPrintf(CTDL_ERR, "CtdlTryPassword: internal error\n");
919 return pass_internal_error;
921 if (password == NULL) {
922 CtdlLogPrintf(CTDL_INFO, "CtdlTryPassword: NULL password string supplied\n");
923 return pass_wrong_password;
928 code = strcmp(password, config.c_master_pass);
931 else if (config.c_auth_mode == AUTHMODE_HOST) {
935 if (validpw(CC->user.uid, password)) {
939 * sooper-seekrit hack: populate the password field in the
940 * citadel database with the password that the user typed,
941 * if it's correct. This allows most sites to convert from
942 * host auth to native auth if they want to. If you think
943 * this is a security hazard, comment it out.
946 CtdlGetUserLock(&CC->user, CC->curr_user);
947 safestrncpy(CC->user.password, password, sizeof CC->user.password);
948 CtdlPutUserLock(&CC->user);
951 * (sooper-seekrit hack ends here)
961 else if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
965 if ((CC->ldap_dn) && (!CtdlTryPasswordLDAP(CC->ldap_dn, password))) {
976 /* native auth mode */
979 strproc(CC->user.password);
980 code = strcasecmp(CC->user.password, password);
982 strproc(CC->user.password);
983 code = strcasecmp(CC->user.password, password);
990 CtdlLogPrintf(CTDL_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
991 return pass_wrong_password;
996 void cmd_pass(char *buf)
1001 memset(password, 0, sizeof(password));
1002 extract_token(password, buf, 0, '|', sizeof password);
1003 a = CtdlTryPassword(password);
1006 case pass_already_logged_in:
1007 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
1010 cprintf("%d You must send a name with USER first.\n",
1011 ERROR + USERNAME_REQUIRED);
1013 case pass_wrong_password:
1014 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
1017 logged_in_response();
1025 * Delete a user record *and* all of its related resources.
1027 int purge_user(char pname[])
1030 struct ctdluser usbuf;
1031 char usernamekey[USERNAME_SIZE];
1033 int user_is_logged_in = 0;
1035 makeuserkey(usernamekey, pname);
1037 /* If the name is empty we can't find them in the DB any way so just return */
1038 if (IsEmptyStr(pname))
1039 return (ERROR + NO_SUCH_USER);
1041 if (CtdlGetUser(&usbuf, pname) != 0) {
1042 CtdlLogPrintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
1043 return (ERROR + NO_SUCH_USER);
1045 /* Don't delete a user who is currently logged in. Instead, just
1046 * set the access level to 0, and let the account get swept up
1047 * during the next purge.
1049 user_is_logged_in = 0;
1050 begin_critical_section(S_SESSION_TABLE);
1051 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
1052 if (ccptr->user.usernum == usbuf.usernum) {
1053 user_is_logged_in = 1;
1056 end_critical_section(S_SESSION_TABLE);
1057 if (user_is_logged_in == 1) {
1058 CtdlLogPrintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
1060 CtdlPutUser(&usbuf);
1063 CtdlLogPrintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
1065 /* Perform any purge functions registered by server extensions */
1066 PerformUserHooks(&usbuf, EVT_PURGEUSER);
1068 /* delete any existing user/room relationships */
1069 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
1071 /* delete the users-by-number index record */
1072 cdb_delete(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long));
1074 /* delete the userlog entry */
1075 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
1077 /* remove the user's bio file */
1085 /* remove the user's picture */
1097 int internal_create_user (char *username, struct ctdluser *usbuf, uid_t uid)
1099 if (!CtdlGetUser(usbuf, username)) {
1100 return (ERROR + ALREADY_EXISTS);
1103 /* Go ahead and initialize a new user record */
1104 memset(usbuf, 0, sizeof(struct ctdluser));
1105 safestrncpy(usbuf->fullname, username, sizeof usbuf->fullname);
1106 strcpy(usbuf->password, "");
1109 /* These are the default flags on new accounts */
1110 usbuf->flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
1112 usbuf->timescalled = 0;
1114 usbuf->axlevel = config.c_initax;
1115 usbuf->USscreenwidth = 80;
1116 usbuf->USscreenheight = 24;
1117 usbuf->lastcall = time(NULL);
1119 /* fetch a new user number */
1120 usbuf->usernum = get_new_user_number();
1122 /* add user to the database */
1124 cdb_store(CDB_USERSBYNUMBER, &usbuf->usernum, sizeof(long), usbuf->fullname, strlen(usbuf->fullname)+1);
1132 * create_user() - back end processing to create a new user
1134 * Set 'newusername' to the desired account name.
1135 * Set 'become_user' to nonzero if this is self-service account creation and we want
1136 * to actually log in as the user we just created, otherwise set it to 0.
1138 int create_user(char *newusername, int become_user)
1140 struct ctdluser usbuf;
1141 struct ctdlroom qrbuf;
1143 char mailboxname[ROOMNAMELEN];
1149 safestrncpy(username, newusername, sizeof username);
1153 if (config.c_auth_mode == AUTHMODE_HOST) {
1155 /* host auth mode */
1158 struct passwd *tempPwdPtr;
1159 char pwdbuffer[256];
1161 #ifdef HAVE_GETPWNAM_R
1162 #ifdef SOLARIS_GETPWUID
1163 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof(pwdbuffer));
1164 #else // SOLARIS_GETPWUID
1165 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
1166 #endif // SOLARIS_GETPWUID
1167 #else // HAVE_GETPWNAM_R
1169 #endif // HAVE_GETPWNAM_R
1170 if (tempPwdPtr != NULL) {
1171 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
1173 if (IsEmptyStr (username))
1175 safestrncpy(username, pd.pw_name, sizeof username);
1179 return (ERROR + NO_SUCH_USER);
1184 if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
1185 if (CtdlTryUserLDAP(username, NULL, 0, username, sizeof username, &uid) != 0) {
1186 return(ERROR + NO_SUCH_USER);
1189 #endif /* HAVE_LDAP */
1191 if ((retval = internal_create_user(username, &usbuf, uid)) != 0)
1195 * Give the user a private mailbox and a configuration room.
1196 * Make the latter an invisible system room.
1198 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
1199 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
1201 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
1202 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
1203 if (CtdlGetRoomLock(&qrbuf, mailboxname) == 0) {
1204 qrbuf.QRflags2 |= QR2_SYSTEM;
1205 CtdlPutRoomLock(&qrbuf);
1208 /* Perform any create functions registered by server extensions */
1209 PerformUserHooks(&usbuf, EVT_NEWUSER);
1211 /* Everything below this line can be bypassed if administratively
1212 * creating a user, instead of doing self-service account creation
1216 /* Now become the user we just created */
1217 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
1218 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
1221 /* Check to make sure we're still who we think we are */
1222 if (CtdlGetUser(&CC->user, CC->curr_user)) {
1223 return (ERROR + INTERNAL_ERROR);
1228 "New user account <%s> has been created, from host %s [%s].\n",
1233 CtdlAideMessage(buf, "User Creation Notice");
1234 CtdlLogPrintf(CTDL_NOTICE, "New user <%s> created\n", username);
1241 * cmd_newu() - create a new user account and log in as that user
1243 void cmd_newu(char *cmdbuf)
1248 if (config.c_auth_mode != AUTHMODE_NATIVE) {
1249 cprintf("%d This system does not use native mode authentication.\n",
1254 if (config.c_disable_newu) {
1255 cprintf("%d Self-service user account creation "
1256 "is disabled on this system.\n", ERROR + NOT_HERE);
1260 if (CC->logged_in) {
1261 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
1265 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
1266 ERROR + MAX_SESSIONS_EXCEEDED,
1267 config.c_nodename, config.c_maxsessions);
1269 extract_token(username, cmdbuf, 0, '|', sizeof username);
1273 if (IsEmptyStr(username)) {
1274 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1278 if ((!strcasecmp(username, "bbs")) ||
1279 (!strcasecmp(username, "new")) ||
1280 (!strcasecmp(username, "."))) {
1281 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
1285 a = create_user(username, 1);
1288 logged_in_response();
1289 } else if (a == ERROR + ALREADY_EXISTS) {
1290 cprintf("%d '%s' already exists.\n",
1291 ERROR + ALREADY_EXISTS, username);
1293 } else if (a == ERROR + INTERNAL_ERROR) {
1294 cprintf("%d Internal error - user record disappeared?\n",
1295 ERROR + INTERNAL_ERROR);
1298 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
1304 * set password - back end api code
1306 void CtdlSetPassword(char *new_pw)
1308 CtdlGetUserLock(&CC->user, CC->curr_user);
1309 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1310 CtdlPutUserLock(&CC->user);
1311 CtdlLogPrintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
1312 PerformSessionHooks(EVT_SETPASS);
1317 * set password - citadel protocol implementation
1319 void cmd_setp(char *new_pw)
1321 int generate_random_password = 0;
1323 if (CtdlAccessCheck(ac_logged_in)) {
1326 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
1327 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
1330 if (CC->is_master) {
1331 cprintf("%d The master prefix password cannot be changed with this command.\n",
1336 if (!strcasecmp(new_pw, "GENERATE_RANDOM_PASSWORD")) {
1337 char random_password[17];
1338 generate_random_password = 1;
1339 snprintf(random_password, sizeof random_password, "%08lx%08lx", random(), random());
1340 CtdlSetPassword(random_password);
1341 cprintf("%d %s\n", CIT_OK, random_password);
1345 if (IsEmptyStr(new_pw)) {
1346 cprintf("%d Password unchanged.\n", CIT_OK);
1349 CtdlSetPassword(new_pw);
1350 cprintf("%d Password changed.\n", CIT_OK);
1356 * cmd_creu() - administratively create a new user account (do not log in to it)
1358 void cmd_creu(char *cmdbuf)
1363 struct ctdluser tmp;
1365 if (CtdlAccessCheck(ac_aide)) {
1369 extract_token(username, cmdbuf, 0, '|', sizeof username);
1370 extract_token(password, cmdbuf, 1, '|', sizeof password);
1376 if (IsEmptyStr(username)) {
1377 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1381 a = create_user(username, 0);
1384 if (!IsEmptyStr(password)) {
1385 CtdlGetUserLock(&tmp, username);
1386 safestrncpy(tmp.password, password, sizeof(tmp.password));
1387 CtdlPutUserLock(&tmp);
1389 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1390 (!IsEmptyStr(password)) ? "and password set" :
1391 "with no password");
1393 } else if (a == ERROR + ALREADY_EXISTS) {
1394 cprintf("%d '%s' already exists.\n", ERROR + ALREADY_EXISTS, username);
1396 } else if ( (config.c_auth_mode != AUTHMODE_NATIVE) && (a == ERROR + NO_SUCH_USER) ) {
1397 cprintf("%d User accounts are not created within Citadel in host authentication mode.\n",
1398 ERROR + NO_SUCH_USER);
1401 cprintf("%d An error occurred creating the user account.\n", ERROR + INTERNAL_ERROR);
1408 * get user parameters
1410 void cmd_getu(char *cmdbuf)
1413 if (CtdlAccessCheck(ac_logged_in))
1416 CtdlGetUser(&CC->user, CC->curr_user);
1417 cprintf("%d %d|%d|%d|\n",
1419 CC->user.USscreenwidth,
1420 CC->user.USscreenheight,
1421 (CC->user.flags & US_USER_SET)
1426 * set user parameters
1428 void cmd_setu(char *new_parms)
1430 if (CtdlAccessCheck(ac_logged_in))
1433 if (num_parms(new_parms) < 3) {
1434 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1437 CtdlGetUserLock(&CC->user, CC->curr_user);
1438 CC->user.USscreenwidth = extract_int(new_parms, 0);
1439 CC->user.USscreenheight = extract_int(new_parms, 1);
1440 CC->user.flags = CC->user.flags & (~US_USER_SET);
1441 CC->user.flags = CC->user.flags |
1442 (extract_int(new_parms, 2) & US_USER_SET);
1444 CtdlPutUserLock(&CC->user);
1445 cprintf("%d Ok\n", CIT_OK);
1449 * set last read pointer
1451 void cmd_slrp(char *new_ptr)
1455 struct visit original_vbuf;
1457 if (CtdlAccessCheck(ac_logged_in)) {
1461 if (!strncasecmp(new_ptr, "highest", 7)) {
1462 newlr = CC->room.QRhighest;
1464 newlr = atol(new_ptr);
1467 CtdlGetUserLock(&CC->user, CC->curr_user);
1469 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1470 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1471 vbuf.v_lastseen = newlr;
1472 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1474 /* Only rewrite the record if it changed */
1475 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1476 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1477 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1480 CtdlPutUserLock(&CC->user);
1481 cprintf("%d %ld\n", CIT_OK, newlr);
1485 void cmd_seen(char *argbuf) {
1486 long target_msgnum = 0L;
1487 int target_setting = 0;
1489 if (CtdlAccessCheck(ac_logged_in)) {
1493 if (num_parms(argbuf) != 2) {
1494 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1498 target_msgnum = extract_long(argbuf, 0);
1499 target_setting = extract_int(argbuf, 1);
1501 CtdlSetSeen(&target_msgnum, 1, target_setting,
1502 ctdlsetseen_seen, NULL, NULL);
1503 cprintf("%d OK\n", CIT_OK);
1507 void cmd_gtsn(char *argbuf) {
1510 if (CtdlAccessCheck(ac_logged_in)) {
1514 CtdlGetSeen(buf, ctdlsetseen_seen);
1515 cprintf("%d %s\n", CIT_OK, buf);
1520 * API function for cmd_invt_kick() and anything else that needs to
1521 * invite or kick out a user to/from a room.
1523 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1525 int CtdlInvtKick(char *iuser, int op) {
1526 struct ctdluser USscratch;
1530 if (CtdlGetUser(&USscratch, iuser) != 0) {
1534 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1536 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1537 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1540 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1541 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1543 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1545 /* post a message in Aide> saying what we just did */
1546 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1548 ((op == 1) ? "invited to" : "kicked out of"),
1551 CtdlAideMessage(bbb,"User Admin Message");
1558 * INVT and KICK commands
1560 void cmd_invt_kick(char *iuser, int op) {
1563 * These commands are only allowed by aides, room aides,
1564 * and room namespace owners
1567 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1568 /* access granted */
1571 cprintf("%d Higher access or room ownership required.\n",
1572 ERROR + HIGHER_ACCESS_REQUIRED);
1576 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1578 cprintf("%d Can't add/remove users from this room.\n",
1583 if (CtdlInvtKick(iuser, op) != 0) {
1584 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1588 cprintf("%d %s %s %s.\n",
1590 ((op == 1) ? "invited to" : "kicked out of"),
1595 void cmd_invt(char *iuser) {cmd_invt_kick(iuser, 1);}
1596 void cmd_kick(char *iuser) {cmd_invt_kick(iuser, 0);}
1599 * Forget (Zap) the current room (API call)
1600 * Returns 0 on success
1602 int CtdlForgetThisRoom(void) {
1605 /* On some systems, Aides are not allowed to forget rooms */
1606 if (is_aide() && (config.c_aide_zap == 0)
1607 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1611 CtdlGetUserLock(&CC->user, CC->curr_user);
1612 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1614 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1615 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1617 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1618 CtdlPutUserLock(&CC->user);
1620 /* Return to the Lobby, so we don't end up in an undefined room */
1621 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
1628 * forget (Zap) the current room
1630 void cmd_forg(char *argbuf)
1633 if (CtdlAccessCheck(ac_logged_in)) {
1637 if (CtdlForgetThisRoom() == 0) {
1638 cprintf("%d Ok\n", CIT_OK);
1641 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1646 * Get Next Unregistered User
1648 void cmd_gnur(char *argbuf)
1650 struct cdbdata *cdbus;
1651 struct ctdluser usbuf;
1653 if (CtdlAccessCheck(ac_aide)) {
1657 if ((CitControl.MMflags & MM_VALID) == 0) {
1658 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1662 /* There are unvalidated users. Traverse the user database,
1663 * and return the first user we find that needs validation.
1665 cdb_rewind(CDB_USERS);
1666 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1667 memset(&usbuf, 0, sizeof(struct ctdluser));
1668 memcpy(&usbuf, cdbus->ptr,
1669 ((cdbus->len > sizeof(struct ctdluser)) ?
1670 sizeof(struct ctdluser) : cdbus->len));
1672 if ((usbuf.flags & US_NEEDVALID)
1673 && (usbuf.axlevel > 0)) {
1674 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1675 cdb_close_cursor(CDB_USERS);
1680 /* If we get to this point, there are no more unvalidated users.
1681 * Therefore we clear the "users need validation" flag.
1684 begin_critical_section(S_CONTROL);
1686 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1688 end_critical_section(S_CONTROL);
1689 cprintf("%d *** End of registration.\n", CIT_OK);
1698 void cmd_vali(char *v_args)
1702 struct ctdluser userbuf;
1704 extract_token(user, v_args, 0, '|', sizeof user);
1705 newax = extract_int(v_args, 1);
1707 if (CtdlAccessCheck(ac_aide)) {
1711 if (CtdlGetUserLock(&userbuf, user) != 0) {
1712 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1716 userbuf.axlevel = newax;
1717 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1719 CtdlPutUserLock(&userbuf);
1721 /* If the access level was set to zero, delete the user */
1723 if (purge_user(user) == 0) {
1724 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1728 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1734 * Traverse the user file...
1736 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1739 struct ctdluser usbuf;
1740 struct cdbdata *cdbus;
1742 cdb_rewind(CDB_USERS);
1744 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1745 memset(&usbuf, 0, sizeof(struct ctdluser));
1746 memcpy(&usbuf, cdbus->ptr,
1747 ((cdbus->len > sizeof(struct ctdluser)) ?
1748 sizeof(struct ctdluser) : cdbus->len));
1750 (*CallBack) (&usbuf, in_data);
1756 * List one user (this works with cmd_list)
1758 void ListThisUser(struct ctdluser *usbuf, void *data)
1762 searchstring = (char *)data;
1763 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1767 if (usbuf->axlevel > 0) {
1768 if ((CC->user.axlevel >= 6)
1769 || ((usbuf->flags & US_UNLISTED) == 0)
1770 || ((CC->internal_pgm))) {
1771 cprintf("%s|%d|%ld|%ld|%ld|%ld||\n",
1775 (long)usbuf->lastcall,
1783 * List users (searchstring may be empty to list all users)
1785 void cmd_list(char *cmdbuf)
1787 char searchstring[256];
1788 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1789 striplt(searchstring);
1790 cprintf("%d \n", LISTING_FOLLOWS);
1791 ForEachUser(ListThisUser, (void *)searchstring );
1799 * assorted info we need to check at login
1801 void cmd_chek(char *argbuf)
1807 if (CtdlAccessCheck(ac_logged_in)) {
1811 CtdlGetUser(&CC->user, CC->curr_user); /* no lock is needed here */
1812 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1815 if (CC->user.axlevel >= 6) {
1817 if (CitControl.MMflags & MM_VALID)
1821 /* check for mail */
1822 mail = InitialMailCheck();
1824 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1829 * check to see if a user exists
1831 void cmd_qusr(char *who)
1833 struct ctdluser usbuf;
1835 if (CtdlGetUser(&usbuf, who) == 0) {
1836 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1838 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1844 * Administrative Get User Parameters
1846 void cmd_agup(char *cmdbuf)
1848 struct ctdluser usbuf;
1849 char requested_user[128];
1851 if (CtdlAccessCheck(ac_aide)) {
1855 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1856 if (CtdlGetUser(&usbuf, requested_user) != 0) {
1857 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1860 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1867 (int) usbuf.axlevel,
1869 (long)usbuf.lastcall,
1876 * Administrative Set User Parameters
1878 void cmd_asup(char *cmdbuf)
1880 struct ctdluser usbuf;
1881 char requested_user[128];
1887 if (CtdlAccessCheck(ac_aide))
1890 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1891 if (CtdlGetUserLock(&usbuf, requested_user) != 0) {
1892 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1895 np = num_parms(cmdbuf);
1897 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1899 usbuf.flags = extract_int(cmdbuf, 2);
1901 usbuf.timescalled = extract_int(cmdbuf, 3);
1903 usbuf.posted = extract_int(cmdbuf, 4);
1905 newax = extract_int(cmdbuf, 5);
1906 if ((newax >= 0) && (newax <= 6)) {
1907 usbuf.axlevel = extract_int(cmdbuf, 5);
1911 usbuf.lastcall = extract_long(cmdbuf, 7);
1914 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1916 CtdlPutUserLock(&usbuf);
1917 if (usbuf.axlevel == 0) {
1918 if (purge_user(requested_user) == 0) {
1924 snprintf(notify, SIZ,
1925 "User \"%s\" has been deleted by %s.\n",
1926 usbuf.fullname, CC->user.fullname);
1927 CtdlAideMessage(notify, "User Deletion Message");
1930 cprintf("%d Ok", CIT_OK);
1932 cprintf(" (%s deleted)", requested_user);
1939 * Check to see if the user who we just sent mail to is logged in. If yes,
1940 * bump the 'new mail' counter for their session. That enables them to
1941 * receive a new mail notification without having to hit the database.
1943 void BumpNewMailCounter(long which_user) {
1946 begin_critical_section(S_SESSION_TABLE);
1948 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1949 if (ptr->user.usernum == which_user) {
1954 end_critical_section(S_SESSION_TABLE);
1959 * Count the number of new mail messages the user has
1963 int num_newmsgs = 0;
1965 num_newmsgs = CC->newmail;
1968 return (num_newmsgs);
1973 * Count the number of new mail messages the user has
1975 int InitialMailCheck()
1977 int num_newmsgs = 0;
1979 char mailboxname[ROOMNAMELEN];
1980 struct ctdlroom mailbox;
1982 struct cdbdata *cdbfr;
1983 long *msglist = NULL;
1986 CtdlMailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1987 if (CtdlGetRoom(&mailbox, mailboxname) != 0)
1989 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1991 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1993 if (cdbfr != NULL) {
1994 msglist = malloc(cdbfr->len);
1995 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1996 num_msgs = cdbfr->len / sizeof(long);
2000 for (a = 0; a < num_msgs; ++a) {
2001 if (msglist[a] > 0L) {
2002 if (msglist[a] > vbuf.v_lastseen) {
2007 if (msglist != NULL)
2010 return (num_newmsgs);
2016 * Set the preferred view for the current user/room combination
2018 void cmd_view(char *cmdbuf) {
2022 if (CtdlAccessCheck(ac_logged_in)) {
2026 requested_view = extract_int(cmdbuf, 0);
2028 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
2029 vbuf.v_view = requested_view;
2030 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
2032 cprintf("%d ok\n", CIT_OK);
2039 void cmd_renu(char *cmdbuf)
2042 char oldname[USERNAME_SIZE];
2043 char newname[USERNAME_SIZE];
2045 if (CtdlAccessCheck(ac_aide)) {
2049 extract_token(oldname, cmdbuf, 0, '|', sizeof oldname);
2050 extract_token(newname, cmdbuf, 1, '|', sizeof newname);
2052 retcode = rename_user(oldname, newname);
2055 cprintf("%d '%s' has been renamed to '%s'.\n", CIT_OK, oldname, newname);
2057 case RENAMEUSER_LOGGED_IN:
2058 cprintf("%d '%s' is currently logged in and cannot be renamed.\n",
2059 ERROR + ALREADY_LOGGED_IN , oldname);
2061 case RENAMEUSER_NOT_FOUND:
2062 cprintf("%d '%s' does not exist.\n", ERROR + NO_SUCH_USER, oldname);
2064 case RENAMEUSER_ALREADY_EXISTS:
2065 cprintf("%d A user named '%s' already exists.\n", ERROR + ALREADY_EXISTS, newname);
2069 cprintf("%d An unknown error occurred.\n", ERROR);
2074 /*****************************************************************************/
2075 /* MODULE INITIALIZATION STUFF */
2076 /*****************************************************************************/
2079 CTDL_MODULE_INIT(user_ops)
2082 CtdlRegisterProtoHook(cmd_user, "USER", "Autoconverted. TODO: document me.");
2083 CtdlRegisterProtoHook(cmd_pass, "PASS", "Autoconverted. TODO: document me.");
2084 CtdlRegisterProtoHook(cmd_creu, "CREU", "Autoconverted. TODO: document me.");
2085 CtdlRegisterProtoHook(cmd_setp, "SETP", "Autoconverted. TODO: document me.");
2086 CtdlRegisterProtoHook(cmd_getu, "GETU", "Autoconverted. TODO: document me.");
2087 CtdlRegisterProtoHook(cmd_setu, "SETU", "Autoconverted. TODO: document me.");
2088 CtdlRegisterProtoHook(cmd_slrp, "SLRP", "Autoconverted. TODO: document me.");
2089 CtdlRegisterProtoHook(cmd_invt, "INVT", "Autoconverted. TODO: document me.");
2090 CtdlRegisterProtoHook(cmd_kick, "KICK", "Autoconverted. TODO: document me.");
2091 CtdlRegisterProtoHook(cmd_forg, "FORG", "Autoconverted. TODO: document me.");
2092 CtdlRegisterProtoHook(cmd_gnur, "GNUR", "Autoconverted. TODO: document me.");
2093 CtdlRegisterProtoHook(cmd_vali, "VALI", "Autoconverted. TODO: document me.");
2094 CtdlRegisterProtoHook(cmd_list, "LIST", "Autoconverted. TODO: document me.");
2095 CtdlRegisterProtoHook(cmd_chek, "CHEK", "Autoconverted. TODO: document me.");
2096 CtdlRegisterProtoHook(cmd_qusr, "QUSR", "Autoconverted. TODO: document me.");
2097 CtdlRegisterProtoHook(cmd_agup, "AGUP", "Autoconverted. TODO: document me.");
2098 CtdlRegisterProtoHook(cmd_asup, "ASUP", "Autoconverted. TODO: document me.");
2099 CtdlRegisterProtoHook(cmd_seen, "SEEN", "Autoconverted. TODO: document me.");
2100 CtdlRegisterProtoHook(cmd_gtsn, "GTSN", "Autoconverted. TODO: document me.");
2101 CtdlRegisterProtoHook(cmd_view, "VIEW", "Autoconverted. TODO: document me.");
2102 CtdlRegisterProtoHook(cmd_renu, "RENU", "Autoconverted. TODO: document me.");
2103 CtdlRegisterProtoHook(cmd_newu, "NEWU", "Autoconverted. TODO: document me.");
2105 /* return our Subversion id for the Log */