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