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