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