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