]> code.citadel.org Git - citadel.git/blob - citadel/user_ops.c
* user_ops.c: 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         who->logged_in = 0;
434
435         /*
436          * If there is a download in progress, abort it.
437          */
438         if (who->download_fp != NULL) {
439                 fclose(who->download_fp);
440                 who->download_fp = NULL;
441         }
442
443         /*
444          * If there is an upload in progress, abort it.
445          */
446         if (who->upload_fp != NULL) {
447                 abort_upl(who);
448         }
449
450         /*
451          * If we were talking to a network node, we're not anymore...
452          */
453         if (strlen(who->net_node) > 0) {
454                 network_talking_to(who->net_node, NTT_REMOVE);
455         }
456
457         /* Do modular stuff... */
458         become_session(who);
459         PerformSessionHooks(EVT_LOGOUT);
460         become_session(NULL);
461 }
462
463 #ifdef ENABLE_CHKPWD
464 /*
465  * an alternate version of validpw() which executes `chkpwd' instead of
466  * verifying the password directly
467  */
468 static int validpw(uid_t uid, const char *pass)
469 {
470         pid_t pid;
471         int status, pipev[2];
472         char buf[24];
473
474         if (pipe(pipev)) {
475                 lprintf(1, "pipe failed (%s): denying autologin access for "
476                         "uid %u\n", strerror(errno), uid);
477                 return 0;
478         }
479         switch (pid = fork()) {
480         case -1:
481                 lprintf(1, "fork failed (%s): denying autologin access for "
482                         "uid %u\n", strerror(errno), uid);
483                 close(pipev[0]);
484                 close(pipev[1]);
485                 return 0;
486
487         case 0:
488                 close(pipev[1]);
489                 if (dup2(pipev[0], 0) == -1) {
490                         perror("dup2");
491                         exit(1);
492                 }
493                 close(pipev[0]);
494
495                 execl(BBSDIR "/chkpwd", BBSDIR "/chkpwd", NULL);
496                 perror(BBSDIR "/chkpwd");
497                 exit(1);
498         }
499
500         close(pipev[0]);
501         write(pipev[1], buf, sprintf(buf, "%lu\n", (unsigned long) uid));
502         write(pipev[1], pass, strlen(pass));
503         write(pipev[1], "\n", 1);
504         close(pipev[1]);
505
506         while (waitpid(pid, &status, 0) == -1)
507                 if (errno != EINTR) {
508                         lprintf(1, "waitpid failed (%s): denying autologin "
509                                 "access for uid %u\n",
510                                 strerror(errno), uid);
511                         return 0;
512                 }
513         if (WIFEXITED(status) && !WEXITSTATUS(status))
514                 return 1;
515
516         return 0;
517 }
518 #endif
519
520 void do_login()
521 {
522         (CC->logged_in) = 1;
523         session_startup();
524 }
525
526
527 int CtdlTryPassword(char *password)
528 {
529         int code;
530
531         if ((CC->logged_in)) {
532                 lprintf(5, "CtdlTryPassword: already logged in\n");
533                 return pass_already_logged_in;
534         }
535         if (!strcmp(CC->curr_user, NLI)) {
536                 lprintf(5, "CtdlTryPassword: no user selected\n");
537                 return pass_no_user;
538         }
539         if (getuser(&CC->usersupp, CC->curr_user)) {
540                 lprintf(5, "CtdlTryPassword: internal error\n");
541                 return pass_internal_error;
542         }
543         if (password == NULL) {
544                 lprintf(5, "CtdlTryPassword: NULL password string supplied\n");
545                 return pass_wrong_password;
546         }
547         code = (-1);
548         if (CC->usersupp.uid == BBSUID) {
549                 strproc(password);
550                 strproc(CC->usersupp.password);
551                 code = strcasecmp(CC->usersupp.password, password);
552         }
553 #ifdef ENABLE_AUTOLOGIN
554         else {
555                 if (validpw(CC->usersupp.uid, password)) {
556                         code = 0;
557                         lgetuser(&CC->usersupp, CC->curr_user);
558                         safestrncpy(CC->usersupp.password, password,
559                                     sizeof CC->usersupp.password);
560                         lputuser(&CC->usersupp);
561                 }
562         }
563 #endif
564
565         if (!code) {
566                 do_login();
567                 return pass_ok;
568         } else {
569                 rec_log(CL_BADPW, CC->curr_user);
570                 return pass_wrong_password;
571         }
572 }
573
574
575 void cmd_pass(char *buf)
576 {
577         char password[SIZ];
578         int a;
579
580         extract(password, buf, 0);
581         a = CtdlTryPassword(password);
582
583         switch (a) {
584         case pass_already_logged_in:
585                 cprintf("%d Already logged in.\n", ERROR);
586                 return;
587         case pass_no_user:
588                 cprintf("%d You must send a name with USER first.\n",
589                         ERROR);
590                 return;
591         case pass_wrong_password:
592                 cprintf("%d Wrong password.\n", ERROR);
593                 return;
594         case pass_ok:
595                 logged_in_response();
596                 return;
597                 cprintf("%d Can't find user record!\n",
598                         ERROR + INTERNAL_ERROR);
599         }
600 }
601
602
603
604 /*
605  * Delete a user record *and* all of its related resources.
606  */
607 int purge_user(char pname[])
608 {
609         char filename[64];
610         struct usersupp usbuf;
611         char lowercase_name[32];
612         int a;
613         struct CitContext *ccptr;
614         int user_is_logged_in = 0;
615
616         for (a = 0; a <= strlen(pname); ++a) {
617                 lowercase_name[a] = tolower(pname[a]);
618         }
619
620         if (getuser(&usbuf, pname) != 0) {
621                 lprintf(5, "Cannot purge user <%s> - not found\n", pname);
622                 return (ERROR + NO_SUCH_USER);
623         }
624         /* Don't delete a user who is currently logged in.  Instead, just
625          * set the access level to 0, and let the account get swept up
626          * during the next purge.
627          */
628         user_is_logged_in = 0;
629         begin_critical_section(S_SESSION_TABLE);
630         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
631                 if (ccptr->usersupp.usernum == usbuf.usernum) {
632                         user_is_logged_in = 1;
633                 }
634         }
635         end_critical_section(S_SESSION_TABLE);
636         if (user_is_logged_in == 1) {
637                 lprintf(5, "User <%s> is logged in; not deleting.\n", pname);
638                 usbuf.axlevel = 0;
639                 putuser(&usbuf);
640                 return (1);
641         }
642         lprintf(5, "Deleting user <%s>\n", pname);
643
644         /* Perform any purge functions registered by server extensions */
645         PerformUserHooks(usbuf.fullname, usbuf.usernum, EVT_PURGEUSER);
646
647         /* delete any existing user/room relationships */
648         cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
649
650         /* delete the userlog entry */
651         cdb_delete(CDB_USERSUPP, lowercase_name, strlen(lowercase_name));
652
653         /* remove the user's bio file */
654         sprintf(filename, "./bio/%ld", usbuf.usernum);
655         unlink(filename);
656
657         /* remove the user's picture */
658         sprintf(filename, "./userpics/%ld.gif", usbuf.usernum);
659         unlink(filename);
660
661         return (0);
662 }
663
664
665 /*
666  * create_user()  -  back end processing to create a new user
667  */
668 int create_user(char *newusername)
669 {
670         struct usersupp usbuf;
671         int a;
672         struct passwd *p = NULL;
673         char username[64];
674
675         strcpy(username, newusername);
676         strproc(username);
677
678 #ifdef ENABLE_AUTOLOGIN
679         p = (struct passwd *) getpwnam(username);
680 #endif
681         if (p != NULL) {
682                 strcpy(username, p->pw_gecos);
683                 for (a = 0; a < strlen(username); ++a) {
684                         if (username[a] == ',')
685                                 username[a] = 0;
686                 }
687                 CC->usersupp.uid = p->pw_uid;
688         } else {
689                 CC->usersupp.uid = BBSUID;
690         }
691
692         if (!getuser(&usbuf, username)) {
693                 return (ERROR + ALREADY_EXISTS);
694         }
695         strcpy(CC->curr_user, username);
696         strcpy(CC->usersupp.fullname, username);
697         strcpy(CC->usersupp.password, "");
698         (CC->logged_in) = 1;
699
700         /* These are the default flags on new accounts */
701         CC->usersupp.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
702
703         CC->usersupp.timescalled = 0;
704         CC->usersupp.posted = 0;
705         CC->usersupp.axlevel = config.c_initax;
706         CC->usersupp.USscreenwidth = 80;
707         CC->usersupp.USscreenheight = 24;
708         time(&CC->usersupp.lastcall);
709         CC->usersupp.moderation_filter = config.c_default_filter;
710
711         /* fetch a new user number */
712         CC->usersupp.usernum = get_new_user_number();
713
714         if (CC->usersupp.usernum == 1L) {
715                 CC->usersupp.axlevel = 6;
716         }
717         /* add user to userlog */
718         putuser(&CC->usersupp);
719         if (getuser(&CC->usersupp, CC->curr_user)) {
720                 return (ERROR + INTERNAL_ERROR);
721         }
722         /* give the user a private mailbox */
723         create_room(MAILROOM, 4, "", 0, 1);
724
725         rec_log(CL_NEWUSER, CC->curr_user);
726         return (0);
727 }
728
729
730
731
732 /*
733  * cmd_newu()  -  create a new user account
734  */
735 void cmd_newu(char *cmdbuf)
736 {
737         int a;
738         char username[SIZ];
739
740         if ((CC->logged_in)) {
741                 cprintf("%d Already logged in.\n", ERROR);
742                 return;
743         }
744         if ((CC->nologin)) {
745                 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
746                         ERROR + MAX_SESSIONS_EXCEEDED,
747                         config.c_nodename, config.c_maxsessions);
748         }
749         extract(username, cmdbuf, 0);
750         username[25] = 0;
751         strproc(username);
752
753         if (strlen(username) == 0) {
754                 cprintf("%d You must supply a user name.\n", ERROR);
755                 return;
756         }
757         a = create_user(username);
758         if ((!strcasecmp(username, "bbs")) ||
759             (!strcasecmp(username, "new")) ||
760             (!strcasecmp(username, "."))) {
761                 cprintf("%d '%s' is an invalid login name.\n", ERROR, username);
762                 return;
763         }
764         if (a == ERROR + ALREADY_EXISTS) {
765                 cprintf("%d '%s' already exists.\n",
766                         ERROR + ALREADY_EXISTS, username);
767                 return;
768         } else if (a == ERROR + INTERNAL_ERROR) {
769                 cprintf("%d Internal error - user record disappeared?\n",
770                         ERROR + INTERNAL_ERROR);
771                 return;
772         } else if (a == 0) {
773                 session_startup();
774                 logged_in_response();
775         } else {
776                 cprintf("%d unknown error\n", ERROR);
777         }
778         rec_log(CL_NEWUSER, CC->curr_user);
779 }
780
781
782
783 /*
784  * set password
785  */
786 void cmd_setp(char *new_pw)
787 {
788         if (CtdlAccessCheck(ac_logged_in))
789                 return;
790
791         if (CC->usersupp.uid != BBSUID) {
792                 cprintf("%d Not allowed.  Use the 'passwd' command.\n", ERROR);
793                 return;
794         }
795         strproc(new_pw);
796         if (strlen(new_pw) == 0) {
797                 cprintf("%d Password unchanged.\n", OK);
798                 return;
799         }
800         lgetuser(&CC->usersupp, CC->curr_user);
801         strcpy(CC->usersupp.password, new_pw);
802         lputuser(&CC->usersupp);
803         cprintf("%d Password changed.\n", OK);
804         rec_log(CL_PWCHANGE, CC->curr_user);
805         PerformSessionHooks(EVT_SETPASS);
806 }
807
808 /*
809  * get user parameters
810  */
811 void cmd_getu(void)
812 {
813
814         if (CtdlAccessCheck(ac_logged_in))
815                 return;
816
817         getuser(&CC->usersupp, CC->curr_user);
818         cprintf("%d %d|%d|%d|%d\n",
819                 OK,
820                 CC->usersupp.USscreenwidth,
821                 CC->usersupp.USscreenheight,
822                 (CC->usersupp.flags & US_USER_SET),
823                 CC->usersupp.moderation_filter
824             );
825 }
826
827 /*
828  * set user parameters
829  */
830 void cmd_setu(char *new_parms)
831 {
832         int new_mod;
833
834         if (CtdlAccessCheck(ac_logged_in))
835                 return;
836
837         if (num_parms(new_parms) < 3) {
838                 cprintf("%d Usage error.\n", ERROR);
839                 return;
840         }
841         lgetuser(&CC->usersupp, CC->curr_user);
842         CC->usersupp.USscreenwidth = extract_int(new_parms, 0);
843         CC->usersupp.USscreenheight = extract_int(new_parms, 1);
844         CC->usersupp.flags = CC->usersupp.flags & (~US_USER_SET);
845         CC->usersupp.flags = CC->usersupp.flags |
846             (extract_int(new_parms, 2) & US_USER_SET);
847
848         if (num_parms(new_parms) >= 4) {
849                 new_mod = extract_int(new_parms, 3);
850                 lprintf(9, "new_mod extracted to %d\n", new_mod);
851
852                 /* Aides cannot set the filter level lower than -100 */
853                 if (new_mod < (-100))
854                         new_mod = -100;
855
856                 /* Normal users cannot set the filter level lower than -63 */
857                 if ((new_mod < (-63)) && (CC->usersupp.axlevel < 6))
858                         new_mod = -63;
859
860                 /* Nobody can set the filter level higher than +63 */
861                 if (new_mod > 63)
862                         new_mod = 63;
863
864                 CC->usersupp.moderation_filter = new_mod;
865                 lprintf(9, "new_mod processed to %d\n", new_mod);
866         }
867         lputuser(&CC->usersupp);
868         cprintf("%d Ok\n", OK);
869 }
870
871 /*
872  * set last read pointer
873  */
874 void cmd_slrp(char *new_ptr)
875 {
876         long newlr;
877         struct visit vbuf;
878
879         if (CtdlAccessCheck(ac_logged_in)) {
880                 return;
881         }
882
883         if (!strncasecmp(new_ptr, "highest", 7)) {
884                 newlr = CC->quickroom.QRhighest;
885         } else {
886                 newlr = atol(new_ptr);
887         }
888
889         lgetuser(&CC->usersupp, CC->curr_user);
890
891         CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
892         vbuf.v_lastseen = newlr;
893         sprintf(vbuf.v_seen, "*:%ld", newlr);
894         CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
895
896         lputuser(&CC->usersupp);
897         cprintf("%d %ld\n", OK, newlr);
898 }
899
900
901 void cmd_seen(char *argbuf) {
902         long target_msgnum = 0L;
903         int target_setting = 0;
904
905         if (CtdlAccessCheck(ac_logged_in)) {
906                 return;
907         }
908
909         if (num_parms(argbuf) != 2) {
910                 cprintf("%d Invalid parameters\n", ERROR);
911                 return;
912         }
913
914         target_msgnum = extract_long(argbuf, 0);
915         target_setting = extract_int(argbuf, 1);
916
917         CtdlSetSeen(target_msgnum, target_setting);
918         cprintf("%d OK\n", OK);
919 }
920
921
922 /*
923  * INVT and KICK commands
924  */
925 void cmd_invt_kick(char *iuser, int op)
926                         /* user name */
927 {                               /* 1 = invite, 0 = kick out */
928         struct usersupp USscratch;
929         char bbb[SIZ];
930         struct visit vbuf;
931
932         if (CtdlAccessCheck(ac_room_aide))
933                 return;
934
935         if (lgetuser(&USscratch, iuser) != 0) {
936                 cprintf("%d No such user.\n", ERROR);
937                 return;
938         }
939         CtdlGetRelationship(&vbuf, &USscratch, &CC->quickroom);
940
941         if (op == 1) {
942                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
943                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
944         }
945         if (op == 0) {
946                 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
947                 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
948         }
949         CtdlSetRelationship(&vbuf, &USscratch, &CC->quickroom);
950
951         lputuser(&USscratch);
952
953         /* post a message in Aide> saying what we just did */
954         sprintf(bbb, "%s %s %s> by %s\n",
955                 iuser,
956                 ((op == 1) ? "invited to" : "kicked out of"),
957                 CC->quickroom.QRname,
958                 CC->usersupp.fullname);
959         aide_message(bbb);
960
961         cprintf("%d %s %s %s.\n",
962                 OK, iuser,
963                 ((op == 1) ? "invited to" : "kicked out of"),
964                 CC->quickroom.QRname);
965         return;
966 }
967
968
969 /*
970  * Forget (Zap) the current room (API call)
971  * Returns 0 on success
972  */
973 int CtdlForgetThisRoom(void) {
974         struct visit vbuf;
975
976         /* On some systems, Aides are not allowed to forget rooms */
977         if (is_aide() && (config.c_aide_zap == 0)) {
978                 return(1);
979         }
980
981         lgetuser(&CC->usersupp, CC->curr_user);
982         CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
983
984         vbuf.v_flags = vbuf.v_flags | V_FORGET;
985         vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
986
987         CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
988         lputuser(&CC->usersupp);
989
990         /* Return to the Lobby, so we don't end up in an undefined room */
991         usergoto(BASEROOM, 0, NULL, NULL);
992         return(0);
993
994 }
995
996
997 /*
998  * forget (Zap) the current room
999  */
1000 void cmd_forg(void)
1001 {
1002
1003         if (CtdlAccessCheck(ac_logged_in)) {
1004                 return;
1005         }
1006
1007         if (CtdlForgetThisRoom() == 0) {
1008                 cprintf("%d Ok\n", OK);
1009         }
1010         else {
1011                 cprintf("%d You may not forget this room.\n", ERROR);
1012         }
1013 }
1014
1015 /*
1016  * Get Next Unregistered User
1017  */
1018 void cmd_gnur(void)
1019 {
1020         struct cdbdata *cdbus;
1021         struct usersupp usbuf;
1022
1023         if (CtdlAccessCheck(ac_aide)) {
1024                 return;
1025         }
1026
1027         if ((CitControl.MMflags & MM_VALID) == 0) {
1028                 cprintf("%d There are no unvalidated users.\n", OK);
1029                 return;
1030         }
1031
1032         /* There are unvalidated users.  Traverse the usersupp database,
1033          * and return the first user we find that needs validation.
1034          */
1035         cdb_rewind(CDB_USERSUPP);
1036         while (cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
1037                 memset(&usbuf, 0, sizeof(struct usersupp));
1038                 memcpy(&usbuf, cdbus->ptr,
1039                        ((cdbus->len > sizeof(struct usersupp)) ?
1040                         sizeof(struct usersupp) : cdbus->len));
1041                 cdb_free(cdbus);
1042                 if ((usbuf.flags & US_NEEDVALID)
1043                     && (usbuf.axlevel > 0)) {
1044                         cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1045                         cdb_close_cursor(CDB_USERSUPP);
1046                         return;
1047                 }
1048         }
1049
1050         /* If we get to this point, there are no more unvalidated users.
1051          * Therefore we clear the "users need validation" flag.
1052          */
1053
1054         begin_critical_section(S_CONTROL);
1055         get_control();
1056         CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1057         put_control();
1058         end_critical_section(S_CONTROL);
1059         cprintf("%d *** End of registration.\n", OK);
1060
1061
1062 }
1063
1064
1065 /*
1066  * validate a user
1067  */
1068 void cmd_vali(char *v_args)
1069 {
1070         char user[SIZ];
1071         int newax;
1072         struct usersupp userbuf;
1073
1074         extract(user, v_args, 0);
1075         newax = extract_int(v_args, 1);
1076
1077         if (CtdlAccessCheck(ac_aide)) {
1078                 return;
1079         }
1080
1081         if (lgetuser(&userbuf, user) != 0) {
1082                 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1083                 return;
1084         }
1085
1086         userbuf.axlevel = newax;
1087         userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1088
1089         lputuser(&userbuf);
1090
1091         /* If the access level was set to zero, delete the user */
1092         if (newax == 0) {
1093                 if (purge_user(user) == 0) {
1094                         cprintf("%d %s Deleted.\n", OK, userbuf.fullname);
1095                         return;
1096                 }
1097         }
1098         cprintf("%d User '%s' validated.\n", OK, userbuf.fullname);
1099 }
1100
1101
1102
1103 /* 
1104  *  Traverse the user file...
1105  */
1106 void ForEachUser(void (*CallBack) (struct usersupp * EachUser, void *out_data),
1107                  void *in_data)
1108 {
1109         struct usersupp usbuf;
1110         struct cdbdata *cdbus;
1111
1112         cdb_rewind(CDB_USERSUPP);
1113
1114         while (cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
1115                 memset(&usbuf, 0, sizeof(struct usersupp));
1116                 memcpy(&usbuf, cdbus->ptr,
1117                        ((cdbus->len > sizeof(struct usersupp)) ?
1118                         sizeof(struct usersupp) : cdbus->len));
1119                 cdb_free(cdbus);
1120                 (*CallBack) (&usbuf, in_data);
1121         }
1122 }
1123
1124
1125 /*
1126  * List one user (this works with cmd_list)
1127  */
1128 void ListThisUser(struct usersupp *usbuf, void *data)
1129 {
1130         if (usbuf->axlevel > 0) {
1131                 if ((CC->usersupp.axlevel >= 6)
1132                     || ((usbuf->flags & US_UNLISTED) == 0)
1133                     || ((CC->internal_pgm))) {
1134                         cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1135                                 usbuf->fullname,
1136                                 usbuf->axlevel,
1137                                 usbuf->usernum,
1138                                 (long)usbuf->lastcall,
1139                                 usbuf->timescalled,
1140                                 usbuf->posted);
1141                         if (CC->usersupp.axlevel >= 6)
1142                                 cprintf("%s", usbuf->password);
1143                         cprintf("\n");
1144                 }
1145         }
1146 }
1147
1148 /* 
1149  *  List users
1150  */
1151 void cmd_list(void)
1152 {
1153         cprintf("%d \n", LISTING_FOLLOWS);
1154         ForEachUser(ListThisUser, NULL);
1155         cprintf("000\n");
1156 }
1157
1158
1159
1160
1161 /*
1162  * assorted info we need to check at login
1163  */
1164 void cmd_chek(void)
1165 {
1166         int mail = 0;
1167         int regis = 0;
1168         int vali = 0;
1169
1170         if (CtdlAccessCheck(ac_logged_in)) {
1171                 return;
1172         }
1173
1174         getuser(&CC->usersupp, CC->curr_user);  /* no lock is needed here */
1175         if ((REGISCALL != 0) && ((CC->usersupp.flags & US_REGIS) == 0))
1176                 regis = 1;
1177
1178         if (CC->usersupp.axlevel >= 6) {
1179                 get_control();
1180                 if (CitControl.MMflags & MM_VALID)
1181                         vali = 1;
1182         }
1183
1184         /* check for mail */
1185         mail = NewMailCount();
1186
1187         cprintf("%d %d|%d|%d\n", OK, mail, regis, vali);
1188 }
1189
1190
1191 /*
1192  * check to see if a user exists
1193  */
1194 void cmd_qusr(char *who)
1195 {
1196         struct usersupp usbuf;
1197
1198         if (getuser(&usbuf, who) == 0) {
1199                 cprintf("%d %s\n", OK, usbuf.fullname);
1200         } else {
1201                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1202         }
1203 }
1204
1205
1206 /*
1207  * Administrative Get User Parameters
1208  */
1209 void cmd_agup(char *cmdbuf)
1210 {
1211         struct usersupp usbuf;
1212         char requested_user[SIZ];
1213
1214         if (CtdlAccessCheck(ac_aide)) {
1215                 return;
1216         }
1217
1218         extract(requested_user, cmdbuf, 0);
1219         if (getuser(&usbuf, requested_user) != 0) {
1220                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1221                 return;
1222         }
1223         cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1224                 OK,
1225                 usbuf.fullname,
1226                 usbuf.password,
1227                 usbuf.flags,
1228                 usbuf.timescalled,
1229                 usbuf.posted,
1230                 (int) usbuf.axlevel,
1231                 usbuf.usernum,
1232                 (long)usbuf.lastcall,
1233                 usbuf.USuserpurge);
1234 }
1235
1236
1237
1238 /*
1239  * Administrative Set User Parameters
1240  */
1241 void cmd_asup(char *cmdbuf)
1242 {
1243         struct usersupp usbuf;
1244         char requested_user[SIZ];
1245         int np;
1246         int newax;
1247         int deleted = 0;
1248
1249         if (CtdlAccessCheck(ac_aide))
1250                 return;
1251
1252         extract(requested_user, cmdbuf, 0);
1253         if (lgetuser(&usbuf, requested_user) != 0) {
1254                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1255                 return;
1256         }
1257         np = num_parms(cmdbuf);
1258         if (np > 1)
1259                 extract(usbuf.password, cmdbuf, 1);
1260         if (np > 2)
1261                 usbuf.flags = extract_int(cmdbuf, 2);
1262         if (np > 3)
1263                 usbuf.timescalled = extract_int(cmdbuf, 3);
1264         if (np > 4)
1265                 usbuf.posted = extract_int(cmdbuf, 4);
1266         if (np > 5) {
1267                 newax = extract_int(cmdbuf, 5);
1268                 if ((newax >= 0) && (newax <= 6)) {
1269                         usbuf.axlevel = extract_int(cmdbuf, 5);
1270                 }
1271         }
1272         if (np > 7) {
1273                 usbuf.lastcall = extract_long(cmdbuf, 7);
1274         }
1275         if (np > 8) {
1276                 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1277         }
1278         lputuser(&usbuf);
1279         if (usbuf.axlevel == 0) {
1280                 if (purge_user(requested_user) == 0) {
1281                         deleted = 1;
1282                 }
1283         }
1284         cprintf("%d Ok", OK);
1285         if (deleted)
1286                 cprintf(" (%s deleted)", requested_user);
1287         cprintf("\n");
1288 }
1289
1290
1291 /*
1292  * Count the number of new mail messages the user has
1293  */
1294 int NewMailCount()
1295 {
1296         int num_newmsgs = 0;
1297         int a;
1298         char mailboxname[ROOMNAMELEN];
1299         struct quickroom mailbox;
1300         struct visit vbuf;
1301         struct cdbdata *cdbfr;
1302         long *msglist = NULL;
1303         int num_msgs = 0;
1304
1305         MailboxName(mailboxname, &CC->usersupp, MAILROOM);
1306         if (getroom(&mailbox, mailboxname) != 0)
1307                 return (0);
1308         CtdlGetRelationship(&vbuf, &CC->usersupp, &mailbox);
1309
1310         cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1311
1312         if (cdbfr != NULL) {
1313                 msglist = mallok(cdbfr->len);
1314                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1315                 num_msgs = cdbfr->len / sizeof(long);
1316                 cdb_free(cdbfr);
1317         }
1318         if (num_msgs > 0)
1319                 for (a = 0; a < num_msgs; ++a) {
1320                         if (msglist[a] > 0L) {
1321                                 if (msglist[a] > vbuf.v_lastseen) {
1322                                         ++num_newmsgs;
1323                                 }
1324                         }
1325                 }
1326         if (msglist != NULL)
1327                 phree(msglist);
1328
1329         return (num_newmsgs);
1330 }