1 /* $Cambridge: exim/src/src/buildconfig.c,v 1.17 2010/06/07 00:12:42 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 size_t test_size_t = 0;
108 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. */
185 #if __STDC_VERSION__ >= 199901L
186 fprintf(new, "#define SIZE_T_FMT \"%%zu\"\n");
190 fprintf(new, "#define SIZE_T_FMT \"%%" PRIdMAX "\"\n");
192 if (sizeof(test_size_t) > sizeof (test_ulong_t))
193 fprintf(new, "#define SIZE_T_FMT \"%%llu\"\n");
195 fprintf(new, "#define SIZE_T_FMT \"%%lu\"\n");
199 /* Now search the makefile for certain settings */
201 base = fopen("Makefile", "rb");
204 printf("*** Buildconfig: failed to open Makefile\n");
209 errno_quota[0] = 0; /* no over-riding value set */
210 ostype[0] = 0; /* just in case */
213 while (fgets(buffer, sizeof(buffer), base) != NULL)
218 char *p = buffer + (int)strlen(buffer);
220 while (p > buffer && isspace((unsigned char)p[-1])) p--;
223 while (isspace((unsigned char)*p)) p++;
225 /* Notice when we hit the user's makefile */
227 if (strcmp(p, "# From Local/Makefile") == 0)
229 in_local_makefile = 1;
233 /* Remember the last DB option setting. If we hit two in the user's
234 Makefile, complain. */
236 for (i = 1; i < sizeof(db_opts)/sizeof(char *); i++)
238 int len = (int)strlen(db_opts[i]);
239 if (strncmp(p, db_opts[i], len) == 0 && (p[len] == ' ' || p[len] == '='))
241 if (in_local_makefile)
243 if (use_which_db_in_local_makefile)
245 printf("*** Only one of USE_DB, USE_GDBM, or USE_TDB should be "
246 "defined in Local/Makefile\n");
249 use_which_db_in_local_makefile = 1;
255 if (i < sizeof(db_opts)/sizeof(char *)) continue;
257 /* Items where we just save a boolean */
259 for (h = have_list; h->name != NULL; h++)
261 int len = (int)strlen(h->name);
262 if (strncmp(p, h->name, len) == 0)
265 while (isspace((unsigned char)*p)) p++;
268 printf("*** Buildconfig: syntax error in Makefile line %d\n", linecount);
271 while (isspace((unsigned char)*p)) p++;
272 if (strcmp(p, "YES") == 0 || strcmp(p, "yes") == 0) *(h->flag) = 1;
273 else *(h->flag) = 0; /* Must reset in case multiple instances */
278 if (h->name != NULL) continue;
280 /* Items where we save the complete string */
282 for (s = save_list; s->name != NULL; s++)
284 int len = (int)strlen(s->name);
285 if (strncmp(p, s->name, len) == 0)
288 while (isspace((unsigned char)*p)) p++;
291 printf("*** Buildconfig: syntax error in Makefile line %d\n", linecount);
294 while (isspace((unsigned char)*p)) p++;
300 fprintf(new, "#define HAVE_IPV6 %s\n",
301 have_ipv6? "TRUE" : "FALSE");
303 fprintf(new, "#define HAVE_ICONV %s\n",
304 have_iconv? "TRUE" : "FALSE");
306 if (errno_quota[0] != 0)
307 fprintf(new, "\n#define ERRNO_QUOTA %s\n", errno_quota);
309 if (strcmp(cc, "gcc") == 0 &&
310 (strstr(ostype, "IRIX") != NULL || strstr(ostype, "AIX") != NULL))
312 fprintf(new, "\n/* This switch includes the code to fix the inet_ntoa() */");
313 fprintf(new, "\n/* bug when using gcc on an IRIX or AIX system. */");
314 fprintf(new, "\n#define USE_INET_NTOA_FIX");
321 /* Now handle the macros listed in the defaults */
323 base = fopen("../src/config.h.defaults", "rb");
326 printf("*** Buildconfig: failed to open ../src/config.h.defaults\n");
331 while (fgets(buffer, sizeof(buffer), base) != NULL)
339 while (*p == ' ' || *p == '\t') p++;
341 if (strncmp(p, "#define ", 8) != 0) continue;
344 while (*p == ' ' || *p == '\t') p++;
346 if (*p < last_initial) fprintf(new, "\n");
349 while (*p && (isalnum((unsigned char)*p) || *p == '_')) *q++ = *p++;
352 /* USE_DB, USE_GDBM, and USE_TDB are special cases. We want to have only
353 one of them set. The scan of the Makefile has saved which was the last one
356 for (i = 1; i < sizeof(db_opts)/sizeof(char *); i++)
358 if (strcmp(name, db_opts[i]) == 0)
360 if (use_which_db == i)
361 fprintf(new, "#define %s %.*syes\n", db_opts[i],
362 21 - (int)strlen(db_opts[i]), " ");
364 fprintf(new, "/* %s not set */\n", name);
368 if (i < sizeof(db_opts)/sizeof(char *)) continue;
370 /* EXIM_USER is a special case. We look in the environment for EXIM_USER or
371 EXIM_UID (the latter for backward compatibility with Exim 3). If the value is
372 not numeric, we look up the user, and default the GID if found. Otherwise,
373 EXIM_GROUP or EXIM_GID must be in the environment. */
375 if (strcmp(name, "EXIM_UID") == 0)
381 char *username = NULL;
382 char *groupname = NULL;
384 char *user = getenv("EXIM_USER");
385 char *group = getenv("EXIM_GROUP");
387 if (user == NULL) user = getenv("EXIM_UID");
388 if (group == NULL) group = getenv("EXIM_GID");
392 printf("\n*** EXIM_USER has not been defined in any of the Makefiles in "
393 "the\n \"Local\" directory. Please review your build-time "
394 "configuration.\n\n");
398 while (isspace((unsigned char)(*user))) user++;
401 printf("\n*** EXIM_USER is defined as an empty string in one of the "
402 "files\n in the \"Local\" directory. Please review your build-time"
403 "\n configuration.\n\n");
407 for (s = user; *s != 0; s++)
409 if (iscntrl((unsigned char)(*s)))
411 printf("\n*** EXIM_USER contains the control character 0x%02X in one "
412 "of the files\n in the \"Local\" directory. Please review your "
413 "build-time\n configuration.\n\n", *s);
418 /* Numeric uid given */
420 if (user[strspn(user, "0123456789")] == 0)
422 uid = (uid_t)atoi(user);
425 /* User name given. Normally, we look up the uid right away. However,
426 people building binary distributions sometimes want to retain the name till
427 runtime. This is supported if the name begins "ref:". */
429 else if (strncmp(user, "ref:", 4) == 0)
432 while (isspace(*user)) user++;
440 struct passwd *pw = getpwnam(user);
443 printf("\n*** User \"%s\" (specified in one of the Makefiles) does not "
444 "exist.\n Please review your build-time configuration.\n\n",
454 /* Use explicit group if set. */
458 while (isspace((unsigned char)(*group))) group++;
461 printf("\n*** EXIM_GROUP is defined as an empty string in one of "
462 "the files in the\n \"Local\" directory. ");
465 printf("If you want the Exim group to be taken from the\n "
466 "password data for the Exim user, just remove the EXIM_GROUP "
467 "setting.\n Otherwise, p");
469 else printf("EXIM_USER is defined numerically, so there is no"
470 "\n default for EXIM_GROUP and you must set it explicitly.\n P");
471 printf("lease review your build-time configuration.\n\n");
475 for (s = group; *s != 0; s++)
477 if (iscntrl((unsigned char)(*s)))
479 printf("\n*** EXIM_GROUP contains the control character 0x%02X in one "
480 "of the files\n in the \"Local\" directory. Please review your "
481 "build-time\n configuration.\n\n", *s);
486 /* Group name given. This may be by reference or to be looked up now,
489 if (strncmp(group, "ref:", 4) == 0)
492 while (isspace(*group)) group++;
496 else if (username != NULL)
501 else if (group[strspn(group, "0123456789")] == 0)
503 gid = (gid_t)atoi(group);
508 struct group *gr = getgrnam(group);
511 printf("\n*** Group \"%s\" (specified in one of the Makefiles) does "
512 "not exist.\n Please review your build-time configuration.\n\n",
520 /* Else trouble unless found in passwd file with user */
524 printf("\n*** No group set for Exim. Please review your build-time "
525 "configuration.\n\n");
529 /* security sanity checks
530 if ref: is being used, we can never be sure, but we can take reasonable
531 steps to filter out the most obvious ones. */
533 if ((!uid_not_set && uid == 0) ||
534 (strcmp(username, "root") == 0) ||
535 (strcmp(username, "toor") == 0) )
537 printf("\n*** Exim's internal user must not be root.\n\n");
541 /* Output user and group names or uid/gid. When names are set, uid/gid
542 are set to zero but will be replaced at runtime. */
544 if (username != NULL)
545 fprintf(new, "#define EXIM_USERNAME \"%s\"\n", username);
546 if (groupname != NULL)
547 fprintf(new, "#define EXIM_GROUPNAME \"%s\"\n", groupname);
549 fprintf(new, "#define EXIM_UID %d\n", (int)uid);
550 fprintf(new, "#define EXIM_GID %d\n", (int)gid);
554 /* CONFIGURE_OWNER and CONFIGURE_GROUP are special cases. We look in the
555 environment for first. If the value is not numeric, we look up the user or
556 group. A lot of this code is similar to that for EXIM_USER, but it's easier
557 to keep it separate. */
559 if (strcmp(name, "CONFIGURE_OWNER") == 0 ||
560 strcmp(name, "CONFIGURE_GROUP") == 0)
562 int isgroup = name[10] == 'G';
566 char *username = NULL;
567 char *user = getenv(name);
569 if (user == NULL) user = "";
570 while (isspace((unsigned char)(*user))) user++;
573 fprintf(new, "/* %s not set */\n", name);
577 for (s = user; *s != 0; s++)
579 if (iscntrl((unsigned char)(*s)))
581 printf("\n*** %s contains the control character 0x%02X in "
582 "one of the files\n in the \"Local\" directory. Please review "
583 "your build-time\n configuration.\n\n", name, *s);
588 /* Numeric uid given */
590 if (user[strspn(user, "0123456789")] == 0)
593 gid = (gid_t)atoi(user);
595 uid = (uid_t)atoi(user);
598 /* Name given. Normally, we look up the uid or gid right away. However,
599 people building binary distributions sometimes want to retain the name till
600 runtime. This is supported if the name begins "ref:". */
602 else if (strncmp(user, "ref:", 4) == 0)
605 while (isspace(*user)) user++;
611 struct group *gr = getgrnam(user);
614 printf("\n*** Group \"%s\" (specified in one of the Makefiles) does not "
615 "exist.\n Please review your build-time configuration.\n\n",
624 struct passwd *pw = getpwnam(user);
627 printf("\n*** User \"%s\" (specified in one of the Makefiles) does not "
628 "exist.\n Please review your build-time configuration.\n\n",
635 /* Output user and group names or uid/gid. When names are set, uid/gid
636 are set to zero but will be replaced at runtime. */
638 if (username != NULL)
641 fprintf(new, "#define CONFIGURE_GROUPNAME \"%s\"\n", username);
643 fprintf(new, "#define CONFIGURE_OWNERNAME \"%s\"\n", username);
647 fprintf(new, "#define CONFIGURE_GROUP %d\n", (int)gid);
649 fprintf(new, "#define CONFIGURE_OWNER %d\n", (int)uid);
653 /* FIXED_NEVER_USERS is another special case. Look up the uid values and
654 create suitable initialization data for a vector. */
656 if (strcmp(name, "FIXED_NEVER_USERS") == 0)
658 char *list = getenv("FIXED_NEVER_USERS");
661 fprintf(new, "#define FIXED_NEVER_USERS 0\n");
669 while (*p != 0) if (*p++ == ':') count++;
671 vector = malloc((count+1) * sizeof(uid_t));
672 vector[0] = (uid_t)count;
674 for (i = 1, j = 0; i <= count; list++, i++)
679 while (*list != 0 && *list != ':') list++;
680 strncpy(name, p, list-p);
687 else if (name[strspn(name, "0123456789")] == 0)
689 vector[j++] = (uid_t)atoi(name);
693 struct passwd *pw = getpwnam(name);
696 printf("\n*** User \"%s\" (specified for FIXED_NEVER_USERS in one of the Makefiles) does not "
697 "exist.\n Please review your build-time configuration.\n\n",
701 vector[j++] = pw->pw_uid;
704 fprintf(new, "#define FIXED_NEVER_USERS %d", j);
705 for (i = 0; i < j; i++) fprintf(new, ", %d", (unsigned int)vector[i]);
711 /* WITH_CONTENT_SCAN is another special case: it must be set if either it or
712 WITH_OLD_DEMIME is set. */
714 if (strcmp(name, "WITH_CONTENT_SCAN") == 0)
716 char *wcs = getenv("WITH_CONTENT_SCAN");
717 char *wod = getenv("WITH_OLD_DEMIME");
718 char *dcc = getenv("EXPERIMENTAL_DCC");
719 if (wcs != NULL || wod != NULL || dcc != NULL)
720 fprintf(new, "#define WITH_CONTENT_SCAN yes\n");
721 else fprintf(new, "/* WITH_CONTENT_SCAN not set */\n");
725 /* Otherwise, check whether a value exists in the environment. Remember if
726 it is an AUTH setting or SUPPORT_CRYPTEQ. */
728 if ((value = getenv(name)) != NULL)
731 len = 21 - (int)strlen(name);
733 if (strncmp(name, "AUTH_", 5) == 0) have_auth = 1;
734 if (strncmp(name, "SUPPORT_CRYPTEQ", 15) == 0) support_crypteq = 1;
736 /* The text value of LDAP_LIB_TYPE refers to a macro that gets set. */
738 if (strcmp(name, "LDAP_LIB_TYPE") == 0)
740 if (strcmp(value, "NETSCAPE") == 0 ||
741 strcmp(value, "UMICHIGAN") == 0 ||
742 strcmp(value, "OPENLDAP1") == 0 ||
743 strcmp(value, "OPENLDAP2") == 0 ||
744 strcmp(value, "SOLARIS") == 0 ||
745 strcmp(value, "SOLARIS7") == 0) /* Compatibility */
747 fprintf(new, "#define LDAP_LIB_%s\n", value);
751 printf("\n*** LDAP_LIB_TYPE=%s is not a recognized LDAP library type."
752 "\n*** Please review your build-time configuration.\n\n", value);
757 else if (strcmp(name, "RADIUS_LIB_TYPE") == 0)
759 if (strcmp(value, "RADIUSCLIENT") == 0 ||
760 strcmp(value, "RADIUSCLIENTNEW") == 0 ||
761 strcmp(value, "RADLIB") == 0)
763 fprintf(new, "#define RADIUS_LIB_%s\n", value);
767 printf("\n*** RADIUS_LIB_TYPE=%s is not a recognized RADIUS library type."
768 "\n*** Please review your build-time configuration.\n\n", value);
773 /* Other macros get set to the environment value. */
777 fprintf(new, "#define %s ", name);
778 while(len-- > 0) fputc(' ', new);
780 /* LOG_FILE_PATH is now messy because it can be a path containing %s or
781 it can be "syslog" or ":syslog" or "syslog:path" or even "path:syslog". */
783 if (strcmp(name, "LOG_FILE_PATH") == 0)
789 char *sss = strchr(ss, ':');
792 strncpy(buffer, ss, sss-ss);
793 buffer[sss-ss] = 0; /* For empty case */
795 else strcpy(buffer, ss);
796 pp = buffer + (int)strlen(buffer);
797 while (pp > buffer && isspace((unsigned char)pp[-1])) pp--;
799 if (buffer[0] != 0 && strcmp(buffer, "syslog") != 0)
800 check_percent_ess(buffer, name);
801 if (sss == NULL) break;
803 while (isspace((unsigned char)*ss)) ss++;
805 fprintf(new, "\"%s\"\n", value);
808 /* Timezone values and HEADERS_CHARSET get quoted */
810 else if (strcmp(name, "TIMEZONE_DEFAULT") == 0||
811 strcmp(name, "HEADERS_CHARSET") == 0)
812 fprintf(new, "\"%s\"\n", value);
814 /* For others, quote any paths and don't quote anything else */
818 if (value[0] == '/') fprintf(new, "\"%s\"\n", value);
819 else fprintf(new, "%s\n", value);
824 /* Value not defined in the environment; use the default */
829 while (*p == ' ' || *p == '\t') p++;
830 if (*p != '\n') fputs(buffer, new); else
833 if (strcmp(name, "BIN_DIRECTORY") == 0 ||
834 strcmp(name, "CONFIGURE_FILE") == 0)
836 printf("\n*** %s has not been defined in any of the Makefiles in the\n"
837 " \"Local\" directory. "
838 "Please review your build-time configuration.\n\n", name);
842 if (strcmp(name, "TIMEZONE_DEFAULT") == 0)
844 char *tz = getenv("TZ");
845 fprintf(new, "#define TIMEZONE_DEFAULT ");
846 if (tz == NULL) fprintf(new, "NULL\n"); else
847 fprintf(new, "\"%s\"\n", tz);
850 else fprintf(new, "/* %s not set */\n", name);
857 /* If any AUTH macros were defined, ensure that SUPPORT_CRYPTEQ is also
862 if (!support_crypteq) fprintf(new, "/* Force SUPPORT_CRYPTEQ for AUTH */\n"
863 "#define SUPPORT_CRYPTEQ\n");
868 fprintf(new, "\n/* End of config.h */\n");
873 /* End of buildconfig.c */