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