2 * Server functions which perform operations on user objects.
4 * Copyright (c) 1987-2017 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.
18 #include "ctdl_module.h"
19 #include "citserver.h"
22 #include "internet_addressing.h"
28 void cmd_user(char *cmdbuf)
33 extract_token(username, cmdbuf, 0, '|', sizeof username);
35 syslog(LOG_DEBUG, "user_ops: cmd_user(%s)", username);
37 a = CtdlLoginExistingUser(NULL, username);
39 case login_already_logged_in:
40 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
42 case login_too_many_users:
44 "Too many users are already online "
46 ERROR + MAX_SESSIONS_EXCEEDED,
47 CtdlGetConfigStr("c_nodename"), CtdlGetConfigInt("c_maxsessions"));
50 cprintf("%d Password required for %s\n",
51 MORE_DATA, CC->curr_user);
54 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
57 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
62 void cmd_pass(char *buf)
68 memset(password, 0, sizeof(password));
69 len = extract_token(password, buf, 0, '|', sizeof password);
70 a = CtdlTryPassword(password, len);
73 case pass_already_logged_in:
74 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
77 cprintf("%d You must send a name with USER first.\n",
78 ERROR + USERNAME_REQUIRED);
80 case pass_wrong_password:
81 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
91 * cmd_newu() - create a new user account and log in as that user
93 void cmd_newu(char *cmdbuf)
99 if (CtdlGetConfigInt("c_auth_mode") != AUTHMODE_NATIVE) {
100 cprintf("%d This system does not use native mode authentication.\n",
105 if (CtdlGetConfigInt("c_disable_newu")) {
106 cprintf("%d Self-service user account creation "
107 "is disabled on this system.\n", ERROR + NOT_HERE);
112 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
116 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
117 ERROR + MAX_SESSIONS_EXCEEDED,
118 CtdlGetConfigStr("c_nodename"), CtdlGetConfigInt("c_maxsessions"));
121 extract_token(username, cmdbuf, 0, '|', sizeof username);
123 len = cutuserkey(username);
125 if (IsEmptyStr(username)) {
126 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
130 if ((!strcasecmp(username, "bbs")) ||
131 (!strcasecmp(username, "new")) ||
132 (!strcasecmp(username, "."))) {
133 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
137 a = create_user(username, len, 1);
140 logged_in_response();
141 } else if (a == ERROR + ALREADY_EXISTS) {
142 cprintf("%d '%s' already exists.\n",
143 ERROR + ALREADY_EXISTS, username);
145 } else if (a == ERROR + INTERNAL_ERROR) {
146 cprintf("%d Internal error - user record disappeared?\n",
147 ERROR + INTERNAL_ERROR);
150 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
156 * set password - citadel protocol implementation
158 void cmd_setp(char *new_pw)
160 if (CtdlAccessCheck(ac_logged_in)) {
163 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
164 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
168 cprintf("%d The master prefix password cannot be changed with this command.\n",
173 if (!strcasecmp(new_pw, "GENERATE_RANDOM_PASSWORD")) {
174 char random_password[17];
175 snprintf(random_password, sizeof random_password, "%08lx%08lx", random(), random());
176 CtdlSetPassword(random_password);
177 cprintf("%d %s\n", CIT_OK, random_password);
181 if (IsEmptyStr(new_pw)) {
182 cprintf("%d Password unchanged.\n", CIT_OK);
185 CtdlSetPassword(new_pw);
186 cprintf("%d Password changed.\n", CIT_OK);
192 * cmd_creu() - administratively create a new user account (do not log in to it)
194 void cmd_creu(char *cmdbuf)
202 if (CtdlAccessCheck(ac_aide)) {
206 extract_token(username, cmdbuf, 0, '|', sizeof username);
209 if (IsEmptyStr(username)) {
210 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
213 len = cutuserkey(username);
216 extract_token(password, cmdbuf, 1, '|', sizeof password);
218 a = create_user(username, len, 0);
221 if (!IsEmptyStr(password)) {
222 CtdlGetUserLock(&tmp, username);
223 safestrncpy(tmp.password, password, sizeof(tmp.password));
224 CtdlPutUserLock(&tmp);
226 cprintf("%d User '%s' created %s.\n", CIT_OK, username, (!IsEmptyStr(password)) ? "and password set" : "with no password");
228 } else if (a == ERROR + ALREADY_EXISTS) {
229 cprintf("%d '%s' already exists.\n", ERROR + ALREADY_EXISTS, username);
231 } else if ( (CtdlGetConfigInt("c_auth_mode") != AUTHMODE_NATIVE) && (a == ERROR + NO_SUCH_USER) ) {
232 cprintf("%d User accounts are not created within Citadel in host authentication mode.\n", ERROR + NO_SUCH_USER);
235 cprintf("%d An error occurred creating the user account.\n", ERROR + INTERNAL_ERROR);
241 * get user parameters
243 void cmd_getu(char *cmdbuf)
245 if (CtdlAccessCheck(ac_logged_in))
248 CtdlGetUser(&CC->user, CC->curr_user);
249 cprintf("%d 80|24|%d|\n", CIT_OK, (CC->user.flags & US_USER_SET));
254 * set user parameters
256 void cmd_setu(char *new_parms)
258 if (CtdlAccessCheck(ac_logged_in))
261 if (num_parms(new_parms) < 3) {
262 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
265 CtdlLockGetCurrentUser();
266 CC->user.flags = CC->user.flags & (~US_USER_SET);
267 CC->user.flags = CC->user.flags | (extract_int(new_parms, 2) & US_USER_SET);
268 CtdlPutCurrentUserLock();
269 cprintf("%d Ok\n", CIT_OK);
274 * set last read pointer
276 void cmd_slrp(char *new_ptr)
282 if (CtdlAccessCheck(ac_logged_in)) {
286 if (!strncasecmp(new_ptr, "highest", 7)) {
287 newlr = CC->room.QRhighest;
289 newlr = atol(new_ptr);
292 CtdlLockGetCurrentUser();
294 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
295 memcpy(&original_vbuf, &vbuf, sizeof(visit));
296 vbuf.v_lastseen = newlr;
297 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
299 /* Only rewrite the record if it changed */
300 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
301 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
302 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
305 CtdlPutCurrentUserLock();
306 cprintf("%d %ld\n", CIT_OK, newlr);
310 void cmd_seen(char *argbuf) {
311 long target_msgnum = 0L;
312 int target_setting = 0;
314 if (CtdlAccessCheck(ac_logged_in)) {
318 if (num_parms(argbuf) != 2) {
319 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
323 target_msgnum = extract_long(argbuf, 0);
324 target_setting = extract_int(argbuf, 1);
326 CtdlSetSeen(&target_msgnum, 1, target_setting,
327 ctdlsetseen_seen, NULL, NULL);
328 cprintf("%d OK\n", CIT_OK);
332 void cmd_gtsn(char *argbuf) {
335 if (CtdlAccessCheck(ac_logged_in)) {
339 /* Learn about the user and room in question */
340 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
342 cprintf("%d ", CIT_OK);
343 client_write(vbuf.v_seen, strlen(vbuf.v_seen));
344 client_write(HKEY("\n"));
349 * INVT and KICK commands
351 void cmd_invt_kick(char *iuser, int op) {
354 * These commands are only allowed by admins, room admins,
355 * and room namespace owners
357 if (is_room_aide()) {
359 } else if ( ((atol(CC->room.QRname) == CC->user.usernum) ) && (CC->user.usernum != 0) ) {
363 cprintf("%d Higher access or room ownership required.\n", ERROR + HIGHER_ACCESS_REQUIRED);
367 if (!strncasecmp(CC->room.QRname, CtdlGetConfigStr("c_baseroom"),
369 cprintf("%d Can't add/remove users from this room.\n", ERROR + NOT_HERE);
373 if (CtdlInvtKick(iuser, op) != 0) {
374 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
378 cprintf("%d %s %s %s.\n",
380 ((op == 1) ? "invited to" : "kicked out of"),
386 void cmd_invt(char *iuser) {
387 cmd_invt_kick(iuser, 1);
391 void cmd_kick(char *iuser) {
392 cmd_invt_kick(iuser, 0);
397 * forget (Zap) the current room
399 void cmd_forg(char *argbuf)
402 if (CtdlAccessCheck(ac_logged_in)) {
406 if (CtdlForgetThisRoom() == 0) {
407 cprintf("%d Ok\n", CIT_OK);
410 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
416 * Get Next Unregistered User
418 void cmd_gnur(char *argbuf)
420 struct cdbdata *cdbus;
421 struct ctdluser usbuf;
423 if (CtdlAccessCheck(ac_aide)) {
427 if ((CtdlGetConfigInt("MMflags") & MM_VALID) == 0) {
428 cprintf("%d There are no unvalidated users.\n", CIT_OK);
432 /* There are unvalidated users. Traverse the user database,
433 * and return the first user we find that needs validation.
435 cdb_rewind(CDB_USERS);
436 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
437 memset(&usbuf, 0, sizeof(struct ctdluser));
438 memcpy(&usbuf, cdbus->ptr, ((cdbus->len > sizeof(struct ctdluser)) ? sizeof(struct ctdluser) : cdbus->len));
440 if ((usbuf.flags & US_NEEDVALID) && (usbuf.axlevel > AxDeleted)) {
441 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
442 cdb_close_cursor(CDB_USERS);
447 /* If we get to this point, there are no more unvalidated users.
448 * Therefore we clear the "users need validation" flag.
451 begin_critical_section(S_CONTROL);
453 flags = CtdlGetConfigInt("MMflags");
454 flags = flags & (~MM_VALID);
455 CtdlSetConfigInt("MMflags", flags);
456 end_critical_section(S_CONTROL);
457 cprintf("%d *** End of registration.\n", CIT_OK);
464 void cmd_vali(char *v_args)
468 struct ctdluser userbuf;
470 extract_token(user, v_args, 0, '|', sizeof user);
471 newax = extract_int(v_args, 1);
473 if (CtdlAccessCheck(ac_aide) || (newax > AxAideU) || (newax < AxDeleted)) {
477 if (CtdlGetUserLock(&userbuf, user) != 0) {
478 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
482 userbuf.axlevel = newax;
483 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
485 CtdlPutUserLock(&userbuf);
487 /* If the access level was set to zero, delete the user */
489 if (purge_user(user) == 0) {
490 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
494 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
498 * List users (searchstring may be empty to list all users)
500 void cmd_list(char *cmdbuf)
502 char searchstring[256];
503 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
504 striplt(searchstring);
505 cprintf("%d \n", LISTING_FOLLOWS);
506 ForEachUser(ListThisUser, (void *)searchstring );
512 * assorted info we need to check at login
514 void cmd_chek(char *argbuf)
520 if (CtdlAccessCheck(ac_logged_in)) {
524 CtdlGetUser(&CC->user, CC->curr_user); // no lock is needed here
525 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0)) {
529 if (CC->user.axlevel >= AxAideU) {
530 if (CtdlGetConfigInt("MMflags") & MM_VALID) {
535 mail = InitialMailCheck(); // check for mail
537 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
542 * check to see if a user exists
544 void cmd_qusr(char *who)
546 struct ctdluser usbuf;
548 if (CtdlGetUser(&usbuf, who) == 0) {
549 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
551 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
557 * Administrative Get User Parameters
559 void cmd_agup(char *cmdbuf)
561 struct ctdluser usbuf;
562 char requested_user[128];
564 if (CtdlAccessCheck(ac_aide)) {
568 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
569 if (CtdlGetUser(&usbuf, requested_user) != 0) {
570 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
573 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
582 (long)usbuf.lastcall,
588 * Administrative Set User Parameters
590 void cmd_asup(char *cmdbuf)
592 struct ctdluser usbuf;
593 char requested_user[128];
599 if (CtdlAccessCheck(ac_aide))
602 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
603 if (CtdlGetUserLock(&usbuf, requested_user) != 0) {
604 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
607 np = num_parms(cmdbuf);
609 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
611 usbuf.flags = extract_int(cmdbuf, 2);
613 usbuf.timescalled = extract_int(cmdbuf, 3);
615 usbuf.posted = extract_int(cmdbuf, 4);
617 newax = extract_int(cmdbuf, 5);
618 if ((newax >= AxDeleted) && (newax <= AxAideU)) {
619 usbuf.axlevel = newax;
623 usbuf.lastcall = extract_long(cmdbuf, 7);
626 usbuf.USuserpurge = extract_int(cmdbuf, 8);
628 CtdlPutUserLock(&usbuf);
629 if (usbuf.axlevel == AxDeleted) {
630 if (purge_user(requested_user) == 0) {
636 snprintf(notify, SIZ,
637 "User \"%s\" has been deleted by %s.\n",
638 usbuf.fullname, (CC->logged_in ? CC->user.fullname : "an administrator")
640 CtdlAideMessage(notify, "User Deletion Message");
643 cprintf("%d Ok", CIT_OK);
645 cprintf(" (%s deleted)", requested_user);
652 * Citadel protocol command to do the same
654 void cmd_isme(char *argbuf) {
657 if (CtdlAccessCheck(ac_logged_in)) return;
658 extract_token(addr, argbuf, 0, '|', sizeof addr);
660 if (CtdlIsMe(addr, sizeof addr)) {
661 cprintf("%d %s\n", CIT_OK, addr);
664 cprintf("%d Not you.\n", ERROR + ILLEGAL_VALUE);
671 * Set the preferred view for the current user/room combination
673 void cmd_view(char *cmdbuf) {
677 if (CtdlAccessCheck(ac_logged_in)) {
681 requested_view = extract_int(cmdbuf, 0);
683 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
684 vbuf.v_view = requested_view;
685 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
687 cprintf("%d ok\n", CIT_OK);
694 void cmd_renu(char *cmdbuf)
697 char oldname[USERNAME_SIZE];
698 char newname[USERNAME_SIZE];
700 if (CtdlAccessCheck(ac_aide)) {
704 extract_token(oldname, cmdbuf, 0, '|', sizeof oldname);
705 extract_token(newname, cmdbuf, 1, '|', sizeof newname);
707 retcode = rename_user(oldname, newname);
710 cprintf("%d '%s' has been renamed to '%s'.\n", CIT_OK, oldname, newname);
712 case RENAMEUSER_LOGGED_IN:
713 cprintf("%d '%s' is currently logged in and cannot be renamed.\n",
714 ERROR + ALREADY_LOGGED_IN , oldname
717 case RENAMEUSER_NOT_FOUND:
718 cprintf("%d '%s' does not exist.\n", ERROR + NO_SUCH_USER, oldname);
720 case RENAMEUSER_ALREADY_EXISTS:
721 cprintf("%d A user named '%s' already exists.\n", ERROR + ALREADY_EXISTS, newname);
725 cprintf("%d An unknown error occurred.\n", ERROR);
729 void cmd_quit(char *argbuf)
731 cprintf("%d Goodbye.\n", CIT_OK);
732 CC->kill_me = KILLME_CLIENT_LOGGED_OUT;
736 void cmd_lout(char *argbuf)
740 cprintf("%d logged out.\n", CIT_OK);
744 /*****************************************************************************/
745 /* MODULE INITIALIZATION STUFF */
746 /*****************************************************************************/
749 CTDL_MODULE_INIT(serv_user)
752 CtdlRegisterProtoHook(cmd_user, "USER", "Submit username for login");
753 CtdlRegisterProtoHook(cmd_pass, "PASS", "Complete login by submitting a password");
754 CtdlRegisterProtoHook(cmd_quit, "QUIT", "log out and disconnect from server");
755 CtdlRegisterProtoHook(cmd_lout, "LOUT", "log out but do not disconnect from server");
756 CtdlRegisterProtoHook(cmd_creu, "CREU", "Create User");
757 CtdlRegisterProtoHook(cmd_setp, "SETP", "Set the password for an account");
758 CtdlRegisterProtoHook(cmd_getu, "GETU", "Get User parameters");
759 CtdlRegisterProtoHook(cmd_setu, "SETU", "Set User parameters");
760 CtdlRegisterProtoHook(cmd_slrp, "SLRP", "Set Last Read Pointer");
761 CtdlRegisterProtoHook(cmd_invt, "INVT", "Invite a user to a room");
762 CtdlRegisterProtoHook(cmd_kick, "KICK", "Kick a user out of a room");
763 CtdlRegisterProtoHook(cmd_forg, "FORG", "Forget a room");
764 CtdlRegisterProtoHook(cmd_gnur, "GNUR", "Get Next Unregistered User");
765 CtdlRegisterProtoHook(cmd_vali, "VALI", "Validate new users");
766 CtdlRegisterProtoHook(cmd_list, "LIST", "List users");
767 CtdlRegisterProtoHook(cmd_chek, "CHEK", "assorted info we need to check at login");
768 CtdlRegisterProtoHook(cmd_qusr, "QUSR", "check to see if a user exists");
769 CtdlRegisterProtoHook(cmd_agup, "AGUP", "Administratively Get User Parameters");
770 CtdlRegisterProtoHook(cmd_asup, "ASUP", "Administratively Set User Parameters");
771 CtdlRegisterProtoHook(cmd_seen, "SEEN", "Manipulate seen/unread message flags");
772 CtdlRegisterProtoHook(cmd_gtsn, "GTSN", "Fetch seen/unread message flags");
773 CtdlRegisterProtoHook(cmd_view, "VIEW", "Set preferred view for user/room combination");
774 CtdlRegisterProtoHook(cmd_renu, "RENU", "Rename a user");
775 CtdlRegisterProtoHook(cmd_newu, "NEWU", "Log in as a new user");
776 CtdlRegisterProtoHook(cmd_isme, "ISME", "Determine whether an email address belongs to a user");
778 /* return our Subversion id for the Log */