Added CONFIGURE_GROUP as a build-time facility, cf CONFIGURE_OWNER.
[exim.git] / src / src / buildconfig.c
CommitLineData
35edf2ff 1/* $Cambridge: exim/src/src/buildconfig.c,v 1.2 2004/10/18 09:16:57 ph10 Exp $ */
059ec3d9
PH
2
3/*************************************************
4* Exim - an Internet mail transport agent *
5*************************************************/
6
7/* Copyright (c) University of Cambridge 1995 - 2004 */
8/* See the file NOTICE for conditions of use and distribution. */
9
10
11/*************************************************
12* Build configuration header for Exim *
13*************************************************/
14
15/* This auxiliary program builds the file config.h by the following
16process:
17
18First it reads Makefile, looking for certain OS-specific definitions which it
19uses to define macros. Then it reads the defaults file config.h.defaults.
20
21The defaults file contains normal C #define statements for various macros; if
22the name of a macro is found in the environment, the environment value replaces
23the default. If the default #define does not contain any value, then that macro
24is not copied to the created file unless there is some value in the
25environment.
26
27This program is compiled and run as part of the Make process and is not
28normally called independently. */
29
30
31#include <ctype.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <sys/types.h>
36#include <pwd.h>
37#include <grp.h>
38
39typedef struct {
40 char *name;
41 int *flag;
42} have_item;
43
44typedef struct {
45 char *name;
46 char *data;
47} save_item;
48
49static char *db_opts[] = { "", "USE_DB", "USE_GDBM", "USE_TDB" };
50
51static int have_ipv6 = 0;
52static int have_iconv = 0;
53
54static char errno_quota[256];
55static char ostype[256];
56static char cc[256];
57
58/* If any entry is an initial substring of another, the longer one must
59appear first. */
60
61static have_item have_list[] = {
62 { "HAVE_IPV6", &have_ipv6 },
63 { "HAVE_ICONV", &have_iconv },
64 { NULL, NULL}
65};
66
67static save_item save_list[] = {
68 { "ERRNO_QUOTA", errno_quota },
69 { "OSTYPE", ostype },
70 { "CC", cc },
71 { NULL, NULL}
72};
73
74
75/* Subroutine to check a string for precisely one instance of "%s". If not,
76bomb out. */
77
78void
79check_percent_ess(char *value, char *name)
80{
81int OK = 0;
82char *p = strstr(value, "%s");
83if (p != NULL) OK = strstr(p+2, "%s") == NULL;
84if (!OK)
85 {
86 printf("\n*** \"%s\" (%s) must contain precisely one occurrence of\n"
87 "*** \"%%s\". Please review your build-time configuration.\n\n/", value,
88 name);
89 exit(1);
90 }
91}
92
93
94/* Main program */
95
96int
97main(int argc, char **argv)
98{
99FILE *base;
100FILE *new;
101int last_initial = 'A';
102int linecount = 0;
103int have_auth = 0;
104int in_local_makefile = 0;
105int use_which_db = 0;
106int use_which_db_in_local_makefile = 0;
107int support_crypteq = 0;
108char buffer[1024];
109
110if (argc != 1)
111 {
112 printf("*** Buildconfig: called with incorrect arguments\n");
113 exit(1);
114 }
115
116new = fopen("config.h", "wb");
117if (new == NULL)
118 {
119 printf("*** Buildconfig: failed to open config.h for output\n");
120 exit(1);
121 }
122
123printf("Building configuration file config.h\n");
124
125fprintf(new, "/*************************************************\n");
126fprintf(new, "* Configuration header for Exim *\n");
127fprintf(new, "*************************************************/\n\n");
128
129fprintf(new, "/* This file was automatically generated from Makefile and "
130 "config.h.defaults,\n");
131fprintf(new, "using values specified in the configuration file Local/Makefile.\n");
132fprintf(new, "Do not edit it. Instead, edit Local/Makefile and "
133 "rerun make. */\n\n");
134
135/* First, search the makefile for certain settings */
136
137base = fopen("Makefile", "rb");
138if (base == NULL)
139 {
140 printf("*** Buildconfig: failed to open Makefile\n");
141 fclose(new);
142 exit(1);
143 }
144
145errno_quota[0] = 0; /* no over-riding value set */
146ostype[0] = 0; /* just in case */
147cc[0] = 0;
148
149while (fgets(buffer, sizeof(buffer), base) != NULL)
150 {
151 int i;
152 have_item *h;
153 save_item *s;
154 char *p = buffer + (int)strlen(buffer);
155 linecount++;
156 while (p > buffer && isspace((unsigned char)p[-1])) p--;
157 *p = 0;
158 p = buffer;
159 while (isspace((unsigned char)*p)) p++;
160
161 /* Notice when we hit the user's makefile */
162
163 if (strcmp(p, "# From Local/Makefile") == 0)
164 {
165 in_local_makefile = 1;
166 continue;
167 }
168
169 /* Remember the last DB option setting. If we hit two in the user's
170 Makefile, complain. */
171
172 for (i = 1; i < sizeof(db_opts)/sizeof(char *); i++)
173 {
174 int len = (int)strlen(db_opts[i]);
175 if (strncmp(p, db_opts[i], len) == 0 && (p[len] == ' ' || p[len] == '='))
176 {
177 if (in_local_makefile)
178 {
179 if (use_which_db_in_local_makefile)
180 {
181 printf("*** Only one of USE_DB, USE_GDBM, or USE_TDB should be "
182 "defined in Local/Makefile\n");
183 exit(1);
184 }
185 use_which_db_in_local_makefile = 1;
186 }
187 use_which_db = i;
188 break;
189 }
190 }
191 if (i < sizeof(db_opts)/sizeof(char *)) continue;
192
193 /* Items where we just save a boolean */
194
195 for (h = have_list; h->name != NULL; h++)
196 {
197 int len = (int)strlen(h->name);
198 if (strncmp(p, h->name, len) == 0)
199 {
200 p += len;
201 while (isspace((unsigned char)*p)) p++;
202 if (*p++ != '=')
203 {
204 printf("*** Buildconfig: syntax error in Makefile line %d\n", linecount);
205 exit(1);
206 }
207 while (isspace((unsigned char)*p)) p++;
208 if (strcmp(p, "YES") == 0 || strcmp(p, "yes") == 0) *(h->flag) = 1;
209 else *(h->flag) = 0; /* Must reset in case multiple instances */
210 break;
211 }
212 }
213
214 if (h->name != NULL) continue;
215
216 /* Items where we save the complete string */
217
218 for (s = save_list; s->name != NULL; s++)
219 {
220 int len = (int)strlen(s->name);
221 if (strncmp(p, s->name, len) == 0)
222 {
223 p += len;
224 while (isspace((unsigned char)*p)) p++;
225 if (*p++ != '=')
226 {
227 printf("*** Buildconfig: syntax error in Makefile line %d\n", linecount);
228 exit(1);
229 }
230 while (isspace((unsigned char)*p)) p++;
231 strcpy(s->data, p);
232 }
233 }
234 }
235
236fprintf(new, "#define HAVE_IPV6 %s\n",
237 have_ipv6? "TRUE" : "FALSE");
238
239fprintf(new, "#define HAVE_ICONV %s\n",
240 have_iconv? "TRUE" : "FALSE");
241
242if (errno_quota[0] != 0)
243 fprintf(new, "\n#define ERRNO_QUOTA %s\n", errno_quota);
244
245if (strcmp(cc, "gcc") == 0 && strstr(ostype, "IRIX") != NULL)
246 {
247 fprintf(new, "\n/* This switch includes the code to fix the inet_ntoa() */");
248 fprintf(new, "\n/* bug when using gcc on an IRIX system. */");
249 fprintf(new, "\n#define USE_INET_NTOA_FIX");
250 }
251
252fprintf(new, "\n");
253fclose(base);
254
255
256/* Now handle the macros listed in the defaults */
257
258base = fopen("../src/config.h.defaults", "rb");
259if (base == NULL)
260 {
261 printf("*** Buildconfig: failed to open ../src/config.h.defaults\n");
262 fclose(new);
263 exit(1);
264 }
265
266while (fgets(buffer, sizeof(buffer), base) != NULL)
267 {
268 int i;
269 char name[256];
270 char *value;
271 char *p = buffer;
272 char *q = name;
273
274 while (*p == ' ' || *p == '\t') p++;
275
276 if (strncmp(p, "#define ", 8) != 0) continue;
277
278 p += 8;
279 while (*p == ' ' || *p == '\t') p++;
280
281 if (*p < last_initial) fprintf(new, "\n");
282 last_initial = *p;
283
284 while (*p && (isalnum((unsigned char)*p) || *p == '_')) *q++ = *p++;
285 *q = 0;
286
287 /* USE_DB, USE_GDBM, and USE_TDB are special cases. We want to have only
288 one of them set. The scan of the Makefile has saved which was the last one
289 encountered. */
290
291 for (i = 1; i < sizeof(db_opts)/sizeof(char *); i++)
292 {
293 if (strcmp(name, db_opts[i]) == 0)
294 {
295 if (use_which_db == i)
296 fprintf(new, "#define %s %.*syes\n", db_opts[i],
297 21 - (int)strlen(db_opts[i]), " ");
298 else
299 fprintf(new, "/* %s not set */\n", name);
300 break;
301 }
302 }
303 if (i < sizeof(db_opts)/sizeof(char *)) continue;
304
305 /* EXIM_USER is a special case. We look in the environment for EXIM_USER or
306 EXIM_UID (the latter for backward compatibility with Exim 3). If the value is
307 not numeric, we look up the user, and default the GID if found. Otherwise,
308 EXIM_GROUP or EXIM_GID must be in the environment. */
309
310 if (strcmp(name, "EXIM_UID") == 0)
311 {
312 uid_t uid = 0;
313 gid_t gid = 0;
314 int gid_set = 0;
315 char *username = NULL;
316 char *groupname = NULL;
317 char *s;
318 char *user = getenv("EXIM_USER");
319 char *group = getenv("EXIM_GROUP");
320
321 if (user == NULL) user = getenv("EXIM_UID");
322 if (group == NULL) group = getenv("EXIM_GID");
323
324 if (user == NULL)
325 {
326 printf("\n*** EXIM_USER has not been defined in any of the Makefiles in "
327 "the\n \"Local\" directory. Please review your build-time "
328 "configuration.\n\n");
329 return 1;
330 }
331
332 while (isspace((unsigned char)(*user))) user++;
333 if (*user == 0)
334 {
335 printf("\n*** EXIM_USER is defined as an empty string in one of the "
336 "files\n in the \"Local\" directory. Please review your build-time"
337 "\n configuration.\n\n");
338 return 1;
339 }
340
341 for (s = user; *s != 0; s++)
342 {
343 if (iscntrl((unsigned char)(*s)))
344 {
345 printf("\n*** EXIM_USER contains the control character 0x%02X in one "
346 "of the files\n in the \"Local\" directory. Please review your "
347 "build-time\n configuration.\n\n", *s);
348 return 1;
349 }
350 }
351
352 /* Numeric uid given */
353
354 if (user[strspn(user, "0123456789")] == 0)
355 {
356 uid = (uid_t)atoi(user);
357 }
358
359 /* User name given. Normally, we look up the uid right away. However,
360 people building binary distributions sometimes want to retain the name till
361 runtime. This is supported if the name begins "ref:". */
362
363 else if (strncmp(user, "ref:", 4) == 0)
364 {
365 user += 4;
366 while (isspace(*user)) user++;
367 username = user;
368 gid_set = 1;
369 }
370
371 else
372 {
373 struct passwd *pw = getpwnam(user);
374 if (pw == NULL)
375 {
376 printf("\n*** User \"%s\" (specified in one of the Makefiles) does not "
377 "exist.\n Please review your build-time configuration.\n\n",
378 user);
379 return 1;
380 }
381
382 uid = pw->pw_uid;
383 gid = pw->pw_gid;
384 gid_set = 1;
385 }
386
387 /* Use explicit group if set. */
388
389 if (group != NULL)
390 {
391 while (isspace((unsigned char)(*group))) group++;
392 if (*group == 0)
393 {
394 printf("\n*** EXIM_GROUP is defined as an empty string in one of "
395 "the files in the\n \"Local\" directory. ");
396 if (gid_set)
397 {
398 printf("If you want the Exim group to be taken from the\n "
399 "password data for the Exim user, just remove the EXIM_GROUP "
400 "setting.\n Otherwise, p");
401 }
402 else printf("EXIM_USER is defined numerically, so there is no"
403 "\n default for EXIM_GROUP and you must set it explicitly.\n P");
404 printf("lease review your build-time configuration.\n\n");
405 return 1;
406 }
407
408 for (s = group; *s != 0; s++)
409 {
410 if (iscntrl((unsigned char)(*s)))
411 {
412 printf("\n*** EXIM_GROUP 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);
415 return 1;
416 }
417 }
418
419 /* Group name given. This may be by reference or to be looked up now,
420 as for user. */
421
422 if (strncmp(group, "ref:", 4) == 0)
423 {
424 group += 4;
425 while (isspace(*group)) group++;
426 groupname = group;
427 }
428
429 else if (username != NULL)
430 {
431 groupname = group;
432 }
433
434 else if (group[strspn(group, "0123456789")] == 0)
435 {
436 gid = (gid_t)atoi(group);
437 }
438
439 else
440 {
441 struct group *gr = getgrnam(group);
442 if (gr == NULL)
443 {
444 printf("\n*** Group \"%s\" (specified in one of the Makefiles) does "
445 "not exist.\n Please review your build-time configuration.\n\n",
446 group);
447 return 1;
448 }
449 gid = gr->gr_gid;
450 }
451 }
452
453 /* Else trouble unless found in passwd file with user */
454
455 else if (!gid_set)
456 {
457 printf("\n*** No group set for Exim. Please review your build-time "
458 "configuration.\n\n");
459 return 1;
460 }
461
462 /* Output user and group names or uid/gid. When names are set, uid/gid
463 are set to zero but will be replaced at runtime. */
464
465 if (username != NULL)
466 fprintf(new, "#define EXIM_USERNAME \"%s\"\n", username);
467 if (groupname != NULL)
468 fprintf(new, "#define EXIM_GROUPNAME \"%s\"\n", groupname);
469
470 fprintf(new, "#define EXIM_UID %d\n", (int)uid);
471 fprintf(new, "#define EXIM_GID %d\n", (int)gid);
472 continue;
473 }
474
35edf2ff
PH
475 /* CONFIGURE_OWNER and CONFIGURE_GROUP are special cases. We look in the
476 environment for first. If the value is not numeric, we look up the user or
477 group. A lot of this code is similar to that for EXIM_USER, but it's easier
478 to keep it separate. */
059ec3d9 479
35edf2ff
PH
480 if (strcmp(name, "CONFIGURE_OWNER") == 0 ||
481 strcmp(name, "CONFIGURE_GROUP") == 0)
059ec3d9 482 {
35edf2ff 483 int isgroup = name[10] == 'G';
059ec3d9 484 uid_t uid = 0;
35edf2ff 485 gid_t gid = 0;
059ec3d9
PH
486 char *s;
487 char *username = NULL;
35edf2ff 488 char *user = getenv(name);
059ec3d9
PH
489
490 if (user == NULL) user = "";
491 while (isspace((unsigned char)(*user))) user++;
492 if (*user == 0)
493 {
494 fprintf(new, "/* %s not set */\n", name);
495 continue;
496 }
497
498 for (s = user; *s != 0; s++)
499 {
500 if (iscntrl((unsigned char)(*s)))
501 {
35edf2ff 502 printf("\n*** %s contains the control character 0x%02X in "
059ec3d9 503 "one of the files\n in the \"Local\" directory. Please review "
35edf2ff 504 "your build-time\n configuration.\n\n", name, *s);
059ec3d9
PH
505 return 1;
506 }
507 }
508
509 /* Numeric uid given */
510
511 if (user[strspn(user, "0123456789")] == 0)
512 {
35edf2ff
PH
513 if (isgroup)
514 gid = (gid_t)atoi(user);
515 else
516 uid = (uid_t)atoi(user);
059ec3d9
PH
517 }
518
35edf2ff 519 /* Name given. Normally, we look up the uid or gid right away. However,
059ec3d9
PH
520 people building binary distributions sometimes want to retain the name till
521 runtime. This is supported if the name begins "ref:". */
522
523 else if (strncmp(user, "ref:", 4) == 0)
524 {
525 user += 4;
526 while (isspace(*user)) user++;
527 username = user;
528 }
529
35edf2ff
PH
530 else if (isgroup)
531 {
532 struct group *gr = getgrnam(user);
533 if (gr == NULL)
534 {
535 printf("\n*** Group \"%s\" (specified in one of the Makefiles) does not "
536 "exist.\n Please review your build-time configuration.\n\n",
537 user);
538 return 1;
539 }
540 gid = gr->gr_gid;
541 }
542
059ec3d9
PH
543 else
544 {
545 struct passwd *pw = getpwnam(user);
546 if (pw == NULL)
547 {
548 printf("\n*** User \"%s\" (specified in one of the Makefiles) does not "
549 "exist.\n Please review your build-time configuration.\n\n",
550 user);
551 return 1;
552 }
059ec3d9
PH
553 uid = pw->pw_uid;
554 }
555
556 /* Output user and group names or uid/gid. When names are set, uid/gid
557 are set to zero but will be replaced at runtime. */
558
559 if (username != NULL)
35edf2ff
PH
560 {
561 if (isgroup)
562 fprintf(new, "#define CONFIGURE_GROUPNAME \"%s\"\n", username);
563 else
564 fprintf(new, "#define CONFIGURE_OWNERNAME \"%s\"\n", username);
565 }
566
567 if (isgroup)
568 fprintf(new, "#define CONFIGURE_GROUP %d\n", (int)gid);
569 else
570 fprintf(new, "#define CONFIGURE_OWNER %d\n", (int)uid);
059ec3d9
PH
571 continue;
572 }
573
574 /* FIXED_NEVER_USERS is another special case. Look up the uid values and
575 create suitable initialization data for a vector. */
576
577 if (strcmp(name, "FIXED_NEVER_USERS") == 0)
578 {
579 char *list = getenv("FIXED_NEVER_USERS");
580 if (list == NULL)
581 {
582 fprintf(new, "#define FIXED_NEVER_USERS 0\n");
583 }
584 else
585 {
586 int count = 1;
587 int i;
588 uid_t *vector;
589 char *p = list;
590 while (*p != 0) if (*p++ == ':') count++;
591
592 vector = malloc((count+1) * sizeof(uid_t));
593 vector[0] = (uid_t)count;
594
595 for (i = 1; i <= count; list++, i++)
596 {
597 char name[64];
598 p = list;
599 while (*list != 0 && *list != ':') list++;
600 strncpy(name, p, list-p);
601 name[list-p] = 0;
602
603 if (name[strspn(name, "0123456789")] == 0)
604 {
605 vector[i] = (uid_t)atoi(name);
606 }
607 else
608 {
609 struct passwd *pw = getpwnam(name);
610 if (pw == NULL)
611 {
612 printf("\n*** User \"%s\" (specified for FIXED_NEVER_USERS in one of the Makefiles) does not "
613 "exist.\n Please review your build-time configuration.\n\n",
614 name);
615 return 1;
616 }
617 vector[i] = pw->pw_uid;
618 }
619 }
620 fprintf(new, "#define FIXED_NEVER_USERS %d, ", count);
621 for (i = 1; i <= count - 1; i++)
622 fprintf(new, "%d, ", (unsigned int)vector[i]);
623 fprintf(new, "%d\n", (unsigned int)vector[i]);
624 }
625 continue;
626 }
627
628 /* Otherwise, check whether a value exists in the environment. Remember if
629 it is an AUTH setting or SUPPORT_CRYPTEQ. */
630
631 if ((value = getenv(name)) != NULL)
632 {
633 int len;
634 len = 21 - (int)strlen(name);
635
636 if (strncmp(name, "AUTH_", 5) == 0) have_auth = 1;
637 if (strncmp(name, "SUPPORT_CRYPTEQ", 15) == 0) support_crypteq = 1;
638
639 /* The text value of LDAP_LIB_TYPE refers to a macro that gets set. */
640
641 if (strcmp(name, "LDAP_LIB_TYPE") == 0)
642 {
643 if (strcmp(value, "NETSCAPE") == 0 ||
644 strcmp(value, "UMICHIGAN") == 0 ||
645 strcmp(value, "OPENLDAP1") == 0 ||
646 strcmp(value, "OPENLDAP2") == 0 ||
647 strcmp(value, "SOLARIS") == 0 ||
648 strcmp(value, "SOLARIS7") == 0) /* Compatibility */
649 {
650 fprintf(new, "#define LDAP_LIB_%s\n", value);
651 }
652 else
653 {
654 printf("\n*** LDAP_LIB_TYPE=%s is not a recognized LDAP library type."
655 "\n*** Please review your build-time configuration.\n\n", value);
656 return 1;
657 }
658 }
659
660 else if (strcmp(name, "RADIUS_LIB_TYPE") == 0)
661 {
662 if (strcmp(value, "RADIUSCLIENT") == 0 ||
663 strcmp(value, "RADLIB") == 0)
664 {
665 fprintf(new, "#define RADIUS_LIB_%s\n", value);
666 }
667 else
668 {
669 printf("\n*** RADIUS_LIB_TYPE=%s is not a recognized RADIUS library type."
670 "\n*** Please review your build-time configuration.\n\n", value);
671 return 1;
672 }
673 }
674
675 /* Other macros get set to the environment value. */
676
677 else
678 {
679 fprintf(new, "#define %s ", name);
680 while(len-- > 0) fputc(' ', new);
681
682 /* LOG_FILE_PATH is now messy because it can be a path containing %s or
683 it can be "syslog" or ":syslog" or "syslog:path" or even "path:syslog". */
684
685 if (strcmp(name, "LOG_FILE_PATH") == 0)
686 {
687 char *ss = value;
688 for(;;)
689 {
690 char *pp;
691 char *sss = strchr(ss, ':');
692 if (sss != NULL)
693 {
694 strncpy(buffer, ss, sss-ss);
695 buffer[sss-ss] = 0; /* For empty case */
696 }
697 else strcpy(buffer, ss);
698 pp = buffer + (int)strlen(buffer);
699 while (pp > buffer && isspace((unsigned char)pp[-1])) pp--;
700 *pp = 0;
701 if (buffer[0] != 0 && strcmp(buffer, "syslog") != 0)
702 check_percent_ess(buffer, name);
703 if (sss == NULL) break;
704 ss = sss + 1;
705 while (isspace((unsigned char)*ss)) ss++;
706 }
707 fprintf(new, "\"%s\"\n", value);
708 }
709
710 /* Timezone values and HEADERS_CHARSET get quoted */
711
712 else if (strcmp(name, "TIMEZONE_DEFAULT") == 0||
713 strcmp(name, "HEADERS_CHARSET") == 0)
714 fprintf(new, "\"%s\"\n", value);
715
716 /* For others, quote any paths and don't quote anything else */
717
718 else
719 {
720 if (value[0] == '/') fprintf(new, "\"%s\"\n", value);
721 else fprintf(new, "%s\n", value);
722 }
723 }
724 }
725
726 /* Value not defined in the environment; use the default */
727
728 else
729 {
730 char *t = p;
731 while (*p == ' ' || *p == '\t') p++;
732 if (*p != '\n') fputs(buffer, new); else
733 {
734 *t = 0;
735 if (strcmp(name, "BIN_DIRECTORY") == 0 ||
736 strcmp(name, "CONFIGURE_FILE") == 0)
737 {
738 printf("\n*** %s has not been defined in any of the Makefiles in the\n"
739 " \"Local\" directory. "
740 "Please review your build-time configuration.\n\n", name);
741 return 1;
742 }
743
744 if (strcmp(name, "TIMEZONE_DEFAULT") == 0)
745 {
746 char *tz = getenv("TZ");
747 fprintf(new, "#define TIMEZONE_DEFAULT ");
748 if (tz == NULL) fprintf(new, "NULL\n"); else
749 fprintf(new, "\"%s\"\n", tz);
750 }
751
752 else fprintf(new, "/* %s not set */\n", name);
753 }
754 }
755 }
756
757fclose(base);
758
759/* If any AUTH macros were defined, ensure that SUPPORT_CRYPTEQ is also
760defined. */
761
762if (have_auth)
763 {
764 if (!support_crypteq) fprintf(new, "/* Force SUPPORT_CRYPTEQ for AUTH */\n"
765 "#define SUPPORT_CRYPTEQ\n");
766 }
767
768/* End off */
769
770fprintf(new, "\n/* End of config.h */\n");
771fclose(new);
772return 0;
773}
774
775/* End of buildconfig.c */