4 * Server functions which perform operations on user objects.
17 #include <sys/types.h>
19 #ifdef HAVE_SYS_STAT_H
23 #if TIME_WITH_SYS_TIME
24 # include <sys/time.h>
28 # include <sys/time.h>
36 #include <libcitadel.h>
42 #include "sysdep_decls.h"
49 #include "citserver.h"
50 #include "citadel_dirs.h"
53 #include "citadel_ldap.h"
56 #include "ctdl_module.h"
58 /* These pipes are used to talk to the chkpwd daemon, which is forked during startup */
59 int chkpwd_write_pipe[2];
60 int chkpwd_read_pipe[2];
63 * makeuserkey() - convert a username into the format used as a database key
64 * (it's just the username converted into lower case)
66 INLINE void makeuserkey(char *key, char *username) {
69 len = strlen(username);
70 if (len >= USERNAME_SIZE)
72 CtdlLogPrintf (CTDL_EMERG, "Username to long: %s", username);
74 len = USERNAME_SIZE - 1;
75 username[USERNAME_SIZE - 1]='\0';
77 for (i=0; i<=len; ++i) {
78 key[i] = tolower(username[i]);
84 * getuser() - retrieve named user into supplied buffer.
85 * returns 0 on success
87 int getuser(struct ctdluser *usbuf, char name[])
89 return CtdlGetUser(usbuf, name);
94 * CtdlGetUser() - retrieve named user into supplied buffer.
95 * returns 0 on success
97 int CtdlGetUser(struct ctdluser *usbuf, char name[])
100 char usernamekey[USERNAME_SIZE];
101 struct cdbdata *cdbus;
104 memset(usbuf, 0, sizeof(struct ctdluser));
107 makeuserkey(usernamekey, name);
108 cdbus = cdb_fetch(CDB_USERS, usernamekey, strlen(usernamekey));
110 if (cdbus == NULL) { /* user not found */
114 memcpy(usbuf, cdbus->ptr,
115 ((cdbus->len > sizeof(struct ctdluser)) ?
116 sizeof(struct ctdluser) : cdbus->len));
125 * CtdlGetUserLock() - same as getuser() but locks the record
127 int CtdlGetUserLock(struct ctdluser *usbuf, char *name)
131 retcode = CtdlGetUser(usbuf, name);
133 begin_critical_section(S_USERS);
140 * lgetuser() - same as getuser() but locks the record
142 int lgetuser(struct ctdluser *usbuf, char *name)
144 return CtdlGetUserLock(usbuf, name);
149 * CtdlPutUser() - write user buffer into the correct place on disk
151 void CtdlPutUser(struct ctdluser *usbuf)
153 char usernamekey[USERNAME_SIZE];
155 makeuserkey(usernamekey, usbuf->fullname);
157 usbuf->version = REV_LEVEL;
159 usernamekey, strlen(usernamekey),
160 usbuf, sizeof(struct ctdluser));
166 * putuser() - write user buffer into the correct place on disk
168 void putuser(struct ctdluser *usbuf)
175 * CtdlPutUserLock() - same as putuser() but locks the record
177 void CtdlPutUserLock(struct ctdluser *usbuf)
180 end_critical_section(S_USERS);
185 * lputuser() - same as putuser() but locks the record
187 void lputuser(struct ctdluser *usbuf)
189 CtdlPutUserLock(usbuf);
194 * rename_user() - this is tricky because the user's display name is the database key
196 * Returns 0 on success or nonzero if there was an error...
199 int rename_user(char *oldname, char *newname) {
200 int retcode = RENAMEUSER_OK;
201 struct ctdluser usbuf;
203 char oldnamekey[USERNAME_SIZE];
204 char newnamekey[USERNAME_SIZE];
206 /* Create the database keys... */
207 makeuserkey(oldnamekey, oldname);
208 makeuserkey(newnamekey, newname);
210 /* Lock up and get going */
211 begin_critical_section(S_USERS);
213 /* We cannot rename a user who is currently logged in */
214 if (CtdlIsUserLoggedIn(oldname)) {
215 end_critical_section(S_USERS);
216 return RENAMEUSER_LOGGED_IN;
219 if (CtdlGetUser(&usbuf, newname) == 0) {
220 retcode = RENAMEUSER_ALREADY_EXISTS;
224 if (CtdlGetUser(&usbuf, oldname) != 0) {
225 retcode = RENAMEUSER_NOT_FOUND;
228 else { /* Sanity checks succeeded. Now rename the user. */
229 if (usbuf.usernum == 0)
231 CtdlLogPrintf (CTDL_DEBUG, "Can not rename user \"Citadel\".\n");
232 retcode = RENAMEUSER_NOT_FOUND;
234 CtdlLogPrintf(CTDL_DEBUG, "Renaming <%s> to <%s>\n", oldname, newname);
235 cdb_delete(CDB_USERS, oldnamekey, strlen(oldnamekey));
236 safestrncpy(usbuf.fullname, newname, sizeof usbuf.fullname);
238 cdb_store(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long),
239 usbuf.fullname, strlen(usbuf.fullname)+1 );
241 retcode = RENAMEUSER_OK;
247 end_critical_section(S_USERS);
254 * Index-generating function used by Ctdl[Get|Set]Relationship
256 int GenerateRelationshipIndex(char *IndexBuf,
268 TheIndex.iRoomID = RoomID;
269 TheIndex.iRoomGen = RoomGen;
270 TheIndex.iUserID = UserID;
272 memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
273 return (sizeof(TheIndex));
279 * Back end for CtdlSetRelationship()
281 void put_visit(struct visit *newvisit)
286 memset (IndexBuf, 0, sizeof (IndexBuf));
287 /* Generate an index */
288 IndexLen = GenerateRelationshipIndex(IndexBuf,
291 newvisit->v_usernum);
293 /* Store the record */
294 cdb_store(CDB_VISIT, IndexBuf, IndexLen,
295 newvisit, sizeof(struct visit)
303 * Define a relationship between a user and a room
305 void CtdlSetRelationship(struct visit *newvisit,
306 struct ctdluser *rel_user,
307 struct ctdlroom *rel_room)
311 /* We don't use these in Citadel because they're implicit by the
312 * index, but they must be present if the database is exported.
314 newvisit->v_roomnum = rel_room->QRnumber;
315 newvisit->v_roomgen = rel_room->QRgen;
316 newvisit->v_usernum = rel_user->usernum;
322 * Locate a relationship between a user and a room
324 void CtdlGetRelationship(struct visit *vbuf,
325 struct ctdluser *rel_user,
326 struct ctdlroom *rel_room)
331 struct cdbdata *cdbvisit;
333 /* Generate an index */
334 IndexLen = GenerateRelationshipIndex(IndexBuf,
339 /* Clear out the buffer */
340 memset(vbuf, 0, sizeof(struct visit));
342 cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
343 if (cdbvisit != NULL) {
344 memcpy(vbuf, cdbvisit->ptr,
345 ((cdbvisit->len > sizeof(struct visit)) ?
346 sizeof(struct visit) : cdbvisit->len));
350 /* If this is the first time the user has seen this room,
351 * set the view to be the default for the room.
353 vbuf->v_view = rel_room->QRdefaultview;
356 /* Set v_seen if necessary */
357 if (vbuf->v_seen[0] == 0) {
358 snprintf(vbuf->v_seen, sizeof vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
363 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
365 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
369 void MailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix)
371 snprintf(buf, n, "%010ld.%s", who->usernum, prefix);
376 * Is the user currently logged in an Aide?
380 if (CC->user.axlevel >= 6)
388 * Is the user currently logged in an Aide *or* the room aide for this room?
390 int is_room_aide(void)
393 if (!CC->logged_in) {
397 if ((CC->user.axlevel >= 6)
398 || (CC->room.QRroomaide == CC->user.usernum)) {
406 * CtdlGetUserByNumber() - get user by number
407 * returns 0 if user was found
409 * Note: fetching a user this way requires one additional database operation.
411 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number)
413 struct cdbdata *cdbun;
416 cdbun = cdb_fetch(CDB_USERSBYNUMBER, &number, sizeof(long));
418 CtdlLogPrintf(CTDL_INFO, "User %ld not found\n", number);
422 CtdlLogPrintf(CTDL_INFO, "User %ld maps to %s\n", number, cdbun->ptr);
423 r = CtdlGetUser(usbuf, cdbun->ptr);
429 * getuserbynumber() - get user by number
430 * returns 0 if user was found
432 * Note: fetching a user this way requires one additional database operation.
434 int getuserbynumber(struct ctdluser *usbuf, long number)
436 return CtdlGetUserByNumber(usbuf, number);
442 * Helper function for rebuild_usersbynumber()
444 void rebuild_ubn_for_user(struct ctdluser *usbuf, void *data) {
447 struct ubnlist *next;
448 char username[USERNAME_SIZE];
452 static struct ubnlist *u = NULL;
453 struct ubnlist *ptr = NULL;
455 /* Lazy programming here. Call this function as a ForEachUser backend
456 * in order to queue up the room names, or call it with a null user
457 * to make it do the processing.
460 ptr = (struct ubnlist *) malloc(sizeof (struct ubnlist));
461 if (ptr == NULL) return;
463 ptr->usernum = usbuf->usernum;
464 safestrncpy(ptr->username, usbuf->fullname, sizeof ptr->username);
471 CtdlLogPrintf(CTDL_DEBUG, "Rebuilding usersbynumber index %10ld : %s\n",
472 u->usernum, u->username);
473 cdb_store(CDB_USERSBYNUMBER, &u->usernum, sizeof(long), u->username, strlen(u->username)+1);
484 * Rebuild the users-by-number index
486 void rebuild_usersbynumber(void) {
487 cdb_trunc(CDB_USERSBYNUMBER); /* delete the old indices */
488 ForEachUser(rebuild_ubn_for_user, NULL); /* enumerate the users */
489 rebuild_ubn_for_user(NULL, NULL); /* and index them */
495 * getuserbyuid() - get user by system uid (for PAM mode authentication)
496 * returns 0 if user was found
498 * WARNING: don't use this function unless you absolutely have to. It does
499 * a sequential search and therefore is computationally expensive.
501 int getuserbyuid(struct ctdluser *usbuf, uid_t number)
503 struct cdbdata *cdbus;
505 cdb_rewind(CDB_USERS);
507 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
508 memset(usbuf, 0, sizeof(struct ctdluser));
509 memcpy(usbuf, cdbus->ptr,
510 ((cdbus->len > sizeof(struct ctdluser)) ?
511 sizeof(struct ctdluser) : cdbus->len));
513 if (usbuf->uid == number) {
514 cdb_close_cursor(CDB_USERS);
522 * Back end for cmd_user() and its ilk
524 * NOTE: "authname" should only be used if we are attempting to use the "master user" feature
526 int CtdlLoginExistingUser(char *authname, char *trythisname)
531 CtdlLogPrintf(9, "CtdlLoginExistingUser(%s, %s)\n", authname, trythisname);
533 if ((CC->logged_in)) {
534 return login_already_logged_in;
537 if (trythisname == NULL) return login_not_found;
539 if (!strncasecmp(trythisname, "SYS_", 4))
541 CtdlLogPrintf(CTDL_DEBUG, "System user \"%s\" is not allowed to log in.\n", trythisname);
542 return login_not_found;
545 /* If a "master user" is defined, handle its authentication if specified */
547 if (strlen(config.c_master_user) > 0) if (strlen(config.c_master_pass) > 0) if (authname) {
548 if (!strcasecmp(authname, config.c_master_user)) {
553 /* Continue attempting user validation... */
554 safestrncpy(username, trythisname, USERNAME_SIZE);
557 if (IsEmptyStr(username)) {
558 return login_not_found;
561 if (config.c_auth_mode == AUTHMODE_HOST) {
566 struct passwd *tempPwdPtr;
569 CtdlLogPrintf(CTDL_DEBUG, "asking host about <%s>\n", username);
570 #ifdef HAVE_GETPWNAM_R
571 #ifdef SOLARIS_GETPWUID
572 CtdlLogPrintf(CTDL_DEBUG, "Calling getpwnam_r()\n");
573 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer);
574 #else // SOLARIS_GETPWUID
575 CtdlLogPrintf(CTDL_DEBUG, "Calling getpwnam_r()\n");
576 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
577 #endif // SOLARIS_GETPWUID
578 #else // HAVE_GETPWNAM_R
579 CtdlLogPrintf(CTDL_DEBUG, "SHOULD NEVER GET HERE!!!\n");
581 #endif // HAVE_GETPWNAM_R
582 if (tempPwdPtr == NULL) {
583 CtdlLogPrintf(CTDL_DEBUG, "no such user <%s>\n", username);
584 return login_not_found;
587 /* Locate the associated Citadel account.
588 * If not found, make one attempt to create it.
590 found_user = getuserbyuid(&CC->user, pd.pw_uid);
591 CtdlLogPrintf(CTDL_DEBUG, "found it: uid=%ld, gecos=%s here: %d\n",
592 (long)pd.pw_uid, pd.pw_gecos, found_user);
593 if (found_user != 0) {
594 create_user(username, 0);
595 found_user = getuserbyuid(&CC->user, pd.pw_uid);
601 else if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
609 found_user = CtdlTryUserLDAP(username, ldap_dn, sizeof ldap_dn, ldap_cn, sizeof ldap_cn, &ldap_uid);
610 if (found_user != 0) {
611 return login_not_found;
614 found_user = getuserbyuid(&CC->user, ldap_uid);
615 if (found_user != 0) {
616 create_user(trythisname, 0);
617 found_user = getuserbyuid(&CC->user, ldap_uid);
620 if (found_user == 0) {
621 if (CC->ldap_dn != NULL) free(CC->ldap_dn);
622 CC->ldap_dn = strdup(ldap_dn);
629 /* native auth mode */
631 struct recptypes *valid = NULL;
633 /* First, try to log in as if the supplied name is a display name */
634 found_user = CtdlGetUser(&CC->user, username);
636 /* If that didn't work, try to log in as if the supplied name
637 * is an e-mail address
639 if (found_user != 0) {
640 valid = validate_recipients(username, NULL, 0);
642 if (valid->num_local == 1) {
643 found_user = CtdlGetUser(&CC->user, valid->recp_local);
645 free_recipients(valid);
650 /* Did we find something? */
651 if (found_user == 0) {
652 if (((CC->nologin)) && (CC->user.axlevel < 6)) {
653 return login_too_many_users;
655 safestrncpy(CC->curr_user, CC->user.fullname,
656 sizeof CC->curr_user);
660 return login_not_found;
668 void cmd_user(char *cmdbuf)
673 CtdlLogPrintf(CTDL_DEBUG, "cmd_user(%s)\n", cmdbuf);
674 extract_token(username, cmdbuf, 0, '|', sizeof username);
675 CtdlLogPrintf(CTDL_DEBUG, "username: %s\n", username);
677 CtdlLogPrintf(CTDL_DEBUG, "username: %s\n", username);
679 a = CtdlLoginExistingUser(NULL, username);
681 case login_already_logged_in:
682 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
684 case login_too_many_users:
686 "Too many users are already online "
688 ERROR + MAX_SESSIONS_EXCEEDED,
689 config.c_nodename, config.c_maxsessions);
692 cprintf("%d Password required for %s\n",
693 MORE_DATA, CC->curr_user);
695 case login_not_found:
696 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
699 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
706 * session startup code which is common to both cmd_pass() and cmd_newu()
711 CtdlLogPrintf(CTDL_NOTICE, "<%s> logged in\n", CC->curr_user);
713 CtdlGetUserLock(&CC->user, CC->curr_user);
714 ++(CC->user.timescalled);
715 CC->previous_login = CC->user.lastcall;
716 time(&CC->user.lastcall);
718 /* If this user's name is the name of the system administrator
719 * (as specified in setup), automatically assign access level 6.
721 if (!strcasecmp(CC->user.fullname, config.c_sysadm)) {
722 CC->user.axlevel = 6;
725 /* If we're authenticating off the host system, automatically give
726 * root the highest level of access.
728 if (config.c_auth_mode == AUTHMODE_HOST) {
729 if (CC->user.uid == 0) {
730 CC->user.axlevel = 6;
734 CtdlPutUserLock(&CC->user);
737 * Populate CC->cs_inet_email with a default address. This will be
738 * overwritten with the user's directory address, if one exists, when
739 * the vCard module's login hook runs.
741 snprintf(CC->cs_inet_email, sizeof CC->cs_inet_email, "%s@%s",
742 CC->user.fullname, config.c_fqdn);
743 convert_spaces_to_underscores(CC->cs_inet_email);
745 /* Create any personal rooms required by the system.
746 * (Technically, MAILROOM should be there already, but just in case...)
748 CtdlCreateRoom(MAILROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
749 CtdlCreateRoom(SENTITEMS, 4, "", 0, 1, 0, VIEW_MAILBOX);
750 CtdlCreateRoom(USERTRASHROOM, 4, "", 0, 1, 0, VIEW_MAILBOX);
751 /* CtdlCreateRoom(USERDRAFTROOM, 4, "", 0, 1, 0, VIEW_MAILBOX); temporarily disabled for 7.60 */
753 /* Run any startup routines registered by loadable modules */
754 PerformSessionHooks(EVT_LOGIN);
756 /* Enter the lobby */
757 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
761 void logged_in_response(void)
763 cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
764 CIT_OK, CC->user.fullname, CC->user.axlevel,
765 CC->user.timescalled, CC->user.posted,
766 CC->user.flags, CC->user.usernum,
773 * misc things to be taken care of when a user is logged out
781 void CtdlUserLogout(void)
783 CitContext *CCC = CC; /* CachedCitContext - performance boost */
785 * If there is a download in progress, abort it.
787 if (CCC->download_fp != NULL) {
788 fclose(CCC->download_fp);
789 CCC->download_fp = NULL;
793 * If there is an upload in progress, abort it.
795 if (CCC->upload_fp != NULL) {
800 * If we were talking to a network node, we're not anymore...
802 if (!IsEmptyStr(CCC->net_node)) {
803 network_talking_to(CCC->net_node, NTT_REMOVE);
806 /* Run any hooks registered by modules... */
807 PerformSessionHooks(EVT_LOGOUT);
810 * Clear out some session data. Most likely, the CitContext for this
811 * session is about to get nuked when the session disconnects, but
812 * since it's possible to log in again without reconnecting, we cannot
813 * make that assumption.
815 strcpy(CCC->fake_username, "");
816 strcpy(CCC->fake_hostname, "");
817 strcpy(CCC->fake_roomname, "");
820 /* Check to see if the user was deleted whilst logged in and purge them if necessary */
821 if ((CCC->user.axlevel == 0) && (CCC->user.usernum))
822 purge_user(CCC->user.fullname);
824 /* Free any output buffers */
829 * Validate a password on the host unix system by talking to the chkpwd daemon
831 static int validpw(uid_t uid, const char *pass)
836 if (IsEmptyStr(pass)) {
837 CtdlLogPrintf(CTDL_DEBUG, "refusing to check empty password for uid=%d using chkpwd...\n", uid);
841 CtdlLogPrintf(CTDL_DEBUG, "Validating password for uid=%d using chkpwd...\n", uid);
843 begin_critical_section(S_CHKPWD);
844 rv = write(chkpwd_write_pipe[1], &uid, sizeof(uid_t));
845 rv = write(chkpwd_write_pipe[1], pass, 256);
846 rv = read(chkpwd_read_pipe[0], buf, 4);
847 end_critical_section(S_CHKPWD);
849 if (!strncmp(buf, "PASS", 4)) {
850 CtdlLogPrintf(CTDL_DEBUG, "...pass\n");
854 CtdlLogPrintf(CTDL_DEBUG, "...fail\n");
859 * Start up the chkpwd daemon so validpw() has something to talk to
861 void start_chkpwd_daemon(void) {
863 struct stat filestats;
866 CtdlLogPrintf(CTDL_DEBUG, "Starting chkpwd daemon for host authentication mode\n");
868 if ((stat(file_chkpwd, &filestats)==-1) ||
869 (filestats.st_size==0)){
870 printf("didn't find chkpwd daemon in %s: %s\n", file_chkpwd, strerror(errno));
873 if (pipe(chkpwd_write_pipe) != 0) {
874 CtdlLogPrintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
877 if (pipe(chkpwd_read_pipe) != 0) {
878 CtdlLogPrintf(CTDL_EMERG, "Unable to create pipe for chkpwd daemon: %s\n", strerror(errno));
883 if (chkpwd_pid < 0) {
884 CtdlLogPrintf(CTDL_EMERG, "Unable to fork chkpwd daemon: %s\n", strerror(errno));
887 if (chkpwd_pid == 0) {
888 CtdlLogPrintf(CTDL_DEBUG, "Now calling dup2() write\n");
889 dup2(chkpwd_write_pipe[0], 0);
890 CtdlLogPrintf(CTDL_DEBUG, "Now calling dup2() write\n");
891 dup2(chkpwd_read_pipe[1], 1);
892 CtdlLogPrintf(CTDL_DEBUG, "Now closing stuff\n");
893 for (i=2; i<256; ++i) close(i);
894 CtdlLogPrintf(CTDL_DEBUG, "Now calling execl(%s)\n", file_chkpwd);
895 execl(file_chkpwd, file_chkpwd, NULL);
896 CtdlLogPrintf(CTDL_EMERG, "Unable to exec chkpwd daemon: %s\n", strerror(errno));
903 int CtdlTryPassword(char *password)
907 if ((CC->logged_in)) {
908 CtdlLogPrintf(CTDL_WARNING, "CtdlTryPassword: already logged in\n");
909 return pass_already_logged_in;
911 if (!strcmp(CC->curr_user, NLI)) {
912 CtdlLogPrintf(CTDL_WARNING, "CtdlTryPassword: no user selected\n");
915 if (CtdlGetUser(&CC->user, CC->curr_user)) {
916 CtdlLogPrintf(CTDL_ERR, "CtdlTryPassword: internal error\n");
917 return pass_internal_error;
919 if (password == NULL) {
920 CtdlLogPrintf(CTDL_INFO, "CtdlTryPassword: NULL password string supplied\n");
921 return pass_wrong_password;
926 code = strcmp(password, config.c_master_pass);
929 else if (config.c_auth_mode == AUTHMODE_HOST) {
933 if (validpw(CC->user.uid, password)) {
937 * sooper-seekrit hack: populate the password field in the
938 * citadel database with the password that the user typed,
939 * if it's correct. This allows most sites to convert from
940 * host auth to native auth if they want to. If you think
941 * this is a security hazard, comment it out.
944 CtdlGetUserLock(&CC->user, CC->curr_user);
945 safestrncpy(CC->user.password, password, sizeof CC->user.password);
946 CtdlPutUserLock(&CC->user);
949 * (sooper-seekrit hack ends here)
959 else if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
963 if ((CC->ldap_dn) && (!CtdlTryPasswordLDAP(CC->ldap_dn, password))) {
974 /* native auth mode */
977 strproc(CC->user.password);
978 code = strcasecmp(CC->user.password, password);
980 strproc(CC->user.password);
981 code = strcasecmp(CC->user.password, password);
988 CtdlLogPrintf(CTDL_WARNING, "Bad password specified for <%s>\n", CC->curr_user);
989 return pass_wrong_password;
994 void cmd_pass(char *buf)
999 memset(password, 0, sizeof(password));
1000 extract_token(password, buf, 0, '|', sizeof password);
1001 a = CtdlTryPassword(password);
1004 case pass_already_logged_in:
1005 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
1008 cprintf("%d You must send a name with USER first.\n",
1009 ERROR + USERNAME_REQUIRED);
1011 case pass_wrong_password:
1012 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
1015 logged_in_response();
1023 * Delete a user record *and* all of its related resources.
1025 int purge_user(char pname[])
1028 struct ctdluser usbuf;
1029 char usernamekey[USERNAME_SIZE];
1031 int user_is_logged_in = 0;
1033 makeuserkey(usernamekey, pname);
1035 /* If the name is empty we can't find them in the DB any way so just return */
1036 if (IsEmptyStr(pname))
1037 return (ERROR + NO_SUCH_USER);
1039 if (CtdlGetUser(&usbuf, pname) != 0) {
1040 CtdlLogPrintf(CTDL_ERR, "Cannot purge user <%s> - not found\n", pname);
1041 return (ERROR + NO_SUCH_USER);
1043 /* Don't delete a user who is currently logged in. Instead, just
1044 * set the access level to 0, and let the account get swept up
1045 * during the next purge.
1047 user_is_logged_in = 0;
1048 begin_critical_section(S_SESSION_TABLE);
1049 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
1050 if (ccptr->user.usernum == usbuf.usernum) {
1051 user_is_logged_in = 1;
1054 end_critical_section(S_SESSION_TABLE);
1055 if (user_is_logged_in == 1) {
1056 CtdlLogPrintf(CTDL_WARNING, "User <%s> is logged in; not deleting.\n", pname);
1058 CtdlPutUser(&usbuf);
1061 CtdlLogPrintf(CTDL_NOTICE, "Deleting user <%s>\n", pname);
1063 /* Perform any purge functions registered by server extensions */
1064 PerformUserHooks(&usbuf, EVT_PURGEUSER);
1066 /* delete any existing user/room relationships */
1067 cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
1069 /* delete the users-by-number index record */
1070 cdb_delete(CDB_USERSBYNUMBER, &usbuf.usernum, sizeof(long));
1072 /* delete the userlog entry */
1073 cdb_delete(CDB_USERS, usernamekey, strlen(usernamekey));
1075 /* remove the user's bio file */
1083 /* remove the user's picture */
1095 int internal_create_user (char *username, struct ctdluser *usbuf, uid_t uid)
1097 if (!CtdlGetUser(usbuf, username)) {
1098 return (ERROR + ALREADY_EXISTS);
1101 /* Go ahead and initialize a new user record */
1102 memset(usbuf, 0, sizeof(struct ctdluser));
1103 safestrncpy(usbuf->fullname, username, sizeof usbuf->fullname);
1104 strcpy(usbuf->password, "");
1107 /* These are the default flags on new accounts */
1108 usbuf->flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
1110 usbuf->timescalled = 0;
1112 usbuf->axlevel = config.c_initax;
1113 usbuf->USscreenwidth = 80;
1114 usbuf->USscreenheight = 24;
1115 usbuf->lastcall = time(NULL);
1117 /* fetch a new user number */
1118 usbuf->usernum = get_new_user_number();
1120 /* add user to the database */
1122 cdb_store(CDB_USERSBYNUMBER, &usbuf->usernum, sizeof(long), usbuf->fullname, strlen(usbuf->fullname)+1);
1130 * create_user() - back end processing to create a new user
1132 * Set 'newusername' to the desired account name.
1133 * Set 'become_user' to nonzero if this is self-service account creation and we want
1134 * to actually log in as the user we just created, otherwise set it to 0.
1136 int create_user(char *newusername, int become_user)
1138 struct ctdluser usbuf;
1139 struct ctdlroom qrbuf;
1141 char mailboxname[ROOMNAMELEN];
1147 safestrncpy(username, newusername, sizeof username);
1151 if (config.c_auth_mode == AUTHMODE_HOST) {
1153 /* host auth mode */
1156 struct passwd *tempPwdPtr;
1157 char pwdbuffer[256];
1159 #ifdef HAVE_GETPWNAM_R
1160 #ifdef SOLARIS_GETPWUID
1161 tempPwdPtr = getpwnam_r(username, &pd, pwdbuffer, sizeof(pwdbuffer));
1162 #else // SOLARIS_GETPWUID
1163 getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
1164 #endif // SOLARIS_GETPWUID
1165 #else // HAVE_GETPWNAM_R
1167 #endif // HAVE_GETPWNAM_R
1168 if (tempPwdPtr != NULL) {
1169 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
1171 if (IsEmptyStr (username))
1173 safestrncpy(username, pd.pw_name, sizeof username);
1177 return (ERROR + NO_SUCH_USER);
1182 if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
1183 if (CtdlTryUserLDAP(username, NULL, 0, username, sizeof username, &uid) != 0) {
1184 return(ERROR + NO_SUCH_USER);
1187 #endif /* HAVE_LDAP */
1189 if ((retval = internal_create_user(username, &usbuf, uid)) != 0)
1193 * Give the user a private mailbox and a configuration room.
1194 * Make the latter an invisible system room.
1196 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
1197 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
1199 CtdlMailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
1200 CtdlCreateRoom(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
1201 if (CtdlGetRoomLock(&qrbuf, mailboxname) == 0) {
1202 qrbuf.QRflags2 |= QR2_SYSTEM;
1203 CtdlPutRoomLock(&qrbuf);
1206 /* Perform any create functions registered by server extensions */
1207 PerformUserHooks(&usbuf, EVT_NEWUSER);
1209 /* Everything below this line can be bypassed if administratively
1210 * creating a user, instead of doing self-service account creation
1214 /* Now become the user we just created */
1215 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
1216 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
1219 /* Check to make sure we're still who we think we are */
1220 if (CtdlGetUser(&CC->user, CC->curr_user)) {
1221 return (ERROR + INTERNAL_ERROR);
1226 "New user account <%s> has been created, from host %s [%s].\n",
1231 CtdlAideMessage(buf, "User Creation Notice");
1232 CtdlLogPrintf(CTDL_NOTICE, "New user <%s> created\n", username);
1239 * cmd_newu() - create a new user account and log in as that user
1241 void cmd_newu(char *cmdbuf)
1246 if (config.c_auth_mode != AUTHMODE_NATIVE) {
1247 cprintf("%d This system does not use native mode authentication.\n",
1252 if (config.c_disable_newu) {
1253 cprintf("%d Self-service user account creation "
1254 "is disabled on this system.\n", ERROR + NOT_HERE);
1258 if (CC->logged_in) {
1259 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
1263 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
1264 ERROR + MAX_SESSIONS_EXCEEDED,
1265 config.c_nodename, config.c_maxsessions);
1267 extract_token(username, cmdbuf, 0, '|', sizeof username);
1271 if (IsEmptyStr(username)) {
1272 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1276 if ((!strcasecmp(username, "bbs")) ||
1277 (!strcasecmp(username, "new")) ||
1278 (!strcasecmp(username, "."))) {
1279 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
1283 a = create_user(username, 1);
1286 logged_in_response();
1287 } else if (a == ERROR + ALREADY_EXISTS) {
1288 cprintf("%d '%s' already exists.\n",
1289 ERROR + ALREADY_EXISTS, username);
1291 } else if (a == ERROR + INTERNAL_ERROR) {
1292 cprintf("%d Internal error - user record disappeared?\n",
1293 ERROR + INTERNAL_ERROR);
1296 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
1302 * set password - back end api code
1304 void CtdlSetPassword(char *new_pw)
1306 CtdlGetUserLock(&CC->user, CC->curr_user);
1307 safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
1308 CtdlPutUserLock(&CC->user);
1309 CtdlLogPrintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
1310 PerformSessionHooks(EVT_SETPASS);
1315 * set password - citadel protocol implementation
1317 void cmd_setp(char *new_pw)
1319 int generate_random_password = 0;
1321 if (CtdlAccessCheck(ac_logged_in)) {
1324 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
1325 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
1328 if (CC->is_master) {
1329 cprintf("%d The master prefix password cannot be changed with this command.\n",
1334 if (!strcasecmp(new_pw, "GENERATE_RANDOM_PASSWORD")) {
1335 char random_password[17];
1336 generate_random_password = 1;
1337 snprintf(random_password, sizeof random_password, "%08lx%08lx", random(), random());
1338 CtdlSetPassword(random_password);
1339 cprintf("%d %s\n", CIT_OK, random_password);
1343 if (IsEmptyStr(new_pw)) {
1344 cprintf("%d Password unchanged.\n", CIT_OK);
1347 CtdlSetPassword(new_pw);
1348 cprintf("%d Password changed.\n", CIT_OK);
1354 * cmd_creu() - administratively create a new user account (do not log in to it)
1356 void cmd_creu(char *cmdbuf)
1361 struct ctdluser tmp;
1363 if (CtdlAccessCheck(ac_aide)) {
1367 extract_token(username, cmdbuf, 0, '|', sizeof username);
1368 extract_token(password, cmdbuf, 1, '|', sizeof password);
1374 if (IsEmptyStr(username)) {
1375 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1379 a = create_user(username, 0);
1382 if (!IsEmptyStr(password)) {
1383 CtdlGetUserLock(&tmp, username);
1384 safestrncpy(tmp.password, password, sizeof(tmp.password));
1385 CtdlPutUserLock(&tmp);
1387 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1388 (!IsEmptyStr(password)) ? "and password set" :
1389 "with no password");
1391 } else if (a == ERROR + ALREADY_EXISTS) {
1392 cprintf("%d '%s' already exists.\n", ERROR + ALREADY_EXISTS, username);
1394 } else if ( (config.c_auth_mode != AUTHMODE_NATIVE) && (a == ERROR + NO_SUCH_USER) ) {
1395 cprintf("%d User accounts are not created within Citadel in host authentication mode.\n",
1396 ERROR + NO_SUCH_USER);
1399 cprintf("%d An error occurred creating the user account.\n", ERROR + INTERNAL_ERROR);
1406 * get user parameters
1408 void cmd_getu(char *cmdbuf)
1411 if (CtdlAccessCheck(ac_logged_in))
1414 CtdlGetUser(&CC->user, CC->curr_user);
1415 cprintf("%d %d|%d|%d|\n",
1417 CC->user.USscreenwidth,
1418 CC->user.USscreenheight,
1419 (CC->user.flags & US_USER_SET)
1424 * set user parameters
1426 void cmd_setu(char *new_parms)
1428 if (CtdlAccessCheck(ac_logged_in))
1431 if (num_parms(new_parms) < 3) {
1432 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1435 CtdlGetUserLock(&CC->user, CC->curr_user);
1436 CC->user.USscreenwidth = extract_int(new_parms, 0);
1437 CC->user.USscreenheight = extract_int(new_parms, 1);
1438 CC->user.flags = CC->user.flags & (~US_USER_SET);
1439 CC->user.flags = CC->user.flags |
1440 (extract_int(new_parms, 2) & US_USER_SET);
1442 CtdlPutUserLock(&CC->user);
1443 cprintf("%d Ok\n", CIT_OK);
1447 * set last read pointer
1449 void cmd_slrp(char *new_ptr)
1453 struct visit original_vbuf;
1455 if (CtdlAccessCheck(ac_logged_in)) {
1459 if (!strncasecmp(new_ptr, "highest", 7)) {
1460 newlr = CC->room.QRhighest;
1462 newlr = atol(new_ptr);
1465 CtdlGetUserLock(&CC->user, CC->curr_user);
1467 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1468 memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1469 vbuf.v_lastseen = newlr;
1470 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1472 /* Only rewrite the record if it changed */
1473 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1474 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1475 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1478 CtdlPutUserLock(&CC->user);
1479 cprintf("%d %ld\n", CIT_OK, newlr);
1483 void cmd_seen(char *argbuf) {
1484 long target_msgnum = 0L;
1485 int target_setting = 0;
1487 if (CtdlAccessCheck(ac_logged_in)) {
1491 if (num_parms(argbuf) != 2) {
1492 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1496 target_msgnum = extract_long(argbuf, 0);
1497 target_setting = extract_int(argbuf, 1);
1499 CtdlSetSeen(&target_msgnum, 1, target_setting,
1500 ctdlsetseen_seen, NULL, NULL);
1501 cprintf("%d OK\n", CIT_OK);
1505 void cmd_gtsn(char *argbuf) {
1508 if (CtdlAccessCheck(ac_logged_in)) {
1512 CtdlGetSeen(buf, ctdlsetseen_seen);
1513 cprintf("%d %s\n", CIT_OK, buf);
1518 * API function for cmd_invt_kick() and anything else that needs to
1519 * invite or kick out a user to/from a room.
1521 * Set iuser to the name of the user, and op to 1=invite or 0=kick
1523 int CtdlInvtKick(char *iuser, int op) {
1524 struct ctdluser USscratch;
1528 if (CtdlGetUser(&USscratch, iuser) != 0) {
1532 CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1534 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1535 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1538 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1539 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1541 CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1543 /* post a message in Aide> saying what we just did */
1544 snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1546 ((op == 1) ? "invited to" : "kicked out of"),
1549 CtdlAideMessage(bbb,"User Admin Message");
1556 * INVT and KICK commands
1558 void cmd_invt_kick(char *iuser, int op) {
1561 * These commands are only allowed by aides, room aides,
1562 * and room namespace owners
1565 || (atol(CC->room.QRname) == CC->user.usernum) ) {
1566 /* access granted */
1569 cprintf("%d Higher access or room ownership required.\n",
1570 ERROR + HIGHER_ACCESS_REQUIRED);
1574 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1576 cprintf("%d Can't add/remove users from this room.\n",
1581 if (CtdlInvtKick(iuser, op) != 0) {
1582 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1586 cprintf("%d %s %s %s.\n",
1588 ((op == 1) ? "invited to" : "kicked out of"),
1593 void cmd_invt(char *iuser) {cmd_invt_kick(iuser, 1);}
1594 void cmd_kick(char *iuser) {cmd_invt_kick(iuser, 0);}
1597 * Forget (Zap) the current room (API call)
1598 * Returns 0 on success
1600 int CtdlForgetThisRoom(void) {
1603 /* On some systems, Aides are not allowed to forget rooms */
1604 if (is_aide() && (config.c_aide_zap == 0)
1605 && ((CC->room.QRflags & QR_MAILBOX) == 0) ) {
1609 CtdlGetUserLock(&CC->user, CC->curr_user);
1610 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1612 vbuf.v_flags = vbuf.v_flags | V_FORGET;
1613 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1615 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1616 CtdlPutUserLock(&CC->user);
1618 /* Return to the Lobby, so we don't end up in an undefined room */
1619 CtdlUserGoto(config.c_baseroom, 0, 0, NULL, NULL);
1626 * forget (Zap) the current room
1628 void cmd_forg(char *argbuf)
1631 if (CtdlAccessCheck(ac_logged_in)) {
1635 if (CtdlForgetThisRoom() == 0) {
1636 cprintf("%d Ok\n", CIT_OK);
1639 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1644 * Get Next Unregistered User
1646 void cmd_gnur(char *argbuf)
1648 struct cdbdata *cdbus;
1649 struct ctdluser usbuf;
1651 if (CtdlAccessCheck(ac_aide)) {
1655 if ((CitControl.MMflags & MM_VALID) == 0) {
1656 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1660 /* There are unvalidated users. Traverse the user database,
1661 * and return the first user we find that needs validation.
1663 cdb_rewind(CDB_USERS);
1664 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1665 memset(&usbuf, 0, sizeof(struct ctdluser));
1666 memcpy(&usbuf, cdbus->ptr,
1667 ((cdbus->len > sizeof(struct ctdluser)) ?
1668 sizeof(struct ctdluser) : cdbus->len));
1670 if ((usbuf.flags & US_NEEDVALID)
1671 && (usbuf.axlevel > 0)) {
1672 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1673 cdb_close_cursor(CDB_USERS);
1678 /* If we get to this point, there are no more unvalidated users.
1679 * Therefore we clear the "users need validation" flag.
1682 begin_critical_section(S_CONTROL);
1684 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1686 end_critical_section(S_CONTROL);
1687 cprintf("%d *** End of registration.\n", CIT_OK);
1696 void cmd_vali(char *v_args)
1700 struct ctdluser userbuf;
1702 extract_token(user, v_args, 0, '|', sizeof user);
1703 newax = extract_int(v_args, 1);
1705 if (CtdlAccessCheck(ac_aide)) {
1709 if (CtdlGetUserLock(&userbuf, user) != 0) {
1710 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1714 userbuf.axlevel = newax;
1715 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1717 CtdlPutUserLock(&userbuf);
1719 /* If the access level was set to zero, delete the user */
1721 if (purge_user(user) == 0) {
1722 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1726 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1732 * Traverse the user file...
1734 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1737 struct ctdluser usbuf;
1738 struct cdbdata *cdbus;
1740 cdb_rewind(CDB_USERS);
1742 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1743 memset(&usbuf, 0, sizeof(struct ctdluser));
1744 memcpy(&usbuf, cdbus->ptr,
1745 ((cdbus->len > sizeof(struct ctdluser)) ?
1746 sizeof(struct ctdluser) : cdbus->len));
1748 (*CallBack) (&usbuf, in_data);
1754 * List one user (this works with cmd_list)
1756 void ListThisUser(struct ctdluser *usbuf, void *data)
1760 searchstring = (char *)data;
1761 if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1765 if (usbuf->axlevel > 0) {
1766 if ((CC->user.axlevel >= 6)
1767 || ((usbuf->flags & US_UNLISTED) == 0)
1768 || ((CC->internal_pgm))) {
1769 cprintf("%s|%d|%ld|%ld|%ld|%ld||\n",
1773 (long)usbuf->lastcall,
1781 * List users (searchstring may be empty to list all users)
1783 void cmd_list(char *cmdbuf)
1785 char searchstring[256];
1786 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1787 striplt(searchstring);
1788 cprintf("%d \n", LISTING_FOLLOWS);
1789 ForEachUser(ListThisUser, (void *)searchstring );
1797 * assorted info we need to check at login
1799 void cmd_chek(char *argbuf)
1805 if (CtdlAccessCheck(ac_logged_in)) {
1809 CtdlGetUser(&CC->user, CC->curr_user); /* no lock is needed here */
1810 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1813 if (CC->user.axlevel >= 6) {
1815 if (CitControl.MMflags & MM_VALID)
1819 /* check for mail */
1820 mail = InitialMailCheck();
1822 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1827 * check to see if a user exists
1829 void cmd_qusr(char *who)
1831 struct ctdluser usbuf;
1833 if (CtdlGetUser(&usbuf, who) == 0) {
1834 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1836 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1842 * Administrative Get User Parameters
1844 void cmd_agup(char *cmdbuf)
1846 struct ctdluser usbuf;
1847 char requested_user[128];
1849 if (CtdlAccessCheck(ac_aide)) {
1853 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1854 if (CtdlGetUser(&usbuf, requested_user) != 0) {
1855 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1858 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1865 (int) usbuf.axlevel,
1867 (long)usbuf.lastcall,
1874 * Administrative Set User Parameters
1876 void cmd_asup(char *cmdbuf)
1878 struct ctdluser usbuf;
1879 char requested_user[128];
1885 if (CtdlAccessCheck(ac_aide))
1888 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1889 if (CtdlGetUserLock(&usbuf, requested_user) != 0) {
1890 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1893 np = num_parms(cmdbuf);
1895 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1897 usbuf.flags = extract_int(cmdbuf, 2);
1899 usbuf.timescalled = extract_int(cmdbuf, 3);
1901 usbuf.posted = extract_int(cmdbuf, 4);
1903 newax = extract_int(cmdbuf, 5);
1904 if ((newax >= 0) && (newax <= 6)) {
1905 usbuf.axlevel = extract_int(cmdbuf, 5);
1909 usbuf.lastcall = extract_long(cmdbuf, 7);
1912 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1914 CtdlPutUserLock(&usbuf);
1915 if (usbuf.axlevel == 0) {
1916 if (purge_user(requested_user) == 0) {
1922 snprintf(notify, SIZ,
1923 "User \"%s\" has been deleted by %s.\n",
1924 usbuf.fullname, CC->user.fullname);
1925 CtdlAideMessage(notify, "User Deletion Message");
1928 cprintf("%d Ok", CIT_OK);
1930 cprintf(" (%s deleted)", requested_user);
1937 * Check to see if the user who we just sent mail to is logged in. If yes,
1938 * bump the 'new mail' counter for their session. That enables them to
1939 * receive a new mail notification without having to hit the database.
1941 void BumpNewMailCounter(long which_user) {
1944 begin_critical_section(S_SESSION_TABLE);
1946 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1947 if (ptr->user.usernum == which_user) {
1952 end_critical_section(S_SESSION_TABLE);
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", "Autoconverted. TODO: document me.");
2081 CtdlRegisterProtoHook(cmd_pass, "PASS", "Autoconverted. TODO: document me.");
2082 CtdlRegisterProtoHook(cmd_creu, "CREU", "Autoconverted. TODO: document me.");
2083 CtdlRegisterProtoHook(cmd_setp, "SETP", "Autoconverted. TODO: document me.");
2084 CtdlRegisterProtoHook(cmd_getu, "GETU", "Autoconverted. TODO: document me.");
2085 CtdlRegisterProtoHook(cmd_setu, "SETU", "Autoconverted. TODO: document me.");
2086 CtdlRegisterProtoHook(cmd_slrp, "SLRP", "Autoconverted. TODO: document me.");
2087 CtdlRegisterProtoHook(cmd_invt, "INVT", "Autoconverted. TODO: document me.");
2088 CtdlRegisterProtoHook(cmd_kick, "KICK", "Autoconverted. TODO: document me.");
2089 CtdlRegisterProtoHook(cmd_forg, "FORG", "Autoconverted. TODO: document me.");
2090 CtdlRegisterProtoHook(cmd_gnur, "GNUR", "Autoconverted. TODO: document me.");
2091 CtdlRegisterProtoHook(cmd_vali, "VALI", "Autoconverted. TODO: document me.");
2092 CtdlRegisterProtoHook(cmd_list, "LIST", "Autoconverted. TODO: document me.");
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 */