6 * (This is basically just an install wizard. It's not required.)
12 #include "webserver.h"
20 #define UI_TEXT 0 /* Default setup type -- text only */
21 #define UI_DIALOG 2 /* Use the 'dialog' program */
22 #define UI_SILENT 3 /* Silent running, for use in scripts */
23 #define UI_NEWT 4 /* Use the "newt" window library */
26 char setup_directory[SIZ];
27 int using_web_installer = 0;
28 char suggested_url[SIZ];
31 * Set an entry in inittab to the desired state
33 void delete_init_entry(char *which_entry)
44 if (inittab == NULL) return;
46 fp = fopen("/etc/inittab", "r");
47 if (fp == NULL) return;
49 while(fgets(buf, sizeof buf, fp) != NULL) {
51 if (num_tokens(buf, ':') == 4) {
52 extract_token(entry, buf, 0, ':', sizeof entry);
53 extract_token(levels, buf, 1, ':', sizeof levels);
54 extract_token(state, buf, 2, ':', sizeof state);
55 extract_token(prog, buf, 3, ':', sizeof prog); /* includes 0x0a LF */
57 if (!strcmp(entry, which_entry)) {
58 buf[0] = 0; /* delete it */
62 inittab = realloc(inittab, strlen(inittab) + strlen(buf) + 2);
63 if (inittab == NULL) {
71 fp = fopen("/etc/inittab", "w");
73 fwrite(inittab, strlen(inittab), 1, fp);
75 kill(1, SIGHUP); /* Tell init to re-read /etc/inittab */
84 * Remove any /etc/inittab entries for webcit, because we don't
85 * start it that way anymore.
87 void delete_the_old_way(void) {
90 char looking_for[1024];
94 char init_entry[1024];
97 strcpy(init_entry, "");
99 /* Determine the fully qualified path name of webserver */
100 snprintf(looking_for, sizeof looking_for, "%s/webserver ", setup_directory);
102 /* Pound through /etc/inittab line by line. Set have_entry to 1 if
103 * an entry is found which we believe starts webserver.
105 infp = fopen("/etc/inittab", "r");
109 while (fgets(buf, sizeof buf, infp) != NULL) {
110 buf[strlen(buf) - 1] = 0;
111 extract_token(entry, buf, 0, ':', sizeof entry);
112 extract_token(prog, buf, 3, ':', sizeof prog);
113 if (!strncasecmp(prog, looking_for,
114 strlen(looking_for))) {
116 strcpy(init_entry, entry);
122 /* Bail out if there's nothing to do. */
123 if (!have_entry) return;
125 delete_init_entry(init_entry);
130 void cleanup(int exitcode)
142 void title(char *text)
144 if (setup_type == UI_TEXT) {
145 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<%s>\n", text);
151 int yesno(char *question)
154 newtComponent form = NULL;
155 newtComponent yesbutton = NULL;
156 newtComponent nobutton = NULL;
162 switch (setup_type) {
166 printf("%s\nYes/No --> ", question);
167 fgets(buf, sizeof buf, stdin);
168 answer = tolower(buf[0]);
171 else if (answer == 'n')
173 } while ((answer < 0) || (answer > 1));
177 sprintf(buf, "exec %s --yesno '%s' 10 72",
178 getenv("CTDL_DIALOG"),
191 newtCenteredWindow(76, 10, "Question");
192 form = newtForm(NULL, NULL, 0);
193 for (i=0; i<num_tokens(question, '\n'); ++i) {
194 extract_token(buf, question, i, '\n', sizeof buf);
195 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
197 yesbutton = newtButton(10, 5, "Yes");
198 nobutton = newtButton(60, 5, "No");
199 newtFormAddComponent(form, yesbutton);
200 newtFormAddComponent(form, nobutton);
201 if (newtRunForm(form) == yesbutton) {
208 newtFormDestroy(form);
217 void set_value(char *prompt, char str[])
225 char dialog_result[PATH_MAX];
229 strcpy(setupmsg, "");
231 switch (setup_type) {
233 title("WebCit setup");
234 printf("\n%s\n", prompt);
235 printf("This is currently set to:\n%s\n", str);
236 printf("Enter new value or press return to leave unchanged:\n");
237 fgets(buf, sizeof buf, stdin);
238 buf[strlen(buf) - 1] = 0;
239 if (strlen(buf) != 0)
244 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
245 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%s' 2>%s",
246 getenv("CTDL_DIALOG"),
251 fp = fopen(dialog_result, "r");
253 fgets(str, sizeof buf, fp);
254 if (str[strlen(str)-1] == 10) {
255 str[strlen(str)-1] = 0;
258 unlink(dialog_result);
265 newtCenteredWindow(76, 10, "WebCit setup");
266 form = newtForm(NULL, NULL, 0);
267 for (i=0; i<num_tokens(prompt, '\n'); ++i) {
268 extract_token(buf, prompt, i, '\n', sizeof buf);
269 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
271 newtFormAddComponent(form, newtEntry(1, 8, str, 74, (const char **) &result,
272 NEWT_FLAG_RETURNEXIT));
277 newtFormDestroy(form);
284 void important_message(char *title, char *msgtext)
287 newtComponent form = NULL;
292 switch (setup_type) {
295 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");
296 printf(" %s \n\n%s\n\n", title, msgtext);
297 printf("Press return to continue...");
298 fgets(buf, sizeof buf, stdin);
302 sprintf(buf, "exec %s --msgbox '%s' 19 72",
303 getenv("CTDL_DIALOG"),
310 newtCenteredWindow(76, 10, title);
311 form = newtForm(NULL, NULL, 0);
312 for (i=0; i<num_tokens(msgtext, '\n'); ++i) {
313 extract_token(buf, msgtext, i, '\n', sizeof buf);
314 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
316 newtFormAddComponent(form, newtButton(35, 5, "OK"));
319 newtFormDestroy(form);
327 void display_error(char *error_message)
329 important_message("Error", error_message);
332 void progress(char *text, long int curr, long int cmax)
336 /* These variables are static because progress() gets called
337 * multiple times during the course of whatever operation is
338 * being performed. This makes setup non-threadsafe, but who
341 static newtComponent form = NULL;
342 static newtComponent scale = NULL;
344 static long dots_printed = 0L;
347 static FILE *fp = NULL;
349 switch (setup_type) {
353 printf("%s\n", text);
354 printf("..........................");
355 printf("..........................");
356 printf("..........................\r");
359 } else if (curr == cmax) {
360 printf("\r%79s\n", "");
362 a = (curr * 100) / cmax;
365 while (dots_printed < a) {
375 sprintf(buf, "exec %s --gauge '%s' 7 72 0",
376 getenv("CTDL_DIALOG"),
378 fp = popen(buf, "w");
384 else if (curr == cmax) {
386 fprintf(fp, "100\n");
392 a = (curr * 100) / cmax;
394 fprintf(fp, "%ld\n", a);
403 newtCenteredWindow(76, 8, text);
404 form = newtForm(NULL, NULL, 0);
405 scale = newtScale(1, 3, 74, cmax);
406 newtFormAddComponent(form, scale);
410 if ((curr > 0) && (curr <= cmax)) {
411 newtScaleSet(scale, curr);
415 newtFormDestroy(form);
429 * install_init_scripts() -- Make sure "webserver" is in /etc/inittab
432 void install_init_scripts(void)
437 char https_port[128];
441 struct utsname my_utsname;
445 /* Otherwise, prompt the user to create an entry. */
446 snprintf(question, sizeof question,
447 "Would you like to automatically start WebCit at boot?"
449 if (yesno(question) == 0)
452 snprintf(question, sizeof question,
453 "On which port do you want WebCit to listen for HTTP "
454 "requests?\n\nYou can use the standard port (80) if you are "
455 "not running another\nweb server (such as Apache), otherwise "
456 "select another port.");
457 sprintf(http_port, "80");
458 set_value(question, http_port);
460 sprintf(suggested_url, "http://%s:%s/", my_utsname.nodename, http_port);
463 snprintf(question, sizeof question,
464 "On which port do you want WebCit to listen for HTTPS "
465 "requests?\n\nYou can use the standard port (443) if you are "
466 "not running another\nweb server (such as Apache), otherwise "
467 "select another port.");
468 sprintf(https_port, "443");
469 set_value(question, https_port);
472 /* Find out where Citadel is. */
473 if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
474 strcpy(hostname, "uds");
475 strcpy(portname, getenv("CITADEL"));
478 snprintf(question, sizeof question,
479 "Is the Citadel service running on the same host as WebCit?");
480 if (yesno(question)) {
481 sprintf(hostname, "uds");
482 sprintf(portname, "/usr/local/citadel");
483 set_value("In what directory is Citadel installed?", portname);
486 sprintf(hostname, "127.0.0.1");
487 sprintf(portname, "504");
488 set_value("Enter the host name or IP address of your "
489 "Citadel server.", hostname);
490 set_value("Enter the port number on which Citadel is "
491 "running (usually 504)", portname);
496 fp = fopen("/etc/init.d/webcit", "w");
498 display_error("Cannot create /etc/init.d/webcit");
502 fprintf(fp, "#!/bin/sh\n"
504 "DAEMON=%s/ctdlsvc\n", setup_directory);
506 "test -x $DAEMON || exit 0\n"
507 "test -d /var/run || exit 0\n"
511 "start) echo -n \"Starting WebCit... \"\n"
512 " if $DAEMON /var/run/webcit.pid %s/webserver "
514 setup_directory, http_port, hostname, portname);
515 fprintf(fp, " then\n"
521 fprintf(fp, " echo -n \"Starting WebCit SSL... \"\n"
522 " if $DAEMON /var/run/webcit-ssl.pid %s/webserver "
524 setup_directory, https_port, hostname, portname);
525 fprintf(fp, " then\n"
532 "stop) echo -n \"Stopping WebCit... \"\n"
533 " if kill `cat /var/run/webcit.pid 2>/dev/null` 2>/dev/null\n"
539 " rm -f /var/run/webcit.pid 2>/dev/null\n");
541 fprintf(fp, " echo -n \"Stopping WebCit SSL... \"\n"
542 " if kill `cat /var/run/webcit-ssl.pid 2>/dev/null` 2>/dev/null\n"
548 " rm -f /var/run/webcit-ssl.pid 2>/dev/null\n");
551 "*) echo \"Usage: $0 {start|stop}\"\n"
558 chmod("/etc/init.d/webcit", 0755);
565 * Figure out what type of user interface we're going to use
567 int discover_ui(void)
570 /* Use "dialog" if we have it */
571 if (getenv("CTDL_DIALOG") != NULL) {
578 newtDrawRootText(0, 0, "WebCit Setup");
588 int main(int argc, char *argv[])
593 strcpy(suggested_url, "http://<your_host_name>:<port>/");
595 /* set an invalid setup type */
598 /* Check to see if we're running the web installer */
599 if (getenv("CITADEL_INSTALLER") != NULL) {
600 using_web_installer = 1;
603 /* parse command line args */
604 for (a = 0; a < argc; ++a) {
605 if (!strncmp(argv[a], "-u", 2)) {
606 strcpy(aaa, argv[a]);
607 strcpy(aaa, &aaa[2]);
608 setup_type = atoi(aaa);
610 if (!strcmp(argv[a], "-i")) {
613 if (!strcmp(argv[a], "-q")) {
614 setup_type = UI_SILENT;
619 /* If a setup type was not specified, try to determine automatically
620 * the best one to use out of all available types.
622 if (setup_type < 0) {
623 setup_type = discover_ui();
625 if (info_only == 1) {
626 important_message("WebCit Setup", "Welcome to WebCit setup");
630 /* Get started in a valid setup directory. */
631 strcpy(setup_directory, PREFIX);
632 if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
633 strcpy(setup_directory, getenv("WEBCIT"));
636 set_value("In what directory is WebCit installed?",
639 if (chdir(setup_directory) != 0) {
640 important_message("WebCit Setup",
641 "The directory you specified does not exist.");
646 * We used to start WebCit by putting it directly into /etc/inittab.
647 * Since some systems are moving away from init, we can't do this anymore.
649 progress("Removing obsolete /etc/inittab entries...", 0, 1);
650 delete_the_old_way();
651 progress("Removing obsolete /etc/inittab entries...", 1, 1);
654 switch (setup_type) {
658 " *** WebCit setup program ***\n\n");
664 * If we're running on SysV, install init scripts.
666 if (!access("/var/run", W_OK)) {
667 install_init_scripts();
670 for (a=0; a<=3; ++a) {
671 progress("FIXME Starting the WebCit service...", a, 3);
672 /* if (a == 0) start_the_service(); */
677 "Setup is finished. You may now log in.\n"
678 "Point your web browser at %s\n", suggested_url
680 important_message("Setup finished", aaa);
683 important_message("Setup finished",
684 "Setup is finished. You may now start the server.");