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
)) {
209 /* Accept the AP-REQ and generate the AP-REP we need for the reply */
210 status
= gensec_update_ev(gensec_security
,
212 kdc
->task
->event_ctx
,
215 if (!NT_STATUS_IS_OK(status
) &&
216 !NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
217 ap_rep_blob
= data_blob_null
;
218 error_code
= KRB5_KPASSWD_HARDERROR
;
219 error_string
= talloc_asprintf(tmp_ctx
,
220 "gensec_update failed - %s\n",
222 DBG_ERR("%s", error_string
);
226 status
= gensec_unwrap(gensec_security
,
230 if (!NT_STATUS_IS_OK(status
)) {
231 ap_rep_blob
= data_blob_null
;
232 error_code
= KRB5_KPASSWD_HARDERROR
;
233 error_string
= talloc_asprintf(tmp_ctx
,
234 "gensec_unwrap failed - %s\n",
236 DBG_ERR("%s", error_string
);
240 code
= kpasswd_handle_request(kdc
,
252 status
= gensec_wrap(gensec_security
,
256 if (!NT_STATUS_IS_OK(status
)) {
257 error_code
= KRB5_KPASSWD_HARDERROR
;
258 error_string
= talloc_asprintf(tmp_ctx
,
259 "gensec_wrap failed - %s\n",
261 DBG_ERR("%s", error_string
);
266 if (error_code
!= 0) {
267 krb5_data k_enc_data
;
268 krb5_data k_dec_data
;
269 const char *principal_string
;
270 krb5_principal server_principal
;
272 if (error_string
== NULL
) {
273 DBG_ERR("Invalid error string! This should not happen\n");
277 ok
= kpasswd_make_error_reply(tmp_ctx
,
282 DBG_ERR("Failed to create error reply\n");
286 k_dec_data
.length
= dec_data_blob
.length
;
287 k_dec_data
.data
= (char *)dec_data_blob
.data
;
289 principal_string
= cli_credentials_get_principal(server_credentials
,
291 if (principal_string
== NULL
) {
295 code
= smb_krb5_parse_name(kdc
->smb_krb5_context
->krb5_context
,
299 DBG_ERR("Failed to create principal: %s\n",
300 error_message(code
));
304 code
= smb_krb5_mk_error(kdc
->smb_krb5_context
->krb5_context
,
311 krb5_free_principal(kdc
->smb_krb5_context
->krb5_context
,
314 DBG_ERR("Failed to create krb5 error reply: %s\n",
315 error_message(code
));
319 enc_data_blob
= data_blob_talloc(tmp_ctx
,
322 if (enc_data_blob
.data
== NULL
) {
323 DBG_ERR("Failed to allocate memory for error reply\n");
328 *reply
= data_blob_talloc(mem_ctx
,
330 HEADER_LEN
+ ap_rep_blob
.length
+ enc_data_blob
.length
);
331 if (reply
->data
== NULL
) {
334 RSSVAL(reply
->data
, 0, reply
->length
);
335 RSSVAL(reply
->data
, 2, 1);
336 RSSVAL(reply
->data
, 4, ap_rep_blob
.length
);
337 memcpy(reply
->data
+ HEADER_LEN
,
340 memcpy(reply
->data
+ HEADER_LEN
+ ap_rep_blob
.length
,
342 enc_data_blob
.length
);
346 talloc_free(tmp_ctx
);