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