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