build: Remove unused dependencies
[Samba.git] / source3 / rpc_client / cli_mdssvc_util.c
blobfe5092c3790a790c7c64f2fb869f8004eee3004e
1 /*
2 Unix SMB/CIFS implementation.
3 Main metadata server / Spotlight client functions
5 Copyright (C) Ralph Boehme 2019
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "rpc_client.h"
23 #include "librpc/gen_ndr/mdssvc.h"
24 #include "cli_mdssvc.h"
25 #include "cli_mdssvc_private.h"
26 #include "cli_mdssvc_util.h"
27 #include "lib/util/tevent_ntstatus.h"
28 #include "rpc_server/mdssvc/dalloc.h"
29 #include "rpc_server/mdssvc/marshalling.h"
31 NTSTATUS mdscli_blob_search(TALLOC_CTX *mem_ctx,
32 struct mdscli_search_ctx *search,
33 struct mdssvc_blob *blob)
35 struct mdscli_ctx *ctx = search->mdscli_ctx;
36 DALLOC_CTX *d = NULL;
37 uint64_t *uint64p = NULL;
38 sl_array_t *array = NULL;
39 sl_array_t *cmd_array = NULL;
40 sl_dict_t *query_dict = NULL;
41 sl_array_t *attr_array = NULL;
42 sl_array_t *scope_array = NULL;
43 double dval;
44 uint64_t uint64val;
45 ssize_t len;
46 int ret;
48 d = dalloc_new(mem_ctx);
49 if (d == NULL) {
50 return NT_STATUS_NO_MEMORY;
53 array = dalloc_zero(d, sl_array_t);
54 if (array == NULL) {
55 TALLOC_FREE(d);
56 return NT_STATUS_NO_MEMORY;
59 ret = dalloc_add(d, array, sl_array_t);
60 if (ret != 0) {
61 TALLOC_FREE(d);
62 return NT_STATUS_NO_MEMORY;
65 cmd_array = dalloc_zero(d, sl_array_t);
66 if (cmd_array == NULL) {
67 TALLOC_FREE(d);
68 return NT_STATUS_NO_MEMORY;
71 ret = dalloc_add(array, cmd_array, sl_array_t);
72 if (ret != 0) {
73 TALLOC_FREE(d);
74 return NT_STATUS_NO_MEMORY;
77 ret = dalloc_stradd(cmd_array, "openQueryWithParams:forContext:");
78 if (ret != 0) {
79 TALLOC_FREE(d);
80 return NT_STATUS_NO_MEMORY;
83 uint64p = talloc_zero_array(cmd_array, uint64_t, 2);
84 if (uint64p == NULL) {
85 TALLOC_FREE(d);
86 return NT_STATUS_NO_MEMORY;
89 talloc_set_name(uint64p, "uint64_t *");
91 uint64p[0] = search->ctx_id.id;
92 uint64p[1] = search->ctx_id.connection;
94 ret = dalloc_add(cmd_array, uint64p, uint64_t *);
95 if (ret != 0) {
96 TALLOC_FREE(d);
97 return NT_STATUS_NO_MEMORY;
100 query_dict = dalloc_zero(array, sl_dict_t);
101 if (query_dict == NULL) {
102 TALLOC_FREE(d);
103 return NT_STATUS_NO_MEMORY;
106 ret = dalloc_add(array, query_dict, sl_dict_t);
107 if (ret != 0) {
108 TALLOC_FREE(d);
109 return NT_STATUS_NO_MEMORY;
112 ret = dalloc_stradd(query_dict, "kMDQueryBatchFirstDelay");
113 if (ret != 0) {
114 TALLOC_FREE(d);
115 return NT_STATUS_NO_MEMORY;
117 dval = 1;
118 ret = dalloc_add_copy(query_dict, &dval, double);
119 if (ret != 0) {
120 TALLOC_FREE(d);
121 return NT_STATUS_NO_MEMORY;
124 ret = dalloc_stradd(query_dict, "kMDQueryUniqueId");
125 if (ret != 0) {
126 TALLOC_FREE(d);
127 return NT_STATUS_NO_MEMORY;
129 ret = dalloc_add_copy(query_dict, &search->unique_id, uint64_t);
130 if (ret != 0) {
131 TALLOC_FREE(d);
132 return NT_STATUS_NO_MEMORY;
135 ret = dalloc_stradd(query_dict, "kMDAttributeArray");
136 if (ret != 0) {
137 TALLOC_FREE(d);
138 return NT_STATUS_NO_MEMORY;
140 attr_array = dalloc_zero(query_dict, sl_array_t);
141 if (attr_array == NULL) {
142 TALLOC_FREE(d);
143 return NT_STATUS_NO_MEMORY;
145 ret = dalloc_add(query_dict, attr_array, sl_array_t);
146 if (ret != 0) {
147 TALLOC_FREE(d);
148 return NT_STATUS_NO_MEMORY;
150 ret = dalloc_stradd(attr_array, "kMDItemFSName");
151 if (ret != 0) {
152 TALLOC_FREE(d);
153 return NT_STATUS_NO_MEMORY;
156 ret = dalloc_stradd(query_dict, "kMDQueryBatchFirstCount");
157 if (ret != 0) {
158 TALLOC_FREE(d);
159 return NT_STATUS_NO_MEMORY;
161 uint64val = 10;
162 ret = dalloc_add_copy(query_dict, &uint64val, uint64_t);
163 if (ret != 0) {
164 TALLOC_FREE(d);
165 return NT_STATUS_NO_MEMORY;
168 ret = dalloc_stradd(query_dict, "kMDQueryBatchUpdateCount");
169 if (ret != 0) {
170 TALLOC_FREE(d);
171 return NT_STATUS_NO_MEMORY;
173 uint64val = 100;
174 ret = dalloc_add_copy(query_dict, &uint64val, uint64_t);
175 if (ret != 0) {
176 TALLOC_FREE(d);
177 return NT_STATUS_NO_MEMORY;
180 ret = dalloc_stradd(query_dict, "kMDQueryString");
181 if (ret != 0) {
182 TALLOC_FREE(d);
183 return NT_STATUS_NO_MEMORY;
185 ret = dalloc_stradd(query_dict, search->mds_query);
186 if (ret != 0) {
187 TALLOC_FREE(d);
188 return NT_STATUS_NO_MEMORY;
191 ret = dalloc_stradd(query_dict, "kMDScopeArray");
192 if (ret != 0) {
193 TALLOC_FREE(d);
194 return NT_STATUS_NO_MEMORY;
196 scope_array = dalloc_zero(query_dict, sl_array_t);
197 if (scope_array == NULL) {
198 TALLOC_FREE(d);
199 return NT_STATUS_NO_MEMORY;
201 ret = dalloc_add(query_dict, scope_array, sl_array_t);
202 if (ret != 0) {
203 TALLOC_FREE(d);
204 return NT_STATUS_NO_MEMORY;
206 ret = dalloc_stradd(scope_array, search->path_scope);
207 if (ret != 0) {
208 TALLOC_FREE(d);
209 return NT_STATUS_NO_MEMORY;
212 blob->spotlight_blob = talloc_array(d,
213 uint8_t,
214 ctx->max_fragment_size);
215 if (blob->spotlight_blob == NULL) {
216 TALLOC_FREE(d);
217 return NT_STATUS_NO_MEMORY;
219 blob->size = ctx->max_fragment_size;
221 len = sl_pack(d, (char *)blob->spotlight_blob, blob->size);
222 TALLOC_FREE(d);
223 if (len == -1) {
224 return NT_STATUS_NO_MEMORY;
227 blob->length = len;
228 blob->size = len;
229 return NT_STATUS_OK;
232 NTSTATUS mdscli_blob_get_results(TALLOC_CTX *mem_ctx,
233 struct mdscli_search_ctx *search,
234 struct mdssvc_blob *blob)
236 struct mdscli_ctx *ctx = search->mdscli_ctx;
237 DALLOC_CTX *d = NULL;
238 uint64_t *uint64p = NULL;
239 sl_array_t *array = NULL;
240 sl_array_t *cmd_array = NULL;
241 ssize_t len;
242 int ret;
244 d = dalloc_new(mem_ctx);
245 if (d == NULL) {
246 return NT_STATUS_NO_MEMORY;
249 array = dalloc_zero(d, sl_array_t);
250 if (array == NULL) {
251 TALLOC_FREE(d);
252 return NT_STATUS_NO_MEMORY;
255 ret = dalloc_add(d, array, sl_array_t);
256 if (ret != 0) {
257 TALLOC_FREE(d);
258 return NT_STATUS_NO_MEMORY;
261 cmd_array = dalloc_zero(d, sl_array_t);
262 if (cmd_array == NULL) {
263 TALLOC_FREE(d);
264 return NT_STATUS_NO_MEMORY;
267 ret = dalloc_add(array, cmd_array, sl_array_t);
268 if (ret != 0) {
269 TALLOC_FREE(d);
270 return NT_STATUS_NO_MEMORY;
273 ret = dalloc_stradd(cmd_array, "fetchQueryResultsForContext:");
274 if (ret != 0) {
275 TALLOC_FREE(d);
276 return NT_STATUS_NO_MEMORY;
279 uint64p = talloc_zero_array(cmd_array, uint64_t, 2);
280 if (uint64p == NULL) {
281 TALLOC_FREE(d);
282 return NT_STATUS_NO_MEMORY;
285 talloc_set_name(uint64p, "uint64_t *");
287 uint64p[0] = search->ctx_id.id;
288 uint64p[1] = search->ctx_id.connection;
290 ret = dalloc_add(cmd_array, uint64p, uint64_t *);
291 if (ret != 0) {
292 TALLOC_FREE(d);
293 return NT_STATUS_NO_MEMORY;
296 blob->spotlight_blob = talloc_array(d,
297 uint8_t,
298 ctx->max_fragment_size);
299 if (blob->spotlight_blob == NULL) {
300 TALLOC_FREE(d);
301 return NT_STATUS_NO_MEMORY;
303 blob->size = ctx->max_fragment_size;
305 len = sl_pack(d, (char *)blob->spotlight_blob, blob->size);
306 TALLOC_FREE(d);
307 if (len == -1) {
308 return NT_STATUS_NO_MEMORY;
311 blob->length = len;
312 blob->size = len;
313 return NT_STATUS_OK;
316 NTSTATUS mdscli_blob_get_path(TALLOC_CTX *mem_ctx,
317 struct mdscli_ctx *ctx,
318 uint64_t cnid,
319 struct mdssvc_blob *blob)
321 struct mdsctx_id ctx_id = mdscli_new_ctx_id(ctx);
322 DALLOC_CTX *d = NULL;
323 uint64_t *uint64var = NULL;
324 sl_array_t *array = NULL;
325 sl_array_t *cmd_array = NULL;
326 sl_array_t *attr_array = NULL;
327 sl_cnids_t *cnids = NULL;
328 ssize_t len;
329 int ret;
331 d = dalloc_new(mem_ctx);
332 if (d == NULL) {
333 return NT_STATUS_NO_MEMORY;
336 array = dalloc_zero(d, sl_array_t);
337 if (array == NULL) {
338 TALLOC_FREE(d);
339 return NT_STATUS_NO_MEMORY;
342 ret = dalloc_add(d, array, sl_array_t);
343 if (ret != 0) {
344 TALLOC_FREE(d);
345 return NT_STATUS_NO_MEMORY;
348 cmd_array = dalloc_zero(d, sl_array_t);
349 if (cmd_array == NULL) {
350 TALLOC_FREE(d);
351 return NT_STATUS_NO_MEMORY;
354 ret = dalloc_add(array, cmd_array, sl_array_t);
355 if (ret != 0) {
356 TALLOC_FREE(d);
357 return NT_STATUS_NO_MEMORY;
360 ret = dalloc_stradd(cmd_array, "fetchAttributes:forOIDArray:context:");
361 if (ret != 0) {
362 TALLOC_FREE(d);
363 return NT_STATUS_NO_MEMORY;
366 uint64var = talloc_zero_array(cmd_array, uint64_t, 2);
367 if (uint64var == NULL) {
368 TALLOC_FREE(d);
369 return NT_STATUS_NO_MEMORY;
372 talloc_set_name(uint64var, "uint64_t *");
374 uint64var[0] = ctx_id.id;
375 uint64var[1] = 0;
377 ret = dalloc_add(cmd_array, &uint64var[0], uint64_t *);
378 if (ret != 0) {
379 TALLOC_FREE(d);
380 return NT_STATUS_NO_MEMORY;
383 attr_array = dalloc_zero(d, sl_array_t);
384 if (attr_array == NULL) {
385 TALLOC_FREE(d);
386 return NT_STATUS_NO_MEMORY;
389 ret = dalloc_add(array, attr_array, sl_array_t);
390 if (ret != 0) {
391 TALLOC_FREE(d);
392 return NT_STATUS_NO_MEMORY;
395 ret = dalloc_stradd(attr_array, "kMDItemPath");
396 if (ret != 0) {
397 TALLOC_FREE(d);
398 return NT_STATUS_NO_MEMORY;
401 /* CNIDs */
402 cnids = talloc_zero(array, sl_cnids_t);
403 if (cnids == NULL) {
404 TALLOC_FREE(d);
405 return NT_STATUS_NO_MEMORY;
408 cnids->ca_cnids = dalloc_new(cnids);
409 if (cnids->ca_cnids == NULL) {
410 TALLOC_FREE(d);
411 return NT_STATUS_NO_MEMORY;
414 cnids->ca_unkn1 = 0xadd;
415 cnids->ca_context = 0x6b000020;
417 ret = dalloc_add_copy(cnids->ca_cnids, &cnid, uint64_t);
418 if (ret != 0) {
419 TALLOC_FREE(d);
420 return NT_STATUS_NO_MEMORY;
423 ret = dalloc_add(array, cnids, sl_cnids_t);
424 if (ret != 0) {
425 TALLOC_FREE(d);
426 return NT_STATUS_NO_MEMORY;
429 blob->spotlight_blob = talloc_array(d,
430 uint8_t,
431 ctx->max_fragment_size);
432 if (blob->spotlight_blob == NULL) {
433 TALLOC_FREE(d);
434 return NT_STATUS_NO_MEMORY;
436 blob->size = ctx->max_fragment_size;
438 len = sl_pack(d, (char *)blob->spotlight_blob, blob->size);
439 TALLOC_FREE(d);
440 if (len == -1) {
441 return NT_STATUS_NO_MEMORY;
444 blob->length = len;
445 blob->size = len;
446 return NT_STATUS_OK;
449 NTSTATUS mdscli_blob_close_search(TALLOC_CTX *mem_ctx,
450 struct mdscli_search_ctx *search,
451 struct mdssvc_blob *blob)
453 struct mdscli_ctx *ctx = search->mdscli_ctx;
454 DALLOC_CTX *d = NULL;
455 uint64_t *uint64p = NULL;
456 sl_array_t *array = NULL;
457 sl_array_t *cmd_array = NULL;
458 ssize_t len;
459 int ret;
461 d = dalloc_new(mem_ctx);
462 if (d == NULL) {
463 return NT_STATUS_NO_MEMORY;
466 array = dalloc_zero(d, sl_array_t);
467 if (array == NULL) {
468 TALLOC_FREE(d);
469 return NT_STATUS_NO_MEMORY;
472 ret = dalloc_add(d, array, sl_array_t);
473 if (ret != 0) {
474 TALLOC_FREE(d);
475 return NT_STATUS_NO_MEMORY;
478 cmd_array = dalloc_zero(d, sl_array_t);
479 if (cmd_array == NULL) {
480 TALLOC_FREE(d);
481 return NT_STATUS_NO_MEMORY;
484 ret = dalloc_add(array, cmd_array, sl_array_t);
485 if (ret != 0) {
486 TALLOC_FREE(d);
487 return NT_STATUS_NO_MEMORY;
490 ret = dalloc_stradd(cmd_array, "closeQueryForContext:");
491 if (ret != 0) {
492 TALLOC_FREE(d);
493 return NT_STATUS_NO_MEMORY;
496 uint64p = talloc_zero_array(cmd_array, uint64_t, 2);
497 if (uint64p == NULL) {
498 TALLOC_FREE(d);
499 return NT_STATUS_NO_MEMORY;
502 talloc_set_name(uint64p, "uint64_t *");
504 uint64p[0] = search->ctx_id.id;
505 uint64p[1] = search->ctx_id.connection;
507 ret = dalloc_add(cmd_array, uint64p, uint64_t *);
508 if (ret != 0) {
509 TALLOC_FREE(d);
510 return NT_STATUS_NO_MEMORY;
513 blob->spotlight_blob = talloc_array(d,
514 uint8_t,
515 ctx->max_fragment_size);
516 if (blob->spotlight_blob == NULL) {
517 TALLOC_FREE(d);
518 return NT_STATUS_NO_MEMORY;
520 blob->size = ctx->max_fragment_size;
522 len = sl_pack(d, (char *)blob->spotlight_blob, blob->size);
523 TALLOC_FREE(d);
524 if (len == -1) {
525 return NT_STATUS_NO_MEMORY;
528 blob->length = len;
529 blob->size = len;
530 return NT_STATUS_OK;