2 * Server functions which perform operations on user objects.
4 * Copyright (c) 1987-2011 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License, version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
24 #include <sys/types.h>
27 #ifdef HAVE_SYS_STAT_H
31 #if TIME_WITH_SYS_TIME
32 # include <sys/time.h>
36 # include <sys/time.h>
44 #include <libcitadel.h>
49 #include "sysdep_decls.h"
56 #include "citserver.h"
57 #include "citadel_dirs.h"
60 #include "citadel_ldap.h"
62 #include "ctdl_module.h"
64 #include "internet_addressing.h"
71 void cmd_user(char *cmdbuf)
76 CON_syslog(LOG_DEBUG, "cmd_user(%s)\n", cmdbuf);
77 extract_token(username, cmdbuf, 0, '|', sizeof username);
78 CON_syslog(LOG_DEBUG, "username: %s\n", username);
80 CON_syslog(LOG_DEBUG, "username: %s\n", username);
82 a = CtdlLoginExistingUser(NULL, username);
84 case login_already_logged_in:
85 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
87 case login_too_many_users:
89 "Too many users are already online "
91 ERROR + MAX_SESSIONS_EXCEEDED,
92 config.c_nodename, config.c_maxsessions);
95 cprintf("%d Password required for %s\n",
96 MORE_DATA, CC->curr_user);
99 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
102 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
107 void cmd_pass(char *buf)
113 memset(password, 0, sizeof(password));
114 len = extract_token(password, buf, 0, '|', sizeof password);
115 a = CtdlTryPassword(password, len);
118 case pass_already_logged_in:
119 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
122 cprintf("%d You must send a name with USER first.\n",
123 ERROR + USERNAME_REQUIRED);
125 case pass_wrong_password:
126 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
129 logged_in_response();
136 * cmd_newu() - create a new user account and log in as that user
138 void cmd_newu(char *cmdbuf)
144 if (config.c_auth_mode != AUTHMODE_NATIVE) {
145 cprintf("%d This system does not use native mode authentication.\n",
150 if (config.c_disable_newu) {
151 cprintf("%d Self-service user account creation "
152 "is disabled on this system.\n", ERROR + NOT_HERE);
157 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
161 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
162 ERROR + MAX_SESSIONS_EXCEEDED,
163 config.c_nodename, config.c_maxsessions);
165 extract_token(username, cmdbuf, 0, '|', sizeof username);
167 len = cutuserkey(username);
169 if (IsEmptyStr(username)) {
170 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
174 if ((!strcasecmp(username, "bbs")) ||
175 (!strcasecmp(username, "new")) ||
176 (!strcasecmp(username, "."))) {
177 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
181 a = create_user(username, len, 1);
184 logged_in_response();
185 } else if (a == ERROR + ALREADY_EXISTS) {
186 cprintf("%d '%s' already exists.\n",
187 ERROR + ALREADY_EXISTS, username);
189 } else if (a == ERROR + INTERNAL_ERROR) {
190 cprintf("%d Internal error - user record disappeared?\n",
191 ERROR + INTERNAL_ERROR);
194 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
199 * set password - citadel protocol implementation
201 void cmd_setp(char *new_pw)
203 if (CtdlAccessCheck(ac_logged_in)) {
206 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
207 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
211 cprintf("%d The master prefix password cannot be changed with this command.\n",
216 if (!strcasecmp(new_pw, "GENERATE_RANDOM_PASSWORD")) {
217 char random_password[17];
218 snprintf(random_password, sizeof random_password, "%08lx%08lx", random(), random());
219 CtdlSetPassword(random_password);
220 cprintf("%d %s\n", CIT_OK, random_password);
224 if (IsEmptyStr(new_pw)) {
225 cprintf("%d Password unchanged.\n", CIT_OK);
228 CtdlSetPassword(new_pw);
229 cprintf("%d Password changed.\n", CIT_OK);
235 * cmd_creu() - administratively create a new user account (do not log in to it)
237 void cmd_creu(char *cmdbuf)
245 if (CtdlAccessCheck(ac_aide)) {
249 extract_token(username, cmdbuf, 0, '|', sizeof username);
252 if (IsEmptyStr(username)) {
253 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
256 len = cutuserkey(username);
259 extract_token(password, cmdbuf, 1, '|', sizeof password);
261 a = create_user(username, len, 0);
264 if (!IsEmptyStr(password)) {
265 CtdlGetUserLock(&tmp, username);
266 safestrncpy(tmp.password, password, sizeof(tmp.password));
267 CtdlPutUserLock(&tmp);
269 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
270 (!IsEmptyStr(password)) ? "and password set" :
273 } else if (a == ERROR + ALREADY_EXISTS) {
274 cprintf("%d '%s' already exists.\n", ERROR + ALREADY_EXISTS, username);
276 } else if ( (config.c_auth_mode != AUTHMODE_NATIVE) && (a == ERROR + NO_SUCH_USER) ) {
277 cprintf("%d User accounts are not created within Citadel in host authentication mode.\n",
278 ERROR + NO_SUCH_USER);
281 cprintf("%d An error occurred creating the user account.\n", ERROR + INTERNAL_ERROR);
288 * get user parameters
290 void cmd_getu(char *cmdbuf)
293 if (CtdlAccessCheck(ac_logged_in))
296 CtdlGetUser(&CC->user, CC->curr_user);
297 cprintf("%d 80|24|%d|\n",
299 (CC->user.flags & US_USER_SET)
304 * set user parameters
306 void cmd_setu(char *new_parms)
308 if (CtdlAccessCheck(ac_logged_in))
311 if (num_parms(new_parms) < 3) {
312 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
315 CtdlGetUserLock(&CC->user, CC->curr_user);
316 CC->user.flags = CC->user.flags & (~US_USER_SET);
317 CC->user.flags = CC->user.flags | (extract_int(new_parms, 2) & US_USER_SET);
318 CtdlPutUserLock(&CC->user);
319 cprintf("%d Ok\n", CIT_OK);
323 * set last read pointer
325 void cmd_slrp(char *new_ptr)
331 if (CtdlAccessCheck(ac_logged_in)) {
335 if (!strncasecmp(new_ptr, "highest", 7)) {
336 newlr = CC->room.QRhighest;
338 newlr = atol(new_ptr);
341 CtdlGetUserLock(&CC->user, CC->curr_user);
343 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
344 memcpy(&original_vbuf, &vbuf, sizeof(visit));
345 vbuf.v_lastseen = newlr;
346 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
348 /* Only rewrite the record if it changed */
349 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
350 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
351 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
354 CtdlPutUserLock(&CC->user);
355 cprintf("%d %ld\n", CIT_OK, newlr);
359 void cmd_seen(char *argbuf) {
360 long target_msgnum = 0L;
361 int target_setting = 0;
363 if (CtdlAccessCheck(ac_logged_in)) {
367 if (num_parms(argbuf) != 2) {
368 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
372 target_msgnum = extract_long(argbuf, 0);
373 target_setting = extract_int(argbuf, 1);
375 CtdlSetSeen(&target_msgnum, 1, target_setting,
376 ctdlsetseen_seen, NULL, NULL);
377 cprintf("%d OK\n", CIT_OK);
381 void cmd_gtsn(char *argbuf) {
384 if (CtdlAccessCheck(ac_logged_in)) {
388 /* Learn about the user and room in question */
389 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
391 cprintf("%d ", CIT_OK);
392 client_write(vbuf.v_seen, strlen(vbuf.v_seen));
393 client_write(HKEY("\n"));
397 * INVT and KICK commands
399 void cmd_invt_kick(char *iuser, int op) {
402 * These commands are only allowed by admins, room admins,
403 * and room namespace owners
405 if (is_room_aide()) {
407 } else if ( ((atol(CC->room.QRname) == CC->user.usernum) ) && (CC->user.usernum != 0) ) {
411 cprintf("%d Higher access or room ownership required.\n",
412 ERROR + HIGHER_ACCESS_REQUIRED);
416 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
418 cprintf("%d Can't add/remove users from this room.\n",
423 if (CtdlInvtKick(iuser, op) != 0) {
424 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
428 cprintf("%d %s %s %s.\n",
430 ((op == 1) ? "invited to" : "kicked out of"),
435 void cmd_invt(char *iuser) {cmd_invt_kick(iuser, 1);}
436 void cmd_kick(char *iuser) {cmd_invt_kick(iuser, 0);}
440 * forget (Zap) the current room
442 void cmd_forg(char *argbuf)
445 if (CtdlAccessCheck(ac_logged_in)) {
449 if (CtdlForgetThisRoom() == 0) {
450 cprintf("%d Ok\n", CIT_OK);
453 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
458 * Get Next Unregistered User
460 void cmd_gnur(char *argbuf)
462 struct cdbdata *cdbus;
463 struct ctdluser usbuf;
465 if (CtdlAccessCheck(ac_aide)) {
469 if ((CitControl.MMflags & MM_VALID) == 0) {
470 cprintf("%d There are no unvalidated users.\n", CIT_OK);
474 /* There are unvalidated users. Traverse the user database,
475 * and return the first user we find that needs validation.
477 cdb_rewind(CDB_USERS);
478 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
479 memset(&usbuf, 0, sizeof(struct ctdluser));
480 memcpy(&usbuf, cdbus->ptr,
481 ((cdbus->len > sizeof(struct ctdluser)) ?
482 sizeof(struct ctdluser) : cdbus->len));
484 if ((usbuf.flags & US_NEEDVALID)
485 && (usbuf.axlevel > AxDeleted)) {
486 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
487 cdb_close_cursor(CDB_USERS);
492 /* If we get to this point, there are no more unvalidated users.
493 * Therefore we clear the "users need validation" flag.
496 begin_critical_section(S_CONTROL);
498 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
500 end_critical_section(S_CONTROL);
501 cprintf("%d *** End of registration.\n", CIT_OK);
510 void cmd_vali(char *v_args)
514 struct ctdluser userbuf;
516 extract_token(user, v_args, 0, '|', sizeof user);
517 newax = extract_int(v_args, 1);
519 if (CtdlAccessCheck(ac_aide) ||
521 (newax < AxDeleted)) {
525 if (CtdlGetUserLock(&userbuf, user) != 0) {
526 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
530 userbuf.axlevel = newax;
531 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
533 CtdlPutUserLock(&userbuf);
535 /* If the access level was set to zero, delete the user */
537 if (purge_user(user) == 0) {
538 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
542 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
546 * List users (searchstring may be empty to list all users)
548 void cmd_list(char *cmdbuf)
550 char searchstring[256];
551 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
552 striplt(searchstring);
553 cprintf("%d \n", LISTING_FOLLOWS);
554 ForEachUser(ListThisUser, (void *)searchstring );
562 * assorted info we need to check at login
564 void cmd_chek(char *argbuf)
570 if (CtdlAccessCheck(ac_logged_in)) {
574 CtdlGetUser(&CC->user, CC->curr_user); /* no lock is needed here */
575 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
578 if (CC->user.axlevel >= AxAideU) {
580 if (CitControl.MMflags & MM_VALID)
585 mail = InitialMailCheck();
587 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
592 * check to see if a user exists
594 void cmd_qusr(char *who)
596 struct ctdluser usbuf;
598 if (CtdlGetUser(&usbuf, who) == 0) {
599 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
601 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
607 * Administrative Get User Parameters
609 void cmd_agup(char *cmdbuf)
611 struct ctdluser usbuf;
612 char requested_user[128];
614 if (CtdlAccessCheck(ac_aide)) {
618 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
619 if (CtdlGetUser(&usbuf, requested_user) != 0) {
620 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
623 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
632 (long)usbuf.lastcall,
639 * Administrative Set User Parameters
641 void cmd_asup(char *cmdbuf)
643 struct ctdluser usbuf;
644 char requested_user[128];
650 if (CtdlAccessCheck(ac_aide))
653 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
654 if (CtdlGetUserLock(&usbuf, requested_user) != 0) {
655 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
658 np = num_parms(cmdbuf);
660 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
662 usbuf.flags = extract_int(cmdbuf, 2);
664 usbuf.timescalled = extract_int(cmdbuf, 3);
666 usbuf.posted = extract_int(cmdbuf, 4);
668 newax = extract_int(cmdbuf, 5);
669 if ((newax >= AxDeleted) && (newax <= AxAideU)) {
670 usbuf.axlevel = newax;
674 usbuf.lastcall = extract_long(cmdbuf, 7);
677 usbuf.USuserpurge = extract_int(cmdbuf, 8);
679 CtdlPutUserLock(&usbuf);
680 if (usbuf.axlevel == AxDeleted) {
681 if (purge_user(requested_user) == 0) {
687 snprintf(notify, SIZ,
688 "User \"%s\" has been deleted by %s.\n",
690 (CC->logged_in ? CC->user.fullname : "an administrator")
692 CtdlAideMessage(notify, "User Deletion Message");
695 cprintf("%d Ok", CIT_OK);
697 cprintf(" (%s deleted)", requested_user);
703 * Set the preferred view for the current user/room combination
705 void cmd_view(char *cmdbuf) {
709 if (CtdlAccessCheck(ac_logged_in)) {
713 requested_view = extract_int(cmdbuf, 0);
715 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
716 vbuf.v_view = requested_view;
717 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
719 cprintf("%d ok\n", CIT_OK);
726 void cmd_renu(char *cmdbuf)
729 char oldname[USERNAME_SIZE];
730 char newname[USERNAME_SIZE];
732 if (CtdlAccessCheck(ac_aide)) {
736 extract_token(oldname, cmdbuf, 0, '|', sizeof oldname);
737 extract_token(newname, cmdbuf, 1, '|', sizeof newname);
739 retcode = rename_user(oldname, newname);
742 cprintf("%d '%s' has been renamed to '%s'.\n", CIT_OK, oldname, newname);
744 case RENAMEUSER_LOGGED_IN:
745 cprintf("%d '%s' is currently logged in and cannot be renamed.\n",
746 ERROR + ALREADY_LOGGED_IN , oldname);
748 case RENAMEUSER_NOT_FOUND:
749 cprintf("%d '%s' does not exist.\n", ERROR + NO_SUCH_USER, oldname);
751 case RENAMEUSER_ALREADY_EXISTS:
752 cprintf("%d A user named '%s' already exists.\n", ERROR + ALREADY_EXISTS, newname);
756 cprintf("%d An unknown error occurred.\n", ERROR);
761 void cmd_quit(char *argbuf)
763 cprintf("%d Goodbye.\n", CIT_OK);
764 CC->kill_me = KILLME_CLIENT_LOGGED_OUT;
768 void cmd_lout(char *argbuf)
772 cprintf("%d logged out.\n", CIT_OK);
776 /*****************************************************************************/
777 /* MODULE INITIALIZATION STUFF */
778 /*****************************************************************************/
781 CTDL_MODULE_INIT(serv_user)
784 CtdlRegisterProtoHook(cmd_user, "USER", "Submit username for login");
785 CtdlRegisterProtoHook(cmd_pass, "PASS", "Complete login by submitting a password");
786 CtdlRegisterProtoHook(cmd_quit, "QUIT", "log out and disconnect from server");
787 CtdlRegisterProtoHook(cmd_lout, "LOUT", "log out but do not disconnect from server");
788 CtdlRegisterProtoHook(cmd_creu, "CREU", "Create User");
789 CtdlRegisterProtoHook(cmd_setp, "SETP", "Set the password for an account");
790 CtdlRegisterProtoHook(cmd_getu, "GETU", "Get User parameters");
791 CtdlRegisterProtoHook(cmd_setu, "SETU", "Set User parameters");
792 CtdlRegisterProtoHook(cmd_slrp, "SLRP", "Set Last Read Pointer");
793 CtdlRegisterProtoHook(cmd_invt, "INVT", "Invite a user to a room");
794 CtdlRegisterProtoHook(cmd_kick, "KICK", "Kick a user out of a room");
795 CtdlRegisterProtoHook(cmd_forg, "FORG", "Forget a room");
796 CtdlRegisterProtoHook(cmd_gnur, "GNUR", "Get Next Unregistered User");
797 CtdlRegisterProtoHook(cmd_vali, "VALI", "Validate new users");
798 CtdlRegisterProtoHook(cmd_list, "LIST", "List users");
799 CtdlRegisterProtoHook(cmd_chek, "CHEK", "assorted info we need to check at login");
800 CtdlRegisterProtoHook(cmd_qusr, "QUSR", "check to see if a user exists");
801 CtdlRegisterProtoHook(cmd_agup, "AGUP", "Administratively Get User Parameters");
802 CtdlRegisterProtoHook(cmd_asup, "ASUP", "Administratively Set User Parameters");
803 CtdlRegisterProtoHook(cmd_seen, "SEEN", "Manipulate seen/unread message flags");
804 CtdlRegisterProtoHook(cmd_gtsn, "GTSN", "Fetch seen/unread message flags");
805 CtdlRegisterProtoHook(cmd_view, "VIEW", "Set preferred view for user/room combination");
806 CtdlRegisterProtoHook(cmd_renu, "RENU", "Rename a user");
807 CtdlRegisterProtoHook(cmd_newu, "NEWU", "Log in as a new user");
809 /* return our Subversion id for the Log */