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 --backtitle '%s' --inputbox '%s' 19 72 '%s' 2>%s",
255 getenv("CTDL_DIALOG"),
261 fp = fopen(dialog_result, "r");
263 fgets(str, sizeof buf, fp);
264 if (str[strlen(str)-1] == 10) {
265 str[strlen(str)-1] = 0;
268 unlink(dialog_result);
275 newtCenteredWindow(76, 10, "WebCit setup");
276 form = newtForm(NULL, NULL, 0);
277 for (i=0; i<num_tokens(prompt, '\n'); ++i) {
278 extract_token(buf, prompt, i, '\n', sizeof buf);
279 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
281 newtFormAddComponent(form, newtEntry(1, 8, str, 74, (const char **) &result,
282 NEWT_FLAG_RETURNEXIT));
287 newtFormDestroy(form);
294 void important_message(char *title, char *msgtext)
297 newtComponent form = NULL;
302 switch (setup_type) {
305 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");
306 printf(" %s \n\n%s\n\n", title, msgtext);
307 printf("Press return to continue...");
308 fgets(buf, sizeof buf, stdin);
312 sprintf(buf, "exec %s --backtitle '%s' --msgbox '%s' 19 72",
313 getenv("CTDL_DIALOG"),
321 newtCenteredWindow(76, 10, title);
322 form = newtForm(NULL, NULL, 0);
323 for (i=0; i<num_tokens(msgtext, '\n'); ++i) {
324 extract_token(buf, msgtext, i, '\n', sizeof buf);
325 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
327 newtFormAddComponent(form, newtButton(35, 5, "OK"));
330 newtFormDestroy(form);
338 void display_error(char *error_message)
340 important_message("Error", error_message);
343 void progress(char *text, long int curr, long int cmax)
347 /* These variables are static because progress() gets called
348 * multiple times during the course of whatever operation is
349 * being performed. This makes setup non-threadsafe, but who
352 static newtComponent form = NULL;
353 static newtComponent scale = NULL;
355 static long dots_printed = 0L;
358 static FILE *fp = NULL;
360 switch (setup_type) {
364 printf("%s\n", text);
365 printf("..........................");
366 printf("..........................");
367 printf("..........................\r");
370 } else if (curr == cmax) {
371 printf("\r%79s\n", "");
373 a = (curr * 100) / cmax;
376 while (dots_printed < a) {
386 sprintf(buf, "exec %s --gauge '%s' 7 72 0",
387 getenv("CTDL_DIALOG"),
389 fp = popen(buf, "w");
395 else if (curr == cmax) {
397 fprintf(fp, "100\n");
403 a = (curr * 100) / cmax;
405 fprintf(fp, "%ld\n", a);
414 newtCenteredWindow(76, 8, text);
415 form = newtForm(NULL, NULL, 0);
416 scale = newtScale(1, 3, 74, cmax);
417 newtFormAddComponent(form, scale);
421 if ((curr > 0) && (curr <= cmax)) {
422 newtScaleSet(scale, curr);
426 newtFormDestroy(form);
440 * check_inittab_entry() -- Make sure "webserver" is in /etc/inittab
443 void check_inittab_entry(void)
447 char looking_for[SIZ];
452 char https_port[128];
456 struct utsname my_utsname;
458 /* Determine the fully qualified path name of webserver */
459 snprintf(looking_for, sizeof looking_for, "%s/webserver", setup_directory);
461 /* If there's already an entry, then we have nothing left to do. */
462 if (strlen(init_entry) > 0) {
466 /* Otherwise, prompt the user to create an entry. */
467 snprintf(question, sizeof question,
468 "There is no '%s' entry in /etc/inittab.\n"
469 "Would you like to add one?",
471 if (yesno(question) == 0)
474 snprintf(question, sizeof question,
475 "On which port do you want WebCit to listen for HTTP "
476 "requests?\n\nYou can use the standard port (80) if you are "
477 "not running another\nweb server (such as Apache), otherwise "
478 "select another port.");
479 sprintf(http_port, "2000");
480 set_value(question, http_port);
482 sprintf(suggested_url, "http://%s:%s/", my_utsname.nodename, http_port);
485 snprintf(question, sizeof question,
486 "On which port do you want WebCit to listen for HTTPS "
487 "requests?\n\nYou can use the standard port (443) if you are "
488 "not running another\nweb server (such as Apache), otherwise "
489 "select another port.");
490 sprintf(https_port, "443");
491 set_value(question, https_port);
494 /* Find out where Citadel is. */
495 if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
496 strcpy(hostname, "uds");
497 strcpy(portname, getenv("CITADEL"));
500 snprintf(question, sizeof question,
501 "Is the Citadel service running on the same host as WebCit?");
502 if (yesno(question)) {
503 sprintf(hostname, "uds");
504 sprintf(portname, "/usr/local/citadel");
505 set_value("In what directory is Citadel installed?", portname);
508 sprintf(hostname, "127.0.0.1");
509 sprintf(portname, "504");
510 set_value("Enter the host name or IP address of your "
511 "Citadel server.", hostname);
512 set_value("Enter the port number on which Citadel is "
513 "running (usually 504)", portname);
517 /* Generate unique entry names for /etc/inittab */
518 snprintf(entryname, sizeof entryname, "c0");
521 if (entryname[1] > '9') {
524 if (entryname[0] > 'z') {
526 "Can't generate a unique entry name");
530 snprintf(buf, sizeof buf,
531 "grep %s: /etc/inittab >/dev/null 2>&1", entryname);
532 } while (system(buf) == 0);
535 /* Now write it out to /etc/inittab */
536 infp = fopen("/etc/inittab", "a");
538 display_error(strerror(errno));
540 fprintf(infp, "# Start the WebCit server...\n");
541 fprintf(infp, "h%s:2345:respawn:%s -p%s %s %s\n",
542 entryname, looking_for,
543 http_port, hostname, portname);
545 fprintf(infp, "s%s:2345:respawn:%s -p%s -s %s %s\n",
546 entryname, looking_for,
547 https_port, hostname, portname);
550 strcpy(init_entry, entryname);
558 * Figure out what type of user interface we're going to use
560 int discover_ui(void)
563 /* Use "dialog" if we have it */
564 if (getenv("CTDL_DIALOG") != NULL) {
571 newtDrawRootText(0, 0, "WebCit Setup");
581 int main(int argc, char *argv[])
586 strcpy(suggested_url, "http://<your_host_name>:<port>/");
588 /* set an invalid setup type */
591 /* Check to see if we're running the web installer */
592 if (getenv("CITADEL_INSTALLER") != NULL) {
593 using_web_installer = 1;
596 /* parse command line args */
597 for (a = 0; a < argc; ++a) {
598 if (!strncmp(argv[a], "-u", 2)) {
599 strcpy(aaa, argv[a]);
600 strcpy(aaa, &aaa[2]);
601 setup_type = atoi(aaa);
603 if (!strcmp(argv[a], "-i")) {
606 if (!strcmp(argv[a], "-q")) {
607 setup_type = UI_SILENT;
612 /* If a setup type was not specified, try to determine automatically
613 * the best one to use out of all available types.
615 if (setup_type < 0) {
616 setup_type = discover_ui();
618 if (info_only == 1) {
619 important_message("WebCit Setup", "Welcome to WebCit setup");
623 /* If we're on something BSDish then we don't have inittab */
624 if (access("/etc/inittab", F_OK)) {
625 important_message("Not running SysV style init",
626 "WebCit Setup can only run on systems that use /etc/inittab.\n"
627 "Please manually configure your startup scripts to run WebCit\n"
628 "when the system is booted.\n");
632 /* Get started in a valid setup directory. */
633 strcpy(setup_directory, WEBCITDIR);
634 if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
635 strcpy(setup_directory, getenv("WEBCIT"));
638 set_value("In what directory is WebCit installed?",
641 if (chdir(setup_directory) != 0) {
642 important_message("WebCit Setup",
643 "The directory you specified does not exist.");
647 /* See if we need to shut down the WebCit service. */
648 for (a=0; a<=3; ++a) {
649 progress("Shutting down the WebCit service...", a, 3);
650 if (a == 0) shutdown_service();
655 switch (setup_type) {
659 " *** WebCit setup program ***\n\n");
664 check_inittab_entry(); /* Check /etc/inittab */
666 /* See if we can start the WebCit service. */
667 if (strlen(init_entry) > 0) {
668 for (a=0; a<=3; ++a) {
669 progress("Starting the WebCit service...", a, 3);
670 if (a == 0) start_the_service();
674 "Setup is finished. You may now log in.\n"
675 "Point your web browser at %s\n", suggested_url);
676 important_message("Setup finished", aaa);
679 important_message("Setup finished",
680 "Setup is finished. You may now start the server.");