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 */
27 #include <atalk/errchk.h>
28 #include <atalk/util.h>
29 #include <atalk/logger.h>
30 #include <atalk/talloc.h>
31 #include <atalk/dalloc.h>
32 #include <atalk/byteorder.h>
33 #include <atalk/netatalk_conf.h>
34 #include <atalk/volume.h>
36 #include "spotlight.h"
39 struct sl_module_export *sl_module_export;
42 /* Helper functions and stuff */
43 static const char *neststrings[] = {
53 static int dd_dump(DALLOC_CTX *dd, int nestinglevel)
57 LOG(log_debug, logtype_sl, "%s%s(#%d): {",
58 neststrings[nestinglevel], talloc_get_name(dd), talloc_array_length(dd->dd_talloc_array));
60 for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
62 type = talloc_get_name(dd->dd_talloc_array[n]);
64 if (STRCMP(type, ==, "DALLOC_CTX")
65 || STRCMP(type, ==, "sl_array_t")
66 || STRCMP(type, ==, "sl_dict_t")) {
67 dd_dump(dd->dd_talloc_array[n], nestinglevel + 1);
68 } else if (STRCMP(type, ==, "uint64_t")) {
70 memcpy(&i, dd->dd_talloc_array[n], sizeof(uint64_t));
71 LOG(log_debug, logtype_sl, "%suint64_t: 0x%04x", neststrings[nestinglevel + 1], i);
72 } else if (STRCMP(type, ==, "char *")) {
74 memcpy(&s, dd->dd_talloc_array[n], sizeof(char *));
75 LOG(log_debug, logtype_sl, "%sstring: %s", neststrings[nestinglevel + 1], s);
76 } else if (STRCMP(type, ==, "sl_bool_t")) {
78 memcpy(&bl, dd->dd_talloc_array[n], sizeof(sl_bool_t));
79 LOG(log_debug, logtype_sl, "%sbool: %s", neststrings[nestinglevel + 1], bl ? "true" : "false");
80 } else if (STRCMP(type, ==, "sl_cnids_t")) {
82 memcpy(&cnids, dd->dd_talloc_array[n], sizeof(sl_cnids_t));
83 LOG(log_debug, logtype_sl, "%sCNIDs: unkn1: %" PRIu16 ", unkn2: %" PRIu32,
84 neststrings[nestinglevel + 1], cnids.ca_unkn1, cnids.ca_context);
86 dd_dump(cnids.ca_cnids, nestinglevel + 1);
89 LOG(log_debug, logtype_sl, "%s}", neststrings[nestinglevel]);
92 /**************************************************************************************************
93 * Spotlight RPC functions
94 **************************************************************************************************/
96 static int sl_rpc_fetchPropertiesForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
106 EC_FAIL_LOG("sl_rpc_fetchPropertiesForContext: missing UUID for volume: %s", v->v_localname);
108 dict = talloc_zero(reply, sl_dict_t);
111 s = talloc_strdup(dict, "kMDSStoreMetaScopes");
112 dalloc_add(dict, &s, char *);
114 array = talloc_zero(dict, sl_array_t);
115 s = talloc_strdup(array, "kMDQueryScopeComputer");
116 dalloc_add(array, &s, char *);
117 dalloc_add(dict, array, sl_array_t);
120 s = talloc_strdup(dict, "kMDSStorePathScopes");
121 dalloc_add(dict, &s, char *);
123 array = talloc_zero(dict, sl_array_t);
124 s = talloc_strdup(array, v->v_path);
125 dalloc_add(array, &s, char *);
126 dalloc_add(dict, array, sl_array_t);
129 s = talloc_strdup(dict, "kMDSStoreUUID");
130 dalloc_add(dict, &s, char *);
132 memcpy(uuid.sl_uuid, v->v_uuid, 16);
133 dalloc_add(dict, &uuid, sl_uuid_t);
136 s = talloc_strdup(dict, "kMDSStoreHasPersistentUUID");
137 dalloc_add(dict, &s, char *);
139 dalloc_add(dict, &b, sl_bool_t);
141 dalloc_add(reply, dict, sl_dict_t);
147 static int sl_rpc_openQuery(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
154 /**************************************************************************************************
155 * Spotlight module functions
156 **************************************************************************************************/
158 int sl_mod_load(const char *path)
162 if ((sl_module = mod_open(path)) == NULL) {
163 LOG(log_error, logtype_sl, "sl_mod_load(%s): failed to load: %s", path, mod_error());
167 if ((sl_module_export = mod_symbol(sl_module, "sl_mod")) == NULL) {
168 LOG(log_error, logtype_sl, "sl_mod_load(%s): mod_symbol error for symbol %s", path, "sl_mod");
172 sl_module_export->sl_mod_init("test");
178 /**************************************************************************************************
180 **************************************************************************************************/
181 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
184 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
187 int endianess = SL_ENC_LITTLE_ENDIAN;
197 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
199 if ((vol = getvolbyvid(vid)) == NULL) {
200 LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
205 /* IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
207 cmd = RIVAL(ibuf, 6);
208 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
210 /* IVAL(ibuf, 10: unknown, always 0x00000000 */
214 case SPOTLIGHT_CMD_OPEN:
215 case SPOTLIGHT_CMD_OPEN2: {
216 RSIVAL(rbuf, 0, ntohs(vid));
218 int len = strlen(vol->v_path) + 1;
219 strncpy(rbuf + 8, vol->v_path, len);
223 case SPOTLIGHT_CMD_FLAGS:
224 RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? Helios uses 0x1eefface */
228 case SPOTLIGHT_CMD_RPC: {
230 EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
232 EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) );
234 EC_NEG1_LOG( sl_unpack(query, ibuf + 22) );
237 EC_NULL_LOG( cmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
239 if (STRCMP(*cmd, ==, "fetchPropertiesForContext:")) {
240 EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) );
241 } else if (STRCMP(*cmd, ==, "openQueryWithParams:forContext:")) {
242 EC_ZERO_LOG( sl_rpc_openQuery(obj, query, reply, vol) );
243 } else if (STRCMP(*cmd, ==, "fetchQueryResultsForContext:")) {
245 if ((p = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1)) != NULL) {
246 LOG(log_info, logtype_sl, "fetchQueryResultsForContext: 0x%" PRIx64, *p);
256 EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
264 talloc_free(tmp_ctx);
272 /**************************************************************************************************
274 **************************************************************************************************/
276 #ifdef SPOT_TEST_MAIN
278 int main(int argc, char **argv)
281 TALLOC_CTX *mem_ctx = talloc_new(NULL);
282 DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
285 set_processname("spot");
286 setuplog("default:info,spotlight:debug", "/dev/tty");
288 LOG(log_info, logtype_sl, "Start");
292 dalloc_add(dd, &i, uint64_t);
295 dalloc_add(dd, &i, uint64_t);
298 char *str = talloc_strdup(dd, "hello world");
299 dalloc_add(dd, &str, char *);
302 dalloc_add(dd, &b, sl_bool_t);
305 dalloc_add(dd, &b, sl_bool_t);
308 /* add a nested array */
309 DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
311 dalloc_add(nested, &i, uint64_t);
312 dalloc_add(dd, nested, DALLOC_CTX);
314 /* test an allocated CNID array */
316 sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
318 cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
323 dalloc_add(cnids->ca_cnids, &id, uint64_t);
324 dalloc_add(dd, cnids, sl_cnids_t);
329 /* Now the Spotlight types */
330 sl_array_t *sl_arrary = talloc_zero(dd, sl_array_t);
332 dalloc_add(sl_arrary, &i, uint64_t);
334 sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
336 dalloc_add(sl_dict, &i, uint64_t);
337 dalloc_add(sl_arrary, sl_dict, sl_dict_t);
339 dalloc_add(dd, sl_arrary, sl_array_t);
343 /* now parse a real spotlight packet */
351 EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
353 EC_NEG1_LOG( fd = open(argv[1], O_RDONLY) );
354 EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
356 EC_NEG1_LOG( sl_unpack(query, ibuf + 24) );
358 /* Now dump the whole thing */
365 char buf[MAX_SLQ_DAT];
366 EC_NEG1_LOG( qlen = sl_pack(query, buf) );
368 EC_NEG1_LOG( fd = open("test.bin", O_RDWR) );
369 lseek(fd, 24, SEEK_SET);
370 write(fd, buf, qlen);
376 talloc_free(mem_ctx);