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