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