s3-util: use shared dom_sid_dup.
[samba.git] / source3 / rpc_client / cli_lsarpc.c
1 /*
2    Unix SMB/CIFS implementation.
3    RPC pipe client
4    Copyright (C) Tim Potter                        2000-2001,
5    Copyright (C) Andrew Tridgell              1992-1997,2000,
6    Copyright (C) Rafal Szczesniak                       2002
7    Copyright (C) Jeremy Allison                         2005.
8    Copyright (C) Michael Adam                           2007.
9    Copyright (C) Guenther Deschner                      2008.
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "includes.h"
26 #include "../librpc/gen_ndr/cli_lsa.h"
27 #include "rpc_client/cli_lsarpc.h"
28 #include "rpc_client/init_lsa.h"
29 #include "../libcli/security/dom_sid.h"
30
31 /** @defgroup lsa LSA - Local Security Architecture
32  *  @ingroup rpc_client
33  *
34  * @{
35  **/
36
37 /**
38  * @file cli_lsarpc.c
39  *
40  * RPC client routines for the LSA RPC pipe.  LSA means "local
41  * security authority", which is half of a password database.
42  **/
43
44 /** Open a LSA policy handle
45  *
46  * @param cli Handle on an initialised SMB connection */
47
48 NTSTATUS rpccli_lsa_open_policy(struct rpc_pipe_client *cli,
49                                 TALLOC_CTX *mem_ctx,
50                                 bool sec_qos, uint32 des_access,
51                                 struct policy_handle *pol)
52 {
53         struct lsa_ObjectAttribute attr;
54         struct lsa_QosInfo qos;
55         uint16_t system_name = '\\';
56
57         ZERO_STRUCT(attr);
58
59         attr.len        = 0x18;
60
61         if (sec_qos) {
62                 qos.len                 = 0xc;
63                 qos.impersonation_level = 2;
64                 qos.context_mode        = 1;
65                 qos.effective_only      = 0;
66
67                 attr.sec_qos            = &qos;
68         }
69
70         return rpccli_lsa_OpenPolicy(cli, mem_ctx,
71                                      &system_name,
72                                      &attr,
73                                      des_access,
74                                      pol);
75 }
76
77 /** Open a LSA policy handle
78   *
79   * @param cli Handle on an initialised SMB connection
80   */
81
82 NTSTATUS rpccli_lsa_open_policy2(struct rpc_pipe_client *cli,
83                                  TALLOC_CTX *mem_ctx, bool sec_qos,
84                                  uint32 des_access, struct policy_handle *pol)
85 {
86         struct lsa_ObjectAttribute attr;
87         struct lsa_QosInfo qos;
88
89         ZERO_STRUCT(attr);
90
91         attr.len        = 0x18;
92
93         if (sec_qos) {
94                 qos.len                 = 0xc;
95                 qos.impersonation_level = 2;
96                 qos.context_mode        = 1;
97                 qos.effective_only      = 0;
98
99                 attr.sec_qos            = &qos;
100         }
101
102         return rpccli_lsa_OpenPolicy2(cli, mem_ctx,
103                                       cli->srv_name_slash,
104                                       &attr,
105                                       des_access,
106                                       pol);
107 }
108
109 /* Lookup a list of sids
110  *
111  * internal version withOUT memory allocation of the target arrays.
112  * this assumes suffciently sized arrays to store domains, names and types. */
113
114 static NTSTATUS rpccli_lsa_lookup_sids_noalloc(struct rpc_pipe_client *cli,
115                                                TALLOC_CTX *mem_ctx,
116                                                struct policy_handle *pol,
117                                                int num_sids,
118                                                const struct dom_sid *sids,
119                                                char **domains,
120                                                char **names,
121                                                enum lsa_SidType *types,
122                                                bool use_lookupsids3)
123 {
124         NTSTATUS result = NT_STATUS_OK;
125         TALLOC_CTX *tmp_ctx = NULL;
126         int i;
127         struct lsa_SidArray sid_array;
128         struct lsa_RefDomainList *ref_domains = NULL;
129         struct lsa_TransNameArray lsa_names;
130         uint32_t count = 0;
131         uint16_t level = 1;
132
133         ZERO_STRUCT(lsa_names);
134
135         tmp_ctx = talloc_new(mem_ctx);
136         if (!tmp_ctx) {
137                 DEBUG(0, ("rpccli_lsa_lookup_sids_noalloc: out of memory!\n"));
138                 result = NT_STATUS_UNSUCCESSFUL;
139                 goto done;
140         }
141
142         sid_array.num_sids = num_sids;
143         sid_array.sids = TALLOC_ARRAY(mem_ctx, struct lsa_SidPtr, num_sids);
144         if (!sid_array.sids) {
145                 return NT_STATUS_NO_MEMORY;
146         }
147
148         for (i = 0; i<num_sids; i++) {
149                 sid_array.sids[i].sid = dom_sid_dup(mem_ctx, &sids[i]);
150                 if (!sid_array.sids[i].sid) {
151                         return NT_STATUS_NO_MEMORY;
152                 }
153         }
154
155         if (use_lookupsids3) {
156                 struct lsa_TransNameArray2 lsa_names2;
157                 uint32_t n;
158
159                 ZERO_STRUCT(lsa_names2);
160
161                 result = rpccli_lsa_LookupSids3(cli, mem_ctx,
162                                                 &sid_array,
163                                                 &ref_domains,
164                                                 &lsa_names2,
165                                                 level,
166                                                 &count,
167                                                 0,
168                                                 0);
169
170                 if (!NT_STATUS_IS_ERR(result)) {
171                         lsa_names.count = lsa_names2.count;
172                         lsa_names.names = talloc_array(mem_ctx, struct lsa_TranslatedName, lsa_names.count);
173                         if (!lsa_names.names) {
174                                 return NT_STATUS_NO_MEMORY;
175                         }
176                         for (n=0; n < lsa_names.count; n++) {
177                                 lsa_names.names[n].sid_type     = lsa_names2.names[n].sid_type;
178                                 lsa_names.names[n].name         = lsa_names2.names[n].name;
179                                 lsa_names.names[n].sid_index    = lsa_names2.names[n].sid_index;
180                         }
181                 }
182
183         } else {
184                 result = rpccli_lsa_LookupSids(cli, mem_ctx,
185                                                pol,
186                                                &sid_array,
187                                                &ref_domains,
188                                                &lsa_names,
189                                                level,
190                                                &count);
191         }
192
193         DEBUG(10, ("LSA_LOOKUPSIDS returned '%s', mapped count = %d'\n",
194                    nt_errstr(result), count));
195
196         if (!NT_STATUS_IS_OK(result) &&
197             !NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
198             !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
199         {
200                 /* An actual error occured */
201                 goto done;
202         }
203
204         /* Return output parameters */
205
206         if (NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) ||
207             (count == 0))
208         {
209                 for (i = 0; i < num_sids; i++) {
210                         (names)[i] = NULL;
211                         (domains)[i] = NULL;
212                         (types)[i] = SID_NAME_UNKNOWN;
213                 }
214                 result = NT_STATUS_NONE_MAPPED;
215                 goto done;
216         }
217
218         for (i = 0; i < num_sids; i++) {
219                 const char *name, *dom_name;
220                 uint32_t dom_idx = lsa_names.names[i].sid_index;
221
222                 /* Translate optimised name through domain index array */
223
224                 if (dom_idx != 0xffffffff) {
225
226                         dom_name = ref_domains->domains[dom_idx].name.string;
227                         name = lsa_names.names[i].name.string;
228
229                         if (name) {
230                                 (names)[i] = talloc_strdup(names, name);
231                                 if ((names)[i] == NULL) {
232                                         DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
233                                         result = NT_STATUS_UNSUCCESSFUL;
234                                         goto done;
235                                 }
236                         } else {
237                                 (names)[i] = NULL;
238                         }
239                         domains[i] = talloc_strdup(domains,
240                                                    dom_name ? dom_name : "");
241                         (types)[i] = lsa_names.names[i].sid_type;
242                         if (((domains)[i] == NULL)) {
243                                 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
244                                 result = NT_STATUS_UNSUCCESSFUL;
245                                 goto done;
246                         }
247
248                 } else {
249                         (names)[i] = NULL;
250                         (domains)[i] = NULL;
251                         (types)[i] = SID_NAME_UNKNOWN;
252                 }
253         }
254
255 done:
256         TALLOC_FREE(tmp_ctx);
257         return result;
258 }
259
260 /* Lookup a list of sids
261  *
262  * do it the right way: there is a limit (of 20480 for w2k3) entries
263  * returned by this call. when the sids list contains more entries,
264  * empty lists are returned. This version of lsa_lookup_sids passes
265  * the list of sids in hunks of LOOKUP_SIDS_HUNK_SIZE to the lsa call. */
266
267 /* This constant defines the limit of how many sids to look up
268  * in one call (maximum). the limit from the server side is
269  * at 20480 for win2k3, but we keep it at a save 1000 for now. */
270 #define LOOKUP_SIDS_HUNK_SIZE 1000
271
272 static NTSTATUS rpccli_lsa_lookup_sids_generic(struct rpc_pipe_client *cli,
273                                                TALLOC_CTX *mem_ctx,
274                                                struct policy_handle *pol,
275                                                int num_sids,
276                                                const struct dom_sid *sids,
277                                                char ***pdomains,
278                                                char ***pnames,
279                                                enum lsa_SidType **ptypes,
280                                                bool use_lookupsids3)
281 {
282         NTSTATUS result = NT_STATUS_OK;
283         int sids_left = 0;
284         int sids_processed = 0;
285         const struct dom_sid *hunk_sids = sids;
286         char **hunk_domains;
287         char **hunk_names;
288         enum lsa_SidType *hunk_types;
289         char **domains = NULL;
290         char **names = NULL;
291         enum lsa_SidType *types = NULL;
292
293         if (num_sids) {
294                 if (!(domains = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
295                         DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
296                         result = NT_STATUS_NO_MEMORY;
297                         goto fail;
298                 }
299
300                 if (!(names = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
301                         DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
302                         result = NT_STATUS_NO_MEMORY;
303                         goto fail;
304                 }
305
306                 if (!(types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_sids))) {
307                         DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
308                         result = NT_STATUS_NO_MEMORY;
309                         goto fail;
310                 }
311         }
312
313         sids_left = num_sids;
314         hunk_domains = domains;
315         hunk_names = names;
316         hunk_types = types;
317
318         while (sids_left > 0) {
319                 int hunk_num_sids;
320                 NTSTATUS hunk_result = NT_STATUS_OK;
321
322                 hunk_num_sids = ((sids_left > LOOKUP_SIDS_HUNK_SIZE)
323                                 ? LOOKUP_SIDS_HUNK_SIZE
324                                 : sids_left);
325
326                 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
327                            "%d -- %d of %d.\n",
328                            sids_processed,
329                            sids_processed + hunk_num_sids - 1,
330                            num_sids));
331
332                 hunk_result = rpccli_lsa_lookup_sids_noalloc(cli,
333                                                              mem_ctx,
334                                                              pol,
335                                                              hunk_num_sids,
336                                                              hunk_sids,
337                                                              hunk_domains,
338                                                              hunk_names,
339                                                              hunk_types,
340                                                              use_lookupsids3);
341
342                 if (!NT_STATUS_IS_OK(hunk_result) &&
343                     !NT_STATUS_EQUAL(hunk_result, STATUS_SOME_UNMAPPED) &&
344                     !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED))
345                 {
346                         /* An actual error occured */
347                         result = hunk_result;
348                         goto fail;
349                 }
350
351                 /* adapt overall result */
352                 if (( NT_STATUS_IS_OK(result) &&
353                      !NT_STATUS_IS_OK(hunk_result))
354                     ||
355                     ( NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
356                      !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED)))
357                 {
358                         result = STATUS_SOME_UNMAPPED;
359                 }
360
361                 sids_left -= hunk_num_sids;
362                 sids_processed += hunk_num_sids; /* only used in DEBUG */
363                 hunk_sids += hunk_num_sids;
364                 hunk_domains += hunk_num_sids;
365                 hunk_names += hunk_num_sids;
366                 hunk_types += hunk_num_sids;
367         }
368
369         *pdomains = domains;
370         *pnames = names;
371         *ptypes = types;
372         return result;
373
374 fail:
375         TALLOC_FREE(domains);
376         TALLOC_FREE(names);
377         TALLOC_FREE(types);
378         return result;
379 }
380
381 NTSTATUS rpccli_lsa_lookup_sids(struct rpc_pipe_client *cli,
382                                 TALLOC_CTX *mem_ctx,
383                                 struct policy_handle *pol,
384                                 int num_sids,
385                                 const struct dom_sid *sids,
386                                 char ***pdomains,
387                                 char ***pnames,
388                                 enum lsa_SidType **ptypes)
389 {
390         return rpccli_lsa_lookup_sids_generic(cli, mem_ctx, pol, num_sids, sids,
391                                               pdomains, pnames, ptypes, false);
392 }
393
394 NTSTATUS rpccli_lsa_lookup_sids3(struct rpc_pipe_client *cli,
395                                  TALLOC_CTX *mem_ctx,
396                                  struct policy_handle *pol,
397                                  int num_sids,
398                                  const struct dom_sid *sids,
399                                  char ***pdomains,
400                                  char ***pnames,
401                                  enum lsa_SidType **ptypes)
402 {
403         return rpccli_lsa_lookup_sids_generic(cli, mem_ctx, pol, num_sids, sids,
404                                               pdomains, pnames, ptypes, true);
405 }
406
407 /** Lookup a list of names */
408
409 static NTSTATUS rpccli_lsa_lookup_names_generic(struct rpc_pipe_client *cli,
410                                                 TALLOC_CTX *mem_ctx,
411                                                 struct policy_handle *pol, int num_names,
412                                                 const char **names,
413                                                 const char ***dom_names,
414                                                 int level,
415                                                 struct dom_sid **sids,
416                                                 enum lsa_SidType **types,
417                                                 bool use_lookupnames4)
418 {
419         NTSTATUS result;
420         int i;
421         struct lsa_String *lsa_names = NULL;
422         struct lsa_RefDomainList *domains = NULL;
423         struct lsa_TransSidArray sid_array;
424         struct lsa_TransSidArray3 sid_array3;
425         uint32_t count = 0;
426
427         ZERO_STRUCT(sid_array);
428         ZERO_STRUCT(sid_array3);
429
430         lsa_names = TALLOC_ARRAY(mem_ctx, struct lsa_String, num_names);
431         if (!lsa_names) {
432                 return NT_STATUS_NO_MEMORY;
433         }
434
435         for (i=0; i<num_names; i++) {
436                 init_lsa_String(&lsa_names[i], names[i]);
437         }
438
439         if (use_lookupnames4) {
440                 result = rpccli_lsa_LookupNames4(cli, mem_ctx,
441                                                  num_names,
442                                                  lsa_names,
443                                                  &domains,
444                                                  &sid_array3,
445                                                  level,
446                                                  &count,
447                                                  0,
448                                                  0);
449         } else {
450                 result = rpccli_lsa_LookupNames(cli, mem_ctx,
451                                                 pol,
452                                                 num_names,
453                                                 lsa_names,
454                                                 &domains,
455                                                 &sid_array,
456                                                 level,
457                                                 &count);
458         }
459
460         if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
461             NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
462
463                 /* An actual error occured */
464
465                 goto done;
466         }
467
468         /* Return output parameters */
469
470         if (count == 0) {
471                 result = NT_STATUS_NONE_MAPPED;
472                 goto done;
473         }
474
475         if (num_names) {
476                 if (!((*sids = TALLOC_ARRAY(mem_ctx, struct dom_sid, num_names)))) {
477                         DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
478                         result = NT_STATUS_NO_MEMORY;
479                         goto done;
480                 }
481
482                 if (!((*types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_names)))) {
483                         DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
484                         result = NT_STATUS_NO_MEMORY;
485                         goto done;
486                 }
487
488                 if (dom_names != NULL) {
489                         *dom_names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
490                         if (*dom_names == NULL) {
491                                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
492                                 result = NT_STATUS_NO_MEMORY;
493                                 goto done;
494                         }
495                 }
496         } else {
497                 *sids = NULL;
498                 *types = NULL;
499                 if (dom_names != NULL) {
500                         *dom_names = NULL;
501                 }
502         }
503
504         for (i = 0; i < num_names; i++) {
505                 uint32_t dom_idx;
506                 struct dom_sid *sid = &(*sids)[i];
507
508                 if (use_lookupnames4) {
509                         dom_idx         = sid_array3.sids[i].sid_index;
510                         (*types)[i]     = sid_array3.sids[i].sid_type;
511                 } else {
512                         dom_idx         = sid_array.sids[i].sid_index;
513                         (*types)[i]     = sid_array.sids[i].sid_type;
514                 }
515
516                 /* Translate optimised sid through domain index array */
517
518                 if (dom_idx == 0xffffffff) {
519                         /* Nothing to do, this is unknown */
520                         ZERO_STRUCTP(sid);
521                         (*types)[i] = SID_NAME_UNKNOWN;
522                         continue;
523                 }
524
525                 if (use_lookupnames4) {
526                         sid_copy(sid, sid_array3.sids[i].sid);
527                 } else {
528                         sid_copy(sid, domains->domains[dom_idx].sid);
529
530                         if (sid_array.sids[i].rid != 0xffffffff) {
531                                 sid_append_rid(sid, sid_array.sids[i].rid);
532                         }
533                 }
534
535                 if (dom_names == NULL) {
536                         continue;
537                 }
538
539                 (*dom_names)[i] = domains->domains[dom_idx].name.string;
540         }
541
542  done:
543
544         return result;
545 }
546
547 NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
548                                  TALLOC_CTX *mem_ctx,
549                                  struct policy_handle *pol, int num_names,
550                                  const char **names,
551                                  const char ***dom_names,
552                                  int level,
553                                  struct dom_sid **sids,
554                                  enum lsa_SidType **types)
555 {
556         return rpccli_lsa_lookup_names_generic(cli, mem_ctx, pol, num_names,
557                                                names, dom_names, level, sids,
558                                                types, false);
559 }
560
561 NTSTATUS rpccli_lsa_lookup_names4(struct rpc_pipe_client *cli,
562                                   TALLOC_CTX *mem_ctx,
563                                   struct policy_handle *pol, int num_names,
564                                   const char **names,
565                                   const char ***dom_names,
566                                   int level,
567                                   struct dom_sid **sids,
568                                   enum lsa_SidType **types)
569 {
570         return rpccli_lsa_lookup_names_generic(cli, mem_ctx, pol, num_names,
571                                                names, dom_names, level, sids,
572                                                types, true);
573 }