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