* use the new regular way to get the accept language header
[citadel.git] / webcit / setup.c
1 /*
2  * $Id$
3  *
4  * WebCit setup utility
5  * 
6  * (This is basically just an install wizard.  It's not required.)
7  *
8  */
9
10 #include "sysdep.h"
11 #include "webcit.h"
12 #include "webserver.h"
13
14
15 #define UI_TEXT         0       /* Default setup type -- text only */
16 #define UI_DIALOG       2       /* Use the 'dialog' program */
17 #define UI_SILENT       3       /* Silent running, for use in scripts */
18
19 int setup_type;
20 char setup_directory[SIZ];
21 int using_web_installer = 0;
22 char suggested_url[SIZ];
23
24 /* some copies... */
25 int lprintf(int loglevel, const char *format, ...){return 0;}
26 void wc_printf(const char *format,...){}
27
28 void RegisterNS(const char *NSName, long len, 
29                 int nMinArgs, 
30                 int nMaxArgs, 
31                 WCHandlerFunc HandlerFunc,
32                 WCPreevalFunc PreEvalFunc,
33                 int ContextRequired){}
34 void RegisterHeaderHandler(const char *Name, long Len, Header_Evaluator F){}
35 pthread_key_t MyConKey;
36
37 #ifdef ENABLE_NLS
38
39 #ifdef HAVE_USELOCALE 
40 int localeoffset = 1;
41 #else
42 int localeoffset = 0;
43 #endif
44
45 #endif
46 /*
47  * Delete an entry from /etc/inittab
48  */
49 void delete_init_entry(char *which_entry)
50 {
51         char *inittab = NULL;
52         FILE *fp;
53         char buf[SIZ];
54         char entry[SIZ];
55         char levels[SIZ];
56         char state[SIZ];
57         char prog[SIZ];
58
59         inittab = strdup("");
60         if (inittab == NULL) return;
61
62         fp = fopen("/etc/inittab", "r");
63         if (fp == NULL) return;
64
65         while(fgets(buf, sizeof buf, fp) != NULL) {
66
67                 if (num_tokens(buf, ':') == 4) {
68                         extract_token(entry, buf, 0, ':', sizeof entry);
69                         extract_token(levels, buf, 1, ':', sizeof levels);
70                         extract_token(state, buf, 2, ':', sizeof state);
71                         extract_token(prog, buf, 3, ':', sizeof prog); /* includes 0x0a LF */
72
73                         if (!strcmp(entry, which_entry)) {
74                                 strcpy(state, "off");   /* disable it */
75                         }
76                 }
77
78                 inittab = realloc(inittab, strlen(inittab) + strlen(buf) + 2);
79                 if (inittab == NULL) {
80                         fclose(fp);
81                         return;
82                 }
83                 
84                 strcat(inittab, buf);
85         }
86         fclose(fp);
87         fp = fopen("/etc/inittab", "w");
88         if (fp != NULL) {
89                 fwrite(inittab, strlen(inittab), 1, fp);
90                 fclose(fp);
91                 kill(1, SIGHUP);        /* Tell init to re-read /etc/inittab */
92         }
93         free(inittab);
94 }
95
96
97
98
99 /* 
100  * Remove any /etc/inittab entries for webcit, because we don't
101  * start it that way anymore.
102  */
103 void delete_the_old_way(void) {
104         FILE *infp;
105         char buf[1024];
106         char looking_for[1024];
107         int have_entry = 0;
108         char entry[1024];
109         char prog[1024];
110         char init_entry[1024];
111
112
113         strcpy(init_entry, "");
114
115         /* Determine the fully qualified path name of webcit */
116         snprintf(looking_for, sizeof looking_for, "%s/webcit ", setup_directory);
117
118         /* Pound through /etc/inittab line by line.  Set have_entry to 1 if
119          * an entry is found which we believe starts webcit.
120          */
121         infp = fopen("/etc/inittab", "r");
122         if (infp == NULL) {
123                 return;
124         } else {
125                 while (fgets(buf, sizeof buf, infp) != NULL) {
126                         buf[strlen(buf) - 1] = 0;
127                         extract_token(entry, buf, 0, ':', sizeof entry);
128                         extract_token(prog, buf, 3, ':', sizeof prog);
129                         if (!strncasecmp(prog, looking_for,
130                            strlen(looking_for))) {
131                                 ++have_entry;
132                                 strcpy(init_entry, entry);
133                         }
134                 }
135                 fclose(infp);
136         }
137
138         /* Bail out if there's nothing to do. */
139         if (!have_entry) return;
140
141         delete_init_entry(init_entry);
142 }
143
144
145
146 void cleanup(int exitcode)
147 {
148         exit(exitcode);
149 }
150
151
152
153 void title(char *text)
154 {
155         if (setup_type == UI_TEXT) {
156                 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<%s>\n", text);
157         }
158 }
159
160
161
162
163 int yesno(char *question, int default_value)
164 {
165         int i = 0;
166         int answer = 0;
167         char buf[SIZ] = "";
168
169         switch (setup_type) {
170
171         case UI_TEXT:
172                 do {
173                         printf("%s\nYes/No [%s] --> ",
174                                 question,
175                                 ( default_value ? "Yes" : "No" )
176                         );
177                         if (fgets(buf, sizeof buf, stdin))
178                         {
179                                 answer = tolower(buf[0]);
180                                 if ((buf[0]==0) || (buf[0]==13) || (buf[0]==10))
181                                         answer = default_value;
182                                 else if (answer == 'y')
183                                         answer = 1;
184                                 else if (answer == 'n')
185                                         answer = 0;
186                         }
187
188                 } while ((answer < 0) || (answer > 1));
189                 break;
190
191         case UI_DIALOG:
192                 sprintf(buf, "exec %s %s --yesno '%s' 15 75",
193                         getenv("CTDL_DIALOG"),
194                         ( default_value ? "" : "--defaultno" ),
195                         question);
196                 i = system(buf);
197                 if (i == 0) {
198                         answer = 1;
199                 }
200                 else {
201                         answer = 0;
202                 }
203                 break;
204
205         }
206         return (answer);
207 }
208
209
210
211
212 void set_value(char *prompt, char str[])
213 {
214         char buf[SIZ] = "";
215         char dialog_result[PATH_MAX];
216         char setupmsg[SIZ];
217         FILE *fp;
218
219         strcpy(setupmsg, "");
220
221         switch (setup_type) {
222         case UI_TEXT:
223                 title("WebCit setup");
224                 printf("\n%s\n", prompt);
225                 printf("This is currently set to:\n%s\n", str);
226                 printf("Enter new value or press return to leave unchanged:\n");
227                 if (fgets(buf, sizeof buf, stdin)) {
228                         buf[strlen(buf) - 1] = 0;
229                 }
230                 if (strlen(buf) != 0)
231                         strcpy(str, buf);
232                 break;
233         case UI_DIALOG:
234                 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
235                 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%s' 2>%s",
236                         getenv("CTDL_DIALOG"),
237                         prompt,
238                         str,
239                         dialog_result);
240                 system(buf);
241                 fp = fopen(dialog_result, "r");
242                 if (fp != NULL) {
243                         if (fgets(str, sizeof buf, fp)){
244                                 if (str[strlen(str)-1] == 10) {
245                                         str[strlen(str)-1] = 0;
246                                 }
247                         }
248                         fclose(fp);
249                         unlink(dialog_result);
250                 }
251                 break;
252
253         }
254 }
255
256
257 extern const char *AvailLang[];
258 int GetLocalePrefs(void)
259 {
260         int nLocales;
261         StrBuf *Buf;
262         char buf[SIZ];
263         char dialog_result[PATH_MAX];
264         FILE *fp;
265         int i = 0;
266         int offs = 0;
267
268
269         nLocales = 0; 
270         while (!IsEmptyStr(AvailLang[nLocales]))
271                 nLocales++;
272
273         Buf = NewStrBuf();
274
275         StrBufAppendBufPlain(Buf, HKEY("Select the locale webcit should use : \n"), 0);
276 #ifdef HAVE_USELOCALE 
277         StrBufAppendBufPlain(Buf, HKEY(" 0 Let the user select it at the login prompt (default)\n"), 0);
278         offs ++;
279 #endif
280         for (i = 0; i < nLocales; i++) {
281                 StrBufAppendPrintf(Buf, " %ld: %s\n", i + offs, AvailLang[i]);
282
283         }
284
285         switch (setup_type) {
286         case UI_TEXT:
287                 title("WebCit setup");
288                 printf("\n%s\n", ChrPtr(Buf));
289                 printf("This is currently set to:\n%ld\n", 0L);
290                 printf("Enter new value or press return to leave unchanged:\n");
291                 if (fgets(buf, sizeof buf, stdin))
292                         return atoi(buf);
293                 break;
294
295         case UI_DIALOG:
296                 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
297                 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%ld' 2>%s",
298                         getenv("CTDL_DIALOG"),
299                         ChrPtr(Buf),
300                         0L,
301                         dialog_result);
302                 system(buf);
303                 fp = fopen(dialog_result, "r");
304                 if (fp != NULL) {
305                         char *str = &buf[0];
306                         if (fgets(str, sizeof buf, fp)){
307                                 if (str[strlen(str)-1] == 10) {
308                                         str[strlen(str)-1] = 0;
309                                 }
310                         }
311                         fclose(fp);
312                         unlink(dialog_result);
313                         return atoi(buf);
314                 }
315                 break;
316
317         }
318         return 0;
319 }
320
321 void important_message(char *title, char *msgtext)
322 {
323         char buf[SIZ];
324
325         switch (setup_type) {
326
327         case UI_TEXT:
328                 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
329                 printf("       %s \n\n%s\n\n", title, msgtext);
330                 printf("Press return to continue...");
331                 if (fgets(buf, sizeof buf, stdin));
332                 break;
333
334         case UI_DIALOG:
335                 sprintf(buf, "exec %s --msgbox '%s' 19 72",
336                         getenv("CTDL_DIALOG"),
337                         msgtext);
338                 system(buf);
339                 break;
340         }
341 }
342
343
344 void display_error(char *error_message)
345 {
346         important_message("Error", error_message);
347 }
348
349 void progress(char *text, long int curr, long int cmax)
350 {
351         static long dots_printed = 0L;
352         long a = 0;
353         char buf[SIZ];
354         static FILE *fp = NULL;
355
356         switch (setup_type) {
357
358         case UI_TEXT:
359                 if (curr == 0) {
360                         printf("%s\n", text);
361                         printf("..........................");
362                         printf("..........................");
363                         printf("..........................\r");
364                         fflush(stdout);
365                         dots_printed = 0;
366                 } else if (curr == cmax) {
367                         printf("\r%79s\n", "");
368                 } else {
369                         a = (curr * 100) / cmax;
370                         a = a * 78;
371                         a = a / 100;
372                         while (dots_printed < a) {
373                                 printf("*");
374                                 ++dots_printed;
375                                 fflush(stdout);
376                         }
377                 }
378                 break;
379
380         case UI_DIALOG:
381                 if (curr == 0) {
382                         sprintf(buf, "exec %s --gauge '%s' 7 72 0",
383                                 getenv("CTDL_DIALOG"),
384                                 text);
385                         fp = popen(buf, "w");
386                         if (fp != NULL) {
387                                 fprintf(fp, "0\n");
388                                 fflush(fp);
389                         }
390                 } 
391                 else if (curr == cmax) {
392                         if (fp != NULL) {
393                                 fprintf(fp, "100\n");
394                                 pclose(fp);
395                                 fp = NULL;
396                         }
397                 }
398                 else {
399                         a = (curr * 100) / cmax;
400                         if (fp != NULL) {
401                                 fprintf(fp, "%ld\n", a);
402                                 fflush(fp);
403                         }
404                 }
405                 break;
406         }
407 }
408
409
410
411
412 /*
413  * install_init_scripts()  -- Create and deploy SysV init scripts.
414  *
415  */
416 void install_init_scripts(void)
417 {
418 #ifdef ENABLE_NLS
419         int localechoice;
420 #endif
421         char question[1024];
422         char buf[256];
423         char http_port[128];
424 #ifdef HAVE_OPENSSL
425         char https_port[128];
426 #endif
427         char hostname[128];
428         char portname[128];
429         char command[SIZ];
430         struct utsname my_utsname;
431         struct stat etcinitd;
432         FILE *fp;
433         char *initfile = "/etc/init.d/webcit";
434
435         fp = fopen(initfile, "r");
436         if (fp != NULL) {
437                 if (yesno("WebCit already appears to be configured to start at boot.\n"
438                           "Would you like to keep your boot configuration as is?\n", 1) == 1) {
439                         return;
440                 }
441                 fclose(fp);
442                 
443         }
444
445         /* Otherwise, prompt the user to create an entry. */
446         snprintf(question, sizeof question,
447                  "Would you like to automatically start WebCit at boot?"
448                 );
449         if (yesno(question, 1) == 0)
450                 return;
451
452
453 #ifdef ENABLE_NLS
454
455         localechoice = GetLocalePrefs();
456
457 #endif
458         /* Defaults */
459         sprintf(http_port, "2000");
460 #ifdef HAVE_OPENSSL
461         sprintf(https_port, "443");
462 #endif
463         sprintf(hostname, "uds");
464         sprintf(portname, "/usr/local/citadel");
465
466         /* This is a very hackish way of learning the port numbers used
467          * in a previous install, if we are upgrading: read them out of
468          * the existing init script.
469          */
470         if ((stat("/etc/init.d/", &etcinitd) == -1) && 
471             (errno == ENOENT))
472         {
473                 if ((stat("/etc/rc.d/init.d/", &etcinitd) == -1) &&
474                     (errno == ENOENT))
475                         initfile = WEBCITDIR"/webcit.init";
476                 else
477                         initfile = "/etc/rc.d/init.d/webcit";
478         }
479
480         fp = fopen(initfile, "r");
481         if (fp != NULL) {
482                 while (fgets(buf, sizeof buf, fp) != NULL) {
483                         if (strlen(buf) > 0) {
484                                 buf[strlen(buf)-1] = 0; /* strip trailing cr */
485                         }
486                         if (!strncasecmp(buf, "HTTP_PORT=", 10)) {
487                                 safestrncpy(http_port, &buf[10], sizeof http_port);
488                         }
489 #ifdef HAVE_OPENSSL
490                         if (!strncasecmp(buf, "HTTPS_PORT=", 11)) {
491                                 safestrncpy(https_port, &buf[11], sizeof https_port);
492                         }
493 #endif
494                         if (!strncasecmp(buf, "CTDL_HOSTNAME=", 14)) {
495                                 safestrncpy(hostname, &buf[14], sizeof hostname);
496                         }
497                         if (!strncasecmp(buf, "CTDL_PORTNAME=", 14)) {
498                                 safestrncpy(portname, &buf[14], sizeof portname);
499                         }
500                 }
501                 fclose(fp);
502         }
503
504         /* Now ask for the port numbers */
505         snprintf(question, sizeof question,
506                  "On which port do you want WebCit to listen for HTTP "
507                  "requests?\n\nYou can use the standard port (80) if you are "
508                  "not running another\nweb server (such as Apache), otherwise "
509                  "select another port.");
510         set_value(question, http_port);
511         uname(&my_utsname);
512         sprintf(suggested_url, "http://%s:%s/", my_utsname.nodename, http_port);
513
514 #ifdef HAVE_OPENSSL
515         snprintf(question, sizeof question,
516                  "On which port do you want WebCit to listen for HTTPS "
517                  "requests?\n\nYou can use the standard port (443) if you are "
518                  "not running another\nweb server (such as Apache), otherwise "
519                  "select another port.");
520         set_value(question, https_port);
521 #endif
522
523         /* Find out where Citadel is. */
524         if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
525                 strcpy(hostname, "uds");
526                 strcpy(portname, getenv("CITADEL"));
527         }
528         else {
529                 snprintf(question, sizeof question,
530                          "Is the Citadel service running on the same host as WebCit?");
531                 if (yesno(question, ((!strcasecmp(hostname, "uds")) ? 1 : 0))) {
532                         strcpy(hostname, "uds");
533                         if (atoi(portname) != 0) strcpy(portname, "/usr/local/citadel");
534                         set_value("In what directory is Citadel installed?", portname);
535                 }
536                 else {
537                         if (!strcasecmp(hostname, "uds")) strcpy(hostname, "127.0.0.1");
538                         if (atoi(portname) == 0) strcpy(portname, "504");
539                         set_value("Enter the host name or IP address of your "
540                                   "Citadel server.", hostname);
541                         set_value("Enter the port number on which Citadel is "
542                                   "running (usually 504)", portname);
543                 }
544         }
545
546
547         fp = fopen(initfile, "w");
548
549         fprintf(fp,     "#!/bin/sh\n"
550                 "\n"
551                 "# uncomment this to create coredumps as described in\n"
552                 "# http://www.citadel.org/doku.php/faq:mastering_your_os:gdb#how.do.i.make.my.system.produce.core-files\n"
553                 "# ulimit -c unlimited\n"
554                         "WEBCIT_DIR=%s\n", setup_directory);
555         fprintf(fp,     "HTTP_PORT=%s\n", http_port);
556 #ifdef HAVE_OPENSSL
557         fprintf(fp,     "HTTPS_PORT=%s\n", https_port);
558 #endif
559         fprintf(fp,     "CTDL_HOSTNAME=%s\n", hostname);
560         fprintf(fp,     "CTDL_PORTNAME=%s\n", portname);
561
562 #ifdef ENABLE_NLS
563         
564         if (localechoice == 0) {
565 #ifdef HAVE_USELOCALE 
566                 fprintf(fp, "unset LANG\n");
567 #else
568                 fprintf(fp, "export WEBCIT_LANG=c\n");
569 #endif
570         }
571         else {
572                 fprintf(fp, "export WEBCIT_LANG=%s\n", AvailLang[localechoice - localeoffset]);
573
574         }
575 #else
576         fprintf(fp,     "# your system doesn't support locales\n");
577 #endif
578         fprintf(fp,     "\n"
579                         "\n"
580                         "case \"$1\" in\n"
581                         "\n"
582                         "start)         echo -n \"Starting WebCit... \"\n"
583                         "               if   $WEBCIT_DIR/webcit "
584                                                         "-D/var/run/webcit.pid "
585                                                         "-p$HTTP_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
586                         "               then\n"
587                         "                       echo \"ok\"\n"
588                         "               else\n"
589                         "                       echo \"failed\"\n"
590                         "               fi\n");
591 #ifdef HAVE_OPENSSL
592         fprintf(fp,     "               echo -n \"Starting WebCit SSL... \"\n"
593                         "               if  $WEBCIT_DIR/webcit "
594                                                         "-D/var/run/webcit-ssl.pid "
595                                                         "-s -p$HTTPS_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
596                         "               then\n"
597                         "                       echo \"ok\"\n"
598                         "               else\n"
599                         "                       echo \"failed\"\n"
600                         "               fi\n");
601 #endif
602         fprintf(fp,     "               ;;\n"
603                         "stop)          echo -n \"Stopping WebCit... \"\n"
604                         "               if kill `cat /var/run/webcit.pid 2>/dev/null` 2>/dev/null\n"
605                         "               then\n"
606                         "                       echo \"ok\"\n"
607                         "               else\n"
608                         "                       echo \"failed\"\n"
609                         "               fi\n"
610                         "               rm -f /var/run/webcit.pid 2>/dev/null\n");
611 #ifdef HAVE_OPENSSL
612         fprintf(fp,     "               echo -n \"Stopping WebCit SSL... \"\n"
613                         "               if kill `cat /var/run/webcit-ssl.pid 2>/dev/null` 2>/dev/null\n"
614                         "               then\n"
615                         "                       echo \"ok\"\n"
616                         "               else\n"
617                         "                       echo \"failed\"\n"
618                         "               fi\n"
619                         "               rm -f /var/run/webcit-ssl.pid 2>/dev/null\n");
620 #endif
621         fprintf(fp,     "               ;;\n"
622                         "restart)       $0 stop\n"
623                         "               $0 start\n"
624                         "               ;;\n"
625                         "*)             echo \"Usage: $0 {start|stop|restart}\"\n"
626                         "               exit 1\n"
627                         "               ;;\n"
628                         "esac\n"
629         );
630
631         fclose(fp);
632         chmod(initfile, 0755);
633
634         /* Set up the run levels. */
635         system("/bin/rm -f /etc/rc?.d/[SK]??webcit 2>/dev/null");
636         snprintf(command, sizeof(command), "for x in 2 3 4 5 ; do [ -d /etc/rc$x.d ] && ln -s %s /etc/rc$x.d/S84webcit ; done 2>/dev/null", initfile);
637         system(command);
638         snprintf(command, sizeof(command), "for x in 0 6 S; do [ -d /etc/rc$x.d ] && ln -s %s /etc/rc$x.d/K15webcit ; done 2>/dev/null", initfile);
639         system(command);
640
641 }
642
643
644
645
646 /*
647  * Figure out what type of user interface we're going to use
648  */
649 int discover_ui(void)
650 {
651
652         /* Use "dialog" if we have it */
653         if (getenv("CTDL_DIALOG") != NULL) {
654                 return UI_DIALOG;
655         }
656                 
657         return UI_TEXT;
658 }
659
660
661
662
663
664 int main(int argc, char *argv[])
665 {
666         int a;
667         char aaa[256];
668         int info_only = 0;
669         strcpy(suggested_url, "http://<your_host_name>:<port>/");
670
671         /* set an invalid setup type */
672         setup_type = (-1);
673
674         /* Check to see if we're running the web installer */
675         if (getenv("CITADEL_INSTALLER") != NULL) {
676                 using_web_installer = 1;
677         }
678
679         /* parse command line args */
680         for (a = 0; a < argc; ++a) {
681                 if (!strncmp(argv[a], "-u", 2)) {
682                         strcpy(aaa, argv[a]);
683                         strcpy(aaa, &aaa[2]);
684                         setup_type = atoi(aaa);
685                 }
686                 if (!strcmp(argv[a], "-i")) {
687                         info_only = 1;
688                 }
689                 if (!strcmp(argv[a], "-q")) {
690                         setup_type = UI_SILENT;
691                 }
692         }
693
694
695         /* If a setup type was not specified, try to determine automatically
696          * the best one to use out of all available types.
697          */
698         if (setup_type < 0) {
699                 setup_type = discover_ui();
700         }
701         if (info_only == 1) {
702                 important_message("WebCit Setup", "Welcome to WebCit setup");
703                 cleanup(0);
704         }
705
706         /* Get started in a valid setup directory. */
707         strcpy(setup_directory, WEBCITDIR);
708         if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
709                 strcpy(setup_directory, getenv("WEBCIT"));
710         }
711         else {
712                 set_value("In what directory is WebCit installed?",
713                         setup_directory);
714         }
715         if (chdir(setup_directory) != 0) {
716                 important_message("WebCit Setup",
717                           "The directory you specified does not exist.");
718                 cleanup(errno);
719         }
720
721         /*
722          * We used to start WebCit by putting it directly into /etc/inittab.
723          * Since some systems are moving away from init, we can't do this anymore.
724          */
725         progress("Removing obsolete /etc/inittab entries...", 0, 1);
726         delete_the_old_way();
727         progress("Removing obsolete /etc/inittab entries...", 1, 1);
728
729         /* Now begin. */
730         switch (setup_type) {
731
732         case UI_TEXT:
733                 printf("\n\n\n"
734                         "               *** WebCit setup program ***\n\n");
735                 break;
736
737         }
738
739         /* 
740          * If we're running on SysV, install init scripts.
741          */
742         if (!access("/var/run", W_OK)) {
743                 install_init_scripts();
744
745                 if (!access("/etc/init.d/webcit", X_OK)) {
746                         system("/etc/init.d/webcit stop");
747                         system("/etc/init.d/webcit start");
748                 }
749
750                 sprintf(aaa,
751                         "Setup is finished.  You may now log in.\n"
752                         "Point your web browser at %s\n", suggested_url
753                 );
754                 important_message("Setup finished", aaa);
755         }
756
757         else {
758                 important_message("Setup finished",
759                         "Setup is finished.  You may now start the server.");
760         }
761
762         cleanup(0);
763         return 0;
764 }