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