blob: 0669cba2f4e99acb36087dba329b11cc8f560417 [file] [log] [blame]
David Carlier8167f302015-06-01 13:50:06 +02001#include <stdio.h>
2
3#include <common/cfgparse.h>
Willy Tarreau876054d2016-12-21 20:39:16 +01004#include <common/errors.h>
Willy Tarreauf63386a2015-06-01 15:39:50 +02005#include <proto/arg.h>
David Carlier8167f302015-06-01 13:50:06 +02006#include <proto/log.h>
David Carlier608c65a2015-09-25 14:16:30 +01007#include <proto/proto_http.h>
Willy Tarreauf63386a2015-06-01 15:39:50 +02008#include <proto/sample.h>
Willy Tarreaubee9dde2016-12-21 21:25:06 +01009#include <dac.h>
10
11static struct {
12 void *atlasimgptr;
13 char *jsonpath;
14 char *cookiename;
15 size_t cookienamelen;
16 da_atlas_t atlas;
17 da_evidence_id_t useragentid;
18 da_severity_t loglevel;
19 char separator;
20 unsigned char daset:1;
21} global_deviceatlas = {
22 .loglevel = 0,
23 .jsonpath = 0,
24 .cookiename = 0,
25 .cookienamelen = 0,
26 .useragentid = 0,
27 .daset = 0,
28 .separator = '|',
29};
David Carlier8167f302015-06-01 13:50:06 +020030
31static int da_json_file(char **args, int section_type, struct proxy *curpx,
32 struct proxy *defpx, const char *file, int line,
33 char **err)
34{
35 if (*(args[1]) == 0) {
36 memprintf(err, "deviceatlas json file : expects a json path.\n");
37 return -1;
38 }
Willy Tarreaubee9dde2016-12-21 21:25:06 +010039 global_deviceatlas.jsonpath = strdup(args[1]);
David Carlier8167f302015-06-01 13:50:06 +020040 return 0;
41}
42
43static int da_log_level(char **args, int section_type, struct proxy *curpx,
44 struct proxy *defpx, const char *file, int line,
45 char **err)
46{
47 int loglevel;
48 if (*(args[1]) == 0) {
49 memprintf(err, "deviceatlas log level : expects an integer argument.\n");
50 return -1;
51 }
52
53 loglevel = atol(args[1]);
54 if (loglevel < 0 || loglevel > 3) {
55 memprintf(err, "deviceatlas log level : expects a log level between 0 and 3, %s given.\n", args[1]);
56 } else {
Willy Tarreaubee9dde2016-12-21 21:25:06 +010057 global_deviceatlas.loglevel = (da_severity_t)loglevel;
David Carlier8167f302015-06-01 13:50:06 +020058 }
59
60 return 0;
61}
62
63static int da_property_separator(char **args, int section_type, struct proxy *curpx,
64 struct proxy *defpx, const char *file, int line,
65 char **err)
66{
67 if (*(args[1]) == 0) {
68 memprintf(err, "deviceatlas property separator : expects a character argument.\n");
69 return -1;
70 }
Willy Tarreaubee9dde2016-12-21 21:25:06 +010071 global_deviceatlas.separator = *args[1];
David Carlier8167f302015-06-01 13:50:06 +020072 return 0;
73}
74
David Carlier608c65a2015-09-25 14:16:30 +010075static int da_properties_cookie(char **args, int section_type, struct proxy *curpx,
76 struct proxy *defpx, const char *file, int line,
77 char **err)
78{
79 if (*(args[1]) == 0) {
80 memprintf(err, "deviceatlas cookie name : expects a string argument.\n");
81 return -1;
82 } else {
Willy Tarreaubee9dde2016-12-21 21:25:06 +010083 global_deviceatlas.cookiename = strdup(args[1]);
David Carlier608c65a2015-09-25 14:16:30 +010084 }
Willy Tarreaubee9dde2016-12-21 21:25:06 +010085 global_deviceatlas.cookienamelen = strlen(global_deviceatlas.cookiename);
David Carlier608c65a2015-09-25 14:16:30 +010086 return 0;
87}
88
David Carlier8167f302015-06-01 13:50:06 +020089static size_t da_haproxy_read(void *ctx, size_t len, char *buf)
90{
91 return fread(buf, 1, len, ctx);
92}
93
94static da_status_t da_haproxy_seek(void *ctx, off_t off)
95{
96 return fseek(ctx, off, SEEK_SET) != -1 ? DA_OK : DA_SYS;
97}
98
99static void da_haproxy_log(da_severity_t severity, da_status_t status,
100 const char *fmt, va_list args)
101{
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100102 if (global_deviceatlas.loglevel && severity <= global_deviceatlas.loglevel) {
David Carlier8167f302015-06-01 13:50:06 +0200103 char logbuf[256];
104 vsnprintf(logbuf, sizeof(logbuf), fmt, args);
Christopher Faulet767a84b2017-11-24 16:50:31 +0100105 ha_warning("deviceatlas : %s.\n", logbuf);
David Carlier8167f302015-06-01 13:50:06 +0200106 }
107}
108
David Carlier608c65a2015-09-25 14:16:30 +0100109#define DA_COOKIENAME_DEFAULT "DAPROPS"
110
Willy Tarreau876054d2016-12-21 20:39:16 +0100111/*
112 * module init / deinit functions. Returns 0 if OK, or a combination of ERR_*.
113 */
114static int init_deviceatlas(void)
David Carlier8167f302015-06-01 13:50:06 +0200115{
Willy Tarreau876054d2016-12-21 20:39:16 +0100116 int err_code = 0;
117
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100118 if (global_deviceatlas.jsonpath != 0) {
David Carlier8167f302015-06-01 13:50:06 +0200119 FILE *jsonp;
120 da_property_decl_t extraprops[] = {{0, 0}};
121 size_t atlasimglen;
122 da_status_t status;
123
Christopher Faulete8ca4342017-10-25 17:23:02 +0200124 if (global.nbthread > 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100125 ha_alert("deviceatlas: multithreading is not supported for now.\n");
Christopher Faulete8ca4342017-10-25 17:23:02 +0200126 err_code |= ERR_ALERT | ERR_FATAL;
127 goto out;
128 }
129
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100130 jsonp = fopen(global_deviceatlas.jsonpath, "r");
David Carlier8167f302015-06-01 13:50:06 +0200131 if (jsonp == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100132 ha_alert("deviceatlas : '%s' json file has invalid path or is not readable.\n",
133 global_deviceatlas.jsonpath);
Willy Tarreau876054d2016-12-21 20:39:16 +0100134 err_code |= ERR_ALERT | ERR_FATAL;
David Carlier8167f302015-06-01 13:50:06 +0200135 goto out;
136 }
137
138 da_init();
139 da_seterrorfunc(da_haproxy_log);
140 status = da_atlas_compile(jsonp, da_haproxy_read, da_haproxy_seek,
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100141 &global_deviceatlas.atlasimgptr, &atlasimglen);
David Carlier8167f302015-06-01 13:50:06 +0200142 fclose(jsonp);
143 if (status != DA_OK) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100144 ha_alert("deviceatlas : '%s' json file is invalid.\n",
145 global_deviceatlas.jsonpath);
Willy Tarreau876054d2016-12-21 20:39:16 +0100146 err_code |= ERR_ALERT | ERR_FATAL;
David Carlier8167f302015-06-01 13:50:06 +0200147 goto out;
148 }
149
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100150 status = da_atlas_open(&global_deviceatlas.atlas, extraprops,
151 global_deviceatlas.atlasimgptr, atlasimglen);
David Carlier8167f302015-06-01 13:50:06 +0200152
153 if (status != DA_OK) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100154 ha_alert("deviceatlas : data could not be compiled.\n");
Willy Tarreau876054d2016-12-21 20:39:16 +0100155 err_code |= ERR_ALERT | ERR_FATAL;
David Carlier8167f302015-06-01 13:50:06 +0200156 goto out;
157 }
158
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100159 if (global_deviceatlas.cookiename == 0) {
160 global_deviceatlas.cookiename = strdup(DA_COOKIENAME_DEFAULT);
161 global_deviceatlas.cookienamelen = strlen(global_deviceatlas.cookiename);
David Carlier608c65a2015-09-25 14:16:30 +0100162 }
163
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100164 global_deviceatlas.useragentid = da_atlas_header_evidence_id(&global_deviceatlas.atlas,
David Carlier8167f302015-06-01 13:50:06 +0200165 "user-agent");
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100166 global_deviceatlas.daset = 1;
David Carlier8167f302015-06-01 13:50:06 +0200167
168 fprintf(stdout, "Deviceatlas module loaded.\n");
169 }
170
171out:
Willy Tarreau876054d2016-12-21 20:39:16 +0100172 return err_code;
David Carlier8167f302015-06-01 13:50:06 +0200173}
174
Willy Tarreaub149eed2016-12-21 21:03:49 +0100175static void deinit_deviceatlas(void)
David Carlier8167f302015-06-01 13:50:06 +0200176{
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100177 if (global_deviceatlas.jsonpath != 0) {
178 free(global_deviceatlas.jsonpath);
David Carlier8167f302015-06-01 13:50:06 +0200179 }
180
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100181 if (global_deviceatlas.daset == 1) {
182 free(global_deviceatlas.cookiename);
183 da_atlas_close(&global_deviceatlas.atlas);
184 free(global_deviceatlas.atlasimgptr);
David Carlier8167f302015-06-01 13:50:06 +0200185 }
186
187 da_fini();
188}
189
David Carlier608c65a2015-09-25 14:16:30 +0100190static int da_haproxy(const struct arg *args, struct sample *smp, da_deviceinfo_t *devinfo)
David Carlier8167f302015-06-01 13:50:06 +0200191{
192 struct chunk *tmp;
David Carlier8167f302015-06-01 13:50:06 +0200193 da_propid_t prop, *pprop;
David Carlier8167f302015-06-01 13:50:06 +0200194 da_status_t status;
David Carlier608c65a2015-09-25 14:16:30 +0100195 da_type_t proptype;
196 const char *propname;
David Carlier8167f302015-06-01 13:50:06 +0200197 int i;
198
David Carlier8167f302015-06-01 13:50:06 +0200199 tmp = get_trash_chunk();
200 chunk_reset(tmp);
201
David Carlier8167f302015-06-01 13:50:06 +0200202 propname = (const char *)args[0].data.str.str;
203 i = 0;
204
David Carlier8167f302015-06-01 13:50:06 +0200205 for (; propname != 0; i ++, propname = (const char *)args[i].data.str.str) {
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100206 status = da_atlas_getpropid(&global_deviceatlas.atlas,
David Carlier8167f302015-06-01 13:50:06 +0200207 propname, &prop);
208 if (status != DA_OK) {
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100209 chunk_appendf(tmp, "%c", global_deviceatlas.separator);
David Carlier8167f302015-06-01 13:50:06 +0200210 continue;
211 }
212 pprop = &prop;
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100213 da_atlas_getproptype(&global_deviceatlas.atlas, *pprop, &proptype);
David Carlier8167f302015-06-01 13:50:06 +0200214
215 switch (proptype) {
216 case DA_TYPE_BOOLEAN: {
217 bool val;
David Carlier608c65a2015-09-25 14:16:30 +0100218 status = da_getpropboolean(devinfo, *pprop, &val);
David Carlier8167f302015-06-01 13:50:06 +0200219 if (status == DA_OK) {
220 chunk_appendf(tmp, "%d", val);
221 }
222 break;
223 }
224 case DA_TYPE_INTEGER:
225 case DA_TYPE_NUMBER: {
226 long val;
David Carlier608c65a2015-09-25 14:16:30 +0100227 status = da_getpropinteger(devinfo, *pprop, &val);
David Carlier8167f302015-06-01 13:50:06 +0200228 if (status == DA_OK) {
229 chunk_appendf(tmp, "%ld", val);
230 }
231 break;
232 }
233 case DA_TYPE_STRING: {
234 const char *val;
David Carlier608c65a2015-09-25 14:16:30 +0100235 status = da_getpropstring(devinfo, *pprop, &val);
David Carlier8167f302015-06-01 13:50:06 +0200236 if (status == DA_OK) {
237 chunk_appendf(tmp, "%s", val);
238 }
239 break;
David Carlier608c65a2015-09-25 14:16:30 +0100240 }
David Carlier8167f302015-06-01 13:50:06 +0200241 default:
242 break;
243 }
244
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100245 chunk_appendf(tmp, "%c", global_deviceatlas.separator);
David Carlier8167f302015-06-01 13:50:06 +0200246 }
247
David Carlier608c65a2015-09-25 14:16:30 +0100248 da_close(devinfo);
David Carlier8167f302015-06-01 13:50:06 +0200249
250 if (tmp->len) {
251 --tmp->len;
252 tmp->str[tmp->len] = 0;
253 }
254
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200255 smp->data.u.str.str = tmp->str;
David Carlier608c65a2015-09-25 14:16:30 +0100256 smp->data.u.str.len = tmp->len;
David Carlier8167f302015-06-01 13:50:06 +0200257
258 return 1;
259}
260
David Carlier608c65a2015-09-25 14:16:30 +0100261static int da_haproxy_conv(const struct arg *args, struct sample *smp, void *private)
262{
263 da_deviceinfo_t devinfo;
264 da_status_t status;
265 const char *useragent;
266 char useragentbuf[1024] = { 0 };
267 int i;
268
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100269 if (global_deviceatlas.daset == 0 || smp->data.u.str.len == 0) {
David Carlier608c65a2015-09-25 14:16:30 +0100270 return 1;
271 }
272
273 i = smp->data.u.str.len > sizeof(useragentbuf) ? sizeof(useragentbuf) : smp->data.u.str.len;
274 memcpy(useragentbuf, smp->data.u.str.str, i - 1);
275 useragentbuf[i - 1] = 0;
276
277 useragent = (const char *)useragentbuf;
278
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100279 status = da_search(&global_deviceatlas.atlas, &devinfo,
280 global_deviceatlas.useragentid, useragent, 0);
David Carlier608c65a2015-09-25 14:16:30 +0100281
282 return status != DA_OK ? 0 : da_haproxy(args, smp, &devinfo);
283}
284
285#define DA_MAX_HEADERS 24
286
287static int da_haproxy_fetch(const struct arg *args, struct sample *smp, const char *kw, void *private)
288{
289 struct hdr_idx *hidx;
290 struct hdr_ctx hctx;
291 const struct http_msg *hmsg;
292 da_evidence_t ev[DA_MAX_HEADERS];
293 da_deviceinfo_t devinfo;
294 da_status_t status;
295 char vbuf[DA_MAX_HEADERS][1024] = {{ 0 }};
296 int i, nbh = 0;
297
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100298 if (global_deviceatlas.daset == 0) {
David Carlier608c65a2015-09-25 14:16:30 +0100299 return 1;
300 }
301
302 CHECK_HTTP_MESSAGE_FIRST();
303 smp->data.type = SMP_T_STR;
304
305 /**
306 * Here we go through the whole list of headers from start
307 * they will be filtered via the DeviceAtlas API itself
308 */
309 hctx.idx = 0;
310 hidx = &smp->strm->txn->hdr_idx;
311 hmsg = &smp->strm->txn->req;
312
313 while (http_find_next_header(hmsg->chn->buf->p, hidx, &hctx) == 1 &&
314 nbh < DA_MAX_HEADERS) {
315 char *pval;
316 size_t vlen;
317 da_evidence_id_t evid = -1;
318 char hbuf[24] = { 0 };
319
320 /* The HTTP headers used by the DeviceAtlas API are not longer */
David Carlier91a88b02017-11-17 08:47:25 +0000321 if (hctx.del >= sizeof(hbuf) || hctx.del <= 0 || hctx.vlen <= 0) {
David Carlier608c65a2015-09-25 14:16:30 +0100322 continue;
323 }
324
325 vlen = hctx.vlen;
326 memcpy(hbuf, hctx.line, hctx.del);
327 hbuf[hctx.del] = 0;
328 pval = (hctx.line + hctx.val);
329
330 if (strcmp(hbuf, "Accept-Language") == 0) {
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100331 evid = da_atlas_accept_language_evidence_id(&global_deviceatlas.
David Carlier608c65a2015-09-25 14:16:30 +0100332 atlas);
333 } else if (strcmp(hbuf, "Cookie") == 0) {
334 char *p, *eval;
335 int pl;
336
337 eval = pval + hctx.vlen;
338 /**
339 * The cookie value, if it exists, is located between the current header's
340 * value position and the next one
341 */
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100342 if (extract_cookie_value(pval, eval, global_deviceatlas.cookiename,
343 global_deviceatlas.cookienamelen, 1, &p, &pl) == NULL) {
David Carlier608c65a2015-09-25 14:16:30 +0100344 continue;
345 }
346
347 vlen = (size_t)pl;
348 pval = p;
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100349 evid = da_atlas_clientprop_evidence_id(&global_deviceatlas.atlas);
David Carlier608c65a2015-09-25 14:16:30 +0100350 } else {
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100351 evid = da_atlas_header_evidence_id(&global_deviceatlas.atlas,
David Carlier608c65a2015-09-25 14:16:30 +0100352 hbuf);
353 }
354
355 if (evid == -1) {
356 continue;
357 }
358
359 i = vlen > sizeof(vbuf[nbh]) ? sizeof(vbuf[nbh]) : vlen;
360 memcpy(vbuf[nbh], pval, i - 1);
361 vbuf[nbh][i - 1] = 0;
362 ev[nbh].key = evid;
363 ev[nbh].value = vbuf[nbh];
364 ev[nbh].value[vlen] = 0;
365 ++ nbh;
366 }
367
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100368 status = da_searchv(&global_deviceatlas.atlas, &devinfo,
David Carlier608c65a2015-09-25 14:16:30 +0100369 ev, nbh);
370
371 return status != DA_OK ? 0 : da_haproxy(args, smp, &devinfo);
372}
373
Willy Tarreau0d74f772015-06-01 15:42:29 +0200374static struct cfg_kw_list dacfg_kws = {{ }, {
375 { CFG_GLOBAL, "deviceatlas-json-file", da_json_file },
376 { CFG_GLOBAL, "deviceatlas-log-level", da_log_level },
377 { CFG_GLOBAL, "deviceatlas-property-separator", da_property_separator },
David Carlier608c65a2015-09-25 14:16:30 +0100378 { CFG_GLOBAL, "deviceatlas-properties-cookie", da_properties_cookie },
Willy Tarreau0d74f772015-06-01 15:42:29 +0200379 { 0, NULL, NULL },
380}};
381
Willy Tarreauf63386a2015-06-01 15:39:50 +0200382/* Note: must not be declared <const> as its list will be overwritten */
David Carlier608c65a2015-09-25 14:16:30 +0100383static struct sample_fetch_kw_list fetch_kws = {ILH, {
David Carlier840b0242016-03-16 10:09:55 +0000384 { "da-csv-fetch", da_haproxy_fetch, ARG12(1,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
David Carlier608c65a2015-09-25 14:16:30 +0100385 { NULL, NULL, 0, 0, 0 },
386}};
387
388/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreauf63386a2015-06-01 15:39:50 +0200389static struct sample_conv_kw_list conv_kws = {ILH, {
David Carlier840b0242016-03-16 10:09:55 +0000390 { "da-csv-conv", da_haproxy_conv, ARG12(1,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR), NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreauf63386a2015-06-01 15:39:50 +0200391 { NULL, NULL, 0, 0, 0 },
392}};
393
394__attribute__((constructor))
395static void __da_init(void)
396{
397 /* register sample fetch and format conversion keywords */
David Carlier608c65a2015-09-25 14:16:30 +0100398 sample_register_fetches(&fetch_kws);
Willy Tarreauf63386a2015-06-01 15:39:50 +0200399 sample_register_convs(&conv_kws);
Willy Tarreau0d74f772015-06-01 15:42:29 +0200400 cfg_register_keywords(&dacfg_kws);
Willy Tarreau3dd483e2016-12-21 18:50:22 +0100401 hap_register_build_opts("Built with DeviceAtlas support.", 0);
Willy Tarreau876054d2016-12-21 20:39:16 +0100402 hap_register_post_check(init_deviceatlas);
Willy Tarreaub149eed2016-12-21 21:03:49 +0100403 hap_register_post_deinit(deinit_deviceatlas);
Willy Tarreauf63386a2015-06-01 15:39:50 +0200404}