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