Remove unused marshalling for LSA_ENUM_TRUST_DOM.
[ira/wip.git] / source3 / rpc_client / cli_lsarpc.c
1 /*
2    Unix SMB/CIFS implementation.
3    RPC pipe client
4    Copyright (C) Tim Potter                        2000-2001,
5    Copyright (C) Andrew Tridgell              1992-1997,2000,
6    Copyright (C) Rafal Szczesniak                       2002
7    Copyright (C) Jeremy Allison                         2005.
8    Copyright (C) Michael Adam                           2007.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25
26 /** @defgroup lsa LSA - Local Security Architecture
27  *  @ingroup rpc_client
28  *
29  * @{
30  **/
31
32 /**
33  * @file cli_lsarpc.c
34  *
35  * RPC client routines for the LSA RPC pipe.  LSA means "local
36  * security authority", which is half of a password database.
37  **/
38
39 /** Open a LSA policy handle
40  *
41  * @param cli Handle on an initialised SMB connection */
42
43 NTSTATUS rpccli_lsa_open_policy(struct rpc_pipe_client *cli,
44                                 TALLOC_CTX *mem_ctx,
45                                 bool sec_qos, uint32 des_access,
46                                 POLICY_HND *pol)
47 {
48         struct lsa_ObjectAttribute attr;
49         struct lsa_QosInfo qos;
50         uint16_t system_name = '\\';
51
52         if (sec_qos) {
53                 init_lsa_sec_qos(&qos, 0xc, 2, 1, 0);
54                 init_lsa_obj_attr(&attr,
55                                   0x18,
56                                   NULL,
57                                   NULL,
58                                   0,
59                                   NULL,
60                                   &qos);
61         } else {
62                 init_lsa_obj_attr(&attr,
63                                   0x18,
64                                   NULL,
65                                   NULL,
66                                   0,
67                                   NULL,
68                                   NULL);
69         }
70
71         return rpccli_lsa_OpenPolicy(cli, mem_ctx,
72                                      &system_name,
73                                      &attr,
74                                      des_access,
75                                      pol);
76 }
77
78 /** Open a LSA policy handle
79   *
80   * @param cli Handle on an initialised SMB connection
81   */
82
83 NTSTATUS rpccli_lsa_open_policy2(struct rpc_pipe_client *cli,
84                                  TALLOC_CTX *mem_ctx, bool sec_qos,
85                                  uint32 des_access, POLICY_HND *pol)
86 {
87         struct lsa_ObjectAttribute attr;
88         struct lsa_QosInfo qos;
89         char *srv_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", cli->cli->desthost);
90
91         if (sec_qos) {
92                 init_lsa_sec_qos(&qos, 0xc, 2, 1, 0);
93                 init_lsa_obj_attr(&attr,
94                                   0x18,
95                                   NULL,
96                                   NULL,
97                                   0,
98                                   NULL,
99                                   &qos);
100         } else {
101                 init_lsa_obj_attr(&attr,
102                                   0x18,
103                                   NULL,
104                                   NULL,
105                                   0,
106                                   NULL,
107                                   NULL);
108         }
109
110         return rpccli_lsa_OpenPolicy2(cli, mem_ctx,
111                                       srv_name_slash,
112                                       &attr,
113                                       des_access,
114                                       pol);
115 }
116
117 /* Lookup a list of sids
118  *
119  * internal version withOUT memory allocation of the target arrays.
120  * this assumes suffciently sized arrays to store domains, names and types. */
121
122 static NTSTATUS rpccli_lsa_lookup_sids_noalloc(struct rpc_pipe_client *cli,
123                                                TALLOC_CTX *mem_ctx,
124                                                POLICY_HND *pol,
125                                                int num_sids,
126                                                const DOM_SID *sids,
127                                                char **domains,
128                                                char **names,
129                                                enum lsa_SidType *types)
130 {
131         prs_struct qbuf, rbuf;
132         LSA_Q_LOOKUP_SIDS q;
133         LSA_R_LOOKUP_SIDS r;
134         DOM_R_REF ref;
135         NTSTATUS result = NT_STATUS_OK;
136         TALLOC_CTX *tmp_ctx = NULL;
137         int i;
138
139         tmp_ctx = talloc_new(mem_ctx);
140         if (!tmp_ctx) {
141                 DEBUG(0, ("rpccli_lsa_lookup_sids_noalloc: out of memory!\n"));
142                 result = NT_STATUS_UNSUCCESSFUL;
143                 goto done;
144         }
145
146         ZERO_STRUCT(q);
147         ZERO_STRUCT(r);
148
149         init_q_lookup_sids(tmp_ctx, &q, pol, num_sids, sids, 1);
150
151         ZERO_STRUCT(ref);
152
153         r.dom_ref = &ref;
154
155         CLI_DO_RPC( cli, tmp_ctx, PI_LSARPC, LSA_LOOKUPSIDS,
156                         q, r,
157                         qbuf, rbuf,
158                         lsa_io_q_lookup_sids,
159                         lsa_io_r_lookup_sids,
160                         NT_STATUS_UNSUCCESSFUL );
161
162         DEBUG(10, ("LSA_LOOKUPSIDS returned '%s', mapped count = %d'\n",
163                    nt_errstr(r.status), r.mapped_count));
164
165         if (!NT_STATUS_IS_OK(r.status) &&
166             !NT_STATUS_EQUAL(r.status, NT_STATUS_NONE_MAPPED) &&
167             !NT_STATUS_EQUAL(r.status, STATUS_SOME_UNMAPPED))
168         {
169                 /* An actual error occured */
170                 result = r.status;
171                 goto done;
172         }
173
174         /* Return output parameters */
175
176         if (NT_STATUS_EQUAL(r.status, NT_STATUS_NONE_MAPPED) ||
177             (r.mapped_count == 0))
178         {
179                 for (i = 0; i < num_sids; i++) {
180                         (names)[i] = NULL;
181                         (domains)[i] = NULL;
182                         (types)[i] = SID_NAME_UNKNOWN;
183                 }
184                 result = NT_STATUS_NONE_MAPPED;
185                 goto done;
186         }
187
188         for (i = 0; i < num_sids; i++) {
189                 fstring name, dom_name;
190                 uint32 dom_idx = r.names.name[i].domain_idx;
191
192                 /* Translate optimised name through domain index array */
193
194                 if (dom_idx != 0xffffffff) {
195
196                         rpcstr_pull_unistr2_fstring(
197                                 dom_name, &ref.ref_dom[dom_idx].uni_dom_name);
198                         rpcstr_pull_unistr2_fstring(
199                                 name, &r.names.uni_name[i]);
200
201                         (names)[i] = talloc_strdup(mem_ctx, name);
202                         (domains)[i] = talloc_strdup(mem_ctx, dom_name);
203                         (types)[i] = r.names.name[i].sid_name_use;
204
205                         if (((names)[i] == NULL) || ((domains)[i] == NULL)) {
206                                 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
207                                 result = NT_STATUS_UNSUCCESSFUL;
208                                 goto done;
209                         }
210
211                 } else {
212                         (names)[i] = NULL;
213                         (domains)[i] = NULL;
214                         (types)[i] = SID_NAME_UNKNOWN;
215                 }
216         }
217
218 done:
219         TALLOC_FREE(tmp_ctx);
220         return result;
221 }
222
223 /* Lookup a list of sids
224  *
225  * do it the right way: there is a limit (of 20480 for w2k3) entries
226  * returned by this call. when the sids list contains more entries,
227  * empty lists are returned. This version of lsa_lookup_sids passes
228  * the list of sids in hunks of LOOKUP_SIDS_HUNK_SIZE to the lsa call. */
229
230 /* This constant defines the limit of how many sids to look up
231  * in one call (maximum). the limit from the server side is
232  * at 20480 for win2k3, but we keep it at a save 1000 for now. */
233 #define LOOKUP_SIDS_HUNK_SIZE 1000
234
235 NTSTATUS rpccli_lsa_lookup_sids(struct rpc_pipe_client *cli,
236                                 TALLOC_CTX *mem_ctx,
237                                 POLICY_HND *pol,
238                                 int num_sids,
239                                 const DOM_SID *sids,
240                                 char ***domains,
241                                 char ***names,
242                                 enum lsa_SidType **types)
243 {
244         NTSTATUS result = NT_STATUS_OK;
245         int sids_left = 0;
246         int sids_processed = 0;
247         const DOM_SID *hunk_sids = sids;
248         char **hunk_domains = NULL;
249         char **hunk_names = NULL;
250         enum lsa_SidType *hunk_types = NULL;
251
252         if (num_sids) {
253                 if (!((*domains) = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
254                         DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
255                         result = NT_STATUS_NO_MEMORY;
256                         goto fail;
257                 }
258
259                 if (!((*names) = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
260                         DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
261                         result = NT_STATUS_NO_MEMORY;
262                         goto fail;
263                 }
264
265                 if (!((*types) = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_sids))) {
266                         DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
267                         result = NT_STATUS_NO_MEMORY;
268                         goto fail;
269                 }
270         } else {
271                 (*domains) = NULL;
272                 (*names) = NULL;
273                 (*types) = NULL;
274         }
275
276         sids_left = num_sids;
277         hunk_domains = *domains;
278         hunk_names = *names;
279         hunk_types = *types;
280
281         while (sids_left > 0) {
282                 int hunk_num_sids;
283                 NTSTATUS hunk_result = NT_STATUS_OK;
284
285                 hunk_num_sids = ((sids_left > LOOKUP_SIDS_HUNK_SIZE)
286                                 ? LOOKUP_SIDS_HUNK_SIZE
287                                 : sids_left);
288
289                 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
290                            "%d -- %d of %d.\n",
291                            sids_processed,
292                            sids_processed + hunk_num_sids - 1,
293                            num_sids));
294
295                 hunk_result = rpccli_lsa_lookup_sids_noalloc(cli,
296                                                              mem_ctx,
297                                                              pol,
298                                                              hunk_num_sids,
299                                                              hunk_sids,
300                                                              hunk_domains,
301                                                              hunk_names,
302                                                              hunk_types);
303
304                 if (!NT_STATUS_IS_OK(hunk_result) &&
305                     !NT_STATUS_EQUAL(hunk_result, STATUS_SOME_UNMAPPED) &&
306                     !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED))
307                 {
308                         /* An actual error occured */
309                         result = hunk_result;
310                         goto fail;
311                 }
312
313                 /* adapt overall result */
314                 if (( NT_STATUS_IS_OK(result) &&
315                      !NT_STATUS_IS_OK(hunk_result))
316                     ||
317                     ( NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
318                      !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED)))
319                 {
320                         result = STATUS_SOME_UNMAPPED;
321                 }
322
323                 sids_left -= hunk_num_sids;
324                 sids_processed += hunk_num_sids; /* only used in DEBUG */
325                 hunk_sids += hunk_num_sids;
326                 hunk_domains += hunk_num_sids;
327                 hunk_names += hunk_num_sids;
328                 hunk_types += hunk_num_sids;
329         }
330
331         return result;
332
333 fail:
334         TALLOC_FREE(*domains);
335         TALLOC_FREE(*names);
336         TALLOC_FREE(*types);
337         return result;
338 }
339
340 /** Lookup a list of names */
341
342 NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
343                                  TALLOC_CTX *mem_ctx,
344                                  POLICY_HND *pol, int num_names,
345                                  const char **names,
346                                  const char ***dom_names,
347                                  int level,
348                                  DOM_SID **sids,
349                                  enum lsa_SidType **types)
350 {
351         prs_struct qbuf, rbuf;
352         LSA_Q_LOOKUP_NAMES q;
353         LSA_R_LOOKUP_NAMES r;
354         DOM_R_REF ref;
355         NTSTATUS result;
356         int i;
357
358         ZERO_STRUCT(q);
359         ZERO_STRUCT(r);
360
361         ZERO_STRUCT(ref);
362         r.dom_ref = &ref;
363
364         init_q_lookup_names(mem_ctx, &q, pol, num_names, names, level);
365
366         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_LOOKUPNAMES,
367                         q, r,
368                         qbuf, rbuf,
369                         lsa_io_q_lookup_names,
370                         lsa_io_r_lookup_names,
371                         NT_STATUS_UNSUCCESSFUL);
372
373         result = r.status;
374
375         if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
376             NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
377
378                 /* An actual error occured */
379
380                 goto done;
381         }
382
383         /* Return output parameters */
384
385         if (r.mapped_count == 0) {
386                 result = NT_STATUS_NONE_MAPPED;
387                 goto done;
388         }
389
390         if (num_names) {
391                 if (!((*sids = TALLOC_ARRAY(mem_ctx, DOM_SID, num_names)))) {
392                         DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
393                         result = NT_STATUS_NO_MEMORY;
394                         goto done;
395                 }
396
397                 if (!((*types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_names)))) {
398                         DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
399                         result = NT_STATUS_NO_MEMORY;
400                         goto done;
401                 }
402
403                 if (dom_names != NULL) {
404                         *dom_names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
405                         if (*dom_names == NULL) {
406                                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
407                                 result = NT_STATUS_NO_MEMORY;
408                                 goto done;
409                         }
410                 }
411         } else {
412                 *sids = NULL;
413                 *types = NULL;
414                 if (dom_names != NULL) {
415                         *dom_names = NULL;
416                 }
417         }
418
419         for (i = 0; i < num_names; i++) {
420                 DOM_RID *t_rids = r.dom_rid;
421                 uint32 dom_idx = t_rids[i].rid_idx;
422                 uint32 dom_rid = t_rids[i].rid;
423                 DOM_SID *sid = &(*sids)[i];
424
425                 /* Translate optimised sid through domain index array */
426
427                 if (dom_idx == 0xffffffff) {
428                         /* Nothing to do, this is unknown */
429                         ZERO_STRUCTP(sid);
430                         (*types)[i] = SID_NAME_UNKNOWN;
431                         continue;
432                 }
433
434                 sid_copy(sid, &ref.ref_dom[dom_idx].ref_dom.sid);
435
436                 if (dom_rid != 0xffffffff) {
437                         sid_append_rid(sid, dom_rid);
438                 }
439
440                 (*types)[i] = t_rids[i].type;
441
442                 if (dom_names == NULL) {
443                         continue;
444                 }
445
446                 (*dom_names)[i] = rpcstr_pull_unistr2_talloc(
447                         *dom_names, &ref.ref_dom[dom_idx].uni_dom_name);
448         }
449
450  done:
451
452         return result;
453 }
454
455 /** Enumerate list of SIDs  */
456
457 NTSTATUS rpccli_lsa_enum_sids(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
458                                 POLICY_HND *pol, uint32 *enum_ctx, uint32 pref_max_length,
459                                 uint32 *num_sids, DOM_SID **sids)
460 {
461         prs_struct qbuf, rbuf;
462         LSA_Q_ENUM_ACCOUNTS q;
463         LSA_R_ENUM_ACCOUNTS r;
464         NTSTATUS result;
465         int i;
466
467         ZERO_STRUCT(q);
468         ZERO_STRUCT(r);
469
470         init_lsa_q_enum_accounts(&q, pol, *enum_ctx, pref_max_length);
471
472         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUM_ACCOUNTS,
473                 q, r,
474                 qbuf, rbuf,
475                 lsa_io_q_enum_accounts,
476                 lsa_io_r_enum_accounts,
477                 NT_STATUS_UNSUCCESSFUL);
478
479         result = r.status;
480
481         if (!NT_STATUS_IS_OK(result)) {
482                 goto done;
483         }
484
485         if (r.sids.num_entries==0)
486                 goto done;
487
488         /* Return output parameters */
489
490         *sids = TALLOC_ARRAY(mem_ctx, DOM_SID, r.sids.num_entries);
491         if (!*sids) {
492                 DEBUG(0, ("(cli_lsa_enum_sids): out of memory\n"));
493                 result = NT_STATUS_UNSUCCESSFUL;
494                 goto done;
495         }
496
497         /* Copy across names and sids */
498
499         for (i = 0; i < r.sids.num_entries; i++) {
500                 sid_copy(&(*sids)[i], &r.sids.sid[i].sid);
501         }
502
503         *num_sids= r.sids.num_entries;
504         *enum_ctx = r.enum_context;
505
506  done:
507
508         return result;
509 }
510
511 /** Enumerate user privileges
512  *
513  * @param cli Handle on an initialised SMB connection */
514
515 NTSTATUS rpccli_lsa_enum_privsaccount(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
516                              POLICY_HND *pol, uint32 *count, LUID_ATTR **set)
517 {
518         prs_struct qbuf, rbuf;
519         LSA_Q_ENUMPRIVSACCOUNT q;
520         LSA_R_ENUMPRIVSACCOUNT r;
521         NTSTATUS result;
522         int i;
523
524         ZERO_STRUCT(q);
525         ZERO_STRUCT(r);
526
527         /* Initialise input parameters */
528
529         init_lsa_q_enum_privsaccount(&q, pol);
530
531         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUMPRIVSACCOUNT,
532                 q, r,
533                 qbuf, rbuf,
534                 lsa_io_q_enum_privsaccount,
535                 lsa_io_r_enum_privsaccount,
536                 NT_STATUS_UNSUCCESSFUL);
537
538         /* Return output parameters */
539
540         result = r.status;
541
542         if (!NT_STATUS_IS_OK(result)) {
543                 goto done;
544         }
545
546         if (r.count == 0)
547                 goto done;
548
549         if (!((*set = TALLOC_ARRAY(mem_ctx, LUID_ATTR, r.count)))) {
550                 DEBUG(0, ("(cli_lsa_enum_privsaccount): out of memory\n"));
551                 result = NT_STATUS_UNSUCCESSFUL;
552                 goto done;
553         }
554
555         for (i=0; i<r.count; i++) {
556                 (*set)[i].luid.low = r.set.set[i].luid.low;
557                 (*set)[i].luid.high = r.set.set[i].luid.high;
558                 (*set)[i].attr = r.set.set[i].attr;
559         }
560
561         *count=r.count;
562  done:
563
564         return result;
565 }
566
567 /** Get a privilege value given its name */
568
569 NTSTATUS rpccli_lsa_lookup_priv_value(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
570                                  POLICY_HND *pol, const char *name, LUID *luid)
571 {
572         prs_struct qbuf, rbuf;
573         LSA_Q_LOOKUP_PRIV_VALUE q;
574         LSA_R_LOOKUP_PRIV_VALUE r;
575         NTSTATUS result;
576
577         ZERO_STRUCT(q);
578         ZERO_STRUCT(r);
579
580         /* Marshall data and send request */
581
582         init_lsa_q_lookup_priv_value(&q, pol, name);
583
584         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_LOOKUPPRIVVALUE,
585                 q, r,
586                 qbuf, rbuf,
587                 lsa_io_q_lookup_priv_value,
588                 lsa_io_r_lookup_priv_value,
589                 NT_STATUS_UNSUCCESSFUL);
590
591         result = r.status;
592
593         if (!NT_STATUS_IS_OK(result)) {
594                 goto done;
595         }
596
597         /* Return output parameters */
598
599         (*luid).low=r.luid.low;
600         (*luid).high=r.luid.high;
601
602  done:
603
604         return result;
605 }
606
607 /* Enumerate account rights This is similar to enum_privileges but
608    takes a SID directly, avoiding the open_account call.
609 */
610
611 NTSTATUS rpccli_lsa_enum_account_rights(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
612                                      POLICY_HND *pol, DOM_SID *sid,
613                                      uint32 *count, char ***priv_names)
614 {
615         prs_struct qbuf, rbuf;
616         LSA_Q_ENUM_ACCT_RIGHTS q;
617         LSA_R_ENUM_ACCT_RIGHTS r;
618         NTSTATUS result;
619         int i;
620         fstring *privileges;
621         char **names;
622
623         ZERO_STRUCT(q);
624         ZERO_STRUCT(r);
625
626         /* Marshall data and send request */
627         init_q_enum_acct_rights(&q, pol, 2, sid);
628
629         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUMACCTRIGHTS,
630                 q, r,
631                 qbuf, rbuf,
632                 lsa_io_q_enum_acct_rights,
633                 lsa_io_r_enum_acct_rights,
634                 NT_STATUS_UNSUCCESSFUL);
635
636         result = r.status;
637
638         if (!NT_STATUS_IS_OK(result)) {
639                 goto done;
640         }
641
642         *count = r.count;
643         if (! *count) {
644                 goto done;
645         }
646
647
648         privileges = TALLOC_ARRAY( mem_ctx, fstring, *count );
649         names      = TALLOC_ARRAY( mem_ctx, char *, *count );
650
651         if ((privileges == NULL) || (names == NULL)) {
652                 TALLOC_FREE(privileges);
653                 TALLOC_FREE(names);
654                 return NT_STATUS_NO_MEMORY;
655         }
656
657         for ( i=0; i<*count; i++ ) {
658                 UNISTR4 *uni_string = &r.rights->strings[i];
659
660                 if ( !uni_string->string )
661                         continue;
662
663                 rpcstr_pull( privileges[i], uni_string->string->buffer, sizeof(privileges[i]), -1, STR_TERMINATE );
664
665                 /* now copy to the return array */
666                 names[i] = talloc_strdup( mem_ctx, privileges[i] );
667         }
668
669         *priv_names = names;
670
671 done:
672
673         return result;
674 }
675
676
677
678 /* add account rights to an account. */
679
680 NTSTATUS rpccli_lsa_add_account_rights(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
681                                     POLICY_HND *pol, DOM_SID sid,
682                                         uint32 count, const char **privs_name)
683 {
684         prs_struct qbuf, rbuf;
685         LSA_Q_ADD_ACCT_RIGHTS q;
686         LSA_R_ADD_ACCT_RIGHTS r;
687         NTSTATUS result;
688
689         ZERO_STRUCT(q);
690         ZERO_STRUCT(r);
691
692         /* Marshall data and send request */
693         init_q_add_acct_rights(&q, pol, &sid, count, privs_name);
694
695         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ADDACCTRIGHTS,
696                 q, r,
697                 qbuf, rbuf,
698                 lsa_io_q_add_acct_rights,
699                 lsa_io_r_add_acct_rights,
700                 NT_STATUS_UNSUCCESSFUL);
701
702         result = r.status;
703
704         if (!NT_STATUS_IS_OK(result)) {
705                 goto done;
706         }
707 done:
708
709         return result;
710 }
711
712
713 /* remove account rights for an account. */
714
715 NTSTATUS rpccli_lsa_remove_account_rights(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
716                                        POLICY_HND *pol, DOM_SID sid, bool removeall,
717                                        uint32 count, const char **privs_name)
718 {
719         prs_struct qbuf, rbuf;
720         LSA_Q_REMOVE_ACCT_RIGHTS q;
721         LSA_R_REMOVE_ACCT_RIGHTS r;
722         NTSTATUS result;
723
724         ZERO_STRUCT(q);
725         ZERO_STRUCT(r);
726
727         /* Marshall data and send request */
728         init_q_remove_acct_rights(&q, pol, &sid, removeall?1:0, count, privs_name);
729
730         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_REMOVEACCTRIGHTS,
731                 q, r,
732                 qbuf, rbuf,
733                 lsa_io_q_remove_acct_rights,
734                 lsa_io_r_remove_acct_rights,
735                 NT_STATUS_UNSUCCESSFUL);
736
737         result = r.status;
738
739         if (!NT_STATUS_IS_OK(result)) {
740                 goto done;
741         }
742 done:
743
744         return result;
745 }
746
747
748 #if 0
749
750 /** An example of how to use the routines in this file.  Fetch a DOMAIN
751     sid. Does complete cli setup / teardown anonymously. */
752
753 bool fetch_domain_sid( char *domain, char *remote_machine, DOM_SID *psid)
754 {
755         struct cli_state cli;
756         NTSTATUS result;
757         POLICY_HND lsa_pol;
758         bool ret = False;
759
760         ZERO_STRUCT(cli);
761         if(cli_initialise(&cli) == False) {
762                 DEBUG(0,("fetch_domain_sid: unable to initialize client connection.\n"));
763                 return False;
764         }
765
766         if(!resolve_name( remote_machine, &cli.dest_ip, 0x20)) {
767                 DEBUG(0,("fetch_domain_sid: Can't resolve address for %s\n", remote_machine));
768                 goto done;
769         }
770
771         if (!cli_connect(&cli, remote_machine, &cli.dest_ip)) {
772                 DEBUG(0,("fetch_domain_sid: unable to connect to SMB server on \
773 machine %s. Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
774                 goto done;
775         }
776
777         if (!attempt_netbios_session_request(&cli, global_myname(), remote_machine, &cli.dest_ip)) {
778                 DEBUG(0,("fetch_domain_sid: machine %s rejected the NetBIOS session request.\n",
779                         remote_machine));
780                 goto done;
781         }
782
783         cli.protocol = PROTOCOL_NT1;
784
785         if (!cli_negprot(&cli)) {
786                 DEBUG(0,("fetch_domain_sid: machine %s rejected the negotiate protocol. \
787 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
788                 goto done;
789         }
790
791         if (cli.protocol != PROTOCOL_NT1) {
792                 DEBUG(0,("fetch_domain_sid: machine %s didn't negotiate NT protocol.\n",
793                         remote_machine));
794                 goto done;
795         }
796
797         /*
798          * Do an anonymous session setup.
799          */
800
801         if (!cli_session_setup(&cli, "", "", 0, "", 0, "")) {
802                 DEBUG(0,("fetch_domain_sid: machine %s rejected the session setup. \
803 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
804                 goto done;
805         }
806
807         if (!(cli.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
808                 DEBUG(0,("fetch_domain_sid: machine %s isn't in user level security mode\n",
809                         remote_machine));
810                 goto done;
811         }
812
813         if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
814                 DEBUG(0,("fetch_domain_sid: machine %s rejected the tconX on the IPC$ share. \
815 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
816                 goto done;
817         }
818
819         /* Fetch domain sid */
820
821         if (!cli_nt_session_open(&cli, PI_LSARPC)) {
822                 DEBUG(0, ("fetch_domain_sid: Error connecting to SAM pipe\n"));
823                 goto done;
824         }
825
826         result = cli_lsa_open_policy(&cli, cli.mem_ctx, True, SEC_RIGHTS_QUERY_VALUE, &lsa_pol);
827         if (!NT_STATUS_IS_OK(result)) {
828                 DEBUG(0, ("fetch_domain_sid: Error opening lsa policy handle. %s\n",
829                         nt_errstr(result) ));
830                 goto done;
831         }
832
833         result = cli_lsa_query_info_policy(&cli, cli.mem_ctx, &lsa_pol, 5, domain, psid);
834         if (!NT_STATUS_IS_OK(result)) {
835                 DEBUG(0, ("fetch_domain_sid: Error querying lsa policy handle. %s\n",
836                         nt_errstr(result) ));
837                 goto done;
838         }
839
840         ret = True;
841
842   done:
843
844         cli_shutdown(&cli);
845         return ret;
846 }
847
848 #endif