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