]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/spotlight.c
Move and rename Spotlight module
[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 #define USE_LIST
16
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif /* HAVE_CONFIG_H */
20
21 #include <string.h>
22 #include <strings.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <stdbool.h>
27 #include <inttypes.h>
28 #include <time.h>
29
30 #include <atalk/list.h>
31 #include <atalk/errchk.h>
32 #include <atalk/util.h>
33 #include <atalk/logger.h>
34 #include <atalk/talloc.h>
35 #include <atalk/dalloc.h>
36 #include <atalk/byteorder.h>
37 #include <atalk/netatalk_conf.h>
38 #include <atalk/volume.h>
39 #include <atalk/spotlight.h>
40
41 static TALLOC_CTX *sl_ctx;
42 static void *sl_module;
43 static struct sl_module_export *sl_module_export;
44
45 /* Helper functions and stuff */
46 static const char *neststrings[] = {
47     "",
48     "\t",
49     "\t\t",
50     "\t\t\t",
51     "\t\t\t\t",
52     "\t\t\t\t\t",
53     "\t\t\t\t\t\t",
54 };
55
56 static int dd_dump(DALLOC_CTX *dd, int nestinglevel)
57 {
58     const char *type;
59
60     LOG(log_debug, logtype_sl, "%s%s(#%d): {",
61         neststrings[nestinglevel], talloc_get_name(dd), talloc_array_length(dd->dd_talloc_array));
62
63     for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
64
65         type = talloc_get_name(dd->dd_talloc_array[n]);
66
67         if (STRCMP(type, ==, "DALLOC_CTX")
68                    || STRCMP(type, ==, "sl_array_t")
69                    || STRCMP(type, ==, "sl_filemeta_t")
70                    || STRCMP(type, ==, "sl_dict_t")) {
71             dd_dump(dd->dd_talloc_array[n], nestinglevel + 1);
72         } else if (STRCMP(type, ==, "uint64_t")) {
73             uint64_t i;
74             memcpy(&i, dd->dd_talloc_array[n], sizeof(uint64_t));
75             LOG(log_debug, logtype_sl, "%suint64_t: 0x%04x", neststrings[nestinglevel + 1], i);
76         } else if (STRCMP(type, ==, "char *")) {
77             LOG(log_debug, logtype_sl, "%sstring: %s", neststrings[nestinglevel + 1], (char *)dd->dd_talloc_array[n]);
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_nil_t")) {
83             LOG(log_debug, logtype_sl, "%snil", neststrings[nestinglevel + 1]);
84         } else if (STRCMP(type, ==, "sl_cnids_t")) {
85             sl_cnids_t cnids;
86             memcpy(&cnids, dd->dd_talloc_array[n], sizeof(sl_cnids_t));
87             LOG(log_debug, logtype_sl, "%sCNIDs: unkn1: 0x%" PRIx16 ", unkn2: 0x%" PRIx32,
88                    neststrings[nestinglevel + 1], cnids.ca_unkn1, cnids.ca_context);
89             if (cnids.ca_cnids)
90                 dd_dump(cnids.ca_cnids, nestinglevel + 2);
91         } else {
92             LOG(log_debug, logtype_sl, "%stype: %s", neststrings[nestinglevel + 1], type);
93         }
94     }
95     LOG(log_debug, logtype_sl, "%s}", neststrings[nestinglevel]);
96 }
97
98 /**************************************************************************************************
99  * Spotlight queries
100  **************************************************************************************************/
101
102 static ATALK_LIST_HEAD(sl_queries);
103
104 /*!
105  * Add a query to the list of active queries
106  */
107 static int slq_add(slq_t *slq)
108 {
109     list_add(&(slq->slq_list), &sl_queries);
110     return 0;
111 }
112
113 static int slq_remove(slq_t *slq)
114 {
115     EC_INIT;
116     struct list_head *p;
117     slq_t *q = NULL;
118
119     list_for_each(p, &sl_queries) {
120         q = list_entry(p, slq_t, slq_list);
121         if ((q->slq_ctx1 == slq->slq_ctx1) && (q->slq_ctx2 == slq->slq_ctx2)) {            
122             list_del(p);
123             break;
124         }
125         q = NULL;
126     }
127
128     if (q == NULL) {
129         /* The SL query 'slq' was not found in the list, this is not supposed to happen! */
130         LOG(log_warning, logtype_sl, "slq_remove: slq not in active query list");
131     }
132
133 EC_CLEANUP:
134     EC_EXIT;
135 }
136
137 static slq_t *slq_for_ctx(uint64_t ctx1, uint64_t ctx2)
138 {
139     EC_INIT;
140     slq_t *q = NULL;
141     struct list_head *p;
142
143     list_for_each(p, &sl_queries) {
144         q = list_entry(p, slq_t, slq_list);
145
146         LOG(log_debug, logtype_sl, "slq_for_ctx(ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64
147             "): active: ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64,
148             ctx1, ctx2, q->slq_ctx1, q->slq_ctx2);
149
150         if ((q->slq_ctx1 == ctx1) && (q->slq_ctx2 == ctx2)) {            
151             break;
152         }
153         q = NULL;
154     }
155
156 EC_CLEANUP:
157     if (ret != 0)
158         q = NULL;
159     return q;
160 }
161
162 /* Error handling for queries */
163 static void slq_error(slq_t *slq)
164 {
165     if (!slq)
166         return;
167     sl_module_export->sl_mod_error(slq);
168     slq_remove(slq);
169     talloc_free(slq);
170 }
171
172 /**************************************************************************************************
173  * Spotlight RPC functions
174  **************************************************************************************************/
175
176 static int sl_rpc_fetchPropertiesForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
177 {
178     EC_INIT;
179
180     char *s;
181     sl_dict_t *dict;
182     sl_array_t *array;
183     sl_uuid_t uuid;
184
185     if (!v->v_uuid)
186         EC_FAIL_LOG("sl_rpc_fetchPropertiesForContext: missing UUID for volume: %s", v->v_localname);
187
188     dict = talloc_zero(reply, sl_dict_t);
189
190     /* key/val 1 */
191     s = dalloc_strdup(dict, "kMDSStoreMetaScopes");
192     dalloc_add(dict, s, char *);
193
194     array = talloc_zero(dict, sl_array_t);
195     s = dalloc_strdup(array, "kMDQueryScopeComputer");
196     dalloc_add(array, s, char *);
197     dalloc_add(dict, array, sl_array_t);
198
199     /* key/val 2 */
200     s = dalloc_strdup(dict, "kMDSStorePathScopes");
201     dalloc_add(dict, s, char *);
202
203     array = talloc_zero(dict, sl_array_t);
204     s = dalloc_strdup(array, v->v_path);
205     dalloc_add(array, s, char *);
206     dalloc_add(dict, array, sl_array_t);
207
208     /* key/val 3 */
209     s = dalloc_strdup(dict, "kMDSStoreUUID");
210     dalloc_add(dict, s, char *);
211
212     memcpy(uuid.sl_uuid, v->v_uuid, 16);
213     dalloc_add_copy(dict, &uuid, sl_uuid_t);
214
215     /* key/val 4 */
216     s = dalloc_strdup(dict, "kMDSStoreHasPersistentUUID");
217     dalloc_add(dict, s, char *);
218     sl_bool_t b = true;
219     dalloc_add_copy(dict, &b, sl_bool_t);
220
221     dalloc_add(reply, dict, sl_dict_t);
222
223 EC_CLEANUP:
224     EC_EXIT;
225 }
226
227 static int sl_rpc_openQuery(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, struct vol *v)
228 {
229     EC_INIT;
230     char *sl_query;
231     uint64_t *uint64;
232     DALLOC_CTX *reqinfo;
233     sl_array_t *array;
234     slq_t *slq = NULL;
235
236     /* Allocate and initialize query object */
237     slq = talloc_zero(sl_ctx, slq_t);
238     slq->slq_state = SLQ_STATE_NEW;
239     slq->slq_obj = obj;
240     slq->slq_vol = v;
241     EC_NULL_LOG( sl_query = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDQueryString") );
242     LOG(log_debug, logtype_sl, "sl_rpc_openQuery: %s", sl_query);
243     slq->slq_qstring = talloc_steal(slq, sl_query);
244
245     slq->slq_time = time(NULL);
246     EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
247     slq->slq_ctx1 = *uint64;
248     EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
249     slq->slq_ctx2 = *uint64;
250     EC_NULL_LOG( reqinfo = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDAttributeArray") );
251     slq->slq_reqinfo = talloc_steal(slq, reqinfo);
252
253     LOG(log_maxdebug, logtype_sl, "sl_rpc_openQuery: requested attributes:");
254     dd_dump(slq->slq_reqinfo, 0);
255
256     (void)slq_add(slq);
257
258     /* Run the query */
259     EC_ZERO( sl_module_export->sl_mod_start_search(slq) );
260
261     array = talloc_zero(reply, sl_array_t);
262     uint64_t sl_res = ret == 0 ? 0 : UINT64_MAX;
263     dalloc_add_copy(array, &sl_res, uint64_t);
264     dalloc_add(reply, array, sl_array_t);
265
266 EC_CLEANUP:
267     if (ret != 0) {
268         slq_error(slq);
269     }
270     EC_EXIT;
271 }
272
273 static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
274 {
275     EC_INIT;
276     slq_t *slq = NULL;
277     uint64_t *uint64, ctx1, ctx2;
278
279     /* Context */
280     EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
281     ctx1 = *uint64;
282     EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
283     ctx2 = *uint64;
284
285     /* Get query for context */
286     EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
287     if (slq->slq_state != SLQ_STATE_RUNNING && slq->slq_state != SLQ_STATE_DONE) {
288         EC_FAIL_LOG("Spotlight: attempt to fetch results for query that isn't active");
289     }
290
291     /* Create and pass reply handle */
292     EC_NULL( slq->slq_reply = talloc_zero(reply, sl_array_t) );
293
294     /* Fetch Tracker results*/
295     EC_ZERO_LOG( sl_module_export->sl_mod_fetch_result(slq) );
296
297     dalloc_add(reply, slq->slq_reply, sl_array_t);
298
299 EC_CLEANUP:
300     if (ret != 0) {
301         slq_error(slq);
302     }
303     EC_EXIT;
304 }
305
306 static int sl_rpc_closeQueryForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
307 {
308     EC_INIT;
309     slq_t *slq = NULL;
310     uint64_t *uint64, ctx1, ctx2;
311     sl_array_t *array;
312     
313     /* Context */
314     EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
315     ctx1 = *uint64;
316     EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
317     ctx2 = *uint64;
318
319     /* Get query for context and free it */
320     EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
321     if (slq->slq_state != SLQ_STATE_DONE)
322         LOG(log_warning, logtype_sl, "Closing active query");
323     sl_module_export->sl_mod_end_search(slq);
324     slq_remove(slq);
325     talloc_free(slq);
326     slq = NULL;
327
328     array = talloc_zero(reply, sl_array_t);
329     uint64_t sl_res = 0;
330     dalloc_add_copy(array, &sl_res, uint64_t);
331     dalloc_add(reply, array, sl_array_t);
332
333 EC_CLEANUP:
334     if (ret != 0) {
335         slq_error(slq);
336     }
337     EC_EXIT;
338 }
339
340 /**************************************************************************************************
341  * Spotlight module functions
342  **************************************************************************************************/
343
344 int sl_mod_load(const char *path)
345 {
346     EC_INIT;
347
348     sl_ctx = talloc_new(NULL);
349
350     if ((sl_module = mod_open(path)) == NULL) {
351         LOG(log_error, logtype_sl, "Failed to load: %s", path, mod_error());
352         EC_FAIL;
353     }
354
355     if ((sl_module_export = mod_symbol(sl_module, "sl_mod")) == NULL) {
356         LOG(log_error, logtype_sl, "sl_mod_load(%s): mod_symbol error for symbol %s", path, "sl_mod");
357         EC_FAIL;
358     }
359
360     sl_module_export->sl_mod_init("test");
361    
362 EC_CLEANUP:
363     EC_EXIT;
364 }
365
366 /**
367  * Index a file
368  **/
369 void sl_index_file(const char *path)
370 {
371     if (sl_module_export && sl_module_export->sl_mod_index_file)
372         sl_module_export->sl_mod_index_file(path);
373 }
374
375 /**************************************************************************************************
376  * AFP functions
377  **************************************************************************************************/
378
379 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
380 {
381     EC_INIT;
382     TALLOC_CTX *tmp_ctx = talloc_new(NULL);
383     uint16_t vid;
384     int cmd;
385     int endianess = SL_ENC_LITTLE_ENDIAN;
386     struct vol      *vol;
387     DALLOC_CTX *query;
388     DALLOC_CTX *reply;
389     char *rpccmd;
390     int len;
391
392     *rbuflen = 0;
393
394     if (sl_module == NULL)
395         return AFPERR_NOOP;
396
397     ibuf += 2;
398     ibuflen -= 2;
399
400     vid = SVAL(ibuf, 0);
401     LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
402
403     if ((vol = getvolbyvid(vid)) == NULL) {
404         LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
405         ret = AFPERR_ACCESS;
406         goto EC_CLEANUP;
407     }
408
409     /*    IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
410
411     cmd = RIVAL(ibuf, 6);
412     LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
413
414     /*    IVAL(ibuf, 10: unknown, always 0x00000000 */
415
416     switch (cmd) {
417
418     case SPOTLIGHT_CMD_OPEN:
419     case SPOTLIGHT_CMD_OPEN2:
420         RSIVAL(rbuf, 0, ntohs(vid));
421         RSIVAL(rbuf, 4, 0);
422         len = strlen(vol->v_path) + 1;
423         strncpy(rbuf + 8, vol->v_path, len);
424         *rbuflen += 8 + len;
425         break;
426
427     case SPOTLIGHT_CMD_FLAGS:
428         RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? Helios uses 0x1eefface */
429         *rbuflen += 4;
430         break;
431
432     case SPOTLIGHT_CMD_RPC:
433         EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
434         EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) );
435         EC_NEG1_LOG( sl_unpack(query, ibuf + 22) );
436
437         LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Request dump:");
438         dd_dump(query, 0);
439
440         EC_NULL_LOG( rpccmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
441
442         if (STRCMP(rpccmd, ==, "fetchPropertiesForContext:")) {
443             EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) );
444         } else if (STRCMP(rpccmd, ==, "openQueryWithParams:forContext:")) {
445             EC_ZERO_LOG( sl_rpc_openQuery(obj, query, reply, vol) );
446         } else if (STRCMP(rpccmd, ==, "fetchQueryResultsForContext:")) {
447             EC_ZERO_LOG( sl_rpc_fetchQueryResultsForContext(obj, query, reply, vol) );
448         } else if (STRCMP(rpccmd, ==, "closeQueryForContext:")) {
449             EC_ZERO_LOG( sl_rpc_closeQueryForContext(obj, query, reply, vol) );
450         } else {
451             LOG(log_error, logtype_sl, "afp_spotlight_rpc: unknown Spotlight RPC: %s", rpccmd);
452         }
453
454         LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Reply dump:");
455         dd_dump(reply, 0);
456
457         memset(rbuf, 0, 4);
458         *rbuflen += 4;
459
460         EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
461         *rbuflen += len;
462         break;
463     }
464
465 EC_CLEANUP:
466     talloc_free(tmp_ctx);
467     if (ret != AFP_OK) {
468         *rbuflen = 0;
469         return AFPERR_MISC;
470     }
471     EC_EXIT;
472 }
473
474 /**************************************************************************************************
475  * Testing
476  **************************************************************************************************/
477
478 #ifdef SPOT_TEST_MAIN
479
480 int main(int argc, char **argv)
481 {
482     EC_INIT;
483     TALLOC_CTX *mem_ctx = talloc_new(NULL);
484     DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
485     uint64_t i;
486
487     set_processname("spot");
488     setuplog("default:info,spotlight:debug", "/dev/tty");
489
490     LOG(log_info, logtype_sl, "Start");
491
492     i = 1;
493     dalloc_add_copy(dd, &i, uint64_t);
494     char *str = dalloc_strdup(dd, "hello world");
495     dalloc_add(dd, str, char *);
496     sl_bool_t b = true;
497     dalloc_add_copy(dd, &b, sl_bool_t);
498
499     /* add a nested array */
500     DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
501     i = 3;
502     dalloc_add_copy(nested, &i, uint64_t);
503     dalloc_add(dd, nested, DALLOC_CTX);
504
505     /* test an allocated CNID array */
506     uint64_t id = 16;
507     sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
508     cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
509     cnids->ca_unkn1 = 1;
510     dalloc_add_copy(cnids->ca_cnids, &id, uint64_t);
511     dalloc_add(dd, cnids, sl_cnids_t);
512
513     /* Now the Spotlight types */
514     sl_array_t *sl_array = talloc_zero(dd, sl_array_t);
515     i = 0x1234;
516     dalloc_add_copy(sl_array, &i, uint64_t);
517
518     sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
519     i = 0xffff;
520     dalloc_add_copy(sl_dict, &i, uint64_t);
521     dalloc_add(sl_array, sl_dict, sl_dict_t);
522
523     dalloc_add(dd, sl_array, sl_array_t);
524
525     dd_dump(dd, 0);
526
527     /* now parse a real spotlight packet */
528     if (argc > 1) {
529         char ibuf[8192];
530         char rbuf[8192];
531         int fd;
532         size_t len;
533         DALLOC_CTX *query;
534
535         EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
536
537         EC_NEG1_LOG( fd = open(argv[1], O_RDONLY) );
538         EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
539         close(fd);
540         EC_NEG1_LOG( sl_unpack(query, ibuf + 24) );
541
542         /* Now dump the whole thing */
543         dd_dump(query, 0);
544     }
545
546 #if 0
547     /* packing  */
548     int qlen;
549     char buf[MAX_SLQ_DAT];
550     EC_NEG1_LOG( qlen = sl_pack(query, buf) );
551
552     EC_NEG1_LOG( fd = open("test.bin", O_RDWR) );
553     lseek(fd, 24, SEEK_SET);
554     write(fd, buf, qlen);
555     close(fd);
556 #endif
557
558 EC_CLEANUP:
559     if (mem_ctx) {
560         talloc_free(mem_ctx);
561         mem_ctx = NULL;
562     }
563     EC_EXIT;
564 }
565 #endif