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, "%s1: %s(#%d): {", neststrings[nestinglevel], talloc_get_name(dd), talloc_array_length(dd->dd_talloc_array));
96 for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
98 type = talloc_get_name(dd->dd_talloc_array[n]);
100 if (STRCMP(type, ==, "DALLOC_CTX")
101 || STRCMP(type, ==, "sl_array_t")
102 || STRCMP(type, ==, "sl_dict_t")) {
103 dd_dump(dd->dd_talloc_array[n], nestinglevel + 1);
104 } else if (STRCMP(type, ==, "uint64_t")) {
106 memcpy(&i, dd->dd_talloc_array[n], sizeof(uint64_t));
107 LOG(log_debug, logtype_sl, "%s%u:\t0x%04x", neststrings[nestinglevel + 1], n + 1, i);
108 } else if (STRCMP(type, ==, "int64_t")) {
110 memcpy(&i, dd->dd_talloc_array[n], sizeof(int64_t));
111 LOG(log_debug, logtype_sl, "%s%d:\t%" PRId64, neststrings[nestinglevel + 1], n + 1, i);
112 } else if (STRCMP(type, ==, "uint32_t")) {
114 memcpy(&i, dd->dd_talloc_array[n], sizeof(uint32_t));
115 LOG(log_debug, logtype_sl, "%s%d:\t%" PRIu32, neststrings[nestinglevel + 1], n + 1, i);
116 } else if (STRCMP(type, ==, "char *")) {
118 memcpy(&s, dd->dd_talloc_array[n], sizeof(char *));
119 LOG(log_debug, logtype_sl, "%s%d:\t%s", neststrings[nestinglevel + 1], n + +1, s);
120 } else if (STRCMP(type, ==, "sl_bool_t")) {
122 memcpy(&bl, dd->dd_talloc_array[n], sizeof(sl_bool_t));
123 LOG(log_debug, logtype_sl, "%s%d:\t%s", neststrings[nestinglevel + 1], n + +1, bl ? "true" : "false");
124 } else if (STRCMP(type, ==, "sl_cnids_t")) {
126 memcpy(&cnids, dd->dd_talloc_array[n], sizeof(sl_cnids_t));
127 LOG(log_debug, logtype_sl, "%s%d:\tunkn1: %" PRIu16 ", unkn2: %" PRIu32,
128 neststrings[nestinglevel + 1], n + 1, cnids.ca_unkn1, cnids.ca_unkn2);
130 dd_dump(cnids.ca_cnids, nestinglevel + 1);
133 LOG(log_debug, logtype_sl, "%s}", neststrings[nestinglevel]);
137 * Returns the UTF-16 string encoding, by checking the 2-byte byte order mark.
138 * If there is no byte order mark, -1 is returned.
140 static uint spotlight_get_utf16_string_encoding(const char *buf, int offset, int query_length, uint encoding) {
143 /* check for byte order mark */
144 utf16_encoding = SL_ENC_BIG_ENDIAN;
145 if (query_length >= 2) {
146 uint16_t byte_order_mark;
147 if (encoding == SL_ENC_LITTLE_ENDIAN)
148 byte_order_mark = SVAL(buf, offset);
150 byte_order_mark = RSVAL(buf, offset);
152 if (byte_order_mark == 0xFFFE) {
153 utf16_encoding = SL_ENC_BIG_ENDIAN | SL_ENC_UTF_16;
155 else if (byte_order_mark == 0xFEFF) {
156 utf16_encoding = SL_ENC_LITTLE_ENDIAN | SL_ENC_UTF_16;
160 return utf16_encoding;
163 /**************************************************************************************************
164 * marshalling functions
165 **************************************************************************************************/
167 #define SL_OFFSET_DELTA 16
169 static uint64_t sl_pack_tag(uint16_t type, uint16_t size_or_count, uint32_t val)
171 uint64_t tag = ((uint64_t)val << 32) | ((uint64_t)type << 16) | size_or_count;
175 static int sl_pack_float(double d, char *buf, int offset)
182 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_FLOAT, 2, 1));
183 SLVAL(buf, offset + 8, ieee_fp_union.w);
185 return offset + 2 * sizeof(uint64_t);
188 static int sl_pack_uint64(uint64_t u, char *buf, int offset)
190 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_INT64, 2, 1));
191 SLVAL(buf, offset + 8, u);
193 return offset + 2 * sizeof(uint64_t);
196 static int sl_pack_date(sl_time_t t, char *buf, int offset)
200 data = (t.tv_sec + SPOTLIGHT_TIME_DELTA) << 24;
202 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_DATE, 2, 1));
203 SLVAL(buf, offset + 8, data);
205 return offset + 2 * sizeof(uint64_t);
208 static int sl_pack_uuid(sl_uuid_t uuid, char *buf, int offset)
210 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_UUID, 3, 1));
211 memcpy(buf + offset + 8, &uuid, 16);
213 return offset + sizeof(uint64_t) + 16;
216 static int sl_pack_CNID(sl_cnids_t *cnids, uint32_t context, char *buf, int offset, char *toc_buf, int *toc_idx)
218 int len = 0, off = 0;
219 int cnid_count = talloc_array_length(cnids->ca_cnids);
221 SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_CNIDS, (offset + SL_OFFSET_DELTA) / 8, cnid_count));
222 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx));
226 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_CNIDS, 2 + cnid_count, 8 /* unknown meaning, but always 8 */));
229 SLVAL(buf, offset, sl_pack_tag(0x0add, cnid_count, context));
232 for (int i = 0; i < cnid_count; i++) {
233 SLVAL(buf, offset, cnids->ca_cnids->dd_talloc_array[i]);
240 static int sl_pack_array(sl_array_t *array, char *buf, int offset, char *toc_buf, int *toc_idx)
242 SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_ARRAY, (offset + SL_OFFSET_DELTA) / 8, talloc_array_length(array->dd_talloc_array)));
243 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx));
247 offset = sl_pack_loop(array, buf, offset, toc_buf, toc_idx);
252 static int sl_pack_dict(sl_array_t *dict, char *buf, int offset, char *toc_buf, int *toc_idx)
254 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)));
255 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx));
259 offset = sl_pack_loop(dict, buf, offset, toc_buf, toc_idx);
264 static int sl_pack_string(char *s, char *buf, int offset, char *toc_buf, int *toc_idx)
266 int len, octets, used_in_last_octet;
268 octets = (len / 8) + (len & 7 ? 1 : 0);
269 used_in_last_octet = 8 - (octets * 8 - len);
271 SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_DICT, (offset + SL_OFFSET_DELTA) / 8, used_in_last_octet));
272 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx));
276 SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_DATA, octets + 1, used_in_last_octet));
279 memset(buf + offset, 0, octets * 8);
280 strncpy(buf + offset, s, len);
281 offset += octets * 8;
286 static int sl_pack_loop(DALLOC_CTX *query, char *buf, int offset, char *toc_buf, int *toc_idx)
291 /**************************************************************************************************
292 * unmarshalling functions
293 **************************************************************************************************/
295 static uint64_t sl_unpack_uint64(const char *buf, int offset, uint encoding)
297 if (encoding == SL_ENC_LITTLE_ENDIAN)
298 return LVAL(buf, offset);
300 return RLVAL(buf, offset);
303 static int sl_unpack_ints(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
306 uint64_t query_data64;
308 query_data64 = sl_unpack_uint64(buf, offset, encoding);
309 count = query_data64 >> 32;
313 while (i++ < count) {
314 query_data64 = sl_unpack_uint64(buf, offset, encoding);
315 dalloc_add(query, &query_data64, uint64_t);
322 static int sl_unpack_date(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
325 uint64_t query_data64;
328 query_data64 = sl_unpack_uint64(buf, offset, encoding);
329 count = query_data64 >> 32;
333 while (i++ < count) {
334 query_data64 = sl_unpack_uint64(buf, offset, encoding) >> 24;
335 t.tv_sec = query_data64 - SPOTLIGHT_TIME_DELTA;
337 dalloc_add(query, &t, sl_time_t);
344 static int sl_unpack_uuid(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
347 uint64_t query_data64;
349 query_data64 = sl_unpack_uint64(buf, offset, encoding);
350 count = query_data64 >> 32;
354 while (i++ < count) {
355 memcpy(uuid.sl_uuid, buf + offset, 16);
356 dalloc_add(query, &uuid, sl_uuid_t);
363 static int sl_unpack_floats(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
366 uint64_t query_data64;
373 query_data64 = sl_unpack_uint64(buf, offset, encoding);
374 count = query_data64 >> 32;
378 while (i++ < count) {
379 if (encoding == SL_ENC_LITTLE_ENDIAN) {
380 #ifdef WORDS_BIGENDIAN
381 ieee_fp_union.w[0] = IVAL(buf, offset + 4);
382 ieee_fp_union.w[1] = IVAL(buf, offset);
384 ieee_fp_union.w[0] = IVAL(buf, offset);
385 ieee_fp_union.w[1] = IVAL(buf, offset + 4);
388 #ifdef WORDS_BIGENDIAN
389 ieee_fp_union.w[0] = RIVAL(buf, offset);
390 ieee_fp_union.w[1] = RIVAL(buf, offset + 4);
392 ieee_fp_union.w[0] = RIVAL(buf, offset + 4);
393 ieee_fp_union.w[1] = RIVAL(buf, offset);
396 dalloc_add(query, &ieee_fp_union.d, double);
403 static int sl_unpack_CNID(DALLOC_CTX *query, const char *buf, int offset, int length, uint encoding)
407 uint64_t query_data64;
410 EC_NULL( cnids.ca_cnids = talloc_zero(query, DALLOC_CTX) );
413 /* that's permitted, it's an empty array */
416 query_data64 = sl_unpack_uint64(buf, offset, encoding);
417 count = query_data64 & 0xffff;
419 cnids.ca_unkn1 = (query_data64 & 0xffff0000) >> 16;
420 cnids.ca_unkn2 = query_data64 >> 32;
425 query_data64 = sl_unpack_uint64(buf, offset, encoding);
426 dalloc_add(cnids.ca_cnids, &query_data64, uint64_t);
430 dalloc_add(query, &cnids, sl_cnids_t);
436 static const char *spotlight_get_qtype_string(uint64_t query_type)
438 switch (query_type) {
441 case SQ_TYPE_COMPLEX:
458 static const char *spotlight_get_cpx_qtype_string(uint64_t cpx_query_type)
460 switch (cpx_query_type) {
461 case SQ_CPX_TYPE_ARRAY:
463 case SQ_CPX_TYPE_STRING:
465 case SQ_CPX_TYPE_UTF16_STRING:
466 return "utf-16 string";
467 case SQ_CPX_TYPE_DICT:
469 case SQ_CPX_TYPE_CNIDS:
471 case SQ_CPX_TYPE_FILEMETA:
478 static int spotlight_dissect_loop(DALLOC_CTX *query,
482 const uint toc_offset,
486 int i, toc_index, query_length;
487 uint subcount, cpx_query_type, cpx_query_count;
488 uint64_t query_data64, query_type;
489 uint unicode_encoding;
494 while (count > 0 && (offset < toc_offset)) {
495 query_data64 = sl_unpack_uint64(buf, offset, encoding);
496 query_length = (query_data64 & 0xffff) * 8;
497 query_type = (query_data64 & 0xffff0000) >> 16;
498 if (query_length == 0)
501 switch (query_type) {
502 case SQ_TYPE_COMPLEX:
503 toc_index = (query_data64 >> 32) - 1;
504 query_data64 = sl_unpack_uint64(buf, toc_offset + toc_index * 8, encoding);
505 cpx_query_type = (query_data64 & 0xffff0000) >> 16;
506 cpx_query_count = query_data64 >> 32;
508 switch (cpx_query_type) {
509 case SQ_CPX_TYPE_ARRAY: {
510 sl_array_t *sl_arrary = talloc_zero(query, sl_array_t);
511 EC_NEG1_LOG( offset = spotlight_dissect_loop(sl_arrary, buf, offset + 8, cpx_query_count, toc_offset, encoding) );
512 dalloc_add(query, sl_arrary, sl_array_t);
516 case SQ_CPX_TYPE_DICT: {
517 sl_dict_t *sl_dict = talloc_zero(query, sl_dict_t);
518 EC_NEG1_LOG( offset = spotlight_dissect_loop(sl_dict, buf, offset + 8, cpx_query_count, toc_offset, encoding) );
519 dalloc_add(query, sl_dict, sl_dict_t);
522 case SQ_CPX_TYPE_STRING:
523 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
524 query_length += (query_data64 & 0xffff) * 8;
525 if ((padding = 8 - (query_data64 >> 32)) < 0)
527 if ((slen = query_length - 16 - padding) < 1)
529 p = talloc_strndup(query, buf + offset + 16, slen);
530 dalloc_add(query, &p, char *);
533 case SQ_CPX_TYPE_UTF16_STRING:
534 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
535 query_length += (query_data64 & 0xffff) * 8;
536 if ((padding = 8 - (query_data64 >> 32)) < 0)
538 if ((slen = query_length - 16 - padding) < 1)
541 unicode_encoding = spotlight_get_utf16_string_encoding(buf, offset + 16, slen, encoding);
542 mark_exists = (unicode_encoding & SL_ENC_UTF_16);
543 unicode_encoding &= ~SL_ENC_UTF_16;
545 EC_NEG1( convert_string_allocate(CH_UCS2, CH_UTF8, buf + offset + (mark_exists ? 18 : 16), slen, &p) );
546 dalloc_add(query, &p, char *);
549 case SQ_CPX_TYPE_FILEMETA:
550 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
551 query_length += (query_data64 & 0xffff) * 8;
553 if (query_length <= 8) {
554 EC_FAIL_LOG("SQ_CPX_TYPE_FILEMETA: query_length <= 8%s", "");
556 EC_NEG1_LOG( dissect_spotlight(query, buf + offset + 16) );
560 case SQ_CPX_TYPE_CNIDS:
561 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
562 query_length += (query_data64 & 0xffff) * 8;
563 EC_NEG1_LOG( sl_unpack_CNID(query, buf, offset + 16, query_length, encoding) );
565 } /* switch (cpx_query_type) */
571 subcount = query_data64 >> 32;
575 for (i = 0; i < subcount; i++)
576 dalloc_add(query, &nil, sl_nil_t);
581 sl_bool_t b = query_data64 >> 32;
582 dalloc_add(query, &b, sl_bool_t);
587 EC_NEG1_LOG( subcount = sl_unpack_ints(query, buf, offset, encoding) );
591 EC_NEG1_LOG( subcount = sl_unpack_uuid(query, buf, offset, encoding) );
595 EC_NEG1_LOG( subcount = sl_unpack_floats(query, buf, offset, encoding) );
599 EC_NEG1_LOG( subcount = sl_unpack_date(query, buf, offset, encoding) );
606 offset += query_length;
616 static int dissect_spotlight(DALLOC_CTX *query, const char *buf)
619 int encoding, i, toc_entries;
620 uint64_t toc_offset, tquerylen, toc_entry;
622 if (strncmp(buf, "md031234", 8) == 0)
623 encoding = SL_ENC_BIG_ENDIAN;
625 encoding = SL_ENC_LITTLE_ENDIAN;
629 toc_offset = ((sl_unpack_uint64(buf, 0, encoding) >> 32) - 1 ) * 8;
630 if (toc_offset < 0 || (toc_offset > 65000)) {
636 toc_entries = (int)(sl_unpack_uint64(buf, toc_offset, encoding) & 0xffff);
638 EC_NEG1( spotlight_dissect_loop(query, buf, 0, 1, toc_offset + 8, encoding) );
644 /**************************************************************************************************
646 **************************************************************************************************/
647 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
650 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
653 int endianess = SL_ENC_LITTLE_ENDIAN;
663 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
665 if ((vol = getvolbyvid(vid)) == NULL) {
666 LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
671 /* IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
673 cmd = RIVAL(ibuf, 6);
674 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
676 /* IVAL(ibuf, 10: unknown, always 0x00000000 */
680 case SPOTLIGHT_CMD_VOLPATH: {
681 RSIVAL(rbuf, 0, ntohs(vid));
683 int len = strlen(vol->v_path) + 1;
684 strncpy(rbuf + 8, vol->v_path, len);
688 case SPOTLIGHT_CMD_FLAGS:
689 RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? */
693 case SPOTLIGHT_CMD_RPC: {
695 EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
696 (void)dissect_spotlight(query, ibuf + 22);
703 talloc_free(tmp_ctx);
710 /**************************************************************************************************
712 **************************************************************************************************/
714 #ifdef SPOT_TEST_MAIN
716 int main(int argc, char **argv)
719 TALLOC_CTX *mem_ctx = talloc_new(NULL);
720 DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
723 set_processname("spot");
724 setuplog("default:info,spotlight:debug", "/dev/tty");
726 LOG(log_info, logtype_sl, "Start");
730 dalloc_add(dd, &i, int64_t);
733 dalloc_add(dd, &i, int64_t);
736 char *str = talloc_strdup(dd, "hello world");
737 dalloc_add(dd, &str, char *);
740 dalloc_add(dd, &b, sl_bool_t);
743 dalloc_add(dd, &b, sl_bool_t);
746 /* add a nested array */
747 DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
749 dalloc_add(nested, &i, int64_t);
750 dalloc_add(dd, nested, DALLOC_CTX);
752 /* test an allocated CNID array */
754 sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
756 cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
761 dalloc_add(cnids->ca_cnids, &id, uint32_t);
762 dalloc_add(dd, cnids, sl_cnids_t);
764 /* Now the Spotlight types */
765 sl_array_t *sl_arrary = talloc_zero(dd, sl_array_t);
767 dalloc_add(sl_arrary, &i, int64_t);
769 sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
771 dalloc_add(sl_dict, &i, int64_t);
772 dalloc_add(sl_arrary, sl_dict, sl_dict_t);
774 dalloc_add(dd, sl_arrary, sl_array_t);
777 /* now parse a real spotlight packet */
784 EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
786 EC_NEG1_LOG( fd = open("spotlight-packet2.bin", O_RDONLY) );
787 EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
788 EC_NEG1_LOG( dissect_spotlight(query, ibuf + 24) );
790 /* Now dump the whole thing */
795 talloc_free(mem_ctx);