r6114: the marker is const and is 0x434B 'CK'
[Samba.git] / source / libads / krb5_setpw.c
blob257c28e755afc4cb6ea2f9ca9b2833f04b193c88
1 /*
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.
22 #include "includes.h"
24 #ifdef HAVE_KRB5
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;
49 char* realm = NULL;
50 char* c;
51 char* princ;
52 struct asn1_data req;
53 DATA_BLOB ret;
56 princ = strdup(principal);
58 if ((c = strchr(princ, '/')) == NULL) {
59 c = princ;
60 } else {
61 *c = '\0';
62 c++;
63 princ_part1 = princ;
66 princ_part2 = c;
68 if ((c = strchr(c, '@')) != NULL) {
69 *c = '\0';
70 c++;
71 realm = c;
74 memset(&req, 0, sizeof(req));
76 asn1_push_tag(&req, ASN1_SEQUENCE(0));
77 asn1_push_tag(&req, ASN1_CONTEXT(0));
78 asn1_write_OctetString(&req, password, strlen(password));
79 asn1_pop_tag(&req);
81 asn1_push_tag(&req, ASN1_CONTEXT(1));
82 asn1_push_tag(&req, ASN1_SEQUENCE(0));
84 asn1_push_tag(&req, ASN1_CONTEXT(0));
85 asn1_write_Integer(&req, 1);
86 asn1_pop_tag(&req);
88 asn1_push_tag(&req, ASN1_CONTEXT(1));
89 asn1_push_tag(&req, ASN1_SEQUENCE(0));
91 if (princ_part1)
92 asn1_write_GeneralString(&req, princ_part1);
94 asn1_write_GeneralString(&req, princ_part2);
95 asn1_pop_tag(&req);
96 asn1_pop_tag(&req);
97 asn1_pop_tag(&req);
98 asn1_pop_tag(&req);
100 asn1_push_tag(&req, ASN1_CONTEXT(2));
101 asn1_write_GeneralString(&req, realm);
102 asn1_pop_tag(&req);
103 asn1_pop_tag(&req);
105 ret = data_blob(req.data, req.length);
106 asn1_free(&req);
108 free(princ);
110 return ret;
113 static krb5_error_code build_kpasswd_request(uint16_t pversion,
114 krb5_context context,
115 krb5_auth_context auth_context,
116 krb5_data *ap_req,
117 const char *princ,
118 const char *passwd,
119 krb5_data *packet)
121 krb5_error_code ret;
122 krb5_data cipherpw;
123 krb5_data encoded_setpw;
124 krb5_replay_data replay;
125 char *p;
126 DATA_BLOB setpw;
128 ret = krb5_auth_con_setflags(context,
129 auth_context,KRB5_AUTH_CONTEXT_DO_SEQUENCE);
130 if (ret) {
131 DEBUG(1,("krb5_auth_con_setflags failed (%s)\n",
132 error_message(ret)));
133 return ret;
136 /* handle protocol differences in chpw and setpw */
137 if (pversion == KRB5_KPASSWD_VERS_CHANGEPW)
138 setpw = data_blob(passwd, strlen(passwd));
139 else if (pversion == KRB5_KPASSWD_VERS_SETPW ||
140 pversion == KRB5_KPASSWD_VERS_SETPW_MS)
141 setpw = encode_krb5_setpw(princ, passwd);
142 else
143 return EINVAL;
145 encoded_setpw.data = (char *)setpw.data;
146 encoded_setpw.length = setpw.length;
148 ret = krb5_mk_priv(context, auth_context,
149 &encoded_setpw, &cipherpw, &replay);
151 data_blob_free(&setpw); /*from 'encode_krb5_setpw(...)' */
153 if (ret) {
154 DEBUG(1,("krb5_mk_priv failed (%s)\n", error_message(ret)));
155 return ret;
158 packet->data = (char *)malloc(ap_req->length + cipherpw.length + 6);
159 if (!packet->data)
160 return -1;
162 /* see the RFC for details */
163 p = ((char *)packet->data) + 2;
164 RSSVAL(p, 0, pversion);
165 p += 2;
166 RSSVAL(p, 0, ap_req->length);
167 p += 2;
168 memcpy(p, ap_req->data, ap_req->length);
169 p += ap_req->length;
170 memcpy(p, cipherpw.data, cipherpw.length);
171 p += cipherpw.length;
172 packet->length = PTR_DIFF(p,packet->data);
173 RSSVAL(packet->data, 0, packet->length);
175 free(cipherpw.data); /* from 'krb5_mk_priv(...)' */
177 return 0;
180 static const struct kpasswd_errors {
181 int result_code;
182 const char *error_string;
183 } kpasswd_errors[] = {
184 {KRB5_KPASSWD_MALFORMED, "Malformed request error"},
185 {KRB5_KPASSWD_HARDERROR, "Server error"},
186 {KRB5_KPASSWD_AUTHERROR, "Authentication error"},
187 {KRB5_KPASSWD_SOFTERROR, "Password change rejected"},
188 {KRB5_KPASSWD_ACCESSDENIED, "Client does not have proper authorization"},
189 {KRB5_KPASSWD_BAD_VERSION, "Protocol version not supported"},
190 {KRB5_KPASSWD_INITIAL_FLAG_NEEDED, "Authorization ticket must have initial flag set"},
191 {KRB5_KPASSWD_POLICY_REJECT, "Password rejected due to policy requirements"},
192 {KRB5_KPASSWD_BAD_PRINCIPAL, "Target principal does not exist"},
193 {KRB5_KPASSWD_ETYPE_NOSUPP, "Unsupported encryption type"},
194 {0, NULL}
197 static krb5_error_code setpw_result_code_string(krb5_context context,
198 int result_code,
199 const char **code_string)
201 uint_t idx = 0;
203 while (kpasswd_errors[idx].error_string != NULL) {
204 if (kpasswd_errors[idx].result_code ==
205 result_code) {
206 *code_string = kpasswd_errors[idx].error_string;
207 return 0;
209 idx++;
211 *code_string = "Password change failed";
212 return (0);
215 static krb5_error_code parse_setpw_reply(krb5_context context,
216 krb5_auth_context auth_context,
217 krb5_data *packet)
219 krb5_data ap_rep;
220 char *p;
221 int vnum, ret, res_code;
222 krb5_data cipherresult;
223 krb5_data clearresult;
224 krb5_ap_rep_enc_part *ap_rep_enc;
225 krb5_replay_data replay;
227 if (packet->length < 4) {
228 return KRB5KRB_AP_ERR_MODIFIED;
231 p = packet->data;
233 if (((char *)packet->data)[0] == 0x7e || ((char *)packet->data)[0] == 0x5e) {
234 /* it's an error packet. We should parse it ... */
235 DEBUG(1,("Got error packet 0x%x from kpasswd server\n",
236 ((char *)packet->data)[0]));
237 return KRB5KRB_AP_ERR_MODIFIED;
240 if (RSVAL(p, 0) != packet->length) {
241 DEBUG(1,("Bad packet length (%d/%d) from kpasswd server\n",
242 RSVAL(p, 0), packet->length));
243 return KRB5KRB_AP_ERR_MODIFIED;
246 p += 2;
248 vnum = RSVAL(p, 0); p += 2;
250 /* FIXME: According to standard there is only one type of reply */
251 if (vnum != KRB5_KPASSWD_VERS_SETPW &&
252 vnum != KRB5_KPASSWD_VERS_SETPW_MS &&
253 vnum != KRB5_KPASSWD_VERS_CHANGEPW) {
254 DEBUG(1,("Bad vnum (%d) from kpasswd server\n", vnum));
255 return KRB5KDC_ERR_BAD_PVNO;
258 ap_rep.length = RSVAL(p, 0); p += 2;
260 if (p + ap_rep.length >= (char *)packet->data + packet->length) {
261 DEBUG(1,("ptr beyond end of packet from kpasswd server\n"));
262 return KRB5KRB_AP_ERR_MODIFIED;
265 if (ap_rep.length == 0) {
266 DEBUG(1,("got unencrypted setpw result?!\n"));
267 return KRB5KRB_AP_ERR_MODIFIED;
270 /* verify ap_rep */
271 ap_rep.data = p;
272 p += ap_rep.length;
274 ret = krb5_rd_rep(context, auth_context, &ap_rep, &ap_rep_enc);
275 if (ret) {
276 DEBUG(1,("failed to rd setpw reply (%s)\n", error_message(ret)));
277 return KRB5KRB_AP_ERR_MODIFIED;
280 krb5_free_ap_rep_enc_part(context, ap_rep_enc);
282 cipherresult.data = p;
283 cipherresult.length = ((char *)packet->data + packet->length) - p;
285 ret = krb5_rd_priv(context, auth_context, &cipherresult, &clearresult,
286 &replay);
287 if (ret) {
288 DEBUG(1,("failed to decrypt setpw reply (%s)\n", error_message(ret)));
289 return KRB5KRB_AP_ERR_MODIFIED;
292 if (clearresult.length < 2) {
293 free(clearresult.data);
294 ret = KRB5KRB_AP_ERR_MODIFIED;
295 return KRB5KRB_AP_ERR_MODIFIED;
298 p = clearresult.data;
300 res_code = RSVAL(p, 0);
302 free(clearresult.data);
304 if ((res_code < KRB5_KPASSWD_SUCCESS) ||
305 (res_code > KRB5_KPASSWD_ETYPE_NOSUPP)) {
306 return KRB5KRB_AP_ERR_MODIFIED;
309 if(res_code == KRB5_KPASSWD_SUCCESS)
310 return 0;
311 else {
312 const char *errstr;
313 setpw_result_code_string(context, res_code, &errstr);
314 DEBUG(1, ("Error changing password: %s\n", errstr));
316 switch(res_code) {
317 case KRB5_KPASSWD_ACCESSDENIED:
318 return KRB5KDC_ERR_BADOPTION;
319 break;
320 case KRB5_KPASSWD_INITIAL_FLAG_NEEDED:
321 return KRB5KDC_ERR_BADOPTION;
322 /* return KV5M_ALT_METHOD; MIT-only define */
323 break;
324 case KRB5_KPASSWD_ETYPE_NOSUPP:
325 return KRB5KDC_ERR_ETYPE_NOSUPP;
326 break;
327 case KRB5_KPASSWD_BAD_PRINCIPAL:
328 return KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
329 break;
330 case KRB5_KPASSWD_POLICY_REJECT:
331 return KRB5KDC_ERR_POLICY;
332 break;
333 default:
334 return KRB5KRB_ERR_GENERIC;
335 break;
340 static ADS_STATUS do_krb5_kpasswd_request(krb5_context context,
341 const char *kdc_host,
342 uint16_t pversion,
343 krb5_creds *credsp,
344 const char *princ,
345 const char *newpw)
347 krb5_auth_context auth_context = NULL;
348 krb5_data ap_req, chpw_req, chpw_rep;
349 int ret, sock, addr_len;
350 struct sockaddr remote_addr, local_addr;
351 krb5_address local_kaddr, remote_kaddr;
353 ret = krb5_mk_req_extended(context, &auth_context, AP_OPTS_USE_SUBKEY,
354 NULL, credsp, &ap_req);
355 if (ret) {
356 DEBUG(1,("krb5_mk_req_extended failed (%s)\n", error_message(ret)));
357 return ADS_ERROR_KRB5(ret);
360 sock = open_udp_socket(kdc_host, DEFAULT_KPASSWD_PORT);
361 if (sock == -1) {
362 int rc = errno;
363 free(ap_req.data);
364 krb5_auth_con_free(context, auth_context);
365 DEBUG(1,("failed to open kpasswd socket to %s (%s)\n",
366 kdc_host, strerror(errno)));
367 return ADS_ERROR_SYSTEM(rc);
370 addr_len = sizeof(remote_addr);
371 getpeername(sock, &remote_addr, &addr_len);
372 addr_len = sizeof(local_addr);
373 getsockname(sock, &local_addr, &addr_len);
375 setup_kaddr(&remote_kaddr, &remote_addr);
376 setup_kaddr(&local_kaddr, &local_addr);
378 ret = krb5_auth_con_setaddrs(context, auth_context, &local_kaddr, NULL);
379 if (ret) {
380 close(sock);
381 free(ap_req.data);
382 krb5_auth_con_free(context, auth_context);
383 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n", error_message(ret)));
384 return ADS_ERROR_KRB5(ret);
387 ret = build_kpasswd_request(pversion, context, auth_context, &ap_req,
388 princ, newpw, &chpw_req);
389 if (ret) {
390 close(sock);
391 free(ap_req.data);
392 krb5_auth_con_free(context, auth_context);
393 DEBUG(1,("build_setpw_request failed (%s)\n", error_message(ret)));
394 return ADS_ERROR_KRB5(ret);
397 if (write(sock, chpw_req.data, chpw_req.length) != chpw_req.length) {
398 close(sock);
399 free(chpw_req.data);
400 free(ap_req.data);
401 krb5_auth_con_free(context, auth_context);
402 DEBUG(1,("send of chpw failed (%s)\n", strerror(errno)));
403 return ADS_ERROR_SYSTEM(errno);
406 free(chpw_req.data);
408 chpw_rep.length = 1500;
409 chpw_rep.data = (char *) malloc(chpw_rep.length);
410 if (!chpw_rep.data) {
411 close(sock);
412 free(ap_req.data);
413 krb5_auth_con_free(context, auth_context);
414 DEBUG(1,("send of chpw failed (%s)\n", strerror(errno)));
415 errno = ENOMEM;
416 return ADS_ERROR_SYSTEM(errno);
419 ret = read(sock, chpw_rep.data, chpw_rep.length);
420 if (ret < 0) {
421 close(sock);
422 free(chpw_rep.data);
423 free(ap_req.data);
424 krb5_auth_con_free(context, auth_context);
425 DEBUG(1,("recv of chpw reply failed (%s)\n", strerror(errno)));
426 return ADS_ERROR_SYSTEM(errno);
429 close(sock);
430 chpw_rep.length = ret;
432 ret = krb5_auth_con_setaddrs(context, auth_context, NULL,&remote_kaddr);
433 if (ret) {
434 free(chpw_rep.data);
435 free(ap_req.data);
436 krb5_auth_con_free(context, auth_context);
437 DEBUG(1,("krb5_auth_con_setaddrs on reply failed (%s)\n",
438 error_message(ret)));
439 return ADS_ERROR_KRB5(ret);
442 ret = parse_setpw_reply(context, auth_context, &chpw_rep);
443 free(chpw_rep.data);
445 if (ret) {
446 free(ap_req.data);
447 krb5_auth_con_free(context, auth_context);
448 DEBUG(1,("parse_setpw_reply failed (%s)\n",
449 error_message(ret)));
450 return ADS_ERROR_KRB5(ret);
453 free(ap_req.data);
454 krb5_auth_con_free(context, auth_context);
456 return ADS_SUCCESS;
459 ADS_STATUS ads_krb5_set_password(const char *kdc_host, const char *princ,
460 const char *newpw, int time_offset)
463 ADS_STATUS aret;
464 krb5_error_code ret;
465 krb5_context context;
466 krb5_principal principal;
467 char *princ_name;
468 char *realm;
469 krb5_creds creds, *credsp;
470 krb5_ccache ccache;
472 ret = krb5_init_context(&context);
473 if (ret) {
474 DEBUG(1,("Failed to init krb5 context (%s)\n", error_message(ret)));
475 return ADS_ERROR_KRB5(ret);
478 if (time_offset != 0) {
479 krb5_set_real_time(context, time(NULL) + time_offset, 0);
482 ret = krb5_cc_default(context, &ccache);
483 if (ret) {
484 krb5_free_context(context);
485 DEBUG(1,("Failed to get default creds (%s)\n", error_message(ret)));
486 return ADS_ERROR_KRB5(ret);
489 ZERO_STRUCT(creds);
491 realm = strchr(princ, '@');
492 realm++;
494 asprintf(&princ_name, "kadmin/changepw@%s", realm);
495 ret = krb5_parse_name(context, princ_name, &creds.server);
496 if (ret) {
497 krb5_free_context(context);
498 DEBUG(1,("Failed to parse kadmin/changepw (%s)\n", error_message(ret)));
499 return ADS_ERROR_KRB5(ret);
501 free(princ_name);
503 /* parse the principal we got as a function argument */
504 ret = krb5_parse_name(context, princ, &principal);
505 if (ret) {
506 krb5_free_context(context);
507 DEBUG(1,("Failed to parse %s (%s)\n", princ_name, error_message(ret)));
508 return ADS_ERROR_KRB5(ret);
511 krb5_princ_set_realm(context, creds.server,
512 krb5_princ_realm(context, principal));
514 ret = krb5_cc_get_principal(context, ccache, &creds.client);
515 if (ret) {
516 krb5_free_principal(context, principal);
517 krb5_free_context(context);
518 DEBUG(1,("Failed to get principal from ccache (%s)\n",
519 error_message(ret)));
520 return ADS_ERROR_KRB5(ret);
523 ret = krb5_get_credentials(context, 0, ccache, &creds, &credsp);
524 if (ret) {
525 krb5_free_principal(context, creds.client);
526 krb5_free_principal(context, principal);
527 krb5_free_context(context);
528 DEBUG(1,("krb5_get_credentials failed (%s)\n", error_message(ret)));
529 return ADS_ERROR_KRB5(ret);
532 /* we might have to call krb5_free_creds(...) from now on ... */
534 aret = do_krb5_kpasswd_request(context, kdc_host,
535 KRB5_KPASSWD_VERS_SETPW_MS,
536 credsp, princ, newpw);
538 krb5_free_creds(context, credsp);
539 krb5_free_principal(context, creds.client);
540 krb5_free_principal(context, principal);
541 krb5_free_context(context);
543 return aret;
547 we use a prompter to avoid a crash bug in the kerberos libs when
548 dealing with empty passwords
549 this prompter is just a string copy ...
551 static krb5_error_code
552 kerb_prompter(krb5_context ctx, void *data,
553 const char *name,
554 const char *banner,
555 int num_prompts,
556 krb5_prompt prompts[])
558 if (num_prompts == 0) return 0;
560 memset(prompts[0].reply->data, 0, prompts[0].reply->length);
561 if (prompts[0].reply->length > 0) {
562 if (data) {
563 strncpy(prompts[0].reply->data, data, prompts[0].reply->length-1);
564 prompts[0].reply->length = strlen(prompts[0].reply->data);
565 } else {
566 prompts[0].reply->length = 0;
569 return 0;
572 static ADS_STATUS ads_krb5_chg_password(const char *kdc_host,
573 const char *principal,
574 const char *oldpw,
575 const char *newpw,
576 int time_offset)
578 ADS_STATUS aret;
579 krb5_error_code ret;
580 krb5_context context;
581 krb5_principal princ;
582 krb5_get_init_creds_opt opts;
583 krb5_creds creds;
584 char *chpw_princ = NULL, *password;
586 ret = krb5_init_context(&context);
587 if (ret) {
588 DEBUG(1,("Failed to init krb5 context (%s)\n", error_message(ret)));
589 return ADS_ERROR_KRB5(ret);
592 if ((ret = krb5_parse_name(context, principal,
593 &princ))) {
594 krb5_free_context(context);
595 DEBUG(1,("Failed to parse %s (%s)\n", principal, error_message(ret)));
596 return ADS_ERROR_KRB5(ret);
599 krb5_get_init_creds_opt_init(&opts);
600 krb5_get_init_creds_opt_set_tkt_life(&opts, 5*60);
601 krb5_get_init_creds_opt_set_renew_life(&opts, 0);
602 krb5_get_init_creds_opt_set_forwardable(&opts, 0);
603 krb5_get_init_creds_opt_set_proxiable(&opts, 0);
605 /* We have to obtain an INITIAL changepw ticket for changing password */
606 asprintf(&chpw_princ, "kadmin/changepw@%s",
607 (char *) krb5_princ_realm(context, princ));
608 password = strdup(oldpw);
609 ret = krb5_get_init_creds_password(context, &creds, princ, password,
610 kerb_prompter, NULL,
611 0, chpw_princ, &opts);
612 SAFE_FREE(chpw_princ);
613 SAFE_FREE(password);
615 if (ret) {
616 if (ret == KRB5KRB_AP_ERR_BAD_INTEGRITY)
617 DEBUG(1,("Password incorrect while getting initial ticket"));
618 else
619 DEBUG(1,("krb5_get_init_creds_password failed (%s)\n", error_message(ret)));
621 krb5_free_principal(context, princ);
622 krb5_free_context(context);
623 return ADS_ERROR_KRB5(ret);
626 aret = do_krb5_kpasswd_request(context, kdc_host,
627 KRB5_KPASSWD_VERS_CHANGEPW,
628 &creds, principal, newpw);
630 krb5_free_principal(context, princ);
631 krb5_free_context(context);
633 return aret;
637 ADS_STATUS kerberos_set_password(const char *kpasswd_server,
638 const char *auth_principal, const char *auth_password,
639 const char *target_principal, const char *new_password,
640 int time_offset)
642 int ret;
644 if ((ret = kerberos_kinit_password(auth_principal, auth_password, time_offset))) {
645 DEBUG(1,("Failed kinit for principal %s (%s)\n", auth_principal, error_message(ret)));
646 return ADS_ERROR_KRB5(ret);
649 if (!strcmp(auth_principal, target_principal))
650 return ads_krb5_chg_password(kpasswd_server, target_principal,
651 auth_password, new_password, time_offset);
652 else
653 return ads_krb5_set_password(kpasswd_server, target_principal,
654 new_password, time_offset);
659 * Set the machine account password
660 * @param ads connection to ads server
661 * @param hostname machine whose password is being set
662 * @param password new password
663 * @return status of password change
665 ADS_STATUS ads_set_machine_password(ADS_STRUCT *ads,
666 const char *machine_account,
667 const char *password)
669 ADS_STATUS status;
670 char *principal = NULL;
673 we need to use the '$' form of the name here (the machine account name),
674 as otherwise the server might end up setting the password for a user
675 instead
677 asprintf(&principal, "%s@%s", machine_account, ads->config.realm);
679 status = ads_krb5_set_password(ads->auth.kdc_server, principal,
680 password, ads->auth.time_offset);
682 free(principal);
684 return status;
689 #endif