2 * Server functions which perform operations on user objects.
4 * Copyright (c) 1987-2011 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License, version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
24 #include <sys/types.h>
27 #ifdef HAVE_SYS_STAT_H
31 #if TIME_WITH_SYS_TIME
32 # include <sys/time.h>
36 # include <sys/time.h>
44 #include <libcitadel.h>
49 #include "sysdep_decls.h"
56 #include "citserver.h"
57 #include "citadel_dirs.h"
60 #include "citadel_ldap.h"
62 #include "ctdl_module.h"
64 #include "internet_addressing.h"
66 /* These pipes are used to talk to the chkpwd daemon, which is forked during startup */
67 int chkpwd_write_pipe[2];
68 int chkpwd_read_pipe[2];
73 * getuser() - retrieve named user into supplied buffer.
74 * returns 0 on success
76 int getuser(struct ctdluser *usbuf, char name[])
78 return CtdlGetUser(usbuf, name);
83 * CtdlGetUser() - retrieve named user into supplied buffer.
84 * returns 0 on success
86 int CtdlGetUserLen(struct ctdluser *usbuf, const char *name, long len)
89 char usernamekey[USERNAME_SIZE];
90 struct cdbdata *cdbus;
93 memset(usbuf, 0, sizeof(struct ctdluser));
96 makeuserkey(usernamekey, name, len);
97 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
99 if (cdbus == NULL) { /* user not found */
103 memcpy(usbuf, cdbus->ptr,
104 ((cdbus->len > sizeof(struct ctdluser)) ?
105 sizeof(struct ctdluser) : cdbus->len));
113 int CtdlGetUser(struct ctdluser *usbuf, char *name)
115 return CtdlGetUserLen(usbuf, name, cutuserkey(name));
120 * CtdlGetUserLock() - same as getuser() but locks the record
122 int CtdlGetUserLock(struct ctdluser *usbuf, char *name)
126 retcode = CtdlGetUser(usbuf, name);
128 begin_critical_section(S_USERS);
135 * lgetuser() - same as getuser() but locks the record
137 int lgetuser(struct ctdluser *usbuf, char *name)
139 return CtdlGetUserLock(usbuf, name);
144 * CtdlPutUser() - write user buffer into the correct place on disk
146 void CtdlPutUser(struct ctdluser *usbuf)
148 char usernamekey[USERNAME_SIZE];
150 makeuserkey(usernamekey,
152 cutuserkey(usbuf->fullname));
154 usbuf->version = REV_LEVEL;
156 usernamekey, strlen(usernamekey),
157 usbuf, sizeof(struct ctdluser));
163 * putuser() - write user buffer into the correct place on disk
165 void putuser(struct ctdluser *usbuf)
172 * CtdlPutUserLock() - same as putuser() but locks the record
174 void CtdlPutUserLock(struct ctdluser *usbuf)
177 end_critical_section(S_USERS);
182 * lputuser() - same as putuser() but locks the record
184 void lputuser(struct ctdluser *usbuf)
186 CtdlPutUserLock(usbuf);
191 * rename_user() - this is tricky because the user's display name is the database key
193 * Returns 0 on success or nonzero if there was an error...
196 int rename_user(char *oldname, char *newname) {
197 int retcode = RENAMEUSER_OK;
198 struct ctdluser usbuf;
200 char oldnamekey[USERNAME_SIZE];
201 char newnamekey[USERNAME_SIZE];
203 /* Create the database keys... */
204 makeuserkey(oldnamekey, oldname, cutuserkey(oldname));
205 makeuserkey(newnamekey, newname, cutuserkey(newname));
207 /* Lock up and get going */
208 begin_critical_section(S_USERS);
210 /* We cannot rename a user who is currently logged in */
211 if (CtdlIsUserLoggedIn(oldname)) {
212 end_critical_section(S_USERS);
213 return RENAMEUSER_LOGGED_IN;
216 if (CtdlGetUser(&usbuf, newname) == 0) {
217 retcode = RENAMEUSER_ALREADY_EXISTS;
221 if (CtdlGetUser(&usbuf, oldname) != 0) {
222 retcode = RENAMEUSER_NOT_FOUND;
225 else { /* Sanity checks succeeded. Now rename the user. */
226 if (usbuf.usernum == 0)
228 CONM_syslog(LOG_DEBUG, "Can not rename user \"Citadel\".\n");
229 retcode = RENAMEUSER_NOT_FOUND;
231 CON_syslog(LOG_DEBUG, "Renaming <%s> to <%s>\n", oldname, newname);
232 cdb_delete(CDB_USERS, oldnamekey, strlen(oldnamekey));
233 safestrncpy(usbuf.fullname, newname, sizeof usbuf.fullname);
235 cdb_store(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long),
236 usbuf.fullname, strlen(usbuf.fullname)+1 );
238 retcode = RENAMEUSER_OK;
244 end_critical_section(S_USERS);
251 * Index-generating function used by Ctdl[Get|Set]Relationship
253 int GenerateRelationshipIndex(char *IndexBuf,
265 TheIndex.iRoomID = RoomID;
266 TheIndex.iRoomGen = RoomGen;
267 TheIndex.iUserID = UserID;
269 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
270 return (sizeof(TheIndex));
276 * Back end for CtdlSetRelationship()
278 void put_visit(visit *newvisit)
283 memset (IndexBuf, 0, sizeof (IndexBuf));
284 /* Generate an index */
285 IndexLen = GenerateRelationshipIndex(IndexBuf,
288 newvisit->v_usernum);
290 /* Store the record */
291 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
292 newvisit, sizeof(visit)
300 * Define a relationship between a user and a room
302 void CtdlSetRelationship(visit *newvisit,
303 struct ctdluser *rel_user,
304 struct ctdlroom *rel_room)
308 /* We don't use these in Citadel because they're implicit by the
309 * index, but they must be present if the database is exported.
311 newvisit->v_roomnum = rel_room->QRnumber;
312 newvisit->v_roomgen = rel_room->QRgen;
313 newvisit->v_usernum = rel_user->usernum;
319 * Locate a relationship between a user and a room
321 void CtdlGetRelationship(visit *vbuf,
322 struct ctdluser *rel_user,
323 struct ctdlroom *rel_room)
328 struct cdbdata *cdbvisit;
330 /* Generate an index */
331 IndexLen = GenerateRelationshipIndex(IndexBuf,
336 /* Clear out the buffer */
337 memset(vbuf, 0, sizeof(visit));
339 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
340 if (cdbvisit != NULL) {
341 memcpy(vbuf, cdbvisit->ptr,
342 ((cdbvisit->len > sizeof(visit)) ?
343 sizeof(visit) : cdbvisit->len));
347 /* If this is the first time the user has seen this room,
348 * set the view to be the default for the room.
350 vbuf->v_view = rel_room->QRdefaultview;
353 /* Set v_seen if necessary */
354 if (vbuf->v_seen[0] == 0) {
355 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
360 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
362 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
366 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
368 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
373 * Check to see if the specified user has Internet mail permission
374 * (returns nonzero if permission is granted)
376 int CtdlCheckInternetMailPermission(struct ctdluser *who) {
378 /* Do not allow twits to send Internet mail */
379 if (who->axlevel <= AxProbU) return(0);
381 /* Globally enabled? */
382 if (config.c_restrict == 0) return(1);
384 /* User flagged ok? */
385 if (who->flags & US_INTERNET) return(2);
387 /* Admin level access? */
388 if (who->axlevel >= AxAideU) return(3);
390 /* No mail for you! */
396 * Is the user currently logged in an Admin?
400 if (CC->user.axlevel >= AxAideU)
408 * Is the user currently logged in an Admin *or* the room Admin for this room?
410 int is_room_aide(void)
413 if (!CC->logged_in) {
417 if ((CC->user.axlevel >= AxAideU)
418 || (CC->room.QRroomaide == CC->user.usernum)) {
426 * CtdlGetUserByNumber() - get user by number
427 * returns 0 if user was found
429 * Note: fetching a user this way requires one additional database operation.
431 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number)
433 struct cdbdata *cdbun;
436 cdbun = cdb_fetch(CDB_USERSBYNUMBER, &number, sizeof(long));
438 CON_syslog(LOG_INFO, "User %ld not found\n", number);
442 CON_syslog(LOG_INFO, "User %ld maps to %s\n", number, cdbun->ptr);
443 r = CtdlGetUser(usbuf, cdbun->ptr);
449 * getuserbynumber() - get user by number
450 * returns 0 if user was found
452 * Note: fetching a user this way requires one additional database operation.
454 int getuserbynumber(struct ctdluser *usbuf, long number)
456 return CtdlGetUserByNumber(usbuf, number);
462 * Helper function for rebuild_usersbynumber()
464 void rebuild_ubn_for_user(struct ctdluser *usbuf, void *data) {
467 struct ubnlist *next;
468 char username[USERNAME_SIZE];
472 static struct ubnlist *u = NULL;
473 struct ubnlist *ptr = NULL;
475 /* Lazy programming here. Call this function as a ForEachUser backend
476 * in order to queue up the room names, or call it with a null user
477 * to make it do the processing.
480 ptr = (struct ubnlist *) malloc(sizeof (struct ubnlist));
481 if (ptr == NULL) return;
483 ptr->usernum = usbuf->usernum;
484 safestrncpy(ptr->username, usbuf->fullname, sizeof ptr->username);
491 CON_syslog(LOG_DEBUG, "Rebuilding usersbynumber index %10ld : %s\n",
492 u->usernum, u->username);
493 cdb_store(CDB_USERSBYNUMBER, &u->usernum, sizeof(long), u->username, strlen(u->username)+1);
504 * Rebuild the users-by-number index
506 void rebuild_usersbynumber(void) {
507 cdb_trunc(CDB_USERSBYNUMBER); /* delete the old indices */
508 ForEachUser(rebuild_ubn_for_user, NULL); /* enumerate the users */
509 rebuild_ubn_for_user(NULL, NULL); /* and index them */
515 * getuserbyuid() - get user by system uid (for PAM mode authentication)
516 * returns 0 if user was found
518 * WARNING: don't use this function unless you absolutely have to. It does
519 * a sequential search and therefore is computationally expensive.
521 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
523 struct cdbdata *cdbus;
525 cdb_rewind(CDB_USERS);
527 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
528 memset(usbuf, 0, sizeof(struct ctdluser));
529 memcpy(usbuf, cdbus->ptr,
530 ((cdbus->len > sizeof(struct ctdluser)) ?
531 sizeof(struct ctdluser) : cdbus->len));
533 if (usbuf->uid == number) {
534 cdb_close_cursor(CDB_USERS);
542 * Back end for cmd_user() and its ilk
544 * NOTE: "authname" should only be used if we are attempting to use the "master user" feature
546 int CtdlLoginExistingUser(char *authname, const char *trythisname)
552 CON_syslog(LOG_DEBUG, "CtdlLoginExistingUser(%s, %s)\n", authname, trythisname);
554 if ((CC->logged_in)) {
555 return login_already_logged_in;
558 if (trythisname == NULL) return login_not_found;
560 if (!strncasecmp(trythisname, "SYS_", 4))
562 CON_syslog(LOG_DEBUG, "System user \"%s\" is not allowed to log in.\n", trythisname);
563 return login_not_found;
566 /* If a "master user" is defined, handle its authentication if specified */
568 if (strlen(config.c_master_user) > 0) if (strlen(config.c_master_pass) > 0) if (authname) {
569 if (!strcasecmp(authname, config.c_master_user)) {
574 /* Continue attempting user validation... */
575 safestrncpy(username, trythisname, sizeof (username));
577 len = cutuserkey(username);
579 if (IsEmptyStr(username)) {
580 return login_not_found;
583 if (config.c_auth_mode == AUTHMODE_HOST) {
588 struct passwd *tempPwdPtr;
591 CON_syslog(LOG_DEBUG, "asking host about <%s>\n", username);
592 #ifdef HAVE_GETPWNAM_R
593 #ifdef SOLARIS_GETPWUID
594 CON_syslog(LOG_DEBUG, "Calling getpwnam_r()\n");
595 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
596 #else // SOLARIS_GETPWUID
597 CONM_syslog(LOG_DEBUG, "Calling getpwnam_r()\n");
598 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
599 #endif // SOLARIS_GETPWUID
600 #else // HAVE_GETPWNAM_R
601 CON_syslog(LOG_DEBUG, "SHOULD NEVER GET HERE!!!\n");
603 #endif // HAVE_GETPWNAM_R
604 if (tempPwdPtr == NULL) {
605 CON_syslog(LOG_DEBUG, "no such user <%s>\n", username);
606 return login_not_found;
609 /* Locate the associated Citadel account.
610 * If not found, make one attempt to create it.
612 found_user = getuserbyuid(&CC->user, pd.pw_uid);
613 CON_syslog(LOG_DEBUG, "found it: uid=%ld, gecos=%s here: %d\n",
614 (long)pd.pw_uid, pd.pw_gecos, found_user);
615 if (found_user != 0) {
616 len = cutuserkey(username);
617 create_user(username, len, 0);
618 found_user = getuserbyuid(&CC->user, pd.pw_uid);
624 else if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
632 found_user = CtdlTryUserLDAP(username, ldap_dn, sizeof ldap_dn, ldap_cn, sizeof ldap_cn, &ldap_uid);
633 if (found_user != 0) {
634 return login_not_found;
637 found_user = getuserbyuid(&CC->user, ldap_uid);
638 if (found_user != 0) {
639 create_user(username, len, 0);
640 found_user = getuserbyuid(&CC->user, ldap_uid);
643 if (found_user == 0) {
644 if (CC->ldap_dn != NULL) free(CC->ldap_dn);
645 CC->ldap_dn = strdup(ldap_dn);
652 /* native auth mode */
654 struct recptypes *valid = NULL;
656 /* First, try to log in as if the supplied name is a display name */
657 found_user = CtdlGetUser(&CC->user, username);
659 /* If that didn't work, try to log in as if the supplied name
660 * is an e-mail address
662 if (found_user != 0) {
663 valid = validate_recipients(username, NULL, 0);
665 if (valid->num_local == 1) {
666 found_user = CtdlGetUser(&CC->user, valid->recp_local);
668 free_recipients(valid);
673 /* Did we find something? */
674 if (found_user == 0) {
675 if (((CC->nologin)) && (CC->user.axlevel < AxAideU)) {
676 return login_too_many_users;
678 safestrncpy(CC->curr_user, CC->user.fullname,
679 sizeof CC->curr_user);
683 return login_not_found;
691 void cmd_user(char *cmdbuf)
696 CON_syslog(LOG_DEBUG, "cmd_user(%s)\n", cmdbuf);
697 extract_token(username, cmdbuf, 0, '|', sizeof username);
698 CON_syslog(LOG_DEBUG, "username: %s\n", username);
700 CON_syslog(LOG_DEBUG, "username: %s\n", username);
702 a = CtdlLoginExistingUser(NULL, username);
704 case login_already_logged_in:
705 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
707 case login_too_many_users:
709 "Too many users are already online "
711 ERROR + MAX_SESSIONS_EXCEEDED,
712 config.c_nodename, config.c_maxsessions);
715 cprintf("%d Password required for %s\n",
716 MORE_DATA, CC->curr_user);
718 case login_not_found:
719 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
722 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
729 * session startup code which is common to both cmd_pass() and cmd_newu()
733 struct CitContext *CCC = CC;
736 CON_syslog(LOG_NOTICE, "<%s> logged in\n", CCC->curr_user);
738 CtdlGetUserLock(&CCC->user, CCC->curr_user);
739 ++(CCC->user.timescalled);
740 CCC->previous_login = CCC->user.lastcall;
741 time(&CCC->user.lastcall);
743 /* If this user's name is the name of the system administrator
744 * (as specified in setup), automatically assign access level 6.
746 if (!strcasecmp(CCC->user.fullname, config.c_sysadm)) {
747 CCC->user.axlevel = AxAideU;
750 /* If we're authenticating off the host system, automatically give
751 * root the highest level of access.
753 if (config.c_auth_mode == AUTHMODE_HOST) {
754 if (CCC->user.uid == 0) {
755 CCC->user.axlevel = AxAideU;
759 CtdlPutUserLock(&CCC->user);
762 * Populate CCC->cs_inet_email with a default address. This will be
763 * overwritten with the user's directory address, if one exists, when
764 * the vCard module's login hook runs.
766 snprintf(CCC->cs_inet_email, sizeof CCC->cs_inet_email, "%s@%s",
767 CCC->user.fullname, config.c_fqdn);
768 convert_spaces_to_underscores(CCC->cs_inet_email);
770 /* Create any personal rooms required by the system.
771 * (Technically, MAILROOM should be there already, but just in case...)
773 CtdlCreateRoom(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
774 CtdlCreateRoom(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
775 CtdlCreateRoom(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
776 CtdlCreateRoom(USERDRAFTROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
778 /* Run any startup routines registered by loadable modules */
779 PerformSessionHooks(EVT_LOGIN);
781 /* Enter the lobby */
782 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
786 void logged_in_response(void)
788 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
789 CIT_OK, CC->user.fullname, CC->user.axlevel,
790 CC->user.timescalled, CC->user.posted,
791 CC->user.flags, CC->user.usernum,
797 void CtdlUserLogout(void)
799 CitContext *CCC = MyContext();
801 CON_syslog(LOG_DEBUG, "CtdlUserLogout() logging out <%s> from session %d",
802 CCC->curr_user, CCC->cs_pid
806 * If there is a download in progress, abort it.
808 if (CCC->download_fp != NULL) {
809 fclose(CCC->download_fp);
810 CCC->download_fp = NULL;
814 * If there is an upload in progress, abort it.
816 if (CCC->upload_fp != NULL) {
820 /* Run any hooks registered by modules... */
821 PerformSessionHooks(EVT_LOGOUT);
824 * Clear out some session data. Most likely, the CitContext for this
825 * session is about to get nuked when the session disconnects, but
826 * since it's possible to log in again without reconnecting, we cannot
827 * make that assumption.
829 strcpy(CCC->fake_username, "");
830 strcpy(CCC->fake_hostname, "");
831 strcpy(CCC->fake_roomname, "");
834 /* Check to see if the user was deleted whilst logged in and purge them if necessary */
835 if ((CCC->user.axlevel == AxDeleted) && (CCC->user.usernum))
836 purge_user(CCC->user.fullname);
838 /* Clear out the user record in memory so we don't behave like a ghost */
839 memset(&CCC->user, 0, sizeof(struct ctdluser));
840 CCC->curr_user[0] = 0;
842 CCC->cs_inet_email[0] = 0;
843 CCC->cs_inet_other_emails[0] = 0;
844 CCC->cs_inet_fn[0] = 0;
845 CCC->fake_username[0] = 0;
846 CCC->fake_hostname[0] = 0;
847 CCC->fake_roomname[0] = 0;
850 /* Free any output buffers */
856 * Validate a password on the host unix system by talking to the chkpwd daemon
858 static int validpw(uid_t uid, const char *pass)
863 if (IsEmptyStr(pass)) {
864 CON_syslog(LOG_DEBUG, "Refusing to chkpwd for uid=%d with empty password.\n", uid);
868 CON_syslog(LOG_DEBUG, "Validating password for uid=%d using chkpwd...\n", uid);
870 begin_critical_section(S_CHKPWD);
871 rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
873 CON_syslog(LOG_EMERG, "Communicatino with chkpwd broken: %s\n", strerror(errno));
874 end_critical_section(S_CHKPWD);
877 rv = write(chkpwd_write_pipe[1], pass, 256);
879 CON_syslog(LOG_EMERG, "Communicatino with chkpwd broken: %s\n", strerror(errno));
880 end_critical_section(S_CHKPWD);
883 rv = read(chkpwd_read_pipe[0], buf, 4);
885 CON_syslog(LOG_EMERG, "Communicatino with chkpwd broken: %s\n", strerror(errno));
886 end_critical_section(S_CHKPWD);
889 end_critical_section(S_CHKPWD);
891 if (!strncmp(buf, "PASS", 4)) {
892 CONM_syslog(LOG_DEBUG, "...pass\n");
896 CONM_syslog(LOG_DEBUG, "...fail\n");
901 * Start up the chkpwd daemon so validpw() has something to talk to
903 void start_chkpwd_daemon(void) {
905 struct stat filestats;
908 CONM_syslog(LOG_DEBUG, "Starting chkpwd daemon for host authentication mode\n");
910 if ((stat(file_chkpwd, &filestats)==-1) ||
911 (filestats.st_size==0)){
912 printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
915 if (pipe(chkpwd_write_pipe) != 0) {
916 CON_syslog(LOG_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
919 if (pipe(chkpwd_read_pipe) != 0) {
920 CON_syslog(LOG_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
925 if (chkpwd_pid < 0) {
926 CON_syslog(LOG_EMERG, "Unable to fork chkpwd daemon: %s\n", strerror(errno));
929 if (chkpwd_pid == 0) {
930 CONM_syslog(LOG_DEBUG, "Now calling dup2() write\n");
931 dup2(chkpwd_write_pipe[0], 0);
932 CONM_syslog(LOG_DEBUG, "Now calling dup2() write\n");
933 dup2(chkpwd_read_pipe[1], 1);
934 CONM_syslog(LOG_DEBUG, "Now closing stuff\n");
935 for (i=2; i<256; ++i) close(i);
936 CON_syslog(LOG_DEBUG, "Now calling execl(%s)\n", file_chkpwd);
937 execl(file_chkpwd, file_chkpwd, NULL);
938 CON_syslog(LOG_EMERG, "Unable to exec chkpwd daemon: %s\n", strerror(errno));
945 int CtdlTryPassword(const char *password, long len)
948 CitContext *CCC = CC;
950 if ((CCC->logged_in)) {
951 CONM_syslog(LOG_WARNING, "CtdlTryPassword: already logged in\n");
952 return pass_already_logged_in;
954 if (!strcmp(CCC->curr_user, NLI)) {
955 CONM_syslog(LOG_WARNING, "CtdlTryPassword: no user selected\n");
958 if (CtdlGetUser(&CCC->user, CCC->curr_user)) {
959 CONM_syslog(LOG_ERR, "CtdlTryPassword: internal error\n");
960 return pass_internal_error;
962 if (password == NULL) {
963 CONM_syslog(LOG_INFO, "CtdlTryPassword: NULL password string supplied\n");
964 return pass_wrong_password;
967 if (CCC->is_master) {
968 code = strcmp(password, config.c_master_pass);
971 else if (config.c_auth_mode == AUTHMODE_HOST) {
975 if (validpw(CCC->user.uid, password)) {
979 * sooper-seekrit hack: populate the password field in the
980 * citadel database with the password that the user typed,
981 * if it's correct. This allows most sites to convert from
982 * host auth to native auth if they want to. If you think
983 * this is a security hazard, comment it out.
986 CtdlGetUserLock(&CCC->user, CCC->curr_user);
987 safestrncpy(CCC->user.password, password, sizeof CCC->user.password);
988 CtdlPutUserLock(&CCC->user);
991 * (sooper-seekrit hack ends here)
1001 else if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
1003 /* LDAP auth mode */
1005 if ((CCC->ldap_dn) && (!CtdlTryPasswordLDAP(CCC->ldap_dn, password))) {
1016 /* native auth mode */
1019 pw = (char*) malloc(len + 1);
1020 memcpy(pw, password, len + 1);
1022 strproc(CCC->user.password);
1023 code = strcasecmp(CCC->user.password, pw);
1026 strproc(CCC->user.password);
1027 code = strcasecmp(CCC->user.password, pw);
1036 CON_syslog(LOG_WARNING, "Bad password specified for <%s> Service <%s> Port <%ld> Remote <%s / %s>\n",
1044 //citserver[5610]: Bad password specified for <willi> Service <citadel-TCP> Remote <PotzBlitz / >
1046 return pass_wrong_password;
1051 void cmd_pass(char *buf)
1057 memset(password, 0, sizeof(password));
1058 len = extract_token(password, buf, 0, '|', sizeof password);
1059 a = CtdlTryPassword(password, len);
1062 case pass_already_logged_in:
1063 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
1066 cprintf("%d You must send a name with USER first.\n",
1067 ERROR + USERNAME_REQUIRED);
1069 case pass_wrong_password:
1070 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
1073 logged_in_response();
1081 * Delete a user record *and* all of its related resources.
1083 int purge_user(char pname[])
1086 struct ctdluser usbuf;
1087 char usernamekey[USERNAME_SIZE];
1089 makeuserkey(usernamekey, pname, cutuserkey(pname));
1091 /* If the name is empty we can't find them in the DB any way so just return */
1092 if (IsEmptyStr(pname))
1093 return (ERROR + NO_SUCH_USER);
1095 if (CtdlGetUser(&usbuf, pname) != 0) {
1096 CON_syslog(LOG_ERR, "Cannot purge user <%s> - not found\n", pname);
1097 return (ERROR + NO_SUCH_USER);
1099 /* Don't delete a user who is currently logged in. Instead, just
1100 * set the access level to 0, and let the account get swept up
1101 * during the next purge.
1103 if (CtdlIsUserLoggedInByNum(usbuf.usernum)) {
1104 CON_syslog(LOG_WARNING, "User <%s> is logged in; not deleting.\n", pname);
1105 usbuf.axlevel = AxDeleted;
1106 CtdlPutUser(&usbuf);
1109 CON_syslog(LOG_NOTICE, "Deleting user <%s>\n", pname);
1113 * This should all be wrapped in a S_USERS mutex.
1114 * Without the mutex the user could log in before we get to the next function
1115 * That would truly mess things up :-(
1116 * I would like to see the S_USERS start before the CtdlIsUserLoggedInByNum() above
1117 * and end after the user has been deleted from the database, below.
1118 * Question is should we enter the EVT_PURGEUSER whilst S_USERS is active?
1121 /* Perform any purge functions registered by server extensions */
1122 PerformUserHooks(&usbuf, EVT_PURGEUSER);
1124 /* delete any existing user/room relationships */
1125 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
1127 /* delete the users-by-number index record */
1128 cdb_delete(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long));
1130 /* delete the userlog entry */
1131 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
1133 /* remove the user's bio file */
1141 /* remove the user's picture */
1153 int internal_create_user (const char *username, long len, struct ctdluser *usbuf, uid_t uid)
1155 if (!CtdlGetUserLen(usbuf, username, len)) {
1156 return (ERROR + ALREADY_EXISTS);
1159 /* Go ahead and initialize a new user record */
1160 memset(usbuf, 0, sizeof(struct ctdluser));
1161 safestrncpy(usbuf->fullname, username, sizeof usbuf->fullname);
1162 strcpy(usbuf->password, "");
1165 /* These are the default flags on new accounts */
1166 usbuf->flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
1168 usbuf->timescalled = 0;
1170 usbuf->axlevel = config.c_initax;
1171 usbuf->lastcall = time(NULL);
1173 /* fetch a new user number */
1174 usbuf->usernum = get_new_user_number();
1176 /* add user to the database */
1178 cdb_store(CDB_USERSBYNUMBER, &usbuf->usernum, sizeof(long), usbuf->fullname, strlen(usbuf->fullname)+1);
1186 * create_user() - back end processing to create a new user
1188 * Set 'newusername' to the desired account name.
1189 * Set 'become_user' to nonzero if this is self-service account creation and we want
1190 * to actually log in as the user we just created, otherwise set it to 0.
1192 int create_user(const char *newusername, long len, int become_user)
1194 struct ctdluser usbuf;
1195 struct ctdlroom qrbuf;
1197 char mailboxname[ROOMNAMELEN];
1203 safestrncpy(username, newusername, sizeof username);
1207 if (config.c_auth_mode == AUTHMODE_HOST) {
1209 /* host auth mode */
1212 struct passwd *tempPwdPtr;
1213 char pwdbuffer[SIZ];
1215 #ifdef HAVE_GETPWNAM_R
1216 #ifdef SOLARIS_GETPWUID
1217 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof(pwdbuffer));
1218 #else // SOLARIS_GETPWUID
1219 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
1220 #endif // SOLARIS_GETPWUID
1221 #else // HAVE_GETPWNAM_R
1223 #endif // HAVE_GETPWNAM_R
1224 if (tempPwdPtr != NULL) {
1225 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
1227 if (IsEmptyStr (username))
1229 safestrncpy(username, pd.pw_name, sizeof username);
1230 len = cutuserkey(username);
1234 return (ERROR + NO_SUCH_USER);
1239 if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
1240 if (CtdlTryUserLDAP(username, NULL, 0, username, sizeof username, &uid) != 0) {
1241 return(ERROR + NO_SUCH_USER);
1244 #endif /* HAVE_LDAP */
1246 if ((retval = internal_create_user(username, len, &usbuf, uid)) != 0)
1250 * Give the user a private mailbox and a configuration room.
1251 * Make the latter an invisible system room.
1253 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
1254 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
1256 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
1257 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
1258 if (CtdlGetRoomLock(&qrbuf, mailboxname) == 0) {
1259 qrbuf.QRflags2 |= QR2_SYSTEM;
1260 CtdlPutRoomLock(&qrbuf);
1263 /* Perform any create functions registered by server extensions */
1264 PerformUserHooks(&usbuf, EVT_NEWUSER);
1266 /* Everything below this line can be bypassed if administratively
1267 * creating a user, instead of doing self-service account creation
1271 /* Now become the user we just created */
1272 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
1273 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
1276 /* Check to make sure we're still who we think we are */
1277 if (CtdlGetUser(&CC->user, CC->curr_user)) {
1278 return (ERROR + INTERNAL_ERROR);
1283 "New user account <%s> has been created, from host %s [%s].\n",
1288 CtdlAideMessage(buf, "User Creation Notice");
1289 CON_syslog(LOG_NOTICE, "New user <%s> created\n", username);
1296 * cmd_newu() - create a new user account and log in as that user
1298 void cmd_newu(char *cmdbuf)
1304 if (config.c_auth_mode != AUTHMODE_NATIVE) {
1305 cprintf("%d This system does not use native mode authentication.\n",
1310 if (config.c_disable_newu) {
1311 cprintf("%d Self-service user account creation "
1312 "is disabled on this system.\n", ERROR + NOT_HERE);
1316 if (CC->logged_in) {
1317 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
1321 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
1322 ERROR + MAX_SESSIONS_EXCEEDED,
1323 config.c_nodename, config.c_maxsessions);
1325 extract_token(username, cmdbuf, 0, '|', sizeof username);
1327 len = cutuserkey(username);
1329 if (IsEmptyStr(username)) {
1330 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1334 if ((!strcasecmp(username, "bbs")) ||
1335 (!strcasecmp(username, "new")) ||
1336 (!strcasecmp(username, "."))) {
1337 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
1341 a = create_user(username, len, 1);
1344 logged_in_response();
1345 } else if (a == ERROR + ALREADY_EXISTS) {
1346 cprintf("%d '%s' already exists.\n",
1347 ERROR + ALREADY_EXISTS, username);
1349 } else if (a == ERROR + INTERNAL_ERROR) {
1350 cprintf("%d Internal error - user record disappeared?\n",
1351 ERROR + INTERNAL_ERROR);
1354 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
1360 * set password - back end api code
1362 void CtdlSetPassword(char *new_pw)
1364 CtdlGetUserLock(&CC->user, CC->curr_user);
1365 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1366 CtdlPutUserLock(&CC->user);
1367 CON_syslog(LOG_INFO, "Password changed for user <%s>\n", CC->curr_user);
1368 PerformSessionHooks(EVT_SETPASS);
1373 * set password - citadel protocol implementation
1375 void cmd_setp(char *new_pw)
1377 if (CtdlAccessCheck(ac_logged_in)) {
1380 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
1381 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
1384 if (CC->is_master) {
1385 cprintf("%d The master prefix password cannot be changed with this command.\n",
1390 if (!strcasecmp(new_pw, "GENERATE_RANDOM_PASSWORD")) {
1391 char random_password[17];
1392 snprintf(random_password, sizeof random_password, "%08lx%08lx", random(), random());
1393 CtdlSetPassword(random_password);
1394 cprintf("%d %s\n", CIT_OK, random_password);
1398 if (IsEmptyStr(new_pw)) {
1399 cprintf("%d Password unchanged.\n", CIT_OK);
1402 CtdlSetPassword(new_pw);
1403 cprintf("%d Password changed.\n", CIT_OK);
1409 * cmd_creu() - administratively create a new user account (do not log in to it)
1411 void cmd_creu(char *cmdbuf)
1417 struct ctdluser tmp;
1419 if (CtdlAccessCheck(ac_aide)) {
1423 extract_token(username, cmdbuf, 0, '|', sizeof username);
1426 if (IsEmptyStr(username)) {
1427 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1430 len = cutuserkey(username);
1433 extract_token(password, cmdbuf, 1, '|', sizeof password);
1435 a = create_user(username, len, 0);
1438 if (!IsEmptyStr(password)) {
1439 CtdlGetUserLock(&tmp, username);
1440 safestrncpy(tmp.password, password, sizeof(tmp.password));
1441 CtdlPutUserLock(&tmp);
1443 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1444 (!IsEmptyStr(password)) ? "and password set" :
1445 "with no password");
1447 } else if (a == ERROR + ALREADY_EXISTS) {
1448 cprintf("%d '%s' already exists.\n", ERROR + ALREADY_EXISTS, username);
1450 } else if ( (config.c_auth_mode != AUTHMODE_NATIVE) && (a == ERROR + NO_SUCH_USER) ) {
1451 cprintf("%d User accounts are not created within Citadel in host authentication mode.\n",
1452 ERROR + NO_SUCH_USER);
1455 cprintf("%d An error occurred creating the user account.\n", ERROR + INTERNAL_ERROR);
1462 * get user parameters
1464 void cmd_getu(char *cmdbuf)
1467 if (CtdlAccessCheck(ac_logged_in))
1470 CtdlGetUser(&CC->user, CC->curr_user);
1471 cprintf("%d 80|24|%d|\n",
1473 (CC->user.flags & US_USER_SET)
1478 * set user parameters
1480 void cmd_setu(char *new_parms)
1482 if (CtdlAccessCheck(ac_logged_in))
1485 if (num_parms(new_parms) < 3) {
1486 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1489 CtdlGetUserLock(&CC->user, CC->curr_user);
1490 CC->user.flags = CC->user.flags & (~US_USER_SET);
1491 CC->user.flags = CC->user.flags | (extract_int(new_parms, 2) & US_USER_SET);
1492 CtdlPutUserLock(&CC->user);
1493 cprintf("%d Ok\n", CIT_OK);
1497 * set last read pointer
1499 void cmd_slrp(char *new_ptr)
1503 visit original_vbuf;
1505 if (CtdlAccessCheck(ac_logged_in)) {
1509 if (!strncasecmp(new_ptr, "highest", 7)) {
1510 newlr = CC->room.QRhighest;
1512 newlr = atol(new_ptr);
1515 CtdlGetUserLock(&CC->user, CC->curr_user);
1517 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1518 memcpy(&original_vbuf, &vbuf, sizeof(visit));
1519 vbuf.v_lastseen = newlr;
1520 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1522 /* Only rewrite the record if it changed */
1523 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1524 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1525 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1528 CtdlPutUserLock(&CC->user);
1529 cprintf("%d %ld\n", CIT_OK, newlr);
1533 void cmd_seen(char *argbuf) {
1534 long target_msgnum = 0L;
1535 int target_setting = 0;
1537 if (CtdlAccessCheck(ac_logged_in)) {
1541 if (num_parms(argbuf) != 2) {
1542 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1546 target_msgnum = extract_long(argbuf, 0);
1547 target_setting = extract_int(argbuf, 1);
1549 CtdlSetSeen(&target_msgnum, 1, target_setting,
1550 ctdlsetseen_seen, NULL, NULL);
1551 cprintf("%d OK\n", CIT_OK);
1555 void cmd_gtsn(char *argbuf) {
1558 if (CtdlAccessCheck(ac_logged_in)) {
1562 /* Learn about the user and room in question */
1563 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1565 cprintf("%d ", CIT_OK);
1566 client_write(vbuf.v_seen, strlen(vbuf.v_seen));
1567 client_write(HKEY("\n"));
1572 * API function for cmd_invt_kick() and anything else that needs to
1573 * invite or kick out a user to/from a room.
1575 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1577 int CtdlInvtKick(char *iuser, int op) {
1578 struct ctdluser USscratch;
1582 if (CtdlGetUser(&USscratch, iuser) != 0) {
1586 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1588 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1589 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1592 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1593 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1595 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1597 /* post a message in Aide> saying what we just did */
1598 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1600 ((op == 1) ? "invited to" : "kicked out of"),
1602 (CC->logged_in ? CC->user.fullname : "an administrator")
1604 CtdlAideMessage(bbb,"User Admin Message");
1611 * INVT and KICK commands
1613 void cmd_invt_kick(char *iuser, int op) {
1616 * These commands are only allowed by admins, room admins,
1617 * and room namespace owners
1619 if (is_room_aide()) {
1620 /* access granted */
1621 } else if ( ((atol(CC->room.QRname) == CC->user.usernum) ) && (CC->user.usernum != 0) ) {
1622 /* access granted */
1625 cprintf("%d Higher access or room ownership required.\n",
1626 ERROR + HIGHER_ACCESS_REQUIRED);
1630 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1632 cprintf("%d Can't add/remove users from this room.\n",
1637 if (CtdlInvtKick(iuser, op) != 0) {
1638 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1642 cprintf("%d %s %s %s.\n",
1644 ((op == 1) ? "invited to" : "kicked out of"),
1649 void cmd_invt(char *iuser) {cmd_invt_kick(iuser, 1);}
1650 void cmd_kick(char *iuser) {cmd_invt_kick(iuser, 0);}
1653 * Forget (Zap) the current room (API call)
1654 * Returns 0 on success
1656 int CtdlForgetThisRoom(void) {
1659 /* On some systems, Admins are not allowed to forget rooms */
1660 if (is_aide() && (config.c_aide_zap == 0)
1661 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1665 CtdlGetUserLock(&CC->user, CC->curr_user);
1666 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1668 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1669 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1671 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1672 CtdlPutUserLock(&CC->user);
1674 /* Return to the Lobby, so we don't end up in an undefined room */
1675 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
1682 * forget (Zap) the current room
1684 void cmd_forg(char *argbuf)
1687 if (CtdlAccessCheck(ac_logged_in)) {
1691 if (CtdlForgetThisRoom() == 0) {
1692 cprintf("%d Ok\n", CIT_OK);
1695 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1700 * Get Next Unregistered User
1702 void cmd_gnur(char *argbuf)
1704 struct cdbdata *cdbus;
1705 struct ctdluser usbuf;
1707 if (CtdlAccessCheck(ac_aide)) {
1711 if ((CitControl.MMflags & MM_VALID) == 0) {
1712 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1716 /* There are unvalidated users. Traverse the user database,
1717 * and return the first user we find that needs validation.
1719 cdb_rewind(CDB_USERS);
1720 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1721 memset(&usbuf, 0, sizeof(struct ctdluser));
1722 memcpy(&usbuf, cdbus->ptr,
1723 ((cdbus->len > sizeof(struct ctdluser)) ?
1724 sizeof(struct ctdluser) : cdbus->len));
1726 if ((usbuf.flags & US_NEEDVALID)
1727 && (usbuf.axlevel > AxDeleted)) {
1728 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1729 cdb_close_cursor(CDB_USERS);
1734 /* If we get to this point, there are no more unvalidated users.
1735 * Therefore we clear the "users need validation" flag.
1738 begin_critical_section(S_CONTROL);
1740 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1742 end_critical_section(S_CONTROL);
1743 cprintf("%d *** End of registration.\n", CIT_OK);
1752 void cmd_vali(char *v_args)
1756 struct ctdluser userbuf;
1758 extract_token(user, v_args, 0, '|', sizeof user);
1759 newax = extract_int(v_args, 1);
1761 if (CtdlAccessCheck(ac_aide) ||
1762 (newax > AxAideU) ||
1763 (newax < AxDeleted)) {
1767 if (CtdlGetUserLock(&userbuf, user) != 0) {
1768 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1772 userbuf.axlevel = newax;
1773 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1775 CtdlPutUserLock(&userbuf);
1777 /* If the access level was set to zero, delete the user */
1779 if (purge_user(user) == 0) {
1780 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1784 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1790 * Traverse the user file...
1792 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1795 struct ctdluser usbuf;
1796 struct cdbdata *cdbus;
1798 cdb_rewind(CDB_USERS);
1800 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1801 memset(&usbuf, 0, sizeof(struct ctdluser));
1802 memcpy(&usbuf, cdbus->ptr,
1803 ((cdbus->len > sizeof(struct ctdluser)) ?
1804 sizeof(struct ctdluser) : cdbus->len));
1806 (*CallBack) (&usbuf, in_data);
1812 * List one user (this works with cmd_list)
1814 void ListThisUser(struct ctdluser *usbuf, void *data)
1818 searchstring = (char *)data;
1819 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1823 if (usbuf->axlevel > AxDeleted) {
1824 if ((CC->user.axlevel >= AxAideU)
1825 || ((usbuf->flags & US_UNLISTED) == 0)
1826 || ((CC->internal_pgm))) {
1827 cprintf("%s|%d|%ld|%ld|%ld|%ld||\n",
1831 (long)usbuf->lastcall,
1839 * List users (searchstring may be empty to list all users)
1841 void cmd_list(char *cmdbuf)
1843 char searchstring[256];
1844 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1845 striplt(searchstring);
1846 cprintf("%d \n", LISTING_FOLLOWS);
1847 ForEachUser(ListThisUser, (void *)searchstring );
1855 * assorted info we need to check at login
1857 void cmd_chek(char *argbuf)
1863 if (CtdlAccessCheck(ac_logged_in)) {
1867 CtdlGetUser(&CC->user, CC->curr_user); /* no lock is needed here */
1868 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1871 if (CC->user.axlevel >= AxAideU) {
1873 if (CitControl.MMflags & MM_VALID)
1877 /* check for mail */
1878 mail = InitialMailCheck();
1880 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1885 * check to see if a user exists
1887 void cmd_qusr(char *who)
1889 struct ctdluser usbuf;
1891 if (CtdlGetUser(&usbuf, who) == 0) {
1892 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1894 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1900 * Administrative Get User Parameters
1902 void cmd_agup(char *cmdbuf)
1904 struct ctdluser usbuf;
1905 char requested_user[128];
1907 if (CtdlAccessCheck(ac_aide)) {
1911 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1912 if (CtdlGetUser(&usbuf, requested_user) != 0) {
1913 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1916 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1923 (int) usbuf.axlevel,
1925 (long)usbuf.lastcall,
1932 * Administrative Set User Parameters
1934 void cmd_asup(char *cmdbuf)
1936 struct ctdluser usbuf;
1937 char requested_user[128];
1943 if (CtdlAccessCheck(ac_aide))
1946 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1947 if (CtdlGetUserLock(&usbuf, requested_user) != 0) {
1948 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1951 np = num_parms(cmdbuf);
1953 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1955 usbuf.flags = extract_int(cmdbuf, 2);
1957 usbuf.timescalled = extract_int(cmdbuf, 3);
1959 usbuf.posted = extract_int(cmdbuf, 4);
1961 newax = extract_int(cmdbuf, 5);
1962 if ((newax >= AxDeleted) && (newax <= AxAideU)) {
1963 usbuf.axlevel = newax;
1967 usbuf.lastcall = extract_long(cmdbuf, 7);
1970 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1972 CtdlPutUserLock(&usbuf);
1973 if (usbuf.axlevel == AxDeleted) {
1974 if (purge_user(requested_user) == 0) {
1980 snprintf(notify, SIZ,
1981 "User \"%s\" has been deleted by %s.\n",
1983 (CC->logged_in ? CC->user.fullname : "an administrator")
1985 CtdlAideMessage(notify, "User Deletion Message");
1988 cprintf("%d Ok", CIT_OK);
1990 cprintf(" (%s deleted)", requested_user);
1997 * Count the number of new mail messages the user has
2001 int num_newmsgs = 0;
2003 num_newmsgs = CC->newmail;
2006 return (num_newmsgs);
2011 * Count the number of new mail messages the user has
2013 int InitialMailCheck()
2015 int num_newmsgs = 0;
2017 char mailboxname[ROOMNAMELEN];
2018 struct ctdlroom mailbox;
2020 struct cdbdata *cdbfr;
2021 long *msglist = NULL;
2024 CtdlMailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
2025 if (CtdlGetRoom(&mailbox, mailboxname) != 0)
2027 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
2029 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
2031 if (cdbfr != NULL) {
2032 msglist = malloc(cdbfr->len);
2033 memcpy(msglist, cdbfr->ptr, cdbfr->len);
2034 num_msgs = cdbfr->len / sizeof(long);
2038 for (a = 0; a < num_msgs; ++a) {
2039 if (msglist[a] > 0L) {
2040 if (msglist[a] > vbuf.v_lastseen) {
2045 if (msglist != NULL)
2048 return (num_newmsgs);
2054 * Set the preferred view for the current user/room combination
2056 void cmd_view(char *cmdbuf) {
2060 if (CtdlAccessCheck(ac_logged_in)) {
2064 requested_view = extract_int(cmdbuf, 0);
2066 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
2067 vbuf.v_view = requested_view;
2068 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
2070 cprintf("%d ok\n", CIT_OK);
2077 void cmd_renu(char *cmdbuf)
2080 char oldname[USERNAME_SIZE];
2081 char newname[USERNAME_SIZE];
2083 if (CtdlAccessCheck(ac_aide)) {
2087 extract_token(oldname, cmdbuf, 0, '|', sizeof oldname);
2088 extract_token(newname, cmdbuf, 1, '|', sizeof newname);
2090 retcode = rename_user(oldname, newname);
2093 cprintf("%d '%s' has been renamed to '%s'.\n", CIT_OK, oldname, newname);
2095 case RENAMEUSER_LOGGED_IN:
2096 cprintf("%d '%s' is currently logged in and cannot be renamed.\n",
2097 ERROR + ALREADY_LOGGED_IN , oldname);
2099 case RENAMEUSER_NOT_FOUND:
2100 cprintf("%d '%s' does not exist.\n", ERROR + NO_SUCH_USER, oldname);
2102 case RENAMEUSER_ALREADY_EXISTS:
2103 cprintf("%d A user named '%s' already exists.\n", ERROR + ALREADY_EXISTS, newname);
2107 cprintf("%d An unknown error occurred.\n", ERROR);
2112 /*****************************************************************************/
2113 /* MODULE INITIALIZATION STUFF */
2114 /*****************************************************************************/
2117 CTDL_MODULE_INIT(user_ops)
2120 CtdlRegisterProtoHook(cmd_user, "USER", "Submit username for login");
2121 CtdlRegisterProtoHook(cmd_pass, "PASS", "Complete login by submitting a password");
2122 CtdlRegisterProtoHook(cmd_creu, "CREU", "Create User");
2123 CtdlRegisterProtoHook(cmd_setp, "SETP", "Set the password for an account");
2124 CtdlRegisterProtoHook(cmd_getu, "GETU", "Get User parameters");
2125 CtdlRegisterProtoHook(cmd_setu, "SETU", "Set User parameters");
2126 CtdlRegisterProtoHook(cmd_slrp, "SLRP", "Set Last Read Pointer");
2127 CtdlRegisterProtoHook(cmd_invt, "INVT", "Invite a user to a room");
2128 CtdlRegisterProtoHook(cmd_kick, "KICK", "Kick a user out of a room");
2129 CtdlRegisterProtoHook(cmd_forg, "FORG", "Forget a room");
2130 CtdlRegisterProtoHook(cmd_gnur, "GNUR", "Get Next Unregistered User");
2131 CtdlRegisterProtoHook(cmd_vali, "VALI", "Validate new users");
2132 CtdlRegisterProtoHook(cmd_list, "LIST", "List users");
2133 CtdlRegisterProtoHook(cmd_chek, "CHEK", "assorted info we need to check at login");
2134 CtdlRegisterProtoHook(cmd_qusr, "QUSR", "check to see if a user exists");
2135 CtdlRegisterProtoHook(cmd_agup, "AGUP", "Administratively Get User Parameters");
2136 CtdlRegisterProtoHook(cmd_asup, "ASUP", "Administratively Set User Parameters");
2137 CtdlRegisterProtoHook(cmd_seen, "SEEN", "Manipulate seen/unread message flags");
2138 CtdlRegisterProtoHook(cmd_gtsn, "GTSN", "Fetch seen/unread message flags");
2139 CtdlRegisterProtoHook(cmd_view, "VIEW", "Set preferred view for user/room combination");
2140 CtdlRegisterProtoHook(cmd_renu, "RENU", "Rename a user");
2141 CtdlRegisterProtoHook(cmd_newu, "NEWU", "Log in as a new user");
2143 /* return our Subversion id for the Log */