Changeset 988 for vendor/current/nsswitch/wins.c
- Timestamp:
- Nov 24, 2016, 1:14:11 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vendor/current/nsswitch/wins.c
r740 r988 21 21 #include "includes.h" 22 22 #include "nsswitch/winbind_nss.h" 23 #include "nsswitch/libwbclient/wbclient.h" 23 24 24 25 #ifdef HAVE_NS_API_H … … 38 39 #define INADDRSZ 4 39 40 #endif 40 41 static int initialised;42 41 43 42 NSS_STATUS _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he, … … 46 45 char *buffer, size_t buflen, int *h_errnop); 47 46 48 static void nss_wins_init(void) 49 { 50 initialised = 1; 51 load_case_tables_library(); 52 lp_set_cmdline("log level", "0"); 53 54 TimeInit(); 55 setup_logging("nss_wins",False); 56 lp_load(get_dyn_CONFIGFILE(),True,False,False,True); 57 load_interfaces(); 58 } 59 60 static struct in_addr *lookup_byname_backend(const char *name, int *count) 61 { 62 struct ip_service *address = NULL; 63 struct in_addr *ret = NULL; 64 int j; 65 66 if (!initialised) { 67 nss_wins_init(); 68 } 69 70 *count = 0; 71 72 /* always try with wins first */ 73 if (NT_STATUS_IS_OK(resolve_wins(name,0x00,&address,count))) { 74 if ( (ret = SMB_MALLOC_P(struct in_addr)) == NULL ) { 75 free( address ); 76 return NULL; 77 } 78 if (address[0].ss.ss_family != AF_INET) { 79 free(address); 80 free(ret); 81 return NULL; 82 } 83 *ret = ((struct sockaddr_in *)(void *)&address[0].ss) 84 ->sin_addr; 85 free( address ); 86 return ret; 87 } 88 89 /* uggh, we have to broadcast to each interface in turn */ 90 for (j=iface_count() - 1;j >= 0;j--) { 91 const struct in_addr *bcast = iface_n_bcast_v4(j); 92 struct sockaddr_storage ss; 93 struct sockaddr_storage *pss; 94 NTSTATUS status; 95 96 if (!bcast) { 97 continue; 98 } 99 in_addr_to_sockaddr_storage(&ss, *bcast); 100 status = name_query(name, 0x00, True, True, &ss, 101 NULL, &pss, count, NULL); 102 if (NT_STATUS_IS_OK(status) && (*count > 0)) { 103 if ((ret = SMB_MALLOC_P(struct in_addr)) == NULL) { 104 return NULL; 105 } 106 *ret = ((struct sockaddr_in *)pss)->sin_addr; 107 TALLOC_FREE(pss); 108 break; 109 } 110 } 111 112 return ret; 47 static char *lookup_byname_backend(const char *name) 48 { 49 const char *p; 50 char *ip, *ipp; 51 size_t nbt_len; 52 wbcErr result; 53 54 nbt_len = strlen(name); 55 if (nbt_len > MAX_NETBIOSNAME_LEN - 1) { 56 return NULL; 57 } 58 p = strchr(name, '.'); 59 if (p != NULL) { 60 return NULL; 61 } 62 63 result = wbcResolveWinsByName(name, &ip); 64 if (result != WBC_ERR_SUCCESS) { 65 return NULL; 66 } 67 68 ipp = strchr(ip, '\t'); 69 if (ipp != NULL) { 70 *ipp = '\0'; 71 } 72 73 return ip; 113 74 } 114 75 115 76 #ifdef HAVE_NS_API_H 116 77 117 static struct node_status *lookup_byaddr_backend(char *addr, int *count) 118 { 119 struct sockaddr_storage ss; 120 struct nmb_name nname; 121 struct node_status *result; 122 NTSTATUS status; 123 124 if (!initialised) { 125 nss_wins_init(); 126 } 127 128 make_nmb_name(&nname, "*", 0); 129 if (!interpret_string_addr(&ss, addr, AI_NUMERICHOST)) { 130 return NULL; 131 } 132 status = node_status_query(NULL, &nname, &ss, &result, count, NULL); 133 if (!NT_STATUS_IS_OK(status)) { 134 return NULL; 135 } 136 137 return result; 78 static char *lookup_byaddr_backend(const char *ip) 79 { 80 wbcErr result; 81 char *name = NULL; 82 83 result = wbcResolveWinsByIP(ip, &name); 84 if (result != WBC_ERR_SUCCESS) { 85 return NULL; 86 } 87 88 return name; 138 89 } 139 90 … … 142 93 int init(void) 143 94 { 95 bool ok; 96 144 97 nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n"); 145 nss_wins_init(); 98 99 ok = nss_wins_init(); 100 if (!ok) { 101 return NSD_ERROR; 102 } 103 146 104 return NSD_OK; 147 105 } … … 152 110 char *key; 153 111 char *addr; 154 struct in_addr *ip_list;155 struct node_status *status;156 112 int i, count, len, size; 157 113 char response[1024]; … … 181 137 * ip_address[ ip_address]*\tname[ alias]* 182 138 */ 183 if (StrCaseCmp(map,"hosts.byaddr") == 0) { 184 if ( status = lookup_byaddr_backend(key, &count)) { 185 size = strlen(key) + 1; 186 if (size > len) { 187 talloc_free(status); 188 return NSD_ERROR; 189 } 190 len -= size; 191 strncat(response,key,size); 192 strncat(response,"\t",1); 193 for (i = 0; i < count; i++) { 194 /* ignore group names */ 195 if (status[i].flags & 0x80) continue; 196 if (status[i].type == 0x20) { 197 size = sizeof(status[i].name) + 1; 198 if (size > len) { 199 talloc_free(status); 200 return NSD_ERROR; 201 } 202 len -= size; 203 strncat(response, status[i].name, size); 204 strncat(response, " ", 1); 205 found = True; 139 if (strcasecmp_m(map,"hosts.byaddr") == 0) { 140 char *name; 141 142 name = lookup_byaddr_backend(key); 143 if (name != NULL) { 144 size = strlen(key) + 1; 145 if (size > len) { 146 return NSD_ERROR; 206 147 } 207 } 208 response[strlen(response)-1] = '\n'; 209 talloc_free(status); 148 len -= size; 149 strncat(response,key,size); 150 strncat(response,"\t",1); 151 152 size = strlen(name) + 1; 153 if (size > len) { 154 return NSD_ERROR; 155 } 156 len -= size; 157 strncat(response, name, size); 158 strncat(response, " ", 1); 159 found = True; 210 160 } 211 } else if (StrCaseCmp(map,"hosts.byname") == 0) { 212 if (ip_list = lookup_byname_backend(key, &count)) { 213 for (i = count; i ; i--) { 214 addr = inet_ntoa(ip_list[i-1]); 215 size = strlen(addr) + 1; 216 if (size > len) { 217 free(ip_list); 218 return NSD_ERROR; 219 } 220 len -= size; 221 if (i != 0) 222 response[strlen(response)-1] = ' '; 223 strncat(response,addr,size); 224 strncat(response,"\t",1); 161 response[strlen(response)-1] = '\n'; 162 } else if (strcasecmp_m(map,"hosts.byname") == 0) { 163 char *ip; 164 165 ip = lookup_byname_backend(key); 166 if (ip != NULL) { 167 size = strlen(ip) + 1; 168 if (size > len) { 169 wbcFreeMemory(ip); 170 return NSD_ERROR; 171 } 172 len -= size; 173 strncat(response,ip,size); 174 strncat(response,"\t",1); 175 size = strlen(key) + 1; 176 wbcFreeMemory(ip); 177 if (size > len) { 178 return NSD_ERROR; 179 } 180 strncat(response,key,size); 181 strncat(response,"\n",1); 182 183 found = True; 225 184 } 226 size = strlen(key) + 1;227 if (size > len) {228 free(ip_list);229 return NSD_ERROR;230 }231 strncat(response,key,size);232 strncat(response,"\n",1);233 found = True;234 free(ip_list);235 }236 185 } 237 186 … … 252 201 functions. */ 253 202 254 static char *get_static(char **buffer, size_t *buflen, int len)203 static char *get_static(char **buffer, size_t *buflen, size_t len) 255 204 { 256 205 char *result; … … 281 230 { 282 231 NSS_STATUS nss_status = NSS_STATUS_SUCCESS; 283 struct in_addr *ip_list; 284 int i, count; 232 char *ip; 233 struct in_addr in; 234 int i; 285 235 fstring name; 286 236 size_t namelen; 287 TALLOC_CTX *frame;237 int rc; 288 238 289 239 #if HAVE_PTHREAD … … 291 241 #endif 292 242 293 frame = talloc_stackframe();294 295 243 memset(he, '\0', sizeof(*he)); 296 244 fstrcpy(name, hostname); … … 298 246 /* Do lookup */ 299 247 300 ip_list = lookup_byname_backend(name, &count); 301 302 if (!ip_list) { 248 ip = lookup_byname_backend(name); 249 if (ip == NULL) { 303 250 nss_status = NSS_STATUS_NOTFOUND; 304 251 goto out; 305 252 } 306 253 254 rc = inet_pton(AF_INET, ip, &in); 255 wbcFreeMemory(ip); 256 if (rc == 0) { 257 nss_status = NSS_STATUS_TRYAGAIN; 258 goto out; 259 } 260 307 261 /* Copy h_name */ 308 262 … … 310 264 311 265 if ((he->h_name = get_static(&buffer, &buflen, namelen)) == NULL) { 312 free(ip_list);313 266 nss_status = NSS_STATUS_TRYAGAIN; 314 267 goto out; … … 323 276 324 277 if (get_static(&buffer, &buflen, i) == NULL) { 325 free(ip_list);326 278 nss_status = NSS_STATUS_TRYAGAIN; 327 279 goto out; … … 329 281 330 282 if ((he->h_addr_list = (char **)get_static( 331 &buffer, &buflen, (count + 1) * sizeof(char *))) == NULL) { 332 free(ip_list); 333 nss_status = NSS_STATUS_TRYAGAIN; 334 goto out; 335 } 336 337 for (i = 0; i < count; i++) { 338 if ((he->h_addr_list[i] = get_static(&buffer, &buflen, 339 INADDRSZ)) == NULL) { 340 free(ip_list); 341 nss_status = NSS_STATUS_TRYAGAIN; 342 goto out; 343 } 344 memcpy(he->h_addr_list[i], &ip_list[i], INADDRSZ); 345 } 346 347 he->h_addr_list[count] = NULL; 348 349 free(ip_list); 283 &buffer, &buflen, 2 * sizeof(char *))) == NULL) { 284 nss_status = NSS_STATUS_TRYAGAIN; 285 goto out; 286 } 287 288 if ((he->h_addr_list[0] = get_static(&buffer, &buflen, 289 INADDRSZ)) == NULL) { 290 nss_status = NSS_STATUS_TRYAGAIN; 291 goto out; 292 } 293 294 memcpy(he->h_addr_list[0], &in, INADDRSZ); 295 296 he->h_addr_list[1] = NULL; 350 297 351 298 /* Set h_addr_type and h_length */ … … 375 322 376 323 out: 377 378 TALLOC_FREE(frame);379 324 380 325 #if HAVE_PTHREAD
Note:
See TracChangeset
for help on using the changeset viewer.