]> code.citadel.org Git - citadel.git/blob - citadel/user_ops.c
* user_ops.c: fixed become_session() when calling EVT_LOGOUT session hooks
[citadel.git] / citadel / user_ops.c
1 /* 
2  * $Id$
3  *
4  * Server functions which perform operations on user objects.
5  *
6  */
7
8 #ifdef DLL_EXPORT
9 #define IN_LIBCIT
10 #endif
11
12 #include "sysdep.h"
13 #include <errno.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <fcntl.h>
18 #include <signal.h>
19 #include <pwd.h>
20 #include <sys/types.h>
21 #include <sys/wait.h>
22
23 #if TIME_WITH_SYS_TIME
24 # include <sys/time.h>
25 # include <time.h>
26 #else
27 # if HAVE_SYS_TIME_H
28 #  include <sys/time.h>
29 # else
30 #  include <time.h>
31 # endif
32 #endif
33
34 #include <string.h>
35 #include <syslog.h>
36 #include <limits.h>
37 #ifndef ENABLE_CHKPWD
38 #include "auth.h"
39 #endif
40 #include "citadel.h"
41 #include "server.h"
42 #include "database.h"
43 #include "user_ops.h"
44 #include "dynloader.h"
45 #include "sysdep_decls.h"
46 #include "support.h"
47 #include "room_ops.h"
48 #include "logging.h"
49 #include "file_ops.h"
50 #include "control.h"
51 #include "msgbase.h"
52 #include "config.h"
53 #include "tools.h"
54 #include "citserver.h"
55
56
57 /*
58  * getuser()  -  retrieve named user into supplied buffer.
59  *               returns 0 on success
60  */
61 int getuser(struct usersupp *usbuf, char name[])
62 {
63
64         char lowercase_name[32];
65         int a;
66         struct cdbdata *cdbus;
67
68         memset(usbuf, 0, sizeof(struct usersupp));
69         for (a = 0; a <= strlen(name); ++a) {
70                 if (a < sizeof(lowercase_name))
71                         lowercase_name[a] = tolower(name[a]);
72         }
73         lowercase_name[sizeof(lowercase_name) - 1] = 0;
74
75         cdbus = cdb_fetch(CDB_USERSUPP, lowercase_name, strlen(lowercase_name));
76         if (cdbus == NULL) {
77                 return (1);     /* user not found */
78         }
79         memcpy(usbuf, cdbus->ptr,
80                ((cdbus->len > sizeof(struct usersupp)) ?
81                 sizeof(struct usersupp) : cdbus->len));
82         cdb_free(cdbus);
83
84         if (usbuf->version < 573) {
85                 CC->usersupp.moderation_filter = config.c_default_filter;
86         }
87         return (0);
88 }
89
90
91 /*
92  * lgetuser()  -  same as getuser() but locks the record
93  */
94 int lgetuser(struct usersupp *usbuf, char *name)
95 {
96         int retcode;
97
98         retcode = getuser(usbuf, name);
99         if (retcode == 0) {
100                 begin_critical_section(S_USERSUPP);
101         }
102         return (retcode);
103 }
104
105
106 /*
107  * putuser()  -  write user buffer into the correct place on disk
108  */
109 void putuser(struct usersupp *usbuf)
110 {
111         char lowercase_name[32];
112         int a;
113
114         for (a = 0; a <= strlen(usbuf->fullname); ++a) {
115                 if (a < sizeof(lowercase_name))
116                         lowercase_name[a] = tolower(usbuf->fullname[a]);
117         }
118         lowercase_name[sizeof(lowercase_name) - 1] = 0;
119
120         usbuf->version = REV_LEVEL;
121         cdb_store(CDB_USERSUPP,
122                   lowercase_name, strlen(lowercase_name),
123                   usbuf, sizeof(struct usersupp));
124
125 }
126
127
128 /*
129  * lputuser()  -  same as putuser() but locks the record
130  */
131 void lputuser(struct usersupp *usbuf)
132 {
133         putuser(usbuf);
134         end_critical_section(S_USERSUPP);
135 }
136
137 /*
138  * Index-generating function used by Ctdl[Get|Set]Relationship
139  */
140 int GenerateRelationshipIndex(char *IndexBuf,
141                               long RoomID,
142                               long RoomGen,
143                               long UserID)
144 {
145
146         struct {
147                 long iRoomID;
148                 long iRoomGen;
149                 long iUserID;
150         } TheIndex;
151
152         TheIndex.iRoomID = RoomID;
153         TheIndex.iRoomGen = RoomGen;
154         TheIndex.iUserID = UserID;
155
156         memcpy(IndexBuf, &TheIndex, sizeof(TheIndex));
157         return (sizeof(TheIndex));
158 }
159
160
161
162 /*
163  * Back end for CtdlSetRelationship()
164  */
165 void put_visit(struct visit *newvisit)
166 {
167         char IndexBuf[32];
168         int IndexLen;
169
170         /* Generate an index */
171         IndexLen = GenerateRelationshipIndex(IndexBuf,
172                                              newvisit->v_roomnum,
173                                              newvisit->v_roomgen,
174                                              newvisit->v_usernum);
175
176         /* Store the record */
177         cdb_store(CDB_VISIT, IndexBuf, IndexLen,
178                   newvisit, sizeof(struct visit)
179         );
180 }
181
182
183
184
185 /*
186  * Define a relationship between a user and a room
187  */
188 void CtdlSetRelationship(struct visit *newvisit,
189                          struct usersupp *rel_user,
190                          struct quickroom *rel_room)
191 {
192
193
194         /* We don't use these in Citadel because they're implicit by the
195          * index, but they must be present if the database is exported.
196          */
197         newvisit->v_roomnum = rel_room->QRnumber;
198         newvisit->v_roomgen = rel_room->QRgen;
199         newvisit->v_usernum = rel_user->usernum;
200
201         put_visit(newvisit);
202 }
203
204 /*
205  * Locate a relationship between a user and a room
206  */
207 void CtdlGetRelationship(struct visit *vbuf,
208                          struct usersupp *rel_user,
209                          struct quickroom *rel_room)
210 {
211
212         char IndexBuf[32];
213         int IndexLen;
214         struct cdbdata *cdbvisit;
215
216         /* Generate an index */
217         IndexLen = GenerateRelationshipIndex(IndexBuf,
218                                              rel_room->QRnumber,
219                                              rel_room->QRgen,
220                                              rel_user->usernum);
221
222         /* Clear out the buffer */
223         memset(vbuf, 0, sizeof(struct visit));
224
225         cdbvisit = cdb_fetch(CDB_VISIT, IndexBuf, IndexLen);
226         if (cdbvisit != NULL) {
227                 memcpy(vbuf, cdbvisit->ptr,
228                        ((cdbvisit->len > sizeof(struct visit)) ?
229                         sizeof(struct visit) : cdbvisit->len));
230                 cdb_free(cdbvisit);
231         }
232
233         /* Set v_seen if necessary */
234         if (vbuf->v_seen[0] == 0) {
235                 sprintf(vbuf->v_seen, "*:%ld", vbuf->v_lastseen);
236         }
237 }
238
239
240 void MailboxName(char *buf, struct usersupp *who, char *prefix)
241 {
242         sprintf(buf, "%010ld.%s", who->usernum, prefix);
243 }
244
245
246 /*
247  * Is the user currently logged in an Aide?
248  */
249 int is_aide(void)
250 {
251         if (CC->usersupp.axlevel >= 6)
252                 return (1);
253         else
254                 return (0);
255 }
256
257
258 /*
259  * Is the user currently logged in an Aide *or* the room aide for this room?
260  */
261 int is_room_aide(void)
262 {
263
264         if (!CC->logged_in) {
265                 return (0);
266         }
267
268         if ((CC->usersupp.axlevel >= 6)
269             || (CC->quickroom.QRroomaide == CC->usersupp.usernum)) {
270                 return (1);
271         } else {
272                 return (0);
273         }
274 }
275
276 /*
277  * getuserbynumber()  -  get user by number
278  *                       returns 0 if user was found
279  *
280  * WARNING: don't use this function unless you absolutely have to.  It does
281  *          a sequential search and therefore is computationally expensive.
282  */
283 int getuserbynumber(struct usersupp *usbuf, long int number)
284 {
285         struct cdbdata *cdbus;
286
287         cdb_rewind(CDB_USERSUPP);
288
289         while (cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
290                 memset(usbuf, 0, sizeof(struct usersupp));
291                 memcpy(usbuf, cdbus->ptr,
292                        ((cdbus->len > sizeof(struct usersupp)) ?
293                         sizeof(struct usersupp) : cdbus->len));
294                 cdb_free(cdbus);
295                 if (usbuf->usernum == number) {
296                         cdb_close_cursor(CDB_USERSUPP);
297                         return (0);
298                 }
299         }
300         return (-1);
301 }
302
303
304 /*
305  * Back end for cmd_user() and its ilk
306  */
307 int CtdlLoginExistingUser(char *trythisname)
308 {
309         char username[SIZ];
310         char autoname[SIZ];
311         int found_user = 0;
312         struct passwd *p;
313         int a;
314
315         if (trythisname == NULL) return login_not_found;
316         safestrncpy(username, trythisname, sizeof username);
317         strproc(username);
318
319         if ((CC->logged_in)) {
320                 return login_already_logged_in;
321         }
322         found_user = getuser(&CC->usersupp, username);
323         if (found_user != 0) {
324                 p = (struct passwd *) getpwnam(username);
325                 if (p != NULL) {
326                         strcpy(autoname, p->pw_gecos);
327                         for (a = 0; a < strlen(autoname); ++a)
328                                 if (autoname[a] == ',')
329                                         autoname[a] = 0;
330                         found_user = getuser(&CC->usersupp, autoname);
331                 }
332         }
333         if (found_user == 0) {
334                 if (((CC->nologin)) && (CC->usersupp.axlevel < 6)) {
335                         return login_too_many_users;
336                 } else {
337                         strcpy(CC->curr_user, CC->usersupp.fullname);
338                         return login_ok;
339                 }
340         }
341         return login_not_found;
342 }
343
344
345
346 /*
347  * USER cmd
348  */
349 void cmd_user(char *cmdbuf)
350 {
351         char username[SIZ];
352         int a;
353
354         extract(username, cmdbuf, 0);
355         username[25] = 0;
356         strproc(username);
357
358         a = CtdlLoginExistingUser(username);
359         switch (a) {
360         case login_already_logged_in:
361                 cprintf("%d Already logged in.\n", ERROR);
362                 return;
363         case login_too_many_users:
364                 cprintf("%d %s: "
365                         "Too many users are already online "
366                         "(maximum is %d)\n",
367                         ERROR + MAX_SESSIONS_EXCEEDED,
368                         config.c_nodename, config.c_maxsessions);
369                 return;
370         case login_ok:
371                 cprintf("%d Password required for %s\n",
372                         MORE_DATA, CC->curr_user);
373                 return;
374         case login_not_found:
375                 cprintf("%d %s not found.\n", ERROR, username);
376                 return;
377                 cprintf("%d Internal error\n", ERROR);
378         }
379 }
380
381
382
383 /*
384  * session startup code which is common to both cmd_pass() and cmd_newu()
385  */
386 void session_startup(void)
387 {
388         syslog(LOG_NOTICE, "session %d: user <%s> logged in",
389                CC->cs_pid, CC->curr_user);
390
391         lgetuser(&CC->usersupp, CC->curr_user);
392         ++(CC->usersupp.timescalled);
393         time(&CC->usersupp.lastcall);
394
395         /* If this user's name is the name of the system administrator
396          * (as specified in setup), automatically assign access level 6.
397          */
398         if (!strcasecmp(CC->usersupp.fullname, config.c_sysadm)) {
399                 CC->usersupp.axlevel = 6;
400         }
401         lputuser(&CC->usersupp);
402
403         /* Run any startup routines registered by loadable modules */
404         PerformSessionHooks(EVT_LOGIN);
405
406         /* Create any personal rooms required by the system */
407         create_room(SENTITEMS, 4, "", 0, 1);
408
409         /* Enter the lobby */
410         usergoto(BASEROOM, 0, NULL, NULL);
411
412         /* Record this login in the Citadel log */
413         rec_log(CL_LOGIN, CC->curr_user);
414 }
415
416
417 void logged_in_response(void)
418 {
419         cprintf("%d %s|%d|%ld|%ld|%u|%ld|%ld\n",
420                 OK, CC->usersupp.fullname, CC->usersupp.axlevel,
421                 CC->usersupp.timescalled, CC->usersupp.posted,
422                 CC->usersupp.flags, CC->usersupp.usernum,
423                 CC->usersupp.lastcall);
424 }
425
426
427
428 /* 
429  * misc things to be taken care of when a user is logged out
430  */
431 void logout(struct CitContext *who)
432 {
433         struct CitContext *saved_cc = NULL;
434
435         who->logged_in = 0;
436
437         /*
438          * If there is a download in progress, abort it.
439          */
440         if (who->download_fp != NULL) {
441                 fclose(who->download_fp);
442                 who->download_fp = NULL;
443         }
444
445         /*
446          * If there is an upload in progress, abort it.
447          */
448         if (who->upload_fp != NULL) {
449                 abort_upl(who);
450         }
451
452         /*
453          * If we were talking to a network node, we're not anymore...
454          */
455         if (strlen(who->net_node) > 0) {
456                 network_talking_to(who->net_node, NTT_REMOVE);
457         }
458
459         /* Do modular stuff... */
460         if (who != CC)
461                 saved_cc = CC;
462         become_session(who);
463         PerformSessionHooks(EVT_LOGOUT);
464         become_session(saved_cc);
465 }
466
467 #ifdef ENABLE_CHKPWD
468 /*
469  * an alternate version of validpw() which executes `chkpwd' instead of
470  * verifying the password directly
471  */
472 static int validpw(uid_t uid, const char *pass)
473 {
474         pid_t pid;
475         int status, pipev[2];
476         char buf[24];
477
478         if (pipe(pipev)) {
479                 lprintf(1, "pipe failed (%s): denying autologin access for "
480                         "uid %u\n", strerror(errno), uid);
481                 return 0;
482         }
483         switch (pid = fork()) {
484         case -1:
485                 lprintf(1, "fork failed (%s): denying autologin access for "
486                         "uid %u\n", strerror(errno), uid);
487                 close(pipev[0]);
488                 close(pipev[1]);
489                 return 0;
490
491         case 0:
492                 close(pipev[1]);
493                 if (dup2(pipev[0], 0) == -1) {
494                         perror("dup2");
495                         exit(1);
496                 }
497                 close(pipev[0]);
498
499                 execl(BBSDIR "/chkpwd", BBSDIR "/chkpwd", NULL);
500                 perror(BBSDIR "/chkpwd");
501                 exit(1);
502         }
503
504         close(pipev[0]);
505         write(pipev[1], buf, sprintf(buf, "%lu\n", (unsigned long) uid));
506         write(pipev[1], pass, strlen(pass));
507         write(pipev[1], "\n", 1);
508         close(pipev[1]);
509
510         while (waitpid(pid, &status, 0) == -1)
511                 if (errno != EINTR) {
512                         lprintf(1, "waitpid failed (%s): denying autologin "
513                                 "access for uid %u\n",
514                                 strerror(errno), uid);
515                         return 0;
516                 }
517         if (WIFEXITED(status) && !WEXITSTATUS(status))
518                 return 1;
519
520         return 0;
521 }
522 #endif
523
524 void do_login()
525 {
526         (CC->logged_in) = 1;
527         session_startup();
528 }
529
530
531 int CtdlTryPassword(char *password)
532 {
533         int code;
534
535         if ((CC->logged_in)) {
536                 lprintf(5, "CtdlTryPassword: already logged in\n");
537                 return pass_already_logged_in;
538         }
539         if (!strcmp(CC->curr_user, NLI)) {
540                 lprintf(5, "CtdlTryPassword: no user selected\n");
541                 return pass_no_user;
542         }
543         if (getuser(&CC->usersupp, CC->curr_user)) {
544                 lprintf(5, "CtdlTryPassword: internal error\n");
545                 return pass_internal_error;
546         }
547         if (password == NULL) {
548                 lprintf(5, "CtdlTryPassword: NULL password string supplied\n");
549                 return pass_wrong_password;
550         }
551         code = (-1);
552         if (CC->usersupp.uid == BBSUID) {
553                 strproc(password);
554                 strproc(CC->usersupp.password);
555                 code = strcasecmp(CC->usersupp.password, password);
556         }
557 #ifdef ENABLE_AUTOLOGIN
558         else {
559                 if (validpw(CC->usersupp.uid, password)) {
560                         code = 0;
561                         lgetuser(&CC->usersupp, CC->curr_user);
562                         safestrncpy(CC->usersupp.password, password,
563                                     sizeof CC->usersupp.password);
564                         lputuser(&CC->usersupp);
565                 }
566         }
567 #endif
568
569         if (!code) {
570                 do_login();
571                 return pass_ok;
572         } else {
573                 rec_log(CL_BADPW, CC->curr_user);
574                 return pass_wrong_password;
575         }
576 }
577
578
579 void cmd_pass(char *buf)
580 {
581         char password[SIZ];
582         int a;
583
584         extract(password, buf, 0);
585         a = CtdlTryPassword(password);
586
587         switch (a) {
588         case pass_already_logged_in:
589                 cprintf("%d Already logged in.\n", ERROR);
590                 return;
591         case pass_no_user:
592                 cprintf("%d You must send a name with USER first.\n",
593                         ERROR);
594                 return;
595         case pass_wrong_password:
596                 cprintf("%d Wrong password.\n", ERROR);
597                 return;
598         case pass_ok:
599                 logged_in_response();
600                 return;
601                 cprintf("%d Can't find user record!\n",
602                         ERROR + INTERNAL_ERROR);
603         }
604 }
605
606
607
608 /*
609  * Delete a user record *and* all of its related resources.
610  */
611 int purge_user(char pname[])
612 {
613         char filename[64];
614         struct usersupp usbuf;
615         char lowercase_name[32];
616         int a;
617         struct CitContext *ccptr;
618         int user_is_logged_in = 0;
619
620         for (a = 0; a <= strlen(pname); ++a) {
621                 lowercase_name[a] = tolower(pname[a]);
622         }
623
624         if (getuser(&usbuf, pname) != 0) {
625                 lprintf(5, "Cannot purge user <%s> - not found\n", pname);
626                 return (ERROR + NO_SUCH_USER);
627         }
628         /* Don't delete a user who is currently logged in.  Instead, just
629          * set the access level to 0, and let the account get swept up
630          * during the next purge.
631          */
632         user_is_logged_in = 0;
633         begin_critical_section(S_SESSION_TABLE);
634         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
635                 if (ccptr->usersupp.usernum == usbuf.usernum) {
636                         user_is_logged_in = 1;
637                 }
638         }
639         end_critical_section(S_SESSION_TABLE);
640         if (user_is_logged_in == 1) {
641                 lprintf(5, "User <%s> is logged in; not deleting.\n", pname);
642                 usbuf.axlevel = 0;
643                 putuser(&usbuf);
644                 return (1);
645         }
646         lprintf(5, "Deleting user <%s>\n", pname);
647
648         /* Perform any purge functions registered by server extensions */
649         PerformUserHooks(usbuf.fullname, usbuf.usernum, EVT_PURGEUSER);
650
651         /* delete any existing user/room relationships */
652         cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
653
654         /* delete the userlog entry */
655         cdb_delete(CDB_USERSUPP, lowercase_name, strlen(lowercase_name));
656
657         /* remove the user's bio file */
658         sprintf(filename, "./bio/%ld", usbuf.usernum);
659         unlink(filename);
660
661         /* remove the user's picture */
662         sprintf(filename, "./userpics/%ld.gif", usbuf.usernum);
663         unlink(filename);
664
665         return (0);
666 }
667
668
669 /*
670  * create_user()  -  back end processing to create a new user
671  */
672 int create_user(char *newusername)
673 {
674         struct usersupp usbuf;
675         int a;
676         struct passwd *p = NULL;
677         char username[64];
678
679         strcpy(username, newusername);
680         strproc(username);
681
682 #ifdef ENABLE_AUTOLOGIN
683         p = (struct passwd *) getpwnam(username);
684 #endif
685         if (p != NULL) {
686                 strcpy(username, p->pw_gecos);
687                 for (a = 0; a < strlen(username); ++a) {
688                         if (username[a] == ',')
689                                 username[a] = 0;
690                 }
691                 CC->usersupp.uid = p->pw_uid;
692         } else {
693                 CC->usersupp.uid = BBSUID;
694         }
695
696         if (!getuser(&usbuf, username)) {
697                 return (ERROR + ALREADY_EXISTS);
698         }
699         strcpy(CC->curr_user, username);
700         strcpy(CC->usersupp.fullname, username);
701         strcpy(CC->usersupp.password, "");
702         (CC->logged_in) = 1;
703
704         /* These are the default flags on new accounts */
705         CC->usersupp.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
706
707         CC->usersupp.timescalled = 0;
708         CC->usersupp.posted = 0;
709         CC->usersupp.axlevel = config.c_initax;
710         CC->usersupp.USscreenwidth = 80;
711         CC->usersupp.USscreenheight = 24;
712         time(&CC->usersupp.lastcall);
713         CC->usersupp.moderation_filter = config.c_default_filter;
714
715         /* fetch a new user number */
716         CC->usersupp.usernum = get_new_user_number();
717
718         if (CC->usersupp.usernum == 1L) {
719                 CC->usersupp.axlevel = 6;
720         }
721         /* add user to userlog */
722         putuser(&CC->usersupp);
723         if (getuser(&CC->usersupp, CC->curr_user)) {
724                 return (ERROR + INTERNAL_ERROR);
725         }
726         /* give the user a private mailbox */
727         create_room(MAILROOM, 4, "", 0, 1);
728
729         rec_log(CL_NEWUSER, CC->curr_user);
730         return (0);
731 }
732
733
734
735
736 /*
737  * cmd_newu()  -  create a new user account
738  */
739 void cmd_newu(char *cmdbuf)
740 {
741         int a;
742         char username[SIZ];
743
744         if ((CC->logged_in)) {
745                 cprintf("%d Already logged in.\n", ERROR);
746                 return;
747         }
748         if ((CC->nologin)) {
749                 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
750                         ERROR + MAX_SESSIONS_EXCEEDED,
751                         config.c_nodename, config.c_maxsessions);
752         }
753         extract(username, cmdbuf, 0);
754         username[25] = 0;
755         strproc(username);
756
757         if (strlen(username) == 0) {
758                 cprintf("%d You must supply a user name.\n", ERROR);
759                 return;
760         }
761         a = create_user(username);
762         if ((!strcasecmp(username, "bbs")) ||
763             (!strcasecmp(username, "new")) ||
764             (!strcasecmp(username, "."))) {
765                 cprintf("%d '%s' is an invalid login name.\n", ERROR, username);
766                 return;
767         }
768         if (a == ERROR + ALREADY_EXISTS) {
769                 cprintf("%d '%s' already exists.\n",
770                         ERROR + ALREADY_EXISTS, username);
771                 return;
772         } else if (a == ERROR + INTERNAL_ERROR) {
773                 cprintf("%d Internal error - user record disappeared?\n",
774                         ERROR + INTERNAL_ERROR);
775                 return;
776         } else if (a == 0) {
777                 session_startup();
778                 logged_in_response();
779         } else {
780                 cprintf("%d unknown error\n", ERROR);
781         }
782         rec_log(CL_NEWUSER, CC->curr_user);
783 }
784
785
786
787 /*
788  * set password
789  */
790 void cmd_setp(char *new_pw)
791 {
792         if (CtdlAccessCheck(ac_logged_in))
793                 return;
794
795         if (CC->usersupp.uid != BBSUID) {
796                 cprintf("%d Not allowed.  Use the 'passwd' command.\n", ERROR);
797                 return;
798         }
799         strproc(new_pw);
800         if (strlen(new_pw) == 0) {
801                 cprintf("%d Password unchanged.\n", OK);
802                 return;
803         }
804         lgetuser(&CC->usersupp, CC->curr_user);
805         strcpy(CC->usersupp.password, new_pw);
806         lputuser(&CC->usersupp);
807         cprintf("%d Password changed.\n", OK);
808         rec_log(CL_PWCHANGE, CC->curr_user);
809         PerformSessionHooks(EVT_SETPASS);
810 }
811
812 /*
813  * get user parameters
814  */
815 void cmd_getu(void)
816 {
817
818         if (CtdlAccessCheck(ac_logged_in))
819                 return;
820
821         getuser(&CC->usersupp, CC->curr_user);
822         cprintf("%d %d|%d|%d|%d\n",
823                 OK,
824                 CC->usersupp.USscreenwidth,
825                 CC->usersupp.USscreenheight,
826                 (CC->usersupp.flags & US_USER_SET),
827                 CC->usersupp.moderation_filter
828             );
829 }
830
831 /*
832  * set user parameters
833  */
834 void cmd_setu(char *new_parms)
835 {
836         int new_mod;
837
838         if (CtdlAccessCheck(ac_logged_in))
839                 return;
840
841         if (num_parms(new_parms) < 3) {
842                 cprintf("%d Usage error.\n", ERROR);
843                 return;
844         }
845         lgetuser(&CC->usersupp, CC->curr_user);
846         CC->usersupp.USscreenwidth = extract_int(new_parms, 0);
847         CC->usersupp.USscreenheight = extract_int(new_parms, 1);
848         CC->usersupp.flags = CC->usersupp.flags & (~US_USER_SET);
849         CC->usersupp.flags = CC->usersupp.flags |
850             (extract_int(new_parms, 2) & US_USER_SET);
851
852         if (num_parms(new_parms) >= 4) {
853                 new_mod = extract_int(new_parms, 3);
854                 lprintf(9, "new_mod extracted to %d\n", new_mod);
855
856                 /* Aides cannot set the filter level lower than -100 */
857                 if (new_mod < (-100))
858                         new_mod = -100;
859
860                 /* Normal users cannot set the filter level lower than -63 */
861                 if ((new_mod < (-63)) && (CC->usersupp.axlevel < 6))
862                         new_mod = -63;
863
864                 /* Nobody can set the filter level higher than +63 */
865                 if (new_mod > 63)
866                         new_mod = 63;
867
868                 CC->usersupp.moderation_filter = new_mod;
869                 lprintf(9, "new_mod processed to %d\n", new_mod);
870         }
871         lputuser(&CC->usersupp);
872         cprintf("%d Ok\n", OK);
873 }
874
875 /*
876  * set last read pointer
877  */
878 void cmd_slrp(char *new_ptr)
879 {
880         long newlr;
881         struct visit vbuf;
882
883         if (CtdlAccessCheck(ac_logged_in)) {
884                 return;
885         }
886
887         if (!strncasecmp(new_ptr, "highest", 7)) {
888                 newlr = CC->quickroom.QRhighest;
889         } else {
890                 newlr = atol(new_ptr);
891         }
892
893         lgetuser(&CC->usersupp, CC->curr_user);
894
895         CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
896         vbuf.v_lastseen = newlr;
897         sprintf(vbuf.v_seen, "*:%ld", newlr);
898         CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
899
900         lputuser(&CC->usersupp);
901         cprintf("%d %ld\n", OK, newlr);
902 }
903
904
905 void cmd_seen(char *argbuf) {
906         long target_msgnum = 0L;
907         int target_setting = 0;
908
909         if (CtdlAccessCheck(ac_logged_in)) {
910                 return;
911         }
912
913         if (num_parms(argbuf) != 2) {
914                 cprintf("%d Invalid parameters\n", ERROR);
915                 return;
916         }
917
918         target_msgnum = extract_long(argbuf, 0);
919         target_setting = extract_int(argbuf, 1);
920
921         CtdlSetSeen(target_msgnum, target_setting);
922         cprintf("%d OK\n", OK);
923 }
924
925
926 /*
927  * INVT and KICK commands
928  */
929 void cmd_invt_kick(char *iuser, int op)
930                         /* user name */
931 {                               /* 1 = invite, 0 = kick out */
932         struct usersupp USscratch;
933         char bbb[SIZ];
934         struct visit vbuf;
935
936         if (CtdlAccessCheck(ac_room_aide))
937                 return;
938
939         if (lgetuser(&USscratch, iuser) != 0) {
940                 cprintf("%d No such user.\n", ERROR);
941                 return;
942         }
943         CtdlGetRelationship(&vbuf, &USscratch, &CC->quickroom);
944
945         if (op == 1) {
946                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
947                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
948         }
949         if (op == 0) {
950                 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
951                 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
952         }
953         CtdlSetRelationship(&vbuf, &USscratch, &CC->quickroom);
954
955         lputuser(&USscratch);
956
957         /* post a message in Aide> saying what we just did */
958         sprintf(bbb, "%s %s %s> by %s\n",
959                 iuser,
960                 ((op == 1) ? "invited to" : "kicked out of"),
961                 CC->quickroom.QRname,
962                 CC->usersupp.fullname);
963         aide_message(bbb);
964
965         cprintf("%d %s %s %s.\n",
966                 OK, iuser,
967                 ((op == 1) ? "invited to" : "kicked out of"),
968                 CC->quickroom.QRname);
969         return;
970 }
971
972
973 /*
974  * Forget (Zap) the current room (API call)
975  * Returns 0 on success
976  */
977 int CtdlForgetThisRoom(void) {
978         struct visit vbuf;
979
980         /* On some systems, Aides are not allowed to forget rooms */
981         if (is_aide() && (config.c_aide_zap == 0)) {
982                 return(1);
983         }
984
985         lgetuser(&CC->usersupp, CC->curr_user);
986         CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
987
988         vbuf.v_flags = vbuf.v_flags | V_FORGET;
989         vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
990
991         CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
992         lputuser(&CC->usersupp);
993
994         /* Return to the Lobby, so we don't end up in an undefined room */
995         usergoto(BASEROOM, 0, NULL, NULL);
996         return(0);
997
998 }
999
1000
1001 /*
1002  * forget (Zap) the current room
1003  */
1004 void cmd_forg(void)
1005 {
1006
1007         if (CtdlAccessCheck(ac_logged_in)) {
1008                 return;
1009         }
1010
1011         if (CtdlForgetThisRoom() == 0) {
1012                 cprintf("%d Ok\n", OK);
1013         }
1014         else {
1015                 cprintf("%d You may not forget this room.\n", ERROR);
1016         }
1017 }
1018
1019 /*
1020  * Get Next Unregistered User
1021  */
1022 void cmd_gnur(void)
1023 {
1024         struct cdbdata *cdbus;
1025         struct usersupp usbuf;
1026
1027         if (CtdlAccessCheck(ac_aide)) {
1028                 return;
1029         }
1030
1031         if ((CitControl.MMflags & MM_VALID) == 0) {
1032                 cprintf("%d There are no unvalidated users.\n", OK);
1033                 return;
1034         }
1035
1036         /* There are unvalidated users.  Traverse the usersupp database,
1037          * and return the first user we find that needs validation.
1038          */
1039         cdb_rewind(CDB_USERSUPP);
1040         while (cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
1041                 memset(&usbuf, 0, sizeof(struct usersupp));
1042                 memcpy(&usbuf, cdbus->ptr,
1043                        ((cdbus->len > sizeof(struct usersupp)) ?
1044                         sizeof(struct usersupp) : cdbus->len));
1045                 cdb_free(cdbus);
1046                 if ((usbuf.flags & US_NEEDVALID)
1047                     && (usbuf.axlevel > 0)) {
1048                         cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1049                         cdb_close_cursor(CDB_USERSUPP);
1050                         return;
1051                 }
1052         }
1053
1054         /* If we get to this point, there are no more unvalidated users.
1055          * Therefore we clear the "users need validation" flag.
1056          */
1057
1058         begin_critical_section(S_CONTROL);
1059         get_control();
1060         CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1061         put_control();
1062         end_critical_section(S_CONTROL);
1063         cprintf("%d *** End of registration.\n", OK);
1064
1065
1066 }
1067
1068
1069 /*
1070  * validate a user
1071  */
1072 void cmd_vali(char *v_args)
1073 {
1074         char user[SIZ];
1075         int newax;
1076         struct usersupp userbuf;
1077
1078         extract(user, v_args, 0);
1079         newax = extract_int(v_args, 1);
1080
1081         if (CtdlAccessCheck(ac_aide)) {
1082                 return;
1083         }
1084
1085         if (lgetuser(&userbuf, user) != 0) {
1086                 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1087                 return;
1088         }
1089
1090         userbuf.axlevel = newax;
1091         userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1092
1093         lputuser(&userbuf);
1094
1095         /* If the access level was set to zero, delete the user */
1096         if (newax == 0) {
1097                 if (purge_user(user) == 0) {
1098                         cprintf("%d %s Deleted.\n", OK, userbuf.fullname);
1099                         return;
1100                 }
1101         }
1102         cprintf("%d User '%s' validated.\n", OK, userbuf.fullname);
1103 }
1104
1105
1106
1107 /* 
1108  *  Traverse the user file...
1109  */
1110 void ForEachUser(void (*CallBack) (struct usersupp * EachUser, void *out_data),
1111                  void *in_data)
1112 {
1113         struct usersupp usbuf;
1114         struct cdbdata *cdbus;
1115
1116         cdb_rewind(CDB_USERSUPP);
1117
1118         while (cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
1119                 memset(&usbuf, 0, sizeof(struct usersupp));
1120                 memcpy(&usbuf, cdbus->ptr,
1121                        ((cdbus->len > sizeof(struct usersupp)) ?
1122                         sizeof(struct usersupp) : cdbus->len));
1123                 cdb_free(cdbus);
1124                 (*CallBack) (&usbuf, in_data);
1125         }
1126 }
1127
1128
1129 /*
1130  * List one user (this works with cmd_list)
1131  */
1132 void ListThisUser(struct usersupp *usbuf, void *data)
1133 {
1134         if (usbuf->axlevel > 0) {
1135                 if ((CC->usersupp.axlevel >= 6)
1136                     || ((usbuf->flags & US_UNLISTED) == 0)
1137                     || ((CC->internal_pgm))) {
1138                         cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1139                                 usbuf->fullname,
1140                                 usbuf->axlevel,
1141                                 usbuf->usernum,
1142                                 (long)usbuf->lastcall,
1143                                 usbuf->timescalled,
1144                                 usbuf->posted);
1145                         if (CC->usersupp.axlevel >= 6)
1146                                 cprintf("%s", usbuf->password);
1147                         cprintf("\n");
1148                 }
1149         }
1150 }
1151
1152 /* 
1153  *  List users
1154  */
1155 void cmd_list(void)
1156 {
1157         cprintf("%d \n", LISTING_FOLLOWS);
1158         ForEachUser(ListThisUser, NULL);
1159         cprintf("000\n");
1160 }
1161
1162
1163
1164
1165 /*
1166  * assorted info we need to check at login
1167  */
1168 void cmd_chek(void)
1169 {
1170         int mail = 0;
1171         int regis = 0;
1172         int vali = 0;
1173
1174         if (CtdlAccessCheck(ac_logged_in)) {
1175                 return;
1176         }
1177
1178         getuser(&CC->usersupp, CC->curr_user);  /* no lock is needed here */
1179         if ((REGISCALL != 0) && ((CC->usersupp.flags & US_REGIS) == 0))
1180                 regis = 1;
1181
1182         if (CC->usersupp.axlevel >= 6) {
1183                 get_control();
1184                 if (CitControl.MMflags & MM_VALID)
1185                         vali = 1;
1186         }
1187
1188         /* check for mail */
1189         mail = NewMailCount();
1190
1191         cprintf("%d %d|%d|%d\n", OK, mail, regis, vali);
1192 }
1193
1194
1195 /*
1196  * check to see if a user exists
1197  */
1198 void cmd_qusr(char *who)
1199 {
1200         struct usersupp usbuf;
1201
1202         if (getuser(&usbuf, who) == 0) {
1203                 cprintf("%d %s\n", OK, usbuf.fullname);
1204         } else {
1205                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1206         }
1207 }
1208
1209
1210 /*
1211  * Administrative Get User Parameters
1212  */
1213 void cmd_agup(char *cmdbuf)
1214 {
1215         struct usersupp usbuf;
1216         char requested_user[SIZ];
1217
1218         if (CtdlAccessCheck(ac_aide)) {
1219                 return;
1220         }
1221
1222         extract(requested_user, cmdbuf, 0);
1223         if (getuser(&usbuf, requested_user) != 0) {
1224                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1225                 return;
1226         }
1227         cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1228                 OK,
1229                 usbuf.fullname,
1230                 usbuf.password,
1231                 usbuf.flags,
1232                 usbuf.timescalled,
1233                 usbuf.posted,
1234                 (int) usbuf.axlevel,
1235                 usbuf.usernum,
1236                 (long)usbuf.lastcall,
1237                 usbuf.USuserpurge);
1238 }
1239
1240
1241
1242 /*
1243  * Administrative Set User Parameters
1244  */
1245 void cmd_asup(char *cmdbuf)
1246 {
1247         struct usersupp usbuf;
1248         char requested_user[SIZ];
1249         int np;
1250         int newax;
1251         int deleted = 0;
1252
1253         if (CtdlAccessCheck(ac_aide))
1254                 return;
1255
1256         extract(requested_user, cmdbuf, 0);
1257         if (lgetuser(&usbuf, requested_user) != 0) {
1258                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1259                 return;
1260         }
1261         np = num_parms(cmdbuf);
1262         if (np > 1)
1263                 extract(usbuf.password, cmdbuf, 1);
1264         if (np > 2)
1265                 usbuf.flags = extract_int(cmdbuf, 2);
1266         if (np > 3)
1267                 usbuf.timescalled = extract_int(cmdbuf, 3);
1268         if (np > 4)
1269                 usbuf.posted = extract_int(cmdbuf, 4);
1270         if (np > 5) {
1271                 newax = extract_int(cmdbuf, 5);
1272                 if ((newax >= 0) && (newax <= 6)) {
1273                         usbuf.axlevel = extract_int(cmdbuf, 5);
1274                 }
1275         }
1276         if (np > 7) {
1277                 usbuf.lastcall = extract_long(cmdbuf, 7);
1278         }
1279         if (np > 8) {
1280                 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1281         }
1282         lputuser(&usbuf);
1283         if (usbuf.axlevel == 0) {
1284                 if (purge_user(requested_user) == 0) {
1285                         deleted = 1;
1286                 }
1287         }
1288         cprintf("%d Ok", OK);
1289         if (deleted)
1290                 cprintf(" (%s deleted)", requested_user);
1291         cprintf("\n");
1292 }
1293
1294
1295 /*
1296  * Count the number of new mail messages the user has
1297  */
1298 int NewMailCount()
1299 {
1300         int num_newmsgs = 0;
1301         int a;
1302         char mailboxname[ROOMNAMELEN];
1303         struct quickroom mailbox;
1304         struct visit vbuf;
1305         struct cdbdata *cdbfr;
1306         long *msglist = NULL;
1307         int num_msgs = 0;
1308
1309         MailboxName(mailboxname, &CC->usersupp, MAILROOM);
1310         if (getroom(&mailbox, mailboxname) != 0)
1311                 return (0);
1312         CtdlGetRelationship(&vbuf, &CC->usersupp, &mailbox);
1313
1314         cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1315
1316         if (cdbfr != NULL) {
1317                 msglist = mallok(cdbfr->len);
1318                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1319                 num_msgs = cdbfr->len / sizeof(long);
1320                 cdb_free(cdbfr);
1321         }
1322         if (num_msgs > 0)
1323                 for (a = 0; a < num_msgs; ++a) {
1324                         if (msglist[a] > 0L) {
1325                                 if (msglist[a] > vbuf.v_lastseen) {
1326                                         ++num_newmsgs;
1327                                 }
1328                         }
1329                 }
1330         if (msglist != NULL)
1331                 phree(msglist);
1332
1333         return (num_newmsgs);
1334 }