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;
43 * Delete an entry from /etc/inittab
45 void delete_init_entry(char *which_entry)
56 if (inittab == NULL) return;
58 fp = fopen("/etc/inittab", "r");
59 if (fp == NULL) return;
61 while(fgets(buf, sizeof buf, fp) != NULL) {
63 if (num_tokens(buf, ':') == 4) {
64 extract_token(entry, buf, 0, ':', sizeof entry);
65 extract_token(levels, buf, 1, ':', sizeof levels);
66 extract_token(state, buf, 2, ':', sizeof state);
67 extract_token(prog, buf, 3, ':', sizeof prog); /* includes 0x0a LF */
69 if (!strcmp(entry, which_entry)) {
70 strcpy(state, "off"); /* disable it */
74 inittab = realloc(inittab, strlen(inittab) + strlen(buf) + 2);
75 if (inittab == NULL) {
83 fp = fopen("/etc/inittab", "w");
85 fwrite(inittab, strlen(inittab), 1, fp);
87 kill(1, SIGHUP); /* Tell init to re-read /etc/inittab */
96 * Remove any /etc/inittab entries for webcit, because we don't
97 * start it that way anymore.
99 void delete_the_old_way(void) {
102 char looking_for[1024];
106 char init_entry[1024];
109 strcpy(init_entry, "");
111 /* Determine the fully qualified path name of webcit */
112 snprintf(looking_for, sizeof looking_for, "%s/webcit ", setup_directory);
114 /* Pound through /etc/inittab line by line. Set have_entry to 1 if
115 * an entry is found which we believe starts webcit.
117 infp = fopen("/etc/inittab", "r");
121 while (fgets(buf, sizeof buf, infp) != NULL) {
122 buf[strlen(buf) - 1] = 0;
123 extract_token(entry, buf, 0, ':', sizeof entry);
124 extract_token(prog, buf, 3, ':', sizeof prog);
125 if (!strncasecmp(prog, looking_for,
126 strlen(looking_for))) {
128 strcpy(init_entry, entry);
134 /* Bail out if there's nothing to do. */
135 if (!have_entry) return;
137 delete_init_entry(init_entry);
142 void cleanup(int exitcode)
149 void title(char *text)
151 if (setup_type == UI_TEXT) {
152 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<%s>\n", text);
159 int yesno(char *question, int default_value)
165 switch (setup_type) {
169 printf("%s\nYes/No [%s] --> ",
171 ( default_value ? "Yes" : "No" )
173 if (fgets(buf, sizeof buf, stdin))
175 answer = tolower(buf[0]);
176 if ((buf[0]==0) || (buf[0]==13) || (buf[0]==10))
177 answer = default_value;
178 else if (answer == 'y')
180 else if (answer == 'n')
184 } while ((answer < 0) || (answer > 1));
188 sprintf(buf, "exec %s %s --yesno '%s' 15 75",
189 getenv("CTDL_DIALOG"),
190 ( default_value ? "" : "--defaultno" ),
208 void set_value(char *prompt, char str[])
211 char dialog_result[PATH_MAX];
215 strcpy(setupmsg, "");
217 switch (setup_type) {
219 title("WebCit setup");
220 printf("\n%s\n", prompt);
221 printf("This is currently set to:\n%s\n", str);
222 printf("Enter new value or press return to leave unchanged:\n");
223 if (fgets(buf, sizeof buf, stdin)) {
224 buf[strlen(buf) - 1] = 0;
226 if (strlen(buf) != 0)
230 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
231 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%s' 2>%s",
232 getenv("CTDL_DIALOG"),
237 fp = fopen(dialog_result, "r");
239 if (fgets(str, sizeof buf, fp)){
240 if (str[strlen(str)-1] == 10) {
241 str[strlen(str)-1] = 0;
245 unlink(dialog_result);
253 extern char **AvailLang;
254 int GetLocalePrefs(void)
259 char dialog_result[PATH_MAX];
266 while (!IsEmptyStr(AvailLang[nLocales]))
271 StrBufAppendBufPlain(Buf, HKEY("Select the locale webcit should use : \n"), 0);
272 #ifdef HAVE_USELOCALE
273 StrBufAppendBufPlain(Buf, HKEY(" 0 Let the user select it at the login prompt (default)\n"), 0);
276 for (i = 0; i < nLocales; i++) {
277 StrBufAppendPrintf(Buf, " %ld: %s\n", i + offs, AvailLang[i]);
281 switch (setup_type) {
283 title("WebCit setup");
284 printf("\n%s\n", ChrPtr(Buf));
285 printf("This is currently set to:\n%ld\n", 0L);
286 printf("Enter new value or press return to leave unchanged:\n");
287 if (fgets(buf, sizeof buf, stdin))
292 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
293 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%ld' 2>%s",
294 getenv("CTDL_DIALOG"),
299 fp = fopen(dialog_result, "r");
302 if (fgets(str, sizeof buf, fp)){
303 if (str[strlen(str)-1] == 10) {
304 str[strlen(str)-1] = 0;
308 unlink(dialog_result);
317 void important_message(char *title, char *msgtext)
321 switch (setup_type) {
324 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");
325 printf(" %s \n\n%s\n\n", title, msgtext);
326 printf("Press return to continue...");
327 if (fgets(buf, sizeof buf, stdin));
331 sprintf(buf, "exec %s --msgbox '%s' 19 72",
332 getenv("CTDL_DIALOG"),
340 void display_error(char *error_message)
342 important_message("Error", error_message);
345 void progress(char *text, long int curr, long int cmax)
347 static long dots_printed = 0L;
350 static FILE *fp = NULL;
352 switch (setup_type) {
356 printf("%s\n", text);
357 printf("..........................");
358 printf("..........................");
359 printf("..........................\r");
362 } else if (curr == cmax) {
363 printf("\r%79s\n", "");
365 a = (curr * 100) / cmax;
368 while (dots_printed < a) {
378 sprintf(buf, "exec %s --gauge '%s' 7 72 0",
379 getenv("CTDL_DIALOG"),
381 fp = popen(buf, "w");
387 else if (curr == cmax) {
389 fprintf(fp, "100\n");
395 a = (curr * 100) / cmax;
397 fprintf(fp, "%ld\n", a);
409 * install_init_scripts() -- Create and deploy SysV init scripts.
412 void install_init_scripts(void)
421 char https_port[128];
426 struct utsname my_utsname;
427 struct stat etcinitd;
429 char *initfile = "/etc/init.d/webcit";
431 fp = fopen(initfile, "r");
433 if (yesno("WebCit already appears to be configured to start at boot.\n"
434 "Would you like to keep your boot configuration as is?\n", 1) == 1) {
441 /* Otherwise, prompt the user to create an entry. */
442 snprintf(question, sizeof question,
443 "Would you like to automatically start WebCit at boot?"
445 if (yesno(question, 1) == 0)
451 localechoice = GetLocalePrefs();
455 sprintf(http_port, "2000");
457 sprintf(https_port, "443");
459 sprintf(hostname, "uds");
460 sprintf(portname, "/usr/local/citadel");
462 /* This is a very hackish way of learning the port numbers used
463 * in a previous install, if we are upgrading: read them out of
464 * the existing init script.
466 if ((stat("/etc/init.d/", &etcinitd) == -1) &&
469 if ((stat("/etc/rc.d/init.d/", &etcinitd) == -1) &&
471 initfile = WEBCITDIR"/webcit.init";
473 initfile = "/etc/rc.d/init.d/webcit";
476 fp = fopen(initfile, "r");
478 while (fgets(buf, sizeof buf, fp) != NULL) {
479 if (strlen(buf) > 0) {
480 buf[strlen(buf)-1] = 0; /* strip trailing cr */
482 if (!strncasecmp(buf, "HTTP_PORT=", 10)) {
483 safestrncpy(http_port, &buf[10], sizeof http_port);
486 if (!strncasecmp(buf, "HTTPS_PORT=", 11)) {
487 safestrncpy(https_port, &buf[11], sizeof https_port);
490 if (!strncasecmp(buf, "CTDL_HOSTNAME=", 14)) {
491 safestrncpy(hostname, &buf[14], sizeof hostname);
493 if (!strncasecmp(buf, "CTDL_PORTNAME=", 14)) {
494 safestrncpy(portname, &buf[14], sizeof portname);
500 /* Now ask for the port numbers */
501 snprintf(question, sizeof question,
502 "On which port do you want WebCit to listen for HTTP "
503 "requests?\n\nYou can use the standard port (80) if you are "
504 "not running another\nweb server (such as Apache), otherwise "
505 "select another port.");
506 set_value(question, http_port);
508 sprintf(suggested_url, "http://%s:%s/", my_utsname.nodename, http_port);
511 snprintf(question, sizeof question,
512 "On which port do you want WebCit to listen for HTTPS "
513 "requests?\n\nYou can use the standard port (443) if you are "
514 "not running another\nweb server (such as Apache), otherwise "
515 "select another port.");
516 set_value(question, https_port);
519 /* Find out where Citadel is. */
520 if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
521 strcpy(hostname, "uds");
522 strcpy(portname, getenv("CITADEL"));
525 snprintf(question, sizeof question,
526 "Is the Citadel service running on the same host as WebCit?");
527 if (yesno(question, ((!strcasecmp(hostname, "uds")) ? 1 : 0))) {
528 strcpy(hostname, "uds");
529 if (atoi(portname) != 0) strcpy(portname, "/usr/local/citadel");
530 set_value("In what directory is Citadel installed?", portname);
533 if (!strcasecmp(hostname, "uds")) strcpy(hostname, "127.0.0.1");
534 if (atoi(portname) == 0) strcpy(portname, "504");
535 set_value("Enter the host name or IP address of your "
536 "Citadel server.", hostname);
537 set_value("Enter the port number on which Citadel is "
538 "running (usually 504)", portname);
543 fp = fopen(initfile, "w");
545 fprintf(fp, "#!/bin/sh\n"
547 "# uncomment this to create coredumps as described in\n"
548 "# http://www.citadel.org/doku.php/faq:mastering_your_os:gdb#how.do.i.make.my.system.produce.core-files\n"
549 "# ulimit -c unlimited\n"
550 "WEBCIT_DIR=%s\n", setup_directory);
551 fprintf(fp, "HTTP_PORT=%s\n", http_port);
553 fprintf(fp, "HTTPS_PORT=%s\n", https_port);
555 fprintf(fp, "CTDL_HOSTNAME=%s\n", hostname);
556 fprintf(fp, "CTDL_PORTNAME=%s\n", portname);
560 if (localechoice == 0) {
561 #ifdef HAVE_USELOCALE
562 fprintf(fp, "unset LANG\n");
564 fprintf(fp, "export WEBCIT_LANG=c\n");
568 fprintf(fp, "export WEBCIT_LANG=%s\n", AvailLang[localechoice - localeoffset]);
572 fprintf(fp, "# your system doesn't support locales\n");
578 "start) echo -n \"Starting WebCit... \"\n"
579 " if $WEBCIT_DIR/webcit "
580 "-D/var/run/webcit.pid "
581 "-p$HTTP_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
588 fprintf(fp, " echo -n \"Starting WebCit SSL... \"\n"
589 " if $WEBCIT_DIR/webcit "
590 "-D/var/run/webcit-ssl.pid "
591 "-s -p$HTTPS_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
599 "stop) echo -n \"Stopping WebCit... \"\n"
600 " if kill `cat /var/run/webcit.pid 2>/dev/null` 2>/dev/null\n"
606 " rm -f /var/run/webcit.pid 2>/dev/null\n");
608 fprintf(fp, " echo -n \"Stopping WebCit SSL... \"\n"
609 " if kill `cat /var/run/webcit-ssl.pid 2>/dev/null` 2>/dev/null\n"
615 " rm -f /var/run/webcit-ssl.pid 2>/dev/null\n");
621 "*) echo \"Usage: $0 {start|stop|restart}\"\n"
628 chmod(initfile, 0755);
630 /* Set up the run levels. */
631 system("/bin/rm -f /etc/rc?.d/[SK]??webcit 2>/dev/null");
632 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);
634 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);
643 * Figure out what type of user interface we're going to use
645 int discover_ui(void)
648 /* Use "dialog" if we have it */
649 if (getenv("CTDL_DIALOG") != NULL) {
660 int main(int argc, char *argv[])
665 strcpy(suggested_url, "http://<your_host_name>:<port>/");
667 /* set an invalid setup type */
670 /* Check to see if we're running the web installer */
671 if (getenv("CITADEL_INSTALLER") != NULL) {
672 using_web_installer = 1;
675 /* parse command line args */
676 for (a = 0; a < argc; ++a) {
677 if (!strncmp(argv[a], "-u", 2)) {
678 strcpy(aaa, argv[a]);
679 strcpy(aaa, &aaa[2]);
680 setup_type = atoi(aaa);
682 if (!strcmp(argv[a], "-i")) {
685 if (!strcmp(argv[a], "-q")) {
686 setup_type = UI_SILENT;
691 /* If a setup type was not specified, try to determine automatically
692 * the best one to use out of all available types.
694 if (setup_type < 0) {
695 setup_type = discover_ui();
697 if (info_only == 1) {
698 important_message("WebCit Setup", "Welcome to WebCit setup");
702 /* Get started in a valid setup directory. */
703 strcpy(setup_directory, WEBCITDIR);
704 if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
705 strcpy(setup_directory, getenv("WEBCIT"));
708 set_value("In what directory is WebCit installed?",
711 if (chdir(setup_directory) != 0) {
712 important_message("WebCit Setup",
713 "The directory you specified does not exist.");
718 * We used to start WebCit by putting it directly into /etc/inittab.
719 * Since some systems are moving away from init, we can't do this anymore.
721 progress("Removing obsolete /etc/inittab entries...", 0, 1);
722 delete_the_old_way();
723 progress("Removing obsolete /etc/inittab entries...", 1, 1);
726 switch (setup_type) {
730 " *** WebCit setup program ***\n\n");
736 * If we're running on SysV, install init scripts.
738 if (!access("/var/run", W_OK)) {
739 install_init_scripts();
741 if (!access("/etc/init.d/webcit", X_OK)) {
742 system("/etc/init.d/webcit stop");
743 system("/etc/init.d/webcit start");
747 "Setup is finished. You may now log in.\n"
748 "Point your web browser at %s\n", suggested_url
750 important_message("Setup finished", aaa);
754 important_message("Setup finished",
755 "Setup is finished. You may now start the server.");