1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
4
5 /* Copyright (c) Jeremy Harris 2019-2020 */
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 *
json_malloc(size_t nbytes)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
json_free(void * p)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 *
json_open(const uschar * filename,uschar ** errmsg)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 *errmsg = string_open_failed("%s for json search", filename);
51 return f;
52 }
53
54
55
56 /*************************************************
57 * Check entry point *
58 *************************************************/
59
60 static BOOL
json_check(void * handle,const uschar * filename,int modemask,uid_t * owners,gid_t * owngroups,uschar ** errmsg)61 json_check(void *handle, const uschar *filename, int modemask, uid_t *owners,
62 gid_t *owngroups, uschar **errmsg)
63 {
64 return lf_check_file(fileno((FILE *)handle), filename, S_IFREG, modemask,
65 owners, owngroups, "json", errmsg) == 0;
66 }
67
68
69
70 /*************************************************
71 * Find entry point for lsearch *
72 *************************************************/
73
74 /* See local README for interface description */
75
76 static int
json_find(void * handle,const uschar * filename,const uschar * keystring,int length,uschar ** result,uschar ** errmsg,uint * do_cache,const uschar * opts)77 json_find(void * handle, const uschar * filename, const uschar * keystring,
78 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
79 const uschar * opts)
80 {
81 FILE * f = handle;
82 json_t * j, * j0;
83 json_error_t jerr;
84 uschar * key;
85 int sep = 0;
86
87 rewind(f);
88 if (!(j = json_loadf(f, 0, &jerr)))
89 {
90 *errmsg = string_sprintf("json error on open: %.*s\n",
91 JSON_ERROR_TEXT_LENGTH, jerr.text);
92 return FAIL;
93 }
94 j0 = j;
95
96 for (int k = 1; (key = string_nextinlist(&keystring, &sep, NULL, 0)); k++)
97 {
98 BOOL numeric = TRUE;
99 for (uschar * s = key; *s; s++) if (!isdigit(*s)) { numeric = FALSE; break; }
100
101 if (!(j = numeric
102 ? json_array_get(j, (size_t) strtoul(CS key, NULL, 10))
103 : json_object_get(j, CCS key)
104 ) )
105 {
106 DEBUG(D_lookup) debug_printf_indent("%s, for key %d: '%s'\n",
107 numeric
108 ? US"bad index, or not json array"
109 : US"no such key, or not json object",
110 k, key);
111 json_decref(j0);
112 return FAIL;
113 }
114 }
115
116 switch (json_typeof(j))
117 {
118 case JSON_STRING:
119 *result = string_copyn(CUS json_string_value(j), json_string_length(j));
120 break;
121 case JSON_INTEGER:
122 *result = string_sprintf("%" JSON_INTEGER_FORMAT, json_integer_value(j));
123 break;
124 case JSON_REAL:
125 *result = string_sprintf("%f", json_real_value(j));
126 break;
127 case JSON_TRUE: *result = US"true"; break;
128 case JSON_FALSE: *result = US"false"; break;
129 case JSON_NULL: *result = NULL; break;
130 default: *result = US json_dumps(j, 0); break;
131 }
132 json_decref(j0);
133 return OK;
134 }
135
136
137
138 /*************************************************
139 * Close entry point *
140 *************************************************/
141
142 /* See local README for interface description */
143
144 static void
json_close(void * handle)145 json_close(void *handle)
146 {
147 (void)fclose((FILE *)handle);
148 }
149
150
151
152 /*************************************************
153 * Version reporting entry point *
154 *************************************************/
155
156 /* See local README for interface description. */
157
158 #include "../version.h"
159
160 void
json_version_report(FILE * f)161 json_version_report(FILE *f)
162 {
163 fprintf(f, "Library version: json: Jansonn version %s\n", JANSSON_VERSION);
164 }
165
166
167 static lookup_info json_lookup_info = {
168 .name = US"json", /* lookup name */
169 .type = lookup_absfile, /* uses absolute file name */
170 .open = json_open, /* open function */
171 .check = json_check, /* check function */
172 .find = json_find, /* find function */
173 .close = json_close, /* close function */
174 .tidy = NULL, /* no tidy function */
175 .quote = NULL, /* no quoting function */
176 .version_report = json_version_report /* version reporting */
177 };
178
179
180 #ifdef DYNLOOKUP
181 #define json_lookup_module_info _lookup_module_info
182 #endif
183
184 static lookup_info *_lookup_list[] = { &json_lookup_info };
185 lookup_module_info json_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
186
187 /* End of lookups/json.c */
188