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 * Delete an entry from /etc/inittab
27 void delete_init_entry(char *which_entry)
38 if (inittab == NULL) return;
40 fp = fopen("/etc/inittab", "r");
41 if (fp == NULL) return;
43 while(fgets(buf, sizeof buf, fp) != NULL) {
45 if (num_tokens(buf, ':') == 4) {
46 extract_token(entry, buf, 0, ':', sizeof entry);
47 extract_token(levels, buf, 1, ':', sizeof levels);
48 extract_token(state, buf, 2, ':', sizeof state);
49 extract_token(prog, buf, 3, ':', sizeof prog); /* includes 0x0a LF */
51 if (!strcmp(entry, which_entry)) {
52 strcpy(state, "off"); /* disable it */
56 inittab = realloc(inittab, strlen(inittab) + strlen(buf) + 2);
57 if (inittab == NULL) {
65 fp = fopen("/etc/inittab", "w");
67 fwrite(inittab, strlen(inittab), 1, fp);
69 kill(1, SIGHUP); /* Tell init to re-read /etc/inittab */
78 * Remove any /etc/inittab entries for webcit, because we don't
79 * start it that way anymore.
81 void delete_the_old_way(void) {
84 char looking_for[1024];
88 char init_entry[1024];
91 strcpy(init_entry, "");
93 /* Determine the fully qualified path name of webserver */
94 snprintf(looking_for, sizeof looking_for, "%s/webserver ", setup_directory);
96 /* Pound through /etc/inittab line by line. Set have_entry to 1 if
97 * an entry is found which we believe starts webserver.
99 infp = fopen("/etc/inittab", "r");
103 while (fgets(buf, sizeof buf, infp) != NULL) {
104 buf[strlen(buf) - 1] = 0;
105 extract_token(entry, buf, 0, ':', sizeof entry);
106 extract_token(prog, buf, 3, ':', sizeof prog);
107 if (!strncasecmp(prog, looking_for,
108 strlen(looking_for))) {
110 strcpy(init_entry, entry);
116 /* Bail out if there's nothing to do. */
117 if (!have_entry) return;
119 delete_init_entry(init_entry);
124 void cleanup(int exitcode)
131 void title(char *text)
133 if (setup_type == UI_TEXT) {
134 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<%s>\n", text);
140 int yesno(char *question)
146 switch (setup_type) {
150 printf("%s\nYes/No --> ", question);
151 fgets(buf, sizeof buf, stdin);
152 answer = tolower(buf[0]);
155 else if (answer == 'n')
157 } while ((answer < 0) || (answer > 1));
161 sprintf(buf, "exec %s --yesno '%s' 10 72",
162 getenv("CTDL_DIALOG"),
177 void set_value(char *prompt, char str[])
180 char dialog_result[PATH_MAX];
184 strcpy(setupmsg, "");
186 switch (setup_type) {
188 title("WebCit setup");
189 printf("\n%s\n", prompt);
190 printf("This is currently set to:\n%s\n", str);
191 printf("Enter new value or press return to leave unchanged:\n");
192 fgets(buf, sizeof buf, stdin);
193 buf[strlen(buf) - 1] = 0;
194 if (strlen(buf) != 0)
199 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
200 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%s' 2>%s",
201 getenv("CTDL_DIALOG"),
206 fp = fopen(dialog_result, "r");
208 fgets(str, sizeof buf, fp);
209 if (str[strlen(str)-1] == 10) {
210 str[strlen(str)-1] = 0;
213 unlink(dialog_result);
221 void important_message(char *title, char *msgtext)
225 switch (setup_type) {
228 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");
229 printf(" %s \n\n%s\n\n", title, msgtext);
230 printf("Press return to continue...");
231 fgets(buf, sizeof buf, stdin);
235 sprintf(buf, "exec %s --msgbox '%s' 19 72",
236 getenv("CTDL_DIALOG"),
244 void display_error(char *error_message)
246 important_message("Error", error_message);
249 void progress(char *text, long int curr, long int cmax)
251 static long dots_printed = 0L;
254 static FILE *fp = NULL;
256 switch (setup_type) {
260 printf("%s\n", text);
261 printf("..........................");
262 printf("..........................");
263 printf("..........................\r");
266 } else if (curr == cmax) {
267 printf("\r%79s\n", "");
269 a = (curr * 100) / cmax;
272 while (dots_printed < a) {
282 sprintf(buf, "exec %s --gauge '%s' 7 72 0",
283 getenv("CTDL_DIALOG"),
285 fp = popen(buf, "w");
291 else if (curr == cmax) {
293 fprintf(fp, "100\n");
299 a = (curr * 100) / cmax;
301 fprintf(fp, "%ld\n", a);
313 * install_init_scripts() -- Create and deploy SysV init scripts.
316 void install_init_scripts(void)
322 char https_port[128];
326 struct utsname my_utsname;
330 /* Otherwise, prompt the user to create an entry. */
331 snprintf(question, sizeof question,
332 "Would you like to automatically start WebCit at boot?"
334 if (yesno(question) == 0)
337 /* Default port numbers */
338 sprintf(http_port, "2000");
340 sprintf(https_port, "443");
343 /* This is a very hackish way of learning the port numbers used
344 * in a previous install, if we are upgrading: read them out of
345 * the existing init script.
347 fp = fopen("/etc/init.d/webcit", "r");
349 while (fgets(buf, sizeof buf, fp) != NULL) {
350 if (strlen(buf) > 0) {
351 buf[strlen(buf)-1] = 0; /* strip trailing cr */
353 if (!strncasecmp(buf, "HTTP_PORT=", 10)) {
354 safestrncpy(http_port, &buf[10], sizeof http_port);
357 if (!strncasecmp(buf, "HTTPS_PORT=", 11)) {
358 safestrncpy(https_port, &buf[11], sizeof https_port);
365 /* Now ask for the port numbers */
366 snprintf(question, sizeof question,
367 "On which port do you want WebCit to listen for HTTP "
368 "requests?\n\nYou can use the standard port (80) if you are "
369 "not running another\nweb server (such as Apache), otherwise "
370 "select another port.");
371 set_value(question, http_port);
373 sprintf(suggested_url, "http://%s:%s/", my_utsname.nodename, http_port);
376 snprintf(question, sizeof question,
377 "On which port do you want WebCit to listen for HTTPS "
378 "requests?\n\nYou can use the standard port (443) if you are "
379 "not running another\nweb server (such as Apache), otherwise "
380 "select another port.");
381 set_value(question, https_port);
384 /* Find out where Citadel is. */
385 if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
386 strcpy(hostname, "uds");
387 strcpy(portname, getenv("CITADEL"));
390 snprintf(question, sizeof question,
391 "Is the Citadel service running on the same host as WebCit?");
392 if (yesno(question)) {
393 sprintf(hostname, "uds");
394 sprintf(portname, "/usr/local/citadel");
395 set_value("In what directory is Citadel installed?", portname);
398 sprintf(hostname, "127.0.0.1");
399 sprintf(portname, "504");
400 set_value("Enter the host name or IP address of your "
401 "Citadel server.", hostname);
402 set_value("Enter the port number on which Citadel is "
403 "running (usually 504)", portname);
408 fp = fopen("/etc/init.d/webcit", "w");
410 display_error("Cannot create /etc/init.d/webcit");
414 fprintf(fp, "#!/bin/sh\n"
416 "WEBCIT_DIR=%s\n", setup_directory);
417 fprintf(fp, "HTTP_PORT=%s\n", http_port);
419 fprintf(fp, "HTTPS_PORT=%s\n", https_port);
421 fprintf(fp, "CTDL_HOSTNAME=%s\n", hostname);
422 fprintf(fp, "CTDL_PORTNAME=%s\n", portname);
427 "start) echo -n \"Starting WebCit... \"\n"
428 " if $WEBCIT_DIR/webserver "
429 "-D/var/run/webcit.pid "
430 "-p$HTTP_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
437 fprintf(fp, " echo -n \"Starting WebCit SSL... \"\n"
438 " if $WEBCIT_DIR/webserver "
439 "-D/var/run/webcit-ssl.pid "
440 "-s -p$HTTPS_PORT $CTDL_HOSTNAME $CTDL_PORTNAME\n"
448 "stop) echo -n \"Stopping WebCit... \"\n"
449 " if kill `cat /var/run/webcit.pid 2>/dev/null` 2>/dev/null\n"
455 " rm -f /var/run/webcit.pid 2>/dev/null\n");
457 fprintf(fp, " echo -n \"Stopping WebCit SSL... \"\n"
458 " if kill `cat /var/run/webcit-ssl.pid 2>/dev/null` 2>/dev/null\n"
464 " rm -f /var/run/webcit-ssl.pid 2>/dev/null\n");
470 "*) echo \"Usage: $0 {start|stop|restart}\"\n"
477 chmod("/etc/init.d/webcit", 0755);
479 /* Set up the run levels. */
480 system("/bin/rm -f /etc/rc?.d/[SK]??webcit 2>/dev/null");
481 system("for x in 2 3 4 5 ; do [ -d /etc/rc$x.d ] && ln -s /etc/init.d/webcit /etc/rc$x.d/S84webcit ; done 2>/dev/null");
482 system("for x in 0 6 S; do [ -d /etc/rc$x.d ] && ln -s /etc/init.d/webcit /etc/rc$x.d/K15webcit ; done 2>/dev/null");
490 * Figure out what type of user interface we're going to use
492 int discover_ui(void)
495 /* Use "dialog" if we have it */
496 if (getenv("CTDL_DIALOG") != NULL) {
507 int main(int argc, char *argv[])
512 strcpy(suggested_url, "http://<your_host_name>:<port>/");
514 /* set an invalid setup type */
517 /* Check to see if we're running the web installer */
518 if (getenv("CITADEL_INSTALLER") != NULL) {
519 using_web_installer = 1;
522 /* parse command line args */
523 for (a = 0; a < argc; ++a) {
524 if (!strncmp(argv[a], "-u", 2)) {
525 strcpy(aaa, argv[a]);
526 strcpy(aaa, &aaa[2]);
527 setup_type = atoi(aaa);
529 if (!strcmp(argv[a], "-i")) {
532 if (!strcmp(argv[a], "-q")) {
533 setup_type = UI_SILENT;
538 /* If a setup type was not specified, try to determine automatically
539 * the best one to use out of all available types.
541 if (setup_type < 0) {
542 setup_type = discover_ui();
544 if (info_only == 1) {
545 important_message("WebCit Setup", "Welcome to WebCit setup");
549 /* Get started in a valid setup directory. */
550 strcpy(setup_directory, PREFIX);
551 if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
552 strcpy(setup_directory, getenv("WEBCIT"));
555 set_value("In what directory is WebCit installed?",
558 if (chdir(setup_directory) != 0) {
559 important_message("WebCit Setup",
560 "The directory you specified does not exist.");
565 * We used to start WebCit by putting it directly into /etc/inittab.
566 * Since some systems are moving away from init, we can't do this anymore.
568 progress("Removing obsolete /etc/inittab entries...", 0, 1);
569 delete_the_old_way();
570 progress("Removing obsolete /etc/inittab entries...", 1, 1);
573 switch (setup_type) {
577 " *** WebCit setup program ***\n\n");
583 * If we're running on SysV, install init scripts.
585 if (!access("/var/run", W_OK)) {
586 install_init_scripts();
588 if (!access("/etc/init.d/webcit", X_OK)) {
589 system("/etc/init.d/webcit stop");
590 system("/etc/init.d/webcit start");
594 "Setup is finished. You may now log in.\n"
595 "Point your web browser at %s\n", suggested_url
597 important_message("Setup finished", aaa);
601 important_message("Setup finished",
602 "Setup is finished. You may now start the server.");