blob: ee3d9af5d9ec1c5489612ddfb4484def10574c0d [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 Tarreau491cec22018-10-02 18:37:27 +02005#include <common/http.h>
Willy Tarreau0108d902018-11-25 19:14:37 +01006#include <common/initcall.h>
Willy Tarreau80713382018-11-26 10:19:54 +01007#include <types/global.h>
Willy Tarreauf63386a2015-06-01 15:39:50 +02008#include <proto/arg.h>
Willy Tarreau79e57332018-10-02 16:01:16 +02009#include <proto/http_fetch.h>
David Carlier8167f302015-06-01 13:50:06 +020010#include <proto/log.h>
David Carlier608c65a2015-09-25 14:16:30 +010011#include <proto/proto_http.h>
Willy Tarreauf63386a2015-06-01 15:39:50 +020012#include <proto/sample.h>
Willy Tarreaubee9dde2016-12-21 21:25:06 +010013#include <dac.h>
14
15static struct {
16 void *atlasimgptr;
17 char *jsonpath;
18 char *cookiename;
19 size_t cookienamelen;
20 da_atlas_t atlas;
21 da_evidence_id_t useragentid;
22 da_severity_t loglevel;
23 char separator;
24 unsigned char daset:1;
25} global_deviceatlas = {
26 .loglevel = 0,
27 .jsonpath = 0,
28 .cookiename = 0,
29 .cookienamelen = 0,
30 .useragentid = 0,
31 .daset = 0,
32 .separator = '|',
33};
David Carlier8167f302015-06-01 13:50:06 +020034
35static int da_json_file(char **args, int section_type, struct proxy *curpx,
36 struct proxy *defpx, const char *file, int line,
37 char **err)
38{
39 if (*(args[1]) == 0) {
40 memprintf(err, "deviceatlas json file : expects a json path.\n");
41 return -1;
42 }
Willy Tarreaubee9dde2016-12-21 21:25:06 +010043 global_deviceatlas.jsonpath = strdup(args[1]);
David Carlier8167f302015-06-01 13:50:06 +020044 return 0;
45}
46
47static int da_log_level(char **args, int section_type, struct proxy *curpx,
48 struct proxy *defpx, const char *file, int line,
49 char **err)
50{
51 int loglevel;
52 if (*(args[1]) == 0) {
53 memprintf(err, "deviceatlas log level : expects an integer argument.\n");
54 return -1;
55 }
56
57 loglevel = atol(args[1]);
58 if (loglevel < 0 || loglevel > 3) {
59 memprintf(err, "deviceatlas log level : expects a log level between 0 and 3, %s given.\n", args[1]);
60 } else {
Willy Tarreaubee9dde2016-12-21 21:25:06 +010061 global_deviceatlas.loglevel = (da_severity_t)loglevel;
David Carlier8167f302015-06-01 13:50:06 +020062 }
63
64 return 0;
65}
66
67static int da_property_separator(char **args, int section_type, struct proxy *curpx,
68 struct proxy *defpx, const char *file, int line,
69 char **err)
70{
71 if (*(args[1]) == 0) {
72 memprintf(err, "deviceatlas property separator : expects a character argument.\n");
73 return -1;
74 }
Willy Tarreaubee9dde2016-12-21 21:25:06 +010075 global_deviceatlas.separator = *args[1];
David Carlier8167f302015-06-01 13:50:06 +020076 return 0;
77}
78
David Carlier608c65a2015-09-25 14:16:30 +010079static int da_properties_cookie(char **args, int section_type, struct proxy *curpx,
80 struct proxy *defpx, const char *file, int line,
81 char **err)
82{
83 if (*(args[1]) == 0) {
84 memprintf(err, "deviceatlas cookie name : expects a string argument.\n");
85 return -1;
86 } else {
Willy Tarreaubee9dde2016-12-21 21:25:06 +010087 global_deviceatlas.cookiename = strdup(args[1]);
David Carlier608c65a2015-09-25 14:16:30 +010088 }
Willy Tarreaubee9dde2016-12-21 21:25:06 +010089 global_deviceatlas.cookienamelen = strlen(global_deviceatlas.cookiename);
David Carlier608c65a2015-09-25 14:16:30 +010090 return 0;
91}
92
David Carlier8167f302015-06-01 13:50:06 +020093static size_t da_haproxy_read(void *ctx, size_t len, char *buf)
94{
95 return fread(buf, 1, len, ctx);
96}
97
98static da_status_t da_haproxy_seek(void *ctx, off_t off)
99{
100 return fseek(ctx, off, SEEK_SET) != -1 ? DA_OK : DA_SYS;
101}
102
103static void da_haproxy_log(da_severity_t severity, da_status_t status,
104 const char *fmt, va_list args)
105{
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100106 if (global_deviceatlas.loglevel && severity <= global_deviceatlas.loglevel) {
David Carlier8167f302015-06-01 13:50:06 +0200107 char logbuf[256];
108 vsnprintf(logbuf, sizeof(logbuf), fmt, args);
Christopher Faulet767a84b2017-11-24 16:50:31 +0100109 ha_warning("deviceatlas : %s.\n", logbuf);
David Carlier8167f302015-06-01 13:50:06 +0200110 }
111}
112
David Carlier608c65a2015-09-25 14:16:30 +0100113#define DA_COOKIENAME_DEFAULT "DAPROPS"
114
Willy Tarreau876054d2016-12-21 20:39:16 +0100115/*
116 * module init / deinit functions. Returns 0 if OK, or a combination of ERR_*.
117 */
118static int init_deviceatlas(void)
David Carlier8167f302015-06-01 13:50:06 +0200119{
Willy Tarreau876054d2016-12-21 20:39:16 +0100120 int err_code = 0;
121
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100122 if (global_deviceatlas.jsonpath != 0) {
David Carlier8167f302015-06-01 13:50:06 +0200123 FILE *jsonp;
124 da_property_decl_t extraprops[] = {{0, 0}};
125 size_t atlasimglen;
126 da_status_t status;
127
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100128 jsonp = fopen(global_deviceatlas.jsonpath, "r");
David Carlier8167f302015-06-01 13:50:06 +0200129 if (jsonp == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100130 ha_alert("deviceatlas : '%s' json file has invalid path or is not readable.\n",
131 global_deviceatlas.jsonpath);
Willy Tarreau876054d2016-12-21 20:39:16 +0100132 err_code |= ERR_ALERT | ERR_FATAL;
David Carlier8167f302015-06-01 13:50:06 +0200133 goto out;
134 }
135
136 da_init();
137 da_seterrorfunc(da_haproxy_log);
138 status = da_atlas_compile(jsonp, da_haproxy_read, da_haproxy_seek,
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100139 &global_deviceatlas.atlasimgptr, &atlasimglen);
David Carlier8167f302015-06-01 13:50:06 +0200140 fclose(jsonp);
141 if (status != DA_OK) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100142 ha_alert("deviceatlas : '%s' json file is invalid.\n",
143 global_deviceatlas.jsonpath);
Willy Tarreau876054d2016-12-21 20:39:16 +0100144 err_code |= ERR_ALERT | ERR_FATAL;
David Carlier8167f302015-06-01 13:50:06 +0200145 goto out;
146 }
147
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100148 status = da_atlas_open(&global_deviceatlas.atlas, extraprops,
149 global_deviceatlas.atlasimgptr, atlasimglen);
David Carlier8167f302015-06-01 13:50:06 +0200150
151 if (status != DA_OK) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100152 ha_alert("deviceatlas : data could not be compiled.\n");
Willy Tarreau876054d2016-12-21 20:39:16 +0100153 err_code |= ERR_ALERT | ERR_FATAL;
David Carlier8167f302015-06-01 13:50:06 +0200154 goto out;
155 }
156
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100157 if (global_deviceatlas.cookiename == 0) {
158 global_deviceatlas.cookiename = strdup(DA_COOKIENAME_DEFAULT);
159 global_deviceatlas.cookienamelen = strlen(global_deviceatlas.cookiename);
David Carlier608c65a2015-09-25 14:16:30 +0100160 }
161
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100162 global_deviceatlas.useragentid = da_atlas_header_evidence_id(&global_deviceatlas.atlas,
David Carlier8167f302015-06-01 13:50:06 +0200163 "user-agent");
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100164 global_deviceatlas.daset = 1;
David Carlier8167f302015-06-01 13:50:06 +0200165
166 fprintf(stdout, "Deviceatlas module loaded.\n");
167 }
168
169out:
Willy Tarreau876054d2016-12-21 20:39:16 +0100170 return err_code;
David Carlier8167f302015-06-01 13:50:06 +0200171}
172
Willy Tarreaub149eed2016-12-21 21:03:49 +0100173static void deinit_deviceatlas(void)
David Carlier8167f302015-06-01 13:50:06 +0200174{
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100175 if (global_deviceatlas.jsonpath != 0) {
176 free(global_deviceatlas.jsonpath);
David Carlier8167f302015-06-01 13:50:06 +0200177 }
178
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100179 if (global_deviceatlas.daset == 1) {
180 free(global_deviceatlas.cookiename);
181 da_atlas_close(&global_deviceatlas.atlas);
182 free(global_deviceatlas.atlasimgptr);
David Carlier8167f302015-06-01 13:50:06 +0200183 }
184
185 da_fini();
186}
187
David Carlier608c65a2015-09-25 14:16:30 +0100188static int da_haproxy(const struct arg *args, struct sample *smp, da_deviceinfo_t *devinfo)
David Carlier8167f302015-06-01 13:50:06 +0200189{
Willy Tarreau83061a82018-07-13 11:56:34 +0200190 struct buffer *tmp;
David Carlier8167f302015-06-01 13:50:06 +0200191 da_propid_t prop, *pprop;
David Carlier8167f302015-06-01 13:50:06 +0200192 da_status_t status;
David Carlier608c65a2015-09-25 14:16:30 +0100193 da_type_t proptype;
194 const char *propname;
David Carlier8167f302015-06-01 13:50:06 +0200195 int i;
196
David Carlier8167f302015-06-01 13:50:06 +0200197 tmp = get_trash_chunk();
198 chunk_reset(tmp);
199
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200200 propname = (const char *) args[0].data.str.area;
David Carlier8167f302015-06-01 13:50:06 +0200201 i = 0;
202
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200203 for (; propname != 0; i ++,
204 propname = (const char *) args[i].data.str.area) {
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100205 status = da_atlas_getpropid(&global_deviceatlas.atlas,
David Carlier8167f302015-06-01 13:50:06 +0200206 propname, &prop);
207 if (status != DA_OK) {
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100208 chunk_appendf(tmp, "%c", global_deviceatlas.separator);
David Carlier8167f302015-06-01 13:50:06 +0200209 continue;
210 }
211 pprop = &prop;
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100212 da_atlas_getproptype(&global_deviceatlas.atlas, *pprop, &proptype);
David Carlier8167f302015-06-01 13:50:06 +0200213
214 switch (proptype) {
215 case DA_TYPE_BOOLEAN: {
216 bool val;
David Carlier608c65a2015-09-25 14:16:30 +0100217 status = da_getpropboolean(devinfo, *pprop, &val);
David Carlier8167f302015-06-01 13:50:06 +0200218 if (status == DA_OK) {
219 chunk_appendf(tmp, "%d", val);
220 }
221 break;
222 }
223 case DA_TYPE_INTEGER:
224 case DA_TYPE_NUMBER: {
225 long val;
David Carlier608c65a2015-09-25 14:16:30 +0100226 status = da_getpropinteger(devinfo, *pprop, &val);
David Carlier8167f302015-06-01 13:50:06 +0200227 if (status == DA_OK) {
228 chunk_appendf(tmp, "%ld", val);
229 }
230 break;
231 }
232 case DA_TYPE_STRING: {
233 const char *val;
David Carlier608c65a2015-09-25 14:16:30 +0100234 status = da_getpropstring(devinfo, *pprop, &val);
David Carlier8167f302015-06-01 13:50:06 +0200235 if (status == DA_OK) {
236 chunk_appendf(tmp, "%s", val);
237 }
238 break;
David Carlier608c65a2015-09-25 14:16:30 +0100239 }
David Carlier8167f302015-06-01 13:50:06 +0200240 default:
241 break;
242 }
243
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100244 chunk_appendf(tmp, "%c", global_deviceatlas.separator);
David Carlier8167f302015-06-01 13:50:06 +0200245 }
246
David Carlier608c65a2015-09-25 14:16:30 +0100247 da_close(devinfo);
David Carlier8167f302015-06-01 13:50:06 +0200248
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200249 if (tmp->data) {
250 --tmp->data;
251 tmp->area[tmp->data] = 0;
David Carlier8167f302015-06-01 13:50:06 +0200252 }
253
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200254 smp->data.u.str.area = tmp->area;
255 smp->data.u.str.data = tmp->data;
David Carlier8167f302015-06-01 13:50:06 +0200256
257 return 1;
258}
259
David Carlier608c65a2015-09-25 14:16:30 +0100260static int da_haproxy_conv(const struct arg *args, struct sample *smp, void *private)
261{
262 da_deviceinfo_t devinfo;
263 da_status_t status;
264 const char *useragent;
265 char useragentbuf[1024] = { 0 };
266 int i;
267
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200268 if (global_deviceatlas.daset == 0 || smp->data.u.str.data == 0) {
David Carlier608c65a2015-09-25 14:16:30 +0100269 return 1;
270 }
271
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200272 i = smp->data.u.str.data > sizeof(useragentbuf) ? sizeof(useragentbuf) : smp->data.u.str.data;
273 memcpy(useragentbuf, smp->data.u.str.area, i - 1);
David Carlier608c65a2015-09-25 14:16:30 +0100274 useragentbuf[i - 1] = 0;
275
276 useragent = (const char *)useragentbuf;
277
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100278 status = da_search(&global_deviceatlas.atlas, &devinfo,
279 global_deviceatlas.useragentid, useragent, 0);
David Carlier608c65a2015-09-25 14:16:30 +0100280
281 return status != DA_OK ? 0 : da_haproxy(args, smp, &devinfo);
282}
283
284#define DA_MAX_HEADERS 24
285
286static int da_haproxy_fetch(const struct arg *args, struct sample *smp, const char *kw, void *private)
287{
288 struct hdr_idx *hidx;
289 struct hdr_ctx hctx;
290 const struct http_msg *hmsg;
291 da_evidence_t ev[DA_MAX_HEADERS];
292 da_deviceinfo_t devinfo;
293 da_status_t status;
294 char vbuf[DA_MAX_HEADERS][1024] = {{ 0 }};
295 int i, nbh = 0;
296
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100297 if (global_deviceatlas.daset == 0) {
David Carlier608c65a2015-09-25 14:16:30 +0100298 return 1;
299 }
300
301 CHECK_HTTP_MESSAGE_FIRST();
302 smp->data.type = SMP_T_STR;
303
304 /**
305 * Here we go through the whole list of headers from start
306 * they will be filtered via the DeviceAtlas API itself
307 */
308 hctx.idx = 0;
309 hidx = &smp->strm->txn->hdr_idx;
310 hmsg = &smp->strm->txn->req;
311
312 while (http_find_next_header(hmsg->chn->buf->p, hidx, &hctx) == 1 &&
313 nbh < DA_MAX_HEADERS) {
314 char *pval;
315 size_t vlen;
316 da_evidence_id_t evid = -1;
317 char hbuf[24] = { 0 };
318
319 /* The HTTP headers used by the DeviceAtlas API are not longer */
David Carlier91a88b02017-11-17 08:47:25 +0000320 if (hctx.del >= sizeof(hbuf) || hctx.del <= 0 || hctx.vlen <= 0) {
David Carlier608c65a2015-09-25 14:16:30 +0100321 continue;
322 }
323
324 vlen = hctx.vlen;
325 memcpy(hbuf, hctx.line, hctx.del);
326 hbuf[hctx.del] = 0;
327 pval = (hctx.line + hctx.val);
328
329 if (strcmp(hbuf, "Accept-Language") == 0) {
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100330 evid = da_atlas_accept_language_evidence_id(&global_deviceatlas.
David Carlier608c65a2015-09-25 14:16:30 +0100331 atlas);
332 } else if (strcmp(hbuf, "Cookie") == 0) {
333 char *p, *eval;
334 int pl;
335
336 eval = pval + hctx.vlen;
337 /**
338 * The cookie value, if it exists, is located between the current header's
339 * value position and the next one
340 */
Willy Tarreau491cec22018-10-02 18:37:27 +0200341 if (http_extract_cookie_value(pval, eval, global_deviceatlas.cookiename,
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100342 global_deviceatlas.cookienamelen, 1, &p, &pl) == NULL) {
David Carlier608c65a2015-09-25 14:16:30 +0100343 continue;
344 }
345
346 vlen = (size_t)pl;
347 pval = p;
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100348 evid = da_atlas_clientprop_evidence_id(&global_deviceatlas.atlas);
David Carlier608c65a2015-09-25 14:16:30 +0100349 } else {
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100350 evid = da_atlas_header_evidence_id(&global_deviceatlas.atlas,
David Carlier608c65a2015-09-25 14:16:30 +0100351 hbuf);
352 }
353
354 if (evid == -1) {
355 continue;
356 }
357
358 i = vlen > sizeof(vbuf[nbh]) ? sizeof(vbuf[nbh]) : vlen;
359 memcpy(vbuf[nbh], pval, i - 1);
360 vbuf[nbh][i - 1] = 0;
361 ev[nbh].key = evid;
362 ev[nbh].value = vbuf[nbh];
363 ev[nbh].value[vlen] = 0;
364 ++ nbh;
365 }
366
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100367 status = da_searchv(&global_deviceatlas.atlas, &devinfo,
David Carlier608c65a2015-09-25 14:16:30 +0100368 ev, nbh);
369
370 return status != DA_OK ? 0 : da_haproxy(args, smp, &devinfo);
371}
372
Willy Tarreau0d74f772015-06-01 15:42:29 +0200373static struct cfg_kw_list dacfg_kws = {{ }, {
374 { CFG_GLOBAL, "deviceatlas-json-file", da_json_file },
375 { CFG_GLOBAL, "deviceatlas-log-level", da_log_level },
376 { CFG_GLOBAL, "deviceatlas-property-separator", da_property_separator },
David Carlier608c65a2015-09-25 14:16:30 +0100377 { CFG_GLOBAL, "deviceatlas-properties-cookie", da_properties_cookie },
Willy Tarreau0d74f772015-06-01 15:42:29 +0200378 { 0, NULL, NULL },
379}};
380
Willy Tarreau0108d902018-11-25 19:14:37 +0100381INITCALL1(STG_REGISTER, cfg_register_keywords, &dacfg_kws);
382
Willy Tarreauf63386a2015-06-01 15:39:50 +0200383/* Note: must not be declared <const> as its list will be overwritten */
David Carlier608c65a2015-09-25 14:16:30 +0100384static struct sample_fetch_kw_list fetch_kws = {ILH, {
David Carlier840b0242016-03-16 10:09:55 +0000385 { "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 +0100386 { NULL, NULL, 0, 0, 0 },
387}};
388
Willy Tarreau0108d902018-11-25 19:14:37 +0100389INITCALL1(STG_REGISTER, sample_register_fetches, &fetch_kws);
390
David Carlier608c65a2015-09-25 14:16:30 +0100391/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreauf63386a2015-06-01 15:39:50 +0200392static struct sample_conv_kw_list conv_kws = {ILH, {
David Carlier840b0242016-03-16 10:09:55 +0000393 { "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 +0200394 { NULL, NULL, 0, 0, 0 },
395}};
396
Willy Tarreau0108d902018-11-25 19:14:37 +0100397INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
398
Willy Tarreauf63386a2015-06-01 15:39:50 +0200399__attribute__((constructor))
400static void __da_init(void)
401{
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}
Willy Tarreau80713382018-11-26 10:19:54 +0100405
406REGISTER_BUILD_OPTS("Built with DeviceAtlas support.");