2 Unix SMB/CIFS implementation.
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/>.
24 #include <ns_daemon.h>
32 static pthread_mutex_t wins_nss_mutex = PTHREAD_MUTEX_INITIALIZER;
39 static int initialised;
41 extern bool AllowDebugChange;
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 /* Use our own create socket code so we don't recurse.... */
50 static int wins_lookup_open_socket_in(void)
52 struct sockaddr_in sock;
56 memset((char *)&sock,'\0',sizeof(sock));
58 #ifdef HAVE_SOCK_SIN_LEN
59 sock.sin_len = sizeof(sock);
62 sock.sin_family = AF_INET;
63 sock.sin_addr.s_addr = interpret_addr("0.0.0.0");
64 res = socket(AF_INET, SOCK_DGRAM, 0);
68 if (setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val)) != 0) {
73 if (setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val)) != 0) {
77 #endif /* SO_REUSEPORT */
79 /* now we've got a socket - we need to bind it */
81 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0) {
86 set_socket_options(res,"SO_BROADCAST");
92 static void nss_wins_init(void)
96 AllowDebugChange = False;
99 setup_logging("nss_wins",False);
101 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
105 static struct in_addr *lookup_byname_backend(const char *name, int *count)
108 struct ip_service *address = NULL;
109 struct in_addr *ret = NULL;
118 /* always try with wins first */
119 if (NT_STATUS_IS_OK(resolve_wins(name,0x00,&address,count))) {
120 if ( (ret = SMB_MALLOC_P(struct in_addr)) == NULL ) {
124 if (address[0].ss.ss_family != AF_INET) {
129 *ret = ((struct sockaddr_in *)&address[0].ss)->sin_addr;
134 fd = wins_lookup_open_socket_in();
139 /* uggh, we have to broadcast to each interface in turn */
140 for (j=iface_count() - 1;j >= 0;j--) {
141 const struct in_addr *bcast = iface_n_bcast_v4(j);
142 struct sockaddr_storage ss;
143 struct sockaddr_storage *pss;
147 in_addr_to_sockaddr_storage(&ss, *bcast);
148 pss = name_query(fd,name,0x00,True,True,&ss,count, &flags, NULL);
150 if ((ret = SMB_MALLOC_P(struct in_addr)) == NULL) {
153 *ret = ((struct sockaddr_in *)pss)->sin_addr;
164 static NODE_STATUS_STRUCT *lookup_byaddr_backend(char *addr, int *count)
167 struct sockaddr_storage ss;
168 struct nmb_name nname;
169 NODE_STATUS_STRUCT *status;
175 fd = wins_lookup_open_socket_in();
179 make_nmb_name(&nname, "*", 0);
180 if (!interpret_string_addr(&ss, addr, AI_NUMERICHOST)) {
183 status = node_status_query(fd, &nname, &ss, count, NULL);
193 nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
198 int lookup(nsd_file_t *rq)
203 struct in_addr *ip_list;
204 NODE_STATUS_STRUCT *status;
205 int i, count, len, size;
209 nsd_logprintf(NSD_LOG_MIN, "entering lookup (wins)\n");
213 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
215 rq->f_status = NS_FATAL;
219 key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
220 if (! key || ! *key) {
221 rq->f_status = NS_FATAL;
226 len = sizeof(response) - 2;
229 * response needs to be a string of the following format
230 * ip_address[ ip_address]*\tname[ alias]*
232 if (StrCaseCmp(map,"hosts.byaddr") == 0) {
233 if ( status = lookup_byaddr_backend(key, &count)) {
234 size = strlen(key) + 1;
240 strncat(response,key,size);
241 strncat(response,"\t",1);
242 for (i = 0; i < count; i++) {
243 /* ignore group names */
244 if (status[i].flags & 0x80) continue;
245 if (status[i].type == 0x20) {
246 size = sizeof(status[i].name) + 1;
252 strncat(response, status[i].name, size);
253 strncat(response, " ", 1);
257 response[strlen(response)-1] = '\n';
260 } else if (StrCaseCmp(map,"hosts.byname") == 0) {
261 if (ip_list = lookup_byname_backend(key, &count)) {
262 for (i = count; i ; i--) {
263 addr = inet_ntoa(ip_list[i-1]);
264 size = strlen(addr) + 1;
271 response[strlen(response)-1] = ' ';
272 strncat(response,addr,size);
273 strncat(response,"\t",1);
275 size = strlen(key) + 1;
280 strncat(response,key,size);
281 strncat(response,"\n",1);
288 nsd_logprintf(NSD_LOG_LOW, "lookup (wins %s) %s\n",map,response);
289 nsd_set_result(rq,NS_SUCCESS,response,strlen(response),VOLATILE);
292 nsd_logprintf(NSD_LOG_LOW, "lookup (wins) not found\n");
293 rq->f_status = NS_NOTFOUND;
299 /* Allocate some space from the nss static buffer. The buffer and buflen
300 are the pointers passed in by the C library to the _nss_*_*
303 static char *get_static(char **buffer, size_t *buflen, int len)
307 /* Error check. We return false if things aren't set up right, or
308 there isn't enough buffer space left. */
310 if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
314 /* Return an index into the static buffer */
323 /****************************************************************************
324 gethostbyname() - we ignore any domain portion of the name and only
325 handle names that are at most 15 characters long
326 **************************************************************************/
328 _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
329 char *buffer, size_t buflen, int *h_errnop)
331 NSS_STATUS nss_status = NSS_STATUS_SUCCESS;
332 struct in_addr *ip_list;
338 pthread_mutex_lock(&wins_nss_mutex);
341 memset(he, '\0', sizeof(*he));
342 fstrcpy(name, hostname);
346 ip_list = lookup_byname_backend(name, &count);
349 nss_status = NSS_STATUS_NOTFOUND;
355 namelen = strlen(name) + 1;
357 if ((he->h_name = get_static(&buffer, &buflen, namelen)) == NULL) {
359 nss_status = NSS_STATUS_TRYAGAIN;
363 memcpy(he->h_name, name, namelen);
365 /* Copy h_addr_list, align to pointer boundary first */
367 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
368 i = sizeof(char*) - i;
370 if (get_static(&buffer, &buflen, i) == NULL) {
372 nss_status = NSS_STATUS_TRYAGAIN;
376 if ((he->h_addr_list = (char **)get_static(
377 &buffer, &buflen, (count + 1) * sizeof(char *))) == NULL) {
379 nss_status = NSS_STATUS_TRYAGAIN;
383 for (i = 0; i < count; i++) {
384 if ((he->h_addr_list[i] = get_static(&buffer, &buflen,
385 INADDRSZ)) == NULL) {
387 nss_status = NSS_STATUS_TRYAGAIN;
390 memcpy(he->h_addr_list[i], &ip_list[i], INADDRSZ);
393 he->h_addr_list[count] = NULL;
397 /* Set h_addr_type and h_length */
399 he->h_addrtype = AF_INET;
400 he->h_length = INADDRSZ;
404 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
405 i = sizeof(char*) - i;
407 if (get_static(&buffer, &buflen, i) == NULL) {
408 nss_status = NSS_STATUS_TRYAGAIN;
412 if ((he->h_aliases = (char **)get_static(
413 &buffer, &buflen, sizeof(char *))) == NULL) {
414 nss_status = NSS_STATUS_TRYAGAIN;
418 he->h_aliases[0] = NULL;
420 nss_status = NSS_STATUS_SUCCESS;
425 pthread_mutex_unlock(&wins_nss_mutex);
432 _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
433 char *buffer, size_t buflen, int *h_errnop)
435 NSS_STATUS nss_status;
439 nss_status = NSS_STATUS_UNAVAIL;
441 nss_status = _nss_wins_gethostbyname_r(
442 name, he, buffer, buflen, h_errnop);