~vcs-imports/samba/main

« back to all changes in this revision

Viewing changes to source/nsswitch/wins.c

  • Committer: jerry
  • Date: 2006-07-14 21:48:39 UTC
  • Revision ID: vcs-imports@canonical.com-20060714214839-586d8c489a8fcead
gutting trunk to move to svn:externals

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* 
2
 
   Unix SMB/CIFS implementation.
3
 
   a WINS nsswitch module 
4
 
   Copyright (C) Andrew Tridgell 1999
5
 
   
6
 
   This program is free software; you can redistribute it and/or modify
7
 
   it under the terms of the GNU General Public License as published by
8
 
   the Free Software Foundation; either version 2 of the License, or
9
 
   (at your option) any later version.
10
 
   
11
 
   This program is distributed in the hope that it will be useful,
12
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
   GNU General Public License for more details.
15
 
   
16
 
   You should have received a copy of the GNU General Public License
17
 
   along with this program; if not, write to the Free Software
18
 
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
 
   
20
 
*/
21
 
 
22
 
#include "includes.h"
23
 
#ifdef HAVE_NS_API_H
24
 
#undef VOLATILE
25
 
 
26
 
#include <ns_daemon.h>
27
 
#endif
28
 
 
29
 
#ifndef INADDRSZ
30
 
#define INADDRSZ 4
31
 
#endif
32
 
 
33
 
static int initialised;
34
 
 
35
 
extern BOOL AllowDebugChange;
36
 
 
37
 
/* Use our own create socket code so we don't recurse.... */
38
 
 
39
 
static int wins_lookup_open_socket_in(void)
40
 
{
41
 
        struct sockaddr_in sock;
42
 
        int val=1;
43
 
        int res;
44
 
 
45
 
        memset((char *)&sock,'\0',sizeof(sock));
46
 
 
47
 
#ifdef HAVE_SOCK_SIN_LEN
48
 
        sock.sin_len = sizeof(sock);
49
 
#endif
50
 
        sock.sin_port = 0;
51
 
        sock.sin_family = AF_INET;
52
 
        sock.sin_addr.s_addr = interpret_addr("0.0.0.0");
53
 
        res = socket(AF_INET, SOCK_DGRAM, 0);
54
 
        if (res == -1)
55
 
                return -1;
56
 
 
57
 
        setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val));
58
 
#ifdef SO_REUSEPORT
59
 
        setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val));
60
 
#endif /* SO_REUSEPORT */
61
 
 
62
 
        /* now we've got a socket - we need to bind it */
63
 
 
64
 
        if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0) {
65
 
                close(res);
66
 
                return(-1);
67
 
        }
68
 
 
69
 
        set_socket_options(res,"SO_BROADCAST");
70
 
 
71
 
        return res;
72
 
}
73
 
 
74
 
 
75
 
static void nss_wins_init(void)
76
 
{
77
 
        initialised = 1;
78
 
        DEBUGLEVEL = 0;
79
 
        AllowDebugChange = False;
80
 
 
81
 
        TimeInit();
82
 
        setup_logging("nss_wins",False);
83
 
        lp_load(dyn_CONFIGFILE,True,False,False,True);
84
 
        load_interfaces();
85
 
}
86
 
 
87
 
static struct in_addr *lookup_byname_backend(const char *name, int *count)
88
 
{
89
 
        int fd = -1;
90
 
        struct ip_service *address = NULL;
91
 
        struct in_addr *ret = NULL;
92
 
        int j, flags = 0;
93
 
 
94
 
        if (!initialised) {
95
 
                nss_wins_init();
96
 
        }
97
 
 
98
 
        *count = 0;
99
 
 
100
 
        /* always try with wins first */
101
 
        if (resolve_wins(name,0x00,&address,count)) {
102
 
                if ( (ret = SMB_MALLOC_P(struct in_addr)) == NULL ) {
103
 
                        free( address );
104
 
                        return NULL;
105
 
                }
106
 
                *ret = address[0].ip;
107
 
                free( address );
108
 
                return ret;
109
 
        }
110
 
 
111
 
        fd = wins_lookup_open_socket_in();
112
 
        if (fd == -1) {
113
 
                return NULL;
114
 
        }
115
 
 
116
 
        /* uggh, we have to broadcast to each interface in turn */
117
 
        for (j=iface_count() - 1;j >= 0;j--) {
118
 
                struct in_addr *bcast = iface_n_bcast(j);
119
 
                ret = name_query(fd,name,0x00,True,True,*bcast,count, &flags, NULL);
120
 
                if (ret) break;
121
 
        }
122
 
 
123
 
        close(fd);
124
 
        return ret;
125
 
}
126
 
 
127
 
#ifdef HAVE_NS_API_H
128
 
 
129
 
static NODE_STATUS_STRUCT *lookup_byaddr_backend(char *addr, int *count)
130
 
{
131
 
        int fd;
132
 
        struct in_addr  ip;
133
 
        struct nmb_name nname;
134
 
        NODE_STATUS_STRUCT *status;
135
 
 
136
 
        if (!initialised) {
137
 
                nss_wins_init();
138
 
        }
139
 
 
140
 
        fd = wins_lookup_open_socket_in();
141
 
        if (fd == -1)
142
 
                return NULL;
143
 
 
144
 
        make_nmb_name(&nname, "*", 0);
145
 
        ip = *interpret_addr2(addr);
146
 
        status = node_status_query(fd,&nname,ip, count, NULL);
147
 
 
148
 
        close(fd);
149
 
        return status;
150
 
}
151
 
 
152
 
/* IRIX version */
153
 
 
154
 
int init(void)
155
 
{
156
 
        nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
157
 
        nss_wins_init();
158
 
        return NSD_OK;
159
 
}
160
 
 
161
 
int lookup(nsd_file_t *rq)
162
 
{
163
 
        char *map;
164
 
        char *key;
165
 
        char *addr;
166
 
        struct in_addr *ip_list;
167
 
        NODE_STATUS_STRUCT *status;
168
 
        int i, count, len, size;
169
 
        char response[1024];
170
 
        BOOL found = False;
171
 
 
172
 
        nsd_logprintf(NSD_LOG_MIN, "entering lookup (wins)\n");
173
 
        if (! rq) 
174
 
                return NSD_ERROR;
175
 
 
176
 
        map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
177
 
        if (! map) {
178
 
                rq->f_status = NS_FATAL;
179
 
                return NSD_ERROR;
180
 
        }
181
 
 
182
 
        key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
183
 
        if (! key || ! *key) {
184
 
                rq->f_status = NS_FATAL;
185
 
                return NSD_ERROR;
186
 
        }
187
 
 
188
 
        response[0] = '\0';
189
 
        len = sizeof(response) - 2;
190
 
 
191
 
        /* 
192
 
         * response needs to be a string of the following format
193
 
         * ip_address[ ip_address]*\tname[ alias]*
194
 
         */
195
 
        if (StrCaseCmp(map,"hosts.byaddr") == 0) {
196
 
                if ( status = lookup_byaddr_backend(key, &count)) {
197
 
                    size = strlen(key) + 1;
198
 
                    if (size > len) {
199
 
                        free(status);
200
 
                        return NSD_ERROR;
201
 
                    }
202
 
                    len -= size;
203
 
                    strncat(response,key,size);
204
 
                    strncat(response,"\t",1);
205
 
                    for (i = 0; i < count; i++) {
206
 
                        /* ignore group names */
207
 
                        if (status[i].flags & 0x80) continue;
208
 
                        if (status[i].type == 0x20) {
209
 
                                size = sizeof(status[i].name) + 1;
210
 
                                if (size > len) {
211
 
                                    free(status);
212
 
                                    return NSD_ERROR;
213
 
                                }
214
 
                                len -= size;
215
 
                                strncat(response, status[i].name, size);
216
 
                                strncat(response, " ", 1);
217
 
                                found = True;
218
 
                        }
219
 
                    }
220
 
                    response[strlen(response)-1] = '\n';
221
 
                    free(status);
222
 
                }
223
 
        } else if (StrCaseCmp(map,"hosts.byname") == 0) {
224
 
            if (ip_list = lookup_byname_backend(key, &count)) {
225
 
                for (i = count; i ; i--) {
226
 
                    addr = inet_ntoa(ip_list[i-1]);
227
 
                    size = strlen(addr) + 1;
228
 
                    if (size > len) {
229
 
                        free(ip_list);
230
 
                        return NSD_ERROR;
231
 
                    }
232
 
                    len -= size;
233
 
                    if (i != 0)
234
 
                        response[strlen(response)-1] = ' ';
235
 
                    strncat(response,addr,size);
236
 
                    strncat(response,"\t",1);
237
 
                }
238
 
                size = strlen(key) + 1;
239
 
                if (size > len) {
240
 
                    free(ip_list);
241
 
                    return NSD_ERROR;
242
 
                }   
243
 
                strncat(response,key,size);
244
 
                strncat(response,"\n",1);
245
 
                found = True;
246
 
                free(ip_list);
247
 
            }
248
 
        }
249
 
 
250
 
        if (found) {
251
 
            nsd_logprintf(NSD_LOG_LOW, "lookup (wins %s) %s\n",map,response);
252
 
            nsd_set_result(rq,NS_SUCCESS,response,strlen(response),VOLATILE);
253
 
            return NSD_OK;
254
 
        }
255
 
        nsd_logprintf(NSD_LOG_LOW, "lookup (wins) not found\n");
256
 
        rq->f_status = NS_NOTFOUND;
257
 
        return NSD_NEXT;
258
 
}
259
 
 
260
 
#else
261
 
 
262
 
/* Allocate some space from the nss static buffer.  The buffer and buflen
263
 
   are the pointers passed in by the C library to the _nss_*_*
264
 
   functions. */
265
 
 
266
 
static char *get_static(char **buffer, size_t *buflen, int len)
267
 
{
268
 
        char *result;
269
 
 
270
 
        /* Error check.  We return false if things aren't set up right, or
271
 
           there isn't enough buffer space left. */
272
 
        
273
 
        if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
274
 
                return NULL;
275
 
        }
276
 
 
277
 
        /* Return an index into the static buffer */
278
 
 
279
 
        result = *buffer;
280
 
        *buffer += len;
281
 
        *buflen -= len;
282
 
 
283
 
        return result;
284
 
}
285
 
 
286
 
/****************************************************************************
287
 
gethostbyname() - we ignore any domain portion of the name and only
288
 
handle names that are at most 15 characters long
289
 
  **************************************************************************/
290
 
NSS_STATUS
291
 
_nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
292
 
                          char *buffer, size_t buflen, int *h_errnop)
293
 
{
294
 
        struct in_addr *ip_list;
295
 
        int i, count;
296
 
        fstring name;
297
 
        size_t namelen;
298
 
                
299
 
        memset(he, '\0', sizeof(*he));
300
 
        fstrcpy(name, hostname);
301
 
 
302
 
        /* Do lookup */
303
 
 
304
 
        ip_list = lookup_byname_backend(name, &count);
305
 
 
306
 
        if (!ip_list)
307
 
                return NSS_STATUS_NOTFOUND;
308
 
 
309
 
        /* Copy h_name */
310
 
 
311
 
        namelen = strlen(name) + 1;
312
 
 
313
 
        if ((he->h_name = get_static(&buffer, &buflen, namelen)) == NULL)
314
 
                return NSS_STATUS_TRYAGAIN;
315
 
 
316
 
        memcpy(he->h_name, name, namelen);
317
 
 
318
 
        /* Copy h_addr_list, align to pointer boundary first */
319
 
 
320
 
        if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
321
 
                i = sizeof(char*) - i;
322
 
 
323
 
        if (get_static(&buffer, &buflen, i) == NULL)
324
 
                return NSS_STATUS_TRYAGAIN;
325
 
 
326
 
        if ((he->h_addr_list = (char **)get_static(
327
 
                     &buffer, &buflen, (count + 1) * sizeof(char *))) == NULL)
328
 
                return NSS_STATUS_TRYAGAIN;
329
 
 
330
 
        for (i = 0; i < count; i++) {
331
 
                if ((he->h_addr_list[i] = get_static(&buffer, &buflen,
332
 
                                                     INADDRSZ)) == NULL)
333
 
                        return NSS_STATUS_TRYAGAIN;
334
 
                memcpy(he->h_addr_list[i], &ip_list[i], INADDRSZ);
335
 
        }
336
 
 
337
 
        he->h_addr_list[count] = NULL;
338
 
 
339
 
        if (ip_list)
340
 
                free(ip_list);
341
 
 
342
 
        /* Set h_addr_type and h_length */
343
 
 
344
 
        he->h_addrtype = AF_INET;
345
 
        he->h_length = INADDRSZ;
346
 
 
347
 
        /* Set h_aliases */
348
 
 
349
 
        if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
350
 
                i = sizeof(char*) - i;
351
 
 
352
 
        if (get_static(&buffer, &buflen, i) == NULL)
353
 
                return NSS_STATUS_TRYAGAIN;
354
 
 
355
 
        if ((he->h_aliases = (char **)get_static(
356
 
                     &buffer, &buflen, sizeof(char *))) == NULL)
357
 
                return NSS_STATUS_TRYAGAIN;
358
 
 
359
 
        he->h_aliases[0] = NULL;
360
 
 
361
 
        return NSS_STATUS_SUCCESS;
362
 
}
363
 
 
364
 
 
365
 
NSS_STATUS
366
 
_nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
367
 
                           char *buffer, size_t buflen, int *h_errnop)
368
 
{
369
 
        if(af!=AF_INET) {
370
 
                *h_errnop = NO_DATA;
371
 
                return NSS_STATUS_UNAVAIL;
372
 
        }
373
 
 
374
 
        return _nss_wins_gethostbyname_r(
375
 
                name, he, buffer, buflen, h_errnop);
376
 
}
377
 
#endif