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