1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) Jeremy Harris 2019 */
6 /* See the file NOTICE for conditions of use and distribution. */
9 #include "lf_functions.h"
14 /*************************************************
16 *************************************************/
18 /* See local README for interface description */
21 json_open(uschar *filename, uschar **errmsg)
23 FILE *f = Ufopen(filename, "rb");
26 int save_errno = errno;
27 *errmsg = string_open_failed(errno, "%s for json search", filename);
36 /*************************************************
38 *************************************************/
41 json_check(void *handle, uschar *filename, int modemask, uid_t *owners,
42 gid_t *owngroups, uschar **errmsg)
44 return lf_check_file(fileno((FILE *)handle), filename, S_IFREG, modemask,
45 owners, owngroups, "json", errmsg) == 0;
50 /*************************************************
51 * Find entry point for lsearch *
52 *************************************************/
54 /* See local README for interface description */
57 json_find(void *handle, uschar *filename, const uschar *keystring, int length,
58 uschar **result, uschar **errmsg, uint *do_cache)
66 length = length; /* Keep picky compilers happy */
67 do_cache = do_cache; /* Keep picky compilers happy */
70 if (!(j = json_loadf(f, 0, &jerr)))
72 *errmsg = string_sprintf("json error on open: %.*s\n",
73 JSON_ERROR_TEXT_LENGTH, jerr.text);
78 for (int k = 1; (key = string_nextinlist(&keystring, &sep, NULL, 0)); k++)
81 for (uschar * s = key; *s; s++) if (!isdigit(*s)) { numeric = FALSE; break; }
84 ? json_array_get(j, (size_t) strtoul(CS key, NULL, 10))
85 : json_object_get(j, CCS key)
88 DEBUG(D_lookup) debug_printf("%s, for key %d: '%s'\n",
90 ? US"bad index, or not json array"
91 : US"no such key, or not json object",
98 switch (json_typeof(j))
101 *result = string_copyn(CUS json_string_value(j), json_string_length(j));
104 *result = string_sprintf("%" JSON_INTEGER_FORMAT, json_integer_value(j));
107 *result = string_sprintf("%f", json_real_value(j));
109 case JSON_TRUE: *result = US"true"; break;
110 case JSON_FALSE: *result = US"false"; break;
111 case JSON_NULL: *result = NULL; break;
112 default: *result = US json_dumps(j, 0); break;
120 /*************************************************
121 * Close entry point *
122 *************************************************/
124 /* See local README for interface description */
127 json_close(void *handle)
129 (void)fclose((FILE *)handle);
134 /*************************************************
135 * Version reporting entry point *
136 *************************************************/
138 /* See local README for interface description. */
140 #include "../version.h"
143 json_version_report(FILE *f)
145 fprintf(f, "Library version: json: Jansonn version %s\n", JANSSON_VERSION);
149 static lookup_info json_lookup_info = {
150 US"json", /* lookup name */
151 lookup_absfile, /* uses absolute file name */
152 json_open, /* open function */
153 json_check, /* check function */
154 json_find, /* find function */
155 json_close, /* close function */
156 NULL, /* no tidy function */
157 NULL, /* no quoting function */
158 json_version_report /* version reporting */
163 #define json_lookup_module_info _lookup_module_info
166 static lookup_info *_lookup_list[] = { &json_lookup_info };
167 lookup_module_info json_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
169 /* End of lookups/json.c */