In cli_lsa_lookup_sids() don't append a separator character between domain
[kai/samba.git] / source3 / 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
272                         rpcstr_pull_unistr2_fstring(
273                                 dom_name, &ref.ref_dom[dom_idx].uni_dom_name);
274                         rpcstr_pull_unistr2_fstring(
275                                 name, &t_names.uni_name[i]);
276
277                         slprintf(full_name, sizeof(full_name) - 1,
278                                  "%s%s%s", dom_name, 
279                                  (dom_name[0] && name[0]) ? 
280                                  lp_winbind_separator() : "", name);
281
282                         (*names)[i] = talloc_strdup(mem_ctx, full_name);
283                         (*types)[i] = t_names.name[i].sid_name_use;
284
285                 } else {
286                         (*names)[i] = NULL;
287                         (*types)[i] = SID_NAME_UNKNOWN;
288                 }
289         }
290
291  done:
292         prs_mem_free(&qbuf);
293         prs_mem_free(&rbuf);
294
295         return result;
296 }
297
298 /* Lookup a list of names */
299
300 uint32 cli_lsa_lookup_names(struct cli_state *cli, TALLOC_CTX *mem_ctx,
301                             POLICY_HND *pol, int num_names, char **names, 
302                             DOM_SID **sids, uint32 **types, int *num_sids)
303 {
304         prs_struct qbuf, rbuf;
305         LSA_Q_LOOKUP_NAMES q;
306         LSA_R_LOOKUP_NAMES r;
307         DOM_R_REF ref;
308         uint32 result;
309         int i;
310         
311         ZERO_STRUCT(q);
312         ZERO_STRUCT(r);
313
314         /* Initialise parse structures */
315
316         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
317         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
318
319         /* Marshall data and send request */
320
321         init_q_lookup_names(mem_ctx, &q, pol, num_names, names);
322
323         if (!lsa_io_q_lookup_names("", &q, &qbuf, 0) ||
324             !rpc_api_pipe_req(cli, LSA_LOOKUPNAMES, &qbuf, &rbuf)) {
325                 result = NT_STATUS_UNSUCCESSFUL;
326                 goto done;
327         }
328         
329         /* Unmarshall response */
330
331         ZERO_STRUCT(ref);
332         r.dom_ref = &ref;
333
334         if (!lsa_io_r_lookup_names("", &r, &rbuf, 0)) {
335                 result = NT_STATUS_UNSUCCESSFUL;
336                 goto done;
337         }
338
339         result = r.status;
340
341         if (result != NT_STATUS_NOPROBLEMO && 
342             result != (0xC0000000 | NT_STATUS_NONE_MAPPED)) {
343
344                 /* An actual error occured */
345
346                 goto done;
347         }
348
349         result = NT_STATUS_NOPROBLEMO;
350
351         /* Return output parameters */
352
353         (*num_sids) = r.num_entries;
354
355         if (!((*sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) *
356                                          r.num_entries)))) {
357                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
358                 result = NT_STATUS_UNSUCCESSFUL;
359                 goto done;
360         }
361
362         if (!((*types = (uint32 *)talloc(mem_ctx, sizeof(uint32) *
363                                           r.num_entries)))) {
364                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
365                 result = NT_STATUS_UNSUCCESSFUL;
366                 goto done;
367         }
368
369         for (i = 0; i < r.num_entries; i++) {
370                 DOM_RID2 *t_rids = r.dom_rid;
371                 uint32 dom_idx = t_rids[i].rid_idx;
372                 uint32 dom_rid = t_rids[i].rid;
373                 DOM_SID *sid = &(*sids)[i];
374
375                 /* Translate optimised sid through domain index array */
376
377                 if (dom_idx != 0xffffffff) {
378
379                         sid_copy(sid, &ref.ref_dom[dom_idx].ref_dom.sid);
380
381                         if (dom_rid != 0xffffffff) {
382                                 sid_append_rid(sid, dom_rid);
383                         }
384
385                         (*types)[i] = t_rids[i].type;
386                 } else {
387                         ZERO_STRUCTP(sid);
388                         (*types)[i] = SID_NAME_UNKNOWN;
389                 }
390         }
391
392  done:
393         prs_mem_free(&qbuf);
394         prs_mem_free(&rbuf);
395
396         return result;
397 }
398
399 /* Query info policy */
400
401 uint32 cli_lsa_query_info_policy(struct cli_state *cli, TALLOC_CTX *mem_ctx,
402                                  POLICY_HND *pol, uint16 info_class, 
403                                  fstring domain_name, DOM_SID *domain_sid)
404 {
405         prs_struct qbuf, rbuf;
406         LSA_Q_QUERY_INFO q;
407         LSA_R_QUERY_INFO r;
408         uint32 result;
409
410         ZERO_STRUCT(q);
411         ZERO_STRUCT(r);
412
413         /* Initialise parse structures */
414
415         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
416         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
417
418         /* Marshall data and send request */
419
420         init_q_query(&q, pol, info_class);
421
422         if (!lsa_io_q_query("", &q, &qbuf, 0) ||
423             !rpc_api_pipe_req(cli, LSA_QUERYINFOPOLICY, &qbuf, &rbuf)) {
424                 result = NT_STATUS_UNSUCCESSFUL;
425                 goto done;
426         }
427
428         /* Unmarshall response */
429
430         if (!lsa_io_r_query("", &r, &rbuf, 0)) {
431                 result = NT_STATUS_UNSUCCESSFUL;
432                 goto done;
433         }
434
435         if ((result = r.status) != NT_STATUS_NOPROBLEMO) {
436                 goto done;
437         }
438
439         /* Return output parameters */
440
441         ZERO_STRUCTP(domain_sid);
442         domain_name[0] = '\0';
443
444         switch (info_class) {
445
446         case 3:
447                 if (r.dom.id3.buffer_dom_name != 0) {
448                         unistr2_to_ascii(domain_name,
449                                          &r.dom.id3.
450                                          uni_domain_name,
451                                          sizeof (fstring) - 1);
452                 }
453
454                 if (r.dom.id3.buffer_dom_sid != 0) {
455                         *domain_sid = r.dom.id3.dom_sid.sid;
456                 }
457
458                 break;
459
460         case 5:
461                 
462                 if (r.dom.id5.buffer_dom_name != 0) {
463                         unistr2_to_ascii(domain_name, &r.dom.id5.
464                                          uni_domain_name,
465                                          sizeof (fstring) - 1);
466                 }
467                         
468                 if (r.dom.id5.buffer_dom_sid != 0) {
469                         *domain_sid = r.dom.id5.dom_sid.sid;
470                 }
471
472                 break;
473                 
474         default:
475                 DEBUG(3, ("unknown info class %d\n", info_class));
476                 break;                
477         }
478         
479  done:
480         prs_mem_free(&qbuf);
481         prs_mem_free(&rbuf);
482
483         return result;
484 }
485
486 /* Enumerate list of trusted domains */
487
488 uint32 cli_lsa_enum_trust_dom(struct cli_state *cli, TALLOC_CTX *mem_ctx,
489                               POLICY_HND *pol, uint32 *enum_ctx, 
490                               uint32 *num_domains, char ***domain_names, 
491                               DOM_SID **domain_sids)
492 {
493         prs_struct qbuf, rbuf;
494         LSA_Q_ENUM_TRUST_DOM q;
495         LSA_R_ENUM_TRUST_DOM r;
496         uint32 result;
497         int i;
498
499         ZERO_STRUCT(q);
500         ZERO_STRUCT(r);
501
502         /* Initialise parse structures */
503
504         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
505         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
506
507         /* Marshall data and send request */
508
509         init_q_enum_trust_dom(&q, pol, *enum_ctx, 0xffffffff);
510
511         if (!lsa_io_q_enum_trust_dom("", &q, &qbuf, 0) ||
512             !rpc_api_pipe_req(cli, LSA_ENUMTRUSTDOM, &qbuf, &rbuf)) {
513                 result = NT_STATUS_UNSUCCESSFUL;
514                 goto done;
515         }
516
517         /* Unmarshall response */
518
519         if (!lsa_io_r_enum_trust_dom("", &r, &rbuf, 0)) {
520                 result = NT_STATUS_UNSUCCESSFUL;
521                 goto done;
522         }
523
524         result = r.status;
525
526         /* For some undocumented reason this function sometimes returns
527            0x8000001a (NT_STATUS_UNABLE_TO_FREE_VM) so we ignore it and
528            pretend everything is OK. */
529
530         if (result != NT_STATUS_NOPROBLEMO && 
531             result != NT_STATUS_UNABLE_TO_FREE_VM) {
532
533                 /* An actual error ocured */
534
535                 goto done;
536         }
537
538         result = NT_STATUS_NOPROBLEMO;
539
540         /* Return output parameters */
541
542         if (r.num_domains) {
543
544                 /* Allocate memory for trusted domain names and sids */
545
546                 *domain_names = (char **)talloc(mem_ctx, sizeof(char *) *
547                                                 r.num_domains);
548
549                 if (!*domain_names) {
550                         DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
551                         result = NT_STATUS_UNSUCCESSFUL;
552                         goto done;
553                 }
554
555                 *domain_sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) *
556                                                  r.num_domains);
557                 if (!domain_sids) {
558                         DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
559                         result = NT_STATUS_UNSUCCESSFUL;
560                         goto done;
561                 }
562
563                 /* Copy across names and sids */
564
565                 for (i = 0; i < r.num_domains; i++) {
566                         fstring tmp;
567
568                         unistr2_to_ascii(tmp, &r.uni_domain_name[i], 
569                                          sizeof(tmp) - 1);
570                         (*domain_names)[i] = strdup(tmp);
571                         sid_copy(&(*domain_sids)[i], &r.domain_sid[i].sid);
572                 }
573         }
574
575         *num_domains = r.num_domains;
576         *enum_ctx = r.enum_context;
577
578  done:
579         prs_mem_free(&qbuf);
580         prs_mem_free(&rbuf);
581
582         return result;
583 }