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