]> code.citadel.org Git - citadel.git/blob - citadel/user_ops.c
* Tried my hand at adding the ability for server extensions to
[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 "database.h"
20 #include "user_ops.h"
21 #include "sysdep_decls.h"
22 #include "support.h"
23 #include "hooks.h"
24 #include "room_ops.h"
25 #include "logging.h"
26 #include "file_ops.h"
27 #include "control.h"
28 #include "msgbase.h"
29 #include "config.h"
30
31
32 /*
33  * getuser()  -  retrieve named user into supplied buffer.
34  *               returns 0 on success
35  */
36 int getuser(struct usersupp *usbuf, char name[]) {
37
38         char lowercase_name[32];
39         int a;
40         struct cdbdata *cdbus;
41
42         bzero(usbuf, sizeof(struct usersupp));
43         for (a=0; a<=strlen(name); ++a) {
44                 lowercase_name[a] = tolower(name[a]);
45                 }
46
47         cdbus = cdb_fetch(CDB_USERSUPP, lowercase_name, strlen(lowercase_name));
48         if (cdbus == NULL) {
49                 return(1);      /* user not found */
50                 }
51
52         memcpy(usbuf, cdbus->ptr,
53                 ( (cdbus->len > sizeof(struct usersupp)) ?
54                 sizeof(struct usersupp) : cdbus->len) );
55         cdb_free(cdbus);
56         return(0);
57         }
58
59
60 /*
61  * lgetuser()  -  same as getuser() but locks the record
62  */
63 int lgetuser(struct usersupp *usbuf, char *name)
64 {
65         int retcode;
66
67         retcode = getuser(usbuf,name);
68         if (retcode == 0) {
69                 begin_critical_section(S_USERSUPP);
70                 }
71         return(retcode);
72         }
73
74
75 /*
76  * putuser()  -  write user buffer into the correct place on disk
77  */
78 void putuser(struct usersupp *usbuf, char *name)
79 {
80         char lowercase_name[32];
81         int a;
82
83         for (a=0; a<=strlen(name); ++a) {
84                 lowercase_name[a] = tolower(name[a]);
85                 }
86
87         cdb_store(CDB_USERSUPP,
88                 lowercase_name, strlen(lowercase_name),
89                 usbuf, sizeof(struct usersupp));
90
91         }
92
93
94 /*
95  * lputuser()  -  same as putuser() but locks the record
96  */
97 void lputuser(struct usersupp *usbuf, char *name) {
98         putuser(usbuf,name);
99         end_critical_section(S_USERSUPP);
100         }
101
102
103 /*
104  * Is the user currently logged in an Aide?
105  */
106 int is_aide(void) {
107         if (CC->usersupp.axlevel >= 6) return(1);
108         else return(0);
109         }
110
111
112 /*
113  * Is the user currently logged in an Aide *or* the room aide for this room?
114  */
115 int is_room_aide(void) {
116         if ( (CC->usersupp.axlevel >= 6)
117            || (CC->quickroom.QRroomaide == CC->usersupp.usernum) ) {
118                 return(1);
119                 }
120         else {
121                 return(0);
122                 }
123         }
124
125 /*
126  * getuserbynumber()  -  get user by number
127  *                       returns 0 if user was found
128  */
129 int getuserbynumber(struct usersupp *usbuf, long int number)
130 {
131         struct cdbdata *cdbus;
132
133         cdb_rewind(CDB_USERSUPP);
134
135         while(cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
136                 bzero(usbuf, sizeof(struct usersupp));
137                 memcpy(usbuf, cdbus->ptr,
138                         ( (cdbus->len > sizeof(struct usersupp)) ?
139                         sizeof(struct usersupp) : cdbus->len) );
140                 cdb_free(cdbus);
141                 if (usbuf->usernum == number) {
142                         return(0);
143                         }
144                 }
145         return(-1);
146         }
147
148
149 /*
150  * USER cmd
151  */
152 void cmd_user(char *cmdbuf)
153 {
154         char username[256];
155         char autoname[256];
156         int found_user = 0;
157         struct passwd *p;
158         int a;
159
160         extract(username,cmdbuf,0);
161         username[25] = 0;
162         strproc(username);
163
164         if ((CC->logged_in)) {
165                 cprintf("%d Already logged in.\n",ERROR);
166                 return;
167                 }
168
169         found_user = getuser(&CC->usersupp,username);
170         if (found_user != 0) {
171                 p = (struct passwd *)getpwnam(username);
172                 if (p!=NULL) {
173                         strcpy(autoname,p->pw_gecos);
174                         for (a=0; a<strlen(autoname); ++a)
175                                 if (autoname[a]==',') autoname[a]=0;
176                         found_user = getuser(&CC->usersupp,autoname);
177                         }
178                 }
179         if (found_user == 0) {
180                 if (((CC->nologin)) && (CC->usersupp.axlevel < 6)) {
181                         cprintf("%d %s: Too many users are already online (maximum is %d)\n",
182                         ERROR+MAX_SESSIONS_EXCEEDED,
183                         config.c_nodename,config.c_maxsessions);
184                         }
185                 else {
186                         strcpy(CC->curr_user,CC->usersupp.fullname);
187                         cprintf("%d Password required for %s\n",
188                                 MORE_DATA,CC->curr_user);
189                         }
190                 }
191         else {
192                 cprintf("%d %s not found.\n",ERROR,username);
193                 }
194         }
195
196
197
198 /*
199  * session startup code which is common to both cmd_pass() and cmd_newu()
200  */
201 void session_startup(void) {
202         int a;
203         struct quickroom qr;
204
205         syslog(LOG_NOTICE,"user <%s> logged in",CC->curr_user);
206         /* hook_user_login(CC->cs_pid, CC->curr_user); FIX */
207         lgetuser(&CC->usersupp,CC->curr_user);
208         ++(CC->usersupp.timescalled);
209         CC->fake_username[0] = '\0';
210         CC->fake_postname[0] = '\0';
211         CC->fake_hostname[0] = '\0';
212         CC->fake_roomname[0] = '\0';
213         CC->last_pager[0] = '\0';
214         time(&CC->usersupp.lastcall);
215
216         /* If this user's name is the name of the system administrator
217          * (as specified in setup), automatically assign access level 6.
218          */
219         if (!strcasecmp(CC->usersupp.fullname, config.c_sysadm)) {
220                 CC->usersupp.axlevel = 6;
221                 }
222
223 /* A room's generation number changes each time it is recycled. Users are kept
224  * out of private rooms or forget rooms by matching the generation numbers. To
225  * avoid an accidental matchup, unmatched numbers are set to -1 here.
226  */
227         for (a=0; a<MAXROOMS; ++a) {
228                 getroom(&qr,a);
229                 if (CC->usersupp.generation[a] != qr.QRgen)
230                                         CC->usersupp.generation[a]=(-1);
231                 if (CC->usersupp.forget[a] != qr.QRgen)
232                                         CC->usersupp.forget[a]=(-1);
233                 }
234
235         lputuser(&CC->usersupp,CC->curr_user);
236
237         cprintf("%d %s|%d|%d|%d|%u|%ld\n",OK,CC->usersupp.fullname,CC->usersupp.axlevel,
238                 CC->usersupp.timescalled,CC->usersupp.posted,CC->usersupp.flags,
239                 CC->usersupp.usernum);
240         usergoto(0,0);          /* Enter the lobby */   
241         rec_log(CL_LOGIN,CC->curr_user);
242         }
243
244
245
246 /* 
247  * misc things to be taken care of when a user is logged out
248  */
249 void logout(struct CitContext *who)
250 {
251         who->logged_in = 0;
252         if (who->download_fp != NULL) {
253                 fclose(who->download_fp);
254                 who->download_fp = NULL;
255                 }
256         if (who->upload_fp != NULL) {
257                 abort_upl(who);
258                 }
259         }
260
261
262 void cmd_pass(char *buf)
263 {
264         char password[256];
265         int code;
266         struct passwd *p;
267
268         extract(password,buf,0);
269
270         if ((CC->logged_in)) {
271                 cprintf("%d Already logged in.\n",ERROR);
272                 return;
273                 }
274         if (!strcmp(CC->curr_user,"")) {
275                 cprintf("%d You must send a name with USER first.\n",ERROR);
276                 return;
277                 }
278         if (getuser(&CC->usersupp,CC->curr_user)) {
279                 cprintf("%d Can't find user record!\n",ERROR+INTERNAL_ERROR);
280                 return;
281                 }
282
283         code = (-1);
284         if (CC->usersupp.USuid == BBSUID) {
285                 strproc(password);
286                 strproc(CC->usersupp.password);
287                 code = strcasecmp(CC->usersupp.password,password);
288                 }
289         else {
290                 p = (struct passwd *)getpwuid(CC->usersupp.USuid);
291 #ifdef ENABLE_AUTOLOGIN
292                 if (p!=NULL) {
293                         if (!strcmp(p->pw_passwd,
294                            (char *)crypt(password,p->pw_passwd))) {
295                                 code = 0;
296                                 lgetuser(&CC->usersupp, CC->curr_user);
297                                 strcpy(CC->usersupp.password, password);
298                                 lputuser(&CC->usersupp, CC->curr_user);
299                                 }
300                         }
301 #endif
302                 }
303
304         if (!code) {
305                 (CC->logged_in) = 1;
306                 session_startup();
307                 }
308         else {
309                 cprintf("%d Wrong password.\n",ERROR);
310                 rec_log(CL_BADPW,CC->curr_user);
311                 }
312         }
313
314
315 /*
316  * Delete a user record *and* all of its related resources.
317  */
318 int purge_user(char *pname) {
319         char filename[64];
320         struct usersupp usbuf;
321         int a;
322         struct cdbdata *cdbmb;
323         long *mailbox;
324         int num_mails;
325
326         if (getuser(&usbuf, pname) != 0) {
327                 lprintf(5, "Cannot purge user <%s> - not found\n", pname);
328                 return(ERROR+NO_SUCH_USER);
329                 }
330
331         /* FIX   Don't delete a user who is currently logged in. */
332
333         /* delete any messages in the user's mailbox */
334         cdbmb = cdb_fetch(CDB_MAILBOXES, &usbuf.usernum, sizeof(long));
335         if (cdbmb != NULL) {
336                 num_mails = cdbmb->len / sizeof(long);
337                 mailbox = (long *) cdbmb->ptr;
338                 if (num_mails > 0) for (a=0; a<num_mails; ++a) {
339                         cdb_delete(CDB_MSGMAIN, &mailbox[a], sizeof(long));
340                         }
341                 cdb_free(cdbmb);
342                 /* now delete the mailbox itself */
343                 cdb_delete(CDB_MAILBOXES, &usbuf.usernum, sizeof(long));
344                 }
345
346
347         /* delete the userlog entry */
348         cdb_delete(CDB_USERSUPP, pname, strlen(pname));
349
350         /* remove the user's bio file */        
351         sprintf(filename, "./bio/%ld", usbuf.usernum);
352         unlink(filename);
353
354         /* remove the user's picture */
355         sprintf(filename, "./userpics/%ld.gif", usbuf.usernum);
356         unlink(filename);
357
358         return(0);
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,
721                         ( (cdbus->len > sizeof(struct usersupp)) ?
722                         sizeof(struct usersupp) : cdbus->len) );
723                 cdb_free(cdbus);
724                 if ((usbuf.flags & US_NEEDVALID)
725                    &&(usbuf.axlevel > 0)) {
726                         cprintf("%d %s\n",MORE_DATA,usbuf.fullname);
727                         return;
728                         }
729                 } 
730
731         /* If we get to this point, there are no more unvalidated users.
732          * Therefore we clear the "users need validation" flag.
733          */
734
735         begin_critical_section(S_CONTROL);
736         get_control();
737         CitControl.MMflags = CitControl.MMflags&(~MM_VALID);
738         put_control();
739         end_critical_section(S_CONTROL);
740         cprintf("%d *** End of registration.\n",OK);
741
742
743         }
744
745
746 /*
747  * get registration info for a user
748  */
749 void cmd_greg(char *who)
750 {
751         struct usersupp usbuf;
752         int a,b;
753         char pbuf[32];
754
755         if (!(CC->logged_in)) {
756                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
757                 return;
758                 }
759
760         if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
761
762         if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
763                 cprintf("%d Higher access required.\n",
764                         ERROR+HIGHER_ACCESS_REQUIRED);
765                 return;
766                 }
767
768         if (getuser(&usbuf,who) != 0) {
769                 cprintf("%d '%s' not found.\n",ERROR+NO_SUCH_USER,who);
770                 return;
771                 }
772
773         cprintf("%d %s\n",LISTING_FOLLOWS,usbuf.fullname);
774         cprintf("%ld\n",usbuf.usernum);
775         cprintf("%s\n",usbuf.password);
776         cprintf("%s\n",usbuf.USname);
777         cprintf("%s\n",usbuf.USaddr);
778         cprintf("%s\n%s\n%s\n",
779                 usbuf.UScity,usbuf.USstate,usbuf.USzip);
780         strcpy(pbuf,usbuf.USphone);
781         usbuf.USphone[0]=0;
782         for (a=0; a<strlen(pbuf); ++a) {
783                 if ((pbuf[a]>='0')&&(pbuf[a]<='9')) {
784                         b=strlen(usbuf.USphone);
785                         usbuf.USphone[b]=pbuf[a];
786                         usbuf.USphone[b+1]=0;
787                         }
788                 }
789         while(strlen(usbuf.USphone)<10) {
790                 strcpy(pbuf,usbuf.USphone);
791                 strcpy(usbuf.USphone," ");
792                 strcat(usbuf.USphone,pbuf);
793                 }
794
795         cprintf("(%c%c%c) %c%c%c-%c%c%c%c\n",
796                 usbuf.USphone[0],usbuf.USphone[1],
797                 usbuf.USphone[2],usbuf.USphone[3],
798                 usbuf.USphone[4],usbuf.USphone[5],
799                 usbuf.USphone[6],usbuf.USphone[7],
800                 usbuf.USphone[8],usbuf.USphone[9]);
801
802         cprintf("%d\n",usbuf.axlevel);
803         cprintf("%s\n",usbuf.USemail);
804         cprintf("000\n");
805         }
806
807 /*
808  * validate a user
809  */
810 void cmd_vali(char *v_args)
811 {
812         char user[256];
813         int newax;
814         struct usersupp userbuf;
815
816         extract(user,v_args,0);
817         newax = extract_int(v_args,1);
818
819         if (!(CC->logged_in)) {
820                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
821                 return;
822                 }
823
824         if (CC->usersupp.axlevel < 6) {
825                 cprintf("%d Higher access required.\n",
826                         ERROR+HIGHER_ACCESS_REQUIRED);
827                 return;
828                 }
829
830         if (lgetuser(&userbuf,user)!=0) {
831                 cprintf("%d '%s' not found.\n",ERROR+NO_SUCH_USER,user);
832                 return;
833                 }
834
835         userbuf.axlevel = newax;
836         userbuf.flags = (userbuf.flags & ~US_NEEDVALID);
837
838         lputuser(&userbuf,user);
839
840         /* If the access level was set to zero, delete the user */
841         if (newax == 0) {
842                 if (purge_user(user)==0) {
843                         cprintf("%d %s Deleted.\n", OK, userbuf.fullname);
844                         return;
845                         }
846                 }
847
848         cprintf("%d ok\n",OK);
849         }
850
851
852
853 /* 
854  *  Traverse the user file...
855  */
856 void ForEachUser(void (*CallBack)(struct usersupp *EachUser)) {
857         struct usersupp usbuf;
858         struct cdbdata *cdbus;
859
860         cdb_rewind(CDB_USERSUPP);
861
862         while(cdbus = cdb_next_item(CDB_USERSUPP), cdbus != NULL) {
863                 bzero(&usbuf, sizeof(struct usersupp));
864                 memcpy(&usbuf, cdbus->ptr,
865                         ( (cdbus->len > sizeof(struct usersupp)) ?
866                         sizeof(struct usersupp) : cdbus->len) );
867                 cdb_free(cdbus);
868                 (*CallBack)(&usbuf);
869                 }
870         }
871
872
873 /*
874  * List one user (this works with cmd_list)
875  */
876 void ListThisUser(struct usersupp *usbuf) {
877         if (usbuf->axlevel > 0) {
878                 if ((CC->usersupp.axlevel>=6)
879                    ||((usbuf->flags&US_UNLISTED)==0)
880                    ||((CC->internal_pgm))) {
881                         cprintf("%s|%d|%ld|%ld|%d|%d|",
882                                 usbuf->fullname,
883                                 usbuf->axlevel,
884                                 usbuf->usernum,
885                                 usbuf->lastcall,
886                                 usbuf->timescalled,
887                                 usbuf->posted);
888                         if (CC->usersupp.axlevel >= 6)
889                                 cprintf("%s",usbuf->password);
890                         cprintf("\n");
891                         }
892                 }
893         }
894
895 /* 
896  *  List users
897  */
898 void cmd_list(void) {
899         cprintf("%d \n",LISTING_FOLLOWS);
900         ForEachUser(ListThisUser);
901         cprintf("000\n");
902         }
903
904
905 /*
906  * enter registration info
907  */
908 void cmd_regi(void) {
909         int a,b,c;
910         char buf[256];
911
912         char tmpname[256];
913         char tmpaddr[256];
914         char tmpcity[256];
915         char tmpstate[256];
916         char tmpzip[256];
917         char tmpphone[256];
918         char tmpemail[256];
919
920         if (!(CC->logged_in)) {
921                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
922                 return;
923                 }
924
925         strcpy(tmpname,"");
926         strcpy(tmpaddr,"");
927         strcpy(tmpcity,"");
928         strcpy(tmpstate,"");
929         strcpy(tmpzip,"");
930         strcpy(tmpphone,"");
931         strcpy(tmpemail,"");
932
933         cprintf("%d Send registration...\n",SEND_LISTING);
934         a=0;
935         while (client_gets(buf), strcmp(buf,"000")) {
936                 if (a==0) strcpy(tmpname,buf);
937                 if (a==1) strcpy(tmpaddr,buf);
938                 if (a==2) strcpy(tmpcity,buf);
939                 if (a==3) strcpy(tmpstate,buf);
940                 if (a==4) {
941                         for (c=0; c<strlen(buf); ++c) {
942                                 if ((buf[c]>='0')&&(buf[c]<='9')) {
943                                         b=strlen(tmpzip);
944                                         tmpzip[b]=buf[c];
945                                         tmpzip[b+1]=0;
946                                         }
947                                 }
948                         }
949                 if (a==5) {
950                         for (c=0; c<strlen(buf); ++c) {
951                                 if ((buf[c]>='0')&&(buf[c]<='9')) {
952                                         b=strlen(tmpphone);
953                                         tmpphone[b]=buf[c];
954                                         tmpphone[b+1]=0;
955                                         }
956                                 }
957                         }
958                 if (a==6) strncpy(tmpemail,buf,31);
959                 ++a;
960                 }
961
962         tmpname[29]=0;
963         tmpaddr[24]=0;
964         tmpcity[14]=0;
965         tmpstate[2]=0;
966         tmpzip[9]=0;
967         tmpphone[10]=0;
968         tmpemail[31]=0;
969
970         lgetuser(&CC->usersupp,CC->curr_user);
971         strcpy(CC->usersupp.USname,tmpname);
972         strcpy(CC->usersupp.USaddr,tmpaddr);
973         strcpy(CC->usersupp.UScity,tmpcity);
974         strcpy(CC->usersupp.USstate,tmpstate);
975         strcpy(CC->usersupp.USzip,tmpzip);
976         strcpy(CC->usersupp.USphone,tmpphone);
977         strcpy(CC->usersupp.USemail,tmpemail);
978         CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
979         lputuser(&CC->usersupp,CC->curr_user);
980
981         /* set global flag calling for validation */
982         begin_critical_section(S_CONTROL);
983         get_control();
984         CitControl.MMflags = CitControl.MMflags | MM_VALID ;
985         put_control();
986         end_critical_section(S_CONTROL);
987         cprintf("%d *** End of registration.\n",OK);
988         }
989
990
991 /*
992  * assorted info we need to check at login
993  */
994 void cmd_chek(void) {
995         int mail = 0;
996         int regis = 0;
997         int vali = 0;
998         int a;
999         struct cdbdata *cdbmb;
1000         long *mailbox;
1001         int num_mails;
1002         
1003
1004         if (!(CC->logged_in)) {
1005                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1006                 return;
1007                 }
1008
1009         getuser(&CC->usersupp,CC->curr_user); /* no lock is needed here */
1010         if ((REGISCALL!=0)&&((CC->usersupp.flags&US_REGIS)==0)) regis = 1;
1011
1012         if (CC->usersupp.axlevel >= 6) {
1013                 get_control();
1014                 if (CitControl.MMflags&MM_VALID) vali = 1;
1015                 }
1016
1017
1018         /* check for mail */
1019         mail = 0;
1020         cdbmb = cdb_fetch(CDB_MAILBOXES, &CC->usersupp.usernum, sizeof(long));
1021         if (cdbmb != NULL) {
1022                 num_mails = cdbmb->len / sizeof(long);
1023                 mailbox = (long *) cdbmb->ptr;
1024                 if (num_mails > 0) for (a=0; a<num_mails; ++a) {
1025                         if (mailbox[a] > (CC->usersupp.lastseen[1])) ++mail;
1026                         }
1027                 cdb_free(cdbmb);
1028                 }
1029
1030
1031         cprintf("%d %d|%d|%d\n",OK,mail,regis,vali);
1032         }
1033
1034
1035 /*
1036  * check to see if a user exists
1037  */
1038 void cmd_qusr(char *who)
1039 {
1040         struct usersupp usbuf;
1041
1042         if (getuser(&usbuf,who) == 0) {
1043                 cprintf("%d %s\n",OK,usbuf.fullname);
1044                 }
1045         else {
1046                 cprintf("%d No such user.\n",ERROR+NO_SUCH_USER);
1047                 }
1048         }
1049
1050
1051 /*
1052  * enter user bio
1053  */
1054 void cmd_ebio(void) {
1055         char buf[256];
1056         FILE *fp;
1057
1058         if (!(CC->logged_in)) {
1059                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
1060                 return;
1061                 }
1062
1063         sprintf(buf,"./bio/%ld",CC->usersupp.usernum);
1064         fp = fopen(buf,"w");
1065         if (fp == NULL) {
1066                 cprintf("%d Cannot create file\n",ERROR);
1067                 return;
1068                 }
1069         cprintf("%d  \n",SEND_LISTING);
1070         while(client_gets(buf), strcmp(buf,"000")) {
1071                 fprintf(fp,"%s\n",buf);
1072                 }
1073         fclose(fp);
1074         }
1075
1076 /*
1077  * read user bio
1078  */
1079 void cmd_rbio(char *cmdbuf)
1080 {
1081         struct usersupp ruser;
1082         char buf[256];
1083         FILE *fp;
1084
1085         extract(buf,cmdbuf,0);
1086         if (getuser(&ruser,buf)!=0) {
1087                 cprintf("%d No such user.\n",ERROR+NO_SUCH_USER);
1088                 return;
1089                 }
1090         sprintf(buf,"./bio/%ld",ruser.usernum);
1091         
1092         fp = fopen(buf,"r");
1093         if (fp == NULL) {
1094                 cprintf("%d %s has no bio on file.\n",
1095                         ERROR+FILE_NOT_FOUND,ruser.fullname);
1096                 return;
1097                 }
1098         cprintf("%d  \n",LISTING_FOLLOWS);
1099         while (fgets(buf,256,fp)!=NULL) cprintf("%s",buf);
1100         fclose(fp);
1101         cprintf("000\n");
1102         }
1103
1104 /*
1105  * list of users who have entered bios
1106  */
1107 void cmd_lbio(void) {
1108         char buf[256];
1109         FILE *ls;
1110         struct usersupp usbuf;
1111
1112         ls=popen("cd ./bio; ls","r");
1113         if (ls==NULL) {
1114                 cprintf("%d Cannot open listing.\n",ERROR+FILE_NOT_FOUND);
1115                 return;
1116                 }
1117
1118         cprintf("%d\n",LISTING_FOLLOWS);
1119         while (fgets(buf,255,ls)!=NULL)
1120                 if (getuserbynumber(&usbuf,atol(buf))==0)
1121                         cprintf("%s\n",usbuf.fullname);
1122         pclose(ls);
1123         cprintf("000\n");
1124         }
1125
1126
1127 /*
1128  * Administrative Get User Parameters
1129  */
1130 void cmd_agup(char *cmdbuf) {
1131         struct usersupp usbuf;
1132         char requested_user[256];
1133
1134         if ( (CC->internal_pgm==0)
1135            && ( (CC->logged_in == 0) || (is_aide()==0) ) ) {
1136                 cprintf("%d Higher access required.\n", 
1137                         ERROR + HIGHER_ACCESS_REQUIRED);
1138                 return;
1139                 }
1140
1141         extract(requested_user, cmdbuf, 0);
1142         if (getuser(&usbuf, requested_user) != 0) {
1143                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1144                 return;
1145                 }
1146
1147         cprintf("%d %s|%s|%u|%d|%d|%d|%ld\n", 
1148                 OK,
1149                 usbuf.fullname,
1150                 usbuf.password,
1151                 usbuf.flags,
1152                 usbuf.timescalled,
1153                 usbuf.posted,
1154                 (int)usbuf.axlevel,
1155                 usbuf.usernum);
1156
1157         }
1158
1159
1160
1161 /*
1162  * Administrative Set User Parameters
1163  */
1164 void cmd_asup(char *cmdbuf) {
1165         struct usersupp usbuf;
1166         char requested_user[256];
1167         int np;
1168         int newax;
1169         
1170         if ( (CC->internal_pgm==0)
1171            && ( (CC->logged_in == 0) || (is_aide()==0) ) ) {
1172                 cprintf("%d Higher access required.\n", 
1173                         ERROR + HIGHER_ACCESS_REQUIRED);
1174                 return;
1175                 }
1176
1177         extract(requested_user, cmdbuf, 0);
1178         if (lgetuser(&usbuf, requested_user) != 0) {
1179                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1180                 return;
1181                 }
1182
1183         np = num_parms(cmdbuf);
1184         if (np > 1) extract(usbuf.password, cmdbuf, 1);
1185         if (np > 2) usbuf.flags = extract_int(cmdbuf, 2);
1186         if (np > 3) usbuf.timescalled = extract_int(cmdbuf, 3);
1187         if (np > 4) usbuf.posted = extract_int(cmdbuf, 4);
1188         if (np > 5) {
1189                 newax = extract_int(cmdbuf, 5);
1190                 if ((newax >=0) && (newax <= 6)) {
1191                         usbuf.axlevel = extract_int(cmdbuf, 5);
1192                         }
1193                 }
1194
1195         lputuser(&usbuf, requested_user);
1196         if (usbuf.axlevel == 0) {
1197                 if (purge_user(requested_user)==0) {
1198                         cprintf("%d %s deleted.\n", OK, requested_user);
1199                         }
1200                 }
1201         cprintf("%d Ok\n", OK);
1202         }