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