2 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <krb5_locl.h>
36 RCSID("$Id: changepw.c 21505 2007-07-12 12:28:38Z lha $");
39 str2data (krb5_data *d,
41 ...) __attribute__ ((format (printf, 2, 3)));
44 str2data (krb5_data *d,
52 d->length = vasprintf (&str, fmt, args);
58 * Change password protocol defined by
59 * draft-ietf-cat-kerb-chg-password-02.txt
61 * Share the response part of the protocol with MS set password
65 static krb5_error_code
66 chgpw_send_request (krb5_context context,
67 krb5_auth_context *auth_context,
69 krb5_principal targprinc,
76 krb5_data ap_req_data;
77 krb5_data krb_priv_data;
78 krb5_data passwd_data;
86 return KRB5_KPASSWD_MALFORMED;
89 krb5_principal_compare(context, creds->client, targprinc) != TRUE)
90 return KRB5_KPASSWD_MALFORMED;
92 krb5_data_zero (&ap_req_data);
94 ret = krb5_mk_req_extended (context,
96 AP_OPTS_MUTUAL_REQUIRED | AP_OPTS_USE_SUBKEY,
103 passwd_data.data = rk_UNCONST(passwd);
104 passwd_data.length = strlen(passwd);
106 krb5_data_zero (&krb_priv_data);
108 ret = krb5_mk_priv (context,
116 len = 6 + ap_req_data.length + krb_priv_data.length;
118 *p++ = (len >> 8) & 0xFF;
119 *p++ = (len >> 0) & 0xFF;
122 *p++ = (ap_req_data.length >> 8) & 0xFF;
123 *p++ = (ap_req_data.length >> 0) & 0xFF;
125 memset(&msghdr, 0, sizeof(msghdr));
126 msghdr.msg_name = NULL;
127 msghdr.msg_namelen = 0;
128 msghdr.msg_iov = iov;
129 msghdr.msg_iovlen = sizeof(iov)/sizeof(*iov);
131 msghdr.msg_control = NULL;
132 msghdr.msg_controllen = 0;
135 iov[0].iov_base = (void*)header;
137 iov[1].iov_base = ap_req_data.data;
138 iov[1].iov_len = ap_req_data.length;
139 iov[2].iov_base = krb_priv_data.data;
140 iov[2].iov_len = krb_priv_data.length;
142 if (sendmsg (sock, &msghdr, 0) < 0) {
144 krb5_set_error_string(context, "sendmsg %s: %s", host, strerror(ret));
147 krb5_data_free (&krb_priv_data);
149 krb5_data_free (&ap_req_data);
154 * Set password protocol as defined by RFC3244 --
155 * Microsoft Windows 2000 Kerberos Change Password and Set Password Protocols
158 static krb5_error_code
159 setpw_send_request (krb5_context context,
160 krb5_auth_context *auth_context,
162 krb5_principal targprinc,
169 krb5_data ap_req_data;
170 krb5_data krb_priv_data;
172 ChangePasswdDataMS chpw;
174 u_char header[4 + 6];
177 struct msghdr msghdr;
179 krb5_data_zero (&ap_req_data);
181 ret = krb5_mk_req_extended (context,
183 AP_OPTS_MUTUAL_REQUIRED | AP_OPTS_USE_SUBKEY,
190 chpw.newpasswd.length = strlen(passwd);
191 chpw.newpasswd.data = rk_UNCONST(passwd);
193 chpw.targname = &targprinc->name;
194 chpw.targrealm = &targprinc->realm;
196 chpw.targname = NULL;
197 chpw.targrealm = NULL;
200 ASN1_MALLOC_ENCODE(ChangePasswdDataMS, pwd_data.data, pwd_data.length,
203 krb5_data_free (&ap_req_data);
207 if(pwd_data.length != len)
208 krb5_abortx(context, "internal error in ASN.1 encoder");
210 ret = krb5_mk_priv (context,
218 len = 6 + ap_req_data.length + krb_priv_data.length;
221 _krb5_put_int(p, len, 4);
224 *p++ = (len >> 8) & 0xFF;
225 *p++ = (len >> 0) & 0xFF;
228 *p++ = (ap_req_data.length >> 8) & 0xFF;
229 *p++ = (ap_req_data.length >> 0) & 0xFF;
231 memset(&msghdr, 0, sizeof(msghdr));
232 msghdr.msg_name = NULL;
233 msghdr.msg_namelen = 0;
234 msghdr.msg_iov = iov;
235 msghdr.msg_iovlen = sizeof(iov)/sizeof(*iov);
237 msghdr.msg_control = NULL;
238 msghdr.msg_controllen = 0;
241 iov[0].iov_base = (void*)header;
246 iov[1].iov_base = ap_req_data.data;
247 iov[1].iov_len = ap_req_data.length;
248 iov[2].iov_base = krb_priv_data.data;
249 iov[2].iov_len = krb_priv_data.length;
251 if (sendmsg (sock, &msghdr, 0) < 0) {
253 krb5_set_error_string(context, "sendmsg %s: %s", host, strerror(ret));
256 krb5_data_free (&krb_priv_data);
258 krb5_data_free (&ap_req_data);
259 krb5_data_free (&pwd_data);
263 static krb5_error_code
264 process_reply (krb5_context context,
265 krb5_auth_context auth_context,
269 krb5_data *result_code_string,
270 krb5_data *result_string,
274 u_char reply[1024 * 3];
276 uint16_t pkt_len, pkt_ver;
277 krb5_data ap_rep_data;
282 while (len < sizeof(reply)) {
285 ret = recvfrom (sock, reply + len, sizeof(reply) - len,
289 krb5_set_error_string(context, "recvfrom %s: %s",
290 host, strerror(save_errno));
292 } else if (ret == 0) {
293 krb5_set_error_string(context, "recvfrom timeout %s", host);
299 _krb5_get_int(reply, &size, 4);
302 memmove(reply, reply + 4, size);
306 if (len == sizeof(reply)) {
307 krb5_set_error_string(context, "message too large from %s",
312 ret = recvfrom (sock, reply, sizeof(reply), 0, NULL, NULL);
315 krb5_set_error_string(context, "recvfrom %s: %s",
316 host, strerror(save_errno));
323 str2data (result_string, "server %s sent to too short message "
324 "(%ld bytes)", host, (long)len);
325 *result_code = KRB5_KPASSWD_MALFORMED;
329 pkt_len = (reply[0] << 8) | (reply[1]);
330 pkt_ver = (reply[2] << 8) | (reply[3]);
332 if ((pkt_len != len) || (reply[1] == 0x7e || reply[1] == 0x5e)) {
337 memset(&error, 0, sizeof(error));
339 ret = decode_KRB_ERROR(reply, len, &error, &size);
343 if (error.e_data->length < 2) {
344 str2data(result_string, "server %s sent too short "
345 "e_data to print anything usable", host);
346 free_KRB_ERROR(&error);
347 *result_code = KRB5_KPASSWD_MALFORMED;
351 p = error.e_data->data;
352 *result_code = (p[0] << 8) | p[1];
353 if (error.e_data->length == 2)
354 str2data(result_string, "server only sent error code");
356 krb5_data_copy (result_string,
358 error.e_data->length - 2);
359 free_KRB_ERROR(&error);
363 if (pkt_len != len) {
364 str2data (result_string, "client: wrong len in reply");
365 *result_code = KRB5_KPASSWD_MALFORMED;
368 if (pkt_ver != KRB5_KPASSWD_VERS_CHANGEPW) {
369 str2data (result_string,
370 "client: wrong version number (%d)", pkt_ver);
371 *result_code = KRB5_KPASSWD_MALFORMED;
375 ap_rep_data.data = reply + 6;
376 ap_rep_data.length = (reply[4] << 8) | (reply[5]);
378 if (reply + len < (u_char *)ap_rep_data.data + ap_rep_data.length) {
379 str2data (result_string, "client: wrong AP len in reply");
380 *result_code = KRB5_KPASSWD_MALFORMED;
384 if (ap_rep_data.length) {
385 krb5_ap_rep_enc_part *ap_rep;
389 priv_data.data = (u_char*)ap_rep_data.data + ap_rep_data.length;
390 priv_data.length = len - ap_rep_data.length - 6;
392 ret = krb5_rd_rep (context,
399 krb5_free_ap_rep_enc_part (context, ap_rep);
401 ret = krb5_rd_priv (context,
407 krb5_data_free (result_code_string);
411 if (result_code_string->length < 2) {
412 *result_code = KRB5_KPASSWD_MALFORMED;
413 str2data (result_string,
414 "client: bad length in result");
418 p = result_code_string->data;
420 *result_code = (p[0] << 8) | p[1];
421 krb5_data_copy (result_string,
422 (unsigned char*)result_code_string->data + 2,
423 result_code_string->length - 2);
430 ret = decode_KRB_ERROR(reply + 6, len - 6, &error, &size);
434 if (error.e_data->length < 2) {
435 krb5_warnx (context, "too short e_data to print anything usable");
439 p = error.e_data->data;
440 *result_code = (p[0] << 8) | p[1];
441 krb5_data_copy (result_string,
443 error.e_data->length - 2);
450 * change the password using the credentials in `creds' (for the
451 * principal indicated in them) to `newpw', storing the result of
452 * the operation in `result_*' and an error code or 0.
455 typedef krb5_error_code (*kpwd_send_request) (krb5_context,
463 typedef krb5_error_code (*kpwd_process_reply) (krb5_context,
472 static struct kpwd_proc {
475 #define SUPPORT_TCP 1
476 #define SUPPORT_UDP 2
477 kpwd_send_request send_req;
478 kpwd_process_reply process_rep;
482 SUPPORT_TCP|SUPPORT_UDP,
495 static struct kpwd_proc *
496 find_chpw_proto(const char *name)
499 for (p = procs; p->name != NULL; p++) {
500 if (strcmp(p->name, name) == 0)
510 static krb5_error_code
511 change_password_loop (krb5_context context,
513 krb5_principal targprinc,
516 krb5_data *result_code_string,
517 krb5_data *result_string,
518 struct kpwd_proc *proc)
521 krb5_auth_context auth_context = NULL;
522 krb5_krbhst_handle handle = NULL;
523 krb5_krbhst_info *hi;
530 realm = targprinc->realm;
532 realm = creds->client->realm;
534 ret = krb5_auth_con_init (context, &auth_context);
538 krb5_auth_con_setflags (context, auth_context,
539 KRB5_AUTH_CONTEXT_DO_SEQUENCE);
541 ret = krb5_krbhst_init (context, realm, KRB5_KRBHST_CHANGEPW, &handle);
545 while (!done && (ret = krb5_krbhst_next(context, handle, &hi)) == 0) {
546 struct addrinfo *ai, *a;
550 case KRB5_KRBHST_UDP:
551 if ((proc->flags & SUPPORT_UDP) == 0)
555 case KRB5_KRBHST_TCP:
556 if ((proc->flags & SUPPORT_TCP) == 0)
564 ret = krb5_krbhst_get_addrinfo(context, hi, &ai);
568 for (a = ai; !done && a != NULL; a = a->ai_next) {
571 sock = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
575 ret = connect(sock, a->ai_addr, a->ai_addrlen);
581 ret = krb5_auth_con_genaddrs (context, auth_context, sock,
582 KRB5_AUTH_CONTEXT_GENERATE_LOCAL_ADDR);
588 for (i = 0; !done && i < 5; ++i) {
595 ret = (*proc->send_req) (context,
609 if (sock >= FD_SETSIZE) {
610 krb5_set_error_string(context, "fd %d too large", sock);
617 FD_SET(sock, &fdset);
619 tv.tv_sec = 1 + (1 << i);
621 ret = select (sock + 1, &fdset, NULL, NULL, &tv);
622 if (ret < 0 && errno != EINTR) {
627 ret = (*proc->process_rep) (context,
637 else if (i > 0 && ret == KRB5KRB_AP_ERR_MUT_FAIL)
640 ret = KRB5_KDC_UNREACH;
648 krb5_krbhst_free (context, handle);
649 krb5_auth_con_free (context, auth_context);
653 if (ret == KRB5_KDC_UNREACH) {
654 krb5_set_error_string(context,
655 "unable to reach any changepw server "
656 " in realm %s", realm);
657 *result_code = KRB5_KPASSWD_HARDERROR;
665 * change the password using the credentials in `creds' (for the
666 * principal indicated in them) to `newpw', storing the result of
667 * the operation in `result_*' and an error code or 0.
670 krb5_error_code KRB5_LIB_FUNCTION
671 krb5_change_password (krb5_context context,
675 krb5_data *result_code_string,
676 krb5_data *result_string)
678 struct kpwd_proc *p = find_chpw_proto("change password");
680 *result_code = KRB5_KPASSWD_MALFORMED;
681 result_code_string->data = result_string->data = NULL;
682 result_code_string->length = result_string->length = 0;
685 return KRB5_KPASSWD_MALFORMED;
687 return change_password_loop(context, creds, NULL, newpw,
688 result_code, result_code_string,
696 krb5_error_code KRB5_LIB_FUNCTION
697 krb5_set_password(krb5_context context,
700 krb5_principal targprinc,
702 krb5_data *result_code_string,
703 krb5_data *result_string)
705 krb5_principal principal = NULL;
706 krb5_error_code ret = 0;
709 *result_code = KRB5_KPASSWD_MALFORMED;
710 result_code_string->data = result_string->data = NULL;
711 result_code_string->length = result_string->length = 0;
713 if (targprinc == NULL) {
714 ret = krb5_get_default_principal(context, &principal);
718 principal = targprinc;
720 for (i = 0; procs[i].name != NULL; i++) {
722 ret = change_password_loop(context, creds, principal, newpw,
723 result_code, result_code_string,
726 if (ret == 0 && *result_code == 0)
730 if (targprinc == NULL)
731 krb5_free_principal(context, principal);
739 krb5_error_code KRB5_LIB_FUNCTION
740 krb5_set_password_using_ccache(krb5_context context,
743 krb5_principal targprinc,
745 krb5_data *result_code_string,
746 krb5_data *result_string)
748 krb5_creds creds, *credsp;
750 krb5_principal principal = NULL;
752 *result_code = KRB5_KPASSWD_MALFORMED;
753 result_code_string->data = result_string->data = NULL;
754 result_code_string->length = result_string->length = 0;
756 memset(&creds, 0, sizeof(creds));
758 if (targprinc == NULL) {
759 ret = krb5_cc_get_principal(context, ccache, &principal);
763 principal = targprinc;
765 ret = krb5_make_principal(context, &creds.server,
766 krb5_principal_get_realm(context, principal),
767 "kadmin", "changepw", NULL);
771 ret = krb5_cc_get_principal(context, ccache, &creds.client);
773 krb5_free_principal(context, creds.server);
777 ret = krb5_get_credentials(context, 0, ccache, &creds, &credsp);
778 krb5_free_principal(context, creds.server);
779 krb5_free_principal(context, creds.client);
783 ret = krb5_set_password(context,
791 krb5_free_creds(context, credsp);
795 if (targprinc == NULL)
796 krb5_free_principal(context, principal);
804 const char* KRB5_LIB_FUNCTION
805 krb5_passwd_result_to_string (krb5_context context,
808 static const char *strings[] = {
816 "Initial flag needed"
819 if (result < 0 || result > KRB5_KPASSWD_INITIAL_FLAG_NEEDED)
820 return "unknown result code";
822 return strings[result];