2 * Server functions which perform operations on user objects.
4 * Copyright (c) 1987-2011 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License, version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
24 #include <sys/types.h>
27 #ifdef HAVE_SYS_STAT_H
31 #if TIME_WITH_SYS_TIME
32 # include <sys/time.h>
36 # include <sys/time.h>
44 #include <libcitadel.h>
49 #include "sysdep_decls.h"
55 #include "citserver.h"
56 #include "citadel_dirs.h"
59 #include "citadel_ldap.h"
61 #include "ctdl_module.h"
63 #include "internet_addressing.h"
70 void cmd_user(char *cmdbuf)
75 CON_syslog(LOG_DEBUG, "cmd_user(%s)\n", cmdbuf);
76 extract_token(username, cmdbuf, 0, '|', sizeof username);
77 CON_syslog(LOG_DEBUG, "username: %s\n", username);
79 CON_syslog(LOG_DEBUG, "username: %s\n", username);
81 a = CtdlLoginExistingUser(NULL, username);
83 case login_already_logged_in:
84 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
86 case login_too_many_users:
88 "Too many users are already online "
90 ERROR + MAX_SESSIONS_EXCEEDED,
91 config.c_nodename, config.c_maxsessions);
94 cprintf("%d Password required for %s\n",
95 MORE_DATA, CC->curr_user);
98 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
101 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
106 void cmd_pass(char *buf)
112 memset(password, 0, sizeof(password));
113 len = extract_token(password, buf, 0, '|', sizeof password);
114 a = CtdlTryPassword(password, len);
117 case pass_already_logged_in:
118 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
121 cprintf("%d You must send a name with USER first.\n",
122 ERROR + USERNAME_REQUIRED);
124 case pass_wrong_password:
125 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
128 logged_in_response();
135 * cmd_newu() - create a new user account and log in as that user
137 void cmd_newu(char *cmdbuf)
143 if (config.c_auth_mode != AUTHMODE_NATIVE) {
144 cprintf("%d This system does not use native mode authentication.\n",
149 if (config.c_disable_newu) {
150 cprintf("%d Self-service user account creation "
151 "is disabled on this system.\n", ERROR + NOT_HERE);
156 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
160 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
161 ERROR + MAX_SESSIONS_EXCEEDED,
162 config.c_nodename, config.c_maxsessions);
164 extract_token(username, cmdbuf, 0, '|', sizeof username);
166 len = cutuserkey(username);
168 if (IsEmptyStr(username)) {
169 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
173 if ((!strcasecmp(username, "bbs")) ||
174 (!strcasecmp(username, "new")) ||
175 (!strcasecmp(username, "."))) {
176 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
180 a = create_user(username, len, 1);
183 logged_in_response();
184 } else if (a == ERROR + ALREADY_EXISTS) {
185 cprintf("%d '%s' already exists.\n",
186 ERROR + ALREADY_EXISTS, username);
188 } else if (a == ERROR + INTERNAL_ERROR) {
189 cprintf("%d Internal error - user record disappeared?\n",
190 ERROR + INTERNAL_ERROR);
193 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
198 * set password - citadel protocol implementation
200 void cmd_setp(char *new_pw)
202 if (CtdlAccessCheck(ac_logged_in)) {
205 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
206 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
210 cprintf("%d The master prefix password cannot be changed with this command.\n",
215 if (!strcasecmp(new_pw, "GENERATE_RANDOM_PASSWORD")) {
216 char random_password[17];
217 snprintf(random_password, sizeof random_password, "%08lx%08lx", random(), random());
218 CtdlSetPassword(random_password);
219 cprintf("%d %s\n", CIT_OK, random_password);
223 if (IsEmptyStr(new_pw)) {
224 cprintf("%d Password unchanged.\n", CIT_OK);
227 CtdlSetPassword(new_pw);
228 cprintf("%d Password changed.\n", CIT_OK);
234 * cmd_creu() - administratively create a new user account (do not log in to it)
236 void cmd_creu(char *cmdbuf)
244 if (CtdlAccessCheck(ac_aide)) {
248 extract_token(username, cmdbuf, 0, '|', sizeof username);
251 if (IsEmptyStr(username)) {
252 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
255 len = cutuserkey(username);
258 extract_token(password, cmdbuf, 1, '|', sizeof password);
260 a = create_user(username, len, 0);
263 if (!IsEmptyStr(password)) {
264 CtdlGetUserLock(&tmp, username);
265 safestrncpy(tmp.password, password, sizeof(tmp.password));
266 CtdlPutUserLock(&tmp);
268 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
269 (!IsEmptyStr(password)) ? "and password set" :
272 } else if (a == ERROR + ALREADY_EXISTS) {
273 cprintf("%d '%s' already exists.\n", ERROR + ALREADY_EXISTS, username);
275 } else if ( (config.c_auth_mode != AUTHMODE_NATIVE) && (a == ERROR + NO_SUCH_USER) ) {
276 cprintf("%d User accounts are not created within Citadel in host authentication mode.\n",
277 ERROR + NO_SUCH_USER);
280 cprintf("%d An error occurred creating the user account.\n", ERROR + INTERNAL_ERROR);
287 * get user parameters
289 void cmd_getu(char *cmdbuf)
292 if (CtdlAccessCheck(ac_logged_in))
295 CtdlGetUser(&CC->user, CC->curr_user);
296 cprintf("%d 80|24|%d|\n",
298 (CC->user.flags & US_USER_SET)
303 * set user parameters
305 void cmd_setu(char *new_parms)
307 if (CtdlAccessCheck(ac_logged_in))
310 if (num_parms(new_parms) < 3) {
311 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
314 CtdlGetUserLock(&CC->user, CC->curr_user);
315 CC->user.flags = CC->user.flags & (~US_USER_SET);
316 CC->user.flags = CC->user.flags | (extract_int(new_parms, 2) & US_USER_SET);
317 CtdlPutUserLock(&CC->user);
318 cprintf("%d Ok\n", CIT_OK);
322 * set last read pointer
324 void cmd_slrp(char *new_ptr)
330 if (CtdlAccessCheck(ac_logged_in)) {
334 if (!strncasecmp(new_ptr, "highest", 7)) {
335 newlr = CC->room.QRhighest;
337 newlr = atol(new_ptr);
340 CtdlGetUserLock(&CC->user, CC->curr_user);
342 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
343 memcpy(&original_vbuf, &vbuf, sizeof(visit));
344 vbuf.v_lastseen = newlr;
345 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
347 /* Only rewrite the record if it changed */
348 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
349 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
350 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
353 CtdlPutUserLock(&CC->user);
354 cprintf("%d %ld\n", CIT_OK, newlr);
358 void cmd_seen(char *argbuf) {
359 long target_msgnum = 0L;
360 int target_setting = 0;
362 if (CtdlAccessCheck(ac_logged_in)) {
366 if (num_parms(argbuf) != 2) {
367 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
371 target_msgnum = extract_long(argbuf, 0);
372 target_setting = extract_int(argbuf, 1);
374 CtdlSetSeen(&target_msgnum, 1, target_setting,
375 ctdlsetseen_seen, NULL, NULL);
376 cprintf("%d OK\n", CIT_OK);
380 void cmd_gtsn(char *argbuf) {
383 if (CtdlAccessCheck(ac_logged_in)) {
387 /* Learn about the user and room in question */
388 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
390 cprintf("%d ", CIT_OK);
391 client_write(vbuf.v_seen, strlen(vbuf.v_seen));
392 client_write(HKEY("\n"));
396 * INVT and KICK commands
398 void cmd_invt_kick(char *iuser, int op) {
401 * These commands are only allowed by admins, room admins,
402 * and room namespace owners
404 if (is_room_aide()) {
406 } else if ( ((atol(CC->room.QRname) == CC->user.usernum) ) && (CC->user.usernum != 0) ) {
410 cprintf("%d Higher access or room ownership required.\n",
411 ERROR + HIGHER_ACCESS_REQUIRED);
415 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
417 cprintf("%d Can't add/remove users from this room.\n",
422 if (CtdlInvtKick(iuser, op) != 0) {
423 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
427 cprintf("%d %s %s %s.\n",
429 ((op == 1) ? "invited to" : "kicked out of"),
434 void cmd_invt(char *iuser) {cmd_invt_kick(iuser, 1);}
435 void cmd_kick(char *iuser) {cmd_invt_kick(iuser, 0);}
439 * forget (Zap) the current room
441 void cmd_forg(char *argbuf)
444 if (CtdlAccessCheck(ac_logged_in)) {
448 if (CtdlForgetThisRoom() == 0) {
449 cprintf("%d Ok\n", CIT_OK);
452 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
457 * Get Next Unregistered User
459 void cmd_gnur(char *argbuf)
461 struct cdbdata *cdbus;
462 struct ctdluser usbuf;
464 if (CtdlAccessCheck(ac_aide)) {
468 if ((CitControl.MMflags & MM_VALID) == 0) {
469 cprintf("%d There are no unvalidated users.\n", CIT_OK);
473 /* There are unvalidated users. Traverse the user database,
474 * and return the first user we find that needs validation.
476 cdb_rewind(CDB_USERS);
477 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
478 memset(&usbuf, 0, sizeof(struct ctdluser));
479 memcpy(&usbuf, cdbus->ptr,
480 ((cdbus->len > sizeof(struct ctdluser)) ?
481 sizeof(struct ctdluser) : cdbus->len));
483 if ((usbuf.flags & US_NEEDVALID)
484 && (usbuf.axlevel > AxDeleted)) {
485 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
486 cdb_close_cursor(CDB_USERS);
491 /* If we get to this point, there are no more unvalidated users.
492 * Therefore we clear the "users need validation" flag.
495 begin_critical_section(S_CONTROL);
497 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
499 end_critical_section(S_CONTROL);
500 cprintf("%d *** End of registration.\n", CIT_OK);
509 void cmd_vali(char *v_args)
513 struct ctdluser userbuf;
515 extract_token(user, v_args, 0, '|', sizeof user);
516 newax = extract_int(v_args, 1);
518 if (CtdlAccessCheck(ac_aide) ||
520 (newax < AxDeleted)) {
524 if (CtdlGetUserLock(&userbuf, user) != 0) {
525 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
529 userbuf.axlevel = newax;
530 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
532 CtdlPutUserLock(&userbuf);
534 /* If the access level was set to zero, delete the user */
536 if (purge_user(user) == 0) {
537 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
541 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
545 * List users (searchstring may be empty to list all users)
547 void cmd_list(char *cmdbuf)
549 char searchstring[256];
550 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
551 striplt(searchstring);
552 cprintf("%d \n", LISTING_FOLLOWS);
553 ForEachUser(ListThisUser, (void *)searchstring );
561 * assorted info we need to check at login
563 void cmd_chek(char *argbuf)
569 if (CtdlAccessCheck(ac_logged_in)) {
573 CtdlGetUser(&CC->user, CC->curr_user); /* no lock is needed here */
574 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
577 if (CC->user.axlevel >= AxAideU) {
579 if (CitControl.MMflags & MM_VALID)
584 mail = InitialMailCheck();
586 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
591 * check to see if a user exists
593 void cmd_qusr(char *who)
595 struct ctdluser usbuf;
597 if (CtdlGetUser(&usbuf, who) == 0) {
598 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
600 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
606 * Administrative Get User Parameters
608 void cmd_agup(char *cmdbuf)
610 struct ctdluser usbuf;
611 char requested_user[128];
613 if (CtdlAccessCheck(ac_aide)) {
617 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
618 if (CtdlGetUser(&usbuf, requested_user) != 0) {
619 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
622 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
631 (long)usbuf.lastcall,
638 * Administrative Set User Parameters
640 void cmd_asup(char *cmdbuf)
642 struct ctdluser usbuf;
643 char requested_user[128];
649 if (CtdlAccessCheck(ac_aide))
652 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
653 if (CtdlGetUserLock(&usbuf, requested_user) != 0) {
654 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
657 np = num_parms(cmdbuf);
659 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
661 usbuf.flags = extract_int(cmdbuf, 2);
663 usbuf.timescalled = extract_int(cmdbuf, 3);
665 usbuf.posted = extract_int(cmdbuf, 4);
667 newax = extract_int(cmdbuf, 5);
668 if ((newax >= AxDeleted) && (newax <= AxAideU)) {
669 usbuf.axlevel = newax;
673 usbuf.lastcall = extract_long(cmdbuf, 7);
676 usbuf.USuserpurge = extract_int(cmdbuf, 8);
678 CtdlPutUserLock(&usbuf);
679 if (usbuf.axlevel == AxDeleted) {
680 if (purge_user(requested_user) == 0) {
686 snprintf(notify, SIZ,
687 "User \"%s\" has been deleted by %s.\n",
689 (CC->logged_in ? CC->user.fullname : "an administrator")
691 CtdlAideMessage(notify, "User Deletion Message");
694 cprintf("%d Ok", CIT_OK);
696 cprintf(" (%s deleted)", requested_user);
702 * Set the preferred view for the current user/room combination
704 void cmd_view(char *cmdbuf) {
708 if (CtdlAccessCheck(ac_logged_in)) {
712 requested_view = extract_int(cmdbuf, 0);
714 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
715 vbuf.v_view = requested_view;
716 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
718 cprintf("%d ok\n", CIT_OK);
725 void cmd_renu(char *cmdbuf)
728 char oldname[USERNAME_SIZE];
729 char newname[USERNAME_SIZE];
731 if (CtdlAccessCheck(ac_aide)) {
735 extract_token(oldname, cmdbuf, 0, '|', sizeof oldname);
736 extract_token(newname, cmdbuf, 1, '|', sizeof newname);
738 retcode = rename_user(oldname, newname);
741 cprintf("%d '%s' has been renamed to '%s'.\n", CIT_OK, oldname, newname);
743 case RENAMEUSER_LOGGED_IN:
744 cprintf("%d '%s' is currently logged in and cannot be renamed.\n",
745 ERROR + ALREADY_LOGGED_IN , oldname);
747 case RENAMEUSER_NOT_FOUND:
748 cprintf("%d '%s' does not exist.\n", ERROR + NO_SUCH_USER, oldname);
750 case RENAMEUSER_ALREADY_EXISTS:
751 cprintf("%d A user named '%s' already exists.\n", ERROR + ALREADY_EXISTS, newname);
755 cprintf("%d An unknown error occurred.\n", ERROR);
760 void cmd_quit(char *argbuf)
762 cprintf("%d Goodbye.\n", CIT_OK);
763 CC->kill_me = KILLME_CLIENT_LOGGED_OUT;
767 void cmd_lout(char *argbuf)
771 cprintf("%d logged out.\n", CIT_OK);
775 /*****************************************************************************/
776 /* MODULE INITIALIZATION STUFF */
777 /*****************************************************************************/
780 CTDL_MODULE_INIT(serv_user)
783 CtdlRegisterProtoHook(cmd_user, "USER", "Submit username for login");
784 CtdlRegisterProtoHook(cmd_pass, "PASS", "Complete login by submitting a password");
785 CtdlRegisterProtoHook(cmd_quit, "QUIT", "log out and disconnect from server");
786 CtdlRegisterProtoHook(cmd_lout, "LOUT", "log out but do not disconnect from server");
787 CtdlRegisterProtoHook(cmd_creu, "CREU", "Create User");
788 CtdlRegisterProtoHook(cmd_setp, "SETP", "Set the password for an account");
789 CtdlRegisterProtoHook(cmd_getu, "GETU", "Get User parameters");
790 CtdlRegisterProtoHook(cmd_setu, "SETU", "Set User parameters");
791 CtdlRegisterProtoHook(cmd_slrp, "SLRP", "Set Last Read Pointer");
792 CtdlRegisterProtoHook(cmd_invt, "INVT", "Invite a user to a room");
793 CtdlRegisterProtoHook(cmd_kick, "KICK", "Kick a user out of a room");
794 CtdlRegisterProtoHook(cmd_forg, "FORG", "Forget a room");
795 CtdlRegisterProtoHook(cmd_gnur, "GNUR", "Get Next Unregistered User");
796 CtdlRegisterProtoHook(cmd_vali, "VALI", "Validate new users");
797 CtdlRegisterProtoHook(cmd_list, "LIST", "List users");
798 CtdlRegisterProtoHook(cmd_chek, "CHEK", "assorted info we need to check at login");
799 CtdlRegisterProtoHook(cmd_qusr, "QUSR", "check to see if a user exists");
800 CtdlRegisterProtoHook(cmd_agup, "AGUP", "Administratively Get User Parameters");
801 CtdlRegisterProtoHook(cmd_asup, "ASUP", "Administratively Set User Parameters");
802 CtdlRegisterProtoHook(cmd_seen, "SEEN", "Manipulate seen/unread message flags");
803 CtdlRegisterProtoHook(cmd_gtsn, "GTSN", "Fetch seen/unread message flags");
804 CtdlRegisterProtoHook(cmd_view, "VIEW", "Set preferred view for user/room combination");
805 CtdlRegisterProtoHook(cmd_renu, "RENU", "Rename a user");
806 CtdlRegisterProtoHook(cmd_newu, "NEWU", "Log in as a new user");
808 /* return our Subversion id for the Log */