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];
336 struct utsname my_utsname;
340 /* Otherwise, prompt the user to create an entry. */
341 snprintf(question, sizeof question,
342 "Would you like to automatically start WebCit at boot?"
344 if (yesno(question, 1) == 0)
348 sprintf(http_port, "2000");
350 sprintf(https_port, "443");
352 sprintf(hostname, "uds");
353 sprintf(portname, "/usr/local/citadel");
355 /* This is a very hackish way of learning the port numbers used
356 * in a previous install, if we are upgrading: read them out of
357 * the existing init script.
359 fp = fopen("/etc/init.d/webcit", "r");
361 while (fgets(buf, sizeof buf, fp) != NULL) {
362 if (strlen(buf) > 0) {
363 buf[strlen(buf)-1] = 0; /* strip trailing cr */
365 if (!strncasecmp(buf, "HTTP_PORT=", 10)) {
366 safestrncpy(http_port, &buf[10], sizeof http_port);
369 if (!strncasecmp(buf, "HTTPS_PORT=", 11)) {
370 safestrncpy(https_port, &buf[11], sizeof https_port);
373 if (!strncasecmp(buf, "CTDL_HOSTNAME=", 14)) {
374 safestrncpy(hostname, &buf[14], sizeof hostname);
376 if (!strncasecmp(buf, "CTDL_PORTNAME=", 14)) {
377 safestrncpy(portname, &buf[14], sizeof portname);
383 /* Now ask for the port numbers */
384 snprintf(question, sizeof question,
385 "On which port do you want WebCit to listen for HTTP "
386 "requests?\n\nYou can use the standard port (80) if you are "
387 "not running another\nweb server (such as Apache), otherwise "
388 "select another port.");
389 set_value(question, http_port);
391 sprintf(suggested_url, "http://%s:%s/", my_utsname.nodename, http_port);
394 snprintf(question, sizeof question,
395 "On which port do you want WebCit to listen for HTTPS "
396 "requests?\n\nYou can use the standard port (443) if you are "
397 "not running another\nweb server (such as Apache), otherwise "
398 "select another port.");
399 set_value(question, https_port);
402 /* Find out where Citadel is. */
403 if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
404 strcpy(hostname, "uds");
405 strcpy(portname, getenv("CITADEL"));
408 snprintf(question, sizeof question,
409 "Is the Citadel service running on the same host as WebCit?");
410 if (yesno(question, ((!strcasecmp(hostname, "uds")) ? 1 : 0))) {
411 strcpy(hostname, "uds");
412 if (atoi(portname) != 0) strcpy(portname, "/usr/local/citadel");
413 set_value("In what directory is Citadel installed?", portname);
416 if (!strcasecmp(hostname, "uds")) strcpy(hostname, "127.0.0.1");
417 if (atoi(portname) == 0) strcpy(portname, "504");
418 set_value("Enter the host name or IP address of your "
419 "Citadel server.", hostname);
420 set_value("Enter the port number on which Citadel is "
421 "running (usually 504)", portname);
426 fp = fopen("/etc/init.d/webcit", "w");
428 display_error("Cannot create /etc/init.d/webcit");
432 fprintf(fp, "#!/bin/sh\n"
434 "WEBCIT_DIR=%s\n", setup_directory);
435 fprintf(fp, "HTTP_PORT=%s\n", http_port);
437 fprintf(fp, "HTTPS_PORT=%s\n", https_port);
439 fprintf(fp, "CTDL_HOSTNAME=%s\n", hostname);
440 fprintf(fp, "CTDL_PORTNAME=%s\n", portname);
445 "start) echo -n \"Starting WebCit... \"\n"
446 " if $WEBCIT_DIR/webserver "
447 "-D/var/run/webcit.pid "
448 "-p$HTTP_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
455 fprintf(fp, " echo -n \"Starting WebCit SSL... \"\n"
456 " if $WEBCIT_DIR/webserver "
457 "-D/var/run/webcit-ssl.pid "
458 "-s -p$HTTPS_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
466 "stop) echo -n \"Stopping WebCit... \"\n"
467 " if kill `cat /var/run/webcit.pid 2>/dev/null` 2>/dev/null\n"
473 " rm -f /var/run/webcit.pid 2>/dev/null\n");
475 fprintf(fp, " echo -n \"Stopping WebCit SSL... \"\n"
476 " if kill `cat /var/run/webcit-ssl.pid 2>/dev/null` 2>/dev/null\n"
482 " rm -f /var/run/webcit-ssl.pid 2>/dev/null\n");
488 "*) echo \"Usage: $0 {start|stop|restart}\"\n"
495 chmod("/etc/init.d/webcit", 0755);
497 /* Set up the run levels. */
498 system("/bin/rm -f /etc/rc?.d/[SK]??webcit 2>/dev/null");
499 system("for x in 2 3 4 5 ; do [ -d /etc/rc$x.d ] && ln -s /etc/init.d/webcit /etc/rc$x.d/S84webcit ; done 2>/dev/null");
500 system("for x in 0 6 S; do [ -d /etc/rc$x.d ] && ln -s /etc/init.d/webcit /etc/rc$x.d/K15webcit ; done 2>/dev/null");
508 * Figure out what type of user interface we're going to use
510 int discover_ui(void)
513 /* Use "dialog" if we have it */
514 if (getenv("CTDL_DIALOG") != NULL) {
525 int main(int argc, char *argv[])
530 strcpy(suggested_url, "http://<your_host_name>:<port>/");
532 /* set an invalid setup type */
535 /* Check to see if we're running the web installer */
536 if (getenv("CITADEL_INSTALLER") != NULL) {
537 using_web_installer = 1;
540 /* parse command line args */
541 for (a = 0; a < argc; ++a) {
542 if (!strncmp(argv[a], "-u", 2)) {
543 strcpy(aaa, argv[a]);
544 strcpy(aaa, &aaa[2]);
545 setup_type = atoi(aaa);
547 if (!strcmp(argv[a], "-i")) {
550 if (!strcmp(argv[a], "-q")) {
551 setup_type = UI_SILENT;
556 /* If a setup type was not specified, try to determine automatically
557 * the best one to use out of all available types.
559 if (setup_type < 0) {
560 setup_type = discover_ui();
562 if (info_only == 1) {
563 important_message("WebCit Setup", "Welcome to WebCit setup");
567 /* Get started in a valid setup directory. */
568 strcpy(setup_directory, PREFIX);
569 if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
570 strcpy(setup_directory, getenv("WEBCIT"));
573 set_value("In what directory is WebCit installed?",
576 if (chdir(setup_directory) != 0) {
577 important_message("WebCit Setup",
578 "The directory you specified does not exist.");
583 * We used to start WebCit by putting it directly into /etc/inittab.
584 * Since some systems are moving away from init, we can't do this anymore.
586 progress("Removing obsolete /etc/inittab entries...", 0, 1);
587 delete_the_old_way();
588 progress("Removing obsolete /etc/inittab entries...", 1, 1);
591 switch (setup_type) {
595 " *** WebCit setup program ***\n\n");
601 * If we're running on SysV, install init scripts.
603 if (!access("/var/run", W_OK)) {
604 install_init_scripts();
606 if (!access("/etc/init.d/webcit", X_OK)) {
607 system("/etc/init.d/webcit stop");
608 system("/etc/init.d/webcit start");
612 "Setup is finished. You may now log in.\n"
613 "Point your web browser at %s\n", suggested_url
615 important_message("Setup finished", aaa);
619 important_message("Setup finished",
620 "Setup is finished. You may now start the server.");