2 * Server functions which perform operations on user objects.
4 * Copyright (c) 1987-2015 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.
17 #include "ctdl_module.h"
18 #include "citserver.h"
21 #include "internet_addressing.h"
27 void cmd_user(char *cmdbuf)
32 CON_syslog(LOG_DEBUG, "cmd_user(%s)\n", cmdbuf);
33 extract_token(username, cmdbuf, 0, '|', sizeof username);
34 CON_syslog(LOG_DEBUG, "username: %s\n", username);
36 CON_syslog(LOG_DEBUG, "username: %s\n", username);
38 a = CtdlLoginExistingUser(NULL, username);
40 case login_already_logged_in:
41 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
43 case login_too_many_users:
45 "Too many users are already online "
47 ERROR + MAX_SESSIONS_EXCEEDED,
48 CtdlGetConfigStr("c_nodename"), CtdlGetConfigInt("c_maxsessions"));
51 cprintf("%d Password required for %s\n",
52 MORE_DATA, CC->curr_user);
55 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
58 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
63 void cmd_pass(char *buf)
69 memset(password, 0, sizeof(password));
70 len = extract_token(password, buf, 0, '|', sizeof password);
71 a = CtdlTryPassword(password, len);
74 case pass_already_logged_in:
75 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
78 cprintf("%d You must send a name with USER first.\n",
79 ERROR + USERNAME_REQUIRED);
81 case pass_wrong_password:
82 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
92 * cmd_newu() - create a new user account and log in as that user
94 void cmd_newu(char *cmdbuf)
100 if (CtdlGetConfigInt("c_auth_mode") != AUTHMODE_NATIVE) {
101 cprintf("%d This system does not use native mode authentication.\n",
106 if (CtdlGetConfigInt("c_disable_newu")) {
107 cprintf("%d Self-service user account creation "
108 "is disabled on this system.\n", ERROR + NOT_HERE);
113 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
117 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
118 ERROR + MAX_SESSIONS_EXCEEDED,
119 CtdlGetConfigStr("c_nodename"), CtdlGetConfigInt("c_maxsessions"));
122 extract_token(username, cmdbuf, 0, '|', sizeof username);
124 len = cutuserkey(username);
126 if (IsEmptyStr(username)) {
127 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
131 if ((!strcasecmp(username, "bbs")) ||
132 (!strcasecmp(username, "new")) ||
133 (!strcasecmp(username, "."))) {
134 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
138 a = create_user(username, len, 1);
141 logged_in_response();
142 } else if (a == ERROR + ALREADY_EXISTS) {
143 cprintf("%d '%s' already exists.\n",
144 ERROR + ALREADY_EXISTS, username);
146 } else if (a == ERROR + INTERNAL_ERROR) {
147 cprintf("%d Internal error - user record disappeared?\n",
148 ERROR + INTERNAL_ERROR);
151 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,
227 (!IsEmptyStr(password)) ? "and password set" :
230 } else if (a == ERROR + ALREADY_EXISTS) {
231 cprintf("%d '%s' already exists.\n", ERROR + ALREADY_EXISTS, username);
233 } else if ( (CtdlGetConfigInt("c_auth_mode") != AUTHMODE_NATIVE) && (a == ERROR + NO_SUCH_USER) ) {
234 cprintf("%d User accounts are not created within Citadel in host authentication mode.\n",
235 ERROR + NO_SUCH_USER);
238 cprintf("%d An error occurred creating the user account.\n", ERROR + INTERNAL_ERROR);
245 * get user parameters
247 void cmd_getu(char *cmdbuf)
250 if (CtdlAccessCheck(ac_logged_in))
253 CtdlGetUser(&CC->user, CC->curr_user);
254 cprintf("%d 80|24|%d|\n",
256 (CC->user.flags & US_USER_SET)
261 * set user parameters
263 void cmd_setu(char *new_parms)
265 if (CtdlAccessCheck(ac_logged_in))
268 if (num_parms(new_parms) < 3) {
269 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
272 CtdlLockGetCurrentUser();
273 CC->user.flags = CC->user.flags & (~US_USER_SET);
274 CC->user.flags = CC->user.flags | (extract_int(new_parms, 2) & US_USER_SET);
275 CtdlPutCurrentUserLock();
276 cprintf("%d Ok\n", CIT_OK);
280 * set last read pointer
282 void cmd_slrp(char *new_ptr)
288 if (CtdlAccessCheck(ac_logged_in)) {
292 if (!strncasecmp(new_ptr, "highest", 7)) {
293 newlr = CC->room.QRhighest;
295 newlr = atol(new_ptr);
298 CtdlLockGetCurrentUser();
300 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
301 memcpy(&original_vbuf, &vbuf, sizeof(visit));
302 vbuf.v_lastseen = newlr;
303 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
305 /* Only rewrite the record if it changed */
306 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
307 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
308 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
311 CtdlPutCurrentUserLock();
312 cprintf("%d %ld\n", CIT_OK, newlr);
316 void cmd_seen(char *argbuf) {
317 long target_msgnum = 0L;
318 int target_setting = 0;
320 if (CtdlAccessCheck(ac_logged_in)) {
324 if (num_parms(argbuf) != 2) {
325 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
329 target_msgnum = extract_long(argbuf, 0);
330 target_setting = extract_int(argbuf, 1);
332 CtdlSetSeen(&target_msgnum, 1, target_setting,
333 ctdlsetseen_seen, NULL, NULL);
334 cprintf("%d OK\n", CIT_OK);
338 void cmd_gtsn(char *argbuf) {
341 if (CtdlAccessCheck(ac_logged_in)) {
345 /* Learn about the user and room in question */
346 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
348 cprintf("%d ", CIT_OK);
349 client_write(vbuf.v_seen, strlen(vbuf.v_seen));
350 client_write(HKEY("\n"));
354 * INVT and KICK commands
356 void cmd_invt_kick(char *iuser, int op) {
359 * These commands are only allowed by admins, room admins,
360 * and room namespace owners
362 if (is_room_aide()) {
364 } else if ( ((atol(CC->room.QRname) == CC->user.usernum) ) && (CC->user.usernum != 0) ) {
368 cprintf("%d Higher access or room ownership required.\n",
369 ERROR + HIGHER_ACCESS_REQUIRED);
373 if (!strncasecmp(CC->room.QRname, CtdlGetConfigStr("c_baseroom"),
375 cprintf("%d Can't add/remove users from this room.\n",
380 if (CtdlInvtKick(iuser, op) != 0) {
381 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
385 cprintf("%d %s %s %s.\n",
387 ((op == 1) ? "invited to" : "kicked out of"),
392 void cmd_invt(char *iuser) {cmd_invt_kick(iuser, 1);}
393 void cmd_kick(char *iuser) {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);
415 * Get Next Unregistered User
417 void cmd_gnur(char *argbuf)
419 struct cdbdata *cdbus;
420 struct ctdluser usbuf;
422 if (CtdlAccessCheck(ac_aide)) {
426 if ((CitControl.MMflags & MM_VALID) == 0) {
427 cprintf("%d There are no unvalidated users.\n", CIT_OK);
431 /* There are unvalidated users. Traverse the user database,
432 * and return the first user we find that needs validation.
434 cdb_rewind(CDB_USERS);
435 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
436 memset(&usbuf, 0, sizeof(struct ctdluser));
437 memcpy(&usbuf, cdbus->ptr,
438 ((cdbus->len > sizeof(struct ctdluser)) ?
439 sizeof(struct ctdluser) : cdbus->len));
441 if ((usbuf.flags & US_NEEDVALID)
442 && (usbuf.axlevel > AxDeleted)) {
443 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
444 cdb_close_cursor(CDB_USERS);
449 /* If we get to this point, there are no more unvalidated users.
450 * Therefore we clear the "users need validation" flag.
453 begin_critical_section(S_CONTROL);
455 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
457 end_critical_section(S_CONTROL);
458 cprintf("%d *** End of registration.\n", CIT_OK);
467 void cmd_vali(char *v_args)
471 struct ctdluser userbuf;
473 extract_token(user, v_args, 0, '|', sizeof user);
474 newax = extract_int(v_args, 1);
476 if (CtdlAccessCheck(ac_aide) ||
478 (newax < AxDeleted)) {
482 if (CtdlGetUserLock(&userbuf, user) != 0) {
483 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
487 userbuf.axlevel = newax;
488 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
490 CtdlPutUserLock(&userbuf);
492 /* If the access level was set to zero, delete the user */
494 if (purge_user(user) == 0) {
495 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
499 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
503 * List users (searchstring may be empty to list all users)
505 void cmd_list(char *cmdbuf)
507 char searchstring[256];
508 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
509 striplt(searchstring);
510 cprintf("%d \n", LISTING_FOLLOWS);
511 ForEachUser(ListThisUser, (void *)searchstring );
519 * assorted info we need to check at login
521 void cmd_chek(char *argbuf)
527 if (CtdlAccessCheck(ac_logged_in)) {
531 CtdlGetUser(&CC->user, CC->curr_user); /* no lock is needed here */
532 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
535 if (CC->user.axlevel >= AxAideU) {
537 if (CitControl.MMflags & MM_VALID)
542 mail = InitialMailCheck();
544 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
549 * check to see if a user exists
551 void cmd_qusr(char *who)
553 struct ctdluser usbuf;
555 if (CtdlGetUser(&usbuf, who) == 0) {
556 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
558 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
564 * Administrative Get User Parameters
566 void cmd_agup(char *cmdbuf)
568 struct ctdluser usbuf;
569 char requested_user[128];
571 if (CtdlAccessCheck(ac_aide)) {
575 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
576 if (CtdlGetUser(&usbuf, requested_user) != 0) {
577 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
580 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
589 (long)usbuf.lastcall,
596 * Administrative Set User Parameters
598 void cmd_asup(char *cmdbuf)
600 struct ctdluser usbuf;
601 char requested_user[128];
607 if (CtdlAccessCheck(ac_aide))
610 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
611 if (CtdlGetUserLock(&usbuf, requested_user) != 0) {
612 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
615 np = num_parms(cmdbuf);
617 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
619 usbuf.flags = extract_int(cmdbuf, 2);
621 usbuf.timescalled = extract_int(cmdbuf, 3);
623 usbuf.posted = extract_int(cmdbuf, 4);
625 newax = extract_int(cmdbuf, 5);
626 if ((newax >= AxDeleted) && (newax <= AxAideU)) {
627 usbuf.axlevel = newax;
631 usbuf.lastcall = extract_long(cmdbuf, 7);
634 usbuf.USuserpurge = extract_int(cmdbuf, 8);
636 CtdlPutUserLock(&usbuf);
637 if (usbuf.axlevel == AxDeleted) {
638 if (purge_user(requested_user) == 0) {
644 snprintf(notify, SIZ,
645 "User \"%s\" has been deleted by %s.\n",
647 (CC->logged_in ? CC->user.fullname : "an administrator")
649 CtdlAideMessage(notify, "User Deletion Message");
652 cprintf("%d Ok", CIT_OK);
654 cprintf(" (%s deleted)", requested_user);
660 * Citadel protocol command to do the same
662 void cmd_isme(char *argbuf) {
665 if (CtdlAccessCheck(ac_logged_in)) return;
666 extract_token(addr, argbuf, 0, '|', sizeof addr);
668 if (CtdlIsMe(addr, sizeof addr)) {
669 cprintf("%d %s\n", CIT_OK, addr);
672 cprintf("%d Not you.\n", ERROR + ILLEGAL_VALUE);
679 * Set the preferred view for the current user/room combination
681 void cmd_view(char *cmdbuf) {
685 if (CtdlAccessCheck(ac_logged_in)) {
689 requested_view = extract_int(cmdbuf, 0);
691 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
692 vbuf.v_view = requested_view;
693 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
695 cprintf("%d ok\n", CIT_OK);
702 void cmd_renu(char *cmdbuf)
705 char oldname[USERNAME_SIZE];
706 char newname[USERNAME_SIZE];
708 if (CtdlAccessCheck(ac_aide)) {
712 extract_token(oldname, cmdbuf, 0, '|', sizeof oldname);
713 extract_token(newname, cmdbuf, 1, '|', sizeof newname);
715 retcode = rename_user(oldname, newname);
718 cprintf("%d '%s' has been renamed to '%s'.\n", CIT_OK, oldname, newname);
720 case RENAMEUSER_LOGGED_IN:
721 cprintf("%d '%s' is currently logged in and cannot be renamed.\n",
722 ERROR + ALREADY_LOGGED_IN , oldname);
724 case RENAMEUSER_NOT_FOUND:
725 cprintf("%d '%s' does not exist.\n", ERROR + NO_SUCH_USER, oldname);
727 case RENAMEUSER_ALREADY_EXISTS:
728 cprintf("%d A user named '%s' already exists.\n", ERROR + ALREADY_EXISTS, newname);
732 cprintf("%d An unknown error occurred.\n", ERROR);
737 void cmd_quit(char *argbuf)
739 cprintf("%d Goodbye.\n", CIT_OK);
740 CC->kill_me = KILLME_CLIENT_LOGGED_OUT;
744 void cmd_lout(char *argbuf)
748 cprintf("%d logged out.\n", CIT_OK);
752 /*****************************************************************************/
753 /* MODULE INITIALIZATION STUFF */
754 /*****************************************************************************/
757 CTDL_MODULE_INIT(serv_user)
760 CtdlRegisterProtoHook(cmd_user, "USER", "Submit username for login");
761 CtdlRegisterProtoHook(cmd_pass, "PASS", "Complete login by submitting a password");
762 CtdlRegisterProtoHook(cmd_quit, "QUIT", "log out and disconnect from server");
763 CtdlRegisterProtoHook(cmd_lout, "LOUT", "log out but do not disconnect from server");
764 CtdlRegisterProtoHook(cmd_creu, "CREU", "Create User");
765 CtdlRegisterProtoHook(cmd_setp, "SETP", "Set the password for an account");
766 CtdlRegisterProtoHook(cmd_getu, "GETU", "Get User parameters");
767 CtdlRegisterProtoHook(cmd_setu, "SETU", "Set User parameters");
768 CtdlRegisterProtoHook(cmd_slrp, "SLRP", "Set Last Read Pointer");
769 CtdlRegisterProtoHook(cmd_invt, "INVT", "Invite a user to a room");
770 CtdlRegisterProtoHook(cmd_kick, "KICK", "Kick a user out of a room");
771 CtdlRegisterProtoHook(cmd_forg, "FORG", "Forget a room");
772 CtdlRegisterProtoHook(cmd_gnur, "GNUR", "Get Next Unregistered User");
773 CtdlRegisterProtoHook(cmd_vali, "VALI", "Validate new users");
774 CtdlRegisterProtoHook(cmd_list, "LIST", "List users");
775 CtdlRegisterProtoHook(cmd_chek, "CHEK", "assorted info we need to check at login");
776 CtdlRegisterProtoHook(cmd_qusr, "QUSR", "check to see if a user exists");
777 CtdlRegisterProtoHook(cmd_agup, "AGUP", "Administratively Get User Parameters");
778 CtdlRegisterProtoHook(cmd_asup, "ASUP", "Administratively Set User Parameters");
779 CtdlRegisterProtoHook(cmd_seen, "SEEN", "Manipulate seen/unread message flags");
780 CtdlRegisterProtoHook(cmd_gtsn, "GTSN", "Fetch seen/unread message flags");
781 CtdlRegisterProtoHook(cmd_view, "VIEW", "Set preferred view for user/room combination");
782 CtdlRegisterProtoHook(cmd_renu, "RENU", "Rename a user");
783 CtdlRegisterProtoHook(cmd_newu, "NEWU", "Log in as a new user");
784 CtdlRegisterProtoHook(cmd_isme, "ISME", "Determine whether an email address belongs to a user");
786 /* return our Subversion id for the Log */