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