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