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;
39 * Delete an entry from /etc/inittab
41 void delete_init_entry(char *which_entry)
52 if (inittab == NULL) return;
54 fp = fopen("/etc/inittab", "r");
55 if (fp == NULL) return;
57 while(fgets(buf, sizeof buf, fp) != NULL) {
59 if (num_tokens(buf, ':') == 4) {
60 extract_token(entry, buf, 0, ':', sizeof entry);
61 extract_token(levels, buf, 1, ':', sizeof levels);
62 extract_token(state, buf, 2, ':', sizeof state);
63 extract_token(prog, buf, 3, ':', sizeof prog); /* includes 0x0a LF */
65 if (!strcmp(entry, which_entry)) {
66 strcpy(state, "off"); /* disable it */
70 inittab = realloc(inittab, strlen(inittab) + strlen(buf) + 2);
71 if (inittab == NULL) {
79 fp = fopen("/etc/inittab", "w");
81 fwrite(inittab, strlen(inittab), 1, fp);
83 kill(1, SIGHUP); /* Tell init to re-read /etc/inittab */
92 * Remove any /etc/inittab entries for webcit, because we don't
93 * start it that way anymore.
95 void delete_the_old_way(void) {
98 char looking_for[1024];
102 char init_entry[1024];
105 strcpy(init_entry, "");
107 /* Determine the fully qualified path name of webcit */
108 snprintf(looking_for, sizeof looking_for, "%s/webcit ", setup_directory);
110 /* Pound through /etc/inittab line by line. Set have_entry to 1 if
111 * an entry is found which we believe starts webcit.
113 infp = fopen("/etc/inittab", "r");
117 while (fgets(buf, sizeof buf, infp) != NULL) {
118 buf[strlen(buf) - 1] = 0;
119 extract_token(entry, buf, 0, ':', sizeof entry);
120 extract_token(prog, buf, 3, ':', sizeof prog);
121 if (!strncasecmp(prog, looking_for,
122 strlen(looking_for))) {
124 strcpy(init_entry, entry);
130 /* Bail out if there's nothing to do. */
131 if (!have_entry) return;
133 delete_init_entry(init_entry);
138 void cleanup(int exitcode)
145 void title(char *text)
147 if (setup_type == UI_TEXT) {
148 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<%s>\n", text);
155 int yesno(char *question, int default_value)
161 switch (setup_type) {
165 printf("%s\nYes/No [%s] --> ",
167 ( default_value ? "Yes" : "No" )
169 if (fgets(buf, sizeof buf, stdin))
171 answer = tolower(buf[0]);
172 if ((buf[0]==0) || (buf[0]==13) || (buf[0]==10))
173 answer = default_value;
174 else if (answer == 'y')
176 else if (answer == 'n')
180 } while ((answer < 0) || (answer > 1));
184 sprintf(buf, "exec %s %s --yesno '%s' 15 75",
185 getenv("CTDL_DIALOG"),
186 ( default_value ? "" : "--defaultno" ),
204 void set_value(char *prompt, char str[])
207 char dialog_result[PATH_MAX];
211 strcpy(setupmsg, "");
213 switch (setup_type) {
215 title("WebCit setup");
216 printf("\n%s\n", prompt);
217 printf("This is currently set to:\n%s\n", str);
218 printf("Enter new value or press return to leave unchanged:\n");
219 if (fgets(buf, sizeof buf, stdin)) {
220 buf[strlen(buf) - 1] = 0;
222 if (strlen(buf) != 0)
226 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
227 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%s' 2>%s",
228 getenv("CTDL_DIALOG"),
233 fp = fopen(dialog_result, "r");
235 if (fgets(str, sizeof buf, fp)){
236 if (str[strlen(str)-1] == 10) {
237 str[strlen(str)-1] = 0;
241 unlink(dialog_result);
249 extern const char *AvailLang[];
250 int GetLocalePrefs(void)
255 char dialog_result[PATH_MAX];
261 while (!IsEmptyStr(AvailLang[nLocales]))
266 StrBufAppendBufPlain(Buf, HKEY("Select the locale webcit should use : \n"), 0);
267 #ifdef HAVE_USELOCALE
268 StrBufAppendBufPlain(Buf, HKEY(" 0 Let the user select it at the login prompt (default)\n"), 0);
271 for (i = 0; i < nLocales; i++) {
272 StrBufAppendPrintf(Buf, " %ld: %s\n", i + offs, AvailLang[i]);
276 switch (setup_type) {
278 title("WebCit setup");
279 printf("\n%s\n", ChrPtr(Buf));
280 printf("This is currently set to:\n%ld\n", 0L);
281 printf("Enter new value or press return to leave unchanged:\n");
282 if (fgets(buf, sizeof buf, stdin))
287 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
288 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%ld' 2>%s",
289 getenv("CTDL_DIALOG"),
294 fp = fopen(dialog_result, "r");
297 if (fgets(str, sizeof buf, fp)){
298 if (str[strlen(str)-1] == 10) {
299 str[strlen(str)-1] = 0;
303 unlink(dialog_result);
312 void important_message(char *title, char *msgtext)
316 switch (setup_type) {
319 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");
320 printf(" %s \n\n%s\n\n", title, msgtext);
321 printf("Press return to continue...");
322 if (fgets(buf, sizeof buf, stdin));
326 sprintf(buf, "exec %s --msgbox '%s' 19 72",
327 getenv("CTDL_DIALOG"),
335 void display_error(char *error_message)
337 important_message("Error", error_message);
340 void progress(char *text, long int curr, long int cmax)
342 static long dots_printed = 0L;
345 static FILE *fp = NULL;
347 switch (setup_type) {
351 printf("%s\n", text);
352 printf("..........................");
353 printf("..........................");
354 printf("..........................\r");
357 } else if (curr == cmax) {
358 printf("\r%79s\n", "");
360 a = (curr * 100) / cmax;
363 while (dots_printed < a) {
373 sprintf(buf, "exec %s --gauge '%s' 7 72 0",
374 getenv("CTDL_DIALOG"),
376 fp = popen(buf, "w");
382 else if (curr == cmax) {
384 fprintf(fp, "100\n");
390 a = (curr * 100) / cmax;
392 fprintf(fp, "%ld\n", a);
404 * install_init_scripts() -- Create and deploy SysV init scripts.
407 void install_init_scripts(void)
409 #ifdef HAVE_USELOCALE
416 char https_port[128];
421 struct utsname my_utsname;
422 struct stat etcinitd;
424 char *initfile = "/etc/init.d/webcit";
426 fp = fopen(initfile, "r");
428 if (yesno("WebCit already appears to be configured to start at boot.\n"
429 "Would you like to keep your boot configuration as is?\n", 1) == 1) {
436 /* Otherwise, prompt the user to create an entry. */
437 snprintf(question, sizeof question,
438 "Would you like to automatically start WebCit at boot?"
440 if (yesno(question, 1) == 0)
444 #ifdef HAVE_USELOCALE
445 localechoice = GetLocalePrefs();
448 sprintf(http_port, "2000");
450 sprintf(https_port, "443");
452 sprintf(hostname, "uds");
453 sprintf(portname, "/usr/local/citadel");
455 /* This is a very hackish way of learning the port numbers used
456 * in a previous install, if we are upgrading: read them out of
457 * the existing init script.
459 if ((stat("/etc/init.d/", &etcinitd) == -1) &&
462 if ((stat("/etc/rc.d/init.d/", &etcinitd) == -1) &&
464 initfile = WEBCITDIR"/webcit.init";
466 initfile = "/etc/rc.d/init.d/webcit";
469 fp = fopen(initfile, "r");
471 while (fgets(buf, sizeof buf, fp) != NULL) {
472 if (strlen(buf) > 0) {
473 buf[strlen(buf)-1] = 0; /* strip trailing cr */
475 if (!strncasecmp(buf, "HTTP_PORT=", 10)) {
476 safestrncpy(http_port, &buf[10], sizeof http_port);
479 if (!strncasecmp(buf, "HTTPS_PORT=", 11)) {
480 safestrncpy(https_port, &buf[11], sizeof https_port);
483 if (!strncasecmp(buf, "CTDL_HOSTNAME=", 14)) {
484 safestrncpy(hostname, &buf[14], sizeof hostname);
486 if (!strncasecmp(buf, "CTDL_PORTNAME=", 14)) {
487 safestrncpy(portname, &buf[14], sizeof portname);
493 /* Now ask for the port numbers */
494 snprintf(question, sizeof question,
495 "On which port do you want WebCit to listen for HTTP "
496 "requests?\n\nYou can use the standard port (80) if you are "
497 "not running another\nweb server (such as Apache), otherwise "
498 "select another port.");
499 set_value(question, http_port);
501 sprintf(suggested_url, "http://%s:%s/", my_utsname.nodename, http_port);
504 snprintf(question, sizeof question,
505 "On which port do you want WebCit to listen for HTTPS "
506 "requests?\n\nYou can use the standard port (443) if you are "
507 "not running another\nweb server (such as Apache), otherwise "
508 "select another port.");
509 set_value(question, https_port);
512 /* Find out where Citadel is. */
513 if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
514 strcpy(hostname, "uds");
515 strcpy(portname, getenv("CITADEL"));
518 snprintf(question, sizeof question,
519 "Is the Citadel service running on the same host as WebCit?");
520 if (yesno(question, ((!strcasecmp(hostname, "uds")) ? 1 : 0))) {
521 strcpy(hostname, "uds");
522 if (atoi(portname) != 0) strcpy(portname, "/usr/local/citadel");
523 set_value("In what directory is Citadel installed?", portname);
526 if (!strcasecmp(hostname, "uds")) strcpy(hostname, "127.0.0.1");
527 if (atoi(portname) == 0) strcpy(portname, "504");
528 set_value("Enter the host name or IP address of your "
529 "Citadel server.", hostname);
530 set_value("Enter the port number on which Citadel is "
531 "running (usually 504)", portname);
536 fp = fopen(initfile, "w");
538 fprintf(fp, "#!/bin/sh\n"
540 "# uncomment this to create coredumps as described in\n"
541 "# http://www.citadel.org/doku.php/faq:mastering_your_os:gdb#how.do.i.make.my.system.produce.core-files\n"
542 "# ulimit -c unlimited\n"
543 "WEBCIT_DIR=%s\n", setup_directory);
544 fprintf(fp, "HTTP_PORT=%s\n", http_port);
546 fprintf(fp, "HTTPS_PORT=%s\n", https_port);
548 fprintf(fp, "CTDL_HOSTNAME=%s\n", hostname);
549 fprintf(fp, "CTDL_PORTNAME=%s\n", portname);
551 #ifdef HAVE_USELOCALE
552 if (localechoice == 0) {
553 fprintf(fp, "export WEBCIT_LANG=%s\n", AvailLang[localechoice - localeoffset]);
556 fprintf(fp, "# your system doesn't support locales\n");
567 "start) echo -n \"Starting WebCit... \"\n"
568 " if $WEBCIT_DIR/webcit "
569 "-D/var/run/webcit.pid "
570 "-p$HTTP_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
577 fprintf(fp, " echo -n \"Starting WebCit SSL... \"\n"
578 " if $WEBCIT_DIR/webcit "
579 "-D/var/run/webcit-ssl.pid "
580 "-s -p$HTTPS_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
588 "stop) echo -n \"Stopping WebCit... \"\n"
589 " if kill `cat /var/run/webcit.pid 2>/dev/null` 2>/dev/null\n"
595 " rm -f /var/run/webcit.pid 2>/dev/null\n");
597 fprintf(fp, " echo -n \"Stopping WebCit SSL... \"\n"
598 " if kill `cat /var/run/webcit-ssl.pid 2>/dev/null` 2>/dev/null\n"
604 " rm -f /var/run/webcit-ssl.pid 2>/dev/null\n");
610 "*) echo \"Usage: $0 {start|stop|restart}\"\n"
617 chmod(initfile, 0755);
619 /* Set up the run levels. */
620 system("/bin/rm -f /etc/rc?.d/[SK]??webcit 2>/dev/null");
621 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);
623 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);
632 * Figure out what type of user interface we're going to use
634 int discover_ui(void)
637 /* Use "dialog" if we have it */
638 if (getenv("CTDL_DIALOG") != NULL) {
649 int main(int argc, char *argv[])
655 strcpy(suggested_url, "http://<your_host_name>:<port>/");
657 /* set an invalid setup type */
660 /* Check to see if we're running the web installer */
661 if (getenv("CITADEL_INSTALLER") != NULL) {
662 using_web_installer = 1;
665 /* parse command line args */
666 for (a = 0; a < argc; ++a) {
667 if (!strncmp(argv[a], "-u", 2)) {
668 strcpy(aaa, argv[a]);
669 strcpy(aaa, &aaa[2]);
670 setup_type = atoi(aaa);
672 if (!strcmp(argv[a], "-i")) {
675 if (!strcmp(argv[a], "-q")) {
676 setup_type = UI_SILENT;
681 /* If a setup type was not specified, try to determine automatically
682 * the best one to use out of all available types.
684 if (setup_type < 0) {
685 setup_type = discover_ui();
687 if (info_only == 1) {
688 important_message("WebCit Setup", "Welcome to WebCit setup");
692 /* Get started in a valid setup directory. */
693 strcpy(setup_directory, WEBCITDIR);
694 if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
695 strcpy(setup_directory, getenv("WEBCIT"));
698 set_value("In what directory is WebCit installed?",
701 if (chdir(setup_directory) != 0) {
702 important_message("WebCit Setup",
703 "The directory you specified does not exist.");
708 * We used to start WebCit by putting it directly into /etc/inittab.
709 * Since some systems are moving away from init, we can't do this anymore.
711 progress("Removing obsolete /etc/inittab entries...", 0, 1);
712 delete_the_old_way();
713 progress("Removing obsolete /etc/inittab entries...", 1, 1);
716 switch (setup_type) {
720 " *** WebCit setup program ***\n\n");
726 * If we're running on SysV, install init scripts.
728 if (!access("/var/run", W_OK)) {
729 install_init_scripts();
731 if (!access("/etc/init.d/webcit", X_OK)) {
732 system("/etc/init.d/webcit stop");
733 system("/etc/init.d/webcit start");
737 "Setup is finished. You may now log in.\n"
738 "Point your web browser at %s\n", suggested_url
740 important_message("Setup finished", aaa);
744 important_message("Setup finished",
745 "Setup is finished. You may now start the server.");