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
282
283
284
285
286
287
288
289
|
/*
Authors:
Sumit Bose <sbose@redhat.com>
Copyright (C) 2009 Red Hat
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <netdb.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <krb5/locate_plugin.h>
#include "providers/krb5/krb5_common.h"
#define BUFSIZE 512
#define SSSD_KRB5_LOCATOR_DEBUG "SSSD_KRB5_LOCATOR_DEBUG"
#define DEBUG_KEY "[sssd_krb5_locator] "
#define PLUGIN_DEBUG(body) do { \
if (ctx->debug) { \
debug_fn body; \
} \
} while(0);
struct sssd_ctx {
char *sssd_realm;
struct addrinfo *sssd_kdc_addrinfo;
bool debug;
};
void debug_fn(const char *format, ...)
{
va_list ap;
char *s = NULL;
int ret;
va_start(ap, format);
ret = vasprintf(&s, format, ap);
if (ret < 0) {
/* ENOMEM */
return;
}
va_end(ap);
fprintf(stderr, DEBUG_KEY "%s", s);
free(s);
}
static int get_kdcinfo(const char *realm, struct sssd_ctx *ctx)
{
int ret;
char *kdcinfo_name = NULL;
size_t len;
uint8_t buf[BUFSIZE + 1];
uint8_t *p;
int fd = -1;
len = strlen(realm) + strlen(KDCINFO_TMPL);
kdcinfo_name = calloc(1, len + 1);
if (kdcinfo_name == NULL) {
PLUGIN_DEBUG(("malloc failed.\n"));
return ENOMEM;
}
ret = snprintf(kdcinfo_name, len, KDCINFO_TMPL, realm);
if (ret < 0) {
PLUGIN_DEBUG(("snprintf failed"));
ret = EINVAL;
}
kdcinfo_name[len] = '\0';
fd = open(kdcinfo_name, O_RDONLY);
if (fd == -1) {
PLUGIN_DEBUG(("open failed [%d][%s].\n", errno, strerror(errno)));
ret = errno;
goto done;
}
len = BUFSIZE;
p = buf;
memset(buf, 0, BUFSIZE+1);
while (len != 0 && (ret = read(fd, p, len)) != 0) {
if (ret == -1) {
if (errno == EINTR) continue;
PLUGIN_DEBUG(("read failed [%d][%s].\n", errno, strerror(errno)));
close(fd);
goto done;
}
len -= ret;
p += ret;
}
close(fd);
if (len == 0) {
PLUGIN_DEBUG(("Content of kdcinfo file [%s] is [%d] or larger.\n",
kdcinfo_name, BUFSIZE));
}
PLUGIN_DEBUG(("Found kdcinfo [%s].\n", buf));
ret = getaddrinfo((char *) buf, "kerberos", NULL, &ctx->sssd_kdc_addrinfo);
if (ret != 0) {
PLUGIN_DEBUG(("getaddrinfo failed [%d][%s].\n", ret,
gai_strerror(ret)));
if (ret == EAI_SYSTEM) {
PLUGIN_DEBUG(("getaddrinfo failed [%d][%s].\n", errno,
strerror(errno)));
}
goto done;
}
ctx->sssd_realm = strdup(realm);
if (ctx->sssd_realm == NULL) {
PLUGIN_DEBUG(("strdup failed.\n"));
ret = ENOMEM;
goto done;
}
done:
free(kdcinfo_name);
return ret;
}
krb5_error_code sssd_krb5_locator_init(krb5_context context,
void **private_data)
{
struct sssd_ctx *ctx;
const char *dummy;
ctx = calloc(1,sizeof(struct sssd_ctx));
if (ctx == NULL) return ENOMEM;
dummy = getenv(SSSD_KRB5_LOCATOR_DEBUG);
if (dummy == NULL) {
ctx->debug = false;
} else {
ctx->debug = true;
PLUGIN_DEBUG(("sssd_krb5_locator_init called\n"));
}
*private_data = ctx;
return 0;
}
void sssd_krb5_locator_close(void *private_data)
{
struct sssd_ctx *ctx;
if (private_data == NULL) return;
ctx = (struct sssd_ctx *) private_data;
PLUGIN_DEBUG(("sssd_krb5_locator_close called\n"));
freeaddrinfo(ctx->sssd_kdc_addrinfo);
free(ctx->sssd_realm);
free(ctx);
private_data = NULL;
return;
}
krb5_error_code sssd_krb5_locator_lookup(void *private_data,
enum locate_service_type svc,
const char *realm,
int socktype,
int family,
int (*cbfunc)(void *, int, struct sockaddr *),
void *cbdata)
{
int ret;
struct addrinfo *ai;
struct sssd_ctx *ctx;
char hostip[NI_MAXHOST];
if (private_data == NULL) return KRB5_PLUGIN_NO_HANDLE;
ctx = (struct sssd_ctx *) private_data;
if (ctx->sssd_realm == NULL || strcmp(ctx->sssd_realm, realm) != 0) {
freeaddrinfo(ctx->sssd_kdc_addrinfo);
ctx->sssd_kdc_addrinfo = NULL;
free(ctx->sssd_realm);
ctx->sssd_realm = NULL;
ret = get_kdcinfo(realm, ctx);
if (ret != EOK) {
PLUGIN_DEBUG(("get_kdcinfo failed.\n"));
return KRB5_PLUGIN_NO_HANDLE;
}
}
PLUGIN_DEBUG(("sssd_realm[%s] requested realm[%s] family[%d] socktype[%d] "
"locate_service[%d]\n", ctx->sssd_realm, realm, family,
socktype, svc));
switch (svc) {
case locate_service_kdc:
case locate_service_master_kdc:
case locate_service_kadmin:
break;
case locate_service_krb524:
case locate_service_kpasswd:
return KRB5_PLUGIN_NO_HANDLE;
default:
return EINVAL;
}
switch (family) {
case AF_UNSPEC:
case AF_INET:
case AF_INET6:
break;
default:
return KRB5_PLUGIN_NO_HANDLE;
}
switch (socktype) {
case SOCK_STREAM:
case SOCK_DGRAM:
break;
default:
return EINVAL;
}
if (strcmp(realm, ctx->sssd_realm) != 0)
return KRB5_PLUGIN_NO_HANDLE;
for (ai = ctx->sssd_kdc_addrinfo; ai != NULL; ai = ai->ai_next) {
ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, hostip, NI_MAXHOST,
NULL, 0, NI_NUMERICHOST);
if (ret != 0) {
PLUGIN_DEBUG(("getnameinfo failed [%d][%s].\n", ret,
gai_strerror(ret)));
if (ret == EAI_SYSTEM) {
PLUGIN_DEBUG(("getnameinfo failed [%d][%s].\n", errno,
strerror(errno)));
}
}
PLUGIN_DEBUG(("addr[%s] family[%d] socktype[%d] - ", hostip,
ai->ai_family, ai->ai_socktype));
if ((family == AF_UNSPEC || ai->ai_family == family) &&
ai->ai_socktype == socktype) {
ret = cbfunc(cbdata, socktype, ai->ai_addr);
if (ret != 0) {
PLUGIN_DEBUG(("\ncbfunc failed\n"));
} else {
PLUGIN_DEBUG(("used\n"));
}
} else {
PLUGIN_DEBUG((" NOT used\n"));
}
}
return 0;
}
const krb5plugin_service_locate_ftable service_locator = {
0, /* version */
sssd_krb5_locator_init,
sssd_krb5_locator_close,
sssd_krb5_locator_lookup,
};
|