s3-rpc_client: Move client pipe functions to own header.
[samba.git] / source3 / utils / netlookup.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Name lookup.
5
6    Copyright (C) Jeremy Allison 2005
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "utils/net.h"
24 #include "rpc_client/cli_pipe.h"
25 #include "../librpc/gen_ndr/ndr_lsa.h"
26 #include "rpc_client/cli_lsarpc.h"
27
28 /********************************************************
29  Connection cachine struct. Goes away when ctx destroyed.
30 ********************************************************/
31
32 struct con_struct {
33         bool failed_connect;
34         NTSTATUS err;
35         struct cli_state *cli;
36         struct rpc_pipe_client *lsapipe;
37         struct policy_handle pol;
38 };
39
40 static struct con_struct *cs;
41
42 /********************************************************
43  Close connection on context destruction.
44 ********************************************************/
45
46 static int cs_destructor(struct con_struct *p)
47 {
48         if (cs->cli) {
49                 cli_shutdown(cs->cli);
50         }
51         cs = NULL;
52         return 0;
53 }
54
55 /********************************************************
56  Create the connection to localhost.
57 ********************************************************/
58
59 static struct con_struct *create_cs(struct net_context *c,
60                                     TALLOC_CTX *ctx, NTSTATUS *perr)
61 {
62         NTSTATUS nt_status;
63         struct sockaddr_storage loopback_ss;
64
65         *perr = NT_STATUS_OK;
66
67         if (!interpret_string_addr(&loopback_ss, "127.0.0.1", AI_NUMERICHOST)) {
68                 *perr = NT_STATUS_INVALID_PARAMETER;
69                 return NULL;
70         }
71
72         if (cs) {
73                 if (cs->failed_connect) {
74                         *perr = cs->err;
75                         return NULL;
76                 }
77                 return cs;
78         }
79
80         cs = TALLOC_P(ctx, struct con_struct);
81         if (!cs) {
82                 *perr = NT_STATUS_NO_MEMORY;
83                 return NULL;
84         }
85
86         ZERO_STRUCTP(cs);
87         talloc_set_destructor(cs, cs_destructor);
88
89         /* Connect to localhost with given username/password. */
90         /* JRA. Pretty sure we can just do this anonymously.... */
91 #if 0
92         if (!opt_password && !opt_machine_pass) {
93                 char *pass = getpass("Password:");
94                 if (pass) {
95                         opt_password = SMB_STRDUP(pass);
96                 }
97         }
98 #endif
99
100         nt_status = cli_full_connection(&cs->cli, global_myname(), global_myname(),
101                                         &loopback_ss, 0,
102                                         "IPC$", "IPC",
103 #if 0
104                                         c->opt_user_name,
105                                         c->opt_workgroup,
106                                         c->opt_password,
107 #else
108                                         "",
109                                         c->opt_workgroup,
110                                         "",
111 #endif
112                                         0,
113                                         Undefined);
114
115         if (!NT_STATUS_IS_OK(nt_status)) {
116                 DEBUG(2,("create_cs: Connect failed. Error was %s\n", nt_errstr(nt_status)));
117                 cs->failed_connect = true;
118                 cs->err = nt_status;
119                 *perr = nt_status;
120                 return NULL;
121         }
122
123         nt_status = cli_rpc_pipe_open_noauth(cs->cli,
124                                         &ndr_table_lsarpc.syntax_id,
125                                         &cs->lsapipe);
126
127         if (!NT_STATUS_IS_OK(nt_status)) {
128                 DEBUG(2,("create_cs: open LSA pipe failed. Error was %s\n", nt_errstr(nt_status)));
129                 cs->failed_connect = true;
130                 cs->err = nt_status;
131                 *perr = nt_status;
132                 return NULL;
133         }
134
135         nt_status = rpccli_lsa_open_policy(cs->lsapipe, ctx, true,
136                                 SEC_FLAG_MAXIMUM_ALLOWED,
137                                 &cs->pol);
138
139         if (!NT_STATUS_IS_OK(nt_status)) {
140                 DEBUG(2,("create_cs: rpccli_lsa_open_policy failed. Error was %s\n", nt_errstr(nt_status)));
141                 cs->failed_connect = true;
142                 cs->err = nt_status;
143                 *perr = nt_status;
144                 return NULL;
145         }
146
147         return cs;
148 }
149
150 /********************************************************
151  Do a lookup_sids call to localhost.
152  Check if the local machine is authoritative for this sid. We can't
153  check if this is our SID as that's stored in the root-read-only
154  secrets.tdb.
155  The local smbd will also ask winbindd for us, so we don't have to.
156 ********************************************************/
157
158 NTSTATUS net_lookup_name_from_sid(struct net_context *c,
159                                 TALLOC_CTX *ctx,
160                                 struct dom_sid *psid,
161                                 const char **ppdomain,
162                                 const char **ppname)
163 {
164         NTSTATUS nt_status;
165         struct con_struct *csp = NULL;
166         char **domains;
167         char **names;
168         enum lsa_SidType *types;
169
170         *ppdomain = NULL;
171         *ppname = NULL;
172
173         csp = create_cs(c, ctx, &nt_status);
174         if (csp == NULL) {
175                 return nt_status;
176         }
177
178         nt_status = rpccli_lsa_lookup_sids(csp->lsapipe, ctx,
179                                                 &csp->pol,
180                                                 1, psid,
181                                                 &domains,
182                                                 &names,
183                                                 &types);
184
185         if (!NT_STATUS_IS_OK(nt_status)) {
186                 return nt_status;
187         }
188
189         *ppdomain = domains[0];
190         *ppname = names[0];
191         /* Don't care about type here. */
192
193         /* Converted OK */
194         return NT_STATUS_OK;
195 }
196
197 /********************************************************
198  Do a lookup_names call to localhost.
199 ********************************************************/
200
201 NTSTATUS net_lookup_sid_from_name(struct net_context *c, TALLOC_CTX *ctx,
202                                   const char *full_name, struct dom_sid *pret_sid)
203 {
204         NTSTATUS nt_status;
205         struct con_struct *csp = NULL;
206         struct dom_sid *sids = NULL;
207         enum lsa_SidType *types = NULL;
208
209         csp = create_cs(c, ctx, &nt_status);
210         if (csp == NULL) {
211                 return nt_status;
212         }
213
214         nt_status = rpccli_lsa_lookup_names(csp->lsapipe, ctx,
215                                                 &csp->pol,
216                                                 1,
217                                                 &full_name,
218                                                 NULL, 1,
219                                                 &sids, &types);
220
221         if (!NT_STATUS_IS_OK(nt_status)) {
222                 return nt_status;
223         }
224
225         *pret_sid = sids[0];
226
227         /* Converted OK */
228         return NT_STATUS_OK;
229 }