]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/spotlight.c
Dispatch queries to Tracker and get results
[netatalk.git] / etc / afpd / spotlight.c
1 /*
2   Copyright (c) 2012 Frank Lahm <franklahm@gmail.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13 */
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif /* HAVE_CONFIG_H */
18
19 #include <string.h>
20 #include <strings.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <stdbool.h>
25 #include <inttypes.h>
26 #include <time.h>
27
28 #include <atalk/errchk.h>
29 #include <atalk/util.h>
30 #include <atalk/logger.h>
31 #include <atalk/talloc.h>
32 #include <atalk/dalloc.h>
33 #include <atalk/byteorder.h>
34 #include <atalk/netatalk_conf.h>
35 #include <atalk/volume.h>
36 #include <atalk/queue.h>
37
38 #include "spotlight.h"
39
40 static TALLOC_CTX *sl_ctx;
41 static void *sl_module;
42 static struct sl_module_export *sl_module_export;
43
44 /* Helper functions and stuff */
45 static const char *neststrings[] = {
46     "",
47     "\t",
48     "\t\t",
49     "\t\t\t",
50     "\t\t\t\t",
51     "\t\t\t\t\t",
52     "\t\t\t\t\t\t",
53 };
54
55 static int dd_dump(DALLOC_CTX *dd, int nestinglevel)
56 {
57     const char *type;
58
59     LOG(log_debug, logtype_sl, "%s%s(#%d): {",
60         neststrings[nestinglevel], talloc_get_name(dd), talloc_array_length(dd->dd_talloc_array));
61
62     for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
63
64         type = talloc_get_name(dd->dd_talloc_array[n]);
65
66         if (STRCMP(type, ==, "DALLOC_CTX")
67                    || STRCMP(type, ==, "sl_array_t")
68                    || STRCMP(type, ==, "sl_dict_t")) {
69             dd_dump(dd->dd_talloc_array[n], nestinglevel + 1);
70         } else if (STRCMP(type, ==, "uint64_t")) {
71             uint64_t i;
72             memcpy(&i, dd->dd_talloc_array[n], sizeof(uint64_t));
73             LOG(log_debug, logtype_sl, "%suint64_t: 0x%04x", neststrings[nestinglevel + 1], i);
74         } else if (STRCMP(type, ==, "char *")) {
75             char *s;
76             memcpy(&s, dd->dd_talloc_array[n], sizeof(char *));
77             LOG(log_debug, logtype_sl, "%sstring: %s", neststrings[nestinglevel + 1], s);
78         } else if (STRCMP(type, ==, "sl_bool_t")) {
79             sl_bool_t bl;
80             memcpy(&bl, dd->dd_talloc_array[n], sizeof(sl_bool_t));
81             LOG(log_debug, logtype_sl, "%sbool: %s", neststrings[nestinglevel + 1], bl ? "true" : "false");
82         } else if (STRCMP(type, ==, "sl_cnids_t")) {
83             sl_cnids_t cnids;
84             memcpy(&cnids, dd->dd_talloc_array[n], sizeof(sl_cnids_t));
85             LOG(log_debug, logtype_sl, "%sCNIDs: unkn1: %" PRIu16 ", unkn2: %" PRIu32,
86                    neststrings[nestinglevel + 1], cnids.ca_unkn1, cnids.ca_context);
87             if (cnids.ca_cnids)
88                 dd_dump(cnids.ca_cnids, nestinglevel + 1);
89         }
90     }
91     LOG(log_debug, logtype_sl, "%s}", neststrings[nestinglevel]);
92 }
93
94 /**************************************************************************************************
95  * Spotlight queries
96  **************************************************************************************************/
97
98 static q_t *sl_queries;
99 static slq_t *slq_active;
100
101 /*!
102  * Add a query to the list of active queries
103  */
104 static int slq_add(slq_t *slq)
105 {
106     EC_INIT;
107
108     LOG(log_debug, logtype_sl, "slq_add(q: \"%s\"ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64 ")",
109         slq->slq_qstring, slq->slq_ctx1, slq->slq_ctx2);
110
111     if (slq_active)
112         talloc_free(slq_active);
113     slq_active = slq;
114
115 EC_CLEANUP:
116     EC_EXIT;
117 }
118
119 static slq_t *slq_for_ctx(uint64_t ctx1, uint64_t ctx2)
120 {
121     EC_INIT;
122     slq_t *q;
123
124     LOG(log_debug, logtype_sl, "slq_for_ctx(ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64
125         "): active: ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64,
126         ctx1, ctx2, slq_active->slq_ctx1, slq_active->slq_ctx2);
127
128     if ((slq_active->slq_ctx1 == ctx1) && (slq_active->slq_ctx2 == ctx2))
129         q = slq_active;
130     else
131         q = NULL;
132     
133 EC_CLEANUP:
134     if (ret != 0)
135         q = NULL;
136     return q;
137 }
138
139 /**************************************************************************************************
140  * Spotlight RPC functions
141  **************************************************************************************************/
142
143 static int sl_rpc_fetchPropertiesForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
144 {
145     EC_INIT;
146
147     char *s;
148     sl_dict_t *dict;
149     sl_array_t *array;
150     sl_uuid_t uuid;
151
152     if (!v->v_uuid)
153         EC_FAIL_LOG("sl_rpc_fetchPropertiesForContext: missing UUID for volume: %s", v->v_localname);
154
155     dict = talloc_zero(reply, sl_dict_t);
156
157     /* key/val 1 */
158     s = talloc_strdup(dict, "kMDSStoreMetaScopes");
159     dalloc_add(dict, &s, char *);
160
161     array = talloc_zero(dict, sl_array_t);
162     s = talloc_strdup(array, "kMDQueryScopeComputer");
163     dalloc_add(array, &s, char *);
164     dalloc_add(dict, array, sl_array_t);
165
166     /* key/val 2 */
167     s = talloc_strdup(dict, "kMDSStorePathScopes");
168     dalloc_add(dict, &s, char *);
169
170     array = talloc_zero(dict, sl_array_t);
171     s = talloc_strdup(array, v->v_path);
172     dalloc_add(array, &s, char *);
173     dalloc_add(dict, array, sl_array_t);
174
175     /* key/val 3 */
176     s = talloc_strdup(dict, "kMDSStoreUUID");
177     dalloc_add(dict, &s, char *);
178
179     memcpy(uuid.sl_uuid, v->v_uuid, 16);
180     dalloc_add(dict, &uuid, sl_uuid_t);
181
182     /* key/val 4 */
183     s = talloc_strdup(dict, "kMDSStoreHasPersistentUUID");
184     dalloc_add(dict, &s, char *);
185     sl_bool_t b = true;
186     dalloc_add(dict, &b, sl_bool_t);
187
188     dalloc_add(reply, dict, sl_dict_t);
189
190 EC_CLEANUP:
191     EC_EXIT;
192 }
193
194 static int sl_rpc_openQuery(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
195 {
196     EC_INIT;
197     char **sl_query;
198     uint64_t *uint64;
199     DALLOC_CTX *dalloc_ctx;
200     sl_array_t *array;
201
202     slq_t *slq = talloc_zero(sl_ctx, slq_t);
203
204     /* Allocate and initialize query object */
205     EC_NULL_LOG( sl_query = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDQueryString") );
206     LOG(log_debug, logtype_sl, "sl_rpc_openQuery: %s", *sl_query);
207     slq->slq_qstring = talloc_steal(slq, *sl_query);
208     slq->slq_state = SLQ_STATE_NEW;
209     slq->slq_time = time(NULL);
210     EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
211     slq->slq_ctx1 = *uint64;
212     EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
213     slq->slq_ctx2 = *uint64;
214     EC_NULL_LOG (dalloc_ctx = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDAttributeArray") );
215     slq->slq_reqinfo = talloc_steal(slq, dalloc_ctx);
216     LOG(log_maxdebug, logtype_sl, "sl_rpc_openQuery: requested attributes:");
217     dd_dump(slq->slq_reqinfo, 0);
218
219     (void)slq_add(slq);
220
221     /* Run the query */
222     sl_module_export->sl_mod_start_search(slq);
223
224 EC_CLEANUP:
225     array = talloc_zero(reply, sl_array_t);
226     uint64_t sl_res = ret == 0 ? 0 : UINT64_MAX;
227     dalloc_add(array, &sl_res, uint64_t);
228     dalloc_add(reply, array, sl_array_t);
229
230     EC_EXIT;
231 }
232
233 static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
234 {
235     EC_INIT;
236     slq_t *slq;
237     uint64_t *uint64, ctx1, ctx2;
238     sl_array_t *array;
239     
240     /* Context */
241     EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
242     ctx1 = *uint64;
243     EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
244     ctx2 = *uint64;
245
246     /* Get query for context */
247     EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
248
249     /* Fetch Tracker results*/
250     sl_module_export->sl_mod_fetch_result(slq);
251
252 EC_CLEANUP:
253     array = talloc_zero(reply, sl_array_t);
254     uint64_t sl_res = ret == 0 ? 0 : UINT64_MAX;
255     dalloc_add(array, &sl_res, uint64_t);
256     dalloc_add(reply, array, sl_array_t);
257
258     EC_EXIT;
259 }
260
261 /**************************************************************************************************
262  * Spotlight module functions
263  **************************************************************************************************/
264
265 int sl_mod_load(const char *path)
266 {
267     EC_INIT;
268
269     sl_ctx = talloc_new(NULL);
270     sl_queries = queue_init();
271
272     if ((sl_module = mod_open(path)) == NULL) {
273         LOG(log_error, logtype_sl, "sl_mod_load(%s): failed to load: %s", path, mod_error());
274         EC_FAIL;
275     }
276
277     if ((sl_module_export = mod_symbol(sl_module, "sl_mod")) == NULL) {
278         LOG(log_error, logtype_sl, "sl_mod_load(%s): mod_symbol error for symbol %s", path, "sl_mod");
279         EC_FAIL;
280     }
281
282     sl_module_export->sl_mod_init("test");
283    
284 EC_CLEANUP:
285     EC_EXIT;
286 }
287
288 /**************************************************************************************************
289  * AFP functions
290  **************************************************************************************************/
291 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
292 {
293     EC_INIT;
294     TALLOC_CTX *tmp_ctx = talloc_new(NULL);
295     uint16_t vid;
296     int cmd;
297     int endianess = SL_ENC_LITTLE_ENDIAN;
298     struct vol      *vol;
299     DALLOC_CTX *query;
300
301     *rbuflen = 0;
302
303     ibuf += 2;
304     ibuflen -= 2;
305
306     vid = SVAL(ibuf, 0);
307     LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
308
309     if ((vol = getvolbyvid(vid)) == NULL) {
310         LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
311         ret = AFPERR_ACCESS;
312         goto EC_CLEANUP;
313     }
314
315     /*    IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
316
317     cmd = RIVAL(ibuf, 6);
318     LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
319
320     /*    IVAL(ibuf, 10: unknown, always 0x00000000 */
321
322     switch (cmd) {
323
324     case SPOTLIGHT_CMD_OPEN:
325     case SPOTLIGHT_CMD_OPEN2: {
326         RSIVAL(rbuf, 0, ntohs(vid));
327         RSIVAL(rbuf, 4, 0);
328         int len = strlen(vol->v_path) + 1;
329         strncpy(rbuf + 8, vol->v_path, len);
330         *rbuflen += 8 + len;
331         break;
332     }
333     case SPOTLIGHT_CMD_FLAGS:
334         RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? Helios uses 0x1eefface */
335         *rbuflen += 4;
336         break;
337
338     case SPOTLIGHT_CMD_RPC: {
339         DALLOC_CTX *query;
340         EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
341         DALLOC_CTX *reply;
342         EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) );
343
344         EC_NEG1_LOG( sl_unpack(query, ibuf + 22) );
345         dd_dump(query, 0);
346
347         char **cmd;
348         EC_NULL_LOG( cmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
349
350         if (STRCMP(*cmd, ==, "fetchPropertiesForContext:")) {
351             EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) );
352         } else if (STRCMP(*cmd, ==, "openQueryWithParams:forContext:")) {
353             uint64_t *ctx1, *ctx2;
354             EC_NULL_LOG (ctx1 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
355             EC_NULL_LOG (ctx2 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
356             LOG(log_debug, logtype_sl, "ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64, *ctx1, *ctx2);
357
358             EC_ZERO_LOG( sl_rpc_openQuery(obj, query, reply, vol) );
359         } else if (STRCMP(*cmd, ==, "fetchQueryResultsForContext:")) {
360             EC_ZERO_LOG( sl_rpc_fetchQueryResultsForContext(obj, query, reply, vol) );
361         }
362
363         dd_dump(reply, 0);
364
365         memset(rbuf, 0, 4);
366         *rbuflen += 4;
367
368         int len;
369         EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
370         *rbuflen += len;
371
372         break;
373     }
374     }
375
376 EC_CLEANUP:
377     talloc_free(tmp_ctx);
378     if (ret != AFP_OK) {
379         *rbuflen = 0;
380         return AFPERR_MISC;
381     }
382     EC_EXIT;
383 }
384
385 /**************************************************************************************************
386  * Testing
387  **************************************************************************************************/
388
389 #ifdef SPOT_TEST_MAIN
390
391 int main(int argc, char **argv)
392 {
393     EC_INIT;
394     TALLOC_CTX *mem_ctx = talloc_new(NULL);
395     DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
396     uint64_t i;
397
398     set_processname("spot");
399     setuplog("default:info,spotlight:debug", "/dev/tty");
400
401     LOG(log_info, logtype_sl, "Start");
402
403     i = 2;
404     dalloc_add(dd, &i, uint64_t);
405
406     i = 1;
407     dalloc_add(dd, &i, uint64_t);
408
409
410     char *str = talloc_strdup(dd, "hello world");
411     dalloc_add(dd, &str, char *);
412
413     sl_bool_t b = true;
414     dalloc_add(dd, &b, sl_bool_t);
415
416     b = false;
417     dalloc_add(dd, &b, sl_bool_t);
418
419
420     /* add a nested array */
421     DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
422     i = 3;
423     dalloc_add(nested, &i, uint64_t);
424     dalloc_add(dd, nested, DALLOC_CTX);
425
426 #if 0
427     /* test an allocated CNID array */
428     uint64_t id = 16;
429     sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
430     cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
431     cnids->ca_unkn1 = 1;
432     dalloc_add(cnids->ca_cnids, &id, uint64_t);
433     dalloc_add(dd, cnids, sl_cnids_t);
434 #endif
435
436     /* Now the Spotlight types */
437     sl_array_t *sl_arrary = talloc_zero(dd, sl_array_t);
438     i = 0x1234;
439     dalloc_add(sl_arrary, &i, uint64_t);
440     i = 0x5678;
441     dalloc_add(sl_arrary, &i, uint64_t);
442
443 #if 0
444     sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
445     i = 0xffff;
446     dalloc_add(sl_dict, &i, uint64_t);
447     dalloc_add(sl_arrary, sl_dict, sl_dict_t);
448 #endif
449
450     dalloc_add(dd, sl_arrary, sl_array_t);
451     dd_dump(dd, 0);
452
453     uint64_t *int1, *int2;
454     EC_NULL_LOG (int1 = dalloc_get(dd, "DALLOC_CTX", 6, "uint64_t", 0) );
455     EC_NULL_LOG (int2 = dalloc_get(dd, "DALLOC_CTX", 6, "uint64_t", 1) );
456     LOG(log_debug, logtype_sl, "ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64, *int1, *int2);
457
458 #if 0
459     /* now parse a real spotlight packet */
460     if (argc > 1) {
461         char ibuf[8192];
462         char rbuf[8192];
463         int fd;
464         size_t len;
465         DALLOC_CTX *query;
466
467         EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
468
469         EC_NEG1_LOG( fd = open(argv[1], O_RDONLY) );
470         EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
471         close(fd);
472         EC_NEG1_LOG( sl_unpack(query, ibuf + 24) );
473
474         /* Now dump the whole thing */
475         dd_dump(query, 0);
476     }
477 #endif
478
479 #if 0
480     /* packing  */
481     int qlen;
482     char buf[MAX_SLQ_DAT];
483     EC_NEG1_LOG( qlen = sl_pack(query, buf) );
484
485     EC_NEG1_LOG( fd = open("test.bin", O_RDWR) );
486     lseek(fd, 24, SEEK_SET);
487     write(fd, buf, qlen);
488     close(fd);
489 #endif
490
491 EC_CLEANUP:
492     if (mem_ctx) {
493         talloc_free(mem_ctx);
494         mem_ctx = NULL;
495     }
496     EC_EXIT;
497 }
498 #endif