added lsaenumprivsaccount and lsalookupprivvalue to rpcclient
[tprouty/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 /** @defgroup lsa LSA rpc client routines
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 /** Opens a SMB connection and connects to the LSARPC pipe.
42  *
43  * @param cli Uninitialised client handle.
44  * @param system_name NETBIOS name of the machine to connect to.
45  * @param creds User credentials to connect as.
46  * @returns Initialised client handle.
47  */
48 struct cli_state *cli_lsa_initialise(struct cli_state *cli, char *system_name,
49                                      struct ntuser_creds *creds)
50 {
51         return cli_pipe_initialise(cli, system_name, PIPE_LSARPC, creds);
52 }
53
54 /** Open a LSA policy handle
55  *
56  * @param cli Handle on an initialised SMB connection */
57
58 NTSTATUS cli_lsa_open_policy(struct cli_state *cli, TALLOC_CTX *mem_ctx,
59                              BOOL sec_qos, uint32 des_access, POLICY_HND *pol)
60 {
61         prs_struct qbuf, rbuf;
62         LSA_Q_OPEN_POL q;
63         LSA_R_OPEN_POL r;
64         LSA_SEC_QOS qos;
65         NTSTATUS result;
66
67         ZERO_STRUCT(q);
68         ZERO_STRUCT(r);
69
70         /* Initialise parse structures */
71
72         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
73         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
74
75         /* Initialise input parameters */
76
77         if (sec_qos) {
78                 init_lsa_sec_qos(&qos, 2, 1, 0, des_access);
79                 init_q_open_pol(&q, '\\', 0, des_access, &qos);
80         } else {
81                 init_q_open_pol(&q, '\\', 0, des_access, NULL);
82         }
83
84         /* Marshall data and send request */
85
86         if (!lsa_io_q_open_pol("", &q, &qbuf, 0) ||
87             !rpc_api_pipe_req(cli, LSA_OPENPOLICY, &qbuf, &rbuf)) {
88                 result = NT_STATUS_UNSUCCESSFUL;
89                 goto done;
90         }
91
92         /* Unmarshall response */
93
94         if (!lsa_io_r_open_pol("", &r, &rbuf, 0)) {
95                 result = NT_STATUS_UNSUCCESSFUL;
96                 goto done;
97         }
98
99         /* Return output parameters */
100
101         if (NT_STATUS_IS_OK(result = r.status)) {
102                 *pol = r.pol;
103         }
104
105  done:
106         prs_mem_free(&qbuf);
107         prs_mem_free(&rbuf);
108
109         return result;
110 }
111
112 /** Open a LSA policy handle */
113
114 NTSTATUS cli_lsa_open_policy2(struct cli_state *cli, TALLOC_CTX *mem_ctx,
115                               BOOL sec_qos, uint32 des_access, POLICY_HND *pol)
116 {
117         prs_struct qbuf, rbuf;
118         LSA_Q_OPEN_POL2 q;
119         LSA_R_OPEN_POL2 r;
120         LSA_SEC_QOS qos;
121         NTSTATUS result;
122
123         ZERO_STRUCT(q);
124         ZERO_STRUCT(r);
125
126         /* Initialise parse structures */
127
128         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
129         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
130
131         /* Initialise input parameters */
132
133         if (sec_qos) {
134                 init_lsa_sec_qos(&qos, 2, 1, 0, des_access);
135                 init_q_open_pol2(&q, cli->clnt_name_slash, 0, des_access, 
136                                  &qos);
137         } else {
138                 init_q_open_pol2(&q, cli->clnt_name_slash, 0, des_access, 
139                                  NULL);
140         }
141
142         /* Marshall data and send request */
143
144         if (!lsa_io_q_open_pol2("", &q, &qbuf, 0) ||
145             !rpc_api_pipe_req(cli, LSA_OPENPOLICY2, &qbuf, &rbuf)) {
146                 result = NT_STATUS_UNSUCCESSFUL;
147                 goto done;
148         }
149
150         /* Unmarshall response */
151
152         if (!lsa_io_r_open_pol2("", &r, &rbuf, 0)) {
153                 result = NT_STATUS_UNSUCCESSFUL;
154                 goto done;
155         }
156
157         /* Return output parameters */
158
159         if (NT_STATUS_IS_OK(result = r.status)) {
160                 *pol = r.pol;
161         }
162
163  done:
164         prs_mem_free(&qbuf);
165         prs_mem_free(&rbuf);
166
167         return result;
168 }
169
170 /** Close a LSA policy handle */
171
172 NTSTATUS cli_lsa_close(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
173                        POLICY_HND *pol)
174 {
175         prs_struct qbuf, rbuf;
176         LSA_Q_CLOSE q;
177         LSA_R_CLOSE r;
178         NTSTATUS result;
179
180         ZERO_STRUCT(q);
181         ZERO_STRUCT(r);
182
183         /* Initialise parse structures */
184
185         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
186         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
187
188         /* Marshall data and send request */
189
190         init_lsa_q_close(&q, pol);
191
192         if (!lsa_io_q_close("", &q, &qbuf, 0) ||
193             !rpc_api_pipe_req(cli, LSA_CLOSE, &qbuf, &rbuf)) {
194                 result = NT_STATUS_UNSUCCESSFUL;
195                 goto done;
196         }
197
198         /* Unmarshall response */
199
200         if (!lsa_io_r_close("", &r, &rbuf, 0)) {
201                 result = NT_STATUS_UNSUCCESSFUL;
202                 goto done;
203         }
204
205         /* Return output parameters */
206
207         if (NT_STATUS_IS_OK(result = r.status)) {
208                 *pol = r.pol;
209         }
210
211  done:
212         prs_mem_free(&qbuf);
213         prs_mem_free(&rbuf);
214
215         return result;
216 }
217
218 /** Lookup a list of sids */
219
220 NTSTATUS cli_lsa_lookup_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
221                              POLICY_HND *pol, int num_sids, DOM_SID *sids, 
222                              char ***names, uint32 **types, int *num_names)
223 {
224         prs_struct qbuf, rbuf;
225         LSA_Q_LOOKUP_SIDS q;
226         LSA_R_LOOKUP_SIDS r;
227         DOM_R_REF ref;
228         LSA_TRANS_NAME_ENUM t_names;
229         NTSTATUS result;
230         int i;
231
232         ZERO_STRUCT(q);
233         ZERO_STRUCT(r);
234
235         /* Initialise parse structures */
236
237         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
238         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
239
240         /* Marshall data and send request */
241
242         init_q_lookup_sids(mem_ctx, &q, pol, num_sids, sids, 1);
243
244         if (!lsa_io_q_lookup_sids("", &q, &qbuf, 0) ||
245             !rpc_api_pipe_req(cli, LSA_LOOKUPSIDS, &qbuf, &rbuf)) {
246                 result = NT_STATUS_UNSUCCESSFUL;
247                 goto done;
248         }
249
250         /* Unmarshall response */
251
252         ZERO_STRUCT(ref);
253         ZERO_STRUCT(t_names);
254
255         r.dom_ref = &ref;
256         r.names = &t_names;
257
258         if (!lsa_io_r_lookup_sids("", &r, &rbuf, 0)) {
259                 result = NT_STATUS_UNSUCCESSFUL;
260                 goto done;
261         }
262
263         result = r.status;
264
265         if (!NT_STATUS_IS_OK(result) &&
266                 NT_STATUS_V(result) != NT_STATUS_V(NT_STATUS_FILES_OPEN)) {
267                 /* An actual error occured */
268
269                 goto done;
270         }
271
272
273         /* Return output parameters */
274
275         if (r.mapped_count == 0) {
276                 result = NT_STATUS_NONE_MAPPED;
277                 goto done;
278         }
279
280         (*num_names) = r.mapped_count;
281         result = NT_STATUS_OK;
282
283         if (!((*names) = (char **)talloc(mem_ctx, sizeof(char *) * r.mapped_count))) {
284                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
285                 result = NT_STATUS_UNSUCCESSFUL;
286                 goto done;
287         }
288
289         if (!((*types) = (uint32 *)talloc(mem_ctx, sizeof(uint32) * r.mapped_count))) {
290                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
291                 result = NT_STATUS_UNSUCCESSFUL;
292                 goto done;
293         }
294                 
295         for (i = 0; i < r.mapped_count; i++) {
296                 fstring name, dom_name, full_name;
297                 uint32 dom_idx = t_names.name[i].domain_idx;
298
299                 /* Translate optimised name through domain index array */
300
301                 if (dom_idx != 0xffffffff) {
302
303                         rpcstr_pull_unistr2_fstring(
304                                 dom_name, &ref.ref_dom[dom_idx].uni_dom_name);
305                         rpcstr_pull_unistr2_fstring(
306                                 name, &t_names.uni_name[i]);
307
308                         slprintf(full_name, sizeof(full_name) - 1,
309                                  "%s%s%s", dom_name, 
310                                  (dom_name[0] && name[0]) ? 
311                                  lp_winbind_separator() : "", name);
312
313                         (*names)[i] = talloc_strdup(mem_ctx, full_name);
314                         (*types)[i] = t_names.name[i].sid_name_use;
315
316                 } else {
317                         (*names)[i] = NULL;
318                         (*types)[i] = SID_NAME_UNKNOWN;
319                 }
320         }
321
322  done:
323         prs_mem_free(&qbuf);
324         prs_mem_free(&rbuf);
325
326         return result;
327 }
328
329 /** Lookup a list of names */
330
331 NTSTATUS cli_lsa_lookup_names(struct cli_state *cli, TALLOC_CTX *mem_ctx,
332                               POLICY_HND *pol, int num_names, char **names, 
333                               DOM_SID **sids, uint32 **types, int *num_sids)
334 {
335         prs_struct qbuf, rbuf;
336         LSA_Q_LOOKUP_NAMES q;
337         LSA_R_LOOKUP_NAMES r;
338         DOM_R_REF ref;
339         NTSTATUS result;
340         int i;
341         
342         ZERO_STRUCT(q);
343         ZERO_STRUCT(r);
344
345         /* Initialise parse structures */
346
347         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
348         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
349
350         /* Marshall data and send request */
351
352         init_q_lookup_names(mem_ctx, &q, pol, num_names, names);
353
354         if (!lsa_io_q_lookup_names("", &q, &qbuf, 0) ||
355             !rpc_api_pipe_req(cli, LSA_LOOKUPNAMES, &qbuf, &rbuf)) {
356                 result = NT_STATUS_UNSUCCESSFUL;
357                 goto done;
358         }
359         
360         /* Unmarshall response */
361
362         ZERO_STRUCT(ref);
363         r.dom_ref = &ref;
364
365         if (!lsa_io_r_lookup_names("", &r, &rbuf, 0)) {
366                 result = NT_STATUS_UNSUCCESSFUL;
367                 goto done;
368         }
369
370         result = r.status;
371
372         if (!NT_STATUS_IS_OK(result)) {
373                 /* An actual error occured */
374
375                 goto done;
376         }
377
378
379         /* Return output parameters */
380
381         if (r.mapped_count == 0) {
382                 result = NT_STATUS_NONE_MAPPED;
383                 goto done;
384         }
385
386         (*num_sids) = r.mapped_count;
387         result = NT_STATUS_OK;
388
389         if (!((*sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) * r.mapped_count)))) {
390                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
391                 result = NT_STATUS_UNSUCCESSFUL;
392                 goto done;
393         }
394
395         if (!((*types = (uint32 *)talloc(mem_ctx, sizeof(uint32) * r.mapped_count)))) {
396                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
397                 result = NT_STATUS_UNSUCCESSFUL;
398                 goto done;
399         }
400
401         for (i = 0; i < r.mapped_count; i++) {
402                 DOM_RID2 *t_rids = r.dom_rid;
403                 uint32 dom_idx = t_rids[i].rid_idx;
404                 uint32 dom_rid = t_rids[i].rid;
405                 DOM_SID *sid = &(*sids)[i];
406
407                 /* Translate optimised sid through domain index array */
408
409                 if (dom_idx != 0xffffffff) {
410
411                         sid_copy(sid, &ref.ref_dom[dom_idx].ref_dom.sid);
412
413                         if (dom_rid != 0xffffffff) {
414                                 sid_append_rid(sid, dom_rid);
415                         }
416
417                         (*types)[i] = t_rids[i].type;
418                 } else {
419                         ZERO_STRUCTP(sid);
420                         (*types)[i] = SID_NAME_UNKNOWN;
421                 }
422         }
423
424  done:
425         prs_mem_free(&qbuf);
426         prs_mem_free(&rbuf);
427
428         return result;
429 }
430
431 /** Query info policy */
432
433 NTSTATUS cli_lsa_query_info_policy(struct cli_state *cli, TALLOC_CTX *mem_ctx,
434                                    POLICY_HND *pol, uint16 info_class, 
435                                    fstring domain_name, DOM_SID *domain_sid)
436 {
437         prs_struct qbuf, rbuf;
438         LSA_Q_QUERY_INFO q;
439         LSA_R_QUERY_INFO r;
440         NTSTATUS result;
441
442         ZERO_STRUCT(q);
443         ZERO_STRUCT(r);
444
445         /* Initialise parse structures */
446
447         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
448         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
449
450         /* Marshall data and send request */
451
452         init_q_query(&q, pol, info_class);
453
454         if (!lsa_io_q_query("", &q, &qbuf, 0) ||
455             !rpc_api_pipe_req(cli, LSA_QUERYINFOPOLICY, &qbuf, &rbuf)) {
456                 result = NT_STATUS_UNSUCCESSFUL;
457                 goto done;
458         }
459
460         /* Unmarshall response */
461
462         if (!lsa_io_r_query("", &r, &rbuf, 0)) {
463                 result = NT_STATUS_UNSUCCESSFUL;
464                 goto done;
465         }
466
467         if (!NT_STATUS_IS_OK(result = r.status)) {
468                 goto done;
469         }
470
471         /* Return output parameters */
472
473         ZERO_STRUCTP(domain_sid);
474         domain_name[0] = '\0';
475
476         switch (info_class) {
477
478         case 3:
479                 if (r.dom.id3.buffer_dom_name != 0) {
480                         unistr2_to_ascii(domain_name,
481                                          &r.dom.id3.
482                                          uni_domain_name,
483                                          sizeof (fstring) - 1);
484                 }
485
486                 if (r.dom.id3.buffer_dom_sid != 0) {
487                         *domain_sid = r.dom.id3.dom_sid.sid;
488                 }
489
490                 break;
491
492         case 5:
493                 
494                 if (r.dom.id5.buffer_dom_name != 0) {
495                         unistr2_to_ascii(domain_name, &r.dom.id5.
496                                          uni_domain_name,
497                                          sizeof (fstring) - 1);
498                 }
499                         
500                 if (r.dom.id5.buffer_dom_sid != 0) {
501                         *domain_sid = r.dom.id5.dom_sid.sid;
502                 }
503
504                 break;
505                 
506         default:
507                 DEBUG(3, ("unknown info class %d\n", info_class));
508                 break;                
509         }
510         
511  done:
512         prs_mem_free(&qbuf);
513         prs_mem_free(&rbuf);
514
515         return result;
516 }
517
518 /** Enumerate list of trusted domains */
519
520 NTSTATUS cli_lsa_enum_trust_dom(struct cli_state *cli, TALLOC_CTX *mem_ctx,
521                                 POLICY_HND *pol, uint32 *enum_ctx, 
522                                 uint32 *num_domains, char ***domain_names, 
523                                 DOM_SID **domain_sids)
524 {
525         prs_struct qbuf, rbuf;
526         LSA_Q_ENUM_TRUST_DOM q;
527         LSA_R_ENUM_TRUST_DOM r;
528         NTSTATUS result;
529         int i;
530
531         ZERO_STRUCT(q);
532         ZERO_STRUCT(r);
533
534         /* Initialise parse structures */
535
536         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
537         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
538
539         /* Marshall data and send request */
540
541         init_q_enum_trust_dom(&q, pol, *enum_ctx, 0xffffffff);
542
543         if (!lsa_io_q_enum_trust_dom("", &q, &qbuf, 0) ||
544             !rpc_api_pipe_req(cli, LSA_ENUMTRUSTDOM, &qbuf, &rbuf)) {
545                 result = NT_STATUS_UNSUCCESSFUL;
546                 goto done;
547         }
548
549         /* Unmarshall response */
550
551         if (!lsa_io_r_enum_trust_dom("", &r, &rbuf, 0)) {
552                 result = NT_STATUS_UNSUCCESSFUL;
553                 goto done;
554         }
555
556         result = r.status;
557
558         if (!NT_STATUS_IS_OK(result) && 
559             NT_STATUS_V(result) != NT_STATUS_V(NT_STATUS_NO_MORE_ENTRIES)) {
560
561                 /* An actual error ocured */
562
563                 goto done;
564         }
565
566         result = NT_STATUS_OK;
567
568         /* Return output parameters */
569
570         if (r.num_domains) {
571
572                 /* Allocate memory for trusted domain names and sids */
573
574                 *domain_names = (char **)talloc(mem_ctx, sizeof(char *) *
575                                                 r.num_domains);
576
577                 if (!*domain_names) {
578                         DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
579                         result = NT_STATUS_UNSUCCESSFUL;
580                         goto done;
581                 }
582
583                 *domain_sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) *
584                                                  r.num_domains);
585                 if (!domain_sids) {
586                         DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
587                         result = NT_STATUS_UNSUCCESSFUL;
588                         goto done;
589                 }
590
591                 /* Copy across names and sids */
592
593                 for (i = 0; i < r.num_domains; i++) {
594                         fstring tmp;
595
596                         unistr2_to_ascii(tmp, &r.uni_domain_name[i], 
597                                          sizeof(tmp) - 1);
598                         (*domain_names)[i] = strdup(tmp);
599                         sid_copy(&(*domain_sids)[i], &r.domain_sid[i].sid);
600                 }
601         }
602
603         *num_domains = r.num_domains;
604         *enum_ctx = r.enum_context;
605
606  done:
607         prs_mem_free(&qbuf);
608         prs_mem_free(&rbuf);
609
610         return result;
611 }
612
613 /** Enumerate privileges*/
614
615 NTSTATUS cli_lsa_enum_privilege(struct cli_state *cli, TALLOC_CTX *mem_ctx,
616                                 POLICY_HND *pol, uint32 *enum_context, uint32 pref_max_length,
617                                 uint32 *count, char ***privs_name, uint32 **privs_high, uint32 **privs_low)
618 {
619         prs_struct qbuf, rbuf;
620         LSA_Q_ENUM_PRIVS q;
621         LSA_R_ENUM_PRIVS r;
622         NTSTATUS result;
623         int i;
624
625         ZERO_STRUCT(q);
626         ZERO_STRUCT(r);
627
628         /* Initialise parse structures */
629
630         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
631         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
632
633         /* Marshall data and send request */
634
635         init_q_enum_privs(&q, pol, *enum_context, pref_max_length);
636
637         if (!lsa_io_q_enum_privs("", &q, &qbuf, 0) ||
638             !rpc_api_pipe_req(cli, LSA_ENUM_PRIVS, &qbuf, &rbuf)) {
639                 result = NT_STATUS_UNSUCCESSFUL;
640                 goto done;
641         }
642
643         /* Unmarshall response */
644
645         if (!lsa_io_r_enum_privs("", &r, &rbuf, 0)) {
646                 result = NT_STATUS_UNSUCCESSFUL;
647                 goto done;
648         }
649
650         if (!NT_STATUS_IS_OK(result = r.status)) {
651                 goto done;
652         }
653
654         /* Return output parameters */
655
656         *enum_context = r.enum_context;
657         *count = r.count;
658
659         if (!((*privs_name = (char **)talloc(mem_ctx, sizeof(char *) * r.count)))) {
660                 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
661                 result = NT_STATUS_UNSUCCESSFUL;
662                 goto done;
663         }
664
665         if (!((*privs_high = (uint32 *)talloc(mem_ctx, sizeof(uint32) * r.count)))) {
666                 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
667                 result = NT_STATUS_UNSUCCESSFUL;
668                 goto done;
669         }
670
671         if (!((*privs_low = (uint32 *)talloc(mem_ctx, sizeof(uint32) * r.count)))) {
672                 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
673                 result = NT_STATUS_UNSUCCESSFUL;
674                 goto done;
675         }
676
677         for (i = 0; i < r.count; i++) {
678                 fstring name;
679
680                 rpcstr_pull_unistr2_fstring( name, &r.privs[i].name);
681
682                 (*privs_name)[i] = talloc_strdup(mem_ctx, name);
683
684                 (*privs_high)[i] = r.privs[i].luid_high;
685                 (*privs_low)[i] = r.privs[i].luid_low;
686         }
687
688  done:
689         prs_mem_free(&qbuf);
690         prs_mem_free(&rbuf);
691
692         return result;
693 }
694
695 /** Get privilege name */
696
697 NTSTATUS cli_lsa_get_dispname(struct cli_state *cli, TALLOC_CTX *mem_ctx,
698                               POLICY_HND *pol, char *name, uint16 lang_id, uint16 lang_id_sys,
699                               fstring description, uint16 *lang_id_desc)
700 {
701         prs_struct qbuf, rbuf;
702         LSA_Q_PRIV_GET_DISPNAME q;
703         LSA_R_PRIV_GET_DISPNAME r;
704         NTSTATUS result;
705
706         ZERO_STRUCT(q);
707         ZERO_STRUCT(r);
708
709         /* Initialise parse structures */
710
711         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
712         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
713
714         /* Marshall data and send request */
715
716         init_lsa_priv_get_dispname(&q, pol, name, lang_id, lang_id_sys);
717
718         if (!lsa_io_q_priv_get_dispname("", &q, &qbuf, 0) ||
719             !rpc_api_pipe_req(cli, LSA_PRIV_GET_DISPNAME, &qbuf, &rbuf)) {
720                 result = NT_STATUS_UNSUCCESSFUL;
721                 goto done;
722         }
723
724         /* Unmarshall response */
725
726         if (!lsa_io_r_priv_get_dispname("", &r, &rbuf, 0)) {
727                 result = NT_STATUS_UNSUCCESSFUL;
728                 goto done;
729         }
730
731         if (!NT_STATUS_IS_OK(result = r.status)) {
732                 goto done;
733         }
734
735         /* Return output parameters */
736         
737         rpcstr_pull_unistr2_fstring(description , &r.desc);
738         *lang_id_desc = r.lang_id;
739
740  done:
741         prs_mem_free(&qbuf);
742         prs_mem_free(&rbuf);
743
744         return result;
745 }
746
747 /** Enumerate list of SIDs  */
748
749 NTSTATUS cli_lsa_enum_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
750                                 POLICY_HND *pol, uint32 *enum_ctx, uint32 pref_max_length, 
751                                 uint32 *num_sids, DOM_SID **sids)
752 {
753         prs_struct qbuf, rbuf;
754         LSA_Q_ENUM_ACCOUNTS q;
755         LSA_R_ENUM_ACCOUNTS r;
756         NTSTATUS result;
757         int i;
758
759         ZERO_STRUCT(q);
760         ZERO_STRUCT(r);
761
762         /* Initialise parse structures */
763
764         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
765         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
766
767         /* Marshall data and send request */
768
769         init_lsa_q_enum_accounts(&q, pol, *enum_ctx, pref_max_length);
770
771         if (!lsa_io_q_enum_accounts("", &q, &qbuf, 0) ||
772             !rpc_api_pipe_req(cli, LSA_ENUM_ACCOUNTS, &qbuf, &rbuf)) {
773                 result = NT_STATUS_UNSUCCESSFUL;
774                 goto done;
775         }
776
777         /* Unmarshall response */
778
779         if (!lsa_io_r_enum_accounts("", &r, &rbuf, 0)) {
780                 result = NT_STATUS_UNSUCCESSFUL;
781                 goto done;
782         }
783
784         result = r.status;
785
786         if (!NT_STATUS_IS_OK(result = r.status)) {
787                 goto done;
788         }
789
790         if (r.sids.num_entries==0)
791                 goto done;
792
793         /* Return output parameters */
794
795         *sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) * r.sids.num_entries);
796         if (!*sids) {
797                 DEBUG(0, ("(cli_lsa_enum_sids): out of memory\n"));
798                 result = NT_STATUS_UNSUCCESSFUL;
799                 goto done;
800         }
801
802         /* Copy across names and sids */
803
804         for (i = 0; i < r.sids.num_entries; i++) {
805                 sid_copy(&(*sids)[i], &r.sids.sid[i].sid);
806         }
807
808         *num_sids= r.sids.num_entries;
809         *enum_ctx = r.enum_context;
810
811  done:
812         prs_mem_free(&qbuf);
813         prs_mem_free(&rbuf);
814
815         return result;
816 }
817
818 /** Open a LSA user handle
819  *
820  * @param cli Handle on an initialised SMB connection */
821
822 NTSTATUS cli_lsa_open_account(struct cli_state *cli, TALLOC_CTX *mem_ctx,
823                              POLICY_HND *dom_pol, DOM_SID *sid, uint32 des_access, 
824                              POLICY_HND *user_pol)
825 {
826         prs_struct qbuf, rbuf;
827         LSA_Q_OPENACCOUNT q;
828         LSA_R_OPENACCOUNT r;
829         NTSTATUS result;
830
831         ZERO_STRUCT(q);
832         ZERO_STRUCT(r);
833
834         /* Initialise parse structures */
835
836         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
837         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
838
839         /* Initialise input parameters */
840
841         init_lsa_q_open_account(&q, dom_pol, sid, des_access);
842
843         /* Marshall data and send request */
844
845         if (!lsa_io_q_open_account("", &q, &qbuf, 0) ||
846             !rpc_api_pipe_req(cli, LSA_OPENACCOUNT, &qbuf, &rbuf)) {
847                 result = NT_STATUS_UNSUCCESSFUL;
848                 goto done;
849         }
850
851         /* Unmarshall response */
852
853         if (!lsa_io_r_open_account("", &r, &rbuf, 0)) {
854                 result = NT_STATUS_UNSUCCESSFUL;
855                 goto done;
856         }
857
858         /* Return output parameters */
859
860         if (NT_STATUS_IS_OK(result = r.status)) {
861                 *user_pol = r.pol;
862         }
863
864  done:
865         prs_mem_free(&qbuf);
866         prs_mem_free(&rbuf);
867
868         return result;
869 }
870
871 /** Enumerate user privileges
872  *
873  * @param cli Handle on an initialised SMB connection */
874
875 NTSTATUS cli_lsa_enum_privsaccount(struct cli_state *cli, TALLOC_CTX *mem_ctx,
876                              POLICY_HND *pol, uint32 *count, LUID_ATTR **set)
877 {
878         prs_struct qbuf, rbuf;
879         LSA_Q_ENUMPRIVSACCOUNT q;
880         LSA_R_ENUMPRIVSACCOUNT r;
881         NTSTATUS result;
882         int i;
883
884         ZERO_STRUCT(q);
885         ZERO_STRUCT(r);
886
887         /* Initialise parse structures */
888
889         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
890         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
891
892         /* Initialise input parameters */
893
894         init_lsa_q_enum_privsaccount(&q, pol);
895
896         /* Marshall data and send request */
897
898         if (!lsa_io_q_enum_privsaccount("", &q, &qbuf, 0) ||
899             !rpc_api_pipe_req(cli, LSA_ENUMPRIVSACCOUNT, &qbuf, &rbuf)) {
900                 result = NT_STATUS_UNSUCCESSFUL;
901                 goto done;
902         }
903
904         /* Unmarshall response */
905
906         if (!lsa_io_r_enum_privsaccount("", &r, &rbuf, 0)) {
907                 result = NT_STATUS_UNSUCCESSFUL;
908                 goto done;
909         }
910
911         /* Return output parameters */
912
913         if (!NT_STATUS_IS_OK(result = r.status)) {
914                 goto done;
915         }
916
917         if (r.count == 0)
918                 goto done;
919
920         if (!((*set = (LUID_ATTR *)talloc(mem_ctx, sizeof(LUID_ATTR) * r.count)))) {
921                 DEBUG(0, ("(cli_lsa_enum_privsaccount): out of memory\n"));
922                 result = NT_STATUS_UNSUCCESSFUL;
923                 goto done;
924         }
925
926         for (i=0; i<r.count; i++) {
927                 (*set)[i].luid.low = r.set.set[i].luid.low;
928                 (*set)[i].luid.high = r.set.set[i].luid.high;
929                 (*set)[i].attr = r.set.set[i].attr;
930         }
931
932         *count=r.count;
933  done:
934         prs_mem_free(&qbuf);
935         prs_mem_free(&rbuf);
936
937         return result;
938 }
939
940 /** Get a privilege value given its name */
941
942 NTSTATUS cli_lsa_lookupprivvalue(struct cli_state *cli, TALLOC_CTX *mem_ctx,
943                               POLICY_HND *pol, char *name, LUID *luid)
944 {
945         prs_struct qbuf, rbuf;
946         LSA_Q_LOOKUPPRIVVALUE q;
947         LSA_R_LOOKUPPRIVVALUE r;
948         NTSTATUS result;
949
950         ZERO_STRUCT(q);
951         ZERO_STRUCT(r);
952
953         /* Initialise parse structures */
954
955         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
956         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
957
958         /* Marshall data and send request */
959
960         init_lsa_q_lookupprivvalue(&q, pol, name);
961
962         if (!lsa_io_q_lookupprivvalue("", &q, &qbuf, 0) ||
963             !rpc_api_pipe_req(cli, LSA_LOOKUPPRIVVALUE, &qbuf, &rbuf)) {
964                 result = NT_STATUS_UNSUCCESSFUL;
965                 goto done;
966         }
967
968         /* Unmarshall response */
969
970         if (!lsa_io_r_lookupprivvalue("", &r, &rbuf, 0)) {
971                 result = NT_STATUS_UNSUCCESSFUL;
972                 goto done;
973         }
974
975         if (!NT_STATUS_IS_OK(result = r.status)) {
976                 goto done;
977         }
978
979         /* Return output parameters */
980
981         (*luid).low=r.luid.low;
982         (*luid).high=r.luid.high;
983
984  done:
985         prs_mem_free(&qbuf);
986         prs_mem_free(&rbuf);
987
988         return result;
989 }
990
991
992 /** @} **/