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