s3-smbldap: move ldap_open_with_timeout out of smb_ldap.h to ads where it lives.
[Samba/gebeck_regimport.git] / source3 / auth / auth_samba4.c
blob21c7b44eedf2a503458881a9a9262f64c29f99b0
1 /*
2 Unix SMB/CIFS implementation.
3 Authenticate against Samba4's auth subsystem
4 Copyright (C) Volker Lendecke 2008
5 Copyright (C) Andrew Bartlett 2010
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/>.
21 #include "includes.h"
22 #include "source3/include/auth.h"
23 #include "source4/auth/auth.h"
24 #include "auth/auth_sam_reply.h"
25 #include "param/param.h"
26 #include "source4/lib/events/events.h"
27 #include "source4/lib/messaging/messaging.h"
28 #include "auth/gensec/gensec.h"
29 #include "auth/credentials/credentials.h"
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_AUTH
34 static NTSTATUS check_samba4_security(const struct auth_context *auth_context,
35 void *my_private_data,
36 TALLOC_CTX *mem_ctx,
37 const struct auth_usersupplied_info *user_info,
38 struct auth_serversupplied_info **server_info)
40 TALLOC_CTX *frame = talloc_stackframe();
41 struct netr_SamInfo3 *info3 = NULL;
42 NTSTATUS nt_status;
43 struct auth_user_info_dc *user_info_dc;
44 struct auth4_context *auth4_context;
45 struct loadparm_context *lp_ctx;
47 lp_ctx = loadparm_init_s3(frame, loadparm_s3_context());
48 if (lp_ctx == NULL) {
49 DEBUG(10, ("loadparm_init_s3 failed\n"));
50 talloc_free(frame);
51 return NT_STATUS_INVALID_SERVER_STATE;
54 /* We create a private tevent context here to avoid nested loops in
55 * the s3 one, as that may not be expected */
56 nt_status = auth_context_create(mem_ctx,
57 s4_event_context_init(frame), NULL,
58 lp_ctx,
59 &auth4_context);
60 NT_STATUS_NOT_OK_RETURN(nt_status);
62 nt_status = auth_context_set_challenge(auth4_context, auth_context->challenge.data, "auth_samba4");
63 NT_STATUS_NOT_OK_RETURN_AND_FREE(nt_status, auth4_context);
65 nt_status = auth_check_password(auth4_context, auth4_context, user_info, &user_info_dc);
66 NT_STATUS_NOT_OK_RETURN_AND_FREE(nt_status, auth4_context);
68 nt_status = auth_convert_user_info_dc_saminfo3(mem_ctx,
69 user_info_dc,
70 &info3);
71 if (NT_STATUS_IS_OK(nt_status)) {
72 /* We need the strings from the server_info to be valid as long as the info3 is around */
73 talloc_steal(info3, user_info_dc);
75 talloc_free(auth4_context);
77 if (!NT_STATUS_IS_OK(nt_status)) {
78 goto done;
81 nt_status = make_server_info_info3(mem_ctx, user_info->client.account_name,
82 user_info->mapped.domain_name, server_info,
83 info3);
84 if (!NT_STATUS_IS_OK(nt_status)) {
85 DEBUG(10, ("make_server_info_info3 failed: %s\n",
86 nt_errstr(nt_status)));
87 TALLOC_FREE(frame);
88 return nt_status;
91 nt_status = NT_STATUS_OK;
93 done:
94 TALLOC_FREE(frame);
95 return nt_status;
98 /* Hook to allow GENSEC to handle blob-based authentication
99 * mechanisms, without directly linking the mechansim code */
100 static NTSTATUS prepare_gensec(TALLOC_CTX *mem_ctx,
101 struct gensec_security **gensec_context)
103 NTSTATUS status;
104 struct loadparm_context *lp_ctx;
105 struct tevent_context *event_ctx;
106 TALLOC_CTX *frame = talloc_stackframe();
107 struct gensec_security *gensec_ctx;
108 struct imessaging_context *msg_ctx;
109 struct cli_credentials *server_credentials;
111 lp_ctx = loadparm_init_s3(frame, loadparm_s3_context());
112 if (lp_ctx == NULL) {
113 DEBUG(1, ("loadparm_init_s3 failed\n"));
114 TALLOC_FREE(frame);
115 return NT_STATUS_INVALID_SERVER_STATE;
117 event_ctx = s4_event_context_init(mem_ctx);
118 if (event_ctx == NULL) {
119 DEBUG(1, ("s4_event_context_init failed\n"));
120 TALLOC_FREE(frame);
121 return NT_STATUS_INVALID_SERVER_STATE;
124 msg_ctx = imessaging_client_init(frame,
125 lp_ctx,
126 event_ctx);
127 if (msg_ctx == NULL) {
128 DEBUG(1, ("imessaging_init failed\n"));
129 TALLOC_FREE(frame);
130 return NT_STATUS_INVALID_SERVER_STATE;
133 server_credentials
134 = cli_credentials_init(frame);
135 if (!server_credentials) {
136 DEBUG(1, ("Failed to init server credentials"));
137 TALLOC_FREE(frame);
138 return NT_STATUS_INVALID_SERVER_STATE;
141 cli_credentials_set_conf(server_credentials, lp_ctx);
142 status = cli_credentials_set_machine_account(server_credentials, lp_ctx);
143 if (!NT_STATUS_IS_OK(status)) {
144 DEBUG(10, ("Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
145 talloc_free(server_credentials);
146 server_credentials = NULL;
149 status = samba_server_gensec_start(mem_ctx,
150 event_ctx, msg_ctx,
151 lp_ctx, server_credentials, "cifs",
152 &gensec_ctx);
153 if (!NT_STATUS_IS_OK(status)) {
154 DEBUG(1, ("Failed to start GENSEC server code: %s\n", nt_errstr(status)));
155 TALLOC_FREE(frame);
156 return status;
159 talloc_reparent(frame, gensec_ctx, msg_ctx);
160 talloc_reparent(frame, gensec_ctx, event_ctx);
161 talloc_reparent(frame, gensec_ctx, lp_ctx);
162 talloc_reparent(frame, gensec_ctx, server_credentials);
164 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_SESSION_KEY);
165 gensec_want_feature(gensec_ctx, GENSEC_FEATURE_UNIX_TOKEN);
167 *gensec_context = gensec_ctx;
168 TALLOC_FREE(frame);
169 return status;
172 /* module initialisation */
173 static NTSTATUS auth_init_samba4(struct auth_context *auth_context,
174 const char *param,
175 auth_methods **auth_method)
177 struct auth_methods *result;
179 gensec_init();
181 result = talloc_zero(auth_context, struct auth_methods);
182 if (result == NULL) {
183 return NT_STATUS_NO_MEMORY;
185 result->name = "samba4";
186 result->auth = check_samba4_security;
187 result->prepare_gensec = prepare_gensec;
188 result->gensec_start_mech_by_oid = gensec_start_mech_by_oid;
189 result->gensec_start_mech_by_authtype = gensec_start_mech_by_authtype;
191 *auth_method = result;
192 return NT_STATUS_OK;
195 NTSTATUS auth_samba4_init(void)
197 smb_register_auth(AUTH_INTERFACE_VERSION, "samba4",
198 auth_init_samba4);
199 return NT_STATUS_OK;