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)
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 = dalloc_strdup(dict, "kMDSStoreMetaScopes");
162 dalloc_add(dict, s, char *);
164 array = talloc_zero(dict, sl_array_t);
165 s = dalloc_strdup(array, "kMDQueryScopeComputer");
166 dalloc_add(array, s, char *);
167 dalloc_add(dict, array, sl_array_t);
170 s = dalloc_strdup(dict, "kMDSStorePathScopes");
171 dalloc_add(dict, s, char *);
173 array = talloc_zero(dict, sl_array_t);
174 s = dalloc_strdup(array, v->v_path);
175 dalloc_add(array, s, char *);
176 dalloc_add(dict, array, sl_array_t);
179 s = dalloc_strdup(dict, "kMDSStoreUUID");
180 dalloc_add(dict, s, char *);
182 memcpy(uuid.sl_uuid, v->v_uuid, 16);
183 dalloc_add_copy(dict, &uuid, sl_uuid_t);
186 s = dalloc_strdup(dict, "kMDSStoreHasPersistentUUID");
187 dalloc_add(dict, s, char *);
189 dalloc_add_copy(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);
222 LOG(log_maxdebug, logtype_sl, "sl_rpc_openQuery: requested attributes:");
223 dd_dump(slq->slq_reqinfo, 0);
228 sl_module_export->sl_mod_start_search(slq);
231 array = talloc_zero(reply, sl_array_t);
232 uint64_t sl_res = ret == 0 ? 0 : UINT64_MAX;
233 dalloc_add_copy(array, &sl_res, uint64_t);
234 dalloc_add(reply, array, sl_array_t);
239 static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
243 uint64_t *uint64, ctx1, ctx2;
246 array = talloc_zero(reply, sl_array_t);
248 dalloc_add_copy(array, &sl_res, uint64_t);
251 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
253 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
256 /* Get query for context */
257 EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
259 /* Pass reply handle */
260 slq->slq_reply = array;
262 /* Fetch Tracker results*/
263 sl_module_export->sl_mod_fetch_result(slq);
267 dalloc_add(reply, array, sl_array_t);
272 /**************************************************************************************************
273 * Spotlight module functions
274 **************************************************************************************************/
276 int sl_mod_load(const char *path)
280 sl_ctx = talloc_new(NULL);
281 sl_queries = queue_init();
283 if ((sl_module = mod_open(path)) == NULL) {
284 LOG(log_error, logtype_sl, "sl_mod_load(%s): failed to load: %s", path, mod_error());
288 if ((sl_module_export = mod_symbol(sl_module, "sl_mod")) == NULL) {
289 LOG(log_error, logtype_sl, "sl_mod_load(%s): mod_symbol error for symbol %s", path, "sl_mod");
293 sl_module_export->sl_mod_init("test");
299 /**************************************************************************************************
301 **************************************************************************************************/
302 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
305 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
308 int endianess = SL_ENC_LITTLE_ENDIAN;
318 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
320 if ((vol = getvolbyvid(vid)) == NULL) {
321 LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
326 /* IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
328 cmd = RIVAL(ibuf, 6);
329 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
331 /* IVAL(ibuf, 10: unknown, always 0x00000000 */
335 case SPOTLIGHT_CMD_OPEN:
336 case SPOTLIGHT_CMD_OPEN2: {
337 RSIVAL(rbuf, 0, ntohs(vid));
339 int len = strlen(vol->v_path) + 1;
340 strncpy(rbuf + 8, vol->v_path, len);
344 case SPOTLIGHT_CMD_FLAGS:
345 RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? Helios uses 0x1eefface */
349 case SPOTLIGHT_CMD_RPC: {
351 EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
353 EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) );
355 EC_NEG1_LOG( sl_unpack(query, ibuf + 22) );
356 LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Request dump:");
360 EC_NULL_LOG( cmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
362 if (STRCMP(cmd, ==, "fetchPropertiesForContext:")) {
363 EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) );
364 } else if (STRCMP(cmd, ==, "openQueryWithParams:forContext:")) {
365 EC_ZERO_LOG( sl_rpc_openQuery(obj, query, reply, vol) );
366 } else if (STRCMP(cmd, ==, "fetchQueryResultsForContext:")) {
367 EC_ZERO_LOG( sl_rpc_fetchQueryResultsForContext(obj, query, reply, vol) );
370 LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Reply dump:");
377 EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
385 talloc_free(tmp_ctx);
393 /**************************************************************************************************
395 **************************************************************************************************/
397 #ifdef SPOT_TEST_MAIN
399 int main(int argc, char **argv)
402 TALLOC_CTX *mem_ctx = talloc_new(NULL);
403 DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
406 set_processname("spot");
407 setuplog("default:info,spotlight:debug", "/dev/tty");
409 LOG(log_info, logtype_sl, "Start");
412 dalloc_add_copy(dd, &i, uint64_t);
413 char *str = dalloc_strdup(dd, "hello world");
414 dalloc_add(dd, str, char *);
416 dalloc_add_copy(dd, &b, sl_bool_t);
418 /* add a nested array */
419 DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
421 dalloc_add_copy(nested, &i, uint64_t);
422 dalloc_add(dd, nested, DALLOC_CTX);
424 /* test an allocated CNID array */
426 sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
427 cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
429 dalloc_add_copy(cnids->ca_cnids, &id, uint64_t);
430 dalloc_add(dd, cnids, sl_cnids_t);
432 /* Now the Spotlight types */
433 sl_array_t *sl_array = talloc_zero(dd, sl_array_t);
435 dalloc_add_copy(sl_array, &i, uint64_t);
437 sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
439 dalloc_add_copy(sl_dict, &i, uint64_t);
440 dalloc_add(sl_array, sl_dict, sl_dict_t);
442 dalloc_add(dd, sl_array, sl_array_t);
446 /* now parse a real spotlight packet */
454 EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
456 EC_NEG1_LOG( fd = open(argv[1], O_RDONLY) );
457 EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
459 EC_NEG1_LOG( sl_unpack(query, ibuf + 24) );
461 /* Now dump the whole thing */
468 char buf[MAX_SLQ_DAT];
469 EC_NEG1_LOG( qlen = sl_pack(query, buf) );
471 EC_NEG1_LOG( fd = open("test.bin", O_RDWR) );
472 lseek(fd, 24, SEEK_SET);
473 write(fd, buf, qlen);
479 talloc_free(mem_ctx);