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 *")) {
77 memcpy(&s, dd->dd_talloc_array[n], sizeof(char *));
78 LOG(log_debug, logtype_sl, "%sstring: %s", neststrings[nestinglevel + 1], s);
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);
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)
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);
115 talloc_free(slq_active);
122 static slq_t *slq_for_ctx(uint64_t ctx1, uint64_t ctx2)
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);
131 if ((slq_active->slq_ctx1 == ctx1) && (slq_active->slq_ctx2 == ctx2))
142 /**************************************************************************************************
143 * Spotlight RPC functions
144 **************************************************************************************************/
146 static int sl_rpc_fetchPropertiesForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
156 EC_FAIL_LOG("sl_rpc_fetchPropertiesForContext: missing UUID for volume: %s", v->v_localname);
158 dict = talloc_zero(reply, sl_dict_t);
161 s = talloc_strdup(dict, "kMDSStoreMetaScopes");
162 dalloc_add(dict, &s, char *);
164 array = talloc_zero(dict, sl_array_t);
165 s = talloc_strdup(array, "kMDQueryScopeComputer");
166 dalloc_add(array, &s, char *);
167 dalloc_add(dict, array, sl_array_t);
170 s = talloc_strdup(dict, "kMDSStorePathScopes");
171 dalloc_add(dict, &s, char *);
173 array = talloc_zero(dict, sl_array_t);
174 s = talloc_strdup(array, v->v_path);
175 dalloc_add(array, &s, char *);
176 dalloc_add(dict, array, sl_array_t);
179 s = talloc_strdup(dict, "kMDSStoreUUID");
180 dalloc_add(dict, &s, char *);
182 memcpy(uuid.sl_uuid, v->v_uuid, 16);
183 dalloc_add(dict, &uuid, sl_uuid_t);
186 s = talloc_strdup(dict, "kMDSStoreHasPersistentUUID");
187 dalloc_add(dict, &s, char *);
189 dalloc_add(dict, &b, sl_bool_t);
191 dalloc_add(reply, dict, sl_dict_t);
197 static int sl_rpc_openQuery(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, struct vol *v)
205 slq_t *slq = talloc_zero(sl_ctx, slq_t);
207 /* Allocate and initialize query object */
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);
223 LOG(log_maxdebug, logtype_sl, "sl_rpc_openQuery: requested attributes:");
224 dd_dump(slq->slq_reqinfo, 0);
229 sl_module_export->sl_mod_start_search(slq);
232 array = talloc_zero(reply, sl_array_t);
233 uint64_t sl_res = ret == 0 ? 0 : UINT64_MAX;
234 dalloc_add(array, &sl_res, uint64_t);
235 dalloc_add(reply, array, sl_array_t);
240 static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
244 uint64_t *uint64, ctx1, ctx2;
247 array = talloc_zero(reply, sl_array_t);
249 dalloc_add(array, &sl_res, uint64_t);
252 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
254 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
257 /* Get query for context */
258 EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
260 /* Pass reply handle */
261 slq->slq_reply = array;
263 /* Fetch Tracker results*/
264 sl_module_export->sl_mod_fetch_result(slq);
268 dalloc_add(reply, array, sl_array_t);
273 /**************************************************************************************************
274 * Spotlight module functions
275 **************************************************************************************************/
277 int sl_mod_load(const char *path)
281 sl_ctx = talloc_new(NULL);
282 sl_queries = queue_init();
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());
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");
294 sl_module_export->sl_mod_init("test");
300 /**************************************************************************************************
302 **************************************************************************************************/
303 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
306 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
309 int endianess = SL_ENC_LITTLE_ENDIAN;
319 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
321 if ((vol = getvolbyvid(vid)) == NULL) {
322 LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
327 /* IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
329 cmd = RIVAL(ibuf, 6);
330 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
332 /* IVAL(ibuf, 10: unknown, always 0x00000000 */
336 case SPOTLIGHT_CMD_OPEN:
337 case SPOTLIGHT_CMD_OPEN2: {
338 RSIVAL(rbuf, 0, ntohs(vid));
340 int len = strlen(vol->v_path) + 1;
341 strncpy(rbuf + 8, vol->v_path, len);
345 case SPOTLIGHT_CMD_FLAGS:
346 RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? Helios uses 0x1eefface */
350 case SPOTLIGHT_CMD_RPC: {
352 EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
354 EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) );
356 EC_NEG1_LOG( sl_unpack(query, ibuf + 22) );
357 LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Request dump:");
361 EC_NULL_LOG( cmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
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) );
371 LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Reply dump:");
378 EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
386 talloc_free(tmp_ctx);
394 /**************************************************************************************************
396 **************************************************************************************************/
398 #ifdef SPOT_TEST_MAIN
400 int main(int argc, char **argv)
403 TALLOC_CTX *mem_ctx = talloc_new(NULL);
404 DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
407 set_processname("spot");
408 setuplog("default:info,spotlight:debug", "/dev/tty");
410 LOG(log_info, logtype_sl, "Start");
413 dalloc_add(dd, &i, uint64_t);
416 dalloc_add(dd, &i, uint64_t);
419 char *str = talloc_strdup(dd, "hello world");
420 dalloc_add(dd, &str, char *);
423 dalloc_add(dd, &b, sl_bool_t);
426 dalloc_add(dd, &b, sl_bool_t);
429 /* add a nested array */
430 DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
432 dalloc_add(nested, &i, uint64_t);
433 dalloc_add(dd, nested, DALLOC_CTX);
436 /* test an allocated CNID array */
438 sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
439 cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
441 dalloc_add(cnids->ca_cnids, &id, uint64_t);
442 dalloc_add(dd, cnids, sl_cnids_t);
445 /* Now the Spotlight types */
446 sl_array_t *sl_arrary = talloc_zero(dd, sl_array_t);
448 dalloc_add(sl_arrary, &i, uint64_t);
450 dalloc_add(sl_arrary, &i, uint64_t);
453 sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
455 dalloc_add(sl_dict, &i, uint64_t);
456 dalloc_add(sl_arrary, sl_dict, sl_dict_t);
459 dalloc_add(dd, sl_arrary, sl_array_t);
462 uint64_t *int1, *int2;
463 EC_NULL_LOG (int1 = dalloc_get(dd, "DALLOC_CTX", 6, "uint64_t", 0) );
464 EC_NULL_LOG (int2 = dalloc_get(dd, "DALLOC_CTX", 6, "uint64_t", 1) );
465 LOG(log_debug, logtype_sl, "ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64, *int1, *int2);
467 /* now parse a real spotlight packet */
475 EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
477 EC_NEG1_LOG( fd = open(argv[1], O_RDONLY) );
478 EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
480 EC_NEG1_LOG( sl_unpack(query, ibuf + 24) );
482 /* Now dump the whole thing */
489 char buf[MAX_SLQ_DAT];
490 EC_NEG1_LOG( qlen = sl_pack(query, buf) );
492 EC_NEG1_LOG( fd = open("test.bin", O_RDWR) );
493 lseek(fd, 24, SEEK_SET);
494 write(fd, buf, qlen);
500 talloc_free(mem_ctx);