2 * Citadel setup utility
5 #define SHOW_ME_VAPPEND_PRINTF
6 #include "ctdl_module.h"
13 #include <sys/types.h>
15 #include <sys/utsname.h>
23 #include <libcitadel.h>
28 #include "citadel_dirs.h"
39 #define _(string) gettext(string)
41 #define _(string) (string)
45 #define MAXSETUP 11 /* How many setup questions to ask */
47 #define UI_TEXT 0 /* Default setup type -- text only */
48 #define UI_DIALOG 2 /* Use the 'dialog' program */
49 #define UI_SILENT 3 /* Silent running, for use in scripts */
51 #define SERVICE_NAME "citadel"
52 #define PROTO_NAME "tcp"
53 #define NSSCONF "/etc/nsswitch.conf"
56 typedef enum _SetupStep {
72 ///"CREATE_XINETD_ENTRY";
73 /* Environment variables, don't translate! */
74 const char *EnvNames [eMaxQuestions] = {
90 int using_web_installer = 0;
95 const char *setup_titles[eMaxQuestions];
96 const char *setup_text[eMaxQuestions];
98 /* calculate all our path on a central place */
99 /* where to keep our config */
113 setlocale(LC_MESSAGES, getenv("LANG"));
115 bindtextdomain("citadel-setup", LOCALEDIR"/locale");
116 textdomain("citadel-setup");
117 bind_textdomain_codeset("citadel-setup","UTF8");
120 setup_titles[eCitadelHomeDir] = _("Citadel Home Directory");
122 setup_text[eCitadelHomeDir] = _(
123 "Enter the full pathname of the directory in which the Citadel\n"
124 "installation you are creating or updating resides. If you\n"
125 "specify a directory other than the default, you will need to\n"
126 "specify the -h flag to the server when you start it up.\n");
128 setup_text[eCitadelHomeDir] = _(
129 "Enter the subdirectory name for an alternate installation of "
130 "Citadel. To do a default installation just leave it blank."
131 "If you 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"
133 "note that it may not have a leading /");
136 setup_titles[eSysAdminName] = _("Citadel administrator username:");
137 setup_text[eSysAdminName] = _(
138 "Please enter the name of the Citadel user account that should be granted "
139 "administrative privileges once created. If using internal authentication "
140 "this user account will be created if it does not exist. For external "
141 "authentication this user account has to exist.");
144 setup_titles[eSysAdminPW] = _("Administrator password:");
145 setup_text[eSysAdminPW] = _(
146 "Enter a password for the system administrator. When setup\n"
147 "completes it will attempt to create the administrator user\n"
148 "and set the password specified here.\n");
150 setup_titles[eUID] = _("Citadel User ID:");
151 setup_text[eUID] = _(
152 "Citadel needs to run under its own user ID. This would\n"
153 "typically be called \"citadel\", but if you are running Citadel\n"
154 "as a public BBS, you might also call it \"bbs\" or \"guest\".\n"
155 "The server will run under this user ID. Please specify that\n"
156 "user ID here. You may specify either a user name or a numeric\n"
159 setup_titles[eIP_ADDR] = _("Listening address for the Citadel server:");
160 setup_text[eIP_ADDR] = _(
161 "Please specify the IP address which the server should be listening to. "
162 "You can name a specific IPv4 or IPv6 address, or you can specify\n"
163 "\"*\" for \"any address\", \"::\" for \"any IPv6 address\", or \"0.0.0.0\"\n"
164 "for \"any IPv4 address\". If you leave this blank, Citadel will\n"
165 "listen on all addresses. "
166 "This can usually be left to the default unless multiple instances of Citadel "
167 "are running on the same computer.");
169 setup_titles[eCTDL_Port] = _("Server port number:");
170 setup_text[eCTDL_Port] = _(
171 "Specify the TCP port number on which your server will run.\n"
172 "Normally, this will be port 504, which is the official port\n"
173 "assigned by the IANA for Citadel servers. You will only need\n"
174 "to specify a different port number if you run multiple instances\n"
175 "of Citadel on the same computer and there is something else\n"
176 "already using port 504.\n");
178 setup_titles[eAuthType] = _("Authentication method to use:");
179 setup_text[eAuthType] = _(
180 "Please choose the user authentication mode. By default Citadel will use its "
181 "own internal user accounts database. If you choose Host, Citadel users will "
182 "have accounts on the host system, authenticated via /etc/passwd or a PAM "
183 "source. LDAP chooses an RFC 2307 compliant directory server, the last option "
184 "chooses the nonstandard MS Active Directory LDAP scheme."
186 "Do not change this option unless you are sure it is required, since changing "
187 "back requires a full reinstall of Citadel."
189 " 0. Self contained authentication\n"
190 " 1. Host system integrated authentication\n"
191 " 2. External LDAP - RFC 2307 compliant directory\n"
192 " 3. External LDAP - nonstandard MS Active Directory\n"
194 "For help: http://www.citadel.org/doku.php/faq:installation:authmodes\n"
196 "ANSWER \"0\" UNLESS YOU COMPLETELY UNDERSTAND THIS OPTION.\n");
198 setup_titles[eLDAP_Host] = _("LDAP host:");
199 setup_text[eLDAP_Host] = _(
200 "Please enter the host name or IP address of your LDAP server.\n");
202 setup_titles[eLDAP_Port] = _("LDAP port number:");
203 setup_text[eLDAP_Port] = _(
204 "Please enter the port number of the LDAP service (usually 389).\n");
206 setup_titles[eLDAP_Base_DN] = _("LDAP base DN:");
207 setup_text[eLDAP_Base_DN] = _(
208 "Please enter the Base DN to search for authentication\n"
209 "(for example: dc=example,dc=com)\n");
211 setup_titles[eLDAP_Bind_DN] = _("LDAP bind DN:");
212 setup_text[eLDAP_Bind_DN] = _(
213 "Please enter the DN of an account to use for binding to the LDAP server for "
214 "performing queries. The account does not require any other privileges. If "
215 "your LDAP server allows anonymous queries, you can leave this blank."
216 "Please enter the DN of an account to use for binding to the LDAP server\n"
217 "for performing queries. The account does not require any other\n"
218 "privileges. If your LDAP server allows anonymous queries, you can\n"
219 "leave this blank.\n");
221 setup_titles[eLDAP_Bind_PW] = _("LDAP bind password:");
222 setup_text[eLDAP_Bind_PW] = _(
223 "If you entered a Bind DN in the previous question, you must now enter\n"
224 "the password associated with that account. Otherwise, you can leave this\n"
228 // Debug loading of locales... Strace does a better job though.
229 printf("Message catalog directory: %s\n", bindtextdomain("citadel-setup", LOCALEDIR"/locale"));
230 printf("Text domain: %s\n", textdomain("citadel-setup"));
231 printf("Text domain Charset: %s\n", bind_textdomain_codeset("citadel-setup","UTF8"));
234 for (i = 0; i < eMaxQuestions; i++)
235 printf("%s - %s\n", setup_titles[i], _(setup_titles[i]));
242 * print the actual stack frame.
244 void cit_backtrace(void)
246 #ifdef HAVE_BACKTRACE
247 void *stack_frames[50];
251 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
252 strings = backtrace_symbols(stack_frames, size);
253 for (i = 0; i < size; i++) {
255 fprintf(stderr, "%s\n", strings[i]);
257 fprintf(stderr, "%p\n", stack_frames[i]);
263 struct config config;
268 void cleanup(int exitcode)
270 // printf("Exitcode: %d\n", exitcode);
277 void title(const char *text)
279 if (setup_type == UI_TEXT) {
280 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<%s>\n", text);
286 int yesno(const char *question, int default_value)
292 switch (setup_type) {
296 printf("%s\n%s [%s] --> ",
299 ( default_value ? _("Yes") : _("No") )
301 if (fgets(buf, sizeof buf, stdin))
303 answer = tolower(buf[0]);
304 if ((buf[0]==0) || (buf[0]==13) || (buf[0]==10))
305 answer = default_value;
306 else if (answer == 'y')
308 else if (answer == 'n')
311 } while ((answer < 0) || (answer > 1));
315 sprintf(buf, "exec %s %s --yesno '%s' 15 75",
316 getenv("CTDL_DIALOG"),
317 ( default_value ? "" : "--defaultno" ),
335 void important_message(const char *title, const char *msgtext)
340 switch (setup_type) {
343 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");
344 printf(" %s \n\n%s\n\n", title, msgtext);
345 printf("%s", _("Press return to continue..."));
346 if (fgets(buf, sizeof buf, stdin));
350 sprintf(buf, "exec %s --msgbox '%s' 19 72",
351 getenv("CTDL_DIALOG"),
355 fprintf(stderr, _("failed to run the dialog command\n"));
358 fprintf(stderr, "%s\n", msgtext);
363 void important_msgnum(int msgnum)
365 important_message(_("Important Message"), setup_text[msgnum]);
368 void display_error(char *error_message_format, ...)
374 va_start(arg_ptr, error_message_format);
375 StrBufVAppendPrintf(Msg,
376 error_message_format,
380 important_message(_("Error"), ChrPtr(Msg));
384 void progress(char *text, long int curr, long int cmax)
386 static long dots_printed = 0L;
388 static FILE *fp = NULL;
391 switch (setup_type) {
395 printf("%s\n", text);
396 printf("....................................................");
397 printf("..........................\r");
400 } else if (curr == cmax) {
401 printf("\r%79s\n", "");
403 a = (curr * 100) / cmax;
406 while (dots_printed < a) {
416 sprintf(buf, "exec %s --gauge '%s' 7 72 0",
417 getenv("CTDL_DIALOG"),
419 fp = popen(buf, "w");
425 else if (curr == cmax) {
427 fprintf(fp, "100\n");
433 a = (curr * 100) / cmax;
435 fprintf(fp, "%ld\n", a);
449 * check_services_entry() -- Make sure "citadel" is in /etc/services
452 void check_services_entry(void)
458 if (getservbyname(SERVICE_NAME, PROTO_NAME) == NULL) {
459 for (i=0; i<=2; ++i) {
460 progress(_("Adding service entry..."), i, 2);
462 sfp = fopen("/etc/services", "a");
464 sprintf(errmsg, "%s /etc/services: %s", _("Cannot open"), strerror(errno));
465 display_error(errmsg);
467 fprintf(sfp, "%s 504/tcp\n", SERVICE_NAME);
479 * delete_inittab_entry() -- Remove obsolete /etc/inittab entry for Citadel
481 void delete_inittab_entry(void)
485 char looking_for[256];
487 char outfilename[32];
488 int changes_made = 0;
491 /* Determine the fully qualified path name of citserver */
492 snprintf(looking_for,
498 /* Now tweak /etc/inittab */
499 infp = fopen("/etc/inittab", "r");
502 /* If /etc/inittab does not exist, return quietly.
503 * Not all host platforms have it.
505 if (errno == ENOENT) {
509 /* Other errors might mean something really did go wrong.
511 sprintf(buf, "%s /etc/inittab: %s", _("Cannot open"), strerror(errno));
516 strcpy(outfilename, "/tmp/ctdlsetup.XXXXXX");
517 outfp = fdopen(mkstemp(outfilename), "w+");
519 sprintf(buf, "%s %s: %s", _("Cannot open"), outfilename, strerror(errno));
525 while (fgets(buf, sizeof buf, infp) != NULL) {
526 if (strstr(buf, looking_for) != NULL) {
527 rv = fwrite("#", 1, 1, outfp);
530 display_error("%s %s\n",
531 _("failed to modify inittab"),
536 rv = fwrite(buf, strlen(buf), 1, outfp);
539 display_error("%s %s\n",
540 _("failed to modify inittab"),
549 sprintf(buf, "/bin/mv -f %s /etc/inittab 2>/dev/null", outfilename);
551 rv = system("/sbin/init q 2>/dev/null");
560 * install_init_scripts() -- Try to configure to start Citadel at boot
562 void install_init_scripts(void)
564 struct stat etcinitd;
566 char *initfile = "/etc/init.d/citadel";
570 if ((stat("/etc/init.d/", &etcinitd) == -1) &&
573 if ((stat("/etc/rc.d/init.d/", &etcinitd) == -1) &&
575 initfile = CTDLDIR"/citadel.init";
577 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"));
661 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);
662 rv = system(command);
664 display_error(_("failed to set system V init links \n"));
666 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);
667 rv = system(command);
669 display_error(_("failed to set system V init links \n"));
675 * On systems which use xinetd, see if we can offer to install Citadel as
676 * the default telnet target.
678 void check_xinetd_entry(void) {
679 char *filename = "/etc/xinetd.d/telnet";
682 int already_citadel = 0;
685 fp = fopen(filename, "r+");
686 if (fp == NULL) return; /* Not there. Oh well... */
688 while (fgets(buf, sizeof buf, fp) != NULL) {
689 if (strstr(buf, "/citadel") != NULL) already_citadel = 1;
692 if (already_citadel) return; /* Already set up this way. */
694 /* Otherwise, prompt the user to create an entry. */
695 if (getenv("CREATE_XINETD_ENTRY") != NULL) {
696 if (strcasecmp(getenv("CREATE_XINETD_ENTRY"), "yes")) {
701 snprintf(buf, sizeof buf,
702 _("Setup can configure the \"xinetd\" service to automatically\n"
703 "connect incoming telnet sessions to Citadel, bypassing the\n"
704 "host system login: prompt. Would you like to do this?\n"
706 if (yesno(buf, 1) == 0) {
711 fp = fopen(filename, "w");
713 "# description: telnet service for Citadel users\n"
718 " socket_type = stream\n"
721 " server = /usr/sbin/in.telnetd\n"
722 " server_args = -h -L %s/citadel\n"
723 " log_on_failure += USERID\n"
728 /* Now try to restart the service */
729 rv = system("/etc/init.d/xinetd restart >/dev/null 2>&1");
731 display_error(_("failed to restart xinetd.\n"));
737 * Offer to disable other MTA's
739 void disable_other_mta(const char *mta) {
745 sprintf(buf, "/bin/ls -l /etc/rc*.d/S*%s 2>/dev/null; "
746 "/bin/ls -l /etc/rc.d/rc*.d/S*%s 2>/dev/null",
748 fp = popen(buf, "r");
749 if (fp == NULL) return;
751 while (fgets(buf, sizeof buf, fp) != NULL) {
755 if (lines == 0) return; /* Nothing to do. */
758 /* Offer to replace other MTA with the vastly superior Citadel :) */
760 snprintf(buf, sizeof buf,
761 "%s \"%s\" %s%s%s%s%s%s%s",
762 _("You appear to have the "),
765 "running on your system. If you want Citadel mail\n"
768 _(" you will have to manually integrate\n"
769 "them. It is preferable to disable "),
771 _(", and use Citadel's\n"
772 "SMTP, POP3, and IMAP services.\n\n"
775 _("so that Citadel has access to ports\n"
776 "25, 110, and 143?\n")
778 if (yesno(buf, 1) == 0) {
783 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);
786 display_error("%s %s.\n", _("failed to disable other mta"), mta);
788 sprintf(buf, "/etc/init.d/%s stop >/dev/null 2>&1", mta);
791 display_error(" %s.\n", _("failed to disable other mta"), mta);
794 const char *other_mtas[] = {
795 "courier-authdaemon",
818 void disable_other_mtas(void)
821 if ((getenv("ACT_AS_MTA") == NULL) ||
822 (getenv("ACT_AS_MTA") &&
823 strcasecmp(getenv("ACT_AS_MTA"), "yes") == 0)) {
824 /* Offer to disable other MTA's on the system. */
825 while (!IsEmptyStr(other_mtas[i]))
827 disable_other_mta(other_mtas[i]);
834 * Check to see if our server really works. Returns 0 on success.
836 int test_server(char *relhomestr, int relhome) {
843 /* Generate a silly little cookie. We're going to write it out
844 * to the server and try to get it back. The cookie does not
845 * have to be secret ... just unique.
847 sprintf(cookie, "--test--%d--", getpid());
850 sprintf(cmd, "%s/sendcommand -h%s ECHO %s 2>&1",
855 sprintf(cmd, "%s/sendcommand ECHO %s 2>&1",
859 fp = popen(cmd, "r");
860 if (fp == NULL) return(errno);
862 while (fgets(buf, sizeof buf, fp) != NULL) {
864 && (strstr(buf, cookie) != NULL) ) {
876 void strprompt(const char *prompt_title, const char *prompt_text, char *Target, char *DefValue)
880 char dialog_result[PATH_MAX];
884 strcpy(setupmsg, "");
886 switch (setup_type) {
889 printf("\n%s\n", prompt_text);
890 printf("%s\n%s\n", _("This is currently set to:"), Target);
891 printf("%s\n", _("Enter new value or press return to leave unchanged:"));
892 if (fgets(buf, sizeof buf, stdin)){
893 buf[strlen(buf) - 1] = 0;
895 if (!IsEmptyStr(buf))
900 CtdlMakeTempFileName(dialog_result, sizeof dialog_result);
901 sprintf(buf, "exec %s --inputbox '%s' 19 72 '%s' 2>%s",
902 getenv("CTDL_DIALOG"),
908 fprintf(stderr, "failed to run Dialog.\n");
910 fp = fopen(dialog_result, "r");
912 if (fgets(Target, sizeof buf, fp)) {
913 if (Target[strlen(Target)-1] == 10) {
914 Target[strlen(Target)-1] = 0;
918 unlink(dialog_result);
922 if (*DefValue != '\0')
923 strcpy(Target, DefValue);
928 void set_bool_val(int msgpos, int *ip, char *DefValue)
930 title(setup_titles[msgpos]);
931 *ip = yesno(setup_text[msgpos], *ip);
934 void set_str_val(int msgpos, char *Target, char *DefValue)
936 strprompt(setup_titles[msgpos],
942 void set_int_val(int msgpos, int *ip, char *DefValue)
945 snprintf(buf, sizeof buf, "%d", (int) *ip);
946 set_str_val(msgpos, buf, DefValue);
951 void set_char_val(int msgpos, char *ip, char *DefValue)
954 snprintf(buf, sizeof buf, "%d", (int) *ip);
955 set_str_val(msgpos, buf, DefValue);
956 *ip = (char) atoi(buf);
960 void set_long_val(int msgpos, long int *ip, char *DefValue)
963 snprintf(buf, sizeof buf, "%ld", *ip);
964 set_str_val(msgpos, buf, DefValue);
969 void edit_value(int curr)
973 char ctdluidname[256];
976 if (setup_type == UI_SILENT)
978 Value = getenv(EnvNames[curr]);
987 set_str_val(curr, config.c_sysadm, Value);
991 set_str_val(curr, admin_pass, Value);
995 if (setup_type == UI_SILENT)
998 config.c_ctdluid = atoi(Value);
1004 config.c_ctdluid = 0; /* XXX Windows hack, prob. insecure */
1006 i = config.c_ctdluid;
1009 set_int_val(curr, &i, Value);
1010 config.c_ctdluid = i;
1013 strcpy(ctdluidname, pw->pw_name);
1014 set_str_val(curr, ctdluidname, Value);
1015 pw = getpwnam(ctdluidname);
1017 config.c_ctdluid = pw->pw_uid;
1019 else if (atoi(ctdluidname) > 0) {
1020 config.c_ctdluid = atoi(ctdluidname);
1028 set_str_val(curr, config.c_ip_addr, Value);
1032 set_int_val(curr, &config.c_port_number, Value);
1036 if (setup_type == UI_SILENT)
1039 config.c_auth_mode = AUTHMODE_NATIVE;
1043 if ((strcasecmp(auth, "yes") == 0) ||
1044 (strcasecmp(auth, "host") == 0))
1046 config.c_auth_mode = AUTHMODE_HOST;
1048 else if (strcasecmp(auth, "ldap") == 0){
1049 config.c_auth_mode = AUTHMODE_LDAP;
1051 else if ((strcasecmp(auth, "ldap_ad") == 0) ||
1052 (strcasecmp(auth, "active directory") == 0)){
1053 config.c_auth_mode = AUTHMODE_LDAP_AD;
1058 set_int_val(curr, &config.c_auth_mode, Value);
1063 set_str_val(curr, config.c_ldap_host, Value);
1067 if (config.c_ldap_port == 0) {
1068 config.c_ldap_port = 389;
1070 set_int_val(curr, &config.c_ldap_port, Value);
1074 set_str_val(curr, config.c_ldap_base_dn, Value);
1078 set_str_val(curr, config.c_ldap_bind_dn, Value);
1082 set_str_val(curr, config.c_ldap_bind_pw, Value);
1089 * (re-)write the config data to disk
1091 void write_config_to_disk(void)
1097 if ((fd = creat(file_citadel_config, S_IRUSR | S_IWUSR)) == -1) {
1098 display_error("%s citadel.config [%s][%s]\n", _("setup: cannot open"), file_citadel_config, strerror(errno));
1101 fp = fdopen(fd, "wb");
1103 display_error("%s citadel.config [%s][%s]\n", _("setup: cannot open"), file_citadel_config, strerror(errno));
1107 rv = fwrite((char *) &config, sizeof(struct config), 1, fp);
1110 display_error("%s citadel.config [%s][%s]\n", _("setup: cannot write"), file_citadel_config, strerror(errno));
1119 * Figure out what type of user interface we're going to use
1121 int discover_ui(void)
1124 /* Use "dialog" if we have it */
1125 if (getenv("CTDL_DIALOG") != NULL) {
1135 * Strip "db" entries out of /etc/nsswitch.conf
1144 int file_changed = 0;
1145 char new_filename[64];
1148 fp_read = fopen(NSSCONF, "r");
1149 if (fp_read == NULL) {
1153 strcpy(new_filename, "/tmp/ctdl_fixnss_XXXXXX");
1154 fd_write = mkstemp(new_filename);
1160 while (fgets(buf, sizeof buf, fp_read) != NULL) {
1161 strcpy(buf_nc, buf);
1162 for (i=0; i<strlen(buf_nc); ++i) {
1163 if (buf_nc[i] == '#') {
1167 for (i=0; i<strlen(buf_nc); ++i) {
1168 if (!strncasecmp(&buf_nc[i], "db", 2)) {
1170 if ((isspace(buf_nc[i+2])) || (buf_nc[i+2]==0)) {
1172 strcpy(&buf_nc[i], &buf_nc[i+2]);
1173 strcpy(&buf[i], &buf[i+2]);
1175 strcpy(&buf_nc[i], &buf_nc[i+1]);
1176 strcpy(&buf[i], &buf[i+1]);
1182 if (write(fd_write, buf, strlen(buf)) != strlen(buf)) {
1185 unlink(new_filename);
1192 if (!file_changed) {
1193 unlink(new_filename);
1197 snprintf(question, sizeof question,
1200 "/etc/nsswitch.conf is configured to use the 'db' module for\n"
1201 "one or more services. This is not necessary on most systems,\n"
1202 "and it is known to crash the Citadel server when delivering\n"
1203 "mail to the Internet.\n"
1205 "Do you want this module to be automatically disabled?\n"
1210 if (yesno(question, 1)) {
1211 sprintf(buf, "/bin/mv -f %s %s", new_filename, NSSCONF);
1214 fprintf(stderr, "failed to edit %s.\n", NSSCONF);
1216 chmod(NSSCONF, 0644);
1218 unlink(new_filename);
1221 void check_init_script (char *relhome)
1227 * If we're running on SysV, install init scripts.
1229 if (!access("/var/run", W_OK)) {
1231 if (getenv("NO_INIT_SCRIPTS") == NULL) {
1232 install_init_scripts();
1235 if (!access("/etc/init.d/citadel", X_OK)) {
1236 rv = system("/etc/init.d/citadel start");
1238 fprintf(stderr, "failed to call our initscript.");
1242 if (test_server(relhome, enable_home) == 0) {
1246 if (config.c_auth_mode == AUTHMODE_NATIVE) {
1247 snprintf (admin_cmd, sizeof(admin_cmd), "%s/sendcommand \"CREU %s|%s\" 2>&1",
1248 ctdl_sbin_dir, config.c_sysadm, admin_pass);
1249 fp = popen(admin_cmd, "r");
1251 while (fgets(buf, sizeof buf, fp) != NULL)
1253 if ((atol(buf) == 574) || (atol(buf) == 200))
1259 if (found_it == 0) {
1260 important_message("Error","Setup failed to create your admin user");
1264 if (setup_type != UI_SILENT)
1265 important_message(_("Setup finished"),
1266 _("Setup of the Citadel server is complete.\n"
1267 "If you will be using WebCit, please run its\n"
1268 "setup program now; otherwise, run './citadel'\n"
1272 important_message(_("Setup failed"),
1273 _("Setup is finished, but the Citadel server failed to start.\n"
1274 "Go back and check your configuration.\n")
1281 important_message(_("Setup finished"),
1282 _("Setup is finished. You may now start the server."));
1288 #define GetDefaultVALINT(CFGNAME, DEFL) GetDefaultValInt(&config.CFGNAME, "CITADEL_"#CFGNAME, DEFL)
1289 void GetDefaultValInt(int *WhereTo, const char *VarName, int DefVal)
1292 if (*WhereTo == 0) *WhereTo = DefVal;
1294 if ((setup_type == UI_SILENT) &&
1295 (ch = getenv(VarName), ch != NULL))
1297 *WhereTo = atoi(ch);
1300 #define GetDefaultVALCHAR(CFGNAME, DEFL) GetDefaultValChar(&config.CFGNAME, "CITADEL_"#CFGNAME, DEFL)
1301 void GetDefaultValChar(char *WhereTo, const char *VarName, char DefVal)
1304 if (*WhereTo == 0) *WhereTo = DefVal;
1306 if ((setup_type == UI_SILENT) &&
1307 (ch = getenv(VarName), ch != NULL))
1309 *WhereTo = atoi(ch);
1312 #define GetDefaultVALSTR(CFGNAME, DEFL) GetDefaultValStr(&config.CFGNAME[0], sizeof(config.CFGNAME), "CITADEL_"#CFGNAME, DEFL)
1313 void GetDefaultValStr(char *WhereTo, size_t nMax, const char *VarName, const char *DefVal)
1316 if (*WhereTo == '\0')
1317 safestrncpy(WhereTo, DefVal, nMax);
1319 if ((setup_type == UI_SILENT) &&
1320 (ch = getenv(VarName), ch != NULL))
1322 safestrncpy(WhereTo, ch, nMax);
1327 void set_default_values(void)
1330 struct utsname my_utsname;
1333 /* Determine our host name, in case we need to use it as a default */
1336 /* set some sample/default values in place of blanks... */
1337 GetDefaultVALSTR(c_nodename, my_utsname.nodename);
1338 strtok(config.c_nodename, ".");
1339 if (IsEmptyStr(config.c_fqdn) ) {
1340 if ((he = gethostbyname(my_utsname.nodename)) != NULL) {
1341 safestrncpy(config.c_fqdn, he->h_name, sizeof config.c_fqdn);
1343 safestrncpy(config.c_fqdn, my_utsname.nodename, sizeof config.c_fqdn);
1346 GetDefaultVALSTR(c_humannode, _("My System"));
1347 GetDefaultVALSTR(c_phonenum, _("US 800 555 1212"));
1349 GetDefaultVALCHAR(c_initax, 4);
1351 GetDefaultVALSTR(c_moreprompt, "<more>");
1352 GetDefaultVALSTR(c_twitroom, "Trashcan");
1353 GetDefaultVALSTR(c_baseroom, BASEROOM);
1354 GetDefaultVALSTR(c_aideroom, "Aide");
1355 GetDefaultVALINT(c_port_number, 504);
1357 GetDefaultVALINT(c_sleeping, 900);
1359 if (config.c_ctdluid == 0) {
1360 pw = getpwnam("citadel");
1362 config.c_ctdluid = pw->pw_uid;
1365 if (config.c_ctdluid == 0) {
1366 pw = getpwnam("bbs");
1368 config.c_ctdluid = pw->pw_uid;
1371 if (config.c_ctdluid == 0) {
1372 pw = getpwnam("guest");
1374 config.c_ctdluid = pw->pw_uid;
1377 if (config.c_createax == 0) {
1378 config.c_createax = 3;
1381 * Negative values for maxsessions are not allowed.
1383 if (config.c_maxsessions < 0) {
1384 config.c_maxsessions = 0;
1386 /* We need a system default message expiry policy, because this is
1387 * the top level and there's no 'higher' policy to fall back on.
1388 * By default, do not expire messages at all.
1390 if (config.c_ep.expire_mode == 0) {
1391 config.c_ep.expire_mode = EXPIRE_MANUAL;
1392 config.c_ep.expire_value = 0;
1396 * Default port numbers for various services
1398 GetDefaultVALINT(c_smtp_port, 25);
1399 GetDefaultVALINT(c_pop3_port, 110);
1400 GetDefaultVALINT(c_imap_port, 143);
1401 GetDefaultVALINT(c_msa_port, 587);
1402 GetDefaultVALINT(c_smtps_port, 465);
1403 GetDefaultVALINT(c_pop3s_port, 995);
1404 GetDefaultVALINT(c_imaps_port, 993);
1405 GetDefaultVALINT(c_pftcpdict_port, -1);
1406 GetDefaultVALINT(c_managesieve_port, 2020);
1407 GetDefaultVALINT(c_xmpp_c2s_port, 5222);
1408 GetDefaultVALINT(c_xmpp_s2s_port, 5269);
1412 void get_config (void)
1419 * What we're going to try to do here is append a whole bunch of
1420 * nulls to the citadel.config file, so we can keep the old config
1421 * values if they exist, but if the file is missing or from an
1422 * earlier version with a shorter config structure, when setup tries
1423 * to read the old config parameters, they'll all come up zero.
1424 * The length of the config file will be set to what it's supposed
1425 * to be when we rewrite it, because we replace the old file with a
1426 * completely new copy.
1428 if ((a = open(file_citadel_config, O_WRONLY | O_CREAT | O_APPEND,
1429 S_IRUSR | S_IWUSR)) == -1) {
1430 display_error("%s citadel.config [%s][%s]\n", _("setup: cannot append"), file_citadel_config, strerror(errno));
1433 fp = fdopen(a, "ab");
1435 display_error("%s citadel.config [%s][%s]\n", _("setup: cannot append"), file_citadel_config, strerror(errno));
1438 for (a = 0; a < sizeof(struct config); ++a) {
1443 /* now we re-open it, and read the old or blank configuration */
1444 fp = fopen(file_citadel_config, "rb");
1446 display_error("%s citadel.config [%s][%s]\n", _("setup: cannot open"), file_citadel_config, strerror(errno));
1450 rv = fread((char *) &config, sizeof(struct config), 1, fp);
1452 display_error("%s citadel.config [%s][%s]\n", _("setup: cannot write"), file_citadel_config, strerror(errno));
1457 int main(int argc, char *argv[])
1462 int old_setup_level = 0;
1466 char relhome[PATH_MAX]="";
1467 char ctdldir[PATH_MAX]=CTDLDIR;
1472 /* set an invalid setup type */
1475 /* Check to see if we're running the web installer */
1476 if (getenv("CITADEL_INSTALLER") != NULL) {
1477 using_web_installer = 1;
1480 /* parse command line args */
1481 for (a = 0; a < argc; ++a) {
1482 if (!strncmp(argv[a], "-u", 2)) {
1483 strcpy(aaa, argv[a]);
1484 strcpy(aaa, &aaa[2]);
1485 setup_type = atoi(aaa);
1487 else if (!strcmp(argv[a], "-i")) {
1490 else if (!strcmp(argv[a], "-q")) {
1491 setup_type = UI_SILENT;
1493 else if (!strncmp(argv[a], "-h", 2)) {
1494 relh=argv[a][2]!='/';
1496 safestrncpy(ctdl_home_directory, &argv[a][2], sizeof ctdl_home_directory);
1498 safestrncpy(relhome, &argv[a][2], sizeof relhome);
1505 calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
1508 /* If a setup type was not specified, try to determine automatically
1509 * the best one to use out of all available types.
1511 if (setup_type < 0) {
1512 setup_type = discover_ui();
1514 if (info_only == 1) {
1515 important_message(_("Citadel Setup"), CITADEL);
1519 enable_home = ( relh | home );
1521 if (chdir(ctdl_run_dir) != 0) {
1522 display_error(_("Citadel Setup"),
1524 _("The directory you specified does not exist"),
1530 /* Try to stop Citadel if we can */
1531 if (!access("/etc/init.d/citadel", X_OK)) {
1532 rv = system("/etc/init.d/citadel stop");
1534 fprintf(stderr, _("failed to stop us using the initscript.\n"));
1537 /* Make sure Citadel is not running. */
1538 if (test_server(relhome, enable_home) == 0) {
1539 important_message(_("Citadel Setup"),
1540 _("The Citadel service is still running.\n"
1541 "Please stop the service manually and run "
1547 switch (setup_type) {
1552 _("Citadel setup program"));
1559 set_default_values();
1561 /* Go through a series of dialogs prompting for config info */
1562 for (curr = 1; curr <= MAXSETUP; ++curr) {
1564 if ((curr == 6) && (config.c_auth_mode != AUTHMODE_LDAP) && (config.c_auth_mode != AUTHMODE_LDAP_AD)) {
1565 curr += 5; /* skip LDAP questions if we're not authenticating against LDAP */
1569 /***** begin version update section *****/
1571 old_setup_level = config.c_setup_level;
1573 if (old_setup_level == 0) {
1577 if (old_setup_level < 555) {
1580 _("This Citadel installation is too old to be upgraded.")
1584 write_config_to_disk();
1586 old_setup_level = config.c_setup_level;
1588 /***** end of version update section *****/
1591 config.c_setup_level = REV_LEVEL;
1593 if ((pw = getpwuid(config.c_ctdluid)) == NULL) {
1599 create_run_directories(config.c_ctdluid, gid);
1601 write_config_to_disk();
1603 if ( ((setup_type == UI_SILENT)
1604 && (getenv("ALTER_ETC_SERVICES")!=NULL))
1605 || (setup_type != UI_SILENT)
1607 check_services_entry(); /* Check /etc/services */
1611 delete_inittab_entry(); /* Remove obsolete /etc/inittab entry */
1612 check_xinetd_entry(); /* Check /etc/xinetd.d/telnet */
1613 disable_other_mtas(); /* Offer to disable other MTAs */
1614 fixnss(); /* Check for the 'db' nss and offer to disable it */
1617 progress(_("Setting file permissions"), 1, 3);
1618 rv = chown(file_citadel_config, config.c_ctdluid, gid);
1619 progress(_("Setting file permissions"), 2, 3);
1620 rv = chmod(file_citadel_config, S_IRUSR | S_IWUSR);
1621 progress(_("Setting file permissions"), 3, 3);
1623 check_init_script(relhome);