6 * (This is basically just an install wizard. It's not required.)
12 #include "webserver.h"
15 #define UI_TEXT 0 /* Default setup type -- text only */
16 #define UI_DIALOG 2 /* Use the 'dialog' program */
17 #define UI_SILENT 3 /* Silent running, for use in scripts */
20 char setup_directory[SIZ];
21 int using_web_installer = 0;
22 char suggested_url[SIZ];
25 * Delete an entry from /etc/inittab
27 void delete_init_entry(char *which_entry)
38 if (inittab == NULL) return;
40 fp = fopen("/etc/inittab", "r");
41 if (fp == NULL) return;
43 while(fgets(buf, sizeof buf, fp) != NULL) {
45 if (num_tokens(buf, ':') == 4) {
46 extract_token(entry, buf, 0, ':', sizeof entry);
47 extract_token(levels, buf, 1, ':', sizeof levels);
48 extract_token(state, buf, 2, ':', sizeof state);
49 extract_token(prog, buf, 3, ':', sizeof prog); /* includes 0x0a LF */
51 if (!strcmp(entry, which_entry)) {
52 strcpy(state, "off"); /* disable it */
56 inittab = realloc(inittab, strlen(inittab) + strlen(buf) + 2);
57 if (inittab == NULL) {
65 fp = fopen("/etc/inittab", "w");
67 fwrite(inittab, strlen(inittab), 1, fp);
69 kill(1, SIGHUP); /* Tell init to re-read /etc/inittab */
78 * Remove any /etc/inittab entries for webcit, because we don't
79 * start it that way anymore.
81 void delete_the_old_way(void) {
84 char looking_for[1024];
88 char init_entry[1024];
91 strcpy(init_entry, "");
93 /* Determine the fully qualified path name of webserver */
94 snprintf(looking_for, sizeof looking_for, "%s/webserver ", setup_directory);
96 /* Pound through /etc/inittab line by line. Set have_entry to 1 if
97 * an entry is found which we believe starts webserver.
99 infp = fopen("/etc/inittab", "r");
103 while (fgets(buf, sizeof buf, infp) != NULL) {
104 buf[strlen(buf) - 1] = 0;
105 extract_token(entry, buf, 0, ':', sizeof entry);
106 extract_token(prog, buf, 3, ':', sizeof prog);
107 if (!strncasecmp(prog, looking_for,
108 strlen(looking_for))) {
110 strcpy(init_entry, entry);
116 /* Bail out if there's nothing to do. */
117 if (!have_entry) return;
119 delete_init_entry(init_entry);
124 void cleanup(int exitcode)
131 void title(char *text)
133 if (setup_type == UI_TEXT) {
134 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<%s>\n", text);
141 int yesno(char *question, int default_value)
147 switch (setup_type) {
151 printf("%s\nYes/No [%s] --> ",
153 ( default_value ? "Yes" : "No" )
155 fgets(buf, sizeof buf, stdin);
156 answer = tolower(buf[0]);
157 if ((buf[0]==0) || (buf[0]==13) || (buf[0]==10))
158 answer = default_value;
159 else if (answer == 'y')
161 else if (answer == 'n')
163 } while ((answer < 0) || (answer > 1));
167 sprintf(buf, "exec %s %s --yesno '%s' 15 75",
168 getenv("CTDL_DIALOG"),
169 ( default_value ? "" : "--defaultno" ),
187 void set_value(char *prompt, char str[])
190 char dialog_result[PATH_MAX];
194 strcpy(setupmsg, "");
196 switch (setup_type) {
198 title("WebCit setup");
199 printf("\n%s\n", prompt);
200 printf("This is currently set to:\n%s\n", str);
201 printf("Enter new value or press return to leave unchanged:\n");
202 fgets(buf, sizeof buf, stdin);
203 buf[strlen(buf) - 1] = 0;
204 if (strlen(buf) != 0)
209 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
210 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%s' 2>%s",
211 getenv("CTDL_DIALOG"),
216 fp = fopen(dialog_result, "r");
218 fgets(str, sizeof buf, fp);
219 if (str[strlen(str)-1] == 10) {
220 str[strlen(str)-1] = 0;
223 unlink(dialog_result);
231 void important_message(char *title, char *msgtext)
235 switch (setup_type) {
238 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");
239 printf(" %s \n\n%s\n\n", title, msgtext);
240 printf("Press return to continue...");
241 fgets(buf, sizeof buf, stdin);
245 sprintf(buf, "exec %s --msgbox '%s' 19 72",
246 getenv("CTDL_DIALOG"),
254 void display_error(char *error_message)
256 important_message("Error", error_message);
259 void progress(char *text, long int curr, long int cmax)
261 static long dots_printed = 0L;
264 static FILE *fp = NULL;
266 switch (setup_type) {
270 printf("%s\n", text);
271 printf("..........................");
272 printf("..........................");
273 printf("..........................\r");
276 } else if (curr == cmax) {
277 printf("\r%79s\n", "");
279 a = (curr * 100) / cmax;
282 while (dots_printed < a) {
292 sprintf(buf, "exec %s --gauge '%s' 7 72 0",
293 getenv("CTDL_DIALOG"),
295 fp = popen(buf, "w");
301 else if (curr == cmax) {
303 fprintf(fp, "100\n");
309 a = (curr * 100) / cmax;
311 fprintf(fp, "%ld\n", a);
323 * install_init_scripts() -- Create and deploy SysV init scripts.
326 void install_init_scripts(void)
332 char https_port[128];
337 struct utsname my_utsname;
338 struct stat etcinitd;
340 char *initfile = "/etc/init.d/webcit";
342 fp = fopen(initfile, "r");
344 if (yesno("WebCit already appears to be configured to start at boot.\n"
345 "Would you like to keep your boot configuration as is?\n", 1) == 1) {
352 /* Otherwise, prompt the user to create an entry. */
353 snprintf(question, sizeof question,
354 "Would you like to automatically start WebCit at boot?"
356 if (yesno(question, 1) == 0)
360 sprintf(http_port, "2000");
362 sprintf(https_port, "443");
364 sprintf(hostname, "uds");
365 sprintf(portname, "/usr/local/citadel");
367 /* This is a very hackish way of learning the port numbers used
368 * in a previous install, if we are upgrading: read them out of
369 * the existing init script.
371 if ((stat("/etc/init.d/", &etcinitd) == -1) &&
374 if ((stat("/etc/rc.d/init.d/", &etcinitd) == -1) &&
376 initfile = WEBCITDIR"/webcit.init";
378 initfile = "/etc/rc.d/init.d/webcit";
381 fp = fopen(initfile, "r");
383 while (fgets(buf, sizeof buf, fp) != NULL) {
384 if (strlen(buf) > 0) {
385 buf[strlen(buf)-1] = 0; /* strip trailing cr */
387 if (!strncasecmp(buf, "HTTP_PORT=", 10)) {
388 safestrncpy(http_port, &buf[10], sizeof http_port);
391 if (!strncasecmp(buf, "HTTPS_PORT=", 11)) {
392 safestrncpy(https_port, &buf[11], sizeof https_port);
395 if (!strncasecmp(buf, "CTDL_HOSTNAME=", 14)) {
396 safestrncpy(hostname, &buf[14], sizeof hostname);
398 if (!strncasecmp(buf, "CTDL_PORTNAME=", 14)) {
399 safestrncpy(portname, &buf[14], sizeof portname);
405 /* Now ask for the port numbers */
406 snprintf(question, sizeof question,
407 "On which port do you want WebCit to listen for HTTP "
408 "requests?\n\nYou can use the standard port (80) if you are "
409 "not running another\nweb server (such as Apache), otherwise "
410 "select another port.");
411 set_value(question, http_port);
413 sprintf(suggested_url, "http://%s:%s/", my_utsname.nodename, http_port);
416 snprintf(question, sizeof question,
417 "On which port do you want WebCit to listen for HTTPS "
418 "requests?\n\nYou can use the standard port (443) if you are "
419 "not running another\nweb server (such as Apache), otherwise "
420 "select another port.");
421 set_value(question, https_port);
424 /* Find out where Citadel is. */
425 if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
426 strcpy(hostname, "uds");
427 strcpy(portname, getenv("CITADEL"));
430 snprintf(question, sizeof question,
431 "Is the Citadel service running on the same host as WebCit?");
432 if (yesno(question, ((!strcasecmp(hostname, "uds")) ? 1 : 0))) {
433 strcpy(hostname, "uds");
434 if (atoi(portname) != 0) strcpy(portname, "/usr/local/citadel");
435 set_value("In what directory is Citadel installed?", portname);
438 if (!strcasecmp(hostname, "uds")) strcpy(hostname, "127.0.0.1");
439 if (atoi(portname) == 0) strcpy(portname, "504");
440 set_value("Enter the host name or IP address of your "
441 "Citadel server.", hostname);
442 set_value("Enter the port number on which Citadel is "
443 "running (usually 504)", portname);
448 fp = fopen(initfile, "w");
450 fprintf(fp, "#!/bin/sh\n"
452 "WEBCIT_DIR=%s\n", setup_directory);
453 fprintf(fp, "HTTP_PORT=%s\n", http_port);
455 fprintf(fp, "HTTPS_PORT=%s\n", https_port);
457 fprintf(fp, "CTDL_HOSTNAME=%s\n", hostname);
458 fprintf(fp, "CTDL_PORTNAME=%s\n", portname);
463 "start) echo -n \"Starting WebCit... \"\n"
464 " if $WEBCIT_DIR/webserver "
465 "-D/var/run/webcit.pid "
466 "-p$HTTP_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
473 fprintf(fp, " echo -n \"Starting WebCit SSL... \"\n"
474 " if $WEBCIT_DIR/webserver "
475 "-D/var/run/webcit-ssl.pid "
476 "-s -p$HTTPS_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
484 "stop) echo -n \"Stopping WebCit... \"\n"
485 " if kill `cat /var/run/webcit.pid 2>/dev/null` 2>/dev/null\n"
491 " rm -f /var/run/webcit.pid 2>/dev/null\n");
493 fprintf(fp, " echo -n \"Stopping WebCit SSL... \"\n"
494 " if kill `cat /var/run/webcit-ssl.pid 2>/dev/null` 2>/dev/null\n"
500 " rm -f /var/run/webcit-ssl.pid 2>/dev/null\n");
506 "*) echo \"Usage: $0 {start|stop|restart}\"\n"
513 chmod(initfile, 0755);
515 /* Set up the run levels. */
516 system("/bin/rm -f /etc/rc?.d/[SK]??webcit 2>/dev/null");
517 snprintf(command, sizeof(command), "for x in 2 3 4 5 ; do [ -d /etc/rc$x.d ] && ln -s %s /etc/rc$x.d/S84webcit ; done 2>/dev/null", initfile);
519 snprintf(command, sizeof(command), "for x in 0 6 S; do [ -d /etc/rc$x.d ] && ln -s %s /etc/rc$x.d/K15webcit ; done 2>/dev/null", initfile);
528 * Figure out what type of user interface we're going to use
530 int discover_ui(void)
533 /* Use "dialog" if we have it */
534 if (getenv("CTDL_DIALOG") != NULL) {
545 int main(int argc, char *argv[])
550 strcpy(suggested_url, "http://<your_host_name>:<port>/");
552 /* set an invalid setup type */
555 /* Check to see if we're running the web installer */
556 if (getenv("CITADEL_INSTALLER") != NULL) {
557 using_web_installer = 1;
560 /* parse command line args */
561 for (a = 0; a < argc; ++a) {
562 if (!strncmp(argv[a], "-u", 2)) {
563 strcpy(aaa, argv[a]);
564 strcpy(aaa, &aaa[2]);
565 setup_type = atoi(aaa);
567 if (!strcmp(argv[a], "-i")) {
570 if (!strcmp(argv[a], "-q")) {
571 setup_type = UI_SILENT;
576 /* If a setup type was not specified, try to determine automatically
577 * the best one to use out of all available types.
579 if (setup_type < 0) {
580 setup_type = discover_ui();
582 if (info_only == 1) {
583 important_message("WebCit Setup", "Welcome to WebCit setup");
587 /* Get started in a valid setup directory. */
588 strcpy(setup_directory, WEBCITDIR);
589 if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
590 strcpy(setup_directory, getenv("WEBCIT"));
593 set_value("In what directory is WebCit installed?",
596 if (chdir(setup_directory) != 0) {
597 important_message("WebCit Setup",
598 "The directory you specified does not exist.");
603 * We used to start WebCit by putting it directly into /etc/inittab.
604 * Since some systems are moving away from init, we can't do this anymore.
606 progress("Removing obsolete /etc/inittab entries...", 0, 1);
607 delete_the_old_way();
608 progress("Removing obsolete /etc/inittab entries...", 1, 1);
611 switch (setup_type) {
615 " *** WebCit setup program ***\n\n");
621 * If we're running on SysV, install init scripts.
623 if (!access("/var/run", W_OK)) {
624 install_init_scripts();
626 if (!access("/etc/init.d/webcit", X_OK)) {
627 system("/etc/init.d/webcit stop");
628 system("/etc/init.d/webcit start");
632 "Setup is finished. You may now log in.\n"
633 "Point your web browser at %s\n", suggested_url
635 important_message("Setup finished", aaa);
639 important_message("Setup finished",
640 "Setup is finished. You may now start the server.");