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