* Reduced iconbar button size to 90% because it looked obtuse on a smaller screen
[citadel.git] / webcit / setup.c
blobf781ab9bc02f25d60a9bf4256048bb3ba07e4407
1 /*
2 * $Id$
4 * WebCit setup utility
5 *
6 * (This is basically just an install wizard. It's not required.)
8 */
10 #include "sysdep.h"
11 #include "webcit.h"
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 */
19 int setup_type;
20 char setup_directory[SIZ];
21 int using_web_installer = 0;
22 char suggested_url[SIZ];
24 /* some copies... */
25 int lprintf(int loglevel, const char *format, ...){return 0;}
26 void RegisterNS(const char *NSName, long len,
27 int nMinArgs,
28 int nMaxArgs,
29 WCHandlerFunc HandlerFunc,
30 int ContextRequired){}
31 pthread_key_t MyConKey;
33 #include "wc_gettext.h"
35 #ifdef ENABLE_NLS
37 #ifdef HAVE_USELOCALE
38 int localeoffset = 1;
39 #else
40 int localeoffset = 0;
41 #endif
43 #endif
45 * Delete an entry from /etc/inittab
47 void delete_init_entry(char *which_entry)
49 char *inittab = NULL;
50 FILE *fp;
51 char buf[SIZ];
52 char entry[SIZ];
53 char levels[SIZ];
54 char state[SIZ];
55 char prog[SIZ];
57 inittab = strdup("");
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) {
78 fclose(fp);
79 return;
82 strcat(inittab, buf);
84 fclose(fp);
85 fp = fopen("/etc/inittab", "w");
86 if (fp != NULL) {
87 fwrite(inittab, strlen(inittab), 1, fp);
88 fclose(fp);
89 kill(1, SIGHUP); /* Tell init to re-read /etc/inittab */
91 free(inittab);
97 /*
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) {
102 FILE *infp;
103 char buf[1024];
104 char looking_for[1024];
105 int have_entry = 0;
106 char entry[1024];
107 char prog[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");
120 if (infp == NULL) {
121 return;
122 } else {
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))) {
129 ++have_entry;
130 strcpy(init_entry, entry);
133 fclose(infp);
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)
146 exit(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)
163 int i = 0;
164 int answer = 0;
165 char buf[SIZ];
167 switch (setup_type) {
169 case UI_TEXT:
170 do {
171 printf("%s\nYes/No [%s] --> ",
172 question,
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')
180 answer = 1;
181 else if (answer == 'n')
182 answer = 0;
183 } while ((answer < 0) || (answer > 1));
184 break;
186 case UI_DIALOG:
187 sprintf(buf, "exec %s %s --yesno '%s' 15 75",
188 getenv("CTDL_DIALOG"),
189 ( default_value ? "" : "--defaultno" ),
190 question);
191 i = system(buf);
192 if (i == 0) {
193 answer = 1;
195 else {
196 answer = 0;
198 break;
201 return (answer);
207 void set_value(char *prompt, char str[])
209 char buf[SIZ];
210 char dialog_result[PATH_MAX];
211 char setupmsg[SIZ];
212 FILE *fp;
214 strcpy(setupmsg, "");
216 switch (setup_type) {
217 case UI_TEXT:
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)
225 strcpy(str, buf);
226 break;
228 case UI_DIALOG:
229 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
230 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%s' 2>%s",
231 getenv("CTDL_DIALOG"),
232 prompt,
233 str,
234 dialog_result);
235 system(buf);
236 fp = fopen(dialog_result, "r");
237 if (fp != NULL) {
238 fgets(str, sizeof buf, fp);
239 if (str[strlen(str)-1] == 10) {
240 str[strlen(str)-1] = 0;
242 fclose(fp);
243 unlink(dialog_result);
245 break;
252 int GetLocalePrefs(void)
254 StrBuf *Buf;
255 char buf[SIZ];
256 char dialog_result[PATH_MAX];
257 FILE *fp;
258 int i = 0;
259 int offs = 0;
262 Buf = NewStrBuf();
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);
267 offs ++;
268 #endif
269 for (i = 0; i < NUM_LANGS; i++) {
270 StrBufAppendPrintf(Buf, " %ld: %s\n", i + offs, AvailLang[i]);
274 switch (setup_type) {
275 case UI_TEXT:
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);
281 return atoi(buf);
282 break;
284 case UI_DIALOG:
285 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
286 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%ld' 2>%s",
287 getenv("CTDL_DIALOG"),
288 ChrPtr(Buf),
290 dialog_result);
291 system(buf);
292 fp = fopen(dialog_result, "r");
293 if (fp != NULL) {
294 char *str = &buf[0];
295 fgets(str, sizeof buf, fp);
296 if (str[strlen(str)-1] == 10) {
297 str[strlen(str)-1] = 0;
299 fclose(fp);
300 unlink(dialog_result);
301 return atoi(buf);
303 break;
306 return 0;
309 void important_message(char *title, char *msgtext)
311 char buf[SIZ];
313 switch (setup_type) {
315 case UI_TEXT:
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);
320 break;
322 case UI_DIALOG:
323 sprintf(buf, "exec %s --msgbox '%s' 19 72",
324 getenv("CTDL_DIALOG"),
325 msgtext);
326 system(buf);
327 break;
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;
340 long a = 0;
341 char buf[SIZ];
342 static FILE *fp = NULL;
344 switch (setup_type) {
346 case UI_TEXT:
347 if (curr == 0) {
348 printf("%s\n", text);
349 printf("..........................");
350 printf("..........................");
351 printf("..........................\r");
352 fflush(stdout);
353 dots_printed = 0;
354 } else if (curr == cmax) {
355 printf("\r%79s\n", "");
356 } else {
357 a = (curr * 100) / cmax;
358 a = a * 78;
359 a = a / 100;
360 while (dots_printed < a) {
361 printf("*");
362 ++dots_printed;
363 fflush(stdout);
366 break;
368 case UI_DIALOG:
369 if (curr == 0) {
370 sprintf(buf, "exec %s --gauge '%s' 7 72 0",
371 getenv("CTDL_DIALOG"),
372 text);
373 fp = popen(buf, "w");
374 if (fp != NULL) {
375 fprintf(fp, "0\n");
376 fflush(fp);
379 else if (curr == cmax) {
380 if (fp != NULL) {
381 fprintf(fp, "100\n");
382 pclose(fp);
383 fp = NULL;
386 else {
387 a = (curr * 100) / cmax;
388 if (fp != NULL) {
389 fprintf(fp, "%ld\n", a);
390 fflush(fp);
393 break;
401 * install_init_scripts() -- Create and deploy SysV init scripts.
404 void install_init_scripts(void)
406 #ifdef ENABLE_NLS
407 int localechoice;
408 #endif
409 char question[1024];
410 char buf[256];
411 char http_port[128];
412 #ifdef HAVE_OPENSSL
413 char https_port[128];
414 #endif
415 char hostname[128];
416 char portname[128];
417 char command[SIZ];
418 struct utsname my_utsname;
419 struct stat etcinitd;
420 FILE *fp;
421 char *initfile = "/etc/init.d/webcit";
423 fp = fopen(initfile, "r");
424 if (fp != NULL) {
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) {
427 return;
429 fclose(fp);
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)
438 return;
441 #ifdef ENABLE_NLS
443 localechoice = GetLocalePrefs();
445 #endif
446 /* Defaults */
447 sprintf(http_port, "2000");
448 #ifdef HAVE_OPENSSL
449 sprintf(https_port, "443");
450 #endif
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) &&
459 (errno == ENOENT))
461 if ((stat("/etc/rc.d/init.d/", &etcinitd) == -1) &&
462 (errno == ENOENT))
463 initfile = WEBCITDIR"/webcit.init";
464 else
465 initfile = "/etc/rc.d/init.d/webcit";
468 fp = fopen(initfile, "r");
469 if (fp != NULL) {
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);
477 #ifdef HAVE_OPENSSL
478 if (!strncasecmp(buf, "HTTPS_PORT=", 11)) {
479 safestrncpy(https_port, &buf[11], sizeof https_port);
481 #endif
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);
489 fclose(fp);
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);
499 uname(&my_utsname);
500 sprintf(suggested_url, "http://%s:%s/", my_utsname.nodename, http_port);
502 #ifdef HAVE_OPENSSL
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);
509 #endif
511 /* Find out where Citadel is. */
512 if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
513 strcpy(hostname, "uds");
514 strcpy(portname, getenv("CITADEL"));
516 else {
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);
524 else {
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"
538 "\n"
539 "WEBCIT_DIR=%s\n", setup_directory);
540 fprintf(fp, "HTTP_PORT=%s\n", http_port);
541 #ifdef HAVE_OPENSSL
542 fprintf(fp, "HTTPS_PORT=%s\n", https_port);
543 #endif
544 fprintf(fp, "CTDL_HOSTNAME=%s\n", hostname);
545 fprintf(fp, "CTDL_PORTNAME=%s\n", portname);
547 #ifdef ENABLE_NLS
549 if (localechoice == 0) {
550 #ifdef HAVE_USELOCALE
551 fprintf(fp, "unset LANG\n");
552 #else
553 fprintf(fp, "export WEBCIT_LANG=c\n");
554 #endif
556 else {
557 fprintf(fp, "export WEBCIT_LANG=%s\n", AvailLang[localechoice - localeoffset]);
560 #else
561 fprintf(fp, "# your system doesn't support locales\n");
562 #endif
563 fprintf(fp, "\n"
564 "\n"
565 "case \"$1\" in\n"
566 "\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"
571 " then\n"
572 " echo \"ok\"\n"
573 " else\n"
574 " echo \"failed\"\n"
575 " fi\n");
576 #ifdef HAVE_OPENSSL
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"
581 " then\n"
582 " echo \"ok\"\n"
583 " else\n"
584 " echo \"failed\"\n"
585 " fi\n");
586 #endif
587 fprintf(fp, " ;;\n"
588 "stop) echo -n \"Stopping WebCit... \"\n"
589 " if kill `cat /var/run/webcit.pid 2>/dev/null` 2>/dev/null\n"
590 " then\n"
591 " echo \"ok\"\n"
592 " else\n"
593 " echo \"failed\"\n"
594 " fi\n"
595 " rm -f /var/run/webcit.pid 2>/dev/null\n");
596 #ifdef HAVE_OPENSSL
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"
599 " then\n"
600 " echo \"ok\"\n"
601 " else\n"
602 " echo \"failed\"\n"
603 " fi\n"
604 " rm -f /var/run/webcit-ssl.pid 2>/dev/null\n");
605 #endif
606 fprintf(fp, " ;;\n"
607 "restart) $0 stop\n"
608 " $0 start\n"
609 " ;;\n"
610 "*) echo \"Usage: $0 {start|stop|restart}\"\n"
611 " exit 1\n"
612 " ;;\n"
613 "esac\n"
616 fclose(fp);
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);
622 system(command);
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);
624 system(command);
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) {
639 return UI_DIALOG;
642 return UI_TEXT;
649 int main(int argc, char *argv[])
651 int a;
652 char aaa[256];
653 int info_only = 0;
654 strcpy(suggested_url, "http://<your_host_name>:<port>/");
656 /* set an invalid setup type */
657 setup_type = (-1);
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")) {
672 info_only = 1;
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");
688 cleanup(0);
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"));
696 else {
697 set_value("In what directory is WebCit installed?",
698 setup_directory);
700 if (chdir(setup_directory) != 0) {
701 important_message("WebCit Setup",
702 "The directory you specified does not exist.");
703 cleanup(errno);
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);
714 /* Now begin. */
715 switch (setup_type) {
717 case UI_TEXT:
718 printf("\n\n\n"
719 " *** WebCit setup program ***\n\n");
720 break;
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");
735 sprintf(aaa,
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);
742 else {
743 important_message("Setup finished",
744 "Setup is finished. You may now start the server.");
747 cleanup(0);
748 return 0;