6 * (This is basically just an install wizard. It's not required.)
16 #include <sys/types.h>
18 #include <sys/utsname.h>
33 #define UI_TEXT 0 /* Default setup type -- text only */
34 #define UI_DIALOG 2 /* Use the 'dialog' program */
35 #define UI_SILENT 3 /* Silent running, for use in scripts */
36 #define UI_NEWT 4 /* Use the "newt" window library */
39 char setup_directory[SIZ];
41 int using_web_installer = 0;
42 char suggested_url[SIZ];
45 * Set an entry in inittab to the desired state
47 void set_init_entry(char *which_entry, char *new_state) {
57 if (inittab == NULL) return;
59 fp = fopen("/etc/inittab", "r");
60 if (fp == NULL) return;
62 while(fgets(buf, sizeof buf, fp) != NULL) {
64 if (num_tokens(buf, ':') == 4) {
65 extract_token(entry, buf, 0, ':', sizeof entry);
66 extract_token(levels, buf, 1, ':', sizeof levels);
67 extract_token(state, buf, 2, ':', sizeof state);
68 extract_token(prog, buf, 3, ':', sizeof prog); /* includes 0x0a LF */
70 if (!strcmp(entry, which_entry)) {
71 strcpy(state, new_state);
72 sprintf(buf, "%s:%s:%s:%s",
73 entry, levels, state, prog);
77 inittab = realloc(inittab, strlen(inittab) + strlen(buf) + 2);
78 if (inittab == NULL) {
86 fp = fopen("/etc/inittab", "w");
88 fwrite(inittab, strlen(inittab), 1, fp);
90 kill(1, SIGHUP); /* Tell init to re-read /etc/inittab */
99 * Shut down the Citadel service if necessary, during setup.
101 void shutdown_service(void) {
104 char looking_for[SIZ];
109 strcpy(init_entry, "");
111 /* Determine the fully qualified path name of webserver */
112 snprintf(looking_for, sizeof looking_for, "%s/webserver ", setup_directory);
114 /* Pound through /etc/inittab line by line. Set have_entry to 1 if
115 * an entry is found which we believe starts webserver.
117 infp = fopen("/etc/inittab", "r");
121 while (fgets(buf, sizeof buf, infp) != NULL) {
122 buf[strlen(buf) - 1] = 0;
123 extract_token(entry, buf, 0, ':', sizeof entry);
124 extract_token(prog, buf, 3, ':', sizeof prog);
125 if (!strncasecmp(prog, looking_for,
126 strlen(looking_for))) {
128 strcpy(init_entry, entry);
134 /* Bail out if there's nothing to do. */
135 if (!have_entry) return;
137 set_init_entry(init_entry, "off");
142 * Start the Citadel service.
144 void start_the_service(void) {
145 if (strlen(init_entry) > 0) {
146 set_init_entry(init_entry, "respawn");
152 void cleanup(int exitcode)
164 void title(char *text)
166 if (setup_type == UI_TEXT) {
167 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<%s>\n", text);
173 int yesno(char *question)
176 newtComponent form = NULL;
177 newtComponent yesbutton = NULL;
178 newtComponent nobutton = NULL;
184 switch (setup_type) {
188 printf("%s\nYes/No --> ", question);
189 fgets(buf, sizeof buf, stdin);
190 answer = tolower(buf[0]);
193 else if (answer == 'n')
195 } while ((answer < 0) || (answer > 1));
199 sprintf(buf, "exec %s --yesno '%s' 10 72",
200 getenv("CTDL_DIALOG"),
213 newtCenteredWindow(76, 10, "Question");
214 form = newtForm(NULL, NULL, 0);
215 for (i=0; i<num_tokens(question, '\n'); ++i) {
216 extract_token(buf, question, i, '\n', sizeof buf);
217 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
219 yesbutton = newtButton(10, 5, "Yes");
220 nobutton = newtButton(60, 5, "No");
221 newtFormAddComponent(form, yesbutton);
222 newtFormAddComponent(form, nobutton);
223 if (newtRunForm(form) == yesbutton) {
230 newtFormDestroy(form);
239 void set_value(char *prompt, char str[])
251 strcpy(setupmsg, "");
253 switch (setup_type) {
255 title("WebCit setup");
256 printf("\n%s\n", prompt);
257 printf("This is currently set to:\n%s\n", str);
258 printf("Enter new value or press return to leave unchanged:\n");
259 fgets(buf, sizeof buf, stdin);
260 buf[strlen(buf) - 1] = 0;
261 if (strlen(buf) != 0)
266 dialog_result = tmpnam(NULL);
267 sprintf(buf, "exec %s --backtitle '%s' --inputbox '%s' 19 72 '%s' 2>%s",
268 getenv("CTDL_DIALOG"),
274 fp = fopen(dialog_result, "r");
276 fgets(str, sizeof buf, fp);
277 if (str[strlen(str)-1] == 10) {
278 str[strlen(str)-1] = 0;
281 unlink(dialog_result);
288 newtCenteredWindow(76, 10, "WebCit setup");
289 form = newtForm(NULL, NULL, 0);
290 for (i=0; i<num_tokens(prompt, '\n'); ++i) {
291 extract_token(buf, prompt, i, '\n', sizeof buf);
292 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
294 newtFormAddComponent(form, newtEntry(1, 8, str, 74, &result,
295 NEWT_FLAG_RETURNEXIT));
300 newtFormDestroy(form);
307 void important_message(char *title, char *msgtext)
310 newtComponent form = NULL;
315 switch (setup_type) {
318 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");
319 printf(" %s \n\n%s\n\n", title, msgtext);
320 printf("Press return to continue...");
321 fgets(buf, sizeof buf, stdin);
325 sprintf(buf, "exec %s --backtitle '%s' --msgbox '%s' 19 72",
326 getenv("CTDL_DIALOG"),
334 newtCenteredWindow(76, 10, title);
335 form = newtForm(NULL, NULL, 0);
336 for (i=0; i<num_tokens(msgtext, '\n'); ++i) {
337 extract_token(buf, msgtext, i, '\n', sizeof buf);
338 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
340 newtFormAddComponent(form, newtButton(35, 5, "OK"));
343 newtFormDestroy(form);
351 void display_error(char *error_message)
353 important_message("Error", error_message);
356 void progress(char *text, long int curr, long int cmax)
360 /* These variables are static because progress() gets called
361 * multiple times during the course of whatever operation is
362 * being performed. This makes setup non-threadsafe, but who
365 static newtComponent form = NULL;
366 static newtComponent scale = NULL;
368 static long dots_printed = 0L;
371 static FILE *fp = NULL;
373 switch (setup_type) {
377 printf("%s\n", text);
378 printf("..........................");
379 printf("..........................");
380 printf("..........................\r");
383 } else if (curr == cmax) {
384 printf("\r%79s\n", "");
386 a = (curr * 100) / cmax;
389 while (dots_printed < a) {
399 sprintf(buf, "exec %s --gauge '%s' 7 72 0",
400 getenv("CTDL_DIALOG"),
402 fp = popen(buf, "w");
408 else if (curr == cmax) {
410 fprintf(fp, "100\n");
416 a = (curr * 100) / cmax;
418 fprintf(fp, "%ld\n", a);
427 newtCenteredWindow(76, 8, text);
428 form = newtForm(NULL, NULL, 0);
429 scale = newtScale(1, 3, 74, cmax);
430 newtFormAddComponent(form, scale);
434 if ((curr > 0) && (curr <= cmax)) {
435 newtScaleSet(scale, curr);
439 newtFormDestroy(form);
453 * check_inittab_entry() -- Make sure "webserver" is in /etc/inittab
456 void check_inittab_entry(void)
460 char looking_for[SIZ];
465 char https_port[128];
469 struct utsname my_utsname;
471 /* Determine the fully qualified path name of webserver */
472 snprintf(looking_for, sizeof looking_for, "%s/webserver", setup_directory);
474 /* If there's already an entry, then we have nothing left to do. */
475 if (strlen(init_entry) > 0) {
479 /* Otherwise, prompt the user to create an entry. */
480 snprintf(question, sizeof question,
481 "There is no '%s' entry in /etc/inittab.\n"
482 "Would you like to add one?",
484 if (yesno(question) == 0)
487 snprintf(question, sizeof question,
488 "On which port do you want WebCit to listen for HTTP "
489 "requests?\n\nYou can use the standard port (80) if you are "
490 "not running another\nweb server (such as Apache), otherwise "
491 "select another port.");
492 sprintf(http_port, "2000");
493 set_value(question, http_port);
495 sprintf(suggested_url, "http://%s:%s/", my_utsname.nodename, http_port);
498 snprintf(question, sizeof question,
499 "On which port do you want WebCit to listen for HTTPS "
500 "requests?\n\nYou can use the standard port (443) if you are "
501 "not running another\nweb server (such as Apache), otherwise "
502 "select another port.");
503 sprintf(https_port, "443");
504 set_value(question, https_port);
507 /* Find out where Citadel is. */
508 if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
509 strcpy(hostname, "uds");
510 strcpy(portname, getenv("CITADEL"));
513 snprintf(question, sizeof question,
514 "Is the Citadel service running on the same host as WebCit?");
515 if (yesno(question)) {
516 sprintf(hostname, "uds");
517 sprintf(portname, "/usr/local/citadel");
518 set_value("In what directory is Citadel installed?", portname);
521 sprintf(hostname, "127.0.0.1");
522 sprintf(portname, "504");
523 set_value("Enter the host name or IP address of your "
524 "Citadel server.", hostname);
525 set_value("Enter the port number on which Citadel is "
526 "running (usually 504)", portname);
530 /* Generate unique entry names for /etc/inittab */
531 snprintf(entryname, sizeof entryname, "c0");
534 if (entryname[1] > '9') {
537 if (entryname[0] > 'z') {
539 "Can't generate a unique entry name");
543 snprintf(buf, sizeof buf,
544 "grep %s: /etc/inittab >/dev/null 2>&1", entryname);
545 } while (system(buf) == 0);
548 /* Now write it out to /etc/inittab */
549 infp = fopen("/etc/inittab", "a");
551 display_error(strerror(errno));
553 fprintf(infp, "# Start the WebCit server...\n");
554 fprintf(infp, "h%s:2345:respawn:%s -p%s %s %s\n",
555 entryname, looking_for,
556 http_port, hostname, portname);
558 fprintf(infp, "s%s:2345:respawn:%s -p%s -s %s %s\n",
559 entryname, looking_for,
560 https_port, hostname, portname);
563 strcpy(init_entry, entryname);
571 * Figure out what type of user interface we're going to use
573 int discover_ui(void)
576 /* Use "dialog" if we have it */
577 if (getenv("CTDL_DIALOG") != NULL) {
584 newtDrawRootText(0, 0, "WebCit Setup");
594 int main(int argc, char *argv[])
599 strcpy(suggested_url, "http://<your_host_name>:<port>/");
601 /* set an invalid setup type */
604 /* Check to see if we're running the web installer */
605 if (getenv("CITADEL_INSTALLER") != NULL) {
606 using_web_installer = 1;
609 /* parse command line args */
610 for (a = 0; a < argc; ++a) {
611 if (!strncmp(argv[a], "-u", 2)) {
612 strcpy(aaa, argv[a]);
613 strcpy(aaa, &aaa[2]);
614 setup_type = atoi(aaa);
616 if (!strcmp(argv[a], "-i")) {
619 if (!strcmp(argv[a], "-q")) {
620 setup_type = UI_SILENT;
625 /* If a setup type was not specified, try to determine automatically
626 * the best one to use out of all available types.
628 if (setup_type < 0) {
629 setup_type = discover_ui();
631 if (info_only == 1) {
632 important_message("WebCit Setup", "Welcome to WebCit setup");
636 /* Get started in a valid setup directory. */
637 strcpy(setup_directory, WEBCITDIR);
638 if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
639 strcpy(setup_directory, getenv("WEBCIT"));
642 set_value("In what directory is WebCit installed?",
645 if (chdir(setup_directory) != 0) {
646 important_message("WebCit Setup",
647 "The directory you specified does not exist.");
651 /* See if we need to shut down the WebCit service. */
652 for (a=0; a<=3; ++a) {
653 progress("Shutting down the WebCit service...", a, 3);
654 if (a == 0) shutdown_service();
659 switch (setup_type) {
663 " *** WebCit setup program ***\n\n");
668 check_inittab_entry(); /* Check /etc/inittab */
670 /* See if we can start the WebCit service. */
671 if (strlen(init_entry) > 0) {
672 for (a=0; a<=3; ++a) {
673 progress("Starting the WebCit service...", a, 3);
674 if (a == 0) start_the_service();
678 "Setup is finished. You may now log in.\n"
679 "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.");