RIP BOOL. Convert BOOL -> bool. I found a few interesting
[kai/samba.git] / source / utils / net_util.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Helper routines for net
4  *  Copyright (C) Volker Lendecke 2006
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "includes.h"
22 #include "utils/net.h"
23
24 bool is_valid_policy_hnd(const POLICY_HND *hnd)
25 {
26         POLICY_HND tmp;
27         ZERO_STRUCT(tmp);
28         return (memcmp(&tmp, hnd, sizeof(tmp)) != 0);
29 }
30
31 NTSTATUS net_rpc_lookup_name(TALLOC_CTX *mem_ctx, struct cli_state *cli,
32                              const char *name, const char **ret_domain,
33                              const char **ret_name, DOM_SID *ret_sid,
34                              enum lsa_SidType *ret_type)
35 {
36         struct rpc_pipe_client *lsa_pipe;
37         POLICY_HND pol;
38         NTSTATUS result = NT_STATUS_OK;
39         const char **dom_names;
40         DOM_SID *sids;
41         enum lsa_SidType *types;
42
43         ZERO_STRUCT(pol);
44
45         lsa_pipe = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
46         if (lsa_pipe == NULL) {
47                 d_fprintf(stderr, "Could not initialise lsa pipe\n");
48                 return result;
49         }
50
51         result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, False, 
52                                         SEC_RIGHTS_MAXIMUM_ALLOWED,
53                                         &pol);
54         if (!NT_STATUS_IS_OK(result)) {
55                 d_fprintf(stderr, "open_policy failed: %s\n",
56                           nt_errstr(result));
57                 return result;
58         }
59
60         result = rpccli_lsa_lookup_names(lsa_pipe, mem_ctx, &pol, 1,
61                                          &name, &dom_names, 1, &sids, &types);
62
63         if (!NT_STATUS_IS_OK(result)) {
64                 /* This can happen easily, don't log an error */
65                 goto done;
66         }
67
68         if (ret_domain != NULL) {
69                 *ret_domain = dom_names[0];
70         }
71         if (ret_name != NULL) {
72                 *ret_name = talloc_strdup(mem_ctx, name);
73         }
74         if (ret_sid != NULL) {
75                 sid_copy(ret_sid, &sids[0]);
76         }
77         if (ret_type != NULL) {
78                 *ret_type = types[0];
79         }
80
81  done:
82         if (is_valid_policy_hnd(&pol)) {
83                 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
84         }
85         cli_rpc_pipe_close(lsa_pipe);
86
87         return result;
88 }