fa631314c485b13f37241cede6ceca2703f7c38c
[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 "../librpc/gen_ndr/ndr_lsa.h"
25 #include "rpc_client/cli_lsarpc.h"
26
27 /********************************************************
28  Connection cachine struct. Goes away when ctx destroyed.
29 ********************************************************/
30
31 struct con_struct {
32         bool failed_connect;
33         NTSTATUS err;
34         struct cli_state *cli;
35         struct rpc_pipe_client *lsapipe;
36         struct policy_handle pol;
37 };
38
39 static struct con_struct *cs;
40
41 /********************************************************
42  Close connection on context destruction.
43 ********************************************************/
44
45 static int cs_destructor(struct con_struct *p)
46 {
47         if (cs->cli) {
48                 cli_shutdown(cs->cli);
49         }
50         cs = NULL;
51         return 0;
52 }
53
54 /********************************************************
55  Create the connection to localhost.
56 ********************************************************/
57
58 static struct con_struct *create_cs(struct net_context *c,
59                                     TALLOC_CTX *ctx, NTSTATUS *perr)
60 {
61         NTSTATUS nt_status;
62         struct sockaddr_storage loopback_ss;
63
64         *perr = NT_STATUS_OK;
65
66         if (!interpret_string_addr(&loopback_ss, "127.0.0.1", AI_NUMERICHOST)) {
67                 *perr = NT_STATUS_INVALID_PARAMETER;
68                 return NULL;
69         }
70
71         if (cs) {
72                 if (cs->failed_connect) {
73                         *perr = cs->err;
74                         return NULL;
75                 }
76                 return cs;
77         }
78
79         cs = TALLOC_P(ctx, struct con_struct);
80         if (!cs) {
81                 *perr = NT_STATUS_NO_MEMORY;
82                 return NULL;
83         }
84
85         ZERO_STRUCTP(cs);
86         talloc_set_destructor(cs, cs_destructor);
87
88         /* Connect to localhost with given username/password. */
89         /* JRA. Pretty sure we can just do this anonymously.... */
90 #if 0
91         if (!opt_password && !opt_machine_pass) {
92                 char *pass = getpass("Password:");
93                 if (pass) {
94                         opt_password = SMB_STRDUP(pass);
95                 }
96         }
97 #endif
98
99         nt_status = cli_full_connection(&cs->cli, global_myname(), global_myname(),
100                                         &loopback_ss, 0,
101                                         "IPC$", "IPC",
102 #if 0
103                                         c->opt_user_name,
104                                         c->opt_workgroup,
105                                         c->opt_password,
106 #else
107                                         "",
108                                         c->opt_workgroup,
109                                         "",
110 #endif
111                                         0,
112                                         Undefined);
113
114         if (!NT_STATUS_IS_OK(nt_status)) {
115                 DEBUG(2,("create_cs: Connect failed. Error was %s\n", nt_errstr(nt_status)));
116                 cs->failed_connect = true;
117                 cs->err = nt_status;
118                 *perr = nt_status;
119                 return NULL;
120         }
121
122         nt_status = cli_rpc_pipe_open_noauth(cs->cli,
123                                         &ndr_table_lsarpc.syntax_id,
124                                         &cs->lsapipe);
125
126         if (!NT_STATUS_IS_OK(nt_status)) {
127                 DEBUG(2,("create_cs: open LSA pipe failed. Error was %s\n", nt_errstr(nt_status)));
128                 cs->failed_connect = true;
129                 cs->err = nt_status;
130                 *perr = nt_status;
131                 return NULL;
132         }
133
134         nt_status = rpccli_lsa_open_policy(cs->lsapipe, ctx, true,
135                                 SEC_FLAG_MAXIMUM_ALLOWED,
136                                 &cs->pol);
137
138         if (!NT_STATUS_IS_OK(nt_status)) {
139                 DEBUG(2,("create_cs: rpccli_lsa_open_policy failed. Error was %s\n", nt_errstr(nt_status)));
140                 cs->failed_connect = true;
141                 cs->err = nt_status;
142                 *perr = nt_status;
143                 return NULL;
144         }
145
146         return cs;
147 }
148
149 /********************************************************
150  Do a lookup_sids call to localhost.
151  Check if the local machine is authoritative for this sid. We can't
152  check if this is our SID as that's stored in the root-read-only
153  secrets.tdb.
154  The local smbd will also ask winbindd for us, so we don't have to.
155 ********************************************************/
156
157 NTSTATUS net_lookup_name_from_sid(struct net_context *c,
158                                 TALLOC_CTX *ctx,
159                                 struct dom_sid *psid,
160                                 const char **ppdomain,
161                                 const char **ppname)
162 {
163         NTSTATUS nt_status;
164         struct con_struct *csp = NULL;
165         char **domains;
166         char **names;
167         enum lsa_SidType *types;
168
169         *ppdomain = NULL;
170         *ppname = NULL;
171
172         csp = create_cs(c, ctx, &nt_status);
173         if (csp == NULL) {
174                 return nt_status;
175         }
176
177         nt_status = rpccli_lsa_lookup_sids(csp->lsapipe, ctx,
178                                                 &csp->pol,
179                                                 1, psid,
180                                                 &domains,
181                                                 &names,
182                                                 &types);
183
184         if (!NT_STATUS_IS_OK(nt_status)) {
185                 return nt_status;
186         }
187
188         *ppdomain = domains[0];
189         *ppname = names[0];
190         /* Don't care about type here. */
191
192         /* Converted OK */
193         return NT_STATUS_OK;
194 }
195
196 /********************************************************
197  Do a lookup_names call to localhost.
198 ********************************************************/
199
200 NTSTATUS net_lookup_sid_from_name(struct net_context *c, TALLOC_CTX *ctx,
201                                   const char *full_name, struct dom_sid *pret_sid)
202 {
203         NTSTATUS nt_status;
204         struct con_struct *csp = NULL;
205         struct dom_sid *sids = NULL;
206         enum lsa_SidType *types = NULL;
207
208         csp = create_cs(c, ctx, &nt_status);
209         if (csp == NULL) {
210                 return nt_status;
211         }
212
213         nt_status = rpccli_lsa_lookup_names(csp->lsapipe, ctx,
214                                                 &csp->pol,
215                                                 1,
216                                                 &full_name,
217                                                 NULL, 1,
218                                                 &sids, &types);
219
220         if (!NT_STATUS_IS_OK(nt_status)) {
221                 return nt_status;
222         }
223
224         *pret_sid = sids[0];
225
226         /* Converted OK */
227         return NT_STATUS_OK;
228 }