* fix bug in locale declaration array (missing ,)
[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 extern char **AvailLang;
256 int GetLocalePrefs(void)
257 {
258         int nLocales;
259         StrBuf *Buf;
260         char buf[SIZ];
261         char dialog_result[PATH_MAX];
262         FILE *fp;
263         int i = 0;
264         int offs = 0;
265
266
267         nLocales = 0; 
268         while (!IsEmptyStr(AvailLang[nLocales]))
269                 nLocales++;
270
271         Buf = NewStrBuf();
272
273         StrBufAppendBufPlain(Buf, HKEY("Select the locale webcit should use : \n"), 0);
274 #ifdef HAVE_USELOCALE 
275         StrBufAppendBufPlain(Buf, HKEY(" 0 Let the user select it at the login prompt (default)\n"), 0);
276         offs ++;
277 #endif
278         for (i = 0; i < nLocales; i++) {
279                 StrBufAppendPrintf(Buf, " %ld: %s\n", i + offs, AvailLang[i]);
280
281         }
282
283         switch (setup_type) {
284         case UI_TEXT:
285                 title("WebCit setup");
286                 printf("\n%s\n", ChrPtr(Buf));
287                 printf("This is currently set to:\n%ld\n", 0L);
288                 printf("Enter new value or press return to leave unchanged:\n");
289                 if (fgets(buf, sizeof buf, stdin))
290                         return atoi(buf);
291                 break;
292
293         case UI_DIALOG:
294                 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
295                 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%ld' 2>%s",
296                         getenv("CTDL_DIALOG"),
297                         ChrPtr(Buf),
298                         0L,
299                         dialog_result);
300                 system(buf);
301                 fp = fopen(dialog_result, "r");
302                 if (fp != NULL) {
303                         char *str = &buf[0];
304                         if (fgets(str, sizeof buf, fp)){
305                                 if (str[strlen(str)-1] == 10) {
306                                         str[strlen(str)-1] = 0;
307                                 }
308                         }
309                         fclose(fp);
310                         unlink(dialog_result);
311                         return atoi(buf);
312                 }
313                 break;
314
315         }
316         return 0;
317 }
318
319 void important_message(char *title, char *msgtext)
320 {
321         char buf[SIZ];
322
323         switch (setup_type) {
324
325         case UI_TEXT:
326                 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");
327                 printf("       %s \n\n%s\n\n", title, msgtext);
328                 printf("Press return to continue...");
329                 if (fgets(buf, sizeof buf, stdin));
330                 break;
331
332         case UI_DIALOG:
333                 sprintf(buf, "exec %s --msgbox '%s' 19 72",
334                         getenv("CTDL_DIALOG"),
335                         msgtext);
336                 system(buf);
337                 break;
338         }
339 }
340
341
342 void display_error(char *error_message)
343 {
344         important_message("Error", error_message);
345 }
346
347 void progress(char *text, long int curr, long int cmax)
348 {
349         static long dots_printed = 0L;
350         long a = 0;
351         char buf[SIZ];
352         static FILE *fp = NULL;
353
354         switch (setup_type) {
355
356         case UI_TEXT:
357                 if (curr == 0) {
358                         printf("%s\n", text);
359                         printf("..........................");
360                         printf("..........................");
361                         printf("..........................\r");
362                         fflush(stdout);
363                         dots_printed = 0;
364                 } else if (curr == cmax) {
365                         printf("\r%79s\n", "");
366                 } else {
367                         a = (curr * 100) / cmax;
368                         a = a * 78;
369                         a = a / 100;
370                         while (dots_printed < a) {
371                                 printf("*");
372                                 ++dots_printed;
373                                 fflush(stdout);
374                         }
375                 }
376                 break;
377
378         case UI_DIALOG:
379                 if (curr == 0) {
380                         sprintf(buf, "exec %s --gauge '%s' 7 72 0",
381                                 getenv("CTDL_DIALOG"),
382                                 text);
383                         fp = popen(buf, "w");
384                         if (fp != NULL) {
385                                 fprintf(fp, "0\n");
386                                 fflush(fp);
387                         }
388                 } 
389                 else if (curr == cmax) {
390                         if (fp != NULL) {
391                                 fprintf(fp, "100\n");
392                                 pclose(fp);
393                                 fp = NULL;
394                         }
395                 }
396                 else {
397                         a = (curr * 100) / cmax;
398                         if (fp != NULL) {
399                                 fprintf(fp, "%ld\n", a);
400                                 fflush(fp);
401                         }
402                 }
403                 break;
404         }
405 }
406
407
408
409
410 /*
411  * install_init_scripts()  -- Create and deploy SysV init scripts.
412  *
413  */
414 void install_init_scripts(void)
415 {
416 #ifdef ENABLE_NLS
417         int localechoice;
418 #endif
419         char question[1024];
420         char buf[256];
421         char http_port[128];
422 #ifdef HAVE_OPENSSL
423         char https_port[128];
424 #endif
425         char hostname[128];
426         char portname[128];
427         char command[SIZ];
428         struct utsname my_utsname;
429         struct stat etcinitd;
430         FILE *fp;
431         char *initfile = "/etc/init.d/webcit";
432
433         fp = fopen(initfile, "r");
434         if (fp != NULL) {
435                 if (yesno("WebCit already appears to be configured to start at boot.\n"
436                           "Would you like to keep your boot configuration as is?\n", 1) == 1) {
437                         return;
438                 }
439                 fclose(fp);
440                 
441         }
442
443         /* Otherwise, prompt the user to create an entry. */
444         snprintf(question, sizeof question,
445                  "Would you like to automatically start WebCit at boot?"
446                 );
447         if (yesno(question, 1) == 0)
448                 return;
449
450
451 #ifdef ENABLE_NLS
452
453         localechoice = GetLocalePrefs();
454
455 #endif
456         /* Defaults */
457         sprintf(http_port, "2000");
458 #ifdef HAVE_OPENSSL
459         sprintf(https_port, "443");
460 #endif
461         sprintf(hostname, "uds");
462         sprintf(portname, "/usr/local/citadel");
463
464         /* This is a very hackish way of learning the port numbers used
465          * in a previous install, if we are upgrading: read them out of
466          * the existing init script.
467          */
468         if ((stat("/etc/init.d/", &etcinitd) == -1) && 
469             (errno == ENOENT))
470         {
471                 if ((stat("/etc/rc.d/init.d/", &etcinitd) == -1) &&
472                     (errno == ENOENT))
473                         initfile = WEBCITDIR"/webcit.init";
474                 else
475                         initfile = "/etc/rc.d/init.d/webcit";
476         }
477
478         fp = fopen(initfile, "r");
479         if (fp != NULL) {
480                 while (fgets(buf, sizeof buf, fp) != NULL) {
481                         if (strlen(buf) > 0) {
482                                 buf[strlen(buf)-1] = 0; /* strip trailing cr */
483                         }
484                         if (!strncasecmp(buf, "HTTP_PORT=", 10)) {
485                                 safestrncpy(http_port, &buf[10], sizeof http_port);
486                         }
487 #ifdef HAVE_OPENSSL
488                         if (!strncasecmp(buf, "HTTPS_PORT=", 11)) {
489                                 safestrncpy(https_port, &buf[11], sizeof https_port);
490                         }
491 #endif
492                         if (!strncasecmp(buf, "CTDL_HOSTNAME=", 14)) {
493                                 safestrncpy(hostname, &buf[14], sizeof hostname);
494                         }
495                         if (!strncasecmp(buf, "CTDL_PORTNAME=", 14)) {
496                                 safestrncpy(portname, &buf[14], sizeof portname);
497                         }
498                 }
499                 fclose(fp);
500         }
501
502         /* Now ask for the port numbers */
503         snprintf(question, sizeof question,
504                  "On which port do you want WebCit to listen for HTTP "
505                  "requests?\n\nYou can use the standard port (80) if you are "
506                  "not running another\nweb server (such as Apache), otherwise "
507                  "select another port.");
508         set_value(question, http_port);
509         uname(&my_utsname);
510         sprintf(suggested_url, "http://%s:%s/", my_utsname.nodename, http_port);
511
512 #ifdef HAVE_OPENSSL
513         snprintf(question, sizeof question,
514                  "On which port do you want WebCit to listen for HTTPS "
515                  "requests?\n\nYou can use the standard port (443) if you are "
516                  "not running another\nweb server (such as Apache), otherwise "
517                  "select another port.");
518         set_value(question, https_port);
519 #endif
520
521         /* Find out where Citadel is. */
522         if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
523                 strcpy(hostname, "uds");
524                 strcpy(portname, getenv("CITADEL"));
525         }
526         else {
527                 snprintf(question, sizeof question,
528                          "Is the Citadel service running on the same host as WebCit?");
529                 if (yesno(question, ((!strcasecmp(hostname, "uds")) ? 1 : 0))) {
530                         strcpy(hostname, "uds");
531                         if (atoi(portname) != 0) strcpy(portname, "/usr/local/citadel");
532                         set_value("In what directory is Citadel installed?", portname);
533                 }
534                 else {
535                         if (!strcasecmp(hostname, "uds")) strcpy(hostname, "127.0.0.1");
536                         if (atoi(portname) == 0) strcpy(portname, "504");
537                         set_value("Enter the host name or IP address of your "
538                                   "Citadel server.", hostname);
539                         set_value("Enter the port number on which Citadel is "
540                                   "running (usually 504)", portname);
541                 }
542         }
543
544
545         fp = fopen(initfile, "w");
546
547         fprintf(fp,     "#!/bin/sh\n"
548                 "\n"
549                 "# uncomment this to create coredumps as described in\n"
550                 "# http://www.citadel.org/doku.php/faq:mastering_your_os:gdb#how.do.i.make.my.system.produce.core-files\n"
551                 "# ulimit -c unlimited\n"
552                         "WEBCIT_DIR=%s\n", setup_directory);
553         fprintf(fp,     "HTTP_PORT=%s\n", http_port);
554 #ifdef HAVE_OPENSSL
555         fprintf(fp,     "HTTPS_PORT=%s\n", https_port);
556 #endif
557         fprintf(fp,     "CTDL_HOSTNAME=%s\n", hostname);
558         fprintf(fp,     "CTDL_PORTNAME=%s\n", portname);
559
560 #ifdef ENABLE_NLS
561         
562         if (localechoice == 0) {
563 #ifdef HAVE_USELOCALE 
564                 fprintf(fp, "unset LANG\n");
565 #else
566                 fprintf(fp, "export WEBCIT_LANG=c\n");
567 #endif
568         }
569         else {
570                 fprintf(fp, "export WEBCIT_LANG=%s\n", AvailLang[localechoice - localeoffset]);
571
572         }
573 #else
574         fprintf(fp,     "# your system doesn't support locales\n");
575 #endif
576         fprintf(fp,     "\n"
577                         "\n"
578                         "case \"$1\" in\n"
579                         "\n"
580                         "start)         echo -n \"Starting WebCit... \"\n"
581                         "               if   $WEBCIT_DIR/webcit "
582                                                         "-D/var/run/webcit.pid "
583                                                         "-p$HTTP_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
584                         "               then\n"
585                         "                       echo \"ok\"\n"
586                         "               else\n"
587                         "                       echo \"failed\"\n"
588                         "               fi\n");
589 #ifdef HAVE_OPENSSL
590         fprintf(fp,     "               echo -n \"Starting WebCit SSL... \"\n"
591                         "               if  $WEBCIT_DIR/webcit "
592                                                         "-D/var/run/webcit-ssl.pid "
593                                                         "-s -p$HTTPS_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
594                         "               then\n"
595                         "                       echo \"ok\"\n"
596                         "               else\n"
597                         "                       echo \"failed\"\n"
598                         "               fi\n");
599 #endif
600         fprintf(fp,     "               ;;\n"
601                         "stop)          echo -n \"Stopping WebCit... \"\n"
602                         "               if kill `cat /var/run/webcit.pid 2>/dev/null` 2>/dev/null\n"
603                         "               then\n"
604                         "                       echo \"ok\"\n"
605                         "               else\n"
606                         "                       echo \"failed\"\n"
607                         "               fi\n"
608                         "               rm -f /var/run/webcit.pid 2>/dev/null\n");
609 #ifdef HAVE_OPENSSL
610         fprintf(fp,     "               echo -n \"Stopping WebCit SSL... \"\n"
611                         "               if kill `cat /var/run/webcit-ssl.pid 2>/dev/null` 2>/dev/null\n"
612                         "               then\n"
613                         "                       echo \"ok\"\n"
614                         "               else\n"
615                         "                       echo \"failed\"\n"
616                         "               fi\n"
617                         "               rm -f /var/run/webcit-ssl.pid 2>/dev/null\n");
618 #endif
619         fprintf(fp,     "               ;;\n"
620                         "restart)       $0 stop\n"
621                         "               $0 start\n"
622                         "               ;;\n"
623                         "*)             echo \"Usage: $0 {start|stop|restart}\"\n"
624                         "               exit 1\n"
625                         "               ;;\n"
626                         "esac\n"
627         );
628
629         fclose(fp);
630         chmod(initfile, 0755);
631
632         /* Set up the run levels. */
633         system("/bin/rm -f /etc/rc?.d/[SK]??webcit 2>/dev/null");
634         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);
635         system(command);
636         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);
637         system(command);
638
639 }
640
641
642
643
644 /*
645  * Figure out what type of user interface we're going to use
646  */
647 int discover_ui(void)
648 {
649
650         /* Use "dialog" if we have it */
651         if (getenv("CTDL_DIALOG") != NULL) {
652                 return UI_DIALOG;
653         }
654                 
655         return UI_TEXT;
656 }
657
658
659
660
661
662 int main(int argc, char *argv[])
663 {
664         int a;
665         char aaa[256];
666         int info_only = 0;
667         strcpy(suggested_url, "http://<your_host_name>:<port>/");
668
669         /* set an invalid setup type */
670         setup_type = (-1);
671
672         /* Check to see if we're running the web installer */
673         if (getenv("CITADEL_INSTALLER") != NULL) {
674                 using_web_installer = 1;
675         }
676
677         /* parse command line args */
678         for (a = 0; a < argc; ++a) {
679                 if (!strncmp(argv[a], "-u", 2)) {
680                         strcpy(aaa, argv[a]);
681                         strcpy(aaa, &aaa[2]);
682                         setup_type = atoi(aaa);
683                 }
684                 if (!strcmp(argv[a], "-i")) {
685                         info_only = 1;
686                 }
687                 if (!strcmp(argv[a], "-q")) {
688                         setup_type = UI_SILENT;
689                 }
690         }
691
692
693         /* If a setup type was not specified, try to determine automatically
694          * the best one to use out of all available types.
695          */
696         if (setup_type < 0) {
697                 setup_type = discover_ui();
698         }
699         if (info_only == 1) {
700                 important_message("WebCit Setup", "Welcome to WebCit setup");
701                 cleanup(0);
702         }
703
704         /* Get started in a valid setup directory. */
705         strcpy(setup_directory, WEBCITDIR);
706         if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
707                 strcpy(setup_directory, getenv("WEBCIT"));
708         }
709         else {
710                 set_value("In what directory is WebCit installed?",
711                         setup_directory);
712         }
713         if (chdir(setup_directory) != 0) {
714                 important_message("WebCit Setup",
715                           "The directory you specified does not exist.");
716                 cleanup(errno);
717         }
718
719         /*
720          * We used to start WebCit by putting it directly into /etc/inittab.
721          * Since some systems are moving away from init, we can't do this anymore.
722          */
723         progress("Removing obsolete /etc/inittab entries...", 0, 1);
724         delete_the_old_way();
725         progress("Removing obsolete /etc/inittab entries...", 1, 1);
726
727         /* Now begin. */
728         switch (setup_type) {
729
730         case UI_TEXT:
731                 printf("\n\n\n"
732                         "               *** WebCit setup program ***\n\n");
733                 break;
734
735         }
736
737         /* 
738          * If we're running on SysV, install init scripts.
739          */
740         if (!access("/var/run", W_OK)) {
741                 install_init_scripts();
742
743                 if (!access("/etc/init.d/webcit", X_OK)) {
744                         system("/etc/init.d/webcit stop");
745                         system("/etc/init.d/webcit start");
746                 }
747
748                 sprintf(aaa,
749                         "Setup is finished.  You may now log in.\n"
750                         "Point your web browser at %s\n", suggested_url
751                 );
752                 important_message("Setup finished", aaa);
753         }
754
755         else {
756                 important_message("Setup finished",
757                         "Setup is finished.  You may now start the server.");
758         }
759
760         cleanup(0);
761         return 0;
762 }