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