4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Andrew Bartlett 2001-2003,2011
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "auth/ntlmssp/ntlmssp.h"
23 #include "auth_generic.h"
24 #include "auth/gensec/gensec.h"
25 #include "auth/credentials/credentials.h"
26 #include "librpc/rpc/dcerpc.h"
27 #include "lib/param/param.h"
28 #include "librpc/crypto/gse.h"
30 NTSTATUS
auth_generic_set_username(struct auth_generic_state
*ans
,
33 cli_credentials_set_username(ans
->credentials
, user
, CRED_SPECIFIED
);
37 NTSTATUS
auth_generic_set_domain(struct auth_generic_state
*ans
,
40 cli_credentials_set_domain(ans
->credentials
, domain
, CRED_SPECIFIED
);
44 NTSTATUS
auth_generic_set_password(struct auth_generic_state
*ans
,
47 cli_credentials_set_password(ans
->credentials
, password
, CRED_SPECIFIED
);
51 NTSTATUS
auth_generic_set_creds(struct auth_generic_state
*ans
,
52 struct cli_credentials
*creds
)
54 talloc_unlink(ans
->credentials
, creds
);
55 ans
->credentials
= creds
;
59 NTSTATUS
auth_generic_client_prepare(TALLOC_CTX
*mem_ctx
, struct auth_generic_state
**auth_generic_state
)
61 struct auth_generic_state
*ans
;
64 struct gensec_settings
*gensec_settings
;
65 const struct gensec_security_ops
**backends
= NULL
;
66 struct loadparm_context
*lp_ctx
;
69 ans
= talloc_zero(mem_ctx
, struct auth_generic_state
);
71 DEBUG(0,("auth_generic_start: talloc failed!\n"));
72 return NT_STATUS_NO_MEMORY
;
75 lp_ctx
= loadparm_init_s3(ans
, loadparm_s3_helpers());
77 DEBUG(10, ("loadparm_init_s3 failed\n"));
79 return NT_STATUS_INVALID_SERVER_STATE
;
82 gensec_settings
= lpcfg_gensec_settings(ans
, lp_ctx
);
84 DEBUG(10, ("lpcfg_gensec_settings failed\n"));
86 return NT_STATUS_NO_MEMORY
;
89 backends
= talloc_zero_array(gensec_settings
,
90 const struct gensec_security_ops
*, 7);
91 if (backends
== NULL
) {
93 return NT_STATUS_NO_MEMORY
;
95 gensec_settings
->backends
= backends
;
99 /* These need to be in priority order, krb5 before NTLMSSP */
100 #if defined(HAVE_KRB5)
101 backends
[idx
++] = &gensec_gse_krb5_security_ops
;
104 backends
[idx
++] = gensec_security_by_oid(NULL
, GENSEC_OID_NTLMSSP
);
105 backends
[idx
++] = gensec_security_by_name(NULL
, "ntlmssp_resume_ccache");
107 backends
[idx
++] = gensec_security_by_oid(NULL
, GENSEC_OID_SPNEGO
);
108 backends
[idx
++] = gensec_security_by_auth_type(NULL
, DCERPC_AUTH_TYPE_SCHANNEL
);
109 backends
[idx
++] = gensec_security_by_auth_type(NULL
, DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM
);
111 nt_status
= gensec_client_start(ans
, &ans
->gensec_security
, gensec_settings
);
113 if (!NT_STATUS_IS_OK(nt_status
)) {
118 ans
->credentials
= cli_credentials_init(ans
);
119 if (!ans
->credentials
) {
121 return NT_STATUS_NO_MEMORY
;
124 ok
= cli_credentials_guess(ans
->credentials
, lp_ctx
);
127 return NT_STATUS_INTERNAL_ERROR
;
130 talloc_unlink(ans
, lp_ctx
);
131 talloc_unlink(ans
, gensec_settings
);
133 *auth_generic_state
= ans
;
137 NTSTATUS
auth_generic_client_start(struct auth_generic_state
*ans
, const char *oid
)
141 /* Transfer the credentials to gensec */
142 status
= gensec_set_credentials(ans
->gensec_security
, ans
->credentials
);
143 if (!NT_STATUS_IS_OK(status
)) {
144 DEBUG(1, ("Failed to set GENSEC credentials: %s\n",
148 talloc_unlink(ans
, ans
->credentials
);
149 ans
->credentials
= NULL
;
151 status
= gensec_start_mech_by_oid(ans
->gensec_security
,
153 if (!NT_STATUS_IS_OK(status
)) {
160 NTSTATUS
auth_generic_client_start_by_name(struct auth_generic_state
*ans
,
165 /* Transfer the credentials to gensec */
166 status
= gensec_set_credentials(ans
->gensec_security
, ans
->credentials
);
167 if (!NT_STATUS_IS_OK(status
)) {
168 DEBUG(1, ("Failed to set GENSEC credentials: %s\n",
172 talloc_unlink(ans
, ans
->credentials
);
173 ans
->credentials
= NULL
;
175 status
= gensec_start_mech_by_name(ans
->gensec_security
, name
);
176 if (!NT_STATUS_IS_OK(status
)) {
183 NTSTATUS
auth_generic_client_start_by_authtype(struct auth_generic_state
*ans
,
189 /* Transfer the credentials to gensec */
190 status
= gensec_set_credentials(ans
->gensec_security
, ans
->credentials
);
191 if (!NT_STATUS_IS_OK(status
)) {
192 DEBUG(1, ("Failed to set GENSEC credentials: %s\n",
196 talloc_unlink(ans
, ans
->credentials
);
197 ans
->credentials
= NULL
;
199 status
= gensec_start_mech_by_authtype(ans
->gensec_security
,
200 auth_type
, auth_level
);
201 if (!NT_STATUS_IS_OK(status
)) {
208 NTSTATUS
auth_generic_client_start_by_sasl(struct auth_generic_state
*ans
,
209 const char **sasl_list
)
213 /* Transfer the credentials to gensec */
214 status
= gensec_set_credentials(ans
->gensec_security
, ans
->credentials
);
215 if (!NT_STATUS_IS_OK(status
)) {
216 DEBUG(1, ("Failed to set GENSEC credentials: %s\n",
220 talloc_unlink(ans
, ans
->credentials
);
221 ans
->credentials
= NULL
;
223 status
= gensec_start_mech_by_sasl_list(ans
->gensec_security
, sasl_list
);
224 if (!NT_STATUS_IS_OK(status
)) {