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.
19 #endif /* HAVE_CONFIG_H */
30 #include <atalk/list.h>
31 #include <atalk/errchk.h>
32 #include <atalk/util.h>
33 #include <atalk/logger.h>
34 #include <atalk/talloc.h>
35 #include <atalk/dalloc.h>
36 #include <atalk/byteorder.h>
37 #include <atalk/netatalk_conf.h>
38 #include <atalk/volume.h>
39 #include <atalk/spotlight.h>
41 static TALLOC_CTX *sl_ctx;
42 static void *sl_module;
43 static struct sl_module_export *sl_module_export;
45 /* Helper functions and stuff */
46 static const char *neststrings[] = {
56 static int dd_dump(DALLOC_CTX *dd, int nestinglevel)
60 LOG(log_debug, logtype_sl, "%s%s(#%d): {",
61 neststrings[nestinglevel], talloc_get_name(dd), talloc_array_length(dd->dd_talloc_array));
63 for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
65 type = talloc_get_name(dd->dd_talloc_array[n]);
67 if (STRCMP(type, ==, "DALLOC_CTX")
68 || STRCMP(type, ==, "sl_array_t")
69 || STRCMP(type, ==, "sl_filemeta_t")
70 || STRCMP(type, ==, "sl_dict_t")) {
71 dd_dump(dd->dd_talloc_array[n], nestinglevel + 1);
72 } else if (STRCMP(type, ==, "uint64_t")) {
74 memcpy(&i, dd->dd_talloc_array[n], sizeof(uint64_t));
75 LOG(log_debug, logtype_sl, "%suint64_t: 0x%04x", neststrings[nestinglevel + 1], i);
76 } else if (STRCMP(type, ==, "char *")) {
77 LOG(log_debug, logtype_sl, "%sstring: %s", neststrings[nestinglevel + 1], (char *)dd->dd_talloc_array[n]);
78 } else if (STRCMP(type, ==, "sl_bool_t")) {
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_nil_t")) {
83 LOG(log_debug, logtype_sl, "%snil", neststrings[nestinglevel + 1]);
84 } else if (STRCMP(type, ==, "sl_cnids_t")) {
86 memcpy(&cnids, dd->dd_talloc_array[n], sizeof(sl_cnids_t));
87 LOG(log_debug, logtype_sl, "%sCNIDs: unkn1: 0x%" PRIx16 ", unkn2: 0x%" PRIx32,
88 neststrings[nestinglevel + 1], cnids.ca_unkn1, cnids.ca_context);
90 dd_dump(cnids.ca_cnids, nestinglevel + 2);
92 LOG(log_debug, logtype_sl, "%stype: %s", neststrings[nestinglevel + 1], type);
95 LOG(log_debug, logtype_sl, "%s}", neststrings[nestinglevel]);
98 /**************************************************************************************************
100 **************************************************************************************************/
102 static ATALK_LIST_HEAD(sl_queries);
105 * Add a query to the list of active queries
107 static int slq_add(slq_t *slq)
109 list_add(&(slq->slq_list), &sl_queries);
113 static int slq_remove(slq_t *slq)
119 list_for_each(p, &sl_queries) {
120 q = list_entry(p, slq_t, slq_list);
121 if ((q->slq_ctx1 == slq->slq_ctx1) && (q->slq_ctx2 == slq->slq_ctx2)) {
129 /* The SL query 'slq' was not found in the list, this is not supposed to happen! */
130 LOG(log_warning, logtype_sl, "slq_remove: slq not in active query list");
137 static slq_t *slq_for_ctx(uint64_t ctx1, uint64_t ctx2)
143 list_for_each(p, &sl_queries) {
144 q = list_entry(p, slq_t, slq_list);
146 LOG(log_debug, logtype_sl, "slq_for_ctx(ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64
147 "): active: ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64,
148 ctx1, ctx2, q->slq_ctx1, q->slq_ctx2);
150 if ((q->slq_ctx1 == ctx1) && (q->slq_ctx2 == ctx2)) {
162 /* Error handling for queries */
163 static void slq_error(slq_t *slq)
167 sl_module_export->sl_mod_error(slq);
172 /**************************************************************************************************
173 * Spotlight RPC functions
174 **************************************************************************************************/
176 static int sl_rpc_fetchPropertiesForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
186 EC_FAIL_LOG("sl_rpc_fetchPropertiesForContext: missing UUID for volume: %s", v->v_localname);
188 dict = talloc_zero(reply, sl_dict_t);
191 s = dalloc_strdup(dict, "kMDSStoreMetaScopes");
192 dalloc_add(dict, s, char *);
194 array = talloc_zero(dict, sl_array_t);
195 s = dalloc_strdup(array, "kMDQueryScopeComputer");
196 dalloc_add(array, s, char *);
197 dalloc_add(dict, array, sl_array_t);
200 s = dalloc_strdup(dict, "kMDSStorePathScopes");
201 dalloc_add(dict, s, char *);
203 array = talloc_zero(dict, sl_array_t);
204 s = dalloc_strdup(array, v->v_path);
205 dalloc_add(array, s, char *);
206 dalloc_add(dict, array, sl_array_t);
209 s = dalloc_strdup(dict, "kMDSStoreUUID");
210 dalloc_add(dict, s, char *);
212 memcpy(uuid.sl_uuid, v->v_uuid, 16);
213 dalloc_add_copy(dict, &uuid, sl_uuid_t);
216 s = dalloc_strdup(dict, "kMDSStoreHasPersistentUUID");
217 dalloc_add(dict, s, char *);
219 dalloc_add_copy(dict, &b, sl_bool_t);
221 dalloc_add(reply, dict, sl_dict_t);
227 static int cnid_comp_fn(const void *p1, const void *p2)
229 const uint64_t *cnid1 = p1, *cnid2 = p2;
230 if (*cnid1 == *cnid2)
238 static int sl_createCNIDArray(slq_t *slq, const DALLOC_CTX *p)
241 uint64_t *cnids = NULL;
243 EC_NULL( cnids = talloc_array(slq, uint64_t, talloc_array_length(p)) );
244 for (int i = 0; i < talloc_array_length(p); i++)
245 memcpy(&cnids[i], p->dd_talloc_array[i], sizeof(uint64_t));
246 qsort(cnids, talloc_array_length(p), sizeof(uint64_t), cnid_comp_fn);
248 slq->slq_cnids = cnids;
249 slq->slq_cnids_num = talloc_array_length(p);
259 static int sl_rpc_openQuery(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, struct vol *v)
269 /* Allocate and initialize query object */
270 slq = talloc_zero(sl_ctx, slq_t);
271 slq->slq_state = SLQ_STATE_NEW;
274 EC_NULL_LOG( sl_query = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDQueryString") );
275 LOG(log_debug, logtype_sl, "sl_rpc_openQuery: %s", sl_query);
276 slq->slq_qstring = talloc_steal(slq, sl_query);
278 slq->slq_time = time(NULL);
279 EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
280 slq->slq_ctx1 = *uint64;
281 EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
282 slq->slq_ctx2 = *uint64;
283 EC_NULL_LOG( reqinfo = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDAttributeArray") );
284 slq->slq_reqinfo = talloc_steal(slq, reqinfo);
285 if ((cnids = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDQueryItemArray"))) {
286 EC_ZERO_LOG( sl_createCNIDArray(slq, cnids->ca_cnids) );
289 LOG(log_maxdebug, logtype_sl, "sl_rpc_openQuery: requested attributes:");
290 dd_dump(slq->slq_reqinfo, 0);
295 EC_ZERO( sl_module_export->sl_mod_start_search(slq) );
297 array = talloc_zero(reply, sl_array_t);
298 uint64_t sl_res = ret == 0 ? 0 : UINT64_MAX;
299 dalloc_add_copy(array, &sl_res, uint64_t);
300 dalloc_add(reply, array, sl_array_t);
309 static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
313 uint64_t *uint64, ctx1, ctx2;
316 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
318 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
321 /* Get query for context */
322 EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
323 if (slq->slq_state != SLQ_STATE_RUNNING && slq->slq_state != SLQ_STATE_DONE) {
324 EC_FAIL_LOG("Spotlight: attempt to fetch results for query that isn't active");
327 /* Create and pass reply handle */
328 EC_NULL( slq->slq_reply = talloc_zero(reply, sl_array_t) );
330 /* Fetch Tracker results*/
331 EC_ZERO_LOG( sl_module_export->sl_mod_fetch_result(slq) );
333 dalloc_add(reply, slq->slq_reply, sl_array_t);
342 static int sl_rpc_closeQueryForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
346 uint64_t *uint64, ctx1, ctx2;
350 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
352 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
355 /* Get query for context and free it */
356 EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
357 if (slq->slq_state != SLQ_STATE_DONE)
358 LOG(log_warning, logtype_sl, "Closing active query");
359 sl_module_export->sl_mod_end_search(slq);
364 array = talloc_zero(reply, sl_array_t);
366 dalloc_add_copy(array, &sl_res, uint64_t);
367 dalloc_add(reply, array, sl_array_t);
376 /**************************************************************************************************
377 * Spotlight module functions
378 **************************************************************************************************/
380 int sl_mod_load(const char *path)
384 sl_ctx = talloc_new(NULL);
386 if ((sl_module = mod_open(path)) == NULL) {
387 LOG(log_error, logtype_sl, "Failed to load module \'%s\': %s", path, mod_error());
391 if ((sl_module_export = mod_symbol(sl_module, "sl_mod")) == NULL) {
392 LOG(log_error, logtype_sl, "sl_mod_load(%s): mod_symbol error for symbol %s", path, "sl_mod");
396 sl_module_export->sl_mod_init("test");
405 void sl_index_file(const char *path)
407 if (sl_module_export && sl_module_export->sl_mod_index_file)
408 sl_module_export->sl_mod_index_file(path);
411 /**************************************************************************************************
413 **************************************************************************************************/
415 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
418 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
421 int endianess = SL_ENC_LITTLE_ENDIAN;
430 if (sl_module == NULL)
437 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
439 if ((vol = getvolbyvid(vid)) == NULL) {
440 LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
445 /* IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
447 cmd = RIVAL(ibuf, 6);
448 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
450 /* IVAL(ibuf, 10: unknown, always 0x00000000 */
454 case SPOTLIGHT_CMD_OPEN:
455 case SPOTLIGHT_CMD_OPEN2:
456 RSIVAL(rbuf, 0, ntohs(vid));
458 len = strlen(vol->v_path) + 1;
459 strncpy(rbuf + 8, vol->v_path, len);
463 case SPOTLIGHT_CMD_FLAGS:
464 RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? Helios uses 0x1eefface */
468 case SPOTLIGHT_CMD_RPC:
469 EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
470 EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) );
471 EC_NEG1_LOG( sl_unpack(query, ibuf + 22) );
473 LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Request dump:");
476 EC_NULL_LOG( rpccmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
478 if (STRCMP(rpccmd, ==, "fetchPropertiesForContext:")) {
479 EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) );
480 } else if (STRCMP(rpccmd, ==, "openQueryWithParams:forContext:")) {
481 EC_ZERO_LOG( sl_rpc_openQuery(obj, query, reply, vol) );
482 } else if (STRCMP(rpccmd, ==, "fetchQueryResultsForContext:")) {
483 EC_ZERO_LOG( sl_rpc_fetchQueryResultsForContext(obj, query, reply, vol) );
484 } else if (STRCMP(rpccmd, ==, "closeQueryForContext:")) {
485 EC_ZERO_LOG( sl_rpc_closeQueryForContext(obj, query, reply, vol) );
487 LOG(log_error, logtype_sl, "afp_spotlight_rpc: unknown Spotlight RPC: %s", rpccmd);
490 LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Reply dump:");
496 EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
502 talloc_free(tmp_ctx);
510 /**************************************************************************************************
512 **************************************************************************************************/
514 #ifdef SPOT_TEST_MAIN
516 int main(int argc, char **argv)
519 TALLOC_CTX *mem_ctx = talloc_new(NULL);
520 DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
523 set_processname("spot");
524 setuplog("default:info,spotlight:debug", "/dev/tty");
526 LOG(log_info, logtype_sl, "Start");
529 dalloc_add_copy(dd, &i, uint64_t);
530 char *str = dalloc_strdup(dd, "hello world");
531 dalloc_add(dd, str, char *);
533 dalloc_add_copy(dd, &b, sl_bool_t);
535 /* add a nested array */
536 DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
538 dalloc_add_copy(nested, &i, uint64_t);
539 dalloc_add(dd, nested, DALLOC_CTX);
541 /* test an allocated CNID array */
543 sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
544 cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
546 dalloc_add_copy(cnids->ca_cnids, &id, uint64_t);
547 dalloc_add(dd, cnids, sl_cnids_t);
549 /* Now the Spotlight types */
550 sl_array_t *sl_array = talloc_zero(dd, sl_array_t);
552 dalloc_add_copy(sl_array, &i, uint64_t);
554 sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
556 dalloc_add_copy(sl_dict, &i, uint64_t);
557 dalloc_add(sl_array, sl_dict, sl_dict_t);
559 dalloc_add(dd, sl_array, sl_array_t);
563 /* now parse a real spotlight packet */
571 EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
573 EC_NEG1_LOG( fd = open(argv[1], O_RDONLY) );
574 EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
576 EC_NEG1_LOG( sl_unpack(query, ibuf + 24) );
578 /* Now dump the whole thing */
585 char buf[MAX_SLQ_DAT];
586 EC_NEG1_LOG( qlen = sl_pack(query, buf) );
588 EC_NEG1_LOG( fd = open("test.bin", O_RDWR) );
589 lseek(fd, 24, SEEK_SET);
590 write(fd, buf, qlen);
596 talloc_free(mem_ctx);