blob: c1c07e397a8324800b4931728e5ffa2c7a8ff5a3 [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 Tarreauf63386a2015-06-01 15:39:50 +02007#include <proto/arg.h>
Willy Tarreau79e57332018-10-02 16:01:16 +02008#include <proto/http_fetch.h>
David Carlier8167f302015-06-01 13:50:06 +02009#include <proto/log.h>
David Carlier608c65a2015-09-25 14:16:30 +010010#include <proto/proto_http.h>
Willy Tarreauf63386a2015-06-01 15:39:50 +020011#include <proto/sample.h>
Willy Tarreaubee9dde2016-12-21 21:25:06 +010012#include <dac.h>
13
14static struct {
15 void *atlasimgptr;
16 char *jsonpath;
17 char *cookiename;
18 size_t cookienamelen;
19 da_atlas_t atlas;
20 da_evidence_id_t useragentid;
21 da_severity_t loglevel;
22 char separator;
23 unsigned char daset:1;
24} global_deviceatlas = {
25 .loglevel = 0,
26 .jsonpath = 0,
27 .cookiename = 0,
28 .cookienamelen = 0,
29 .useragentid = 0,
30 .daset = 0,
31 .separator = '|',
32};
David Carlier8167f302015-06-01 13:50:06 +020033
34static int da_json_file(char **args, int section_type, struct proxy *curpx,
35 struct proxy *defpx, const char *file, int line,
36 char **err)
37{
38 if (*(args[1]) == 0) {
39 memprintf(err, "deviceatlas json file : expects a json path.\n");
40 return -1;
41 }
Willy Tarreaubee9dde2016-12-21 21:25:06 +010042 global_deviceatlas.jsonpath = strdup(args[1]);
David Carlier8167f302015-06-01 13:50:06 +020043 return 0;
44}
45
46static int da_log_level(char **args, int section_type, struct proxy *curpx,
47 struct proxy *defpx, const char *file, int line,
48 char **err)
49{
50 int loglevel;
51 if (*(args[1]) == 0) {
52 memprintf(err, "deviceatlas log level : expects an integer argument.\n");
53 return -1;
54 }
55
56 loglevel = atol(args[1]);
57 if (loglevel < 0 || loglevel > 3) {
58 memprintf(err, "deviceatlas log level : expects a log level between 0 and 3, %s given.\n", args[1]);
59 } else {
Willy Tarreaubee9dde2016-12-21 21:25:06 +010060 global_deviceatlas.loglevel = (da_severity_t)loglevel;
David Carlier8167f302015-06-01 13:50:06 +020061 }
62
63 return 0;
64}
65
66static int da_property_separator(char **args, int section_type, struct proxy *curpx,
67 struct proxy *defpx, const char *file, int line,
68 char **err)
69{
70 if (*(args[1]) == 0) {
71 memprintf(err, "deviceatlas property separator : expects a character argument.\n");
72 return -1;
73 }
Willy Tarreaubee9dde2016-12-21 21:25:06 +010074 global_deviceatlas.separator = *args[1];
David Carlier8167f302015-06-01 13:50:06 +020075 return 0;
76}
77
David Carlier608c65a2015-09-25 14:16:30 +010078static int da_properties_cookie(char **args, int section_type, struct proxy *curpx,
79 struct proxy *defpx, const char *file, int line,
80 char **err)
81{
82 if (*(args[1]) == 0) {
83 memprintf(err, "deviceatlas cookie name : expects a string argument.\n");
84 return -1;
85 } else {
Willy Tarreaubee9dde2016-12-21 21:25:06 +010086 global_deviceatlas.cookiename = strdup(args[1]);
David Carlier608c65a2015-09-25 14:16:30 +010087 }
Willy Tarreaubee9dde2016-12-21 21:25:06 +010088 global_deviceatlas.cookienamelen = strlen(global_deviceatlas.cookiename);
David Carlier608c65a2015-09-25 14:16:30 +010089 return 0;
90}
91
David Carlier8167f302015-06-01 13:50:06 +020092static size_t da_haproxy_read(void *ctx, size_t len, char *buf)
93{
94 return fread(buf, 1, len, ctx);
95}
96
97static da_status_t da_haproxy_seek(void *ctx, off_t off)
98{
99 return fseek(ctx, off, SEEK_SET) != -1 ? DA_OK : DA_SYS;
100}
101
102static void da_haproxy_log(da_severity_t severity, da_status_t status,
103 const char *fmt, va_list args)
104{
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100105 if (global_deviceatlas.loglevel && severity <= global_deviceatlas.loglevel) {
David Carlier8167f302015-06-01 13:50:06 +0200106 char logbuf[256];
107 vsnprintf(logbuf, sizeof(logbuf), fmt, args);
Christopher Faulet767a84b2017-11-24 16:50:31 +0100108 ha_warning("deviceatlas : %s.\n", logbuf);
David Carlier8167f302015-06-01 13:50:06 +0200109 }
110}
111
David Carlier608c65a2015-09-25 14:16:30 +0100112#define DA_COOKIENAME_DEFAULT "DAPROPS"
113
Willy Tarreau876054d2016-12-21 20:39:16 +0100114/*
115 * module init / deinit functions. Returns 0 if OK, or a combination of ERR_*.
116 */
117static int init_deviceatlas(void)
David Carlier8167f302015-06-01 13:50:06 +0200118{
Willy Tarreau876054d2016-12-21 20:39:16 +0100119 int err_code = 0;
120
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100121 if (global_deviceatlas.jsonpath != 0) {
David Carlier8167f302015-06-01 13:50:06 +0200122 FILE *jsonp;
123 da_property_decl_t extraprops[] = {{0, 0}};
124 size_t atlasimglen;
125 da_status_t status;
126
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100127 jsonp = fopen(global_deviceatlas.jsonpath, "r");
David Carlier8167f302015-06-01 13:50:06 +0200128 if (jsonp == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100129 ha_alert("deviceatlas : '%s' json file has invalid path or is not readable.\n",
130 global_deviceatlas.jsonpath);
Willy Tarreau876054d2016-12-21 20:39:16 +0100131 err_code |= ERR_ALERT | ERR_FATAL;
David Carlier8167f302015-06-01 13:50:06 +0200132 goto out;
133 }
134
135 da_init();
136 da_seterrorfunc(da_haproxy_log);
137 status = da_atlas_compile(jsonp, da_haproxy_read, da_haproxy_seek,
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100138 &global_deviceatlas.atlasimgptr, &atlasimglen);
David Carlier8167f302015-06-01 13:50:06 +0200139 fclose(jsonp);
140 if (status != DA_OK) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100141 ha_alert("deviceatlas : '%s' json file is invalid.\n",
142 global_deviceatlas.jsonpath);
Willy Tarreau876054d2016-12-21 20:39:16 +0100143 err_code |= ERR_ALERT | ERR_FATAL;
David Carlier8167f302015-06-01 13:50:06 +0200144 goto out;
145 }
146
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100147 status = da_atlas_open(&global_deviceatlas.atlas, extraprops,
148 global_deviceatlas.atlasimgptr, atlasimglen);
David Carlier8167f302015-06-01 13:50:06 +0200149
150 if (status != DA_OK) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100151 ha_alert("deviceatlas : data could not be compiled.\n");
Willy Tarreau876054d2016-12-21 20:39:16 +0100152 err_code |= ERR_ALERT | ERR_FATAL;
David Carlier8167f302015-06-01 13:50:06 +0200153 goto out;
154 }
155
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100156 if (global_deviceatlas.cookiename == 0) {
157 global_deviceatlas.cookiename = strdup(DA_COOKIENAME_DEFAULT);
158 global_deviceatlas.cookienamelen = strlen(global_deviceatlas.cookiename);
David Carlier608c65a2015-09-25 14:16:30 +0100159 }
160
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100161 global_deviceatlas.useragentid = da_atlas_header_evidence_id(&global_deviceatlas.atlas,
David Carlier8167f302015-06-01 13:50:06 +0200162 "user-agent");
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100163 global_deviceatlas.daset = 1;
David Carlier8167f302015-06-01 13:50:06 +0200164
165 fprintf(stdout, "Deviceatlas module loaded.\n");
166 }
167
168out:
Willy Tarreau876054d2016-12-21 20:39:16 +0100169 return err_code;
David Carlier8167f302015-06-01 13:50:06 +0200170}
171
Willy Tarreaub149eed2016-12-21 21:03:49 +0100172static void deinit_deviceatlas(void)
David Carlier8167f302015-06-01 13:50:06 +0200173{
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100174 if (global_deviceatlas.jsonpath != 0) {
175 free(global_deviceatlas.jsonpath);
David Carlier8167f302015-06-01 13:50:06 +0200176 }
177
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100178 if (global_deviceatlas.daset == 1) {
179 free(global_deviceatlas.cookiename);
180 da_atlas_close(&global_deviceatlas.atlas);
181 free(global_deviceatlas.atlasimgptr);
David Carlier8167f302015-06-01 13:50:06 +0200182 }
183
184 da_fini();
185}
186
David Carlier608c65a2015-09-25 14:16:30 +0100187static int da_haproxy(const struct arg *args, struct sample *smp, da_deviceinfo_t *devinfo)
David Carlier8167f302015-06-01 13:50:06 +0200188{
Willy Tarreau83061a82018-07-13 11:56:34 +0200189 struct buffer *tmp;
David Carlier8167f302015-06-01 13:50:06 +0200190 da_propid_t prop, *pprop;
David Carlier8167f302015-06-01 13:50:06 +0200191 da_status_t status;
David Carlier608c65a2015-09-25 14:16:30 +0100192 da_type_t proptype;
193 const char *propname;
David Carlier8167f302015-06-01 13:50:06 +0200194 int i;
195
David Carlier8167f302015-06-01 13:50:06 +0200196 tmp = get_trash_chunk();
197 chunk_reset(tmp);
198
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200199 propname = (const char *) args[0].data.str.area;
David Carlier8167f302015-06-01 13:50:06 +0200200 i = 0;
201
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200202 for (; propname != 0; i ++,
203 propname = (const char *) args[i].data.str.area) {
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100204 status = da_atlas_getpropid(&global_deviceatlas.atlas,
David Carlier8167f302015-06-01 13:50:06 +0200205 propname, &prop);
206 if (status != DA_OK) {
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100207 chunk_appendf(tmp, "%c", global_deviceatlas.separator);
David Carlier8167f302015-06-01 13:50:06 +0200208 continue;
209 }
210 pprop = &prop;
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100211 da_atlas_getproptype(&global_deviceatlas.atlas, *pprop, &proptype);
David Carlier8167f302015-06-01 13:50:06 +0200212
213 switch (proptype) {
214 case DA_TYPE_BOOLEAN: {
215 bool val;
David Carlier608c65a2015-09-25 14:16:30 +0100216 status = da_getpropboolean(devinfo, *pprop, &val);
David Carlier8167f302015-06-01 13:50:06 +0200217 if (status == DA_OK) {
218 chunk_appendf(tmp, "%d", val);
219 }
220 break;
221 }
222 case DA_TYPE_INTEGER:
223 case DA_TYPE_NUMBER: {
224 long val;
David Carlier608c65a2015-09-25 14:16:30 +0100225 status = da_getpropinteger(devinfo, *pprop, &val);
David Carlier8167f302015-06-01 13:50:06 +0200226 if (status == DA_OK) {
227 chunk_appendf(tmp, "%ld", val);
228 }
229 break;
230 }
231 case DA_TYPE_STRING: {
232 const char *val;
David Carlier608c65a2015-09-25 14:16:30 +0100233 status = da_getpropstring(devinfo, *pprop, &val);
David Carlier8167f302015-06-01 13:50:06 +0200234 if (status == DA_OK) {
235 chunk_appendf(tmp, "%s", val);
236 }
237 break;
David Carlier608c65a2015-09-25 14:16:30 +0100238 }
David Carlier8167f302015-06-01 13:50:06 +0200239 default:
240 break;
241 }
242
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100243 chunk_appendf(tmp, "%c", global_deviceatlas.separator);
David Carlier8167f302015-06-01 13:50:06 +0200244 }
245
David Carlier608c65a2015-09-25 14:16:30 +0100246 da_close(devinfo);
David Carlier8167f302015-06-01 13:50:06 +0200247
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200248 if (tmp->data) {
249 --tmp->data;
250 tmp->area[tmp->data] = 0;
David Carlier8167f302015-06-01 13:50:06 +0200251 }
252
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200253 smp->data.u.str.area = tmp->area;
254 smp->data.u.str.data = tmp->data;
David Carlier8167f302015-06-01 13:50:06 +0200255
256 return 1;
257}
258
David Carlier608c65a2015-09-25 14:16:30 +0100259static int da_haproxy_conv(const struct arg *args, struct sample *smp, void *private)
260{
261 da_deviceinfo_t devinfo;
262 da_status_t status;
263 const char *useragent;
264 char useragentbuf[1024] = { 0 };
265 int i;
266
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200267 if (global_deviceatlas.daset == 0 || smp->data.u.str.data == 0) {
David Carlier608c65a2015-09-25 14:16:30 +0100268 return 1;
269 }
270
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200271 i = smp->data.u.str.data > sizeof(useragentbuf) ? sizeof(useragentbuf) : smp->data.u.str.data;
272 memcpy(useragentbuf, smp->data.u.str.area, i - 1);
David Carlier608c65a2015-09-25 14:16:30 +0100273 useragentbuf[i - 1] = 0;
274
275 useragent = (const char *)useragentbuf;
276
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100277 status = da_search(&global_deviceatlas.atlas, &devinfo,
278 global_deviceatlas.useragentid, useragent, 0);
David Carlier608c65a2015-09-25 14:16:30 +0100279
280 return status != DA_OK ? 0 : da_haproxy(args, smp, &devinfo);
281}
282
283#define DA_MAX_HEADERS 24
284
285static int da_haproxy_fetch(const struct arg *args, struct sample *smp, const char *kw, void *private)
286{
287 struct hdr_idx *hidx;
288 struct hdr_ctx hctx;
289 const struct http_msg *hmsg;
290 da_evidence_t ev[DA_MAX_HEADERS];
291 da_deviceinfo_t devinfo;
292 da_status_t status;
293 char vbuf[DA_MAX_HEADERS][1024] = {{ 0 }};
294 int i, nbh = 0;
295
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100296 if (global_deviceatlas.daset == 0) {
David Carlier608c65a2015-09-25 14:16:30 +0100297 return 1;
298 }
299
300 CHECK_HTTP_MESSAGE_FIRST();
301 smp->data.type = SMP_T_STR;
302
303 /**
304 * Here we go through the whole list of headers from start
305 * they will be filtered via the DeviceAtlas API itself
306 */
307 hctx.idx = 0;
308 hidx = &smp->strm->txn->hdr_idx;
309 hmsg = &smp->strm->txn->req;
310
311 while (http_find_next_header(hmsg->chn->buf->p, hidx, &hctx) == 1 &&
312 nbh < DA_MAX_HEADERS) {
313 char *pval;
314 size_t vlen;
315 da_evidence_id_t evid = -1;
316 char hbuf[24] = { 0 };
317
318 /* The HTTP headers used by the DeviceAtlas API are not longer */
David Carlier91a88b02017-11-17 08:47:25 +0000319 if (hctx.del >= sizeof(hbuf) || hctx.del <= 0 || hctx.vlen <= 0) {
David Carlier608c65a2015-09-25 14:16:30 +0100320 continue;
321 }
322
323 vlen = hctx.vlen;
324 memcpy(hbuf, hctx.line, hctx.del);
325 hbuf[hctx.del] = 0;
326 pval = (hctx.line + hctx.val);
327
328 if (strcmp(hbuf, "Accept-Language") == 0) {
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100329 evid = da_atlas_accept_language_evidence_id(&global_deviceatlas.
David Carlier608c65a2015-09-25 14:16:30 +0100330 atlas);
331 } else if (strcmp(hbuf, "Cookie") == 0) {
332 char *p, *eval;
333 int pl;
334
335 eval = pval + hctx.vlen;
336 /**
337 * The cookie value, if it exists, is located between the current header's
338 * value position and the next one
339 */
Willy Tarreau491cec22018-10-02 18:37:27 +0200340 if (http_extract_cookie_value(pval, eval, global_deviceatlas.cookiename,
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100341 global_deviceatlas.cookienamelen, 1, &p, &pl) == NULL) {
David Carlier608c65a2015-09-25 14:16:30 +0100342 continue;
343 }
344
345 vlen = (size_t)pl;
346 pval = p;
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100347 evid = da_atlas_clientprop_evidence_id(&global_deviceatlas.atlas);
David Carlier608c65a2015-09-25 14:16:30 +0100348 } else {
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100349 evid = da_atlas_header_evidence_id(&global_deviceatlas.atlas,
David Carlier608c65a2015-09-25 14:16:30 +0100350 hbuf);
351 }
352
353 if (evid == -1) {
354 continue;
355 }
356
357 i = vlen > sizeof(vbuf[nbh]) ? sizeof(vbuf[nbh]) : vlen;
358 memcpy(vbuf[nbh], pval, i - 1);
359 vbuf[nbh][i - 1] = 0;
360 ev[nbh].key = evid;
361 ev[nbh].value = vbuf[nbh];
362 ev[nbh].value[vlen] = 0;
363 ++ nbh;
364 }
365
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100366 status = da_searchv(&global_deviceatlas.atlas, &devinfo,
David Carlier608c65a2015-09-25 14:16:30 +0100367 ev, nbh);
368
369 return status != DA_OK ? 0 : da_haproxy(args, smp, &devinfo);
370}
371
Willy Tarreau0d74f772015-06-01 15:42:29 +0200372static struct cfg_kw_list dacfg_kws = {{ }, {
373 { CFG_GLOBAL, "deviceatlas-json-file", da_json_file },
374 { CFG_GLOBAL, "deviceatlas-log-level", da_log_level },
375 { CFG_GLOBAL, "deviceatlas-property-separator", da_property_separator },
David Carlier608c65a2015-09-25 14:16:30 +0100376 { CFG_GLOBAL, "deviceatlas-properties-cookie", da_properties_cookie },
Willy Tarreau0d74f772015-06-01 15:42:29 +0200377 { 0, NULL, NULL },
378}};
379
Willy Tarreau0108d902018-11-25 19:14:37 +0100380INITCALL1(STG_REGISTER, cfg_register_keywords, &dacfg_kws);
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
Willy Tarreau0108d902018-11-25 19:14:37 +0100388INITCALL1(STG_REGISTER, sample_register_fetches, &fetch_kws);
389
David Carlier608c65a2015-09-25 14:16:30 +0100390/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreauf63386a2015-06-01 15:39:50 +0200391static struct sample_conv_kw_list conv_kws = {ILH, {
David Carlier840b0242016-03-16 10:09:55 +0000392 { "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 +0200393 { NULL, NULL, 0, 0, 0 },
394}};
395
Willy Tarreau0108d902018-11-25 19:14:37 +0100396INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
397
Willy Tarreauf63386a2015-06-01 15:39:50 +0200398__attribute__((constructor))
399static void __da_init(void)
400{
401 /* register sample fetch and format conversion keywords */
Willy Tarreau3dd483e2016-12-21 18:50:22 +0100402 hap_register_build_opts("Built with DeviceAtlas support.", 0);
Willy Tarreau876054d2016-12-21 20:39:16 +0100403 hap_register_post_check(init_deviceatlas);
Willy Tarreaub149eed2016-12-21 21:03:49 +0100404 hap_register_post_deinit(deinit_deviceatlas);
Willy Tarreauf63386a2015-06-01 15:39:50 +0200405}