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];
28 int using_web_installer = 0;
29 char suggested_url[SIZ];
32 * Set an entry in inittab to the desired state
34 void set_init_entry(char *which_entry, char *new_state) {
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 strcpy(state, new_state);
59 sprintf(buf, "%s:%s:%s:%s",
60 entry, levels, state, prog);
64 inittab = realloc(inittab, strlen(inittab) + strlen(buf) + 2);
65 if (inittab == NULL) {
73 fp = fopen("/etc/inittab", "w");
75 fwrite(inittab, strlen(inittab), 1, fp);
77 kill(1, SIGHUP); /* Tell init to re-read /etc/inittab */
86 * Shut down the Citadel service if necessary, during setup.
88 void shutdown_service(void) {
91 char looking_for[SIZ];
96 strcpy(init_entry, "");
98 /* Determine the fully qualified path name of webserver */
99 snprintf(looking_for, sizeof looking_for, "%s/webserver ", setup_directory);
101 /* Pound through /etc/inittab line by line. Set have_entry to 1 if
102 * an entry is found which we believe starts webserver.
104 infp = fopen("/etc/inittab", "r");
108 while (fgets(buf, sizeof buf, infp) != NULL) {
109 buf[strlen(buf) - 1] = 0;
110 extract_token(entry, buf, 0, ':', sizeof entry);
111 extract_token(prog, buf, 3, ':', sizeof prog);
112 if (!strncasecmp(prog, looking_for,
113 strlen(looking_for))) {
115 strcpy(init_entry, entry);
121 /* Bail out if there's nothing to do. */
122 if (!have_entry) return;
124 set_init_entry(init_entry, "off");
129 * Start the Citadel service.
131 void start_the_service(void) {
132 if (strlen(init_entry) > 0) {
133 set_init_entry(init_entry, "respawn");
139 void cleanup(int exitcode)
151 void title(char *text)
153 if (setup_type == UI_TEXT) {
154 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<%s>\n", text);
160 int yesno(char *question)
163 newtComponent form = NULL;
164 newtComponent yesbutton = NULL;
165 newtComponent nobutton = NULL;
171 switch (setup_type) {
175 printf("%s\nYes/No --> ", question);
176 fgets(buf, sizeof buf, stdin);
177 answer = tolower(buf[0]);
180 else if (answer == 'n')
182 } while ((answer < 0) || (answer > 1));
186 sprintf(buf, "exec %s --yesno '%s' 10 72",
187 getenv("CTDL_DIALOG"),
200 newtCenteredWindow(76, 10, "Question");
201 form = newtForm(NULL, NULL, 0);
202 for (i=0; i<num_tokens(question, '\n'); ++i) {
203 extract_token(buf, question, i, '\n', sizeof buf);
204 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
206 yesbutton = newtButton(10, 5, "Yes");
207 nobutton = newtButton(60, 5, "No");
208 newtFormAddComponent(form, yesbutton);
209 newtFormAddComponent(form, nobutton);
210 if (newtRunForm(form) == yesbutton) {
217 newtFormDestroy(form);
226 void set_value(char *prompt, char str[])
234 char dialog_result[PATH_MAX];
238 strcpy(setupmsg, "");
240 switch (setup_type) {
242 title("WebCit setup");
243 printf("\n%s\n", prompt);
244 printf("This is currently set to:\n%s\n", str);
245 printf("Enter new value or press return to leave unchanged:\n");
246 fgets(buf, sizeof buf, stdin);
247 buf[strlen(buf) - 1] = 0;
248 if (strlen(buf) != 0)
253 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
254 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%s' 2>%s",
255 getenv("CTDL_DIALOG"),
260 fp = fopen(dialog_result, "r");
262 fgets(str, sizeof buf, fp);
263 if (str[strlen(str)-1] == 10) {
264 str[strlen(str)-1] = 0;
267 unlink(dialog_result);
274 newtCenteredWindow(76, 10, "WebCit setup");
275 form = newtForm(NULL, NULL, 0);
276 for (i=0; i<num_tokens(prompt, '\n'); ++i) {
277 extract_token(buf, prompt, i, '\n', sizeof buf);
278 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
280 newtFormAddComponent(form, newtEntry(1, 8, str, 74, (const char **) &result,
281 NEWT_FLAG_RETURNEXIT));
286 newtFormDestroy(form);
293 void important_message(char *title, char *msgtext)
296 newtComponent form = NULL;
301 switch (setup_type) {
304 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");
305 printf(" %s \n\n%s\n\n", title, msgtext);
306 printf("Press return to continue...");
307 fgets(buf, sizeof buf, stdin);
311 sprintf(buf, "exec %s --msgbox '%s' 19 72",
312 getenv("CTDL_DIALOG"),
319 newtCenteredWindow(76, 10, title);
320 form = newtForm(NULL, NULL, 0);
321 for (i=0; i<num_tokens(msgtext, '\n'); ++i) {
322 extract_token(buf, msgtext, i, '\n', sizeof buf);
323 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
325 newtFormAddComponent(form, newtButton(35, 5, "OK"));
328 newtFormDestroy(form);
336 void display_error(char *error_message)
338 important_message("Error", error_message);
341 void progress(char *text, long int curr, long int cmax)
345 /* These variables are static because progress() gets called
346 * multiple times during the course of whatever operation is
347 * being performed. This makes setup non-threadsafe, but who
350 static newtComponent form = NULL;
351 static newtComponent scale = NULL;
353 static long dots_printed = 0L;
356 static FILE *fp = NULL;
358 switch (setup_type) {
362 printf("%s\n", text);
363 printf("..........................");
364 printf("..........................");
365 printf("..........................\r");
368 } else if (curr == cmax) {
369 printf("\r%79s\n", "");
371 a = (curr * 100) / cmax;
374 while (dots_printed < a) {
384 sprintf(buf, "exec %s --gauge '%s' 7 72 0",
385 getenv("CTDL_DIALOG"),
387 fp = popen(buf, "w");
393 else if (curr == cmax) {
395 fprintf(fp, "100\n");
401 a = (curr * 100) / cmax;
403 fprintf(fp, "%ld\n", a);
412 newtCenteredWindow(76, 8, text);
413 form = newtForm(NULL, NULL, 0);
414 scale = newtScale(1, 3, 74, cmax);
415 newtFormAddComponent(form, scale);
419 if ((curr > 0) && (curr <= cmax)) {
420 newtScaleSet(scale, curr);
424 newtFormDestroy(form);
438 * check_inittab_entry() -- Make sure "webserver" is in /etc/inittab
441 void check_inittab_entry(void)
445 char looking_for[SIZ];
450 char https_port[128];
454 struct utsname my_utsname;
456 /* Determine the fully qualified path name of webserver */
457 snprintf(looking_for, sizeof looking_for, "%s/webserver", setup_directory);
459 /* If there's already an entry, then we have nothing left to do. */
460 if (strlen(init_entry) > 0) {
464 /* Otherwise, prompt the user to create an entry. */
465 snprintf(question, sizeof question,
466 "There is no '%s' entry in /etc/inittab.\n"
467 "Would you like to add one?",
469 if (yesno(question) == 0)
472 snprintf(question, sizeof question,
473 "On which port do you want WebCit to listen for HTTP "
474 "requests?\n\nYou can use the standard port (80) if you are "
475 "not running another\nweb server (such as Apache), otherwise "
476 "select another port.");
477 sprintf(http_port, "2000");
478 set_value(question, http_port);
480 sprintf(suggested_url, "http://%s:%s/", my_utsname.nodename, http_port);
483 snprintf(question, sizeof question,
484 "On which port do you want WebCit to listen for HTTPS "
485 "requests?\n\nYou can use the standard port (443) if you are "
486 "not running another\nweb server (such as Apache), otherwise "
487 "select another port.");
488 sprintf(https_port, "443");
489 set_value(question, https_port);
492 /* Find out where Citadel is. */
493 if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
494 strcpy(hostname, "uds");
495 strcpy(portname, getenv("CITADEL"));
498 snprintf(question, sizeof question,
499 "Is the Citadel service running on the same host as WebCit?");
500 if (yesno(question)) {
501 sprintf(hostname, "uds");
502 sprintf(portname, "/usr/local/citadel");
503 set_value("In what directory is Citadel installed?", portname);
506 sprintf(hostname, "127.0.0.1");
507 sprintf(portname, "504");
508 set_value("Enter the host name or IP address of your "
509 "Citadel server.", hostname);
510 set_value("Enter the port number on which Citadel is "
511 "running (usually 504)", portname);
515 /* Generate unique entry names for /etc/inittab */
516 snprintf(entryname, sizeof entryname, "c0");
519 if (entryname[1] > '9') {
522 if (entryname[0] > 'z') {
524 "Can't generate a unique entry name");
528 snprintf(buf, sizeof buf,
529 "grep %s: /etc/inittab >/dev/null 2>&1", entryname);
530 } while (system(buf) == 0);
533 /* Now write it out to /etc/inittab */
534 infp = fopen("/etc/inittab", "a");
536 display_error(strerror(errno));
538 fprintf(infp, "# Start the WebCit server...\n");
539 fprintf(infp, "h%s:2345:respawn:%s -p%s %s %s\n",
540 entryname, looking_for,
541 http_port, hostname, portname);
543 fprintf(infp, "s%s:2345:respawn:%s -p%s -s %s %s\n",
544 entryname, looking_for,
545 https_port, hostname, portname);
548 strcpy(init_entry, entryname);
556 * Figure out what type of user interface we're going to use
558 int discover_ui(void)
561 /* Use "dialog" if we have it */
562 if (getenv("CTDL_DIALOG") != NULL) {
569 newtDrawRootText(0, 0, "WebCit Setup");
579 int main(int argc, char *argv[])
584 strcpy(suggested_url, "http://<your_host_name>:<port>/");
586 /* set an invalid setup type */
589 /* Check to see if we're running the web installer */
590 if (getenv("CITADEL_INSTALLER") != NULL) {
591 using_web_installer = 1;
594 /* parse command line args */
595 for (a = 0; a < argc; ++a) {
596 if (!strncmp(argv[a], "-u", 2)) {
597 strcpy(aaa, argv[a]);
598 strcpy(aaa, &aaa[2]);
599 setup_type = atoi(aaa);
601 if (!strcmp(argv[a], "-i")) {
604 if (!strcmp(argv[a], "-q")) {
605 setup_type = UI_SILENT;
610 /* If a setup type was not specified, try to determine automatically
611 * the best one to use out of all available types.
613 if (setup_type < 0) {
614 setup_type = discover_ui();
616 if (info_only == 1) {
617 important_message("WebCit Setup", "Welcome to WebCit setup");
621 /* If we're on something BSDish then we don't have inittab */
622 if (access("/etc/inittab", F_OK)) {
623 important_message("Not running SysV style init",
624 "WebCit Setup can only run on systems that use /etc/inittab.\n"
625 "Please manually configure your startup scripts to run WebCit\n"
626 "when the system is booted.\n");
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.");
645 /* See if we need to shut down the WebCit service. */
646 for (a=0; a<=3; ++a) {
647 progress("Shutting down the WebCit service...", a, 3);
648 if (a == 0) shutdown_service();
653 switch (setup_type) {
657 " *** WebCit setup program ***\n\n");
662 check_inittab_entry(); /* Check /etc/inittab */
664 /* See if we can start the WebCit service. */
665 if (strlen(init_entry) > 0) {
666 for (a=0; a<=3; ++a) {
667 progress("Starting the WebCit service...", a, 3);
668 if (a == 0) start_the_service();
672 "Setup is finished. You may now log in.\n"
673 "Point your web browser at %s\n", suggested_url);
674 important_message("Setup finished", aaa);
677 important_message("Setup finished",
678 "Setup is finished. You may now start the server.");