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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
/*
Unix SMB/CIFS implementation.
a WINS nsswitch module
Copyright (C) Andrew Tridgell 1999
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/>.
*/
#include "includes.h"
#include "nsswitch/winbind_nss.h"
#ifdef HAVE_NS_API_H
#include <ns_daemon.h>
#endif
#if HAVE_PTHREAD_H
#include <pthread.h>
#endif
#if HAVE_PTHREAD
static pthread_mutex_t wins_nss_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
#ifndef INADDRSZ
#define INADDRSZ 4
#endif
static int initialised;
NSS_STATUS _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
char *buffer, size_t buflen, int *h_errnop);
NSS_STATUS _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
char *buffer, size_t buflen, int *h_errnop);
static void nss_wins_init(void)
{
initialised = 1;
lp_set_cmdline("log level", "0");
TimeInit();
setup_logging("nss_wins",False);
lp_load_global_no_reinit(get_dyn_CONFIGFILE());
load_interfaces();
}
static struct in_addr *lookup_byname_backend(const char *name, int *count)
{
TALLOC_CTX *frame;
struct sockaddr_storage *address = NULL;
struct in_addr *ret = NULL;
NTSTATUS status;
const char *p;
size_t nbt_len;
int j;
if (!initialised) {
nss_wins_init();
}
*count = 0;
nbt_len = strlen(name);
if (nbt_len > MAX_NETBIOSNAME_LEN - 1) {
return NULL;
}
p = strchr(name, '.');
if (p != NULL) {
return NULL;
}
frame = talloc_stackframe();
/* always try with wins first */
status = resolve_wins(name, 0x00, talloc_tos(),
&address, count);
if (NT_STATUS_IS_OK(status)) {
if ( (ret = SMB_MALLOC_P(struct in_addr)) == NULL ) {
TALLOC_FREE(frame);
return NULL;
}
if (address[0].ss_family != AF_INET) {
free(ret);
TALLOC_FREE(frame);
return NULL;
}
*ret = ((struct sockaddr_in *)(void *)address)
->sin_addr;
TALLOC_FREE(frame);
return ret;
}
/* uggh, we have to broadcast to each interface in turn */
for (j=iface_count() - 1;j >= 0;j--) {
const struct in_addr *bcast = iface_n_bcast_v4(j);
struct sockaddr_storage ss;
struct sockaddr_storage *pss;
if (!bcast) {
continue;
}
in_addr_to_sockaddr_storage(&ss, *bcast);
status = name_query(name, 0x00, True, True, &ss,
talloc_tos(), &pss, count, NULL);
if (NT_STATUS_IS_OK(status) && (*count > 0)) {
if ((ret = SMB_MALLOC_P(struct in_addr)) == NULL) {
TALLOC_FREE(frame);
return NULL;
}
*ret = ((struct sockaddr_in *)pss)->sin_addr;
break;
}
}
TALLOC_FREE(frame);
return ret;
}
#ifdef HAVE_NS_API_H
static struct node_status *lookup_byaddr_backend(char *addr, int *count)
{
struct sockaddr_storage ss;
struct nmb_name nname;
struct node_status *result;
NTSTATUS status;
if (!initialised) {
nss_wins_init();
}
make_nmb_name(&nname, "*", 0);
if (!interpret_string_addr(&ss, addr, AI_NUMERICHOST)) {
return NULL;
}
status = node_status_query(NULL, &nname, &ss, &result, count, NULL);
if (!NT_STATUS_IS_OK(status)) {
return NULL;
}
return result;
}
/* IRIX version */
int init(void)
{
nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
nss_wins_init();
return NSD_OK;
}
int lookup(nsd_file_t *rq)
{
char *map;
char *key;
char *addr;
struct in_addr *ip_list;
struct node_status *status;
int i, count, len, size;
char response[1024];
bool found = False;
nsd_logprintf(NSD_LOG_MIN, "entering lookup (wins)\n");
if (! rq)
return NSD_ERROR;
map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
if (! map) {
rq->f_status = NS_FATAL;
return NSD_ERROR;
}
key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
if (! key || ! *key) {
rq->f_status = NS_FATAL;
return NSD_ERROR;
}
response[0] = '\0';
len = sizeof(response) - 2;
/*
* response needs to be a string of the following format
* ip_address[ ip_address]*\tname[ alias]*
*/
if (strcasecmp_m(map,"hosts.byaddr") == 0) {
if ( status = lookup_byaddr_backend(key, &count)) {
size = strlen(key) + 1;
if (size > len) {
talloc_free(status);
return NSD_ERROR;
}
len -= size;
strncat(response,key,size);
strncat(response,"\t",1);
for (i = 0; i < count; i++) {
/* ignore group names */
if (status[i].flags & 0x80) continue;
if (status[i].type == 0x20) {
size = sizeof(status[i].name) + 1;
if (size > len) {
talloc_free(status);
return NSD_ERROR;
}
len -= size;
strncat(response, status[i].name, size);
strncat(response, " ", 1);
found = True;
}
}
response[strlen(response)-1] = '\n';
talloc_free(status);
}
} else if (strcasecmp_m(map,"hosts.byname") == 0) {
if (ip_list = lookup_byname_backend(key, &count)) {
for (i = count; i ; i--) {
addr = inet_ntoa(ip_list[i-1]);
size = strlen(addr) + 1;
if (size > len) {
free(ip_list);
return NSD_ERROR;
}
len -= size;
if (i != 0)
response[strlen(response)-1] = ' ';
strncat(response,addr,size);
strncat(response,"\t",1);
}
size = strlen(key) + 1;
if (size > len) {
free(ip_list);
return NSD_ERROR;
}
strncat(response,key,size);
strncat(response,"\n",1);
found = True;
free(ip_list);
}
}
if (found) {
nsd_logprintf(NSD_LOG_LOW, "lookup (wins %s) %s\n",map,response);
nsd_set_result(rq,NS_SUCCESS,response,strlen(response),VOLATILE);
return NSD_OK;
}
nsd_logprintf(NSD_LOG_LOW, "lookup (wins) not found\n");
rq->f_status = NS_NOTFOUND;
return NSD_NEXT;
}
#else
/* Allocate some space from the nss static buffer. The buffer and buflen
are the pointers passed in by the C library to the _nss_*_*
functions. */
static char *get_static(char **buffer, size_t *buflen, int len)
{
char *result;
/* Error check. We return false if things aren't set up right, or
there isn't enough buffer space left. */
if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
return NULL;
}
/* Return an index into the static buffer */
result = *buffer;
*buffer += len;
*buflen -= len;
return result;
}
/****************************************************************************
gethostbyname() - we ignore any domain portion of the name and only
handle names that are at most 15 characters long
**************************************************************************/
NSS_STATUS
_nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
char *buffer, size_t buflen, int *h_errnop)
{
NSS_STATUS nss_status = NSS_STATUS_SUCCESS;
struct in_addr *ip_list;
int i, count;
fstring name;
size_t namelen;
TALLOC_CTX *frame;
#if HAVE_PTHREAD
pthread_mutex_lock(&wins_nss_mutex);
#endif
frame = talloc_stackframe();
memset(he, '\0', sizeof(*he));
fstrcpy(name, hostname);
/* Do lookup */
ip_list = lookup_byname_backend(name, &count);
if (!ip_list) {
nss_status = NSS_STATUS_NOTFOUND;
goto out;
}
/* Copy h_name */
namelen = strlen(name) + 1;
if ((he->h_name = get_static(&buffer, &buflen, namelen)) == NULL) {
free(ip_list);
nss_status = NSS_STATUS_TRYAGAIN;
goto out;
}
memcpy(he->h_name, name, namelen);
/* Copy h_addr_list, align to pointer boundary first */
if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
i = sizeof(char*) - i;
if (get_static(&buffer, &buflen, i) == NULL) {
free(ip_list);
nss_status = NSS_STATUS_TRYAGAIN;
goto out;
}
if ((he->h_addr_list = (char **)get_static(
&buffer, &buflen, (count + 1) * sizeof(char *))) == NULL) {
free(ip_list);
nss_status = NSS_STATUS_TRYAGAIN;
goto out;
}
for (i = 0; i < count; i++) {
if ((he->h_addr_list[i] = get_static(&buffer, &buflen,
INADDRSZ)) == NULL) {
free(ip_list);
nss_status = NSS_STATUS_TRYAGAIN;
goto out;
}
memcpy(he->h_addr_list[i], &ip_list[i], INADDRSZ);
}
he->h_addr_list[count] = NULL;
free(ip_list);
/* Set h_addr_type and h_length */
he->h_addrtype = AF_INET;
he->h_length = INADDRSZ;
/* Set h_aliases */
if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
i = sizeof(char*) - i;
if (get_static(&buffer, &buflen, i) == NULL) {
nss_status = NSS_STATUS_TRYAGAIN;
goto out;
}
if ((he->h_aliases = (char **)get_static(
&buffer, &buflen, sizeof(char *))) == NULL) {
nss_status = NSS_STATUS_TRYAGAIN;
goto out;
}
he->h_aliases[0] = NULL;
nss_status = NSS_STATUS_SUCCESS;
out:
TALLOC_FREE(frame);
#if HAVE_PTHREAD
pthread_mutex_unlock(&wins_nss_mutex);
#endif
return nss_status;
}
NSS_STATUS
_nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
char *buffer, size_t buflen, int *h_errnop)
{
NSS_STATUS nss_status;
if(af!=AF_INET) {
*h_errnop = NO_DATA;
nss_status = NSS_STATUS_UNAVAIL;
} else {
nss_status = _nss_wins_gethostbyname_r(
name, he, buffer, buflen, h_errnop);
}
return nss_status;
}
#endif
|