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