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 as published
8 * by the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <sys/types.h>
33 #ifdef HAVE_SYS_STAT_H
37 #if TIME_WITH_SYS_TIME
38 # include <sys/time.h>
42 # include <sys/time.h>
50 #include <libcitadel.h>
55 #include "sysdep_decls.h"
62 #include "citserver.h"
63 #include "citadel_dirs.h"
66 #include "citadel_ldap.h"
68 #include "ctdl_module.h"
71 /* These pipes are used to talk to the chkpwd daemon, which is forked during startup */
72 int chkpwd_write_pipe[2];
73 int chkpwd_read_pipe[2];
78 * getuser() - retrieve named user into supplied buffer.
79 * returns 0 on success
81 int getuser(struct ctdluser *usbuf, char name[])
83 return CtdlGetUser(usbuf, name);
88 * CtdlGetUser() - retrieve named user into supplied buffer.
89 * returns 0 on success
91 int CtdlGetUserLen(struct ctdluser *usbuf, const char *name, long len)
94 char usernamekey[USERNAME_SIZE];
95 struct cdbdata *cdbus;
98 memset(usbuf, 0, sizeof(struct ctdluser));
101 makeuserkey(usernamekey, name, len);
102 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
104 if (cdbus == NULL) { /* user not found */
108 memcpy(usbuf, cdbus->ptr,
109 ((cdbus->len > sizeof(struct ctdluser)) ?
110 sizeof(struct ctdluser) : cdbus->len));
118 int CtdlGetUser(struct ctdluser *usbuf, char *name)
120 return CtdlGetUserLen(usbuf, name, cutuserkey(name));
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,
157 cutuserkey(usbuf->fullname));
159 usbuf->version = REV_LEVEL;
161 usernamekey, strlen(usernamekey),
162 usbuf, sizeof(struct ctdluser));
168 * putuser() - write user buffer into the correct place on disk
170 void putuser(struct ctdluser *usbuf)
177 * CtdlPutUserLock() - same as putuser() but locks the record
179 void CtdlPutUserLock(struct ctdluser *usbuf)
182 end_critical_section(S_USERS);
187 * lputuser() - same as putuser() but locks the record
189 void lputuser(struct ctdluser *usbuf)
191 CtdlPutUserLock(usbuf);
196 * rename_user() - this is tricky because the user's display name is the database key
198 * Returns 0 on success or nonzero if there was an error...
201 int rename_user(char *oldname, char *newname) {
202 int retcode = RENAMEUSER_OK;
203 struct ctdluser usbuf;
205 char oldnamekey[USERNAME_SIZE];
206 char newnamekey[USERNAME_SIZE];
208 /* Create the database keys... */
209 makeuserkey(oldnamekey, oldname, cutuserkey(oldname));
210 makeuserkey(newnamekey, newname, cutuserkey(newname));
212 /* Lock up and get going */
213 begin_critical_section(S_USERS);
215 /* We cannot rename a user who is currently logged in */
216 if (CtdlIsUserLoggedIn(oldname)) {
217 end_critical_section(S_USERS);
218 return RENAMEUSER_LOGGED_IN;
221 if (CtdlGetUser(&usbuf, newname) == 0) {
222 retcode = RENAMEUSER_ALREADY_EXISTS;
226 if (CtdlGetUser(&usbuf, oldname) != 0) {
227 retcode = RENAMEUSER_NOT_FOUND;
230 else { /* Sanity checks succeeded. Now rename the user. */
231 if (usbuf.usernum == 0)
233 syslog(LOG_DEBUG, "Can not rename user \"Citadel\".\n");
234 retcode = RENAMEUSER_NOT_FOUND;
236 syslog(LOG_DEBUG, "Renaming <%s> to <%s>\n", oldname, newname);
237 cdb_delete(CDB_USERS, oldnamekey, strlen(oldnamekey));
238 safestrncpy(usbuf.fullname, newname, sizeof usbuf.fullname);
240 cdb_store(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long),
241 usbuf.fullname, strlen(usbuf.fullname)+1 );
243 retcode = RENAMEUSER_OK;
249 end_critical_section(S_USERS);
256 * Index-generating function used by Ctdl[Get|Set]Relationship
258 int GenerateRelationshipIndex(char *IndexBuf,
270 TheIndex.iRoomID = RoomID;
271 TheIndex.iRoomGen = RoomGen;
272 TheIndex.iUserID = UserID;
274 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
275 return (sizeof(TheIndex));
281 * Back end for CtdlSetRelationship()
283 void put_visit(visit *newvisit)
288 memset (IndexBuf, 0, sizeof (IndexBuf));
289 /* Generate an index */
290 IndexLen = GenerateRelationshipIndex(IndexBuf,
293 newvisit->v_usernum);
295 /* Store the record */
296 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
297 newvisit, sizeof(visit)
305 * Define a relationship between a user and a room
307 void CtdlSetRelationship(visit *newvisit,
308 struct ctdluser *rel_user,
309 struct ctdlroom *rel_room)
313 /* We don't use these in Citadel because they're implicit by the
314 * index, but they must be present if the database is exported.
316 newvisit->v_roomnum = rel_room->QRnumber;
317 newvisit->v_roomgen = rel_room->QRgen;
318 newvisit->v_usernum = rel_user->usernum;
324 * Locate a relationship between a user and a room
326 void CtdlGetRelationship(visit *vbuf,
327 struct ctdluser *rel_user,
328 struct ctdlroom *rel_room)
333 struct cdbdata *cdbvisit;
335 /* Generate an index */
336 IndexLen = GenerateRelationshipIndex(IndexBuf,
341 /* Clear out the buffer */
342 memset(vbuf, 0, sizeof(visit));
344 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
345 if (cdbvisit != NULL) {
346 memcpy(vbuf, cdbvisit->ptr,
347 ((cdbvisit->len > sizeof(visit)) ?
348 sizeof(visit) : cdbvisit->len));
352 /* If this is the first time the user has seen this room,
353 * set the view to be the default for the room.
355 vbuf->v_view = rel_room->QRdefaultview;
358 /* Set v_seen if necessary */
359 if (vbuf->v_seen[0] == 0) {
360 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
365 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
367 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
371 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
373 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
378 * Is the user currently logged in an Aide?
382 if (CC->user.axlevel >= AxAideU)
390 * Is the user currently logged in an Aide *or* the room aide for this room?
392 int is_room_aide(void)
395 if (!CC->logged_in) {
399 if ((CC->user.axlevel >= AxAideU)
400 || (CC->room.QRroomaide == CC->user.usernum)) {
408 * CtdlGetUserByNumber() - get user by number
409 * returns 0 if user was found
411 * Note: fetching a user this way requires one additional database operation.
413 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number)
415 struct cdbdata *cdbun;
418 cdbun = cdb_fetch(CDB_USERSBYNUMBER, &number, sizeof(long));
420 syslog(LOG_INFO, "User %ld not found\n", number);
424 syslog(LOG_INFO, "User %ld maps to %s\n", number, cdbun->ptr);
425 r = CtdlGetUser(usbuf, cdbun->ptr);
431 * getuserbynumber() - get user by number
432 * returns 0 if user was found
434 * Note: fetching a user this way requires one additional database operation.
436 int getuserbynumber(struct ctdluser *usbuf, long number)
438 return CtdlGetUserByNumber(usbuf, number);
444 * Helper function for rebuild_usersbynumber()
446 void rebuild_ubn_for_user(struct ctdluser *usbuf, void *data) {
449 struct ubnlist *next;
450 char username[USERNAME_SIZE];
454 static struct ubnlist *u = NULL;
455 struct ubnlist *ptr = NULL;
457 /* Lazy programming here. Call this function as a ForEachUser backend
458 * in order to queue up the room names, or call it with a null user
459 * to make it do the processing.
462 ptr = (struct ubnlist *) malloc(sizeof (struct ubnlist));
463 if (ptr == NULL) return;
465 ptr->usernum = usbuf->usernum;
466 safestrncpy(ptr->username, usbuf->fullname, sizeof ptr->username);
473 syslog(LOG_DEBUG, "Rebuilding usersbynumber index %10ld : %s\n",
474 u->usernum, u->username);
475 cdb_store(CDB_USERSBYNUMBER, &u->usernum, sizeof(long), u->username, strlen(u->username)+1);
486 * Rebuild the users-by-number index
488 void rebuild_usersbynumber(void) {
489 cdb_trunc(CDB_USERSBYNUMBER); /* delete the old indices */
490 ForEachUser(rebuild_ubn_for_user, NULL); /* enumerate the users */
491 rebuild_ubn_for_user(NULL, NULL); /* and index them */
497 * getuserbyuid() - get user by system uid (for PAM mode authentication)
498 * returns 0 if user was found
500 * WARNING: don't use this function unless you absolutely have to. It does
501 * a sequential search and therefore is computationally expensive.
503 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
505 struct cdbdata *cdbus;
507 cdb_rewind(CDB_USERS);
509 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
510 memset(usbuf, 0, sizeof(struct ctdluser));
511 memcpy(usbuf, cdbus->ptr,
512 ((cdbus->len > sizeof(struct ctdluser)) ?
513 sizeof(struct ctdluser) : cdbus->len));
515 if (usbuf->uid == number) {
516 cdb_close_cursor(CDB_USERS);
524 * Back end for cmd_user() and its ilk
526 * NOTE: "authname" should only be used if we are attempting to use the "master user" feature
528 int CtdlLoginExistingUser(char *authname, const char *trythisname)
534 syslog(LOG_DEBUG, "CtdlLoginExistingUser(%s, %s)\n", authname, trythisname);
536 if ((CC->logged_in)) {
537 return login_already_logged_in;
540 if (trythisname == NULL) return login_not_found;
542 if (!strncasecmp(trythisname, "SYS_", 4))
544 syslog(LOG_DEBUG, "System user \"%s\" is not allowed to log in.\n", trythisname);
545 return login_not_found;
548 /* If a "master user" is defined, handle its authentication if specified */
550 if (strlen(config.c_master_user) > 0) if (strlen(config.c_master_pass) > 0) if (authname) {
551 if (!strcasecmp(authname, config.c_master_user)) {
556 /* Continue attempting user validation... */
557 safestrncpy(username, trythisname, sizeof (username));
559 len = cutuserkey(username);
561 if (IsEmptyStr(username)) {
562 return login_not_found;
565 if (config.c_auth_mode == AUTHMODE_HOST) {
570 struct passwd *tempPwdPtr;
573 syslog(LOG_DEBUG, "asking host about <%s>\n", username);
574 #ifdef HAVE_GETPWNAM_R
575 #ifdef SOLARIS_GETPWUID
576 syslog(LOG_DEBUG, "Calling getpwnam_r()\n");
577 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
578 #else // SOLARIS_GETPWUID
579 syslog(LOG_DEBUG, "Calling getpwnam_r()\n");
580 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
581 #endif // SOLARIS_GETPWUID
582 #else // HAVE_GETPWNAM_R
583 syslog(LOG_DEBUG, "SHOULD NEVER GET HERE!!!\n");
585 #endif // HAVE_GETPWNAM_R
586 if (tempPwdPtr == NULL) {
587 syslog(LOG_DEBUG, "no such user <%s>\n", username);
588 return login_not_found;
591 /* Locate the associated Citadel account.
592 * If not found, make one attempt to create it.
594 found_user = getuserbyuid(&CC->user, pd.pw_uid);
595 syslog(LOG_DEBUG, "found it: uid=%ld, gecos=%s here: %d\n",
596 (long)pd.pw_uid, pd.pw_gecos, found_user);
597 if (found_user != 0) {
598 len = cutuserkey(username);
599 create_user(username, len, 0);
600 found_user = getuserbyuid(&CC->user, pd.pw_uid);
606 else if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
614 found_user = CtdlTryUserLDAP(username, ldap_dn, sizeof ldap_dn, ldap_cn, sizeof ldap_cn, &ldap_uid);
615 if (found_user != 0) {
616 return login_not_found;
619 found_user = getuserbyuid(&CC->user, ldap_uid);
620 if (found_user != 0) {
621 create_user(username, len, 0);
622 found_user = getuserbyuid(&CC->user, ldap_uid);
625 if (found_user == 0) {
626 if (CC->ldap_dn != NULL) free(CC->ldap_dn);
627 CC->ldap_dn = strdup(ldap_dn);
634 /* native auth mode */
636 struct recptypes *valid = NULL;
638 /* First, try to log in as if the supplied name is a display name */
639 found_user = CtdlGetUser(&CC->user, username);
641 /* If that didn't work, try to log in as if the supplied name
642 * is an e-mail address
644 if (found_user != 0) {
645 valid = validate_recipients(username, NULL, 0);
647 if (valid->num_local == 1) {
648 found_user = CtdlGetUser(&CC->user, valid->recp_local);
650 free_recipients(valid);
655 /* Did we find something? */
656 if (found_user == 0) {
657 if (((CC->nologin)) && (CC->user.axlevel < AxAideU)) {
658 return login_too_many_users;
660 safestrncpy(CC->curr_user, CC->user.fullname,
661 sizeof CC->curr_user);
665 return login_not_found;
673 void cmd_user(char *cmdbuf)
678 syslog(LOG_DEBUG, "cmd_user(%s)\n", cmdbuf);
679 extract_token(username, cmdbuf, 0, '|', sizeof username);
680 syslog(LOG_DEBUG, "username: %s\n", username);
682 syslog(LOG_DEBUG, "username: %s\n", username);
684 a = CtdlLoginExistingUser(NULL, username);
686 case login_already_logged_in:
687 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
689 case login_too_many_users:
691 "Too many users are already online "
693 ERROR + MAX_SESSIONS_EXCEEDED,
694 config.c_nodename, config.c_maxsessions);
697 cprintf("%d Password required for %s\n",
698 MORE_DATA, CC->curr_user);
700 case login_not_found:
701 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
704 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
711 * session startup code which is common to both cmd_pass() and cmd_newu()
716 syslog(LOG_NOTICE, "<%s> logged in\n", CC->curr_user);
718 CtdlGetUserLock(&CC->user, CC->curr_user);
719 ++(CC->user.timescalled);
720 CC->previous_login = CC->user.lastcall;
721 time(&CC->user.lastcall);
723 /* If this user's name is the name of the system administrator
724 * (as specified in setup), automatically assign access level 6.
726 if (!strcasecmp(CC->user.fullname, config.c_sysadm)) {
727 CC->user.axlevel = AxAideU;
730 /* If we're authenticating off the host system, automatically give
731 * root the highest level of access.
733 if (config.c_auth_mode == AUTHMODE_HOST) {
734 if (CC->user.uid == 0) {
735 CC->user.axlevel = AxAideU;
739 CtdlPutUserLock(&CC->user);
742 * Populate CC->cs_inet_email with a default address. This will be
743 * overwritten with the user's directory address, if one exists, when
744 * the vCard module's login hook runs.
746 snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
747 CC->user.fullname, config.c_fqdn);
748 convert_spaces_to_underscores(CC->cs_inet_email);
750 /* Create any personal rooms required by the system.
751 * (Technically, MAILROOM should be there already, but just in case...)
753 CtdlCreateRoom(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
754 CtdlCreateRoom(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
755 CtdlCreateRoom(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
756 CtdlCreateRoom(USERDRAFTROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
758 /* Run any startup routines registered by loadable modules */
759 PerformSessionHooks(EVT_LOGIN);
761 /* Enter the lobby */
762 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
766 void logged_in_response(void)
768 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
769 CIT_OK, CC->user.fullname, CC->user.axlevel,
770 CC->user.timescalled, CC->user.posted,
771 CC->user.flags, CC->user.usernum,
777 void CtdlUserLogout(void)
779 CitContext *CCC = MyContext();
781 syslog(LOG_DEBUG, "CtdlUserLogout() logging out <%s>\n", CCC->curr_user);
784 * If there is a download in progress, abort it.
786 if (CCC->download_fp != NULL) {
787 fclose(CCC->download_fp);
788 CCC->download_fp = NULL;
792 * If there is an upload in progress, abort it.
794 if (CCC->upload_fp != NULL) {
799 * If we were talking to a network node, we're not anymore...
801 if (!IsEmptyStr(CCC->net_node)) {
802 network_talking_to(CCC->net_node, NTT_REMOVE);
805 /* Run any hooks registered by modules... */
806 PerformSessionHooks(EVT_LOGOUT);
809 * Clear out some session data. Most likely, the CitContext for this
810 * session is about to get nuked when the session disconnects, but
811 * since it's possible to log in again without reconnecting, we cannot
812 * make that assumption.
814 strcpy(CCC->fake_username, "");
815 strcpy(CCC->fake_hostname, "");
816 strcpy(CCC->fake_roomname, "");
819 /* Check to see if the user was deleted whilst logged in and purge them if necessary */
820 if ((CCC->user.axlevel == AxDeleted) && (CCC->user.usernum))
821 purge_user(CCC->user.fullname);
823 /* Clear out the user record in memory so we don't behave like a ghost */
824 memset(&CCC->user, 0, sizeof(struct ctdluser));
825 CCC->curr_user[0] = 0;
827 CCC->cs_inet_email[0] = 0;
828 CCC->cs_inet_other_emails[0] = 0;
829 CCC->cs_inet_fn[0] = 0;
830 CCC->fake_username[0] = 0;
831 CCC->fake_hostname[0] = 0;
832 CCC->fake_roomname[0] = 0;
835 /* Free any output buffers */
841 * Validate a password on the host unix system by talking to the chkpwd daemon
843 static int validpw(uid_t uid, const char *pass)
848 if (IsEmptyStr(pass)) {
849 syslog(LOG_DEBUG, "Refusing to chkpwd for uid=%d with empty password.\n", uid);
853 syslog(LOG_DEBUG, "Validating password for uid=%d using chkpwd...\n", uid);
855 begin_critical_section(S_CHKPWD);
856 rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
857 rv = write(chkpwd_write_pipe[1], pass, 256);
858 rv = read(chkpwd_read_pipe[0], buf, 4);
859 end_critical_section(S_CHKPWD);
861 if (!strncmp(buf, "PASS", 4)) {
862 syslog(LOG_DEBUG, "...pass\n");
866 syslog(LOG_DEBUG, "...fail\n");
871 * Start up the chkpwd daemon so validpw() has something to talk to
873 void start_chkpwd_daemon(void) {
875 struct stat filestats;
878 syslog(LOG_DEBUG, "Starting chkpwd daemon for host authentication mode\n");
880 if ((stat(file_chkpwd, &filestats)==-1) ||
881 (filestats.st_size==0)){
882 printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
885 if (pipe(chkpwd_write_pipe) != 0) {
886 syslog(LOG_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
889 if (pipe(chkpwd_read_pipe) != 0) {
890 syslog(LOG_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
895 if (chkpwd_pid < 0) {
896 syslog(LOG_EMERG, "Unable to fork chkpwd daemon: %s\n", strerror(errno));
899 if (chkpwd_pid == 0) {
900 syslog(LOG_DEBUG, "Now calling dup2() write\n");
901 dup2(chkpwd_write_pipe[0], 0);
902 syslog(LOG_DEBUG, "Now calling dup2() write\n");
903 dup2(chkpwd_read_pipe[1], 1);
904 syslog(LOG_DEBUG, "Now closing stuff\n");
905 for (i=2; i<256; ++i) close(i);
906 syslog(LOG_DEBUG, "Now calling execl(%s)\n", file_chkpwd);
907 execl(file_chkpwd, file_chkpwd, NULL);
908 syslog(LOG_EMERG, "Unable to exec chkpwd daemon: %s\n", strerror(errno));
915 int CtdlTryPassword(const char *password, long len)
919 if ((CC->logged_in)) {
920 syslog(LOG_WARNING, "CtdlTryPassword: already logged in\n");
921 return pass_already_logged_in;
923 if (!strcmp(CC->curr_user, NLI)) {
924 syslog(LOG_WARNING, "CtdlTryPassword: no user selected\n");
927 if (CtdlGetUser(&CC->user, CC->curr_user)) {
928 syslog(LOG_ERR, "CtdlTryPassword: internal error\n");
929 return pass_internal_error;
931 if (password == NULL) {
932 syslog(LOG_INFO, "CtdlTryPassword: NULL password string supplied\n");
933 return pass_wrong_password;
938 code = strcmp(password, config.c_master_pass);
941 else if (config.c_auth_mode == AUTHMODE_HOST) {
945 if (validpw(CC->user.uid, password)) {
949 * sooper-seekrit hack: populate the password field in the
950 * citadel database with the password that the user typed,
951 * if it's correct. This allows most sites to convert from
952 * host auth to native auth if they want to. If you think
953 * this is a security hazard, comment it out.
956 CtdlGetUserLock(&CC->user, CC->curr_user);
957 safestrncpy(CC->user.password, password, sizeof CC->user.password);
958 CtdlPutUserLock(&CC->user);
961 * (sooper-seekrit hack ends here)
971 else if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
975 if ((CC->ldap_dn) && (!CtdlTryPasswordLDAP(CC->ldap_dn, password))) {
986 /* native auth mode */
989 pw = (char*) malloc(len + 1);
990 memcpy(pw, password, len + 1);
992 strproc(CC->user.password);
993 code = strcasecmp(CC->user.password, pw);
995 strproc(CC->user.password);
996 code = strcasecmp(CC->user.password, pw);
1004 syslog(LOG_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
1005 return pass_wrong_password;
1010 void cmd_pass(char *buf)
1016 memset(password, 0, sizeof(password));
1017 len = extract_token(password, buf, 0, '|', sizeof password);
1018 a = CtdlTryPassword(password, len);
1021 case pass_already_logged_in:
1022 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
1025 cprintf("%d You must send a name with USER first.\n",
1026 ERROR + USERNAME_REQUIRED);
1028 case pass_wrong_password:
1029 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
1032 logged_in_response();
1040 * Delete a user record *and* all of its related resources.
1042 int purge_user(char pname[])
1045 struct ctdluser usbuf;
1046 char usernamekey[USERNAME_SIZE];
1048 makeuserkey(usernamekey, pname, cutuserkey(pname));
1050 /* If the name is empty we can't find them in the DB any way so just return */
1051 if (IsEmptyStr(pname))
1052 return (ERROR + NO_SUCH_USER);
1054 if (CtdlGetUser(&usbuf, pname) != 0) {
1055 syslog(LOG_ERR, "Cannot purge user <%s> - not found\n", pname);
1056 return (ERROR + NO_SUCH_USER);
1058 /* Don't delete a user who is currently logged in. Instead, just
1059 * set the access level to 0, and let the account get swept up
1060 * during the next purge.
1062 if (CtdlIsUserLoggedInByNum(usbuf.usernum)) {
1063 syslog(LOG_WARNING, "User <%s> is logged in; not deleting.\n", pname);
1064 usbuf.axlevel = AxDeleted;
1065 CtdlPutUser(&usbuf);
1068 syslog(LOG_NOTICE, "Deleting user <%s>\n", pname);
1072 * This should all be wrapped in a S_USERS mutex.
1073 * Without the mutex the user could log in before we get to the next function
1074 * That would truly mess things up :-(
1075 * I would like to see the S_USERS start before the CtdlIsUserLoggedInByNum() above
1076 * and end after the user has been deleted from the database, below.
1077 * Question is should we enter the EVT_PURGEUSER whilst S_USERS is active?
1080 /* Perform any purge functions registered by server extensions */
1081 PerformUserHooks(&usbuf, EVT_PURGEUSER);
1083 /* delete any existing user/room relationships */
1084 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
1086 /* delete the users-by-number index record */
1087 cdb_delete(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long));
1089 /* delete the userlog entry */
1090 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
1092 /* remove the user's bio file */
1100 /* remove the user's picture */
1112 int internal_create_user (const char *username, long len, struct ctdluser *usbuf, uid_t uid)
1114 if (!CtdlGetUserLen(usbuf, username, len)) {
1115 return (ERROR + ALREADY_EXISTS);
1118 /* Go ahead and initialize a new user record */
1119 memset(usbuf, 0, sizeof(struct ctdluser));
1120 safestrncpy(usbuf->fullname, username, sizeof usbuf->fullname);
1121 strcpy(usbuf->password, "");
1124 /* These are the default flags on new accounts */
1125 usbuf->flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
1127 usbuf->timescalled = 0;
1129 usbuf->axlevel = config.c_initax;
1130 usbuf->lastcall = time(NULL);
1132 /* fetch a new user number */
1133 usbuf->usernum = get_new_user_number();
1135 /* add user to the database */
1137 cdb_store(CDB_USERSBYNUMBER, &usbuf->usernum, sizeof(long), usbuf->fullname, strlen(usbuf->fullname)+1);
1145 * create_user() - back end processing to create a new user
1147 * Set 'newusername' to the desired account name.
1148 * Set 'become_user' to nonzero if this is self-service account creation and we want
1149 * to actually log in as the user we just created, otherwise set it to 0.
1151 int create_user(const char *newusername, long len, int become_user)
1153 struct ctdluser usbuf;
1154 struct ctdlroom qrbuf;
1156 char mailboxname[ROOMNAMELEN];
1162 safestrncpy(username, newusername, sizeof username);
1166 if (config.c_auth_mode == AUTHMODE_HOST) {
1168 /* host auth mode */
1171 struct passwd *tempPwdPtr;
1172 char pwdbuffer[SIZ];
1174 #ifdef HAVE_GETPWNAM_R
1175 #ifdef SOLARIS_GETPWUID
1176 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof(pwdbuffer));
1177 #else // SOLARIS_GETPWUID
1178 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
1179 #endif // SOLARIS_GETPWUID
1180 #else // HAVE_GETPWNAM_R
1182 #endif // HAVE_GETPWNAM_R
1183 if (tempPwdPtr != NULL) {
1184 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
1186 if (IsEmptyStr (username))
1188 safestrncpy(username, pd.pw_name, sizeof username);
1189 len = cutuserkey(username);
1193 return (ERROR + NO_SUCH_USER);
1198 if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
1199 if (CtdlTryUserLDAP(username, NULL, 0, username, sizeof username, &uid) != 0) {
1200 return(ERROR + NO_SUCH_USER);
1203 #endif /* HAVE_LDAP */
1205 if ((retval = internal_create_user(username, len, &usbuf, uid)) != 0)
1209 * Give the user a private mailbox and a configuration room.
1210 * Make the latter an invisible system room.
1212 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
1213 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
1215 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
1216 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
1217 if (CtdlGetRoomLock(&qrbuf, mailboxname) == 0) {
1218 qrbuf.QRflags2 |= QR2_SYSTEM;
1219 CtdlPutRoomLock(&qrbuf);
1222 /* Perform any create functions registered by server extensions */
1223 PerformUserHooks(&usbuf, EVT_NEWUSER);
1225 /* Everything below this line can be bypassed if administratively
1226 * creating a user, instead of doing self-service account creation
1230 /* Now become the user we just created */
1231 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
1232 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
1235 /* Check to make sure we're still who we think we are */
1236 if (CtdlGetUser(&CC->user, CC->curr_user)) {
1237 return (ERROR + INTERNAL_ERROR);
1242 "New user account <%s> has been created, from host %s [%s].\n",
1247 CtdlAideMessage(buf, "User Creation Notice");
1248 syslog(LOG_NOTICE, "New user <%s> created\n", username);
1255 * cmd_newu() - create a new user account and log in as that user
1257 void cmd_newu(char *cmdbuf)
1263 if (config.c_auth_mode != AUTHMODE_NATIVE) {
1264 cprintf("%d This system does not use native mode authentication.\n",
1269 if (config.c_disable_newu) {
1270 cprintf("%d Self-service user account creation "
1271 "is disabled on this system.\n", ERROR + NOT_HERE);
1275 if (CC->logged_in) {
1276 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
1280 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
1281 ERROR + MAX_SESSIONS_EXCEEDED,
1282 config.c_nodename, config.c_maxsessions);
1284 extract_token(username, cmdbuf, 0, '|', sizeof username);
1286 len = cutuserkey(username);
1288 if (IsEmptyStr(username)) {
1289 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1293 if ((!strcasecmp(username, "bbs")) ||
1294 (!strcasecmp(username, "new")) ||
1295 (!strcasecmp(username, "."))) {
1296 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
1300 a = create_user(username, len, 1);
1303 logged_in_response();
1304 } else if (a == ERROR + ALREADY_EXISTS) {
1305 cprintf("%d '%s' already exists.\n",
1306 ERROR + ALREADY_EXISTS, username);
1308 } else if (a == ERROR + INTERNAL_ERROR) {
1309 cprintf("%d Internal error - user record disappeared?\n",
1310 ERROR + INTERNAL_ERROR);
1313 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
1319 * set password - back end api code
1321 void CtdlSetPassword(char *new_pw)
1323 CtdlGetUserLock(&CC->user, CC->curr_user);
1324 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1325 CtdlPutUserLock(&CC->user);
1326 syslog(LOG_INFO, "Password changed for user <%s>\n", CC->curr_user);
1327 PerformSessionHooks(EVT_SETPASS);
1332 * set password - citadel protocol implementation
1334 void cmd_setp(char *new_pw)
1336 int generate_random_password = 0;
1338 if (CtdlAccessCheck(ac_logged_in)) {
1341 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
1342 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
1345 if (CC->is_master) {
1346 cprintf("%d The master prefix password cannot be changed with this command.\n",
1351 if (!strcasecmp(new_pw, "GENERATE_RANDOM_PASSWORD")) {
1352 char random_password[17];
1353 generate_random_password = 1;
1354 snprintf(random_password, sizeof random_password, "%08lx%08lx", random(), random());
1355 CtdlSetPassword(random_password);
1356 cprintf("%d %s\n", CIT_OK, random_password);
1360 if (IsEmptyStr(new_pw)) {
1361 cprintf("%d Password unchanged.\n", CIT_OK);
1364 CtdlSetPassword(new_pw);
1365 cprintf("%d Password changed.\n", CIT_OK);
1371 * cmd_creu() - administratively create a new user account (do not log in to it)
1373 void cmd_creu(char *cmdbuf)
1379 struct ctdluser tmp;
1381 if (CtdlAccessCheck(ac_aide)) {
1385 extract_token(username, cmdbuf, 0, '|', sizeof username);
1386 extract_token(password, cmdbuf, 1, '|', sizeof password);
1387 ////username[25] = 0;
1391 len = cutuserkey(username);
1393 if (IsEmptyStr(username)) {
1394 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1398 a = create_user(username, len, 0);
1401 if (!IsEmptyStr(password)) {
1402 CtdlGetUserLock(&tmp, username);
1403 safestrncpy(tmp.password, password, sizeof(tmp.password));
1404 CtdlPutUserLock(&tmp);
1406 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1407 (!IsEmptyStr(password)) ? "and password set" :
1408 "with no password");
1410 } else if (a == ERROR + ALREADY_EXISTS) {
1411 cprintf("%d '%s' already exists.\n", ERROR + ALREADY_EXISTS, username);
1413 } else if ( (config.c_auth_mode != AUTHMODE_NATIVE) && (a == ERROR + NO_SUCH_USER) ) {
1414 cprintf("%d User accounts are not created within Citadel in host authentication mode.\n",
1415 ERROR + NO_SUCH_USER);
1418 cprintf("%d An error occurred creating the user account.\n", ERROR + INTERNAL_ERROR);
1425 * get user parameters
1427 void cmd_getu(char *cmdbuf)
1430 if (CtdlAccessCheck(ac_logged_in))
1433 CtdlGetUser(&CC->user, CC->curr_user);
1434 cprintf("%d 80|24|%d|\n",
1436 (CC->user.flags & US_USER_SET)
1441 * set user parameters
1443 void cmd_setu(char *new_parms)
1445 if (CtdlAccessCheck(ac_logged_in))
1448 if (num_parms(new_parms) < 3) {
1449 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1452 CtdlGetUserLock(&CC->user, CC->curr_user);
1453 CC->user.flags = CC->user.flags & (~US_USER_SET);
1454 CC->user.flags = CC->user.flags | (extract_int(new_parms, 2) & US_USER_SET);
1455 CtdlPutUserLock(&CC->user);
1456 cprintf("%d Ok\n", CIT_OK);
1460 * set last read pointer
1462 void cmd_slrp(char *new_ptr)
1466 visit original_vbuf;
1468 if (CtdlAccessCheck(ac_logged_in)) {
1472 if (!strncasecmp(new_ptr, "highest", 7)) {
1473 newlr = CC->room.QRhighest;
1475 newlr = atol(new_ptr);
1478 CtdlGetUserLock(&CC->user, CC->curr_user);
1480 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1481 memcpy(&original_vbuf, &vbuf, sizeof(visit));
1482 vbuf.v_lastseen = newlr;
1483 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1485 /* Only rewrite the record if it changed */
1486 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1487 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1488 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1491 CtdlPutUserLock(&CC->user);
1492 cprintf("%d %ld\n", CIT_OK, newlr);
1496 void cmd_seen(char *argbuf) {
1497 long target_msgnum = 0L;
1498 int target_setting = 0;
1500 if (CtdlAccessCheck(ac_logged_in)) {
1504 if (num_parms(argbuf) != 2) {
1505 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1509 target_msgnum = extract_long(argbuf, 0);
1510 target_setting = extract_int(argbuf, 1);
1512 CtdlSetSeen(&target_msgnum, 1, target_setting,
1513 ctdlsetseen_seen, NULL, NULL);
1514 cprintf("%d OK\n", CIT_OK);
1518 void cmd_gtsn(char *argbuf) {
1521 if (CtdlAccessCheck(ac_logged_in)) {
1525 /* Learn about the user and room in question */
1526 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1528 cprintf("%d ", CIT_OK);
1529 client_write(vbuf.v_seen, strlen(vbuf.v_seen));
1530 client_write(HKEY("\n"));
1535 * API function for cmd_invt_kick() and anything else that needs to
1536 * invite or kick out a user to/from a room.
1538 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1540 int CtdlInvtKick(char *iuser, int op) {
1541 struct ctdluser USscratch;
1545 if (CtdlGetUser(&USscratch, iuser) != 0) {
1549 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1551 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1552 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1555 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1556 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1558 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1560 /* post a message in Aide> saying what we just did */
1561 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1563 ((op == 1) ? "invited to" : "kicked out of"),
1566 CtdlAideMessage(bbb,"User Admin Message");
1573 * INVT and KICK commands
1575 void cmd_invt_kick(char *iuser, int op) {
1578 * These commands are only allowed by aides, room aides,
1579 * and room namespace owners
1581 if (is_room_aide()) {
1582 /* access granted */
1583 } else if ( ((atol(CC->room.QRname) == CC->user.usernum) ) && (CC->user.usernum != 0) ) {
1584 /* access granted */
1587 cprintf("%d Higher access or room ownership required.\n",
1588 ERROR + HIGHER_ACCESS_REQUIRED);
1592 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1594 cprintf("%d Can't add/remove users from this room.\n",
1599 if (CtdlInvtKick(iuser, op) != 0) {
1600 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1604 cprintf("%d %s %s %s.\n",
1606 ((op == 1) ? "invited to" : "kicked out of"),
1611 void cmd_invt(char *iuser) {cmd_invt_kick(iuser, 1);}
1612 void cmd_kick(char *iuser) {cmd_invt_kick(iuser, 0);}
1615 * Forget (Zap) the current room (API call)
1616 * Returns 0 on success
1618 int CtdlForgetThisRoom(void) {
1621 /* On some systems, Aides are not allowed to forget rooms */
1622 if (is_aide() && (config.c_aide_zap == 0)
1623 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1627 CtdlGetUserLock(&CC->user, CC->curr_user);
1628 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1630 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1631 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1633 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1634 CtdlPutUserLock(&CC->user);
1636 /* Return to the Lobby, so we don't end up in an undefined room */
1637 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
1644 * forget (Zap) the current room
1646 void cmd_forg(char *argbuf)
1649 if (CtdlAccessCheck(ac_logged_in)) {
1653 if (CtdlForgetThisRoom() == 0) {
1654 cprintf("%d Ok\n", CIT_OK);
1657 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1662 * Get Next Unregistered User
1664 void cmd_gnur(char *argbuf)
1666 struct cdbdata *cdbus;
1667 struct ctdluser usbuf;
1669 if (CtdlAccessCheck(ac_aide)) {
1673 if ((CitControl.MMflags & MM_VALID) == 0) {
1674 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1678 /* There are unvalidated users. Traverse the user database,
1679 * and return the first user we find that needs validation.
1681 cdb_rewind(CDB_USERS);
1682 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1683 memset(&usbuf, 0, sizeof(struct ctdluser));
1684 memcpy(&usbuf, cdbus->ptr,
1685 ((cdbus->len > sizeof(struct ctdluser)) ?
1686 sizeof(struct ctdluser) : cdbus->len));
1688 if ((usbuf.flags & US_NEEDVALID)
1689 && (usbuf.axlevel > AxDeleted)) {
1690 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1691 cdb_close_cursor(CDB_USERS);
1696 /* If we get to this point, there are no more unvalidated users.
1697 * Therefore we clear the "users need validation" flag.
1700 begin_critical_section(S_CONTROL);
1702 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1704 end_critical_section(S_CONTROL);
1705 cprintf("%d *** End of registration.\n", CIT_OK);
1714 void cmd_vali(char *v_args)
1718 struct ctdluser userbuf;
1720 extract_token(user, v_args, 0, '|', sizeof user);
1721 newax = extract_int(v_args, 1);
1723 if (CtdlAccessCheck(ac_aide) ||
1724 (newax > AxAideU) ||
1725 (newax < AxDeleted)) {
1729 if (CtdlGetUserLock(&userbuf, user) != 0) {
1730 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1734 userbuf.axlevel = newax;
1735 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1737 CtdlPutUserLock(&userbuf);
1739 /* If the access level was set to zero, delete the user */
1741 if (purge_user(user) == 0) {
1742 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1746 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1752 * Traverse the user file...
1754 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1757 struct ctdluser usbuf;
1758 struct cdbdata *cdbus;
1760 cdb_rewind(CDB_USERS);
1762 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1763 memset(&usbuf, 0, sizeof(struct ctdluser));
1764 memcpy(&usbuf, cdbus->ptr,
1765 ((cdbus->len > sizeof(struct ctdluser)) ?
1766 sizeof(struct ctdluser) : cdbus->len));
1768 (*CallBack) (&usbuf, in_data);
1774 * List one user (this works with cmd_list)
1776 void ListThisUser(struct ctdluser *usbuf, void *data)
1780 searchstring = (char *)data;
1781 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1785 if (usbuf->axlevel > AxDeleted) {
1786 if ((CC->user.axlevel >= AxAideU)
1787 || ((usbuf->flags & US_UNLISTED) == 0)
1788 || ((CC->internal_pgm))) {
1789 cprintf("%s|%d|%ld|%ld|%ld|%ld||\n",
1793 (long)usbuf->lastcall,
1801 * List users (searchstring may be empty to list all users)
1803 void cmd_list(char *cmdbuf)
1805 char searchstring[256];
1806 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1807 striplt(searchstring);
1808 cprintf("%d \n", LISTING_FOLLOWS);
1809 ForEachUser(ListThisUser, (void *)searchstring );
1817 * assorted info we need to check at login
1819 void cmd_chek(char *argbuf)
1825 if (CtdlAccessCheck(ac_logged_in)) {
1829 CtdlGetUser(&CC->user, CC->curr_user); /* no lock is needed here */
1830 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1833 if (CC->user.axlevel >= AxAideU) {
1835 if (CitControl.MMflags & MM_VALID)
1839 /* check for mail */
1840 mail = InitialMailCheck();
1842 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1847 * check to see if a user exists
1849 void cmd_qusr(char *who)
1851 struct ctdluser usbuf;
1853 if (CtdlGetUser(&usbuf, who) == 0) {
1854 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1856 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1862 * Administrative Get User Parameters
1864 void cmd_agup(char *cmdbuf)
1866 struct ctdluser usbuf;
1867 char requested_user[128];
1869 if (CtdlAccessCheck(ac_aide)) {
1873 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1874 if (CtdlGetUser(&usbuf, requested_user) != 0) {
1875 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1878 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1885 (int) usbuf.axlevel,
1887 (long)usbuf.lastcall,
1894 * Administrative Set User Parameters
1896 void cmd_asup(char *cmdbuf)
1898 struct ctdluser usbuf;
1899 char requested_user[128];
1905 if (CtdlAccessCheck(ac_aide))
1908 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1909 if (CtdlGetUserLock(&usbuf, requested_user) != 0) {
1910 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1913 np = num_parms(cmdbuf);
1915 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1917 usbuf.flags = extract_int(cmdbuf, 2);
1919 usbuf.timescalled = extract_int(cmdbuf, 3);
1921 usbuf.posted = extract_int(cmdbuf, 4);
1923 newax = extract_int(cmdbuf, 5);
1924 if ((newax >= AxDeleted) && (newax <= AxAideU)) {
1925 usbuf.axlevel = newax;
1929 usbuf.lastcall = extract_long(cmdbuf, 7);
1932 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1934 CtdlPutUserLock(&usbuf);
1935 if (usbuf.axlevel == AxDeleted) {
1936 if (purge_user(requested_user) == 0) {
1942 snprintf(notify, SIZ,
1943 "User \"%s\" has been deleted by %s.\n",
1944 usbuf.fullname, CC->user.fullname);
1945 CtdlAideMessage(notify, "User Deletion Message");
1948 cprintf("%d Ok", CIT_OK);
1950 cprintf(" (%s deleted)", requested_user);
1957 * Count the number of new mail messages the user has
1961 int num_newmsgs = 0;
1963 num_newmsgs = CC->newmail;
1966 return (num_newmsgs);
1971 * Count the number of new mail messages the user has
1973 int InitialMailCheck()
1975 int num_newmsgs = 0;
1977 char mailboxname[ROOMNAMELEN];
1978 struct ctdlroom mailbox;
1980 struct cdbdata *cdbfr;
1981 long *msglist = NULL;
1984 CtdlMailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1985 if (CtdlGetRoom(&mailbox, mailboxname) != 0)
1987 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1989 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1991 if (cdbfr != NULL) {
1992 msglist = malloc(cdbfr->len);
1993 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1994 num_msgs = cdbfr->len / sizeof(long);
1998 for (a = 0; a < num_msgs; ++a) {
1999 if (msglist[a] > 0L) {
2000 if (msglist[a] > vbuf.v_lastseen) {
2005 if (msglist != NULL)
2008 return (num_newmsgs);
2014 * Set the preferred view for the current user/room combination
2016 void cmd_view(char *cmdbuf) {
2020 if (CtdlAccessCheck(ac_logged_in)) {
2024 requested_view = extract_int(cmdbuf, 0);
2026 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
2027 vbuf.v_view = requested_view;
2028 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
2030 cprintf("%d ok\n", CIT_OK);
2037 void cmd_renu(char *cmdbuf)
2040 char oldname[USERNAME_SIZE];
2041 char newname[USERNAME_SIZE];
2043 if (CtdlAccessCheck(ac_aide)) {
2047 extract_token(oldname, cmdbuf, 0, '|', sizeof oldname);
2048 extract_token(newname, cmdbuf, 1, '|', sizeof newname);
2050 retcode = rename_user(oldname, newname);
2053 cprintf("%d '%s' has been renamed to '%s'.\n", CIT_OK, oldname, newname);
2055 case RENAMEUSER_LOGGED_IN:
2056 cprintf("%d '%s' is currently logged in and cannot be renamed.\n",
2057 ERROR + ALREADY_LOGGED_IN , oldname);
2059 case RENAMEUSER_NOT_FOUND:
2060 cprintf("%d '%s' does not exist.\n", ERROR + NO_SUCH_USER, oldname);
2062 case RENAMEUSER_ALREADY_EXISTS:
2063 cprintf("%d A user named '%s' already exists.\n", ERROR + ALREADY_EXISTS, newname);
2067 cprintf("%d An unknown error occurred.\n", ERROR);
2072 /*****************************************************************************/
2073 /* MODULE INITIALIZATION STUFF */
2074 /*****************************************************************************/
2077 CTDL_MODULE_INIT(user_ops)
2080 CtdlRegisterProtoHook(cmd_user, "USER", "Submit username for login");
2081 CtdlRegisterProtoHook(cmd_pass, "PASS", "Complete login by submitting a password");
2082 CtdlRegisterProtoHook(cmd_creu, "CREU", "Create User");
2083 CtdlRegisterProtoHook(cmd_setp, "SETP", "Set the password for an account");
2084 CtdlRegisterProtoHook(cmd_getu, "GETU", "Get User parameters");
2085 CtdlRegisterProtoHook(cmd_setu, "SETU", "Set User parameters");
2086 CtdlRegisterProtoHook(cmd_slrp, "SLRP", "Set Last Read Pointer");
2087 CtdlRegisterProtoHook(cmd_invt, "INVT", "Invite a user to a room");
2088 CtdlRegisterProtoHook(cmd_kick, "KICK", "Kick a user out of a room");
2089 CtdlRegisterProtoHook(cmd_forg, "FORG", "Forget a room");
2090 CtdlRegisterProtoHook(cmd_gnur, "GNUR", "Autoconverted. TODO: document me.");
2091 CtdlRegisterProtoHook(cmd_vali, "VALI", "Validate new users");
2092 CtdlRegisterProtoHook(cmd_list, "LIST", "List users");
2093 CtdlRegisterProtoHook(cmd_chek, "CHEK", "Autoconverted. TODO: document me.");
2094 CtdlRegisterProtoHook(cmd_qusr, "QUSR", "Autoconverted. TODO: document me.");
2095 CtdlRegisterProtoHook(cmd_agup, "AGUP", "Autoconverted. TODO: document me.");
2096 CtdlRegisterProtoHook(cmd_asup, "ASUP", "Autoconverted. TODO: document me.");
2097 CtdlRegisterProtoHook(cmd_seen, "SEEN", "Autoconverted. TODO: document me.");
2098 CtdlRegisterProtoHook(cmd_gtsn, "GTSN", "Autoconverted. TODO: document me.");
2099 CtdlRegisterProtoHook(cmd_view, "VIEW", "Autoconverted. TODO: document me.");
2100 CtdlRegisterProtoHook(cmd_renu, "RENU", "Autoconverted. TODO: document me.");
2101 CtdlRegisterProtoHook(cmd_newu, "NEWU", "Autoconverted. TODO: document me.");
2103 /* return our Subversion id for the Log */