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