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