2 Unix SMB/CIFS implementation.
4 Samba kpasswd implementation
6 Copyright (c) 2005 Andrew Bartlett <abartlet@samba.org>
7 Copyright (c) 2016 Andreas Schneider <asn@samba.org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "smbd/service_task.h"
25 #include "tsocket/tsocket.h"
26 #include "auth/credentials/credentials.h"
27 #include "auth/auth.h"
28 #include "auth/gensec/gensec.h"
29 #include "kdc/kdc-server.h"
30 #include "kdc/kpasswd-service.h"
31 #include "kdc/kpasswd-helper.h"
34 #ifndef RFC3244_VERSION
35 #define RFC3244_VERSION 0xff80
38 kdc_code kpasswd_process(struct kdc_server *kdc,
42 struct tsocket_address *remote_addr,
43 struct tsocket_address *local_addr,
49 uint16_t enc_data_len;
50 DATA_BLOB ap_req_blob = data_blob_null;
51 DATA_BLOB ap_rep_blob = data_blob_null;
52 DATA_BLOB enc_data_blob = data_blob_null;
53 DATA_BLOB dec_data_blob = data_blob_null;
54 DATA_BLOB kpasswd_dec_reply = data_blob_null;
55 const char *error_string = NULL;
56 krb5_error_code error_code = 0;
57 struct cli_credentials *server_credentials;
58 struct gensec_security *gensec_security;
59 #ifndef SAMBA4_USES_HEIMDAL
60 struct sockaddr_storage remote_ss;
62 struct sockaddr_storage local_ss;
65 kdc_code rc = KDC_ERROR;
66 krb5_error_code code = 0;
73 return KDC_PROXY_REQUEST;
76 tmp_ctx = talloc_new(mem_ctx);
77 if (tmp_ctx == NULL) {
81 is_inet = tsocket_address_is_inet(remote_addr, "ip");
83 DBG_WARNING("Invalid remote IP address");
87 #ifndef SAMBA4_USES_HEIMDAL
89 * FIXME: Heimdal fails to to do a krb5_rd_req() in gensec_krb5 if we
90 * set the remote address.
94 socklen = tsocket_address_bsd_sockaddr(remote_addr,
95 (struct sockaddr *)&remote_ss,
96 sizeof(struct sockaddr_storage));
98 DBG_WARNING("Invalid remote IP address");
104 socklen = tsocket_address_bsd_sockaddr(local_addr,
105 (struct sockaddr *)&local_ss,
106 sizeof(struct sockaddr_storage));
108 DBG_WARNING("Invalid local IP address");
112 if (request->length <= HEADER_LEN) {
113 DBG_WARNING("Request truncated\n");
117 len = RSVAL(request->data, 0);
118 if (request->length != len) {
119 DBG_WARNING("Request length does not match\n");
123 verno = RSVAL(request->data, 2);
124 if (verno != 1 && verno != RFC3244_VERSION) {
125 DBG_WARNING("Unsupported version: 0x%04x\n", verno);
128 ap_req_len = RSVAL(request->data, 4);
129 if ((ap_req_len >= len) || ((ap_req_len + HEADER_LEN) >= len)) {
130 DBG_WARNING("AP_REQ truncated\n");
134 ap_req_blob = data_blob_const(&request->data[HEADER_LEN], ap_req_len);
136 enc_data_len = len - ap_req_len;
137 enc_data_blob = data_blob_const(&request->data[HEADER_LEN + ap_req_len],
140 server_credentials = cli_credentials_init(tmp_ctx);
141 if (server_credentials == NULL) {
142 DBG_ERR("Failed to initialize server credentials!\n");
147 * We want the credentials subsystem to use the krb5 context we already
148 * have, rather than a new context.
150 * On this context the KDB plugin has been loaded, so we can access
153 status = cli_credentials_set_krb5_context(server_credentials,
154 kdc->smb_krb5_context);
155 if (!NT_STATUS_IS_OK(status)) {
159 cli_credentials_set_conf(server_credentials, kdc->task->lp_ctx);
161 ok = cli_credentials_set_username(server_credentials,
168 rv = cli_credentials_set_keytab_name(server_credentials,
173 DBG_ERR("Failed to set credentials keytab name\n");
177 status = samba_server_gensec_start(tmp_ctx,
178 kdc->task->event_ctx,
184 if (!NT_STATUS_IS_OK(status)) {
188 status = gensec_set_local_address(gensec_security, local_addr);
189 if (!NT_STATUS_IS_OK(status)) {
193 #ifndef SAMBA4_USES_HEIMDAL
194 status = gensec_set_remote_address(gensec_security, remote_addr);
195 if (!NT_STATUS_IS_OK(status)) {
200 /* We want the GENSEC wrap calls to generate PRIV tokens */
201 gensec_want_feature(gensec_security, GENSEC_FEATURE_SEAL);
203 /* Use the krb5 gesec mechanism so we can load DB modules */
204 status = gensec_start_mech_by_name(gensec_security, "krb5");
205 if (!NT_STATUS_IS_OK(status)) {
210 * Accept the AP-REQ and generate the AP-REP we need for the reply
212 * We only allow KRB5 and make sure the backend to is RPC/IPC free.
214 * See gensec_krb5_update_internal() as GENSEC_SERVER.
216 * It allows gensec_update() not to block.
218 * If that changes in future we need to use
219 * gensec_update_send/recv here!
221 status = gensec_update(gensec_security, tmp_ctx,
222 ap_req_blob, &ap_rep_blob);
223 if (!NT_STATUS_IS_OK(status) &&
224 !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
225 ap_rep_blob = data_blob_null;
226 error_code = KRB5_KPASSWD_HARDERROR;
227 error_string = talloc_asprintf(tmp_ctx,
228 "gensec_update failed - %s\n",
230 DBG_ERR("%s", error_string);
234 status = gensec_unwrap(gensec_security,
238 if (!NT_STATUS_IS_OK(status)) {
239 ap_rep_blob = data_blob_null;
240 error_code = KRB5_KPASSWD_HARDERROR;
241 error_string = talloc_asprintf(tmp_ctx,
242 "gensec_unwrap failed - %s\n",
244 DBG_ERR("%s", error_string);
248 code = kpasswd_handle_request(kdc,
260 status = gensec_wrap(gensec_security,
264 if (!NT_STATUS_IS_OK(status)) {
265 error_code = KRB5_KPASSWD_HARDERROR;
266 error_string = talloc_asprintf(tmp_ctx,
267 "gensec_wrap failed - %s\n",
269 DBG_ERR("%s", error_string);
274 if (error_code != 0) {
275 krb5_data k_enc_data;
276 krb5_data k_dec_data;
277 const char *principal_string;
278 krb5_principal server_principal;
280 if (error_string == NULL) {
281 DBG_ERR("Invalid error string! This should not happen\n");
285 ok = kpasswd_make_error_reply(tmp_ctx,
290 DBG_ERR("Failed to create error reply\n");
294 k_dec_data.length = dec_data_blob.length;
295 k_dec_data.data = (char *)dec_data_blob.data;
297 principal_string = cli_credentials_get_principal(server_credentials,
299 if (principal_string == NULL) {
303 code = smb_krb5_parse_name(kdc->smb_krb5_context->krb5_context,
307 DBG_ERR("Failed to create principal: %s\n",
308 error_message(code));
312 code = smb_krb5_mk_error(kdc->smb_krb5_context->krb5_context,
319 krb5_free_principal(kdc->smb_krb5_context->krb5_context,
322 DBG_ERR("Failed to create krb5 error reply: %s\n",
323 error_message(code));
327 enc_data_blob = data_blob_talloc(tmp_ctx,
330 if (enc_data_blob.data == NULL) {
331 DBG_ERR("Failed to allocate memory for error reply\n");
336 *reply = data_blob_talloc(mem_ctx,
338 HEADER_LEN + ap_rep_blob.length + enc_data_blob.length);
339 if (reply->data == NULL) {
342 RSSVAL(reply->data, 0, reply->length);
343 RSSVAL(reply->data, 2, 1);
344 RSSVAL(reply->data, 4, ap_rep_blob.length);
345 memcpy(reply->data + HEADER_LEN,
348 memcpy(reply->data + HEADER_LEN + ap_rep_blob.length,
350 enc_data_blob.length);
354 talloc_free(tmp_ctx);