blob: 843da9fa7c8a3b579e0c37c8e14a708f90857b83 [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>
David Carlier8167f302015-06-01 13:50:06 +02009#include <import/da.h>
10
11static int da_json_file(char **args, int section_type, struct proxy *curpx,
12 struct proxy *defpx, const char *file, int line,
13 char **err)
14{
15 if (*(args[1]) == 0) {
16 memprintf(err, "deviceatlas json file : expects a json path.\n");
17 return -1;
18 }
19 global.deviceatlas.jsonpath = strdup(args[1]);
20 return 0;
21}
22
23static int da_log_level(char **args, int section_type, struct proxy *curpx,
24 struct proxy *defpx, const char *file, int line,
25 char **err)
26{
27 int loglevel;
28 if (*(args[1]) == 0) {
29 memprintf(err, "deviceatlas log level : expects an integer argument.\n");
30 return -1;
31 }
32
33 loglevel = atol(args[1]);
34 if (loglevel < 0 || loglevel > 3) {
35 memprintf(err, "deviceatlas log level : expects a log level between 0 and 3, %s given.\n", args[1]);
36 } else {
37 global.deviceatlas.loglevel = (da_severity_t)loglevel;
38 }
39
40 return 0;
41}
42
43static int da_property_separator(char **args, int section_type, struct proxy *curpx,
44 struct proxy *defpx, const char *file, int line,
45 char **err)
46{
47 if (*(args[1]) == 0) {
48 memprintf(err, "deviceatlas property separator : expects a character argument.\n");
49 return -1;
50 }
51 global.deviceatlas.separator = *args[1];
52 return 0;
53}
54
David Carlier608c65a2015-09-25 14:16:30 +010055static int da_properties_cookie(char **args, int section_type, struct proxy *curpx,
56 struct proxy *defpx, const char *file, int line,
57 char **err)
58{
59 if (*(args[1]) == 0) {
60 memprintf(err, "deviceatlas cookie name : expects a string argument.\n");
61 return -1;
62 } else {
63 global.deviceatlas.cookiename = strdup(args[1]);
64 }
65 global.deviceatlas.cookienamelen = strlen(global.deviceatlas.cookiename);
66 return 0;
67}
68
David Carlier8167f302015-06-01 13:50:06 +020069static size_t da_haproxy_read(void *ctx, size_t len, char *buf)
70{
71 return fread(buf, 1, len, ctx);
72}
73
74static da_status_t da_haproxy_seek(void *ctx, off_t off)
75{
76 return fseek(ctx, off, SEEK_SET) != -1 ? DA_OK : DA_SYS;
77}
78
79static void da_haproxy_log(da_severity_t severity, da_status_t status,
80 const char *fmt, va_list args)
81{
Willy Tarreau6bd42e72015-06-01 15:25:46 +020082 if (global.deviceatlas.loglevel && severity <= global.deviceatlas.loglevel) {
David Carlier8167f302015-06-01 13:50:06 +020083 char logbuf[256];
84 vsnprintf(logbuf, sizeof(logbuf), fmt, args);
85 Warning("deviceatlas : %s.\n", logbuf);
86 }
87}
88
David Carlier608c65a2015-09-25 14:16:30 +010089#define DA_COOKIENAME_DEFAULT "DAPROPS"
90
Willy Tarreau876054d2016-12-21 20:39:16 +010091/*
92 * module init / deinit functions. Returns 0 if OK, or a combination of ERR_*.
93 */
94static int init_deviceatlas(void)
David Carlier8167f302015-06-01 13:50:06 +020095{
Willy Tarreau876054d2016-12-21 20:39:16 +010096 int err_code = 0;
97
David Carlier8167f302015-06-01 13:50:06 +020098 if (global.deviceatlas.jsonpath != 0) {
99 FILE *jsonp;
100 da_property_decl_t extraprops[] = {{0, 0}};
101 size_t atlasimglen;
102 da_status_t status;
103
104 jsonp = fopen(global.deviceatlas.jsonpath, "r");
105 if (jsonp == 0) {
106 Alert("deviceatlas : '%s' json file has invalid path or is not readable.\n",
107 global.deviceatlas.jsonpath);
Willy Tarreau876054d2016-12-21 20:39:16 +0100108 err_code |= ERR_ALERT | ERR_FATAL;
David Carlier8167f302015-06-01 13:50:06 +0200109 goto out;
110 }
111
112 da_init();
113 da_seterrorfunc(da_haproxy_log);
114 status = da_atlas_compile(jsonp, da_haproxy_read, da_haproxy_seek,
115 &global.deviceatlas.atlasimgptr, &atlasimglen);
116 fclose(jsonp);
117 if (status != DA_OK) {
118 Alert("deviceatlas : '%s' json file is invalid.\n",
119 global.deviceatlas.jsonpath);
Willy Tarreau876054d2016-12-21 20:39:16 +0100120 err_code |= ERR_ALERT | ERR_FATAL;
David Carlier8167f302015-06-01 13:50:06 +0200121 goto out;
122 }
123
124 status = da_atlas_open(&global.deviceatlas.atlas, extraprops,
125 global.deviceatlas.atlasimgptr, atlasimglen);
126
127 if (status != DA_OK) {
128 Alert("deviceatlas : data could not be compiled.\n");
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
David Carlier608c65a2015-09-25 14:16:30 +0100133 if (global.deviceatlas.cookiename == 0) {
134 global.deviceatlas.cookiename = strdup(DA_COOKIENAME_DEFAULT);
135 global.deviceatlas.cookienamelen = strlen(global.deviceatlas.cookiename);
136 }
137
David Carlier8167f302015-06-01 13:50:06 +0200138 global.deviceatlas.useragentid = da_atlas_header_evidence_id(&global.deviceatlas.atlas,
139 "user-agent");
David Carlier608c65a2015-09-25 14:16:30 +0100140 global.deviceatlas.daset = 1;
David Carlier8167f302015-06-01 13:50:06 +0200141
142 fprintf(stdout, "Deviceatlas module loaded.\n");
143 }
144
145out:
Willy Tarreau876054d2016-12-21 20:39:16 +0100146 return err_code;
David Carlier8167f302015-06-01 13:50:06 +0200147}
148
149void deinit_deviceatlas(void)
150{
151 if (global.deviceatlas.jsonpath != 0) {
152 free(global.deviceatlas.jsonpath);
153 }
154
David Carlier608c65a2015-09-25 14:16:30 +0100155 if (global.deviceatlas.daset == 1) {
156 free(global.deviceatlas.cookiename);
David Carlier8167f302015-06-01 13:50:06 +0200157 da_atlas_close(&global.deviceatlas.atlas);
158 free(global.deviceatlas.atlasimgptr);
159 }
160
161 da_fini();
162}
163
David Carlier608c65a2015-09-25 14:16:30 +0100164static int da_haproxy(const struct arg *args, struct sample *smp, da_deviceinfo_t *devinfo)
David Carlier8167f302015-06-01 13:50:06 +0200165{
166 struct chunk *tmp;
David Carlier8167f302015-06-01 13:50:06 +0200167 da_propid_t prop, *pprop;
David Carlier8167f302015-06-01 13:50:06 +0200168 da_status_t status;
David Carlier608c65a2015-09-25 14:16:30 +0100169 da_type_t proptype;
170 const char *propname;
David Carlier8167f302015-06-01 13:50:06 +0200171 int i;
172
David Carlier8167f302015-06-01 13:50:06 +0200173 tmp = get_trash_chunk();
174 chunk_reset(tmp);
175
David Carlier8167f302015-06-01 13:50:06 +0200176 propname = (const char *)args[0].data.str.str;
177 i = 0;
178
David Carlier8167f302015-06-01 13:50:06 +0200179 for (; propname != 0; i ++, propname = (const char *)args[i].data.str.str) {
180 status = da_atlas_getpropid(&global.deviceatlas.atlas,
181 propname, &prop);
182 if (status != DA_OK) {
183 chunk_appendf(tmp, "%c", global.deviceatlas.separator);
184 continue;
185 }
186 pprop = &prop;
187 da_atlas_getproptype(&global.deviceatlas.atlas, *pprop, &proptype);
188
189 switch (proptype) {
190 case DA_TYPE_BOOLEAN: {
191 bool val;
David Carlier608c65a2015-09-25 14:16:30 +0100192 status = da_getpropboolean(devinfo, *pprop, &val);
David Carlier8167f302015-06-01 13:50:06 +0200193 if (status == DA_OK) {
194 chunk_appendf(tmp, "%d", val);
195 }
196 break;
197 }
198 case DA_TYPE_INTEGER:
199 case DA_TYPE_NUMBER: {
200 long val;
David Carlier608c65a2015-09-25 14:16:30 +0100201 status = da_getpropinteger(devinfo, *pprop, &val);
David Carlier8167f302015-06-01 13:50:06 +0200202 if (status == DA_OK) {
203 chunk_appendf(tmp, "%ld", val);
204 }
205 break;
206 }
207 case DA_TYPE_STRING: {
208 const char *val;
David Carlier608c65a2015-09-25 14:16:30 +0100209 status = da_getpropstring(devinfo, *pprop, &val);
David Carlier8167f302015-06-01 13:50:06 +0200210 if (status == DA_OK) {
211 chunk_appendf(tmp, "%s", val);
212 }
213 break;
David Carlier608c65a2015-09-25 14:16:30 +0100214 }
David Carlier8167f302015-06-01 13:50:06 +0200215 default:
216 break;
217 }
218
219 chunk_appendf(tmp, "%c", global.deviceatlas.separator);
220 }
221
David Carlier608c65a2015-09-25 14:16:30 +0100222 da_close(devinfo);
David Carlier8167f302015-06-01 13:50:06 +0200223
224 if (tmp->len) {
225 --tmp->len;
226 tmp->str[tmp->len] = 0;
227 }
228
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200229 smp->data.u.str.str = tmp->str;
David Carlier608c65a2015-09-25 14:16:30 +0100230 smp->data.u.str.len = tmp->len;
David Carlier8167f302015-06-01 13:50:06 +0200231
232 return 1;
233}
234
David Carlier608c65a2015-09-25 14:16:30 +0100235static int da_haproxy_conv(const struct arg *args, struct sample *smp, void *private)
236{
237 da_deviceinfo_t devinfo;
238 da_status_t status;
239 const char *useragent;
240 char useragentbuf[1024] = { 0 };
241 int i;
242
David Carlier3b711382015-12-02 12:05:42 +0000243 if (global.deviceatlas.daset == 0 || smp->data.u.str.len == 0) {
David Carlier608c65a2015-09-25 14:16:30 +0100244 return 1;
245 }
246
247 i = smp->data.u.str.len > sizeof(useragentbuf) ? sizeof(useragentbuf) : smp->data.u.str.len;
248 memcpy(useragentbuf, smp->data.u.str.str, i - 1);
249 useragentbuf[i - 1] = 0;
250
251 useragent = (const char *)useragentbuf;
252
253 status = da_search(&global.deviceatlas.atlas, &devinfo,
254 global.deviceatlas.useragentid, useragent, 0);
255
256 return status != DA_OK ? 0 : da_haproxy(args, smp, &devinfo);
257}
258
259#define DA_MAX_HEADERS 24
260
261static int da_haproxy_fetch(const struct arg *args, struct sample *smp, const char *kw, void *private)
262{
263 struct hdr_idx *hidx;
264 struct hdr_ctx hctx;
265 const struct http_msg *hmsg;
266 da_evidence_t ev[DA_MAX_HEADERS];
267 da_deviceinfo_t devinfo;
268 da_status_t status;
269 char vbuf[DA_MAX_HEADERS][1024] = {{ 0 }};
270 int i, nbh = 0;
271
272 if (global.deviceatlas.daset == 0) {
273 return 1;
274 }
275
276 CHECK_HTTP_MESSAGE_FIRST();
277 smp->data.type = SMP_T_STR;
278
279 /**
280 * Here we go through the whole list of headers from start
281 * they will be filtered via the DeviceAtlas API itself
282 */
283 hctx.idx = 0;
284 hidx = &smp->strm->txn->hdr_idx;
285 hmsg = &smp->strm->txn->req;
286
287 while (http_find_next_header(hmsg->chn->buf->p, hidx, &hctx) == 1 &&
288 nbh < DA_MAX_HEADERS) {
289 char *pval;
290 size_t vlen;
291 da_evidence_id_t evid = -1;
292 char hbuf[24] = { 0 };
293
294 /* The HTTP headers used by the DeviceAtlas API are not longer */
295 if (hctx.del >= sizeof(hbuf)) {
296 continue;
297 }
298
299 vlen = hctx.vlen;
300 memcpy(hbuf, hctx.line, hctx.del);
301 hbuf[hctx.del] = 0;
302 pval = (hctx.line + hctx.val);
303
304 if (strcmp(hbuf, "Accept-Language") == 0) {
305 evid = da_atlas_accept_language_evidence_id(&global.deviceatlas.
306 atlas);
307 } else if (strcmp(hbuf, "Cookie") == 0) {
308 char *p, *eval;
309 int pl;
310
311 eval = pval + hctx.vlen;
312 /**
313 * The cookie value, if it exists, is located between the current header's
314 * value position and the next one
315 */
316 if (extract_cookie_value(pval, eval, global.deviceatlas.cookiename,
317 global.deviceatlas.cookienamelen, 1, &p, &pl) == NULL) {
318 continue;
319 }
320
321 vlen = (size_t)pl;
322 pval = p;
323 evid = da_atlas_clientprop_evidence_id(&global.deviceatlas.atlas);
324 } else {
325 evid = da_atlas_header_evidence_id(&global.deviceatlas.atlas,
326 hbuf);
327 }
328
329 if (evid == -1) {
330 continue;
331 }
332
333 i = vlen > sizeof(vbuf[nbh]) ? sizeof(vbuf[nbh]) : vlen;
334 memcpy(vbuf[nbh], pval, i - 1);
335 vbuf[nbh][i - 1] = 0;
336 ev[nbh].key = evid;
337 ev[nbh].value = vbuf[nbh];
338 ev[nbh].value[vlen] = 0;
339 ++ nbh;
340 }
341
342 status = da_searchv(&global.deviceatlas.atlas, &devinfo,
343 ev, nbh);
344
345 return status != DA_OK ? 0 : da_haproxy(args, smp, &devinfo);
346}
347
Willy Tarreau0d74f772015-06-01 15:42:29 +0200348static struct cfg_kw_list dacfg_kws = {{ }, {
349 { CFG_GLOBAL, "deviceatlas-json-file", da_json_file },
350 { CFG_GLOBAL, "deviceatlas-log-level", da_log_level },
351 { CFG_GLOBAL, "deviceatlas-property-separator", da_property_separator },
David Carlier608c65a2015-09-25 14:16:30 +0100352 { CFG_GLOBAL, "deviceatlas-properties-cookie", da_properties_cookie },
Willy Tarreau0d74f772015-06-01 15:42:29 +0200353 { 0, NULL, NULL },
354}};
355
Willy Tarreauf63386a2015-06-01 15:39:50 +0200356/* Note: must not be declared <const> as its list will be overwritten */
David Carlier608c65a2015-09-25 14:16:30 +0100357static struct sample_fetch_kw_list fetch_kws = {ILH, {
David Carlier840b0242016-03-16 10:09:55 +0000358 { "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 +0100359 { NULL, NULL, 0, 0, 0 },
360}};
361
362/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreauf63386a2015-06-01 15:39:50 +0200363static struct sample_conv_kw_list conv_kws = {ILH, {
David Carlier840b0242016-03-16 10:09:55 +0000364 { "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 +0200365 { NULL, NULL, 0, 0, 0 },
366}};
367
368__attribute__((constructor))
369static void __da_init(void)
370{
371 /* register sample fetch and format conversion keywords */
David Carlier608c65a2015-09-25 14:16:30 +0100372 sample_register_fetches(&fetch_kws);
Willy Tarreauf63386a2015-06-01 15:39:50 +0200373 sample_register_convs(&conv_kws);
Willy Tarreau0d74f772015-06-01 15:42:29 +0200374 cfg_register_keywords(&dacfg_kws);
Willy Tarreau3dd483e2016-12-21 18:50:22 +0100375 hap_register_build_opts("Built with DeviceAtlas support.", 0);
Willy Tarreau876054d2016-12-21 20:39:16 +0100376 hap_register_post_check(init_deviceatlas);
Willy Tarreauf63386a2015-06-01 15:39:50 +0200377}