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