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