blob: c6538481204671c2162cd94f343878f5a16fc471 [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>
Willy Tarreau79e57332018-10-02 16:01:16 +02006#include <proto/http_fetch.h>
David Carlier8167f302015-06-01 13:50:06 +02007#include <proto/log.h>
David Carlier608c65a2015-09-25 14:16:30 +01008#include <proto/proto_http.h>
Willy Tarreauf63386a2015-06-01 15:39:50 +02009#include <proto/sample.h>
Willy Tarreaubee9dde2016-12-21 21:25:06 +010010#include <dac.h>
11
12static struct {
13 void *atlasimgptr;
14 char *jsonpath;
15 char *cookiename;
16 size_t cookienamelen;
17 da_atlas_t atlas;
18 da_evidence_id_t useragentid;
19 da_severity_t loglevel;
20 char separator;
21 unsigned char daset:1;
22} global_deviceatlas = {
23 .loglevel = 0,
24 .jsonpath = 0,
25 .cookiename = 0,
26 .cookienamelen = 0,
27 .useragentid = 0,
28 .daset = 0,
29 .separator = '|',
30};
David Carlier8167f302015-06-01 13:50:06 +020031
32static int da_json_file(char **args, int section_type, struct proxy *curpx,
33 struct proxy *defpx, const char *file, int line,
34 char **err)
35{
36 if (*(args[1]) == 0) {
37 memprintf(err, "deviceatlas json file : expects a json path.\n");
38 return -1;
39 }
Willy Tarreaubee9dde2016-12-21 21:25:06 +010040 global_deviceatlas.jsonpath = strdup(args[1]);
David Carlier8167f302015-06-01 13:50:06 +020041 return 0;
42}
43
44static int da_log_level(char **args, int section_type, struct proxy *curpx,
45 struct proxy *defpx, const char *file, int line,
46 char **err)
47{
48 int loglevel;
49 if (*(args[1]) == 0) {
50 memprintf(err, "deviceatlas log level : expects an integer argument.\n");
51 return -1;
52 }
53
54 loglevel = atol(args[1]);
55 if (loglevel < 0 || loglevel > 3) {
56 memprintf(err, "deviceatlas log level : expects a log level between 0 and 3, %s given.\n", args[1]);
57 } else {
Willy Tarreaubee9dde2016-12-21 21:25:06 +010058 global_deviceatlas.loglevel = (da_severity_t)loglevel;
David Carlier8167f302015-06-01 13:50:06 +020059 }
60
61 return 0;
62}
63
64static int da_property_separator(char **args, int section_type, struct proxy *curpx,
65 struct proxy *defpx, const char *file, int line,
66 char **err)
67{
68 if (*(args[1]) == 0) {
69 memprintf(err, "deviceatlas property separator : expects a character argument.\n");
70 return -1;
71 }
Willy Tarreaubee9dde2016-12-21 21:25:06 +010072 global_deviceatlas.separator = *args[1];
David Carlier8167f302015-06-01 13:50:06 +020073 return 0;
74}
75
David Carlier608c65a2015-09-25 14:16:30 +010076static int da_properties_cookie(char **args, int section_type, struct proxy *curpx,
77 struct proxy *defpx, const char *file, int line,
78 char **err)
79{
80 if (*(args[1]) == 0) {
81 memprintf(err, "deviceatlas cookie name : expects a string argument.\n");
82 return -1;
83 } else {
Willy Tarreaubee9dde2016-12-21 21:25:06 +010084 global_deviceatlas.cookiename = strdup(args[1]);
David Carlier608c65a2015-09-25 14:16:30 +010085 }
Willy Tarreaubee9dde2016-12-21 21:25:06 +010086 global_deviceatlas.cookienamelen = strlen(global_deviceatlas.cookiename);
David Carlier608c65a2015-09-25 14:16:30 +010087 return 0;
88}
89
David Carlier8167f302015-06-01 13:50:06 +020090static size_t da_haproxy_read(void *ctx, size_t len, char *buf)
91{
92 return fread(buf, 1, len, ctx);
93}
94
95static da_status_t da_haproxy_seek(void *ctx, off_t off)
96{
97 return fseek(ctx, off, SEEK_SET) != -1 ? DA_OK : DA_SYS;
98}
99
100static void da_haproxy_log(da_severity_t severity, da_status_t status,
101 const char *fmt, va_list args)
102{
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100103 if (global_deviceatlas.loglevel && severity <= global_deviceatlas.loglevel) {
David Carlier8167f302015-06-01 13:50:06 +0200104 char logbuf[256];
105 vsnprintf(logbuf, sizeof(logbuf), fmt, args);
Christopher Faulet767a84b2017-11-24 16:50:31 +0100106 ha_warning("deviceatlas : %s.\n", logbuf);
David Carlier8167f302015-06-01 13:50:06 +0200107 }
108}
109
David Carlier608c65a2015-09-25 14:16:30 +0100110#define DA_COOKIENAME_DEFAULT "DAPROPS"
111
Willy Tarreau876054d2016-12-21 20:39:16 +0100112/*
113 * module init / deinit functions. Returns 0 if OK, or a combination of ERR_*.
114 */
115static int init_deviceatlas(void)
David Carlier8167f302015-06-01 13:50:06 +0200116{
Willy Tarreau876054d2016-12-21 20:39:16 +0100117 int err_code = 0;
118
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100119 if (global_deviceatlas.jsonpath != 0) {
David Carlier8167f302015-06-01 13:50:06 +0200120 FILE *jsonp;
121 da_property_decl_t extraprops[] = {{0, 0}};
122 size_t atlasimglen;
123 da_status_t status;
124
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100125 jsonp = fopen(global_deviceatlas.jsonpath, "r");
David Carlier8167f302015-06-01 13:50:06 +0200126 if (jsonp == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100127 ha_alert("deviceatlas : '%s' json file has invalid path or is not readable.\n",
128 global_deviceatlas.jsonpath);
Willy Tarreau876054d2016-12-21 20:39:16 +0100129 err_code |= ERR_ALERT | ERR_FATAL;
David Carlier8167f302015-06-01 13:50:06 +0200130 goto out;
131 }
132
133 da_init();
134 da_seterrorfunc(da_haproxy_log);
135 status = da_atlas_compile(jsonp, da_haproxy_read, da_haproxy_seek,
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100136 &global_deviceatlas.atlasimgptr, &atlasimglen);
David Carlier8167f302015-06-01 13:50:06 +0200137 fclose(jsonp);
138 if (status != DA_OK) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100139 ha_alert("deviceatlas : '%s' json file is invalid.\n",
140 global_deviceatlas.jsonpath);
Willy Tarreau876054d2016-12-21 20:39:16 +0100141 err_code |= ERR_ALERT | ERR_FATAL;
David Carlier8167f302015-06-01 13:50:06 +0200142 goto out;
143 }
144
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100145 status = da_atlas_open(&global_deviceatlas.atlas, extraprops,
146 global_deviceatlas.atlasimgptr, atlasimglen);
David Carlier8167f302015-06-01 13:50:06 +0200147
148 if (status != DA_OK) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100149 ha_alert("deviceatlas : data could not be compiled.\n");
Willy Tarreau876054d2016-12-21 20:39:16 +0100150 err_code |= ERR_ALERT | ERR_FATAL;
David Carlier8167f302015-06-01 13:50:06 +0200151 goto out;
152 }
153
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100154 if (global_deviceatlas.cookiename == 0) {
155 global_deviceatlas.cookiename = strdup(DA_COOKIENAME_DEFAULT);
156 global_deviceatlas.cookienamelen = strlen(global_deviceatlas.cookiename);
David Carlier608c65a2015-09-25 14:16:30 +0100157 }
158
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100159 global_deviceatlas.useragentid = da_atlas_header_evidence_id(&global_deviceatlas.atlas,
David Carlier8167f302015-06-01 13:50:06 +0200160 "user-agent");
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100161 global_deviceatlas.daset = 1;
David Carlier8167f302015-06-01 13:50:06 +0200162
163 fprintf(stdout, "Deviceatlas module loaded.\n");
164 }
165
166out:
Willy Tarreau876054d2016-12-21 20:39:16 +0100167 return err_code;
David Carlier8167f302015-06-01 13:50:06 +0200168}
169
Willy Tarreaub149eed2016-12-21 21:03:49 +0100170static void deinit_deviceatlas(void)
David Carlier8167f302015-06-01 13:50:06 +0200171{
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100172 if (global_deviceatlas.jsonpath != 0) {
173 free(global_deviceatlas.jsonpath);
David Carlier8167f302015-06-01 13:50:06 +0200174 }
175
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100176 if (global_deviceatlas.daset == 1) {
177 free(global_deviceatlas.cookiename);
178 da_atlas_close(&global_deviceatlas.atlas);
179 free(global_deviceatlas.atlasimgptr);
David Carlier8167f302015-06-01 13:50:06 +0200180 }
181
182 da_fini();
183}
184
David Carlier608c65a2015-09-25 14:16:30 +0100185static int da_haproxy(const struct arg *args, struct sample *smp, da_deviceinfo_t *devinfo)
David Carlier8167f302015-06-01 13:50:06 +0200186{
Willy Tarreau83061a82018-07-13 11:56:34 +0200187 struct buffer *tmp;
David Carlier8167f302015-06-01 13:50:06 +0200188 da_propid_t prop, *pprop;
David Carlier8167f302015-06-01 13:50:06 +0200189 da_status_t status;
David Carlier608c65a2015-09-25 14:16:30 +0100190 da_type_t proptype;
191 const char *propname;
David Carlier8167f302015-06-01 13:50:06 +0200192 int i;
193
David Carlier8167f302015-06-01 13:50:06 +0200194 tmp = get_trash_chunk();
195 chunk_reset(tmp);
196
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200197 propname = (const char *) args[0].data.str.area;
David Carlier8167f302015-06-01 13:50:06 +0200198 i = 0;
199
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200200 for (; propname != 0; i ++,
201 propname = (const char *) args[i].data.str.area) {
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100202 status = da_atlas_getpropid(&global_deviceatlas.atlas,
David Carlier8167f302015-06-01 13:50:06 +0200203 propname, &prop);
204 if (status != DA_OK) {
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100205 chunk_appendf(tmp, "%c", global_deviceatlas.separator);
David Carlier8167f302015-06-01 13:50:06 +0200206 continue;
207 }
208 pprop = &prop;
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100209 da_atlas_getproptype(&global_deviceatlas.atlas, *pprop, &proptype);
David Carlier8167f302015-06-01 13:50:06 +0200210
211 switch (proptype) {
212 case DA_TYPE_BOOLEAN: {
213 bool val;
David Carlier608c65a2015-09-25 14:16:30 +0100214 status = da_getpropboolean(devinfo, *pprop, &val);
David Carlier8167f302015-06-01 13:50:06 +0200215 if (status == DA_OK) {
216 chunk_appendf(tmp, "%d", val);
217 }
218 break;
219 }
220 case DA_TYPE_INTEGER:
221 case DA_TYPE_NUMBER: {
222 long val;
David Carlier608c65a2015-09-25 14:16:30 +0100223 status = da_getpropinteger(devinfo, *pprop, &val);
David Carlier8167f302015-06-01 13:50:06 +0200224 if (status == DA_OK) {
225 chunk_appendf(tmp, "%ld", val);
226 }
227 break;
228 }
229 case DA_TYPE_STRING: {
230 const char *val;
David Carlier608c65a2015-09-25 14:16:30 +0100231 status = da_getpropstring(devinfo, *pprop, &val);
David Carlier8167f302015-06-01 13:50:06 +0200232 if (status == DA_OK) {
233 chunk_appendf(tmp, "%s", val);
234 }
235 break;
David Carlier608c65a2015-09-25 14:16:30 +0100236 }
David Carlier8167f302015-06-01 13:50:06 +0200237 default:
238 break;
239 }
240
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100241 chunk_appendf(tmp, "%c", global_deviceatlas.separator);
David Carlier8167f302015-06-01 13:50:06 +0200242 }
243
David Carlier608c65a2015-09-25 14:16:30 +0100244 da_close(devinfo);
David Carlier8167f302015-06-01 13:50:06 +0200245
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200246 if (tmp->data) {
247 --tmp->data;
248 tmp->area[tmp->data] = 0;
David Carlier8167f302015-06-01 13:50:06 +0200249 }
250
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200251 smp->data.u.str.area = tmp->area;
252 smp->data.u.str.data = tmp->data;
David Carlier8167f302015-06-01 13:50:06 +0200253
254 return 1;
255}
256
David Carlier608c65a2015-09-25 14:16:30 +0100257static int da_haproxy_conv(const struct arg *args, struct sample *smp, void *private)
258{
259 da_deviceinfo_t devinfo;
260 da_status_t status;
261 const char *useragent;
262 char useragentbuf[1024] = { 0 };
263 int i;
264
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200265 if (global_deviceatlas.daset == 0 || smp->data.u.str.data == 0) {
David Carlier608c65a2015-09-25 14:16:30 +0100266 return 1;
267 }
268
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200269 i = smp->data.u.str.data > sizeof(useragentbuf) ? sizeof(useragentbuf) : smp->data.u.str.data;
270 memcpy(useragentbuf, smp->data.u.str.area, i - 1);
David Carlier608c65a2015-09-25 14:16:30 +0100271 useragentbuf[i - 1] = 0;
272
273 useragent = (const char *)useragentbuf;
274
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100275 status = da_search(&global_deviceatlas.atlas, &devinfo,
276 global_deviceatlas.useragentid, useragent, 0);
David Carlier608c65a2015-09-25 14:16:30 +0100277
278 return status != DA_OK ? 0 : da_haproxy(args, smp, &devinfo);
279}
280
281#define DA_MAX_HEADERS 24
282
283static int da_haproxy_fetch(const struct arg *args, struct sample *smp, const char *kw, void *private)
284{
285 struct hdr_idx *hidx;
286 struct hdr_ctx hctx;
287 const struct http_msg *hmsg;
288 da_evidence_t ev[DA_MAX_HEADERS];
289 da_deviceinfo_t devinfo;
290 da_status_t status;
291 char vbuf[DA_MAX_HEADERS][1024] = {{ 0 }};
292 int i, nbh = 0;
293
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100294 if (global_deviceatlas.daset == 0) {
David Carlier608c65a2015-09-25 14:16:30 +0100295 return 1;
296 }
297
298 CHECK_HTTP_MESSAGE_FIRST();
299 smp->data.type = SMP_T_STR;
300
301 /**
302 * Here we go through the whole list of headers from start
303 * they will be filtered via the DeviceAtlas API itself
304 */
305 hctx.idx = 0;
306 hidx = &smp->strm->txn->hdr_idx;
307 hmsg = &smp->strm->txn->req;
308
309 while (http_find_next_header(hmsg->chn->buf->p, hidx, &hctx) == 1 &&
310 nbh < DA_MAX_HEADERS) {
311 char *pval;
312 size_t vlen;
313 da_evidence_id_t evid = -1;
314 char hbuf[24] = { 0 };
315
316 /* The HTTP headers used by the DeviceAtlas API are not longer */
David Carlier91a88b02017-11-17 08:47:25 +0000317 if (hctx.del >= sizeof(hbuf) || hctx.del <= 0 || hctx.vlen <= 0) {
David Carlier608c65a2015-09-25 14:16:30 +0100318 continue;
319 }
320
321 vlen = hctx.vlen;
322 memcpy(hbuf, hctx.line, hctx.del);
323 hbuf[hctx.del] = 0;
324 pval = (hctx.line + hctx.val);
325
326 if (strcmp(hbuf, "Accept-Language") == 0) {
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100327 evid = da_atlas_accept_language_evidence_id(&global_deviceatlas.
David Carlier608c65a2015-09-25 14:16:30 +0100328 atlas);
329 } else if (strcmp(hbuf, "Cookie") == 0) {
330 char *p, *eval;
331 int pl;
332
333 eval = pval + hctx.vlen;
334 /**
335 * The cookie value, if it exists, is located between the current header's
336 * value position and the next one
337 */
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100338 if (extract_cookie_value(pval, eval, global_deviceatlas.cookiename,
339 global_deviceatlas.cookienamelen, 1, &p, &pl) == NULL) {
David Carlier608c65a2015-09-25 14:16:30 +0100340 continue;
341 }
342
343 vlen = (size_t)pl;
344 pval = p;
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100345 evid = da_atlas_clientprop_evidence_id(&global_deviceatlas.atlas);
David Carlier608c65a2015-09-25 14:16:30 +0100346 } else {
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100347 evid = da_atlas_header_evidence_id(&global_deviceatlas.atlas,
David Carlier608c65a2015-09-25 14:16:30 +0100348 hbuf);
349 }
350
351 if (evid == -1) {
352 continue;
353 }
354
355 i = vlen > sizeof(vbuf[nbh]) ? sizeof(vbuf[nbh]) : vlen;
356 memcpy(vbuf[nbh], pval, i - 1);
357 vbuf[nbh][i - 1] = 0;
358 ev[nbh].key = evid;
359 ev[nbh].value = vbuf[nbh];
360 ev[nbh].value[vlen] = 0;
361 ++ nbh;
362 }
363
Willy Tarreaubee9dde2016-12-21 21:25:06 +0100364 status = da_searchv(&global_deviceatlas.atlas, &devinfo,
David Carlier608c65a2015-09-25 14:16:30 +0100365 ev, nbh);
366
367 return status != DA_OK ? 0 : da_haproxy(args, smp, &devinfo);
368}
369
Willy Tarreau0d74f772015-06-01 15:42:29 +0200370static struct cfg_kw_list dacfg_kws = {{ }, {
371 { CFG_GLOBAL, "deviceatlas-json-file", da_json_file },
372 { CFG_GLOBAL, "deviceatlas-log-level", da_log_level },
373 { CFG_GLOBAL, "deviceatlas-property-separator", da_property_separator },
David Carlier608c65a2015-09-25 14:16:30 +0100374 { CFG_GLOBAL, "deviceatlas-properties-cookie", da_properties_cookie },
Willy Tarreau0d74f772015-06-01 15:42:29 +0200375 { 0, NULL, NULL },
376}};
377
Willy Tarreauf63386a2015-06-01 15:39:50 +0200378/* Note: must not be declared <const> as its list will be overwritten */
David Carlier608c65a2015-09-25 14:16:30 +0100379static struct sample_fetch_kw_list fetch_kws = {ILH, {
David Carlier840b0242016-03-16 10:09:55 +0000380 { "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 +0100381 { NULL, NULL, 0, 0, 0 },
382}};
383
384/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreauf63386a2015-06-01 15:39:50 +0200385static struct sample_conv_kw_list conv_kws = {ILH, {
David Carlier840b0242016-03-16 10:09:55 +0000386 { "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 +0200387 { NULL, NULL, 0, 0, 0 },
388}};
389
390__attribute__((constructor))
391static void __da_init(void)
392{
393 /* register sample fetch and format conversion keywords */
David Carlier608c65a2015-09-25 14:16:30 +0100394 sample_register_fetches(&fetch_kws);
Willy Tarreauf63386a2015-06-01 15:39:50 +0200395 sample_register_convs(&conv_kws);
Willy Tarreau0d74f772015-06-01 15:42:29 +0200396 cfg_register_keywords(&dacfg_kws);
Willy Tarreau3dd483e2016-12-21 18:50:22 +0100397 hap_register_build_opts("Built with DeviceAtlas support.", 0);
Willy Tarreau876054d2016-12-21 20:39:16 +0100398 hap_register_post_check(init_deviceatlas);
Willy Tarreaub149eed2016-12-21 21:03:49 +0100399 hap_register_post_deinit(deinit_deviceatlas);
Willy Tarreauf63386a2015-06-01 15:39:50 +0200400}