Cleanup of cli_lsa_enum_trust_dom(). talloc() doesn't like attempts to
[ira/wip.git] / source / libsmb / cli_lsarpc.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.2
4    RPC pipe client
5    Copyright (C) Tim Potter                        2000-2001,
6    Copyright (C) Andrew Tridgell              1992-1997,2000,
7    Copyright (C) Luke Kenneth Casson Leighton 1996-1997,2000,
8    Copyright (C) Paul Ashton                       1997,2000,
9    Copyright (C) Elrond                                 2000.
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 2 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, write to the Free Software
23    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26 #include "includes.h"
27
28 /* Opens a SMB connection to the lsa pipe */
29
30 struct cli_state *cli_lsa_initialise(struct cli_state *cli, char *system_name,
31                                      struct ntuser_creds *creds)
32 {
33         struct in_addr dest_ip;
34         struct nmb_name calling, called;
35         fstring dest_host;
36         extern pstring global_myname;
37         struct ntuser_creds anon;
38
39         /* Initialise cli_state information */
40
41         if (!cli_initialise(cli)) {
42                 return NULL;
43         }
44
45         if (!creds) {
46                 ZERO_STRUCT(anon);
47                 anon.pwd.null_pwd = 1;
48                 creds = &anon;
49         }
50
51         cli_init_creds(cli, creds);
52
53         /* Establish a SMB connection */
54
55         if (!resolve_srv_name(system_name, dest_host, &dest_ip)) {
56                 return NULL;
57         }
58
59         make_nmb_name(&called, dns_to_netbios_name(dest_host), 0x20);
60         make_nmb_name(&calling, dns_to_netbios_name(global_myname), 0);
61
62         if (!cli_establish_connection(cli, dest_host, &dest_ip, &calling, 
63                                       &called, "IPC$", "IPC", False, True)) {
64                 return NULL;
65         }
66
67         /* Open a NT session thingy */
68
69         if (!cli_nt_session_open(cli, PIPE_LSARPC)) {
70                 cli_shutdown(cli);
71                 return NULL;
72         }
73
74         return cli;
75 }
76
77 /* Shut down a SMB connection to the LSA pipe */
78
79 void cli_lsa_shutdown(struct cli_state *cli)
80 {
81         if (cli->fd != -1) cli_ulogoff(cli);
82         cli_shutdown(cli);
83 }
84
85 /* Open a LSA policy handle */
86
87 uint32 cli_lsa_open_policy(struct cli_state *cli, TALLOC_CTX *mem_ctx,
88                            BOOL sec_qos, uint32 des_access, POLICY_HND *pol)
89 {
90         prs_struct qbuf, rbuf;
91         LSA_Q_OPEN_POL q;
92         LSA_R_OPEN_POL r;
93         LSA_SEC_QOS qos;
94         uint32 result;
95
96         ZERO_STRUCT(q);
97         ZERO_STRUCT(r);
98
99         /* Initialise parse structures */
100
101         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
102         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
103
104         /* Initialise input parameters */
105
106         if (sec_qos) {
107                 init_lsa_sec_qos(&qos, 2, 1, 0, des_access);
108                 init_q_open_pol(&q, '\\', 0, des_access, &qos);
109         } else {
110                 init_q_open_pol(&q, '\\', 0, des_access, NULL);
111         }
112
113         /* Marshall data and send request */
114
115         if (!lsa_io_q_open_pol("", &q, &qbuf, 0) ||
116             !rpc_api_pipe_req(cli, LSA_OPENPOLICY, &qbuf, &rbuf)) {
117                 result = NT_STATUS_UNSUCCESSFUL;
118                 goto done;
119         }
120
121         /* Unmarshall response */
122
123         if (!lsa_io_r_open_pol("", &r, &rbuf, 0)) {
124                 result = NT_STATUS_UNSUCCESSFUL;
125                 goto done;
126         }
127
128         /* Return output parameters */
129
130         if ((result = r.status) == NT_STATUS_NOPROBLEMO) {
131                 *pol = r.pol;
132         }
133
134  done:
135         prs_mem_free(&qbuf);
136         prs_mem_free(&rbuf);
137
138         return result;
139 }
140
141 /* Close a LSA policy handle */
142
143 uint32 cli_lsa_close(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
144                      POLICY_HND *pol)
145 {
146         prs_struct qbuf, rbuf;
147         LSA_Q_CLOSE q;
148         LSA_R_CLOSE r;
149         uint32 result;
150
151         ZERO_STRUCT(q);
152         ZERO_STRUCT(r);
153
154         /* Initialise parse structures */
155
156         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
157         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
158
159         /* Marshall data and send request */
160
161         init_lsa_q_close(&q, pol);
162
163         if (!lsa_io_q_close("", &q, &qbuf, 0) ||
164             !rpc_api_pipe_req(cli, LSA_CLOSE, &qbuf, &rbuf)) {
165                 result = NT_STATUS_UNSUCCESSFUL;
166                 goto done;
167         }
168
169         /* Unmarshall response */
170
171         if (!lsa_io_r_close("", &r, &rbuf, 0)) {
172                 result = NT_STATUS_UNSUCCESSFUL;
173                 goto done;
174         }
175
176         /* Return output parameters */
177
178         if ((result = r.status) == NT_STATUS_NOPROBLEMO) {
179                 *pol = r.pol;
180         }
181
182  done:
183         prs_mem_free(&qbuf);
184         prs_mem_free(&rbuf);
185
186         return result;
187 }
188
189 /* Lookup a list of sids */
190
191 uint32 cli_lsa_lookup_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
192                            POLICY_HND *pol, int num_sids, DOM_SID *sids, 
193                            char ***names, uint32 **types, int *num_names)
194 {
195         prs_struct qbuf, rbuf;
196         LSA_Q_LOOKUP_SIDS q;
197         LSA_R_LOOKUP_SIDS r;
198         DOM_R_REF ref;
199         LSA_TRANS_NAME_ENUM t_names;
200         uint32 result;
201         int i;
202
203         ZERO_STRUCT(q);
204         ZERO_STRUCT(r);
205
206         /* Initialise parse structures */
207
208         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
209         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
210
211         /* Marshall data and send request */
212
213         init_q_lookup_sids(mem_ctx, &q, pol, num_sids, sids, 1);
214
215         if (!lsa_io_q_lookup_sids("", &q, &qbuf, 0) ||
216             !rpc_api_pipe_req(cli, LSA_LOOKUPSIDS, &qbuf, &rbuf)) {
217                 result = NT_STATUS_UNSUCCESSFUL;
218                 goto done;
219         }
220
221         /* Unmarshall response */
222
223         ZERO_STRUCT(ref);
224         ZERO_STRUCT(t_names);
225
226         r.dom_ref = &ref;
227         r.names = &t_names;
228
229         if (!lsa_io_r_lookup_sids("", &r, &rbuf, 0)) {
230                 result = NT_STATUS_UNSUCCESSFUL;
231                 goto done;
232         }
233
234         result = r.status;
235
236         if (result != NT_STATUS_NOPROBLEMO && result != 0x00000107 &&
237             result != (0xC0000000 | NT_STATUS_NONE_MAPPED)) {
238                 
239                 /* An actual error occured */
240
241                 goto done;
242         }
243
244         result = NT_STATUS_NOPROBLEMO;
245
246         /* Return output parameters */
247
248         (*num_names) = r.names->num_entries;
249         
250         if (!((*names) = (char **)talloc(mem_ctx, sizeof(char *) * 
251                                          r.names->num_entries))) {
252                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
253                 result = NT_STATUS_UNSUCCESSFUL;
254                 goto done;
255         }
256
257         if (!((*types) = (uint32 *)talloc(mem_ctx, sizeof(uint32) * 
258                                       r.names->num_entries))) {
259                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
260                 result = NT_STATUS_UNSUCCESSFUL;
261                 goto done;
262         }
263                 
264         for (i = 0; i < r.names->num_entries; i++) {
265                 fstring name, dom_name, full_name;
266                 uint32 dom_idx = t_names.name[i].domain_idx;
267
268                 /* Translate optimised name through domain index array */
269
270                 if (dom_idx != 0xffffffff) {
271                         unistr2_to_ascii(dom_name, 
272                                          &ref.ref_dom[dom_idx].uni_dom_name, 
273                                          sizeof(dom_name)- 1);
274                         unistr2_to_ascii(name, &t_names.uni_name[i], 
275                                          sizeof(name) - 1);
276
277                         slprintf(full_name, sizeof(full_name) - 1,
278                                  "%s%s%s", dom_name, dom_name[0] ? 
279                                  "\\" : "", name);
280
281                         (*names)[i] = talloc_strdup(mem_ctx, full_name);
282                         (*types)[i] = t_names.name[i].sid_name_use;
283                 } else {
284                         (*names)[i] = NULL;
285                         (*types)[i] = SID_NAME_UNKNOWN;
286                 }
287         }
288
289  done:
290         prs_mem_free(&qbuf);
291         prs_mem_free(&rbuf);
292
293         return result;
294 }
295
296 /* Lookup a list of names */
297
298 uint32 cli_lsa_lookup_names(struct cli_state *cli, TALLOC_CTX *mem_ctx,
299                             POLICY_HND *pol, int num_names, char **names, 
300                             DOM_SID **sids, uint32 **types, int *num_sids)
301 {
302         prs_struct qbuf, rbuf;
303         LSA_Q_LOOKUP_NAMES q;
304         LSA_R_LOOKUP_NAMES r;
305         DOM_R_REF ref;
306         uint32 result;
307         int i;
308         
309         ZERO_STRUCT(q);
310         ZERO_STRUCT(r);
311
312         /* Initialise parse structures */
313
314         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
315         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
316
317         /* Marshall data and send request */
318
319         init_q_lookup_names(mem_ctx, &q, pol, num_names, names);
320
321         if (!lsa_io_q_lookup_names("", &q, &qbuf, 0) ||
322             !rpc_api_pipe_req(cli, LSA_LOOKUPNAMES, &qbuf, &rbuf)) {
323                 result = NT_STATUS_UNSUCCESSFUL;
324                 goto done;
325         }
326         
327         /* Unmarshall response */
328
329         ZERO_STRUCT(ref);
330         r.dom_ref = &ref;
331
332         if (!lsa_io_r_lookup_names("", &r, &rbuf, 0)) {
333                 result = NT_STATUS_UNSUCCESSFUL;
334                 goto done;
335         }
336
337         result = r.status;
338
339         if (result != NT_STATUS_NOPROBLEMO && 
340             result != (0xC0000000 | NT_STATUS_NONE_MAPPED)) {
341
342                 /* An actual error occured */
343
344                 goto done;
345         }
346
347         result = NT_STATUS_NOPROBLEMO;
348
349         /* Return output parameters */
350
351         (*num_sids) = r.num_entries;
352
353         if (!((*sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) *
354                                          r.num_entries)))) {
355                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
356                 result = NT_STATUS_UNSUCCESSFUL;
357                 goto done;
358         }
359
360         if (!((*types = (uint32 *)talloc(mem_ctx, sizeof(uint32) *
361                                           r.num_entries)))) {
362                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
363                 result = NT_STATUS_UNSUCCESSFUL;
364                 goto done;
365         }
366
367         for (i = 0; i < r.num_entries; i++) {
368                 DOM_RID2 *t_rids = r.dom_rid;
369                 uint32 dom_idx = t_rids[i].rid_idx;
370                 uint32 dom_rid = t_rids[i].rid;
371                 DOM_SID *sid = &(*sids)[i];
372
373                 /* Translate optimised sid through domain index array */
374
375                 if (dom_idx != 0xffffffff) {
376
377                         sid_copy(sid, &ref.ref_dom[dom_idx].ref_dom.sid);
378
379                         if (dom_rid != 0xffffffff) {
380                                 sid_append_rid(sid, dom_rid);
381                         }
382
383                         (*types)[i] = t_rids[i].type;
384                 } else {
385                         ZERO_STRUCTP(sid);
386                         (*types)[i] = SID_NAME_UNKNOWN;
387                 }
388         }
389
390  done:
391         prs_mem_free(&qbuf);
392         prs_mem_free(&rbuf);
393
394         return result;
395 }
396
397 /* Query info policy */
398
399 uint32 cli_lsa_query_info_policy(struct cli_state *cli, TALLOC_CTX *mem_ctx,
400                                  POLICY_HND *pol, uint16 info_class, 
401                                  fstring domain_name, DOM_SID *domain_sid)
402 {
403         prs_struct qbuf, rbuf;
404         LSA_Q_QUERY_INFO q;
405         LSA_R_QUERY_INFO r;
406         uint32 result;
407
408         ZERO_STRUCT(q);
409         ZERO_STRUCT(r);
410
411         /* Initialise parse structures */
412
413         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
414         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
415
416         /* Marshall data and send request */
417
418         init_q_query(&q, pol, info_class);
419
420         if (!lsa_io_q_query("", &q, &qbuf, 0) ||
421             !rpc_api_pipe_req(cli, LSA_QUERYINFOPOLICY, &qbuf, &rbuf)) {
422                 result = NT_STATUS_UNSUCCESSFUL;
423                 goto done;
424         }
425
426         /* Unmarshall response */
427
428         if (!lsa_io_r_query("", &r, &rbuf, 0)) {
429                 result = NT_STATUS_UNSUCCESSFUL;
430                 goto done;
431         }
432
433         if ((result = r.status) != NT_STATUS_NOPROBLEMO) {
434                 goto done;
435         }
436
437         /* Return output parameters */
438
439         ZERO_STRUCTP(domain_sid);
440         domain_name[0] = '\0';
441
442         switch (info_class) {
443
444         case 3:
445                 if (r.dom.id3.buffer_dom_name != 0) {
446                         unistr2_to_ascii(domain_name,
447                                          &r.dom.id3.
448                                          uni_domain_name,
449                                          sizeof (fstring) - 1);
450                 }
451
452                 if (r.dom.id3.buffer_dom_sid != 0) {
453                         *domain_sid = r.dom.id3.dom_sid.sid;
454                 }
455
456                 break;
457
458         case 5:
459                 
460                 if (r.dom.id5.buffer_dom_name != 0) {
461                         unistr2_to_ascii(domain_name, &r.dom.id5.
462                                          uni_domain_name,
463                                          sizeof (fstring) - 1);
464                 }
465                         
466                 if (r.dom.id5.buffer_dom_sid != 0) {
467                         *domain_sid = r.dom.id5.dom_sid.sid;
468                 }
469
470                 break;
471                 
472         default:
473                 DEBUG(3, ("unknown info class %d\n", info_class));
474                 break;                
475         }
476         
477  done:
478         prs_mem_free(&qbuf);
479         prs_mem_free(&rbuf);
480
481         return result;
482 }
483
484 /* Enumerate list of trusted domains */
485
486 uint32 cli_lsa_enum_trust_dom(struct cli_state *cli, TALLOC_CTX *mem_ctx,
487                               POLICY_HND *pol, uint32 *enum_ctx, 
488                               uint32 *num_domains, char ***domain_names, 
489                               DOM_SID **domain_sids)
490 {
491         prs_struct qbuf, rbuf;
492         LSA_Q_ENUM_TRUST_DOM q;
493         LSA_R_ENUM_TRUST_DOM r;
494         uint32 result;
495         int i;
496
497         ZERO_STRUCT(q);
498         ZERO_STRUCT(r);
499
500         /* Initialise parse structures */
501
502         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
503         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
504
505         /* Marshall data and send request */
506
507         init_q_enum_trust_dom(&q, pol, *enum_ctx, 0xffffffff);
508
509         if (!lsa_io_q_enum_trust_dom("", &q, &qbuf, 0) ||
510             !rpc_api_pipe_req(cli, LSA_ENUMTRUSTDOM, &qbuf, &rbuf)) {
511                 result = NT_STATUS_UNSUCCESSFUL;
512                 goto done;
513         }
514
515         /* Unmarshall response */
516
517         if (!lsa_io_r_enum_trust_dom("", &r, &rbuf, 0)) {
518                 result = NT_STATUS_UNSUCCESSFUL;
519                 goto done;
520         }
521
522         result = r.status;
523
524         /* For some undocumented reason this function sometimes returns
525            0x8000001a (NT_STATUS_UNABLE_TO_FREE_VM) so we ignore it and
526            pretend everything is OK. */
527
528         if (result != NT_STATUS_NOPROBLEMO && 
529             result != NT_STATUS_UNABLE_TO_FREE_VM) {
530
531                 /* An actual error ocured */
532
533                 goto done;
534         }
535
536         result = NT_STATUS_NOPROBLEMO;
537
538         /* Return output parameters */
539
540         if (r.num_domains) {
541
542                 /* Allocate memory for trusted domain names and sids */
543
544                 *domain_names = (char **)talloc(mem_ctx, sizeof(char *) *
545                                                 r.num_domains);
546
547                 if (!*domain_names) {
548                         DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
549                         result = NT_STATUS_UNSUCCESSFUL;
550                         goto done;
551                 }
552
553                 *domain_sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) *
554                                                  r.num_domains);
555                 if (!domain_sids) {
556                         DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
557                         result = NT_STATUS_UNSUCCESSFUL;
558                         goto done;
559                 }
560
561                 /* Copy across names and sids */
562
563                 for (i = 0; i < r.num_domains; i++) {
564                         fstring tmp;
565
566                         unistr2_to_ascii(tmp, &r.uni_domain_name[i], 
567                                          sizeof(tmp) - 1);
568                         (*domain_names)[i] = strdup(tmp);
569                         sid_copy(&(*domain_sids)[i], &r.domain_sid[i].sid);
570                 }
571         }
572
573         *num_domains = r.num_domains;
574         *enum_ctx = r.enum_context;
575
576  done:
577         prs_mem_free(&qbuf);
578         prs_mem_free(&rbuf);
579
580         return result;
581 }