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_FLAGS 2
44 #define SPOTLIGHT_CMD_RPC 3
45 #define SPOTLIGHT_CMD_VOLPATH 4
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 static const char *neststrings[] = {
87 static int dd_dump(DALLOC_CTX *dd, int nestinglevel)
91 LOG(logtype_default, log_debug, "%s1: %s(#%d): {", neststrings[nestinglevel], talloc_get_name(dd), talloc_array_length(dd->dd_talloc_array));
93 for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
95 type = talloc_get_name(dd->dd_talloc_array[n]);
97 if (STRCMP(type, ==, "DALLOC_CTX")
98 || STRCMP(type, ==, "sl_array_t")
99 || STRCMP(type, ==, "sl_dict_t")) {
100 dd_dump(dd->dd_talloc_array[n], nestinglevel + 1);
101 } else if (STRCMP(type, ==, "uint64_t")) {
103 memcpy(&i, dd->dd_talloc_array[n], sizeof(uint64_t));
104 LOG(logtype_default, log_debug, "%s%u:\t0x%04x", neststrings[nestinglevel + 1], n + 1, i);
105 } else if (STRCMP(type, ==, "int64_t")) {
107 memcpy(&i, dd->dd_talloc_array[n], sizeof(int64_t));
108 LOG(logtype_default, log_debug, "%s%d:\t%" PRId64, neststrings[nestinglevel + 1], n + 1, i);
109 } else if (STRCMP(type, ==, "uint32_t")) {
111 memcpy(&i, dd->dd_talloc_array[n], sizeof(uint32_t));
112 LOG(logtype_default, log_debug, "%s%d:\t%" PRIu32, neststrings[nestinglevel + 1], n + 1, i);
113 } else if (STRCMP(type, ==, "char *")) {
115 memcpy(&s, dd->dd_talloc_array[n], sizeof(char *));
116 LOG(logtype_default, log_debug, "%s%d:\t%s", neststrings[nestinglevel + 1], n + +1, s);
117 } else if (STRCMP(type, ==, "sl_bool_t")) {
119 memcpy(&bl, dd->dd_talloc_array[n], sizeof(sl_bool_t));
120 LOG(logtype_default, log_debug, "%s%d:\t%s", neststrings[nestinglevel + 1], n + +1, bl ? "true" : "false");
121 } else if (STRCMP(type, ==, "sl_cnids_t")) {
123 memcpy(&cnids, dd->dd_talloc_array[n], sizeof(sl_cnids_t));
124 LOG(logtype_default, log_debug, "%s%d:\tunkn1: %" PRIu16 ", unkn2: %" PRIu32,
125 neststrings[nestinglevel + 1], n + 1, cnids.ca_unkn1, cnids.ca_unkn2);
127 dd_dump(cnids.ca_cnids, nestinglevel + 1);
130 LOG(logtype_default, log_debug, "%s}", neststrings[nestinglevel]);
133 static double spotlight_ieee_double(const char *buf, int offset, uint encoding)
140 if (encoding == SL_ENC_LITTLE_ENDIAN) {
141 #ifdef WORDS_BIGENDIAN
142 ieee_fp_union.w[0] = IVAL(buf, offset + 4);
143 ieee_fp_union.w[1] = IVAL(buf, offset);
145 ieee_fp_union.w[0] = IVAL(buf, offset);
146 ieee_fp_union.w[1] = IVAL(buf, offset + 4);
148 return ieee_fp_union.d;
150 #ifdef WORDS_BIGENDIAN
151 ieee_fp_union.w[0] = RIVAL(buf, offset);
152 ieee_fp_union.w[1] = RIVAL(buf, offset + 4);
154 ieee_fp_union.w[0] = RIVAL(buf, offset + 4);
155 ieee_fp_union.w[1] = RIVAL(buf, offset);
157 return ieee_fp_union.d;
161 static uint64_t spotlight_ntoh64(const char *buf, int off, uint encoding)
163 if (encoding == SL_ENC_LITTLE_ENDIAN)
164 return LVAL(buf, off);
166 return ntoh64(LVAL(buf, off));
170 * Returns the UTF-16 string encoding, by checking the 2-byte byte order mark.
171 * If there is no byte order mark, -1 is returned.
173 static uint spotlight_get_utf16_string_encoding(const char *buf, int offset, int query_length, uint encoding) {
176 /* check for byte order mark */
177 utf16_encoding = SL_ENC_BIG_ENDIAN;
178 if (query_length >= 2) {
179 uint16_t byte_order_mark;
180 if (encoding == SL_ENC_LITTLE_ENDIAN)
181 byte_order_mark = SVAL(buf, offset);
183 byte_order_mark = RSVAL(buf, offset);
185 if (byte_order_mark == 0xFFFE) {
186 utf16_encoding = SL_ENC_BIG_ENDIAN | SL_ENC_UTF_16;
188 else if (byte_order_mark == 0xFEFF) {
189 utf16_encoding = SL_ENC_LITTLE_ENDIAN | SL_ENC_UTF_16;
193 return utf16_encoding;
196 static int spotlight_int64(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
199 uint64_t query_data64;
201 query_data64 = spotlight_ntoh64(buf, offset, encoding);
202 count = query_data64 >> 32;
206 while (i++ < count) {
207 query_data64 = spotlight_ntoh64(buf, offset, encoding);
208 dalloc_add(query, &query_data64, uint64_t);
215 static int spotlight_date(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
218 uint64_t query_data64;
221 query_data64 = spotlight_ntoh64(buf, offset, encoding);
222 count = query_data64 >> 32;
226 while (i++ < count) {
227 query_data64 = spotlight_ntoh64(buf, offset, encoding) >> 24;
228 t.tv_sec = query_data64 - SPOTLIGHT_TIME_DELTA;
230 dalloc_add(query, &t, sl_time_t);
237 static int spotlight_uuid(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
240 uint64_t query_data64;
242 query_data64 = spotlight_ntoh64(buf, offset, encoding);
243 count = query_data64 >> 32;
247 while (i++ < count) {
248 memcpy(uuid.sl_uuid, buf + offset, 16);
249 dalloc_add(query, &uuid, sl_uuid_t);
256 static int spotlight_float(DALLOC_CTX *query, const char *buf, int offset, uint encoding)
259 uint64_t query_data64;
262 query_data64 = spotlight_ntoh64(buf, offset, encoding);
263 count = query_data64 >> 32;
267 while (i++ < count) {
268 fval = spotlight_ieee_double(buf, offset, encoding);
269 dalloc_add(query, &fval, double);
276 static int spotlight_CNID_array(DALLOC_CTX *query, const char *buf, int offset, int length, uint encoding)
280 uint64_t query_data64;
283 EC_NULL( cnids.ca_cnids = talloc_zero(query, DALLOC_CTX) );
286 /* that's permitted, it's an empty array */
289 query_data64 = spotlight_ntoh64(buf, offset, encoding);
290 count = query_data64 & 0xffff;
292 cnids.ca_unkn1 = (query_data64 & 0xffff0000) >> 16;
293 cnids.ca_unkn2 = query_data64 >> 32;
298 query_data64 = spotlight_ntoh64(buf, offset, encoding);
299 dalloc_add(cnids.ca_cnids, &query_data64, uint64_t);
303 dalloc_add(query, &cnids, sl_cnids_t);
309 static const char *spotlight_get_qtype_string(uint64_t query_type)
311 switch (query_type) {
314 case SQ_TYPE_COMPLEX:
331 static const char *spotlight_get_cpx_qtype_string(uint64_t cpx_query_type)
333 switch (cpx_query_type) {
334 case SQ_CPX_TYPE_ARRAY:
336 case SQ_CPX_TYPE_STRING:
338 case SQ_CPX_TYPE_UTF16_STRING:
339 return "utf-16 string";
340 case SQ_CPX_TYPE_DICT:
342 case SQ_CPX_TYPE_CNIDS:
344 case SQ_CPX_TYPE_FILEMETA:
351 static int spotlight_dissect_loop(DALLOC_CTX *query,
355 const uint toc_offset,
359 int i, toc_index, query_length;
360 uint subcount, cpx_query_type, cpx_query_count;
361 uint64_t query_data64, query_type;
362 uint unicode_encoding;
367 while (count > 0 && (offset < toc_offset)) {
368 query_data64 = spotlight_ntoh64(buf, offset, encoding);
369 query_length = (query_data64 & 0xffff) * 8;
370 query_type = (query_data64 & 0xffff0000) >> 16;
371 if (query_length == 0)
374 switch (query_type) {
375 case SQ_TYPE_COMPLEX:
376 toc_index = (query_data64 >> 32) - 1;
377 query_data64 = spotlight_ntoh64(buf, toc_offset + toc_index * 8, encoding);
378 cpx_query_type = (query_data64 & 0xffff0000) >> 16;
379 cpx_query_count = query_data64 >> 32;
381 switch (cpx_query_type) {
382 case SQ_CPX_TYPE_ARRAY: {
383 sl_array_t *sl_arrary = talloc_zero(query, sl_array_t);
384 EC_NEG1_LOG( offset = spotlight_dissect_loop(sl_arrary, buf, offset + 8, cpx_query_count, toc_offset, encoding) );
385 dalloc_add(query, sl_arrary, sl_array_t);
389 case SQ_CPX_TYPE_DICT: {
390 sl_dict_t *sl_dict = talloc_zero(query, sl_dict_t);
391 EC_NEG1_LOG( offset = spotlight_dissect_loop(sl_dict, buf, offset + 8, cpx_query_count, toc_offset, encoding) );
392 dalloc_add(query, sl_dict, sl_dict_t);
395 case SQ_CPX_TYPE_STRING:
396 query_data64 = spotlight_ntoh64(buf, offset + 8, encoding);
397 query_length += (query_data64 & 0xffff) * 8;
398 if ((padding = 8 - (query_data64 >> 32)) < 0)
400 if ((slen = query_length - 16 - padding) < 1)
402 p = talloc_strndup(query, buf + offset + 16, slen);
403 dalloc_add(query, &p, char *);
406 case SQ_CPX_TYPE_UTF16_STRING:
407 query_data64 = spotlight_ntoh64(buf, offset + 8, encoding);
408 query_length += (query_data64 & 0xffff) * 8;
409 if ((padding = 8 - (query_data64 >> 32)) < 0)
411 if ((slen = query_length - 16 - padding) < 1)
414 unicode_encoding = spotlight_get_utf16_string_encoding(buf, offset + 16, slen, encoding);
415 mark_exists = (unicode_encoding & SL_ENC_UTF_16);
416 unicode_encoding &= ~SL_ENC_UTF_16;
418 EC_NEG1( convert_string_allocate(CH_UCS2, CH_UTF8, buf + offset + (mark_exists ? 18 : 16), slen, &p) );
419 dalloc_add(query, &p, char *);
422 case SQ_CPX_TYPE_FILEMETA:
423 if (query_length <= 8) {
425 EC_NEG1_LOG( dissect_spotlight(query, buf + offset + 16) );
428 case SQ_CPX_TYPE_CNIDS:
429 EC_NEG1_LOG( spotlight_CNID_array(query, buf, offset + 16, query_length, encoding) );
431 } /* switch (cpx_query_type) */
437 subcount = query_data64 >> 32;
441 for (i = 0; i < subcount; i++)
442 dalloc_add(query, &nil, sl_nil_t);
447 sl_bool_t b = query_data64 >> 32;
448 dalloc_add(query, &b, sl_bool_t);
453 EC_NEG1_LOG( subcount = spotlight_int64(query, buf, offset, encoding) );
457 EC_NEG1_LOG( subcount = spotlight_uuid(query, buf, offset, encoding) );
461 EC_NEG1_LOG( subcount = spotlight_float(query, buf, offset, encoding) );
465 EC_NEG1_LOG( subcount = spotlight_date(query, buf, offset, encoding) );
469 EC_NEG1_LOG( spotlight_CNID_array(query, buf, offset + 8, query_length, encoding) );
475 offset += query_length;
485 static int dissect_spotlight(DALLOC_CTX *query, const char *buf)
488 int encoding, i, toc_entries;
489 uint64_t toc_offset, tquerylen, toc_entry;
491 if (strncmp(buf, "md031234", 8) == 0)
492 encoding = SL_ENC_BIG_ENDIAN;
494 encoding = SL_ENC_LITTLE_ENDIAN;
498 toc_offset = ((spotlight_ntoh64(buf, 0, encoding) >> 32) - 1 ) * 8;
499 if (toc_offset < 0 || (toc_offset > 65000)) {
505 toc_entries = (int)(spotlight_ntoh64(buf, toc_offset, encoding) & 0xffff);
507 EC_NEG1( spotlight_dissect_loop(query, buf, 0, 1, toc_offset + 8, encoding) );
513 /**************************************************************************************************
515 **************************************************************************************************/
516 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
519 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
522 int endianess = SL_ENC_LITTLE_ENDIAN;
532 LOG(logtype_default, log_note, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
534 if ((vol = getvolbyvid(vid)) == NULL) {
535 LOG(logtype_default, log_error, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
540 /* IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
542 cmd = RIVAL(ibuf, 6);
543 LOG(logtype_default, log_note, "afp_spotlight_rpc(cmd: %d)", cmd);
545 /* IVAL(ibuf, 10: unknown, always 0x00000000 */
549 case SPOTLIGHT_CMD_VOLPATH: {
550 RSIVAL(rbuf, 0, ntohs(vid));
552 int len = strlen(vol->v_path) + 1;
553 strncpy(rbuf + 8, vol->v_path, len);
557 case SPOTLIGHT_CMD_FLAGS:
558 RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? */
562 case SPOTLIGHT_CMD_RPC: {
564 EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
565 (void)dissect_spotlight(query, ibuf + 22);
572 talloc_free(tmp_ctx);
579 /**************************************************************************************************
581 **************************************************************************************************/
583 #ifdef SPOT_TEST_MAIN
585 int main(int argc, char **argv)
588 TALLOC_CTX *mem_ctx = talloc_new(NULL);
589 DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
592 set_processname("spot");
593 setuplog("default:info", "/dev/tty");
595 LOG(logtype_default, log_info, "Start");
599 dalloc_add(dd, &i, int64_t);
602 dalloc_add(dd, &i, int64_t);
605 char *str = talloc_strdup(dd, "hello world");
606 dalloc_add(dd, &str, char *);
609 dalloc_add(dd, &b, sl_bool_t);
612 dalloc_add(dd, &b, sl_bool_t);
615 /* add a nested array */
616 DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
618 dalloc_add(nested, &i, int64_t);
619 dalloc_add(dd, nested, DALLOC_CTX);
621 /* test an allocated CNID array */
623 sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
625 cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
630 dalloc_add(cnids->ca_cnids, &id, uint32_t);
631 dalloc_add(dd, cnids, sl_cnids_t);
633 /* Now the Spotlight types */
634 sl_array_t *sl_arrary = talloc_zero(dd, sl_array_t);
636 dalloc_add(sl_arrary, &i, int64_t);
638 sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
640 dalloc_add(sl_dict, &i, int64_t);
641 dalloc_add(sl_arrary, sl_dict, sl_dict_t);
643 dalloc_add(dd, sl_arrary, sl_array_t);
646 /* now parse a real spotlight packet */
653 EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
655 EC_NEG1_LOG( fd = open("/home/ralph/netatalk/spot/etc/afpd/spotlight-packet.bin", O_RDONLY) );
656 EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
657 EC_NEG1_LOG( dissect_spotlight(query, ibuf + 24) );
659 /* Now dump the whole thing */
664 talloc_free(mem_ctx);