1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* See the file NOTICE for conditions of use and distribution. */
10 #include "lf_functions.h"
14 /*************************************************
15 * Call SQL server(s) to run an actual query *
16 *************************************************/
18 /* All the SQL lookups are of the same form, with a list of servers to try
19 until one can be accessed. It is now also possible to provide the server data
20 as part of the query. This function manages server selection and looping; each
21 lookup has its own function for actually performing the lookup.
24 name the lookup name, e.g. "MySQL"
25 optionname the name of the servers option, e.g. "mysql_servers"
26 optserverlist the value of the servers option
28 result where to pass back the result
29 errmsg where to pass back an error message
30 do_cache to be set zero if data is changed
31 func the lookup function to call
33 Returns: the return from the lookup function, or DEFER
37 lf_sqlperform(const uschar *name, const uschar *optionname,
38 const uschar *optserverlist, const uschar *query,
39 uschar **result, uschar **errmsg, uint *do_cache, const uschar * opts,
40 int(*fn)(const uschar *, uschar *, uschar **, uschar **, BOOL *, uint *, const uschar *))
44 BOOL defer_break = FALSE;
46 DEBUG(D_lookup) debug_printf_indent("%s query: \"%s\" opts '%s'\n", name, query, opts);
48 /* Handle queries that do have server information at the start. */
50 if (Ustrncmp(query, "servers", 7) == 0)
54 const uschar *qserverlist;
61 *errmsg = string_sprintf("missing = after \"servers\" in %s lookup", name);
69 *errmsg = string_sprintf("missing ; after \"servers=\" in %s lookup",
76 *errmsg = string_sprintf("\"servers=\" defines no servers in \"%s\"",
81 qserverlist = string_sprintf("%.*s", (int)(ss - s), s);
83 while ((qserver = string_nextinlist(&qserverlist, &qsep, NULL, 0)))
85 if (Ustrchr(qserver, '/'))
89 int len = Ustrlen(qserver);
90 const uschar * serverlist = optserverlist;
92 for (int sep = 0; server = string_nextinlist(&serverlist, &sep, NULL, 0);)
93 if (Ustrncmp(server, qserver, len) == 0 && server[len] == '/')
98 *errmsg = string_sprintf("%s server \"%s\" not found in %s", name,
104 if (is_tainted(server))
106 *errmsg = string_sprintf("%s server \"%s\" is tainted", name, server);
110 rc = (*fn)(ss+1, server, result, errmsg, &defer_break, do_cache, opts);
111 if (rc != DEFER || defer_break) return rc;
115 /* Handle queries that do not have server information at the start. */
119 const uschar * serverlist = NULL;
121 /* If options are present, scan for a server definition. Default to
122 the "optserverlist" srgument. */
127 for (int sep = ','; ele = string_nextinlist(&opts, &sep, NULL, 0); )
128 if (Ustrncmp(ele, "servers=", 8) == 0)
129 { serverlist = ele + 8; break; }
133 serverlist = optserverlist;
135 *errmsg = string_sprintf("no %s servers defined (%s option)", name,
138 for (int d = 0; (server = string_nextinlist(&serverlist, &d, NULL, 0)); )
140 /* If not a full spec assume from options; scan main list for matching
143 if (!Ustrchr(server, '/'))
145 int len = Ustrlen(server);
146 const uschar * slist = optserverlist;
148 for (int sep = 0; ele = string_nextinlist(&slist, &sep, NULL, 0); )
149 if (Ustrncmp(ele, server, len) == 0 && ele[len] == '/')
153 *errmsg = string_sprintf("%s server \"%s\" not found in %s", name,
160 if (is_tainted(server))
162 *errmsg = string_sprintf("%s server \"%s\" is tainted", name, server);
166 rc = (*fn)(query, server, result, errmsg, &defer_break, do_cache, opts);
167 if (rc != DEFER || defer_break) return rc;
174 /* End of lf_sqlperform.c */