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