2 Unix SMB/CIFS implementation.
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 3 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, see <http://www.gnu.org/licenses/>.
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 #include "libsmb/libsmb.h"
29 /********************************************************
30 Connection cachine struct. Goes away when ctx destroyed.
31 ********************************************************/
36 struct cli_state
*cli
;
37 struct rpc_pipe_client
*lsapipe
;
38 struct policy_handle pol
;
41 static struct con_struct
*cs
;
43 /********************************************************
44 Close connection on context destruction.
45 ********************************************************/
47 static int cs_destructor(struct con_struct
*p
)
50 cli_shutdown(cs
->cli
);
56 /********************************************************
57 Create the connection to localhost.
58 ********************************************************/
60 static struct con_struct
*create_cs(struct net_context
*c
,
61 TALLOC_CTX
*ctx
, NTSTATUS
*perr
)
64 struct sockaddr_storage loopback_ss
;
65 struct cli_credentials
*anon_creds
= NULL
;
69 if (!interpret_string_addr(&loopback_ss
, "127.0.0.1", AI_NUMERICHOST
)) {
70 *perr
= NT_STATUS_INVALID_PARAMETER
;
75 if (cs
->failed_connect
) {
82 cs
= talloc(ctx
, struct con_struct
);
84 *perr
= NT_STATUS_NO_MEMORY
;
88 anon_creds
= cli_credentials_init_anon(cs
);
89 if (anon_creds
== NULL
) {
91 *perr
= NT_STATUS_NO_MEMORY
;
96 talloc_set_destructor(cs
, cs_destructor
);
98 nt_status
= cli_full_connection_creds(c
,
107 CLI_FULL_CONNECTION_IPC
);
109 if (!NT_STATUS_IS_OK(nt_status
)) {
110 DEBUG(2,("create_cs: Connect failed. Error was %s\n", nt_errstr(nt_status
)));
111 cs
->failed_connect
= true;
117 nt_status
= cli_rpc_pipe_open_noauth(cs
->cli
,
121 if (!NT_STATUS_IS_OK(nt_status
)) {
122 DEBUG(2,("create_cs: open LSA pipe failed. Error was %s\n", nt_errstr(nt_status
)));
123 cs
->failed_connect
= true;
129 nt_status
= rpccli_lsa_open_policy(cs
->lsapipe
, ctx
, true,
130 SEC_FLAG_MAXIMUM_ALLOWED
,
133 if (!NT_STATUS_IS_OK(nt_status
)) {
134 DEBUG(2,("create_cs: rpccli_lsa_open_policy failed. Error was %s\n", nt_errstr(nt_status
)));
135 cs
->failed_connect
= true;
144 /********************************************************
145 Do a lookup_sids call to localhost.
146 Check if the local machine is authoritative for this sid. We can't
147 check if this is our SID as that's stored in the root-read-only
149 The local smbd will also ask winbindd for us, so we don't have to.
150 ********************************************************/
152 NTSTATUS
net_lookup_name_from_sid(struct net_context
*c
,
154 struct dom_sid
*psid
,
155 const char **ppdomain
,
159 struct con_struct
*csp
= NULL
;
162 enum lsa_SidType
*types
;
167 csp
= create_cs(c
, ctx
, &nt_status
);
172 nt_status
= rpccli_lsa_lookup_sids(csp
->lsapipe
, ctx
,
179 if (!NT_STATUS_IS_OK(nt_status
)) {
183 *ppdomain
= domains
[0];
185 /* Don't care about type here. */
191 /********************************************************
192 Do a lookup_names call to localhost.
193 ********************************************************/
195 NTSTATUS
net_lookup_sid_from_name(struct net_context
*c
, TALLOC_CTX
*ctx
,
196 const char *full_name
, struct dom_sid
*pret_sid
)
199 struct con_struct
*csp
= NULL
;
200 struct dom_sid
*sids
= NULL
;
201 enum lsa_SidType
*types
= NULL
;
203 csp
= create_cs(c
, ctx
, &nt_status
);
208 nt_status
= rpccli_lsa_lookup_names(csp
->lsapipe
, ctx
,
215 if (!NT_STATUS_IS_OK(nt_status
)) {