| 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 3 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, see <http://www.gnu.org/licenses/>.
|
|---|
| 18 |
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | #include "includes.h"
|
|---|
| 22 | #ifdef HAVE_NS_API_H
|
|---|
| 23 | #undef VOLATILE
|
|---|
| 24 |
|
|---|
| 25 | #include <ns_daemon.h>
|
|---|
| 26 | #endif
|
|---|
| 27 |
|
|---|
| 28 | #if HAVE_PTHREAD_H
|
|---|
| 29 | #include <pthread.h>
|
|---|
| 30 | #endif
|
|---|
| 31 |
|
|---|
| 32 | #if HAVE_PTHREAD
|
|---|
| 33 | static pthread_mutex_t wins_nss_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|---|
| 34 | #endif
|
|---|
| 35 |
|
|---|
| 36 | #ifndef INADDRSZ
|
|---|
| 37 | #define INADDRSZ 4
|
|---|
| 38 | #endif
|
|---|
| 39 |
|
|---|
| 40 | static int initialised;
|
|---|
| 41 |
|
|---|
| 42 | extern bool AllowDebugChange;
|
|---|
| 43 |
|
|---|
| 44 | NSS_STATUS _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
|
|---|
| 45 | char *buffer, size_t buflen, int *h_errnop);
|
|---|
| 46 | NSS_STATUS _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
|
|---|
| 47 | char *buffer, size_t buflen, int *h_errnop);
|
|---|
| 48 |
|
|---|
| 49 | /* Use our own create socket code so we don't recurse.... */
|
|---|
| 50 |
|
|---|
| 51 | static int wins_lookup_open_socket_in(void)
|
|---|
| 52 | {
|
|---|
| 53 | struct sockaddr_in sock;
|
|---|
| 54 | int val=1;
|
|---|
| 55 | int res;
|
|---|
| 56 |
|
|---|
| 57 | memset((char *)&sock,'\0',sizeof(sock));
|
|---|
| 58 |
|
|---|
| 59 | #ifdef HAVE_SOCK_SIN_LEN
|
|---|
| 60 | sock.sin_len = sizeof(sock);
|
|---|
| 61 | #endif
|
|---|
| 62 | sock.sin_port = 0;
|
|---|
| 63 | sock.sin_family = AF_INET;
|
|---|
| 64 | sock.sin_addr.s_addr = interpret_addr("0.0.0.0");
|
|---|
| 65 | res = socket(AF_INET, SOCK_DGRAM, 0);
|
|---|
| 66 | if (res == -1)
|
|---|
| 67 | return -1;
|
|---|
| 68 |
|
|---|
| 69 | if (setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val)) != 0) {
|
|---|
| 70 | close(res);
|
|---|
| 71 | return -1;
|
|---|
| 72 | }
|
|---|
| 73 | #ifdef SO_REUSEPORT
|
|---|
| 74 | if (setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val)) != 0) {
|
|---|
| 75 | close(res);
|
|---|
| 76 | return -1;
|
|---|
| 77 | }
|
|---|
| 78 | #endif /* SO_REUSEPORT */
|
|---|
| 79 |
|
|---|
| 80 | /* now we've got a socket - we need to bind it */
|
|---|
| 81 |
|
|---|
| 82 | if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0) {
|
|---|
| 83 | close(res);
|
|---|
| 84 | return(-1);
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | set_socket_options(res,"SO_BROADCAST");
|
|---|
| 88 |
|
|---|
| 89 | return res;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 | static void nss_wins_init(void)
|
|---|
| 94 | {
|
|---|
| 95 | initialised = 1;
|
|---|
| 96 | DEBUGLEVEL = 0;
|
|---|
| 97 | AllowDebugChange = False;
|
|---|
| 98 |
|
|---|
| 99 | TimeInit();
|
|---|
| 100 | setup_logging("nss_wins",False);
|
|---|
| 101 | load_case_tables();
|
|---|
| 102 | lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
|
|---|
| 103 | load_interfaces();
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | static struct in_addr *lookup_byname_backend(const char *name, int *count)
|
|---|
| 107 | {
|
|---|
| 108 | int fd = -1;
|
|---|
| 109 | struct ip_service *address = NULL;
|
|---|
| 110 | struct in_addr *ret = NULL;
|
|---|
| 111 | int j, flags = 0;
|
|---|
| 112 |
|
|---|
| 113 | if (!initialised) {
|
|---|
| 114 | nss_wins_init();
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | *count = 0;
|
|---|
| 118 |
|
|---|
| 119 | /* always try with wins first */
|
|---|
| 120 | if (NT_STATUS_IS_OK(resolve_wins(name,0x00,&address,count))) {
|
|---|
| 121 | if ( (ret = SMB_MALLOC_P(struct in_addr)) == NULL ) {
|
|---|
| 122 | free( address );
|
|---|
| 123 | return NULL;
|
|---|
| 124 | }
|
|---|
| 125 | if (address[0].ss.ss_family != AF_INET) {
|
|---|
| 126 | free(address);
|
|---|
| 127 | free(ret);
|
|---|
| 128 | return NULL;
|
|---|
| 129 | }
|
|---|
| 130 | *ret = ((struct sockaddr_in *)&address[0].ss)->sin_addr;
|
|---|
| 131 | free( address );
|
|---|
| 132 | return ret;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | fd = wins_lookup_open_socket_in();
|
|---|
| 136 | if (fd == -1) {
|
|---|
| 137 | return NULL;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | /* uggh, we have to broadcast to each interface in turn */
|
|---|
| 141 | for (j=iface_count() - 1;j >= 0;j--) {
|
|---|
| 142 | const struct in_addr *bcast = iface_n_bcast_v4(j);
|
|---|
| 143 | struct sockaddr_storage ss;
|
|---|
| 144 | struct sockaddr_storage *pss;
|
|---|
| 145 | if (!bcast) {
|
|---|
| 146 | continue;
|
|---|
| 147 | }
|
|---|
| 148 | in_addr_to_sockaddr_storage(&ss, *bcast);
|
|---|
| 149 | pss = name_query(fd,name,0x00,True,True,&ss,count, &flags, NULL);
|
|---|
| 150 | if (pss) {
|
|---|
| 151 | if ((ret = SMB_MALLOC_P(struct in_addr)) == NULL) {
|
|---|
| 152 | return NULL;
|
|---|
| 153 | }
|
|---|
| 154 | *ret = ((struct sockaddr_in *)pss)->sin_addr;
|
|---|
| 155 | break;
|
|---|
| 156 | }
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | close(fd);
|
|---|
| 160 | return ret;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | #ifdef HAVE_NS_API_H
|
|---|
| 164 |
|
|---|
| 165 | static NODE_STATUS_STRUCT *lookup_byaddr_backend(char *addr, int *count)
|
|---|
| 166 | {
|
|---|
| 167 | int fd;
|
|---|
| 168 | struct sockaddr_storage ss;
|
|---|
| 169 | struct nmb_name nname;
|
|---|
| 170 | NODE_STATUS_STRUCT *status;
|
|---|
| 171 |
|
|---|
| 172 | if (!initialised) {
|
|---|
| 173 | nss_wins_init();
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | fd = wins_lookup_open_socket_in();
|
|---|
| 177 | if (fd == -1)
|
|---|
| 178 | return NULL;
|
|---|
| 179 |
|
|---|
| 180 | make_nmb_name(&nname, "*", 0);
|
|---|
| 181 | if (!interpret_string_addr(&ss, addr, AI_NUMERICHOST)) {
|
|---|
| 182 | return NULL;
|
|---|
| 183 | }
|
|---|
| 184 | status = node_status_query(fd, &nname, &ss, count, NULL);
|
|---|
| 185 |
|
|---|
| 186 | close(fd);
|
|---|
| 187 | return status;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | /* IRIX version */
|
|---|
| 191 |
|
|---|
| 192 | int init(void)
|
|---|
|
|---|