1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
/*
* lib/kdb/kdb_ldap/ldap_service_stash.c
*
* Copyright (c) 2004-2005, Novell, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The copyright holder's name is not used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <ctype.h>
#include "ldap_main.h"
#include "kdb_ldap.h"
#include "ldap_service_stash.h"
krb5_error_code
krb5_ldap_readpassword(context, ldap_context, password)
krb5_context context;
krb5_ldap_context *ldap_context;
unsigned char **password;
{
int entryfound=0;
krb5_error_code st=0;
char line[RECORDLEN]="0", *start=NULL, *file=NULL;
char errbuf[1024];
FILE *fptr=NULL;
*password = NULL;
if (ldap_context->service_password_file)
file = ldap_context->service_password_file;
#ifndef HAVE_STRERROR_R
# undef strerror_r
# define strerror_r(ERRNUM, BUF, SIZE) (strncpy(BUF, strerror(ERRNUM), SIZE), BUF[(SIZE)-1] = 0)
#endif
/* check whether file exists */
if (access(file, F_OK) < 0) {
st = errno;
strerror_r(errno, errbuf, sizeof(errbuf));
krb5_set_error_message (context, st, "%s", errbuf);
goto rp_exit;
}
/* check read access */
if (access(file, R_OK) < 0) {
st = errno;
strerror_r(errno, errbuf, sizeof(errbuf));
krb5_set_error_message (context, st, "%s", errbuf);
goto rp_exit;
}
if ((fptr=fopen(file, "r")) == NULL) {
st = errno;
strerror_r(errno, errbuf, sizeof(errbuf));
krb5_set_error_message (context, st, "%s", errbuf);
goto rp_exit;
}
set_cloexec_file(fptr);
/* get the record from the file */
while (fgets(line, RECORDLEN, fptr)!= NULL) {
char tmp[RECORDLEN];
tmp[0] = '\0';
/* Handle leading white-spaces */
for (start = line; isspace(*start); ++start);
/* Handle comment lines */
if (*start == '!' || *start == '#')
continue;
sscanf(line, "%*[ \t]%[^#]", tmp);
if (tmp[0] == '\0')
sscanf(line, "%[^#]", tmp);
if (strcasecmp(tmp, ldap_context->bind_dn) == 0) {
entryfound = 1; /* service_dn record found !!! */
break;
}
}
fclose (fptr);
if (entryfound == 0) {
st = KRB5_KDB_SERVER_INTERNAL_ERR;
krb5_set_error_message (context, st, "Bind DN entry missing in stash file");
goto rp_exit;
}
/* replace the \n with \0 */
start = strchr(line, '\n');
if (start)
*start = '\0';
start = strchr(line, '#');
if (start == NULL) {
/* password field missing */
st = KRB5_KDB_SERVER_INTERNAL_ERR;
krb5_set_error_message (context, st, "Stash file entry corrupt");
goto rp_exit;
}
++ start;
/* Extract the plain password / certificate file information */
{
struct data PT, CT;
/* Check if the entry has the path of a certificate */
if (!strncmp(start, "{FILE}", strlen("{FILE}"))) {
/* Set *password = {FILE}<path to cert>\0<cert password> */
size_t len = strlen(start);
*password = (unsigned char *)malloc(len + 2);
if (*password == NULL) {
st = ENOMEM;
goto rp_exit;
}
memcpy((char *)(*password), start, len);
(*password)[len] = '\0';
(*password)[len + 1] = '\0';
goto got_password;
} else {
CT.value = (unsigned char *)start;
CT.len = strlen((char *)CT.value);
st = dec_password(CT, &PT);
if (st != 0) {
switch (st) {
case ERR_NO_MEM:
st = ENOMEM;
break;
case ERR_PWD_ZERO:
st = EINVAL;
krb5_set_error_message(context, st, "Password has zero length");
break;
case ERR_PWD_BAD:
st = EINVAL;
krb5_set_error_message(context, st, "Password corrupted");
break;
case ERR_PWD_NOT_HEX:
st = EINVAL;
krb5_set_error_message(context, st, "Not a hexadecimal password");
break;
default:
st = KRB5_KDB_SERVER_INTERNAL_ERR;
break;
}
goto rp_exit;
}
*password = PT.value;
}
}
got_password:
rp_exit:
if (st) {
if (*password)
free (*password);
*password = NULL;
}
return st;
}
/* Encodes a sequence of bytes in hexadecimal */
int
tohex(in, ret)
krb5_data in;
krb5_data *ret;
{
int i=0, err = 0;
ret->length = 0;
ret->data = NULL;
ret->data = malloc((unsigned int)in.length * 2 + 1 /*Null termination */);
if (ret->data == NULL) {
err = ENOMEM;
goto cleanup;
}
ret->length = in.length * 2;
ret->data[ret->length] = 0;
for (i = 0; i < in.length; i++)
sprintf(ret->data + 2 * i, "%02x", in.data[i] & 0xff);
cleanup:
if (ret->length == 0) {
free(ret->data);
ret->data = NULL;
}
return err;
}
/* The entry in the password file will have the following format
* <FQDN of service> = <secret>
* <secret> := {HEX}<password in hexadecimal>
*
* <password> is the actual eDirectory password of the service
* Return values:
* ERR_NO_MEM - No Memory
* ERR_PWD_ZERO - Password has zero length
* ERR_PWD_BAD - Passowrd corrupted
* ERR_PWD_NOT_HEX - Not a hexadecimal password
*/
int dec_password(struct data pwd, struct data *ret) {
int err=0;
int i=0, j=0;
ret->len = 0;
ret->value = NULL;
if (pwd.len == 0) {
err = ERR_PWD_ZERO;
ret->len = 0;
goto cleanup;
}
/* Check if it is a hexadecimal encoded password */
if (pwd.len >= strlen("{HEX}") &&
strncmp((char *)pwd.value, "{HEX}", strlen("{HEX}")) == 0) {
if ((pwd.len - strlen("{HEX}")) % 2 != 0) {
/* A hexadecimal encoded password should have even length */
err = ERR_PWD_BAD;
ret->len = 0;
goto cleanup;
}
ret->value = (unsigned char *)malloc((pwd.len - strlen("{HEX}")) / 2 + 1);
if (ret->value == NULL) {
err = ERR_NO_MEM;
ret->len = 0;
goto cleanup;
}
ret->len = (pwd.len - strlen("{HEX}")) / 2;
ret->value[ret->len] = '\0';
for (i = strlen("{HEX}"), j = 0; i < pwd.len; i += 2, j++) {
unsigned int k;
/* Check if it is a hexadecimal number */
if (isxdigit(pwd.value[i]) == 0 || isxdigit(pwd.value[i + 1]) == 0) {
err = ERR_PWD_NOT_HEX;
ret->len = 0;
goto cleanup;
}
sscanf((char *)pwd.value + i, "%2x", &k);
ret->value[j] = k;
}
goto cleanup;
} else {
err = ERR_PWD_NOT_HEX;
ret->len = 0;
goto cleanup;
}
cleanup:
if (ret->len == 0) {
free(ret->value);
ret->value = NULL;
}
return(err);
}
|