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 fgets(buf
, sizeof buf
, stdin
);
176 answer
= tolower(buf
[0]);
177 if ((buf
[0]==0) || (buf
[0]==13) || (buf
[0]==10))
178 answer
= default_value
;
179 else if (answer
== 'y')
181 else if (answer
== 'n')
183 } while ((answer
< 0) || (answer
> 1));
187 sprintf(buf
, "exec %s %s --yesno '%s' 15 75",
188 getenv("CTDL_DIALOG"),
189 ( default_value
? "" : "--defaultno" ),
207 void set_value(char *prompt
, char str
[])
210 char dialog_result
[PATH_MAX
];
214 strcpy(setupmsg
, "");
216 switch (setup_type
) {
218 title("WebCit setup");
219 printf("\n%s\n", prompt
);
220 printf("This is currently set to:\n%s\n", str
);
221 printf("Enter new value or press return to leave unchanged:\n");
222 fgets(buf
, sizeof buf
, stdin
);
223 buf
[strlen(buf
) - 1] = 0;
224 if (strlen(buf
) != 0)
229 CtdlMakeTempFileName(dialog_result
, sizeof dialog_result
);
230 sprintf(buf
, "exec %s --inputbox '%s' 19 72 '%s' 2>%s",
231 getenv("CTDL_DIALOG"),
236 fp
= fopen(dialog_result
, "r");
238 fgets(str
, sizeof buf
, fp
);
239 if (str
[strlen(str
)-1] == 10) {
240 str
[strlen(str
)-1] = 0;
243 unlink(dialog_result
);
252 int GetLocalePrefs(void)
256 char dialog_result
[PATH_MAX
];
264 StrBufAppendBufPlain(Buf
, HKEY("Select the locale webcit should use : \n"), 0);
265 #ifdef HAVE_USELOCALE
266 StrBufAppendBufPlain(Buf
, HKEY(" 0 Let the user select it at the login prompt (default)\n"), 0);
269 for (i
= 0; i
< NUM_LANGS
; i
++) {
270 StrBufAppendPrintf(Buf
, " %ld: %s\n", i
+ offs
, AvailLang
[i
]);
274 switch (setup_type
) {
276 title("WebCit setup");
277 printf("\n%s\n", ChrPtr(Buf
));
278 printf("This is currently set to:\n%ld\n", 0L);
279 printf("Enter new value or press return to leave unchanged:\n");
280 fgets(buf
, sizeof buf
, stdin
);
285 CtdlMakeTempFileName(dialog_result
, sizeof dialog_result
);
286 sprintf(buf
, "exec %s --inputbox '%s' 19 72 '%ld' 2>%s",
287 getenv("CTDL_DIALOG"),
292 fp
= fopen(dialog_result
, "r");
295 fgets(str
, sizeof buf
, fp
);
296 if (str
[strlen(str
)-1] == 10) {
297 str
[strlen(str
)-1] = 0;
300 unlink(dialog_result
);
309 void important_message(char *title
, char *msgtext
)
313 switch (setup_type
) {
316 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");
317 printf(" %s \n\n%s\n\n", title
, msgtext
);
318 printf("Press return to continue...");
319 fgets(buf
, sizeof buf
, stdin
);
323 sprintf(buf
, "exec %s --msgbox '%s' 19 72",
324 getenv("CTDL_DIALOG"),
332 void display_error(char *error_message
)
334 important_message("Error", error_message
);
337 void progress(char *text
, long int curr
, long int cmax
)
339 static long dots_printed
= 0L;
342 static FILE *fp
= NULL
;
344 switch (setup_type
) {
348 printf("%s\n", text
);
349 printf("..........................");
350 printf("..........................");
351 printf("..........................\r");
354 } else if (curr
== cmax
) {
355 printf("\r%79s\n", "");
357 a
= (curr
* 100) / cmax
;
360 while (dots_printed
< a
) {
370 sprintf(buf
, "exec %s --gauge '%s' 7 72 0",
371 getenv("CTDL_DIALOG"),
373 fp
= popen(buf
, "w");
379 else if (curr
== cmax
) {
381 fprintf(fp
, "100\n");
387 a
= (curr
* 100) / cmax
;
389 fprintf(fp
, "%ld\n", a
);
401 * install_init_scripts() -- Create and deploy SysV init scripts.
404 void install_init_scripts(void)
413 char https_port
[128];
418 struct utsname my_utsname
;
419 struct stat etcinitd
;
421 char *initfile
= "/etc/init.d/webcit";
423 fp
= fopen(initfile
, "r");
425 if (yesno("WebCit already appears to be configured to start at boot.\n"
426 "Would you like to keep your boot configuration as is?\n", 1) == 1) {
433 /* Otherwise, prompt the user to create an entry. */
434 snprintf(question
, sizeof question
,
435 "Would you like to automatically start WebCit at boot?"
437 if (yesno(question
, 1) == 0)
443 localechoice
= GetLocalePrefs();
447 sprintf(http_port
, "2000");
449 sprintf(https_port
, "443");
451 sprintf(hostname
, "uds");
452 sprintf(portname
, "/usr/local/citadel");
454 /* This is a very hackish way of learning the port numbers used
455 * in a previous install, if we are upgrading: read them out of
456 * the existing init script.
458 if ((stat("/etc/init.d/", &etcinitd
) == -1) &&
461 if ((stat("/etc/rc.d/init.d/", &etcinitd
) == -1) &&
463 initfile
= WEBCITDIR
"/webcit.init";
465 initfile
= "/etc/rc.d/init.d/webcit";
468 fp
= fopen(initfile
, "r");
470 while (fgets(buf
, sizeof buf
, fp
) != NULL
) {
471 if (strlen(buf
) > 0) {
472 buf
[strlen(buf
)-1] = 0; /* strip trailing cr */
474 if (!strncasecmp(buf
, "HTTP_PORT=", 10)) {
475 safestrncpy(http_port
, &buf
[10], sizeof http_port
);
478 if (!strncasecmp(buf
, "HTTPS_PORT=", 11)) {
479 safestrncpy(https_port
, &buf
[11], sizeof https_port
);
482 if (!strncasecmp(buf
, "CTDL_HOSTNAME=", 14)) {
483 safestrncpy(hostname
, &buf
[14], sizeof hostname
);
485 if (!strncasecmp(buf
, "CTDL_PORTNAME=", 14)) {
486 safestrncpy(portname
, &buf
[14], sizeof portname
);
492 /* Now ask for the port numbers */
493 snprintf(question
, sizeof question
,
494 "On which port do you want WebCit to listen for HTTP "
495 "requests?\n\nYou can use the standard port (80) if you are "
496 "not running another\nweb server (such as Apache), otherwise "
497 "select another port.");
498 set_value(question
, http_port
);
500 sprintf(suggested_url
, "http://%s:%s/", my_utsname
.nodename
, http_port
);
503 snprintf(question
, sizeof question
,
504 "On which port do you want WebCit to listen for HTTPS "
505 "requests?\n\nYou can use the standard port (443) if you are "
506 "not running another\nweb server (such as Apache), otherwise "
507 "select another port.");
508 set_value(question
, https_port
);
511 /* Find out where Citadel is. */
512 if ( (using_web_installer
) && (getenv("CITADEL") != NULL
) ) {
513 strcpy(hostname
, "uds");
514 strcpy(portname
, getenv("CITADEL"));
517 snprintf(question
, sizeof question
,
518 "Is the Citadel service running on the same host as WebCit?");
519 if (yesno(question
, ((!strcasecmp(hostname
, "uds")) ? 1 : 0))) {
520 strcpy(hostname
, "uds");
521 if (atoi(portname
) != 0) strcpy(portname
, "/usr/local/citadel");
522 set_value("In what directory is Citadel installed?", portname
);
525 if (!strcasecmp(hostname
, "uds")) strcpy(hostname
, "127.0.0.1");
526 if (atoi(portname
) == 0) strcpy(portname
, "504");
527 set_value("Enter the host name or IP address of your "
528 "Citadel server.", hostname
);
529 set_value("Enter the port number on which Citadel is "
530 "running (usually 504)", portname
);
535 fp
= fopen(initfile
, "w");
537 fprintf(fp
, "#!/bin/sh\n"
539 "WEBCIT_DIR=%s\n", setup_directory
);
540 fprintf(fp
, "HTTP_PORT=%s\n", http_port
);
542 fprintf(fp
, "HTTPS_PORT=%s\n", https_port
);
544 fprintf(fp
, "CTDL_HOSTNAME=%s\n", hostname
);
545 fprintf(fp
, "CTDL_PORTNAME=%s\n", portname
);
549 if (localechoice
== 0) {
550 #ifdef HAVE_USELOCALE
551 fprintf(fp
, "unset LANG\n");
553 fprintf(fp
, "export WEBCIT_LANG=c\n");
557 fprintf(fp
, "export WEBCIT_LANG=%s\n", AvailLang
[localechoice
- localeoffset
]);
561 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
[])
654 strcpy(suggested_url
, "http://<your_host_name>:<port>/");
656 /* set an invalid setup type */
659 /* Check to see if we're running the web installer */
660 if (getenv("CITADEL_INSTALLER") != NULL
) {
661 using_web_installer
= 1;
664 /* parse command line args */
665 for (a
= 0; a
< argc
; ++a
) {
666 if (!strncmp(argv
[a
], "-u", 2)) {
667 strcpy(aaa
, argv
[a
]);
668 strcpy(aaa
, &aaa
[2]);
669 setup_type
= atoi(aaa
);
671 if (!strcmp(argv
[a
], "-i")) {
674 if (!strcmp(argv
[a
], "-q")) {
675 setup_type
= UI_SILENT
;
680 /* If a setup type was not specified, try to determine automatically
681 * the best one to use out of all available types.
683 if (setup_type
< 0) {
684 setup_type
= discover_ui();
686 if (info_only
== 1) {
687 important_message("WebCit Setup", "Welcome to WebCit setup");
691 /* Get started in a valid setup directory. */
692 strcpy(setup_directory
, WEBCITDIR
);
693 if ( (using_web_installer
) && (getenv("WEBCIT") != NULL
) ) {
694 strcpy(setup_directory
, getenv("WEBCIT"));
697 set_value("In what directory is WebCit installed?",
700 if (chdir(setup_directory
) != 0) {
701 important_message("WebCit Setup",
702 "The directory you specified does not exist.");
707 * We used to start WebCit by putting it directly into /etc/inittab.
708 * Since some systems are moving away from init, we can't do this anymore.
710 progress("Removing obsolete /etc/inittab entries...", 0, 1);
711 delete_the_old_way();
712 progress("Removing obsolete /etc/inittab entries...", 1, 1);
715 switch (setup_type
) {
719 " *** WebCit setup program ***\n\n");
725 * If we're running on SysV, install init scripts.
727 if (!access("/var/run", W_OK
)) {
728 install_init_scripts();
730 if (!access("/etc/init.d/webcit", X_OK
)) {
731 system("/etc/init.d/webcit stop");
732 system("/etc/init.d/webcit start");
736 "Setup is finished. You may now log in.\n"
737 "Point your web browser at %s\n", suggested_url
739 important_message("Setup finished", aaa
);
743 important_message("Setup finished",
744 "Setup is finished. You may now start the server.");