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 INLINE long cutuserkey(char *username) {
65 len = strlen(username);
66 if (len >= USERNAME_SIZE)
68 CtdlLogPrintf (CTDL_EMERG, "Username to long: %s", username);
70 len = USERNAME_SIZE - 1;
71 ((char*)username)[USERNAME_SIZE - 1]='\0';
77 * makeuserkey() - convert a username into the format used as a database key
78 * (it's just the username converted into lower case)
80 INLINE void makeuserkey(char *key, const char *username, long len) {
83 if (len >= USERNAME_SIZE)
85 CtdlLogPrintf (CTDL_EMERG, "Username to long: %s", username);
87 len = USERNAME_SIZE - 1;
89 for (i=0; i<=len; ++i) {
90 key[i] = tolower(username[i]);
96 * getuser() - retrieve named user into supplied buffer.
97 * returns 0 on success
99 int getuser(struct ctdluser *usbuf, char name[])
101 return CtdlGetUser(usbuf, name);
106 * CtdlGetUser() - retrieve named user into supplied buffer.
107 * returns 0 on success
109 int CtdlGetUserLen(struct ctdluser *usbuf, const char *name, long len)
112 char usernamekey[USERNAME_SIZE];
113 struct cdbdata *cdbus;
116 memset(usbuf, 0, sizeof(struct ctdluser));
119 makeuserkey(usernamekey, name, len);
120 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
122 if (cdbus == NULL) { /* user not found */
126 memcpy(usbuf, cdbus->ptr,
127 ((cdbus->len > sizeof(struct ctdluser)) ?
128 sizeof(struct ctdluser) : cdbus->len));
136 int CtdlGetUser(struct ctdluser *usbuf, char *name)
138 return CtdlGetUserLen(usbuf, name, cutuserkey(name));
143 * CtdlGetUserLock() - same as getuser() but locks the record
145 int CtdlGetUserLock(struct ctdluser *usbuf, char *name)
149 retcode = CtdlGetUser(usbuf, name);
151 begin_critical_section(S_USERS);
158 * lgetuser() - same as getuser() but locks the record
160 int lgetuser(struct ctdluser *usbuf, char *name)
162 return CtdlGetUserLock(usbuf, name);
167 * CtdlPutUser() - write user buffer into the correct place on disk
169 void CtdlPutUser(struct ctdluser *usbuf)
171 char usernamekey[USERNAME_SIZE];
173 makeuserkey(usernamekey,
175 cutuserkey(usbuf->fullname));
177 usbuf->version = REV_LEVEL;
179 usernamekey, strlen(usernamekey),
180 usbuf, sizeof(struct ctdluser));
186 * putuser() - write user buffer into the correct place on disk
188 void putuser(struct ctdluser *usbuf)
195 * CtdlPutUserLock() - same as putuser() but locks the record
197 void CtdlPutUserLock(struct ctdluser *usbuf)
200 end_critical_section(S_USERS);
205 * lputuser() - same as putuser() but locks the record
207 void lputuser(struct ctdluser *usbuf)
209 CtdlPutUserLock(usbuf);
214 * rename_user() - this is tricky because the user's display name is the database key
216 * Returns 0 on success or nonzero if there was an error...
219 int rename_user(char *oldname, char *newname) {
220 int retcode = RENAMEUSER_OK;
221 struct ctdluser usbuf;
223 char oldnamekey[USERNAME_SIZE];
224 char newnamekey[USERNAME_SIZE];
226 /* Create the database keys... */
227 makeuserkey(oldnamekey, oldname, cutuserkey(oldname));
228 makeuserkey(newnamekey, newname, cutuserkey(newname));
230 /* Lock up and get going */
231 begin_critical_section(S_USERS);
233 /* We cannot rename a user who is currently logged in */
234 if (CtdlIsUserLoggedIn(oldname)) {
235 end_critical_section(S_USERS);
236 return RENAMEUSER_LOGGED_IN;
239 if (CtdlGetUser(&usbuf, newname) == 0) {
240 retcode = RENAMEUSER_ALREADY_EXISTS;
244 if (CtdlGetUser(&usbuf, oldname) != 0) {
245 retcode = RENAMEUSER_NOT_FOUND;
248 else { /* Sanity checks succeeded. Now rename the user. */
249 if (usbuf.usernum == 0)
251 CtdlLogPrintf (CTDL_DEBUG, "Can not rename user \"Citadel\".\n");
252 retcode = RENAMEUSER_NOT_FOUND;
254 CtdlLogPrintf(CTDL_DEBUG, "Renaming <%s> to <%s>\n", oldname, newname);
255 cdb_delete(CDB_USERS, oldnamekey, strlen(oldnamekey));
256 safestrncpy(usbuf.fullname, newname, sizeof usbuf.fullname);
258 cdb_store(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long),
259 usbuf.fullname, strlen(usbuf.fullname)+1 );
261 retcode = RENAMEUSER_OK;
267 end_critical_section(S_USERS);
274 * Index-generating function used by Ctdl[Get|Set]Relationship
276 int GenerateRelationshipIndex(char *IndexBuf,
288 TheIndex.iRoomID = RoomID;
289 TheIndex.iRoomGen = RoomGen;
290 TheIndex.iUserID = UserID;
292 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
293 return (sizeof(TheIndex));
299 * Back end for CtdlSetRelationship()
301 void put_visit(struct visit *newvisit)
306 memset (IndexBuf, 0, sizeof (IndexBuf));
307 /* Generate an index */
308 IndexLen = GenerateRelationshipIndex(IndexBuf,
311 newvisit->v_usernum);
313 /* Store the record */
314 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
315 newvisit, sizeof(struct visit)
323 * Define a relationship between a user and a room
325 void CtdlSetRelationship(struct visit *newvisit,
326 struct ctdluser *rel_user,
327 struct ctdlroom *rel_room)
331 /* We don't use these in Citadel because they're implicit by the
332 * index, but they must be present if the database is exported.
334 newvisit->v_roomnum = rel_room->QRnumber;
335 newvisit->v_roomgen = rel_room->QRgen;
336 newvisit->v_usernum = rel_user->usernum;
342 * Locate a relationship between a user and a room
344 void CtdlGetRelationship(struct visit *vbuf,
345 struct ctdluser *rel_user,
346 struct ctdlroom *rel_room)
351 struct cdbdata *cdbvisit;
353 /* Generate an index */
354 IndexLen = GenerateRelationshipIndex(IndexBuf,
359 /* Clear out the buffer */
360 memset(vbuf, 0, sizeof(struct visit));
362 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
363 if (cdbvisit != NULL) {
364 memcpy(vbuf, cdbvisit->ptr,
365 ((cdbvisit->len > sizeof(struct visit)) ?
366 sizeof(struct visit) : cdbvisit->len));
370 /* If this is the first time the user has seen this room,
371 * set the view to be the default for the room.
373 vbuf->v_view = rel_room->QRdefaultview;
376 /* Set v_seen if necessary */
377 if (vbuf->v_seen[0] == 0) {
378 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
383 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
385 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
389 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
391 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
396 * Is the user currently logged in an Aide?
400 if (CC->user.axlevel >= AxAideU)
408 * Is the user currently logged in an Aide *or* the room aide 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 CtdlLogPrintf(CTDL_INFO, "User %ld not found\n", number);
442 CtdlLogPrintf(CTDL_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 CtdlLogPrintf(CTDL_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 CtdlLogPrintf(9, "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 CtdlLogPrintf(CTDL_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 CtdlLogPrintf(CTDL_DEBUG, "asking host about <%s>\n", username);
592 #ifdef HAVE_GETPWNAM_R
593 #ifdef SOLARIS_GETPWUID
594 CtdlLogPrintf(CTDL_DEBUG, "Calling getpwnam_r()\n");
595 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
596 #else // SOLARIS_GETPWUID
597 CtdlLogPrintf(CTDL_DEBUG, "Calling getpwnam_r()\n");
598 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
599 #endif // SOLARIS_GETPWUID
600 #else // HAVE_GETPWNAM_R
601 CtdlLogPrintf(CTDL_DEBUG, "SHOULD NEVER GET HERE!!!\n");
603 #endif // HAVE_GETPWNAM_R
604 if (tempPwdPtr == NULL) {
605 CtdlLogPrintf(CTDL_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 CtdlLogPrintf(CTDL_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 CtdlLogPrintf(CTDL_DEBUG, "cmd_user(%s)\n", cmdbuf);
697 extract_token(username, cmdbuf, 0, '|', sizeof username);
698 CtdlLogPrintf(CTDL_DEBUG, "username: %s\n", username);
700 CtdlLogPrintf(CTDL_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()
734 CtdlLogPrintf(CTDL_NOTICE, "<%s> logged in\n", CC->curr_user);
736 CtdlGetUserLock(&CC->user, CC->curr_user);
737 ++(CC->user.timescalled);
738 CC->previous_login = CC->user.lastcall;
739 time(&CC->user.lastcall);
741 /* If this user's name is the name of the system administrator
742 * (as specified in setup), automatically assign access level 6.
744 if (!strcasecmp(CC->user.fullname, config.c_sysadm)) {
745 CC->user.axlevel = AxAideU;
748 /* If we're authenticating off the host system, automatically give
749 * root the highest level of access.
751 if (config.c_auth_mode == AUTHMODE_HOST) {
752 if (CC->user.uid == 0) {
753 CC->user.axlevel = AxAideU;
757 CtdlPutUserLock(&CC->user);
760 * Populate CC->cs_inet_email with a default address. This will be
761 * overwritten with the user's directory address, if one exists, when
762 * the vCard module's login hook runs.
764 snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
765 CC->user.fullname, config.c_fqdn);
766 convert_spaces_to_underscores(CC->cs_inet_email);
768 /* Create any personal rooms required by the system.
769 * (Technically, MAILROOM should be there already, but just in case...)
771 CtdlCreateRoom(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
772 CtdlCreateRoom(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
773 CtdlCreateRoom(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
774 /* CtdlCreateRoom(USERDRAFTROOM, 4, "", 0, 1, 0, VIEW_MAILBOX); temporarily disabled for 7.60 */
776 /* Run any startup routines registered by loadable modules */
777 PerformSessionHooks(EVT_LOGIN);
779 /* Enter the lobby */
780 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
784 void logged_in_response(void)
786 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
787 CIT_OK, CC->user.fullname, CC->user.axlevel,
788 CC->user.timescalled, CC->user.posted,
789 CC->user.flags, CC->user.usernum,
796 * misc things to be taken care of when a user is logged out
804 void CtdlUserLogout(void)
806 CitContext *CCC = CC; /* CachedCitContext - performance boost */
808 * If there is a download in progress, abort it.
810 if (CCC->download_fp != NULL) {
811 fclose(CCC->download_fp);
812 CCC->download_fp = NULL;
816 * If there is an upload in progress, abort it.
818 if (CCC->upload_fp != NULL) {
823 * If we were talking to a network node, we're not anymore...
825 if (!IsEmptyStr(CCC->net_node)) {
826 network_talking_to(CCC->net_node, NTT_REMOVE);
829 /* Run any hooks registered by modules... */
830 PerformSessionHooks(EVT_LOGOUT);
833 * Clear out some session data. Most likely, the CitContext for this
834 * session is about to get nuked when the session disconnects, but
835 * since it's possible to log in again without reconnecting, we cannot
836 * make that assumption.
838 strcpy(CCC->fake_username, "");
839 strcpy(CCC->fake_hostname, "");
840 strcpy(CCC->fake_roomname, "");
843 /* Check to see if the user was deleted whilst logged in and purge them if necessary */
844 if ((CCC->user.axlevel == AxDeleted) && (CCC->user.usernum))
845 purge_user(CCC->user.fullname);
847 /* Free any output buffers */
852 * Validate a password on the host unix system by talking to the chkpwd daemon
854 static int validpw(uid_t uid, const char *pass)
859 if (IsEmptyStr(pass)) {
860 CtdlLogPrintf(CTDL_DEBUG, "refusing to check empty password for uid=%d using chkpwd...\n", uid);
864 CtdlLogPrintf(CTDL_DEBUG, "Validating password for uid=%d using chkpwd...\n", uid);
866 begin_critical_section(S_CHKPWD);
867 rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
868 rv = write(chkpwd_write_pipe[1], pass, 256);
869 rv = read(chkpwd_read_pipe[0], buf, 4);
870 end_critical_section(S_CHKPWD);
872 if (!strncmp(buf, "PASS", 4)) {
873 CtdlLogPrintf(CTDL_DEBUG, "...pass\n");
877 CtdlLogPrintf(CTDL_DEBUG, "...fail\n");
882 * Start up the chkpwd daemon so validpw() has something to talk to
884 void start_chkpwd_daemon(void) {
886 struct stat filestats;
889 CtdlLogPrintf(CTDL_DEBUG, "Starting chkpwd daemon for host authentication mode\n");
891 if ((stat(file_chkpwd, &filestats)==-1) ||
892 (filestats.st_size==0)){
893 printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
896 if (pipe(chkpwd_write_pipe) != 0) {
897 CtdlLogPrintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
900 if (pipe(chkpwd_read_pipe) != 0) {
901 CtdlLogPrintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
906 if (chkpwd_pid < 0) {
907 CtdlLogPrintf(CTDL_EMERG, "Unable to fork chkpwd daemon: %s\n", strerror(errno));
910 if (chkpwd_pid == 0) {
911 CtdlLogPrintf(CTDL_DEBUG, "Now calling dup2() write\n");
912 dup2(chkpwd_write_pipe[0], 0);
913 CtdlLogPrintf(CTDL_DEBUG, "Now calling dup2() write\n");
914 dup2(chkpwd_read_pipe[1], 1);
915 CtdlLogPrintf(CTDL_DEBUG, "Now closing stuff\n");
916 for (i=2; i<256; ++i) close(i);
917 CtdlLogPrintf(CTDL_DEBUG, "Now calling execl(%s)\n", file_chkpwd);
918 execl(file_chkpwd, file_chkpwd, NULL);
919 CtdlLogPrintf(CTDL_EMERG, "Unable to exec chkpwd daemon: %s\n", strerror(errno));
926 int CtdlTryPassword(const char *password, long len)
930 if ((CC->logged_in)) {
931 CtdlLogPrintf(CTDL_WARNING, "CtdlTryPassword: already logged in\n");
932 return pass_already_logged_in;
934 if (!strcmp(CC->curr_user, NLI)) {
935 CtdlLogPrintf(CTDL_WARNING, "CtdlTryPassword: no user selected\n");
938 if (CtdlGetUser(&CC->user, CC->curr_user)) {
939 CtdlLogPrintf(CTDL_ERR, "CtdlTryPassword: internal error\n");
940 return pass_internal_error;
942 if (password == NULL) {
943 CtdlLogPrintf(CTDL_INFO, "CtdlTryPassword: NULL password string supplied\n");
944 return pass_wrong_password;
949 code = strcmp(password, config.c_master_pass);
952 else if (config.c_auth_mode == AUTHMODE_HOST) {
956 if (validpw(CC->user.uid, password)) {
960 * sooper-seekrit hack: populate the password field in the
961 * citadel database with the password that the user typed,
962 * if it's correct. This allows most sites to convert from
963 * host auth to native auth if they want to. If you think
964 * this is a security hazard, comment it out.
967 CtdlGetUserLock(&CC->user, CC->curr_user);
968 safestrncpy(CC->user.password, password, sizeof CC->user.password);
969 CtdlPutUserLock(&CC->user);
972 * (sooper-seekrit hack ends here)
982 else if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
986 if ((CC->ldap_dn) && (!CtdlTryPasswordLDAP(CC->ldap_dn, password))) {
997 /* native auth mode */
1000 pw = (char*) malloc(len + 1);
1001 memcpy(pw, password, len + 1);
1003 strproc(CC->user.password);
1004 code = strcasecmp(CC->user.password, pw);
1006 strproc(CC->user.password);
1007 code = strcasecmp(CC->user.password, pw);
1014 CtdlLogPrintf(CTDL_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
1015 return pass_wrong_password;
1020 void cmd_pass(char *buf)
1026 memset(password, 0, sizeof(password));
1027 len = extract_token(password, buf, 0, '|', sizeof password);
1028 a = CtdlTryPassword(password, len);
1031 case pass_already_logged_in:
1032 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
1035 cprintf("%d You must send a name with USER first.\n",
1036 ERROR + USERNAME_REQUIRED);
1038 case pass_wrong_password:
1039 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
1042 logged_in_response();
1050 * Delete a user record *and* all of its related resources.
1052 int purge_user(char pname[])
1055 struct ctdluser usbuf;
1056 char usernamekey[USERNAME_SIZE];
1058 makeuserkey(usernamekey, pname, cutuserkey(pname));
1060 /* If the name is empty we can't find them in the DB any way so just return */
1061 if (IsEmptyStr(pname))
1062 return (ERROR + NO_SUCH_USER);
1064 if (CtdlGetUser(&usbuf, pname) != 0) {
1065 CtdlLogPrintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
1066 return (ERROR + NO_SUCH_USER);
1068 /* Don't delete a user who is currently logged in. Instead, just
1069 * set the access level to 0, and let the account get swept up
1070 * during the next purge.
1072 if (CtdlIsUserLoggedInByNum(usbuf.usernum)) {
1073 CtdlLogPrintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
1074 usbuf.axlevel = AxDeleted;
1075 CtdlPutUser(&usbuf);
1078 CtdlLogPrintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
1082 * This should all be wrapped in a S_USERS mutex.
1083 * Without the mutex the user could log in before we get to the next function
1084 * That would truly mess things up :-(
1085 * I would like to see the S_USERS start before the CtdlIsUserLoggedInByNum() above
1086 * and end after the user has been deleted from the database, below.
1087 * Question is should we enter the EVT_PURGEUSER whilst S_USERS is active?
1090 /* Perform any purge functions registered by server extensions */
1091 PerformUserHooks(&usbuf, EVT_PURGEUSER);
1093 /* delete any existing user/room relationships */
1094 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
1096 /* delete the users-by-number index record */
1097 cdb_delete(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long));
1099 /* delete the userlog entry */
1100 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
1102 /* remove the user's bio file */
1110 /* remove the user's picture */
1122 int internal_create_user (const char *username, long len, struct ctdluser *usbuf, uid_t uid)
1124 if (!CtdlGetUserLen(usbuf, username, len)) {
1125 return (ERROR + ALREADY_EXISTS);
1128 /* Go ahead and initialize a new user record */
1129 memset(usbuf, 0, sizeof(struct ctdluser));
1130 safestrncpy(usbuf->fullname, username, sizeof usbuf->fullname);
1131 strcpy(usbuf->password, "");
1134 /* These are the default flags on new accounts */
1135 usbuf->flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
1137 usbuf->timescalled = 0;
1139 usbuf->axlevel = config.c_initax;
1140 usbuf->USscreenwidth = 80;
1141 usbuf->USscreenheight = 24;
1142 usbuf->lastcall = time(NULL);
1144 /* fetch a new user number */
1145 usbuf->usernum = get_new_user_number();
1147 /* add user to the database */
1149 cdb_store(CDB_USERSBYNUMBER, &usbuf->usernum, sizeof(long), usbuf->fullname, strlen(usbuf->fullname)+1);
1157 * create_user() - back end processing to create a new user
1159 * Set 'newusername' to the desired account name.
1160 * Set 'become_user' to nonzero if this is self-service account creation and we want
1161 * to actually log in as the user we just created, otherwise set it to 0.
1163 int create_user(const char *newusername, long len, int become_user)
1165 struct ctdluser usbuf;
1166 struct ctdlroom qrbuf;
1168 char mailboxname[ROOMNAMELEN];
1174 safestrncpy(username, newusername, sizeof username);
1178 if (config.c_auth_mode == AUTHMODE_HOST) {
1180 /* host auth mode */
1183 struct passwd *tempPwdPtr;
1184 char pwdbuffer[SIZ];
1186 #ifdef HAVE_GETPWNAM_R
1187 #ifdef SOLARIS_GETPWUID
1188 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof(pwdbuffer));
1189 #else // SOLARIS_GETPWUID
1190 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
1191 #endif // SOLARIS_GETPWUID
1192 #else // HAVE_GETPWNAM_R
1194 #endif // HAVE_GETPWNAM_R
1195 if (tempPwdPtr != NULL) {
1196 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
1198 if (IsEmptyStr (username))
1200 safestrncpy(username, pd.pw_name, sizeof username);
1201 len = cutuserkey(username);
1205 return (ERROR + NO_SUCH_USER);
1210 if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
1211 if (CtdlTryUserLDAP(username, NULL, 0, username, sizeof username, &uid) != 0) {
1212 return(ERROR + NO_SUCH_USER);
1215 #endif /* HAVE_LDAP */
1217 if ((retval = internal_create_user(username, len, &usbuf, uid)) != 0)
1221 * Give the user a private mailbox and a configuration room.
1222 * Make the latter an invisible system room.
1224 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
1225 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
1227 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
1228 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
1229 if (CtdlGetRoomLock(&qrbuf, mailboxname) == 0) {
1230 qrbuf.QRflags2 |= QR2_SYSTEM;
1231 CtdlPutRoomLock(&qrbuf);
1234 /* Perform any create functions registered by server extensions */
1235 PerformUserHooks(&usbuf, EVT_NEWUSER);
1237 /* Everything below this line can be bypassed if administratively
1238 * creating a user, instead of doing self-service account creation
1242 /* Now become the user we just created */
1243 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
1244 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
1247 /* Check to make sure we're still who we think we are */
1248 if (CtdlGetUser(&CC->user, CC->curr_user)) {
1249 return (ERROR + INTERNAL_ERROR);
1254 "New user account <%s> has been created, from host %s [%s].\n",
1259 CtdlAideMessage(buf, "User Creation Notice");
1260 CtdlLogPrintf(CTDL_NOTICE, "New user <%s> created\n", username);
1267 * cmd_newu() - create a new user account and log in as that user
1269 void cmd_newu(char *cmdbuf)
1275 if (config.c_auth_mode != AUTHMODE_NATIVE) {
1276 cprintf("%d This system does not use native mode authentication.\n",
1281 if (config.c_disable_newu) {
1282 cprintf("%d Self-service user account creation "
1283 "is disabled on this system.\n", ERROR + NOT_HERE);
1287 if (CC->logged_in) {
1288 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
1292 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
1293 ERROR + MAX_SESSIONS_EXCEEDED,
1294 config.c_nodename, config.c_maxsessions);
1296 extract_token(username, cmdbuf, 0, '|', sizeof username);
1298 len = cutuserkey(username);
1300 if (IsEmptyStr(username)) {
1301 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1305 if ((!strcasecmp(username, "bbs")) ||
1306 (!strcasecmp(username, "new")) ||
1307 (!strcasecmp(username, "."))) {
1308 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
1312 a = create_user(username, len, 1);
1315 logged_in_response();
1316 } else if (a == ERROR + ALREADY_EXISTS) {
1317 cprintf("%d '%s' already exists.\n",
1318 ERROR + ALREADY_EXISTS, username);
1320 } else if (a == ERROR + INTERNAL_ERROR) {
1321 cprintf("%d Internal error - user record disappeared?\n",
1322 ERROR + INTERNAL_ERROR);
1325 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
1331 * set password - back end api code
1333 void CtdlSetPassword(char *new_pw)
1335 CtdlGetUserLock(&CC->user, CC->curr_user);
1336 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1337 CtdlPutUserLock(&CC->user);
1338 CtdlLogPrintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
1339 PerformSessionHooks(EVT_SETPASS);
1344 * set password - citadel protocol implementation
1346 void cmd_setp(char *new_pw)
1348 int generate_random_password = 0;
1350 if (CtdlAccessCheck(ac_logged_in)) {
1353 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
1354 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
1357 if (CC->is_master) {
1358 cprintf("%d The master prefix password cannot be changed with this command.\n",
1363 if (!strcasecmp(new_pw, "GENERATE_RANDOM_PASSWORD")) {
1364 char random_password[17];
1365 generate_random_password = 1;
1366 snprintf(random_password, sizeof random_password, "%08lx%08lx", random(), random());
1367 CtdlSetPassword(random_password);
1368 cprintf("%d %s\n", CIT_OK, random_password);
1372 if (IsEmptyStr(new_pw)) {
1373 cprintf("%d Password unchanged.\n", CIT_OK);
1376 CtdlSetPassword(new_pw);
1377 cprintf("%d Password changed.\n", CIT_OK);
1383 * cmd_creu() - administratively create a new user account (do not log in to it)
1385 void cmd_creu(char *cmdbuf)
1391 struct ctdluser tmp;
1393 if (CtdlAccessCheck(ac_aide)) {
1397 extract_token(username, cmdbuf, 0, '|', sizeof username);
1398 extract_token(password, cmdbuf, 1, '|', sizeof password);
1399 ////username[25] = 0;
1403 len = cutuserkey(username);
1405 if (IsEmptyStr(username)) {
1406 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1410 a = create_user(username, len, 0);
1413 if (!IsEmptyStr(password)) {
1414 CtdlGetUserLock(&tmp, username);
1415 safestrncpy(tmp.password, password, sizeof(tmp.password));
1416 CtdlPutUserLock(&tmp);
1418 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1419 (!IsEmptyStr(password)) ? "and password set" :
1420 "with no password");
1422 } else if (a == ERROR + ALREADY_EXISTS) {
1423 cprintf("%d '%s' already exists.\n", ERROR + ALREADY_EXISTS, username);
1425 } else if ( (config.c_auth_mode != AUTHMODE_NATIVE) && (a == ERROR + NO_SUCH_USER) ) {
1426 cprintf("%d User accounts are not created within Citadel in host authentication mode.\n",
1427 ERROR + NO_SUCH_USER);
1430 cprintf("%d An error occurred creating the user account.\n", ERROR + INTERNAL_ERROR);
1437 * get user parameters
1439 void cmd_getu(char *cmdbuf)
1442 if (CtdlAccessCheck(ac_logged_in))
1445 CtdlGetUser(&CC->user, CC->curr_user);
1446 cprintf("%d %d|%d|%d|\n",
1448 CC->user.USscreenwidth,
1449 CC->user.USscreenheight,
1450 (CC->user.flags & US_USER_SET)
1455 * set user parameters
1457 void cmd_setu(char *new_parms)
1459 if (CtdlAccessCheck(ac_logged_in))
1462 if (num_parms(new_parms) < 3) {
1463 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1466 CtdlGetUserLock(&CC->user, CC->curr_user);
1467 CC->user.USscreenwidth = extract_int(new_parms, 0);
1468 CC->user.USscreenheight = extract_int(new_parms, 1);
1469 CC->user.flags = CC->user.flags & (~US_USER_SET);
1470 CC->user.flags = CC->user.flags |
1471 (extract_int(new_parms, 2) & US_USER_SET);
1473 CtdlPutUserLock(&CC->user);
1474 cprintf("%d Ok\n", CIT_OK);
1478 * set last read pointer
1480 void cmd_slrp(char *new_ptr)
1484 struct visit original_vbuf;
1486 if (CtdlAccessCheck(ac_logged_in)) {
1490 if (!strncasecmp(new_ptr, "highest", 7)) {
1491 newlr = CC->room.QRhighest;
1493 newlr = atol(new_ptr);
1496 CtdlGetUserLock(&CC->user, CC->curr_user);
1498 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1499 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1500 vbuf.v_lastseen = newlr;
1501 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1503 /* Only rewrite the record if it changed */
1504 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1505 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1506 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1509 CtdlPutUserLock(&CC->user);
1510 cprintf("%d %ld\n", CIT_OK, newlr);
1514 void cmd_seen(char *argbuf) {
1515 long target_msgnum = 0L;
1516 int target_setting = 0;
1518 if (CtdlAccessCheck(ac_logged_in)) {
1522 if (num_parms(argbuf) != 2) {
1523 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1527 target_msgnum = extract_long(argbuf, 0);
1528 target_setting = extract_int(argbuf, 1);
1530 CtdlSetSeen(&target_msgnum, 1, target_setting,
1531 ctdlsetseen_seen, NULL, NULL);
1532 cprintf("%d OK\n", CIT_OK);
1536 void cmd_gtsn(char *argbuf) {
1539 if (CtdlAccessCheck(ac_logged_in)) {
1543 CtdlGetSeen(buf, ctdlsetseen_seen);
1544 cprintf("%d %s\n", CIT_OK, buf);
1549 * API function for cmd_invt_kick() and anything else that needs to
1550 * invite or kick out a user to/from a room.
1552 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1554 int CtdlInvtKick(char *iuser, int op) {
1555 struct ctdluser USscratch;
1559 if (CtdlGetUser(&USscratch, iuser) != 0) {
1563 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1565 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1566 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1569 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1570 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1572 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1574 /* post a message in Aide> saying what we just did */
1575 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1577 ((op == 1) ? "invited to" : "kicked out of"),
1580 CtdlAideMessage(bbb,"User Admin Message");
1587 * INVT and KICK commands
1589 void cmd_invt_kick(char *iuser, int op) {
1592 * These commands are only allowed by aides, room aides,
1593 * and room namespace owners
1596 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1597 /* access granted */
1600 cprintf("%d Higher access or room ownership required.\n",
1601 ERROR + HIGHER_ACCESS_REQUIRED);
1605 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1607 cprintf("%d Can't add/remove users from this room.\n",
1612 if (CtdlInvtKick(iuser, op) != 0) {
1613 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1617 cprintf("%d %s %s %s.\n",
1619 ((op == 1) ? "invited to" : "kicked out of"),
1624 void cmd_invt(char *iuser) {cmd_invt_kick(iuser, 1);}
1625 void cmd_kick(char *iuser) {cmd_invt_kick(iuser, 0);}
1628 * Forget (Zap) the current room (API call)
1629 * Returns 0 on success
1631 int CtdlForgetThisRoom(void) {
1634 /* On some systems, Aides are not allowed to forget rooms */
1635 if (is_aide() && (config.c_aide_zap == 0)
1636 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1640 CtdlGetUserLock(&CC->user, CC->curr_user);
1641 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1643 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1644 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1646 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1647 CtdlPutUserLock(&CC->user);
1649 /* Return to the Lobby, so we don't end up in an undefined room */
1650 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
1657 * forget (Zap) the current room
1659 void cmd_forg(char *argbuf)
1662 if (CtdlAccessCheck(ac_logged_in)) {
1666 if (CtdlForgetThisRoom() == 0) {
1667 cprintf("%d Ok\n", CIT_OK);
1670 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1675 * Get Next Unregistered User
1677 void cmd_gnur(char *argbuf)
1679 struct cdbdata *cdbus;
1680 struct ctdluser usbuf;
1682 if (CtdlAccessCheck(ac_aide)) {
1686 if ((CitControl.MMflags & MM_VALID) == 0) {
1687 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1691 /* There are unvalidated users. Traverse the user database,
1692 * and return the first user we find that needs validation.
1694 cdb_rewind(CDB_USERS);
1695 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1696 memset(&usbuf, 0, sizeof(struct ctdluser));
1697 memcpy(&usbuf, cdbus->ptr,
1698 ((cdbus->len > sizeof(struct ctdluser)) ?
1699 sizeof(struct ctdluser) : cdbus->len));
1701 if ((usbuf.flags & US_NEEDVALID)
1702 && (usbuf.axlevel > AxDeleted)) {
1703 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1704 cdb_close_cursor(CDB_USERS);
1709 /* If we get to this point, there are no more unvalidated users.
1710 * Therefore we clear the "users need validation" flag.
1713 begin_critical_section(S_CONTROL);
1715 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1717 end_critical_section(S_CONTROL);
1718 cprintf("%d *** End of registration.\n", CIT_OK);
1727 void cmd_vali(char *v_args)
1731 struct ctdluser userbuf;
1733 extract_token(user, v_args, 0, '|', sizeof user);
1734 newax = extract_int(v_args, 1);
1736 if (CtdlAccessCheck(ac_aide) ||
1737 (newax > AxAideU) ||
1738 (newax < AxDeleted)) {
1742 if (CtdlGetUserLock(&userbuf, user) != 0) {
1743 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1747 userbuf.axlevel = newax;
1748 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1750 CtdlPutUserLock(&userbuf);
1752 /* If the access level was set to zero, delete the user */
1754 if (purge_user(user) == 0) {
1755 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1759 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1765 * Traverse the user file...
1767 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1770 struct ctdluser usbuf;
1771 struct cdbdata *cdbus;
1773 cdb_rewind(CDB_USERS);
1775 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1776 memset(&usbuf, 0, sizeof(struct ctdluser));
1777 memcpy(&usbuf, cdbus->ptr,
1778 ((cdbus->len > sizeof(struct ctdluser)) ?
1779 sizeof(struct ctdluser) : cdbus->len));
1781 (*CallBack) (&usbuf, in_data);
1787 * List one user (this works with cmd_list)
1789 void ListThisUser(struct ctdluser *usbuf, void *data)
1793 searchstring = (char *)data;
1794 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1798 if (usbuf->axlevel > AxDeleted) {
1799 if ((CC->user.axlevel >= AxAideU)
1800 || ((usbuf->flags & US_UNLISTED) == 0)
1801 || ((CC->internal_pgm))) {
1802 cprintf("%s|%d|%ld|%ld|%ld|%ld||\n",
1806 (long)usbuf->lastcall,
1814 * List users (searchstring may be empty to list all users)
1816 void cmd_list(char *cmdbuf)
1818 char searchstring[256];
1819 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1820 striplt(searchstring);
1821 cprintf("%d \n", LISTING_FOLLOWS);
1822 ForEachUser(ListThisUser, (void *)searchstring );
1830 * assorted info we need to check at login
1832 void cmd_chek(char *argbuf)
1838 if (CtdlAccessCheck(ac_logged_in)) {
1842 CtdlGetUser(&CC->user, CC->curr_user); /* no lock is needed here */
1843 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1846 if (CC->user.axlevel >= AxAideU) {
1848 if (CitControl.MMflags & MM_VALID)
1852 /* check for mail */
1853 mail = InitialMailCheck();
1855 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1860 * check to see if a user exists
1862 void cmd_qusr(char *who)
1864 struct ctdluser usbuf;
1866 if (CtdlGetUser(&usbuf, who) == 0) {
1867 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1869 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1875 * Administrative Get User Parameters
1877 void cmd_agup(char *cmdbuf)
1879 struct ctdluser usbuf;
1880 char requested_user[128];
1882 if (CtdlAccessCheck(ac_aide)) {
1886 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1887 if (CtdlGetUser(&usbuf, requested_user) != 0) {
1888 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1891 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1898 (int) usbuf.axlevel,
1900 (long)usbuf.lastcall,
1907 * Administrative Set User Parameters
1909 void cmd_asup(char *cmdbuf)
1911 struct ctdluser usbuf;
1912 char requested_user[128];
1918 if (CtdlAccessCheck(ac_aide))
1921 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1922 if (CtdlGetUserLock(&usbuf, requested_user) != 0) {
1923 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1926 np = num_parms(cmdbuf);
1928 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1930 usbuf.flags = extract_int(cmdbuf, 2);
1932 usbuf.timescalled = extract_int(cmdbuf, 3);
1934 usbuf.posted = extract_int(cmdbuf, 4);
1936 newax = extract_int(cmdbuf, 5);
1937 if ((newax >= AxDeleted) && (newax <= AxAideU)) {
1938 usbuf.axlevel = newax;
1942 usbuf.lastcall = extract_long(cmdbuf, 7);
1945 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1947 CtdlPutUserLock(&usbuf);
1948 if (usbuf.axlevel == AxDeleted) {
1949 if (purge_user(requested_user) == 0) {
1955 snprintf(notify, SIZ,
1956 "User \"%s\" has been deleted by %s.\n",
1957 usbuf.fullname, CC->user.fullname);
1958 CtdlAideMessage(notify, "User Deletion Message");
1961 cprintf("%d Ok", CIT_OK);
1963 cprintf(" (%s deleted)", requested_user);
1970 * Count the number of new mail messages the user has
1974 int num_newmsgs = 0;
1976 num_newmsgs = CC->newmail;
1979 return (num_newmsgs);
1984 * Count the number of new mail messages the user has
1986 int InitialMailCheck()
1988 int num_newmsgs = 0;
1990 char mailboxname[ROOMNAMELEN];
1991 struct ctdlroom mailbox;
1993 struct cdbdata *cdbfr;
1994 long *msglist = NULL;
1997 CtdlMailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1998 if (CtdlGetRoom(&mailbox, mailboxname) != 0)
2000 CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
2002 cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
2004 if (cdbfr != NULL) {
2005 msglist = malloc(cdbfr->len);
2006 memcpy(msglist, cdbfr->ptr, cdbfr->len);
2007 num_msgs = cdbfr->len / sizeof(long);
2011 for (a = 0; a < num_msgs; ++a) {
2012 if (msglist[a] > 0L) {
2013 if (msglist[a] > vbuf.v_lastseen) {
2018 if (msglist != NULL)
2021 return (num_newmsgs);
2027 * Set the preferred view for the current user/room combination
2029 void cmd_view(char *cmdbuf) {
2033 if (CtdlAccessCheck(ac_logged_in)) {
2037 requested_view = extract_int(cmdbuf, 0);
2039 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
2040 vbuf.v_view = requested_view;
2041 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
2043 cprintf("%d ok\n", CIT_OK);
2050 void cmd_renu(char *cmdbuf)
2053 char oldname[USERNAME_SIZE];
2054 char newname[USERNAME_SIZE];
2056 if (CtdlAccessCheck(ac_aide)) {
2060 extract_token(oldname, cmdbuf, 0, '|', sizeof oldname);
2061 extract_token(newname, cmdbuf, 1, '|', sizeof newname);
2063 retcode = rename_user(oldname, newname);
2066 cprintf("%d '%s' has been renamed to '%s'.\n", CIT_OK, oldname, newname);
2068 case RENAMEUSER_LOGGED_IN:
2069 cprintf("%d '%s' is currently logged in and cannot be renamed.\n",
2070 ERROR + ALREADY_LOGGED_IN , oldname);
2072 case RENAMEUSER_NOT_FOUND:
2073 cprintf("%d '%s' does not exist.\n", ERROR + NO_SUCH_USER, oldname);
2075 case RENAMEUSER_ALREADY_EXISTS:
2076 cprintf("%d A user named '%s' already exists.\n", ERROR + ALREADY_EXISTS, newname);
2080 cprintf("%d An unknown error occurred.\n", ERROR);
2085 /*****************************************************************************/
2086 /* MODULE INITIALIZATION STUFF */
2087 /*****************************************************************************/
2090 CTDL_MODULE_INIT(user_ops)
2093 CtdlRegisterProtoHook(cmd_user, "USER", "Autoconverted. TODO: document me.");
2094 CtdlRegisterProtoHook(cmd_pass, "PASS", "Autoconverted. TODO: document me.");
2095 CtdlRegisterProtoHook(cmd_creu, "CREU", "Autoconverted. TODO: document me.");
2096 CtdlRegisterProtoHook(cmd_setp, "SETP", "Autoconverted. TODO: document me.");
2097 CtdlRegisterProtoHook(cmd_getu, "GETU", "Autoconverted. TODO: document me.");
2098 CtdlRegisterProtoHook(cmd_setu, "SETU", "Autoconverted. TODO: document me.");
2099 CtdlRegisterProtoHook(cmd_slrp, "SLRP", "Autoconverted. TODO: document me.");
2100 CtdlRegisterProtoHook(cmd_invt, "INVT", "Autoconverted. TODO: document me.");
2101 CtdlRegisterProtoHook(cmd_kick, "KICK", "Autoconverted. TODO: document me.");
2102 CtdlRegisterProtoHook(cmd_forg, "FORG", "Autoconverted. TODO: document me.");
2103 CtdlRegisterProtoHook(cmd_gnur, "GNUR", "Autoconverted. TODO: document me.");
2104 CtdlRegisterProtoHook(cmd_vali, "VALI", "Autoconverted. TODO: document me.");
2105 CtdlRegisterProtoHook(cmd_list, "LIST", "Autoconverted. TODO: document me.");
2106 CtdlRegisterProtoHook(cmd_chek, "CHEK", "Autoconverted. TODO: document me.");
2107 CtdlRegisterProtoHook(cmd_qusr, "QUSR", "Autoconverted. TODO: document me.");
2108 CtdlRegisterProtoHook(cmd_agup, "AGUP", "Autoconverted. TODO: document me.");
2109 CtdlRegisterProtoHook(cmd_asup, "ASUP", "Autoconverted. TODO: document me.");
2110 CtdlRegisterProtoHook(cmd_seen, "SEEN", "Autoconverted. TODO: document me.");
2111 CtdlRegisterProtoHook(cmd_gtsn, "GTSN", "Autoconverted. TODO: document me.");
2112 CtdlRegisterProtoHook(cmd_view, "VIEW", "Autoconverted. TODO: document me.");
2113 CtdlRegisterProtoHook(cmd_renu, "RENU", "Autoconverted. TODO: document me.");
2114 CtdlRegisterProtoHook(cmd_newu, "NEWU", "Autoconverted. TODO: document me.");
2116 /* return our Subversion id for the Log */