* -d to daemonize, -D to daemonize with specifying
[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()  -- Create and deploy SysV init scripts.
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 -d /var/run || exit 0\n"
397                         "\n"
398                         "case \"$1\" in\n"
399                         "\n"
400                         "start)         echo -n \"Starting WebCit... \"\n"
401                         "               if   $WEBCIT_DIR/webserver "
402                                                         "-D/var/run/webcit.pid "
403                                                         "-t/dev/null "
404                                                         "-p$HTTP_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
405                         "               then\n"
406                         "                       echo \"ok\"\n"
407                         "               else\n"
408                         "                       echo \"failed\"\n"
409                         "               fi\n");
410 #ifdef HAVE_OPENSSL
411         fprintf(fp,     "               echo -n \"Starting WebCit SSL... \"\n"
412                         "               if  $WEBCIT_DIR/webserver "
413                                                         "-D/var/run/webcit-ssl.pid "
414                                                         "-t/dev/null "
415                                                         "-s -p$HTTPS_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
416                         "               then\n"
417                         "                       echo \"ok\"\n"
418                         "               else\n"
419                         "                       echo \"failed\"\n"
420                         "               fi\n");
421 #endif
422         fprintf(fp,     "               ;;\n"
423                         "stop)          echo -n \"Stopping WebCit... \"\n"
424                         "               if kill `cat /var/run/webcit.pid 2>/dev/null` 2>/dev/null\n"
425                         "               then\n"
426                         "                       echo \"ok\"\n"
427                         "               else\n"
428                         "                       echo \"failed\"\n"
429                         "               fi\n"
430                         "               rm -f /var/run/webcit.pid 2>/dev/null\n");
431 #ifdef HAVE_OPENSSL
432         fprintf(fp,     "               echo -n \"Stopping WebCit SSL... \"\n"
433                         "               if kill `cat /var/run/webcit-ssl.pid 2>/dev/null` 2>/dev/null\n"
434                         "               then\n"
435                         "                       echo \"ok\"\n"
436                         "               else\n"
437                         "                       echo \"failed\"\n"
438                         "               fi\n"
439                         "               rm -f /var/run/webcit-ssl.pid 2>/dev/null\n");
440 #endif
441         fprintf(fp,     "               ;;\n"
442                         "restart)       $0 stop\n"
443                         "               $0 start\n"
444                         "               ;;\n"
445                         "*)             echo \"Usage: $0 {start|stop|restart}\"\n"
446                         "               exit 1\n"
447                         "               ;;\n"
448                         "esac\n"
449         );
450
451         fclose(fp);
452         chmod("/etc/init.d/webcit", 0755);
453
454         /* Set up the run levels. */
455         system("/bin/rm -f /etc/rc?.d/[SK]??webcit 2>/dev/null");
456         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");
457         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");
458
459 }
460
461
462
463
464 /*
465  * Figure out what type of user interface we're going to use
466  */
467 int discover_ui(void)
468 {
469
470         /* Use "dialog" if we have it */
471         if (getenv("CTDL_DIALOG") != NULL) {
472                 return UI_DIALOG;
473         }
474                 
475         return UI_TEXT;
476 }
477
478
479
480
481
482 int main(int argc, char *argv[])
483 {
484         int a;
485         char aaa[256];
486         int info_only = 0;
487         strcpy(suggested_url, "http://<your_host_name>:<port>/");
488
489         /* set an invalid setup type */
490         setup_type = (-1);
491
492         /* Check to see if we're running the web installer */
493         if (getenv("CITADEL_INSTALLER") != NULL) {
494                 using_web_installer = 1;
495         }
496
497         /* parse command line args */
498         for (a = 0; a < argc; ++a) {
499                 if (!strncmp(argv[a], "-u", 2)) {
500                         strcpy(aaa, argv[a]);
501                         strcpy(aaa, &aaa[2]);
502                         setup_type = atoi(aaa);
503                 }
504                 if (!strcmp(argv[a], "-i")) {
505                         info_only = 1;
506                 }
507                 if (!strcmp(argv[a], "-q")) {
508                         setup_type = UI_SILENT;
509                 }
510         }
511
512
513         /* If a setup type was not specified, try to determine automatically
514          * the best one to use out of all available types.
515          */
516         if (setup_type < 0) {
517                 setup_type = discover_ui();
518         }
519         if (info_only == 1) {
520                 important_message("WebCit Setup", "Welcome to WebCit setup");
521                 cleanup(0);
522         }
523
524         /* Get started in a valid setup directory. */
525         strcpy(setup_directory, PREFIX);
526         if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
527                 strcpy(setup_directory, getenv("WEBCIT"));
528         }
529         else {
530                 set_value("In what directory is WebCit installed?",
531                         setup_directory);
532         }
533         if (chdir(setup_directory) != 0) {
534                 important_message("WebCit Setup",
535                           "The directory you specified does not exist.");
536                 cleanup(errno);
537         }
538
539         /*
540          * We used to start WebCit by putting it directly into /etc/inittab.
541          * Since some systems are moving away from init, we can't do this anymore.
542          */
543         progress("Removing obsolete /etc/inittab entries...", 0, 1);
544         delete_the_old_way();
545         progress("Removing obsolete /etc/inittab entries...", 1, 1);
546
547         /* Now begin. */
548         switch (setup_type) {
549
550         case UI_TEXT:
551                 printf("\n\n\n"
552                         "               *** WebCit setup program ***\n\n");
553                 break;
554
555         }
556
557         /* 
558          * If we're running on SysV, install init scripts.
559          */
560         if (!access("/var/run", W_OK)) {
561                 install_init_scripts();
562
563                 if (!access("/etc/init.d/webcit", X_OK)) {
564                         system("/etc/init.d/webcit stop");
565                         system("/etc/init.d/webcit start");
566                 }
567
568                 sprintf(aaa,
569                         "Setup is finished.  You may now log in.\n"
570                         "Point your web browser at %s\n", suggested_url
571                 );
572                 important_message("Setup finished", aaa);
573         }
574
575         else {
576                 important_message("Setup finished",
577                         "Setup is finished.  You may now start the server.");
578         }
579
580         cleanup(0);
581         return 0;
582 }