2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
8 * lib/krb5/os/changepw.c
10 * Copyright 1990,1999 by the Massachusetts Institute of Technology.
11 * All Rights Reserved.
13 * Export of this software from the United States of America may
14 * require a specific license from the United States Government.
15 * It is the responsibility of any person or organization contemplating
16 * export to obtain such a license before exporting.
18 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
19 * distribute this software and its documentation for any purpose and
20 * without fee is hereby granted, provided that the above copyright
21 * notice appear in all copies and that both that copyright notice and
22 * this permission notice appear in supporting documentation, and that
23 * the name of M.I.T. not be used in advertising or publicity pertaining
24 * to distribution of the software without specific, written prior
25 * permission. Furthermore if you modify this software you must label
26 * your software as modified software and not distribute it in such a
27 * fashion that it might be confused with the original M.I.T. software.
28 * M.I.T. makes no representations about the suitability of
29 * this software for any purpose. It is provided "as is" without express
30 * or implied warranty.
36 #include <kadm5/admin.h>
37 #include <client_internal.h>
38 #include <gssapi/gssapi.h>
39 #include <gssapi_krb5.h>
40 #include <gssapiP_krb5.h>
43 /* #include "adm_err.h" */
47 extern krb5_error_code
krb5int_mk_chpw_req(krb5_context context
,
48 krb5_auth_context auth_context
,
49 krb5_data
*ap_req
, char *passwd
,
52 extern krb5_error_code
krb5int_rd_chpw_rep(krb5_context context
,
53 krb5_auth_context auth_context
,
54 krb5_data
*packet
, int *result_code
,
55 krb5_data
*result_data
);
58 * _kadm5_get_kpasswd_protocol
60 * returns the password change protocol value to the caller.
61 * Since the 'handle' is an opaque value to higher up callers,
62 * this method is needed to provide a way for them to get a peek
63 * at the protocol being used without having to expose the entire
67 _kadm5_get_kpasswd_protocol(void *handle
)
69 kadm5_server_handle_t srvrhdl
= (kadm5_server_handle_t
)handle
;
71 return (srvrhdl
->params
.kpasswd_protocol
);
75 * krb5_change_password
77 * Prepare and send a CHANGEPW request to a password server
78 * using UDP datagrams. This is only used for sending to
79 * non-SEAM servers which support the Marc Horowitz defined
80 * protocol (1998) for password changing.
82 * SUNW14resync - added _local as it conflicts with one in krb5.h
84 static krb5_error_code
85 krb5_change_password_local(context
, params
, creds
, newpw
, srvr_rsp_code
,
88 kadm5_config_params
*params
;
91 kadm5_ret_t
*srvr_rsp_code
;
94 krb5_auth_context auth_context
;
95 krb5_data ap_req
, chpw_req
, chpw_rep
;
96 krb5_address local_kaddr
, remote_kaddr
;
97 krb5_error_code code
= 0;
99 struct sockaddr
*addr_p
, local_addr
, remote_addr
, tmp_addr
;
100 struct sockaddr_in
*sin_p
;
103 int cc
, local_result_code
, tmp_len
;
104 SOCKET s1
= INVALID_SOCKET
;
105 SOCKET s2
= INVALID_SOCKET
;
108 /* Initialize values so that cleanup call can safely check for NULL */
111 memset(&chpw_req
, 0, sizeof (krb5_data
));
112 memset(&chpw_rep
, 0, sizeof (krb5_data
));
113 memset(&ap_req
, 0, sizeof (krb5_data
));
115 /* initialize auth_context so that we know we have to free it */
116 if ((code
= krb5_auth_con_init(context
, &auth_context
)))
119 if (code
= krb5_mk_req_extended(context
, &auth_context
,
121 NULL
, creds
, &ap_req
))
125 * find the address of the kpasswd_server.
127 addr_p
= (struct sockaddr
*)malloc(sizeof (struct sockaddr
));
130 memset(addr_p
, 0, sizeof (struct sockaddr
));
131 if ((hp
= gethostbyname(params
->kpasswd_server
)) == NULL
) {
132 code
= KRB5_REALM_CANT_RESOLVE
;
135 sin_p
= (struct sockaddr_in
*)addr_p
;
136 memset((char *)sin_p
, 0, sizeof (struct sockaddr
));
137 sin_p
->sin_family
= hp
->h_addrtype
;
138 sin_p
->sin_port
= htons(params
->kpasswd_port
);
139 memcpy((char *)&sin_p
->sin_addr
, (char *)hp
->h_addr
, hp
->h_length
);
144 * this is really obscure. s1 is used for all communications. it
145 * is left unconnected in case the server is multihomed and routes
146 * are asymmetric. s2 is connected to resolve routes and get
147 * addresses. this is the *only* way to get proper addresses for
148 * multihomed hosts if routing is asymmetric.
150 * A related problem in the server, but not the client, is that
151 * many os's have no way to disconnect a connected udp socket, so
152 * the s2 socket needs to be closed and recreated for each
153 * request. The s1 socket must not be closed, or else queued
154 * requests will be lost.
156 * A "naive" client implementation (one socket, no connect,
157 * hostname resolution to get the local ip addr) will work and
158 * interoperate if the client is single-homed.
161 if ((s1
= socket(AF_INET
, SOCK_DGRAM
, 0)) == INVALID_SOCKET
)
167 if ((s2
= socket(AF_INET
, SOCK_DGRAM
, 0)) == INVALID_SOCKET
)
173 for (i
= 0; i
< naddr_p
; i
++)
176 struct timeval timeout
;
178 if (connect(s2
, &addr_p
[i
], sizeof (addr_p
[i
])) ==
181 if ((errno
== ECONNREFUSED
) ||
182 (errno
== EHOSTUNREACH
))
183 continue; /* try the next addr */
189 addrlen
= sizeof (local_addr
);
191 if (getsockname(s2
, &local_addr
, &addrlen
) < 0)
193 if ((errno
== ECONNREFUSED
) ||
194 (errno
== EHOSTUNREACH
))
195 continue; /* try the next addr */
202 * some brain-dead OS's don't return useful information from
203 * the getsockname call. Namely, windows and solaris.
205 if (((struct sockaddr_in
*)&local_addr
)->sin_addr
.s_addr
!= 0)
207 local_kaddr
.addrtype
= ADDRTYPE_INET
;
208 local_kaddr
.length
= sizeof (((struct sockaddr_in
*)
209 &local_addr
)->sin_addr
);
210 local_kaddr
.contents
= (krb5_octet
*)
211 &(((struct sockaddr_in
*)
212 &local_addr
)->sin_addr
);
216 krb5_address
**addrs
;
218 krb5_os_localaddr(context
, &addrs
);
220 local_kaddr
.magic
= addrs
[0]->magic
;
221 local_kaddr
.addrtype
= addrs
[0]->addrtype
;
222 local_kaddr
.length
= addrs
[0]->length
;
223 local_kaddr
.contents
= malloc(addrs
[0]->length
);
224 memcpy(local_kaddr
.contents
, addrs
[0]->contents
,
227 krb5_free_addresses(context
, addrs
);
230 addrlen
= sizeof (remote_addr
);
231 if (getpeername(s2
, &remote_addr
, &addrlen
) < 0)
233 if ((errno
== ECONNREFUSED
) ||
234 (errno
== EHOSTUNREACH
))
235 continue; /* try the next addr */
241 remote_kaddr
.addrtype
= ADDRTYPE_INET
;
242 remote_kaddr
.length
= sizeof (((struct sockaddr_in
*)
243 &remote_addr
)->sin_addr
);
244 remote_kaddr
.contents
= (krb5_octet
*)
245 &(((struct sockaddr_in
*)&remote_addr
)->sin_addr
);
248 * mk_priv requires that the local address be set.
249 * getsockname is used for this. rd_priv requires that the
250 * remote address be set. recvfrom is used for this. If
251 * rd_priv is given a local address, and the message has the
252 * recipient addr in it, this will be checked. However, there
253 * is simply no way to know ahead of time what address the
254 * message will be delivered *to*. Therefore, it is important
255 * that either no recipient address is in the messages when
256 * mk_priv is called, or that no local address is passed to
257 * rd_priv. Both is a better idea, and I have done that. In
258 * summary, when mk_priv is called, *only* a local address is
259 * specified. when rd_priv is called, *only* a remote address
260 * is specified. Are we having fun yet?
263 if (code
= krb5_auth_con_setaddrs(context
, auth_context
,
270 if (code
= krb5int_mk_chpw_req(context
, auth_context
,
271 &ap_req
, newpw
, &chpw_req
))
277 if ((cc
= sendto(s1
, chpw_req
.data
, chpw_req
.length
, 0,
278 (struct sockaddr
*)&addr_p
[i
],
279 sizeof (addr_p
[i
]))) != chpw_req
.length
)
281 if ((cc
< 0) && ((errno
== ECONNREFUSED
) ||
282 (errno
== EHOSTUNREACH
)))
283 continue; /* try the next addr */
285 code
= (cc
< 0) ? errno
: ECONNABORTED
;
289 chpw_rep
.length
= 1500;
290 chpw_rep
.data
= (char *)malloc(chpw_rep
.length
);
292 /* XXX need a timeout/retry loop here */
295 timeout
.tv_sec
= 120;
297 switch (select(s1
+ 1, &fdset
, 0, 0, &timeout
)) {
309 tmp_len
= sizeof (tmp_addr
);
310 if ((cc
= recvfrom(s1
, chpw_rep
.data
, chpw_rep
.length
,
311 0, &tmp_addr
, &tmp_len
)) < 0)
322 chpw_rep
.length
= cc
;
324 if (code
= krb5_auth_con_setaddrs(context
, auth_context
,
325 NULL
, &remote_kaddr
))
328 if (code
= krb5int_rd_chpw_rep(context
, auth_context
, &chpw_rep
,
329 &local_result_code
, srvr_msg
))
333 *srvr_rsp_code
= local_result_code
;
342 if (auth_context
!= NULL
)
343 krb5_auth_con_free(context
, auth_context
);
348 if (s1
!= INVALID_SOCKET
)
351 if (s2
!= INVALID_SOCKET
)
354 krb5_xfree(chpw_req
.data
);
355 krb5_xfree(chpw_rep
.data
);
356 krb5_xfree(ap_req
.data
);
363 * kadm5_chpass_principal_v2
365 * New function used to prepare to make the change password request to a
366 * non-SEAM admin server. The protocol used in this case is not based on
367 * RPCSEC_GSS, it simply makes the request to port 464 (udp and tcp).
368 * This is the same way that MIT KRB5 1.2.1 changes passwords.
371 kadm5_chpass_principal_v2(void *server_handle
,
372 krb5_principal princ
,
374 kadm5_ret_t
*srvr_rsp_code
,
378 kadm5_server_handle_t handle
= (kadm5_server_handle_t
)server_handle
;
379 krb5_error_code result
;
384 char *cpw_service
= NULL
;
387 * The credentials have already been stored in the cache in the
388 * initialization step earlier, but we dont have direct access to it
389 * at this level. Derive the cache and fetch the credentials to use for
390 * sending the request.
392 memset(&mcreds
, 0, sizeof (krb5_creds
));
393 if ((code
= krb5_cc_resolve(handle
->context
, handle
->cache_name
,
397 /* set the client principal in the credential match structure */
398 mcreds
.client
= princ
;
401 * set the server principal (kadmin/changepw@REALM) in the credential
404 cpwlen
= strlen(KADM5_CHANGEPW_SERVICE
) +
405 strlen(handle
->params
.realm
) + 2;
406 cpw_service
= malloc(cpwlen
);
407 if (cpw_service
== NULL
) {
411 snprintf(cpw_service
, cpwlen
, "%s@%s",
412 KADM5_CHANGEPW_SERVICE
, handle
->params
.realm
);
414 /* generate the server principal from the name string we generated */
415 if ((code
= krb5_parse_name(handle
->context
, cpw_service
,
421 /* Find the credentials in the cache */
422 if ((code
= krb5_cc_retrieve_cred(handle
->context
, ccache
, 0, &mcreds
,
428 /* Now we have all we need to make the change request. */
429 result
= krb5_change_password_local(handle
->context
, &handle
->params
,