]> code.citadel.org Git - citadel.git/blob - citadel/routines.c
* Implemented the CREU server command to administratively create user accounts
[citadel.git] / citadel / routines.c
1 /*
2  * $Id$
3  *
4  * Client-side support functions.
5  *
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <ctype.h>
14 #include <string.h>
15 #include <sys/types.h>
16 #include <sys/ioctl.h>
17 #include <pwd.h>
18 #include <signal.h>
19 #include <dirent.h>
20 #include <errno.h>
21
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
24 # include <time.h>
25 #else
26 # if HAVE_SYS_TIME_H
27 #  include <sys/time.h>
28 # else
29 #  include <time.h>
30 # endif
31 #endif
32 #ifdef HAVE_LIMITS_H
33 #include <limits.h>
34 #endif
35 #ifdef HAVE_UTMP_H
36 #include <utmp.h>
37 #endif
38 #ifdef HAVE_UTMPX_H
39 #include <utmpx.h>
40 #endif
41 #include "screen.h"
42
43 #ifndef HAVE_GETUTLINE
44 struct utmp *getutline(struct utmp *ut);
45 #endif
46
47 #define ROUTINES_C
48
49 #include "citadel.h"
50 #include "routines.h"
51 #include "commands.h"
52 #include "tools.h"
53
54 void sttybbs(int cmd);
55 void newprompt(char *prompt, char *str, int len);
56 void val_user(char *, int);
57 void formout(char *name);
58 void logoff(int code);
59 void set_keepalives(int s);
60 void strprompt(char *prompt, char *str, int len);
61 void newprompt(char *prompt, char *str, int len);
62 void color(int colornum);
63
64 #define IFAIDE if(axlevel>=6)
65 #define IFNAIDE if (axlevel<6)
66
67 extern unsigned userflags;
68 extern char *axdefs[7];
69 extern char sigcaught;
70 extern struct CtdlServInfo serv_info;
71 extern char rc_floor_mode;
72 extern int rc_ansi_color;
73 extern int rc_prompt_control;
74
75 void back(int spaces) /* Destructive backspace */
76             {
77         int a;
78         for (a=1; a<=spaces; ++a) {
79                 scr_putc(8); scr_putc(32); scr_putc(8);
80                 }
81         }
82
83 void hit_any_key(void) {                /* hit any key to continue */
84         int a,b;
85
86         color(COLOR_PUSH);
87         color(DIM_RED);
88         scr_printf("%s\r",serv_info.serv_moreprompt);
89         color(COLOR_POP);
90         sttybbs(0);
91         b=inkey();
92         for (a=0; a<strlen(serv_info.serv_moreprompt); ++a)
93                 scr_putc(' ');
94         scr_putc(13);
95         sttybbs(1);
96         if ( (rc_prompt_control == 1)
97            || ((rc_prompt_control == 3) && (userflags & US_PROMPTCTL)) ) {
98                 if (b == 'q' || b == 'Q' || b == 's' || b == 'S')
99                         b = STOP_KEY;
100                 if (b == 'n' || b == 'N')
101                         b = NEXT_KEY;
102         }
103         if (b==NEXT_KEY) sigcaught = SIGINT;
104         if (b==STOP_KEY) sigcaught = SIGQUIT;
105 }
106
107 /*
108  * change a user's access level
109  */
110 void edituser(void)
111 {
112         char buf[SIZ];
113         char who[SIZ];
114         char pass[SIZ];
115         int flags;
116         int timescalled;
117         int posted;
118         int axlevel;
119         long usernum;
120         time_t lastcall;
121         int userpurge;
122
123         newprompt("User name: ",who,25);
124 AGUP:   sprintf(buf,"AGUP %s",who);
125         serv_puts(buf);
126         serv_gets(buf);
127         if (buf[0]!='2') {
128                 scr_printf("%s\n",&buf[4]);
129                 scr_printf("Do you want to create this user? ");
130                 if (yesno()) {
131                         sprintf(buf, "CREU %s", who);
132                         serv_puts(buf);
133                         serv_gets(buf);
134                         if (buf[0] == '2') goto AGUP;
135                         scr_printf("%s\n",&buf[4]);
136                         return;
137                 }
138                 return;
139         }
140         extract(who, &buf[4], 0);
141         extract(pass, &buf[4], 1);
142         flags = extract_int(&buf[4], 2);
143         timescalled = extract_int(&buf[4], 3);
144         posted = extract_int(&buf[4], 4);
145         axlevel = extract_int(&buf[4], 5);
146         usernum = extract_long(&buf[4], 6);
147         lastcall = extract_long(&buf[4], 7);
148         userpurge = extract_int(&buf[4], 8);
149
150         val_user(who, 0); /* Display registration */
151         if (boolprompt("Change password", 0)) {
152                 strprompt("Password", pass, 19);
153         }
154         axlevel = intprompt("Access level", axlevel, 0, 6);
155         if (boolprompt("Ask user to register again", !(flags & US_REGIS)))
156                 flags &= ~US_REGIS;
157         else
158                 flags |= US_REGIS;
159         timescalled = intprompt("Times called", timescalled, 0, INT_MAX);
160         posted = intprompt("Messages posted", posted, 0, INT_MAX);
161         lastcall = (boolprompt("Set last call to now", 0)?time(NULL):lastcall);
162         userpurge = intprompt("Purge time (in days, 0 for system default",
163                                 userpurge, 0, INT_MAX);
164
165         sprintf(buf, "ASUP %s|%s|%d|%d|%d|%d|%ld|%ld|%d",
166                 who, pass, flags, timescalled, posted, axlevel, usernum,
167                 (long)lastcall, userpurge);
168         serv_puts(buf);
169         serv_gets(buf);
170         if (buf[0]!='2') {
171                 scr_printf("%s\n",&buf[4]);
172                 }
173         }
174
175
176 int set_attr(int sval, char *prompt, unsigned int sbit)
177 {
178         int a;
179         int temp;
180
181         temp = sval;
182         color(DIM_WHITE);
183         scr_printf("%45s ", prompt);
184         color(DIM_MAGENTA);
185         scr_printf("[");
186         color(BRIGHT_MAGENTA);
187         scr_printf("%3s", ((temp&sbit) ? "Yes":"No"));
188         color(DIM_MAGENTA);
189         scr_printf("]? ");
190         color(BRIGHT_CYAN);
191         a=yesno_d(temp&sbit);
192         color(DIM_WHITE);
193         temp=(temp|sbit);
194         if (!a) temp=(temp^sbit);
195         return(temp);
196         }
197
198 /*
199  * modes are:  0 - .EC command, 1 - .EC for new user,
200  *             2 - toggle Xpert mode  3 - toggle floor mode
201  */
202 void enter_config(int mode)
203 {
204         int width, height, flags, filter;
205         char buf[128];
206
207         sprintf(buf,"GETU");
208         serv_puts(buf);
209         serv_gets(buf);
210         if (buf[0]!='2') {
211                 scr_printf("%s\n",&buf[4]);
212                 return;
213                 }
214
215         width = extract_int(&buf[4],0);
216         height = extract_int(&buf[4],1);
217         flags = extract_int(&buf[4],2);
218         filter = extract_int(&buf[4],3);
219
220         if ((mode==0)||(mode==1)) {
221
222          width = intprompt("Enter your screen width",width,20,255);
223          height = intprompt("Enter your screen height",height,3,255);
224  
225          flags = set_attr(flags,
226                 "Are you an experienced Citadel user",US_EXPERT);
227          if ( ((flags&US_EXPERT)==0) && (mode==1))
228                 return;
229          flags = set_attr(flags,
230                 "Print last old message on New message request",US_LASTOLD);
231          if ((flags&US_EXPERT)==0) formout("unlisted");
232          flags = set_attr(flags,"Be unlisted in userlog",US_UNLISTED);
233          flags = set_attr(flags,"Suppress message prompts",US_NOPROMPT);
234          if ((flags & US_NOPROMPT)==0)
235             flags = set_attr(flags,"Use 'disappearing' prompts",US_DISAPPEAR);
236          flags = set_attr(flags,
237                 "Pause after each screenful of text",US_PAGINATOR);
238          if ( (rc_prompt_control == 3) && (flags & US_PAGINATOR) ) {
239                 flags = set_attr(flags,
240                 "<N>ext and <S>top work at paginator prompt", US_PROMPTCTL);
241          }
242          if (rc_floor_mode == RC_DEFAULT) {
243           flags = set_attr(flags,
244                 "View rooms by floor",US_FLOORS);
245           }
246          if (rc_ansi_color == 3) {
247           flags = set_attr(flags,
248                 "Enable color support",US_COLOR);
249           }
250         
251          /* filter = intprompt("Moderation filter level", filter, -63, 63); */
252
253          }
254
255         if (mode==2) {
256          if (flags & US_EXPERT) {
257                 flags = (flags ^ US_EXPERT);
258                 scr_printf("Expert mode now OFF\n");
259                 }
260          else {
261                 flags = (flags | US_EXPERT);
262                 scr_printf("Expert mode now ON\n");
263                 }
264          }
265
266         if (mode==3) {
267          if (flags & US_FLOORS) {
268                 flags = (flags ^ US_FLOORS);
269                 scr_printf("Floor mode now OFF\n");
270                 }
271          else {
272                 flags = (flags | US_FLOORS);
273                 scr_printf("Floor mode now ON\n");
274                 }
275          }
276
277         sprintf(buf,"SETU %d|%d|%d|%d",width,height,flags,filter);
278         serv_puts(buf);
279         serv_gets(buf);
280         if (buf[0]!='2') scr_printf("%s\n",&buf[4]);
281         userflags = flags;
282 }
283
284 /*
285  * getstring()  -  get a line of text from a file
286  *                 ignores lines beginning with "#"
287  */
288 int getstring(FILE *fp, char *string)
289 {
290         int a,c;
291         do {
292                 strcpy(string,"");
293                 a=0;
294                 do {
295                         c=getc(fp);
296                         if (c<0) {
297                                 string[a]=0;
298                                 return(-1);
299                                 }
300                         string[a++]=c;
301                         } while(c!=10);
302                         string[a-1]=0;
303                 } while(string[0]=='#');
304         return(strlen(string));
305         }
306
307 int pattern(char *search, char *patn)   /* Searches for patn in search string */
308               
309             
310 {
311         int a,b;
312         for (a=0; a<strlen(search); ++a)
313         {       b=strncasecmp(&search[a],patn,strlen(patn));
314                 if (b==0) return(b);
315                 }
316         return(-1);
317 }
318
319 void interr(int errnum) /* display internal error as defined in errmsgs */
320             {
321         scr_printf("*** INTERNAL ERROR %d\n"
322                 "(Press any key to continue)\n", errnum);
323         inkey();
324         logoff(errnum);
325 }
326
327
328
329 void strproc(char *string)
330 {
331         int a;
332
333         if (strlen(string)==0) return;
334
335         /* Convert non-printable characters to blanks */
336         for (a=0; a<strlen(string); ++a) {
337                 if (string[a]<32) string[a]=32;
338                 if (string[a]>126) string[a]=32;
339                 }
340
341         /* Remove leading and trailing blanks */
342         while(string[0]<33) strcpy(string,&string[1]);
343         while(string[strlen(string)-1]<33) string[strlen(string)-1]=0;
344
345         /* Remove double blanks */
346         for (a=0; a<strlen(string); ++a) {
347                 if ((string[a]==32)&&(string[a+1]==32)) {
348                         strcpy(&string[a],&string[a+1]);
349                         a=0;
350                         }
351                 }
352
353         /* remove characters which would interfere with the network */
354         for (a=0; a<strlen(string); ++a) {
355                 if (string[a]=='!') strcpy(&string[a],&string[a+1]);
356                 if (string[a]=='@') strcpy(&string[a],&string[a+1]);
357                 if (string[a]=='_') strcpy(&string[a],&string[a+1]);
358                 if (string[a]==',') strcpy(&string[a],&string[a+1]);
359                 if (string[a]=='%') strcpy(&string[a],&string[a+1]);
360                 if (string[a]=='|') strcpy(&string[a],&string[a+1]);
361                 }
362
363         }
364
365
366 #ifndef HAVE_STRERROR
367 /*
368  * replacement strerror() for systems that don't have it
369  */
370 char *strerror(int e)
371 {
372         static char buf[128];
373
374         sprintf(buf,"errno = %d",e);
375         return(buf);
376         }
377 #endif
378
379
380 void progress(long int curr, long int cmax)
381 {
382         static long dots_printed;
383         long a;
384
385         if (curr==0) {
386                 scr_printf(".......................................");
387                 scr_printf(".......................................\r");
388                 scr_flush();
389                 dots_printed = 0;
390                 }
391         else if (curr==cmax) {
392                 scr_printf("\r%79s\n","");
393                 }
394         else {
395                 a=(curr * 100) / cmax;
396                 a=a*78; a=a/100;
397                 while (dots_printed < a) {
398                         scr_printf("*");
399                         ++dots_printed;
400                         scr_flush();
401                         }
402                 }
403         }
404
405
406 /*
407  * NOT the same locate_host() in locate_host.c.  This one just does a
408  * 'who am i' to try to discover where the user is...
409  */
410 void locate_host(char *hbuf)
411 {
412 #ifndef HAVE_UTMP_H
413         char buf[SIZ];
414         FILE *who;
415         int a,b;
416
417         who = (FILE *)popen("who am i","r");
418         if (who==NULL) {
419                 strcpy(hbuf,serv_info.serv_fqdn);
420                 return; 
421                 }
422         fgets(buf,sizeof buf,who);
423         pclose(who);
424
425         b = 0;
426         for (a=0; a<strlen(buf); ++a) {
427                 if ((buf[a]=='(')||(buf[a]==')')) ++b;
428                 }
429         if (b<2) {
430                 strcpy(hbuf,serv_info.serv_fqdn);
431                 return;
432                 }
433
434         for (a=0; a<strlen(buf); ++a) {
435                 if (buf[a]=='(') {
436                         strcpy(buf,&buf[a+1]);
437                         }
438                 }
439         for (a=0; a<strlen(buf); ++a) {
440                 if (buf[a]==')') buf[a] = 0;
441                 }
442
443         if (strlen(buf)==0) strcpy(hbuf,serv_info.serv_fqdn);
444         else strncpy(hbuf,buf,24);
445 #else
446         char *tty = ttyname(0);
447 #ifdef HAVE_GETUTXLINE
448         struct utmpx ut, *put;
449 #else
450         struct utmp ut, *put;
451 #endif
452
453         if (tty == NULL) {
454             fail:
455                 safestrncpy(hbuf, serv_info.serv_fqdn, 24);
456                 return;
457                 }
458
459         if (strncmp(tty, "/dev/", 5))
460                 goto fail;
461
462         safestrncpy(ut.ut_line, &tty[5], sizeof ut.ut_line);
463
464 #ifdef HAVE_GETUTXLINE /* Solaris uses this */
465         if ((put = getutxline(&ut)) == NULL)
466 #else
467         if ((put = getutline(&ut)) == NULL)
468 #endif
469                 goto fail;
470
471 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
472         if (put->ut_type == USER_PROCESS) {
473 #endif
474 #if defined(HAVE_UT_HOST) || defined(HAVE_GETUTXLINE)
475                 if (*put->ut_host)
476                         safestrncpy(hbuf, put->ut_host, 24);
477                 else
478 #endif
479                         safestrncpy(hbuf, put->ut_line, 24);
480 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
481                 }
482         else goto fail;
483 #endif
484 #endif /* HAVE_UTMP_H */
485         }
486
487 /*
488  * miscellaneous server commands (testing, etc.)
489  */
490 void misc_server_cmd(char *cmd) {
491         char buf[SIZ];
492
493         serv_puts(cmd);
494         serv_gets(buf);
495         scr_printf("%s\n",buf);
496         if (buf[0]=='1') {
497                 set_keepalives(KA_NO);
498                 while (serv_gets(buf), strcmp(buf,"000")) {
499                         scr_printf("%s\n",buf);
500                         }
501                 set_keepalives(KA_YES);
502                 return;
503                 }
504         if (buf[0]=='4') {
505                 do {
506                         newprompt("> ",buf,255);
507                         serv_puts(buf);
508                         } while(strcmp(buf,"000"));
509                 return;
510                 }
511         }
512
513
514 /*
515  * compute the checksum of a file
516  */
517 int file_checksum(char *filename)
518 {
519         int cksum = 0;
520         int ch;
521         FILE *fp;
522
523         fp = fopen(filename,"r");
524         if (fp == NULL) return(0);
525
526         /* yes, this algorithm may allow cksum to overflow, but that's ok
527          * as long as it overflows consistently, which it will.
528          */
529         while (ch=getc(fp), ch>=0) {
530                 cksum = (cksum + ch);
531                 }
532
533         fclose(fp);
534         return(cksum);
535         }
536
537 /*
538  * nuke a directory and its contents
539  */
540 int nukedir(char *dirname)
541 {
542         DIR *dp;
543         struct dirent *d;
544         char filename[SIZ];
545
546         dp = opendir(dirname);
547         if (dp == NULL) {
548                 return(errno);
549                 }
550
551         while (d = readdir(dp), d != NULL) {
552                 sprintf(filename, "%s/%s", dirname, d->d_name);
553                 unlink(filename);
554                 }
555
556         closedir(dp);
557         return(rmdir(dirname));
558         }