s3:rpc_client: let rpccli_netlogon_network_logon() fallback to workstation = lp_netbi...
[samba.git] / source3 / rpc_client / cli_mdssvc_util.c
1 /*
2    Unix SMB/CIFS implementation.
3    Main metadata server / Spotlight client functions
4
5    Copyright (C) Ralph Boehme 2019
6
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.
11
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.
16
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/>.
19 */
20
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"
30
31 NTSTATUS mdscli_blob_search(TALLOC_CTX *mem_ctx,
32                             struct mdscli_search_ctx *search,
33                             struct mdssvc_blob *blob)
34 {
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;
47
48         d = dalloc_new(mem_ctx);
49         if (d == NULL) {
50                 return NT_STATUS_NO_MEMORY;
51         }
52
53         array = dalloc_zero(d, sl_array_t);
54         if (array == NULL) {
55                 TALLOC_FREE(d);
56                 return NT_STATUS_NO_MEMORY;
57         }
58
59         ret = dalloc_add(d, array, sl_array_t);
60         if (ret != 0) {
61                 TALLOC_FREE(d);
62                 return NT_STATUS_NO_MEMORY;
63         }
64
65         cmd_array = dalloc_zero(d, sl_array_t);
66         if (cmd_array == NULL) {
67                 TALLOC_FREE(d);
68                 return NT_STATUS_NO_MEMORY;
69         }
70
71         ret = dalloc_add(array, cmd_array, sl_array_t);
72         if (ret != 0) {
73                 TALLOC_FREE(d);
74                 return NT_STATUS_NO_MEMORY;
75         }
76
77         ret = dalloc_stradd(cmd_array, "openQueryWithParams:forContext:");
78         if (ret != 0) {
79                 TALLOC_FREE(d);
80                 return NT_STATUS_NO_MEMORY;
81         }
82
83         uint64p = talloc_zero_array(cmd_array, uint64_t, 2);
84         if (uint64p == NULL) {
85                 TALLOC_FREE(d);
86                 return NT_STATUS_NO_MEMORY;
87         }
88
89         talloc_set_name(uint64p, "uint64_t *");
90
91         uint64p[0] = search->ctx_id.id;
92         uint64p[1] = search->ctx_id.connection;
93
94         ret = dalloc_add(cmd_array, uint64p, uint64_t *);
95         if (ret != 0) {
96                 TALLOC_FREE(d);
97                 return NT_STATUS_NO_MEMORY;
98         }
99
100         query_dict = dalloc_zero(array, sl_dict_t);
101         if (query_dict == NULL) {
102                 TALLOC_FREE(d);
103                 return NT_STATUS_NO_MEMORY;
104         }
105
106         ret = dalloc_add(array, query_dict, sl_dict_t);
107         if (ret != 0) {
108                 TALLOC_FREE(d);
109                 return NT_STATUS_NO_MEMORY;
110         }
111
112         ret = dalloc_stradd(query_dict, "kMDQueryBatchFirstDelay");
113         if (ret != 0) {
114                 TALLOC_FREE(d);
115                 return NT_STATUS_NO_MEMORY;
116         }
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;
122         }
123
124         ret = dalloc_stradd(query_dict, "kMDQueryUniqueId");
125         if (ret != 0) {
126                 TALLOC_FREE(d);
127                 return NT_STATUS_NO_MEMORY;
128         }
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;
133         }
134
135         ret = dalloc_stradd(query_dict, "kMDAttributeArray");
136         if (ret != 0) {
137                 TALLOC_FREE(d);
138                 return NT_STATUS_NO_MEMORY;
139         }
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;
144         }
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;
149         }
150         ret = dalloc_stradd(attr_array, "kMDItemFSName");
151         if (ret != 0) {
152                 TALLOC_FREE(d);
153                 return NT_STATUS_NO_MEMORY;
154         }
155
156         ret = dalloc_stradd(query_dict, "kMDQueryBatchFirstCount");
157         if (ret != 0) {
158                 TALLOC_FREE(d);
159                 return NT_STATUS_NO_MEMORY;
160         }
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;
166         }
167
168         ret = dalloc_stradd(query_dict, "kMDQueryBatchUpdateCount");
169         if (ret != 0) {
170                 TALLOC_FREE(d);
171                 return NT_STATUS_NO_MEMORY;
172         }
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;
178         }
179
180         ret = dalloc_stradd(query_dict, "kMDQueryString");
181         if (ret != 0) {
182                 TALLOC_FREE(d);
183                 return NT_STATUS_NO_MEMORY;
184         }
185         ret = dalloc_stradd(query_dict, search->mds_query);
186         if (ret != 0) {
187                 TALLOC_FREE(d);
188                 return NT_STATUS_NO_MEMORY;
189         }
190
191         ret = dalloc_stradd(query_dict, "kMDScopeArray");
192         if (ret != 0) {
193                 TALLOC_FREE(d);
194                 return NT_STATUS_NO_MEMORY;
195         }
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;
200         }
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;
205         }
206         ret = dalloc_stradd(scope_array, search->path_scope);
207         if (ret != 0) {
208                 TALLOC_FREE(d);
209                 return NT_STATUS_NO_MEMORY;
210         }
211
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;
218         }
219         blob->size = ctx->max_fragment_size;
220
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;
225         }
226
227         blob->length = len;
228         blob->size = len;
229         return NT_STATUS_OK;
230 }
231
232 NTSTATUS mdscli_blob_get_results(TALLOC_CTX *mem_ctx,
233                                  struct mdscli_search_ctx *search,
234                                  struct mdssvc_blob *blob)
235 {
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;
243
244         d = dalloc_new(mem_ctx);
245         if (d == NULL) {
246                 return NT_STATUS_NO_MEMORY;
247         }
248
249         array = dalloc_zero(d, sl_array_t);
250         if (array == NULL) {
251                 TALLOC_FREE(d);
252                 return NT_STATUS_NO_MEMORY;
253         }
254
255         ret = dalloc_add(d, array, sl_array_t);
256         if (ret != 0) {
257                 TALLOC_FREE(d);
258                 return NT_STATUS_NO_MEMORY;
259         }
260
261         cmd_array = dalloc_zero(d, sl_array_t);
262         if (cmd_array == NULL) {
263                 TALLOC_FREE(d);
264                 return NT_STATUS_NO_MEMORY;
265         }
266
267         ret = dalloc_add(array, cmd_array, sl_array_t);
268         if (ret != 0) {
269                 TALLOC_FREE(d);
270                 return NT_STATUS_NO_MEMORY;
271         }
272
273         ret = dalloc_stradd(cmd_array, "fetchQueryResultsForContext:");
274         if (ret != 0) {
275                 TALLOC_FREE(d);
276                 return NT_STATUS_NO_MEMORY;
277         }
278
279         uint64p = talloc_zero_array(cmd_array, uint64_t, 2);
280         if (uint64p == NULL) {
281                 TALLOC_FREE(d);
282                 return NT_STATUS_NO_MEMORY;
283         }
284
285         talloc_set_name(uint64p, "uint64_t *");
286
287         uint64p[0] = search->ctx_id.id;
288         uint64p[1] = search->ctx_id.connection;
289
290         ret = dalloc_add(cmd_array, uint64p, uint64_t *);
291         if (ret != 0) {
292                 TALLOC_FREE(d);
293                 return NT_STATUS_NO_MEMORY;
294         }
295
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;
302         }
303         blob->size = ctx->max_fragment_size;
304
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;
309         }
310
311         blob->length = len;
312         blob->size = len;
313         return NT_STATUS_OK;
314 }
315
316 NTSTATUS mdscli_blob_get_path(TALLOC_CTX *mem_ctx,
317                               struct mdscli_ctx *ctx,
318                               uint64_t cnid,
319                               struct mdssvc_blob *blob)
320 {
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;
330
331         d = dalloc_new(mem_ctx);
332         if (d == NULL) {
333                 return NT_STATUS_NO_MEMORY;
334         }
335
336         array = dalloc_zero(d, sl_array_t);
337         if (array == NULL) {
338                 TALLOC_FREE(d);
339                 return NT_STATUS_NO_MEMORY;
340         }
341
342         ret = dalloc_add(d, array, sl_array_t);
343         if (ret != 0) {
344                 TALLOC_FREE(d);
345                 return NT_STATUS_NO_MEMORY;
346         }
347
348         cmd_array = dalloc_zero(d, sl_array_t);
349         if (cmd_array == NULL) {
350                 TALLOC_FREE(d);
351                 return NT_STATUS_NO_MEMORY;
352         }
353
354         ret = dalloc_add(array, cmd_array, sl_array_t);
355         if (ret != 0) {
356                 TALLOC_FREE(d);
357                 return NT_STATUS_NO_MEMORY;
358         }
359
360         ret = dalloc_stradd(cmd_array, "fetchAttributes:forOIDArray:context:");
361         if (ret != 0) {
362                 TALLOC_FREE(d);
363                 return NT_STATUS_NO_MEMORY;
364         }
365
366         uint64var = talloc_zero_array(cmd_array, uint64_t, 2);
367         if (uint64var == NULL) {
368                 TALLOC_FREE(d);
369                 return NT_STATUS_NO_MEMORY;
370         }
371
372         talloc_set_name(uint64var, "uint64_t *");
373
374         uint64var[0] = ctx_id.id;
375         uint64var[1] = 0;
376
377         ret = dalloc_add(cmd_array, &uint64var[0], uint64_t *);
378         if (ret != 0) {
379                 TALLOC_FREE(d);
380                 return NT_STATUS_NO_MEMORY;
381         }
382
383         attr_array = dalloc_zero(d, sl_array_t);
384         if (attr_array == NULL) {
385                 TALLOC_FREE(d);
386                 return NT_STATUS_NO_MEMORY;
387         }
388
389         ret = dalloc_add(array, attr_array, sl_array_t);
390         if (ret != 0) {
391                 TALLOC_FREE(d);
392                 return NT_STATUS_NO_MEMORY;
393         }
394
395         ret = dalloc_stradd(attr_array, "kMDItemPath");
396         if (ret != 0) {
397                 TALLOC_FREE(d);
398                 return NT_STATUS_NO_MEMORY;
399         }
400
401         /* CNIDs */
402         cnids = talloc_zero(array, sl_cnids_t);
403         if (cnids == NULL) {
404                 TALLOC_FREE(d);
405                 return NT_STATUS_NO_MEMORY;
406         }
407
408         cnids->ca_cnids = dalloc_new(cnids);
409         if (cnids->ca_cnids == NULL) {
410                 TALLOC_FREE(d);
411                 return NT_STATUS_NO_MEMORY;
412         }
413
414         cnids->ca_unkn1 = 0xadd;
415         cnids->ca_context = 0x6b000020;
416
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;
421         }
422
423         ret = dalloc_add(array, cnids, sl_cnids_t);
424         if (ret != 0) {
425                 TALLOC_FREE(d);
426                 return NT_STATUS_NO_MEMORY;
427         }
428
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;
435         }
436         blob->size = ctx->max_fragment_size;
437
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;
442         }
443
444         blob->length = len;
445         blob->size = len;
446         return NT_STATUS_OK;
447 }
448
449 NTSTATUS mdscli_blob_close_search(TALLOC_CTX *mem_ctx,
450                                   struct mdscli_search_ctx *search,
451                                   struct mdssvc_blob *blob)
452 {
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;
460
461         d = dalloc_new(mem_ctx);
462         if (d == NULL) {
463                 return NT_STATUS_NO_MEMORY;
464         }
465
466         array = dalloc_zero(d, sl_array_t);
467         if (array == NULL) {
468                 TALLOC_FREE(d);
469                 return NT_STATUS_NO_MEMORY;
470         }
471
472         ret = dalloc_add(d, array, sl_array_t);
473         if (ret != 0) {
474                 TALLOC_FREE(d);
475                 return NT_STATUS_NO_MEMORY;
476         }
477
478         cmd_array = dalloc_zero(d, sl_array_t);
479         if (cmd_array == NULL) {
480                 TALLOC_FREE(d);
481                 return NT_STATUS_NO_MEMORY;
482         }
483
484         ret = dalloc_add(array, cmd_array, sl_array_t);
485         if (ret != 0) {
486                 TALLOC_FREE(d);
487                 return NT_STATUS_NO_MEMORY;
488         }
489
490         ret = dalloc_stradd(cmd_array, "closeQueryForContext:");
491         if (ret != 0) {
492                 TALLOC_FREE(d);
493                 return NT_STATUS_NO_MEMORY;
494         }
495
496         uint64p = talloc_zero_array(cmd_array, uint64_t, 2);
497         if (uint64p == NULL) {
498                 TALLOC_FREE(d);
499                 return NT_STATUS_NO_MEMORY;
500         }
501
502         talloc_set_name(uint64p, "uint64_t *");
503
504         uint64p[0] = search->ctx_id.id;
505         uint64p[1] = search->ctx_id.connection;
506
507         ret = dalloc_add(cmd_array, uint64p, uint64_t *);
508         if (ret != 0) {
509                 TALLOC_FREE(d);
510                 return NT_STATUS_NO_MEMORY;
511         }
512
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;
519         }
520         blob->size = ctx->max_fragment_size;
521
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;
526         }
527
528         blob->length = len;
529         blob->size = len;
530         return NT_STATUS_OK;
531 }