3c3485e856654d302004660f3e43acc8d8927c12
[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 #include "genstamp.h"
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         char buf[SIZ];
810         uid_t uid = (-1);
811
812         safestrncpy(username, newusername, sizeof username);
813         strproc(username);
814
815 #ifdef ENABLE_AUTOLOGIN
816         struct passwd pd;
817         struct passwd *tempPwdPtr;
818         char pwdbuffer[256];
819
820         getpwnam_r(username, &pd, pwdbuffer, sizeof pwdbuffer, &tempPwdPtr);
821         if (tempPwdPtr != NULL) {
822                 extract_token(username, pd.pw_gecos, 0, ',', sizeof username);
823                 uid = pd.pw_uid;
824         }
825         else {
826                 return (ERROR + NO_SUCH_USER);
827         }
828 #endif
829
830         if (!getuser(&usbuf, username)) {
831                 return (ERROR + ALREADY_EXISTS);
832         }
833
834         /* Go ahead and initialize a new user record */
835         memset(&usbuf, 0, sizeof(struct ctdluser));
836         safestrncpy(usbuf.fullname, username, sizeof usbuf.fullname);
837         strcpy(usbuf.password, "");
838         usbuf.uid = uid;
839
840         /* These are the default flags on new accounts */
841         usbuf.flags = US_LASTOLD | US_DISAPPEAR | US_PAGINATOR | US_FLOORS;
842
843         usbuf.timescalled = 0;
844         usbuf.posted = 0;
845         usbuf.axlevel = config.c_initax;
846         usbuf.USscreenwidth = 80;
847         usbuf.USscreenheight = 24;
848         usbuf.lastcall = time(NULL);
849
850         /* fetch a new user number */
851         usbuf.usernum = get_new_user_number();
852
853         /* The very first user created on the system will always be an Aide */
854         if (usbuf.usernum == 1L) {
855                 usbuf.axlevel = 6;
856         }
857
858         /* add user to userlog */
859         putuser(&usbuf);
860
861         /*
862          * Give the user a private mailbox and a configuration room.
863          * Make the latter an invisible system room.
864          */
865         MailboxName(mailboxname, sizeof mailboxname, &usbuf, MAILROOM);
866         create_room(mailboxname, 5, "", 0, 1, 1, VIEW_MAILBOX);
867
868         MailboxName(mailboxname, sizeof mailboxname, &usbuf, USERCONFIGROOM);
869         create_room(mailboxname, 5, "", 0, 1, 1, VIEW_BBS);
870         if (lgetroom(&qrbuf, mailboxname) == 0) {
871                 qrbuf.QRflags2 |= QR2_SYSTEM;
872                 lputroom(&qrbuf);
873         }
874
875         /* Perform any create functions registered by server extensions */
876         PerformUserHooks(&usbuf, EVT_NEWUSER);
877
878         /* Everything below this line can be bypassed if administratively
879          * creating a user, instead of doing self-service account creation
880          */
881
882         if (become_user) {
883                 /* Now become the user we just created */
884                 memcpy(&CC->user, &usbuf, sizeof(struct ctdluser));
885                 safestrncpy(CC->curr_user, username, sizeof CC->curr_user);
886                 CC->logged_in = 1;
887         
888                 /* Check to make sure we're still who we think we are */
889                 if (getuser(&CC->user, CC->curr_user)) {
890                         return (ERROR + INTERNAL_ERROR);
891                 }
892         }
893         
894         snprintf(buf, SIZ, 
895                 "New user account <%s> has been created, from host %s [%s].\n",
896                 username,
897                 CC->cs_host,
898                 CC->cs_addr
899         );
900         aide_message(buf, "User Creation Notice");
901         lprintf(CTDL_NOTICE, "New user <%s> created\n", username);
902         return (0);
903 }
904
905
906
907
908 /*
909  * cmd_newu()  -  create a new user account and log in as that user
910  */
911 void cmd_newu(char *cmdbuf)
912 {
913         int a;
914         char username[26];
915
916 #ifdef ENABLE_AUTOLOGIN
917         cprintf("%d This system does not use native mode authentication.\n",
918                 ERROR + NOT_HERE);
919         return;
920 #endif /* ENABLE_AUTOLOGIN */
921
922         if (config.c_disable_newu) {
923                 cprintf("%d Self-service user account creation "
924                         "is disabled on this system.\n", ERROR + NOT_HERE);
925                 return;
926         }
927
928         if (CC->logged_in) {
929                 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
930                 return;
931         }
932         if (CC->nologin) {
933                 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
934                         ERROR + MAX_SESSIONS_EXCEEDED,
935                         config.c_nodename, config.c_maxsessions);
936         }
937         extract_token(username, cmdbuf, 0, '|', sizeof username);
938         username[25] = 0;
939         strproc(username);
940
941         if (strlen(username) == 0) {
942                 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
943                 return;
944         }
945
946         if ((!strcasecmp(username, "bbs")) ||
947             (!strcasecmp(username, "new")) ||
948             (!strcasecmp(username, "."))) {
949                 cprintf("%d '%s' is an invalid login name.\n", ERROR + ILLEGAL_VALUE, username);
950                 return;
951         }
952
953         a = create_user(username, 1);
954
955         if (a == 0) {
956                 session_startup();
957                 logged_in_response();
958         } else if (a == ERROR + ALREADY_EXISTS) {
959                 cprintf("%d '%s' already exists.\n",
960                         ERROR + ALREADY_EXISTS, username);
961                 return;
962         } else if (a == ERROR + INTERNAL_ERROR) {
963                 cprintf("%d Internal error - user record disappeared?\n",
964                         ERROR + INTERNAL_ERROR);
965                 return;
966         } else {
967                 cprintf("%d unknown error\n", ERROR + INTERNAL_ERROR);
968         }
969 }
970
971
972
973 /*
974  * set password
975  */
976 void cmd_setp(char *new_pw)
977 {
978         if (CtdlAccessCheck(ac_logged_in)) {
979                 return;
980         }
981         if ( (CC->user.uid != CTDLUID) && (CC->user.uid != (-1)) ) {
982                 cprintf("%d Not allowed.  Use the 'passwd' command.\n", ERROR + NOT_HERE);
983                 return;
984         }
985         strproc(new_pw);
986         if (strlen(new_pw) == 0) {
987                 cprintf("%d Password unchanged.\n", CIT_OK);
988                 return;
989         }
990         lgetuser(&CC->user, CC->curr_user);
991         safestrncpy(CC->user.password, new_pw, sizeof(CC->user.password));
992         lputuser(&CC->user);
993         cprintf("%d Password changed.\n", CIT_OK);
994         lprintf(CTDL_INFO, "Password changed for user <%s>\n", CC->curr_user);
995         PerformSessionHooks(EVT_SETPASS);
996 }
997
998
999 /*
1000  * cmd_creu() - administratively create a new user account (do not log in to it)
1001  */
1002 void cmd_creu(char *cmdbuf)
1003 {
1004         int a;
1005         char username[26];
1006         char password[32];
1007         struct ctdluser tmp;
1008
1009         if (CtdlAccessCheck(ac_aide)) {
1010                 return;
1011         }
1012
1013         extract_token(username, cmdbuf, 0, '|', sizeof username);
1014         extract_token(password, cmdbuf, 1, '|', sizeof password);
1015         username[25] = 0;
1016         password[31] = 0;
1017         strproc(username);
1018         strproc(password);
1019
1020         if (strlen(username) == 0) {
1021                 cprintf("%d You must supply a user name.\n", ERROR + USERNAME_REQUIRED);
1022                 return;
1023         }
1024
1025         a = create_user(username, 0);
1026
1027         if (a == 0) {
1028                 if (strlen(password) > 0) {
1029                         lgetuser(&tmp, username);
1030                         safestrncpy(tmp.password, password, sizeof(tmp.password));
1031                         lputuser(&tmp);
1032                 }
1033                 cprintf("%d User '%s' created %s.\n", CIT_OK, username,
1034                                 (strlen(password) > 0) ? "and password set" :
1035                                 "with no password");
1036                 return;
1037         } else if (a == ERROR + ALREADY_EXISTS) {
1038                 cprintf("%d '%s' already exists.\n",
1039                         ERROR + ALREADY_EXISTS, username);
1040                 return;
1041         } else {
1042                 cprintf("%d An error occured creating the user account.\n", ERROR + INTERNAL_ERROR);
1043         }
1044 }
1045
1046
1047
1048 /*
1049  * get user parameters
1050  */
1051 void cmd_getu(void)
1052 {
1053
1054         if (CtdlAccessCheck(ac_logged_in))
1055                 return;
1056
1057         getuser(&CC->user, CC->curr_user);
1058         cprintf("%d %d|%d|%d|\n",
1059                 CIT_OK,
1060                 CC->user.USscreenwidth,
1061                 CC->user.USscreenheight,
1062                 (CC->user.flags & US_USER_SET)
1063             );
1064 }
1065
1066 /*
1067  * set user parameters
1068  */
1069 void cmd_setu(char *new_parms)
1070 {
1071         if (CtdlAccessCheck(ac_logged_in))
1072                 return;
1073
1074         if (num_parms(new_parms) < 3) {
1075                 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
1076                 return;
1077         }
1078         lgetuser(&CC->user, CC->curr_user);
1079         CC->user.USscreenwidth = extract_int(new_parms, 0);
1080         CC->user.USscreenheight = extract_int(new_parms, 1);
1081         CC->user.flags = CC->user.flags & (~US_USER_SET);
1082         CC->user.flags = CC->user.flags |
1083             (extract_int(new_parms, 2) & US_USER_SET);
1084
1085         lputuser(&CC->user);
1086         cprintf("%d Ok\n", CIT_OK);
1087 }
1088
1089 /*
1090  * set last read pointer
1091  */
1092 void cmd_slrp(char *new_ptr)
1093 {
1094         long newlr;
1095         struct visit vbuf;
1096         struct visit original_vbuf;
1097
1098         if (CtdlAccessCheck(ac_logged_in)) {
1099                 return;
1100         }
1101
1102         if (!strncasecmp(new_ptr, "highest", 7)) {
1103                 newlr = CC->room.QRhighest;
1104         } else {
1105                 newlr = atol(new_ptr);
1106         }
1107
1108         lgetuser(&CC->user, CC->curr_user);
1109
1110         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1111         memcpy(&original_vbuf, &vbuf, sizeof(struct visit));
1112         vbuf.v_lastseen = newlr;
1113         snprintf(vbuf.v_seen, sizeof vbuf.v_seen, "*:%ld", newlr);
1114
1115         /* Only rewrite the record if it changed */
1116         if ( (vbuf.v_lastseen != original_vbuf.v_lastseen)
1117            || (strcmp(vbuf.v_seen, original_vbuf.v_seen)) ) {
1118                 CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1119         }
1120
1121         lputuser(&CC->user);
1122         cprintf("%d %ld\n", CIT_OK, newlr);
1123 }
1124
1125
1126 void cmd_seen(char *argbuf) {
1127         long target_msgnum = 0L;
1128         int target_setting = 0;
1129
1130         if (CtdlAccessCheck(ac_logged_in)) {
1131                 return;
1132         }
1133
1134         if (num_parms(argbuf) != 2) {
1135                 cprintf("%d Invalid parameters\n", ERROR + ILLEGAL_VALUE);
1136                 return;
1137         }
1138
1139         target_msgnum = extract_long(argbuf, 0);
1140         target_setting = extract_int(argbuf, 1);
1141
1142         CtdlSetSeen(&target_msgnum, 1, target_setting,
1143                         ctdlsetseen_seen, NULL, NULL);
1144         cprintf("%d OK\n", CIT_OK);
1145 }
1146
1147
1148 void cmd_gtsn(char *argbuf) {
1149         char buf[SIZ];
1150
1151         if (CtdlAccessCheck(ac_logged_in)) {
1152                 return;
1153         }
1154
1155         CtdlGetSeen(buf, ctdlsetseen_seen);
1156         cprintf("%d %s\n", CIT_OK, buf);
1157 }
1158
1159
1160 /*
1161  * API function for cmd_invt_kick() and anything else that needs to
1162  * invite or kick out a user to/from a room.
1163  * 
1164  * Set iuser to the name of the user, and op to 1=invite or 0=kick
1165  */
1166 int CtdlInvtKick(char *iuser, int op) {
1167         struct ctdluser USscratch;
1168         struct visit vbuf;
1169         char bbb[SIZ];
1170
1171         if (getuser(&USscratch, iuser) != 0) {
1172                 return(1);
1173         }
1174
1175         CtdlGetRelationship(&vbuf, &USscratch, &CC->room);
1176         if (op == 1) {
1177                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
1178                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
1179         }
1180         if (op == 0) {
1181                 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1182                 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
1183         }
1184         CtdlSetRelationship(&vbuf, &USscratch, &CC->room);
1185
1186         /* post a message in Aide> saying what we just did */
1187         snprintf(bbb, sizeof bbb, "%s has been %s \"%s\" by %s.\n",
1188                 iuser,
1189                 ((op == 1) ? "invited to" : "kicked out of"),
1190                 CC->room.QRname,
1191                 CC->user.fullname);
1192         aide_message(bbb,"User Admin Message");
1193
1194         return(0);
1195 }
1196
1197
1198 /*
1199  * INVT and KICK commands
1200  */
1201 void cmd_invt_kick(char *iuser, int op) {
1202
1203         /*
1204          * These commands are only allowed by aides, room aides,
1205          * and room namespace owners
1206          */
1207         if (is_room_aide()
1208            || (atol(CC->room.QRname) == CC->user.usernum) ) {
1209                 /* access granted */
1210         } else {
1211                 /* access denied */
1212                 cprintf("%d Higher access or room ownership required.\n",
1213                         ERROR + HIGHER_ACCESS_REQUIRED);
1214                 return;
1215         }
1216
1217         if (!strncasecmp(CC->room.QRname, config.c_baseroom,
1218                          ROOMNAMELEN)) {
1219                 cprintf("%d Can't add/remove users from this room.\n",
1220                         ERROR + NOT_HERE);
1221                 return;
1222         }
1223
1224         if (CtdlInvtKick(iuser, op) != 0) {
1225                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1226                 return;
1227         }
1228
1229         cprintf("%d %s %s %s.\n",
1230                 CIT_OK, iuser,
1231                 ((op == 1) ? "invited to" : "kicked out of"),
1232                 CC->room.QRname);
1233         return;
1234 }
1235
1236
1237 /*
1238  * Forget (Zap) the current room (API call)
1239  * Returns 0 on success
1240  */
1241 int CtdlForgetThisRoom(void) {
1242         struct visit vbuf;
1243
1244         /* On some systems, Aides are not allowed to forget rooms */
1245         if (is_aide() && (config.c_aide_zap == 0)
1246            && ((CC->room.QRflags & QR_MAILBOX) == 0)  ) {
1247                 return(1);
1248         }
1249
1250         lgetuser(&CC->user, CC->curr_user);
1251         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1252
1253         vbuf.v_flags = vbuf.v_flags | V_FORGET;
1254         vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
1255
1256         CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1257         lputuser(&CC->user);
1258
1259         /* Return to the Lobby, so we don't end up in an undefined room */
1260         usergoto(config.c_baseroom, 0, 0, NULL, NULL);
1261         return(0);
1262
1263 }
1264
1265
1266 /*
1267  * forget (Zap) the current room
1268  */
1269 void cmd_forg(void)
1270 {
1271
1272         if (CtdlAccessCheck(ac_logged_in)) {
1273                 return;
1274         }
1275
1276         if (CtdlForgetThisRoom() == 0) {
1277                 cprintf("%d Ok\n", CIT_OK);
1278         }
1279         else {
1280                 cprintf("%d You may not forget this room.\n", ERROR + NOT_HERE);
1281         }
1282 }
1283
1284 /*
1285  * Get Next Unregistered User
1286  */
1287 void cmd_gnur(void)
1288 {
1289         struct cdbdata *cdbus;
1290         struct ctdluser usbuf;
1291
1292         if (CtdlAccessCheck(ac_aide)) {
1293                 return;
1294         }
1295
1296         if ((CitControl.MMflags & MM_VALID) == 0) {
1297                 cprintf("%d There are no unvalidated users.\n", CIT_OK);
1298                 return;
1299         }
1300
1301         /* There are unvalidated users.  Traverse the user database,
1302          * and return the first user we find that needs validation.
1303          */
1304         cdb_rewind(CDB_USERS);
1305         while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1306                 memset(&usbuf, 0, sizeof(struct ctdluser));
1307                 memcpy(&usbuf, cdbus->ptr,
1308                        ((cdbus->len > sizeof(struct ctdluser)) ?
1309                         sizeof(struct ctdluser) : cdbus->len));
1310                 cdb_free(cdbus);
1311                 if ((usbuf.flags & US_NEEDVALID)
1312                     && (usbuf.axlevel > 0)) {
1313                         cprintf("%d %s\n", MORE_DATA, usbuf.fullname);
1314                         cdb_close_cursor(CDB_USERS);
1315                         return;
1316                 }
1317         }
1318
1319         /* If we get to this point, there are no more unvalidated users.
1320          * Therefore we clear the "users need validation" flag.
1321          */
1322
1323         begin_critical_section(S_CONTROL);
1324         get_control();
1325         CitControl.MMflags = CitControl.MMflags & (~MM_VALID);
1326         put_control();
1327         end_critical_section(S_CONTROL);
1328         cprintf("%d *** End of registration.\n", CIT_OK);
1329
1330
1331 }
1332
1333
1334 /*
1335  * validate a user
1336  */
1337 void cmd_vali(char *v_args)
1338 {
1339         char user[128];
1340         int newax;
1341         struct ctdluser userbuf;
1342
1343         extract_token(user, v_args, 0, '|', sizeof user);
1344         newax = extract_int(v_args, 1);
1345
1346         if (CtdlAccessCheck(ac_aide)) {
1347                 return;
1348         }
1349
1350         if (lgetuser(&userbuf, user) != 0) {
1351                 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, user);
1352                 return;
1353         }
1354
1355         userbuf.axlevel = newax;
1356         userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
1357
1358         lputuser(&userbuf);
1359
1360         /* If the access level was set to zero, delete the user */
1361         if (newax == 0) {
1362                 if (purge_user(user) == 0) {
1363                         cprintf("%d %s Deleted.\n", CIT_OK, userbuf.fullname);
1364                         return;
1365                 }
1366         }
1367         cprintf("%d User '%s' validated.\n", CIT_OK, userbuf.fullname);
1368 }
1369
1370
1371
1372 /* 
1373  *  Traverse the user file...
1374  */
1375 void ForEachUser(void (*CallBack) (struct ctdluser * EachUser, void *out_data),
1376                  void *in_data)
1377 {
1378         struct ctdluser usbuf;
1379         struct cdbdata *cdbus;
1380
1381         cdb_rewind(CDB_USERS);
1382
1383         while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) {
1384                 memset(&usbuf, 0, sizeof(struct ctdluser));
1385                 memcpy(&usbuf, cdbus->ptr,
1386                        ((cdbus->len > sizeof(struct ctdluser)) ?
1387                         sizeof(struct ctdluser) : cdbus->len));
1388                 cdb_free(cdbus);
1389                 (*CallBack) (&usbuf, in_data);
1390         }
1391 }
1392
1393
1394 /*
1395  * List one user (this works with cmd_list)
1396  */
1397 void ListThisUser(struct ctdluser *usbuf, void *data)
1398 {
1399         char *searchstring;
1400
1401         searchstring = (char *)data;
1402         if (bmstrcasestr(usbuf->fullname, searchstring) == NULL) {
1403                 return;
1404         }
1405
1406         if (usbuf->axlevel > 0) {
1407                 if ((CC->user.axlevel >= 6)
1408                     || ((usbuf->flags & US_UNLISTED) == 0)
1409                     || ((CC->internal_pgm))) {
1410                         cprintf("%s|%d|%ld|%ld|%ld|%ld|",
1411                                 usbuf->fullname,
1412                                 usbuf->axlevel,
1413                                 usbuf->usernum,
1414                                 (long)usbuf->lastcall,
1415                                 usbuf->timescalled,
1416                                 usbuf->posted);
1417                         if (CC->user.axlevel >= 6)
1418                                 cprintf("%s", usbuf->password);
1419                         cprintf("\n");
1420                 }
1421         }
1422 }
1423
1424 /* 
1425  *  List users (searchstring may be empty to list all users)
1426  */
1427 void cmd_list(char *cmdbuf)
1428 {
1429         char searchstring[256];
1430         extract_token(searchstring, cmdbuf, 0, '|', sizeof searchstring);
1431         striplt(searchstring);
1432         cprintf("%d \n", LISTING_FOLLOWS);
1433         ForEachUser(ListThisUser, (void *)searchstring );
1434         cprintf("000\n");
1435 }
1436
1437
1438
1439
1440 /*
1441  * assorted info we need to check at login
1442  */
1443 void cmd_chek(void)
1444 {
1445         int mail = 0;
1446         int regis = 0;
1447         int vali = 0;
1448
1449         if (CtdlAccessCheck(ac_logged_in)) {
1450                 return;
1451         }
1452
1453         getuser(&CC->user, CC->curr_user);      /* no lock is needed here */
1454         if ((REGISCALL != 0) && ((CC->user.flags & US_REGIS) == 0))
1455                 regis = 1;
1456
1457         if (CC->user.axlevel >= 6) {
1458                 get_control();
1459                 if (CitControl.MMflags & MM_VALID)
1460                         vali = 1;
1461         }
1462
1463         /* check for mail */
1464         mail = InitialMailCheck();
1465
1466         cprintf("%d %d|%d|%d|%s|\n", CIT_OK, mail, regis, vali, CC->cs_inet_email);
1467 }
1468
1469
1470 /*
1471  * check to see if a user exists
1472  */
1473 void cmd_qusr(char *who)
1474 {
1475         struct ctdluser usbuf;
1476
1477         if (getuser(&usbuf, who) == 0) {
1478                 cprintf("%d %s\n", CIT_OK, usbuf.fullname);
1479         } else {
1480                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1481         }
1482 }
1483
1484
1485 /*
1486  * Administrative Get User Parameters
1487  */
1488 void cmd_agup(char *cmdbuf)
1489 {
1490         struct ctdluser usbuf;
1491         char requested_user[128];
1492
1493         if (CtdlAccessCheck(ac_aide)) {
1494                 return;
1495         }
1496
1497         extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1498         if (getuser(&usbuf, requested_user) != 0) {
1499                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1500                 return;
1501         }
1502         cprintf("%d %s|%s|%u|%ld|%ld|%d|%ld|%ld|%d\n",
1503                 CIT_OK,
1504                 usbuf.fullname,
1505                 usbuf.password,
1506                 usbuf.flags,
1507                 usbuf.timescalled,
1508                 usbuf.posted,
1509                 (int) usbuf.axlevel,
1510                 usbuf.usernum,
1511                 (long)usbuf.lastcall,
1512                 usbuf.USuserpurge);
1513 }
1514
1515
1516
1517 /*
1518  * Administrative Set User Parameters
1519  */
1520 void cmd_asup(char *cmdbuf)
1521 {
1522         struct ctdluser usbuf;
1523         char requested_user[128];
1524         char notify[SIZ];
1525         int np;
1526         int newax;
1527         int deleted = 0;
1528
1529         if (CtdlAccessCheck(ac_aide))
1530                 return;
1531
1532         extract_token(requested_user, cmdbuf, 0, '|', sizeof requested_user);
1533         if (lgetuser(&usbuf, requested_user) != 0) {
1534                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1535                 return;
1536         }
1537         np = num_parms(cmdbuf);
1538         if (np > 1)
1539                 extract_token(usbuf.password, cmdbuf, 1, '|', sizeof usbuf.password);
1540         if (np > 2)
1541                 usbuf.flags = extract_int(cmdbuf, 2);
1542         if (np > 3)
1543                 usbuf.timescalled = extract_int(cmdbuf, 3);
1544         if (np > 4)
1545                 usbuf.posted = extract_int(cmdbuf, 4);
1546         if (np > 5) {
1547                 newax = extract_int(cmdbuf, 5);
1548                 if ((newax >= 0) && (newax <= 6)) {
1549                         usbuf.axlevel = extract_int(cmdbuf, 5);
1550                 }
1551         }
1552         if (np > 7) {
1553                 usbuf.lastcall = extract_long(cmdbuf, 7);
1554         }
1555         if (np > 8) {
1556                 usbuf.USuserpurge = extract_int(cmdbuf, 8);
1557         }
1558         lputuser(&usbuf);
1559         if (usbuf.axlevel == 0) {
1560                 if (purge_user(requested_user) == 0) {
1561                         deleted = 1;
1562                 }
1563         }
1564
1565         if (deleted) {
1566                 sprintf(notify, "User \"%s\" has been deleted by %s.\n",
1567                         usbuf.fullname, CC->user.fullname);
1568                 aide_message(notify, "User Deletion Message");
1569         }
1570
1571         cprintf("%d Ok", CIT_OK);
1572         if (deleted)
1573                 cprintf(" (%s deleted)", requested_user);
1574         cprintf("\n");
1575 }
1576
1577
1578
1579 /*
1580  * Check to see if the user who we just sent mail to is logged in.  If yes,
1581  * bump the 'new mail' counter for their session.  That enables them to
1582  * receive a new mail notification without having to hit the database.
1583  */
1584 void BumpNewMailCounter(long which_user) {
1585         struct CitContext *ptr;
1586
1587         begin_critical_section(S_SESSION_TABLE);
1588
1589         for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1590                 if (ptr->user.usernum == which_user) {
1591                         ptr->newmail += 1;
1592                 }
1593         }
1594
1595         end_critical_section(S_SESSION_TABLE);
1596 }
1597
1598
1599 /*
1600  * Count the number of new mail messages the user has
1601  */
1602 int NewMailCount()
1603 {
1604         int num_newmsgs = 0;
1605
1606         num_newmsgs = CC->newmail;
1607         CC->newmail = 0;
1608
1609         return (num_newmsgs);
1610 }
1611
1612
1613 /*
1614  * Count the number of new mail messages the user has
1615  */
1616 int InitialMailCheck()
1617 {
1618         int num_newmsgs = 0;
1619         int a;
1620         char mailboxname[ROOMNAMELEN];
1621         struct ctdlroom mailbox;
1622         struct visit vbuf;
1623         struct cdbdata *cdbfr;
1624         long *msglist = NULL;
1625         int num_msgs = 0;
1626
1627         MailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
1628         if (getroom(&mailbox, mailboxname) != 0)
1629                 return (0);
1630         CtdlGetRelationship(&vbuf, &CC->user, &mailbox);
1631
1632         cdbfr = cdb_fetch(CDB_MSGLISTS, &mailbox.QRnumber, sizeof(long));
1633
1634         if (cdbfr != NULL) {
1635                 msglist = malloc(cdbfr->len);
1636                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
1637                 num_msgs = cdbfr->len / sizeof(long);
1638                 cdb_free(cdbfr);
1639         }
1640         if (num_msgs > 0)
1641                 for (a = 0; a < num_msgs; ++a) {
1642                         if (msglist[a] > 0L) {
1643                                 if (msglist[a] > vbuf.v_lastseen) {
1644                                         ++num_newmsgs;
1645                                 }
1646                         }
1647                 }
1648         if (msglist != NULL)
1649                 free(msglist);
1650
1651         return (num_newmsgs);
1652 }
1653
1654
1655
1656 /*
1657  * Set the preferred view for the current user/room combination
1658  */
1659 void cmd_view(char *cmdbuf) {
1660         int requested_view;
1661         struct visit vbuf;
1662
1663         if (CtdlAccessCheck(ac_logged_in)) {
1664                 return;
1665         }
1666
1667         requested_view = extract_int(cmdbuf, 0);
1668
1669         CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
1670         vbuf.v_view = requested_view;
1671         CtdlSetRelationship(&vbuf, &CC->user, &CC->room);
1672         
1673         cprintf("%d ok\n", CIT_OK);
1674 }