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 int lprintf(int loglevel, const char *format, ...){return 0;}
26 void wc_printf(const char *format,...){}
28 void RegisterNS(const char *NSName, long len,
31 WCHandlerFunc HandlerFunc,
32 WCPreevalFunc PreEvalFunc,
33 int ContextRequired){}
34 pthread_key_t MyConKey;
46 * Delete an entry from /etc/inittab
48 void delete_init_entry(char *which_entry)
59 if (inittab == NULL) return;
61 fp = fopen("/etc/inittab", "r");
62 if (fp == NULL) return;
64 while(fgets(buf, sizeof buf, fp) != NULL) {
66 if (num_tokens(buf, ':') == 4) {
67 extract_token(entry, buf, 0, ':', sizeof entry);
68 extract_token(levels, buf, 1, ':', sizeof levels);
69 extract_token(state, buf, 2, ':', sizeof state);
70 extract_token(prog, buf, 3, ':', sizeof prog); /* includes 0x0a LF */
72 if (!strcmp(entry, which_entry)) {
73 strcpy(state, "off"); /* disable it */
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 * Remove any /etc/inittab entries for webcit, because we don't
100 * start it that way anymore.
102 void delete_the_old_way(void) {
105 char looking_for[1024];
109 char init_entry[1024];
112 strcpy(init_entry, "");
114 /* Determine the fully qualified path name of webcit */
115 snprintf(looking_for, sizeof looking_for, "%s/webcit ", setup_directory);
117 /* Pound through /etc/inittab line by line. Set have_entry to 1 if
118 * an entry is found which we believe starts webcit.
120 infp = fopen("/etc/inittab", "r");
124 while (fgets(buf, sizeof buf, infp) != NULL) {
125 buf[strlen(buf) - 1] = 0;
126 extract_token(entry, buf, 0, ':', sizeof entry);
127 extract_token(prog, buf, 3, ':', sizeof prog);
128 if (!strncasecmp(prog, looking_for,
129 strlen(looking_for))) {
131 strcpy(init_entry, entry);
137 /* Bail out if there's nothing to do. */
138 if (!have_entry) return;
140 delete_init_entry(init_entry);
145 void cleanup(int exitcode)
152 void title(char *text)
154 if (setup_type == UI_TEXT) {
155 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<%s>\n", text);
162 int yesno(char *question, int default_value)
168 switch (setup_type) {
172 printf("%s\nYes/No [%s] --> ",
174 ( default_value ? "Yes" : "No" )
176 if (fgets(buf, sizeof buf, stdin))
178 answer = tolower(buf[0]);
179 if ((buf[0]==0) || (buf[0]==13) || (buf[0]==10))
180 answer = default_value;
181 else if (answer == 'y')
183 else if (answer == 'n')
187 } while ((answer < 0) || (answer > 1));
191 sprintf(buf, "exec %s %s --yesno '%s' 15 75",
192 getenv("CTDL_DIALOG"),
193 ( default_value ? "" : "--defaultno" ),
211 void set_value(char *prompt, char str[])
214 char dialog_result[PATH_MAX];
218 strcpy(setupmsg, "");
220 switch (setup_type) {
222 title("WebCit setup");
223 printf("\n%s\n", prompt);
224 printf("This is currently set to:\n%s\n", str);
225 printf("Enter new value or press return to leave unchanged:\n");
226 if (fgets(buf, sizeof buf, stdin)) {
227 buf[strlen(buf) - 1] = 0;
229 if (strlen(buf) != 0)
233 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
234 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%s' 2>%s",
235 getenv("CTDL_DIALOG"),
240 fp = fopen(dialog_result, "r");
242 if (fgets(str, sizeof buf, fp)){
243 if (str[strlen(str)-1] == 10) {
244 str[strlen(str)-1] = 0;
248 unlink(dialog_result);
256 extern const char *AvailLang[];
257 int GetLocalePrefs(void)
262 char dialog_result[PATH_MAX];
269 while (!IsEmptyStr(AvailLang[nLocales]))
274 StrBufAppendBufPlain(Buf, HKEY("Select the locale webcit should use : \n"), 0);
275 #ifdef HAVE_USELOCALE
276 StrBufAppendBufPlain(Buf, HKEY(" 0 Let the user select it at the login prompt (default)\n"), 0);
279 for (i = 0; i < nLocales; i++) {
280 StrBufAppendPrintf(Buf, " %ld: %s\n", i + offs, AvailLang[i]);
284 switch (setup_type) {
286 title("WebCit setup");
287 printf("\n%s\n", ChrPtr(Buf));
288 printf("This is currently set to:\n%ld\n", 0L);
289 printf("Enter new value or press return to leave unchanged:\n");
290 if (fgets(buf, sizeof buf, stdin))
295 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
296 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%ld' 2>%s",
297 getenv("CTDL_DIALOG"),
302 fp = fopen(dialog_result, "r");
305 if (fgets(str, sizeof buf, fp)){
306 if (str[strlen(str)-1] == 10) {
307 str[strlen(str)-1] = 0;
311 unlink(dialog_result);
320 void important_message(char *title, char *msgtext)
324 switch (setup_type) {
327 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");
328 printf(" %s \n\n%s\n\n", title, msgtext);
329 printf("Press return to continue...");
330 if (fgets(buf, sizeof buf, stdin));
334 sprintf(buf, "exec %s --msgbox '%s' 19 72",
335 getenv("CTDL_DIALOG"),
343 void display_error(char *error_message)
345 important_message("Error", error_message);
348 void progress(char *text, long int curr, long int cmax)
350 static long dots_printed = 0L;
353 static FILE *fp = NULL;
355 switch (setup_type) {
359 printf("%s\n", text);
360 printf("..........................");
361 printf("..........................");
362 printf("..........................\r");
365 } else if (curr == cmax) {
366 printf("\r%79s\n", "");
368 a = (curr * 100) / cmax;
371 while (dots_printed < a) {
381 sprintf(buf, "exec %s --gauge '%s' 7 72 0",
382 getenv("CTDL_DIALOG"),
384 fp = popen(buf, "w");
390 else if (curr == cmax) {
392 fprintf(fp, "100\n");
398 a = (curr * 100) / cmax;
400 fprintf(fp, "%ld\n", a);
412 * install_init_scripts() -- Create and deploy SysV init scripts.
415 void install_init_scripts(void)
424 char https_port[128];
429 struct utsname my_utsname;
430 struct stat etcinitd;
432 char *initfile = "/etc/init.d/webcit";
434 fp = fopen(initfile, "r");
436 if (yesno("WebCit already appears to be configured to start at boot.\n"
437 "Would you like to keep your boot configuration as is?\n", 1) == 1) {
444 /* Otherwise, prompt the user to create an entry. */
445 snprintf(question, sizeof question,
446 "Would you like to automatically start WebCit at boot?"
448 if (yesno(question, 1) == 0)
454 localechoice = GetLocalePrefs();
458 sprintf(http_port, "2000");
460 sprintf(https_port, "443");
462 sprintf(hostname, "uds");
463 sprintf(portname, "/usr/local/citadel");
465 /* This is a very hackish way of learning the port numbers used
466 * in a previous install, if we are upgrading: read them out of
467 * the existing init script.
469 if ((stat("/etc/init.d/", &etcinitd) == -1) &&
472 if ((stat("/etc/rc.d/init.d/", &etcinitd) == -1) &&
474 initfile = WEBCITDIR"/webcit.init";
476 initfile = "/etc/rc.d/init.d/webcit";
479 fp = fopen(initfile, "r");
481 while (fgets(buf, sizeof buf, fp) != NULL) {
482 if (strlen(buf) > 0) {
483 buf[strlen(buf)-1] = 0; /* strip trailing cr */
485 if (!strncasecmp(buf, "HTTP_PORT=", 10)) {
486 safestrncpy(http_port, &buf[10], sizeof http_port);
489 if (!strncasecmp(buf, "HTTPS_PORT=", 11)) {
490 safestrncpy(https_port, &buf[11], sizeof https_port);
493 if (!strncasecmp(buf, "CTDL_HOSTNAME=", 14)) {
494 safestrncpy(hostname, &buf[14], sizeof hostname);
496 if (!strncasecmp(buf, "CTDL_PORTNAME=", 14)) {
497 safestrncpy(portname, &buf[14], sizeof portname);
503 /* Now ask for the port numbers */
504 snprintf(question, sizeof question,
505 "On which port do you want WebCit to listen for HTTP "
506 "requests?\n\nYou can use the standard port (80) if you are "
507 "not running another\nweb server (such as Apache), otherwise "
508 "select another port.");
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 set_value(question, https_port);
522 /* Find out where Citadel is. */
523 if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
524 strcpy(hostname, "uds");
525 strcpy(portname, getenv("CITADEL"));
528 snprintf(question, sizeof question,
529 "Is the Citadel service running on the same host as WebCit?");
530 if (yesno(question, ((!strcasecmp(hostname, "uds")) ? 1 : 0))) {
531 strcpy(hostname, "uds");
532 if (atoi(portname) != 0) strcpy(portname, "/usr/local/citadel");
533 set_value("In what directory is Citadel installed?", portname);
536 if (!strcasecmp(hostname, "uds")) strcpy(hostname, "127.0.0.1");
537 if (atoi(portname) == 0) strcpy(portname, "504");
538 set_value("Enter the host name or IP address of your "
539 "Citadel server.", hostname);
540 set_value("Enter the port number on which Citadel is "
541 "running (usually 504)", portname);
546 fp = fopen(initfile, "w");
548 fprintf(fp, "#!/bin/sh\n"
550 "# uncomment this to create coredumps as described in\n"
551 "# http://www.citadel.org/doku.php/faq:mastering_your_os:gdb#how.do.i.make.my.system.produce.core-files\n"
552 "# ulimit -c unlimited\n"
553 "WEBCIT_DIR=%s\n", setup_directory);
554 fprintf(fp, "HTTP_PORT=%s\n", http_port);
556 fprintf(fp, "HTTPS_PORT=%s\n", https_port);
558 fprintf(fp, "CTDL_HOSTNAME=%s\n", hostname);
559 fprintf(fp, "CTDL_PORTNAME=%s\n", portname);
563 if (localechoice == 0) {
564 #ifdef HAVE_USELOCALE
565 fprintf(fp, "unset LANG\n");
567 fprintf(fp, "export WEBCIT_LANG=c\n");
571 fprintf(fp, "export WEBCIT_LANG=%s\n", AvailLang[localechoice - localeoffset]);
575 fprintf(fp, "# your system doesn't support locales\n");
581 "start) echo -n \"Starting WebCit... \"\n"
582 " if $WEBCIT_DIR/webcit "
583 "-D/var/run/webcit.pid "
584 "-p$HTTP_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
591 fprintf(fp, " echo -n \"Starting WebCit SSL... \"\n"
592 " if $WEBCIT_DIR/webcit "
593 "-D/var/run/webcit-ssl.pid "
594 "-s -p$HTTPS_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
602 "stop) echo -n \"Stopping WebCit... \"\n"
603 " if kill `cat /var/run/webcit.pid 2>/dev/null` 2>/dev/null\n"
609 " rm -f /var/run/webcit.pid 2>/dev/null\n");
611 fprintf(fp, " echo -n \"Stopping WebCit SSL... \"\n"
612 " if kill `cat /var/run/webcit-ssl.pid 2>/dev/null` 2>/dev/null\n"
618 " rm -f /var/run/webcit-ssl.pid 2>/dev/null\n");
624 "*) echo \"Usage: $0 {start|stop|restart}\"\n"
631 chmod(initfile, 0755);
633 /* Set up the run levels. */
634 system("/bin/rm -f /etc/rc?.d/[SK]??webcit 2>/dev/null");
635 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);
637 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);
646 * Figure out what type of user interface we're going to use
648 int discover_ui(void)
651 /* Use "dialog" if we have it */
652 if (getenv("CTDL_DIALOG") != NULL) {
663 int main(int argc, char *argv[])
668 strcpy(suggested_url, "http://<your_host_name>:<port>/");
670 /* set an invalid setup type */
673 /* Check to see if we're running the web installer */
674 if (getenv("CITADEL_INSTALLER") != NULL) {
675 using_web_installer = 1;
678 /* parse command line args */
679 for (a = 0; a < argc; ++a) {
680 if (!strncmp(argv[a], "-u", 2)) {
681 strcpy(aaa, argv[a]);
682 strcpy(aaa, &aaa[2]);
683 setup_type = atoi(aaa);
685 if (!strcmp(argv[a], "-i")) {
688 if (!strcmp(argv[a], "-q")) {
689 setup_type = UI_SILENT;
694 /* If a setup type was not specified, try to determine automatically
695 * the best one to use out of all available types.
697 if (setup_type < 0) {
698 setup_type = discover_ui();
700 if (info_only == 1) {
701 important_message("WebCit Setup", "Welcome to WebCit setup");
705 /* Get started in a valid setup directory. */
706 strcpy(setup_directory, WEBCITDIR);
707 if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
708 strcpy(setup_directory, getenv("WEBCIT"));
711 set_value("In what directory is WebCit installed?",
714 if (chdir(setup_directory) != 0) {
715 important_message("WebCit Setup",
716 "The directory you specified does not exist.");
721 * We used to start WebCit by putting it directly into /etc/inittab.
722 * Since some systems are moving away from init, we can't do this anymore.
724 progress("Removing obsolete /etc/inittab entries...", 0, 1);
725 delete_the_old_way();
726 progress("Removing obsolete /etc/inittab entries...", 1, 1);
729 switch (setup_type) {
733 " *** WebCit setup program ***\n\n");
739 * If we're running on SysV, install init scripts.
741 if (!access("/var/run", W_OK)) {
742 install_init_scripts();
744 if (!access("/etc/init.d/webcit", X_OK)) {
745 system("/etc/init.d/webcit stop");
746 system("/etc/init.d/webcit start");
750 "Setup is finished. You may now log in.\n"
751 "Point your web browser at %s\n", suggested_url
753 important_message("Setup finished", aaa);
757 important_message("Setup finished",
758 "Setup is finished. You may now start the server.");