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
60 #define SQ_CPX_TYPE_ARRAY 0x0a00
61 #define SQ_CPX_TYPE_STRING 0x0c00
62 #define SQ_CPX_TYPE_UTF16_STRING 0x1c00
63 #define SQ_CPX_TYPE_DICT 0x0d00
64 #define SQ_CPX_TYPE_CNIDS 0x1a00
65 #define SQ_CPX_TYPE_FILEMETA 0x1b00
67 #define SUBQ_SAFETY_LIM 20
69 /* Can be ored and used as flags */
70 #define SL_ENC_LITTLE_ENDIAN 1
71 #define SL_ENC_BIG_ENDIAN 2
72 #define SL_ENC_UTF_16 4
74 /* Forward declarations */
75 static int dissect_spotlight(DALLOC_CTX *query, const char *buf);
77 /* Helper functions and stuff */
78 static const char *neststrings[] = {
88 static int dd_dump(DALLOC_CTX *dd, int nestinglevel)
92 LOG(log_debug, logtype_sl, "%s1: %s(#%d): {", neststrings[nestinglevel], talloc_get_name(dd), talloc_array_length(dd->dd_talloc_array));
94 for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
96 type = talloc_get_name(dd->dd_talloc_array[n]);
98 if (STRCMP(type, ==, "DALLOC_CTX")
99 || STRCMP(type, ==, "sl_array_t")
100 || STRCMP(type, ==, "sl_dict_t")) {
101 dd_dump(dd->dd_talloc_array[n], nestinglevel + 1);
102 } else if (STRCMP(type, ==, "uint64_t")) {
104 memcpy(&i, dd->dd_talloc_array[n], sizeof(uint64_t));
105 LOG(log_debug, logtype_sl, "%s%u:\t0x%04x", neststrings[nestinglevel + 1], n + 1, i);
106 } else if (STRCMP(type, ==, "int64_t")) {
108 memcpy(&i, dd->dd_talloc_array[n], sizeof(int64_t));
109 LOG(log_debug, logtype_sl, "%s%d:\t%" PRId64, neststrings[nestinglevel + 1], n + 1, i);
110 } else if (STRCMP(type, ==, "uint32_t")) {
112 memcpy(&i, dd->dd_talloc_array[n], sizeof(uint32_t));
113 LOG(log_debug, logtype_sl, "%s%d:\t%" PRIu32, neststrings[nestinglevel + 1], n + 1, i);
114 } else if (STRCMP(type, ==, "char *")) {
116 memcpy(&s, dd->dd_talloc_array[n], sizeof(char *));
117 LOG(log_debug, logtype_sl, "%s%d:\t%s", neststrings[nestinglevel + 1], n + +1, s);
118 } else if (STRCMP(type, ==, "sl_bool_t")) {
120 memcpy(&bl, dd->dd_talloc_array[n], sizeof(sl_bool_t));
121 LOG(log_debug, logtype_sl, "%s%d:\t%s", neststrings[nestinglevel + 1], n + +1, bl ? "true" : "false");
122 } else if (STRCMP(type, ==, "sl_cnids_t")) {
124 memcpy(&cnids, dd->dd_talloc_array[n], sizeof(sl_cnids_t));
125 LOG(log_debug, logtype_sl, "%s%d:\tunkn1: %" PRIu16 ", unkn2: %" PRIu32,
126 neststrings[nestinglevel + 1], n + 1, cnids.ca_unkn1, cnids.ca_unkn2);
128 dd_dump(cnids.ca_cnids, nestinglevel + 1);
131 LOG(log_debug, logtype_sl, "%s}", neststrings[nestinglevel]);
135 * Returns the UTF-16 string encoding, by checking the 2-byte byte order mark.
136 * If there is no byte order mark, -1 is returned.
138 static uint spotlight_get_utf16_string_encoding(const char *buf, int offset, int query_length, uint encoding) {
141 /* check for byte order mark */
142 utf16_encoding = SL_ENC_BIG_ENDIAN;
143 if (query_length >= 2) {
144 uint16_t byte_order_mark;
145 if (encoding == SL_ENC_LITTLE_ENDIAN)
146 byte_order_mark = SVAL(buf, offset);
148 byte_order_mark = RSVAL(buf, offset);
150 if (byte_order_mark == 0xFFFE) {
151 utf16_encoding = SL_ENC_BIG_ENDIAN | SL_ENC_UTF_16;
153 else if (byte_order_mark == 0xFEFF) {
154 utf16_encoding = SL_ENC_LITTLE_ENDIAN | SL_ENC_UTF_16;
158 return utf16_encoding;
161 /**************************************************************************************************
162 * marshalling functions
163 **************************************************************************************************/
165 static uint64_t sl_pack_tag(uint16_t type, uint16_t size, uint32_t val)
167 uint64_t tag = ((uint64_t)val << 32) | ((uint64_t)type << 16) | size;
171 static int sl_pack_float(double d, char **buf, int *buf_len, char **toc_buf, int *toc_len, int *toc_idx)
178 SLVAL(*buf, 0, sl_pack_tag(SQ_TYPE_FLOAT, 2, 1));
179 SLVAL(*buf, 8, ieee_fp_union.w);
180 *buf += 2 * sizeof(uint64_t);
181 buf_len += 2 * sizeof(uint64_t);
186 static int sl_pack_uint64(uint64_t u, char **buf, int *buf_len, char **toc_buf, int *toc_len, int *toc_idx)
188 SLVAL(*buf, 0, sl_pack_tag(SQ_TYPE_INT64, 2, 1));
190 *buf += 2 * sizeof(uint64_t);
191 *buf_len += 2 * sizeof(uint64_t);
194 /**************************************************************************************************
195 * unmarshalling functions
196 **************************************************************************************************/
198 static uint64_t sl_unpack_uint64(const char *buf, int offset, uint encoding)
200 if (encoding == SL_ENC_LITTLE_ENDIAN)
201 return LVAL(buf, offset);
203 return RLVAL(buf, offset);
206 static int sl_unpack_ints(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
209 uint64_t query_data64;
211 query_data64 = sl_unpack_uint64(buf, offset, encoding);
212 count = query_data64 >> 32;
216 while (i++ < count) {
217 query_data64 = sl_unpack_uint64(buf, offset, encoding);
218 dalloc_add(query, &query_data64, uint64_t);
225 static int sl_unpack_date(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
228 uint64_t query_data64;
231 query_data64 = sl_unpack_uint64(buf, offset, encoding);
232 count = query_data64 >> 32;
236 while (i++ < count) {
237 query_data64 = sl_unpack_uint64(buf, offset, encoding) >> 24;
238 t.tv_sec = query_data64 - SPOTLIGHT_TIME_DELTA;
240 dalloc_add(query, &t, sl_time_t);
247 static int sl_unpack_uuid(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
250 uint64_t query_data64;
252 query_data64 = sl_unpack_uint64(buf, offset, encoding);
253 count = query_data64 >> 32;
257 while (i++ < count) {
258 memcpy(uuid.sl_uuid, buf + offset, 16);
259 dalloc_add(query, &uuid, sl_uuid_t);
266 static int sl_unpack_floats(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
269 uint64_t query_data64;
276 query_data64 = sl_unpack_uint64(buf, offset, encoding);
277 count = query_data64 >> 32;
281 while (i++ < count) {
282 if (encoding == SL_ENC_LITTLE_ENDIAN) {
283 #ifdef WORDS_BIGENDIAN
284 ieee_fp_union.w[0] = IVAL(buf, offset + 4);
285 ieee_fp_union.w[1] = IVAL(buf, offset);
287 ieee_fp_union.w[0] = IVAL(buf, offset);
288 ieee_fp_union.w[1] = IVAL(buf, offset + 4);
291 #ifdef WORDS_BIGENDIAN
292 ieee_fp_union.w[0] = RIVAL(buf, offset);
293 ieee_fp_union.w[1] = RIVAL(buf, offset + 4);
295 ieee_fp_union.w[0] = RIVAL(buf, offset + 4);
296 ieee_fp_union.w[1] = RIVAL(buf, offset);
299 dalloc_add(query, &ieee_fp_union.d, double);
306 static int sl_unpack_CNID(DALLOC_CTX *query, const char *buf, int offset, int length, uint encoding)
310 uint64_t query_data64;
313 EC_NULL( cnids.ca_cnids = talloc_zero(query, DALLOC_CTX) );
316 /* that's permitted, it's an empty array */
319 query_data64 = sl_unpack_uint64(buf, offset, encoding);
320 count = query_data64 & 0xffff;
322 cnids.ca_unkn1 = (query_data64 & 0xffff0000) >> 16;
323 cnids.ca_unkn2 = query_data64 >> 32;
328 query_data64 = sl_unpack_uint64(buf, offset, encoding);
329 dalloc_add(cnids.ca_cnids, &query_data64, uint64_t);
333 dalloc_add(query, &cnids, sl_cnids_t);
339 static const char *spotlight_get_qtype_string(uint64_t query_type)
341 switch (query_type) {
344 case SQ_TYPE_COMPLEX:
361 static const char *spotlight_get_cpx_qtype_string(uint64_t cpx_query_type)
363 switch (cpx_query_type) {
364 case SQ_CPX_TYPE_ARRAY:
366 case SQ_CPX_TYPE_STRING:
368 case SQ_CPX_TYPE_UTF16_STRING:
369 return "utf-16 string";
370 case SQ_CPX_TYPE_DICT:
372 case SQ_CPX_TYPE_CNIDS:
374 case SQ_CPX_TYPE_FILEMETA:
381 static int spotlight_dissect_loop(DALLOC_CTX *query,
385 const uint toc_offset,
389 int i, toc_index, query_length;
390 uint subcount, cpx_query_type, cpx_query_count;
391 uint64_t query_data64, query_type;
392 uint unicode_encoding;
397 while (count > 0 && (offset < toc_offset)) {
398 query_data64 = sl_unpack_uint64(buf, offset, encoding);
399 query_length = (query_data64 & 0xffff) * 8;
400 query_type = (query_data64 & 0xffff0000) >> 16;
401 if (query_length == 0)
404 switch (query_type) {
405 case SQ_TYPE_COMPLEX:
406 toc_index = (query_data64 >> 32) - 1;
407 query_data64 = sl_unpack_uint64(buf, toc_offset + toc_index * 8, encoding);
408 cpx_query_type = (query_data64 & 0xffff0000) >> 16;
409 cpx_query_count = query_data64 >> 32;
411 switch (cpx_query_type) {
412 case SQ_CPX_TYPE_ARRAY: {
413 sl_array_t *sl_arrary = talloc_zero(query, sl_array_t);
414 EC_NEG1_LOG( offset = spotlight_dissect_loop(sl_arrary, buf, offset + 8, cpx_query_count, toc_offset, encoding) );
415 dalloc_add(query, sl_arrary, sl_array_t);
419 case SQ_CPX_TYPE_DICT: {
420 sl_dict_t *sl_dict = talloc_zero(query, sl_dict_t);
421 EC_NEG1_LOG( offset = spotlight_dissect_loop(sl_dict, buf, offset + 8, cpx_query_count, toc_offset, encoding) );
422 dalloc_add(query, sl_dict, sl_dict_t);
425 case SQ_CPX_TYPE_STRING:
426 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
427 query_length += (query_data64 & 0xffff) * 8;
428 if ((padding = 8 - (query_data64 >> 32)) < 0)
430 if ((slen = query_length - 16 - padding) < 1)
432 p = talloc_strndup(query, buf + offset + 16, slen);
433 dalloc_add(query, &p, char *);
436 case SQ_CPX_TYPE_UTF16_STRING:
437 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
438 query_length += (query_data64 & 0xffff) * 8;
439 if ((padding = 8 - (query_data64 >> 32)) < 0)
441 if ((slen = query_length - 16 - padding) < 1)
444 unicode_encoding = spotlight_get_utf16_string_encoding(buf, offset + 16, slen, encoding);
445 mark_exists = (unicode_encoding & SL_ENC_UTF_16);
446 unicode_encoding &= ~SL_ENC_UTF_16;
448 EC_NEG1( convert_string_allocate(CH_UCS2, CH_UTF8, buf + offset + (mark_exists ? 18 : 16), slen, &p) );
449 dalloc_add(query, &p, char *);
452 case SQ_CPX_TYPE_FILEMETA:
453 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
454 query_length += (query_data64 & 0xffff) * 8;
456 if (query_length <= 8) {
457 EC_FAIL_LOG("SQ_CPX_TYPE_FILEMETA: query_length <= 8%s", "");
459 EC_NEG1_LOG( dissect_spotlight(query, buf + offset + 16) );
463 case SQ_CPX_TYPE_CNIDS:
464 query_data64 = sl_unpack_uint64(buf, offset + 8, encoding);
465 query_length += (query_data64 & 0xffff) * 8;
466 EC_NEG1_LOG( sl_unpack_CNID(query, buf, offset + 16, query_length, encoding) );
468 } /* switch (cpx_query_type) */
474 subcount = query_data64 >> 32;
478 for (i = 0; i < subcount; i++)
479 dalloc_add(query, &nil, sl_nil_t);
484 sl_bool_t b = query_data64 >> 32;
485 dalloc_add(query, &b, sl_bool_t);
490 EC_NEG1_LOG( subcount = sl_unpack_ints(query, buf, offset, encoding) );
494 EC_NEG1_LOG( subcount = sl_unpack_uuid(query, buf, offset, encoding) );
498 EC_NEG1_LOG( subcount = sl_unpack_floats(query, buf, offset, encoding) );
502 EC_NEG1_LOG( subcount = sl_unpack_date(query, buf, offset, encoding) );
509 offset += query_length;
519 static int dissect_spotlight(DALLOC_CTX *query, const char *buf)
522 int encoding, i, toc_entries;
523 uint64_t toc_offset, tquerylen, toc_entry;
525 if (strncmp(buf, "md031234", 8) == 0)
526 encoding = SL_ENC_BIG_ENDIAN;
528 encoding = SL_ENC_LITTLE_ENDIAN;
532 toc_offset = ((sl_unpack_uint64(buf, 0, encoding) >> 32) - 1 ) * 8;
533 if (toc_offset < 0 || (toc_offset > 65000)) {
539 toc_entries = (int)(sl_unpack_uint64(buf, toc_offset, encoding) & 0xffff);
541 EC_NEG1( spotlight_dissect_loop(query, buf, 0, 1, toc_offset + 8, encoding) );
547 /**************************************************************************************************
549 **************************************************************************************************/
550 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
553 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
556 int endianess = SL_ENC_LITTLE_ENDIAN;
566 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
568 if ((vol = getvolbyvid(vid)) == NULL) {
569 LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
574 /* IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
576 cmd = RIVAL(ibuf, 6);
577 LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
579 /* IVAL(ibuf, 10: unknown, always 0x00000000 */
583 case SPOTLIGHT_CMD_VOLPATH: {
584 RSIVAL(rbuf, 0, ntohs(vid));
586 int len = strlen(vol->v_path) + 1;
587 strncpy(rbuf + 8, vol->v_path, len);
591 case SPOTLIGHT_CMD_FLAGS:
592 RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? */
596 case SPOTLIGHT_CMD_RPC: {
598 EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
599 (void)dissect_spotlight(query, ibuf + 22);
606 talloc_free(tmp_ctx);
613 /**************************************************************************************************
615 **************************************************************************************************/
617 #ifdef SPOT_TEST_MAIN
619 int main(int argc, char **argv)
622 TALLOC_CTX *mem_ctx = talloc_new(NULL);
623 DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
626 set_processname("spot");
627 setuplog("default:info", "/dev/tty");
629 LOG(log_info, logtype_sl, "Start");
633 dalloc_add(dd, &i, int64_t);
636 dalloc_add(dd, &i, int64_t);
639 char *str = talloc_strdup(dd, "hello world");
640 dalloc_add(dd, &str, char *);
643 dalloc_add(dd, &b, sl_bool_t);
646 dalloc_add(dd, &b, sl_bool_t);
649 /* add a nested array */
650 DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
652 dalloc_add(nested, &i, int64_t);
653 dalloc_add(dd, nested, DALLOC_CTX);
655 /* test an allocated CNID array */
657 sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
659 cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
664 dalloc_add(cnids->ca_cnids, &id, uint32_t);
665 dalloc_add(dd, cnids, sl_cnids_t);
667 /* Now the Spotlight types */
668 sl_array_t *sl_arrary = talloc_zero(dd, sl_array_t);
670 dalloc_add(sl_arrary, &i, int64_t);
672 sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
674 dalloc_add(sl_dict, &i, int64_t);
675 dalloc_add(sl_arrary, sl_dict, sl_dict_t);
677 dalloc_add(dd, sl_arrary, sl_array_t);
680 /* now parse a real spotlight packet */
687 EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
689 EC_NEG1_LOG( fd = open("/home/ralph/netatalk/spot/etc/afpd/spotlight-packet.bin", O_RDONLY) );
690 EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
691 EC_NEG1_LOG( dissect_spotlight(query, ibuf + 24) );
693 /* Now dump the whole thing */
698 talloc_free(mem_ctx);