]>
Commit | Line | Data |
---|---|---|
d7051053 | 1 | #define LDAP_AUTH_OTHERKIND 0x86L |
2 | #define LDAP_AUTH_NEGOTIATE (LDAP_AUTH_OTHERKIND | 0x0400) | |
cd9e6b16 | 3 | /*-- |
4 | ||
5 | THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF | |
6 | ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED | |
7 | TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A | |
8 | PARTICULAR PURPOSE. | |
9 | ||
10 | Copyright (C) 1999 Microsoft Corporation. All rights reserved. | |
11 | ||
12 | Module Name: | |
13 | ||
f78c7eaf | 14 | setpw.c |
cd9e6b16 | 15 | |
16 | Abstract: | |
17 | ||
18 | Set a user's password using the | |
19 | Kerberos Change Password Protocol (I-D) variant for Windows 2000 | |
20 | ||
21 | --*/ | |
22 | /* | |
23 | * lib/krb5/os/changepw.c | |
24 | * | |
25 | * Copyright 1990 by the Massachusetts Institute of Technology. | |
26 | * All Rights Reserved. | |
27 | * | |
28 | * Export of this software from the United States of America may | |
29 | * require a specific license from the United States Government. | |
30 | * It is the responsibility of any person or organization contemplating | |
31 | * export to obtain such a license before exporting. | |
32 | * | |
33 | * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and | |
34 | * distribute this software and its documentation for any purpose and | |
35 | * without fee is hereby granted, provided that the above copyright | |
36 | * notice appear in all copies and that both that copyright notice and | |
37 | * this permission notice appear in supporting documentation, and that | |
38 | * the name of M.I.T. not be used in advertising or publicity pertaining | |
39 | * to distribution of the software without specific, written prior | |
40 | * permission. M.I.T. makes no representations about the suitability of | |
41 | * this software for any purpose. It is provided "as is" without express | |
42 | * or implied warranty. | |
43 | * | |
44 | */ | |
45 | ||
f78c7eaf | 46 | |
cd9e6b16 | 47 | #define NEED_SOCKETS |
57524e14 | 48 | #ifndef _WIN32 |
49 | #include "port-sockets.h" | |
50 | #endif | |
cd9e6b16 | 51 | #include <krb5.h> |
cb974713 | 52 | #ifdef HAVE_KRB4 |
cd9e6b16 | 53 | #include <krb.h> |
cb974713 | 54 | #endif |
f78c7eaf | 55 | #include <ldap.h> |
cd9e6b16 | 56 | #ifdef _WIN32 |
f78c7eaf | 57 | #include <wshelper.h> |
cd9e6b16 | 58 | #include "krb5_err.h" |
f78c7eaf | 59 | #else |
cd9e6b16 | 60 | #include <sys/socket.h> |
61 | #include <netdb.h> | |
62 | #include <sys/select.h> | |
63 | #endif | |
f78c7eaf | 64 | #include <auth_con.h> |
cd9e6b16 | 65 | #include <stdio.h> |
66 | #include <stdlib.h> | |
67 | #include <time.h> | |
68 | #include <sys/timeb.h> | |
69 | #include <errno.h> | |
f78c7eaf | 70 | #include "kpasswd.h" |
71 | #include "gsssasl.h" | |
72 | #include "gssldap.h" | |
cd9e6b16 | 73 | |
cd9e6b16 | 74 | #define PW_LENGTH 25 |
f78c7eaf | 75 | #define KDC_PORT 464 |
76 | #define ULONG unsigned long | |
cd9e6b16 | 77 | |
78 | #ifndef krb5_is_krb_error | |
79 | #define krb5_is_krb_error(dat)\ | |
f78c7eaf | 80 | ((dat) && (dat)->length && ((dat)->data[0] == 0x7e ||\ |
81 | (dat)->data[0] == 0x5e)) | |
82 | #endif | |
83 | ||
84 | #ifdef _WIN32 | |
26503e15 | 85 | extern krb5_error_code decode_krb5_error |
86 | (const krb5_data *output, krb5_error **rep); | |
f78c7eaf | 87 | #define sleep(Seconds) Sleep(Seconds * 1000) |
88 | #define gethostbyname(Server) rgethostbyname(Server) | |
cd9e6b16 | 89 | #endif |
90 | ||
91 | /* Win32 defines. */ | |
92 | #if defined(_WIN32) && !defined(__CYGWIN32__) | |
93 | #ifndef ECONNABORTED | |
94 | #define ECONNABORTED WSAECONNABORTED | |
95 | #endif | |
96 | #ifndef ECONNREFUSED | |
97 | #define ECONNREFUSED WSAECONNREFUSED | |
98 | #endif | |
99 | #ifndef EHOSTUNREACH | |
100 | #define EHOSTUNREACH WSAEHOSTUNREACH | |
101 | #endif | |
102 | #endif /* _WIN32 && !__CYGWIN32__ */ | |
103 | ||
104 | static const char rcsid[] = "$Id$"; | |
105 | ||
106 | static int frequency[26][26] = | |
107 | { {4, 20, 28, 52, 2, 11, 28, 4, 32, 4, 6, 62, 23, 167, 2, 14, 0, 83, 76, | |
108 | 127, 7, 25, 8, 1, 9, 1}, /* aa - az */ | |
109 | {13, 0, 0, 0, 55, 0, 0, 0, 8, 2, 0, 22, 0, 0, 11, 0, 0, 15, 4, 2, 13, 0, | |
110 | 0, 0, 15, 0}, /* ba - bz */ | |
111 | {32, 0, 7, 1, 69, 0, 0, 33, 17, 0, 10, 9, 1, 0, 50, 3, 0, 10, 0, 28, 11, | |
112 | 0, 0, 0, 3, 0}, /* ca - cz */ | |
113 | {40, 16, 9, 5, 65, 18, 3, 9, 56, 0, 1, 4, 15, 6, 16, 4, 0, 21, 18, 53, | |
114 | 19, 5, 15, 0, 3, 0}, /* da - dz */ | |
115 | {84, 20, 55, 125, 51, 40, 19, 16, 50, 1, 4, 55, 54, 146, 35, 37, 6, 191, | |
116 | 149, 65, 9, 26, 21, 12, 5, 0}, /* ea - ez */ | |
117 | {19, 3, 5, 1, 19, 21, 1, 3, 30, 2, 0, 11, 1, 0, 51, 0, 0, 26, 8, 47, 6, | |
118 | 3, 3, 0, 2, 0}, /* fa - fz */ | |
119 | {20, 4, 3, 2, 35, 1, 3, 15, 18, 0, 0, 5, 1, 4, 21, 1, 1, 20, 9, 21, 9, | |
120 | 0, 5, 0, 1, 0}, /* ga - gz */ | |
121 | {101, 1, 3, 0, 270, 5, 1, 6, 57, 0, 0, 0, 3, 2, 44, 1, 0, 3, 10, 18, 6, | |
122 | 0, 5, 0, 3, 0}, /* ha - hz */ | |
123 | {40, 7, 51, 23, 25, 9, 11, 3, 0, 0, 2, 38, 25, 202, 56, 12, 1, 46, 79, | |
124 | 117, 1, 22, 0, 4, 0, 3}, /* ia - iz */ | |
125 | {3, 0, 0, 0, 5, 0, 0, 0, 1, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 3, 0, 0, 0, | |
126 | 0, 0}, /* ja - jz */ | |
127 | {1, 0, 0, 0, 11, 0, 0, 0, 13, 0, 0, 0, 0, 2, 0, 0, 0, 0, 6, 2, 1, 0, 2, | |
128 | 0, 1, 0}, /* ka - kz */ | |
129 | {44, 2, 5, 12, 62, 7, 5, 2, 42, 1, 1, 53, 2, 2, 25, 1, 1, 2, 16, 23, 9, | |
130 | 0, 1, 0, 33, 0}, /* la - lz */ | |
131 | {52, 14, 1, 0, 64, 0, 0, 3, 37, 0, 0, 0, 7, 1, 17, 18, 1, 2, 12, 3, 8, | |
132 | 0, 1, 0, 2, 0}, /* ma - mz */ | |
133 | {42, 10, 47, 122, 63, 19, 106, 12, 30, 1, 6, 6, 9, 7, 54, 7, 1, 7, 44, | |
134 | 124, 6, 1, 15, 0, 12, 0}, /* na - nz */ | |
135 | {7, 12, 14, 17, 5, 95, 3, 5, 14, 0, 0, 19, 41, 134, 13, 23, 0, 91, 23, | |
136 | 42, 55, 16, 28, 0, 4, 1}, /* oa - oz */ | |
137 | {19, 1, 0, 0, 37, 0, 0, 4, 8, 0, 0, 15, 1, 0, 27, 9, 0, 33, 14, 7, 6, 0, | |
138 | 0, 0, 0, 0}, /* pa - pz */ | |
139 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 0, 0, | |
140 | 0, 0, 0}, /* qa - qz */ | |
141 | {83, 8, 16, 23, 169, 4, 8, 8, 77, 1, 10, 5, 26, 16, 60, 4, 0, 24, 37, | |
142 | 55, 6, 11, 4, 0, 28, 0}, /* ra - rz */ | |
143 | {65, 9, 17, 9, 73, 13, 1, 47, 75, 3, 0, 7, 11, 12, 56, 17, 6, 9, 48, | |
144 | 116, 35, 1, 28, 0, 4, 0}, /* sa - sz */ | |
145 | {57, 22, 3, 1, 76, 5, 2, 330, 126, 1, 0, 14, 10, 6, 79, 7, 0, 49, 50, | |
146 | 56, 21, 2, 27, 0, 24, 0}, /* ta - tz */ | |
147 | {11, 5, 9, 6, 9, 1, 6, 0, 9, 0, 1, 19, 5, 31, 1, 15, 0, 47, 39, 31, 0, | |
148 | 3, 0, 0, 0, 0}, /* ua - uz */ | |
149 | {7, 0, 0, 0, 72, 0, 0, 0, 28, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, | |
150 | 0, 3, 0}, /* va - vz */ | |
151 | {36, 1, 1, 0, 38, 0, 0, 33, 36, 0, 0, 4, 1, 8, 15, 0, 0, 0, 4, 2, 0, 0, | |
152 | 1, 0, 0, 0}, /* wa - wz */ | |
153 | {1, 0, 2, 0, 0, 1, 0, 0, 3, 0, 0, 0, 0, 0, 1, 5, 0, 0, 0, 3, 0, 0, 1, 0, | |
154 | 0, 0}, /* xa - xz */ | |
155 | {14, 5, 4, 2, 7, 12, 12, 6, 10, 0, 0, 3, 7, 5, 17, 3, 0, 4, 16, 30, 0, | |
156 | 0, 5, 0, 0, 0}, /* ya - yz */ | |
157 | {1, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
158 | 0, 0}}; /* za - zz */ | |
159 | ||
160 | /* | |
161 | * This MUST be equal to the sum of the equivalent rows above. | |
162 | */ | |
163 | ||
164 | static int row_sums[26] = | |
fc53249d | 165 | {796,160,284,401,1276,262,199,539,777, |
166 | 16,39,351,243,751,662,181,17,683, | |
167 | 662,968,248,115,180,17,162,5}; | |
cd9e6b16 | 168 | |
169 | /* | |
170 | * Frequencies of starting characters | |
171 | */ | |
172 | ||
173 | static int start_freq [26] = | |
fc53249d | 174 | {1299,425,725,271,375,470,93,223,1009, |
175 | 24,20,355,379,319,823,618,21,317, | |
176 | 962,1991,271,104,516,6,16,14}; | |
cd9e6b16 | 177 | |
178 | /* | |
179 | * This MUST be equal to the sum of all elements in the above array. | |
180 | */ | |
f78c7eaf | 181 | |
182 | struct sockaddr_in kdc_server; | |
183 | SOCKET kdc_socket; | |
184 | krb5_context context; | |
185 | krb5_ccache ccache; | |
186 | krb5_auth_context auth_context = NULL; | |
187 | krb5_data ap_req; | |
188 | krb5_creds *credsp = NULL; | |
189 | krb5_creds creds; | |
190 | char connected_server[128]; | |
191 | ||
cd9e6b16 | 192 | static int total_sum = 11646; |
193 | ||
f78c7eaf | 194 | int get_krb5_error(krb5_error_code rc, char *in, char *out); |
195 | int ad_connect(LDAP **ldap_handle, char *ldap_domain, char *dn_path, | |
196 | char *Win2kPassword, char *Win2kUser, char *default_server, | |
26503e15 | 197 | int connect_to_kdc, char **ServerList); |
f78c7eaf | 198 | int ad_kdc_connect(char *connectedServer); |
199 | int ad_server_connect(char *connectedServer, char *domain); | |
200 | void ad_kdc_disconnect(); | |
201 | int compare_elements(const void *arg1, const void *arg2); | |
202 | int convert_domain_to_dn(char *domain, char *dnp); | |
203 | int set_password(char *user, char *password, char *domain); | |
204 | ||
205 | int locate_ldap_server(char *domain, char **server_name); | |
206 | ||
cd9e6b16 | 207 | long myrandom(); |
208 | void generate_password(char *password); | |
26503e15 | 209 | |
210 | #ifdef WIN32 | |
cd9e6b16 | 211 | krb5_error_code encode_krb5_setpw |
f78c7eaf | 212 | PROTOTYPE((const krb5_setpw *rep, krb5_data ** code)); |
26503e15 | 213 | #endif |
cd9e6b16 | 214 | |
f78c7eaf | 215 | krb5_error_code make_setpw_req(krb5_context context, krb5_auth_context auth_context, |
cd9e6b16 | 216 | krb5_data *ap_req, krb5_principal targprinc, |
217 | char *passwd, krb5_data *packet) | |
218 | { | |
219 | krb5_error_code ret; | |
220 | krb5_setpw setpw; | |
221 | krb5_data cipherpw; | |
222 | krb5_data *encoded_setpw; | |
223 | krb5_replay_data replay; | |
224 | char *ptr; | |
225 | register int count = 2; | |
226 | ||
f78c7eaf | 227 | memset(&setpw, 0, sizeof(krb5_setpw)); |
cd9e6b16 | 228 | if (ret = krb5_auth_con_setflags(context, auth_context, |
229 | KRB5_AUTH_CONTEXT_DO_SEQUENCE)) | |
fc53249d | 230 | return(ret); |
cd9e6b16 | 231 | setpw.targprinc = targprinc; |
232 | setpw.newpasswd.length = strlen(passwd); | |
233 | setpw.newpasswd.data = passwd; | |
234 | if ((ret = encode_krb5_setpw(&setpw, &encoded_setpw))) | |
fc53249d | 235 | return( ret ); |
cd9e6b16 | 236 | if (ret = krb5_mk_priv(context, auth_context, |
fc53249d | 237 | encoded_setpw, &cipherpw, &replay)) |
238 | return(ret); | |
cd9e6b16 | 239 | packet->length = 6 + ap_req->length + cipherpw.length; |
240 | packet->data = (char *) malloc(packet->length); | |
241 | ptr = packet->data; | |
242 | /* Length */ | |
243 | *ptr++ = (packet->length>>8) & 0xff; | |
244 | *ptr++ = packet->length & 0xff; | |
245 | /* version */ | |
246 | *ptr++ = (char)0xff; | |
247 | *ptr++ = (char)0x80; | |
248 | /* ap_req length, big-endian */ | |
249 | *ptr++ = (ap_req->length>>8) & 0xff; | |
250 | *ptr++ = ap_req->length & 0xff; | |
251 | /* ap-req data */ | |
252 | memcpy(ptr, ap_req->data, ap_req->length); | |
253 | ptr += ap_req->length; | |
254 | /* krb-priv of password */ | |
255 | memcpy(ptr, cipherpw.data, cipherpw.length); | |
f78c7eaf | 256 | free(cipherpw.data); |
257 | /* krb5_free_data_contents(context, &cipherpw);*/ | |
258 | krb5_free_data(context, encoded_setpw); | |
cd9e6b16 | 259 | return(0); |
260 | } | |
261 | ||
f78c7eaf | 262 | krb5_error_code get_setpw_rep(krb5_context context, krb5_auth_context auth_context, |
263 | krb5_data *packet, int *result_code, | |
264 | krb5_data *result_data) | |
cd9e6b16 | 265 | { |
266 | char *ptr; | |
267 | int plen; | |
268 | int vno; | |
269 | krb5_data ap_rep; | |
270 | krb5_error_code ret; | |
271 | krb5_data cipherresult; | |
272 | krb5_data clearresult; | |
273 | krb5_error *krberror; | |
274 | krb5_replay_data replay; | |
cd9e6b16 | 275 | krb5_ap_rep_enc_part *ap_rep_enc; |
276 | ||
277 | if (packet->length < 4) | |
fc53249d | 278 | return(KRB5KRB_AP_ERR_MODIFIED); |
cd9e6b16 | 279 | ptr = packet->data; |
280 | if (krb5_is_krb_error(packet)) | |
281 | { | |
fc53249d | 282 | ret = decode_krb5_error(packet, &krberror); |
283 | if (ret) | |
284 | return(ret); | |
285 | ret = krberror->error; | |
286 | krb5_free_error(context, krberror); | |
287 | return(ret); | |
cd9e6b16 | 288 | } |
289 | /* verify length */ | |
290 | plen = (*ptr++ & 0xff); | |
291 | plen = (plen<<8) | (*ptr++ & 0xff); | |
26503e15 | 292 | if (plen != (int)packet->length) |
fc53249d | 293 | return(KRB5KRB_AP_ERR_MODIFIED); |
cd9e6b16 | 294 | vno = (*ptr++ & 0xff); |
295 | vno = (vno<<8) | (*ptr++ & 0xff); | |
296 | if (vno != KRB5_KPASSWD_VERS_SETPW && vno != KRB5_KPASSWD_VERS_CHANGEPW) | |
fc53249d | 297 | return(KRB5KDC_ERR_BAD_PVNO); |
cd9e6b16 | 298 | /* read, check ap-rep length */ |
299 | ap_rep.length = (*ptr++ & 0xff); | |
300 | ap_rep.length = (ap_rep.length<<8) | (*ptr++ & 0xff); | |
301 | if (ptr + ap_rep.length >= packet->data + packet->length) | |
fc53249d | 302 | return(KRB5KRB_AP_ERR_MODIFIED); |
cd9e6b16 | 303 | if (ap_rep.length) |
304 | { | |
fc53249d | 305 | /* verify ap_rep */ |
306 | ap_rep.data = ptr; | |
307 | ptr += ap_rep.length; | |
308 | if (ret = krb5_rd_rep(context, auth_context, &ap_rep, &ap_rep_enc)) | |
309 | return(ret); | |
310 | krb5_free_ap_rep_enc_part(context, ap_rep_enc); | |
311 | /* extract and decrypt the result */ | |
312 | cipherresult.data = ptr; | |
313 | cipherresult.length = (packet->data + packet->length) - ptr; | |
314 | /* XXX there's no api to do this right. The problem is that | |
315 | if there's a remote subkey, it will be used. This is | |
316 | not what the spec requires */ | |
fc53249d | 317 | ret = krb5_rd_priv(context, auth_context, &cipherresult, &clearresult, |
318 | &replay); | |
fc53249d | 319 | if (ret) |
320 | return(ret); | |
cd9e6b16 | 321 | } |
322 | else | |
323 | { | |
fc53249d | 324 | cipherresult.data = ptr; |
325 | cipherresult.length = (packet->data + packet->length) - ptr; | |
f78c7eaf | 326 | |
fc53249d | 327 | if (ret = krb5_rd_error(context, &cipherresult, &krberror)) |
328 | return(ret); | |
f78c7eaf | 329 | |
fc53249d | 330 | clearresult = krberror->e_data; |
cd9e6b16 | 331 | } |
332 | if (clearresult.length < 2) | |
333 | { | |
fc53249d | 334 | ret = KRB5KRB_AP_ERR_MODIFIED; |
335 | goto cleanup; | |
cd9e6b16 | 336 | } |
337 | ptr = clearresult.data; | |
338 | *result_code = (*ptr++ & 0xff); | |
339 | *result_code = (*result_code<<8) | (*ptr++ & 0xff); | |
340 | if ((*result_code < KRB5_KPASSWD_SUCCESS) || | |
fc53249d | 341 | (*result_code > KRB5_KPASSWD_ACCESSDENIED)) |
cd9e6b16 | 342 | { |
fc53249d | 343 | ret = KRB5KRB_AP_ERR_MODIFIED; |
344 | goto cleanup; | |
cd9e6b16 | 345 | } |
346 | /* all success replies should be authenticated/encrypted */ | |
347 | if ((ap_rep.length == 0) && (*result_code == KRB5_KPASSWD_SUCCESS)) | |
348 | { | |
fc53249d | 349 | ret = KRB5KRB_AP_ERR_MODIFIED; |
f78c7eaf | 350 | goto cleanup; |
cd9e6b16 | 351 | } |
352 | result_data->length = (clearresult.data + clearresult.length) - ptr; | |
353 | if (result_data->length) | |
354 | { | |
fc53249d | 355 | result_data->data = (char *) malloc(result_data->length); |
356 | memcpy(result_data->data, ptr, result_data->length); | |
cd9e6b16 | 357 | } |
358 | else | |
f78c7eaf | 359 | result_data->data = NULL; |
cd9e6b16 | 360 | ret = 0; |
361 | cleanup: | |
362 | if (ap_rep.length) | |
fc53249d | 363 | free(clearresult.data); |
cd9e6b16 | 364 | else |
f78c7eaf | 365 | krb5_free_error(context, krberror); |
cd9e6b16 | 366 | return(ret); |
367 | } | |
368 | ||
f78c7eaf | 369 | krb5_error_code kdc_set_password(krb5_context context, krb5_ccache ccache, |
cd9e6b16 | 370 | char *newpw, char *user, char *domain, |
371 | int *result_code) | |
372 | { | |
f78c7eaf | 373 | krb5_data chpw_snd; |
374 | krb5_data chpw_rcv; | |
cd9e6b16 | 375 | krb5_data result_string; |
376 | krb5_address local_kaddr; | |
377 | krb5_address remote_kaddr; | |
378 | char userrealm[256]; | |
379 | char temp[256]; | |
380 | krb5_error_code code; | |
cd9e6b16 | 381 | struct sockaddr local_addr; |
382 | struct sockaddr remote_addr; | |
cd9e6b16 | 383 | int i; |
cd9e6b16 | 384 | int addrlen; |
385 | int cc; | |
386 | int local_result_code; | |
f78c7eaf | 387 | int nfds; |
cd9e6b16 | 388 | krb5_principal targprinc; |
f78c7eaf | 389 | struct timeval TimeVal; |
390 | fd_set readfds; | |
cd9e6b16 | 391 | |
cd9e6b16 | 392 | memset(&local_addr, 0, sizeof(local_addr)); |
393 | memset(&local_kaddr, 0, sizeof(local_kaddr)); | |
394 | memset(&result_string, 0, sizeof(result_string)); | |
395 | memset(&remote_kaddr, 0, sizeof(remote_kaddr)); | |
f78c7eaf | 396 | memset(&chpw_snd, 0, sizeof(krb5_data)); |
397 | memset(&chpw_rcv, 0, sizeof(krb5_data)); | |
cd9e6b16 | 398 | memset(userrealm, '\0', sizeof(userrealm)); |
399 | targprinc = NULL; | |
f78c7eaf | 400 | |
401 | chpw_rcv.length = 1500; | |
402 | chpw_rcv.data = (char *) calloc(1, chpw_rcv.length); | |
403 | ||
cd9e6b16 | 404 | for (i = 0; i < (int)strlen(domain); i++) |
fc53249d | 405 | userrealm[i] = toupper(domain[i]); |
cd9e6b16 | 406 | |
407 | sprintf(temp, "%s@%s", user, userrealm); | |
408 | krb5_parse_name(context, temp, &targprinc); | |
409 | ||
f78c7eaf | 410 | if (credsp == NULL) |
411 | { | |
412 | memset(&creds, 0, sizeof(creds)); | |
413 | memset(&ap_req, 0, sizeof(krb5_data)); | |
414 | sprintf(temp, "%s@%s", "kadmin/changepw", userrealm); | |
415 | if (code = krb5_parse_name(context, temp, &creds.server)) | |
416 | goto cleanup; | |
417 | if (code = krb5_cc_get_principal(context, ccache, &creds.client)) | |
418 | goto cleanup; | |
419 | if (code = krb5_get_credentials(context, 0, ccache, &creds, &credsp)) | |
420 | goto cleanup; | |
421 | if (code = krb5_mk_req_extended(context, &auth_context, AP_OPTS_USE_SUBKEY, | |
422 | NULL, credsp, &ap_req)) | |
423 | goto cleanup; | |
cd9e6b16 | 424 | } |
425 | ||
f78c7eaf | 426 | addrlen = sizeof(local_addr); |
427 | if (getsockname(kdc_socket, &local_addr, &addrlen) < 0) | |
cd9e6b16 | 428 | { |
f78c7eaf | 429 | code = KDC_GETSOCKNAME_ERROR; |
430 | goto cleanup; | |
cd9e6b16 | 431 | } |
f78c7eaf | 432 | if (((struct sockaddr_in *)&local_addr)->sin_addr.s_addr != 0) |
cd9e6b16 | 433 | { |
f78c7eaf | 434 | local_kaddr.addrtype = ADDRTYPE_INET; |
435 | local_kaddr.length = | |
436 | sizeof(((struct sockaddr_in *) &local_addr)->sin_addr); | |
437 | local_kaddr.contents = | |
438 | (char *) &(((struct sockaddr_in *) &local_addr)->sin_addr); | |
cd9e6b16 | 439 | } |
f78c7eaf | 440 | else |
cd9e6b16 | 441 | { |
f78c7eaf | 442 | krb5_address **addrs; |
443 | krb5_os_localaddr(context, &addrs); | |
444 | local_kaddr.magic = addrs[0]->magic; | |
445 | local_kaddr.addrtype = addrs[0]->addrtype; | |
446 | local_kaddr.length = addrs[0]->length; | |
447 | local_kaddr.contents = calloc(1, addrs[0]->length); | |
448 | memcpy(local_kaddr.contents, addrs[0]->contents, addrs[0]->length); | |
449 | krb5_free_addresses(context, addrs); | |
450 | } | |
fc53249d | 451 | |
f78c7eaf | 452 | addrlen = sizeof(remote_addr); |
453 | if (getpeername(kdc_socket, &remote_addr, &addrlen) < 0) | |
454 | { | |
455 | code = KDC_GETPEERNAME_ERROR; | |
456 | goto cleanup; | |
457 | } | |
458 | remote_kaddr.addrtype = ADDRTYPE_INET; | |
459 | remote_kaddr.length = sizeof(((struct sockaddr_in *) &remote_addr)->sin_addr); | |
460 | remote_kaddr.contents = (char *) &(((struct sockaddr_in *) &remote_addr)->sin_addr); | |
fc53249d | 461 | |
f78c7eaf | 462 | if (code = krb5_auth_con_setaddrs(context, auth_context, &local_kaddr, NULL)) |
463 | goto cleanup; | |
464 | if (code = make_setpw_req(context, auth_context, &ap_req, | |
465 | targprinc, newpw, &chpw_snd)) | |
466 | goto cleanup; | |
fc53249d | 467 | |
f78c7eaf | 468 | for (i = 0; i < 3; i++) |
469 | { | |
470 | if ((cc = sendto(kdc_socket, chpw_snd.data, chpw_snd.length, 0, | |
471 | NULL, | |
26503e15 | 472 | 0)) != (int)chpw_snd.length) |
f78c7eaf | 473 | { |
474 | code = KDC_SEND_ERROR; | |
475 | sleep(1); | |
476 | continue; | |
477 | } | |
fc53249d | 478 | |
f78c7eaf | 479 | TimeVal.tv_sec = 3; |
480 | TimeVal.tv_usec = 0; | |
481 | FD_ZERO(&readfds); | |
482 | FD_SET(kdc_socket, &readfds); | |
483 | nfds = kdc_socket + 1; | |
484 | code = select(nfds, &readfds, NULL, NULL, &TimeVal); | |
485 | if ((code == 0) || (code == SOCKET_ERROR)) | |
cd9e6b16 | 486 | { |
f78c7eaf | 487 | code = KDC_RECEIVE_TIMEOUT; |
fc53249d | 488 | sleep(1); |
f78c7eaf | 489 | continue; |
cd9e6b16 | 490 | } |
f78c7eaf | 491 | |
492 | if ((cc = recvfrom(kdc_socket, chpw_rcv.data, chpw_rcv.length, 0, | |
493 | NULL, NULL)) < 0) | |
cd9e6b16 | 494 | { |
f78c7eaf | 495 | code = KDC_RECEIVE_TIMEOUT; |
496 | sleep(1); | |
497 | continue; | |
fc53249d | 498 | } |
f78c7eaf | 499 | chpw_rcv.length = cc; |
500 | if (code = krb5_auth_con_setaddrs(context, auth_context, NULL, &remote_kaddr)) | |
cd9e6b16 | 501 | { |
f78c7eaf | 502 | sleep(1); |
503 | continue; | |
fc53249d | 504 | } |
cd9e6b16 | 505 | local_result_code = 0; |
f78c7eaf | 506 | code = get_setpw_rep(context, auth_context, &chpw_rcv, |
507 | &local_result_code, &result_string); | |
cd9e6b16 | 508 | |
fc53249d | 509 | if (local_result_code) |
510 | { | |
511 | if (local_result_code == KRB5_KPASSWD_SOFTERROR) | |
512 | local_result_code = KRB5_KPASSWD_SUCCESS; | |
513 | *result_code = local_result_code; | |
514 | } | |
f78c7eaf | 515 | if ((code == 0) && (local_result_code == 0)) |
516 | break; | |
517 | sleep(1); | |
cd9e6b16 | 518 | } |
f78c7eaf | 519 | |
cd9e6b16 | 520 | cleanup: |
f78c7eaf | 521 | if (chpw_snd.data != NULL) |
522 | free(chpw_snd.data); | |
523 | if (chpw_rcv.data != NULL) | |
524 | free(chpw_rcv.data); | |
cd9e6b16 | 525 | if (targprinc != NULL) |
526 | krb5_free_principal(context, targprinc); | |
fc53249d | 527 | return(code); |
cd9e6b16 | 528 | } |
529 | ||
f78c7eaf | 530 | int set_password(char *user, char *password, char *domain) |
cd9e6b16 | 531 | { |
cd9e6b16 | 532 | int res_code; |
533 | krb5_error_code retval; | |
fc53249d | 534 | char pw[PW_LENGTH+1]; |
cd9e6b16 | 535 | |
cd9e6b16 | 536 | memset(pw, '\0', sizeof(pw)); |
f78c7eaf | 537 | if (strlen(password) != 0) |
538 | strcpy(pw, password); | |
539 | else | |
540 | generate_password(pw); | |
fc53249d | 541 | res_code = 0; |
f78c7eaf | 542 | retval = kdc_set_password(context, ccache, pw, user, domain, &res_code); |
cd9e6b16 | 543 | |
f78c7eaf | 544 | if (res_code) |
545 | return(res_code); | |
546 | return(retval); | |
cd9e6b16 | 547 | } |
548 | ||
549 | void generate_password(char *password) | |
550 | { | |
fc53249d | 551 | int i; |
cd9e6b16 | 552 | int j; |
553 | int row_position; | |
554 | int nchars; | |
555 | int position; | |
fc53249d | 556 | int word; |
cd9e6b16 | 557 | int line; |
fc53249d | 558 | char *pwp; |
cd9e6b16 | 559 | |
fc53249d | 560 | for (line = 22; line; --line) |
cd9e6b16 | 561 | { |
562 | for (word = 7; word; --word) | |
563 | { | |
564 | position = myrandom()%total_sum; | |
565 | for(row_position = 0, j = 0; position >= row_position; row_position += start_freq[j], j++) | |
566 | continue; | |
567 | *(pwp = password) = j + 'a' - 1; | |
568 | for (nchars = PW_LENGTH-1; nchars; --nchars) | |
569 | { | |
570 | i = *pwp - 'a'; | |
571 | pwp++; | |
572 | position = myrandom()%row_sums[i]; | |
573 | for (row_position = 0, j = 0; position >= row_position; row_position += frequency[i][j], j++) | |
574 | continue; | |
575 | *pwp = j + 'a' - 1; | |
576 | } | |
577 | *(++pwp)='\0'; | |
578 | return; | |
fc53249d | 579 | } |
cd9e6b16 | 580 | putchar('\n'); |
581 | } | |
582 | } | |
583 | ||
584 | long myrandom() | |
585 | { | |
586 | static int init = 0; | |
587 | int pid; | |
588 | #ifdef _WIN32 | |
589 | struct _timeb timebuffer; | |
590 | #else | |
591 | struct timeval tv; | |
592 | #endif | |
593 | ||
594 | if (!init) | |
595 | { | |
596 | init = 1; | |
597 | pid = getpid(); | |
598 | #ifdef _WIN32 | |
599 | _ftime(&timebuffer); | |
600 | srand(timebuffer.time ^ timebuffer.millitm ^ pid); | |
601 | #else | |
602 | gettimeofday(&tv, (struct timezone *) NULL); | |
603 | srandom(tv.tv_sec ^ tv.tv_usec ^ pid); | |
604 | #endif | |
605 | } | |
606 | return (rand()); | |
607 | } | |
f78c7eaf | 608 | |
609 | int get_krb5_error(krb5_error_code rc, char *in, char *out) | |
610 | { | |
611 | int krb5Error; | |
612 | int retval; | |
613 | ||
614 | retval = 1; | |
615 | ||
616 | if (rc < 0) | |
617 | { | |
618 | krb5Error = ((int)(rc & 255)); | |
619 | sprintf(out, "%s: %s(%ld)", in, error_message(rc), krb5Error); | |
620 | } | |
621 | else | |
622 | { | |
623 | switch (rc) | |
624 | { | |
625 | case KDC_RECEIVE_TIMEOUT: | |
626 | { | |
627 | retval = 0; | |
628 | sprintf(out, "%s: %s(%d)", in, "Receive timeout", rc); | |
629 | break; | |
630 | } | |
631 | case KDC_RECEIVE_ERROR: | |
632 | { | |
633 | retval = 0; | |
634 | sprintf(out, "%s: %s(%d)", in, "Receive error", rc); | |
635 | break; | |
636 | } | |
637 | case KRB5_KPASSWD_MALFORMED: | |
638 | { | |
639 | sprintf(out, "%s: %s(%d)", in, "malformed password", rc); | |
640 | break; | |
641 | } | |
642 | case KRB5_KPASSWD_HARDERROR: | |
643 | { | |
644 | sprintf(out, "%s: %s(%d)", in, "hard error", rc); | |
645 | break; | |
646 | } | |
647 | case KRB5_KPASSWD_AUTHERROR: | |
648 | { | |
649 | retval = 0; | |
650 | sprintf(out, "%s: %s(%d)", in, "authentication error", rc); | |
651 | break; | |
652 | } | |
653 | case KRB5_KPASSWD_SOFTERROR: | |
654 | { | |
655 | retval = 0; | |
656 | sprintf(out, "%s: %s(%d)", in, "soft error", rc); | |
657 | break; | |
658 | } | |
659 | case KRB5_KPASSWD_ACCESSDENIED: | |
660 | { | |
661 | sprintf(out, "%s: %s(%d)", in, "Access denied", rc); | |
662 | break; | |
663 | } | |
664 | case KDC_SEND_ERROR: | |
665 | { | |
666 | retval = 0; | |
667 | sprintf(out, "%s: %s(%d)", in, "Send error", rc); | |
668 | break; | |
669 | } | |
670 | case KDC_GETSOCKNAME_ERROR: | |
671 | { | |
672 | retval = 0; | |
673 | sprintf(out, "%s: %s(%d)", in, "Socket error - getsockname", rc); | |
674 | break; | |
675 | } | |
676 | case KDC_GETPEERNAME_ERROR: | |
677 | { | |
678 | retval = 0; | |
679 | sprintf(out, "%s: %s(%d)", in, "Socket error - getpeername", rc); | |
680 | break; | |
681 | } | |
682 | default: | |
683 | { | |
684 | sprintf(out, "%s: %s(%d)", in, "unknown error", rc); | |
685 | break; | |
686 | } | |
687 | } | |
688 | } | |
689 | return(retval); | |
690 | } | |
691 | ||
692 | int ad_connect(LDAP **ldap_handle, char *ldap_domain, char *dn_path, | |
693 | char *Win2kPassword, char *Win2kUser, char *default_server, | |
26503e15 | 694 | int connect_to_kdc, char **ServerList) |
f78c7eaf | 695 | { |
696 | int i; | |
d7051053 | 697 | int k; |
698 | int Count; | |
f78c7eaf | 699 | char *server_name[MAX_SERVER_NAMES]; |
f78c7eaf | 700 | static char temp[128]; |
701 | ULONG version = LDAP_VERSION3; | |
702 | ULONG rc; | |
df2a74ce | 703 | int Max_wait_time = 1000; |
f78c7eaf | 704 | int Max_size_limit = LDAP_NO_LIMIT; |
705 | ||
26503e15 | 706 | if (strlen(ldap_domain) == 0) |
707 | return(1); | |
708 | ||
f78c7eaf | 709 | convert_domain_to_dn(ldap_domain, dn_path); |
710 | if (strlen(dn_path) == 0) | |
26503e15 | 711 | return(1); |
f78c7eaf | 712 | |
d7051053 | 713 | Count = 0; |
714 | while (ServerList[Count] != NULL) | |
715 | ++Count; | |
716 | ||
26503e15 | 717 | if ((Count == 0) && (connect_to_kdc)) |
718 | return(1); | |
719 | ||
f78c7eaf | 720 | memset(server_name, 0, sizeof(server_name[0]) * MAX_SERVER_NAMES); |
d7051053 | 721 | if (locate_ldap_server(ldap_domain, server_name) == -1) |
26503e15 | 722 | return(1); |
d7051053 | 723 | |
724 | for (i = 0; i < MAX_SERVER_NAMES; i++) | |
f78c7eaf | 725 | { |
d7051053 | 726 | if (server_name[i] != NULL) |
f78c7eaf | 727 | { |
d7051053 | 728 | if (Count >= MAX_SERVER_NAMES) |
729 | { | |
730 | free(server_name[i]); | |
731 | server_name[i] = NULL; | |
732 | continue; | |
733 | } | |
734 | for (k = 0; k < (int)strlen(server_name[i]); k++) | |
735 | server_name[i][k] = toupper(server_name[i][k]); | |
736 | for (k = 0; k < Count; k++) | |
737 | { | |
738 | if (!strcasecmp(server_name[i], ServerList[k])) | |
739 | { | |
740 | free(server_name[i]); | |
741 | server_name[i] = NULL; | |
742 | break; | |
743 | } | |
744 | } | |
745 | if (k == Count) | |
f78c7eaf | 746 | { |
d7051053 | 747 | ServerList[Count] = calloc(1, 256); |
748 | strcpy(ServerList[Count], server_name[i]); | |
749 | ServerList[Count] = (char *)strdup((char *)server_name[i]); | |
750 | ++Count; | |
f78c7eaf | 751 | free(server_name[i]); |
f78c7eaf | 752 | } |
753 | } | |
f78c7eaf | 754 | } |
d7051053 | 755 | |
756 | for (i = 0; i < Count; i++) | |
f78c7eaf | 757 | { |
d7051053 | 758 | if (ServerList[i] == NULL) |
759 | continue; | |
760 | ||
761 | if (((*ldap_handle) = ldap_open(ServerList[i], LDAP_PORT)) != NULL) | |
f78c7eaf | 762 | { |
d7051053 | 763 | rc = ldap_set_option((*ldap_handle), LDAP_OPT_PROTOCOL_VERSION, &version); |
764 | rc = ldap_set_option((*ldap_handle), LDAP_OPT_TIMELIMIT, | |
765 | (void *)&Max_wait_time); | |
766 | rc = ldap_set_option((*ldap_handle), LDAP_OPT_SIZELIMIT, | |
767 | (void *)&Max_size_limit); | |
768 | rc = ldap_set_option((*ldap_handle), LDAP_OPT_REFERRALS, LDAP_OPT_OFF); | |
df2a74ce | 769 | rc = ldap_adgssapi_bind((*ldap_handle), dn_path, 0); |
d7051053 | 770 | if (rc == LDAP_SUCCESS) |
f78c7eaf | 771 | { |
d7051053 | 772 | if (connect_to_kdc) |
f78c7eaf | 773 | { |
d7051053 | 774 | if (!ad_server_connect(ServerList[i], ldap_domain)) |
f78c7eaf | 775 | { |
d7051053 | 776 | ldap_unbind_s((*ldap_handle)); |
26503e15 | 777 | (*ldap_handle) = NULL; |
d7051053 | 778 | continue; |
f78c7eaf | 779 | } |
f78c7eaf | 780 | } |
d7051053 | 781 | if (strlen(default_server) == 0) |
782 | strcpy(default_server, ServerList[i]); | |
783 | strcpy(connected_server, ServerList[i]); | |
784 | break; | |
f78c7eaf | 785 | } |
26503e15 | 786 | else |
787 | { | |
788 | (*ldap_handle) = NULL; | |
789 | } | |
d7051053 | 790 | } |
f78c7eaf | 791 | } |
26503e15 | 792 | if ((*ldap_handle) == NULL) |
793 | return(1); | |
f78c7eaf | 794 | return(0); |
795 | } | |
796 | ||
797 | int ad_server_connect(char *connectedServer, char *domain) | |
798 | { | |
799 | krb5_error_code rc; | |
800 | krb5_creds creds; | |
801 | krb5_creds *credsp; | |
802 | char temp[256]; | |
803 | char userrealm[256]; | |
804 | int i; | |
805 | unsigned short port = KDC_PORT; | |
806 | ||
807 | context = NULL; | |
808 | credsp = NULL; | |
809 | memset(&ccache, 0, sizeof(ccache)); | |
810 | memset(&creds, 0, sizeof(creds)); | |
811 | memset(userrealm, '\0', sizeof(userrealm)); | |
812 | ||
813 | rc = 0; | |
814 | if (krb5_init_context(&context)) | |
815 | goto cleanup; | |
816 | if (krb5_cc_default(context, &ccache)) | |
817 | goto cleanup; | |
818 | ||
819 | for (i = 0; i < (int)strlen(domain); i++) | |
820 | userrealm[i] = toupper(domain[i]); | |
821 | sprintf(temp, "%s@%s", "kadmin/changepw", userrealm); | |
822 | if (krb5_parse_name(context, temp, &creds.server)) | |
823 | goto cleanup; | |
824 | if (krb5_cc_get_principal(context, ccache, &creds.client)) | |
825 | goto cleanup; | |
826 | if (krb5_get_credentials(context, 0, ccache, &creds, &credsp)) | |
827 | goto cleanup; | |
828 | ||
829 | rc = ad_kdc_connect(connectedServer); | |
830 | ||
831 | ||
832 | cleanup: | |
833 | if (!rc) | |
834 | { | |
835 | krb5_cc_close(context, ccache); | |
836 | krb5_free_context(context); | |
837 | } | |
838 | krb5_free_cred_contents(context, &creds); | |
839 | if (credsp != NULL) | |
840 | krb5_free_creds(context, credsp); | |
841 | return(rc); | |
842 | } | |
843 | ||
844 | ||
845 | int ad_kdc_connect(char *connectedServer) | |
846 | { | |
847 | struct hostent *hp; | |
848 | int rc; | |
849 | ||
850 | rc = 0; | |
851 | hp = gethostbyname(connectedServer); | |
852 | if (hp == NULL) | |
853 | goto cleanup; | |
854 | memset(&kdc_server, 0, sizeof(kdc_server)); | |
855 | memcpy(&(kdc_server.sin_addr),hp->h_addr_list[0],hp->h_length); | |
856 | kdc_server.sin_family = hp->h_addrtype; | |
857 | kdc_server.sin_port = htons(KDC_PORT); | |
858 | ||
859 | if ((kdc_socket = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) | |
860 | goto cleanup; | |
861 | if (connect(kdc_socket, (struct sockaddr*)&kdc_server, sizeof(kdc_server)) == SOCKET_ERROR) | |
862 | goto cleanup; | |
863 | rc = 1; | |
864 | ||
865 | cleanup: | |
866 | return(rc); | |
867 | } | |
868 | ||
869 | void ad_kdc_disconnect() | |
870 | { | |
871 | ||
872 | if (auth_context != NULL) | |
873 | { | |
874 | krb5_auth_con_free(context, auth_context); | |
875 | if (ap_req.data != NULL) | |
876 | free(ap_req.data); | |
877 | krb5_free_cred_contents(context, &creds); | |
878 | if (credsp != NULL) | |
879 | krb5_free_creds(context, credsp); | |
880 | } | |
881 | credsp = NULL; | |
882 | auth_context = NULL; | |
883 | if (context != NULL) | |
884 | { | |
885 | krb5_cc_close(context, ccache); | |
886 | krb5_free_context(context); | |
887 | } | |
888 | closesocket(kdc_socket); | |
889 | ||
890 | } | |
891 | ||
892 | int convert_domain_to_dn(char *domain, char *dnp) | |
893 | { | |
894 | char *fp; | |
895 | char *dp; | |
896 | char dn[512]; | |
897 | ||
898 | memset(dn, '\0', sizeof(dn)); | |
899 | strcpy(dn, "dc="); | |
900 | dp = dn+3; | |
901 | for (fp = domain; *fp; fp++) | |
902 | { | |
903 | if (*fp == '.') | |
904 | { | |
905 | strcpy(dp, ",dc="); | |
906 | dp += 4; | |
907 | } | |
908 | else | |
909 | *dp++ = *fp; | |
910 | } | |
911 | ||
912 | strcpy(dnp, dn); | |
913 | return 0; | |
914 | } | |
915 | ||
916 | int compare_elements(const void *arg1, const void *arg2) | |
917 | { | |
918 | int rc; | |
919 | ||
920 | rc = strcmp((char*)arg1, (char*)arg2); | |
921 | if (rc < 0) | |
922 | return(1); | |
923 | if (rc > 0) | |
924 | return(-1); | |
925 | return(rc); | |
926 | } |