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