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