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