Vector correctly through reply_openerror() (which uses the same logic).
[Samba/fernandojvsilva.git] / source4 / kdc / kpasswdd.c
blob9b3336a7a126bc4aad479d966877151ba76672ff
1 /*
2 Unix SMB/CIFS implementation.
4 kpasswd Server implementation
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
7 Copyright (C) Andrew Tridgell 2005
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/>.
23 #include "includes.h"
24 #include "smbd/service_task.h"
25 #include "lib/events/events.h"
26 #include "lib/socket/socket.h"
27 #include "lib/tsocket/tsocket.h"
28 #include "system/network.h"
29 #include "../lib/util/dlinklist.h"
30 #include "lib/ldb/include/ldb.h"
31 #include "auth/gensec/gensec.h"
32 #include "auth/credentials/credentials.h"
33 #include "auth/credentials/credentials_krb5.h"
34 #include "auth/auth.h"
35 #include "dsdb/samdb/samdb.h"
36 #include "rpc_server/dcerpc_server.h"
37 #include "rpc_server/samr/proto.h"
38 #include "libcli/security/security.h"
39 #include "param/param.h"
40 #include "kdc/kdc.h"
42 /* TODO: remove all SAMBA4_INTERNAL_HEIMDAL stuff from this file */
43 #ifdef SAMBA4_INTERNAL_HEIMDAL
44 #include "heimdal_build/kpasswdd-glue.h"
45 #endif
47 /* Return true if there is a valid error packet formed in the error_blob */
48 static bool kpasswdd_make_error_reply(struct kdc_server *kdc,
49 TALLOC_CTX *mem_ctx,
50 uint16_t result_code,
51 const char *error_string,
52 DATA_BLOB *error_blob)
54 char *error_string_utf8;
55 size_t len;
57 DEBUG(result_code ? 3 : 10, ("kpasswdd: %s\n", error_string));
59 if (!push_utf8_talloc(mem_ctx, &error_string_utf8, error_string, &len)) {
60 return false;
63 *error_blob = data_blob_talloc(mem_ctx, NULL, 2 + len + 1);
64 if (!error_blob->data) {
65 return false;
67 RSSVAL(error_blob->data, 0, result_code);
68 memcpy(error_blob->data + 2, error_string_utf8, len + 1);
69 return true;
72 /* Return true if there is a valid error packet formed in the error_blob */
73 static bool kpasswdd_make_unauth_error_reply(struct kdc_server *kdc,
74 TALLOC_CTX *mem_ctx,
75 uint16_t result_code,
76 const char *error_string,
77 DATA_BLOB *error_blob)
79 bool ret;
80 int kret;
81 DATA_BLOB error_bytes;
82 krb5_data k5_error_bytes, k5_error_blob;
83 ret = kpasswdd_make_error_reply(kdc, mem_ctx, result_code, error_string,
84 &error_bytes);
85 if (!ret) {
86 return false;
88 k5_error_bytes.data = error_bytes.data;
89 k5_error_bytes.length = error_bytes.length;
90 kret = krb5_mk_error(kdc->smb_krb5_context->krb5_context,
91 result_code, NULL, &k5_error_bytes,
92 NULL, NULL, NULL, NULL, &k5_error_blob);
93 if (kret) {
94 return false;
96 *error_blob = data_blob_talloc(mem_ctx, k5_error_blob.data, k5_error_blob.length);
97 krb5_data_free(&k5_error_blob);
98 if (!error_blob->data) {
99 return false;
101 return true;
104 static bool kpasswd_make_pwchange_reply(struct kdc_server *kdc,
105 TALLOC_CTX *mem_ctx,
106 NTSTATUS status,
107 enum samPwdChangeReason reject_reason,
108 struct samr_DomInfo1 *dominfo,
109 DATA_BLOB *error_blob)
111 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
112 return kpasswdd_make_error_reply(kdc, mem_ctx,
113 KRB5_KPASSWD_ACCESSDENIED,
114 "No such user when changing password",
115 error_blob);
117 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
118 return kpasswdd_make_error_reply(kdc, mem_ctx,
119 KRB5_KPASSWD_ACCESSDENIED,
120 "Not permitted to change password",
121 error_blob);
123 if (dominfo && NT_STATUS_EQUAL(status, NT_STATUS_PASSWORD_RESTRICTION)) {
124 const char *reject_string;
125 switch (reject_reason) {
126 case SAM_PWD_CHANGE_PASSWORD_TOO_SHORT:
127 reject_string = talloc_asprintf(mem_ctx, "Password too short, password must be at least %d characters long",
128 dominfo->min_password_length);
129 break;
130 case SAM_PWD_CHANGE_NOT_COMPLEX:
131 reject_string = "Password does not meet complexity requirements";
132 break;
133 case SAM_PWD_CHANGE_PWD_IN_HISTORY:
134 reject_string = "Password is already in password history";
135 break;
136 default:
137 reject_string = talloc_asprintf(mem_ctx, "Password must be at least %d characters long, and cannot match any of your %d previous passwords",
138 dominfo->min_password_length, dominfo->password_history_length);
139 break;
141 return kpasswdd_make_error_reply(kdc, mem_ctx,
142 KRB5_KPASSWD_SOFTERROR,
143 reject_string,
144 error_blob);
146 if (!NT_STATUS_IS_OK(status)) {
147 return kpasswdd_make_error_reply(kdc, mem_ctx,
148 KRB5_KPASSWD_HARDERROR,
149 talloc_asprintf(mem_ctx, "failed to set password: %s", nt_errstr(status)),
150 error_blob);
153 return kpasswdd_make_error_reply(kdc, mem_ctx, KRB5_KPASSWD_SUCCESS,
154 "Password changed",
155 error_blob);
159 A user password change
161 Return true if there is a valid error packet (or sucess) formed in
162 the error_blob
164 static bool kpasswdd_change_password(struct kdc_server *kdc,
165 TALLOC_CTX *mem_ctx,
166 struct auth_session_info *session_info,
167 const DATA_BLOB *password,
168 DATA_BLOB *reply)
170 NTSTATUS status;
171 enum samPwdChangeReason reject_reason;
172 struct samr_DomInfo1 *dominfo;
173 struct ldb_context *samdb;
175 samdb = samdb_connect(mem_ctx, kdc->task->event_ctx, kdc->task->lp_ctx, system_session(kdc->task->lp_ctx));
176 if (!samdb) {
177 return kpasswdd_make_error_reply(kdc, mem_ctx,
178 KRB5_KPASSWD_HARDERROR,
179 "Failed to open samdb",
180 reply);
183 DEBUG(3, ("Changing password of %s\\%s (%s)\n",
184 session_info->server_info->domain_name,
185 session_info->server_info->account_name,
186 dom_sid_string(mem_ctx, session_info->security_token->user_sid)));
188 /* User password change */
189 status = samdb_set_password_sid(samdb, mem_ctx,
190 session_info->security_token->user_sid,
191 password, NULL, NULL,
192 true, /* this is a user password change */
193 &reject_reason,
194 &dominfo);
195 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
196 status,
197 reject_reason,
198 dominfo,
199 reply);
203 static bool kpasswd_process_request(struct kdc_server *kdc,
204 TALLOC_CTX *mem_ctx,
205 struct gensec_security *gensec_security,
206 uint16_t version,
207 DATA_BLOB *input,
208 DATA_BLOB *reply)
210 struct auth_session_info *session_info;
211 size_t pw_len;
213 if (!NT_STATUS_IS_OK(gensec_session_info(gensec_security,
214 &session_info))) {
215 return kpasswdd_make_error_reply(kdc, mem_ctx,
216 KRB5_KPASSWD_HARDERROR,
217 "gensec_session_info failed!",
218 reply);
221 switch (version) {
222 case KRB5_KPASSWD_VERS_CHANGEPW:
224 DATA_BLOB password;
225 if (!convert_string_talloc_convenience(mem_ctx, lp_iconv_convenience(kdc->task->lp_ctx),
226 CH_UTF8, CH_UTF16,
227 (const char *)input->data,
228 input->length,
229 (void **)&password.data, &pw_len, false)) {
230 return false;
232 password.length = pw_len;
234 return kpasswdd_change_password(kdc, mem_ctx, session_info,
235 &password, reply);
236 break;
238 case KRB5_KPASSWD_VERS_SETPW:
240 NTSTATUS status;
241 enum samPwdChangeReason reject_reason = SAM_PWD_CHANGE_NO_ERROR;
242 struct samr_DomInfo1 *dominfo = NULL;
243 struct ldb_context *samdb;
244 struct ldb_message *msg;
245 krb5_context context = kdc->smb_krb5_context->krb5_context;
247 ChangePasswdDataMS chpw;
248 DATA_BLOB password;
250 krb5_principal principal;
251 char *set_password_on_princ;
252 struct ldb_dn *set_password_on_dn;
254 size_t len;
255 int ret;
257 msg = ldb_msg_new(mem_ctx);
258 if (!msg) {
259 return false;
262 ret = decode_ChangePasswdDataMS(input->data, input->length,
263 &chpw, &len);
264 if (ret) {
265 return kpasswdd_make_error_reply(kdc, mem_ctx,
266 KRB5_KPASSWD_MALFORMED,
267 "failed to decode password change structure",
268 reply);
271 if (!convert_string_talloc_convenience(mem_ctx, lp_iconv_convenience(kdc->task->lp_ctx),
272 CH_UTF8, CH_UTF16,
273 (const char *)chpw.newpasswd.data,
274 chpw.newpasswd.length,
275 (void **)&password.data, &pw_len, false)) {
276 free_ChangePasswdDataMS(&chpw);
277 return false;
280 password.length = pw_len;
282 if ((chpw.targname && !chpw.targrealm)
283 || (!chpw.targname && chpw.targrealm)) {
284 return kpasswdd_make_error_reply(kdc, mem_ctx,
285 KRB5_KPASSWD_MALFORMED,
286 "Realm and principal must be both present, or neither present",
287 reply);
289 if (chpw.targname && chpw.targrealm) {
290 #ifdef SAMBA4_INTERNAL_HEIMDAL
291 if (_krb5_principalname2krb5_principal(kdc->smb_krb5_context->krb5_context,
292 &principal, *chpw.targname,
293 *chpw.targrealm) != 0) {
294 free_ChangePasswdDataMS(&chpw);
295 return kpasswdd_make_error_reply(kdc, mem_ctx,
296 KRB5_KPASSWD_MALFORMED,
297 "failed to extract principal to set",
298 reply);
301 #else /* SAMBA4_INTERNAL_HEIMDAL */
302 return kpasswdd_make_error_reply(kdc, mem_ctx,
303 KRB5_KPASSWD_BAD_VERSION,
304 "Operation Not Implemented",
305 reply);
306 #endif /* SAMBA4_INTERNAL_HEIMDAL */
307 } else {
308 free_ChangePasswdDataMS(&chpw);
309 return kpasswdd_change_password(kdc, mem_ctx, session_info,
310 &password, reply);
312 free_ChangePasswdDataMS(&chpw);
314 if (krb5_unparse_name(context, principal, &set_password_on_princ) != 0) {
315 krb5_free_principal(context, principal);
316 return kpasswdd_make_error_reply(kdc, mem_ctx,
317 KRB5_KPASSWD_MALFORMED,
318 "krb5_unparse_name failed!",
319 reply);
322 krb5_free_principal(context, principal);
324 samdb = samdb_connect(mem_ctx, kdc->task->event_ctx, kdc->task->lp_ctx, session_info);
325 if (!samdb) {
326 return kpasswdd_make_error_reply(kdc, mem_ctx,
327 KRB5_KPASSWD_HARDERROR,
328 "Unable to open database!",
329 reply);
332 DEBUG(3, ("%s\\%s (%s) is changing password of %s\n",
333 session_info->server_info->domain_name,
334 session_info->server_info->account_name,
335 dom_sid_string(mem_ctx, session_info->security_token->user_sid),
336 set_password_on_princ));
337 ret = ldb_transaction_start(samdb);
338 if (ret) {
339 status = NT_STATUS_TRANSACTION_ABORTED;
340 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
341 status,
342 SAM_PWD_CHANGE_NO_ERROR,
343 NULL,
344 reply);
347 status = crack_user_principal_name(samdb, mem_ctx,
348 set_password_on_princ,
349 &set_password_on_dn, NULL);
350 free(set_password_on_princ);
351 if (!NT_STATUS_IS_OK(status)) {
352 ldb_transaction_cancel(samdb);
353 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
354 status,
355 SAM_PWD_CHANGE_NO_ERROR,
356 NULL,
357 reply);
360 msg = ldb_msg_new(mem_ctx);
361 if (msg == NULL) {
362 ldb_transaction_cancel(samdb);
363 status = NT_STATUS_NO_MEMORY;
364 } else {
365 msg->dn = ldb_dn_copy(msg, set_password_on_dn);
366 if (!msg->dn) {
367 status = NT_STATUS_NO_MEMORY;
371 if (NT_STATUS_IS_OK(status)) {
372 /* Admin password set */
373 status = samdb_set_password(samdb, mem_ctx,
374 set_password_on_dn, NULL,
375 msg, &password, NULL, NULL,
376 false, /* this is not a user password change */
377 &reject_reason, &dominfo);
380 if (NT_STATUS_IS_OK(status)) {
381 /* modify the samdb record */
382 ret = samdb_replace(samdb, mem_ctx, msg);
383 if (ret != 0) {
384 DEBUG(2,("Failed to modify record to set password on %s: %s\n",
385 ldb_dn_get_linearized(msg->dn),
386 ldb_errstring(samdb)));
387 status = NT_STATUS_ACCESS_DENIED;
390 if (NT_STATUS_IS_OK(status)) {
391 ret = ldb_transaction_commit(samdb);
392 if (ret != 0) {
393 DEBUG(1,("Failed to commit transaction to set password on %s: %s\n",
394 ldb_dn_get_linearized(msg->dn),
395 ldb_errstring(samdb)));
396 status = NT_STATUS_TRANSACTION_ABORTED;
398 } else {
399 ldb_transaction_cancel(samdb);
401 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
402 status,
403 reject_reason,
404 dominfo,
405 reply);
407 default:
408 return kpasswdd_make_error_reply(kdc, mem_ctx,
409 KRB5_KPASSWD_BAD_VERSION,
410 talloc_asprintf(mem_ctx,
411 "Protocol version %u not supported",
412 version),
413 reply);
415 return true;
418 bool kpasswdd_process(struct kdc_server *kdc,
419 TALLOC_CTX *mem_ctx,
420 DATA_BLOB *input,
421 DATA_BLOB *reply,
422 struct tsocket_address *peer_addr,
423 struct tsocket_address *my_addr,
424 int datagram_reply)
426 bool ret;
427 const uint16_t header_len = 6;
428 uint16_t len;
429 uint16_t ap_req_len;
430 uint16_t krb_priv_len;
431 uint16_t version;
432 NTSTATUS nt_status;
433 DATA_BLOB ap_req, krb_priv_req;
434 DATA_BLOB krb_priv_rep = data_blob(NULL, 0);
435 DATA_BLOB ap_rep = data_blob(NULL, 0);
436 DATA_BLOB kpasswd_req, kpasswd_rep;
437 struct cli_credentials *server_credentials;
438 struct gensec_security *gensec_security;
439 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
441 char *keytab_name;
443 if (!tmp_ctx) {
444 return false;
447 /* Be parinoid. We need to ensure we don't just let the
448 * caller lead us into a buffer overflow */
449 if (input->length <= header_len) {
450 talloc_free(tmp_ctx);
451 return false;
454 len = RSVAL(input->data, 0);
455 if (input->length != len) {
456 talloc_free(tmp_ctx);
457 return false;
460 /* There are two different versions of this protocol so far,
461 * plus others in the standards pipe. Fortunetly they all
462 * take a very similar framing */
463 version = RSVAL(input->data, 2);
464 ap_req_len = RSVAL(input->data, 4);
465 if ((ap_req_len >= len) || (ap_req_len + header_len) >= len) {
466 talloc_free(tmp_ctx);
467 return false;
470 krb_priv_len = len - ap_req_len;
471 ap_req = data_blob_const(&input->data[header_len], ap_req_len);
472 krb_priv_req = data_blob_const(&input->data[header_len + ap_req_len], krb_priv_len);
474 server_credentials = cli_credentials_init(tmp_ctx);
475 if (!server_credentials) {
476 DEBUG(1, ("Failed to init server credentials\n"));
477 return false;
480 /* We want the credentials subsystem to use the krb5 context
481 * we already have, rather than a new context */
482 cli_credentials_set_krb5_context(server_credentials, kdc->smb_krb5_context);
483 cli_credentials_set_conf(server_credentials, kdc->task->lp_ctx);
485 keytab_name = talloc_asprintf(server_credentials, "HDB:samba4&%p", kdc->hdb_samba4_context);
487 cli_credentials_set_username(server_credentials, "kadmin/changepw", CRED_SPECIFIED);
488 ret = cli_credentials_set_keytab_name(server_credentials, kdc->task->event_ctx, kdc->task->lp_ctx, keytab_name, CRED_SPECIFIED);
489 if (ret != 0) {
490 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
491 KRB5_KPASSWD_HARDERROR,
492 talloc_asprintf(mem_ctx,
493 "Failed to obtain server credentials for kadmin/changepw: %s\n",
494 nt_errstr(nt_status)),
495 &krb_priv_rep);
496 ap_rep.length = 0;
497 if (ret) {
498 goto reply;
500 talloc_free(tmp_ctx);
501 return ret;
504 /* We don't strictly need to call this wrapper, and could call
505 * gensec_server_start directly, as we have no need for NTLM
506 * and we have a PAC, but this ensures that the wrapper can be
507 * safely extended for other helpful things in future */
508 nt_status = samba_server_gensec_start(tmp_ctx, kdc->task->event_ctx,
509 kdc->task->msg_ctx,
510 kdc->task->lp_ctx,
511 server_credentials,
512 "kpasswd",
513 &gensec_security);
514 if (!NT_STATUS_IS_OK(nt_status)) {
515 talloc_free(tmp_ctx);
516 return false;
519 /* The kerberos PRIV packets include these addresses. MIT
520 * clients check that they are present */
521 #if 0
522 /* Skip this part for now, it breaks with a NetAPP filer and
523 * in any case where the client address is behind NAT. If
524 * older MIT clients need this, we might have to insert more
525 * complex code */
527 nt_status = gensec_set_local_address(gensec_security, peer_addr);
528 if (!NT_STATUS_IS_OK(nt_status)) {
529 talloc_free(tmp_ctx);
530 return false;
532 #endif
534 nt_status = gensec_set_local_address(gensec_security, my_addr);
535 if (!NT_STATUS_IS_OK(nt_status)) {
536 talloc_free(tmp_ctx);
537 return false;
540 /* We want the GENSEC wrap calls to generate PRIV tokens */
541 gensec_want_feature(gensec_security, GENSEC_FEATURE_SEAL);
543 nt_status = gensec_start_mech_by_name(gensec_security, "krb5");
544 if (!NT_STATUS_IS_OK(nt_status)) {
545 talloc_free(tmp_ctx);
546 return false;
549 /* Accept the AP-REQ and generate teh AP-REP we need for the reply */
550 nt_status = gensec_update(gensec_security, tmp_ctx, ap_req, &ap_rep);
551 if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
553 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
554 KRB5_KPASSWD_HARDERROR,
555 talloc_asprintf(mem_ctx,
556 "gensec_update failed: %s",
557 nt_errstr(nt_status)),
558 &krb_priv_rep);
559 ap_rep.length = 0;
560 if (ret) {
561 goto reply;
563 talloc_free(tmp_ctx);
564 return ret;
567 /* Extract the data from the KRB-PRIV half of the message */
568 nt_status = gensec_unwrap(gensec_security, tmp_ctx, &krb_priv_req, &kpasswd_req);
569 if (!NT_STATUS_IS_OK(nt_status)) {
570 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
571 KRB5_KPASSWD_HARDERROR,
572 talloc_asprintf(mem_ctx,
573 "gensec_unwrap failed: %s",
574 nt_errstr(nt_status)),
575 &krb_priv_rep);
576 ap_rep.length = 0;
577 if (ret) {
578 goto reply;
580 talloc_free(tmp_ctx);
581 return ret;
584 /* Figure out something to do with it (probably changing a password...) */
585 ret = kpasswd_process_request(kdc, tmp_ctx,
586 gensec_security,
587 version,
588 &kpasswd_req, &kpasswd_rep);
589 if (!ret) {
590 /* Argh! */
591 return false;
594 /* And wrap up the reply: This ensures that the error message
595 * or success can be verified by the client */
596 nt_status = gensec_wrap(gensec_security, tmp_ctx,
597 &kpasswd_rep, &krb_priv_rep);
598 if (!NT_STATUS_IS_OK(nt_status)) {
599 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
600 KRB5_KPASSWD_HARDERROR,
601 talloc_asprintf(mem_ctx,
602 "gensec_wrap failed: %s",
603 nt_errstr(nt_status)),
604 &krb_priv_rep);
605 ap_rep.length = 0;
606 if (ret) {
607 goto reply;
609 talloc_free(tmp_ctx);
610 return ret;
613 reply:
614 *reply = data_blob_talloc(mem_ctx, NULL, krb_priv_rep.length + ap_rep.length + header_len);
615 if (!reply->data) {
616 return false;
619 RSSVAL(reply->data, 0, reply->length);
620 RSSVAL(reply->data, 2, 1); /* This is a version 1 reply, MS change/set or otherwise */
621 RSSVAL(reply->data, 4, ap_rep.length);
622 memcpy(reply->data + header_len,
623 ap_rep.data,
624 ap_rep.length);
625 memcpy(reply->data + header_len + ap_rep.length,
626 krb_priv_rep.data,
627 krb_priv_rep.length);
629 talloc_free(tmp_ctx);
630 return ret;