Guard loadable module vars with LOOKUP_MODULE_DIR.
[exim.git] / src / src / drtables.c
1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
4
5 /* Copyright (c) University of Cambridge 1995 - 2009 */
6 /* See the file NOTICE for conditions of use and distribution. */
7
8
9 #include "exim.h"
10
11 #include <string.h>
12
13 /* This module contains tables that define the lookup methods and drivers
14 that are actually included in the binary. Its contents are controlled by
15 various macros in config.h that ultimately come from Local/Makefile. They are
16 all described in src/EDITME. */
17
18
19 lookup_info **lookup_list;
20 int lookup_list_count = 0;
21
22 static int lookup_list_init_done = 0;
23
24 /* Table of information about all possible authentication mechamisms. All
25 entries are always present if any mechanism is declared, but the functions are
26 set to NULL for those that are not compiled into the binary. */
27
28 #ifdef AUTH_CRAM_MD5
29 #include "auths/cram_md5.h"
30 #endif
31
32 #ifdef AUTH_CYRUS_SASL
33 #include "auths/cyrus_sasl.h"
34 #endif
35
36 #ifdef AUTH_DOVECOT
37 #include "auths/dovecot.h"
38 #endif
39
40 #ifdef AUTH_GSASL
41 #include "auths/gsasl_exim.h"
42 #endif
43
44 #ifdef AUTH_HEIMDAL_GSSAPI
45 #include "auths/heimdal_gssapi.h"
46 #endif
47
48 #ifdef AUTH_PLAINTEXT
49 #include "auths/plaintext.h"
50 #endif
51
52 #ifdef AUTH_SPA
53 #include "auths/spa.h"
54 #endif
55
56 auth_info auths_available[] = {
57
58 /* Checking by an expansion condition on plain text */
59
60 #ifdef AUTH_CRAM_MD5
61 {
62 US"cram_md5", /* lookup name */
63 auth_cram_md5_options,
64 &auth_cram_md5_options_count,
65 &auth_cram_md5_option_defaults,
66 sizeof(auth_cram_md5_options_block),
67 auth_cram_md5_init, /* init function */
68 auth_cram_md5_server, /* server function */
69 auth_cram_md5_client, /* client function */
70 NULL /* diagnostic function */
71 },
72 #endif
73
74 #ifdef AUTH_CYRUS_SASL
75 {
76 US"cyrus_sasl", /* lookup name */
77 auth_cyrus_sasl_options,
78 &auth_cyrus_sasl_options_count,
79 &auth_cyrus_sasl_option_defaults,
80 sizeof(auth_cyrus_sasl_options_block),
81 auth_cyrus_sasl_init, /* init function */
82 auth_cyrus_sasl_server, /* server function */
83 NULL, /* client function */
84 auth_cyrus_sasl_version_report /* diagnostic function */
85 },
86 #endif
87
88 #ifdef AUTH_DOVECOT
89 {
90 US"dovecot", /* lookup name */
91 auth_dovecot_options,
92 &auth_dovecot_options_count,
93 &auth_dovecot_option_defaults,
94 sizeof(auth_dovecot_options_block),
95 auth_dovecot_init, /* init function */
96 auth_dovecot_server, /* server function */
97 NULL, /* client function */
98 NULL /* diagnostic function */
99 },
100 #endif
101
102 #ifdef AUTH_GSASL
103 {
104 US"gsasl", /* lookup name */
105 auth_gsasl_options,
106 &auth_gsasl_options_count,
107 &auth_gsasl_option_defaults,
108 sizeof(auth_gsasl_options_block),
109 auth_gsasl_init, /* init function */
110 auth_gsasl_server, /* server function */
111 NULL, /* client function */
112 auth_gsasl_version_report /* diagnostic function */
113 },
114 #endif
115
116 #ifdef AUTH_HEIMDAL_GSSAPI
117 {
118 US"heimdal_gssapi", /* lookup name */
119 auth_heimdal_gssapi_options,
120 &auth_heimdal_gssapi_options_count,
121 &auth_heimdal_gssapi_option_defaults,
122 sizeof(auth_heimdal_gssapi_options_block),
123 auth_heimdal_gssapi_init, /* init function */
124 auth_heimdal_gssapi_server, /* server function */
125 NULL, /* client function */
126 auth_heimdal_gssapi_version_report /* diagnostic function */
127 },
128 #endif
129
130 #ifdef AUTH_PLAINTEXT
131 {
132 US"plaintext", /* lookup name */
133 auth_plaintext_options,
134 &auth_plaintext_options_count,
135 &auth_plaintext_option_defaults,
136 sizeof(auth_plaintext_options_block),
137 auth_plaintext_init, /* init function */
138 auth_plaintext_server, /* server function */
139 auth_plaintext_client, /* client function */
140 NULL /* diagnostic function */
141 },
142 #endif
143
144 #ifdef AUTH_SPA
145 {
146 US"spa", /* lookup name */
147 auth_spa_options,
148 &auth_spa_options_count,
149 &auth_spa_option_defaults,
150 sizeof(auth_spa_options_block),
151 auth_spa_init, /* init function */
152 auth_spa_server, /* server function */
153 auth_spa_client, /* client function */
154 NULL /* diagnostic function */
155 },
156 #endif
157
158 { US"", NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL }
159 };
160
161
162 /* Tables of information about which routers and transports are included in the
163 exim binary. */
164
165 /* Pull in the necessary header files */
166
167 #include "routers/rf_functions.h"
168
169 #ifdef ROUTER_ACCEPT
170 #include "routers/accept.h"
171 #endif
172
173 #ifdef ROUTER_DNSLOOKUP
174 #include "routers/dnslookup.h"
175 #endif
176
177 #ifdef ROUTER_MANUALROUTE
178 #include "routers/manualroute.h"
179 #endif
180
181 #ifdef ROUTER_IPLITERAL
182 #include "routers/ipliteral.h"
183 #endif
184
185 #ifdef ROUTER_IPLOOKUP
186 #include "routers/iplookup.h"
187 #endif
188
189 #ifdef ROUTER_QUERYPROGRAM
190 #include "routers/queryprogram.h"
191 #endif
192
193 #ifdef ROUTER_REDIRECT
194 #include "routers/redirect.h"
195 #endif
196
197 #ifdef TRANSPORT_APPENDFILE
198 #include "transports/appendfile.h"
199 #endif
200
201 #ifdef TRANSPORT_AUTOREPLY
202 #include "transports/autoreply.h"
203 #endif
204
205 #ifdef TRANSPORT_LMTP
206 #include "transports/lmtp.h"
207 #endif
208
209 #ifdef TRANSPORT_PIPE
210 #include "transports/pipe.h"
211 #endif
212
213 #ifdef TRANSPORT_SMTP
214 #include "transports/smtp.h"
215 #endif
216
217
218 /* Now set up the structures, terminated by an entry with a null name. */
219
220 router_info routers_available[] = {
221 #ifdef ROUTER_ACCEPT
222 {
223 US"accept",
224 accept_router_options,
225 &accept_router_options_count,
226 &accept_router_option_defaults,
227 sizeof(accept_router_options_block),
228 accept_router_init,
229 accept_router_entry,
230 NULL, /* no tidyup entry */
231 ri_yestransport
232 },
233 #endif
234 #ifdef ROUTER_DNSLOOKUP
235 {
236 US"dnslookup",
237 dnslookup_router_options,
238 &dnslookup_router_options_count,
239 &dnslookup_router_option_defaults,
240 sizeof(dnslookup_router_options_block),
241 dnslookup_router_init,
242 dnslookup_router_entry,
243 NULL, /* no tidyup entry */
244 ri_yestransport
245 },
246 #endif
247 #ifdef ROUTER_IPLITERAL
248 {
249 US"ipliteral",
250 ipliteral_router_options,
251 &ipliteral_router_options_count,
252 &ipliteral_router_option_defaults,
253 sizeof(ipliteral_router_options_block),
254 ipliteral_router_init,
255 ipliteral_router_entry,
256 NULL, /* no tidyup entry */
257 ri_yestransport
258 },
259 #endif
260 #ifdef ROUTER_IPLOOKUP
261 {
262 US"iplookup",
263 iplookup_router_options,
264 &iplookup_router_options_count,
265 &iplookup_router_option_defaults,
266 sizeof(iplookup_router_options_block),
267 iplookup_router_init,
268 iplookup_router_entry,
269 NULL, /* no tidyup entry */
270 ri_notransport
271 },
272 #endif
273 #ifdef ROUTER_MANUALROUTE
274 {
275 US"manualroute",
276 manualroute_router_options,
277 &manualroute_router_options_count,
278 &manualroute_router_option_defaults,
279 sizeof(manualroute_router_options_block),
280 manualroute_router_init,
281 manualroute_router_entry,
282 NULL, /* no tidyup entry */
283 0
284 },
285 #endif
286 #ifdef ROUTER_QUERYPROGRAM
287 {
288 US"queryprogram",
289 queryprogram_router_options,
290 &queryprogram_router_options_count,
291 &queryprogram_router_option_defaults,
292 sizeof(queryprogram_router_options_block),
293 queryprogram_router_init,
294 queryprogram_router_entry,
295 NULL, /* no tidyup entry */
296 0
297 },
298 #endif
299 #ifdef ROUTER_REDIRECT
300 {
301 US"redirect",
302 redirect_router_options,
303 &redirect_router_options_count,
304 &redirect_router_option_defaults,
305 sizeof(redirect_router_options_block),
306 redirect_router_init,
307 redirect_router_entry,
308 NULL, /* no tidyup entry */
309 ri_notransport
310 },
311 #endif
312 { US"", NULL, NULL, NULL, 0, NULL, NULL, NULL, 0 }
313 };
314
315
316
317 transport_info transports_available[] = {
318 #ifdef TRANSPORT_APPENDFILE
319 {
320 US"appendfile", /* driver name */
321 appendfile_transport_options, /* local options table */
322 &appendfile_transport_options_count, /* number of entries */
323 &appendfile_transport_option_defaults, /* private options defaults */
324 sizeof(appendfile_transport_options_block), /* size of private block */
325 appendfile_transport_init, /* init entry point */
326 appendfile_transport_entry, /* main entry point */
327 NULL, /* no tidyup entry */
328 NULL, /* no closedown entry */
329 TRUE, /* local flag */
330 },
331 #endif
332 #ifdef TRANSPORT_AUTOREPLY
333 {
334 US"autoreply", /* driver name */
335 autoreply_transport_options, /* local options table */
336 &autoreply_transport_options_count, /* number of entries */
337 &autoreply_transport_option_defaults, /* private options defaults */
338 sizeof(autoreply_transport_options_block), /* size of private block */
339 autoreply_transport_init, /* init entry point */
340 autoreply_transport_entry, /* main entry point */
341 NULL, /* no tidyup entry */
342 NULL, /* no closedown entry */
343 TRUE /* local flag */
344 },
345 #endif
346 #ifdef TRANSPORT_LMTP
347 {
348 US"lmtp", /* driver name */
349 lmtp_transport_options, /* local options table */
350 &lmtp_transport_options_count, /* number of entries */
351 &lmtp_transport_option_defaults, /* private options defaults */
352 sizeof(lmtp_transport_options_block), /* size of private block */
353 lmtp_transport_init, /* init entry point */
354 lmtp_transport_entry, /* main entry point */
355 NULL, /* no tidyup entry */
356 NULL, /* no closedown entry */
357 TRUE /* local flag */
358 },
359 #endif
360 #ifdef TRANSPORT_PIPE
361 {
362 US"pipe", /* driver name */
363 pipe_transport_options, /* local options table */
364 &pipe_transport_options_count, /* number of entries */
365 &pipe_transport_option_defaults, /* private options defaults */
366 sizeof(pipe_transport_options_block), /* size of private block */
367 pipe_transport_init, /* init entry point */
368 pipe_transport_entry, /* main entry point */
369 NULL, /* no tidyup entry */
370 NULL, /* no closedown entry */
371 TRUE /* local flag */
372 },
373 #endif
374 #ifdef TRANSPORT_SMTP
375 {
376 US"smtp", /* driver name */
377 smtp_transport_options, /* local options table */
378 &smtp_transport_options_count, /* number of entries */
379 &smtp_transport_option_defaults, /* private options defaults */
380 sizeof(smtp_transport_options_block), /* size of private block */
381 smtp_transport_init, /* init entry point */
382 smtp_transport_entry, /* main entry point */
383 NULL, /* no tidyup entry */
384 smtp_transport_closedown, /* close down passed channel */
385 FALSE /* local flag */
386 },
387 #endif
388 { US"", NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, FALSE }
389 };
390
391 struct lookupmodulestr
392 {
393 void *dl;
394 struct lookup_module_info *info;
395 struct lookupmodulestr *next;
396 };
397
398 static struct lookupmodulestr *lookupmodules = NULL;
399
400 static void addlookupmodule(void *dl, struct lookup_module_info *info)
401 {
402 struct lookupmodulestr *p = store_malloc(sizeof(struct lookupmodulestr));
403 p->dl = dl;
404 p->info = info;
405 p->next = lookupmodules;
406 lookupmodules = p;
407 lookup_list_count += info->lookupcount;
408 }
409
410 /* only valid after lookup_list and lookup_list_count are assigned */
411 static void add_lookup_to_list(lookup_info *info)
412 {
413 /* need to add the lookup to lookup_list, sorted */
414 int pos = 0;
415
416 /* strategy is to go through the list until we find
417 * either an empty spot or a name that is higher.
418 * this can't fail because we have enough space. */
419 while (lookup_list[pos]
420 && (Ustrcmp(lookup_list[pos]->name, info->name) <= 0)) {
421 pos++;
422 }
423 if (lookup_list[pos]) {
424 /* need to insert it, so move all the other items up
425 * (last slot is still empty, of course) */
426 memmove(&lookup_list[pos+1],
427 &lookup_list[pos],
428 sizeof(lookup_info **) * (lookup_list_count-pos-1));
429 }
430 lookup_list[pos] = info;
431 }
432
433
434 /* These need to be at file level for old versions of gcc (2.95.2 reported),
435 * which give parse errors on an extern in function scope. Each entry needs
436 * to also be invoked in init_lookup_list() below */
437
438 #if defined(LOOKUP_WHOSON) && LOOKUP_WHOSON!=2
439 extern lookup_module_info whoson_lookup_module_info;
440 #endif
441 #if defined(LOOKUP_TESTDB) && LOOKUP_TESTDB!=2
442 extern lookup_module_info testdb_lookup_module_info;
443 #endif
444 #if defined(LOOKUP_SQLITE) && LOOKUP_SQLITE!=2
445 extern lookup_module_info sqlite_lookup_module_info;
446 #endif
447 #ifdef EXPERIMENTAL_SPF
448 extern lookup_module_info spf_lookup_module_info;
449 #endif
450 #if defined(LOOKUP_PGSQL) && LOOKUP_PGSQL!=2
451 extern lookup_module_info pgsql_lookup_module_info;
452 #endif
453 #if defined(LOOKUP_PASSWD) && LOOKUP_PASSWD!=2
454 extern lookup_module_info passwd_lookup_module_info;
455 #endif
456 #if defined(LOOKUP_ORACLE) && LOOKUP_ORACLE!=2
457 extern lookup_module_info oracle_lookup_module_info;
458 #endif
459 #if defined(LOOKUP_NISPLUS) && LOOKUP_NISPLUS!=2
460 extern lookup_module_info nisplus_lookup_module_info;
461 #endif
462 #if defined(LOOKUP_NIS) && LOOKUP_NIS!=2
463 extern lookup_module_info nis_lookup_module_info;
464 #endif
465 #if defined(LOOKUP_MYSQL) && LOOKUP_MYSQL!=2
466 extern lookup_module_info mysql_lookup_module_info;
467 #endif
468 #if defined(LOOKUP_LSEARCH) && LOOKUP_LSEARCH!=2
469 extern lookup_module_info lsearch_lookup_module_info;
470 #endif
471 #ifdef LOOKUP_LDAP
472 extern lookup_module_info ldap_lookup_module_info;
473 #endif
474 #if defined(LOOKUP_IBASE) && LOOKUP_IBASE!=2
475 extern lookup_module_info ibase_lookup_module_info;
476 #endif
477 #if defined(LOOKUP_DSEARCH) && LOOKUP_DSEARCH!=2
478 extern lookup_module_info dsearch_lookup_module_info;
479 #endif
480 #if defined(LOOKUP_DNSDB) && LOOKUP_DNSDB!=2
481 extern lookup_module_info dnsdb_lookup_module_info;
482 #endif
483 #if defined(LOOKUP_DBM) && LOOKUP_DBM!=2
484 extern lookup_module_info dbmdb_lookup_module_info;
485 #endif
486 #if defined(LOOKUP_CDB) && LOOKUP_CDB!=2
487 extern lookup_module_info cdb_lookup_module_info;
488 #endif
489
490 void init_lookup_list(void)
491 {
492 #ifdef LOOKUP_MODULE_DIR
493 DIR *dd;
494 struct dirent *ent;
495 int countmodules = 0;
496 int moduleerrors = 0;
497 #endif
498 struct lookupmodulestr *p;
499 const pcre *regex_islookupmod = regex_must_compile(
500 US"\\." DYNLIB_FN_EXT "$", FALSE, TRUE);
501
502 if (lookup_list_init_done)
503 return;
504 lookup_list_init_done = 1;
505
506 #if defined(LOOKUP_CDB) && LOOKUP_CDB!=2
507 addlookupmodule(NULL, &cdb_lookup_module_info);
508 #endif
509
510 #if defined(LOOKUP_DBM) && LOOKUP_DBM!=2
511 addlookupmodule(NULL, &dbmdb_lookup_module_info);
512 #endif
513
514 #if defined(LOOKUP_DNSDB) && LOOKUP_DNSDB!=2
515 addlookupmodule(NULL, &dnsdb_lookup_module_info);
516 #endif
517
518 #if defined(LOOKUP_DSEARCH) && LOOKUP_DSEARCH!=2
519 addlookupmodule(NULL, &dsearch_lookup_module_info);
520 #endif
521
522 #if defined(LOOKUP_IBASE) && LOOKUP_IBASE!=2
523 addlookupmodule(NULL, &ibase_lookup_module_info);
524 #endif
525
526 #ifdef LOOKUP_LDAP
527 addlookupmodule(NULL, &ldap_lookup_module_info);
528 #endif
529
530 #if defined(LOOKUP_LSEARCH) && LOOKUP_LSEARCH!=2
531 addlookupmodule(NULL, &lsearch_lookup_module_info);
532 #endif
533
534 #if defined(LOOKUP_MYSQL) && LOOKUP_MYSQL!=2
535 addlookupmodule(NULL, &mysql_lookup_module_info);
536 #endif
537
538 #if defined(LOOKUP_NIS) && LOOKUP_NIS!=2
539 addlookupmodule(NULL, &nis_lookup_module_info);
540 #endif
541
542 #if defined(LOOKUP_NISPLUS) && LOOKUP_NISPLUS!=2
543 addlookupmodule(NULL, &nisplus_lookup_module_info);
544 #endif
545
546 #if defined(LOOKUP_ORACLE) && LOOKUP_ORACLE!=2
547 addlookupmodule(NULL, &oracle_lookup_module_info);
548 #endif
549
550 #if defined(LOOKUP_PASSWD) && LOOKUP_PASSWD!=2
551 addlookupmodule(NULL, &passwd_lookup_module_info);
552 #endif
553
554 #if defined(LOOKUP_PGSQL) && LOOKUP_PGSQL!=2
555 addlookupmodule(NULL, &pgsql_lookup_module_info);
556 #endif
557
558 #ifdef EXPERIMENTAL_SPF
559 addlookupmodule(NULL, &spf_lookup_module_info);
560 #endif
561
562 #if defined(LOOKUP_SQLITE) && LOOKUP_SQLITE!=2
563 addlookupmodule(NULL, &sqlite_lookup_module_info);
564 #endif
565
566 #if defined(LOOKUP_TESTDB) && LOOKUP_TESTDB!=2
567 addlookupmodule(NULL, &testdb_lookup_module_info);
568 #endif
569
570 #if defined(LOOKUP_WHOSON) && LOOKUP_WHOSON!=2
571 addlookupmodule(NULL, &whoson_lookup_module_info);
572 #endif
573
574 #ifdef LOOKUP_MODULE_DIR
575 dd = opendir(LOOKUP_MODULE_DIR);
576 if (dd == NULL) {
577 DEBUG(D_lookup) debug_printf("Couldn't open %s: not loading lookup modules\n", LOOKUP_MODULE_DIR);
578 log_write(0, LOG_MAIN, "Couldn't open %s: not loading lookup modules\n", LOOKUP_MODULE_DIR);
579 }
580 else {
581 DEBUG(D_lookup) debug_printf("Loading lookup modules from %s\n", LOOKUP_MODULE_DIR);
582 while ((ent = readdir(dd)) != NULL) {
583 char *name = ent->d_name;
584 int len = (int)strlen(name);
585 if (pcre_exec(regex_islookupmod, NULL, name, len, 0, PCRE_EOPT, NULL, 0) >= 0) {
586 int pathnamelen = len + (int)strlen(LOOKUP_MODULE_DIR) + 2;
587 void *dl;
588 struct lookup_module_info *info;
589 const char *errormsg;
590
591 /* SRH: am I being paranoid here or what? */
592 if (pathnamelen > big_buffer_size) {
593 fprintf(stderr, "Loading lookup modules: %s/%s: name too long\n", LOOKUP_MODULE_DIR, name);
594 log_write(0, LOG_MAIN|LOG_PANIC, "%s/%s: name too long\n", LOOKUP_MODULE_DIR, name);
595 continue;
596 }
597
598 /* SRH: snprintf here? */
599 sprintf(CS big_buffer, "%s/%s", LOOKUP_MODULE_DIR, name);
600
601 dl = dlopen(CS big_buffer, RTLD_NOW);// TJ was LAZY
602 if (dl == NULL) {
603 fprintf(stderr, "Error loading %s: %s\n", name, dlerror());
604 moduleerrors++;
605 log_write(0, LOG_MAIN|LOG_PANIC, "Error loading lookup module %s: %s\n", name, dlerror());
606 continue;
607 }
608
609 /* FreeBSD nsdispatch() can trigger dlerror() errors about
610 * _nss_cache_cycle_prevention_function; we need to clear the dlerror()
611 * state before calling dlsym(), so that any error afterwards only
612 * comes from dlsym().
613 */
614 errormsg = dlerror();
615
616 info = (struct lookup_module_info*) dlsym(dl, "_lookup_module_info");
617 if ((errormsg = dlerror()) != NULL) {
618 fprintf(stderr, "%s does not appear to be a lookup module (%s)\n", name, errormsg);
619 dlclose(dl);
620 moduleerrors++;
621 log_write(0, LOG_MAIN|LOG_PANIC, "%s does not appear to be a lookup module (%s)\n", name, errormsg);
622 continue;
623 }
624 if (info->magic != LOOKUP_MODULE_INFO_MAGIC) {
625 fprintf(stderr, "Lookup module %s is not compatible with this version of Exim\n", name);
626 dlclose(dl);
627 moduleerrors++;
628 log_write(0, LOG_MAIN|LOG_PANIC, "Lookup module %s is not compatible with this version of Exim\n", name);
629 continue;
630 }
631
632 addlookupmodule(dl, info);
633 DEBUG(D_lookup) debug_printf("Loaded \"%s\" (%d lookup types)\n", name, info->lookupcount);
634 countmodules++;
635 }
636 }
637 closedir(dd);
638 }
639
640 DEBUG(D_lookup) debug_printf("Loaded %d lookup modules\n", countmodules);
641 #endif
642
643 store_free((void*)regex_islookupmod);
644
645 DEBUG(D_lookup) debug_printf("Total %d lookups\n", lookup_list_count);
646
647 lookup_list = store_malloc(sizeof(lookup_info *) * lookup_list_count);
648 memset(lookup_list, 0, sizeof(lookup_info *) * lookup_list_count);
649
650 /* now add all lookups to the real list */
651 p = lookupmodules;
652 while (p) {
653 int j;
654 struct lookupmodulestr *pnext;
655
656 for (j = 0; j < p->info->lookupcount; j++)
657 add_lookup_to_list(p->info->lookups[j]);
658
659 pnext = p->next;
660 store_free(p);
661 p = pnext;
662 }
663 /* just to be sure */
664 lookupmodules = NULL;
665 }
666
667 /* End of drtables.c */