Fix another segfault.
[Samba.git] / source / utils / netlookup.c
blob33b6c4bb257f3cde765b33ac786e7961befc74a6
1 /*
2 Unix SMB/CIFS implementation.
4 Name lookup.
6 Copyright (C) Jeremy Allison 2005
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 2 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
24 #include "utils/net.h"
26 /********************************************************
27 Connection cachine struct. Goes away when ctx destroyed.
28 ********************************************************/
30 struct con_struct {
31 BOOL failed_connect;
32 NTSTATUS err;
33 struct cli_state *cli;
34 struct rpc_pipe_client *lsapipe;
35 POLICY_HND pol;
38 static struct con_struct *cs;
40 /********************************************************
41 Close connection on context destruction.
42 ********************************************************/
44 static int cs_destructor(struct con_struct *p)
46 if (cs->cli) {
47 cli_shutdown(cs->cli);
49 cs = NULL;
50 return 0;
53 /********************************************************
54 Create the connection to localhost.
55 ********************************************************/
57 static struct con_struct *create_cs(TALLOC_CTX *ctx, NTSTATUS *perr)
59 NTSTATUS nt_status;
60 struct in_addr loopback_ip = *interpret_addr2("127.0.0.1");
62 *perr = NT_STATUS_OK;
64 if (cs) {
65 if (cs->failed_connect) {
66 *perr = cs->err;
67 return NULL;
69 return cs;
72 cs = TALLOC_P(ctx, struct con_struct);
73 if (!cs) {
74 *perr = NT_STATUS_NO_MEMORY;
75 return NULL;
78 ZERO_STRUCTP(cs);
79 talloc_set_destructor(cs, cs_destructor);
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);
90 #endif
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
101 opt_workgroup,
103 #endif
105 Undefined,
106 NULL);
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;
116 cs->lsapipe = cli_rpc_pipe_open_noauth(cs->cli,
117 PI_LSARPC,
118 &nt_status);
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;
128 nt_status = rpccli_lsa_open_policy(cs->lsapipe, ctx, True,
129 SEC_RIGHTS_MAXIMUM_ALLOWED,
130 &cs->pol);
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;
140 return cs;
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 ********************************************************/
151 NTSTATUS net_lookup_name_from_sid(TALLOC_CTX *ctx,
152 DOM_SID *psid,
153 const char **ppdomain,
154 const char **ppname)
156 NTSTATUS nt_status;
157 struct con_struct *csp = NULL;
158 char **domains;
159 char **names;
160 enum lsa_SidType *types;
162 *ppdomain = NULL;
163 *ppname = NULL;
165 csp = create_cs(ctx, &nt_status);
166 if (csp == NULL) {
167 return nt_status;
170 nt_status = rpccli_lsa_lookup_sids(csp->lsapipe, ctx,
171 &csp->pol,
172 1, psid,
173 &domains,
174 &names,
175 &types);
177 if (!NT_STATUS_IS_OK(nt_status)) {
178 return nt_status;
181 *ppdomain = domains[0];
182 *ppname = names[0];
183 /* Don't care about type here. */
185 /* Converted OK */
186 return NT_STATUS_OK;
189 /********************************************************
190 Do a lookup_names call to localhost.
191 ********************************************************/
193 NTSTATUS net_lookup_sid_from_name(TALLOC_CTX *ctx, const char *full_name, DOM_SID *pret_sid)
195 NTSTATUS nt_status;
196 struct con_struct *csp = NULL;
197 DOM_SID *sids = NULL;
198 enum lsa_SidType *types = NULL;
200 csp = create_cs(ctx, &nt_status);
201 if (csp == NULL) {
202 return nt_status;
205 nt_status = rpccli_lsa_lookup_names(csp->lsapipe, ctx,
206 &csp->pol,
208 &full_name,
209 NULL, &sids,
210 &types);
212 if (!NT_STATUS_IS_OK(nt_status)) {
213 return nt_status;
216 *pret_sid = sids[0];
218 /* Converted OK */
219 return NT_STATUS_OK;