1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) The Exim Maintainers 2020 - 2023 */
6 /* Copyright (c) University of Cambridge 1995 - 2018 */
7 /* See the file NOTICE for conditions of use and distribution. */
8 /* SPDX-License-Identifier: GPL-2.0-or-later */
15 /* This module contains tables that define the lookup methods and drivers
16 that are actually included in the binary. Its contents are controlled by
17 various macros in config.h that ultimately come from Local/Makefile. They are
18 all described in src/EDITME. */
21 lookup_info **lookup_list;
22 int lookup_list_count = 0;
24 /* Table of information about all possible authentication mechanisms. 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. */
29 #include "auths/cram_md5.h"
32 #ifdef AUTH_CYRUS_SASL
33 #include "auths/cyrus_sasl.h"
37 #include "auths/dovecot.h"
41 #include "auths/external.h"
45 #include "auths/gsasl_exim.h"
48 #ifdef AUTH_HEIMDAL_GSSAPI
49 #include "auths/heimdal_gssapi.h"
53 #include "auths/plaintext.h"
57 #include "auths/spa.h"
61 #include "auths/tls.h"
64 auth_info * auths_available_newlist = NULL;
65 auth_info auths_available_oldarray[] = {
67 /* Checking by an expansion condition on plain text */
72 .driver_name = US"cram_md5", /* lookup name */
73 .options = auth_cram_md5_options,
74 .options_count = &auth_cram_md5_options_count,
75 .options_block = &auth_cram_md5_option_defaults,
76 .options_len = sizeof(auth_cram_md5_options_block),
77 .init = auth_cram_md5_init,
79 .servercode = auth_cram_md5_server,
80 .clientcode = auth_cram_md5_client,
81 .version_report = NULL,
82 .macros_create = NULL,
86 #ifdef AUTH_CYRUS_SASL
89 .driver_name = US"cyrus_sasl",
90 .options = auth_cyrus_sasl_options,
91 .options_count = &auth_cyrus_sasl_options_count,
92 .options_block = &auth_cyrus_sasl_option_defaults,
93 .options_len = sizeof(auth_cyrus_sasl_options_block),
94 .init = auth_cyrus_sasl_init,
96 .servercode = auth_cyrus_sasl_server,
98 .version_report = auth_cyrus_sasl_version_report,
99 .macros_create = NULL,
106 .driver_name = US"dovecot",
107 .options = auth_dovecot_options,
108 .options_count = &auth_dovecot_options_count,
109 .options_block = &auth_dovecot_option_defaults,
110 .options_len = sizeof(auth_dovecot_options_block),
111 .init = auth_dovecot_init,
113 .servercode = auth_dovecot_server,
115 .version_report = NULL,
116 .macros_create = NULL,
123 .driver_name = US"external",
124 .options = auth_external_options,
125 .options_count = &auth_external_options_count,
126 .options_block = &auth_external_option_defaults,
127 .options_len = sizeof(auth_external_options_block),
128 .init = auth_external_init,
130 .servercode = auth_external_server,
131 .clientcode = auth_external_client,
132 .version_report = NULL,
133 .macros_create = NULL,
140 .driver_name = US"gsasl",
141 .options = auth_gsasl_options,
142 .options_count = &auth_gsasl_options_count,
143 .options_block = &auth_gsasl_option_defaults,
144 .options_len = sizeof(auth_gsasl_options_block),
145 .init = auth_gsasl_init,
147 .servercode = auth_gsasl_server,
148 .clientcode = auth_gsasl_client,
149 .version_report = auth_gsasl_version_report,
150 .macros_create = auth_gsasl_macros,
154 #ifdef AUTH_HEIMDAL_GSSAPI
157 .driver_name = US"heimdal_gssapi",
158 .options = auth_heimdal_gssapi_options,
159 .options_count = &auth_heimdal_gssapi_options_count,
160 .options_block = &auth_heimdal_gssapi_option_defaults,
161 .options_len = sizeof(auth_heimdal_gssapi_options_block),
162 .init = auth_heimdal_gssapi_init,
164 .servercode = auth_heimdal_gssapi_server,
166 .version_report = auth_heimdal_gssapi_version_report,
167 .macros_create = NULL,
171 #ifdef AUTH_PLAINTEXT
174 .driver_name = US"plaintext",
175 .options = auth_plaintext_options,
176 .options_count = &auth_plaintext_options_count,
177 .options_block = &auth_plaintext_option_defaults,
178 .options_len = sizeof(auth_plaintext_options_block),
179 .init = auth_plaintext_init,
181 .servercode = auth_plaintext_server,
182 .clientcode = auth_plaintext_client,
183 .version_report = NULL,
184 .macros_create = NULL,
191 .driver_name = US"spa",
192 .options = auth_spa_options,
193 .options_count = &auth_spa_options_count,
194 .options_block = &auth_spa_option_defaults,
195 .options_len = sizeof(auth_spa_options_block),
196 .init = auth_spa_init,
198 .servercode = auth_spa_server,
199 .clientcode = auth_spa_client,
200 .version_report = NULL,
201 .macros_create = NULL,
208 .driver_name = US"tls",
209 .options = auth_tls_options,
210 .options_count = &auth_tls_options_count,
211 .options_block = &auth_tls_option_defaults,
212 .options_len = sizeof(auth_tls_options_block),
213 .init = auth_tls_init,
215 .servercode = auth_tls_server,
217 .version_report = NULL,
218 .macros_create = NULL,
222 { .drinfo = { .driver_name = US"" }} /* end marker */
226 /* Tables of information about which routers and transports are included in the
229 /* Pull in the necessary header files */
231 #include "routers/rf_functions.h"
234 #ifdef ROUTER_DNSLOOKUP
235 # include "routers/dnslookup.h"
238 #ifdef ROUTER_MANUALROUTE
239 # include "routers/manualroute.h"
242 #ifdef ROUTER_IPLITERAL
243 # include "routers/ipliteral.h"
246 #ifdef ROUTER_IPLOOKUP
247 # include "routers/iplookup.h"
250 #ifdef ROUTER_QUERYPROGRAM
251 # include "routers/queryprogram.h"
254 #ifdef ROUTER_REDIRECT
255 # include "routers/redirect.h"
258 #ifdef TRANSPORT_APPENDFILE
259 # include "transports/appendfile.h"
262 #ifdef TRANSPORT_AUTOREPLY
263 # include "transports/autoreply.h"
266 #ifdef TRANSPORT_LMTP
267 # include "transports/lmtp.h"
270 #ifdef TRANSPORT_PIPE
271 # include "transports/pipe.h"
274 #ifdef EXPERIMENTAL_QUEUEFILE
275 # include "transports/queuefile.h"
278 #ifdef TRANSPORT_SMTP
279 # include "transports/smtp.h"
283 router_info * routers_available_newlist = NULL;
285 /* Now set up the structures, terminated by an entry with a null name. */
287 router_info routers_available_oldarray[] = {
288 { .drinfo = { .driver_name = US"" }}
293 transport_info * transports_available_newlist = NULL;
294 transport_info transports_available_oldarray[] = {
295 #ifdef TRANSPORT_APPENDFILE
298 .driver_name = US"appendfile",
299 .options = appendfile_transport_options,
300 .options_count = &appendfile_transport_options_count,
301 .options_block = &appendfile_transport_option_defaults, /* private options defaults */
302 .options_len = sizeof(appendfile_transport_options_block),
303 .init = appendfile_transport_init,
305 .code = appendfile_transport_entry,
311 #ifdef TRANSPORT_AUTOREPLY
314 .driver_name = US"autoreply",
315 .options = autoreply_transport_options,
316 .options_count = &autoreply_transport_options_count,
317 .options_block = &autoreply_transport_option_defaults,
318 .options_len = sizeof(autoreply_transport_options_block),
319 .init = autoreply_transport_init,
321 .code = autoreply_transport_entry,
327 #ifdef TRANSPORT_LMTP
330 .driver_name = US"lmtp",
331 .options = lmtp_transport_options,
332 .options_count = &lmtp_transport_options_count,
333 .options_block = &lmtp_transport_option_defaults,
334 .options_len = sizeof(lmtp_transport_options_block),
335 .init = lmtp_transport_init,
337 .code = lmtp_transport_entry,
343 #ifdef TRANSPORT_PIPE
346 .driver_name = US"pipe",
347 .options = pipe_transport_options,
348 .options_count = &pipe_transport_options_count,
349 .options_block = &pipe_transport_option_defaults,
350 .options_len = sizeof(pipe_transport_options_block),
351 .init = pipe_transport_init,
353 .code = pipe_transport_entry,
359 #ifdef EXPERIMENTAL_QUEUEFILE
362 .driver_name = US"queuefile",
363 .options = queuefile_transport_options,
364 .options_count = &queuefile_transport_options_count,
365 .options_block = &queuefile_transport_option_defaults,
366 .options_len = sizeof(queuefile_transport_options_block),
367 .init = queuefile_transport_init,
369 .code = queuefile_transport_entry,
375 #ifdef TRANSPORT_SMTP
378 .driver_name = US"smtp",
379 .options = smtp_transport_options,
380 .options_count = &smtp_transport_options_count,
381 .options_block = &smtp_transport_option_defaults,
382 .options_len = sizeof(smtp_transport_options_block),
383 .init = smtp_transport_init,
385 .code = smtp_transport_entry,
387 .closedown = smtp_transport_closedown,
391 { .drinfo = { .driver_name = US"" }}
397 auth_show_supported(gstring * g)
399 g = string_cat(g, US"Authenticators:");
400 /*XXX these run off the static list as we want them before the conf is read */
401 /*XXX Could possibly check for dyn flag + file presence */
402 for (auth_info * ai = auths_available_oldarray; ai->drinfo.driver_name[0]; ai++)
403 g = string_fmt_append(g, " %s", ai->drinfo.driver_name);
404 return string_cat(g, US"\n");
408 route_show_supported(gstring * g)
411 g = string_cat(g, US"Routers:");
412 /*XXX these run off the static list as we want them before the conf is read */
413 /*XXX lookup_show_supported is in exim.c and just works off the #defines, with hardoded strings */
414 for (router_info * rr = routers_available_oldarray; rr->drinfo.driver_name[0]; rr++)
415 g = string_fmt_append(g, " %s", rr->drinfo.driver_name);
416 return string_cat(g, US"\n");
419 uschar * b = US"" /* static-build router names */
420 #if defined(ROUTER_ACCEPT) && ROUTER_ACCEPT!=2
423 #if defined(ROUTER_DNSLOOKUP) && ROUTER_DNSLOOKUP!=2
426 # if defined(ROUTER_IPLITERAL) && ROUTER_IPLITERAL!=2
429 #if defined(ROUTER_IPLOOKUP) && ROUTER_IPLOOKUP!=2
432 #if defined(ROUTER_MANUALROUTE) && ROUTER_MANUALROUTE!=2
435 #if defined(ROUTER_REDIRECT) && ROUTER_REDIRECT!=2
438 #if defined(ROUTER_QUERYPROGRAM) && ROUTER_QUERYPROGRAM!=2
443 uschar * d = US"" /* dynamic-module router names */
444 #if defined(ROUTER_ACCEPT) && ROUTER_ACCEPT==2
447 #if defined(ROUTER_DNSLOOKUP) && ROUTER_DNSLOOKUP==2
450 # if defined(ROUTER_IPLITERAL) && ROUTER_IPLITERAL==2
453 #if defined(ROUTER_IPLOOKUP) && ROUTER_IPLOOKUP==2
456 #if defined(ROUTER_MANUALROUTE) && ROUTER_MANUALROUTE==2
459 #if defined(ROUTER_REDIRECT) && ROUTER_REDIRECT==2
462 #if defined(ROUTER_QUERYPROGRAM) && ROUTER_QUERYPROGRAM==2
467 if (*b) g = string_fmt_append(g, "Routers (built-in):%s\n", b);
468 if (*d) g = string_fmt_append(g, "Routers (dynamic): %s\n", d);
474 transport_show_supported(gstring * g)
476 g = string_cat(g, US"Transports:");
477 #ifdef TRANSPORT_APPENDFILE
478 g = string_cat(g, US" appendfile");
479 #ifdef SUPPORT_MAILDIR
480 g = string_cat(g, US"/maildir"); /* damn these subclasses */
482 #ifdef SUPPORT_MAILSTORE
483 g = string_cat(g, US"/mailstore");
486 g = string_cat(g, US"/mbx");
489 #ifdef TRANSPORT_AUTOREPLY
490 g = string_cat(g, US" autoreply");
492 #ifdef TRANSPORT_LMTP
493 g = string_cat(g, US" lmtp");
495 #ifdef TRANSPORT_PIPE
496 g = string_cat(g, US" pipe");
498 #ifdef EXPERIMENTAL_QUEUEFILE
499 g = string_cat(g, US" queuefile");
501 #ifdef TRANSPORT_SMTP
502 g = string_cat(g, US" smtp");
504 return string_cat(g, US"\n");
509 struct lookupmodulestr
512 struct lookup_module_info *info;
513 struct lookupmodulestr *next;
516 static struct lookupmodulestr *lookupmodules = NULL;
519 addlookupmodule(void *dl, struct lookup_module_info *info)
521 struct lookupmodulestr * p =
522 store_get(sizeof(struct lookupmodulestr), GET_UNTAINTED);
526 p->next = lookupmodules;
528 lookup_list_count += info->lookupcount;
531 /* only valid after lookup_list and lookup_list_count are assigned */
533 add_lookup_to_list(lookup_info *info)
535 /* need to add the lookup to lookup_list, sorted */
538 /* strategy is to go through the list until we find
539 either an empty spot or a name that is higher.
540 this can't fail because we have enough space. */
542 while (lookup_list[pos] && (Ustrcmp(lookup_list[pos]->name, info->name) <= 0))
545 if (lookup_list[pos])
547 /* need to insert it, so move all the other items up
548 (last slot is still empty, of course) */
550 memmove(&lookup_list[pos+1], &lookup_list[pos],
551 sizeof(lookup_info *) * (lookup_list_count-pos-1));
553 lookup_list[pos] = info;
557 /* These need to be at file level for old versions of gcc (2.95.2 reported),
558 which give parse errors on an extern in function scope. Each entry needs
559 to also be invoked in init_lookup_list() below */
561 #if defined(LOOKUP_CDB) && LOOKUP_CDB!=2
562 extern lookup_module_info cdb_lookup_module_info;
564 #if defined(LOOKUP_DBM) && LOOKUP_DBM!=2
565 extern lookup_module_info dbmdb_lookup_module_info;
567 #if defined(LOOKUP_DNSDB) && LOOKUP_DNSDB!=2
568 extern lookup_module_info dnsdb_lookup_module_info;
570 #if defined(LOOKUP_DSEARCH) && LOOKUP_DSEARCH!=2
571 extern lookup_module_info dsearch_lookup_module_info;
573 #if defined(LOOKUP_IBASE) && LOOKUP_IBASE!=2
574 extern lookup_module_info ibase_lookup_module_info;
576 #if defined(LOOKUP_JSON) && LOOKUP_JSON!=2
577 extern lookup_module_info json_lookup_module_info;
579 #if defined(LOOKUP_LDAP)
580 extern lookup_module_info ldap_lookup_module_info;
582 #if defined(LOOKUP_LSEARCH) && LOOKUP_LSEARCH!=2
583 extern lookup_module_info lsearch_lookup_module_info;
585 #if defined(LOOKUP_MYSQL) && LOOKUP_MYSQL!=2
586 extern lookup_module_info mysql_lookup_module_info;
588 #if defined(LOOKUP_NIS) && LOOKUP_NIS!=2
589 extern lookup_module_info nis_lookup_module_info;
591 #if defined(LOOKUP_NISPLUS) && LOOKUP_NISPLUS!=2
592 extern lookup_module_info nisplus_lookup_module_info;
594 #if defined(LOOKUP_ORACLE) && LOOKUP_ORACLE!=2
595 extern lookup_module_info oracle_lookup_module_info;
597 #if defined(LOOKUP_PASSWD) && LOOKUP_PASSWD!=2
598 extern lookup_module_info passwd_lookup_module_info;
600 #if defined(LOOKUP_PGSQL) && LOOKUP_PGSQL!=2
601 extern lookup_module_info pgsql_lookup_module_info;
603 #if defined(LOOKUP_REDIS) && LOOKUP_REDIS!=2
604 extern lookup_module_info redis_lookup_module_info;
606 #if defined(LOOKUP_LMDB)
607 extern lookup_module_info lmdb_lookup_module_info;
609 #if defined(SUPPORT_SPF)
610 extern lookup_module_info spf_lookup_module_info;
612 #if defined(LOOKUP_SQLITE) && LOOKUP_SQLITE!=2
613 extern lookup_module_info sqlite_lookup_module_info;
615 #if defined(LOOKUP_TESTDB) && LOOKUP_TESTDB!=2
616 extern lookup_module_info testdb_lookup_module_info;
618 #if defined(LOOKUP_WHOSON) && LOOKUP_WHOSON!=2
619 extern lookup_module_info whoson_lookup_module_info;
622 extern lookup_module_info readsock_lookup_module_info;
626 init_lookup_list(void)
628 #ifdef LOOKUP_MODULE_DIR
631 int countmodules = 0;
632 int moduleerrors = 0;
634 static BOOL lookup_list_init_done = FALSE;
637 if (lookup_list_init_done)
639 reset_point = store_mark();
640 lookup_list_init_done = TRUE;
642 #if defined(LOOKUP_CDB) && LOOKUP_CDB!=2
643 addlookupmodule(NULL, &cdb_lookup_module_info);
646 #if defined(LOOKUP_DBM) && LOOKUP_DBM!=2
647 addlookupmodule(NULL, &dbmdb_lookup_module_info);
650 #if defined(LOOKUP_DNSDB) && LOOKUP_DNSDB!=2
651 addlookupmodule(NULL, &dnsdb_lookup_module_info);
654 #if defined(LOOKUP_DSEARCH) && LOOKUP_DSEARCH!=2
655 addlookupmodule(NULL, &dsearch_lookup_module_info);
658 #if defined(LOOKUP_IBASE) && LOOKUP_IBASE!=2
659 addlookupmodule(NULL, &ibase_lookup_module_info);
663 addlookupmodule(NULL, &ldap_lookup_module_info);
666 #if defined(LOOKUP_JSON) && LOOKUP_JSON!=2
667 addlookupmodule(NULL, &json_lookup_module_info);
670 #if defined(LOOKUP_LSEARCH) && LOOKUP_LSEARCH!=2
671 addlookupmodule(NULL, &lsearch_lookup_module_info);
674 #if defined(LOOKUP_MYSQL) && LOOKUP_MYSQL!=2
675 addlookupmodule(NULL, &mysql_lookup_module_info);
678 #if defined(LOOKUP_NIS) && LOOKUP_NIS!=2
679 addlookupmodule(NULL, &nis_lookup_module_info);
682 #if defined(LOOKUP_NISPLUS) && LOOKUP_NISPLUS!=2
683 addlookupmodule(NULL, &nisplus_lookup_module_info);
686 #if defined(LOOKUP_ORACLE) && LOOKUP_ORACLE!=2
687 addlookupmodule(NULL, &oracle_lookup_module_info);
690 #if defined(LOOKUP_PASSWD) && LOOKUP_PASSWD!=2
691 addlookupmodule(NULL, &passwd_lookup_module_info);
694 #if defined(LOOKUP_PGSQL) && LOOKUP_PGSQL!=2
695 addlookupmodule(NULL, &pgsql_lookup_module_info);
698 #if defined(LOOKUP_REDIS) && LOOKUP_REDIS!=2
699 addlookupmodule(NULL, &redis_lookup_module_info);
703 addlookupmodule(NULL, &lmdb_lookup_module_info);
707 addlookupmodule(NULL, &spf_lookup_module_info);
710 #if defined(LOOKUP_SQLITE) && LOOKUP_SQLITE!=2
711 addlookupmodule(NULL, &sqlite_lookup_module_info);
714 #if defined(LOOKUP_TESTDB) && LOOKUP_TESTDB!=2
715 addlookupmodule(NULL, &testdb_lookup_module_info);
718 #if defined(LOOKUP_WHOSON) && LOOKUP_WHOSON!=2
719 addlookupmodule(NULL, &whoson_lookup_module_info);
722 /* This is a custom expansion, and not available as either
723 a list-syntax lookup or a lookup expansion. However, it is
724 implemented by a lookup module. */
726 addlookupmodule(NULL, &readsock_lookup_module_info);
728 #ifdef LOOKUP_MODULE_DIR
729 if (!(dd = exim_opendir(CUS LOOKUP_MODULE_DIR)))
731 DEBUG(D_lookup) debug_printf("Couldn't open %s: not loading lookup modules\n", LOOKUP_MODULE_DIR);
732 log_write(0, LOG_MAIN|LOG_PANIC,
733 "Couldn't open %s: not loading lookup modules\n", LOOKUP_MODULE_DIR);
737 const pcre2_code * regex_islookupmod = regex_must_compile(
738 US"_lookup\\." DYNLIB_FN_EXT "$", MCS_NOFLAGS, TRUE);
740 DEBUG(D_lookup) debug_printf("Loading lookup modules from %s\n", LOOKUP_MODULE_DIR);
741 while ((ent = readdir(dd)))
743 char * name = ent->d_name;
744 int len = (int)strlen(name);
745 if (regex_match(regex_islookupmod, US name, len, NULL))
747 int pathnamelen = len + (int)strlen(LOOKUP_MODULE_DIR) + 2;
749 struct lookup_module_info *info;
750 const char *errormsg;
752 /* SRH: am I being paranoid here or what? */
753 if (pathnamelen > big_buffer_size)
755 fprintf(stderr, "Loading lookup modules: %s/%s: name too long\n", LOOKUP_MODULE_DIR, name);
756 log_write(0, LOG_MAIN|LOG_PANIC, "%s/%s: name too long\n", LOOKUP_MODULE_DIR, name);
760 /* SRH: snprintf here? */
761 sprintf(CS big_buffer, "%s/%s", LOOKUP_MODULE_DIR, name);
763 if (!(dl = dlopen(CS big_buffer, RTLD_NOW)))
765 errormsg = dlerror();
766 fprintf(stderr, "Error loading %s: %s\n", name, errormsg);
767 log_write(0, LOG_MAIN|LOG_PANIC, "Error loading lookup module %s: %s\n", name, errormsg);
772 /* FreeBSD nsdispatch() can trigger dlerror() errors about
773 _nss_cache_cycle_prevention_function; we need to clear the dlerror()
774 state before calling dlsym(), so that any error afterwards only comes
777 errormsg = dlerror();
779 info = (struct lookup_module_info*) dlsym(dl, "_lookup_module_info");
780 if ((errormsg = dlerror()))
782 fprintf(stderr, "%s does not appear to be a lookup module (%s)\n", name, errormsg);
783 log_write(0, LOG_MAIN|LOG_PANIC, "%s does not appear to be a lookup module (%s)\n", name, errormsg);
788 if (info->magic != LOOKUP_MODULE_INFO_MAGIC)
790 fprintf(stderr, "Lookup module %s is not compatible with this version of Exim\n", name);
791 log_write(0, LOG_MAIN|LOG_PANIC, "Lookup module %s is not compatible with this version of Exim\n", name);
797 addlookupmodule(dl, info);
798 DEBUG(D_lookup) debug_printf("Loaded \"%s\" (%d lookup types)\n", name, info->lookupcount);
802 store_free((void*)regex_islookupmod);
806 DEBUG(D_lookup) debug_printf("Loaded %d lookup modules\n", countmodules);
809 DEBUG(D_lookup) debug_printf("Total %d lookups\n", lookup_list_count);
811 lookup_list = store_malloc(sizeof(lookup_info *) * lookup_list_count);
812 memset(lookup_list, 0, sizeof(lookup_info *) * lookup_list_count);
814 /* now add all lookups to the real list */
815 for (struct lookupmodulestr * p = lookupmodules; p; p = p->next)
816 for (int j = 0; j < p->info->lookupcount; j++)
817 add_lookup_to_list(p->info->lookups[j]);
818 store_reset(reset_point);
819 /* just to be sure */
820 lookupmodules = NULL;
823 #endif /*!MACRO_PREDEF*/
824 /* End of drtables.c */