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 void RegisterHeaderHandler(const char *Name, long Len, Header_Evaluator F){}
35 pthread_key_t MyConKey;
47 * Delete an entry from /etc/inittab
49 void delete_init_entry(char *which_entry)
61 if (inittab == NULL) return;
63 fp = fopen("/etc/inittab", "r");
64 if (fp == NULL) return;
66 while(fgets(buf, sizeof buf, fp) != NULL) {
68 if (num_tokens(buf, ':') == 4) {
69 extract_token(entry, buf, 0, ':', sizeof entry);
70 extract_token(levels, buf, 1, ':', sizeof levels);
71 extract_token(state, buf, 2, ':', sizeof state);
72 extract_token(prog, buf, 3, ':', sizeof prog); /* includes 0x0a LF */
74 if (!strcmp(entry, which_entry)) {
75 strcpy(state, "off"); /* disable it */
79 inittab = realloc(inittab, strlen(inittab) + strlen(buf) + 2);
80 if (inittab == NULL) {
88 fp = fopen("/etc/inittab", "w");
90 rv = fwrite(inittab, strlen(inittab), 1, fp);
92 kill(1, SIGHUP); /* Tell init to re-read /etc/inittab */
101 * Remove any /etc/inittab entries for webcit, because we don't
102 * start it that way anymore.
104 void delete_the_old_way(void) {
107 char looking_for[1024];
111 char init_entry[1024];
114 strcpy(init_entry, "");
116 /* Determine the fully qualified path name of webcit */
117 snprintf(looking_for, sizeof looking_for, "%s/webcit ", setup_directory);
119 /* Pound through /etc/inittab line by line. Set have_entry to 1 if
120 * an entry is found which we believe starts webcit.
122 infp = fopen("/etc/inittab", "r");
126 while (fgets(buf, sizeof buf, infp) != NULL) {
127 buf[strlen(buf) - 1] = 0;
128 extract_token(entry, buf, 0, ':', sizeof entry);
129 extract_token(prog, buf, 3, ':', sizeof prog);
130 if (!strncasecmp(prog, looking_for,
131 strlen(looking_for))) {
133 strcpy(init_entry, entry);
139 /* Bail out if there's nothing to do. */
140 if (!have_entry) return;
142 delete_init_entry(init_entry);
147 void cleanup(int exitcode)
154 void title(char *text)
156 if (setup_type == UI_TEXT) {
157 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<%s>\n", text);
164 int yesno(char *question, int default_value)
170 switch (setup_type) {
174 printf("%s\nYes/No [%s] --> ",
176 ( default_value ? "Yes" : "No" )
178 if (fgets(buf, sizeof buf, stdin))
180 answer = tolower(buf[0]);
181 if ((buf[0]==0) || (buf[0]==13) || (buf[0]==10))
182 answer = default_value;
183 else if (answer == 'y')
185 else if (answer == 'n')
189 } while ((answer < 0) || (answer > 1));
193 sprintf(buf, "exec %s %s --yesno '%s' 15 75",
194 getenv("CTDL_DIALOG"),
195 ( default_value ? "" : "--defaultno" ),
213 void set_value(char *prompt, char str[])
216 char dialog_result[PATH_MAX];
221 strcpy(setupmsg, "");
223 switch (setup_type) {
225 title("WebCit setup");
226 printf("\n%s\n", prompt);
227 printf("This is currently set to:\n%s\n", str);
228 printf("Enter new value or press return to leave unchanged:\n");
229 if (fgets(buf, sizeof buf, stdin)) {
230 buf[strlen(buf) - 1] = 0;
232 if (strlen(buf) != 0)
236 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
237 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%s' 2>%s",
238 getenv("CTDL_DIALOG"),
243 fp = fopen(dialog_result, "r");
245 if (fgets(str, sizeof buf, fp)){
246 if (str[strlen(str)-1] == 10) {
247 str[strlen(str)-1] = 0;
251 unlink(dialog_result);
259 extern const char *AvailLang[];
260 int GetLocalePrefs(void)
265 char dialog_result[PATH_MAX];
273 while (!IsEmptyStr(AvailLang[nLocales]))
278 StrBufAppendBufPlain(Buf, HKEY("Select the locale webcit should use : \n"), 0);
279 #ifdef HAVE_USELOCALE
280 StrBufAppendBufPlain(Buf, HKEY(" 0 Let the user select it at the login prompt (default)\n"), 0);
283 for (i = 0; i < nLocales; i++) {
284 StrBufAppendPrintf(Buf, " %ld: %s\n", i + offs, AvailLang[i]);
288 switch (setup_type) {
290 title("WebCit setup");
291 printf("\n%s\n", ChrPtr(Buf));
292 printf("This is currently set to:\n%ld\n", 0L);
293 printf("Enter new value or press return to leave unchanged:\n");
294 if (fgets(buf, sizeof buf, stdin))
299 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
300 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%ld' 2>%s",
301 getenv("CTDL_DIALOG"),
306 fp = fopen(dialog_result, "r");
309 if (fgets(str, sizeof buf, fp)){
310 if (str[strlen(str)-1] == 10) {
311 str[strlen(str)-1] = 0;
315 unlink(dialog_result);
324 void important_message(char *title, char *msgtext)
329 switch (setup_type) {
332 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");
333 printf(" %s \n\n%s\n\n", title, msgtext);
334 printf("Press return to continue...");
335 if (fgets(buf, sizeof buf, stdin));
339 sprintf(buf, "exec %s --msgbox '%s' 19 72",
340 getenv("CTDL_DIALOG"),
348 void display_error(char *error_message)
350 important_message("Error", error_message);
353 void progress(char *text, long int curr, long int cmax)
355 static long dots_printed = 0L;
358 static FILE *fp = NULL;
360 switch (setup_type) {
364 printf("%s\n", text);
365 printf("..........................");
366 printf("..........................");
367 printf("..........................\r");
370 } else if (curr == cmax) {
371 printf("\r%79s\n", "");
373 a = (curr * 100) / cmax;
376 while (dots_printed < a) {
386 sprintf(buf, "exec %s --gauge '%s' 7 72 0",
387 getenv("CTDL_DIALOG"),
389 fp = popen(buf, "w");
395 else if (curr == cmax) {
397 fprintf(fp, "100\n");
403 a = (curr * 100) / cmax;
405 fprintf(fp, "%ld\n", a);
417 * install_init_scripts() -- Create and deploy SysV init scripts.
420 void install_init_scripts(void)
429 char https_port[128];
434 struct utsname my_utsname;
435 struct stat etcinitd;
437 char *initfile = "/etc/init.d/webcit";
440 fp = fopen(initfile, "r");
442 if (yesno("WebCit already appears to be configured to start at boot.\n"
443 "Would you like to keep your boot configuration as is?\n", 1) == 1) {
450 /* Otherwise, prompt the user to create an entry. */
451 snprintf(question, sizeof question,
452 "Would you like to automatically start WebCit at boot?"
454 if (yesno(question, 1) == 0)
460 localechoice = GetLocalePrefs();
464 sprintf(http_port, "2000");
466 sprintf(https_port, "443");
468 sprintf(hostname, "uds");
469 sprintf(portname, "/usr/local/citadel");
471 /* This is a very hackish way of learning the port numbers used
472 * in a previous install, if we are upgrading: read them out of
473 * the existing init script.
475 if ((stat("/etc/init.d/", &etcinitd) == -1) &&
478 if ((stat("/etc/rc.d/init.d/", &etcinitd) == -1) &&
480 initfile = WEBCITDIR"/webcit.init";
482 initfile = "/etc/rc.d/init.d/webcit";
485 fp = fopen(initfile, "r");
487 while (fgets(buf, sizeof buf, fp) != NULL) {
488 if (strlen(buf) > 0) {
489 buf[strlen(buf)-1] = 0; /* strip trailing cr */
491 if (!strncasecmp(buf, "HTTP_PORT=", 10)) {
492 safestrncpy(http_port, &buf[10], sizeof http_port);
495 if (!strncasecmp(buf, "HTTPS_PORT=", 11)) {
496 safestrncpy(https_port, &buf[11], sizeof https_port);
499 if (!strncasecmp(buf, "CTDL_HOSTNAME=", 14)) {
500 safestrncpy(hostname, &buf[14], sizeof hostname);
502 if (!strncasecmp(buf, "CTDL_PORTNAME=", 14)) {
503 safestrncpy(portname, &buf[14], sizeof portname);
509 /* Now ask for the port numbers */
510 snprintf(question, sizeof question,
511 "On which port do you want WebCit to listen for HTTP "
512 "requests?\n\nYou can use the standard port (80) if you are "
513 "not running another\nweb server (such as Apache), otherwise "
514 "select another port.");
515 set_value(question, http_port);
517 sprintf(suggested_url, "http://%s:%s/", my_utsname.nodename, http_port);
520 snprintf(question, sizeof question,
521 "On which port do you want WebCit to listen for HTTPS "
522 "requests?\n\nYou can use the standard port (443) if you are "
523 "not running another\nweb server (such as Apache), otherwise "
524 "select another port.");
525 set_value(question, https_port);
528 /* Find out where Citadel is. */
529 if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
530 strcpy(hostname, "uds");
531 strcpy(portname, getenv("CITADEL"));
534 snprintf(question, sizeof question,
535 "Is the Citadel service running on the same host as WebCit?");
536 if (yesno(question, ((!strcasecmp(hostname, "uds")) ? 1 : 0))) {
537 strcpy(hostname, "uds");
538 if (atoi(portname) != 0) strcpy(portname, "/usr/local/citadel");
539 set_value("In what directory is Citadel installed?", portname);
542 if (!strcasecmp(hostname, "uds")) strcpy(hostname, "127.0.0.1");
543 if (atoi(portname) == 0) strcpy(portname, "504");
544 set_value("Enter the host name or IP address of your "
545 "Citadel server.", hostname);
546 set_value("Enter the port number on which Citadel is "
547 "running (usually 504)", portname);
552 fp = fopen(initfile, "w");
554 fprintf(fp, "#!/bin/sh\n"
556 "# uncomment this to create coredumps as described in\n"
557 "# http://www.citadel.org/doku.php/faq:mastering_your_os:gdb#how.do.i.make.my.system.produce.core-files\n"
558 "# ulimit -c unlimited\n"
559 "WEBCIT_DIR=%s\n", setup_directory);
560 fprintf(fp, "HTTP_PORT=%s\n", http_port);
562 fprintf(fp, "HTTPS_PORT=%s\n", https_port);
564 fprintf(fp, "CTDL_HOSTNAME=%s\n", hostname);
565 fprintf(fp, "CTDL_PORTNAME=%s\n", portname);
569 if (localechoice == 0) {
570 #ifdef HAVE_USELOCALE
571 fprintf(fp, "unset LANG\n");
573 fprintf(fp, "export WEBCIT_LANG=c\n");
577 fprintf(fp, "export WEBCIT_LANG=%s\n", AvailLang[localechoice - localeoffset]);
581 fprintf(fp, "# your system doesn't support locales\n");
587 "start) echo -n \"Starting WebCit... \"\n"
588 " if $WEBCIT_DIR/webcit "
589 "-D/var/run/webcit.pid "
590 "-p$HTTP_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
597 fprintf(fp, " echo -n \"Starting WebCit SSL... \"\n"
598 " if $WEBCIT_DIR/webcit "
599 "-D/var/run/webcit-ssl.pid "
600 "-s -p$HTTPS_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
608 "stop) echo -n \"Stopping WebCit... \"\n"
609 " if kill `cat /var/run/webcit.pid 2>/dev/null` 2>/dev/null\n"
615 " rm -f /var/run/webcit.pid 2>/dev/null\n");
617 fprintf(fp, " echo -n \"Stopping WebCit SSL... \"\n"
618 " if kill `cat /var/run/webcit-ssl.pid 2>/dev/null` 2>/dev/null\n"
624 " rm -f /var/run/webcit-ssl.pid 2>/dev/null\n");
630 "*) echo \"Usage: $0 {start|stop|restart}\"\n"
637 chmod(initfile, 0755);
639 /* Set up the run levels. */
640 rv = system("/bin/rm -f /etc/rc?.d/[SK]??webcit 2>/dev/null");
641 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);
642 rv = system(command);
643 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);
644 rv = system(command);
652 * Figure out what type of user interface we're going to use
654 int discover_ui(void)
657 /* Use "dialog" if we have it */
658 if (getenv("CTDL_DIALOG") != NULL) {
669 int main(int argc, char *argv[])
674 strcpy(suggested_url, "http://<your_host_name>:<port>/");
677 /* set an invalid setup type */
680 /* Check to see if we're running the web installer */
681 if (getenv("CITADEL_INSTALLER") != NULL) {
682 using_web_installer = 1;
685 /* parse command line args */
686 for (a = 0; a < argc; ++a) {
687 if (!strncmp(argv[a], "-u", 2)) {
688 strcpy(aaa, argv[a]);
689 strcpy(aaa, &aaa[2]);
690 setup_type = atoi(aaa);
692 if (!strcmp(argv[a], "-i")) {
695 if (!strcmp(argv[a], "-q")) {
696 setup_type = UI_SILENT;
701 /* If a setup type was not specified, try to determine automatically
702 * the best one to use out of all available types.
704 if (setup_type < 0) {
705 setup_type = discover_ui();
707 if (info_only == 1) {
708 important_message("WebCit Setup", "Welcome to WebCit setup");
712 /* Get started in a valid setup directory. */
713 strcpy(setup_directory, WEBCITDIR);
714 if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
715 strcpy(setup_directory, getenv("WEBCIT"));
718 set_value("In what directory is WebCit installed?",
721 if (chdir(setup_directory) != 0) {
722 important_message("WebCit Setup",
723 "The directory you specified does not exist.");
728 * We used to start WebCit by putting it directly into /etc/inittab.
729 * Since some systems are moving away from init, we can't do this anymore.
731 progress("Removing obsolete /etc/inittab entries...", 0, 1);
732 delete_the_old_way();
733 progress("Removing obsolete /etc/inittab entries...", 1, 1);
736 switch (setup_type) {
740 " *** WebCit setup program ***\n\n");
746 * If we're running on SysV, install init scripts.
748 if (!access("/var/run", W_OK)) {
749 install_init_scripts();
751 if (!access("/etc/init.d/webcit", X_OK)) {
752 rv = system("/etc/init.d/webcit stop");
753 rv = system("/etc/init.d/webcit start");
757 "Setup is finished. You may now log in.\n"
758 "Point your web browser at %s\n", suggested_url
760 important_message("Setup finished", aaa);
764 important_message("Setup finished",
765 "Setup is finished. You may now start the server.");