Lookups: per-searchtype options framework
[exim.git] / src / src / lookups / json.c
1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
4
5 /* Copyright (c) Jeremy Harris 2019 */
6 /* See the file NOTICE for conditions of use and distribution. */
7
8 #include "../exim.h"
9 #include "lf_functions.h"
10 #include <jansson.h>
11
12
13
14 /* All use of allocations will be done against the POOL_SEARCH memory,
15 which is freed once by search_tidyup(). Make the free call a dummy.
16 This burns some 300kB in handling a 37kB JSON file, for the benefit of
17 a fast free. The alternative of staying with malloc is nearly as bad,
18 eyeballing the activity there are 20% the number of free vs. alloc
19 calls (before the big bunch at the end).
20
21 Assume that the file is trusted, so no tainting */
22
23 static void *
24 json_malloc(size_t nbytes)
25 {
26 void * p = store_get((int)nbytes, FALSE);
27 /* debug_printf("%s %d: %p\n", __FUNCTION__, (int)nbytes, p); */
28 return p;
29 }
30 static void
31 json_free(void * p)
32 {
33 /* debug_printf("%s: %p\n", __FUNCTION__, p); */
34 }
35
36 /*************************************************
37 * Open entry point *
38 *************************************************/
39
40 /* See local README for interface description */
41
42 static void *
43 json_open(const uschar * filename, uschar ** errmsg)
44 {
45 FILE * f;
46
47 json_set_alloc_funcs(json_malloc, json_free);
48
49 if (!(f = Ufopen(filename, "rb")))
50 {
51 int save_errno = errno;
52 *errmsg = string_open_failed(errno, "%s for json search", filename);
53 errno = save_errno;
54 return NULL;
55 }
56 return f;
57 }
58
59
60
61 /*************************************************
62 * Check entry point *
63 *************************************************/
64
65 static BOOL
66 json_check(void *handle, const uschar *filename, int modemask, uid_t *owners,
67 gid_t *owngroups, uschar **errmsg)
68 {
69 return lf_check_file(fileno((FILE *)handle), filename, S_IFREG, modemask,
70 owners, owngroups, "json", errmsg) == 0;
71 }
72
73
74
75 /*************************************************
76 * Find entry point for lsearch *
77 *************************************************/
78
79 /* See local README for interface description */
80
81 static int
82 json_find(void * handle, const uschar * filename, const uschar * keystring,
83 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
84 const uschar * opts)
85 {
86 FILE * f = handle;
87 json_t * j, * j0;
88 json_error_t jerr;
89 uschar * key;
90 int sep = 0;
91
92 length = length; /* Keep picky compilers happy */
93 do_cache = do_cache; /* Keep picky compilers happy */
94
95 rewind(f);
96 if (!(j = json_loadf(f, 0, &jerr)))
97 {
98 *errmsg = string_sprintf("json error on open: %.*s\n",
99 JSON_ERROR_TEXT_LENGTH, jerr.text);
100 return FAIL;
101 }
102 j0 = j;
103
104 for (int k = 1; (key = string_nextinlist(&keystring, &sep, NULL, 0)); k++)
105 {
106 BOOL numeric = TRUE;
107 for (uschar * s = key; *s; s++) if (!isdigit(*s)) { numeric = FALSE; break; }
108
109 if (!(j = numeric
110 ? json_array_get(j, (size_t) strtoul(CS key, NULL, 10))
111 : json_object_get(j, CCS key)
112 ) )
113 {
114 DEBUG(D_lookup) debug_printf_indent("%s, for key %d: '%s'\n",
115 numeric
116 ? US"bad index, or not json array"
117 : US"no such key, or not json object",
118 k, key);
119 json_decref(j0);
120 return FAIL;
121 }
122 }
123
124 switch (json_typeof(j))
125 {
126 case JSON_STRING:
127 *result = string_copyn(CUS json_string_value(j), json_string_length(j));
128 break;
129 case JSON_INTEGER:
130 *result = string_sprintf("%" JSON_INTEGER_FORMAT, json_integer_value(j));
131 break;
132 case JSON_REAL:
133 *result = string_sprintf("%f", json_real_value(j));
134 break;
135 case JSON_TRUE: *result = US"true"; break;
136 case JSON_FALSE: *result = US"false"; break;
137 case JSON_NULL: *result = NULL; break;
138 default: *result = US json_dumps(j, 0); break;
139 }
140 json_decref(j0);
141 return OK;
142 }
143
144
145
146 /*************************************************
147 * Close entry point *
148 *************************************************/
149
150 /* See local README for interface description */
151
152 static void
153 json_close(void *handle)
154 {
155 (void)fclose((FILE *)handle);
156 }
157
158
159
160 /*************************************************
161 * Version reporting entry point *
162 *************************************************/
163
164 /* See local README for interface description. */
165
166 #include "../version.h"
167
168 void
169 json_version_report(FILE *f)
170 {
171 fprintf(f, "Library version: json: Jansonn version %s\n", JANSSON_VERSION);
172 }
173
174
175 static lookup_info json_lookup_info = {
176 US"json", /* lookup name */
177 lookup_absfile, /* uses absolute file name */
178 json_open, /* open function */
179 json_check, /* check function */
180 json_find, /* find function */
181 json_close, /* close function */
182 NULL, /* no tidy function */
183 NULL, /* no quoting function */
184 json_version_report /* version reporting */
185 };
186
187
188 #ifdef DYNLOOKUP
189 #define json_lookup_module_info _lookup_module_info
190 #endif
191
192 static lookup_info *_lookup_list[] = { &json_lookup_info };
193 lookup_module_info json_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
194
195 /* End of lookups/json.c */