* Remove newt from the webcit build too.
[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 int yesno(char *question)
141 {
142         int i = 0;
143         int answer = 0;
144         char buf[SIZ];
145
146         switch (setup_type) {
147
148         case UI_TEXT:
149                 do {
150                         printf("%s\nYes/No --> ", question);
151                         fgets(buf, sizeof buf, stdin);
152                         answer = tolower(buf[0]);
153                         if (answer == 'y')
154                                 answer = 1;
155                         else if (answer == 'n')
156                                 answer = 0;
157                 } while ((answer < 0) || (answer > 1));
158                 break;
159
160         case UI_DIALOG:
161                 sprintf(buf, "exec %s --yesno '%s' 10 72",
162                         getenv("CTDL_DIALOG"),
163                         question);
164                 i = system(buf);
165                 if (i == 0) {
166                         answer = 1;
167                 }
168                 else {
169                         answer = 0;
170                 }
171                 break;
172
173         }
174         return (answer);
175 }
176
177 void set_value(char *prompt, char str[])
178 {
179         char buf[SIZ];
180         char dialog_result[PATH_MAX];
181         char setupmsg[SIZ];
182         FILE *fp;
183
184         strcpy(setupmsg, "");
185
186         switch (setup_type) {
187         case UI_TEXT:
188                 title("WebCit setup");
189                 printf("\n%s\n", prompt);
190                 printf("This is currently set to:\n%s\n", str);
191                 printf("Enter new value or press return to leave unchanged:\n");
192                 fgets(buf, sizeof buf, stdin);
193                 buf[strlen(buf) - 1] = 0;
194                 if (strlen(buf) != 0)
195                         strcpy(str, buf);
196                 break;
197
198         case UI_DIALOG:
199                 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
200                 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%s' 2>%s",
201                         getenv("CTDL_DIALOG"),
202                         prompt,
203                         str,
204                         dialog_result);
205                 system(buf);
206                 fp = fopen(dialog_result, "r");
207                 if (fp != NULL) {
208                         fgets(str, sizeof buf, fp);
209                         if (str[strlen(str)-1] == 10) {
210                                 str[strlen(str)-1] = 0;
211                         }
212                         fclose(fp);
213                         unlink(dialog_result);
214                 }
215                 break;
216
217         }
218 }
219
220
221 void important_message(char *title, char *msgtext)
222 {
223         char buf[SIZ];
224
225         switch (setup_type) {
226
227         case UI_TEXT:
228                 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");
229                 printf("       %s \n\n%s\n\n", title, msgtext);
230                 printf("Press return to continue...");
231                 fgets(buf, sizeof buf, stdin);
232                 break;
233
234         case UI_DIALOG:
235                 sprintf(buf, "exec %s --msgbox '%s' 19 72",
236                         getenv("CTDL_DIALOG"),
237                         msgtext);
238                 system(buf);
239                 break;
240         }
241 }
242
243
244 void display_error(char *error_message)
245 {
246         important_message("Error", error_message);
247 }
248
249 void progress(char *text, long int curr, long int cmax)
250 {
251         static long dots_printed = 0L;
252         long a = 0;
253         char buf[SIZ];
254         static FILE *fp = NULL;
255
256         switch (setup_type) {
257
258         case UI_TEXT:
259                 if (curr == 0) {
260                         printf("%s\n", text);
261                         printf("..........................");
262                         printf("..........................");
263                         printf("..........................\r");
264                         fflush(stdout);
265                         dots_printed = 0;
266                 } else if (curr == cmax) {
267                         printf("\r%79s\n", "");
268                 } else {
269                         a = (curr * 100) / cmax;
270                         a = a * 78;
271                         a = a / 100;
272                         while (dots_printed < a) {
273                                 printf("*");
274                                 ++dots_printed;
275                                 fflush(stdout);
276                         }
277                 }
278                 break;
279
280         case UI_DIALOG:
281                 if (curr == 0) {
282                         sprintf(buf, "exec %s --gauge '%s' 7 72 0",
283                                 getenv("CTDL_DIALOG"),
284                                 text);
285                         fp = popen(buf, "w");
286                         if (fp != NULL) {
287                                 fprintf(fp, "0\n");
288                                 fflush(fp);
289                         }
290                 } 
291                 else if (curr == cmax) {
292                         if (fp != NULL) {
293                                 fprintf(fp, "100\n");
294                                 pclose(fp);
295                                 fp = NULL;
296                         }
297                 }
298                 else {
299                         a = (curr * 100) / cmax;
300                         if (fp != NULL) {
301                                 fprintf(fp, "%ld\n", a);
302                                 fflush(fp);
303                         }
304                 }
305                 break;
306         }
307 }
308
309
310
311
312 /*
313  * install_init_scripts()  -- Make sure "webserver" is in /etc/inittab
314  *
315  */
316 void install_init_scripts(void)
317 {
318         char question[SIZ];
319         char http_port[128];
320 #ifdef HAVE_OPENSSL
321         char https_port[128];
322 #endif
323         char hostname[128];
324         char portname[128];
325         struct utsname my_utsname;
326
327         FILE *fp;
328
329         /* Otherwise, prompt the user to create an entry. */
330         snprintf(question, sizeof question,
331                 "Would you like to automatically start WebCit at boot?"
332         );
333         if (yesno(question) == 0)
334                 return;
335
336         snprintf(question, sizeof question,
337                 "On which port do you want WebCit to listen for HTTP "
338                 "requests?\n\nYou can use the standard port (80) if you are "
339                 "not running another\nweb server (such as Apache), otherwise "
340                 "select another port.");
341         sprintf(http_port, "2000");
342         set_value(question, http_port);
343         uname(&my_utsname);
344         sprintf(suggested_url, "http://%s:%s/", my_utsname.nodename, http_port);
345
346 #ifdef HAVE_OPENSSL
347         snprintf(question, sizeof question,
348                 "On which port do you want WebCit to listen for HTTPS "
349                 "requests?\n\nYou can use the standard port (443) if you are "
350                 "not running another\nweb server (such as Apache), otherwise "
351                 "select another port.");
352         sprintf(https_port, "443");
353         set_value(question, https_port);
354 #endif
355
356         /* Find out where Citadel is. */
357         if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
358                 strcpy(hostname, "uds");
359                 strcpy(portname, getenv("CITADEL"));
360         }
361         else {
362                 snprintf(question, sizeof question,
363                         "Is the Citadel service running on the same host as WebCit?");
364                 if (yesno(question)) {
365                         sprintf(hostname, "uds");
366                         sprintf(portname, "/usr/local/citadel");
367                         set_value("In what directory is Citadel installed?", portname);
368                 }
369                 else {
370                         sprintf(hostname, "127.0.0.1");
371                         sprintf(portname, "504");
372                         set_value("Enter the host name or IP address of your "
373                                 "Citadel server.", hostname);
374                         set_value("Enter the port number on which Citadel is "
375                                 "running (usually 504)", portname);
376                 }
377         }
378
379
380         fp = fopen("/etc/init.d/webcit", "w");
381         if (fp == NULL) {
382                 display_error("Cannot create /etc/init.d/webcit");
383                 return;
384         }
385
386         fprintf(fp,     "#!/bin/sh\n"
387                         "\n"
388                         "WEBCIT_DIR=%s\n", setup_directory);
389         fprintf(fp,     "HTTP_PORT=%s\n", http_port);
390 #ifdef HAVE_OPENSSL
391         fprintf(fp,     "HTTPS_PORT=%s\n", https_port);
392 #endif
393         fprintf(fp,     "CTDL_HOSTNAME=%s\n", hostname);
394         fprintf(fp,     "CTDL_PORTNAME=%s\n", portname);
395         fprintf(fp,     "\n"
396                         "test -x $WEBCIT_DIR/ctdlsvc || exit 0\n"
397                         "test -d /var/run || exit 0\n"
398                         "\n"
399                         "case \"$1\" in\n"
400                         "\n"
401                         "start)         echo -n \"Starting WebCit... \"\n"
402                         "               if $WEBCIT_DIR/ctdlsvc /var/run/webcit.pid "
403                                                         "$WEBCIT_DIR/webserver "
404                                                         "-t/dev/null "
405                                                         "-p$HTTP_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
406                         "               then\n"
407                         "                       echo \"ok\"\n"
408                         "               else\n"
409                         "                       echo \"failed\"\n"
410                         "               fi\n");
411 #ifdef HAVE_OPENSSL
412         fprintf(fp,     "               echo -n \"Starting WebCit SSL... \"\n"
413                         "               if $WEBCIT_DIR/ctdlsvc /var/run/webcit-ssl.pid "
414                                                         "$WEBCIT_DIR/webserver "
415                                                         "-t/dev/null "
416                                                         "-s -p$HTTPS_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
417                         "               then\n"
418                         "                       echo \"ok\"\n"
419                         "               else\n"
420                         "                       echo \"failed\"\n"
421                         "               fi\n");
422 #endif
423         fprintf(fp,     "               ;;\n"
424                         "stop)          echo -n \"Stopping WebCit... \"\n"
425                         "               if kill `cat /var/run/webcit.pid 2>/dev/null` 2>/dev/null\n"
426                         "               then\n"
427                         "                       echo \"ok\"\n"
428                         "               else\n"
429                         "                       echo \"failed\"\n"
430                         "               fi\n"
431                         "               rm -f /var/run/webcit.pid 2>/dev/null\n");
432 #ifdef HAVE_OPENSSL
433         fprintf(fp,     "               echo -n \"Stopping WebCit SSL... \"\n"
434                         "               if kill `cat /var/run/webcit-ssl.pid 2>/dev/null` 2>/dev/null\n"
435                         "               then\n"
436                         "                       echo \"ok\"\n"
437                         "               else\n"
438                         "                       echo \"failed\"\n"
439                         "               fi\n"
440                         "               rm -f /var/run/webcit-ssl.pid 2>/dev/null\n");
441 #endif
442         fprintf(fp,     "               ;;\n"
443                         "restart)       $0 stop\n"
444                         "               $0 start\n"
445                         "               ;;\n"
446                         "*)             echo \"Usage: $0 {start|stop|restart}\"\n"
447                         "               exit 1\n"
448                         "               ;;\n"
449                         "esac\n"
450         );
451
452         fclose(fp);
453         chmod("/etc/init.d/webcit", 0755);
454
455         /* Set up the run levels. */
456         system("/bin/rm -f /etc/rc?.d/[SK]??webcit 2>/dev/null");
457         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");
458         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");
459
460 }
461
462
463
464
465 /*
466  * Figure out what type of user interface we're going to use
467  */
468 int discover_ui(void)
469 {
470
471         /* Use "dialog" if we have it */
472         if (getenv("CTDL_DIALOG") != NULL) {
473                 return UI_DIALOG;
474         }
475                 
476         return UI_TEXT;
477 }
478
479
480
481
482
483 int main(int argc, char *argv[])
484 {
485         int a;
486         char aaa[256];
487         int info_only = 0;
488         strcpy(suggested_url, "http://<your_host_name>:<port>/");
489
490         /* set an invalid setup type */
491         setup_type = (-1);
492
493         /* Check to see if we're running the web installer */
494         if (getenv("CITADEL_INSTALLER") != NULL) {
495                 using_web_installer = 1;
496         }
497
498         /* parse command line args */
499         for (a = 0; a < argc; ++a) {
500                 if (!strncmp(argv[a], "-u", 2)) {
501                         strcpy(aaa, argv[a]);
502                         strcpy(aaa, &aaa[2]);
503                         setup_type = atoi(aaa);
504                 }
505                 if (!strcmp(argv[a], "-i")) {
506                         info_only = 1;
507                 }
508                 if (!strcmp(argv[a], "-q")) {
509                         setup_type = UI_SILENT;
510                 }
511         }
512
513
514         /* If a setup type was not specified, try to determine automatically
515          * the best one to use out of all available types.
516          */
517         if (setup_type < 0) {
518                 setup_type = discover_ui();
519         }
520         if (info_only == 1) {
521                 important_message("WebCit Setup", "Welcome to WebCit setup");
522                 cleanup(0);
523         }
524
525         /* Get started in a valid setup directory. */
526         strcpy(setup_directory, PREFIX);
527         if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
528                 strcpy(setup_directory, getenv("WEBCIT"));
529         }
530         else {
531                 set_value("In what directory is WebCit installed?",
532                         setup_directory);
533         }
534         if (chdir(setup_directory) != 0) {
535                 important_message("WebCit Setup",
536                           "The directory you specified does not exist.");
537                 cleanup(errno);
538         }
539
540         /*
541          * We used to start WebCit by putting it directly into /etc/inittab.
542          * Since some systems are moving away from init, we can't do this anymore.
543          */
544         progress("Removing obsolete /etc/inittab entries...", 0, 1);
545         delete_the_old_way();
546         progress("Removing obsolete /etc/inittab entries...", 1, 1);
547
548         /* Now begin. */
549         switch (setup_type) {
550
551         case UI_TEXT:
552                 printf("\n\n\n"
553                         "               *** WebCit setup program ***\n\n");
554                 break;
555
556         }
557
558         /* 
559          * If we're running on SysV, install init scripts.
560          */
561         if (!access("/var/run", W_OK)) {
562                 install_init_scripts();
563
564                 if (!access("/etc/init.d/webcit", X_OK)) {
565                         system("/etc/init.d/webcit stop");
566                         system("/etc/init.d/webcit start");
567                 }
568
569                 sprintf(aaa,
570                         "Setup is finished.  You may now log in.\n"
571                         "Point your web browser at %s\n", suggested_url
572                 );
573                 important_message("Setup finished", aaa);
574         }
575
576         else {
577                 important_message("Setup finished",
578                         "Setup is finished.  You may now start the server.");
579         }
580
581         cleanup(0);
582         return 0;
583 }