* check for /etc/rc.d/init.d if /etc/init.d/ is not there and create the file there.
[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         char command[SIZ];
337         struct utsname my_utsname;
338         struct stat etcinitd;
339         FILE *fp;
340         char *initfile = "/etc/init.d/webcit";
341
342         /* Otherwise, prompt the user to create an entry. */
343         snprintf(question, sizeof question,
344                 "Would you like to automatically start WebCit at boot?"
345         );
346         if (yesno(question, 1) == 0)
347                 return;
348
349         /* Defaults */
350         sprintf(http_port, "2000");
351 #ifdef HAVE_OPENSSL
352         sprintf(https_port, "443");
353 #endif
354         sprintf(hostname, "uds");
355         sprintf(portname, "/usr/local/citadel");
356
357         /* This is a very hackish way of learning the port numbers used
358          * in a previous install, if we are upgrading: read them out of
359          * the existing init script.
360          */
361         if ((stat("/etc/init.d/", &etcinitd) == -1) && 
362             (errno == ENOENT))
363         {
364                 if ((stat("/etc/rc.d/init.d/", &etcinitd) == -1) &&
365                     (errno == ENOENT))
366                         initfile = PREFIX"/webcit.init";
367                 else
368                         initfile = "/etc/rc.d/init.d/webcit";
369         }
370
371         fp = fopen(initfile, "r");
372         if (fp != NULL) {
373                 while (fgets(buf, sizeof buf, fp) != NULL) {
374                         if (strlen(buf) > 0) {
375                                 buf[strlen(buf)-1] = 0; /* strip trailing cr */
376                         }
377                         if (!strncasecmp(buf, "HTTP_PORT=", 10)) {
378                                 safestrncpy(http_port, &buf[10], sizeof http_port);
379                         }
380 #ifdef HAVE_OPENSSL
381                         if (!strncasecmp(buf, "HTTPS_PORT=", 11)) {
382                                 safestrncpy(https_port, &buf[11], sizeof https_port);
383                         }
384 #endif
385                         if (!strncasecmp(buf, "CTDL_HOSTNAME=", 14)) {
386                                 safestrncpy(hostname, &buf[14], sizeof hostname);
387                         }
388                         if (!strncasecmp(buf, "CTDL_PORTNAME=", 14)) {
389                                 safestrncpy(portname, &buf[14], sizeof portname);
390                         }
391                 }
392                 fclose(fp);
393         }
394
395         /* Now ask for the port numbers */
396         snprintf(question, sizeof question,
397                 "On which port do you want WebCit to listen for HTTP "
398                 "requests?\n\nYou can use the standard port (80) if you are "
399                 "not running another\nweb server (such as Apache), otherwise "
400                 "select another port.");
401         set_value(question, http_port);
402         uname(&my_utsname);
403         sprintf(suggested_url, "http://%s:%s/", my_utsname.nodename, http_port);
404
405 #ifdef HAVE_OPENSSL
406         snprintf(question, sizeof question,
407                 "On which port do you want WebCit to listen for HTTPS "
408                 "requests?\n\nYou can use the standard port (443) if you are "
409                 "not running another\nweb server (such as Apache), otherwise "
410                 "select another port.");
411         set_value(question, https_port);
412 #endif
413
414         /* Find out where Citadel is. */
415         if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
416                 strcpy(hostname, "uds");
417                 strcpy(portname, getenv("CITADEL"));
418         }
419         else {
420                 snprintf(question, sizeof question,
421                         "Is the Citadel service running on the same host as WebCit?");
422                 if (yesno(question, ((!strcasecmp(hostname, "uds")) ? 1 : 0))) {
423                         strcpy(hostname, "uds");
424                         if (atoi(portname) != 0) strcpy(portname, "/usr/local/citadel");
425                         set_value("In what directory is Citadel installed?", portname);
426                 }
427                 else {
428                         if (!strcasecmp(hostname, "uds")) strcpy(hostname, "127.0.0.1");
429                         if (atoi(portname) == 0) strcpy(portname, "504");
430                         set_value("Enter the host name or IP address of your "
431                                 "Citadel server.", hostname);
432                         set_value("Enter the port number on which Citadel is "
433                                 "running (usually 504)", portname);
434                 }
435         }
436
437
438         fp = fopen(initfile, "w");
439
440         fprintf(fp,     "#!/bin/sh\n"
441                         "\n"
442                         "WEBCIT_DIR=%s\n", setup_directory);
443         fprintf(fp,     "HTTP_PORT=%s\n", http_port);
444 #ifdef HAVE_OPENSSL
445         fprintf(fp,     "HTTPS_PORT=%s\n", https_port);
446 #endif
447         fprintf(fp,     "CTDL_HOSTNAME=%s\n", hostname);
448         fprintf(fp,     "CTDL_PORTNAME=%s\n", portname);
449         fprintf(fp,     "\n"
450                         "\n"
451                         "case \"$1\" in\n"
452                         "\n"
453                         "start)         echo -n \"Starting WebCit... \"\n"
454                         "               if   $WEBCIT_DIR/webserver "
455                                                         "-D/var/run/webcit.pid "
456                                                         "-p$HTTP_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
457                         "               then\n"
458                         "                       echo \"ok\"\n"
459                         "               else\n"
460                         "                       echo \"failed\"\n"
461                         "               fi\n");
462 #ifdef HAVE_OPENSSL
463         fprintf(fp,     "               echo -n \"Starting WebCit SSL... \"\n"
464                         "               if  $WEBCIT_DIR/webserver "
465                                                         "-D/var/run/webcit-ssl.pid "
466                                                         "-s -p$HTTPS_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
467                         "               then\n"
468                         "                       echo \"ok\"\n"
469                         "               else\n"
470                         "                       echo \"failed\"\n"
471                         "               fi\n");
472 #endif
473         fprintf(fp,     "               ;;\n"
474                         "stop)          echo -n \"Stopping WebCit... \"\n"
475                         "               if kill `cat /var/run/webcit.pid 2>/dev/null` 2>/dev/null\n"
476                         "               then\n"
477                         "                       echo \"ok\"\n"
478                         "               else\n"
479                         "                       echo \"failed\"\n"
480                         "               fi\n"
481                         "               rm -f /var/run/webcit.pid 2>/dev/null\n");
482 #ifdef HAVE_OPENSSL
483         fprintf(fp,     "               echo -n \"Stopping WebCit SSL... \"\n"
484                         "               if kill `cat /var/run/webcit-ssl.pid 2>/dev/null` 2>/dev/null\n"
485                         "               then\n"
486                         "                       echo \"ok\"\n"
487                         "               else\n"
488                         "                       echo \"failed\"\n"
489                         "               fi\n"
490                         "               rm -f /var/run/webcit-ssl.pid 2>/dev/null\n");
491 #endif
492         fprintf(fp,     "               ;;\n"
493                         "restart)       $0 stop\n"
494                         "               $0 start\n"
495                         "               ;;\n"
496                         "*)             echo \"Usage: $0 {start|stop|restart}\"\n"
497                         "               exit 1\n"
498                         "               ;;\n"
499                         "esac\n"
500         );
501
502         fclose(fp);
503         chmod(initfile, 0755);
504
505         /* Set up the run levels. */
506         system("/bin/rm -f /etc/rc?.d/[SK]??webcit 2>/dev/null");
507         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);
508         system(command);
509         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);
510         system(command);
511
512 }
513
514
515
516
517 /*
518  * Figure out what type of user interface we're going to use
519  */
520 int discover_ui(void)
521 {
522
523         /* Use "dialog" if we have it */
524         if (getenv("CTDL_DIALOG") != NULL) {
525                 return UI_DIALOG;
526         }
527                 
528         return UI_TEXT;
529 }
530
531
532
533
534
535 int main(int argc, char *argv[])
536 {
537         int a;
538         char aaa[256];
539         int info_only = 0;
540         strcpy(suggested_url, "http://<your_host_name>:<port>/");
541
542         /* set an invalid setup type */
543         setup_type = (-1);
544
545         /* Check to see if we're running the web installer */
546         if (getenv("CITADEL_INSTALLER") != NULL) {
547                 using_web_installer = 1;
548         }
549
550         /* parse command line args */
551         for (a = 0; a < argc; ++a) {
552                 if (!strncmp(argv[a], "-u", 2)) {
553                         strcpy(aaa, argv[a]);
554                         strcpy(aaa, &aaa[2]);
555                         setup_type = atoi(aaa);
556                 }
557                 if (!strcmp(argv[a], "-i")) {
558                         info_only = 1;
559                 }
560                 if (!strcmp(argv[a], "-q")) {
561                         setup_type = UI_SILENT;
562                 }
563         }
564
565
566         /* If a setup type was not specified, try to determine automatically
567          * the best one to use out of all available types.
568          */
569         if (setup_type < 0) {
570                 setup_type = discover_ui();
571         }
572         if (info_only == 1) {
573                 important_message("WebCit Setup", "Welcome to WebCit setup");
574                 cleanup(0);
575         }
576
577         /* Get started in a valid setup directory. */
578         strcpy(setup_directory, PREFIX);
579         if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
580                 strcpy(setup_directory, getenv("WEBCIT"));
581         }
582         else {
583                 set_value("In what directory is WebCit installed?",
584                         setup_directory);
585         }
586         if (chdir(setup_directory) != 0) {
587                 important_message("WebCit Setup",
588                           "The directory you specified does not exist.");
589                 cleanup(errno);
590         }
591
592         /*
593          * We used to start WebCit by putting it directly into /etc/inittab.
594          * Since some systems are moving away from init, we can't do this anymore.
595          */
596         progress("Removing obsolete /etc/inittab entries...", 0, 1);
597         delete_the_old_way();
598         progress("Removing obsolete /etc/inittab entries...", 1, 1);
599
600         /* Now begin. */
601         switch (setup_type) {
602
603         case UI_TEXT:
604                 printf("\n\n\n"
605                         "               *** WebCit setup program ***\n\n");
606                 break;
607
608         }
609
610         /* 
611          * If we're running on SysV, install init scripts.
612          */
613         if (!access("/var/run", W_OK)) {
614                 install_init_scripts();
615
616                 if (!access("/etc/init.d/webcit", X_OK)) {
617                         system("/etc/init.d/webcit stop");
618                         system("/etc/init.d/webcit start");
619                 }
620
621                 sprintf(aaa,
622                         "Setup is finished.  You may now log in.\n"
623                         "Point your web browser at %s\n", suggested_url
624                 );
625                 important_message("Setup finished", aaa);
626         }
627
628         else {
629                 important_message("Setup finished",
630                         "Setup is finished.  You may now start the server.");
631         }
632
633         cleanup(0);
634         return 0;
635 }