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