s4:rpc_server/samr: use the same logic in *info_DomInfo7() as in info_DomGeneralInfor...
[Samba.git] / nsswitch / wins.c
blob5127ee479f40812677f465734d94bd652604e811
1 /*
2 Unix SMB/CIFS implementation.
3 a WINS nsswitch module
4 Copyright (C) Andrew Tridgell 1999
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 3 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "nsswitch/winbind_nss.h"
24 #ifdef HAVE_NS_API_H
26 #include <ns_daemon.h>
27 #endif
29 #if HAVE_PTHREAD_H
30 #include <pthread.h>
31 #endif
33 #if HAVE_PTHREAD
34 static pthread_mutex_t wins_nss_mutex = PTHREAD_MUTEX_INITIALIZER;
35 #endif
37 #ifndef INADDRSZ
38 #define INADDRSZ 4
39 #endif
41 static int initialised;
43 NSS_STATUS _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
44 char *buffer, size_t buflen, int *h_errnop);
45 NSS_STATUS _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
46 char *buffer, size_t buflen, int *h_errnop);
48 static void nss_wins_init(void)
50 initialised = 1;
51 load_case_tables_library();
52 lp_set_cmdline("log level", "0");
54 TimeInit();
55 setup_logging("nss_wins",False);
56 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
57 load_interfaces();
60 static struct in_addr *lookup_byname_backend(const char *name, int *count)
62 TALLOC_CTX *frame;
63 struct sockaddr_storage *address = NULL;
64 struct in_addr *ret = NULL;
65 NTSTATUS status;
66 const char *p;
67 size_t nbt_len;
68 int j;
70 if (!initialised) {
71 nss_wins_init();
74 *count = 0;
76 nbt_len = strlen(name);
77 if (nbt_len > MAX_NETBIOSNAME_LEN - 1) {
78 return NULL;
80 p = strchr(name, '.');
81 if (p != NULL) {
82 return NULL;
85 frame = talloc_stackframe();
86 /* always try with wins first */
87 status = resolve_wins(name, 0x00, talloc_tos(),
88 &address, count);
89 if (NT_STATUS_IS_OK(status)) {
90 if ( (ret = SMB_MALLOC_P(struct in_addr)) == NULL ) {
91 TALLOC_FREE(frame);
92 return NULL;
94 if (address[0].ss_family != AF_INET) {
95 free(ret);
96 TALLOC_FREE(frame);
97 return NULL;
99 *ret = ((struct sockaddr_in *)(void *)address)
100 ->sin_addr;
101 TALLOC_FREE(frame);
102 return ret;
105 /* uggh, we have to broadcast to each interface in turn */
106 for (j=iface_count() - 1;j >= 0;j--) {
107 const struct in_addr *bcast = iface_n_bcast_v4(j);
108 struct sockaddr_storage ss;
109 struct sockaddr_storage *pss;
111 if (!bcast) {
112 continue;
114 in_addr_to_sockaddr_storage(&ss, *bcast);
115 status = name_query(name, 0x00, True, True, &ss,
116 talloc_tos(), &pss, count, NULL);
117 if (NT_STATUS_IS_OK(status) && (*count > 0)) {
118 if ((ret = SMB_MALLOC_P(struct in_addr)) == NULL) {
119 TALLOC_FREE(frame);
120 return NULL;
122 *ret = ((struct sockaddr_in *)pss)->sin_addr;
123 break;
126 TALLOC_FREE(frame);
127 return ret;
130 #ifdef HAVE_NS_API_H
132 static struct node_status *lookup_byaddr_backend(char *addr, int *count)
134 struct sockaddr_storage ss;
135 struct nmb_name nname;
136 struct node_status *result;
137 NTSTATUS status;
139 if (!initialised) {
140 nss_wins_init();
143 make_nmb_name(&nname, "*", 0);
144 if (!interpret_string_addr(&ss, addr, AI_NUMERICHOST)) {
145 return NULL;
147 status = node_status_query(NULL, &nname, &ss, &result, count, NULL);
148 if (!NT_STATUS_IS_OK(status)) {
149 return NULL;
152 return result;
155 /* IRIX version */
157 int init(void)
159 nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
160 nss_wins_init();
161 return NSD_OK;
164 int lookup(nsd_file_t *rq)
166 char *map;
167 char *key;
168 char *addr;
169 struct in_addr *ip_list;
170 struct node_status *status;
171 int i, count, len, size;
172 char response[1024];
173 bool found = False;
175 nsd_logprintf(NSD_LOG_MIN, "entering lookup (wins)\n");
176 if (! rq)
177 return NSD_ERROR;
179 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
180 if (! map) {
181 rq->f_status = NS_FATAL;
182 return NSD_ERROR;
185 key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
186 if (! key || ! *key) {
187 rq->f_status = NS_FATAL;
188 return NSD_ERROR;
191 response[0] = '\0';
192 len = sizeof(response) - 2;
195 * response needs to be a string of the following format
196 * ip_address[ ip_address]*\tname[ alias]*
198 if (strcasecmp_m(map,"hosts.byaddr") == 0) {
199 if ( status = lookup_byaddr_backend(key, &count)) {
200 size = strlen(key) + 1;
201 if (size > len) {
202 talloc_free(status);
203 return NSD_ERROR;
205 len -= size;
206 strncat(response,key,size);
207 strncat(response,"\t",1);
208 for (i = 0; i < count; i++) {
209 /* ignore group names */
210 if (status[i].flags & 0x80) continue;
211 if (status[i].type == 0x20) {
212 size = sizeof(status[i].name) + 1;
213 if (size > len) {
214 talloc_free(status);
215 return NSD_ERROR;
217 len -= size;
218 strncat(response, status[i].name, size);
219 strncat(response, " ", 1);
220 found = True;
223 response[strlen(response)-1] = '\n';
224 talloc_free(status);
226 } else if (strcasecmp_m(map,"hosts.byname") == 0) {
227 if (ip_list = lookup_byname_backend(key, &count)) {
228 for (i = count; i ; i--) {
229 addr = inet_ntoa(ip_list[i-1]);
230 size = strlen(addr) + 1;
231 if (size > len) {
232 free(ip_list);
233 return NSD_ERROR;
235 len -= size;
236 if (i != 0)
237 response[strlen(response)-1] = ' ';
238 strncat(response,addr,size);
239 strncat(response,"\t",1);
241 size = strlen(key) + 1;
242 if (size > len) {
243 free(ip_list);
244 return NSD_ERROR;
246 strncat(response,key,size);
247 strncat(response,"\n",1);
248 found = True;
249 free(ip_list);
253 if (found) {
254 nsd_logprintf(NSD_LOG_LOW, "lookup (wins %s) %s\n",map,response);
255 nsd_set_result(rq,NS_SUCCESS,response,strlen(response),VOLATILE);
256 return NSD_OK;
258 nsd_logprintf(NSD_LOG_LOW, "lookup (wins) not found\n");
259 rq->f_status = NS_NOTFOUND;
260 return NSD_NEXT;
263 #else
265 /* Allocate some space from the nss static buffer. The buffer and buflen
266 are the pointers passed in by the C library to the _nss_*_*
267 functions. */
269 static char *get_static(char **buffer, size_t *buflen, int len)
271 char *result;
273 /* Error check. We return false if things aren't set up right, or
274 there isn't enough buffer space left. */
276 if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
277 return NULL;
280 /* Return an index into the static buffer */
282 result = *buffer;
283 *buffer += len;
284 *buflen -= len;
286 return result;
289 /****************************************************************************
290 gethostbyname() - we ignore any domain portion of the name and only
291 handle names that are at most 15 characters long
292 **************************************************************************/
293 NSS_STATUS
294 _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
295 char *buffer, size_t buflen, int *h_errnop)
297 NSS_STATUS nss_status = NSS_STATUS_SUCCESS;
298 struct in_addr *ip_list;
299 int i, count;
300 fstring name;
301 size_t namelen;
302 TALLOC_CTX *frame;
304 #if HAVE_PTHREAD
305 pthread_mutex_lock(&wins_nss_mutex);
306 #endif
308 frame = talloc_stackframe();
310 memset(he, '\0', sizeof(*he));
311 fstrcpy(name, hostname);
313 /* Do lookup */
315 ip_list = lookup_byname_backend(name, &count);
317 if (!ip_list) {
318 nss_status = NSS_STATUS_NOTFOUND;
319 goto out;
322 /* Copy h_name */
324 namelen = strlen(name) + 1;
326 if ((he->h_name = get_static(&buffer, &buflen, namelen)) == NULL) {
327 free(ip_list);
328 nss_status = NSS_STATUS_TRYAGAIN;
329 goto out;
332 memcpy(he->h_name, name, namelen);
334 /* Copy h_addr_list, align to pointer boundary first */
336 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
337 i = sizeof(char*) - i;
339 if (get_static(&buffer, &buflen, i) == NULL) {
340 free(ip_list);
341 nss_status = NSS_STATUS_TRYAGAIN;
342 goto out;
345 if ((he->h_addr_list = (char **)get_static(
346 &buffer, &buflen, (count + 1) * sizeof(char *))) == NULL) {
347 free(ip_list);
348 nss_status = NSS_STATUS_TRYAGAIN;
349 goto out;
352 for (i = 0; i < count; i++) {
353 if ((he->h_addr_list[i] = get_static(&buffer, &buflen,
354 INADDRSZ)) == NULL) {
355 free(ip_list);
356 nss_status = NSS_STATUS_TRYAGAIN;
357 goto out;
359 memcpy(he->h_addr_list[i], &ip_list[i], INADDRSZ);
362 he->h_addr_list[count] = NULL;
364 free(ip_list);
366 /* Set h_addr_type and h_length */
368 he->h_addrtype = AF_INET;
369 he->h_length = INADDRSZ;
371 /* Set h_aliases */
373 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
374 i = sizeof(char*) - i;
376 if (get_static(&buffer, &buflen, i) == NULL) {
377 nss_status = NSS_STATUS_TRYAGAIN;
378 goto out;
381 if ((he->h_aliases = (char **)get_static(
382 &buffer, &buflen, sizeof(char *))) == NULL) {
383 nss_status = NSS_STATUS_TRYAGAIN;
384 goto out;
387 he->h_aliases[0] = NULL;
389 nss_status = NSS_STATUS_SUCCESS;
391 out:
393 TALLOC_FREE(frame);
395 #if HAVE_PTHREAD
396 pthread_mutex_unlock(&wins_nss_mutex);
397 #endif
398 return nss_status;
402 NSS_STATUS
403 _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
404 char *buffer, size_t buflen, int *h_errnop)
406 NSS_STATUS nss_status;
408 if(af!=AF_INET) {
409 *h_errnop = NO_DATA;
410 nss_status = NSS_STATUS_UNAVAIL;
411 } else {
412 nss_status = _nss_wins_gethostbyname_r(
413 name, he, buffer, buflen, h_errnop);
415 return nss_status;
417 #endif