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 RegisterNS(const char *NSName, long len,
29 WCHandlerFunc HandlerFunc,
30 int ContextRequired){}
31 pthread_key_t MyConKey;
33 #include "wc_gettext.h"
45 * Delete an entry from /etc/inittab
47 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 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];
217 strcpy(setupmsg, "");
219 switch (setup_type) {
221 title("WebCit setup");
222 printf("\n%s\n", prompt);
223 printf("This is currently set to:\n%s\n", str);
224 printf("Enter new value or press return to leave unchanged:\n");
225 if (fgets(buf, sizeof buf, stdin)) {
226 buf[strlen(buf) - 1] = 0;
228 if (strlen(buf) != 0)
232 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
233 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%s' 2>%s",
234 getenv("CTDL_DIALOG"),
239 fp = fopen(dialog_result, "r");
241 if (fgets(str, sizeof buf, fp)){
242 if (str[strlen(str)-1] == 10) {
243 str[strlen(str)-1] = 0;
247 unlink(dialog_result);
256 int GetLocalePrefs(void)
260 char dialog_result[PATH_MAX];
268 StrBufAppendBufPlain(Buf, HKEY("Select the locale webcit should use : \n"), 0);
269 #ifdef HAVE_USELOCALE
270 StrBufAppendBufPlain(Buf, HKEY(" 0 Let the user select it at the login prompt (default)\n"), 0);
273 for (i = 0; i < NUM_LANGS; i++) {
274 StrBufAppendPrintf(Buf, " %ld: %s\n", i + offs, AvailLang[i]);
278 switch (setup_type) {
280 title("WebCit setup");
281 printf("\n%s\n", ChrPtr(Buf));
282 printf("This is currently set to:\n%ld\n", 0L);
283 printf("Enter new value or press return to leave unchanged:\n");
284 if (fgets(buf, sizeof buf, stdin))
289 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
290 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%ld' 2>%s",
291 getenv("CTDL_DIALOG"),
296 fp = fopen(dialog_result, "r");
299 if (fgets(str, sizeof buf, fp)){
300 if (str[strlen(str)-1] == 10) {
301 str[strlen(str)-1] = 0;
305 unlink(dialog_result);
314 void important_message(char *title, char *msgtext)
318 switch (setup_type) {
321 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");
322 printf(" %s \n\n%s\n\n", title, msgtext);
323 printf("Press return to continue...");
324 if (fgets(buf, sizeof buf, stdin));
328 sprintf(buf, "exec %s --msgbox '%s' 19 72",
329 getenv("CTDL_DIALOG"),
337 void display_error(char *error_message)
339 important_message("Error", error_message);
342 void progress(char *text, long int curr, long int cmax)
344 static long dots_printed = 0L;
347 static FILE *fp = NULL;
349 switch (setup_type) {
353 printf("%s\n", text);
354 printf("..........................");
355 printf("..........................");
356 printf("..........................\r");
359 } else if (curr == cmax) {
360 printf("\r%79s\n", "");
362 a = (curr * 100) / cmax;
365 while (dots_printed < a) {
375 sprintf(buf, "exec %s --gauge '%s' 7 72 0",
376 getenv("CTDL_DIALOG"),
378 fp = popen(buf, "w");
384 else if (curr == cmax) {
386 fprintf(fp, "100\n");
392 a = (curr * 100) / cmax;
394 fprintf(fp, "%ld\n", a);
406 * install_init_scripts() -- Create and deploy SysV init scripts.
409 void install_init_scripts(void)
418 char https_port[128];
423 struct utsname my_utsname;
424 struct stat etcinitd;
426 char *initfile = "/etc/init.d/webcit";
428 fp = fopen(initfile, "r");
430 if (yesno("WebCit already appears to be configured to start at boot.\n"
431 "Would you like to keep your boot configuration as is?\n", 1) == 1) {
438 /* Otherwise, prompt the user to create an entry. */
439 snprintf(question, sizeof question,
440 "Would you like to automatically start WebCit at boot?"
442 if (yesno(question, 1) == 0)
448 localechoice = GetLocalePrefs();
452 sprintf(http_port, "2000");
454 sprintf(https_port, "443");
456 sprintf(hostname, "uds");
457 sprintf(portname, "/usr/local/citadel");
459 /* This is a very hackish way of learning the port numbers used
460 * in a previous install, if we are upgrading: read them out of
461 * the existing init script.
463 if ((stat("/etc/init.d/", &etcinitd) == -1) &&
466 if ((stat("/etc/rc.d/init.d/", &etcinitd) == -1) &&
468 initfile = WEBCITDIR"/webcit.init";
470 initfile = "/etc/rc.d/init.d/webcit";
473 fp = fopen(initfile, "r");
475 while (fgets(buf, sizeof buf, fp) != NULL) {
476 if (strlen(buf) > 0) {
477 buf[strlen(buf)-1] = 0; /* strip trailing cr */
479 if (!strncasecmp(buf, "HTTP_PORT=", 10)) {
480 safestrncpy(http_port, &buf[10], sizeof http_port);
483 if (!strncasecmp(buf, "HTTPS_PORT=", 11)) {
484 safestrncpy(https_port, &buf[11], sizeof https_port);
487 if (!strncasecmp(buf, "CTDL_HOSTNAME=", 14)) {
488 safestrncpy(hostname, &buf[14], sizeof hostname);
490 if (!strncasecmp(buf, "CTDL_PORTNAME=", 14)) {
491 safestrncpy(portname, &buf[14], sizeof portname);
497 /* Now ask for the port numbers */
498 snprintf(question, sizeof question,
499 "On which port do you want WebCit to listen for HTTP "
500 "requests?\n\nYou can use the standard port (80) if you are "
501 "not running another\nweb server (such as Apache), otherwise "
502 "select another port.");
503 set_value(question, http_port);
505 sprintf(suggested_url, "http://%s:%s/", my_utsname.nodename, http_port);
508 snprintf(question, sizeof question,
509 "On which port do you want WebCit to listen for HTTPS "
510 "requests?\n\nYou can use the standard port (443) if you are "
511 "not running another\nweb server (such as Apache), otherwise "
512 "select another port.");
513 set_value(question, https_port);
516 /* Find out where Citadel is. */
517 if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
518 strcpy(hostname, "uds");
519 strcpy(portname, getenv("CITADEL"));
522 snprintf(question, sizeof question,
523 "Is the Citadel service running on the same host as WebCit?");
524 if (yesno(question, ((!strcasecmp(hostname, "uds")) ? 1 : 0))) {
525 strcpy(hostname, "uds");
526 if (atoi(portname) != 0) strcpy(portname, "/usr/local/citadel");
527 set_value("In what directory is Citadel installed?", portname);
530 if (!strcasecmp(hostname, "uds")) strcpy(hostname, "127.0.0.1");
531 if (atoi(portname) == 0) strcpy(portname, "504");
532 set_value("Enter the host name or IP address of your "
533 "Citadel server.", hostname);
534 set_value("Enter the port number on which Citadel is "
535 "running (usually 504)", portname);
540 fp = fopen(initfile, "w");
542 fprintf(fp, "#!/bin/sh\n"
544 "# uncomment this to create coredumps as described in\n"
545 "# http://www.citadel.org/doku.php/faq:mastering_your_os:gdb#how.do.i.make.my.system.produce.core-files\n"
546 "# ulimit -c unlimited\n"
547 "WEBCIT_DIR=%s\n", setup_directory);
548 fprintf(fp, "HTTP_PORT=%s\n", http_port);
550 fprintf(fp, "HTTPS_PORT=%s\n", https_port);
552 fprintf(fp, "CTDL_HOSTNAME=%s\n", hostname);
553 fprintf(fp, "CTDL_PORTNAME=%s\n", portname);
557 if (localechoice == 0) {
558 #ifdef HAVE_USELOCALE
559 fprintf(fp, "unset LANG\n");
561 fprintf(fp, "export WEBCIT_LANG=c\n");
565 fprintf(fp, "export WEBCIT_LANG=%s\n", AvailLang[localechoice - localeoffset]);
569 fprintf(fp, "# your system doesn't support locales\n");
575 "start) echo -n \"Starting WebCit... \"\n"
576 " if $WEBCIT_DIR/webcit "
577 "-D/var/run/webcit.pid "
578 "-p$HTTP_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
585 fprintf(fp, " echo -n \"Starting WebCit SSL... \"\n"
586 " if $WEBCIT_DIR/webcit "
587 "-D/var/run/webcit-ssl.pid "
588 "-s -p$HTTPS_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
596 "stop) echo -n \"Stopping WebCit... \"\n"
597 " if kill `cat /var/run/webcit.pid 2>/dev/null` 2>/dev/null\n"
603 " rm -f /var/run/webcit.pid 2>/dev/null\n");
605 fprintf(fp, " echo -n \"Stopping WebCit SSL... \"\n"
606 " if kill `cat /var/run/webcit-ssl.pid 2>/dev/null` 2>/dev/null\n"
612 " rm -f /var/run/webcit-ssl.pid 2>/dev/null\n");
618 "*) echo \"Usage: $0 {start|stop|restart}\"\n"
625 chmod(initfile, 0755);
627 /* Set up the run levels. */
628 system("/bin/rm -f /etc/rc?.d/[SK]??webcit 2>/dev/null");
629 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);
631 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);
640 * Figure out what type of user interface we're going to use
642 int discover_ui(void)
645 /* Use "dialog" if we have it */
646 if (getenv("CTDL_DIALOG") != NULL) {
657 int main(int argc, char *argv[])
662 strcpy(suggested_url, "http://<your_host_name>:<port>/");
664 /* set an invalid setup type */
667 /* Check to see if we're running the web installer */
668 if (getenv("CITADEL_INSTALLER") != NULL) {
669 using_web_installer = 1;
672 /* parse command line args */
673 for (a = 0; a < argc; ++a) {
674 if (!strncmp(argv[a], "-u", 2)) {
675 strcpy(aaa, argv[a]);
676 strcpy(aaa, &aaa[2]);
677 setup_type = atoi(aaa);
679 if (!strcmp(argv[a], "-i")) {
682 if (!strcmp(argv[a], "-q")) {
683 setup_type = UI_SILENT;
688 /* If a setup type was not specified, try to determine automatically
689 * the best one to use out of all available types.
691 if (setup_type < 0) {
692 setup_type = discover_ui();
694 if (info_only == 1) {
695 important_message("WebCit Setup", "Welcome to WebCit setup");
699 /* Get started in a valid setup directory. */
700 strcpy(setup_directory, WEBCITDIR);
701 if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
702 strcpy(setup_directory, getenv("WEBCIT"));
705 set_value("In what directory is WebCit installed?",
708 if (chdir(setup_directory) != 0) {
709 important_message("WebCit Setup",
710 "The directory you specified does not exist.");
715 * We used to start WebCit by putting it directly into /etc/inittab.
716 * Since some systems are moving away from init, we can't do this anymore.
718 progress("Removing obsolete /etc/inittab entries...", 0, 1);
719 delete_the_old_way();
720 progress("Removing obsolete /etc/inittab entries...", 1, 1);
723 switch (setup_type) {
727 " *** WebCit setup program ***\n\n");
733 * If we're running on SysV, install init scripts.
735 if (!access("/var/run", W_OK)) {
736 install_init_scripts();
738 if (!access("/etc/init.d/webcit", X_OK)) {
739 system("/etc/init.d/webcit stop");
740 system("/etc/init.d/webcit start");
744 "Setup is finished. You may now log in.\n"
745 "Point your web browser at %s\n", suggested_url
747 important_message("Setup finished", aaa);
751 important_message("Setup finished",
752 "Setup is finished. You may now start the server.");