This fixes a number of ADS problems, particularly with netbiosless
[kai/samba-autobuild/.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) Luke Kenneth Casson Leighton 1996-1997,2000,
7    Copyright (C) Paul Ashton                       1997,2000,
8    Copyright (C) Elrond                                 2000,
9    Copyright (C) Rafal Szczesniak                       2002
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 /** @defgroup lsa LSA - Local Security Architecture
29  *  @ingroup rpc_client
30  *
31  * @{
32  **/
33
34 /**
35  * @file cli_lsarpc.c
36  *
37  * RPC client routines for the LSA RPC pipe.  LSA means "local
38  * security authority", which is half of a password database.
39  **/
40
41 /** Open a LSA policy handle
42  *
43  * @param cli Handle on an initialised SMB connection */
44
45 NTSTATUS cli_lsa_open_policy(struct cli_state *cli, TALLOC_CTX *mem_ctx,
46                              BOOL sec_qos, uint32 des_access, POLICY_HND *pol)
47 {
48         prs_struct qbuf, rbuf;
49         LSA_Q_OPEN_POL q;
50         LSA_R_OPEN_POL r;
51         LSA_SEC_QOS qos;
52         NTSTATUS result;
53
54         ZERO_STRUCT(q);
55         ZERO_STRUCT(r);
56
57         /* Initialise parse structures */
58
59         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
60         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
61
62         /* Initialise input parameters */
63
64         if (sec_qos) {
65                 init_lsa_sec_qos(&qos, 2, 1, 0);
66                 init_q_open_pol(&q, '\\', 0, des_access, &qos);
67         } else {
68                 init_q_open_pol(&q, '\\', 0, des_access, NULL);
69         }
70
71         /* Marshall data and send request */
72
73         if (!lsa_io_q_open_pol("", &q, &qbuf, 0) ||
74             !rpc_api_pipe_req(cli, LSA_OPENPOLICY, &qbuf, &rbuf)) {
75                 result = NT_STATUS_UNSUCCESSFUL;
76                 goto done;
77         }
78
79         /* Unmarshall response */
80
81         if (!lsa_io_r_open_pol("", &r, &rbuf, 0)) {
82                 result = NT_STATUS_UNSUCCESSFUL;
83                 goto done;
84         }
85
86         /* Return output parameters */
87
88         if (NT_STATUS_IS_OK(result = r.status)) {
89                 *pol = r.pol;
90 #ifdef __INSURE__
91                 pol->marker = malloc(1);
92 #endif
93         }
94
95  done:
96         prs_mem_free(&qbuf);
97         prs_mem_free(&rbuf);
98
99         return result;
100 }
101
102 /** Open a LSA policy handle
103   *
104   * @param cli Handle on an initialised SMB connection 
105   */
106
107 NTSTATUS cli_lsa_open_policy2(struct cli_state *cli, TALLOC_CTX *mem_ctx,
108                               BOOL sec_qos, uint32 des_access, POLICY_HND *pol)
109 {
110         prs_struct qbuf, rbuf;
111         LSA_Q_OPEN_POL2 q;
112         LSA_R_OPEN_POL2 r;
113         LSA_SEC_QOS qos;
114         NTSTATUS result;
115
116         ZERO_STRUCT(q);
117         ZERO_STRUCT(r);
118
119         /* Initialise parse structures */
120
121         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
122         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
123
124         /* Initialise input parameters */
125
126         if (sec_qos) {
127                 init_lsa_sec_qos(&qos, 2, 1, 0);
128                 init_q_open_pol2(&q, cli->srv_name_slash, 0, des_access, 
129                                  &qos);
130         } else {
131                 init_q_open_pol2(&q, cli->srv_name_slash, 0, des_access, 
132                                  NULL);
133         }
134
135         /* Marshall data and send request */
136
137         if (!lsa_io_q_open_pol2("", &q, &qbuf, 0) ||
138             !rpc_api_pipe_req(cli, LSA_OPENPOLICY2, &qbuf, &rbuf)) {
139                 result = NT_STATUS_UNSUCCESSFUL;
140                 goto done;
141         }
142
143         /* Unmarshall response */
144
145         if (!lsa_io_r_open_pol2("", &r, &rbuf, 0)) {
146                 result = NT_STATUS_UNSUCCESSFUL;
147                 goto done;
148         }
149
150         /* Return output parameters */
151
152         if (NT_STATUS_IS_OK(result = r.status)) {
153                 *pol = r.pol;
154 #ifdef __INSURE__
155                 pol->marker = (char *)malloc(1);
156 #endif
157         }
158
159  done:
160         prs_mem_free(&qbuf);
161         prs_mem_free(&rbuf);
162
163         return result;
164 }
165
166 /** Close a LSA policy handle */
167
168 NTSTATUS cli_lsa_close(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
169                        POLICY_HND *pol)
170 {
171         prs_struct qbuf, rbuf;
172         LSA_Q_CLOSE q;
173         LSA_R_CLOSE r;
174         NTSTATUS result;
175
176         ZERO_STRUCT(q);
177         ZERO_STRUCT(r);
178
179         /* Initialise parse structures */
180
181         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
182         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
183
184         /* Marshall data and send request */
185
186         init_lsa_q_close(&q, pol);
187
188         if (!lsa_io_q_close("", &q, &qbuf, 0) ||
189             !rpc_api_pipe_req(cli, LSA_CLOSE, &qbuf, &rbuf)) {
190                 result = NT_STATUS_UNSUCCESSFUL;
191                 goto done;
192         }
193
194         /* Unmarshall response */
195
196         if (!lsa_io_r_close("", &r, &rbuf, 0)) {
197                 result = NT_STATUS_UNSUCCESSFUL;
198                 goto done;
199         }
200
201         /* Return output parameters */
202
203         if (NT_STATUS_IS_OK(result = r.status)) {
204 #ifdef __INSURE__
205                 SAFE_FREE(pol->marker);
206 #endif
207                 *pol = r.pol;
208         }
209
210  done:
211         prs_mem_free(&qbuf);
212         prs_mem_free(&rbuf);
213
214         return result;
215 }
216
217 /** Lookup a list of sids */
218
219 NTSTATUS cli_lsa_lookup_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
220                              POLICY_HND *pol, int num_sids, DOM_SID *sids, 
221                              char ***domains, char ***names, uint32 **types)
222 {
223         prs_struct qbuf, rbuf;
224         LSA_Q_LOOKUP_SIDS q;
225         LSA_R_LOOKUP_SIDS r;
226         DOM_R_REF ref;
227         LSA_TRANS_NAME_ENUM t_names;
228         NTSTATUS result;
229         int i;
230
231         ZERO_STRUCT(q);
232         ZERO_STRUCT(r);
233
234         /* Initialise parse structures */
235
236         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
237         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
238
239         /* Marshall data and send request */
240
241         init_q_lookup_sids(mem_ctx, &q, pol, num_sids, sids, 1);
242
243         if (!lsa_io_q_lookup_sids("", &q, &qbuf, 0) ||
244             !rpc_api_pipe_req(cli, LSA_LOOKUPSIDS, &qbuf, &rbuf)) {
245                 result = NT_STATUS_UNSUCCESSFUL;
246                 goto done;
247         }
248
249         /* Unmarshall response */
250
251         ZERO_STRUCT(ref);
252         ZERO_STRUCT(t_names);
253
254         r.dom_ref = &ref;
255         r.names = &t_names;
256
257         if (!lsa_io_r_lookup_sids("", &r, &rbuf, 0)) {
258                 result = NT_STATUS_UNSUCCESSFUL;
259                 goto done;
260         }
261
262         result = r.status;
263
264         if (!NT_STATUS_IS_OK(result) &&
265             NT_STATUS_V(result) != NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
266           
267                 /* An actual error occured */
268
269                 goto done;
270         }
271
272         /* Return output parameters */
273
274         if (r.mapped_count == 0) {
275                 result = NT_STATUS_NONE_MAPPED;
276                 goto done;
277         }
278
279         if (!((*domains) = (char **)talloc(mem_ctx, sizeof(char *) *
280                                            num_sids))) {
281                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
282                 result = NT_STATUS_UNSUCCESSFUL;
283                 goto done;
284         }
285
286         if (!((*names) = (char **)talloc(mem_ctx, sizeof(char *) *
287                                          num_sids))) {
288                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
289                 result = NT_STATUS_UNSUCCESSFUL;
290                 goto done;
291         }
292
293         if (!((*types) = (uint32 *)talloc(mem_ctx, sizeof(uint32) *
294                                           num_sids))) {
295                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
296                 result = NT_STATUS_UNSUCCESSFUL;
297                 goto done;
298         }
299                 
300         for (i = 0; i < num_sids; i++) {
301                 fstring name, dom_name;
302                 uint32 dom_idx = t_names.name[i].domain_idx;
303
304                 /* Translate optimised name through domain index array */
305
306                 if (dom_idx != 0xffffffff) {
307
308                         rpcstr_pull_unistr2_fstring(
309                                 dom_name, &ref.ref_dom[dom_idx].uni_dom_name);
310                         rpcstr_pull_unistr2_fstring(
311                                 name, &t_names.uni_name[i]);
312
313                         (*names)[i] = talloc_strdup(mem_ctx, name);
314                         (*domains)[i] = talloc_strdup(mem_ctx, dom_name);
315                         (*types)[i] = t_names.name[i].sid_name_use;
316                         
317                         if (((*names)[i] == NULL) || ((*domains)[i] == NULL)) {
318                                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
319                                 result = NT_STATUS_UNSUCCESSFUL;
320                                 goto done;
321                         }
322
323                 } else {
324                         (*names)[i] = NULL;
325                         (*types)[i] = SID_NAME_UNKNOWN;
326                 }
327         }
328
329  done:
330         prs_mem_free(&qbuf);
331         prs_mem_free(&rbuf);
332
333         return result;
334 }
335
336 /** Lookup a list of names */
337
338 NTSTATUS cli_lsa_lookup_names(struct cli_state *cli, TALLOC_CTX *mem_ctx,
339                               POLICY_HND *pol, int num_names, 
340                               const char **names, DOM_SID **sids, 
341                               uint32 **types)
342 {
343         prs_struct qbuf, rbuf;
344         LSA_Q_LOOKUP_NAMES q;
345         LSA_R_LOOKUP_NAMES r;
346         DOM_R_REF ref;
347         NTSTATUS result;
348         int i;
349         
350         ZERO_STRUCT(q);
351         ZERO_STRUCT(r);
352
353         /* Initialise parse structures */
354
355         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
356         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
357
358         /* Marshall data and send request */
359
360         init_q_lookup_names(mem_ctx, &q, pol, num_names, names);
361
362         if (!lsa_io_q_lookup_names("", &q, &qbuf, 0) ||
363             !rpc_api_pipe_req(cli, LSA_LOOKUPNAMES, &qbuf, &rbuf)) {
364                 result = NT_STATUS_UNSUCCESSFUL;
365                 goto done;
366         }
367         
368         /* Unmarshall response */
369
370         ZERO_STRUCT(ref);
371         r.dom_ref = &ref;
372
373         if (!lsa_io_r_lookup_names("", &r, &rbuf, 0)) {
374                 result = NT_STATUS_UNSUCCESSFUL;
375                 goto done;
376         }
377
378         result = r.status;
379
380         if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
381             NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
382
383                 /* An actual error occured */
384
385                 goto done;
386         }
387
388         /* Return output parameters */
389
390         if (r.mapped_count == 0) {
391                 result = NT_STATUS_NONE_MAPPED;
392                 goto done;
393         }
394
395         if (!((*sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) *
396                                          num_names)))) {
397                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
398                 result = NT_STATUS_UNSUCCESSFUL;
399                 goto done;
400         }
401
402         if (!((*types = (uint32 *)talloc(mem_ctx, sizeof(uint32) *
403                                          num_names)))) {
404                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
405                 result = NT_STATUS_UNSUCCESSFUL;
406                 goto done;
407         }
408
409         for (i = 0; i < num_names; i++) {
410                 DOM_RID2 *t_rids = r.dom_rid;
411                 uint32 dom_idx = t_rids[i].rid_idx;
412                 uint32 dom_rid = t_rids[i].rid;
413                 DOM_SID *sid = &(*sids)[i];
414
415                 /* Translate optimised sid through domain index array */
416
417                 if (dom_idx != 0xffffffff) {
418
419                         sid_copy(sid, &ref.ref_dom[dom_idx].ref_dom.sid);
420
421                         if (dom_rid != 0xffffffff) {
422                                 sid_append_rid(sid, dom_rid);
423                         }
424
425                         (*types)[i] = t_rids[i].type;
426                 } else {
427                         ZERO_STRUCTP(sid);
428                         (*types)[i] = SID_NAME_UNKNOWN;
429                 }
430         }
431
432  done:
433         prs_mem_free(&qbuf);
434         prs_mem_free(&rbuf);
435
436         return result;
437 }
438
439 /** Query info policy
440  *
441  *  @param domain_sid - returned remote server's domain sid */
442
443 NTSTATUS cli_lsa_query_info_policy(struct cli_state *cli, TALLOC_CTX *mem_ctx,
444                                    POLICY_HND *pol, uint16 info_class, 
445                                    fstring domain_name, DOM_SID *domain_sid)
446 {
447         prs_struct qbuf, rbuf;
448         LSA_Q_QUERY_INFO q;
449         LSA_R_QUERY_INFO r;
450         NTSTATUS result;
451
452         ZERO_STRUCT(q);
453         ZERO_STRUCT(r);
454
455         /* Initialise parse structures */
456
457         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
458         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
459
460         /* Marshall data and send request */
461
462         init_q_query(&q, pol, info_class);
463
464         if (!lsa_io_q_query("", &q, &qbuf, 0) ||
465             !rpc_api_pipe_req(cli, LSA_QUERYINFOPOLICY, &qbuf, &rbuf)) {
466                 result = NT_STATUS_UNSUCCESSFUL;
467                 goto done;
468         }
469
470         /* Unmarshall response */
471
472         if (!lsa_io_r_query("", &r, &rbuf, 0)) {
473                 result = NT_STATUS_UNSUCCESSFUL;
474                 goto done;
475         }
476
477         if (!NT_STATUS_IS_OK(result = r.status)) {
478                 goto done;
479         }
480
481         /* Return output parameters */
482
483         ZERO_STRUCTP(domain_sid);
484         domain_name[0] = '\0';
485
486         switch (info_class) {
487
488         case 3:
489                 if (r.dom.id3.buffer_dom_name != 0) {
490                         unistr2_to_ascii(domain_name,
491                                          &r.dom.id3.
492                                          uni_domain_name,
493                                          sizeof (fstring) - 1);
494                 }
495
496                 if (r.dom.id3.buffer_dom_sid != 0) {
497                         *domain_sid = r.dom.id3.dom_sid.sid;
498                 }
499
500                 break;
501
502         case 5:
503                 
504                 if (r.dom.id5.buffer_dom_name != 0) {
505                         unistr2_to_ascii(domain_name, &r.dom.id5.
506                                          uni_domain_name,
507                                          sizeof (fstring) - 1);
508                 }
509                         
510                 if (r.dom.id5.buffer_dom_sid != 0) {
511                         *domain_sid = r.dom.id5.dom_sid.sid;
512                 }
513
514                 break;
515                 
516         default:
517                 DEBUG(3, ("unknown info class %d\n", info_class));
518                 break;                
519         }
520         
521  done:
522         prs_mem_free(&qbuf);
523         prs_mem_free(&rbuf);
524
525         return result;
526 }
527
528 /**
529  * Enumerate list of trusted domains
530  *
531  * @param cli client state (cli_state) structure of the connection
532  * @param mem_ctx memory context
533  * @param pol opened lsa policy handle
534  * @param enum_ctx enumeration context ie. index of first returned domain entry
535  * @param pref_num_domains preferred max number of entries returned in one response
536  * @param num_domains total number of trusted domains returned by response
537  * @param domain_names returned trusted domain names
538  * @param domain_sids returned trusted domain sids
539  *
540  * @return nt status code of response
541  **/
542
543 NTSTATUS cli_lsa_enum_trust_dom(struct cli_state *cli, TALLOC_CTX *mem_ctx,
544                                 POLICY_HND *pol, uint32 *enum_ctx, 
545                                 uint32 *num_domains,
546                                 char ***domain_names, DOM_SID **domain_sids)
547 {
548         prs_struct qbuf, rbuf;
549         LSA_Q_ENUM_TRUST_DOM q;
550         LSA_R_ENUM_TRUST_DOM r;
551         NTSTATUS result;
552         int i;
553
554         ZERO_STRUCT(q);
555         ZERO_STRUCT(r);
556
557         /* Initialise parse structures */
558
559         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
560         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
561
562         /* Marshall data and send request */
563
564         /* 64k is enough for about 2000 trusted domains */
565         init_q_enum_trust_dom(&q, pol, *enum_ctx, 0x10000);
566
567         if (!lsa_io_q_enum_trust_dom("", &q, &qbuf, 0) ||
568             !rpc_api_pipe_req(cli, LSA_ENUMTRUSTDOM, &qbuf, &rbuf)) {
569                 result = NT_STATUS_UNSUCCESSFUL;
570                 goto done;
571         }
572
573         /* Unmarshall response */
574
575         if (!lsa_io_r_enum_trust_dom("", &r, &rbuf, 0)) {
576                 result = NT_STATUS_UNSUCCESSFUL;
577                 goto done;
578         }
579
580         result = r.status;
581
582         if (!NT_STATUS_IS_OK(result) &&
583             !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
584             !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
585
586                 /* An actual error ocured */
587
588                 goto done;
589         }
590
591         /* Return output parameters */
592
593         if (r.num_domains) {
594
595                 /* Allocate memory for trusted domain names and sids */
596
597                 *domain_names = (char **)talloc(mem_ctx, sizeof(char *) *
598                                                 r.num_domains);
599
600                 if (!*domain_names) {
601                         DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
602                         result = NT_STATUS_NO_MEMORY;
603                         goto done;
604                 }
605
606                 *domain_sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) *
607                                                  r.num_domains);
608                 if (!domain_sids) {
609                         DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
610                         result = NT_STATUS_NO_MEMORY;
611                         goto done;
612                 }
613
614                 /* Copy across names and sids */
615
616                 for (i = 0; i < r.num_domains; i++) {
617                         fstring tmp;
618
619                         unistr2_to_ascii(tmp, &r.uni_domain_name[i], 
620                                          sizeof(tmp) - 1);
621                         (*domain_names)[i] = talloc_strdup(mem_ctx, tmp);
622                         sid_copy(&(*domain_sids)[i], &r.domain_sid[i].sid);
623                 }
624         }
625
626         *num_domains = r.num_domains;
627         *enum_ctx = r.enum_context;
628
629  done:
630         prs_mem_free(&qbuf);
631         prs_mem_free(&rbuf);
632
633         return result;
634 }
635
636
637 /** Enumerate privileges*/
638
639 NTSTATUS cli_lsa_enum_privilege(struct cli_state *cli, TALLOC_CTX *mem_ctx,
640                                 POLICY_HND *pol, uint32 *enum_context, uint32 pref_max_length,
641                                 uint32 *count, char ***privs_name, uint32 **privs_high, uint32 **privs_low)
642 {
643         prs_struct qbuf, rbuf;
644         LSA_Q_ENUM_PRIVS q;
645         LSA_R_ENUM_PRIVS r;
646         NTSTATUS result;
647         int i;
648
649         ZERO_STRUCT(q);
650         ZERO_STRUCT(r);
651
652         /* Initialise parse structures */
653
654         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
655         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
656
657         /* Marshall data and send request */
658
659         init_q_enum_privs(&q, pol, *enum_context, pref_max_length);
660
661         if (!lsa_io_q_enum_privs("", &q, &qbuf, 0) ||
662             !rpc_api_pipe_req(cli, LSA_ENUM_PRIVS, &qbuf, &rbuf)) {
663                 result = NT_STATUS_UNSUCCESSFUL;
664                 goto done;
665         }
666
667         /* Unmarshall response */
668
669         if (!lsa_io_r_enum_privs("", &r, &rbuf, 0)) {
670                 result = NT_STATUS_UNSUCCESSFUL;
671                 goto done;
672         }
673
674         if (!NT_STATUS_IS_OK(result = r.status)) {
675                 goto done;
676         }
677
678         /* Return output parameters */
679
680         *enum_context = r.enum_context;
681         *count = r.count;
682
683         if (!((*privs_name = (char **)talloc(mem_ctx, sizeof(char *) * r.count)))) {
684                 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
685                 result = NT_STATUS_UNSUCCESSFUL;
686                 goto done;
687         }
688
689         if (!((*privs_high = (uint32 *)talloc(mem_ctx, sizeof(uint32) * r.count)))) {
690                 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
691                 result = NT_STATUS_UNSUCCESSFUL;
692                 goto done;
693         }
694
695         if (!((*privs_low = (uint32 *)talloc(mem_ctx, sizeof(uint32) * r.count)))) {
696                 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
697                 result = NT_STATUS_UNSUCCESSFUL;
698                 goto done;
699         }
700
701         for (i = 0; i < r.count; i++) {
702                 fstring name;
703
704                 rpcstr_pull_unistr2_fstring( name, &r.privs[i].name);
705
706                 (*privs_name)[i] = talloc_strdup(mem_ctx, name);
707
708                 (*privs_high)[i] = r.privs[i].luid_high;
709                 (*privs_low)[i] = r.privs[i].luid_low;
710         }
711
712  done:
713         prs_mem_free(&qbuf);
714         prs_mem_free(&rbuf);
715
716         return result;
717 }
718
719 /** Get privilege name */
720
721 NTSTATUS cli_lsa_get_dispname(struct cli_state *cli, TALLOC_CTX *mem_ctx,
722                               POLICY_HND *pol, char *name, uint16 lang_id, uint16 lang_id_sys,
723                               fstring description, uint16 *lang_id_desc)
724 {
725         prs_struct qbuf, rbuf;
726         LSA_Q_PRIV_GET_DISPNAME q;
727         LSA_R_PRIV_GET_DISPNAME r;
728         NTSTATUS result;
729
730         ZERO_STRUCT(q);
731         ZERO_STRUCT(r);
732
733         /* Initialise parse structures */
734
735         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
736         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
737
738         /* Marshall data and send request */
739
740         init_lsa_priv_get_dispname(&q, pol, name, lang_id, lang_id_sys);
741
742         if (!lsa_io_q_priv_get_dispname("", &q, &qbuf, 0) ||
743             !rpc_api_pipe_req(cli, LSA_PRIV_GET_DISPNAME, &qbuf, &rbuf)) {
744                 result = NT_STATUS_UNSUCCESSFUL;
745                 goto done;
746         }
747
748         /* Unmarshall response */
749
750         if (!lsa_io_r_priv_get_dispname("", &r, &rbuf, 0)) {
751                 result = NT_STATUS_UNSUCCESSFUL;
752                 goto done;
753         }
754
755         if (!NT_STATUS_IS_OK(result = r.status)) {
756                 goto done;
757         }
758
759         /* Return output parameters */
760         
761         rpcstr_pull_unistr2_fstring(description , &r.desc);
762         *lang_id_desc = r.lang_id;
763
764  done:
765         prs_mem_free(&qbuf);
766         prs_mem_free(&rbuf);
767
768         return result;
769 }
770
771 /** Enumerate list of SIDs  */
772
773 NTSTATUS cli_lsa_enum_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
774                                 POLICY_HND *pol, uint32 *enum_ctx, uint32 pref_max_length, 
775                                 uint32 *num_sids, DOM_SID **sids)
776 {
777         prs_struct qbuf, rbuf;
778         LSA_Q_ENUM_ACCOUNTS q;
779         LSA_R_ENUM_ACCOUNTS r;
780         NTSTATUS result;
781         int i;
782
783         ZERO_STRUCT(q);
784         ZERO_STRUCT(r);
785
786         /* Initialise parse structures */
787
788         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
789         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
790
791         /* Marshall data and send request */
792
793         init_lsa_q_enum_accounts(&q, pol, *enum_ctx, pref_max_length);
794
795         if (!lsa_io_q_enum_accounts("", &q, &qbuf, 0) ||
796             !rpc_api_pipe_req(cli, LSA_ENUM_ACCOUNTS, &qbuf, &rbuf)) {
797                 result = NT_STATUS_UNSUCCESSFUL;
798                 goto done;
799         }
800
801         /* Unmarshall response */
802
803         if (!lsa_io_r_enum_accounts("", &r, &rbuf, 0)) {
804                 result = NT_STATUS_UNSUCCESSFUL;
805                 goto done;
806         }
807
808         result = r.status;
809
810         if (!NT_STATUS_IS_OK(result = r.status)) {
811                 goto done;
812         }
813
814         if (r.sids.num_entries==0)
815                 goto done;
816
817         /* Return output parameters */
818
819         *sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) * r.sids.num_entries);
820         if (!*sids) {
821                 DEBUG(0, ("(cli_lsa_enum_sids): out of memory\n"));
822                 result = NT_STATUS_UNSUCCESSFUL;
823                 goto done;
824         }
825
826         /* Copy across names and sids */
827
828         for (i = 0; i < r.sids.num_entries; i++) {
829                 sid_copy(&(*sids)[i], &r.sids.sid[i].sid);
830         }
831
832         *num_sids= r.sids.num_entries;
833         *enum_ctx = r.enum_context;
834
835  done:
836         prs_mem_free(&qbuf);
837         prs_mem_free(&rbuf);
838
839         return result;
840 }
841
842 /** Open a LSA user handle
843  *
844  * @param cli Handle on an initialised SMB connection */
845
846 NTSTATUS cli_lsa_open_account(struct cli_state *cli, TALLOC_CTX *mem_ctx,
847                              POLICY_HND *dom_pol, DOM_SID *sid, uint32 des_access, 
848                              POLICY_HND *user_pol)
849 {
850         prs_struct qbuf, rbuf;
851         LSA_Q_OPENACCOUNT q;
852         LSA_R_OPENACCOUNT r;
853         NTSTATUS result;
854
855         ZERO_STRUCT(q);
856         ZERO_STRUCT(r);
857
858         /* Initialise parse structures */
859
860         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
861         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
862
863         /* Initialise input parameters */
864
865         init_lsa_q_open_account(&q, dom_pol, sid, des_access);
866
867         /* Marshall data and send request */
868
869         if (!lsa_io_q_open_account("", &q, &qbuf, 0) ||
870             !rpc_api_pipe_req(cli, LSA_OPENACCOUNT, &qbuf, &rbuf)) {
871                 result = NT_STATUS_UNSUCCESSFUL;
872                 goto done;
873         }
874
875         /* Unmarshall response */
876
877         if (!lsa_io_r_open_account("", &r, &rbuf, 0)) {
878                 result = NT_STATUS_UNSUCCESSFUL;
879                 goto done;
880         }
881
882         /* Return output parameters */
883
884         if (NT_STATUS_IS_OK(result = r.status)) {
885                 *user_pol = r.pol;
886         }
887
888  done:
889         prs_mem_free(&qbuf);
890         prs_mem_free(&rbuf);
891
892         return result;
893 }
894
895 /** Enumerate user privileges
896  *
897  * @param cli Handle on an initialised SMB connection */
898
899 NTSTATUS cli_lsa_enum_privsaccount(struct cli_state *cli, TALLOC_CTX *mem_ctx,
900                              POLICY_HND *pol, uint32 *count, LUID_ATTR **set)
901 {
902         prs_struct qbuf, rbuf;
903         LSA_Q_ENUMPRIVSACCOUNT q;
904         LSA_R_ENUMPRIVSACCOUNT r;
905         NTSTATUS result;
906         int i;
907
908         ZERO_STRUCT(q);
909         ZERO_STRUCT(r);
910
911         /* Initialise parse structures */
912
913         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
914         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
915
916         /* Initialise input parameters */
917
918         init_lsa_q_enum_privsaccount(&q, pol);
919
920         /* Marshall data and send request */
921
922         if (!lsa_io_q_enum_privsaccount("", &q, &qbuf, 0) ||
923             !rpc_api_pipe_req(cli, LSA_ENUMPRIVSACCOUNT, &qbuf, &rbuf)) {
924                 result = NT_STATUS_UNSUCCESSFUL;
925                 goto done;
926         }
927
928         /* Unmarshall response */
929
930         if (!lsa_io_r_enum_privsaccount("", &r, &rbuf, 0)) {
931                 result = NT_STATUS_UNSUCCESSFUL;
932                 goto done;
933         }
934
935         /* Return output parameters */
936
937         if (!NT_STATUS_IS_OK(result = r.status)) {
938                 goto done;
939         }
940
941         if (r.count == 0)
942                 goto done;
943
944         if (!((*set = (LUID_ATTR *)talloc(mem_ctx, sizeof(LUID_ATTR) * r.count)))) {
945                 DEBUG(0, ("(cli_lsa_enum_privsaccount): out of memory\n"));
946                 result = NT_STATUS_UNSUCCESSFUL;
947                 goto done;
948         }
949
950         for (i=0; i<r.count; i++) {
951                 (*set)[i].luid.low = r.set.set[i].luid.low;
952                 (*set)[i].luid.high = r.set.set[i].luid.high;
953                 (*set)[i].attr = r.set.set[i].attr;
954         }
955
956         *count=r.count;
957  done:
958         prs_mem_free(&qbuf);
959         prs_mem_free(&rbuf);
960
961         return result;
962 }
963
964 /** Get a privilege value given its name */
965
966 NTSTATUS cli_lsa_lookupprivvalue(struct cli_state *cli, TALLOC_CTX *mem_ctx,
967                               POLICY_HND *pol, char *name, LUID *luid)
968 {
969         prs_struct qbuf, rbuf;
970         LSA_Q_LOOKUPPRIVVALUE q;
971         LSA_R_LOOKUPPRIVVALUE r;
972         NTSTATUS result;
973
974         ZERO_STRUCT(q);
975         ZERO_STRUCT(r);
976
977         /* Initialise parse structures */
978
979         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
980         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
981
982         /* Marshall data and send request */
983
984         init_lsa_q_lookupprivvalue(&q, pol, name);
985
986         if (!lsa_io_q_lookupprivvalue("", &q, &qbuf, 0) ||
987             !rpc_api_pipe_req(cli, LSA_LOOKUPPRIVVALUE, &qbuf, &rbuf)) {
988                 result = NT_STATUS_UNSUCCESSFUL;
989                 goto done;
990         }
991
992         /* Unmarshall response */
993
994         if (!lsa_io_r_lookupprivvalue("", &r, &rbuf, 0)) {
995                 result = NT_STATUS_UNSUCCESSFUL;
996                 goto done;
997         }
998
999         if (!NT_STATUS_IS_OK(result = r.status)) {
1000                 goto done;
1001         }
1002
1003         /* Return output parameters */
1004
1005         (*luid).low=r.luid.low;
1006         (*luid).high=r.luid.high;
1007
1008  done:
1009         prs_mem_free(&qbuf);
1010         prs_mem_free(&rbuf);
1011
1012         return result;
1013 }
1014
1015 /** Query LSA security object */
1016
1017 NTSTATUS cli_lsa_query_secobj(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1018                               POLICY_HND *pol, uint32 sec_info, 
1019                               SEC_DESC_BUF **psdb)
1020 {
1021         prs_struct qbuf, rbuf;
1022         LSA_Q_QUERY_SEC_OBJ q;
1023         LSA_R_QUERY_SEC_OBJ r;
1024         NTSTATUS result;
1025
1026         ZERO_STRUCT(q);
1027         ZERO_STRUCT(r);
1028
1029         /* Initialise parse structures */
1030
1031         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1032         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1033
1034         /* Marshall data and send request */
1035
1036         init_q_query_sec_obj(&q, pol, sec_info);
1037
1038         if (!lsa_io_q_query_sec_obj("", &q, &qbuf, 0) ||
1039             !rpc_api_pipe_req(cli, LSA_QUERYSECOBJ, &qbuf, &rbuf)) {
1040                 result = NT_STATUS_UNSUCCESSFUL;
1041                 goto done;
1042         }
1043
1044         /* Unmarshall response */
1045
1046         if (!lsa_io_r_query_sec_obj("", &r, &rbuf, 0)) {
1047                 result = NT_STATUS_UNSUCCESSFUL;
1048                 goto done;
1049         }
1050
1051         if (!NT_STATUS_IS_OK(result = r.status)) {
1052                 goto done;
1053         }
1054
1055         /* Return output parameters */
1056
1057         if (psdb)
1058                 *psdb = r.buf;
1059
1060  done:
1061         prs_mem_free(&qbuf);
1062         prs_mem_free(&rbuf);
1063
1064         return result;
1065 }
1066
1067 #if 0
1068
1069 /** An example of how to use the routines in this file.  Fetch a DOMAIN
1070     sid. Does complete cli setup / teardown anonymously. */
1071
1072 BOOL fetch_domain_sid( char *domain, char *remote_machine, DOM_SID *psid)
1073 {
1074         extern pstring global_myname;
1075         struct cli_state cli;
1076         NTSTATUS result;
1077         POLICY_HND lsa_pol;
1078         BOOL ret = False;
1079  
1080         ZERO_STRUCT(cli);
1081         if(cli_initialise(&cli) == False) {
1082                 DEBUG(0,("fetch_domain_sid: unable to initialize client connection.\n"));
1083                 return False;
1084         }
1085  
1086         if(!resolve_name( remote_machine, &cli.dest_ip, 0x20)) {
1087                 DEBUG(0,("fetch_domain_sid: Can't resolve address for %s\n", remote_machine));
1088                 goto done;
1089         }
1090  
1091         if (!cli_connect(&cli, remote_machine, &cli.dest_ip)) {
1092                 DEBUG(0,("fetch_domain_sid: unable to connect to SMB server on \
1093 machine %s. Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1094                 goto done;
1095         }
1096
1097         if (!attempt_netbios_session_request(&cli, global_myname, remote_machine, &cli.dest_ip)) {
1098                 DEBUG(0,("fetch_domain_sid: machine %s rejected the NetBIOS session request.\n", 
1099                         remote_machine));
1100                 goto done;
1101         }
1102  
1103         cli.protocol = PROTOCOL_NT1;
1104  
1105         if (!cli_negprot(&cli)) {
1106                 DEBUG(0,("fetch_domain_sid: machine %s rejected the negotiate protocol. \
1107 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1108                 goto done;
1109         }
1110  
1111         if (cli.protocol != PROTOCOL_NT1) {
1112                 DEBUG(0,("fetch_domain_sid: machine %s didn't negotiate NT protocol.\n",
1113                         remote_machine));
1114                 goto done;
1115         }
1116  
1117         /*
1118          * Do an anonymous session setup.
1119          */
1120  
1121         if (!cli_session_setup(&cli, "", "", 0, "", 0, "")) {
1122                 DEBUG(0,("fetch_domain_sid: machine %s rejected the session setup. \
1123 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1124                 goto done;
1125         }
1126  
1127         if (!(cli.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
1128                 DEBUG(0,("fetch_domain_sid: machine %s isn't in user level security mode\n",
1129                         remote_machine));
1130                 goto done;
1131         }
1132
1133         if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
1134                 DEBUG(0,("fetch_domain_sid: machine %s rejected the tconX on the IPC$ share. \
1135 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1136                 goto done;
1137         }
1138
1139         /* Fetch domain sid */
1140  
1141         if (!cli_nt_session_open(&cli, PIPE_LSARPC)) {
1142                 DEBUG(0, ("fetch_domain_sid: Error connecting to SAM pipe\n"));
1143                 goto done;
1144         }
1145  
1146         result = cli_lsa_open_policy(&cli, cli.mem_ctx, True, SEC_RIGHTS_QUERY_VALUE, &lsa_pol);
1147         if (!NT_STATUS_IS_OK(result)) {
1148                 DEBUG(0, ("fetch_domain_sid: Error opening lsa policy handle. %s\n",
1149                         nt_errstr(result) ));
1150                 goto done;
1151         }
1152  
1153         result = cli_lsa_query_info_policy(&cli, cli.mem_ctx, &lsa_pol, 5, domain, psid);
1154         if (!NT_STATUS_IS_OK(result)) {
1155                 DEBUG(0, ("fetch_domain_sid: Error querying lsa policy handle. %s\n",
1156                         nt_errstr(result) ));
1157                 goto done;
1158         }
1159  
1160         ret = True;
1161
1162   done:
1163
1164         cli_shutdown(&cli);
1165         return ret;
1166 }
1167
1168 #endif
1169
1170 /** @} **/