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"
38 /**************************************************************************************************
39 * RPC data marshalling and unmarshalling
40 **************************************************************************************************/
42 /* FPSpotlightRPC subcommand codes */
43 #define SPOTLIGHT_CMD_VOLPATH 1
44 #define SPOTLIGHT_CMD_FLAGS 2
45 #define SPOTLIGHT_CMD_RPC 3
47 /* Spotlight epoch is UNIX epoch minus SPOTLIGHT_TIME_DELTA */
48 #define SPOTLIGHT_TIME_DELTA INT64_C(280878921600U)
50 #define SQ_TYPE_NULL 0x0000
51 #define SQ_TYPE_COMPLEX 0x0200
52 #define SQ_TYPE_INT64 0x8400
53 #define SQ_TYPE_BOOL 0x0100
54 #define SQ_TYPE_FLOAT 0x8500
55 #define SQ_TYPE_DATA 0x0700
56 #define SQ_TYPE_CNIDS 0x8700
57 #define SQ_TYPE_UUID 0x0e00
58 #define SQ_TYPE_DATE 0x8600
59 #define SQ_TYPE_TOC 0x8800
61 #define SQ_CPX_TYPE_ARRAY 0x0a00
62 #define SQ_CPX_TYPE_STRING 0x0c00
63 #define SQ_CPX_TYPE_UTF16_STRING 0x1c00
64 #define SQ_CPX_TYPE_DICT 0x0d00
65 #define SQ_CPX_TYPE_CNIDS 0x1a00
66 #define SQ_CPX_TYPE_FILEMETA 0x1b00
68 #define SUBQ_SAFETY_LIM 20
70 /* Can be ored and used as flags */
71 #define SL_ENC_LITTLE_ENDIAN 1
72 #define SL_ENC_BIG_ENDIAN 2
73 #define SL_ENC_UTF_16 4
75 /* Forward declarations */
76 static int dissect_spotlight(DALLOC_CTX *query, const char *buf);
77 static int sl_pack_loop(DALLOC_CTX *query, char *buf, int offset, char *toc_buf, int *toc_idx);
79 /* Helper functions and stuff */
80 static const char *neststrings[] = {
90 static int dd_dump(DALLOC_CTX *dd, int nestinglevel)
94 LOG(log_debug, logtype_sl, "%s%s(#%d): {",
95 neststrings[nestinglevel], talloc_get_name(dd), talloc_array_length(dd->dd_talloc_array));
97 for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
99 type = talloc_get_name(dd->dd_talloc_array[n]);
101 if (STRCMP(type, ==, "DALLOC_CTX")
102 || STRCMP(type, ==, "sl_array_t")
103 || STRCMP(type, ==, "sl_dict_t")) {
104 dd_dump(dd->dd_talloc_array[n], nestinglevel + 1);
105 } else if (STRCMP(type, ==, "uint64_t")) {
107 memcpy(&i, dd->dd_talloc_array[n], sizeof(uint64_t));
108 LOG(log_debug, logtype_sl, "%suint64_t: 0x%04x", neststrings[nestinglevel + 1], i);
109 } else if (STRCMP(type, ==, "int64_t")) {
111 memcpy(&i, dd->dd_talloc_array[n], sizeof(int64_t));
112 LOG(log_debug, logtype_sl, "%sint64_t: %" PRId64, neststrings[nestinglevel + 1], i);
113 } else if (STRCMP(type, ==, "uint32_t")) {
115 memcpy(&i, dd->dd_talloc_array[n], sizeof(uint32_t));
116 LOG(log_debug, logtype_sl, "%s%s: %" PRIu32, neststrings[nestinglevel + 1], type, i);
117 } else if (STRCMP(type, ==, "char *")) {
119 memcpy(&s, dd->dd_talloc_array[n], sizeof(char *));
120 LOG(log_debug, logtype_sl, "%sstring: %s", neststrings[nestinglevel + 1], s);
121 } else if (STRCMP(type, ==, "sl_bool_t")) {
123 memcpy(&bl, dd->dd_talloc_array[n], sizeof(sl_bool_t));
124 LOG(log_debug, logtype_sl, "%sbool: %s", neststrings[nestinglevel + 1], bl ? "true" : "false");
125 } else if (STRCMP(type, ==, "sl_cnids_t")) {
127 memcpy(&cnids, dd->dd_talloc_array[n], sizeof(sl_cnids_t));
128 LOG(log_debug, logtype_sl, "%sCNIDs: unkn1: %" PRIu16 ", unkn2: %" PRIu32,
129 neststrings[nestinglevel + 1], cnids.ca_unkn1, cnids.ca_context);
131 dd_dump(cnids.ca_cnids, nestinglevel + 1);
134 LOG(log_debug, logtype_sl, "%s}", neststrings[nestinglevel]);
138 * Returns the UTF-16 string encoding, by checking the 2-byte byte order mark.
139 * If there is no byte order mark, -1 is returned.
141 static uint spotlight_get_utf16_string_encoding(const char *buf, int offset, int query_length, uint encoding) {
144 /* check for byte order mark */
145 utf16_encoding = SL_ENC_BIG_ENDIAN;
146 if (query_length >= 2) {
147 uint16_t byte_order_mark;
148 if (encoding == SL_ENC_LITTLE_ENDIAN)
149 byte_order_mark = SVAL(buf, offset);
151 byte_order_mark = RSVAL(buf, offset);
153 if (byte_order_mark == 0xFFFE) {
154 utf16_encoding = SL_ENC_BIG_ENDIAN | SL_ENC_UTF_16;
156 else if (byte_order_mark == 0xFEFF) {
157 utf16_encoding = SL_ENC_LITTLE_ENDIAN | SL_ENC_UTF_16;
161 return utf16_encoding;
164 /**************************************************************************************************
165 * marshalling functions
166 **************************************************************************************************/
168 #define SL_OFFSET_DELTA 16
170 static uint64_t sl_pack_tag(uint16_t type, uint16_t size_or_count, uint32_t val)
172 uint64_t tag = ((uint64_t)val << 32) | ((uint64_t)type << 16) | size_or_count;
176 static int sl_pack_float(double d, char *buf, int offset)
183 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_FLOAT, 2, 1));
184 SLVAL(buf, offset + 8, ieee_fp_union.w);
186 return offset + 2 * sizeof(uint64_t);
189 static int sl_pack_uint64(uint64_t u, char *buf, int offset)
191 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_INT64, 2, 1));
192 SLVAL(buf, offset + 8, u);
194 return offset + 2 * sizeof(uint64_t);
197 static int sl_pack_bool(sl_bool_t bl, char *buf, int offset)
199 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_BOOL, 1, bl ? 1 : 0));
201 return offset + sizeof(uint64_t);
204 static int sl_pack_nil(char *buf, int offset)
206 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_NULL, 1, 1));
208 return offset + sizeof(uint64_t);
211 static int sl_pack_date(sl_time_t t, char *buf, int offset)
215 data = (t.tv_sec + SPOTLIGHT_TIME_DELTA) << 24;
217 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_DATE, 2, 1));
218 SLVAL(buf, offset + 8, data);
220 return offset + 2 * sizeof(uint64_t);
223 static int sl_pack_uuid(sl_uuid_t *uuid, char *buf, int offset)
225 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_UUID, 3, 1));
226 memcpy(buf + offset + 8, uuid, 16);
228 return offset + sizeof(uint64_t) + 16;
231 static int sl_pack_CNID(sl_cnids_t *cnids, char *buf, int offset, char *toc_buf, int *toc_idx)
233 int len = 0, off = 0;
234 int cnid_count = talloc_array_length(cnids->ca_cnids);
236 SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_CNIDS, (offset + SL_OFFSET_DELTA) / 8, cnid_count));
237 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
241 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_CNIDS, 2 + cnid_count, 8 /* unknown meaning, but always 8 */));
244 if (cnid_count > 0) {
245 SLVAL(buf, offset, sl_pack_tag(0x0add, cnid_count, cnids->ca_context));
248 for (int i = 0; i < cnid_count; i++) {
249 SLVAL(buf, offset, cnids->ca_cnids->dd_talloc_array[i]);
257 static int sl_pack_array(sl_array_t *array, char *buf, int offset, char *toc_buf, int *toc_idx)
259 int count = talloc_array_length(array->dd_talloc_array);
260 int octets = (offset + SL_OFFSET_DELTA) / 8;
262 LOG(log_maxdebug, logtype_sl, "sl_pack_array: count: %d, offset:%d, octets: %d", count, offset, octets);
264 SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_ARRAY, octets, count));
265 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
269 offset = sl_pack_loop(array, buf, offset, toc_buf, toc_idx);
274 static int sl_pack_dict(sl_array_t *dict, char *buf, int offset, char *toc_buf, int *toc_idx)
276 SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_DICT, (offset + SL_OFFSET_DELTA) / 8, talloc_array_length(dict->dd_talloc_array)));
277 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
281 offset = sl_pack_loop(dict, buf, offset, toc_buf, toc_idx);
286 static int sl_pack_string(char **string, char *buf, int offset, char *toc_buf, int *toc_idx)
288 int len, octets, used_in_last_octet;
291 octets = (len / 8) + (len & 7 ? 1 : 0);
292 used_in_last_octet = 8 - (octets * 8 - len);
294 LOG(log_maxdebug, logtype_sl, "sl_pack_string(\"%s\"): len: %d, octets: %d, used_in_last_octet: %d",
295 s, len, octets, used_in_last_octet);
297 SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_STRING, (offset + SL_OFFSET_DELTA) / 8, used_in_last_octet));
298 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
302 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_DATA, octets + 1, used_in_last_octet));
305 memset(buf + offset, 0, octets * 8);
306 strncpy(buf + offset, s, len);
307 offset += octets * 8;
312 static int sl_pack_loop(DALLOC_CTX *query, char *buf, int offset, char *toc_buf, int *toc_idx)
316 for (int n = 0; n < talloc_array_length(query->dd_talloc_array); n++) {
318 type = talloc_get_name(query->dd_talloc_array[n]);
320 if (STRCMP(type, ==, "sl_array_t")) {
321 offset = sl_pack_array(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
322 } else if (STRCMP(type, ==, "sl_dict_t")) {
323 offset = sl_pack_dict(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
324 } else if (STRCMP(type, ==, "uint64_t")) {
326 memcpy(&i, query->dd_talloc_array[n], sizeof(uint64_t));
327 offset = sl_pack_uint64(i, buf, offset);
328 } else if (STRCMP(type, ==, "char *")) {
329 offset = sl_pack_string(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
330 } else if (STRCMP(type, ==, "sl_bool_t")) {
332 memcpy(&bl, query->dd_talloc_array[n], sizeof(sl_bool_t));
333 offset = sl_pack_bool(bl, buf, offset);
334 } else if (STRCMP(type, ==, "double")) {
336 memcpy(&d, query->dd_talloc_array[n], sizeof(double));
337 offset = sl_pack_float(d, buf, offset);
338 } else if (STRCMP(type, ==, "sl_nil_t")) {
339 offset = sl_pack_nil(buf, offset);
340 } else if (STRCMP(type, ==, "sl_time_t")) {
342 memcpy(&t, query->dd_talloc_array[n], sizeof(sl_time_t));
343 offset = sl_pack_date(t, buf, offset);
344 } else if (STRCMP(type, ==, "sl_uuid_t")) {
345 offset = sl_pack_uuid(query->dd_talloc_array[n], buf, offset);
346 } else if (STRCMP(type, ==, "sl_cnids_t")) {
347 offset = sl_pack_CNID(query->dd_talloc_array[n], buf, offset, toc_buf, toc_idx);
354 #define MAX_SLQ_DAT 65000
355 #define MAX_SLQ_TOC 2048
357 static int sl_pack(DALLOC_CTX *query, char *buf)
360 char toc_buf[MAX_SLQ_TOC];
364 memcpy(buf, "432130dm", 8);
365 EC_NEG1_LOG( len = sl_pack_loop(query, buf + 16, 0, toc_buf + 8, &toc_index) );
366 SIVAL(buf, 8, len / 8 + 1 + toc_index + 1);
367 SIVAL(buf, 12, len / 8 + 1);
369 SLVAL(toc_buf, 0, sl_pack_tag(SQ_TYPE_TOC, toc_index + 1, 0));
370 memcpy(buf + 16 + len, toc_buf, (toc_index + 1 ) * 8);
372 len += 16 + (toc_index + 1 ) * 8;
380 /**************************************************************************************************
381 * unmarshalling functions
382 **************************************************************************************************/
384 static uint64_t sl_unpack_uint64(const char *buf, int offset, uint encoding)
386 if (encoding == SL_ENC_LITTLE_ENDIAN)
387 return LVAL(buf, offset);
389 return RLVAL(buf, offset);
392 static int sl_unpack_ints(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
395 uint64_t query_data64;
397 query_data64 = sl_unpack_uint64(buf, offset, encoding);
398 count = query_data64 >> 32;
402 while (i++ < count) {
403 query_data64 = sl_unpack_uint64(buf, offset, encoding);
404 dalloc_add(query, &query_data64, uint64_t);
411 static int sl_unpack_date(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
414 uint64_t query_data64;
417 query_data64 = sl_unpack_uint64(buf, offset, encoding);
418 count = query_data64 >> 32;
422 while (i++ < count) {
423 query_data64 = sl_unpack_uint64(buf, offset, encoding) >> 24;
424 t.tv_sec = query_data64 - SPOTLIGHT_TIME_DELTA;
426 dalloc_add(query, &t, sl_time_t);
433 static int sl_unpack_uuid(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
436 uint64_t query_data64;
438 query_data64 = sl_unpack_uint64(buf, offset, encoding);
439 count = query_data64 >> 32;
443 while (i++ < count) {
444 memcpy(uuid.sl_uuid, buf + offset, 16);
445 dalloc_add(query, &uuid, sl_uuid_t);
452 static int sl_unpack_floats(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
455 uint64_t query_data64;
462 query_data64 = sl_unpack_uint64(buf, offset, encoding);
463 count = query_data64 >> 32;
467 while (i++ < count) {
468 if (encoding == SL_ENC_LITTLE_ENDIAN) {
469 #ifdef WORDS_BIGENDIAN
470 ieee_fp_union.w[0] = IVAL(buf, offset + 4);
471 ieee_fp_union.w[1] = IVAL(buf, offset);
473 ieee_fp_union.w[0] = IVAL(buf, offset);
474 ieee_fp_union.w[1] = IVAL(buf, offset + 4);
477 #ifdef WORDS_BIGENDIAN
478 ieee_fp_union.w[0] = RIVAL(buf, offset);
479 ieee_fp_union.w[1] = RIVAL(buf, offset + 4);
481 ieee_fp_union.w[0] = RIVAL(buf, offset + 4);
482 ieee_fp_union.w[1] = RIVAL(buf, offset);
485 dalloc_add(query, &ieee_fp_union.d, double);
492 static int sl_unpack_CNID(DALLOC_CTX *query, const char *buf, int offset, int length, uint encoding)
496 uint64_t query_data64;
499 EC_NULL( cnids.ca_cnids = talloc_zero(query, DALLOC_CTX) );
502 /* that's permitted, it's an empty array */
505 query_data64 = sl_unpack_uint64(buf, offset, encoding);
506 count = query_data64 & 0xffff;
508 cnids.ca_unkn1 = (query_data64 & 0xffff0000) >> 16;
509 cnids.ca_context = query_data64 >> 32;
514 query_data64 = sl_unpack_uint64(buf, offset, encoding);
515 dalloc_add(cnids.ca_cnids, &query_data64, uint64_t);
519 dalloc_add(query, &cnids, sl_cnids_t);
525 static const char *spotlight_get_qtype_string(uint64_t query_type)
527 switch (query_type) {
530 case SQ_TYPE_COMPLEX:
547 static const char *spotlight_get_cpx_qtype_string(uint64_t cpx_query_type)
549 switch (cpx_query_type) {
550 case SQ_CPX_TYPE_ARRAY:
552 case SQ_CPX_TYPE_STRING:
554 case SQ_CPX_TYPE_UTF16_STRING:
555 return "utf-16 string";
556 case SQ_CPX_TYPE_DICT:
558 case SQ_CPX_TYPE_CNIDS:
560 case SQ_CPX_TYPE_FILEMETA:
567 static int spotlight_dissect_loop(DALLOC_CTX *query,
571 const uint toc_offset,
575 int i, toc_index, query_length;
576 uint subcount, cpx_query_type, cpx_query_count;
577 uint64_t query_data64, query_type;
578 uint unicode_encoding;
583 while (count > 0 && (offset < toc_offset)) {
584 query_data64 = sl_unpack_uint64(buf, offset, encoding);
585 query_length = (query_data64 & 0xffff) * 8;
586 query_type = (query_data64 & 0xffff0000) >> 16;
587 if (query_length == 0)
590 switch (query_type) {
591 case SQ_TYPE_COMPLEX:
592 toc_index = (query_data64 >> 32) - 1;
593 query_data64 = sl_unpack_uint64(buf, toc_offset + toc_index * 8, encoding);
594 cpx_query_type = (query_data64 & 0xffff0000) >> 16;
595 cpx_query_count = query_data64 >> 32;
597 switch (cpx_query_type) {
598 case SQ_CPX_TYPE_ARRAY: {
599 sl_array_t *sl_arrary = talloc_zero(query, sl_array_t);
600 EC_NEG1_LOG( offset = spotlight_dissect_loop(sl_arrary, buf, offset + 8, cpx_query_count, toc_offset, encoding) );
601 dalloc_add(query, sl_arrary, sl_array_t);
605 case SQ_CPX_TYPE_DICT: {
606 sl_dict_t *sl_dict = talloc_zero(query, sl_dict_t);
607 EC_NEG1_LOG( offset = spotlight_dissect_loop(sl_dict, buf, offset + 8, cpx_query_count, toc_offset, encoding) );
608 dalloc_add(query, sl_dict, sl_dict_t);
611 case SQ_CPX_TYPE_STRING:
612 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
613 query_length += (query_data64 & 0xffff) * 8;
614 if ((padding = 8 - (query_data64 >> 32)) < 0)
616 if ((slen = query_length - 16 - padding) < 1)
618 p = talloc_strndup(query, buf + offset + 16, slen);
619 dalloc_add(query, &p, char *);
622 case SQ_CPX_TYPE_UTF16_STRING:
623 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
624 query_length += (query_data64 & 0xffff) * 8;
625 if ((padding = 8 - (query_data64 >> 32)) < 0)
627 if ((slen = query_length - 16 - padding) < 1)
630 unicode_encoding = spotlight_get_utf16_string_encoding(buf, offset + 16, slen, encoding);
631 mark_exists = (unicode_encoding & SL_ENC_UTF_16);
632 unicode_encoding &= ~SL_ENC_UTF_16;
634 EC_NEG1( convert_string_allocate(CH_UCS2, CH_UTF8, buf + offset + (mark_exists ? 18 : 16), slen, &p) );
635 dalloc_add(query, &p, char *);
638 case SQ_CPX_TYPE_FILEMETA:
639 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
640 query_length += (query_data64 & 0xffff) * 8;
642 if (query_length <= 8) {
643 EC_FAIL_LOG("SQ_CPX_TYPE_FILEMETA: query_length <= 8%s", "");
645 EC_NEG1_LOG( dissect_spotlight(query, buf + offset + 16) );
649 case SQ_CPX_TYPE_CNIDS:
650 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
651 query_length += (query_data64 & 0xffff) * 8;
652 EC_NEG1_LOG( sl_unpack_CNID(query, buf, offset + 16, query_length, encoding) );
654 } /* switch (cpx_query_type) */
660 subcount = query_data64 >> 32;
664 for (i = 0; i < subcount; i++)
665 dalloc_add(query, &nil, sl_nil_t);
670 sl_bool_t b = query_data64 >> 32;
671 dalloc_add(query, &b, sl_bool_t);
676 EC_NEG1_LOG( subcount = sl_unpack_ints(query, buf, offset, encoding) );
680 EC_NEG1_LOG( subcount = sl_unpack_uuid(query, buf, offset, encoding) );
684 EC_NEG1_LOG( subcount = sl_unpack_floats(query, buf, offset, encoding) );
688 EC_NEG1_LOG( subcount = sl_unpack_date(query, buf, offset, encoding) );
695 offset += query_length;
705 static int dissect_spotlight(DALLOC_CTX *query, const char *buf)
708 int encoding, i, toc_entries;
709 uint64_t toc_offset, tquerylen, toc_entry;
711 if (strncmp(buf, "md031234", 8) == 0)
712 encoding = SL_ENC_BIG_ENDIAN;
714 encoding = SL_ENC_LITTLE_ENDIAN;
718 toc_offset = ((sl_unpack_uint64(buf, 0, encoding) >> 32) - 1 ) * 8;
719 if (toc_offset < 0 || (toc_offset > 65000)) {
725 toc_entries = (int)(sl_unpack_uint64(buf, toc_offset, encoding) & 0xffff);
727 EC_NEG1( spotlight_dissect_loop(query, buf, 0, 1, toc_offset + 8, encoding) );
733 /**************************************************************************************************
734 * Spotlight RPC functions
735 **************************************************************************************************/
737 static int sl_rpc_fetchPropertiesForContext(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
747 EC_FAIL_LOG("sl_rpc_fetchPropertiesForContext: missing UUID for volume: %s", v->v_localname);
749 dict = talloc_zero(reply, sl_dict_t);
752 s = talloc_strdup(dict, "kMDSStoreMetaScopes");
753 dalloc_add(dict, &s, char *);
755 array = talloc_zero(dict, sl_array_t);
756 s = talloc_strdup(array, "kMDQueryScopeComputer");
757 dalloc_add(array, &s, char *);
758 dalloc_add(dict, array, sl_array_t);
761 s = talloc_strdup(dict, "kMDSStorePathScopes");
762 dalloc_add(dict, &s, char *);
764 array = talloc_zero(dict, sl_array_t);
765 s = talloc_strdup(array, v->v_path);
766 dalloc_add(array, &s, char *);
767 dalloc_add(dict, array, sl_array_t);
770 s = talloc_strdup(dict, "kMDSStoreUUID");
771 dalloc_add(dict, &s, char *);
773 memcpy(uuid.sl_uuid, v->v_uuid, 16);
774 dalloc_add(dict, &uuid, sl_uuid_t);
777 s = talloc_strdup(dict, "kMDSStoreHasPersistentUUID");
778 dalloc_add(dict, &s, char *);
780 dalloc_add(dict, &b, sl_bool_t);
782 dalloc_add(reply, dict, sl_dict_t);
788 /**************************************************************************************************
790 **************************************************************************************************/
791 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
794 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
797 int endianess = SL_ENC_LITTLE_ENDIAN;
807 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
809 if ((vol = getvolbyvid(vid)) == NULL) {
810 LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
815 /* IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
817 cmd = RIVAL(ibuf, 6);
818 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
820 /* IVAL(ibuf, 10: unknown, always 0x00000000 */
824 case SPOTLIGHT_CMD_VOLPATH: {
825 RSIVAL(rbuf, 0, ntohs(vid));
827 int len = strlen(vol->v_path) + 1;
828 strncpy(rbuf + 8, vol->v_path, len);
832 case SPOTLIGHT_CMD_FLAGS:
833 RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? */
837 case SPOTLIGHT_CMD_RPC: {
839 EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
841 EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) );
843 EC_ZERO( dissect_spotlight(query, ibuf + 22) );
847 EC_NULL_LOG( cmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
850 if (STRCMP(*cmd, ==, "fetchPropertiesForContext:")) {
851 EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) );
852 } else if (STRCMP(*cmd, ==, "fetchQueryResultsForContext:")) {
854 if ((p = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1)) != NULL) {
855 LOG(log_info, logtype_sl, "fetchQueryResultsForContext: 0x%" PRIx64, *p);
859 /* Spotlight RPC status code ? 0 in all traces, we use 0xffffffff for an error, never seen from Apple */
863 memset(rbuf, 0xff, 4);
867 EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
874 talloc_free(tmp_ctx);
882 /**************************************************************************************************
884 **************************************************************************************************/
886 #ifdef SPOT_TEST_MAIN
888 int main(int argc, char **argv)
891 TALLOC_CTX *mem_ctx = talloc_new(NULL);
892 DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
895 set_processname("spot");
896 setuplog("default:info,spotlight:debug", "/dev/tty");
898 LOG(log_info, logtype_sl, "Start");
902 dalloc_add(dd, &i, int64_t);
905 dalloc_add(dd, &i, int64_t);
908 char *str = talloc_strdup(dd, "hello world");
909 dalloc_add(dd, &str, char *);
912 dalloc_add(dd, &b, sl_bool_t);
915 dalloc_add(dd, &b, sl_bool_t);
918 /* add a nested array */
919 DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
921 dalloc_add(nested, &i, int64_t);
922 dalloc_add(dd, nested, DALLOC_CTX);
924 /* test an allocated CNID array */
926 sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
928 cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
933 dalloc_add(cnids->ca_cnids, &id, uint32_t);
934 dalloc_add(dd, cnids, sl_cnids_t);
938 /* Now the Spotlight types */
939 sl_array_t *sl_arrary = talloc_zero(dd, sl_array_t);
941 dalloc_add(sl_arrary, &i, int64_t);
943 sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
945 dalloc_add(sl_dict, &i, int64_t);
946 dalloc_add(sl_arrary, sl_dict, sl_dict_t);
948 dalloc_add(dd, sl_arrary, sl_array_t);
953 /* now parse a real spotlight packet */
960 EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
962 EC_NEG1_LOG( fd = open("spotlight-packet.bin", O_RDONLY) );
963 EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
965 EC_NEG1_LOG( dissect_spotlight(query, ibuf + 24) );
967 /* Now dump the whole thing */
971 char buf[MAX_SLQ_DAT];
972 EC_NEG1_LOG( qlen = sl_pack(query, buf) );
974 EC_NEG1_LOG( fd = open("test.bin", O_RDWR) );
975 lseek(fd, 24, SEEK_SET);
976 write(fd, buf, qlen);
982 talloc_free(mem_ctx);