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