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