]> code.citadel.org Git - citadel.git/blob - citadel/user_ops.c
Removed a few more references to usersupp.lastseen[]
[citadel.git] / citadel / user_ops.c
1 /* needed to properly enable crypt() stuff on some systems */
2 #define _XOPEN_SOURCE
3 /* needed for str[n]casecmp() on some systems if the above is defined */
4 #define _XOPEN_SOURCE_EXTENDED
5 /* needed to enable threads on some systems if the above are defined */
6 #define _POSIX_C_SOURCE 199506L
7
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <signal.h>
13 #include <pwd.h>
14 #include <sys/types.h>
15 #include <sys/time.h>
16 #include <string.h>
17 #include <syslog.h>
18 #include <limits.h>
19 #include <pthread.h>
20 #include "citadel.h"
21 #include "server.h"
22 #include "database.h"
23 #include "user_ops.h"
24 #include "sysdep_decls.h"
25 #include "support.h"
26 #include "room_ops.h"
27 #include "logging.h"
28 #include "file_ops.h"
29 #include "control.h"
30 #include "msgbase.h"
31 #include "config.h"
32 #include "dynloader.h"
33 #include "sysdep.h"
34
35
36 /*
37  * getuser()  -  retrieve named user into supplied buffer.
38  *               returns 0 on success
39  */
40 int getuser(struct usersupp *usbuf, char name[]) {
41
42         char lowercase_name[32];
43         int a;
44         struct cdbdata *cdbus;
45
46         bzero(usbuf, sizeof(struct usersupp));
47         for (a=0; a<=strlen(name); ++a) {
48                 lowercase_name[a] = tolower(name[a]);
49                 }
50
51         cdbus = cdb_fetch(CDB_USERSUPP, lowercase_name, strlen(lowercase_name));
52         if (cdbus == NULL) {
53                 return(1);      /* user not found */
54                 }
55
56         memcpy(usbuf, cdbus->ptr,
57                 ( (cdbus->len > sizeof(struct usersupp)) ?
58                 sizeof(struct usersupp) : cdbus->len) );
59         cdb_free(cdbus);
60         return(0);
61         }
62
63
64 /*
65  * lgetuser()  -  same as getuser() but locks the record
66  */
67 int lgetuser(struct usersupp *usbuf, char *name)
68 {
69         int retcode;
70
71         retcode = getuser(usbuf,name);
72         if (retcode == 0) {
73                 begin_critical_section(S_USERSUPP);
74                 }
75         return(retcode);
76         }
77
78
79 /*
80  * putuser()  -  write user buffer into the correct place on disk
81  */
82 void putuser(struct usersupp *usbuf, char *name)
83 {
84         char lowercase_name[32];
85         int a;
86
87         for (a=0; a<=strlen(name); ++a) {
88                 lowercase_name[a] = tolower(name[a]);
89                 }
90
91         cdb_store(CDB_USERSUPP,
92                 lowercase_name, strlen(lowercase_name),
93                 usbuf, sizeof(struct usersupp));
94
95         }
96
97
98 /*
99  * lputuser()  -  same as putuser() but locks the record
100  */
101 void lputuser(struct usersupp *usbuf, char *name) {
102         putuser(usbuf,name);
103         end_critical_section(S_USERSUPP);
104         }
105
106
107 /*
108  * Define a relationship between a user and a room
109  */
110 void CtdlSetRelationship(struct visit *newvisit,
111                         struct usersupp *rel_user,
112                         struct quickroom *rel_room) {
113
114         struct cdbdata *cdbvisit;
115         struct visit *visits;
116         int num_visits;
117         int a;
118         int replaced = 0;
119
120         cdbvisit = cdb_fetch(CDB_VISIT, &rel_user->usernum, sizeof(long));
121         if (cdbvisit != NULL) {
122                 num_visits = cdbvisit->len / sizeof(struct visit);
123                 visits = (struct visit *)
124                         malloc(num_visits * sizeof(struct visit));
125                 memcpy(visits, cdbvisit->ptr,
126                         (num_visits * sizeof(struct visit)));
127                 cdb_free(cdbvisit);
128                 }
129         else {
130                 num_visits = 0;
131                 visits = NULL;
132                 }
133
134         /* Replace an existing relationship if possible */
135         if (num_visits > 0) for (a=0; a<num_visits; ++a) {
136                 if ( (!strcasecmp(visits[a].v_roomname, rel_room->QRname))
137                    && (visits[a].v_generation == rel_room->QRgen) ) {
138                         memcpy(&visits[a], newvisit, sizeof(struct visit));
139                         replaced = 1;
140                         }
141                 }
142
143         /* Otherwise, define a new one */
144         if (replaced == 0) {
145                 ++num_visits;
146                 visits = realloc(visits, 
147                         (num_visits * sizeof(struct visit)));
148                 memcpy(&visits[num_visits-1], newvisit, sizeof(struct visit));
149                 }
150
151         /* Now write the relationship back to disk */
152         cdb_store(CDB_VISIT,
153                 &rel_user->usernum, sizeof(long),
154                 visits,
155                 (num_visits * sizeof(struct visit)));
156         free(visits);
157         }
158
159 /*
160  * Locate a relationship between a user and a room
161  */
162 void CtdlGetRelationship(struct visit *vbuf,
163                         struct usersupp *rel_user,
164                         struct quickroom *rel_room) {
165
166         struct cdbdata *cdbvisit;
167         struct visit *visits;
168         int num_visits;
169         int a;
170
171         bzero(vbuf, sizeof(struct visit));
172         strcpy(vbuf->v_roomname, rel_room->QRname);
173         vbuf->v_generation = rel_room->QRgen;
174
175         cdbvisit = cdb_fetch(CDB_VISIT, &rel_user->usernum, sizeof(long));
176         if (cdbvisit != NULL) {
177                 if ((num_visits = cdbvisit->len / sizeof(struct visit)) == 0) {
178                         cdb_free(cdbvisit);
179                         return;
180                 }
181                 visits = (struct visit *)
182                         malloc(num_visits * sizeof(struct visit));
183                 memcpy(visits, cdbvisit->ptr,
184                         (num_visits * sizeof(struct visit)));
185                 cdb_free(cdbvisit);
186                 }
187         else return;
188
189         for (a=0; a<num_visits; ++a) {
190         
191                 if ( (!strcasecmp(visits[a].v_roomname, rel_room->QRname))
192                    && (visits[a].v_generation == rel_room->QRgen) ) {
193                         memcpy(vbuf, &visits[a], sizeof(struct visit));
194                         }
195                 }
196         
197         free(visits);
198         }
199         
200         
201 /*
202  * Is the user currently logged in an Aide?
203  */
204 int is_aide(void) {
205         if (CC->usersupp.axlevel >= 6) return(1);
206         else return(0);
207         }
208
209
210 /*
211  * Is the user currently logged in an Aide *or* the room aide for this room?
212  */
213 int is_room_aide(void) {
214         if ( (CC->usersupp.axlevel >= 6)
215            || (CC->quickroom.QRroomaide == CC->usersupp.usernum) ) {
216                 return(1);
217                 }
218         else {
219                 return(0);
220                 }
221         }
222
223 /*
224  * getuserbynumber()  -  get user by number
225  *                       returns 0 if user was found
226  */
227 int getuserbynumber(struct usersupp *usbuf, long int number)
228 {
229         struct cdbdata *cdbus;
230
231         cdb_rewind(CDB_USERSUPP);
232
233         while(cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
234                 bzero(usbuf, sizeof(struct usersupp));
235                 memcpy(usbuf, cdbus->ptr,
236                         ( (cdbus->len > sizeof(struct usersupp)) ?
237                         sizeof(struct usersupp) : cdbus->len) );
238                 cdb_free(cdbus);
239                 if (usbuf->usernum == number) {
240                         return(0);
241                         }
242                 }
243         return(-1);
244         }
245
246
247 /*
248  * USER cmd
249  */
250 void cmd_user(char *cmdbuf)
251 {
252         char username[256];
253         char autoname[256];
254         int found_user = 0;
255         struct passwd *p;
256         int a;
257
258         extract(username,cmdbuf,0);
259         username[25] = 0;
260         strproc(username);
261
262         if ((CC->logged_in)) {
263                 cprintf("%d Already logged in.\n",ERROR);
264                 return;
265                 }
266
267         found_user = getuser(&CC->usersupp,username);
268         if (found_user != 0) {
269                 p = (struct passwd *)getpwnam(username);
270                 if (p!=NULL) {
271                         strcpy(autoname,p->pw_gecos);
272                         for (a=0; a<strlen(autoname); ++a)
273                                 if (autoname[a]==',') autoname[a]=0;
274                         found_user = getuser(&CC->usersupp,autoname);
275                         }
276                 }
277         if (found_user == 0) {
278                 if (((CC->nologin)) && (CC->usersupp.axlevel < 6)) {
279                         cprintf("%d %s: Too many users are already online (maximum is %d)\n",
280                         ERROR+MAX_SESSIONS_EXCEEDED,
281                         config.c_nodename,config.c_maxsessions);
282                         }
283                 else {
284                         strcpy(CC->curr_user,CC->usersupp.fullname);
285                         cprintf("%d Password required for %s\n",
286                                 MORE_DATA,CC->curr_user);
287                         }
288                 }
289         else {
290                 cprintf("%d %s not found.\n",ERROR,username);
291                 }
292         }
293
294
295
296 /*
297  * session startup code which is common to both cmd_pass() and cmd_newu()
298  */
299 void session_startup(void) {
300         int a;
301         struct quickroom qr;
302
303         syslog(LOG_NOTICE,"user <%s> logged in",CC->curr_user);
304
305         lgetuser(&CC->usersupp,CC->curr_user);
306         ++(CC->usersupp.timescalled);
307         CC->fake_username[0] = '\0';
308         CC->fake_postname[0] = '\0';
309         CC->fake_hostname[0] = '\0';
310         CC->fake_roomname[0] = '\0';
311         CC->last_pager[0] = '\0';
312         time(&CC->usersupp.lastcall);
313
314         /* If this user's name is the name of the system administrator
315          * (as specified in setup), automatically assign access level 6.
316          */
317         if (!strcasecmp(CC->usersupp.fullname, config.c_sysadm)) {
318                 CC->usersupp.axlevel = 6;
319                 }
320
321  /******************************************************************************/
322  /* FIX  It is safe to remove this segment during the cutover                  */
323         for (a=0; a<MAXROOMS; ++a) {
324                 getroom(&qr,a);
325                 if (CC->usersupp.generation[a] != qr.QRgen)
326                                         CC->usersupp.generation[a]=(-1);
327                 if (CC->usersupp.forget[a] != qr.QRgen)
328                                         CC->usersupp.forget[a]=(-1);
329                 }
330  /******************************************************************************/
331
332         lputuser(&CC->usersupp,CC->curr_user);
333
334         /* Run any cleanup routines registered by loadable modules */
335         PerformSessionHooks(EVT_LOGIN);
336
337         cprintf("%d %s|%d|%d|%d|%u|%ld\n",OK,CC->usersupp.fullname,CC->usersupp.axlevel,
338                 CC->usersupp.timescalled,CC->usersupp.posted,CC->usersupp.flags,
339                 CC->usersupp.usernum);
340         usergoto(0,0);          /* Enter the lobby */   
341         rec_log(CL_LOGIN,CC->curr_user);
342         }
343
344
345 /* 
346  * misc things to be taken care of when a user is logged out
347  */
348 void logout(struct CitContext *who)
349 {
350         who->logged_in = 0;
351         if (who->download_fp != NULL) {
352                 fclose(who->download_fp);
353                 who->download_fp = NULL;
354                 }
355         if (who->upload_fp != NULL) {
356                 abort_upl(who);
357                 }
358
359         /* Do modular stuff... */
360         PerformSessionHooks(EVT_LOGOUT);
361         }
362
363
364 void cmd_pass(char *buf)
365 {
366         char password[256];
367         int code;
368         struct passwd *p;
369
370         extract(password,buf,0);
371
372         if ((CC->logged_in)) {
373                 cprintf("%d Already logged in.\n",ERROR);
374                 return;
375                 }
376         if (!strcmp(CC->curr_user,"")) {
377                 cprintf("%d You must send a name with USER first.\n",ERROR);
378                 return;
379                 }
380         if (getuser(&CC->usersupp,CC->curr_user)) {
381                 cprintf("%d Can't find user record!\n",ERROR+INTERNAL_ERROR);
382                 return;
383                 }
384
385         code = (-1);
386         if (CC->usersupp.USuid == BBSUID) {
387                 strproc(password);
388                 strproc(CC->usersupp.password);
389                 code = strcasecmp(CC->usersupp.password,password);
390                 }
391         else {
392                 p = (struct passwd *)getpwuid(CC->usersupp.USuid);
393 #ifdef ENABLE_AUTOLOGIN
394                 if (p!=NULL) {
395                         if (!strcmp(p->pw_passwd,
396                            (char *)crypt(password,p->pw_passwd))) {
397                                 code = 0;
398                                 lgetuser(&CC->usersupp, CC->curr_user);
399                                 strcpy(CC->usersupp.password, password);
400                                 lputuser(&CC->usersupp, CC->curr_user);
401                                 }
402                         }
403 #endif
404                 }
405
406         if (!code) {
407                 (CC->logged_in) = 1;
408                 session_startup();
409                 }
410         else {
411                 cprintf("%d Wrong password.\n",ERROR);
412                 rec_log(CL_BADPW,CC->curr_user);
413                 }
414         }
415
416
417 /*
418  * Delete a user record *and* all of its related resources.
419  */
420 int purge_user(char *pname) {
421         char filename[64];
422         struct usersupp usbuf;
423         int a;
424         struct cdbdata *cdbmb;
425         long *mailbox;
426         int num_mails;
427
428         if (getuser(&usbuf, pname) != 0) {
429                 lprintf(5, "Cannot purge user <%s> - not found\n", pname);
430                 return(ERROR+NO_SUCH_USER);
431                 }
432
433         /* FIX   Don't delete a user who is currently logged in. */
434
435         /* Perform any purge functions registered by server extensions */
436         PerformUserHooks(usbuf.fullname, usbuf.usernum, EVT_PURGEUSER);
437
438         /* delete any messages in the user's mailbox */
439         cdbmb = cdb_fetch(CDB_MAILBOXES, &usbuf.usernum, sizeof(long));
440         if (cdbmb != NULL) {
441                 num_mails = cdbmb->len / sizeof(long);
442                 mailbox = (long *) cdbmb->ptr;
443                 if (num_mails > 0) for (a=0; a<num_mails; ++a) {
444                         cdb_delete(CDB_MSGMAIN, &mailbox[a], sizeof(long));
445                         }
446                 cdb_free(cdbmb);
447                 /* now delete the mailbox itself */
448                 cdb_delete(CDB_MAILBOXES, &usbuf.usernum, sizeof(long));
449                 }
450
451         /* delete any existing user/room relationships */
452         cdb_delete(CDB_VISIT, &usbuf.usernum, sizeof(long));
453
454         /* delete the userlog entry */
455         cdb_delete(CDB_USERSUPP, pname, strlen(pname));
456
457         /* remove the user's bio file */        
458         sprintf(filename, "./bio/%ld", usbuf.usernum);
459         unlink(filename);
460
461         /* remove the user's picture */
462         sprintf(filename, "./userpics/%ld.gif", usbuf.usernum);
463         unlink(filename);
464
465         return(0);
466         }
467
468
469 /*
470  * create_user()  -  back end processing to create a new user
471  */
472 int create_user(char *newusername)
473 {
474         struct usersupp usbuf;
475         int a;
476         struct passwd *p = NULL;
477         char username[64];
478
479         strcpy(username, newusername);
480         strproc(username);
481
482 #ifdef ENABLE_AUTOLOGIN
483         p = (struct passwd *)getpwnam(username);
484 #endif
485         if (p != NULL) {
486                 strcpy(username, p->pw_gecos);
487                 for (a=0; a<strlen(username); ++a) {
488                         if (username[a] == ',') username[a] = 0;
489                         }
490                 CC->usersupp.USuid = p->pw_uid;
491                 }
492         else {
493                 CC->usersupp.USuid = BBSUID;
494                 }
495
496         if (!getuser(&usbuf,username)) {
497                 return(ERROR+ALREADY_EXISTS);
498                 }
499
500         strcpy(CC->curr_user,username);
501         strcpy(CC->usersupp.fullname,username);
502         strcpy(CC->usersupp.password,"");
503         (CC->logged_in) = 1;
504
505         /********************************************************/
506         /* FIX this can safely be removed during the cutover... */
507         for (a=0; a<MAXROOMS; ++a) {
508                 CC->usersupp.generation[a]=(-1);
509                 CC->usersupp.forget[a]=(-1);
510                 }
511         /********************************************************/
512
513         /* These are the default flags on new accounts */
514         CC->usersupp.flags =
515                 US_NEEDVALID|US_LASTOLD|US_DISAPPEAR|US_PAGINATOR|US_FLOORS;
516
517         CC->usersupp.timescalled = 0;
518         CC->usersupp.posted = 0;
519         CC->usersupp.axlevel = config.c_initax;
520         CC->usersupp.USscreenwidth = 80;
521         CC->usersupp.USscreenheight = 24;
522         time(&CC->usersupp.lastcall);
523         strcpy(CC->usersupp.USname, "");
524         strcpy(CC->usersupp.USaddr, "");
525         strcpy(CC->usersupp.UScity, "");
526         strcpy(CC->usersupp.USstate, "");
527         strcpy(CC->usersupp.USzip, "");
528         strcpy(CC->usersupp.USphone, "");
529
530         /* fetch a new user number */
531         CC->usersupp.usernum = get_new_user_number();
532
533         if (CC->usersupp.usernum == 1L) {
534                 CC->usersupp.axlevel = 6;
535                 }
536
537         /* add user to userlog */
538         putuser(&CC->usersupp,CC->curr_user);
539         if (getuser(&CC->usersupp,CC->curr_user)) {
540                 return(ERROR+INTERNAL_ERROR);
541                 }
542         rec_log(CL_NEWUSER,CC->curr_user);
543         return(0);
544         }
545
546
547
548
549 /*
550  * cmd_newu()  -  create a new user account
551  */
552 void cmd_newu(char *cmdbuf)
553 {
554         int a;
555         char username[256];
556
557         if ((CC->logged_in)) {
558                 cprintf("%d Already logged in.\n",ERROR);
559                 return;
560                 }
561
562         if ((CC->nologin)) {
563                 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
564                 ERROR+MAX_SESSIONS_EXCEEDED,
565                 config.c_nodename,config.c_maxsessions);
566                 }
567
568         extract(username,cmdbuf,0);
569         username[25] = 0;
570         strproc(username);
571
572         if (strlen(username)==0) {
573                 cprintf("%d You must supply a user name.\n",ERROR);
574                 return;
575                 }
576
577         a = create_user(username);
578         if ((!strcasecmp(username, "bbs")) ||
579             (!strcasecmp(username, "new")) ||
580             (!strcasecmp(username, ".")))
581         {
582            cprintf("%d '%s' is an invalid login name.\n", ERROR);
583            return;
584         }
585         if (a==ERROR+ALREADY_EXISTS) {
586                 cprintf("%d '%s' already exists.\n",
587                         ERROR+ALREADY_EXISTS,username);
588                 return;
589                 }
590         else if (a==ERROR+INTERNAL_ERROR) {
591                 cprintf("%d Internal error - user record disappeared?\n",
592                         ERROR+INTERNAL_ERROR);
593                 return;
594                 }
595         else if (a==0) {
596                 session_startup();
597                 }
598         else {
599                 cprintf("%d unknown error\n",ERROR);
600                 }
601         rec_log(CL_NEWUSER,CC->curr_user);
602         }
603
604
605
606 /*
607  * set password
608  */
609 void cmd_setp(char *new_pw)
610 {
611         if (!(CC->logged_in)) {
612                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
613                 return;
614                 }
615         if (CC->usersupp.USuid != BBSUID) {
616                 cprintf("%d Not allowed.  Use the 'passwd' command.\n",ERROR);
617                 return;
618                 }
619         strproc(new_pw);
620         if (strlen(new_pw)==0) {
621                 cprintf("%d Password unchanged.\n",OK);
622                 return;
623                 }
624         lgetuser(&CC->usersupp,CC->curr_user);
625         strcpy(CC->usersupp.password,new_pw);
626         lputuser(&CC->usersupp,CC->curr_user);
627         cprintf("%d Password changed.\n",OK);
628         rec_log(CL_PWCHANGE,CC->curr_user);
629         PerformSessionHooks(EVT_SETPASS);
630         }
631
632 /*
633  * get user parameters
634  */
635 void cmd_getu(void) {
636         if (!(CC->logged_in)) {
637                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
638                 return;
639                 }
640         getuser(&CC->usersupp,CC->curr_user);
641         cprintf("%d %d|%d|%d\n",
642                 OK,
643                 CC->usersupp.USscreenwidth,
644                 CC->usersupp.USscreenheight,
645                 (CC->usersupp.flags & US_USER_SET)
646                 );
647         }
648
649 /*
650  * set user parameters
651  */
652 void cmd_setu(char *new_parms)
653 {
654
655         if (num_parms(new_parms)!=3) {
656                 cprintf("%d Usage error.\n",ERROR);
657                 return;
658                 }       
659         if (!(CC->logged_in)) {
660                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
661                 return;
662                 }
663         lgetuser(&CC->usersupp,CC->curr_user);
664         CC->usersupp.USscreenwidth = extract_int(new_parms,0);
665         CC->usersupp.USscreenheight = extract_int(new_parms,1);
666         CC->usersupp.flags = CC->usersupp.flags & (~US_USER_SET);
667         CC->usersupp.flags = CC->usersupp.flags | 
668                 (extract_int(new_parms,2) & US_USER_SET);
669         lputuser(&CC->usersupp,CC->curr_user);
670         cprintf("%d Ok\n",OK);
671         }
672
673 /*
674  * set last read pointer
675  */
676 void cmd_slrp(char *new_ptr)
677 {
678         long newlr;
679         struct visit vbuf;
680
681         if (!(CC->logged_in)) {
682                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
683                 return;
684                 }
685
686         if (CC->curr_rm < 0) {
687                 cprintf("%d No current room.\n",ERROR);
688                 return;
689                 }
690
691         if (!strncasecmp(new_ptr,"highest",7)) {
692                 newlr = CC->quickroom.QRhighest;
693 /* FIX ... if the current room is 1 (Mail), newlr needs to be set to the
694  * number of the highest mail message
695  */
696                 }
697         else {
698                 newlr = atol(new_ptr);
699                 }
700
701         lgetuser(&CC->usersupp, CC->curr_user);
702
703         /* old method - remove */
704         CC->usersupp.lastseen[CC->curr_rm] = newlr;
705
706         /* new method */
707         CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
708         vbuf.v_lastseen = newlr;
709         CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
710
711         lputuser(&CC->usersupp, CC->curr_user);
712         cprintf("%d %ld\n",OK,newlr);
713         }
714
715
716 /*
717  * INVT and KICK commands
718  */
719 void cmd_invt_kick(char *iuser, int op)
720                         /* user name */
721         {               /* 1 = invite, 0 = kick out */
722         struct usersupp USscratch;
723         char bbb[256];
724         struct visit vbuf;
725
726         if (!(CC->logged_in)) {
727                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
728                 return;
729                 }
730
731         if (CC->curr_rm < 0) {
732                 cprintf("%d No current room.\n",ERROR);
733                 return;
734                 }
735
736         if (is_room_aide()==0) {
737                 cprintf("%d Higher access required.\n",
738                         ERROR+HIGHER_ACCESS_REQUIRED);
739                 return;
740                 }
741
742         /* FIX - with the new relationships scheme we can lock users out,
743            so it'll make sense to remove this routine */
744         if ( (op==1) && ((CC->quickroom.QRflags&QR_PRIVATE)==0) ) {
745                 cprintf("%d Not a private room.\n",ERROR+NOT_HERE);
746                 return;
747                 }
748
749         if (lgetuser(&USscratch,iuser)!=0) {
750                 cprintf("%d No such user.\n",ERROR);
751                 return;
752                 }
753
754         CtdlGetRelationship(&vbuf, &USscratch, &CC->quickroom);
755
756         if (op==1) {
757                 /* old method -- FIX remove this when we're ready */
758                 USscratch.generation[CC->curr_rm]=CC->quickroom.QRgen;
759                 USscratch.forget[CC->curr_rm]=(-1);
760
761                 /* new method */
762                 vbuf.v_flags = vbuf.v_flags & ~V_FORGET & ~V_LOCKOUT;
763                 vbuf.v_flags = vbuf.v_flags | V_ACCESS;
764                 }
765
766         if (op==0) {
767                 /* old method -- FIX remove this when we're ready */
768                 USscratch.generation[CC->curr_rm]=(-1);
769                 USscratch.forget[CC->curr_rm]=CC->quickroom.QRgen;
770
771                 /* new method */
772                 vbuf.v_flags = vbuf.v_flags & ~V_ACCESS;
773                 vbuf.v_flags = vbuf.v_flags | V_FORGET | V_LOCKOUT;
774                 }
775
776         CtdlSetRelationship(&vbuf, &USscratch, &CC->quickroom);
777
778         lputuser(&USscratch,iuser);
779
780         /* post a message in Aide> saying what we just did */
781         sprintf(bbb,"%s %s %s> by %s",
782                 iuser,
783                 ((op == 1) ? "invited to" : "kicked out of"),
784                 CC->quickroom.QRname,
785                 CC->usersupp.fullname);
786         aide_message(bbb);
787
788         if ((op==0)&&((CC->quickroom.QRflags&QR_PRIVATE)==0)) {
789                 cprintf("%d Ok. (Not a private room, <Z>ap effect only)\n",OK);
790                 }
791         else {
792                 cprintf("%d Ok.\n",OK);
793                 }
794         return;
795         }
796
797
798 /*
799  * forget (Zap) the current room
800  */
801 void cmd_forg(void) {
802         struct visit vbuf;
803
804         if (!(CC->logged_in)) {
805                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
806                 return;
807                 }
808
809         if (CC->curr_rm < 0) {
810                 cprintf("%d No current room.\n",ERROR);
811                 return;
812                 }
813
814         if (CC->curr_rm < 3) {
815                 cprintf("%d You cannot forget this room.\n",ERROR+NOT_HERE);
816                 return;
817                 }
818
819         if (is_aide()) {
820                 cprintf("%d Aides cannot forget rooms.\n",ERROR);
821                 return;
822                 }
823
824         lgetuser(&CC->usersupp,CC->curr_user);
825         CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
826
827         /* old method -- FIX remove this when we're ready */
828         CC->usersupp.forget[CC->curr_rm] = CC->quickroom.QRgen;
829         CC->usersupp.generation[CC->curr_rm] = (-1);
830
831         /* new method */
832         vbuf.v_flags = vbuf.v_flags | V_FORGET;
833
834         CtdlSetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
835         lputuser(&CC->usersupp,CC->curr_user);
836         cprintf("%d Ok\n",OK);
837         CC->curr_rm = (-1);
838         }
839
840 /*
841  * Get Next Unregistered User
842  */
843 void cmd_gnur(void) {
844         struct cdbdata *cdbus;
845         struct usersupp usbuf;
846
847         if (!(CC->logged_in)) {
848                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
849                 return;
850                 }
851
852         if (CC->usersupp.axlevel < 6) {
853                 cprintf("%d Higher access required.\n",
854                         ERROR+HIGHER_ACCESS_REQUIRED);
855                 return;
856                 }
857
858         if ((CitControl.MMflags&MM_VALID)==0) {
859                 cprintf("%d There are no unvalidated users.\n",OK);
860                 return;
861                 }
862
863         /* There are unvalidated users.  Traverse the usersupp database,
864          * and return the first user we find that needs validation.
865          */
866         cdb_rewind(CDB_USERSUPP);
867         while (cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
868                 bzero(&usbuf, sizeof(struct usersupp));
869                 memcpy(&usbuf, cdbus->ptr,
870                         ( (cdbus->len > sizeof(struct usersupp)) ?
871                         sizeof(struct usersupp) : cdbus->len) );
872                 cdb_free(cdbus);
873                 if ((usbuf.flags & US_NEEDVALID)
874                    &&(usbuf.axlevel > 0)) {
875                         cprintf("%d %s\n",MORE_DATA,usbuf.fullname);
876                         return;
877                         }
878                 } 
879
880         /* If we get to this point, there are no more unvalidated users.
881          * Therefore we clear the "users need validation" flag.
882          */
883
884         begin_critical_section(S_CONTROL);
885         get_control();
886         CitControl.MMflags = CitControl.MMflags&(~MM_VALID);
887         put_control();
888         end_critical_section(S_CONTROL);
889         cprintf("%d *** End of registration.\n",OK);
890
891
892         }
893
894
895 /*
896  * get registration info for a user
897  */
898 void cmd_greg(char *who)
899 {
900         struct usersupp usbuf;
901         int a,b;
902         char pbuf[32];
903
904         if (!(CC->logged_in)) {
905                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
906                 return;
907                 }
908
909         if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
910
911         if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
912                 cprintf("%d Higher access required.\n",
913                         ERROR+HIGHER_ACCESS_REQUIRED);
914                 return;
915                 }
916
917         if (getuser(&usbuf,who) != 0) {
918                 cprintf("%d '%s' not found.\n",ERROR+NO_SUCH_USER,who);
919                 return;
920                 }
921
922         cprintf("%d %s\n",LISTING_FOLLOWS,usbuf.fullname);
923         cprintf("%ld\n",usbuf.usernum);
924         cprintf("%s\n",usbuf.password);
925         cprintf("%s\n",usbuf.USname);
926         cprintf("%s\n",usbuf.USaddr);
927         cprintf("%s\n%s\n%s\n",
928                 usbuf.UScity,usbuf.USstate,usbuf.USzip);
929         strcpy(pbuf,usbuf.USphone);
930         usbuf.USphone[0]=0;
931         for (a=0; a<strlen(pbuf); ++a) {
932                 if ((pbuf[a]>='0')&&(pbuf[a]<='9')) {
933                         b=strlen(usbuf.USphone);
934                         usbuf.USphone[b]=pbuf[a];
935                         usbuf.USphone[b+1]=0;
936                         }
937                 }
938         while(strlen(usbuf.USphone)<10) {
939                 strcpy(pbuf,usbuf.USphone);
940                 strcpy(usbuf.USphone," ");
941                 strcat(usbuf.USphone,pbuf);
942                 }
943
944         cprintf("(%c%c%c) %c%c%c-%c%c%c%c\n",
945                 usbuf.USphone[0],usbuf.USphone[1],
946                 usbuf.USphone[2],usbuf.USphone[3],
947                 usbuf.USphone[4],usbuf.USphone[5],
948                 usbuf.USphone[6],usbuf.USphone[7],
949                 usbuf.USphone[8],usbuf.USphone[9]);
950
951         cprintf("%d\n",usbuf.axlevel);
952         cprintf("%s\n",usbuf.USemail);
953         cprintf("000\n");
954         }
955
956 /*
957  * validate a user
958  */
959 void cmd_vali(char *v_args)
960 {
961         char user[256];
962         int newax;
963         struct usersupp userbuf;
964
965         extract(user,v_args,0);
966         newax = extract_int(v_args,1);
967
968         if (!(CC->logged_in)) {
969                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
970                 return;
971                 }
972
973         if (CC->usersupp.axlevel < 6) {
974                 cprintf("%d Higher access required.\n",
975                         ERROR+HIGHER_ACCESS_REQUIRED);
976                 return;
977                 }
978
979         if (lgetuser(&userbuf,user)!=0) {
980                 cprintf("%d '%s' not found.\n",ERROR+NO_SUCH_USER,user);
981                 return;
982                 }
983
984         userbuf.axlevel = newax;
985         userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
986
987         lputuser(&userbuf,user);
988
989         /* If the access level was set to zero, delete the user */
990         if (newax == 0) {
991                 if (purge_user(user)==0) {
992                         cprintf("%d %s Deleted.\n", OK, userbuf.fullname);
993                         return;
994                         }
995                 }
996
997         cprintf("%d ok\n",OK);
998         }
999
1000
1001
1002 /* 
1003  *  Traverse the user file...
1004  */
1005 void ForEachUser(void (*CallBack)(struct usersupp *EachUser)) {
1006         struct usersupp usbuf;
1007         struct cdbdata *cdbus;
1008
1009         cdb_rewind(CDB_USERSUPP);
1010
1011         while(cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
1012                 bzero(&usbuf, sizeof(struct usersupp));
1013                 memcpy(&usbuf, cdbus->ptr,
1014                         ( (cdbus->len > sizeof(struct usersupp)) ?
1015                         sizeof(struct usersupp) : cdbus->len) );
1016                 cdb_free(cdbus);
1017                 (*CallBack)(&usbuf);
1018                 }
1019         }
1020
1021
1022 /*
1023  * List one user (this works with cmd_list)
1024  */
1025 void ListThisUser(struct usersupp *usbuf) {
1026         if (usbuf->axlevel > 0) {
1027                 if ((CC->usersupp.axlevel>=6)
1028                    ||((usbuf->flags&US_UNLISTED)==0)
1029                    ||((CC->internal_pgm))) {
1030                         cprintf("%s|%d|%ld|%ld|%d|%d|",
1031                                 usbuf->fullname,
1032                                 usbuf->axlevel,
1033                                 usbuf->usernum,
1034                                 usbuf->lastcall,
1035                                 usbuf->timescalled,
1036                                 usbuf->posted);
1037                         if (CC->usersupp.axlevel >= 6)
1038                                 cprintf("%s",usbuf->password);
1039                         cprintf("\n");
1040                         }
1041                 }
1042         }
1043
1044 /* 
1045  *  List users
1046  */
1047 void cmd_list(void) {
1048         cprintf("%d \n",LISTING_FOLLOWS);
1049         ForEachUser(ListThisUser);
1050         cprintf("000\n");
1051         }
1052
1053
1054 /*
1055  * enter registration info
1056  */
1057 void cmd_regi(void) {
1058         int a,b,c;
1059         char buf[256];
1060
1061         char tmpname[256];
1062         char tmpaddr[256];
1063         char tmpcity[256];
1064         char tmpstate[256];
1065         char tmpzip[256];
1066         char tmpphone[256];
1067         char tmpemail[256];
1068
1069         if (!(CC->logged_in)) {
1070                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1071                 return;
1072                 }
1073
1074         strcpy(tmpname,"");
1075         strcpy(tmpaddr,"");
1076         strcpy(tmpcity,"");
1077         strcpy(tmpstate,"");
1078         strcpy(tmpzip,"");
1079         strcpy(tmpphone,"");
1080         strcpy(tmpemail,"");
1081
1082         cprintf("%d Send registration...\n",SEND_LISTING);
1083         a=0;
1084         while (client_gets(buf), strcmp(buf,"000")) {
1085                 if (a==0) strcpy(tmpname,buf);
1086                 if (a==1) strcpy(tmpaddr,buf);
1087                 if (a==2) strcpy(tmpcity,buf);
1088                 if (a==3) strcpy(tmpstate,buf);
1089                 if (a==4) {
1090                         for (c=0; c<strlen(buf); ++c) {
1091                                 if ((buf[c]>='0')&&(buf[c]<='9')) {
1092                                         b=strlen(tmpzip);
1093                                         tmpzip[b]=buf[c];
1094                                         tmpzip[b+1]=0;
1095                                         }
1096                                 }
1097                         }
1098                 if (a==5) {
1099                         for (c=0; c<strlen(buf); ++c) {
1100                                 if ((buf[c]>='0')&&(buf[c]<='9')) {
1101                                         b=strlen(tmpphone);
1102                                         tmpphone[b]=buf[c];
1103                                         tmpphone[b+1]=0;
1104                                         }
1105                                 }
1106                         }
1107                 if (a==6) strncpy(tmpemail,buf,31);
1108                 ++a;
1109                 }
1110
1111         tmpname[29]=0;
1112         tmpaddr[24]=0;
1113         tmpcity[14]=0;
1114         tmpstate[2]=0;
1115         tmpzip[9]=0;
1116         tmpphone[10]=0;
1117         tmpemail[31]=0;
1118
1119         lgetuser(&CC->usersupp,CC->curr_user);
1120         strcpy(CC->usersupp.USname,tmpname);
1121         strcpy(CC->usersupp.USaddr,tmpaddr);
1122         strcpy(CC->usersupp.UScity,tmpcity);
1123         strcpy(CC->usersupp.USstate,tmpstate);
1124         strcpy(CC->usersupp.USzip,tmpzip);
1125         strcpy(CC->usersupp.USphone,tmpphone);
1126         strcpy(CC->usersupp.USemail,tmpemail);
1127         CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
1128         lputuser(&CC->usersupp,CC->curr_user);
1129
1130         /* set global flag calling for validation */
1131         begin_critical_section(S_CONTROL);
1132         get_control();
1133         CitControl.MMflags = CitControl.MMflags | MM_VALID ;
1134         put_control();
1135         end_critical_section(S_CONTROL);
1136         cprintf("%d *** End of registration.\n",OK);
1137         }
1138
1139
1140 /*
1141  * assorted info we need to check at login
1142  */
1143 void cmd_chek(void) {
1144         int mail = 0;
1145         int regis = 0;
1146         int vali = 0;
1147         
1148         if (!(CC->logged_in)) {
1149                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1150                 return;
1151                 }
1152
1153         getuser(&CC->usersupp,CC->curr_user); /* no lock is needed here */
1154         if ((REGISCALL!=0)&&((CC->usersupp.flags&US_REGIS)==0)) regis = 1;
1155
1156         if (CC->usersupp.axlevel >= 6) {
1157                 get_control();
1158                 if (CitControl.MMflags&MM_VALID) vali = 1;
1159                 }
1160
1161
1162         /* check for mail */
1163         mail = NewMailCount();
1164
1165         cprintf("%d %d|%d|%d\n",OK,mail,regis,vali);
1166         }
1167
1168
1169 /*
1170  * check to see if a user exists
1171  */
1172 void cmd_qusr(char *who)
1173 {
1174         struct usersupp usbuf;
1175
1176         if (getuser(&usbuf,who) == 0) {
1177                 cprintf("%d %s\n",OK,usbuf.fullname);
1178                 }
1179         else {
1180                 cprintf("%d No such user.\n",ERROR+NO_SUCH_USER);
1181                 }
1182         }
1183
1184
1185 /*
1186  * enter user bio
1187  */
1188 void cmd_ebio(void) {
1189         char buf[256];
1190         FILE *fp;
1191
1192         if (!(CC->logged_in)) {
1193                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1194                 return;
1195                 }
1196
1197         sprintf(buf,"./bio/%ld",CC->usersupp.usernum);
1198         fp = fopen(buf,"w");
1199         if (fp == NULL) {
1200                 cprintf("%d Cannot create file\n",ERROR);
1201                 return;
1202                 }
1203         cprintf("%d  \n",SEND_LISTING);
1204         while(client_gets(buf), strcmp(buf,"000")) {
1205                 fprintf(fp,"%s\n",buf);
1206                 }
1207         fclose(fp);
1208         }
1209
1210 /*
1211  * read user bio
1212  */
1213 void cmd_rbio(char *cmdbuf)
1214 {
1215         struct usersupp ruser;
1216         char buf[256];
1217         FILE *fp;
1218
1219         extract(buf,cmdbuf,0);
1220         if (getuser(&ruser,buf)!=0) {
1221                 cprintf("%d No such user.\n",ERROR+NO_SUCH_USER);
1222                 return;
1223                 }
1224         sprintf(buf,"./bio/%ld",ruser.usernum);
1225         
1226         fp = fopen(buf,"r");
1227         if (fp == NULL) {
1228                 cprintf("%d %s has no bio on file.\n",
1229                         ERROR+FILE_NOT_FOUND,ruser.fullname);
1230                 return;
1231                 }
1232         cprintf("%d  \n",LISTING_FOLLOWS);
1233         while (fgets(buf,256,fp)!=NULL) cprintf("%s",buf);
1234         fclose(fp);
1235         cprintf("000\n");
1236         }
1237
1238 /*
1239  * list of users who have entered bios
1240  */
1241 void cmd_lbio(void) {
1242         char buf[256];
1243         FILE *ls;
1244         struct usersupp usbuf;
1245
1246         ls=popen("cd ./bio; ls","r");
1247         if (ls==NULL) {
1248                 cprintf("%d Cannot open listing.\n",ERROR+FILE_NOT_FOUND);
1249                 return;
1250                 }
1251
1252         cprintf("%d\n",LISTING_FOLLOWS);
1253         while (fgets(buf,255,ls)!=NULL)
1254                 if (getuserbynumber(&usbuf,atol(buf))==0)
1255                         cprintf("%s\n",usbuf.fullname);
1256         pclose(ls);
1257         cprintf("000\n");
1258         }
1259
1260
1261 /*
1262  * Administrative Get User Parameters
1263  */
1264 void cmd_agup(char *cmdbuf) {
1265         struct usersupp usbuf;
1266         char requested_user[256];
1267
1268         if ( (CC->internal_pgm==0)
1269            && ( (CC->logged_in == 0) || (is_aide()==0) ) ) {
1270                 cprintf("%d Higher access required.\n", 
1271                         ERROR + HIGHER_ACCESS_REQUIRED);
1272                 return;
1273                 }
1274
1275         extract(requested_user, cmdbuf, 0);
1276         if (getuser(&usbuf, requested_user) != 0) {
1277                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1278                 return;
1279                 }
1280
1281         cprintf("%d %s|%s|%u|%d|%d|%d|%ld\n", 
1282                 OK,
1283                 usbuf.fullname,
1284                 usbuf.password,
1285                 usbuf.flags,
1286                 usbuf.timescalled,
1287                 usbuf.posted,
1288                 (int)usbuf.axlevel,
1289                 usbuf.usernum);
1290
1291         }
1292
1293
1294
1295 /*
1296  * Administrative Set User Parameters
1297  */
1298 void cmd_asup(char *cmdbuf) {
1299         struct usersupp usbuf;
1300         char requested_user[256];
1301         int np;
1302         int newax;
1303         
1304         if ( (CC->internal_pgm==0)
1305            && ( (CC->logged_in == 0) || (is_aide()==0) ) ) {
1306                 cprintf("%d Higher access required.\n", 
1307                         ERROR + HIGHER_ACCESS_REQUIRED);
1308                 return;
1309                 }
1310
1311         extract(requested_user, cmdbuf, 0);
1312         if (lgetuser(&usbuf, requested_user) != 0) {
1313                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1314                 return;
1315                 }
1316
1317         np = num_parms(cmdbuf);
1318         if (np > 1) extract(usbuf.password, cmdbuf, 1);
1319         if (np > 2) usbuf.flags = extract_int(cmdbuf, 2);
1320         if (np > 3) usbuf.timescalled = extract_int(cmdbuf, 3);
1321         if (np > 4) usbuf.posted = extract_int(cmdbuf, 4);
1322         if (np > 5) {
1323                 newax = extract_int(cmdbuf, 5);
1324                 if ((newax >=0) && (newax <= 6)) {
1325                         usbuf.axlevel = extract_int(cmdbuf, 5);
1326                         }
1327                 }
1328
1329         lputuser(&usbuf, requested_user);
1330         if (usbuf.axlevel == 0) {
1331                 if (purge_user(requested_user)==0) {
1332                         cprintf("%d %s deleted.\n", OK, requested_user);
1333                         }
1334                 }
1335         cprintf("%d Ok\n", OK);
1336         }
1337
1338
1339 /*
1340  * Count the number of new mail messages the user has
1341  */
1342 int NewMailCount() {
1343         int num_newmsgs = 0;
1344         struct cdbdata *cdbmb;
1345         int num_mails;
1346         long *mailbox;
1347         int a;
1348
1349         cdbmb = cdb_fetch(CDB_MAILBOXES, &CC->usersupp.usernum, sizeof(long));
1350         if (cdbmb != NULL) {
1351                 num_mails = cdbmb->len / sizeof(long);
1352                 mailbox = (long *) cdbmb->ptr;
1353                 if (num_mails > 0) for (a=0; a<num_mails; ++a) {
1354                         if (mailbox[a] > (CC->usersupp.lastseen[1]))
1355                                 ++num_newmsgs;
1356                         }
1357                 cdb_free(cdbmb);
1358                 }
1359         return(num_newmsgs);
1360         }