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