[312] | 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 | #if HAVE_PTHREAD_H
|
---|
| 30 | #include <pthread.h>
|
---|
| 31 | #endif
|
---|
| 32 |
|
---|
| 33 | #if HAVE_PTHREAD
|
---|
| 34 | static pthread_mutex_t wins_nss_mutex = PTHREAD_MUTEX_INITIALIZER;
|
---|
| 35 | #endif
|
---|
| 36 |
|
---|
| 37 | #ifndef INADDRSZ
|
---|
| 38 | #define INADDRSZ 4
|
---|
| 39 | #endif
|
---|
| 40 |
|
---|
| 41 | static int initialised;
|
---|
| 42 |
|
---|
| 43 | extern BOOL AllowDebugChange;
|
---|
| 44 |
|
---|
| 45 | NSS_STATUS _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
|
---|
| 46 | char *buffer, size_t buflen, int *h_errnop);
|
---|
| 47 | NSS_STATUS _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
|
---|
| 48 | char *buffer, size_t buflen, int *h_errnop);
|
---|
| 49 |
|
---|
| 50 | /* Use our own create socket code so we don't recurse.... */
|
---|
| 51 |
|
---|
| 52 | static int wins_lookup_open_socket_in(void)
|
---|
| 53 | {
|
---|
| 54 | struct sockaddr_in sock;
|
---|
| 55 | int val=1;
|
---|
| 56 | int res;
|
---|
| 57 |
|
---|
| 58 | memset((char *)&sock,'\0',sizeof(sock));
|
---|
| 59 |
|
---|
| 60 | #ifdef HAVE_SOCK_SIN_LEN
|
---|
| 61 | sock.sin_len = sizeof(sock);
|
---|
| 62 | #endif
|
---|
| 63 | sock.sin_port = 0;
|
---|
| 64 | sock.sin_family = AF_INET;
|
---|
| 65 | sock.sin_addr.s_addr = interpret_addr("0.0.0.0");
|
---|
| 66 | res = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
| 67 | if (res == -1)
|
---|
| 68 | return -1;
|
---|
| 69 |
|
---|
| 70 | setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val));
|
---|
| 71 | #ifdef SO_REUSEPORT
|
---|
| 72 | setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val));
|
---|
| 73 | #endif /* SO_REUSEPORT */
|
---|
| 74 |
|
---|
| 75 | /* now we've got a socket - we need to bind it */
|
---|
| 76 |
|
---|
| 77 | if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0) {
|
---|
| 78 | close(res);
|
---|
| 79 | return(-1);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | set_socket_options(res,"SO_BROADCAST");
|
---|
| 83 |
|
---|
| 84 | return res;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 |
|
---|
| 88 | static void nss_wins_init(void)
|
---|
| 89 | {
|
---|
| 90 | initialised = 1;
|
---|
| 91 | DEBUGLEVEL = 0;
|
---|
| 92 | AllowDebugChange = False;
|
---|
| 93 |
|
---|
| 94 | TimeInit();
|
---|
| 95 | setup_logging("nss_wins",False);
|
---|
| 96 | load_case_tables();
|
---|
| 97 | lp_load(dyn_CONFIGFILE,True,False,False,True);
|
---|
| 98 | load_interfaces();
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | static struct in_addr *lookup_byname_backend(const char *name, int *count)
|
---|
| 102 | {
|
---|
| 103 | int fd = -1;
|
---|
| 104 | struct ip_service *address = NULL;
|
---|
| 105 | struct in_addr *ret = NULL;
|
---|
| 106 | int j, flags = 0;
|
---|
| 107 |
|
---|
| 108 | if (!initialised) {
|
---|
| 109 | nss_wins_init();
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | *count = 0;
|
---|
| 113 |
|
---|
| 114 | /* always try with wins first */
|
---|
| 115 | if (resolve_wins(name,0x00,&address,count)) {
|
---|
| 116 | if ( (ret = SMB_MALLOC_P(struct in_addr)) == NULL ) {
|
---|
| 117 | free( address );
|
---|
| 118 | return NULL;
|
---|
| 119 | }
|
---|
| 120 | *ret = address[0].ip;
|
---|
| 121 | free( address );
|
---|
| 122 | return ret;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | fd = wins_lookup_open_socket_in();
|
---|
| 126 | if (fd == -1) {
|
---|
| 127 | return NULL;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | /* uggh, we have to broadcast to each interface in turn */
|
---|
| 131 | for (j=iface_count() - 1;j >= 0;j--) {
|
---|
| 132 | struct in_addr *bcast = iface_n_bcast(j);
|
---|
| 133 | ret = name_query(fd,name,0x00,True,True,*bcast,count, &flags, NULL);
|
---|
| 134 | if (ret) break;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | close(fd);
|
---|
| 138 | return ret;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | #ifdef HAVE_NS_API_H
|
---|
| 142 |
|
---|
| 143 | static NODE_STATUS_STRUCT *lookup_byaddr_backend(char *addr, int *count)
|
---|
| 144 | {
|
---|
| 145 | int fd;
|
---|
| 146 | struct in_addr ip;
|
---|
| 147 | struct nmb_name nname;
|
---|
| 148 | NODE_STATUS_STRUCT *status;
|
---|
| 149 |
|
---|
| 150 | if (!initialised) {
|
---|
| 151 | nss_wins_init();
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | fd = wins_lookup_open_socket_in();
|
---|
| 155 | if (fd == -1)
|
---|
| 156 | return NULL;
|
---|
| 157 |
|
---|
| 158 | make_nmb_name(&nname, "*", 0);
|
---|
| 159 | ip = *interpret_addr2(addr);
|
---|
| 160 | status = node_status_query(fd,&nname,ip, count, NULL);
|
---|
| 161 |
|
---|
| 162 | close(fd);
|
---|
| 163 | return status;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | /* IRIX version */
|
---|
| 167 |
|
---|
| 168 | int init(void)
|
---|
| 169 | {
|
---|
| 170 | nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
|
---|
| 171 | nss_wins_init();
|
---|
| 172 | return NSD_OK;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | int lookup(nsd_file_t *rq)
|
---|
| 176 | {
|
---|
| 177 | char *map;
|
---|
| 178 | char *key;
|
---|
| 179 | char *addr;
|
---|
| 180 | struct in_addr *ip_list;
|
---|
| 181 | NODE_STATUS_STRUCT *status;
|
---|
| 182 | int i, count, len, size;
|
---|
| 183 | char response[1024];
|
---|
| 184 | BOOL found = False;
|
---|
| 185 |
|
---|
| 186 | nsd_logprintf(NSD_LOG_MIN, "entering lookup (wins)\n");
|
---|
| 187 | if (! rq)
|
---|
| 188 | return NSD_ERROR;
|
---|
| 189 |
|
---|
| 190 | map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
|
---|
| 191 | if (! map) {
|
---|
| 192 | rq->f_status = NS_FATAL;
|
---|
| 193 | return NSD_ERROR;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
|
---|
| 197 | if (! key || ! *key) {
|
---|
| 198 | rq->f_status = NS_FATAL;
|
---|
| 199 | return NSD_ERROR;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | response[0] = '\0';
|
---|
| 203 | len = sizeof(response) - 2;
|
---|
| 204 |
|
---|
| 205 | /*
|
---|
| 206 | * response needs to be a string of the following format
|
---|
| 207 | * ip_address[ ip_address]*\tname[ alias]*
|
---|
| 208 | */
|
---|
| 209 | if (StrCaseCmp(map,"hosts.byaddr") == 0) {
|
---|
| 210 | if ( status = lookup_byaddr_backend(key, &count)) {
|
---|
| 211 | size = strlen(key) + 1;
|
---|
| 212 | if (size > len) {
|
---|
| 213 | free(status);
|
---|
| 214 | return NSD_ERROR;
|
---|
| 215 | }
|
---|
| 216 | len -= size;
|
---|
| 217 | strncat(response,key,size);
|
---|
| 218 | strncat(response,"\t",1);
|
---|
| 219 | for (i = 0; i < count; i++) {
|
---|
| 220 | /* ignore group names */
|
---|
| 221 | if (status[i].flags & 0x80) continue;
|
---|
| 222 | if (status[i].type == 0x20) {
|
---|
| 223 | size = sizeof(status[i].name) + 1;
|
---|
| 224 | if (size > len) {
|
---|
| 225 | free(status);
|
---|
| 226 | return NSD_ERROR;
|
---|
| 227 | }
|
---|
| 228 | len -= size;
|
---|
| 229 | strncat(response, status[i].name, size);
|
---|
| 230 | strncat(response, " ", 1);
|
---|
| 231 | found = True;
|
---|
| 232 | }
|
---|
| 233 | }
|
---|
| 234 | response[strlen(response)-1] = '\n';
|
---|
| 235 | free(status);
|
---|
| 236 | }
|
---|
| 237 | } else if (StrCaseCmp(map,"hosts.byname") == 0) {
|
---|
| 238 | if (ip_list = lookup_byname_backend(key, &count)) {
|
---|
| 239 | for (i = count; i ; i--) {
|
---|
| 240 | addr = inet_ntoa(ip_list[i-1]);
|
---|
| 241 | size = strlen(addr) + 1;
|
---|
| 242 | if (size > len) {
|
---|
| 243 | free(ip_list);
|
---|
| 244 | return NSD_ERROR;
|
---|
| 245 | }
|
---|
| 246 | len -= size;
|
---|
| 247 | if (i != 0)
|
---|
| 248 | response[strlen(response)-1] = ' ';
|
---|
| 249 | strncat(response,addr,size);
|
---|
| 250 | strncat(response,"\t",1);
|
---|
| 251 | }
|
---|
| 252 | size = strlen(key) + 1;
|
---|
| 253 | if (size > len) {
|
---|
| 254 | free(ip_list);
|
---|
| 255 | return NSD_ERROR;
|
---|
| 256 | }
|
---|
| 257 | strncat(response,key,size);
|
---|
| 258 | strncat(response,"\n",1);
|
---|
| 259 | found = True;
|
---|
| 260 | free(ip_list);
|
---|
| 261 | }
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | if (found) {
|
---|
| 265 | nsd_logprintf(NSD_LOG_LOW, "lookup (wins %s) %s\n",map,response);
|
---|
| 266 | nsd_set_result(rq,NS_SUCCESS,response,strlen(response),VOLATILE);
|
---|
| 267 | return NSD_OK;
|
---|
| 268 | }
|
---|
| 269 | nsd_logprintf(NSD_LOG_LOW, "lookup (wins) not found\n");
|
---|
| 270 | rq->f_status = NS_NOTFOUND;
|
---|
| 271 | return NSD_NEXT;
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | #else
|
---|
| 275 |
|
---|
| 276 | /* Allocate some space from the nss static buffer. The buffer and buflen
|
---|
| 277 | are the pointers passed in by the C library to the _nss_*_*
|
---|
| 278 | functions. */
|
---|
| 279 |
|
---|
| 280 | static char *get_static(char **buffer, size_t *buflen, int len)
|
---|
| 281 | {
|
---|
| 282 | char *result;
|
---|
| 283 |
|
---|
| 284 | /* Error check. We return false if things aren't set up right, or
|
---|
| 285 | there isn't enough buffer space left. */
|
---|
| 286 |
|
---|
| 287 | if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
|
---|
| 288 | return NULL;
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | /* Return an index into the static buffer */
|
---|
| 292 |
|
---|
| 293 | result = *buffer;
|
---|
| 294 | *buffer += len;
|
---|
| 295 | *buflen -= len;
|
---|
| 296 |
|
---|
| 297 | return result;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | /****************************************************************************
|
---|
| 301 | gethostbyname() - we ignore any domain portion of the name and only
|
---|
| 302 | handle names that are at most 15 characters long
|
---|
| 303 | **************************************************************************/
|
---|
| 304 | NSS_STATUS
|
---|
| 305 | _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
|
---|
| 306 | char *buffer, size_t buflen, int *h_errnop)
|
---|
| 307 | {
|
---|
| 308 | NSS_STATUS nss_status = NSS_STATUS_SUCCESS;
|
---|
| 309 | struct in_addr *ip_list;
|
---|
| 310 | int i, count;
|
---|
| 311 | fstring name;
|
---|
| 312 | size_t namelen;
|
---|
| 313 |
|
---|
| 314 | #if HAVE_PTHREAD
|
---|
| 315 | pthread_mutex_lock(&wins_nss_mutex);
|
---|
| 316 | #endif
|
---|
| 317 |
|
---|
| 318 | memset(he, '\0', sizeof(*he));
|
---|
| 319 | fstrcpy(name, hostname);
|
---|
| 320 |
|
---|
| 321 | /* Do lookup */
|
---|
| 322 |
|
---|
| 323 | ip_list = lookup_byname_backend(name, &count);
|
---|
| 324 |
|
---|
| 325 | if (!ip_list) {
|
---|
| 326 | nss_status = NSS_STATUS_NOTFOUND;
|
---|
| 327 | goto out;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | /* Copy h_name */
|
---|
| 331 |
|
---|
| 332 | namelen = strlen(name) + 1;
|
---|
| 333 |
|
---|
| 334 | if ((he->h_name = get_static(&buffer, &buflen, namelen)) == NULL) {
|
---|
| 335 | nss_status = NSS_STATUS_TRYAGAIN;
|
---|
| 336 | goto out;
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | memcpy(he->h_name, name, namelen);
|
---|
| 340 |
|
---|
| 341 | /* Copy h_addr_list, align to pointer boundary first */
|
---|
| 342 |
|
---|
| 343 | if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
|
---|
| 344 | i = sizeof(char*) - i;
|
---|
| 345 |
|
---|
| 346 | if (get_static(&buffer, &buflen, i) == NULL) {
|
---|
| 347 | nss_status = NSS_STATUS_TRYAGAIN;
|
---|
| 348 | goto out;
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 | if ((he->h_addr_list = (char **)get_static(
|
---|
| 352 | &buffer, &buflen, (count + 1) * sizeof(char *))) == NULL) {
|
---|
| 353 | nss_status = NSS_STATUS_TRYAGAIN;
|
---|
| 354 | goto out;
|
---|
| 355 | }
|
---|
| 356 |
|
---|
| 357 | for (i = 0; i < count; i++) {
|
---|
| 358 | if ((he->h_addr_list[i] = get_static(&buffer, &buflen,
|
---|
| 359 | INADDRSZ)) == NULL) {
|
---|
| 360 | nss_status = NSS_STATUS_TRYAGAIN;
|
---|
| 361 | goto out;
|
---|
| 362 | }
|
---|
| 363 | memcpy(he->h_addr_list[i], &ip_list[i], INADDRSZ);
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | he->h_addr_list[count] = NULL;
|
---|
| 367 |
|
---|
| 368 | if (ip_list)
|
---|
| 369 | free(ip_list);
|
---|
| 370 |
|
---|
| 371 | /* Set h_addr_type and h_length */
|
---|
| 372 |
|
---|
| 373 | he->h_addrtype = AF_INET;
|
---|
| 374 | he->h_length = INADDRSZ;
|
---|
| 375 |
|
---|
| 376 | /* Set h_aliases */
|
---|
| 377 |
|
---|
| 378 | if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
|
---|
| 379 | i = sizeof(char*) - i;
|
---|
| 380 |
|
---|
| 381 | if (get_static(&buffer, &buflen, i) == NULL) {
|
---|
| 382 | nss_status = NSS_STATUS_TRYAGAIN;
|
---|
| 383 | goto out;
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | if ((he->h_aliases = (char **)get_static(
|
---|
| 387 | &buffer, &buflen, sizeof(char *))) == NULL) {
|
---|
| 388 | nss_status = NSS_STATUS_TRYAGAIN;
|
---|
| 389 | goto out;
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 | he->h_aliases[0] = NULL;
|
---|
| 393 |
|
---|
| 394 | nss_status = NSS_STATUS_SUCCESS;
|
---|
| 395 |
|
---|
| 396 | out:
|
---|
| 397 |
|
---|
| 398 | #if HAVE_PTHREAD
|
---|
| 399 | pthread_mutex_unlock(&wins_nss_mutex);
|
---|
| 400 | #endif
|
---|
| 401 | return nss_status;
|
---|
| 402 | }
|
---|
| 403 |
|
---|
| 404 |
|
---|
| 405 | NSS_STATUS
|
---|
| 406 | _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
|
---|
| 407 | char *buffer, size_t buflen, int *h_errnop)
|
---|
| 408 | {
|
---|
| 409 | NSS_STATUS nss_status;
|
---|
| 410 |
|
---|
| 411 | #if HAVE_PTHREAD
|
---|
| 412 | pthread_mutex_lock(&wins_nss_mutex);
|
---|
| 413 | #endif
|
---|
| 414 |
|
---|
| 415 | if(af!=AF_INET) {
|
---|
| 416 | *h_errnop = NO_DATA;
|
---|
| 417 | nss_status = NSS_STATUS_UNAVAIL;
|
---|
| 418 | } else {
|
---|
| 419 | nss_status = _nss_wins_gethostbyname_r(
|
---|
| 420 | name, he, buffer, buflen, h_errnop);
|
---|
| 421 | }
|
---|
| 422 | #if HAVE_PTHREAD
|
---|
| 423 | pthread_mutex_unlock(&wins_nss_mutex);
|
---|
| 424 | #endif
|
---|
| 425 | return nss_status;
|
---|
| 426 | }
|
---|
| 427 | #endif
|
---|