Use existing srv_name_slash.
[ira/wip.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
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                                       cli->cli->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 ***pdomains,
246                                 char ***pnames,
247                                 enum lsa_SidType **ptypes)
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;
254         char **hunk_names;
255         enum lsa_SidType *hunk_types;
256         char **domains = NULL;
257         char **names = NULL;
258         enum lsa_SidType *types = NULL;
259
260         if (num_sids) {
261                 if (!(domains = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
262                         DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
263                         result = NT_STATUS_NO_MEMORY;
264                         goto fail;
265                 }
266
267                 if (!(names = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
268                         DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
269                         result = NT_STATUS_NO_MEMORY;
270                         goto fail;
271                 }
272
273                 if (!(types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_sids))) {
274                         DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
275                         result = NT_STATUS_NO_MEMORY;
276                         goto fail;
277                 }
278         }
279
280         sids_left = num_sids;
281         hunk_domains = domains;
282         hunk_names = names;
283         hunk_types = types;
284
285         while (sids_left > 0) {
286                 int hunk_num_sids;
287                 NTSTATUS hunk_result = NT_STATUS_OK;
288
289                 hunk_num_sids = ((sids_left > LOOKUP_SIDS_HUNK_SIZE)
290                                 ? LOOKUP_SIDS_HUNK_SIZE
291                                 : sids_left);
292
293                 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
294                            "%d -- %d of %d.\n",
295                            sids_processed,
296                            sids_processed + hunk_num_sids - 1,
297                            num_sids));
298
299                 hunk_result = rpccli_lsa_lookup_sids_noalloc(cli,
300                                                              mem_ctx,
301                                                              pol,
302                                                              hunk_num_sids,
303                                                              hunk_sids,
304                                                              hunk_domains,
305                                                              hunk_names,
306                                                              hunk_types);
307
308                 if (!NT_STATUS_IS_OK(hunk_result) &&
309                     !NT_STATUS_EQUAL(hunk_result, STATUS_SOME_UNMAPPED) &&
310                     !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED))
311                 {
312                         /* An actual error occured */
313                         result = hunk_result;
314                         goto fail;
315                 }
316
317                 /* adapt overall result */
318                 if (( NT_STATUS_IS_OK(result) &&
319                      !NT_STATUS_IS_OK(hunk_result))
320                     ||
321                     ( NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
322                      !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED)))
323                 {
324                         result = STATUS_SOME_UNMAPPED;
325                 }
326
327                 sids_left -= hunk_num_sids;
328                 sids_processed += hunk_num_sids; /* only used in DEBUG */
329                 hunk_sids += hunk_num_sids;
330                 hunk_domains += hunk_num_sids;
331                 hunk_names += hunk_num_sids;
332                 hunk_types += hunk_num_sids;
333         }
334
335         *pdomains = domains;
336         *pnames = names;
337         *ptypes = types;
338         return result;
339
340 fail:
341         TALLOC_FREE(domains);
342         TALLOC_FREE(names);
343         TALLOC_FREE(types);
344         return result;
345 }
346
347 /** Lookup a list of names */
348
349 NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
350                                  TALLOC_CTX *mem_ctx,
351                                  POLICY_HND *pol, int num_names,
352                                  const char **names,
353                                  const char ***dom_names,
354                                  int level,
355                                  DOM_SID **sids,
356                                  enum lsa_SidType **types)
357 {
358         NTSTATUS result;
359         int i;
360         struct lsa_String *lsa_names = NULL;
361         struct lsa_RefDomainList *domains = NULL;
362         struct lsa_TransSidArray sid_array;
363         uint32_t count = 0;
364
365         ZERO_STRUCT(sid_array);
366
367         lsa_names = TALLOC_ARRAY(mem_ctx, struct lsa_String, num_names);
368         if (!lsa_names) {
369                 return NT_STATUS_NO_MEMORY;
370         }
371
372         for (i=0; i<num_names; i++) {
373                 init_lsa_String(&lsa_names[i], names[i]);
374         }
375
376         result = rpccli_lsa_LookupNames(cli, mem_ctx,
377                                         pol,
378                                         num_names,
379                                         lsa_names,
380                                         &domains,
381                                         &sid_array,
382                                         level,
383                                         &count);
384
385         if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
386             NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
387
388                 /* An actual error occured */
389
390                 goto done;
391         }
392
393         /* Return output parameters */
394
395         if (count == 0) {
396                 result = NT_STATUS_NONE_MAPPED;
397                 goto done;
398         }
399
400         if (num_names) {
401                 if (!((*sids = TALLOC_ARRAY(mem_ctx, DOM_SID, num_names)))) {
402                         DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
403                         result = NT_STATUS_NO_MEMORY;
404                         goto done;
405                 }
406
407                 if (!((*types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_names)))) {
408                         DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
409                         result = NT_STATUS_NO_MEMORY;
410                         goto done;
411                 }
412
413                 if (dom_names != NULL) {
414                         *dom_names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
415                         if (*dom_names == NULL) {
416                                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
417                                 result = NT_STATUS_NO_MEMORY;
418                                 goto done;
419                         }
420                 }
421         } else {
422                 *sids = NULL;
423                 *types = NULL;
424                 if (dom_names != NULL) {
425                         *dom_names = NULL;
426                 }
427         }
428
429         for (i = 0; i < num_names; i++) {
430                 uint32_t dom_idx = sid_array.sids[i].sid_index;
431                 uint32_t dom_rid = sid_array.sids[i].rid;
432                 DOM_SID *sid = &(*sids)[i];
433
434                 /* Translate optimised sid through domain index array */
435
436                 if (dom_idx == 0xffffffff) {
437                         /* Nothing to do, this is unknown */
438                         ZERO_STRUCTP(sid);
439                         (*types)[i] = SID_NAME_UNKNOWN;
440                         continue;
441                 }
442
443                 sid_copy(sid, domains->domains[dom_idx].sid);
444
445                 if (dom_rid != 0xffffffff) {
446                         sid_append_rid(sid, dom_rid);
447                 }
448
449                 (*types)[i] = sid_array.sids[i].sid_type;
450
451                 if (dom_names == NULL) {
452                         continue;
453                 }
454
455                 (*dom_names)[i] = domains->domains[dom_idx].name.string;
456         }
457
458  done:
459
460         return result;
461 }
462
463 #if 0
464
465 /** An example of how to use the routines in this file.  Fetch a DOMAIN
466     sid. Does complete cli setup / teardown anonymously. */
467
468 bool fetch_domain_sid( char *domain, char *remote_machine, DOM_SID *psid)
469 {
470         struct cli_state cli;
471         NTSTATUS result;
472         POLICY_HND lsa_pol;
473         bool ret = False;
474
475         ZERO_STRUCT(cli);
476         if(cli_initialise(&cli) == False) {
477                 DEBUG(0,("fetch_domain_sid: unable to initialize client connection.\n"));
478                 return False;
479         }
480
481         if(!resolve_name( remote_machine, &cli.dest_ip, 0x20)) {
482                 DEBUG(0,("fetch_domain_sid: Can't resolve address for %s\n", remote_machine));
483                 goto done;
484         }
485
486         if (!cli_connect(&cli, remote_machine, &cli.dest_ip)) {
487                 DEBUG(0,("fetch_domain_sid: unable to connect to SMB server on \
488 machine %s. Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
489                 goto done;
490         }
491
492         if (!attempt_netbios_session_request(&cli, global_myname(), remote_machine, &cli.dest_ip)) {
493                 DEBUG(0,("fetch_domain_sid: machine %s rejected the NetBIOS session request.\n",
494                         remote_machine));
495                 goto done;
496         }
497
498         cli.protocol = PROTOCOL_NT1;
499
500         if (!cli_negprot(&cli)) {
501                 DEBUG(0,("fetch_domain_sid: machine %s rejected the negotiate protocol. \
502 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
503                 goto done;
504         }
505
506         if (cli.protocol != PROTOCOL_NT1) {
507                 DEBUG(0,("fetch_domain_sid: machine %s didn't negotiate NT protocol.\n",
508                         remote_machine));
509                 goto done;
510         }
511
512         /*
513          * Do an anonymous session setup.
514          */
515
516         if (!cli_session_setup(&cli, "", "", 0, "", 0, "")) {
517                 DEBUG(0,("fetch_domain_sid: machine %s rejected the session setup. \
518 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
519                 goto done;
520         }
521
522         if (!(cli.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
523                 DEBUG(0,("fetch_domain_sid: machine %s isn't in user level security mode\n",
524                         remote_machine));
525                 goto done;
526         }
527
528         if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
529                 DEBUG(0,("fetch_domain_sid: machine %s rejected the tconX on the IPC$ share. \
530 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
531                 goto done;
532         }
533
534         /* Fetch domain sid */
535
536         if (!cli_nt_session_open(&cli, PI_LSARPC)) {
537                 DEBUG(0, ("fetch_domain_sid: Error connecting to SAM pipe\n"));
538                 goto done;
539         }
540
541         result = cli_lsa_open_policy(&cli, cli.mem_ctx, True, SEC_RIGHTS_QUERY_VALUE, &lsa_pol);
542         if (!NT_STATUS_IS_OK(result)) {
543                 DEBUG(0, ("fetch_domain_sid: Error opening lsa policy handle. %s\n",
544                         nt_errstr(result) ));
545                 goto done;
546         }
547
548         result = cli_lsa_query_info_policy(&cli, cli.mem_ctx, &lsa_pol, 5, domain, psid);
549         if (!NT_STATUS_IS_OK(result)) {
550                 DEBUG(0, ("fetch_domain_sid: Error querying lsa policy handle. %s\n",
551                         nt_errstr(result) ));
552                 goto done;
553         }
554
555         ret = True;
556
557   done:
558
559         cli_shutdown(&cli);
560         return ret;
561 }
562
563 #endif