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