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 /* Otherwise, prompt the user to create an entry. */
343 snprintf(question, sizeof question,
344 "Would you like to automatically start WebCit at boot?"
346 if (yesno(question, 1) == 0)
350 sprintf(http_port, "2000");
352 sprintf(https_port, "443");
354 sprintf(hostname, "uds");
355 sprintf(portname, "/usr/local/citadel");
357 /* This is a very hackish way of learning the port numbers used
358 * in a previous install, if we are upgrading: read them out of
359 * the existing init script.
361 if ((stat("/etc/init.d/", &etcinitd) == -1) &&
364 if ((stat("/etc/rc.d/init.d/", &etcinitd) == -1) &&
366 initfile = PREFIX"/webcit.init";
368 initfile = "/etc/rc.d/init.d/webcit";
371 fp = fopen(initfile, "r");
373 while (fgets(buf, sizeof buf, fp) != NULL) {
374 if (strlen(buf) > 0) {
375 buf[strlen(buf)-1] = 0; /* strip trailing cr */
377 if (!strncasecmp(buf, "HTTP_PORT=", 10)) {
378 safestrncpy(http_port, &buf[10], sizeof http_port);
381 if (!strncasecmp(buf, "HTTPS_PORT=", 11)) {
382 safestrncpy(https_port, &buf[11], sizeof https_port);
385 if (!strncasecmp(buf, "CTDL_HOSTNAME=", 14)) {
386 safestrncpy(hostname, &buf[14], sizeof hostname);
388 if (!strncasecmp(buf, "CTDL_PORTNAME=", 14)) {
389 safestrncpy(portname, &buf[14], sizeof portname);
395 /* Now ask for the port numbers */
396 snprintf(question, sizeof question,
397 "On which port do you want WebCit to listen for HTTP "
398 "requests?\n\nYou can use the standard port (80) if you are "
399 "not running another\nweb server (such as Apache), otherwise "
400 "select another port.");
401 set_value(question, http_port);
403 sprintf(suggested_url, "http://%s:%s/", my_utsname.nodename, http_port);
406 snprintf(question, sizeof question,
407 "On which port do you want WebCit to listen for HTTPS "
408 "requests?\n\nYou can use the standard port (443) if you are "
409 "not running another\nweb server (such as Apache), otherwise "
410 "select another port.");
411 set_value(question, https_port);
414 /* Find out where Citadel is. */
415 if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
416 strcpy(hostname, "uds");
417 strcpy(portname, getenv("CITADEL"));
420 snprintf(question, sizeof question,
421 "Is the Citadel service running on the same host as WebCit?");
422 if (yesno(question, ((!strcasecmp(hostname, "uds")) ? 1 : 0))) {
423 strcpy(hostname, "uds");
424 if (atoi(portname) != 0) strcpy(portname, "/usr/local/citadel");
425 set_value("In what directory is Citadel installed?", portname);
428 if (!strcasecmp(hostname, "uds")) strcpy(hostname, "127.0.0.1");
429 if (atoi(portname) == 0) strcpy(portname, "504");
430 set_value("Enter the host name or IP address of your "
431 "Citadel server.", hostname);
432 set_value("Enter the port number on which Citadel is "
433 "running (usually 504)", portname);
438 fp = fopen(initfile, "w");
440 fprintf(fp, "#!/bin/sh\n"
442 "WEBCIT_DIR=%s\n", setup_directory);
443 fprintf(fp, "HTTP_PORT=%s\n", http_port);
445 fprintf(fp, "HTTPS_PORT=%s\n", https_port);
447 fprintf(fp, "CTDL_HOSTNAME=%s\n", hostname);
448 fprintf(fp, "CTDL_PORTNAME=%s\n", portname);
453 "start) echo -n \"Starting WebCit... \"\n"
454 " if $WEBCIT_DIR/webserver "
455 "-D/var/run/webcit.pid "
456 "-p$HTTP_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
463 fprintf(fp, " echo -n \"Starting WebCit SSL... \"\n"
464 " if $WEBCIT_DIR/webserver "
465 "-D/var/run/webcit-ssl.pid "
466 "-s -p$HTTPS_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
474 "stop) echo -n \"Stopping WebCit... \"\n"
475 " if kill `cat /var/run/webcit.pid 2>/dev/null` 2>/dev/null\n"
481 " rm -f /var/run/webcit.pid 2>/dev/null\n");
483 fprintf(fp, " echo -n \"Stopping WebCit SSL... \"\n"
484 " if kill `cat /var/run/webcit-ssl.pid 2>/dev/null` 2>/dev/null\n"
490 " rm -f /var/run/webcit-ssl.pid 2>/dev/null\n");
496 "*) echo \"Usage: $0 {start|stop|restart}\"\n"
503 chmod(initfile, 0755);
505 /* Set up the run levels. */
506 system("/bin/rm -f /etc/rc?.d/[SK]??webcit 2>/dev/null");
507 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);
509 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);
518 * Figure out what type of user interface we're going to use
520 int discover_ui(void)
523 /* Use "dialog" if we have it */
524 if (getenv("CTDL_DIALOG") != NULL) {
535 int main(int argc, char *argv[])
540 strcpy(suggested_url, "http://<your_host_name>:<port>/");
542 /* set an invalid setup type */
545 /* Check to see if we're running the web installer */
546 if (getenv("CITADEL_INSTALLER") != NULL) {
547 using_web_installer = 1;
550 /* parse command line args */
551 for (a = 0; a < argc; ++a) {
552 if (!strncmp(argv[a], "-u", 2)) {
553 strcpy(aaa, argv[a]);
554 strcpy(aaa, &aaa[2]);
555 setup_type = atoi(aaa);
557 if (!strcmp(argv[a], "-i")) {
560 if (!strcmp(argv[a], "-q")) {
561 setup_type = UI_SILENT;
566 /* If a setup type was not specified, try to determine automatically
567 * the best one to use out of all available types.
569 if (setup_type < 0) {
570 setup_type = discover_ui();
572 if (info_only == 1) {
573 important_message("WebCit Setup", "Welcome to WebCit setup");
577 /* Get started in a valid setup directory. */
578 strcpy(setup_directory, PREFIX);
579 if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
580 strcpy(setup_directory, getenv("WEBCIT"));
583 set_value("In what directory is WebCit installed?",
586 if (chdir(setup_directory) != 0) {
587 important_message("WebCit Setup",
588 "The directory you specified does not exist.");
593 * We used to start WebCit by putting it directly into /etc/inittab.
594 * Since some systems are moving away from init, we can't do this anymore.
596 progress("Removing obsolete /etc/inittab entries...", 0, 1);
597 delete_the_old_way();
598 progress("Removing obsolete /etc/inittab entries...", 1, 1);
601 switch (setup_type) {
605 " *** WebCit setup program ***\n\n");
611 * If we're running on SysV, install init scripts.
613 if (!access("/var/run", W_OK)) {
614 install_init_scripts();
616 if (!access("/etc/init.d/webcit", X_OK)) {
617 system("/etc/init.d/webcit stop");
618 system("/etc/init.d/webcit start");
622 "Setup is finished. You may now log in.\n"
623 "Point your web browser at %s\n", suggested_url
625 important_message("Setup finished", aaa);
629 important_message("Setup finished",
630 "Setup is finished. You may now start the server.");