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