4 * (This is basically just an install wizard. It's not required.)
12 #define UI_TEXT 0 /* Default setup type -- text only */
13 #define UI_DIALOG 2 /* Use the 'dialog' program */
14 #define UI_SILENT 3 /* Silent running, for use in scripts */
17 char setup_directory[SIZ];
18 int using_web_installer = 0;
19 char suggested_url[SIZ];
22 int syslog(int loglevel, const char *format, ...){return 0;} */
23 void wc_printf(const char *format,...){}
25 void RegisterNS(const char *NSName, long len,
28 WCHandlerFunc HandlerFunc,
29 WCPreevalFunc PreEvalFunc,
30 int ContextRequired){}
31 void RegisterHeaderHandler(const char *Name, long Len, Header_Evaluator F){}
32 pthread_key_t MyConKey;
44 * Delete an entry from /etc/inittab
46 void delete_init_entry(char *which_entry)
58 if (inittab == NULL) return;
60 fp = fopen("/etc/inittab", "r");
61 if (fp == NULL) return;
63 while(fgets(buf, sizeof buf, fp) != NULL) {
65 if (num_tokens(buf, ':') == 4) {
66 extract_token(entry, buf, 0, ':', sizeof entry);
67 extract_token(levels, buf, 1, ':', sizeof levels);
68 extract_token(state, buf, 2, ':', sizeof state);
69 extract_token(prog, buf, 3, ':', sizeof prog); /* includes 0x0a LF */
71 if (!strcmp(entry, which_entry)) {
72 strcpy(state, "off"); /* disable it */
76 inittab = realloc(inittab, strlen(inittab) + strlen(buf) + 2);
77 if (inittab == NULL) {
85 fp = fopen("/etc/inittab", "w");
87 rv = fwrite(inittab, strlen(inittab), 1, fp);
89 kill(1, SIGHUP); /* Tell init to re-read /etc/inittab */
98 * Remove any /etc/inittab entries for webcit, because we don't
99 * start it that way anymore.
101 void delete_the_old_way(void) {
104 char looking_for[1024];
108 char init_entry[1024];
111 strcpy(init_entry, "");
113 /* Determine the fully qualified path name of webcit */
114 snprintf(looking_for, sizeof looking_for, "%s/webcit ", setup_directory);
116 /* Pound through /etc/inittab line by line. Set have_entry to 1 if
117 * an entry is found which we believe starts webcit.
119 infp = fopen("/etc/inittab", "r");
123 while (fgets(buf, sizeof buf, infp) != NULL) {
124 buf[strlen(buf) - 1] = 0;
125 extract_token(entry, buf, 0, ':', sizeof entry);
126 extract_token(prog, buf, 3, ':', sizeof prog);
127 if (!strncasecmp(prog, looking_for,
128 strlen(looking_for))) {
130 strcpy(init_entry, entry);
136 /* Bail out if there's nothing to do. */
137 if (!have_entry) return;
139 delete_init_entry(init_entry);
144 void cleanup(int exitcode)
151 void title(char *text)
153 if (setup_type == UI_TEXT) {
154 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<%s>\n", text);
161 int yesno(char *question, int default_value)
167 switch (setup_type) {
171 printf("%s\nYes/No [%s] --> ",
173 ( default_value ? "Yes" : "No" )
175 if (fgets(buf, sizeof buf, stdin))
177 answer = tolower(buf[0]);
178 if ((buf[0]==0) || (buf[0]==13) || (buf[0]==10))
179 answer = default_value;
180 else if (answer == 'y')
182 else if (answer == 'n')
186 } while ((answer < 0) || (answer > 1));
190 sprintf(buf, "exec %s %s --yesno '%s' 15 75",
191 getenv("CTDL_DIALOG"),
192 ( default_value ? "" : "--defaultno" ),
210 void set_value(char *prompt, char str[])
213 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];
270 while (!IsEmptyStr(AvailLang[nLocales]))
275 StrBufAppendBufPlain(Buf, HKEY("Select the locale webcit should use : \n"), 0);
276 #ifdef HAVE_USELOCALE
277 StrBufAppendBufPlain(Buf, HKEY(" 0 Let the user select it at the login prompt (default)\n"), 0);
280 for (i = 0; i < nLocales; i++) {
281 StrBufAppendPrintf(Buf, " %ld: %s\n", i + offs, AvailLang[i]);
285 switch (setup_type) {
287 title("WebCit setup");
288 printf("\n%s\n", ChrPtr(Buf));
289 printf("This is currently set to:\n%ld\n", 0L);
290 printf("Enter new value or press return to leave unchanged:\n");
291 if (fgets(buf, sizeof buf, stdin))
296 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
297 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%ld' 2>%s",
298 getenv("CTDL_DIALOG"),
303 fp = fopen(dialog_result, "r");
306 if (fgets(str, sizeof buf, fp)){
307 if (str[strlen(str)-1] == 10) {
308 str[strlen(str)-1] = 0;
312 unlink(dialog_result);
321 void important_message(char *title, char *msgtext)
326 switch (setup_type) {
329 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");
330 printf(" %s \n\n%s\n\n", title, msgtext);
331 printf("Press return to continue...");
332 if (fgets(buf, sizeof buf, stdin));
336 sprintf(buf, "exec %s --msgbox '%s' 19 72",
337 getenv("CTDL_DIALOG"),
345 void display_error(char *error_message)
347 important_message("Error", error_message);
350 void progress(char *text, long int curr, long int cmax)
352 static long dots_printed = 0L;
355 static FILE *fp = NULL;
357 switch (setup_type) {
361 printf("%s\n", text);
362 printf("..........................");
363 printf("..........................");
364 printf("..........................\r");
367 } else if (curr == cmax) {
368 printf("\r%79s\n", "");
370 a = (curr * 100) / cmax;
373 while (dots_printed < a) {
383 sprintf(buf, "exec %s --gauge '%s' 7 72 0",
384 getenv("CTDL_DIALOG"),
386 fp = popen(buf, "w");
392 else if (curr == cmax) {
394 fprintf(fp, "100\n");
400 a = (curr * 100) / cmax;
402 fprintf(fp, "%ld\n", a);
414 * install_init_scripts() -- Create and deploy SysV init scripts.
417 void install_init_scripts(void)
426 char https_port[128];
431 struct utsname my_utsname;
432 struct stat etcinitd;
434 char *initfile = "/etc/init.d/webcit";
437 fp = fopen(initfile, "r");
439 if (yesno("WebCit already appears to be configured to start at boot.\n"
440 "Would you like to keep your boot configuration as is?\n", 1) == 1) {
447 /* Otherwise, prompt the user to create an entry. */
448 snprintf(question, sizeof question,
449 "Would you like to automatically start WebCit at boot?"
451 if (yesno(question, 1) == 0)
457 localechoice = GetLocalePrefs();
461 sprintf(http_port, "2000");
463 sprintf(https_port, "443");
465 sprintf(hostname, "uds");
466 sprintf(portname, "/usr/local/citadel");
468 /* This is a very hackish way of learning the port numbers used
469 * in a previous install, if we are upgrading: read them out of
470 * the existing init script.
472 if ((stat("/etc/init.d/", &etcinitd) == -1) &&
475 if ((stat("/etc/rc.d/init.d/", &etcinitd) == -1) &&
477 initfile = WEBCITDIR"/webcit.init";
479 initfile = "/etc/rc.d/init.d/webcit";
482 fp = fopen(initfile, "r");
484 while (fgets(buf, sizeof buf, fp) != NULL) {
485 if (strlen(buf) > 0) {
486 buf[strlen(buf)-1] = 0; /* strip trailing cr */
488 if (!strncasecmp(buf, "HTTP_PORT=", 10)) {
489 safestrncpy(http_port, &buf[10], sizeof http_port);
492 if (!strncasecmp(buf, "HTTPS_PORT=", 11)) {
493 safestrncpy(https_port, &buf[11], sizeof https_port);
496 if (!strncasecmp(buf, "CTDL_HOSTNAME=", 14)) {
497 safestrncpy(hostname, &buf[14], sizeof hostname);
499 if (!strncasecmp(buf, "CTDL_PORTNAME=", 14)) {
500 safestrncpy(portname, &buf[14], sizeof portname);
506 /* Now ask for the port numbers */
507 snprintf(question, sizeof question,
508 "On which port do you want WebCit to listen for HTTP "
509 "requests?\n\nYou can use the standard port (80) if you are "
510 "not running another\nweb server (such as Apache), otherwise "
511 "select another port.");
512 set_value(question, http_port);
514 sprintf(suggested_url, "http://%s:%s/", my_utsname.nodename, http_port);
517 snprintf(question, sizeof question,
518 "On which port do you want WebCit to listen for HTTPS "
519 "requests?\n\nYou can use the standard port (443) if you are "
520 "not running another\nweb server (such as Apache), otherwise "
521 "select another port.");
522 set_value(question, https_port);
525 /* Find out where Citadel is. */
526 if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
527 strcpy(hostname, "uds");
528 strcpy(portname, getenv("CITADEL"));
531 snprintf(question, sizeof question,
532 "Is the Citadel service running on the same host as WebCit?");
533 if (yesno(question, ((!strcasecmp(hostname, "uds")) ? 1 : 0))) {
534 strcpy(hostname, "uds");
535 if (atoi(portname) != 0) strcpy(portname, "/usr/local/citadel");
536 set_value("In what directory is Citadel installed?", portname);
539 if (!strcasecmp(hostname, "uds")) strcpy(hostname, "127.0.0.1");
540 if (atoi(portname) == 0) strcpy(portname, "504");
541 set_value("Enter the host name or IP address of your "
542 "Citadel server.", hostname);
543 set_value("Enter the port number on which Citadel is "
544 "running (usually 504)", portname);
549 fp = fopen(initfile, "w");
551 fprintf(fp, "#!/bin/sh\n"
553 "# uncomment this to create coredumps as described in\n"
554 "# http://www.citadel.org/doku.php/faq:mastering_your_os:gdb#how.do.i.make.my.system.produce.core-files\n"
555 "# ulimit -c unlimited\n"
556 "WEBCIT_DIR=%s\n", setup_directory);
557 fprintf(fp, "HTTP_PORT=%s\n", http_port);
559 fprintf(fp, "HTTPS_PORT=%s\n", https_port);
561 fprintf(fp, "CTDL_HOSTNAME=%s\n", hostname);
562 fprintf(fp, "CTDL_PORTNAME=%s\n", portname);
566 if (localechoice == 0) {
567 #ifdef HAVE_USELOCALE
568 fprintf(fp, "unset LANG\n");
570 fprintf(fp, "export WEBCIT_LANG=c\n");
574 fprintf(fp, "export WEBCIT_LANG=%s\n", AvailLang[localechoice - localeoffset]);
578 fprintf(fp, "# your system doesn't support locales\n");
584 "start) echo -n \"Starting WebCit... \"\n"
585 " if $WEBCIT_DIR/webcit "
586 "-D/var/run/webcit.pid "
587 "-p$HTTP_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
594 fprintf(fp, " echo -n \"Starting WebCit SSL... \"\n"
595 " if $WEBCIT_DIR/webcit "
596 "-D/var/run/webcit-ssl.pid "
597 "-s -p$HTTPS_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
605 "stop) echo -n \"Stopping WebCit... \"\n"
606 " if kill `cat /var/run/webcit.pid 2>/dev/null` 2>/dev/null\n"
612 " rm -f /var/run/webcit.pid 2>/dev/null\n");
614 fprintf(fp, " echo -n \"Stopping WebCit SSL... \"\n"
615 " if kill `cat /var/run/webcit-ssl.pid 2>/dev/null` 2>/dev/null\n"
621 " rm -f /var/run/webcit-ssl.pid 2>/dev/null\n");
627 "*) echo \"Usage: $0 {start|stop|restart}\"\n"
634 chmod(initfile, 0755);
636 /* Set up the run levels. */
637 rv = system("/bin/rm -f /etc/rc?.d/[SK]??webcit 2>/dev/null");
638 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);
639 rv = system(command);
640 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);
641 rv = system(command);
649 * Figure out what type of user interface we're going to use
651 int discover_ui(void)
654 /* Use "dialog" if we have it */
655 if (getenv("CTDL_DIALOG") != NULL) {
666 int main(int argc, char *argv[])
673 strcpy(suggested_url, "http://<your_host_name>:<port>/");
675 /* set an invalid setup type */
678 /* Check to see if we're running the web installer */
679 if (getenv("CITADEL_INSTALLER") != NULL) {
680 using_web_installer = 1;
683 /* parse command line args */
684 for (a = 0; a < argc; ++a) {
685 if (!strncmp(argv[a], "-u", 2)) {
686 strcpy(aaa, argv[a]);
687 strcpy(aaa, &aaa[2]);
688 setup_type = atoi(aaa);
690 if (!strcmp(argv[a], "-i")) {
693 if (!strcmp(argv[a], "-q")) {
694 setup_type = UI_SILENT;
699 /* If a setup type was not specified, try to determine automatically
700 * the best one to use out of all available types.
702 if (setup_type < 0) {
703 setup_type = discover_ui();
705 if (info_only == 1) {
706 important_message("WebCit Setup", "Welcome to WebCit setup");
710 /* Get started in a valid setup directory. */
711 strcpy(setup_directory, WEBCITDIR);
712 if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
713 strcpy(setup_directory, getenv("WEBCIT"));
716 set_value("In what directory is WebCit installed?",
719 if (chdir(setup_directory) != 0) {
720 important_message("WebCit Setup",
721 "The directory you specified does not exist.");
726 * We used to start WebCit by putting it directly into /etc/inittab.
727 * Since some systems are moving away from init, we can't do this anymore.
729 progress("Removing obsolete /etc/inittab entries...", 0, 1);
730 delete_the_old_way();
731 progress("Removing obsolete /etc/inittab entries...", 1, 1);
734 switch (setup_type) {
738 " *** WebCit setup program ***\n\n");
744 * If we're running on SysV, install init scripts.
746 if (!access("/var/run", W_OK)) {
747 install_init_scripts();
749 if (!access("/etc/init.d/webcit", X_OK)) {
750 rv = system("/etc/init.d/webcit stop");
751 rv = system("/etc/init.d/webcit start");
755 "Setup is finished. You may now log in.\n"
756 "Point your web browser at %s\n", suggested_url
758 important_message("Setup finished", aaa);
762 important_message("Setup finished",
763 "Setup is finished. You may now start the server.");