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_dict_t")) {
69 dd_dump(dd->dd_talloc_array[n], nestinglevel + 1);
70 } else if (STRCMP(type, ==, "uint64_t")) {
72 memcpy(&i, dd->dd_talloc_array[n], sizeof(uint64_t));
73 LOG(log_debug, logtype_sl, "%suint64_t: 0x%04x", neststrings[nestinglevel + 1], i);
74 } else if (STRCMP(type, ==, "char *")) {
76 memcpy(&s, dd->dd_talloc_array[n], sizeof(char *));
77 LOG(log_debug, logtype_sl, "%sstring: %s", neststrings[nestinglevel + 1], s);
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_cnids_t")) {
84 memcpy(&cnids, dd->dd_talloc_array[n], sizeof(sl_cnids_t));
85 LOG(log_debug, logtype_sl, "%sCNIDs: unkn1: %" PRIu16 ", unkn2: %" PRIu32,
86 neststrings[nestinglevel + 1], cnids.ca_unkn1, cnids.ca_context);
88 dd_dump(cnids.ca_cnids, nestinglevel + 1);
91 LOG(log_debug, logtype_sl, "%s}", neststrings[nestinglevel]);
94 /**************************************************************************************************
96 **************************************************************************************************/
98 static q_t *sl_queries;
99 static slq_t *slq_active;
102 * Add a query to the list of active queries
104 static int slq_add(slq_t *slq)
108 LOG(log_debug, logtype_sl, "slq_add(q: \"%s\"ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64 ")",
109 slq->slq_qstring, slq->slq_ctx1, slq->slq_ctx2);
112 talloc_free(slq_active);
119 static slq_t *slq_for_ctx(uint64_t ctx1, uint64_t ctx2)
124 LOG(log_debug, logtype_sl, "slq_for_ctx(ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64
125 "): active: ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64,
126 ctx1, ctx2, slq_active->slq_ctx1, slq_active->slq_ctx2);
128 if ((slq_active->slq_ctx1 == ctx1) && (slq_active->slq_ctx2 == ctx2))
139 /**************************************************************************************************
140 * Spotlight RPC functions
141 **************************************************************************************************/
143 static int sl_rpc_fetchPropertiesForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
153 EC_FAIL_LOG("sl_rpc_fetchPropertiesForContext: missing UUID for volume: %s", v->v_localname);
155 dict = talloc_zero(reply, sl_dict_t);
158 s = talloc_strdup(dict, "kMDSStoreMetaScopes");
159 dalloc_add(dict, &s, char *);
161 array = talloc_zero(dict, sl_array_t);
162 s = talloc_strdup(array, "kMDQueryScopeComputer");
163 dalloc_add(array, &s, char *);
164 dalloc_add(dict, array, sl_array_t);
167 s = talloc_strdup(dict, "kMDSStorePathScopes");
168 dalloc_add(dict, &s, char *);
170 array = talloc_zero(dict, sl_array_t);
171 s = talloc_strdup(array, v->v_path);
172 dalloc_add(array, &s, char *);
173 dalloc_add(dict, array, sl_array_t);
176 s = talloc_strdup(dict, "kMDSStoreUUID");
177 dalloc_add(dict, &s, char *);
179 memcpy(uuid.sl_uuid, v->v_uuid, 16);
180 dalloc_add(dict, &uuid, sl_uuid_t);
183 s = talloc_strdup(dict, "kMDSStoreHasPersistentUUID");
184 dalloc_add(dict, &s, char *);
186 dalloc_add(dict, &b, sl_bool_t);
188 dalloc_add(reply, dict, sl_dict_t);
194 static int sl_rpc_openQuery(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
199 DALLOC_CTX *dalloc_ctx;
202 slq_t *slq = talloc_zero(sl_ctx, slq_t);
204 /* Allocate and initialize query object */
205 EC_NULL_LOG( sl_query = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDQueryString") );
206 LOG(log_debug, logtype_sl, "sl_rpc_openQuery: %s", *sl_query);
207 slq->slq_qstring = talloc_steal(slq, *sl_query);
208 slq->slq_state = SLQ_STATE_NEW;
209 slq->slq_time = time(NULL);
210 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
211 slq->slq_ctx1 = *uint64;
212 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
213 slq->slq_ctx2 = *uint64;
214 EC_NULL_LOG (dalloc_ctx = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDAttributeArray") );
215 slq->slq_reqinfo = talloc_steal(slq, dalloc_ctx);
216 LOG(log_maxdebug, logtype_sl, "sl_rpc_openQuery: requested attributes:");
217 dd_dump(slq->slq_reqinfo, 0);
222 sl_module_export->sl_mod_start_search(slq);
225 array = talloc_zero(reply, sl_array_t);
226 uint64_t sl_res = ret == 0 ? 0 : UINT64_MAX;
227 dalloc_add(array, &sl_res, uint64_t);
228 dalloc_add(reply, array, sl_array_t);
233 static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
237 uint64_t *uint64, ctx1, ctx2;
241 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
243 EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
246 /* Get query for context */
247 EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
249 /* Fetch Tracker results*/
250 sl_module_export->sl_mod_fetch_result(slq);
253 array = talloc_zero(reply, sl_array_t);
254 uint64_t sl_res = ret == 0 ? 0 : UINT64_MAX;
255 dalloc_add(array, &sl_res, uint64_t);
256 dalloc_add(reply, array, sl_array_t);
261 /**************************************************************************************************
262 * Spotlight module functions
263 **************************************************************************************************/
265 int sl_mod_load(const char *path)
269 sl_ctx = talloc_new(NULL);
270 sl_queries = queue_init();
272 if ((sl_module = mod_open(path)) == NULL) {
273 LOG(log_error, logtype_sl, "sl_mod_load(%s): failed to load: %s", path, mod_error());
277 if ((sl_module_export = mod_symbol(sl_module, "sl_mod")) == NULL) {
278 LOG(log_error, logtype_sl, "sl_mod_load(%s): mod_symbol error for symbol %s", path, "sl_mod");
282 sl_module_export->sl_mod_init("test");
288 /**************************************************************************************************
290 **************************************************************************************************/
291 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
294 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
297 int endianess = SL_ENC_LITTLE_ENDIAN;
307 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
309 if ((vol = getvolbyvid(vid)) == NULL) {
310 LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
315 /* IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
317 cmd = RIVAL(ibuf, 6);
318 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
320 /* IVAL(ibuf, 10: unknown, always 0x00000000 */
324 case SPOTLIGHT_CMD_OPEN:
325 case SPOTLIGHT_CMD_OPEN2: {
326 RSIVAL(rbuf, 0, ntohs(vid));
328 int len = strlen(vol->v_path) + 1;
329 strncpy(rbuf + 8, vol->v_path, len);
333 case SPOTLIGHT_CMD_FLAGS:
334 RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? Helios uses 0x1eefface */
338 case SPOTLIGHT_CMD_RPC: {
340 EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
342 EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) );
344 EC_NEG1_LOG( sl_unpack(query, ibuf + 22) );
348 EC_NULL_LOG( cmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
350 if (STRCMP(*cmd, ==, "fetchPropertiesForContext:")) {
351 EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) );
352 } else if (STRCMP(*cmd, ==, "openQueryWithParams:forContext:")) {
353 uint64_t *ctx1, *ctx2;
354 EC_NULL_LOG (ctx1 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
355 EC_NULL_LOG (ctx2 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
356 LOG(log_debug, logtype_sl, "ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64, *ctx1, *ctx2);
358 EC_ZERO_LOG( sl_rpc_openQuery(obj, query, reply, vol) );
359 } else if (STRCMP(*cmd, ==, "fetchQueryResultsForContext:")) {
360 EC_ZERO_LOG( sl_rpc_fetchQueryResultsForContext(obj, query, reply, vol) );
369 EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
377 talloc_free(tmp_ctx);
385 /**************************************************************************************************
387 **************************************************************************************************/
389 #ifdef SPOT_TEST_MAIN
391 int main(int argc, char **argv)
394 TALLOC_CTX *mem_ctx = talloc_new(NULL);
395 DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
398 set_processname("spot");
399 setuplog("default:info,spotlight:debug", "/dev/tty");
401 LOG(log_info, logtype_sl, "Start");
404 dalloc_add(dd, &i, uint64_t);
407 dalloc_add(dd, &i, uint64_t);
410 char *str = talloc_strdup(dd, "hello world");
411 dalloc_add(dd, &str, char *);
414 dalloc_add(dd, &b, sl_bool_t);
417 dalloc_add(dd, &b, sl_bool_t);
420 /* add a nested array */
421 DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
423 dalloc_add(nested, &i, uint64_t);
424 dalloc_add(dd, nested, DALLOC_CTX);
427 /* test an allocated CNID array */
429 sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
430 cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
432 dalloc_add(cnids->ca_cnids, &id, uint64_t);
433 dalloc_add(dd, cnids, sl_cnids_t);
436 /* Now the Spotlight types */
437 sl_array_t *sl_arrary = talloc_zero(dd, sl_array_t);
439 dalloc_add(sl_arrary, &i, uint64_t);
441 dalloc_add(sl_arrary, &i, uint64_t);
444 sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
446 dalloc_add(sl_dict, &i, uint64_t);
447 dalloc_add(sl_arrary, sl_dict, sl_dict_t);
450 dalloc_add(dd, sl_arrary, sl_array_t);
453 uint64_t *int1, *int2;
454 EC_NULL_LOG (int1 = dalloc_get(dd, "DALLOC_CTX", 6, "uint64_t", 0) );
455 EC_NULL_LOG (int2 = dalloc_get(dd, "DALLOC_CTX", 6, "uint64_t", 1) );
456 LOG(log_debug, logtype_sl, "ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64, *int1, *int2);
459 /* now parse a real spotlight packet */
467 EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
469 EC_NEG1_LOG( fd = open(argv[1], O_RDONLY) );
470 EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
472 EC_NEG1_LOG( sl_unpack(query, ibuf + 24) );
474 /* Now dump the whole thing */
482 char buf[MAX_SLQ_DAT];
483 EC_NEG1_LOG( qlen = sl_pack(query, buf) );
485 EC_NEG1_LOG( fd = open("test.bin", O_RDWR) );
486 lseek(fd, 24, SEEK_SET);
487 write(fd, buf, qlen);
493 talloc_free(mem_ctx);