]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/spotlight.c
Fix error handling and freeing of allocated ressources
[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 #include <time.h>
27
28 #include <atalk/errchk.h>
29 #include <atalk/util.h>
30 #include <atalk/logger.h>
31 #include <atalk/talloc.h>
32 #include <atalk/dalloc.h>
33 #include <atalk/byteorder.h>
34 #include <atalk/netatalk_conf.h>
35 #include <atalk/volume.h>
36 #include <atalk/queue.h>
37
38 #include "spotlight.h"
39
40 static TALLOC_CTX *sl_ctx;
41 static void *sl_module;
42 static struct sl_module_export *sl_module_export;
43
44 /* Helper functions and stuff */
45 static const char *neststrings[] = {
46     "",
47     "\t",
48     "\t\t",
49     "\t\t\t",
50     "\t\t\t\t",
51     "\t\t\t\t\t",
52     "\t\t\t\t\t\t",
53 };
54
55 static int dd_dump(DALLOC_CTX *dd, int nestinglevel)
56 {
57     const char *type;
58
59     LOG(log_debug, logtype_sl, "%s%s(#%d): {",
60         neststrings[nestinglevel], talloc_get_name(dd), talloc_array_length(dd->dd_talloc_array));
61
62     for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
63
64         type = talloc_get_name(dd->dd_talloc_array[n]);
65
66         if (STRCMP(type, ==, "DALLOC_CTX")
67                    || STRCMP(type, ==, "sl_array_t")
68                    || STRCMP(type, ==, "sl_filemeta_t")
69                    || STRCMP(type, ==, "sl_dict_t")) {
70             dd_dump(dd->dd_talloc_array[n], nestinglevel + 1);
71         } else if (STRCMP(type, ==, "uint64_t")) {
72             uint64_t i;
73             memcpy(&i, dd->dd_talloc_array[n], sizeof(uint64_t));
74             LOG(log_debug, logtype_sl, "%suint64_t: 0x%04x", neststrings[nestinglevel + 1], i);
75         } else if (STRCMP(type, ==, "char *")) {
76             LOG(log_debug, logtype_sl, "%sstring: %s", neststrings[nestinglevel + 1], (char *)dd->dd_talloc_array[n]);
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_nil_t")) {
82             LOG(log_debug, logtype_sl, "%snil", neststrings[nestinglevel + 1]);
83         } else if (STRCMP(type, ==, "sl_cnids_t")) {
84             sl_cnids_t cnids;
85             memcpy(&cnids, dd->dd_talloc_array[n], sizeof(sl_cnids_t));
86             LOG(log_debug, logtype_sl, "%sCNIDs: unkn1: 0x%" PRIx16 ", unkn2: 0x%" PRIx32,
87                    neststrings[nestinglevel + 1], cnids.ca_unkn1, cnids.ca_context);
88             if (cnids.ca_cnids)
89                 dd_dump(cnids.ca_cnids, nestinglevel + 2);
90         } else {
91             LOG(log_debug, logtype_sl, "%stype: %s", neststrings[nestinglevel + 1], type);
92         }
93     }
94     LOG(log_debug, logtype_sl, "%s}", neststrings[nestinglevel]);
95 }
96
97 /**************************************************************************************************
98  * Spotlight queries
99  **************************************************************************************************/
100
101 static q_t *sl_queries;
102 static slq_t *slq_active;
103
104 /*!
105  * Add a query to the list of active queries
106  */
107 static int slq_add(slq_t *slq)
108 {
109     EC_INIT;
110
111     if (slq_active)
112         talloc_free(slq_active);
113     slq_active = slq;
114
115 EC_CLEANUP:
116     EC_EXIT;
117 }
118
119 static int slq_remove(slq_t *slq)
120 {
121     EC_INIT;
122
123     if ((slq_active->slq_ctx1 == slq->slq_ctx1) && (slq_active->slq_ctx2 == slq->slq_ctx2)) {
124         slq_active = NULL;
125     }
126
127 EC_CLEANUP:
128     EC_EXIT;
129 }
130
131 static slq_t *slq_for_ctx(uint64_t ctx1, uint64_t ctx2)
132 {
133     EC_INIT;
134     slq_t *q;
135
136     LOG(log_debug, logtype_sl, "slq_for_ctx(ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64
137         "): active: ctx1: 0x%" PRIx64 ", ctx2: 0x%" PRIx64,
138         ctx1, ctx2, slq_active->slq_ctx1, slq_active->slq_ctx2);
139
140     if ((slq_active->slq_ctx1 == ctx1) && (slq_active->slq_ctx2 == ctx2))
141         q = slq_active;
142     else
143         q = NULL;
144     
145 EC_CLEANUP:
146     if (ret != 0)
147         q = NULL;
148     return q;
149 }
150
151 /* Error handling for queries */
152 static void slq_error(slq_t *slq)
153 {
154     if (!slq)
155         return;
156     sl_module_export->sl_mod_error(slq);
157     slq_remove(slq);
158     talloc_free(slq);
159 }
160
161 /**************************************************************************************************
162  * Spotlight RPC functions
163  **************************************************************************************************/
164
165 static int sl_rpc_fetchPropertiesForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
166 {
167     EC_INIT;
168
169     char *s;
170     sl_dict_t *dict;
171     sl_array_t *array;
172     sl_uuid_t uuid;
173
174     if (!v->v_uuid)
175         EC_FAIL_LOG("sl_rpc_fetchPropertiesForContext: missing UUID for volume: %s", v->v_localname);
176
177     dict = talloc_zero(reply, sl_dict_t);
178
179     /* key/val 1 */
180     s = dalloc_strdup(dict, "kMDSStoreMetaScopes");
181     dalloc_add(dict, s, char *);
182
183     array = talloc_zero(dict, sl_array_t);
184     s = dalloc_strdup(array, "kMDQueryScopeComputer");
185     dalloc_add(array, s, char *);
186     dalloc_add(dict, array, sl_array_t);
187
188     /* key/val 2 */
189     s = dalloc_strdup(dict, "kMDSStorePathScopes");
190     dalloc_add(dict, s, char *);
191
192     array = talloc_zero(dict, sl_array_t);
193     s = dalloc_strdup(array, v->v_path);
194     dalloc_add(array, s, char *);
195     dalloc_add(dict, array, sl_array_t);
196
197     /* key/val 3 */
198     s = dalloc_strdup(dict, "kMDSStoreUUID");
199     dalloc_add(dict, s, char *);
200
201     memcpy(uuid.sl_uuid, v->v_uuid, 16);
202     dalloc_add_copy(dict, &uuid, sl_uuid_t);
203
204     /* key/val 4 */
205     s = dalloc_strdup(dict, "kMDSStoreHasPersistentUUID");
206     dalloc_add(dict, s, char *);
207     sl_bool_t b = true;
208     dalloc_add_copy(dict, &b, sl_bool_t);
209
210     dalloc_add(reply, dict, sl_dict_t);
211
212 EC_CLEANUP:
213     EC_EXIT;
214 }
215
216 static int sl_rpc_openQuery(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, struct vol *v)
217 {
218     EC_INIT;
219     char *sl_query;
220     uint64_t *uint64;
221     DALLOC_CTX *reqinfo;
222     sl_array_t *array;
223     slq_t *slq = NULL;
224
225     /* Allocate and initialize query object */
226     slq = talloc_zero(sl_ctx, slq_t);
227     slq->slq_state = SLQ_STATE_NEW;
228     slq->slq_obj = obj;
229     slq->slq_vol = v;
230     EC_NULL_LOG( sl_query = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDQueryString") );
231     LOG(log_debug, logtype_sl, "sl_rpc_openQuery: %s", sl_query);
232     slq->slq_qstring = talloc_steal(slq, sl_query);
233
234     slq->slq_time = time(NULL);
235     EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
236     slq->slq_ctx1 = *uint64;
237     EC_NULL_LOG( uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
238     slq->slq_ctx2 = *uint64;
239     EC_NULL_LOG( reqinfo = dalloc_value_for_key(query, "DALLOC_CTX", 0, "DALLOC_CTX", 1, "kMDAttributeArray") );
240     slq->slq_reqinfo = talloc_steal(slq, reqinfo);
241
242     LOG(log_maxdebug, logtype_sl, "sl_rpc_openQuery: requested attributes:");
243     dd_dump(slq->slq_reqinfo, 0);
244
245     (void)slq_add(slq);
246
247     /* Run the query */
248     EC_ZERO( sl_module_export->sl_mod_start_search(slq) );
249
250     array = talloc_zero(reply, sl_array_t);
251     uint64_t sl_res = ret == 0 ? 0 : UINT64_MAX;
252     dalloc_add_copy(array, &sl_res, uint64_t);
253     dalloc_add(reply, array, sl_array_t);
254
255 EC_CLEANUP:
256     if (ret != 0) {
257         slq_error(slq);
258     }
259     EC_EXIT;
260 }
261
262 static int sl_rpc_fetchQueryResultsForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
263 {
264     EC_INIT;
265     slq_t *slq = NULL;
266     uint64_t *uint64, ctx1, ctx2;
267     sl_array_t *array;
268
269     /* Context */
270     EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
271     ctx1 = *uint64;
272     EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
273     ctx2 = *uint64;
274
275     /* Get query for context */
276     EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
277     if (slq->slq_state != SLQ_STATE_RUNNING) {
278         EC_FAIL_LOG("Spotlight: attempt to fetch results for query that isn't runnnig");
279     }
280
281     /* Create and pass reply handle */
282     array = talloc_zero(reply, sl_array_t);
283     uint64_t sl_res = 0;
284     dalloc_add_copy(array, &sl_res, uint64_t);
285     slq->slq_reply = array;
286
287     /* Fetch Tracker results*/
288     EC_ZERO( sl_module_export->sl_mod_fetch_result(slq) );
289
290     dalloc_add(reply, array, sl_array_t);
291
292 EC_CLEANUP:
293     if (ret != 0) {
294         slq_error(slq);
295     }
296     EC_EXIT;
297 }
298
299 static int sl_rpc_closeQueryForContext(const AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
300 {
301     EC_INIT;
302     slq_t *slq = NULL;
303     uint64_t *uint64, ctx1, ctx2;
304     sl_array_t *array;
305     
306     /* Context */
307     EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1) );
308     ctx1 = *uint64;
309     EC_NULL_LOG (uint64 = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 2) );
310     ctx2 = *uint64;
311
312     /* Get query for context and free it */
313     EC_NULL_LOG( slq = slq_for_ctx(ctx1, ctx2) );
314     sl_module_export->sl_mod_end_search(slq);
315     slq_remove(slq);
316     talloc_free(slq);
317     slq = NULL;
318
319     array = talloc_zero(reply, sl_array_t);
320     uint64_t sl_res = 0;
321     dalloc_add_copy(array, &sl_res, uint64_t);
322     dalloc_add(reply, array, sl_array_t);
323
324 EC_CLEANUP:
325     if (ret != 0) {
326         slq_error(slq);
327     }
328     EC_EXIT;
329 }
330
331 /**************************************************************************************************
332  * Spotlight module functions
333  **************************************************************************************************/
334
335 int sl_mod_load(const char *path)
336 {
337     EC_INIT;
338
339     sl_ctx = talloc_new(NULL);
340     sl_queries = queue_init();
341
342     if ((sl_module = mod_open(path)) == NULL) {
343         LOG(log_error, logtype_sl, "sl_mod_load(%s): failed to load: %s", path, mod_error());
344         EC_FAIL;
345     }
346
347     if ((sl_module_export = mod_symbol(sl_module, "sl_mod")) == NULL) {
348         LOG(log_error, logtype_sl, "sl_mod_load(%s): mod_symbol error for symbol %s", path, "sl_mod");
349         EC_FAIL;
350     }
351
352     sl_module_export->sl_mod_init("test");
353    
354 EC_CLEANUP:
355     EC_EXIT;
356 }
357
358 /**************************************************************************************************
359  * AFP functions
360  **************************************************************************************************/
361 int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
362 {
363     EC_INIT;
364     TALLOC_CTX *tmp_ctx = talloc_new(NULL);
365     uint16_t vid;
366     int cmd;
367     int endianess = SL_ENC_LITTLE_ENDIAN;
368     struct vol      *vol;
369     DALLOC_CTX *query;
370     DALLOC_CTX *reply;
371     char *rpccmd;
372     int len;
373
374     *rbuflen = 0;
375
376     ibuf += 2;
377     ibuflen -= 2;
378
379     vid = SVAL(ibuf, 0);
380     LOG(log_debug, logtype_sl, "afp_spotlight_rpc(vid: %" PRIu16 ")", vid);
381
382     if ((vol = getvolbyvid(vid)) == NULL) {
383         LOG(log_error, logtype_sl, "afp_spotlight_rpc: bad volume id: %" PRIu16 ")", vid);
384         ret = AFPERR_ACCESS;
385         goto EC_CLEANUP;
386     }
387
388     /*    IVAL(ibuf, 2): unknown, always 0x00008004, some flags ? */
389
390     cmd = RIVAL(ibuf, 6);
391     LOG(log_debug, logtype_sl, "afp_spotlight_rpc(cmd: %d)", cmd);
392
393     /*    IVAL(ibuf, 10: unknown, always 0x00000000 */
394
395     switch (cmd) {
396
397     case SPOTLIGHT_CMD_OPEN:
398     case SPOTLIGHT_CMD_OPEN2:
399         RSIVAL(rbuf, 0, ntohs(vid));
400         RSIVAL(rbuf, 4, 0);
401         len = strlen(vol->v_path) + 1;
402         strncpy(rbuf + 8, vol->v_path, len);
403         *rbuflen += 8 + len;
404         break;
405
406     case SPOTLIGHT_CMD_FLAGS:
407         RSIVAL(rbuf, 0, 0x0100006b); /* Whatever this value means... flags? Helios uses 0x1eefface */
408         *rbuflen += 4;
409         break;
410
411     case SPOTLIGHT_CMD_RPC:
412         EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
413         EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) );
414         EC_NEG1_LOG( sl_unpack(query, ibuf + 22) );
415
416         LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Request dump:");
417         dd_dump(query, 0);
418
419         EC_NULL_LOG( rpccmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
420
421         if (STRCMP(rpccmd, ==, "fetchPropertiesForContext:")) {
422             EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) );
423         } else if (STRCMP(rpccmd, ==, "openQueryWithParams:forContext:")) {
424             EC_ZERO_LOG( sl_rpc_openQuery(obj, query, reply, vol) );
425         } else if (STRCMP(rpccmd, ==, "fetchQueryResultsForContext:")) {
426             EC_ZERO_LOG( sl_rpc_fetchQueryResultsForContext(obj, query, reply, vol) );
427         } else if (STRCMP(rpccmd, ==, "closeQueryForContext:")) {
428             EC_ZERO_LOG( sl_rpc_closeQueryForContext(obj, query, reply, vol) );
429         } else {
430             LOG(log_error, logtype_sl, "afp_spotlight_rpc: unknown Spotlight RPC: %s", rpccmd);
431         }
432
433         LOG(log_debug, logtype_sl, "afp_spotlight_rpc: Reply dump:");
434         dd_dump(reply, 0);
435
436         memset(rbuf, 0, 4);
437         *rbuflen += 4;
438
439         EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
440         *rbuflen += len;
441         break;
442     }
443
444 EC_CLEANUP:
445     talloc_free(tmp_ctx);
446     if (ret != AFP_OK) {
447         *rbuflen = 0;
448         return AFPERR_MISC;
449     }
450     EC_EXIT;
451 }
452
453 /**************************************************************************************************
454  * Testing
455  **************************************************************************************************/
456
457 #ifdef SPOT_TEST_MAIN
458
459 int main(int argc, char **argv)
460 {
461     EC_INIT;
462     TALLOC_CTX *mem_ctx = talloc_new(NULL);
463     DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
464     uint64_t i;
465
466     set_processname("spot");
467     setuplog("default:info,spotlight:debug", "/dev/tty");
468
469     LOG(log_info, logtype_sl, "Start");
470
471     i = 1;
472     dalloc_add_copy(dd, &i, uint64_t);
473     char *str = dalloc_strdup(dd, "hello world");
474     dalloc_add(dd, str, char *);
475     sl_bool_t b = true;
476     dalloc_add_copy(dd, &b, sl_bool_t);
477
478     /* add a nested array */
479     DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
480     i = 3;
481     dalloc_add_copy(nested, &i, uint64_t);
482     dalloc_add(dd, nested, DALLOC_CTX);
483
484     /* test an allocated CNID array */
485     uint64_t id = 16;
486     sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
487     cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
488     cnids->ca_unkn1 = 1;
489     dalloc_add_copy(cnids->ca_cnids, &id, uint64_t);
490     dalloc_add(dd, cnids, sl_cnids_t);
491
492     /* Now the Spotlight types */
493     sl_array_t *sl_array = talloc_zero(dd, sl_array_t);
494     i = 0x1234;
495     dalloc_add_copy(sl_array, &i, uint64_t);
496
497     sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
498     i = 0xffff;
499     dalloc_add_copy(sl_dict, &i, uint64_t);
500     dalloc_add(sl_array, sl_dict, sl_dict_t);
501
502     dalloc_add(dd, sl_array, sl_array_t);
503
504     dd_dump(dd, 0);
505
506     /* now parse a real spotlight packet */
507     if (argc > 1) {
508         char ibuf[8192];
509         char rbuf[8192];
510         int fd;
511         size_t len;
512         DALLOC_CTX *query;
513
514         EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
515
516         EC_NEG1_LOG( fd = open(argv[1], O_RDONLY) );
517         EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
518         close(fd);
519         EC_NEG1_LOG( sl_unpack(query, ibuf + 24) );
520
521         /* Now dump the whole thing */
522         dd_dump(query, 0);
523     }
524
525 #if 0
526     /* packing  */
527     int qlen;
528     char buf[MAX_SLQ_DAT];
529     EC_NEG1_LOG( qlen = sl_pack(query, buf) );
530
531     EC_NEG1_LOG( fd = open("test.bin", O_RDWR) );
532     lseek(fd, 24, SEEK_SET);
533     write(fd, buf, qlen);
534     close(fd);
535 #endif
536
537 EC_CLEANUP:
538     if (mem_ctx) {
539         talloc_free(mem_ctx);
540         mem_ctx = NULL;
541     }
542     EC_EXIT;
543 }
544 #endif