]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/spotlight.c
2d9d6315e1ee6df933f89ca745ce0623e306cca0
[netatalk.git] / etc / afpd / spotlight.c
1 /*
2   Copyright (c) 2012 Frank Lahm <franklahm@gmail.com>
3
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.
8
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.
13 */
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif /* HAVE_CONFIG_H */
18
19 #include <string.h>
20 #include <strings.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <stdbool.h>
25 #include <inttypes.h>
26
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>
35
36 #include "spotlight.h"
37
38
39 /* Helper functions and stuff */
40 static const char *neststrings[] = {
41     "",
42     "\t",
43     "\t\t",
44     "\t\t\t",
45     "\t\t\t\t",
46     "\t\t\t\t\t",
47     "\t\t\t\t\t\t",
48 };
49
50 static int dd_dump(DALLOC_CTX *dd, int nestinglevel)
51 {
52     const char *type;
53
54     LOG(log_debug, logtype_sl, "%s%s(#%d): {",
55         neststrings[nestinglevel], talloc_get_name(dd), talloc_array_length(dd->dd_talloc_array));
56
57     for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
58
59         type = talloc_get_name(dd->dd_talloc_array[n]);
60
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")) {
66             uint64_t i;
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, ==, "char *")) {
70             char *s;
71             memcpy(&s, dd->dd_talloc_array[n], sizeof(char *));
72             LOG(log_debug, logtype_sl, "%sstring: %s", neststrings[nestinglevel + 1], s);
73         } else if (STRCMP(type, ==, "sl_bool_t")) {
74             sl_bool_t bl;
75             memcpy(&bl, dd->dd_talloc_array[n], sizeof(sl_bool_t));
76             LOG(log_debug, logtype_sl, "%sbool: %s", neststrings[nestinglevel + 1], bl ? "true" : "false");
77         } else if (STRCMP(type, ==, "sl_cnids_t")) {
78             sl_cnids_t cnids;
79             memcpy(&cnids, dd->dd_talloc_array[n], sizeof(sl_cnids_t));
80             LOG(log_debug, logtype_sl, "%sCNIDs: unkn1: %" PRIu16 ", unkn2: %" PRIu32,
81                    neststrings[nestinglevel + 1], cnids.ca_unkn1, cnids.ca_context);
82             if (cnids.ca_cnids)
83                 dd_dump(cnids.ca_cnids, nestinglevel + 1);
84         }
85     }
86     LOG(log_debug, logtype_sl, "%s}", neststrings[nestinglevel]);
87 }
88
89 /**************************************************************************************************
90  * Spotlight RPC functions
91  **************************************************************************************************/
92
93 static int sl_rpc_fetchPropertiesForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
94 {
95     EC_INIT;
96
97     char *s;
98     sl_dict_t *dict;
99     sl_array_t *array;
100     sl_uuid_t uuid;
101
102     if (!v->v_uuid)
103         EC_FAIL_LOG("sl_rpc_fetchPropertiesForContext: missing UUID for volume: %s", v->v_localname);
104
105     dict = talloc_zero(reply, sl_dict_t);
106
107     /* key/val 1 */
108     s = talloc_strdup(dict, "kMDSStoreMetaScopes");
109     dalloc_add(dict, &s, char *);
110
111     array = talloc_zero(dict, sl_array_t);
112     s = talloc_strdup(array, "kMDQueryScopeComputer");
113     dalloc_add(array, &s, char *);
114     dalloc_add(dict, array, sl_array_t);
115
116     /* key/val 2 */
117     s = talloc_strdup(dict, "kMDSStorePathScopes");
118     dalloc_add(dict, &s, char *);
119
120     array = talloc_zero(dict, sl_array_t);
121     s = talloc_strdup(array, v->v_path);
122     dalloc_add(array, &s, char *);
123     dalloc_add(dict, array, sl_array_t);
124
125     /* key/val 3 */
126     s = talloc_strdup(dict, "kMDSStoreUUID");
127     dalloc_add(dict, &s, char *);
128
129     memcpy(uuid.sl_uuid, v->v_uuid, 16);
130     dalloc_add(dict, &uuid, sl_uuid_t);
131
132     /* key/val 4 */
133     s = talloc_strdup(dict, "kMDSStoreHasPersistentUUID");
134     dalloc_add(dict, &s, char *);
135     sl_bool_t b = true;
136     dalloc_add(dict, &b, sl_bool_t);
137
138     dalloc_add(reply, dict, sl_dict_t);
139
140 EC_CLEANUP:
141     EC_EXIT;
142 }
143
144 static int sl_rpc_openQuery(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
145 {
146     EC_INIT;
147 EC_CLEANUP:
148     EC_EXIT;
149 }
150
151 /**************************************************************************************************
152  * AFP functions
153  **************************************************************************************************/
154 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
155 {
156     EC_INIT;
157     TALLOC_CTX *tmp_ctx = talloc_new(NULL);
158     uint16_t vid;
159     int cmd;
160     int endianess = SL_ENC_LITTLE_ENDIAN;
161     struct vol      *vol;
162     DALLOC_CTX *query;
163
164     *rbuflen = 0;
165
166     ibuf += 2;
167     ibuflen -= 2;
168
169     vid = SVAL(ibuf, 0);
170     LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
171
172     if ((vol = getvolbyvid(vid)) == NULL) {
173         LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
174         ret = AFPERR_ACCESS;
175         goto EC_CLEANUP;
176     }
177
178     /*    IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
179
180     cmd = RIVAL(ibuf, 6);
181     LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
182
183     /*    IVAL(ibuf, 10: unknown, always 0x00000000 */
184
185     switch (cmd) {
186
187     case SPOTLIGHT_CMD_OPEN:
188     case SPOTLIGHT_CMD_OPEN2: {
189         RSIVAL(rbuf, 0, ntohs(vid));
190         RSIVAL(rbuf, 4, 0);
191         int len = strlen(vol->v_path) + 1;
192         strncpy(rbuf + 8, vol->v_path, len);
193         *rbuflen += 8 + len;
194         break;
195     }
196     case SPOTLIGHT_CMD_FLAGS:
197         RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? Helios uses 0x1eefface */
198         *rbuflen += 4;
199         break;
200
201     case SPOTLIGHT_CMD_RPC: {
202         DALLOC_CTX *query;
203         EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
204         DALLOC_CTX *reply;
205         EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) );
206
207         EC_ZERO( sl_unpack(query, ibuf + 22) );
208         dd_dump(query, 0);
209
210         char **cmd;
211         EC_NULL_LOG( cmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
212
213
214         if (STRCMP(*cmd, ==, "fetchPropertiesForContext:")) {
215             EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) );
216         } else if (STRCMP(*cmd, ==, "openQueryWithParams:forContext:")) {
217             EC_ZERO_LOG( sl_rpc_openQuery(obj, query, reply, vol) );
218         } else if (STRCMP(*cmd, ==, "fetchQueryResultsForContext:")) {
219             uint64_t *p;
220             if ((p = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1)) != NULL) {
221                 LOG(log_info, logtype_sl, "fetchQueryResultsForContext: 0x%" PRIx64, *p);
222             }
223         }
224
225         /* Spotlight RPC status code ? 0 in all traces, we use 0xffffffff for an error, never seen from Apple */
226         if (ret == 0)
227             memset(rbuf, 0, 4);
228         else
229             memset(rbuf, 0xff, 4);
230         *rbuflen += 4;
231
232         int len;
233         EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
234         *rbuflen += len;
235
236         dd_dump(reply, 0);
237
238         break;
239     }
240     }
241
242 EC_CLEANUP:
243     talloc_free(tmp_ctx);
244     if (ret != AFP_OK) {
245         *rbuflen = 0;
246         return AFPERR_MISC;
247     }
248     EC_EXIT;
249 }
250
251 /**************************************************************************************************
252  * Testing
253  **************************************************************************************************/
254
255 #ifdef SPOT_TEST_MAIN
256
257 int main(int argc, char **argv)
258 {
259     EC_INIT;
260     TALLOC_CTX *mem_ctx = talloc_new(NULL);
261     DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
262     uint64_t i;
263
264     set_processname("spot");
265     setuplog("default:info,spotlight:debug", "/dev/tty");
266
267     LOG(log_info, logtype_sl, "Start");
268
269 #if 0
270     i = 2;
271     dalloc_add(dd, &i, uint64_t);
272
273     i = 1;
274     dalloc_add(dd, &i, uint64_t);
275
276
277     char *str = talloc_strdup(dd, "hello world");
278     dalloc_add(dd, &str, char *);
279
280     sl_bool_t b = true;
281     dalloc_add(dd, &b, sl_bool_t);
282
283     b = false;
284     dalloc_add(dd, &b, sl_bool_t);
285
286
287     /* add a nested array */
288     DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
289     i = 3;
290     dalloc_add(nested, &i, uint64_t);
291     dalloc_add(dd, nested, DALLOC_CTX);
292
293     /* test an allocated CNID array */
294     uint64_t id = 16;
295     sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
296
297     cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
298
299     cnids->ca_unkn1 = 1;
300     cnids->ca_unkn2 = 2;
301
302     dalloc_add(cnids->ca_cnids, &id, uint64_t);
303     dalloc_add(dd, cnids, sl_cnids_t);
304
305 #endif
306
307     /* Now the Spotlight types */
308     sl_array_t *sl_arrary = talloc_zero(dd, sl_array_t);
309     i = 0x1234;
310     dalloc_add(sl_arrary, &i, uint64_t);
311
312     sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
313     i = 0x5678;
314     dalloc_add(sl_dict, &i, uint64_t);
315     dalloc_add(sl_arrary, sl_dict, sl_dict_t);
316
317     dalloc_add(dd, sl_arrary, sl_array_t);
318     dd_dump(dd, 0);
319
320     /* now parse a real spotlight packet */
321     char ibuf[8192];
322     char rbuf[8192];
323     int fd;
324     size_t len;
325     DALLOC_CTX *query;
326
327     EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
328
329     EC_NEG1_LOG( fd = open("openQuery-packet.bin", O_RDONLY) );
330     EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
331     close(fd);
332     EC_NEG1_LOG( sl_unpack(query, ibuf + 24) );
333
334     /* Now dump the whole thing */
335     dd_dump(query, 0);
336
337 #if 0
338     int qlen;
339     char buf[MAX_SLQ_DAT];
340     EC_NEG1_LOG( qlen = sl_pack(query, buf) );
341
342     EC_NEG1_LOG( fd = open("test.bin", O_RDWR) );
343     lseek(fd, 24, SEEK_SET);
344     write(fd, buf, qlen);
345     close(fd);
346 #endif
347
348 EC_CLEANUP:
349     if (mem_ctx) {
350         talloc_free(mem_ctx);
351         mem_ctx = NULL;
352     }
353     EC_EXIT;
354 }
355 #endif