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 2
29 #define KRB5_KPASSWD_VERS_SETPW_MS 0xff80
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 = strdup(principal);
59 if ((c = strchr(princ, '/')) == NULL) {
69 if ((c = strchr(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_MS)
142 setpw = encode_krb5_setpw(princ, passwd);
146 encoded_setpw.data = 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 *)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 krb5_error_code krb5_setpw_result_code_string(krb5_context context,
183 const char **code_string)
185 switch (result_code) {
186 case KRB5_KPASSWD_MALFORMED:
187 *code_string = "Malformed request error";
189 case KRB5_KPASSWD_HARDERROR:
190 *code_string = "Server error";
192 case KRB5_KPASSWD_AUTHERROR:
193 *code_string = "Authentication error";
195 case KRB5_KPASSWD_SOFTERROR:
196 *code_string = "Password change rejected";
198 case KRB5_KPASSWD_ACCESSDENIED:
199 *code_string = "Client does not have proper authorization";
201 case KRB5_KPASSWD_BAD_VERSION:
202 *code_string = "Protocol version not supported";
204 case KRB5_KPASSWD_INITIAL_FLAG_NEEDED:
205 *code_string = "Authorization ticket must have initial flag set";
207 case KRB5_KPASSWD_POLICY_REJECT:
208 *code_string = "Password rejected due to policy requirements";
210 case KRB5_KPASSWD_BAD_PRINCIPAL:
211 *code_string = "Target principal does not exist";
213 case KRB5_KPASSWD_ETYPE_NOSUPP:
214 *code_string = "Unsupported encryption type";
217 *code_string = "Password change failed";
224 static krb5_error_code parse_setpw_reply(krb5_context context,
225 krb5_auth_context auth_context,
230 int vnum, ret, res_code;
231 krb5_data cipherresult;
232 krb5_data clearresult;
233 krb5_ap_rep_enc_part *ap_rep_enc;
234 krb5_replay_data replay;
236 if (packet->length < 4) {
237 return KRB5KRB_AP_ERR_MODIFIED;
242 if (((char *)packet->data)[0] == 0x7e || ((char *)packet->data)[0] == 0x5e) {
243 /* it's an error packet. We should parse it ... */
244 DEBUG(1,("Got error packet 0x%x from kpasswd server\n",
245 ((char *)packet->data)[0]));
246 return KRB5KRB_AP_ERR_MODIFIED;
249 if (RSVAL(p, 0) != packet->length) {
250 DEBUG(1,("Bad packet length (%d/%d) from kpasswd server\n",
251 RSVAL(p, 0), packet->length));
252 return KRB5KRB_AP_ERR_MODIFIED;
257 vnum = RSVAL(p, 0); p += 2;
259 /* FIXME: According to standard there is only one type of reply */
260 if (vnum != KRB5_KPASSWD_VERS_SETPW &&
261 vnum != KRB5_KPASSWD_VERS_SETPW_MS &&
262 vnum != KRB5_KPASSWD_VERS_CHANGEPW) {
263 DEBUG(1,("Bad vnum (%d) from kpasswd server\n", vnum));
264 return KRB5KDC_ERR_BAD_PVNO;
267 ap_rep.length = RSVAL(p, 0); p += 2;
269 if (p + ap_rep.length >= (char *)packet->data + packet->length) {
270 DEBUG(1,("ptr beyond end of packet from kpasswd server\n"));
271 return KRB5KRB_AP_ERR_MODIFIED;
274 if (ap_rep.length == 0) {
275 DEBUG(1,("got unencrypted setpw result?!\n"));
276 return KRB5KRB_AP_ERR_MODIFIED;
283 ret = krb5_rd_rep(context, auth_context, &ap_rep, &ap_rep_enc);
285 DEBUG(1,("failed to rd setpw reply (%s)\n", error_message(ret)));
286 return KRB5KRB_AP_ERR_MODIFIED;
289 krb5_free_ap_rep_enc_part(context, ap_rep_enc);
291 cipherresult.data = p;
292 cipherresult.length = ((char *)packet->data + packet->length) - p;
294 ret = krb5_rd_priv(context, auth_context, &cipherresult, &clearresult,
297 DEBUG(1,("failed to decrypt setpw reply (%s)\n", error_message(ret)));
298 return KRB5KRB_AP_ERR_MODIFIED;
301 if (clearresult.length < 2) {
302 free(clearresult.data);
303 ret = KRB5KRB_AP_ERR_MODIFIED;
304 return KRB5KRB_AP_ERR_MODIFIED;
307 p = clearresult.data;
309 res_code = RSVAL(p, 0);
311 free(clearresult.data);
313 if ((res_code < KRB5_KPASSWD_SUCCESS) ||
314 (res_code > KRB5_KPASSWD_ETYPE_NOSUPP)) {
315 return KRB5KRB_AP_ERR_MODIFIED;
318 if(res_code == KRB5_KPASSWD_SUCCESS)
322 krb5_setpw_result_code_string(context, res_code, &errstr);
323 DEBUG(1, ("Error changing password: %s\n", errstr));
326 case KRB5_KPASSWD_ACCESSDENIED:
327 return KRB5KDC_ERR_BADOPTION;
329 case KRB5_KPASSWD_INITIAL_FLAG_NEEDED:
330 return KRB5KDC_ERR_BADOPTION;
331 /* return KV5M_ALT_METHOD; MIT-only define */
333 case KRB5_KPASSWD_ETYPE_NOSUPP:
334 return KRB5KDC_ERR_ETYPE_NOSUPP;
336 case KRB5_KPASSWD_BAD_PRINCIPAL:
337 return KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
339 case KRB5_KPASSWD_POLICY_REJECT:
340 return KRB5KDC_ERR_POLICY;
343 return KRB5KRB_ERR_GENERIC;
349 static ADS_STATUS do_krb5_kpasswd_request(krb5_context context,
350 const char *kdc_host,
356 krb5_auth_context auth_context = NULL;
357 krb5_data ap_req, chpw_req, chpw_rep;
358 int ret, sock, addr_len;
359 struct sockaddr remote_addr, local_addr;
360 krb5_address local_kaddr, remote_kaddr;
362 ret = krb5_mk_req_extended(context, &auth_context, AP_OPTS_USE_SUBKEY,
363 NULL, credsp, &ap_req);
365 DEBUG(1,("krb5_mk_req_extended failed (%s)\n", error_message(ret)));
366 return ADS_ERROR_KRB5(ret);
369 sock = open_udp_socket(kdc_host, DEFAULT_KPASSWD_PORT);
373 krb5_auth_con_free(context, auth_context);
374 DEBUG(1,("failed to open kpasswd socket to %s (%s)\n",
375 kdc_host, strerror(errno)));
376 return ADS_ERROR_SYSTEM(rc);
379 addr_len = sizeof(remote_addr);
380 getpeername(sock, &remote_addr, &addr_len);
381 addr_len = sizeof(local_addr);
382 getsockname(sock, &local_addr, &addr_len);
384 setup_kaddr(&remote_kaddr, &remote_addr);
385 setup_kaddr(&local_kaddr, &local_addr);
387 ret = krb5_auth_con_setaddrs(context, auth_context, &local_kaddr, NULL);
391 krb5_auth_con_free(context, auth_context);
392 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n", error_message(ret)));
393 return ADS_ERROR_KRB5(ret);
396 ret = build_kpasswd_request(pversion, context, auth_context, &ap_req,
397 princ, newpw, &chpw_req);
401 krb5_auth_con_free(context, auth_context);
402 DEBUG(1,("build_setpw_request failed (%s)\n", error_message(ret)));
403 return ADS_ERROR_KRB5(ret);
406 if (write(sock, chpw_req.data, chpw_req.length) != chpw_req.length) {
410 krb5_auth_con_free(context, auth_context);
411 DEBUG(1,("send of chpw failed (%s)\n", strerror(errno)));
412 return ADS_ERROR_SYSTEM(errno);
417 chpw_rep.length = 1500;
418 chpw_rep.data = (char *) malloc(chpw_rep.length);
419 if (!chpw_rep.data) {
422 krb5_auth_con_free(context, auth_context);
423 DEBUG(1,("send of chpw failed (%s)\n", strerror(errno)));
425 return ADS_ERROR_SYSTEM(errno);
428 ret = read(sock, chpw_rep.data, chpw_rep.length);
433 krb5_auth_con_free(context, auth_context);
434 DEBUG(1,("recv of chpw reply failed (%s)\n", strerror(errno)));
435 return ADS_ERROR_SYSTEM(errno);
439 chpw_rep.length = ret;
441 ret = krb5_auth_con_setaddrs(context, auth_context, NULL,&remote_kaddr);
445 krb5_auth_con_free(context, auth_context);
446 DEBUG(1,("krb5_auth_con_setaddrs on reply failed (%s)\n",
447 error_message(ret)));
448 return ADS_ERROR_KRB5(ret);
451 ret = parse_setpw_reply(context, auth_context, &chpw_rep);
456 krb5_auth_con_free(context, auth_context);
457 DEBUG(1,("parse_setpw_reply failed (%s)\n",
458 error_message(ret)));
459 return ADS_ERROR_KRB5(ret);
463 krb5_auth_con_free(context, auth_context);
468 ADS_STATUS krb5_set_password(const char *kdc_host, const char *princ, const char *newpw,
474 krb5_context context;
475 krb5_principal principal;
478 krb5_creds creds, *credsp;
481 ret = krb5_init_context(&context);
483 DEBUG(1,("Failed to init krb5 context (%s)\n", error_message(ret)));
484 return ADS_ERROR_KRB5(ret);
487 if (time_offset != 0) {
488 krb5_set_real_time(context, time(NULL) + time_offset, 0);
491 ret = krb5_cc_default(context, &ccache);
493 krb5_free_context(context);
494 DEBUG(1,("Failed to get default creds (%s)\n", error_message(ret)));
495 return ADS_ERROR_KRB5(ret);
500 realm = strchr(princ, '@');
503 asprintf(&princ_name, "kadmin/changepw@%s", realm);
504 ret = krb5_parse_name(context, princ_name, &creds.server);
506 krb5_free_context(context);
507 DEBUG(1,("Failed to parse kadmin/changepw (%s)\n", error_message(ret)));
508 return ADS_ERROR_KRB5(ret);
512 /* parse the principal we got as a function argument */
513 ret = krb5_parse_name(context, princ, &principal);
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 krb5_princ_set_realm(context, creds.server,
521 krb5_princ_realm(context, principal));
523 ret = krb5_cc_get_principal(context, ccache, &creds.client);
525 krb5_free_principal(context, principal);
526 krb5_free_context(context);
527 DEBUG(1,("Failed to get principal from ccache (%s)\n",
528 error_message(ret)));
529 return ADS_ERROR_KRB5(ret);
532 ret = krb5_get_credentials(context, 0, ccache, &creds, &credsp);
534 krb5_free_principal(context, creds.client);
535 krb5_free_principal(context, principal);
536 krb5_free_context(context);
537 DEBUG(1,("krb5_get_credentials failed (%s)\n", error_message(ret)));
538 return ADS_ERROR_KRB5(ret);
541 /* we might have to call krb5_free_creds(...) from now on ... */
543 aret = do_krb5_kpasswd_request(context, kdc_host,
544 KRB5_KPASSWD_VERS_SETPW_MS,
545 credsp, princ, newpw);
547 krb5_free_creds(context, credsp);
548 krb5_free_principal(context, creds.client);
549 krb5_free_principal(context, creds.server);
550 krb5_free_principal(context, principal);
551 krb5_free_context(context);
557 we use a prompter to avoid a crash bug in the kerberos libs when
558 dealing with empty passwords
559 this prompter is just a string copy ...
561 static krb5_error_code
562 kerb_prompter(krb5_context ctx, void *data,
566 krb5_prompt prompts[])
568 if (num_prompts == 0) return 0;
570 memset(prompts[0].reply->data, 0, prompts[0].reply->length);
571 if (prompts[0].reply->length > 0) {
573 strncpy(prompts[0].reply->data, data, prompts[0].reply->length-1);
574 prompts[0].reply->length = strlen(prompts[0].reply->data);
576 prompts[0].reply->length = 0;
582 ADS_STATUS krb5_chg_password(const char *kdc_host,
583 const char *principal,
590 krb5_context context;
591 krb5_principal princ;
592 krb5_get_init_creds_opt opts;
594 char *chpw_princ = NULL, *password;
596 ret = krb5_init_context(&context);
598 DEBUG(1,("Failed to init krb5 context (%s)\n", error_message(ret)));
599 return ADS_ERROR_KRB5(ret);
602 if ((ret = krb5_parse_name(context, principal,
604 krb5_free_context(context);
605 DEBUG(1,("Failed to parse %s (%s)\n", principal, error_message(ret)));
606 return ADS_ERROR_KRB5(ret);
609 krb5_get_init_creds_opt_init(&opts);
610 krb5_get_init_creds_opt_set_tkt_life(&opts, 5*60);
611 krb5_get_init_creds_opt_set_renew_life(&opts, 0);
612 krb5_get_init_creds_opt_set_forwardable(&opts, 0);
613 krb5_get_init_creds_opt_set_proxiable(&opts, 0);
615 /* We have to obtain an INITIAL changepw ticket for changing password */
616 asprintf(&chpw_princ, "kadmin/changepw@%s",
617 (char *) krb5_princ_realm(context, princ));
618 password = strdup(oldpw);
619 ret = krb5_get_init_creds_password(context, &creds, princ, password,
621 0, chpw_princ, &opts);
622 SAFE_FREE(chpw_princ);
626 if (ret == KRB5KRB_AP_ERR_BAD_INTEGRITY)
627 DEBUG(1,("Password incorrect while getting initial ticket"));
629 DEBUG(1,("krb5_get_init_creds_password failed (%s)\n", error_message(ret)));
631 krb5_free_principal(context, princ);
632 krb5_free_context(context);
633 return ADS_ERROR_KRB5(ret);
636 aret = do_krb5_kpasswd_request(context, kdc_host,
637 KRB5_KPASSWD_VERS_CHANGEPW,
638 &creds, principal, newpw);
640 krb5_free_principal(context, princ);
641 krb5_free_context(context);
647 ADS_STATUS kerberos_set_password(const char *kpasswd_server,
648 const char *auth_principal, const char *auth_password,
649 const char *target_principal, const char *new_password,
654 if ((ret = kerberos_kinit_password(auth_principal, auth_password, time_offset))) {
655 DEBUG(1,("Failed kinit for principal %s (%s)\n", auth_principal, error_message(ret)));
656 return ADS_ERROR_KRB5(ret);
659 if (!strcmp(auth_principal, target_principal))
660 return krb5_chg_password(kpasswd_server, target_principal,
661 auth_password, new_password, time_offset);
663 return krb5_set_password(kpasswd_server, target_principal,
664 new_password, time_offset);
669 * Set the machine account password
670 * @param ads connection to ads server
671 * @param hostname machine whose password is being set
672 * @param password new password
673 * @return status of password change
675 ADS_STATUS ads_set_machine_password(ADS_STRUCT *ads,
676 const char *hostname,
677 const char *password)
680 char *host = strdup(hostname);
686 we need to use the '$' form of the name here, as otherwise the
687 server might end up setting the password for a user instead
689 asprintf(&principal, "%s$@%s", host, ads->auth.realm);
691 status = krb5_set_password(ads->auth.kdc_server, principal, password, ads->auth.time_offset);