1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
/*************************************************
* Exim - an Internet mail transport agent *
*************************************************/
/* Copyright (c) University of Cambridge 1995 - 2018 */
/* See the file NOTICE for conditions of use and distribution. */
#include "../exim.h"
#include "lf_functions.h"
/*************************************************
* Call SQL server(s) to run an actual query *
*************************************************/
/* All the SQL lookups are of the same form, with a list of servers to try
until one can be accessed. It is now also possible to provide the server data
as part of the query. This function manages server selection and looping; each
lookup has its own function for actually performing the lookup.
Arguments:
name the lookup name, e.g. "MySQL"
optionname the name of the servers option, e.g. "mysql_servers"
optserverlist the value of the servers option
query the query
result where to pass back the result
errmsg where to pass back an error message
do_cache to be set zero if data is changed
func the lookup function to call
Returns: the return from the lookup function, or DEFER
*/
int
lf_sqlperform(const uschar *name, const uschar *optionname,
const uschar *optserverlist, const uschar *query,
uschar **result, uschar **errmsg, uint *do_cache,
int(*fn)(const uschar *, uschar *, uschar **, uschar **, BOOL *, uint *))
{
int sep, rc;
uschar *server;
const uschar *serverlist;
uschar buffer[512];
BOOL defer_break = FALSE;
DEBUG(D_lookup) debug_printf("%s query: %s\n", name, query);
/* Handle queries that do not have server information at the start. */
if (Ustrncmp(query, "servers", 7) != 0)
{
sep = 0;
serverlist = optserverlist;
while ((server = string_nextinlist(&serverlist, &sep, buffer,
sizeof(buffer))) != NULL)
{
rc = (*fn)(query, server, result, errmsg, &defer_break, do_cache);
if (rc != DEFER || defer_break) return rc;
}
if (optserverlist == NULL)
*errmsg = string_sprintf("no %s servers defined (%s option)", name,
optionname);
}
/* Handle queries that do have server information at the start. */
else
{
int qsep;
const uschar *s, *ss;
const uschar *qserverlist;
uschar *qserver;
uschar qbuffer[512];
s = query + 7;
while (isspace(*s)) s++;
if (*s++ != '=')
{
*errmsg = string_sprintf("missing = after \"servers\" in %s lookup", name);
return DEFER;
}
while (isspace(*s)) s++;
ss = Ustrchr(s, ';');
if (ss == NULL)
{
*errmsg = string_sprintf("missing ; after \"servers=\" in %s lookup",
name);
return DEFER;
}
if (ss == s)
{
*errmsg = string_sprintf("\"servers=\" defines no servers in \"%s\"",
query);
return DEFER;
}
qserverlist = string_sprintf("%.*s", (int)(ss - s), s);
qsep = 0;
while ((qserver = string_nextinlist(&qserverlist, &qsep, qbuffer,
sizeof(qbuffer))) != NULL)
{
if (Ustrchr(qserver, '/') != NULL)
server = qserver;
else
{
int len = Ustrlen(qserver);
sep = 0;
serverlist = optserverlist;
while ((server = string_nextinlist(&serverlist, &sep, buffer,
sizeof(buffer))) != NULL)
{
if (Ustrncmp(server, qserver, len) == 0 && server[len] == '/')
break;
}
if (server == NULL)
{
*errmsg = string_sprintf("%s server \"%s\" not found in %s", name,
qserver, optionname);
return DEFER;
}
}
rc = (*fn)(ss+1, server, result, errmsg, &defer_break, do_cache);
if (rc != DEFER || defer_break) return rc;
}
}
return DEFER;
}
/* End of lf_sqlperform.c */
|