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