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