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 /**************************************************************************************************
152 * Spotlight RPC functions
153 **************************************************************************************************/
155 static int sl_rpc_fetchPropertiesForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
165 EC_FAIL_LOG("sl_rpc_fetchPropertiesForContext: missing UUID for volume: %s", v->v_localname);
167 dict = talloc_zero(reply, sl_dict_t);
170 s = dalloc_strdup(dict, "kMDSStoreMetaScopes");
171 dalloc_add(dict, s, char *);
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);
179 s = dalloc_strdup(dict, "kMDSStorePathScopes");
180 dalloc_add(dict, s, char *);
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);
188 s = dalloc_strdup(dict, "kMDSStoreUUID");
189 dalloc_add(dict, s, char *);
191 memcpy(uuid.sl_uuid, v->v_uuid, 16);
192 dalloc_add_copy(dict, &uuid, sl_uuid_t);
195 s = dalloc_strdup(dict, "kMDSStoreHasPersistentUUID");
196 dalloc_add(dict, s, char *);
198 dalloc_add_copy(dict, &b, sl_bool_t);
200 dalloc_add(reply, dict, sl_dict_t);
206 static int sl_rpc_openQuery(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, struct vol *v)
214 slq_t *slq = talloc_zero(sl_ctx, slq_t);
216 /* Allocate and initialize query object */
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);
231 LOG(log_maxdebug, logtype_sl, "sl_rpc_openQuery: requested attributes:");
232 dd_dump(slq->slq_reqinfo, 0);
237 sl_module_export->sl_mod_start_search(slq);
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);
249 static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
253 uint64_t *uint64, ctx1, ctx2;
257 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
259 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
262 /* Get query for context */
263 EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
265 /* Create and pass reply handle */
266 array = talloc_zero(reply, sl_array_t);
268 dalloc_add_copy(array, &sl_res, uint64_t);
269 slq->slq_reply = array;
271 /* Fetch Tracker results*/
272 sl_module_export->sl_mod_fetch_result(slq);
276 dalloc_add(reply, array, sl_array_t);
281 static int sl_rpc_closeQueryForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
285 uint64_t *uint64, ctx1, ctx2;
289 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
291 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
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);
302 array = talloc_zero(reply, sl_array_t);
304 dalloc_add_copy(array, &sl_res, uint64_t);
305 dalloc_add(reply, array, sl_array_t);
310 /**************************************************************************************************
311 * Spotlight module functions
312 **************************************************************************************************/
314 int sl_mod_load(const char *path)
318 sl_ctx = talloc_new(NULL);
319 sl_queries = queue_init();
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());
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");
331 sl_module_export->sl_mod_init("test");
337 /**************************************************************************************************
339 **************************************************************************************************/
340 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
343 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
346 int endianess = SL_ENC_LITTLE_ENDIAN;
359 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
361 if ((vol = getvolbyvid(vid)) == NULL) {
362 LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
367 /* IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
369 cmd = RIVAL(ibuf, 6);
370 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
372 /* IVAL(ibuf, 10: unknown, always 0x00000000 */
376 case SPOTLIGHT_CMD_OPEN:
377 case SPOTLIGHT_CMD_OPEN2:
378 RSIVAL(rbuf, 0, ntohs(vid));
380 len = strlen(vol->v_path) + 1;
381 strncpy(rbuf + 8, vol->v_path, len);
385 case SPOTLIGHT_CMD_FLAGS:
386 RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? Helios uses 0x1eefface */
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) );
395 LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Request dump:");
398 EC_NULL_LOG( rpccmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
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) );
409 LOG(log_error, logtype_sl, "afp_spotlight_rpc: unknown Spotlight RPC: %s", rpccmd);
412 LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Reply dump:");
418 EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
424 talloc_free(tmp_ctx);
432 /**************************************************************************************************
434 **************************************************************************************************/
436 #ifdef SPOT_TEST_MAIN
438 int main(int argc, char **argv)
441 TALLOC_CTX *mem_ctx = talloc_new(NULL);
442 DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
445 set_processname("spot");
446 setuplog("default:info,spotlight:debug", "/dev/tty");
448 LOG(log_info, logtype_sl, "Start");
451 dalloc_add_copy(dd, &i, uint64_t);
452 char *str = dalloc_strdup(dd, "hello world");
453 dalloc_add(dd, str, char *);
455 dalloc_add_copy(dd, &b, sl_bool_t);
457 /* add a nested array */
458 DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
460 dalloc_add_copy(nested, &i, uint64_t);
461 dalloc_add(dd, nested, DALLOC_CTX);
463 /* test an allocated CNID array */
465 sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
466 cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
468 dalloc_add_copy(cnids->ca_cnids, &id, uint64_t);
469 dalloc_add(dd, cnids, sl_cnids_t);
471 /* Now the Spotlight types */
472 sl_array_t *sl_array = talloc_zero(dd, sl_array_t);
474 dalloc_add_copy(sl_array, &i, uint64_t);
476 sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
478 dalloc_add_copy(sl_dict, &i, uint64_t);
479 dalloc_add(sl_array, sl_dict, sl_dict_t);
481 dalloc_add(dd, sl_array, sl_array_t);
485 /* now parse a real spotlight packet */
493 EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
495 EC_NEG1_LOG( fd = open(argv[1], O_RDONLY) );
496 EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
498 EC_NEG1_LOG( sl_unpack(query, ibuf + 24) );
500 /* Now dump the whole thing */
507 char buf[MAX_SLQ_DAT];
508 EC_NEG1_LOG( qlen = sl_pack(query, buf) );
510 EC_NEG1_LOG( fd = open("test.bin", O_RDWR) );
511 lseek(fd, 24, SEEK_SET);
512 write(fd, buf, qlen);
518 talloc_free(mem_ctx);