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