2 Copyright (c) 2012 Frank Lahm <franklahm@gmail.com>
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.
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.
17 #endif /* HAVE_CONFIG_H */
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>
38 #include "spotlight.h"
40 static TALLOC_CTX *sl_ctx;
41 static void *sl_module;
42 static struct sl_module_export *sl_module_export;
44 /* Helper functions and stuff */
45 static const char *neststrings[] = {
55 static int dd_dump(DALLOC_CTX *dd, int nestinglevel)
59 LOG(log_debug, logtype_sl, "%s%s(#%d): {",
60 neststrings[nestinglevel], talloc_get_name(dd), talloc_array_length(dd->dd_talloc_array));
62 for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
64 type = talloc_get_name(dd->dd_talloc_array[n]);
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")) {
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")) {
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")) {
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);
89 dd_dump(cnids.ca_cnids, nestinglevel + 2);
91 LOG(log_debug, logtype_sl, "%stype: %s", neststrings[nestinglevel + 1], type);
94 LOG(log_debug, logtype_sl, "%s}", neststrings[nestinglevel]);
97 /**************************************************************************************************
99 **************************************************************************************************/
101 static q_t *sl_queries;
102 static slq_t *slq_active;
105 * Add a query to the list of active queries
107 static int slq_add(slq_t *slq)
112 talloc_free(slq_active);
119 static int slq_remove(slq_t *slq)
123 if ((slq_active->slq_ctx1 == slq->slq_ctx1) && (slq_active->slq_ctx2 == slq->slq_ctx2)) {
131 static slq_t *slq_for_ctx(uint64_t ctx1, uint64_t ctx2)
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);
140 if ((slq_active->slq_ctx1 == ctx1) && (slq_active->slq_ctx2 == ctx2))
151 /* Error handling for queries */
152 static void slq_error(slq_t *slq)
156 sl_module_export->sl_mod_error(slq);
161 /**************************************************************************************************
162 * Spotlight RPC functions
163 **************************************************************************************************/
165 static int sl_rpc_fetchPropertiesForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
175 EC_FAIL_LOG("sl_rpc_fetchPropertiesForContext: missing UUID for volume: %s", v->v_localname);
177 dict = talloc_zero(reply, sl_dict_t);
180 s = dalloc_strdup(dict, "kMDSStoreMetaScopes");
181 dalloc_add(dict, s, char *);
183 array = talloc_zero(dict, sl_array_t);
184 s = dalloc_strdup(array, "kMDQueryScopeComputer");
185 dalloc_add(array, s, char *);
186 dalloc_add(dict, array, sl_array_t);
189 s = dalloc_strdup(dict, "kMDSStorePathScopes");
190 dalloc_add(dict, s, char *);
192 array = talloc_zero(dict, sl_array_t);
193 s = dalloc_strdup(array, v->v_path);
194 dalloc_add(array, s, char *);
195 dalloc_add(dict, array, sl_array_t);
198 s = dalloc_strdup(dict, "kMDSStoreUUID");
199 dalloc_add(dict, s, char *);
201 memcpy(uuid.sl_uuid, v->v_uuid, 16);
202 dalloc_add_copy(dict, &uuid, sl_uuid_t);
205 s = dalloc_strdup(dict, "kMDSStoreHasPersistentUUID");
206 dalloc_add(dict, s, char *);
208 dalloc_add_copy(dict, &b, sl_bool_t);
210 dalloc_add(reply, dict, sl_dict_t);
216 static int sl_rpc_openQuery(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, struct vol *v)
225 /* Allocate and initialize query object */
226 slq = talloc_zero(sl_ctx, slq_t);
227 slq->slq_state = SLQ_STATE_NEW;
230 EC_NULL_LOG( sl_query = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDQueryString") );
231 LOG(log_debug, logtype_sl, "sl_rpc_openQuery: %s", sl_query);
232 slq->slq_qstring = talloc_steal(slq, sl_query);
234 slq->slq_time = time(NULL);
235 EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
236 slq->slq_ctx1 = *uint64;
237 EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
238 slq->slq_ctx2 = *uint64;
239 EC_NULL_LOG( reqinfo = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDAttributeArray") );
240 slq->slq_reqinfo = talloc_steal(slq, reqinfo);
242 LOG(log_maxdebug, logtype_sl, "sl_rpc_openQuery: requested attributes:");
243 dd_dump(slq->slq_reqinfo, 0);
248 EC_ZERO( sl_module_export->sl_mod_start_search(slq) );
250 array = talloc_zero(reply, sl_array_t);
251 uint64_t sl_res = ret == 0 ? 0 : UINT64_MAX;
252 dalloc_add_copy(array, &sl_res, uint64_t);
253 dalloc_add(reply, array, sl_array_t);
262 static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
266 uint64_t *uint64, ctx1, ctx2;
270 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
272 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
275 /* Get query for context */
276 EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
277 if (slq->slq_state != SLQ_STATE_RUNNING) {
278 EC_FAIL_LOG("Spotlight: attempt to fetch results for query that isn't runnnig");
281 /* Create and pass reply handle */
282 array = talloc_zero(reply, sl_array_t);
284 dalloc_add_copy(array, &sl_res, uint64_t);
285 slq->slq_reply = array;
287 /* Fetch Tracker results*/
288 EC_ZERO( sl_module_export->sl_mod_fetch_result(slq) );
290 dalloc_add(reply, array, sl_array_t);
299 static int sl_rpc_closeQueryForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
303 uint64_t *uint64, ctx1, ctx2;
307 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
309 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
312 /* Get query for context and free it */
313 EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
314 sl_module_export->sl_mod_end_search(slq);
319 array = talloc_zero(reply, sl_array_t);
321 dalloc_add_copy(array, &sl_res, uint64_t);
322 dalloc_add(reply, array, sl_array_t);
331 /**************************************************************************************************
332 * Spotlight module functions
333 **************************************************************************************************/
335 int sl_mod_load(const char *path)
339 sl_ctx = talloc_new(NULL);
340 sl_queries = queue_init();
342 if ((sl_module = mod_open(path)) == NULL) {
343 LOG(log_error, logtype_sl, "sl_mod_load(%s): failed to load: %s", path, mod_error());
347 if ((sl_module_export = mod_symbol(sl_module, "sl_mod")) == NULL) {
348 LOG(log_error, logtype_sl, "sl_mod_load(%s): mod_symbol error for symbol %s", path, "sl_mod");
352 sl_module_export->sl_mod_init("test");
358 /**************************************************************************************************
360 **************************************************************************************************/
361 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
364 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
367 int endianess = SL_ENC_LITTLE_ENDIAN;
380 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
382 if ((vol = getvolbyvid(vid)) == NULL) {
383 LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
388 /* IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
390 cmd = RIVAL(ibuf, 6);
391 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
393 /* IVAL(ibuf, 10: unknown, always 0x00000000 */
397 case SPOTLIGHT_CMD_OPEN:
398 case SPOTLIGHT_CMD_OPEN2:
399 RSIVAL(rbuf, 0, ntohs(vid));
401 len = strlen(vol->v_path) + 1;
402 strncpy(rbuf + 8, vol->v_path, len);
406 case SPOTLIGHT_CMD_FLAGS:
407 RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? Helios uses 0x1eefface */
411 case SPOTLIGHT_CMD_RPC:
412 EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
413 EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) );
414 EC_NEG1_LOG( sl_unpack(query, ibuf + 22) );
416 LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Request dump:");
419 EC_NULL_LOG( rpccmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
421 if (STRCMP(rpccmd, ==, "fetchPropertiesForContext:")) {
422 EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) );
423 } else if (STRCMP(rpccmd, ==, "openQueryWithParams:forContext:")) {
424 EC_ZERO_LOG( sl_rpc_openQuery(obj, query, reply, vol) );
425 } else if (STRCMP(rpccmd, ==, "fetchQueryResultsForContext:")) {
426 EC_ZERO_LOG( sl_rpc_fetchQueryResultsForContext(obj, query, reply, vol) );
427 } else if (STRCMP(rpccmd, ==, "closeQueryForContext:")) {
428 EC_ZERO_LOG( sl_rpc_closeQueryForContext(obj, query, reply, vol) );
430 LOG(log_error, logtype_sl, "afp_spotlight_rpc: unknown Spotlight RPC: %s", rpccmd);
433 LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Reply dump:");
439 EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
445 talloc_free(tmp_ctx);
453 /**************************************************************************************************
455 **************************************************************************************************/
457 #ifdef SPOT_TEST_MAIN
459 int main(int argc, char **argv)
462 TALLOC_CTX *mem_ctx = talloc_new(NULL);
463 DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
466 set_processname("spot");
467 setuplog("default:info,spotlight:debug", "/dev/tty");
469 LOG(log_info, logtype_sl, "Start");
472 dalloc_add_copy(dd, &i, uint64_t);
473 char *str = dalloc_strdup(dd, "hello world");
474 dalloc_add(dd, str, char *);
476 dalloc_add_copy(dd, &b, sl_bool_t);
478 /* add a nested array */
479 DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
481 dalloc_add_copy(nested, &i, uint64_t);
482 dalloc_add(dd, nested, DALLOC_CTX);
484 /* test an allocated CNID array */
486 sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
487 cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
489 dalloc_add_copy(cnids->ca_cnids, &id, uint64_t);
490 dalloc_add(dd, cnids, sl_cnids_t);
492 /* Now the Spotlight types */
493 sl_array_t *sl_array = talloc_zero(dd, sl_array_t);
495 dalloc_add_copy(sl_array, &i, uint64_t);
497 sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
499 dalloc_add_copy(sl_dict, &i, uint64_t);
500 dalloc_add(sl_array, sl_dict, sl_dict_t);
502 dalloc_add(dd, sl_array, sl_array_t);
506 /* now parse a real spotlight packet */
514 EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
516 EC_NEG1_LOG( fd = open(argv[1], O_RDONLY) );
517 EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
519 EC_NEG1_LOG( sl_unpack(query, ibuf + 24) );
521 /* Now dump the whole thing */
528 char buf[MAX_SLQ_DAT];
529 EC_NEG1_LOG( qlen = sl_pack(query, buf) );
531 EC_NEG1_LOG( fd = open("test.bin", O_RDWR) );
532 lseek(fd, 24, SEEK_SET);
533 write(fd, buf, qlen);
539 talloc_free(mem_ctx);