6 * (This is basically just an install wizard. It's not required.)
21 #include <sys/types.h>
23 #include <sys/socket.h>
24 #ifdef HAVE_SYS_TIME_H
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
39 #include <sys/utsname.h>
41 #include "webserver.h"
49 #define UI_TEXT 0 /* Default setup type -- text only */
50 #define UI_DIALOG 2 /* Use the 'dialog' program */
51 #define UI_SILENT 3 /* Silent running, for use in scripts */
52 #define UI_NEWT 4 /* Use the "newt" window library */
55 char setup_directory[SIZ];
57 int using_web_installer = 0;
58 char suggested_url[SIZ];
61 * Set an entry in inittab to the desired state
63 void set_init_entry(char *which_entry, char *new_state) {
73 if (inittab == NULL) return;
75 fp = fopen("/etc/inittab", "r");
76 if (fp == NULL) return;
78 while(fgets(buf, sizeof buf, fp) != NULL) {
80 if (num_tokens(buf, ':') == 4) {
81 extract_token(entry, buf, 0, ':', sizeof entry);
82 extract_token(levels, buf, 1, ':', sizeof levels);
83 extract_token(state, buf, 2, ':', sizeof state);
84 extract_token(prog, buf, 3, ':', sizeof prog); /* includes 0x0a LF */
86 if (!strcmp(entry, which_entry)) {
87 strcpy(state, new_state);
88 sprintf(buf, "%s:%s:%s:%s",
89 entry, levels, state, prog);
93 inittab = realloc(inittab, strlen(inittab) + strlen(buf) + 2);
94 if (inittab == NULL) {
102 fp = fopen("/etc/inittab", "w");
104 fwrite(inittab, strlen(inittab), 1, fp);
106 kill(1, SIGHUP); /* Tell init to re-read /etc/inittab */
115 * Shut down the Citadel service if necessary, during setup.
117 void shutdown_service(void) {
120 char looking_for[SIZ];
125 strcpy(init_entry, "");
127 /* Determine the fully qualified path name of webserver */
128 snprintf(looking_for, sizeof looking_for, "%s/webserver ", setup_directory);
130 /* Pound through /etc/inittab line by line. Set have_entry to 1 if
131 * an entry is found which we believe starts webserver.
133 infp = fopen("/etc/inittab", "r");
137 while (fgets(buf, sizeof buf, infp) != NULL) {
138 buf[strlen(buf) - 1] = 0;
139 extract_token(entry, buf, 0, ':', sizeof entry);
140 extract_token(prog, buf, 3, ':', sizeof prog);
141 if (!strncasecmp(prog, looking_for,
142 strlen(looking_for))) {
144 strcpy(init_entry, entry);
150 /* Bail out if there's nothing to do. */
151 if (!have_entry) return;
153 set_init_entry(init_entry, "off");
158 * Start the Citadel service.
160 void start_the_service(void) {
161 if (strlen(init_entry) > 0) {
162 set_init_entry(init_entry, "respawn");
168 void cleanup(int exitcode)
180 void title(char *text)
182 if (setup_type == UI_TEXT) {
183 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<%s>\n", text);
189 int yesno(char *question)
192 newtComponent form = NULL;
193 newtComponent yesbutton = NULL;
194 newtComponent nobutton = NULL;
200 switch (setup_type) {
204 printf("%s\nYes/No --> ", question);
205 fgets(buf, sizeof buf, stdin);
206 answer = tolower(buf[0]);
209 else if (answer == 'n')
211 } while ((answer < 0) || (answer > 1));
215 sprintf(buf, "exec %s --yesno '%s' 10 72",
216 getenv("CTDL_DIALOG"),
229 newtCenteredWindow(76, 10, "Question");
230 form = newtForm(NULL, NULL, 0);
231 for (i=0; i<num_tokens(question, '\n'); ++i) {
232 extract_token(buf, question, i, '\n', sizeof buf);
233 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
235 yesbutton = newtButton(10, 5, "Yes");
236 nobutton = newtButton(60, 5, "No");
237 newtFormAddComponent(form, yesbutton);
238 newtFormAddComponent(form, nobutton);
239 if (newtRunForm(form) == yesbutton) {
246 newtFormDestroy(form);
255 void set_value(char *prompt, char str[])
267 strcpy(setupmsg, "");
269 switch (setup_type) {
271 title("WebCit setup");
272 printf("\n%s\n", prompt);
273 printf("This is currently set to:\n%s\n", str);
274 printf("Enter new value or press return to leave unchanged:\n");
275 fgets(buf, sizeof buf, stdin);
276 buf[strlen(buf) - 1] = 0;
277 if (strlen(buf) != 0)
282 dialog_result = tmpnam(NULL);
283 sprintf(buf, "exec %s --backtitle '%s' --inputbox '%s' 19 72 '%s' 2>%s",
284 getenv("CTDL_DIALOG"),
290 fp = fopen(dialog_result, "r");
292 fgets(str, sizeof buf, fp);
293 if (str[strlen(str)-1] == 10) {
294 str[strlen(str)-1] = 0;
297 unlink(dialog_result);
304 newtCenteredWindow(76, 10, "WebCit setup");
305 form = newtForm(NULL, NULL, 0);
306 for (i=0; i<num_tokens(prompt, '\n'); ++i) {
307 extract_token(buf, prompt, i, '\n', sizeof buf);
308 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
310 newtFormAddComponent(form, newtEntry(1, 8, str, 74, &result,
311 NEWT_FLAG_RETURNEXIT));
316 newtFormDestroy(form);
323 void important_message(char *title, char *msgtext)
326 newtComponent form = NULL;
331 switch (setup_type) {
334 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");
335 printf(" %s \n\n%s\n\n", title, msgtext);
336 printf("Press return to continue...");
337 fgets(buf, sizeof buf, stdin);
341 sprintf(buf, "exec %s --backtitle '%s' --msgbox '%s' 19 72",
342 getenv("CTDL_DIALOG"),
350 newtCenteredWindow(76, 10, title);
351 form = newtForm(NULL, NULL, 0);
352 for (i=0; i<num_tokens(msgtext, '\n'); ++i) {
353 extract_token(buf, msgtext, i, '\n', sizeof buf);
354 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
356 newtFormAddComponent(form, newtButton(35, 5, "OK"));
359 newtFormDestroy(form);
367 void display_error(char *error_message)
369 important_message("Error", error_message);
372 void progress(char *text, long int curr, long int cmax)
376 /* These variables are static because progress() gets called
377 * multiple times during the course of whatever operation is
378 * being performed. This makes setup non-threadsafe, but who
381 static newtComponent form = NULL;
382 static newtComponent scale = NULL;
384 static long dots_printed = 0L;
387 static FILE *fp = NULL;
389 switch (setup_type) {
393 printf("%s\n", text);
394 printf("..........................");
395 printf("..........................");
396 printf("..........................\r");
399 } else if (curr == cmax) {
400 printf("\r%79s\n", "");
402 a = (curr * 100) / cmax;
405 while (dots_printed < a) {
415 sprintf(buf, "exec %s --gauge '%s' 7 72 0",
416 getenv("CTDL_DIALOG"),
418 fp = popen(buf, "w");
424 else if (curr == cmax) {
426 fprintf(fp, "100\n");
432 a = (curr * 100) / cmax;
434 fprintf(fp, "%ld\n", a);
443 newtCenteredWindow(76, 8, text);
444 form = newtForm(NULL, NULL, 0);
445 scale = newtScale(1, 3, 74, cmax);
446 newtFormAddComponent(form, scale);
450 if ((curr > 0) && (curr <= cmax)) {
451 newtScaleSet(scale, curr);
455 newtFormDestroy(form);
469 * check_inittab_entry() -- Make sure "webserver" is in /etc/inittab
472 void check_inittab_entry(void)
476 char looking_for[SIZ];
481 char https_port[128];
485 struct utsname my_utsname;
487 /* Determine the fully qualified path name of webserver */
488 snprintf(looking_for, sizeof looking_for, "%s/webserver", setup_directory);
490 /* If there's already an entry, then we have nothing left to do. */
491 if (strlen(init_entry) > 0) {
495 /* Otherwise, prompt the user to create an entry. */
496 snprintf(question, sizeof question,
497 "There is no '%s' entry in /etc/inittab.\n"
498 "Would you like to add one?",
500 if (yesno(question) == 0)
503 snprintf(question, sizeof question,
504 "On which port do you want WebCit to listen for HTTP "
505 "requests?\n\nYou can use the standard port (80) if you are "
506 "not running another\nweb server (such as Apache), otherwise "
507 "select another port.");
508 sprintf(http_port, "2000");
509 set_value(question, http_port);
511 sprintf(suggested_url, "http://%s:%s/", my_utsname.nodename, http_port);
514 snprintf(question, sizeof question,
515 "On which port do you want WebCit to listen for HTTPS "
516 "requests?\n\nYou can use the standard port (443) if you are "
517 "not running another\nweb server (such as Apache), otherwise "
518 "select another port.");
519 sprintf(https_port, "443");
520 set_value(question, https_port);
523 /* Find out where Citadel is. */
524 if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
525 strcpy(hostname, "uds");
526 strcpy(portname, getenv("CITADEL"));
529 snprintf(question, sizeof question,
530 "Is the Citadel service running on the same host as WebCit?");
531 if (yesno(question)) {
532 sprintf(hostname, "uds");
533 sprintf(portname, "/usr/local/citadel");
534 set_value("In what directory is Citadel installed?", portname);
537 sprintf(hostname, "127.0.0.1");
538 sprintf(portname, "504");
539 set_value("Enter the host name or IP address of your "
540 "Citadel server.", hostname);
541 set_value("Enter the port number on which Citadel is "
542 "running (usually 504)", portname);
546 /* Generate unique entry names for /etc/inittab */
547 snprintf(entryname, sizeof entryname, "c0");
550 if (entryname[1] > '9') {
553 if (entryname[0] > 'z') {
555 "Can't generate a unique entry name");
559 snprintf(buf, sizeof buf,
560 "grep %s: /etc/inittab >/dev/null 2>&1", entryname);
561 } while (system(buf) == 0);
564 /* Now write it out to /etc/inittab */
565 infp = fopen("/etc/inittab", "a");
567 display_error(strerror(errno));
569 fprintf(infp, "# Start the WebCit server...\n");
570 fprintf(infp, "h%s:2345:respawn:%s -p%s %s %s\n",
571 entryname, looking_for,
572 http_port, hostname, portname);
574 fprintf(infp, "s%s:2345:respawn:%s -p%s -s %s %s\n",
575 entryname, looking_for,
576 https_port, hostname, portname);
579 strcpy(init_entry, entryname);
587 * Figure out what type of user interface we're going to use
589 int discover_ui(void)
592 /* Use "dialog" if we have it */
593 if (getenv("CTDL_DIALOG") != NULL) {
600 newtDrawRootText(0, 0, "WebCit Setup");
610 int main(int argc, char *argv[])
615 strcpy(suggested_url, "http://<your_host_name>:<port>/");
617 /* set an invalid setup type */
620 /* Check to see if we're running the web installer */
621 if (getenv("CITADEL_INSTALLER") != NULL) {
622 using_web_installer = 1;
625 /* parse command line args */
626 for (a = 0; a < argc; ++a) {
627 if (!strncmp(argv[a], "-u", 2)) {
628 strcpy(aaa, argv[a]);
629 strcpy(aaa, &aaa[2]);
630 setup_type = atoi(aaa);
632 if (!strcmp(argv[a], "-i")) {
635 if (!strcmp(argv[a], "-q")) {
636 setup_type = UI_SILENT;
641 /* If a setup type was not specified, try to determine automatically
642 * the best one to use out of all available types.
644 if (setup_type < 0) {
645 setup_type = discover_ui();
647 if (info_only == 1) {
648 important_message("WebCit Setup", "Welcome to WebCit setup");
652 /* Get started in a valid setup directory. */
653 strcpy(setup_directory, WEBCITDIR);
654 if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
655 strcpy(setup_directory, getenv("WEBCIT"));
658 set_value("In what directory is WebCit installed?",
661 if (chdir(setup_directory) != 0) {
662 important_message("WebCit Setup",
663 "The directory you specified does not exist.");
667 /* See if we need to shut down the WebCit service. */
668 for (a=0; a<=3; ++a) {
669 progress("Shutting down the WebCit service...", a, 3);
670 if (a == 0) shutdown_service();
675 switch (setup_type) {
679 " *** WebCit setup program ***\n\n");
684 check_inittab_entry(); /* Check /etc/inittab */
686 /* See if we can start the WebCit service. */
687 if (strlen(init_entry) > 0) {
688 for (a=0; a<=3; ++a) {
689 progress("Starting the WebCit service...", a, 3);
690 if (a == 0) start_the_service();
694 "Setup is finished. You may now log in.\n"
695 "Point your web browser at %s\n", suggested_url);
696 important_message("Setup finished", aaa);
699 important_message("Setup finished",
700 "Setup is finished. You may now start the server.");