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