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