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