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.
17 #include "ctdl_module.h"
19 #include "citserver.h"
22 #include "internet_addressing.h"
29 void cmd_user(char *cmdbuf)
34 CON_syslog(LOG_DEBUG, "cmd_user(%s)\n", cmdbuf);
35 extract_token(username, cmdbuf, 0, '|', sizeof username);
36 CON_syslog(LOG_DEBUG, "username: %s\n", username);
38 CON_syslog(LOG_DEBUG, "username: %s\n", username);
40 a = CtdlLoginExistingUser(NULL, username);
42 case login_already_logged_in:
43 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
45 case login_too_many_users:
47 "Too many users are already online "
49 ERROR + MAX_SESSIONS_EXCEEDED,
50 config.c_nodename, config.c_maxsessions);
53 cprintf("%d Password required for %s\n",
54 MORE_DATA, CC->curr_user);
57 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER, username);
60 cprintf("%d Internal error\n", ERROR + INTERNAL_ERROR);
65 void cmd_pass(char *buf)
71 memset(password, 0, sizeof(password));
72 len = extract_token(password, buf, 0, '|', sizeof password);
73 a = CtdlTryPassword(password, len);
76 case pass_already_logged_in:
77 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
80 cprintf("%d You must send a name with USER first.\n",
81 ERROR + USERNAME_REQUIRED);
83 case pass_wrong_password:
84 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
94 * cmd_newu() - create a new user account and log in as that user
96 void cmd_newu(char *cmdbuf)
102 if (config.c_auth_mode != AUTHMODE_NATIVE) {
103 cprintf("%d This system does not use native mode authentication.\n",
108 if (config.c_disable_newu) {
109 cprintf("%d Self-service user account creation "
110 "is disabled on this system.\n", ERROR + NOT_HERE);
115 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
119 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
120 ERROR + MAX_SESSIONS_EXCEEDED,
121 config.c_nodename, config.c_maxsessions);
124 extract_token(username, cmdbuf, 0, '|', sizeof username);
126 len = cutuserkey(username);
128 if (IsEmptyStr(username)) {
129 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
133 if ((!strcasecmp(username, "bbs")) ||
134 (!strcasecmp(username, "new")) ||
135 (!strcasecmp(username, "."))) {
136 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
140 a = create_user(username, len, 1);
143 logged_in_response();
144 } else if (a == ERROR + ALREADY_EXISTS) {
145 cprintf("%d '%s' already exists.\n",
146 ERROR + ALREADY_EXISTS, username);
148 } else if (a == ERROR + INTERNAL_ERROR) {
149 cprintf("%d Internal error - user record disappeared?\n",
150 ERROR + INTERNAL_ERROR);
153 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
158 * set password - citadel protocol implementation
160 void cmd_setp(char *new_pw)
162 if (CtdlAccessCheck(ac_logged_in)) {
165 if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
166 cprintf("%d Not allowed. Use the 'passwd' command.\n", ERROR + NOT_HERE);
170 cprintf("%d The master prefix password cannot be changed with this command.\n",
175 if (!strcasecmp(new_pw, "GENERATE_RANDOM_PASSWORD")) {
176 char random_password[17];
177 snprintf(random_password, sizeof random_password, "%08lx%08lx", random(), random());
178 CtdlSetPassword(random_password);
179 cprintf("%d %s\n", CIT_OK, random_password);
183 if (IsEmptyStr(new_pw)) {
184 cprintf("%d Password unchanged.\n", CIT_OK);
187 CtdlSetPassword(new_pw);
188 cprintf("%d Password changed.\n", CIT_OK);
194 * cmd_creu() - administratively create a new user account (do not log in to it)
196 void cmd_creu(char *cmdbuf)
204 if (CtdlAccessCheck(ac_aide)) {
208 extract_token(username, cmdbuf, 0, '|', sizeof username);
211 if (IsEmptyStr(username)) {
212 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
215 len = cutuserkey(username);
218 extract_token(password, cmdbuf, 1, '|', sizeof password);
220 a = create_user(username, len, 0);
223 if (!IsEmptyStr(password)) {
224 CtdlGetUserLock(&tmp, username);
225 safestrncpy(tmp.password, password, sizeof(tmp.password));
226 CtdlPutUserLock(&tmp);
228 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
229 (!IsEmptyStr(password)) ? "and password set" :
232 } else if (a == ERROR + ALREADY_EXISTS) {
233 cprintf("%d '%s' already exists.\n", ERROR + ALREADY_EXISTS, username);
235 } else if ( (config.c_auth_mode != AUTHMODE_NATIVE) && (a == ERROR + NO_SUCH_USER) ) {
236 cprintf("%d User accounts are not created within Citadel in host authentication mode.\n",
237 ERROR + NO_SUCH_USER);
240 cprintf("%d An error occurred creating the user account.\n", ERROR + INTERNAL_ERROR);
247 * get user parameters
249 void cmd_getu(char *cmdbuf)
252 if (CtdlAccessCheck(ac_logged_in))
255 CtdlGetUser(&CC->user, CC->curr_user);
256 cprintf("%d 80|24|%d|\n",
258 (CC->user.flags & US_USER_SET)
263 * set user parameters
265 void cmd_setu(char *new_parms)
267 if (CtdlAccessCheck(ac_logged_in))
270 if (num_parms(new_parms) < 3) {
271 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
274 CtdlLockGetCurrentUser();
275 CC->user.flags = CC->user.flags & (~US_USER_SET);
276 CC->user.flags = CC->user.flags | (extract_int(new_parms, 2) & US_USER_SET);
277 CtdlPutCurrentUserLock();
278 cprintf("%d Ok\n", CIT_OK);
282 * set last read pointer
284 void cmd_slrp(char *new_ptr)
290 if (CtdlAccessCheck(ac_logged_in)) {
294 if (!strncasecmp(new_ptr, "highest", 7)) {
295 newlr = CC->room.QRhighest;
297 newlr = atol(new_ptr);
300 CtdlLockGetCurrentUser();
302 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
303 memcpy(&original_vbuf, &vbuf, sizeof(visit));
304 vbuf.v_lastseen = newlr;
305 snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
307 /* Only rewrite the record if it changed */
308 if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
309 || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
310 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
313 CtdlPutCurrentUserLock();
314 cprintf("%d %ld\n", CIT_OK, newlr);
318 void cmd_seen(char *argbuf) {
319 long target_msgnum = 0L;
320 int target_setting = 0;
322 if (CtdlAccessCheck(ac_logged_in)) {
326 if (num_parms(argbuf) != 2) {
327 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
331 target_msgnum = extract_long(argbuf, 0);
332 target_setting = extract_int(argbuf, 1);
334 CtdlSetSeen(&target_msgnum, 1, target_setting,
335 ctdlsetseen_seen, NULL, NULL);
336 cprintf("%d OK\n", CIT_OK);
340 void cmd_gtsn(char *argbuf) {
343 if (CtdlAccessCheck(ac_logged_in)) {
347 /* Learn about the user and room in question */
348 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
350 cprintf("%d ", CIT_OK);
351 client_write(vbuf.v_seen, strlen(vbuf.v_seen));
352 client_write(HKEY("\n"));
356 * INVT and KICK commands
358 void cmd_invt_kick(char *iuser, int op) {
361 * These commands are only allowed by admins, room admins,
362 * and room namespace owners
364 if (is_room_aide()) {
366 } else if ( ((atol(CC->room.QRname) == CC->user.usernum) ) && (CC->user.usernum != 0) ) {
370 cprintf("%d Higher access or room ownership required.\n",
371 ERROR + HIGHER_ACCESS_REQUIRED);
375 if (!strncasecmp(CC->room.QRname, config.c_baseroom,
377 cprintf("%d Can't add/remove users from this room.\n",
382 if (CtdlInvtKick(iuser, op) != 0) {
383 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
387 cprintf("%d %s %s %s.\n",
389 ((op == 1) ? "invited to" : "kicked out of"),
394 void cmd_invt(char *iuser) {cmd_invt_kick(iuser, 1);}
395 void cmd_kick(char *iuser) {cmd_invt_kick(iuser, 0);}
399 * forget (Zap) the current room
401 void cmd_forg(char *argbuf)
404 if (CtdlAccessCheck(ac_logged_in)) {
408 if (CtdlForgetThisRoom() == 0) {
409 cprintf("%d Ok\n", CIT_OK);
412 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
417 * Get Next Unregistered User
419 void cmd_gnur(char *argbuf)
421 struct cdbdata *cdbus;
422 struct ctdluser usbuf;
424 if (CtdlAccessCheck(ac_aide)) {
428 if ((CitControl.MMflags & MM_VALID) == 0) {
429 cprintf("%d There are no unvalidated users.\n", CIT_OK);
433 /* There are unvalidated users. Traverse the user database,
434 * and return the first user we find that needs validation.
436 cdb_rewind(CDB_USERS);
437 while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
438 memset(&usbuf, 0, sizeof(struct ctdluser));
439 memcpy(&usbuf, cdbus->ptr,
440 ((cdbus->len > sizeof(struct ctdluser)) ?
441 sizeof(struct ctdluser) : cdbus->len));
443 if ((usbuf.flags & US_NEEDVALID)
444 && (usbuf.axlevel > AxDeleted)) {
445 cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
446 cdb_close_cursor(CDB_USERS);
451 /* If we get to this point, there are no more unvalidated users.
452 * Therefore we clear the "users need validation" flag.
455 begin_critical_section(S_CONTROL);
457 CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
459 end_critical_section(S_CONTROL);
460 cprintf("%d *** End of registration.\n", CIT_OK);
469 void cmd_vali(char *v_args)
473 struct ctdluser userbuf;
475 extract_token(user, v_args, 0, '|', sizeof user);
476 newax = extract_int(v_args, 1);
478 if (CtdlAccessCheck(ac_aide) ||
480 (newax < AxDeleted)) {
484 if (CtdlGetUserLock(&userbuf, user) != 0) {
485 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
489 userbuf.axlevel = newax;
490 userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
492 CtdlPutUserLock(&userbuf);
494 /* If the access level was set to zero, delete the user */
496 if (purge_user(user) == 0) {
497 cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
501 cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
505 * List users (searchstring may be empty to list all users)
507 void cmd_list(char *cmdbuf)
509 char searchstring[256];
510 extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
511 striplt(searchstring);
512 cprintf("%d \n", LISTING_FOLLOWS);
513 ForEachUser(ListThisUser, (void *)searchstring );
521 * assorted info we need to check at login
523 void cmd_chek(char *argbuf)
529 if (CtdlAccessCheck(ac_logged_in)) {
533 CtdlGetUser(&CC->user, CC->curr_user); /* no lock is needed here */
534 if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
537 if (CC->user.axlevel >= AxAideU) {
539 if (CitControl.MMflags & MM_VALID)
544 mail = InitialMailCheck();
546 cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
551 * check to see if a user exists
553 void cmd_qusr(char *who)
555 struct ctdluser usbuf;
557 if (CtdlGetUser(&usbuf, who) == 0) {
558 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
560 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
566 * Administrative Get User Parameters
568 void cmd_agup(char *cmdbuf)
570 struct ctdluser usbuf;
571 char requested_user[128];
573 if (CtdlAccessCheck(ac_aide)) {
577 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
578 if (CtdlGetUser(&usbuf, requested_user) != 0) {
579 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
582 cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
591 (long)usbuf.lastcall,
598 * Administrative Set User Parameters
600 void cmd_asup(char *cmdbuf)
602 struct ctdluser usbuf;
603 char requested_user[128];
609 if (CtdlAccessCheck(ac_aide))
612 extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
613 if (CtdlGetUserLock(&usbuf, requested_user) != 0) {
614 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
617 np = num_parms(cmdbuf);
619 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
621 usbuf.flags = extract_int(cmdbuf, 2);
623 usbuf.timescalled = extract_int(cmdbuf, 3);
625 usbuf.posted = extract_int(cmdbuf, 4);
627 newax = extract_int(cmdbuf, 5);
628 if ((newax >= AxDeleted) && (newax <= AxAideU)) {
629 usbuf.axlevel = newax;
633 usbuf.lastcall = extract_long(cmdbuf, 7);
636 usbuf.USuserpurge = extract_int(cmdbuf, 8);
638 CtdlPutUserLock(&usbuf);
639 if (usbuf.axlevel == AxDeleted) {
640 if (purge_user(requested_user) == 0) {
646 snprintf(notify, SIZ,
647 "User \"%s\" has been deleted by %s.\n",
649 (CC->logged_in ? CC->user.fullname : "an administrator")
651 CtdlAideMessage(notify, "User Deletion Message");
654 cprintf("%d Ok", CIT_OK);
656 cprintf(" (%s deleted)", requested_user);
662 * Citadel protocol command to do the same
664 void cmd_isme(char *argbuf) {
667 if (CtdlAccessCheck(ac_logged_in)) return;
668 extract_token(addr, argbuf, 0, '|', sizeof addr);
670 if (CtdlIsMe(addr, sizeof addr)) {
671 cprintf("%d %s\n", CIT_OK, addr);
674 cprintf("%d Not you.\n", ERROR + ILLEGAL_VALUE);
681 * Set the preferred view for the current user/room combination
683 void cmd_view(char *cmdbuf) {
687 if (CtdlAccessCheck(ac_logged_in)) {
691 requested_view = extract_int(cmdbuf, 0);
693 CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
694 vbuf.v_view = requested_view;
695 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
697 cprintf("%d ok\n", CIT_OK);
704 void cmd_renu(char *cmdbuf)
707 char oldname[USERNAME_SIZE];
708 char newname[USERNAME_SIZE];
710 if (CtdlAccessCheck(ac_aide)) {
714 extract_token(oldname, cmdbuf, 0, '|', sizeof oldname);
715 extract_token(newname, cmdbuf, 1, '|', sizeof newname);
717 retcode = rename_user(oldname, newname);
720 cprintf("%d '%s' has been renamed to '%s'.\n", CIT_OK, oldname, newname);
722 case RENAMEUSER_LOGGED_IN:
723 cprintf("%d '%s' is currently logged in and cannot be renamed.\n",
724 ERROR + ALREADY_LOGGED_IN , oldname);
726 case RENAMEUSER_NOT_FOUND:
727 cprintf("%d '%s' does not exist.\n", ERROR + NO_SUCH_USER, oldname);
729 case RENAMEUSER_ALREADY_EXISTS:
730 cprintf("%d A user named '%s' already exists.\n", ERROR + ALREADY_EXISTS, newname);
734 cprintf("%d An unknown error occurred.\n", ERROR);
739 void cmd_quit(char *argbuf)
741 cprintf("%d Goodbye.\n", CIT_OK);
742 CC->kill_me = KILLME_CLIENT_LOGGED_OUT;
746 void cmd_lout(char *argbuf)
750 cprintf("%d logged out.\n", CIT_OK);
754 /*****************************************************************************/
755 /* MODULE INITIALIZATION STUFF */
756 /*****************************************************************************/
759 CTDL_MODULE_INIT(serv_user)
762 CtdlRegisterProtoHook(cmd_user, "USER", "Submit username for login");
763 CtdlRegisterProtoHook(cmd_pass, "PASS", "Complete login by submitting a password");
764 CtdlRegisterProtoHook(cmd_quit, "QUIT", "log out and disconnect from server");
765 CtdlRegisterProtoHook(cmd_lout, "LOUT", "log out but do not disconnect from server");
766 CtdlRegisterProtoHook(cmd_creu, "CREU", "Create User");
767 CtdlRegisterProtoHook(cmd_setp, "SETP", "Set the password for an account");
768 CtdlRegisterProtoHook(cmd_getu, "GETU", "Get User parameters");
769 CtdlRegisterProtoHook(cmd_setu, "SETU", "Set User parameters");
770 CtdlRegisterProtoHook(cmd_slrp, "SLRP", "Set Last Read Pointer");
771 CtdlRegisterProtoHook(cmd_invt, "INVT", "Invite a user to a room");
772 CtdlRegisterProtoHook(cmd_kick, "KICK", "Kick a user out of a room");
773 CtdlRegisterProtoHook(cmd_forg, "FORG", "Forget a room");
774 CtdlRegisterProtoHook(cmd_gnur, "GNUR", "Get Next Unregistered User");
775 CtdlRegisterProtoHook(cmd_vali, "VALI", "Validate new users");
776 CtdlRegisterProtoHook(cmd_list, "LIST", "List users");
777 CtdlRegisterProtoHook(cmd_chek, "CHEK", "assorted info we need to check at login");
778 CtdlRegisterProtoHook(cmd_qusr, "QUSR", "check to see if a user exists");
779 CtdlRegisterProtoHook(cmd_agup, "AGUP", "Administratively Get User Parameters");
780 CtdlRegisterProtoHook(cmd_asup, "ASUP", "Administratively Set User Parameters");
781 CtdlRegisterProtoHook(cmd_seen, "SEEN", "Manipulate seen/unread message flags");
782 CtdlRegisterProtoHook(cmd_gtsn, "GTSN", "Fetch seen/unread message flags");
783 CtdlRegisterProtoHook(cmd_view, "VIEW", "Set preferred view for user/room combination");
784 CtdlRegisterProtoHook(cmd_renu, "RENU", "Rename a user");
785 CtdlRegisterProtoHook(cmd_newu, "NEWU", "Log in as a new user");
786 CtdlRegisterProtoHook(cmd_isme, "ISME", "Determine whether an email address belongs to a user");
788 /* return our Subversion id for the Log */