2 Unix SMB/CIFS implementation.
3 krb5 set password implementation
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Remus Koos 2001 (remuskoos@yahoo.com)
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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define DEFAULT_KPASSWD_PORT 464
27 #define KRB5_KPASSWD_VERS_CHANGEPW 1
28 #define KRB5_KPASSWD_VERS_SETPW 0xff80
29 #define KRB5_KPASSWD_VERS_SETPW_ALT 2
30 #define KRB5_KPASSWD_ACCESSDENIED 5
31 #define KRB5_KPASSWD_BAD_VERSION 6
32 #define KRB5_KPASSWD_INITIAL_FLAG_NEEDED 7
34 /* Those are defined by kerberos-set-passwd-02.txt and are probably
35 * not supported by M$ implementation */
36 #define KRB5_KPASSWD_POLICY_REJECT 8
37 #define KRB5_KPASSWD_BAD_PRINCIPAL 9
38 #define KRB5_KPASSWD_ETYPE_NOSUPP 10
40 /* This implements kerberos password change protocol as specified in
41 * kerb-chg-password-02.txt and kerberos-set-passwd-02.txt
42 * as well as microsoft version of the protocol
43 * as specified in kerberos-set-passwd-00.txt
45 static DATA_BLOB encode_krb5_setpw(const char *principal, const char *password)
47 char* princ_part1 = NULL;
48 char* princ_part2 = NULL;
57 princ = SMB_STRDUP(principal);
59 if ((c = strchr_m(princ, '/')) == NULL) {
69 if ((c = strchr_m(c, '@')) != NULL) {
75 memset(&req, 0, sizeof(req));
77 asn1_push_tag(&req, ASN1_SEQUENCE(0));
78 asn1_push_tag(&req, ASN1_CONTEXT(0));
79 asn1_write_OctetString(&req, password, strlen(password));
82 asn1_push_tag(&req, ASN1_CONTEXT(1));
83 asn1_push_tag(&req, ASN1_SEQUENCE(0));
85 asn1_push_tag(&req, ASN1_CONTEXT(0));
86 asn1_write_Integer(&req, 1);
89 asn1_push_tag(&req, ASN1_CONTEXT(1));
90 asn1_push_tag(&req, ASN1_SEQUENCE(0));
93 asn1_write_GeneralString(&req, princ_part1);
95 asn1_write_GeneralString(&req, princ_part2);
101 asn1_push_tag(&req, ASN1_CONTEXT(2));
102 asn1_write_GeneralString(&req, realm);
106 ret = data_blob(req.data, req.length);
114 static krb5_error_code build_kpasswd_request(uint16 pversion,
115 krb5_context context,
116 krb5_auth_context auth_context,
124 krb5_data encoded_setpw;
125 krb5_replay_data replay;
129 ret = krb5_auth_con_setflags(context,
130 auth_context,KRB5_AUTH_CONTEXT_DO_SEQUENCE);
132 DEBUG(1,("krb5_auth_con_setflags failed (%s)\n",
133 error_message(ret)));
137 /* handle protocol differences in chpw and setpw */
138 if (pversion == KRB5_KPASSWD_VERS_CHANGEPW)
139 setpw = data_blob(passwd, strlen(passwd));
140 else if (pversion == KRB5_KPASSWD_VERS_SETPW ||
141 pversion == KRB5_KPASSWD_VERS_SETPW_ALT)
142 setpw = encode_krb5_setpw(princ, passwd);
146 encoded_setpw.data = (char *)setpw.data;
147 encoded_setpw.length = setpw.length;
149 ret = krb5_mk_priv(context, auth_context,
150 &encoded_setpw, &cipherpw, &replay);
152 data_blob_free(&setpw); /*from 'encode_krb5_setpw(...)' */
155 DEBUG(1,("krb5_mk_priv failed (%s)\n", error_message(ret)));
159 packet->data = (char *)SMB_MALLOC(ap_req->length + cipherpw.length + 6);
163 /* see the RFC for details */
164 p = ((char *)packet->data) + 2;
165 RSSVAL(p, 0, pversion);
167 RSSVAL(p, 0, ap_req->length);
169 memcpy(p, ap_req->data, ap_req->length);
171 memcpy(p, cipherpw.data, cipherpw.length);
172 p += cipherpw.length;
173 packet->length = PTR_DIFF(p,packet->data);
174 RSSVAL(packet->data, 0, packet->length);
176 free(cipherpw.data); /* from 'krb5_mk_priv(...)' */
181 static const struct kpasswd_errors {
183 const char *error_string;
184 } kpasswd_errors[] = {
185 {KRB5_KPASSWD_MALFORMED, "Malformed request error"},
186 {KRB5_KPASSWD_HARDERROR, "Server error"},
187 {KRB5_KPASSWD_AUTHERROR, "Authentication error"},
188 {KRB5_KPASSWD_SOFTERROR, "Password change rejected"},
189 {KRB5_KPASSWD_ACCESSDENIED, "Client does not have proper authorization"},
190 {KRB5_KPASSWD_BAD_VERSION, "Protocol version not supported"},
191 {KRB5_KPASSWD_INITIAL_FLAG_NEEDED, "Authorization ticket must have initial flag set"},
192 {KRB5_KPASSWD_POLICY_REJECT, "Password rejected due to policy requirements"},
193 {KRB5_KPASSWD_BAD_PRINCIPAL, "Target principal does not exist"},
194 {KRB5_KPASSWD_ETYPE_NOSUPP, "Unsupported encryption type"},
198 static krb5_error_code setpw_result_code_string(krb5_context context,
200 const char **code_string)
202 unsigned int idx = 0;
204 while (kpasswd_errors[idx].error_string != NULL) {
205 if (kpasswd_errors[idx].result_code ==
207 *code_string = kpasswd_errors[idx].error_string;
212 *code_string = "Password change failed";
216 static krb5_error_code parse_setpw_reply(krb5_context context,
217 krb5_auth_context auth_context,
222 int vnum, ret, res_code;
223 krb5_data cipherresult;
224 krb5_data clearresult;
225 krb5_ap_rep_enc_part *ap_rep_enc;
226 krb5_replay_data replay;
228 if (packet->length < 4) {
229 return KRB5KRB_AP_ERR_MODIFIED;
234 if (((char *)packet->data)[0] == 0x7e || ((char *)packet->data)[0] == 0x5e) {
235 /* it's an error packet. We should parse it ... */
236 DEBUG(1,("Got error packet 0x%x from kpasswd server\n",
237 ((char *)packet->data)[0]));
238 return KRB5KRB_AP_ERR_MODIFIED;
241 if (RSVAL(p, 0) != packet->length) {
242 DEBUG(1,("Bad packet length (%d/%d) from kpasswd server\n",
243 RSVAL(p, 0), packet->length));
244 return KRB5KRB_AP_ERR_MODIFIED;
249 vnum = RSVAL(p, 0); p += 2;
251 /* FIXME: According to standard there is only one type of reply */
252 if (vnum != KRB5_KPASSWD_VERS_SETPW &&
253 vnum != KRB5_KPASSWD_VERS_SETPW_ALT &&
254 vnum != KRB5_KPASSWD_VERS_CHANGEPW) {
255 DEBUG(1,("Bad vnum (%d) from kpasswd server\n", vnum));
256 return KRB5KDC_ERR_BAD_PVNO;
259 ap_rep.length = RSVAL(p, 0); p += 2;
261 if (p + ap_rep.length >= (char *)packet->data + packet->length) {
262 DEBUG(1,("ptr beyond end of packet from kpasswd server\n"));
263 return KRB5KRB_AP_ERR_MODIFIED;
266 if (ap_rep.length == 0) {
267 DEBUG(1,("got unencrypted setpw result?!\n"));
268 return KRB5KRB_AP_ERR_MODIFIED;
275 ret = krb5_rd_rep(context, auth_context, &ap_rep, &ap_rep_enc);
277 DEBUG(1,("failed to rd setpw reply (%s)\n", error_message(ret)));
278 return KRB5KRB_AP_ERR_MODIFIED;
281 krb5_free_ap_rep_enc_part(context, ap_rep_enc);
283 cipherresult.data = p;
284 cipherresult.length = ((char *)packet->data + packet->length) - p;
286 ret = krb5_rd_priv(context, auth_context, &cipherresult, &clearresult,
289 DEBUG(1,("failed to decrypt setpw reply (%s)\n", error_message(ret)));
290 return KRB5KRB_AP_ERR_MODIFIED;
293 if (clearresult.length < 2) {
294 free(clearresult.data);
295 ret = KRB5KRB_AP_ERR_MODIFIED;
296 return KRB5KRB_AP_ERR_MODIFIED;
299 p = clearresult.data;
301 res_code = RSVAL(p, 0);
303 free(clearresult.data);
305 if ((res_code < KRB5_KPASSWD_SUCCESS) ||
306 (res_code > KRB5_KPASSWD_ETYPE_NOSUPP)) {
307 return KRB5KRB_AP_ERR_MODIFIED;
310 if(res_code == KRB5_KPASSWD_SUCCESS)
314 setpw_result_code_string(context, res_code, &errstr);
315 DEBUG(1, ("Error changing password: %s\n", errstr));
318 case KRB5_KPASSWD_ACCESSDENIED:
319 return KRB5KDC_ERR_BADOPTION;
320 case KRB5_KPASSWD_INITIAL_FLAG_NEEDED:
321 return KRB5KDC_ERR_BADOPTION;
322 /* return KV5M_ALT_METHOD; MIT-only define */
323 case KRB5_KPASSWD_ETYPE_NOSUPP:
324 return KRB5KDC_ERR_ETYPE_NOSUPP;
325 case KRB5_KPASSWD_BAD_PRINCIPAL:
326 return KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
327 case KRB5_KPASSWD_POLICY_REJECT:
328 return KRB5KDC_ERR_POLICY;
330 return KRB5KRB_ERR_GENERIC;
335 static ADS_STATUS do_krb5_kpasswd_request(krb5_context context,
336 const char *kdc_host,
342 krb5_auth_context auth_context = NULL;
343 krb5_data ap_req, chpw_req, chpw_rep;
344 int ret, sock, addr_len;
345 struct sockaddr remote_addr, local_addr;
346 krb5_address local_kaddr, remote_kaddr;
348 ret = krb5_mk_req_extended(context, &auth_context, AP_OPTS_USE_SUBKEY,
349 NULL, credsp, &ap_req);
351 DEBUG(1,("krb5_mk_req_extended failed (%s)\n", error_message(ret)));
352 return ADS_ERROR_KRB5(ret);
355 sock = open_udp_socket(kdc_host, DEFAULT_KPASSWD_PORT);
359 krb5_auth_con_free(context, auth_context);
360 DEBUG(1,("failed to open kpasswd socket to %s (%s)\n",
361 kdc_host, strerror(errno)));
362 return ADS_ERROR_SYSTEM(rc);
365 addr_len = sizeof(remote_addr);
366 getpeername(sock, &remote_addr, &addr_len);
367 addr_len = sizeof(local_addr);
368 getsockname(sock, &local_addr, &addr_len);
370 setup_kaddr(&remote_kaddr, &remote_addr);
371 setup_kaddr(&local_kaddr, &local_addr);
373 ret = krb5_auth_con_setaddrs(context, auth_context, &local_kaddr, NULL);
377 krb5_auth_con_free(context, auth_context);
378 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n", error_message(ret)));
379 return ADS_ERROR_KRB5(ret);
382 ret = build_kpasswd_request(pversion, context, auth_context, &ap_req,
383 princ, newpw, &chpw_req);
387 krb5_auth_con_free(context, auth_context);
388 DEBUG(1,("build_setpw_request failed (%s)\n", error_message(ret)));
389 return ADS_ERROR_KRB5(ret);
392 if (write(sock, chpw_req.data, chpw_req.length) != chpw_req.length) {
396 krb5_auth_con_free(context, auth_context);
397 DEBUG(1,("send of chpw failed (%s)\n", strerror(errno)));
398 return ADS_ERROR_SYSTEM(errno);
403 chpw_rep.length = 1500;
404 chpw_rep.data = (char *) SMB_MALLOC(chpw_rep.length);
405 if (!chpw_rep.data) {
408 krb5_auth_con_free(context, auth_context);
409 DEBUG(1,("send of chpw failed (%s)\n", strerror(errno)));
411 return ADS_ERROR_SYSTEM(errno);
414 ret = read(sock, chpw_rep.data, chpw_rep.length);
419 krb5_auth_con_free(context, auth_context);
420 DEBUG(1,("recv of chpw reply failed (%s)\n", strerror(errno)));
421 return ADS_ERROR_SYSTEM(errno);
425 chpw_rep.length = ret;
427 ret = krb5_auth_con_setaddrs(context, auth_context, NULL,&remote_kaddr);
431 krb5_auth_con_free(context, auth_context);
432 DEBUG(1,("krb5_auth_con_setaddrs on reply failed (%s)\n",
433 error_message(ret)));
434 return ADS_ERROR_KRB5(ret);
437 ret = parse_setpw_reply(context, auth_context, &chpw_rep);
442 krb5_auth_con_free(context, auth_context);
443 DEBUG(1,("parse_setpw_reply failed (%s)\n",
444 error_message(ret)));
445 return ADS_ERROR_KRB5(ret);
449 krb5_auth_con_free(context, auth_context);
454 ADS_STATUS ads_krb5_set_password(const char *kdc_host, const char *princ,
455 const char *newpw, int time_offset)
459 krb5_error_code ret = 0;
460 krb5_context context = NULL;
461 krb5_principal principal = NULL;
462 char *princ_name = NULL;
464 krb5_creds creds, *credsp = NULL;
465 #if KRB5_PRINC_REALM_RETURNS_REALM
466 krb5_realm orig_realm;
468 krb5_data orig_realm;
470 krb5_ccache ccache = NULL;
474 ret = krb5_init_context(&context);
476 DEBUG(1,("Failed to init krb5 context (%s)\n", error_message(ret)));
477 return ADS_ERROR_KRB5(ret);
480 if (time_offset != 0) {
481 krb5_set_real_time(context, time(NULL) + time_offset, 0);
484 ret = krb5_cc_default(context, &ccache);
486 krb5_free_context(context);
487 DEBUG(1,("Failed to get default creds (%s)\n", error_message(ret)));
488 return ADS_ERROR_KRB5(ret);
491 realm = strchr_m(princ, '@');
493 krb5_cc_close(context, ccache);
494 krb5_free_context(context);
495 DEBUG(1,("Failed to get realm\n"));
496 return ADS_ERROR_KRB5(-1);
500 asprintf(&princ_name, "kadmin/changepw@%s", realm);
501 ret = krb5_parse_name(context, princ_name, &creds.server);
503 krb5_cc_close(context, ccache);
504 krb5_free_context(context);
505 DEBUG(1,("Failed to parse kadmin/changepw (%s)\n", error_message(ret)));
506 return ADS_ERROR_KRB5(ret);
510 /* parse the principal we got as a function argument */
511 ret = krb5_parse_name(context, princ, &principal);
513 krb5_cc_close(context, ccache);
514 krb5_free_principal(context, creds.server);
515 krb5_free_context(context);
516 DEBUG(1,("Failed to parse %s (%s)\n", princ_name, error_message(ret)));
517 return ADS_ERROR_KRB5(ret);
520 /* The creds.server principal takes ownership of this memory.
521 Remember to set back to original value before freeing. */
522 orig_realm = *krb5_princ_realm(context, creds.server);
523 krb5_princ_set_realm(context, creds.server, krb5_princ_realm(context, principal));
525 ret = krb5_cc_get_principal(context, ccache, &creds.client);
527 krb5_cc_close(context, ccache);
528 krb5_princ_set_realm(context, creds.server, &orig_realm);
529 krb5_free_principal(context, creds.server);
530 krb5_free_principal(context, principal);
531 krb5_free_context(context);
532 DEBUG(1,("Failed to get principal from ccache (%s)\n",
533 error_message(ret)));
534 return ADS_ERROR_KRB5(ret);
537 ret = krb5_get_credentials(context, 0, ccache, &creds, &credsp);
539 krb5_cc_close(context, ccache);
540 krb5_free_principal(context, creds.client);
541 krb5_princ_set_realm(context, creds.server, &orig_realm);
542 krb5_free_principal(context, creds.server);
543 krb5_free_principal(context, principal);
544 krb5_free_context(context);
545 DEBUG(1,("krb5_get_credentials failed (%s)\n", error_message(ret)));
546 return ADS_ERROR_KRB5(ret);
549 /* we might have to call krb5_free_creds(...) from now on ... */
551 aret = do_krb5_kpasswd_request(context, kdc_host,
552 KRB5_KPASSWD_VERS_SETPW,
553 credsp, princ, newpw);
555 krb5_free_creds(context, credsp);
556 krb5_free_principal(context, creds.client);
557 krb5_princ_set_realm(context, creds.server, &orig_realm);
558 krb5_free_principal(context, creds.server);
559 krb5_free_principal(context, principal);
560 krb5_cc_close(context, ccache);
561 krb5_free_context(context);
567 we use a prompter to avoid a crash bug in the kerberos libs when
568 dealing with empty passwords
569 this prompter is just a string copy ...
571 static krb5_error_code
572 kerb_prompter(krb5_context ctx, void *data,
576 krb5_prompt prompts[])
578 if (num_prompts == 0) return 0;
580 memset(prompts[0].reply->data, 0, prompts[0].reply->length);
581 if (prompts[0].reply->length > 0) {
583 strncpy(prompts[0].reply->data, data, prompts[0].reply->length-1);
584 prompts[0].reply->length = strlen(prompts[0].reply->data);
586 prompts[0].reply->length = 0;
592 static ADS_STATUS ads_krb5_chg_password(const char *kdc_host,
593 const char *principal,
600 krb5_context context = NULL;
601 krb5_principal princ;
602 krb5_get_init_creds_opt opts;
604 char *chpw_princ = NULL, *password;
606 ret = krb5_init_context(&context);
608 DEBUG(1,("Failed to init krb5 context (%s)\n", error_message(ret)));
609 return ADS_ERROR_KRB5(ret);
612 if ((ret = krb5_parse_name(context, principal,
614 krb5_free_context(context);
615 DEBUG(1,("Failed to parse %s (%s)\n", principal, error_message(ret)));
616 return ADS_ERROR_KRB5(ret);
619 krb5_get_init_creds_opt_init(&opts);
620 krb5_get_init_creds_opt_set_tkt_life(&opts, 5*60);
621 krb5_get_init_creds_opt_set_renew_life(&opts, 0);
622 krb5_get_init_creds_opt_set_forwardable(&opts, 0);
623 krb5_get_init_creds_opt_set_proxiable(&opts, 0);
625 /* We have to obtain an INITIAL changepw ticket for changing password */
626 asprintf(&chpw_princ, "kadmin/changepw@%s",
627 (char *) krb5_princ_realm(context, princ));
628 password = SMB_STRDUP(oldpw);
629 ret = krb5_get_init_creds_password(context, &creds, princ, password,
631 0, chpw_princ, &opts);
632 SAFE_FREE(chpw_princ);
636 if (ret == KRB5KRB_AP_ERR_BAD_INTEGRITY)
637 DEBUG(1,("Password incorrect while getting initial ticket"));
639 DEBUG(1,("krb5_get_init_creds_password failed (%s)\n", error_message(ret)));
641 krb5_free_principal(context, princ);
642 krb5_free_context(context);
643 return ADS_ERROR_KRB5(ret);
646 aret = do_krb5_kpasswd_request(context, kdc_host,
647 KRB5_KPASSWD_VERS_CHANGEPW,
648 &creds, principal, newpw);
650 krb5_free_principal(context, princ);
651 krb5_free_context(context);
657 ADS_STATUS kerberos_set_password(const char *kpasswd_server,
658 const char *auth_principal, const char *auth_password,
659 const char *target_principal, const char *new_password,
664 if ((ret = kerberos_kinit_password(auth_principal, auth_password, time_offset, NULL, NULL))) {
665 DEBUG(1,("Failed kinit for principal %s (%s)\n", auth_principal, error_message(ret)));
666 return ADS_ERROR_KRB5(ret);
669 if (!strcmp(auth_principal, target_principal))
670 return ads_krb5_chg_password(kpasswd_server, target_principal,
671 auth_password, new_password, time_offset);
673 return ads_krb5_set_password(kpasswd_server, target_principal,
674 new_password, time_offset);
679 * Set the machine account password
680 * @param ads connection to ads server
681 * @param hostname machine whose password is being set
682 * @param password new password
683 * @return status of password change
685 ADS_STATUS ads_set_machine_password(ADS_STRUCT *ads,
686 const char *machine_account,
687 const char *password)
690 char *principal = NULL;
693 we need to use the '$' form of the name here (the machine account name),
694 as otherwise the server might end up setting the password for a user
697 asprintf(&principal, "%s@%s", machine_account, ads->config.realm);
699 status = ads_krb5_set_password(ads->auth.kdc_server, principal,
700 password, ads->auth.time_offset);