]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/spotlight.c
Fix string length calculation for empty strings
[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         ret = sl_unpack(query, ibuf + 22);
208         dd_dump(query, 0);
209         if (ret != 0) {
210             LOG(log_error, logtype_sl, "sl_unpack");
211             EC_FAIL;
212         }
213         char **cmd;
214         EC_NULL_LOG( cmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
215
216
217         if (STRCMP(*cmd, ==, "fetchPropertiesForContext:")) {
218             EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) );
219         } else if (STRCMP(*cmd, ==, "openQueryWithParams:forContext:")) {
220             EC_ZERO_LOG( sl_rpc_openQuery(obj, query, reply, vol) );
221         } else if (STRCMP(*cmd, ==, "fetchQueryResultsForContext:")) {
222             uint64_t *p;
223             if ((p = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1)) != NULL) {
224                 LOG(log_info, logtype_sl, "fetchQueryResultsForContext: 0x%" PRIx64, *p);
225             }
226         }
227
228         /* Spotlight RPC status code ? 0 in all traces, we use 0xffffffff for an error, never seen from Apple */
229         if (ret == 0)
230             memset(rbuf, 0, 4);
231         else
232             memset(rbuf, 0xff, 4);
233         *rbuflen += 4;
234
235         int len;
236         EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
237         *rbuflen += len;
238
239         dd_dump(reply, 0);
240
241         break;
242     }
243     }
244
245 EC_CLEANUP:
246     talloc_free(tmp_ctx);
247     if (ret != AFP_OK) {
248         *rbuflen = 0;
249         return AFPERR_MISC;
250     }
251     EC_EXIT;
252 }
253
254 /**************************************************************************************************
255  * Testing
256  **************************************************************************************************/
257
258 #ifdef SPOT_TEST_MAIN
259
260 int main(int argc, char **argv)
261 {
262     EC_INIT;
263     TALLOC_CTX *mem_ctx = talloc_new(NULL);
264     DALLOC_CTX *dd = talloc_zero(mem_ctx, DALLOC_CTX);
265     uint64_t i;
266
267     set_processname("spot");
268     setuplog("default:info,spotlight:debug", "/dev/tty");
269
270     LOG(log_info, logtype_sl, "Start");
271
272 #if 0
273     i = 2;
274     dalloc_add(dd, &i, uint64_t);
275
276     i = 1;
277     dalloc_add(dd, &i, uint64_t);
278
279
280     char *str = talloc_strdup(dd, "hello world");
281     dalloc_add(dd, &str, char *);
282
283     sl_bool_t b = true;
284     dalloc_add(dd, &b, sl_bool_t);
285
286     b = false;
287     dalloc_add(dd, &b, sl_bool_t);
288
289
290     /* add a nested array */
291     DALLOC_CTX *nested = talloc_zero(dd, DALLOC_CTX);
292     i = 3;
293     dalloc_add(nested, &i, uint64_t);
294     dalloc_add(dd, nested, DALLOC_CTX);
295
296     /* test an allocated CNID array */
297     uint64_t id = 16;
298     sl_cnids_t *cnids = talloc_zero(dd, sl_cnids_t);
299
300     cnids->ca_cnids = talloc_zero(cnids, DALLOC_CTX);
301
302     cnids->ca_unkn1 = 1;
303     cnids->ca_unkn2 = 2;
304
305     dalloc_add(cnids->ca_cnids, &id, uint64_t);
306     dalloc_add(dd, cnids, sl_cnids_t);
307
308 #endif
309
310 #if 0
311     /* Now the Spotlight types */
312     sl_array_t *sl_arrary = talloc_zero(dd, sl_array_t);
313     i = 0x1234;
314     dalloc_add(sl_arrary, &i, uint64_t);
315
316     sl_dict_t *sl_dict = talloc_zero(dd, sl_dict_t);
317     i = 0x5678;
318     dalloc_add(sl_dict, &i, uint64_t);
319     dalloc_add(sl_arrary, sl_dict, sl_dict_t);
320
321     dalloc_add(dd, sl_arrary, sl_array_t);
322     dd_dump(dd, 0);
323 #endif
324
325     /* now parse a real spotlight packet */
326     if (argc > 1) {
327         char ibuf[8192];
328         char rbuf[8192];
329         int fd;
330         size_t len;
331         DALLOC_CTX *query;
332
333         EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
334
335         EC_NEG1_LOG( fd = open(argv[1], O_RDONLY) );
336         EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
337         close(fd);
338         EC_NEG1_LOG( sl_unpack(query, ibuf + 24) );
339
340         /* Now dump the whole thing */
341         dd_dump(query, 0);
342     }
343
344 #if 0
345     /* packing  */
346     int qlen;
347     char buf[MAX_SLQ_DAT];
348     EC_NEG1_LOG( qlen = sl_pack(query, buf) );
349
350     EC_NEG1_LOG( fd = open("test.bin", O_RDWR) );
351     lseek(fd, 24, SEEK_SET);
352     write(fd, buf, qlen);
353     close(fd);
354 #endif
355
356 EC_CLEANUP:
357     if (mem_ctx) {
358         talloc_free(mem_ctx);
359         mem_ctx = NULL;
360     }
361     EC_EXIT;
362 }
363 #endif