smbprinting: fix wrong == in shell tests
[Samba/gebeck_regimport.git] / source4 / kdc / kpasswdd.c
blob7d7e98bd960e5f08893d08faf80b3f90e7eb3040
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 "../lib/util/util_ldb.h"
37 #include "rpc_server/dcerpc_server.h"
38 #include "rpc_server/samr/proto.h"
39 #include "libcli/security/security.h"
40 #include "param/param.h"
41 #include "kdc/kdc-glue.h"
43 /* Return true if there is a valid error packet formed in the error_blob */
44 static bool kpasswdd_make_error_reply(struct kdc_server *kdc,
45 TALLOC_CTX *mem_ctx,
46 uint16_t result_code,
47 const char *error_string,
48 DATA_BLOB *error_blob)
50 char *error_string_utf8;
51 size_t len;
53 DEBUG(result_code ? 3 : 10, ("kpasswdd: %s\n", error_string));
55 if (!push_utf8_talloc(mem_ctx, &error_string_utf8, error_string, &len)) {
56 return false;
59 *error_blob = data_blob_talloc(mem_ctx, NULL, 2 + len + 1);
60 if (!error_blob->data) {
61 return false;
63 RSSVAL(error_blob->data, 0, result_code);
64 memcpy(error_blob->data + 2, error_string_utf8, len + 1);
65 return true;
68 /* Return true if there is a valid error packet formed in the error_blob */
69 static bool kpasswdd_make_unauth_error_reply(struct kdc_server *kdc,
70 TALLOC_CTX *mem_ctx,
71 uint16_t result_code,
72 const char *error_string,
73 DATA_BLOB *error_blob)
75 bool ret;
76 int kret;
77 DATA_BLOB error_bytes;
78 krb5_data k5_error_bytes, k5_error_blob;
79 ret = kpasswdd_make_error_reply(kdc, mem_ctx, result_code, error_string,
80 &error_bytes);
81 if (!ret) {
82 return false;
84 k5_error_bytes.data = error_bytes.data;
85 k5_error_bytes.length = error_bytes.length;
86 kret = krb5_mk_error(kdc->smb_krb5_context->krb5_context,
87 result_code, NULL, &k5_error_bytes,
88 NULL, NULL, NULL, NULL, &k5_error_blob);
89 if (kret) {
90 return false;
92 *error_blob = data_blob_talloc(mem_ctx, k5_error_blob.data, k5_error_blob.length);
93 krb5_data_free(&k5_error_blob);
94 if (!error_blob->data) {
95 return false;
97 return true;
100 static bool kpasswd_make_pwchange_reply(struct kdc_server *kdc,
101 TALLOC_CTX *mem_ctx,
102 NTSTATUS status,
103 enum samPwdChangeReason reject_reason,
104 struct samr_DomInfo1 *dominfo,
105 DATA_BLOB *error_blob)
107 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
108 return kpasswdd_make_error_reply(kdc, mem_ctx,
109 KRB5_KPASSWD_ACCESSDENIED,
110 "No such user when changing password",
111 error_blob);
113 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
114 return kpasswdd_make_error_reply(kdc, mem_ctx,
115 KRB5_KPASSWD_ACCESSDENIED,
116 "Not permitted to change password",
117 error_blob);
119 if (dominfo && NT_STATUS_EQUAL(status, NT_STATUS_PASSWORD_RESTRICTION)) {
120 const char *reject_string;
121 switch (reject_reason) {
122 case SAM_PWD_CHANGE_PASSWORD_TOO_SHORT:
123 reject_string = talloc_asprintf(mem_ctx, "Password too short, password must be at least %d characters long",
124 dominfo->min_password_length);
125 break;
126 case SAM_PWD_CHANGE_NOT_COMPLEX:
127 reject_string = "Password does not meet complexity requirements";
128 break;
129 case SAM_PWD_CHANGE_PWD_IN_HISTORY:
130 reject_string = "Password is already in password history";
131 break;
132 default:
133 reject_string = talloc_asprintf(mem_ctx, "Password must be at least %d characters long, and cannot match any of your %d previous passwords",
134 dominfo->min_password_length, dominfo->password_history_length);
135 break;
137 return kpasswdd_make_error_reply(kdc, mem_ctx,
138 KRB5_KPASSWD_SOFTERROR,
139 reject_string,
140 error_blob);
142 if (!NT_STATUS_IS_OK(status)) {
143 return kpasswdd_make_error_reply(kdc, mem_ctx,
144 KRB5_KPASSWD_HARDERROR,
145 talloc_asprintf(mem_ctx, "failed to set password: %s", nt_errstr(status)),
146 error_blob);
149 return kpasswdd_make_error_reply(kdc, mem_ctx, KRB5_KPASSWD_SUCCESS,
150 "Password changed",
151 error_blob);
155 A user password change
157 Return true if there is a valid error packet (or success) formed in
158 the error_blob
160 static bool kpasswdd_change_password(struct kdc_server *kdc,
161 TALLOC_CTX *mem_ctx,
162 struct auth_session_info *session_info,
163 const DATA_BLOB *password,
164 DATA_BLOB *reply)
166 NTSTATUS status;
167 enum samPwdChangeReason reject_reason;
168 struct samr_DomInfo1 *dominfo;
169 struct samr_Password *oldLmHash, *oldNtHash;
170 struct ldb_context *samdb;
171 const char * const attrs[] = { "dBCSPwd", "unicodePwd", NULL };
172 struct ldb_message **res;
173 int ret;
175 /* Fetch the old hashes to get the old password in order to perform
176 * the password change operation. Naturally it would be much better to
177 * have a password hash from an authentication around but this doesn't
178 * seem to be the case here. */
179 ret = gendb_search(kdc->samdb, mem_ctx, NULL, &res, attrs,
180 "(&(objectClass=user)(sAMAccountName=%s))",
181 session_info->server_info->account_name);
182 if (ret != 1) {
183 return kpasswdd_make_error_reply(kdc, mem_ctx,
184 KRB5_KPASSWD_ACCESSDENIED,
185 "No such user when changing password",
186 reply);
189 status = samdb_result_passwords(mem_ctx, kdc->task->lp_ctx, res[0],
190 &oldLmHash, &oldNtHash);
191 if (!NT_STATUS_IS_OK(status)) {
192 return kpasswdd_make_error_reply(kdc, mem_ctx,
193 KRB5_KPASSWD_ACCESSDENIED,
194 "Not permitted to change password",
195 reply);
198 /* Start a SAM with user privileges for the password change */
199 samdb = samdb_connect(mem_ctx, kdc->task->event_ctx, kdc->task->lp_ctx,
200 session_info, 0);
201 if (!samdb) {
202 return kpasswdd_make_error_reply(kdc, mem_ctx,
203 KRB5_KPASSWD_HARDERROR,
204 "Failed to open samdb",
205 reply);
208 DEBUG(3, ("Changing password of %s\\%s (%s)\n",
209 session_info->server_info->domain_name,
210 session_info->server_info->account_name,
211 dom_sid_string(mem_ctx, &session_info->security_token->sids[PRIMARY_USER_SID_INDEX])));
213 /* Performs the password change */
214 status = samdb_set_password_sid(samdb, mem_ctx,
215 &session_info->security_token->sids[PRIMARY_USER_SID_INDEX],
216 password, NULL, NULL,
217 oldLmHash, oldNtHash, /* this is a user password change */
218 &reject_reason,
219 &dominfo);
220 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
221 status,
222 reject_reason,
223 dominfo,
224 reply);
228 static bool kpasswd_process_request(struct kdc_server *kdc,
229 TALLOC_CTX *mem_ctx,
230 struct gensec_security *gensec_security,
231 uint16_t version,
232 DATA_BLOB *input,
233 DATA_BLOB *reply)
235 struct auth_session_info *session_info;
236 size_t pw_len;
238 if (!NT_STATUS_IS_OK(gensec_session_info(gensec_security,
239 &session_info))) {
240 return kpasswdd_make_error_reply(kdc, mem_ctx,
241 KRB5_KPASSWD_HARDERROR,
242 "gensec_session_info failed!",
243 reply);
246 switch (version) {
247 case KRB5_KPASSWD_VERS_CHANGEPW:
249 DATA_BLOB password;
250 if (!convert_string_talloc_convenience(mem_ctx, lpcfg_iconv_convenience(kdc->task->lp_ctx),
251 CH_UTF8, CH_UTF16,
252 (const char *)input->data,
253 input->length,
254 (void **)&password.data, &pw_len, false)) {
255 return false;
257 password.length = pw_len;
259 return kpasswdd_change_password(kdc, mem_ctx, session_info,
260 &password, reply);
262 case KRB5_KPASSWD_VERS_SETPW:
264 NTSTATUS status;
265 enum samPwdChangeReason reject_reason = SAM_PWD_CHANGE_NO_ERROR;
266 struct samr_DomInfo1 *dominfo = NULL;
267 struct ldb_context *samdb;
268 krb5_context context = kdc->smb_krb5_context->krb5_context;
270 ChangePasswdDataMS chpw;
271 DATA_BLOB password;
273 krb5_principal principal;
274 char *set_password_on_princ;
275 struct ldb_dn *set_password_on_dn;
276 bool service_principal_name = false;
278 size_t len;
279 int ret;
281 ret = decode_ChangePasswdDataMS(input->data, input->length,
282 &chpw, &len);
283 if (ret) {
284 return kpasswdd_make_error_reply(kdc, mem_ctx,
285 KRB5_KPASSWD_MALFORMED,
286 "failed to decode password change structure",
287 reply);
290 if (!convert_string_talloc_convenience(mem_ctx, lpcfg_iconv_convenience(kdc->task->lp_ctx),
291 CH_UTF8, CH_UTF16,
292 (const char *)chpw.newpasswd.data,
293 chpw.newpasswd.length,
294 (void **)&password.data, &pw_len, false)) {
295 free_ChangePasswdDataMS(&chpw);
296 return false;
299 password.length = pw_len;
301 if ((chpw.targname && !chpw.targrealm)
302 || (!chpw.targname && chpw.targrealm)) {
303 return kpasswdd_make_error_reply(kdc, mem_ctx,
304 KRB5_KPASSWD_MALFORMED,
305 "Realm and principal must be both present, or neither present",
306 reply);
308 if (chpw.targname && chpw.targrealm) {
309 krb5_build_principal_ext(kdc->smb_krb5_context->krb5_context,
310 &principal, strlen(*chpw.targrealm), *chpw.targrealm, 0);
311 if (copy_PrincipalName(chpw.targname, &principal->name)) {
312 free_ChangePasswdDataMS(&chpw);
313 return kpasswdd_make_error_reply(kdc, mem_ctx,
314 KRB5_KPASSWD_MALFORMED,
315 "failed to extract principal to set",
316 reply);
318 } else {
319 free_ChangePasswdDataMS(&chpw);
320 return kpasswdd_change_password(kdc, mem_ctx, session_info,
321 &password, reply);
323 free_ChangePasswdDataMS(&chpw);
325 if (principal->name.name_string.len >= 2) {
326 service_principal_name = true;
328 /* We use this, rather than 'no realm' flag,
329 * as we don't want to accept a password
330 * change on a principal from another realm */
332 if (krb5_unparse_name_short(context, principal, &set_password_on_princ) != 0) {
333 krb5_free_principal(context, principal);
334 return kpasswdd_make_error_reply(kdc, mem_ctx,
335 KRB5_KPASSWD_MALFORMED,
336 "krb5_unparse_name failed!",
337 reply);
339 } else {
340 if (krb5_unparse_name(context, principal, &set_password_on_princ) != 0) {
341 krb5_free_principal(context, principal);
342 return kpasswdd_make_error_reply(kdc, mem_ctx,
343 KRB5_KPASSWD_MALFORMED,
344 "krb5_unparse_name failed!",
345 reply);
348 krb5_free_principal(context, principal);
350 samdb = samdb_connect(mem_ctx, kdc->task->event_ctx, kdc->task->lp_ctx, session_info, 0);
351 if (!samdb) {
352 return kpasswdd_make_error_reply(kdc, mem_ctx,
353 KRB5_KPASSWD_HARDERROR,
354 "Unable to open database!",
355 reply);
358 DEBUG(3, ("%s\\%s (%s) is changing password of %s\n",
359 session_info->server_info->domain_name,
360 session_info->server_info->account_name,
361 dom_sid_string(mem_ctx, &session_info->security_token->sids[PRIMARY_USER_SID_INDEX]),
362 set_password_on_princ));
363 ret = ldb_transaction_start(samdb);
364 if (ret != LDB_SUCCESS) {
365 status = NT_STATUS_TRANSACTION_ABORTED;
366 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
367 status,
368 SAM_PWD_CHANGE_NO_ERROR,
369 NULL,
370 reply);
373 if (service_principal_name) {
374 status = crack_service_principal_name(samdb, mem_ctx,
375 set_password_on_princ,
376 &set_password_on_dn, NULL);
377 } else {
378 status = crack_user_principal_name(samdb, mem_ctx,
379 set_password_on_princ,
380 &set_password_on_dn, NULL);
382 free(set_password_on_princ);
383 if (!NT_STATUS_IS_OK(status)) {
384 ldb_transaction_cancel(samdb);
385 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
386 status,
387 SAM_PWD_CHANGE_NO_ERROR,
388 NULL,
389 reply);
392 if (NT_STATUS_IS_OK(status)) {
393 /* Admin password set */
394 status = samdb_set_password(samdb, mem_ctx,
395 set_password_on_dn, NULL,
396 &password, NULL, NULL,
397 NULL, NULL, /* this is not a user password change */
398 &reject_reason, &dominfo);
401 if (NT_STATUS_IS_OK(status)) {
402 ret = ldb_transaction_commit(samdb);
403 if (ret != LDB_SUCCESS) {
404 DEBUG(1,("Failed to commit transaction to set password on %s: %s\n",
405 ldb_dn_get_linearized(set_password_on_dn),
406 ldb_errstring(samdb)));
407 status = NT_STATUS_TRANSACTION_ABORTED;
409 } else {
410 ldb_transaction_cancel(samdb);
412 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
413 status,
414 reject_reason,
415 dominfo,
416 reply);
418 default:
419 return kpasswdd_make_error_reply(kdc, mem_ctx,
420 KRB5_KPASSWD_BAD_VERSION,
421 talloc_asprintf(mem_ctx,
422 "Protocol version %u not supported",
423 version),
424 reply);
428 enum kdc_process_ret kpasswdd_process(struct kdc_server *kdc,
429 TALLOC_CTX *mem_ctx,
430 DATA_BLOB *input,
431 DATA_BLOB *reply,
432 struct tsocket_address *peer_addr,
433 struct tsocket_address *my_addr,
434 int datagram_reply)
436 bool ret;
437 const uint16_t header_len = 6;
438 uint16_t len;
439 uint16_t ap_req_len;
440 uint16_t krb_priv_len;
441 uint16_t version;
442 NTSTATUS nt_status;
443 DATA_BLOB ap_req, krb_priv_req;
444 DATA_BLOB krb_priv_rep = data_blob(NULL, 0);
445 DATA_BLOB ap_rep = data_blob(NULL, 0);
446 DATA_BLOB kpasswd_req, kpasswd_rep;
447 struct cli_credentials *server_credentials;
448 struct gensec_security *gensec_security;
449 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
451 char *keytab_name;
453 if (!tmp_ctx) {
454 return KDC_PROCESS_FAILED;
457 if (kdc->am_rodc) {
458 talloc_free(tmp_ctx);
459 return KDC_PROCESS_PROXY;
462 /* Be parinoid. We need to ensure we don't just let the
463 * caller lead us into a buffer overflow */
464 if (input->length <= header_len) {
465 talloc_free(tmp_ctx);
466 return KDC_PROCESS_FAILED;
469 len = RSVAL(input->data, 0);
470 if (input->length != len) {
471 talloc_free(tmp_ctx);
472 return KDC_PROCESS_FAILED;
475 /* There are two different versions of this protocol so far,
476 * plus others in the standards pipe. Fortunetly they all
477 * take a very similar framing */
478 version = RSVAL(input->data, 2);
479 ap_req_len = RSVAL(input->data, 4);
480 if ((ap_req_len >= len) || (ap_req_len + header_len) >= len) {
481 talloc_free(tmp_ctx);
482 return KDC_PROCESS_FAILED;
485 krb_priv_len = len - ap_req_len;
486 ap_req = data_blob_const(&input->data[header_len], ap_req_len);
487 krb_priv_req = data_blob_const(&input->data[header_len + ap_req_len], krb_priv_len);
489 server_credentials = cli_credentials_init(tmp_ctx);
490 if (!server_credentials) {
491 DEBUG(1, ("Failed to init server credentials\n"));
492 talloc_free(tmp_ctx);
493 return KDC_PROCESS_FAILED;
496 /* We want the credentials subsystem to use the krb5 context
497 * we already have, rather than a new context */
498 cli_credentials_set_krb5_context(server_credentials, kdc->smb_krb5_context);
499 cli_credentials_set_conf(server_credentials, kdc->task->lp_ctx);
501 keytab_name = talloc_asprintf(server_credentials, "HDB:samba4&%p", kdc->base_ctx);
503 cli_credentials_set_username(server_credentials, "kadmin/changepw", CRED_SPECIFIED);
504 ret = cli_credentials_set_keytab_name(server_credentials, kdc->task->lp_ctx, keytab_name, CRED_SPECIFIED);
505 if (ret != 0) {
506 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
507 KRB5_KPASSWD_HARDERROR,
508 talloc_asprintf(mem_ctx,
509 "Failed to obtain server credentials for kadmin/changepw: %s\n",
510 nt_errstr(nt_status)),
511 &krb_priv_rep);
512 ap_rep.length = 0;
513 if (ret) {
514 goto reply;
516 talloc_free(tmp_ctx);
517 return ret;
520 /* We don't strictly need to call this wrapper, and could call
521 * gensec_server_start directly, as we have no need for NTLM
522 * and we have a PAC, but this ensures that the wrapper can be
523 * safely extended for other helpful things in future */
524 nt_status = samba_server_gensec_start(tmp_ctx, kdc->task->event_ctx,
525 kdc->task->msg_ctx,
526 kdc->task->lp_ctx,
527 server_credentials,
528 "kpasswd",
529 &gensec_security);
530 if (!NT_STATUS_IS_OK(nt_status)) {
531 talloc_free(tmp_ctx);
532 return KDC_PROCESS_FAILED;
535 /* The kerberos PRIV packets include these addresses. MIT
536 * clients check that they are present */
537 #if 0
538 /* Skip this part for now, it breaks with a NetAPP filer and
539 * in any case where the client address is behind NAT. If
540 * older MIT clients need this, we might have to insert more
541 * complex code */
543 nt_status = gensec_set_local_address(gensec_security, peer_addr);
544 if (!NT_STATUS_IS_OK(nt_status)) {
545 talloc_free(tmp_ctx);
546 return KDC_PROCESS_FAILED;
548 #endif
550 nt_status = gensec_set_local_address(gensec_security, my_addr);
551 if (!NT_STATUS_IS_OK(nt_status)) {
552 talloc_free(tmp_ctx);
553 return KDC_PROCESS_FAILED;
556 /* We want the GENSEC wrap calls to generate PRIV tokens */
557 gensec_want_feature(gensec_security, GENSEC_FEATURE_SEAL);
559 nt_status = gensec_start_mech_by_name(gensec_security, "krb5");
560 if (!NT_STATUS_IS_OK(nt_status)) {
561 talloc_free(tmp_ctx);
562 return KDC_PROCESS_FAILED;
565 /* Accept the AP-REQ and generate teh AP-REP we need for the reply */
566 nt_status = gensec_update(gensec_security, tmp_ctx, ap_req, &ap_rep);
567 if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
569 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
570 KRB5_KPASSWD_HARDERROR,
571 talloc_asprintf(mem_ctx,
572 "gensec_update failed: %s",
573 nt_errstr(nt_status)),
574 &krb_priv_rep);
575 ap_rep.length = 0;
576 if (ret) {
577 goto reply;
579 talloc_free(tmp_ctx);
580 return KDC_PROCESS_FAILED;
583 /* Extract the data from the KRB-PRIV half of the message */
584 nt_status = gensec_unwrap(gensec_security, tmp_ctx, &krb_priv_req, &kpasswd_req);
585 if (!NT_STATUS_IS_OK(nt_status)) {
586 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
587 KRB5_KPASSWD_HARDERROR,
588 talloc_asprintf(mem_ctx,
589 "gensec_unwrap failed: %s",
590 nt_errstr(nt_status)),
591 &krb_priv_rep);
592 ap_rep.length = 0;
593 if (ret) {
594 goto reply;
596 talloc_free(tmp_ctx);
597 return KDC_PROCESS_FAILED;
600 /* Figure out something to do with it (probably changing a password...) */
601 ret = kpasswd_process_request(kdc, tmp_ctx,
602 gensec_security,
603 version,
604 &kpasswd_req, &kpasswd_rep);
605 if (!ret) {
606 /* Argh! */
607 talloc_free(tmp_ctx);
608 return KDC_PROCESS_FAILED;
611 /* And wrap up the reply: This ensures that the error message
612 * or success can be verified by the client */
613 nt_status = gensec_wrap(gensec_security, tmp_ctx,
614 &kpasswd_rep, &krb_priv_rep);
615 if (!NT_STATUS_IS_OK(nt_status)) {
616 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
617 KRB5_KPASSWD_HARDERROR,
618 talloc_asprintf(mem_ctx,
619 "gensec_wrap failed: %s",
620 nt_errstr(nt_status)),
621 &krb_priv_rep);
622 ap_rep.length = 0;
623 if (ret) {
624 goto reply;
626 talloc_free(tmp_ctx);
627 return KDC_PROCESS_FAILED;
630 reply:
631 *reply = data_blob_talloc(mem_ctx, NULL, krb_priv_rep.length + ap_rep.length + header_len);
632 if (!reply->data) {
633 talloc_free(tmp_ctx);
634 return KDC_PROCESS_FAILED;
637 RSSVAL(reply->data, 0, reply->length);
638 RSSVAL(reply->data, 2, 1); /* This is a version 1 reply, MS change/set or otherwise */
639 RSSVAL(reply->data, 4, ap_rep.length);
640 memcpy(reply->data + header_len,
641 ap_rep.data,
642 ap_rep.length);
643 memcpy(reply->data + header_len + ap_rep.length,
644 krb_priv_rep.data,
645 krb_priv_rep.length);
647 talloc_free(tmp_ctx);
648 return KDC_PROCESS_OK;