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