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, ==, "int64_t")) {
71 memcpy(&i, dd->dd_talloc_array[n], sizeof(int64_t));
72 LOG(log_debug, logtype_sl, "%sint64_t: %" PRId64, neststrings[nestinglevel + 1], i);
73 } else if (STRCMP(type, ==, "uint32_t")) {
75 memcpy(&i, dd->dd_talloc_array[n], sizeof(uint32_t));
76 LOG(log_debug, logtype_sl, "%s%s: %" PRIu32, neststrings[nestinglevel + 1], type, i);
77 } else if (STRCMP(type, ==, "char *")) {
79 memcpy(&s, dd->dd_talloc_array[n], sizeof(char *));
80 LOG(log_debug, logtype_sl, "%sstring: %s", neststrings[nestinglevel + 1], s);
81 } else if (STRCMP(type, ==, "sl_bool_t")) {
83 memcpy(&bl, dd->dd_talloc_array[n], sizeof(sl_bool_t));
84 LOG(log_debug, logtype_sl, "%sbool: %s", neststrings[nestinglevel + 1], bl ? "true" : "false");
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: %" PRIu16 ", unkn2: %" PRIu32,
89 neststrings[nestinglevel + 1], cnids.ca_unkn1, cnids.ca_context);
91 dd_dump(cnids.ca_cnids, nestinglevel + 1);
94 LOG(log_debug, logtype_sl, "%s}", neststrings[nestinglevel]);
97 /**************************************************************************************************
98 * Spotlight RPC functions
99 **************************************************************************************************/
101 static int sl_rpc_fetchPropertiesForContext(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
111 EC_FAIL_LOG("sl_rpc_fetchPropertiesForContext: missing UUID for volume: %s", v->v_localname);
113 dict = talloc_zero(reply, sl_dict_t);
116 s = talloc_strdup(dict, "kMDSStoreMetaScopes");
117 dalloc_add(dict, &s, char *);
119 array = talloc_zero(dict, sl_array_t);
120 s = talloc_strdup(array, "kMDQueryScopeComputer");
121 dalloc_add(array, &s, char *);
122 dalloc_add(dict, array, sl_array_t);
125 s = talloc_strdup(dict, "kMDSStorePathScopes");
126 dalloc_add(dict, &s, char *);
128 array = talloc_zero(dict, sl_array_t);
129 s = talloc_strdup(array, v->v_path);
130 dalloc_add(array, &s, char *);
131 dalloc_add(dict, array, sl_array_t);
134 s = talloc_strdup(dict, "kMDSStoreUUID");
135 dalloc_add(dict, &s, char *);
137 memcpy(uuid.sl_uuid, v->v_uuid, 16);
138 dalloc_add(dict, &uuid, sl_uuid_t);
141 s = talloc_strdup(dict, "kMDSStoreHasPersistentUUID");
142 dalloc_add(dict, &s, char *);
144 dalloc_add(dict, &b, sl_bool_t);
146 dalloc_add(reply, dict, sl_dict_t);
152 /**************************************************************************************************
154 **************************************************************************************************/
155 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
158 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
161 int endianess = SL_ENC_LITTLE_ENDIAN;
171 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
173 if ((vol = getvolbyvid(vid)) == NULL) {
174 LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
179 /* IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
181 cmd = RIVAL(ibuf, 6);
182 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
184 /* IVAL(ibuf, 10: unknown, always 0x00000000 */
188 case SPOTLIGHT_CMD_OPEN:
189 case SPOTLIGHT_CMD_OPEN2: {
190 RSIVAL(rbuf, 0, ntohs(vid));
192 int len = strlen(vol->v_path) + 1;
193 strncpy(rbuf + 8, vol->v_path, len);
197 case SPOTLIGHT_CMD_FLAGS:
198 RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? Helios uses 0x1eefface */
202 case SPOTLIGHT_CMD_RPC: {
204 EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
206 EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) );
208 EC_ZERO( sl_unpack(query, ibuf + 22) );
212 EC_NULL_LOG( cmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
215 if (STRCMP(*cmd, ==, "fetchPropertiesForContext:")) {
216 EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) );
217 } else if (STRCMP(*cmd, ==, "fetchQueryResultsForContext:")) {
219 if ((p = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1)) != NULL) {
220 LOG(log_info, logtype_sl, "fetchQueryResultsForContext: 0x%" PRIx64, *p);
224 /* Spotlight RPC status code ? 0 in all traces, we use 0xffffffff for an error, never seen from Apple */
228 memset(rbuf, 0xff, 4);
232 EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
242 talloc_free(tmp_ctx);
250 /**************************************************************************************************
252 **************************************************************************************************/
254 #ifdef SPOT_TEST_MAIN
256 int main(int argc, char **argv)
259 TALLOC_CTX *mem_ctx = talloc_new(NULL);
260 DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
263 set_processname("spot");
264 setuplog("default:info,spotlight:debug", "/dev/tty");
266 LOG(log_info, logtype_sl, "Start");
270 dalloc_add(dd, &i, int64_t);
273 dalloc_add(dd, &i, int64_t);
276 char *str = talloc_strdup(dd, "hello world");
277 dalloc_add(dd, &str, char *);
280 dalloc_add(dd, &b, sl_bool_t);
283 dalloc_add(dd, &b, sl_bool_t);
286 /* add a nested array */
287 DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
289 dalloc_add(nested, &i, int64_t);
290 dalloc_add(dd, nested, DALLOC_CTX);
292 /* test an allocated CNID array */
294 sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
296 cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
301 dalloc_add(cnids->ca_cnids, &id, uint32_t);
302 dalloc_add(dd, cnids, sl_cnids_t);
306 /* Now the Spotlight types */
307 sl_array_t *sl_arrary = talloc_zero(dd, sl_array_t);
309 dalloc_add(sl_arrary, &i, int64_t);
311 sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
313 dalloc_add(sl_dict, &i, int64_t);
314 dalloc_add(sl_arrary, sl_dict, sl_dict_t);
316 dalloc_add(dd, sl_arrary, sl_array_t);
321 /* now parse a real spotlight packet */
328 EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
330 EC_NEG1_LOG( fd = open("spotlight-packet.bin", O_RDONLY) );
331 EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
333 EC_NEG1_LOG( dissect_spotlight(query, ibuf + 24) );
335 /* 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);