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