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 /* Helper functions and stuff */
40 static const char *neststrings[] = {
50 static int dd_dump(DALLOC_CTX *dd, int nestinglevel)
54 LOG(log_debug, logtype_sl, "%s%s(#%d): {",
55 neststrings[nestinglevel], talloc_get_name(dd), talloc_array_length(dd->dd_talloc_array));
57 for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
59 type = talloc_get_name(dd->dd_talloc_array[n]);
61 if (STRCMP(type, ==, "DALLOC_CTX")
62 || STRCMP(type, ==, "sl_array_t")
63 || STRCMP(type, ==, "sl_dict_t")) {
64 dd_dump(dd->dd_talloc_array[n], nestinglevel + 1);
65 } else if (STRCMP(type, ==, "uint64_t")) {
67 memcpy(&i, dd->dd_talloc_array[n], sizeof(uint64_t));
68 LOG(log_debug, logtype_sl, "%suint64_t: 0x%04x", neststrings[nestinglevel + 1], i);
69 } else if (STRCMP(type, ==, "char *")) {
71 memcpy(&s, dd->dd_talloc_array[n], sizeof(char *));
72 LOG(log_debug, logtype_sl, "%sstring: %s", neststrings[nestinglevel + 1], s);
73 } else if (STRCMP(type, ==, "sl_bool_t")) {
75 memcpy(&bl, dd->dd_talloc_array[n], sizeof(sl_bool_t));
76 LOG(log_debug, logtype_sl, "%sbool: %s", neststrings[nestinglevel + 1], bl ? "true" : "false");
77 } else if (STRCMP(type, ==, "sl_cnids_t")) {
79 memcpy(&cnids, dd->dd_talloc_array[n], sizeof(sl_cnids_t));
80 LOG(log_debug, logtype_sl, "%sCNIDs: unkn1: %" PRIu16 ", unkn2: %" PRIu32,
81 neststrings[nestinglevel + 1], cnids.ca_unkn1, cnids.ca_context);
83 dd_dump(cnids.ca_cnids, nestinglevel + 1);
86 LOG(log_debug, logtype_sl, "%s}", neststrings[nestinglevel]);
89 /**************************************************************************************************
90 * Spotlight RPC functions
91 **************************************************************************************************/
93 static int sl_rpc_fetchPropertiesForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
103 EC_FAIL_LOG("sl_rpc_fetchPropertiesForContext: missing UUID for volume: %s", v->v_localname);
105 dict = talloc_zero(reply, sl_dict_t);
108 s = talloc_strdup(dict, "kMDSStoreMetaScopes");
109 dalloc_add(dict, &s, char *);
111 array = talloc_zero(dict, sl_array_t);
112 s = talloc_strdup(array, "kMDQueryScopeComputer");
113 dalloc_add(array, &s, char *);
114 dalloc_add(dict, array, sl_array_t);
117 s = talloc_strdup(dict, "kMDSStorePathScopes");
118 dalloc_add(dict, &s, char *);
120 array = talloc_zero(dict, sl_array_t);
121 s = talloc_strdup(array, v->v_path);
122 dalloc_add(array, &s, char *);
123 dalloc_add(dict, array, sl_array_t);
126 s = talloc_strdup(dict, "kMDSStoreUUID");
127 dalloc_add(dict, &s, char *);
129 memcpy(uuid.sl_uuid, v->v_uuid, 16);
130 dalloc_add(dict, &uuid, sl_uuid_t);
133 s = talloc_strdup(dict, "kMDSStoreHasPersistentUUID");
134 dalloc_add(dict, &s, char *);
136 dalloc_add(dict, &b, sl_bool_t);
138 dalloc_add(reply, dict, sl_dict_t);
144 static int sl_rpc_openQuery(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
151 /**************************************************************************************************
153 **************************************************************************************************/
154 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
157 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
160 int endianess = SL_ENC_LITTLE_ENDIAN;
170 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
172 if ((vol = getvolbyvid(vid)) == NULL) {
173 LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
178 /* IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
180 cmd = RIVAL(ibuf, 6);
181 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
183 /* IVAL(ibuf, 10: unknown, always 0x00000000 */
187 case SPOTLIGHT_CMD_OPEN:
188 case SPOTLIGHT_CMD_OPEN2: {
189 RSIVAL(rbuf, 0, ntohs(vid));
191 int len = strlen(vol->v_path) + 1;
192 strncpy(rbuf + 8, vol->v_path, len);
196 case SPOTLIGHT_CMD_FLAGS:
197 RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? Helios uses 0x1eefface */
201 case SPOTLIGHT_CMD_RPC: {
203 EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
205 EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) );
207 EC_ZERO( sl_unpack(query, ibuf + 22) );
211 EC_NULL_LOG( cmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
214 if (STRCMP(*cmd, ==, "fetchPropertiesForContext:")) {
215 EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) );
216 } else if (STRCMP(*cmd, ==, "openQueryWithParams:forContext:")) {
217 EC_ZERO_LOG( sl_rpc_openQuery(obj, query, reply, vol) );
218 } else if (STRCMP(*cmd, ==, "fetchQueryResultsForContext:")) {
220 if ((p = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1)) != NULL) {
221 LOG(log_info, logtype_sl, "fetchQueryResultsForContext: 0x%" PRIx64, *p);
225 /* Spotlight RPC status code ? 0 in all traces, we use 0xffffffff for an error, never seen from Apple */
229 memset(rbuf, 0xff, 4);
233 EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
243 talloc_free(tmp_ctx);
251 /**************************************************************************************************
253 **************************************************************************************************/
255 #ifdef SPOT_TEST_MAIN
257 int main(int argc, char **argv)
260 TALLOC_CTX *mem_ctx = talloc_new(NULL);
261 DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
264 set_processname("spot");
265 setuplog("default:info,spotlight:debug", "/dev/tty");
267 LOG(log_info, logtype_sl, "Start");
271 dalloc_add(dd, &i, uint64_t);
274 dalloc_add(dd, &i, uint64_t);
277 char *str = talloc_strdup(dd, "hello world");
278 dalloc_add(dd, &str, char *);
281 dalloc_add(dd, &b, sl_bool_t);
284 dalloc_add(dd, &b, sl_bool_t);
287 /* add a nested array */
288 DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
290 dalloc_add(nested, &i, uint64_t);
291 dalloc_add(dd, nested, DALLOC_CTX);
293 /* test an allocated CNID array */
295 sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
297 cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
302 dalloc_add(cnids->ca_cnids, &id, uint64_t);
303 dalloc_add(dd, cnids, sl_cnids_t);
307 /* Now the Spotlight types */
308 sl_array_t *sl_arrary = talloc_zero(dd, sl_array_t);
310 dalloc_add(sl_arrary, &i, uint64_t);
312 sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
314 dalloc_add(sl_dict, &i, uint64_t);
315 dalloc_add(sl_arrary, sl_dict, sl_dict_t);
317 dalloc_add(dd, sl_arrary, sl_array_t);
320 /* now parse a real spotlight packet */
327 EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
329 EC_NEG1_LOG( fd = open("openQuery-packet.bin", O_RDONLY) );
330 EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
332 EC_NEG1_LOG( sl_unpack(query, ibuf + 24) );
334 /* Now dump the whole thing */
339 char buf[MAX_SLQ_DAT];
340 EC_NEG1_LOG( qlen = sl_pack(query, buf) );
342 EC_NEG1_LOG( fd = open("test.bin", O_RDWR) );
343 lseek(fd, 24, SEEK_SET);
344 write(fd, buf, qlen);
350 talloc_free(mem_ctx);