1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2012 */
6 /* See the file NOTICE for conditions of use and distribution. */
9 /*************************************************
10 * Build configuration header for Exim *
11 *************************************************/
13 /* This auxiliary program builds the file config.h by the following
16 First, it determines the size of off_t and time_t variables, and generates
17 macro code to define OFF_T_FMT and TIME_T_FMT as suitable formats, if they are
18 not already defined in the system-specific header file.
20 Then it reads Makefile, looking for certain OS-specific definitions which it
21 uses to define some specific macros. Finally, it reads the defaults file
24 The defaults file contains normal C #define statements for various macros; if
25 the name of a macro is found in the environment, the environment value replaces
26 the default. If the default #define does not contain any value, then that macro
27 is not copied to the created file unless there is some value in the
30 This program is compiled and run as part of the Make process and is not
31 normally called independently. */
38 #include <sys/types.h>
52 static const char *db_opts[] = { "", "USE_DB", "USE_GDBM", "USE_TDB" };
54 static int have_ipv6 = 0;
55 static int have_iconv = 0;
57 static char errno_quota[256];
58 static char ostype[256];
61 /* If any entry is an initial substring of another, the longer one must
64 static have_item have_list[] = {
65 { "HAVE_IPV6", &have_ipv6 },
66 { "HAVE_ICONV", &have_iconv },
70 static save_item save_list[] = {
71 { "ERRNO_QUOTA", errno_quota },
78 /* Subroutine to check a string for precisely one instance of "%s". If not,
82 check_percent_ess(char *value, char *name)
85 char *p = strstr(value, "%s");
86 if (p != NULL) OK = strstr(p+2, "%s") == NULL;
89 printf("\n*** \"%s\" (%s) must contain precisely one occurrence of\n"
90 "*** \"%%s\". Please review your build-time configuration.\n\n/", value,
100 main(int argc, char **argv)
102 off_t test_off_t = 0;
103 time_t test_time_t = 0;
104 #if ! (__STDC_VERSION__ >= 199901L)
105 size_t test_size_t = 0;
106 ssize_t test_ssize_t = 0;
107 unsigned long test_ulong_t = 0L;
109 long test_long_t = 0;
112 int last_initial = 'A';
115 int in_local_makefile = 0;
116 int use_which_db = 0;
117 int use_which_db_in_local_makefile = 0;
118 int support_crypteq = 0;
123 printf("*** Buildconfig: called with incorrect arguments\n");
127 new = fopen("config.h", "wb");
130 printf("*** Buildconfig: failed to open config.h for output\n");
134 printf("Building configuration file config.h\n");
136 fprintf(new, "/*************************************************\n");
137 fprintf(new, "* Configuration header for Exim *\n");
138 fprintf(new, "*************************************************/\n\n");
140 fprintf(new, "/* This file was automatically generated from Makefile and "
141 "config.h.defaults,\n");
142 fprintf(new, "using values specified in the configuration file Local/Makefile.\n");
143 fprintf(new, "Do not edit it. Instead, edit Local/Makefile and "
144 "rerun make. */\n\n");
146 /* First, deal with the printing format for off_t variables. We assume that if
147 the size of off_t is greater than 4, "%lld" will be available as a format for
148 printing long long variables, and there will be support for the long long type.
149 This assumption is known to be OK for the common operating systems. */
151 fprintf(new, "#ifndef OFF_T_FMT\n");
152 if (sizeof(test_off_t) > sizeof(test_long_t))
154 fprintf(new, "#define OFF_T_FMT \"%%lld\"\n");
155 fprintf(new, "#define LONGLONG_T long long int\n");
159 fprintf(new, "#define OFF_T_FMT \"%%ld\"\n");
160 fprintf(new, "#define LONGLONG_T long int\n");
162 fprintf(new, "#endif\n\n");
164 /* Now do the same thing for time_t variables. If the length is greater than
165 4, we want to assume long long support (even if off_t was less than 4). If the
166 length is 4 or less, we can leave LONGLONG_T to whatever was defined above for
169 fprintf(new, "#ifndef TIME_T_FMT\n");
170 if (sizeof(test_time_t) > sizeof(test_long_t))
172 fprintf(new, "#define TIME_T_FMT \"%%lld\"\n");
173 fprintf(new, "#undef LONGLONG_T\n");
174 fprintf(new, "#define LONGLONG_T long long int\n");
178 fprintf(new, "#define TIME_T_FMT \"%%ld\"\n");
180 fprintf(new, "#endif\n\n");
182 /* And for sizeof() results, size_t, which should with C99 be just %zu, deal
183 with C99 not being ubiquitous yet. Unfortunately. Assume ssize_t is same
184 size as size_t on C99; if someone comes up with a version where it's not, fix
187 #if __STDC_VERSION__ >= 199901L
188 fprintf(new, "#define SIZE_T_FMT \"%%zu\"\n");
189 fprintf(new, "#define SSIZE_T_FMT \"%%zd\"\n");
191 if (sizeof(test_size_t) > sizeof (test_ulong_t))
192 fprintf(new, "#define SIZE_T_FMT \"%%llu\"\n");
194 fprintf(new, "#define SIZE_T_FMT \"%%lu\"\n");
195 if (sizeof(test_ssize_t) > sizeof(test_long_t))
196 fprintf(new, "#define SSIZE_T_FMT \"%%lld\"\n");
198 fprintf(new, "#define SSIZE_T_FMT \"%%ld\"\n");
201 /* Now search the makefile for certain settings */
203 base = fopen("Makefile", "rb");
206 printf("*** Buildconfig: failed to open Makefile\n");
211 errno_quota[0] = 0; /* no over-riding value set */
212 ostype[0] = 0; /* just in case */
215 while (fgets(buffer, sizeof(buffer), base) != NULL)
220 char *p = buffer + (int)strlen(buffer);
222 while (p > buffer && isspace((unsigned char)p[-1])) p--;
225 while (isspace((unsigned char)*p)) p++;
227 /* Notice when we hit the user's makefile */
229 if (strcmp(p, "# From Local/Makefile") == 0)
231 in_local_makefile = 1;
235 /* Remember the last DB option setting. If we hit two in the user's
236 Makefile, complain. */
238 for (i = 1; i < sizeof(db_opts)/sizeof(char *); i++)
240 int len = (int)strlen(db_opts[i]);
241 if (strncmp(p, db_opts[i], len) == 0 && (p[len] == ' ' || p[len] == '='))
243 if (in_local_makefile)
245 if (use_which_db_in_local_makefile)
247 printf("*** Only one of USE_DB, USE_GDBM, or USE_TDB should be "
248 "defined in Local/Makefile\n");
251 use_which_db_in_local_makefile = 1;
257 if (i < sizeof(db_opts)/sizeof(char *)) continue;
259 /* Items where we just save a boolean */
261 for (h = have_list; h->name != NULL; h++)
263 int len = (int)strlen(h->name);
264 if (strncmp(p, h->name, len) == 0)
267 while (isspace((unsigned char)*p)) p++;
270 printf("*** Buildconfig: syntax error in Makefile line %d\n", linecount);
273 while (isspace((unsigned char)*p)) p++;
274 if (strcmp(p, "YES") == 0 || strcmp(p, "yes") == 0) *(h->flag) = 1;
275 else *(h->flag) = 0; /* Must reset in case multiple instances */
280 if (h->name != NULL) continue;
282 /* Items where we save the complete string */
284 for (s = save_list; s->name != NULL; s++)
286 int len = (int)strlen(s->name);
287 if (strncmp(p, s->name, len) == 0)
290 while (isspace((unsigned char)*p)) p++;
293 printf("*** Buildconfig: syntax error in Makefile line %d\n", linecount);
296 while (isspace((unsigned char)*p)) p++;
302 fprintf(new, "#define HAVE_IPV6 %s\n",
303 have_ipv6? "TRUE" : "FALSE");
305 fprintf(new, "#define HAVE_ICONV %s\n",
306 have_iconv? "TRUE" : "FALSE");
308 if (errno_quota[0] != 0)
309 fprintf(new, "\n#define ERRNO_QUOTA %s\n", errno_quota);
311 if (strcmp(cc, "gcc") == 0 &&
312 (strstr(ostype, "IRIX") != NULL || strstr(ostype, "AIX") != NULL))
314 fprintf(new, "\n/* This switch includes the code to fix the inet_ntoa() */");
315 fprintf(new, "\n/* bug when using gcc on an IRIX or AIX system. */");
316 fprintf(new, "\n#define USE_INET_NTOA_FIX");
323 /* Now handle the macros listed in the defaults */
325 base = fopen("../src/config.h.defaults", "rb");
328 printf("*** Buildconfig: failed to open ../src/config.h.defaults\n");
333 while (fgets(buffer, sizeof(buffer), base) != NULL)
341 while (*p == ' ' || *p == '\t') p++;
343 if (strncmp(p, "#ifdef ", 7) == 0
344 || strncmp(p, "#ifndef ", 8) == 0
345 || strncmp(p, "#if ", 4) == 0
346 || strncmp(p, "#endif", 6) == 0
353 if (strncmp(p, "#define ", 8) != 0) continue;
356 while (*p == ' ' || *p == '\t') p++;
358 if (*p < last_initial) fprintf(new, "\n");
361 while (*p && (isalnum((unsigned char)*p) || *p == '_')) *q++ = *p++;
364 /* USE_DB, USE_GDBM, and USE_TDB are special cases. We want to have only
365 one of them set. The scan of the Makefile has saved which was the last one
368 for (i = 1; i < sizeof(db_opts)/sizeof(char *); i++)
370 if (strcmp(name, db_opts[i]) == 0)
372 if (use_which_db == i)
373 fprintf(new, "#define %s %.*syes\n", db_opts[i],
374 21 - (int)strlen(db_opts[i]), " ");
376 fprintf(new, "/* %s not set */\n", name);
380 if (i < sizeof(db_opts)/sizeof(char *)) continue;
382 /* EXIM_USER is a special case. We look in the environment for EXIM_USER or
383 EXIM_UID (the latter for backward compatibility with Exim 3). If the value is
384 not numeric, we look up the user, and default the GID if found. Otherwise,
385 EXIM_GROUP or EXIM_GID must be in the environment. */
387 if (strcmp(name, "EXIM_UID") == 0)
393 char *username = NULL;
394 char *groupname = NULL;
396 char *user = getenv("EXIM_USER");
397 char *group = getenv("EXIM_GROUP");
399 if (user == NULL) user = getenv("EXIM_UID");
400 if (group == NULL) group = getenv("EXIM_GID");
404 printf("\n*** EXIM_USER has not been defined in any of the Makefiles in "
405 "the\n \"Local\" directory. Please review your build-time "
406 "configuration.\n\n");
410 while (isspace((unsigned char)(*user))) user++;
413 printf("\n*** EXIM_USER is defined as an empty string in one of the "
414 "files\n in the \"Local\" directory. Please review your build-time"
415 "\n configuration.\n\n");
419 for (s = user; *s != 0; s++)
421 if (iscntrl((unsigned char)(*s)))
423 printf("\n*** EXIM_USER contains the control character 0x%02X in one "
424 "of the files\n in the \"Local\" directory. Please review your "
425 "build-time\n configuration.\n\n", *s);
430 /* Numeric uid given */
432 if (user[strspn(user, "0123456789")] == 0)
434 uid = (uid_t)atoi(user);
437 /* User name given. Normally, we look up the uid right away. However,
438 people building binary distributions sometimes want to retain the name till
439 runtime. This is supported if the name begins "ref:". */
441 else if (strncmp(user, "ref:", 4) == 0)
444 while (isspace(*user)) user++;
452 struct passwd *pw = getpwnam(user);
455 printf("\n*** User \"%s\" (specified in one of the Makefiles) does not "
456 "exist.\n Please review your build-time configuration.\n\n",
466 /* Use explicit group if set. */
470 while (isspace((unsigned char)(*group))) group++;
473 printf("\n*** EXIM_GROUP is defined as an empty string in one of "
474 "the files in the\n \"Local\" directory. ");
477 printf("If you want the Exim group to be taken from the\n "
478 "password data for the Exim user, just remove the EXIM_GROUP "
479 "setting.\n Otherwise, p");
481 else printf("EXIM_USER is defined numerically, so there is no"
482 "\n default for EXIM_GROUP and you must set it explicitly.\n P");
483 printf("lease review your build-time configuration.\n\n");
487 for (s = group; *s != 0; s++)
489 if (iscntrl((unsigned char)(*s)))
491 printf("\n*** EXIM_GROUP contains the control character 0x%02X in one "
492 "of the files\n in the \"Local\" directory. Please review your "
493 "build-time\n configuration.\n\n", *s);
498 /* Group name given. This may be by reference or to be looked up now,
501 if (strncmp(group, "ref:", 4) == 0)
504 while (isspace(*group)) group++;
508 else if (username != NULL)
513 else if (group[strspn(group, "0123456789")] == 0)
515 gid = (gid_t)atoi(group);
520 struct group *gr = getgrnam(group);
523 printf("\n*** Group \"%s\" (specified in one of the Makefiles) does "
524 "not exist.\n Please review your build-time configuration.\n\n",
532 /* Else trouble unless found in passwd file with user */
536 printf("\n*** No group set for Exim. Please review your build-time "
537 "configuration.\n\n");
541 /* security sanity checks
542 if ref: is being used, we can never be sure, but we can take reasonable
543 steps to filter out the most obvious ones. */
545 if ((!uid_not_set && uid == 0) ||
546 ((username != NULL) && (
547 (strcmp(username, "root") == 0) ||
548 (strcmp(username, "toor") == 0) )))
550 printf("\n*** Exim's internal user must not be root.\n\n");
554 /* Output user and group names or uid/gid. When names are set, uid/gid
555 are set to zero but will be replaced at runtime. */
557 if (username != NULL)
558 fprintf(new, "#define EXIM_USERNAME \"%s\"\n", username);
559 if (groupname != NULL)
560 fprintf(new, "#define EXIM_GROUPNAME \"%s\"\n", groupname);
562 fprintf(new, "#define EXIM_UID %d\n", (int)uid);
563 fprintf(new, "#define EXIM_GID %d\n", (int)gid);
567 /* CONFIGURE_OWNER and CONFIGURE_GROUP are special cases. We look in the
568 environment for first. If the value is not numeric, we look up the user or
569 group. A lot of this code is similar to that for EXIM_USER, but it's easier
570 to keep it separate. */
572 if (strcmp(name, "CONFIGURE_OWNER") == 0 ||
573 strcmp(name, "CONFIGURE_GROUP") == 0)
575 int isgroup = name[10] == 'G';
579 const char *username = NULL;
580 const char *user = getenv(name);
582 if (user == NULL) user = "";
583 while (isspace((unsigned char)(*user))) user++;
586 fprintf(new, "/* %s not set */\n", name);
590 for (s = user; *s != 0; s++)
592 if (iscntrl((unsigned char)(*s)))
594 printf("\n*** %s contains the control character 0x%02X in "
595 "one of the files\n in the \"Local\" directory. Please review "
596 "your build-time\n configuration.\n\n", name, *s);
601 /* Numeric uid given */
603 if (user[strspn(user, "0123456789")] == 0)
606 gid = (gid_t)atoi(user);
608 uid = (uid_t)atoi(user);
611 /* Name given. Normally, we look up the uid or gid right away. However,
612 people building binary distributions sometimes want to retain the name till
613 runtime. This is supported if the name begins "ref:". */
615 else if (strncmp(user, "ref:", 4) == 0)
618 while (isspace(*user)) user++;
623 struct group *gr = getgrnam(user);
626 printf("\n*** Group \"%s\" (specified in one of the Makefiles) does not "
627 "exist.\n Please review your build-time configuration.\n\n",
636 struct passwd *pw = getpwnam(user);
639 printf("\n*** User \"%s\" (specified in one of the Makefiles) does not "
640 "exist.\n Please review your build-time configuration.\n\n",
647 /* Output user and group names or uid/gid. When names are set, uid/gid
648 are set to zero but will be replaced at runtime. */
650 if (username != NULL)
653 fprintf(new, "#define CONFIGURE_GROUPNAME \"%s\"\n", username);
655 fprintf(new, "#define CONFIGURE_OWNERNAME \"%s\"\n", username);
659 fprintf(new, "#define CONFIGURE_GROUP %d\n", (int)gid);
661 fprintf(new, "#define CONFIGURE_OWNER %d\n", (int)uid);
665 /* FIXED_NEVER_USERS is another special case. Look up the uid values and
666 create suitable initialization data for a vector. */
668 if (strcmp(name, "FIXED_NEVER_USERS") == 0)
670 char *list = getenv("FIXED_NEVER_USERS");
673 fprintf(new, "#define FIXED_NEVER_USERS 0\n");
681 while (*p != 0) if (*p++ == ':') count++;
683 vector = malloc((count+1) * sizeof(uid_t));
684 vector[0] = (uid_t)count;
686 for (i = 1, j = 0; i <= count; list++, i++)
691 while (*list != 0 && *list != ':') list++;
692 strncpy(name, p, list-p);
699 else if (name[strspn(name, "0123456789")] == 0)
701 vector[j++] = (uid_t)atoi(name);
705 struct passwd *pw = getpwnam(name);
708 printf("\n*** User \"%s\" (specified for FIXED_NEVER_USERS in one of the Makefiles) does not "
709 "exist.\n Please review your build-time configuration.\n\n",
713 vector[j++] = pw->pw_uid;
716 fprintf(new, "#define FIXED_NEVER_USERS %d", j);
717 for (i = 0; i < j; i++) fprintf(new, ", %d", (unsigned int)vector[i]);
723 /* WITH_CONTENT_SCAN is another special case: it must be set if either it or
724 WITH_OLD_DEMIME is set. */
726 if (strcmp(name, "WITH_CONTENT_SCAN") == 0)
728 char *wcs = getenv("WITH_CONTENT_SCAN");
729 char *wod = getenv("WITH_OLD_DEMIME");
730 char *dcc = getenv("EXPERIMENTAL_DCC");
731 if (wcs != NULL || wod != NULL || dcc != NULL)
732 fprintf(new, "#define WITH_CONTENT_SCAN yes\n");
733 else fprintf(new, "/* WITH_CONTENT_SCAN not set */\n");
737 /* Otherwise, check whether a value exists in the environment. Remember if
738 it is an AUTH setting or SUPPORT_CRYPTEQ. */
740 if ((value = getenv(name)) != NULL)
743 len = 21 - (int)strlen(name);
745 if (strncmp(name, "AUTH_", 5) == 0) have_auth = 1;
746 if (strncmp(name, "SUPPORT_CRYPTEQ", 15) == 0) support_crypteq = 1;
748 /* The text value of LDAP_LIB_TYPE refers to a macro that gets set. */
750 if (strcmp(name, "LDAP_LIB_TYPE") == 0)
752 if (strcmp(value, "NETSCAPE") == 0 ||
753 strcmp(value, "UMICHIGAN") == 0 ||
754 strcmp(value, "OPENLDAP1") == 0 ||
755 strcmp(value, "OPENLDAP2") == 0 ||
756 strcmp(value, "SOLARIS") == 0 ||
757 strcmp(value, "SOLARIS7") == 0) /* Compatibility */
759 fprintf(new, "#define LDAP_LIB_%s\n", value);
763 printf("\n*** LDAP_LIB_TYPE=%s is not a recognized LDAP library type."
764 "\n*** Please review your build-time configuration.\n\n", value);
769 else if (strcmp(name, "RADIUS_LIB_TYPE") == 0)
771 if (strcmp(value, "RADIUSCLIENT") == 0 ||
772 strcmp(value, "RADIUSCLIENTNEW") == 0 ||
773 strcmp(value, "RADLIB") == 0)
775 fprintf(new, "#define RADIUS_LIB_%s\n", value);
779 printf("\n*** RADIUS_LIB_TYPE=%s is not a recognized RADIUS library type."
780 "\n*** Please review your build-time configuration.\n\n", value);
785 /* Other macros get set to the environment value. */
789 fprintf(new, "#define %s ", name);
790 while(len-- > 0) fputc(' ', new);
792 /* LOG_FILE_PATH is now messy because it can be a path containing %s or
793 it can be "syslog" or ":syslog" or "syslog:path" or even "path:syslog". */
795 if (strcmp(name, "LOG_FILE_PATH") == 0)
801 char *sss = strchr(ss, ':');
804 strncpy(buffer, ss, sss-ss);
805 buffer[sss-ss] = 0; /* For empty case */
807 else strcpy(buffer, ss);
808 pp = buffer + (int)strlen(buffer);
809 while (pp > buffer && isspace((unsigned char)pp[-1])) pp--;
811 if (buffer[0] != 0 && strcmp(buffer, "syslog") != 0)
812 check_percent_ess(buffer, name);
813 if (sss == NULL) break;
815 while (isspace((unsigned char)*ss)) ss++;
817 fprintf(new, "\"%s\"\n", value);
820 /* Timezone values HEADERS_CHARSET, TCP_WRAPPERS_DAEMON_NAME and
821 WHITELIST_D_MACROS get quoted */
823 else if (strcmp(name, "TIMEZONE_DEFAULT") == 0||
824 strcmp(name, "TCP_WRAPPERS_DAEMON_NAME") == 0||
825 strcmp(name, "HEADERS_CHARSET") == 0||
826 strcmp(name, "WHITELIST_D_MACROS") == 0)
827 fprintf(new, "\"%s\"\n", value);
829 /* GnuTLS constants; first is for debugging, others are tuning */
831 /* less than 0 is not-active; 0-9 are normal, API suggests higher
832 taken without problems */
833 else if (strcmp(name, "EXIM_GNUTLS_LIBRARY_LOG_LEVEL") == 0)
837 nv = strtol(value, &end, 10);
838 if (end != value && *end == '\0' && nv >= -1 && nv <= 100)
840 fprintf(new, "%s\n", value);
844 printf("Value of %s should be -1..9\n", name);
849 /* how many bits Exim, as a client, demands must be in D-H */
850 /* as of GnuTLS 2.12.x, we ask for "normal" for D-H PK; before that, we
851 specify the number of bits. We've stuck with the historical value, but
852 it can be overriden. */
853 else if ((strcmp(name, "EXIM_CLIENT_DH_MIN_BITS") == 0) ||
854 (strcmp(name, "EXIM_SERVER_DH_BITS_PRE2_12") == 0))
858 nv = strtol(value, &end, 10);
859 if (end != value && *end == '\0' && nv >= 1000 && nv < 50000)
861 fprintf(new, "%s\n", value);
865 printf("Unreasonable value (%s) of \"%s\".\n", value, name);
870 /* For others, quote any paths and don't quote anything else */
874 if (value[0] == '/') fprintf(new, "\"%s\"\n", value);
875 else fprintf(new, "%s\n", value);
880 /* Value not defined in the environment; use the default */
885 while (*p == ' ' || *p == '\t') p++;
886 if (*p != '\n') fputs(buffer, new); else
889 if (strcmp(name, "BIN_DIRECTORY") == 0 ||
890 strcmp(name, "CONFIGURE_FILE") == 0)
892 printf("\n*** %s has not been defined in any of the Makefiles in the\n"
893 " \"Local\" directory. "
894 "Please review your build-time configuration.\n\n", name);
898 if (strcmp(name, "TIMEZONE_DEFAULT") == 0)
900 char *tz = getenv("TZ");
901 fprintf(new, "#define TIMEZONE_DEFAULT ");
902 if (tz == NULL) fprintf(new, "NULL\n"); else
903 fprintf(new, "\"%s\"\n", tz);
906 else fprintf(new, "/* %s not set */\n", name);
913 /* If any AUTH macros were defined, ensure that SUPPORT_CRYPTEQ is also
918 if (!support_crypteq) fprintf(new, "/* Force SUPPORT_CRYPTEQ for AUTH */\n"
919 "#define SUPPORT_CRYPTEQ\n");
924 fprintf(new, "\n/* End of config.h */\n");
929 /* End of buildconfig.c */