]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/spotlight.c
Return query results, CNIDs is working, FileMeta marshalling is missing
[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: 0x%" PRIx16 ", unkn2: 0x%" PRIx32,
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(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, struct vol *v)
195 {
196     EC_INIT;
197     char **sl_query;
198     uint64_t *uint64;
199     DALLOC_CTX *reqinfo;
200     sl_array_t *array;
201
202     slq_t *slq = talloc_zero(sl_ctx, slq_t);
203
204     /* Allocate and initialize query object */
205     slq->slq_obj = obj;
206     slq->slq_vol = v;
207     EC_NULL_LOG( sl_query = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDQueryString") );
208     LOG(log_debug, logtype_sl, "sl_rpc_openQuery: %s", *sl_query);
209     slq->slq_qstring = talloc_steal(slq, *sl_query);
210     slq->slq_state = SLQ_STATE_NEW;
211     slq->slq_time = time(NULL);
212     EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
213     slq->slq_ctx1 = *uint64;
214     EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
215     slq->slq_ctx2 = *uint64;
216     EC_NULL_LOG( reqinfo = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDAttributeArray") );
217     slq->slq_reqinfo = talloc_steal(slq, reqinfo);
218     slq->slq_metacount = dalloc_size(slq->slq_reqinfo);
219
220     LOG(log_maxdebug, logtype_sl, "sl_rpc_openQuery: requested attributes:");
221     dd_dump(slq->slq_reqinfo, 0);
222
223     (void)slq_add(slq);
224
225     /* Run the query */
226     sl_module_export->sl_mod_start_search(slq);
227
228 EC_CLEANUP:
229     array = talloc_zero(reply, sl_array_t);
230     uint64_t sl_res = ret == 0 ? 0 : UINT64_MAX;
231     dalloc_add(array, &sl_res, uint64_t);
232     dalloc_add(reply, array, sl_array_t);
233
234     EC_EXIT;
235 }
236
237 static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
238 {
239     EC_INIT;
240     slq_t *slq;
241     uint64_t *uint64, ctx1, ctx2;
242     sl_array_t *array;
243
244     array = talloc_zero(reply, sl_array_t);
245     uint64_t sl_res = 0;
246     dalloc_add(array, &sl_res, uint64_t);
247     
248     /* Context */
249     EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
250     ctx1 = *uint64;
251     EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
252     ctx2 = *uint64;
253
254     /* Get query for context */
255     EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
256
257     /* Pass reply handle */
258     slq->slq_reply = array;
259
260     /* Fetch Tracker results*/
261     sl_module_export->sl_mod_fetch_result(slq);
262
263 EC_CLEANUP:
264
265     dalloc_add(reply, array, sl_array_t);
266
267     EC_EXIT;
268 }
269
270 /**************************************************************************************************
271  * Spotlight module functions
272  **************************************************************************************************/
273
274 int sl_mod_load(const char *path)
275 {
276     EC_INIT;
277
278     sl_ctx = talloc_new(NULL);
279     sl_queries = queue_init();
280
281     if ((sl_module = mod_open(path)) == NULL) {
282         LOG(log_error, logtype_sl, "sl_mod_load(%s): failed to load: %s", path, mod_error());
283         EC_FAIL;
284     }
285
286     if ((sl_module_export = mod_symbol(sl_module, "sl_mod")) == NULL) {
287         LOG(log_error, logtype_sl, "sl_mod_load(%s): mod_symbol error for symbol %s", path, "sl_mod");
288         EC_FAIL;
289     }
290
291     sl_module_export->sl_mod_init("test");
292    
293 EC_CLEANUP:
294     EC_EXIT;
295 }
296
297 /**************************************************************************************************
298  * AFP functions
299  **************************************************************************************************/
300 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
301 {
302     EC_INIT;
303     TALLOC_CTX *tmp_ctx = talloc_new(NULL);
304     uint16_t vid;
305     int cmd;
306     int endianess = SL_ENC_LITTLE_ENDIAN;
307     struct vol      *vol;
308     DALLOC_CTX *query;
309
310     *rbuflen = 0;
311
312     ibuf += 2;
313     ibuflen -= 2;
314
315     vid = SVAL(ibuf, 0);
316     LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
317
318     if ((vol = getvolbyvid(vid)) == NULL) {
319         LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
320         ret = AFPERR_ACCESS;
321         goto EC_CLEANUP;
322     }
323
324     /*    IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
325
326     cmd = RIVAL(ibuf, 6);
327     LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
328
329     /*    IVAL(ibuf, 10: unknown, always 0x00000000 */
330
331     switch (cmd) {
332
333     case SPOTLIGHT_CMD_OPEN:
334     case SPOTLIGHT_CMD_OPEN2: {
335         RSIVAL(rbuf, 0, ntohs(vid));
336         RSIVAL(rbuf, 4, 0);
337         int len = strlen(vol->v_path) + 1;
338         strncpy(rbuf + 8, vol->v_path, len);
339         *rbuflen += 8 + len;
340         break;
341     }
342     case SPOTLIGHT_CMD_FLAGS:
343         RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? Helios uses 0x1eefface */
344         *rbuflen += 4;
345         break;
346
347     case SPOTLIGHT_CMD_RPC: {
348         DALLOC_CTX *query;
349         EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
350         DALLOC_CTX *reply;
351         EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) );
352
353         EC_NEG1_LOG( sl_unpack(query, ibuf + 22) );
354         LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Request dump:");
355         dd_dump(query, 0);
356
357         char **cmd;
358         EC_NULL_LOG( cmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
359
360         if (STRCMP(*cmd, ==, "fetchPropertiesForContext:")) {
361             EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) );
362         } else if (STRCMP(*cmd, ==, "openQueryWithParams:forContext:")) {
363             EC_ZERO_LOG( sl_rpc_openQuery(obj, query, reply, vol) );
364         } else if (STRCMP(*cmd, ==, "fetchQueryResultsForContext:")) {
365             EC_ZERO_LOG( sl_rpc_fetchQueryResultsForContext(obj, query, reply, vol) );
366         }
367
368         LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Reply dump:");
369         dd_dump(reply, 0);
370
371         memset(rbuf, 0, 4);
372         *rbuflen += 4;
373
374         int len;
375         EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
376         *rbuflen += len;
377
378         break;
379     }
380     }
381
382 EC_CLEANUP:
383     talloc_free(tmp_ctx);
384     if (ret != AFP_OK) {
385         *rbuflen = 0;
386         return AFPERR_MISC;
387     }
388     EC_EXIT;
389 }
390
391 /**************************************************************************************************
392  * Testing
393  **************************************************************************************************/
394
395 #ifdef SPOT_TEST_MAIN
396
397 int main(int argc, char **argv)
398 {
399     EC_INIT;
400     TALLOC_CTX *mem_ctx = talloc_new(NULL);
401     DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
402     uint64_t i;
403
404     set_processname("spot");
405     setuplog("default:info,spotlight:debug", "/dev/tty");
406
407     LOG(log_info, logtype_sl, "Start");
408
409     i = 2;
410     dalloc_add(dd, &i, uint64_t);
411
412     i = 1;
413     dalloc_add(dd, &i, uint64_t);
414
415
416     char *str = talloc_strdup(dd, "hello world");
417     dalloc_add(dd, &str, char *);
418
419     sl_bool_t b = true;
420     dalloc_add(dd, &b, sl_bool_t);
421
422     b = false;
423     dalloc_add(dd, &b, sl_bool_t);
424
425
426     /* add a nested array */
427     DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
428     i = 3;
429     dalloc_add(nested, &i, uint64_t);
430     dalloc_add(dd, nested, DALLOC_CTX);
431
432 #if 0
433     /* test an allocated CNID array */
434     uint64_t id = 16;
435     sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
436     cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
437     cnids->ca_unkn1 = 1;
438     dalloc_add(cnids->ca_cnids, &id, uint64_t);
439     dalloc_add(dd, cnids, sl_cnids_t);
440 #endif
441
442     /* Now the Spotlight types */
443     sl_array_t *sl_arrary = talloc_zero(dd, sl_array_t);
444     i = 0x1234;
445     dalloc_add(sl_arrary, &i, uint64_t);
446     i = 0x5678;
447     dalloc_add(sl_arrary, &i, uint64_t);
448
449 #if 0
450     sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
451     i = 0xffff;
452     dalloc_add(sl_dict, &i, uint64_t);
453     dalloc_add(sl_arrary, sl_dict, sl_dict_t);
454 #endif
455
456     dalloc_add(dd, sl_arrary, sl_array_t);
457     dd_dump(dd, 0);
458
459     uint64_t *int1, *int2;
460     EC_NULL_LOG (int1 = dalloc_get(dd, "DALLOC_CTX", 6, "uint64_t", 0) );
461     EC_NULL_LOG (int2 = dalloc_get(dd, "DALLOC_CTX", 6, "uint64_t", 1) );
462     LOG(log_debug, logtype_sl, "ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64, *int1, *int2);
463
464 #if 0
465     /* now parse a real spotlight packet */
466     if (argc > 1) {
467         char ibuf[8192];
468         char rbuf[8192];
469         int fd;
470         size_t len;
471         DALLOC_CTX *query;
472
473         EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
474
475         EC_NEG1_LOG( fd = open(argv[1], O_RDONLY) );
476         EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
477         close(fd);
478         EC_NEG1_LOG( sl_unpack(query, ibuf + 24) );
479
480         /* Now dump the whole thing */
481         dd_dump(query, 0);
482     }
483 #endif
484
485 #if 0
486     /* packing  */
487     int qlen;
488     char buf[MAX_SLQ_DAT];
489     EC_NEG1_LOG( qlen = sl_pack(query, buf) );
490
491     EC_NEG1_LOG( fd = open("test.bin", O_RDWR) );
492     lseek(fd, 24, SEEK_SET);
493     write(fd, buf, qlen);
494     close(fd);
495 #endif
496
497 EC_CLEANUP:
498     if (mem_ctx) {
499         talloc_free(mem_ctx);
500         mem_ctx = NULL;
501     }
502     EC_EXIT;
503 }
504 #endif