1 /* $Cambridge: exim/src/src/buildconfig.c,v 1.18 2010/06/07 18:09:07 pdp Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2009 */
8 /* See the file NOTICE for conditions of use and distribution. */
11 /*************************************************
12 * Build configuration header for Exim *
13 *************************************************/
15 /* This auxiliary program builds the file config.h by the following
18 First, it determines the size of off_t and time_t variables, and generates
19 macro code to define OFF_T_FMT and TIME_T_FMT as suitable formats, if they are
20 not already defined in the system-specific header file.
22 Then it reads Makefile, looking for certain OS-specific definitions which it
23 uses to define some specific macros. Finally, it reads the defaults file
26 The defaults file contains normal C #define statements for various macros; if
27 the name of a macro is found in the environment, the environment value replaces
28 the default. If the default #define does not contain any value, then that macro
29 is not copied to the created file unless there is some value in the
32 This program is compiled and run as part of the Make process and is not
33 normally called independently. */
41 #include <sys/types.h>
55 static char *db_opts[] = { "", "USE_DB", "USE_GDBM", "USE_TDB" };
57 static int have_ipv6 = 0;
58 static int have_iconv = 0;
60 static char errno_quota[256];
61 static char ostype[256];
64 /* If any entry is an initial substring of another, the longer one must
67 static have_item have_list[] = {
68 { "HAVE_IPV6", &have_ipv6 },
69 { "HAVE_ICONV", &have_iconv },
73 static save_item save_list[] = {
74 { "ERRNO_QUOTA", errno_quota },
81 /* Subroutine to check a string for precisely one instance of "%s". If not,
85 check_percent_ess(char *value, char *name)
88 char *p = strstr(value, "%s");
89 if (p != NULL) OK = strstr(p+2, "%s") == NULL;
92 printf("\n*** \"%s\" (%s) must contain precisely one occurrence of\n"
93 "*** \"%%s\". Please review your build-time configuration.\n\n/", value,
103 main(int argc, char **argv)
105 off_t test_off_t = 0;
106 time_t test_time_t = 0;
107 #if !(__STDC_VERSION__ >= 199901L) && !defined(PRIdMAX)
108 size_t test_size_t = 0;
109 unsigned long test_ulong_t = 0L;
111 long test_long_t = 0;
114 int last_initial = 'A';
117 int in_local_makefile = 0;
118 int use_which_db = 0;
119 int use_which_db_in_local_makefile = 0;
120 int support_crypteq = 0;
125 printf("*** Buildconfig: called with incorrect arguments\n");
129 new = fopen("config.h", "wb");
132 printf("*** Buildconfig: failed to open config.h for output\n");
136 printf("Building configuration file config.h\n");
138 fprintf(new, "/*************************************************\n");
139 fprintf(new, "* Configuration header for Exim *\n");
140 fprintf(new, "*************************************************/\n\n");
142 fprintf(new, "/* This file was automatically generated from Makefile and "
143 "config.h.defaults,\n");
144 fprintf(new, "using values specified in the configuration file Local/Makefile.\n");
145 fprintf(new, "Do not edit it. Instead, edit Local/Makefile and "
146 "rerun make. */\n\n");
148 /* First, deal with the printing format for off_t variables. We assume that if
149 the size of off_t is greater than 4, "%lld" will be available as a format for
150 printing long long variables, and there will be support for the long long type.
151 This assumption is known to be OK for the common operating systems. */
153 fprintf(new, "#ifndef OFF_T_FMT\n");
154 if (sizeof(test_off_t) > sizeof(test_long_t))
156 fprintf(new, "#define OFF_T_FMT \"%%lld\"\n");
157 fprintf(new, "#define LONGLONG_T long long int\n");
161 fprintf(new, "#define OFF_T_FMT \"%%ld\"\n");
162 fprintf(new, "#define LONGLONG_T long int\n");
164 fprintf(new, "#endif\n\n");
166 /* Now do the same thing for time_t variables. If the length is greater than
167 4, we want to assume long long support (even if off_t was less than 4). If the
168 length is 4 or less, we can leave LONGLONG_T to whatever was defined above for
171 fprintf(new, "#ifndef TIME_T_FMT\n");
172 if (sizeof(test_time_t) > sizeof(test_long_t))
174 fprintf(new, "#define TIME_T_FMT \"%%lld\"\n");
175 fprintf(new, "#undef LONGLONG_T\n");
176 fprintf(new, "#define LONGLONG_T long long int\n");
180 fprintf(new, "#define TIME_T_FMT \"%%ld\"\n");
182 fprintf(new, "#endif\n\n");
184 /* And for sizeof() results, size_t, which should with C99 be just %zu, deal
185 with C99 not being ubiquitous yet. Unfortunately. */
187 #if __STDC_VERSION__ >= 199901L
188 fprintf(new, "#define SIZE_T_FMT \"%%zu\"\n");
191 fprintf(new, "#define SIZE_T_FMT \"%%" PRIdMAX "\"\n");
193 if (sizeof(test_size_t) > sizeof (test_ulong_t))
194 fprintf(new, "#define SIZE_T_FMT \"%%llu\"\n");
196 fprintf(new, "#define SIZE_T_FMT \"%%lu\"\n");
200 /* Now search the makefile for certain settings */
202 base = fopen("Makefile", "rb");
205 printf("*** Buildconfig: failed to open Makefile\n");
210 errno_quota[0] = 0; /* no over-riding value set */
211 ostype[0] = 0; /* just in case */
214 while (fgets(buffer, sizeof(buffer), base) != NULL)
219 char *p = buffer + (int)strlen(buffer);
221 while (p > buffer && isspace((unsigned char)p[-1])) p--;
224 while (isspace((unsigned char)*p)) p++;
226 /* Notice when we hit the user's makefile */
228 if (strcmp(p, "# From Local/Makefile") == 0)
230 in_local_makefile = 1;
234 /* Remember the last DB option setting. If we hit two in the user's
235 Makefile, complain. */
237 for (i = 1; i < sizeof(db_opts)/sizeof(char *); i++)
239 int len = (int)strlen(db_opts[i]);
240 if (strncmp(p, db_opts[i], len) == 0 && (p[len] == ' ' || p[len] == '='))
242 if (in_local_makefile)
244 if (use_which_db_in_local_makefile)
246 printf("*** Only one of USE_DB, USE_GDBM, or USE_TDB should be "
247 "defined in Local/Makefile\n");
250 use_which_db_in_local_makefile = 1;
256 if (i < sizeof(db_opts)/sizeof(char *)) continue;
258 /* Items where we just save a boolean */
260 for (h = have_list; h->name != NULL; h++)
262 int len = (int)strlen(h->name);
263 if (strncmp(p, h->name, len) == 0)
266 while (isspace((unsigned char)*p)) p++;
269 printf("*** Buildconfig: syntax error in Makefile line %d\n", linecount);
272 while (isspace((unsigned char)*p)) p++;
273 if (strcmp(p, "YES") == 0 || strcmp(p, "yes") == 0) *(h->flag) = 1;
274 else *(h->flag) = 0; /* Must reset in case multiple instances */
279 if (h->name != NULL) continue;
281 /* Items where we save the complete string */
283 for (s = save_list; s->name != NULL; s++)
285 int len = (int)strlen(s->name);
286 if (strncmp(p, s->name, len) == 0)
289 while (isspace((unsigned char)*p)) p++;
292 printf("*** Buildconfig: syntax error in Makefile line %d\n", linecount);
295 while (isspace((unsigned char)*p)) p++;
301 fprintf(new, "#define HAVE_IPV6 %s\n",
302 have_ipv6? "TRUE" : "FALSE");
304 fprintf(new, "#define HAVE_ICONV %s\n",
305 have_iconv? "TRUE" : "FALSE");
307 if (errno_quota[0] != 0)
308 fprintf(new, "\n#define ERRNO_QUOTA %s\n", errno_quota);
310 if (strcmp(cc, "gcc") == 0 &&
311 (strstr(ostype, "IRIX") != NULL || strstr(ostype, "AIX") != NULL))
313 fprintf(new, "\n/* This switch includes the code to fix the inet_ntoa() */");
314 fprintf(new, "\n/* bug when using gcc on an IRIX or AIX system. */");
315 fprintf(new, "\n#define USE_INET_NTOA_FIX");
322 /* Now handle the macros listed in the defaults */
324 base = fopen("../src/config.h.defaults", "rb");
327 printf("*** Buildconfig: failed to open ../src/config.h.defaults\n");
332 while (fgets(buffer, sizeof(buffer), base) != NULL)
340 while (*p == ' ' || *p == '\t') p++;
342 if (strncmp(p, "#define ", 8) != 0) continue;
345 while (*p == ' ' || *p == '\t') p++;
347 if (*p < last_initial) fprintf(new, "\n");
350 while (*p && (isalnum((unsigned char)*p) || *p == '_')) *q++ = *p++;
353 /* USE_DB, USE_GDBM, and USE_TDB are special cases. We want to have only
354 one of them set. The scan of the Makefile has saved which was the last one
357 for (i = 1; i < sizeof(db_opts)/sizeof(char *); i++)
359 if (strcmp(name, db_opts[i]) == 0)
361 if (use_which_db == i)
362 fprintf(new, "#define %s %.*syes\n", db_opts[i],
363 21 - (int)strlen(db_opts[i]), " ");
365 fprintf(new, "/* %s not set */\n", name);
369 if (i < sizeof(db_opts)/sizeof(char *)) continue;
371 /* EXIM_USER is a special case. We look in the environment for EXIM_USER or
372 EXIM_UID (the latter for backward compatibility with Exim 3). If the value is
373 not numeric, we look up the user, and default the GID if found. Otherwise,
374 EXIM_GROUP or EXIM_GID must be in the environment. */
376 if (strcmp(name, "EXIM_UID") == 0)
382 char *username = NULL;
383 char *groupname = NULL;
385 char *user = getenv("EXIM_USER");
386 char *group = getenv("EXIM_GROUP");
388 if (user == NULL) user = getenv("EXIM_UID");
389 if (group == NULL) group = getenv("EXIM_GID");
393 printf("\n*** EXIM_USER has not been defined in any of the Makefiles in "
394 "the\n \"Local\" directory. Please review your build-time "
395 "configuration.\n\n");
399 while (isspace((unsigned char)(*user))) user++;
402 printf("\n*** EXIM_USER is defined as an empty string in one of the "
403 "files\n in the \"Local\" directory. Please review your build-time"
404 "\n configuration.\n\n");
408 for (s = user; *s != 0; s++)
410 if (iscntrl((unsigned char)(*s)))
412 printf("\n*** EXIM_USER contains the control character 0x%02X in one "
413 "of the files\n in the \"Local\" directory. Please review your "
414 "build-time\n configuration.\n\n", *s);
419 /* Numeric uid given */
421 if (user[strspn(user, "0123456789")] == 0)
423 uid = (uid_t)atoi(user);
426 /* User name given. Normally, we look up the uid right away. However,
427 people building binary distributions sometimes want to retain the name till
428 runtime. This is supported if the name begins "ref:". */
430 else if (strncmp(user, "ref:", 4) == 0)
433 while (isspace(*user)) user++;
441 struct passwd *pw = getpwnam(user);
444 printf("\n*** User \"%s\" (specified in one of the Makefiles) does not "
445 "exist.\n Please review your build-time configuration.\n\n",
455 /* Use explicit group if set. */
459 while (isspace((unsigned char)(*group))) group++;
462 printf("\n*** EXIM_GROUP is defined as an empty string in one of "
463 "the files in the\n \"Local\" directory. ");
466 printf("If you want the Exim group to be taken from the\n "
467 "password data for the Exim user, just remove the EXIM_GROUP "
468 "setting.\n Otherwise, p");
470 else printf("EXIM_USER is defined numerically, so there is no"
471 "\n default for EXIM_GROUP and you must set it explicitly.\n P");
472 printf("lease review your build-time configuration.\n\n");
476 for (s = group; *s != 0; s++)
478 if (iscntrl((unsigned char)(*s)))
480 printf("\n*** EXIM_GROUP contains the control character 0x%02X in one "
481 "of the files\n in the \"Local\" directory. Please review your "
482 "build-time\n configuration.\n\n", *s);
487 /* Group name given. This may be by reference or to be looked up now,
490 if (strncmp(group, "ref:", 4) == 0)
493 while (isspace(*group)) group++;
497 else if (username != NULL)
502 else if (group[strspn(group, "0123456789")] == 0)
504 gid = (gid_t)atoi(group);
509 struct group *gr = getgrnam(group);
512 printf("\n*** Group \"%s\" (specified in one of the Makefiles) does "
513 "not exist.\n Please review your build-time configuration.\n\n",
521 /* Else trouble unless found in passwd file with user */
525 printf("\n*** No group set for Exim. Please review your build-time "
526 "configuration.\n\n");
530 /* security sanity checks
531 if ref: is being used, we can never be sure, but we can take reasonable
532 steps to filter out the most obvious ones. */
534 if ((!uid_not_set && uid == 0) ||
535 (strcmp(username, "root") == 0) ||
536 (strcmp(username, "toor") == 0) )
538 printf("\n*** Exim's internal user must not be root.\n\n");
542 /* Output user and group names or uid/gid. When names are set, uid/gid
543 are set to zero but will be replaced at runtime. */
545 if (username != NULL)
546 fprintf(new, "#define EXIM_USERNAME \"%s\"\n", username);
547 if (groupname != NULL)
548 fprintf(new, "#define EXIM_GROUPNAME \"%s\"\n", groupname);
550 fprintf(new, "#define EXIM_UID %d\n", (int)uid);
551 fprintf(new, "#define EXIM_GID %d\n", (int)gid);
555 /* CONFIGURE_OWNER and CONFIGURE_GROUP are special cases. We look in the
556 environment for first. If the value is not numeric, we look up the user or
557 group. A lot of this code is similar to that for EXIM_USER, but it's easier
558 to keep it separate. */
560 if (strcmp(name, "CONFIGURE_OWNER") == 0 ||
561 strcmp(name, "CONFIGURE_GROUP") == 0)
563 int isgroup = name[10] == 'G';
567 char *username = NULL;
568 char *user = getenv(name);
570 if (user == NULL) user = "";
571 while (isspace((unsigned char)(*user))) user++;
574 fprintf(new, "/* %s not set */\n", name);
578 for (s = user; *s != 0; s++)
580 if (iscntrl((unsigned char)(*s)))
582 printf("\n*** %s contains the control character 0x%02X in "
583 "one of the files\n in the \"Local\" directory. Please review "
584 "your build-time\n configuration.\n\n", name, *s);
589 /* Numeric uid given */
591 if (user[strspn(user, "0123456789")] == 0)
594 gid = (gid_t)atoi(user);
596 uid = (uid_t)atoi(user);
599 /* Name given. Normally, we look up the uid or gid right away. However,
600 people building binary distributions sometimes want to retain the name till
601 runtime. This is supported if the name begins "ref:". */
603 else if (strncmp(user, "ref:", 4) == 0)
606 while (isspace(*user)) user++;
612 struct group *gr = getgrnam(user);
615 printf("\n*** Group \"%s\" (specified in one of the Makefiles) does not "
616 "exist.\n Please review your build-time configuration.\n\n",
625 struct passwd *pw = getpwnam(user);
628 printf("\n*** User \"%s\" (specified in one of the Makefiles) does not "
629 "exist.\n Please review your build-time configuration.\n\n",
636 /* Output user and group names or uid/gid. When names are set, uid/gid
637 are set to zero but will be replaced at runtime. */
639 if (username != NULL)
642 fprintf(new, "#define CONFIGURE_GROUPNAME \"%s\"\n", username);
644 fprintf(new, "#define CONFIGURE_OWNERNAME \"%s\"\n", username);
648 fprintf(new, "#define CONFIGURE_GROUP %d\n", (int)gid);
650 fprintf(new, "#define CONFIGURE_OWNER %d\n", (int)uid);
654 /* FIXED_NEVER_USERS is another special case. Look up the uid values and
655 create suitable initialization data for a vector. */
657 if (strcmp(name, "FIXED_NEVER_USERS") == 0)
659 char *list = getenv("FIXED_NEVER_USERS");
662 fprintf(new, "#define FIXED_NEVER_USERS 0\n");
670 while (*p != 0) if (*p++ == ':') count++;
672 vector = malloc((count+1) * sizeof(uid_t));
673 vector[0] = (uid_t)count;
675 for (i = 1, j = 0; i <= count; list++, i++)
680 while (*list != 0 && *list != ':') list++;
681 strncpy(name, p, list-p);
688 else if (name[strspn(name, "0123456789")] == 0)
690 vector[j++] = (uid_t)atoi(name);
694 struct passwd *pw = getpwnam(name);
697 printf("\n*** User \"%s\" (specified for FIXED_NEVER_USERS in one of the Makefiles) does not "
698 "exist.\n Please review your build-time configuration.\n\n",
702 vector[j++] = pw->pw_uid;
705 fprintf(new, "#define FIXED_NEVER_USERS %d", j);
706 for (i = 0; i < j; i++) fprintf(new, ", %d", (unsigned int)vector[i]);
712 /* WITH_CONTENT_SCAN is another special case: it must be set if either it or
713 WITH_OLD_DEMIME is set. */
715 if (strcmp(name, "WITH_CONTENT_SCAN") == 0)
717 char *wcs = getenv("WITH_CONTENT_SCAN");
718 char *wod = getenv("WITH_OLD_DEMIME");
719 char *dcc = getenv("EXPERIMENTAL_DCC");
720 if (wcs != NULL || wod != NULL || dcc != NULL)
721 fprintf(new, "#define WITH_CONTENT_SCAN yes\n");
722 else fprintf(new, "/* WITH_CONTENT_SCAN not set */\n");
726 /* Otherwise, check whether a value exists in the environment. Remember if
727 it is an AUTH setting or SUPPORT_CRYPTEQ. */
729 if ((value = getenv(name)) != NULL)
732 len = 21 - (int)strlen(name);
734 if (strncmp(name, "AUTH_", 5) == 0) have_auth = 1;
735 if (strncmp(name, "SUPPORT_CRYPTEQ", 15) == 0) support_crypteq = 1;
737 /* The text value of LDAP_LIB_TYPE refers to a macro that gets set. */
739 if (strcmp(name, "LDAP_LIB_TYPE") == 0)
741 if (strcmp(value, "NETSCAPE") == 0 ||
742 strcmp(value, "UMICHIGAN") == 0 ||
743 strcmp(value, "OPENLDAP1") == 0 ||
744 strcmp(value, "OPENLDAP2") == 0 ||
745 strcmp(value, "SOLARIS") == 0 ||
746 strcmp(value, "SOLARIS7") == 0) /* Compatibility */
748 fprintf(new, "#define LDAP_LIB_%s\n", value);
752 printf("\n*** LDAP_LIB_TYPE=%s is not a recognized LDAP library type."
753 "\n*** Please review your build-time configuration.\n\n", value);
758 else if (strcmp(name, "RADIUS_LIB_TYPE") == 0)
760 if (strcmp(value, "RADIUSCLIENT") == 0 ||
761 strcmp(value, "RADIUSCLIENTNEW") == 0 ||
762 strcmp(value, "RADLIB") == 0)
764 fprintf(new, "#define RADIUS_LIB_%s\n", value);
768 printf("\n*** RADIUS_LIB_TYPE=%s is not a recognized RADIUS library type."
769 "\n*** Please review your build-time configuration.\n\n", value);
774 /* Other macros get set to the environment value. */
778 fprintf(new, "#define %s ", name);
779 while(len-- > 0) fputc(' ', new);
781 /* LOG_FILE_PATH is now messy because it can be a path containing %s or
782 it can be "syslog" or ":syslog" or "syslog:path" or even "path:syslog". */
784 if (strcmp(name, "LOG_FILE_PATH") == 0)
790 char *sss = strchr(ss, ':');
793 strncpy(buffer, ss, sss-ss);
794 buffer[sss-ss] = 0; /* For empty case */
796 else strcpy(buffer, ss);
797 pp = buffer + (int)strlen(buffer);
798 while (pp > buffer && isspace((unsigned char)pp[-1])) pp--;
800 if (buffer[0] != 0 && strcmp(buffer, "syslog") != 0)
801 check_percent_ess(buffer, name);
802 if (sss == NULL) break;
804 while (isspace((unsigned char)*ss)) ss++;
806 fprintf(new, "\"%s\"\n", value);
809 /* Timezone values and HEADERS_CHARSET get quoted */
811 else if (strcmp(name, "TIMEZONE_DEFAULT") == 0||
812 strcmp(name, "HEADERS_CHARSET") == 0)
813 fprintf(new, "\"%s\"\n", value);
815 /* For others, quote any paths and don't quote anything else */
819 if (value[0] == '/') fprintf(new, "\"%s\"\n", value);
820 else fprintf(new, "%s\n", value);
825 /* Value not defined in the environment; use the default */
830 while (*p == ' ' || *p == '\t') p++;
831 if (*p != '\n') fputs(buffer, new); else
834 if (strcmp(name, "BIN_DIRECTORY") == 0 ||
835 strcmp(name, "CONFIGURE_FILE") == 0)
837 printf("\n*** %s has not been defined in any of the Makefiles in the\n"
838 " \"Local\" directory. "
839 "Please review your build-time configuration.\n\n", name);
843 if (strcmp(name, "TIMEZONE_DEFAULT") == 0)
845 char *tz = getenv("TZ");
846 fprintf(new, "#define TIMEZONE_DEFAULT ");
847 if (tz == NULL) fprintf(new, "NULL\n"); else
848 fprintf(new, "\"%s\"\n", tz);
851 else fprintf(new, "/* %s not set */\n", name);
858 /* If any AUTH macros were defined, ensure that SUPPORT_CRYPTEQ is also
863 if (!support_crypteq) fprintf(new, "/* Force SUPPORT_CRYPTEQ for AUTH */\n"
864 "#define SUPPORT_CRYPTEQ\n");
869 fprintf(new, "\n/* End of config.h */\n");
874 /* End of buildconfig.c */