2 * Citadel setup utility
4 * Copyright (c) 1987-2012 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #define SHOW_ME_VAPPEND_PRINTF
16 #include "ctdl_module.h"
23 #include <sys/types.h>
25 #include <sys/utsname.h>
33 #include <libcitadel.h>
38 #include "citadel_dirs.h"
49 #define _(string) gettext(string)
51 #define _(string) (string)
55 #define MAXSETUP 11 /* How many setup questions to ask */
57 #define UI_TEXT 0 /* Default setup type -- text only */
58 #define UI_DIALOG 2 /* Use the 'dialog' program */
59 #define UI_SILENT 3 /* Silent running, for use in scripts */
61 #define SERVICE_NAME "citadel"
62 #define PROTO_NAME "tcp"
63 #define NSSCONF "/etc/nsswitch.conf"
66 typedef enum _SetupStep {
82 ///"CREATE_XINETD_ENTRY";
83 /* Environment variables, don't translate! */
84 const char *EnvNames [eMaxQuestions] = {
100 int using_web_installer = 0;
102 char admin_pass[SIZ];
105 const char *setup_titles[eMaxQuestions];
106 const char *setup_text[eMaxQuestions];
119 setlocale(LC_MESSAGES, getenv("LANG"));
121 bindtextdomain("citadel-setup", LOCALEDIR"/locale");
122 textdomain("citadel-setup");
123 bind_textdomain_codeset("citadel-setup","UTF8");
126 setup_titles[eCitadelHomeDir] = _("Citadel Home Directory");
128 setup_text[eCitadelHomeDir] = _(
129 "Enter the full pathname of the directory in which the Citadel\n"
130 "installation you are creating or updating resides. If you\n"
131 "specify a directory other than the default, you will need to\n"
132 "specify the -h flag to the server when you start it up.\n");
134 setup_text[eCitadelHomeDir] = _(
135 "Enter the subdirectory name for an alternate installation of "
136 "Citadel. To do a default installation just leave it blank."
137 "If you specify a directory other than the default, you will need to\n"
138 "specify the -h flag to the server when you start it up.\n"
139 "note that it may not have a leading /");
142 setup_titles[eSysAdminName] = _("Citadel administrator username:");
143 setup_text[eSysAdminName] = _(
144 "Please enter the name of the Citadel user account that should be granted "
145 "administrative privileges once created. If using internal authentication "
146 "this user account will be created if it does not exist. For external "
147 "authentication this user account has to exist.");
150 setup_titles[eSysAdminPW] = _("Administrator password:");
151 setup_text[eSysAdminPW] = _(
152 "Enter a password for the system administrator. When setup\n"
153 "completes it will attempt to create the administrator user\n"
154 "and set the password specified here.\n");
156 setup_titles[eUID] = _("Citadel User ID:");
157 setup_text[eUID] = _(
158 "Citadel needs to run under its own user ID. This would\n"
159 "typically be called \"citadel\", but if you are running Citadel\n"
160 "as a public site, you might also call it \"bbs\" or \"guest\".\n"
161 "The server will run under this user ID. Please specify that\n"
162 "user ID here. You may specify either a user name or a numeric\n"
165 setup_titles[eIP_ADDR] = _("Listening address for the Citadel server:");
166 setup_text[eIP_ADDR] = _(
167 "Please specify the IP address which the server should be listening to. "
168 "You can name a specific IPv4 or IPv6 address, or you can specify\n"
169 "\"*\" for \"any address\", \"::\" for \"any IPv6 address\", or \"0.0.0.0\"\n"
170 "for \"any IPv4 address\". If you leave this blank, Citadel will\n"
171 "listen on all addresses. "
172 "This can usually be left to the default unless multiple instances of Citadel "
173 "are running on the same computer.");
175 setup_titles[eCTDL_Port] = _("Server port number:");
176 setup_text[eCTDL_Port] = _(
177 "Specify the TCP port number on which your server will run.\n"
178 "Normally, this will be port 504, which is the official port\n"
179 "assigned by the IANA for Citadel servers. You will only need\n"
180 "to specify a different port number if you run multiple instances\n"
181 "of Citadel on the same computer and there is something else\n"
182 "already using port 504.\n");
184 setup_titles[eAuthType] = _("Authentication method to use:");
185 setup_text[eAuthType] = _(
186 "Please choose the user authentication mode. By default Citadel will use its "
187 "own internal user accounts database. If you choose Host, Citadel users will "
188 "have accounts on the host system, authenticated via /etc/passwd or a PAM "
189 "source. LDAP chooses an RFC 2307 compliant directory server, the last option "
190 "chooses the nonstandard MS Active Directory LDAP scheme."
192 "Do not change this option unless you are sure it is required, since changing "
193 "back requires a full reinstall of Citadel."
195 " 0. Self contained authentication\n"
196 " 1. Host system integrated authentication\n"
197 " 2. External LDAP - RFC 2307 compliant directory\n"
198 " 3. External LDAP - nonstandard MS Active Directory\n"
200 "For help: http://www.citadel.org/doku.php/faq:installation:authmodes\n"
202 "ANSWER \"0\" UNLESS YOU COMPLETELY UNDERSTAND THIS OPTION.\n");
204 setup_titles[eLDAP_Host] = _("LDAP host:");
205 setup_text[eLDAP_Host] = _(
206 "Please enter the host name or IP address of your LDAP server.\n");
208 setup_titles[eLDAP_Port] = _("LDAP port number:");
209 setup_text[eLDAP_Port] = _(
210 "Please enter the port number of the LDAP service (usually 389).\n");
212 setup_titles[eLDAP_Base_DN] = _("LDAP base DN:");
213 setup_text[eLDAP_Base_DN] = _(
214 "Please enter the Base DN to search for authentication\n"
215 "(for example: dc=example,dc=com)\n");
217 setup_titles[eLDAP_Bind_DN] = _("LDAP bind DN:");
218 setup_text[eLDAP_Bind_DN] = _(
219 "Please enter the DN of an account to use for binding to the LDAP server for "
220 "performing queries. The account does not require any other privileges. If "
221 "your LDAP server allows anonymous queries, you can leave this blank."
222 "Please enter the DN of an account to use for binding to the LDAP server\n"
223 "for performing queries. The account does not require any other\n"
224 "privileges. If your LDAP server allows anonymous queries, you can\n"
225 "leave this blank.\n");
227 setup_titles[eLDAP_Bind_PW] = _("LDAP bind password:");
228 setup_text[eLDAP_Bind_PW] = _(
229 "If you entered a Bind DN in the previous question, you must now enter\n"
230 "the password associated with that account. Otherwise, you can leave this\n"
234 // Debug loading of locales... Strace does a better job though.
235 printf("Message catalog directory: %s\n", bindtextdomain("citadel-setup", LOCALEDIR"/locale"));
236 printf("Text domain: %s\n", textdomain("citadel-setup"));
237 printf("Text domain Charset: %s\n", bind_textdomain_codeset("citadel-setup","UTF8"));
240 for (i = 0; i < eMaxQuestions; i++)
241 printf("%s - %s\n", setup_titles[i], _(setup_titles[i]));
248 * Print the stack frame for a backtrace
250 void cit_backtrace(void)
252 #ifdef HAVE_BACKTRACE
253 void *stack_frames[50];
257 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
258 strings = backtrace_symbols(stack_frames, size);
259 for (i = 0; i < size; i++) {
261 fprintf(stderr, "%s\n", strings[i]);
263 fprintf(stderr, "%p\n", stack_frames[i]);
269 struct config config;
273 void cleanup(int exitcode)
275 // printf("Exitcode: %d\n", exitcode);
282 void title(const char *text)
284 if (setup_type == UI_TEXT) {
285 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<%s>\n", text);
291 int yesno(const char *question, int default_value)
297 switch (setup_type) {
301 printf("%s\n%s [%s] --> ",
304 ( default_value ? _("Yes") : _("No") )
306 if (fgets(buf, sizeof buf, stdin))
308 answer = tolower(buf[0]);
309 if ((buf[0]==0) || (buf[0]==13) || (buf[0]==10))
310 answer = default_value;
311 else if (answer == 'y')
313 else if (answer == 'n')
316 } while ((answer < 0) || (answer > 1));
320 sprintf(buf, "exec %s %s --yesno '%s' 15 75",
321 getenv("CTDL_DIALOG"),
322 ( default_value ? "" : "--defaultno" ),
340 void important_message(const char *title, const char *msgtext)
345 switch (setup_type) {
348 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");
349 printf(" %s \n\n%s\n\n", title, msgtext);
350 printf("%s", _("Press return to continue..."));
351 if (fgets(buf, sizeof buf, stdin));
355 sprintf(buf, "exec %s --msgbox '%s' 19 72",
356 getenv("CTDL_DIALOG"),
360 fprintf(stderr, _("failed to run the dialog command\n"));
363 fprintf(stderr, "%s\n", msgtext);
368 void important_msgnum(int msgnum)
370 important_message(_("Important Message"), setup_text[msgnum]);
373 void display_error(char *error_message_format, ...)
379 va_start(arg_ptr, error_message_format);
380 StrBufVAppendPrintf(Msg, error_message_format, arg_ptr);
383 important_message(_("Error"), ChrPtr(Msg));
387 void progress(char *text, long int curr, long int cmax)
389 static long dots_printed = 0L;
391 static FILE *fp = NULL;
394 switch (setup_type) {
398 printf("%s\n", text);
399 printf("....................................................");
400 printf("..........................\r");
403 } else if (curr == cmax) {
404 printf("\r%79s\n", "");
406 a = (curr * 100) / cmax;
409 while (dots_printed < a) {
419 sprintf(buf, "exec %s --gauge '%s' 7 72 0",
420 getenv("CTDL_DIALOG"),
422 fp = popen(buf, "w");
428 else if (curr == cmax) {
430 fprintf(fp, "100\n");
436 a = (curr * 100) / cmax;
438 fprintf(fp, "%ld\n", a);
452 * check_services_entry() -- Make sure "citadel" is in /etc/services
455 void check_services_entry(void)
461 if (getservbyname(SERVICE_NAME, PROTO_NAME) == NULL) {
462 for (i=0; i<=2; ++i) {
463 progress(_("Adding service entry..."), i, 2);
465 sfp = fopen("/etc/services", "a");
467 sprintf(errmsg, "%s /etc/services: %s", _("Cannot open"), strerror(errno));
468 display_error(errmsg);
470 fprintf(sfp, "%s 504/tcp\n", SERVICE_NAME);
482 * delete_inittab_entry() -- Remove obsolete /etc/inittab entry for Citadel
484 void delete_inittab_entry(void)
488 char looking_for[256];
490 char outfilename[32];
491 int changes_made = 0;
494 /* Determine the fully qualified path name of citserver */
495 snprintf(looking_for, sizeof looking_for, "%s/citserver", ctdl_sbin_dir);
497 /* Now tweak /etc/inittab */
498 infp = fopen("/etc/inittab", "r");
501 /* If /etc/inittab does not exist, return quietly.
502 * Not all host platforms have it.
504 if (errno == ENOENT) {
508 /* Other errors might mean something really did go wrong.
510 sprintf(buf, "%s /etc/inittab: %s", _("Cannot open"), strerror(errno));
515 strcpy(outfilename, "/tmp/ctdlsetup.XXXXXX");
516 outfp = fdopen(mkstemp(outfilename), "w+");
518 sprintf(buf, "%s %s: %s", _("Cannot open"), outfilename, strerror(errno));
524 while (fgets(buf, sizeof buf, infp) != NULL) {
525 if (strstr(buf, looking_for) != NULL) {
526 rv = fwrite("#", 1, 1, outfp);
529 display_error("%s %s\n",
530 _("failed to modify inittab"),
535 rv = fwrite(buf, strlen(buf), 1, outfp);
538 display_error("%s %s\n", _("failed to modify inittab"), strerror(errno));
546 sprintf(buf, "/bin/mv -f %s /etc/inittab 2>/dev/null", outfilename);
548 rv = system("/sbin/init q 2>/dev/null");
557 * install_init_scripts() -- Try to configure to start Citadel at boot
559 void install_init_scripts(void)
561 struct stat etcinitd;
563 char *initfile = "/etc/init.d/citadel";
567 if ( (stat("/etc/init.d/", &etcinitd) == -1)
570 if ( (stat("/etc/rc.d/init.d/", &etcinitd) == -1)
573 initfile = CTDLDIR"/citadel.init";
576 initfile = "/etc/rc.d/init.d/citadel";
580 fp = fopen(initfile, "r");
582 if (yesno(_("Citadel already appears to be configured to start at boot.\n"
583 "Would you like to keep your boot configuration as is?\n"), 1) == 1) {
590 if (yesno(_("Would you like to automatically start Citadel at boot?\n"), 1) == 0) {
594 fp = fopen(initfile, "w");
596 display_error("%s /etc/init.d/citadel", _("Cannot create"));
600 fprintf(fp, "#!/bin/sh\n"
602 "# Init file for Citadel\n"
604 "# chkconfig: - 79 30\n"
605 "# description: Citadel service\n"
606 "# processname: citserver\n"
607 "# pidfile: %s/citadel.pid\n\n"
608 "# uncomment this to create coredumps as described in\n"
609 "# http://www.citadel.org/doku.php/faq:mastering_your_os:gdb#how.do.i.make.my.system.produce.core-files\n"
610 "# ulimit -c unlimited\n"
618 "test -d /var/run || exit 0\n"
622 "start) echo -n \"Starting Citadel... \"\n"
623 " if $CITADEL_DIR/citserver -lmail -d\n"
630 "stop) echo -n \"Stopping Citadel... \"\n"
631 " if $CITADEL_DIR/sendcommand DOWN >/dev/null 2>&1 ; then\n"
636 " rm -f %s/citadel.pid 2>/dev/null\n"
641 "restart) if $CITADEL_DIR/sendcommand DOWN 1 >/dev/null 2>&1 ; then\n"
647 "*) echo \"Usage: $0 {start|stop|restart}\"\n"
654 chmod(initfile, 0755);
656 /* Set up the run levels. */
657 rv = system("/bin/rm -f /etc/rc?.d/[SK]??citadel 2>/dev/null");
659 display_error(_("failed to remove system V init links\n"));
662 snprintf(command, sizeof(command), "for x in 2 3 4 5 ; do [ -d /etc/rc$x.d ] && ln -s %s /etc/rc$x.d/S79citadel ; done 2>/dev/null", initfile);
663 rv = system(command);
665 display_error(_("failed to set system V init links\n"));
668 snprintf(command, sizeof(command),"for x in 0 6 S; do [ -d /etc/rc$x.d ] && ln -s %s /etc/rc$x.d/K30citadel ; done 2>/dev/null", initfile);
669 rv = system(command);
671 display_error(_("failed to set system V init links\n"));
678 * On systems which use xinetd, see if we can offer to install Citadel as
679 * the default telnet target.
681 void check_xinetd_entry(void) {
682 char *filename = "/etc/xinetd.d/telnet";
685 int already_citadel = 0;
688 fp = fopen(filename, "r+");
689 if (fp == NULL) return; /* Not there. Oh well... */
691 while (fgets(buf, sizeof buf, fp) != NULL) {
692 if (strstr(buf, "/citadel") != NULL) already_citadel = 1;
695 if (already_citadel) return; /* Already set up this way. */
697 /* Otherwise, prompt the user to create an entry. */
698 if (getenv("CREATE_XINETD_ENTRY") != NULL) {
699 if (strcasecmp(getenv("CREATE_XINETD_ENTRY"), "yes")) {
704 snprintf(buf, sizeof buf,
705 _("Setup can configure the \"xinetd\" service to automatically\n"
706 "connect incoming telnet sessions to Citadel, bypassing the\n"
707 "host system login: prompt. Would you like to do this?\n"
710 if (yesno(buf, 1) == 0) {
715 fp = fopen(filename, "w");
717 "# description: telnet service for Citadel users\n"
722 " socket_type = stream\n"
725 " server = /usr/sbin/in.telnetd\n"
726 " server_args = -h -L %s/citadel\n"
727 " log_on_failure += USERID\n"
732 /* Now try to restart the service */
733 rv = system("/etc/init.d/xinetd restart >/dev/null 2>&1");
735 display_error(_("failed to restart xinetd.\n"));
741 * Offer to disable other MTA's
743 void disable_other_mta(const char *mta) {
749 sprintf(buf, "/bin/ls -l /etc/rc*.d/S*%s 2>/dev/null; "
750 "/bin/ls -l /etc/rc.d/rc*.d/S*%s 2>/dev/null",
752 fp = popen(buf, "r");
753 if (fp == NULL) return;
755 while (fgets(buf, sizeof buf, fp) != NULL) {
759 if (lines == 0) return; /* Nothing to do. */
762 /* Offer to replace other MTA with the vastly superior Citadel :) */
764 snprintf(buf, sizeof buf,
765 "%s \"%s\" %s%s%s%s%s%s%s",
766 _("You appear to have the "),
769 "running on your system. If you want Citadel mail\n"
772 _(" you will have to manually integrate\n"
773 "them. It is preferable to disable "),
775 _(", and use Citadel's\n"
776 "SMTP, POP3, and IMAP services.\n\n"
779 _("so that Citadel has access to ports\n"
780 "25, 110, and 143?\n")
782 if (yesno(buf, 1) == 0) {
787 sprintf(buf, "for x in /etc/rc*.d/S*%s; do mv $x `echo $x |sed s/S/K/g`; done >/dev/null 2>&1", mta);
790 display_error("%s %s.\n", _("failed to disable other mta"), mta);
792 sprintf(buf, "/etc/init.d/%s stop >/dev/null 2>&1", mta);
795 display_error(" %s.\n", _("failed to disable other mta"), mta);
798 const char *other_mtas[] = {
799 "courier-authdaemon",
822 void disable_other_mtas(void)
825 if ((getenv("ACT_AS_MTA") == NULL) ||
826 (getenv("ACT_AS_MTA") &&
827 strcasecmp(getenv("ACT_AS_MTA"), "yes") == 0)) {
828 /* Offer to disable other MTA's on the system. */
829 while (!IsEmptyStr(other_mtas[i]))
831 disable_other_mta(other_mtas[i]);
838 * Check to see if our server really works. Returns 0 on success.
840 int test_server(char *relhomestr, int relhome) {
847 /* Generate a silly little cookie. We're going to write it out
848 * to the server and try to get it back. The cookie does not
849 * have to be secret ... just unique.
851 generate_uuid(cookie);
854 sprintf(cmd, "%s/sendcommand -h%s ECHO %s 2>&1",
861 sprintf(cmd, "%s/sendcommand ECHO %s 2>&1",
867 fp = popen(cmd, "r");
868 if (fp == NULL) return(errno);
870 while (fgets(buf, sizeof buf, fp) != NULL) {
872 && (strstr(buf, cookie) != NULL) ) {
884 void strprompt(const char *prompt_title, const char *prompt_text, char *Target, char *DefValue)
888 char dialog_result[PATH_MAX];
892 strcpy(setupmsg, "");
894 switch (setup_type) {
897 printf("\n%s\n", prompt_text);
898 printf("%s\n%s\n", _("This is currently set to:"), Target);
899 printf("%s\n", _("Enter new value or press return to leave unchanged:"));
900 if (fgets(buf, sizeof buf, stdin)){
901 buf[strlen(buf) - 1] = 0;
903 if (!IsEmptyStr(buf))
908 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
909 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%s' 2>%s",
910 getenv("CTDL_DIALOG"),
916 fprintf(stderr, "failed to run whiptail or dialog\n");
919 fp = fopen(dialog_result, "r");
921 if (fgets(Target, sizeof buf, fp)) {
922 if (Target[strlen(Target)-1] == 10) {
923 Target[strlen(Target)-1] = 0;
927 unlink(dialog_result);
931 if (*DefValue != '\0')
932 strcpy(Target, DefValue);
937 void set_bool_val(int msgpos, int *ip, char *DefValue)
939 title(setup_titles[msgpos]);
940 *ip = yesno(setup_text[msgpos], *ip);
943 void set_str_val(int msgpos, char *Target, char *DefValue)
945 strprompt(setup_titles[msgpos],
952 void set_int_val(int msgpos, int *ip, char *DefValue)
955 snprintf(buf, sizeof buf, "%d", (int) *ip);
956 set_str_val(msgpos, buf, DefValue);
961 void set_char_val(int msgpos, char *ip, char *DefValue)
964 snprintf(buf, sizeof buf, "%d", (int) *ip);
965 set_str_val(msgpos, buf, DefValue);
966 *ip = (char) atoi(buf);
970 void set_long_val(int msgpos, long int *ip, char *DefValue)
973 snprintf(buf, sizeof buf, "%ld", *ip);
974 set_str_val(msgpos, buf, DefValue);
979 void edit_value(int curr)
983 char ctdluidname[256];
986 if (setup_type == UI_SILENT)
988 Value = getenv(EnvNames[curr]);
997 set_str_val(curr, config.c_sysadm, Value);
1001 set_str_val(curr, admin_pass, Value);
1005 if (setup_type == UI_SILENT)
1008 config.c_ctdluid = atoi(Value);
1014 config.c_ctdluid = 0; /* work-around for Windows */
1016 i = config.c_ctdluid;
1019 set_int_val(curr, &i, Value);
1020 config.c_ctdluid = i;
1023 strcpy(ctdluidname, pw->pw_name);
1024 set_str_val(curr, ctdluidname, Value);
1025 pw = getpwnam(ctdluidname);
1027 config.c_ctdluid = pw->pw_uid;
1029 else if (atoi(ctdluidname) > 0) {
1030 config.c_ctdluid = atoi(ctdluidname);
1038 set_str_val(curr, config.c_ip_addr, Value);
1042 set_int_val(curr, &config.c_port_number, Value);
1046 if (setup_type == UI_SILENT)
1049 config.c_auth_mode = AUTHMODE_NATIVE;
1053 if ((strcasecmp(auth, "yes") == 0) ||
1054 (strcasecmp(auth, "host") == 0))
1056 config.c_auth_mode = AUTHMODE_HOST;
1058 else if (strcasecmp(auth, "ldap") == 0){
1059 config.c_auth_mode = AUTHMODE_LDAP;
1061 else if ((strcasecmp(auth, "ldap_ad") == 0) ||
1062 (strcasecmp(auth, "active directory") == 0)){
1063 config.c_auth_mode = AUTHMODE_LDAP_AD;
1068 set_int_val(curr, &config.c_auth_mode, Value);
1073 set_str_val(curr, config.c_ldap_host, Value);
1077 if (config.c_ldap_port == 0) {
1078 config.c_ldap_port = 389;
1080 set_int_val(curr, &config.c_ldap_port, Value);
1084 set_str_val(curr, config.c_ldap_base_dn, Value);
1088 set_str_val(curr, config.c_ldap_bind_dn, Value);
1092 set_str_val(curr, config.c_ldap_bind_pw, Value);
1099 * (re-)write the config data to disk
1101 void write_config_to_disk(void)
1107 if ((fd = creat(file_citadel_config, S_IRUSR | S_IWUSR)) == -1) {
1108 display_error("%s citadel.config [%s][%s]\n", _("setup: cannot open"), file_citadel_config, strerror(errno));
1111 fp = fdopen(fd, "wb");
1113 display_error("%s citadel.config [%s][%s]\n", _("setup: cannot open"), file_citadel_config, strerror(errno));
1117 rv = fwrite((char *) &config, sizeof(struct config), 1, fp);
1120 display_error("%s citadel.config [%s][%s]\n", _("setup: cannot write"), file_citadel_config, strerror(errno));
1129 * Figure out what type of user interface we're going to use
1131 int discover_ui(void)
1134 /* Use "whiptail" or "dialog" if we have it */
1135 if (getenv("CTDL_DIALOG") != NULL) {
1145 * Strip "db" entries out of /etc/nsswitch.conf
1154 int file_changed = 0;
1155 char new_filename[64];
1158 fp_read = fopen(NSSCONF, "r");
1159 if (fp_read == NULL) {
1163 strcpy(new_filename, "/tmp/ctdl_fixnss_XXXXXX");
1164 fd_write = mkstemp(new_filename);
1170 while (fgets(buf, sizeof buf, fp_read) != NULL) {
1171 strcpy(buf_nc, buf);
1172 for (i=0; i<strlen(buf_nc); ++i) {
1173 if (buf_nc[i] == '#') {
1177 for (i=0; i<strlen(buf_nc); ++i) {
1178 if (!strncasecmp(&buf_nc[i], "db", 2)) {
1180 if ((isspace(buf_nc[i+2])) || (buf_nc[i+2]==0)) {
1182 strcpy(&buf_nc[i], &buf_nc[i+2]);
1183 strcpy(&buf[i], &buf[i+2]);
1185 strcpy(&buf_nc[i], &buf_nc[i+1]);
1186 strcpy(&buf[i], &buf[i+1]);
1192 if (write(fd_write, buf, strlen(buf)) != strlen(buf)) {
1195 unlink(new_filename);
1202 if (!file_changed) {
1203 unlink(new_filename);
1207 snprintf(question, sizeof question,
1210 "/etc/nsswitch.conf is configured to use the 'db' module for\n"
1211 "one or more services. This is not necessary on most systems,\n"
1212 "and it is known to crash the Citadel server when delivering\n"
1213 "mail to the Internet.\n"
1215 "Do you want this module to be automatically disabled?\n"
1220 if (yesno(question, 1)) {
1221 sprintf(buf, "/bin/mv -f %s %s", new_filename, NSSCONF);
1224 fprintf(stderr, "failed to edit %s.\n", NSSCONF);
1226 chmod(NSSCONF, 0644);
1228 unlink(new_filename);
1231 void check_init_script (char *relhome)
1237 * If we're running on SysV, install init scripts.
1239 if (!access("/var/run", W_OK)) {
1241 if (getenv("NO_INIT_SCRIPTS") == NULL) {
1242 install_init_scripts();
1245 if (!access("/etc/init.d/citadel", X_OK)) {
1246 rv = system("/etc/init.d/citadel start");
1248 fprintf(stderr, "failed to call our initscript.");
1252 if (test_server(relhome, enable_home) == 0) {
1256 if (config.c_auth_mode == AUTHMODE_NATIVE) {
1257 snprintf (admin_cmd, sizeof(admin_cmd), "%s/sendcommand \"CREU %s|%s\" 2>&1",
1258 ctdl_sbin_dir, config.c_sysadm, admin_pass);
1259 fp = popen(admin_cmd, "r");
1261 while (fgets(buf, sizeof buf, fp) != NULL)
1263 if ((atol(buf) == 574) || (atol(buf) == 200))
1269 if (found_it == 0) {
1270 important_message("Error","Setup failed to create your admin user");
1274 if (setup_type != UI_SILENT)
1275 important_message(_("Setup finished"),
1276 _("Setup of the Citadel server is complete.\n"
1277 "If you will be using WebCit, please run its\n"
1278 "setup program now; otherwise, run './citadel'\n"
1282 important_message(_("Setup failed"),
1283 _("Setup is finished, but the Citadel server failed to start.\n"
1284 "Go back and check your configuration.\n")
1291 important_message(_("Setup finished"),
1292 _("Setup is finished. You may now start the server."));
1298 #define GetDefaultVALINT(CFGNAME, DEFL) GetDefaultValInt(&config.CFGNAME, "CITADEL_"#CFGNAME, DEFL)
1299 void GetDefaultValInt(int *WhereTo, const char *VarName, int DefVal)
1302 if (*WhereTo == 0) *WhereTo = DefVal;
1304 if ((setup_type == UI_SILENT) &&
1305 (ch = getenv(VarName), ch != NULL))
1307 *WhereTo = atoi(ch);
1310 #define GetDefaultVALCHAR(CFGNAME, DEFL) GetDefaultValChar(&config.CFGNAME, "CITADEL_"#CFGNAME, DEFL)
1311 void GetDefaultValChar(char *WhereTo, const char *VarName, char DefVal)
1314 if (*WhereTo == 0) *WhereTo = DefVal;
1316 if ((setup_type == UI_SILENT) &&
1317 (ch = getenv(VarName), ch != NULL))
1319 *WhereTo = atoi(ch);
1322 #define GetDefaultVALSTR(CFGNAME, DEFL) GetDefaultValStr(&config.CFGNAME[0], sizeof(config.CFGNAME), "CITADEL_"#CFGNAME, DEFL)
1323 void GetDefaultValStr(char *WhereTo, size_t nMax, const char *VarName, const char *DefVal)
1326 if (*WhereTo == '\0')
1327 safestrncpy(WhereTo, DefVal, nMax);
1329 if ((setup_type == UI_SILENT) &&
1330 (ch = getenv(VarName), ch != NULL))
1332 safestrncpy(WhereTo, ch, nMax);
1337 void set_default_values(void)
1340 struct utsname my_utsname;
1343 /* Determine our host name, in case we need to use it as a default */
1346 /* set some sample/default values in place of blanks... */
1347 GetDefaultVALSTR(c_nodename, my_utsname.nodename);
1348 strtok(config.c_nodename, ".");
1349 if (IsEmptyStr(config.c_fqdn) ) {
1350 if ((he = gethostbyname(my_utsname.nodename)) != NULL) {
1351 safestrncpy(config.c_fqdn, he->h_name, sizeof config.c_fqdn);
1353 safestrncpy(config.c_fqdn, my_utsname.nodename, sizeof config.c_fqdn);
1356 GetDefaultVALSTR(c_humannode, _("My System"));
1357 GetDefaultVALSTR(c_phonenum, _("US 800 555 1212"));
1359 GetDefaultVALCHAR(c_initax, 4);
1361 GetDefaultVALSTR(c_moreprompt, "<more>");
1362 GetDefaultVALSTR(c_twitroom, "Trashcan");
1363 GetDefaultVALSTR(c_baseroom, BASEROOM);
1364 GetDefaultVALSTR(c_aideroom, "Aide");
1365 GetDefaultVALINT(c_port_number, 504);
1367 GetDefaultVALINT(c_sleeping, 900);
1369 if (config.c_ctdluid == 0) {
1370 pw = getpwnam("citadel");
1372 config.c_ctdluid = pw->pw_uid;
1375 if (config.c_ctdluid == 0) {
1376 pw = getpwnam("bbs");
1378 config.c_ctdluid = pw->pw_uid;
1381 if (config.c_ctdluid == 0) {
1382 pw = getpwnam("guest");
1384 config.c_ctdluid = pw->pw_uid;
1387 if (config.c_createax == 0) {
1388 config.c_createax = 3;
1391 * Negative values for maxsessions are not allowed.
1393 if (config.c_maxsessions < 0) {
1394 config.c_maxsessions = 0;
1396 /* We need a system default message expiry policy, because this is
1397 * the top level and there's no 'higher' policy to fall back on.
1398 * By default, do not expire messages at all.
1400 if (config.c_ep.expire_mode == 0) {
1401 config.c_ep.expire_mode = EXPIRE_MANUAL;
1402 config.c_ep.expire_value = 0;
1406 * Default port numbers for various services
1408 GetDefaultVALINT(c_smtp_port, 25);
1409 GetDefaultVALINT(c_pop3_port, 110);
1410 GetDefaultVALINT(c_imap_port, 143);
1411 GetDefaultVALINT(c_msa_port, 587);
1412 GetDefaultVALINT(c_smtps_port, 465);
1413 GetDefaultVALINT(c_pop3s_port, 995);
1414 GetDefaultVALINT(c_imaps_port, 993);
1415 GetDefaultVALINT(c_pftcpdict_port, -1);
1416 GetDefaultVALINT(c_managesieve_port, 2020);
1417 GetDefaultVALINT(c_xmpp_c2s_port, 5222);
1418 GetDefaultVALINT(c_xmpp_s2s_port, 5269);
1422 void get_config (void)
1429 * What we're going to try to do here is append a whole bunch of
1430 * nulls to the citadel.config file, so we can keep the old config
1431 * values if they exist, but if the file is missing or from an
1432 * earlier version with a shorter config structure, when setup tries
1433 * to read the old config parameters, they'll all come up zero.
1434 * The length of the config file will be set to what it's supposed
1435 * to be when we rewrite it, because we replace the old file with a
1436 * completely new copy.
1438 if ((a = open(file_citadel_config, O_WRONLY | O_CREAT | O_APPEND,
1439 S_IRUSR | S_IWUSR)) == -1) {
1440 display_error("%s citadel.config [%s][%s]\n", _("setup: cannot append"), file_citadel_config, strerror(errno));
1443 fp = fdopen(a, "ab");
1445 display_error("%s citadel.config [%s][%s]\n", _("setup: cannot append"), file_citadel_config, strerror(errno));
1448 for (a = 0; a < sizeof(struct config); ++a) {
1453 /* now we re-open it, and read the old or blank configuration */
1454 fp = fopen(file_citadel_config, "rb");
1456 display_error("%s citadel.config [%s][%s]\n", _("setup: cannot open"), file_citadel_config, strerror(errno));
1460 rv = fread((char *) &config, sizeof(struct config), 1, fp);
1462 display_error("%s citadel.config [%s][%s]\n", _("setup: cannot write"), file_citadel_config, strerror(errno));
1467 int main(int argc, char *argv[])
1472 int old_setup_level = 0;
1476 char relhome[PATH_MAX]="";
1477 char ctdldir[PATH_MAX]=CTDLDIR;
1482 /* set an invalid setup type */
1485 /* Check to see if we're running the web installer */
1486 if (getenv("CITADEL_INSTALLER") != NULL) {
1487 using_web_installer = 1;
1490 /* parse command line args */
1491 for (a = 0; a < argc; ++a) {
1492 if (!strncmp(argv[a], "-u", 2)) {
1493 strcpy(aaa, argv[a]);
1494 strcpy(aaa, &aaa[2]);
1495 setup_type = atoi(aaa);
1497 else if (!strcmp(argv[a], "-i")) {
1500 else if (!strcmp(argv[a], "-q")) {
1501 setup_type = UI_SILENT;
1503 else if (!strncmp(argv[a], "-h", 2)) {
1504 relh=argv[a][2]!='/';
1506 safestrncpy(ctdl_home_directory, &argv[a][2], sizeof ctdl_home_directory);
1508 safestrncpy(relhome, &argv[a][2], sizeof relhome);
1515 calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
1518 /* If a setup type was not specified, try to determine automatically
1519 * the best one to use out of all available types.
1521 if (setup_type < 0) {
1522 setup_type = discover_ui();
1524 if (info_only == 1) {
1525 important_message(_("Citadel Setup"), CITADEL);
1529 enable_home = ( relh | home );
1531 if (chdir(ctdl_run_dir) != 0) {
1532 display_error(_("Citadel Setup"),
1534 _("The directory you specified does not exist"),
1540 /* Try to stop Citadel if we can */
1541 if (!access("/etc/init.d/citadel", X_OK)) {
1542 rv = system("/etc/init.d/citadel stop");
1544 fprintf(stderr, _("failed to stop us using the initscript.\n"));
1547 /* Make sure Citadel is not running. */
1548 if (test_server(relhome, enable_home) == 0) {
1549 important_message(_("Citadel Setup"),
1550 _("The Citadel service is still running.\n"
1551 "Please stop the service manually and run "
1557 switch (setup_type) {
1562 _("Citadel setup program"));
1568 set_default_values();
1570 /* Go through a series of dialogs prompting for config info */
1571 for (curr = 1; curr <= MAXSETUP; ++curr) {
1573 if ((curr == 6) && (config.c_auth_mode != AUTHMODE_LDAP) && (config.c_auth_mode != AUTHMODE_LDAP_AD)) {
1574 curr += 5; /* skip LDAP questions if we're not authenticating against LDAP */
1578 /***** begin version update section *****/
1580 old_setup_level = config.c_setup_level;
1582 if (old_setup_level == 0) {
1586 if (old_setup_level < 555) {
1589 _("This Citadel installation is too old to be upgraded.")
1593 write_config_to_disk();
1595 old_setup_level = config.c_setup_level;
1597 /***** end of version update section *****/
1600 config.c_setup_level = REV_LEVEL;
1602 if ((pw = getpwuid(config.c_ctdluid)) == NULL) {
1608 create_run_directories(config.c_ctdluid, gid);
1610 write_config_to_disk();
1612 if ( ((setup_type == UI_SILENT)
1613 && (getenv("ALTER_ETC_SERVICES")!=NULL))
1614 || (setup_type != UI_SILENT)
1616 check_services_entry(); /* Check /etc/services */
1620 delete_inittab_entry(); /* Remove obsolete /etc/inittab entry */
1621 check_xinetd_entry(); /* Check /etc/xinetd.d/telnet */
1622 disable_other_mtas(); /* Offer to disable other MTAs */
1623 fixnss(); /* Check for the 'db' nss and offer to disable it */
1626 progress(_("Setting file permissions"), 1, 3);
1627 rv = chown(file_citadel_config, config.c_ctdluid, gid);
1628 progress(_("Setting file permissions"), 2, 3);
1629 rv = chmod(file_citadel_config, S_IRUSR | S_IWUSR);
1630 progress(_("Setting file permissions"), 3, 3);
1632 check_init_script(relhome);