1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* Copyright (c) The Exim Maintainers 2020 */
7 /* See the file NOTICE for conditions of use and distribution. */
11 #include "lf_functions.h"
15 /*************************************************
16 * Call SQL server(s) to run an actual query *
17 *************************************************/
19 /* All the SQL lookups are of the same form, with a list of servers to try
20 until one can be accessed. It is now also possible to provide the server data
21 as part of the query. This function manages server selection and looping; each
22 lookup has its own function for actually performing the lookup.
25 name the lookup name, e.g. "MySQL"
26 optionname the name of the servers option, e.g. "mysql_servers"
27 optserverlist the value of the servers option
29 result where to pass back the result
30 errmsg where to pass back an error message
31 do_cache to be set zero if data is changed
32 func the lookup function to call
34 Returns: the return from the lookup function, or DEFER
38 lf_sqlperform(const uschar *name, const uschar *optionname,
39 const uschar *optserverlist, const uschar *query,
40 uschar **result, uschar **errmsg, uint *do_cache, const uschar * opts,
41 int(*fn)(const uschar *, uschar *, uschar **, uschar **, BOOL *, uint *, const uschar *))
45 BOOL defer_break = FALSE;
47 DEBUG(D_lookup) debug_printf_indent("%s query: \"%s\" opts '%s'\n", name, query, opts);
49 /* Handle queries that do have server information at the start. */
51 if (Ustrncmp(query, "servers", 7) == 0)
55 const uschar *qserverlist;
62 *errmsg = string_sprintf("missing = after \"servers\" in %s lookup", name);
70 *errmsg = string_sprintf("missing ; after \"servers=\" in %s lookup",
77 *errmsg = string_sprintf("\"servers=\" defines no servers in \"%s\"",
82 qserverlist = string_sprintf("%.*s", (int)(ss - s), s);
84 while ((qserver = string_nextinlist(&qserverlist, &qsep, NULL, 0)))
86 if (Ustrchr(qserver, '/'))
90 int len = Ustrlen(qserver);
91 const uschar * serverlist = optserverlist;
93 for (int sep = 0; server = string_nextinlist(&serverlist, &sep, NULL, 0);)
94 if (Ustrncmp(server, qserver, len) == 0 && server[len] == '/')
99 *errmsg = string_sprintf("%s server \"%s\" not found in %s", name,
100 qserver, optionname);
106 if ((m = is_tainted2(server, 0, "Tainted %s server '%s'", name, server)))
113 rc = (*fn)(ss+1, server, result, errmsg, &defer_break, do_cache, opts);
114 if (rc != DEFER || defer_break) return rc;
118 /* Handle queries that do not have server information at the start. */
122 const uschar * serverlist = NULL;
124 /* If options are present, scan for a server definition. Default to
125 the "optserverlist" srgument. */
130 for (int sep = ','; ele = string_nextinlist(&opts, &sep, NULL, 0); )
131 if (Ustrncmp(ele, "servers=", 8) == 0)
132 { serverlist = ele + 8; break; }
136 serverlist = optserverlist;
138 *errmsg = string_sprintf("no %s servers defined (%s option)", name,
141 for (int d = 0; (server = string_nextinlist(&serverlist, &d, NULL, 0)); )
143 /* If not a full spec assume from options; scan main list for matching
146 if (!Ustrchr(server, '/'))
148 int len = Ustrlen(server);
149 const uschar * slist = optserverlist;
151 for (int sep = 0; ele = string_nextinlist(&slist, &sep, NULL, 0); )
152 if (Ustrncmp(ele, server, len) == 0 && ele[len] == '/')
156 *errmsg = string_sprintf("%s server \"%s\" not found in %s", name,
164 if ((m = is_tainted2(server, 0, "Tainted %s server '%s'", name, server)))
171 rc = (*fn)(query, server, result, errmsg, &defer_break, do_cache, opts);
172 if (rc != DEFER || defer_break) return rc;
179 /* End of lf_sqlperform.c */