1 /* $Cambridge: exim/src/src/buildconfig.c,v 1.20 2010/06/12 15:21:26 jetmore 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 ((username != NULL) && (
536 (strcmp(username, "root") == 0) ||
537 (strcmp(username, "toor") == 0) )))
539 printf("\n*** Exim's internal user must not be root.\n\n");
543 /* Output user and group names or uid/gid. When names are set, uid/gid
544 are set to zero but will be replaced at runtime. */
546 if (username != NULL)
547 fprintf(new, "#define EXIM_USERNAME \"%s\"\n", username);
548 if (groupname != NULL)
549 fprintf(new, "#define EXIM_GROUPNAME \"%s\"\n", groupname);
551 fprintf(new, "#define EXIM_UID %d\n", (int)uid);
552 fprintf(new, "#define EXIM_GID %d\n", (int)gid);
556 /* CONFIGURE_OWNER and CONFIGURE_GROUP are special cases. We look in the
557 environment for first. If the value is not numeric, we look up the user or
558 group. A lot of this code is similar to that for EXIM_USER, but it's easier
559 to keep it separate. */
561 if (strcmp(name, "CONFIGURE_OWNER") == 0 ||
562 strcmp(name, "CONFIGURE_GROUP") == 0)
564 int isgroup = name[10] == 'G';
568 char *username = NULL;
569 char *user = getenv(name);
571 if (user == NULL) user = "";
572 while (isspace((unsigned char)(*user))) user++;
575 fprintf(new, "/* %s not set */\n", name);
579 for (s = user; *s != 0; s++)
581 if (iscntrl((unsigned char)(*s)))
583 printf("\n*** %s contains the control character 0x%02X in "
584 "one of the files\n in the \"Local\" directory. Please review "
585 "your build-time\n configuration.\n\n", name, *s);
590 /* Numeric uid given */
592 if (user[strspn(user, "0123456789")] == 0)
595 gid = (gid_t)atoi(user);
597 uid = (uid_t)atoi(user);
600 /* Name given. Normally, we look up the uid or gid right away. However,
601 people building binary distributions sometimes want to retain the name till
602 runtime. This is supported if the name begins "ref:". */
604 else if (strncmp(user, "ref:", 4) == 0)
607 while (isspace(*user)) user++;
613 struct group *gr = getgrnam(user);
616 printf("\n*** Group \"%s\" (specified in one of the Makefiles) does not "
617 "exist.\n Please review your build-time configuration.\n\n",
626 struct passwd *pw = getpwnam(user);
629 printf("\n*** User \"%s\" (specified in one of the Makefiles) does not "
630 "exist.\n Please review your build-time configuration.\n\n",
637 /* Output user and group names or uid/gid. When names are set, uid/gid
638 are set to zero but will be replaced at runtime. */
640 if (username != NULL)
643 fprintf(new, "#define CONFIGURE_GROUPNAME \"%s\"\n", username);
645 fprintf(new, "#define CONFIGURE_OWNERNAME \"%s\"\n", username);
649 fprintf(new, "#define CONFIGURE_GROUP %d\n", (int)gid);
651 fprintf(new, "#define CONFIGURE_OWNER %d\n", (int)uid);
655 /* FIXED_NEVER_USERS is another special case. Look up the uid values and
656 create suitable initialization data for a vector. */
658 if (strcmp(name, "FIXED_NEVER_USERS") == 0)
660 char *list = getenv("FIXED_NEVER_USERS");
663 fprintf(new, "#define FIXED_NEVER_USERS 0\n");
671 while (*p != 0) if (*p++ == ':') count++;
673 vector = malloc((count+1) * sizeof(uid_t));
674 vector[0] = (uid_t)count;
676 for (i = 1, j = 0; i <= count; list++, i++)
681 while (*list != 0 && *list != ':') list++;
682 strncpy(name, p, list-p);
689 else if (name[strspn(name, "0123456789")] == 0)
691 vector[j++] = (uid_t)atoi(name);
695 struct passwd *pw = getpwnam(name);
698 printf("\n*** User \"%s\" (specified for FIXED_NEVER_USERS in one of the Makefiles) does not "
699 "exist.\n Please review your build-time configuration.\n\n",
703 vector[j++] = pw->pw_uid;
706 fprintf(new, "#define FIXED_NEVER_USERS %d", j);
707 for (i = 0; i < j; i++) fprintf(new, ", %d", (unsigned int)vector[i]);
713 /* WITH_CONTENT_SCAN is another special case: it must be set if either it or
714 WITH_OLD_DEMIME is set. */
716 if (strcmp(name, "WITH_CONTENT_SCAN") == 0)
718 char *wcs = getenv("WITH_CONTENT_SCAN");
719 char *wod = getenv("WITH_OLD_DEMIME");
720 char *dcc = getenv("EXPERIMENTAL_DCC");
721 if (wcs != NULL || wod != NULL || dcc != NULL)
722 fprintf(new, "#define WITH_CONTENT_SCAN yes\n");
723 else fprintf(new, "/* WITH_CONTENT_SCAN not set */\n");
727 /* Otherwise, check whether a value exists in the environment. Remember if
728 it is an AUTH setting or SUPPORT_CRYPTEQ. */
730 if ((value = getenv(name)) != NULL)
733 len = 21 - (int)strlen(name);
735 if (strncmp(name, "AUTH_", 5) == 0) have_auth = 1;
736 if (strncmp(name, "SUPPORT_CRYPTEQ", 15) == 0) support_crypteq = 1;
738 /* The text value of LDAP_LIB_TYPE refers to a macro that gets set. */
740 if (strcmp(name, "LDAP_LIB_TYPE") == 0)
742 if (strcmp(value, "NETSCAPE") == 0 ||
743 strcmp(value, "UMICHIGAN") == 0 ||
744 strcmp(value, "OPENLDAP1") == 0 ||
745 strcmp(value, "OPENLDAP2") == 0 ||
746 strcmp(value, "SOLARIS") == 0 ||
747 strcmp(value, "SOLARIS7") == 0) /* Compatibility */
749 fprintf(new, "#define LDAP_LIB_%s\n", value);
753 printf("\n*** LDAP_LIB_TYPE=%s is not a recognized LDAP library type."
754 "\n*** Please review your build-time configuration.\n\n", value);
759 else if (strcmp(name, "RADIUS_LIB_TYPE") == 0)
761 if (strcmp(value, "RADIUSCLIENT") == 0 ||
762 strcmp(value, "RADIUSCLIENTNEW") == 0 ||
763 strcmp(value, "RADLIB") == 0)
765 fprintf(new, "#define RADIUS_LIB_%s\n", value);
769 printf("\n*** RADIUS_LIB_TYPE=%s is not a recognized RADIUS library type."
770 "\n*** Please review your build-time configuration.\n\n", value);
775 /* Other macros get set to the environment value. */
779 fprintf(new, "#define %s ", name);
780 while(len-- > 0) fputc(' ', new);
782 /* LOG_FILE_PATH is now messy because it can be a path containing %s or
783 it can be "syslog" or ":syslog" or "syslog:path" or even "path:syslog". */
785 if (strcmp(name, "LOG_FILE_PATH") == 0)
791 char *sss = strchr(ss, ':');
794 strncpy(buffer, ss, sss-ss);
795 buffer[sss-ss] = 0; /* For empty case */
797 else strcpy(buffer, ss);
798 pp = buffer + (int)strlen(buffer);
799 while (pp > buffer && isspace((unsigned char)pp[-1])) pp--;
801 if (buffer[0] != 0 && strcmp(buffer, "syslog") != 0)
802 check_percent_ess(buffer, name);
803 if (sss == NULL) break;
805 while (isspace((unsigned char)*ss)) ss++;
807 fprintf(new, "\"%s\"\n", value);
810 /* Timezone values HEADERS_CHARSET, and TCP_WRAPPERS_DAEMON_NAME get quoted */
812 else if (strcmp(name, "TIMEZONE_DEFAULT") == 0||
813 strcmp(name, "TCP_WRAPPERS_DAEMON_NAME") == 0||
814 strcmp(name, "HEADERS_CHARSET") == 0)
815 fprintf(new, "\"%s\"\n", value);
817 /* For others, quote any paths and don't quote anything else */
821 if (value[0] == '/') fprintf(new, "\"%s\"\n", value);
822 else fprintf(new, "%s\n", value);
827 /* Value not defined in the environment; use the default */
832 while (*p == ' ' || *p == '\t') p++;
833 if (*p != '\n') fputs(buffer, new); else
836 if (strcmp(name, "BIN_DIRECTORY") == 0 ||
837 strcmp(name, "CONFIGURE_FILE") == 0)
839 printf("\n*** %s has not been defined in any of the Makefiles in the\n"
840 " \"Local\" directory. "
841 "Please review your build-time configuration.\n\n", name);
845 if (strcmp(name, "TIMEZONE_DEFAULT") == 0)
847 char *tz = getenv("TZ");
848 fprintf(new, "#define TIMEZONE_DEFAULT ");
849 if (tz == NULL) fprintf(new, "NULL\n"); else
850 fprintf(new, "\"%s\"\n", tz);
853 else fprintf(new, "/* %s not set */\n", name);
860 /* If any AUTH macros were defined, ensure that SUPPORT_CRYPTEQ is also
865 if (!support_crypteq) fprintf(new, "/* Force SUPPORT_CRYPTEQ for AUTH */\n"
866 "#define SUPPORT_CRYPTEQ\n");
871 fprintf(new, "\n/* End of config.h */\n");
876 /* End of buildconfig.c */