summaryrefslogtreecommitdiffstats
path: root/source3/rpc_server/mdssvc/sparql_parser.y
blob: 68d4d870eb68e40529a7482aa5acfd78ebd01641 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
   Unix SMB/CIFS implementation.
   Main metadata server / Spotlight routines

   Copyright (C) Ralph Boehme 2012-2014

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

%{
	#include "includes.h"
	#include "rpc_server/mdssvc/mdssvc.h"
	#include "rpc_server/mdssvc/mdssvc_tracker.h"
	#include "rpc_server/mdssvc/sparql_parser.tab.h"
	#include "rpc_server/mdssvc/sparql_mapping.h"

	#define YYMALLOC SMB_MALLOC
	#define YYREALLOC SMB_REALLOC

	struct yy_buffer_state;
	typedef struct yy_buffer_state *YY_BUFFER_STATE;
	extern int mdsyylex (void);
	extern void mdsyyerror (char const *);
	extern void *mdsyyterminate(void);
	extern YY_BUFFER_STATE mdsyy_scan_string( const char *str);
	extern void mdsyy_delete_buffer ( YY_BUFFER_STATE buffer );

	/* forward declarations */
	static const char *map_expr(const char *attr, char op, const char *val);
	static const char *map_daterange(const char *dateattr,
					 time_t date1, time_t date2);
	static time_t isodate2unix(const char *s);

	/* global vars, eg needed by the lexer */
	struct sparql_parser_state {
		TALLOC_CTX *frame;
		YY_BUFFER_STATE s;
		char var;
		const char *result;
	} *global_sparql_parser_state;
%}

%code provides {
	#include <stdbool.h>
	#include "rpc_server/mdssvc/mdssvc.h"
	#define SPRAW_TIME_OFFSET 978307200
	extern int mdsyywrap(void);
	extern bool map_spotlight_to_sparql_query(struct sl_query *slq);
}

%union {
	int ival;
	const char *sval;
	bool bval;
	time_t tval;
}

%name-prefix "mdsyy"
%expect 5
%error-verbose

%type <sval> match expr line function
%type <tval> date

%token <sval> WORD
%token <bval> BOOL
%token FUNC_INRANGE
%token DATE_ISO
%token OBRACE CBRACE EQUAL UNEQUAL GT LT COMMA QUOTE
%left AND
%left OR
%%

input:
/* empty */
| input line
;

line:
expr {
	global_sparql_parser_state->result = $1;
}
;

expr:
BOOL {
	/*
	 * We can't properly handle these in expressions, fortunately this
	 * is probably only ever used by OS X as sole element in an
	 * expression ie "False" (when Finder window selected our share
	 * but no search string entered yet). Packet traces showed that OS
	 * X Spotlight server then returns a failure (ie -1) which is what
	 * we do here too by calling YYABORT.
	 */
	YYABORT;
}
/*
 * We have "match OR match" and "expr OR expr", because the former is
 * supposed to catch and coalesque expressions of the form
 *
 *   MDSattribute1="hello"||MDSattribute2="hello"
 *
 * into a single SPARQL expression for the case where both
 * MDSattribute1 and MDSattribute2 map to the same SPARQL attribute,
 * which is eg the case for "*" and "kMDItemTextContent" which both
 * map to SPARQL "fts:match".
 */

| match OR match {
	if (strcmp($1, $3) != 0) {
		$$ = talloc_asprintf(talloc_tos(), "{ %s } UNION { %s }", $1, $3);
	} else {
		$$ = talloc_asprintf(talloc_tos(), "%s", $1);
	}
}
| match {
	$$ = $1;
}
| function {
	$$ = $1;
}
| OBRACE expr CBRACE {
	$$ = talloc_asprintf(talloc_tos(), "%s", $2);
}
| expr AND expr {
	$$ = talloc_asprintf(talloc_tos(), "%s . %s", $1, $3);
}
| expr OR expr {
	if (strcmp($1, $3) != 0) {
		$$ = talloc_asprintf(talloc_tos(), "{ %s } UNION { %s }", $1, $3);
	} else {
		$$ = talloc_asprintf(talloc_tos(), "%s", $1);
	}
}
;

match:
WORD EQUAL QUOTE WORD QUOTE {
	$$ = map_expr($1, '=', $4);
	if ($$ == NULL) YYABORT;
}
| WORD UNEQUAL QUOTE WORD QUOTE {
	$$ = map_expr($1, '!', $4);
	if ($$ == NULL) YYABORT;
}
| WORD LT QUOTE WORD QUOTE {
	$$ = map_expr($1, '<', $4);
	if ($$ == NULL) YYABORT;
}
| WORD GT QUOTE WORD QUOTE {
	$$ = map_expr($1, '>', $4);
	if ($$ == NULL) YYABORT;
}
| WORD EQUAL QUOTE WORD QUOTE WORD {
	$$ = map_expr($1, '=', $4);
	if ($$ == NULL) YYABORT;
}
| WORD UNEQUAL QUOTE WORD QUOTE WORD {
	$$ = map_expr($1, '!', $4);
	if ($$ == NULL) YYABORT;
}
| WORD LT QUOTE WORD QUOTE WORD {
	$$ = map_expr($1, '<', $4);
	if ($$ == NULL) YYABORT;
}
| WORD GT QUOTE WORD QUOTE WORD {
	$$ = map_expr($1, '>', $4);
	if ($$ == NULL) YYABORT;
}
;

function:
FUNC_INRANGE OBRACE WORD COMMA date COMMA date CBRACE {
	$$ = map_daterange($3, $5, $7);
	if ($$ == NULL) YYABORT;
}
;

date:
DATE_ISO OBRACE WORD CBRACE    {$$ = isodate2unix($3);}
| WORD                         {$$ = atoi($1) + SPRAW_TIME_OFFSET;}
;

%%

static time_t isodate2unix(const char *s)
{
	struct tm tm = {};
	const char *p;

	p = strptime(s, "%Y-%m-%dT%H:%M:%SZ", &tm);
	if (p == NULL) {
		return (time_t)-1;
	}
	return mktime(&tm);
}

static const char *map_daterange(const char *dateattr,
				 time_t date1, time_t date2)
{
	struct sparql_parser_state *s = global_sparql_parser_state;
	int result = 0;
	char *sparql = NULL;
	const struct sl_attr_map *p;
	struct tm *tmp;
	char buf1[64], buf2[64];

	if (s->var == 'z') {
		return NULL;
	}

	tmp = localtime(&date1);
	if (tmp == NULL) {
		return NULL;
	}
	result = strftime(buf1, sizeof(buf1), "%Y-%m-%dT%H:%M:%SZ", tmp);
	if (result == 0) {
		return NULL;
	}

	tmp = localtime(&date2);
	if (tmp == NULL) {
		return NULL;
	}
	result = strftime(buf2, sizeof(buf2), "%Y-%m-%dT%H:%M:%SZ", tmp);
	if (result == 0) {
		return NULL;
	}

	p = sl_attr_map_by_spotlight(dateattr);
	if (p == NULL) {
		return NULL;
	}

	sparql = talloc_asprintf(talloc_tos(),
				 "?obj %s ?%c FILTER (?%c > '%s' && ?%c < '%s')",
				 p->sparql_attr,
				 s->var,
				 s->var,
				 buf1,
				 s->var,
				 buf2);
	if (sparql == NULL) {
		return NULL;
	}

	s->var++;
	return sparql;
}

static char *map_type_search(const char *attr, char op, const char *val)
{
	char *result = NULL;
	const char *sparqlAttr;
	const struct sl_type_map *p;

	p = sl_type_map_by_spotlight(val);
	if (p == NULL) {
		return NULL;
	}

	switch (p->type) {
	case kMDTypeMapRDF:
		sparqlAttr = "rdf:type";
		break;
	case kMDTypeMapMime:
		sparqlAttr = "nie:mimeType";
		break;
	default:
		return NULL;
	}

	result = talloc_asprintf(talloc_tos(), "?obj %s '%s'",
				 sparqlAttr,
				 p->sparql_type);
	if (result == NULL) {
		return NULL;
	}

	return result;
}

static const char *map_expr(const char *attr, char op, const char *val)
{
	struct sparql_parser_state *s = global_sparql_parser_state;
	int result = 0;
	char *sparql = NULL;
	const struct sl_attr_map *p;
	time_t t;
	struct tm *tmp;
	char buf1[64];
	char *q;
	const char *start;

	if (s->var == 'z') {
		return NULL;
	}

	p = sl_attr_map_by_spotlight(attr);
	if (p == NULL) {
		return NULL;
	}

	if ((p->type != ssmt_type) && (p->sparql_attr == NULL)) {
		yyerror("unsupported Spotlight attribute");
		return NULL;
	}

	switch (p->type) {
	case ssmt_bool:
		sparql = talloc_asprintf(talloc_tos(), "?obj %s '%s'",
					 p->sparql_attr, val);
		if (sparql == NULL) {
			return NULL;
		}
		break;

	case ssmt_num:
		sparql = talloc_asprintf(talloc_tos(),
					 "?obj %s ?%c FILTER(?%c %c%c '%s')",
					 p->sparql_attr,
					 s->var,
					 s->var,
					 op,
					 /* append '=' to '!' */
					 op == '!' ? '=' : ' ',
					 val);
		if (sparql == NULL) {
			return NULL;
		}
		s->var++;
		break;

	case ssmt_str:
		q = talloc_strdup(talloc_tos(), "");
		if (q == NULL) {
			return NULL;
		}
		start = val;
		while (*val) {
			if (*val != '*') {
				val++;
				continue;
			}
			if (val > start) {
				q = talloc_strndup_append(q, start, val - start);
				if (q == NULL) {
					return NULL;
				}
			}
			q = talloc_strdup_append(q, ".*");
			if (q == NULL) {
				return NULL;
			}
			val++;
			start = val;
		}
		if (val > start) {
			q = talloc_strndup_append(q, start, val - start);
			if (q == NULL) {
				return NULL;
			}
		}
		sparql = talloc_asprintf(talloc_tos(),
					 "?obj %s ?%c "
					 "FILTER(regex(?%c, '^%s$', 'i'))",
					 p->sparql_attr,
					 s->var,
					 s->var,
					 q);
		TALLOC_FREE(q);
		if (sparql == NULL) {
			return NULL;
		}
		s->var++;
		break;

	case ssmt_fts:
		sparql = talloc_asprintf(talloc_tos(), "?obj %s '%s'",
					 p->sparql_attr, val);
		if (sparql == NULL) {
			return NULL;
		}
		break;

	case ssmt_date:
		t = atoi(val) + SPRAW_TIME_OFFSET;
		tmp = localtime(&t);
		if (tmp == NULL) {
			return NULL;
		}
		result = strftime(buf1, sizeof(buf1),
				  "%Y-%m-%dT%H:%M:%SZ", tmp);
		if (result == 0) {
			return NULL;
		}
		sparql = talloc_asprintf(talloc_tos(),
					 "?obj %s ?%c FILTER(?%c %c '%s')",
					 p->sparql_attr,
					 s->var,
					 s->var,
					 op,
					 buf1);
		if (sparql == NULL) {
			return NULL;
		}
		s->var++;
		break;

	case ssmt_type:
		sparql = map_type_search(attr, op, val);
		if (sparql == NULL) {
			return NULL;
		}
		break;

	default:
		return NULL;
	}

	return sparql;
}

void mdsyyerror(const char *str)
{
	DEBUG(1, ("mdsyyerror: %s\n", str));
}

int mdsyywrap(void)
{
	return 1;
}

/**
 * Map a Spotlight RAW query string to a SPARQL query string
 **/
bool map_spotlight_to_sparql_query(struct sl_query *slq)
{
	struct sl_tracker_query *tq = talloc_get_type_abort(
		slq->backend_private, struct sl_tracker_query);
	struct sparql_parser_state s = {
		.frame = talloc_stackframe(),
		.var = 'a',
	};
	int result;

	s.s = mdsyy_scan_string(slq->query_string);
	if (s.s == NULL) {
		TALLOC_FREE(s.frame);
		return false;
	}
	global_sparql_parser_state = &s;
	result = mdsyyparse();
	global_sparql_parser_state = NULL;
	mdsyy_delete_buffer(s.s);

	if (result != 0) {
		TALLOC_FREE(s.frame);
		return false;
	}

	tq->sparql_query = talloc_asprintf(slq,
		"SELECT ?url WHERE { %s . ?obj nie:url ?url . "
		"FILTER(tracker:uri-is-descendant('file://%s/', ?url)) }",
		s.result, tq->path_scope);
	TALLOC_FREE(s.frame);
	if (tq->sparql_query == NULL) {
		return false;
	}

	return true;
}