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>
40 #include "spotlight.h"
42 static TALLOC_CTX *sl_ctx;
43 static void *sl_module;
44 static struct sl_module_export *sl_module_export;
46 /* Helper functions and stuff */
47 static const char *neststrings[] = {
57 static int dd_dump(DALLOC_CTX *dd, int nestinglevel)
61 LOG(log_debug, logtype_sl, "%s%s(#%d): {",
62 neststrings[nestinglevel], talloc_get_name(dd), talloc_array_length(dd->dd_talloc_array));
64 for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
66 type = talloc_get_name(dd->dd_talloc_array[n]);
68 if (STRCMP(type, ==, "DALLOC_CTX")
69 || STRCMP(type, ==, "sl_array_t")
70 || STRCMP(type, ==, "sl_filemeta_t")
71 || STRCMP(type, ==, "sl_dict_t")) {
72 dd_dump(dd->dd_talloc_array[n], nestinglevel + 1);
73 } else if (STRCMP(type, ==, "uint64_t")) {
75 memcpy(&i, dd->dd_talloc_array[n], sizeof(uint64_t));
76 LOG(log_debug, logtype_sl, "%suint64_t: 0x%04x", neststrings[nestinglevel + 1], i);
77 } else if (STRCMP(type, ==, "char *")) {
78 LOG(log_debug, logtype_sl, "%sstring: %s", neststrings[nestinglevel + 1], (char *)dd->dd_talloc_array[n]);
79 } else if (STRCMP(type, ==, "sl_bool_t")) {
81 memcpy(&bl, dd->dd_talloc_array[n], sizeof(sl_bool_t));
82 LOG(log_debug, logtype_sl, "%sbool: %s", neststrings[nestinglevel + 1], bl ? "true" : "false");
83 } else if (STRCMP(type, ==, "sl_nil_t")) {
84 LOG(log_debug, logtype_sl, "%snil", neststrings[nestinglevel + 1]);
85 } else if (STRCMP(type, ==, "sl_cnids_t")) {
87 memcpy(&cnids, dd->dd_talloc_array[n], sizeof(sl_cnids_t));
88 LOG(log_debug, logtype_sl, "%sCNIDs: unkn1: 0x%" PRIx16 ", unkn2: 0x%" PRIx32,
89 neststrings[nestinglevel + 1], cnids.ca_unkn1, cnids.ca_context);
91 dd_dump(cnids.ca_cnids, nestinglevel + 2);
93 LOG(log_debug, logtype_sl, "%stype: %s", neststrings[nestinglevel + 1], type);
96 LOG(log_debug, logtype_sl, "%s}", neststrings[nestinglevel]);
99 /**************************************************************************************************
101 **************************************************************************************************/
103 static ATALK_LIST_HEAD(sl_queries);
106 * Add a query to the list of active queries
108 static int slq_add(slq_t *slq)
110 list_add(&(slq->slq_list), &sl_queries);
114 static int slq_remove(slq_t *slq)
120 list_for_each(p, &sl_queries) {
121 q = list_entry(p, slq_t, slq_list);
122 if ((q->slq_ctx1 == slq->slq_ctx1) && (q->slq_ctx2 == slq->slq_ctx2)) {
130 /* The SL query 'slq' was not found in the list, this is not supposed to happen! */
131 LOG(log_warning, logtype_sl, "slq_remove: slq not in active query list");
138 static slq_t *slq_for_ctx(uint64_t ctx1, uint64_t ctx2)
144 list_for_each(p, &sl_queries) {
145 q = list_entry(p, slq_t, slq_list);
147 LOG(log_debug, logtype_sl, "slq_for_ctx(ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64
148 "): active: ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64,
149 ctx1, ctx2, q->slq_ctx1, q->slq_ctx2);
151 if ((q->slq_ctx1 == ctx1) && (q->slq_ctx2 == ctx2)) {
163 /* Error handling for queries */
164 static void slq_error(slq_t *slq)
168 sl_module_export->sl_mod_error(slq);
173 /**************************************************************************************************
174 * Spotlight RPC functions
175 **************************************************************************************************/
177 static int sl_rpc_fetchPropertiesForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
187 EC_FAIL_LOG("sl_rpc_fetchPropertiesForContext: missing UUID for volume: %s", v->v_localname);
189 dict = talloc_zero(reply, sl_dict_t);
192 s = dalloc_strdup(dict, "kMDSStoreMetaScopes");
193 dalloc_add(dict, s, char *);
195 array = talloc_zero(dict, sl_array_t);
196 s = dalloc_strdup(array, "kMDQueryScopeComputer");
197 dalloc_add(array, s, char *);
198 dalloc_add(dict, array, sl_array_t);
201 s = dalloc_strdup(dict, "kMDSStorePathScopes");
202 dalloc_add(dict, s, char *);
204 array = talloc_zero(dict, sl_array_t);
205 s = dalloc_strdup(array, v->v_path);
206 dalloc_add(array, s, char *);
207 dalloc_add(dict, array, sl_array_t);
210 s = dalloc_strdup(dict, "kMDSStoreUUID");
211 dalloc_add(dict, s, char *);
213 memcpy(uuid.sl_uuid, v->v_uuid, 16);
214 dalloc_add_copy(dict, &uuid, sl_uuid_t);
217 s = dalloc_strdup(dict, "kMDSStoreHasPersistentUUID");
218 dalloc_add(dict, s, char *);
220 dalloc_add_copy(dict, &b, sl_bool_t);
222 dalloc_add(reply, dict, sl_dict_t);
228 static int sl_rpc_openQuery(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, struct vol *v)
237 /* Allocate and initialize query object */
238 slq = talloc_zero(sl_ctx, slq_t);
239 slq->slq_state = SLQ_STATE_NEW;
242 EC_NULL_LOG( sl_query = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDQueryString") );
243 LOG(log_debug, logtype_sl, "sl_rpc_openQuery: %s", sl_query);
244 slq->slq_qstring = talloc_steal(slq, sl_query);
246 slq->slq_time = time(NULL);
247 EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
248 slq->slq_ctx1 = *uint64;
249 EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
250 slq->slq_ctx2 = *uint64;
251 EC_NULL_LOG( reqinfo = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDAttributeArray") );
252 slq->slq_reqinfo = talloc_steal(slq, reqinfo);
254 LOG(log_maxdebug, logtype_sl, "sl_rpc_openQuery: requested attributes:");
255 dd_dump(slq->slq_reqinfo, 0);
260 EC_ZERO( sl_module_export->sl_mod_start_search(slq) );
262 array = talloc_zero(reply, sl_array_t);
263 uint64_t sl_res = ret == 0 ? 0 : UINT64_MAX;
264 dalloc_add_copy(array, &sl_res, uint64_t);
265 dalloc_add(reply, array, sl_array_t);
274 static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
278 uint64_t *uint64, ctx1, ctx2;
281 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
283 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
286 /* Get query for context */
287 EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
288 if (slq->slq_state != SLQ_STATE_RUNNING && slq->slq_state != SLQ_STATE_DONE) {
289 EC_FAIL_LOG("Spotlight: attempt to fetch results for query that isn't active");
292 /* Create and pass reply handle */
293 EC_NULL( slq->slq_reply = talloc_zero(reply, sl_array_t) );
295 /* Fetch Tracker results*/
296 EC_ZERO_LOG( sl_module_export->sl_mod_fetch_result(slq) );
298 dalloc_add(reply, slq->slq_reply, sl_array_t);
307 static int sl_rpc_closeQueryForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
311 uint64_t *uint64, ctx1, ctx2;
315 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
317 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
320 /* Get query for context and free it */
321 EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
322 if (slq->slq_state != SLQ_STATE_DONE)
323 LOG(log_warning, logtype_sl, "Closing active query");
324 sl_module_export->sl_mod_end_search(slq);
329 array = talloc_zero(reply, sl_array_t);
331 dalloc_add_copy(array, &sl_res, uint64_t);
332 dalloc_add(reply, array, sl_array_t);
341 /**************************************************************************************************
342 * Spotlight module functions
343 **************************************************************************************************/
345 int sl_mod_load(const char *path)
349 sl_ctx = talloc_new(NULL);
351 if ((sl_module = mod_open(path)) == NULL) {
352 LOG(log_error, logtype_sl, "Failed to load: %s", path, mod_error());
356 if ((sl_module_export = mod_symbol(sl_module, "sl_mod")) == NULL) {
357 LOG(log_error, logtype_sl, "sl_mod_load(%s): mod_symbol error for symbol %s", path, "sl_mod");
361 sl_module_export->sl_mod_init("test");
370 void sl_index_file(const char *path)
372 sl_module_export->sl_mod_index_file(path);
375 /**************************************************************************************************
377 **************************************************************************************************/
379 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
382 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
385 int endianess = SL_ENC_LITTLE_ENDIAN;
394 if (sl_module == NULL)
401 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
403 if ((vol = getvolbyvid(vid)) == NULL) {
404 LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
409 /* IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
411 cmd = RIVAL(ibuf, 6);
412 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
414 /* IVAL(ibuf, 10: unknown, always 0x00000000 */
418 case SPOTLIGHT_CMD_OPEN:
419 case SPOTLIGHT_CMD_OPEN2:
420 RSIVAL(rbuf, 0, ntohs(vid));
422 len = strlen(vol->v_path) + 1;
423 strncpy(rbuf + 8, vol->v_path, len);
427 case SPOTLIGHT_CMD_FLAGS:
428 RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? Helios uses 0x1eefface */
432 case SPOTLIGHT_CMD_RPC:
433 EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
434 EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) );
435 EC_NEG1_LOG( sl_unpack(query, ibuf + 22) );
437 LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Request dump:");
440 EC_NULL_LOG( rpccmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
442 if (STRCMP(rpccmd, ==, "fetchPropertiesForContext:")) {
443 EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) );
444 } else if (STRCMP(rpccmd, ==, "openQueryWithParams:forContext:")) {
445 EC_ZERO_LOG( sl_rpc_openQuery(obj, query, reply, vol) );
446 } else if (STRCMP(rpccmd, ==, "fetchQueryResultsForContext:")) {
447 EC_ZERO_LOG( sl_rpc_fetchQueryResultsForContext(obj, query, reply, vol) );
448 } else if (STRCMP(rpccmd, ==, "closeQueryForContext:")) {
449 EC_ZERO_LOG( sl_rpc_closeQueryForContext(obj, query, reply, vol) );
451 LOG(log_error, logtype_sl, "afp_spotlight_rpc: unknown Spotlight RPC: %s", rpccmd);
454 LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Reply dump:");
460 EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
466 talloc_free(tmp_ctx);
474 /**************************************************************************************************
476 **************************************************************************************************/
478 #ifdef SPOT_TEST_MAIN
480 int main(int argc, char **argv)
483 TALLOC_CTX *mem_ctx = talloc_new(NULL);
484 DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
487 set_processname("spot");
488 setuplog("default:info,spotlight:debug", "/dev/tty");
490 LOG(log_info, logtype_sl, "Start");
493 dalloc_add_copy(dd, &i, uint64_t);
494 char *str = dalloc_strdup(dd, "hello world");
495 dalloc_add(dd, str, char *);
497 dalloc_add_copy(dd, &b, sl_bool_t);
499 /* add a nested array */
500 DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
502 dalloc_add_copy(nested, &i, uint64_t);
503 dalloc_add(dd, nested, DALLOC_CTX);
505 /* test an allocated CNID array */
507 sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
508 cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
510 dalloc_add_copy(cnids->ca_cnids, &id, uint64_t);
511 dalloc_add(dd, cnids, sl_cnids_t);
513 /* Now the Spotlight types */
514 sl_array_t *sl_array = talloc_zero(dd, sl_array_t);
516 dalloc_add_copy(sl_array, &i, uint64_t);
518 sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
520 dalloc_add_copy(sl_dict, &i, uint64_t);
521 dalloc_add(sl_array, sl_dict, sl_dict_t);
523 dalloc_add(dd, sl_array, sl_array_t);
527 /* now parse a real spotlight packet */
535 EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
537 EC_NEG1_LOG( fd = open(argv[1], O_RDONLY) );
538 EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
540 EC_NEG1_LOG( sl_unpack(query, ibuf + 24) );
542 /* Now dump the whole thing */
549 char buf[MAX_SLQ_DAT];
550 EC_NEG1_LOG( qlen = sl_pack(query, buf) );
552 EC_NEG1_LOG( fd = open("test.bin", O_RDWR) );
553 lseek(fd, 24, SEEK_SET);
554 write(fd, buf, qlen);
560 talloc_free(mem_ctx);