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