smbd: Remove "st" from struct open_symlink_err
[Samba.git] / nsswitch / wins.c
bloba310477cfad216ddd9a0cc4088bc9969f07a6481
1 /*
2 Unix SMB/CIFS implementation.
3 a WINS nsswitch module
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/>.
21 #include "includes.h"
22 #include "nsswitch/winbind_client.h"
23 #include "nsswitch/libwbclient/wbclient.h"
24 #include "lib/util/string_wrappers.h"
26 #ifdef HAVE_NS_API_H
28 #include <ns_daemon.h>
29 #endif
31 #ifdef HAVE_PTHREAD_H
32 #include <pthread.h>
33 #endif
35 #ifdef HAVE_PTHREAD
36 static pthread_mutex_t wins_nss_mutex = PTHREAD_MUTEX_INITIALIZER;
37 #endif
39 #ifndef INADDRSZ
40 #define INADDRSZ 4
41 #endif
43 #ifndef NETDB_INTERNAL
44 #define NETDB_INTERNAL -1
45 #endif
47 #ifndef NETDB_SUCCESS
48 #define NETDB_SUCCESS 0
49 #endif
51 _PUBLIC_ON_LINUX_
52 NSS_STATUS _nss_wins_gethostbyname_r(const char *hostname,
53 struct hostent *he,
54 char *buffer,
55 size_t buflen,
56 int *errnop,
57 int *h_errnop);
58 NSS_STATUS _nss_wins_gethostbyname2_r(const char *name,
59 int af,
60 struct hostent *he,
61 char *buffer,
62 size_t buflen,
63 int *errnop,
64 int *h_errnop);
66 static char *lookup_byname_backend(const char *name)
68 const char *p;
69 char *ip, *ipp;
70 size_t nbt_len;
71 wbcErr result;
73 nbt_len = strlen(name);
74 if (nbt_len > MAX_NETBIOSNAME_LEN - 1) {
75 return NULL;
77 p = strchr(name, '.');
78 if (p != NULL) {
79 return NULL;
82 wbcSetClientProcessName("nss_wins");
83 result = wbcResolveWinsByName(name, &ip);
84 if (result != WBC_ERR_SUCCESS) {
85 return NULL;
88 ipp = strchr(ip, '\t');
89 if (ipp != NULL) {
90 *ipp = '\0';
93 return ip;
96 #ifdef HAVE_NS_API_H
98 static char *lookup_byaddr_backend(const char *ip)
100 wbcErr result;
101 char *name = NULL;
103 wbcSetClientProcessName("nss_wins");
104 result = wbcResolveWinsByIP(ip, &name);
105 if (result != WBC_ERR_SUCCESS) {
106 return NULL;
109 return name;
112 /* IRIX version */
114 int init(void)
116 bool ok;
118 nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
120 ok = nss_wins_init();
121 if (!ok) {
122 return NSD_ERROR;
125 return NSD_OK;
128 int lookup(nsd_file_t *rq)
130 char *map;
131 char *key;
132 char *addr;
133 int i, count, len, size;
134 char response[1024];
135 bool found = False;
137 nsd_logprintf(NSD_LOG_MIN, "entering lookup (wins)\n");
138 if (! rq)
139 return NSD_ERROR;
141 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
142 if (! map) {
143 rq->f_status = NS_FATAL;
144 return NSD_ERROR;
147 key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
148 if (! key || ! *key) {
149 rq->f_status = NS_FATAL;
150 return NSD_ERROR;
153 response[0] = '\0';
154 len = sizeof(response) - 2;
157 * response needs to be a string of the following format
158 * ip_address[ ip_address]*\tname[ alias]*
160 if (strcasecmp_m(map,"hosts.byaddr") == 0) {
161 char *name;
163 name = lookup_byaddr_backend(key);
164 if (name != NULL) {
165 size = strlen(key) + 1;
166 if (size > len) {
167 return NSD_ERROR;
169 len -= size;
170 strncat(response,key,size);
171 strncat(response,"\t",1);
173 size = strlen(name) + 1;
174 if (size > len) {
175 return NSD_ERROR;
177 len -= size;
178 strncat(response, name, size);
179 strncat(response, " ", 1);
180 found = True;
182 response[strlen(response)-1] = '\n';
183 } else if (strcasecmp_m(map,"hosts.byname") == 0) {
184 char *ip;
186 ip = lookup_byname_backend(key);
187 if (ip != NULL) {
188 size = strlen(ip) + 1;
189 if (size > len) {
190 wbcFreeMemory(ip);
191 return NSD_ERROR;
193 len -= size;
194 strncat(response,ip,size);
195 strncat(response,"\t",1);
196 size = strlen(key) + 1;
197 wbcFreeMemory(ip);
198 if (size > len) {
199 return NSD_ERROR;
201 strncat(response,key,size);
202 strncat(response,"\n",1);
204 found = True;
208 if (found) {
209 nsd_logprintf(NSD_LOG_LOW, "lookup (wins %s) %s\n",map,response);
210 nsd_set_result(rq,NS_SUCCESS,response,strlen(response),VOLATILE);
211 return NSD_OK;
213 nsd_logprintf(NSD_LOG_LOW, "lookup (wins) not found\n");
214 rq->f_status = NS_NOTFOUND;
215 return NSD_NEXT;
218 #else
220 /* Allocate some space from the nss static buffer. The buffer and buflen
221 are the pointers passed in by the C library to the _nss_*_*
222 functions. */
224 static char *get_static(char **buffer, size_t *buflen, size_t len)
226 char *result;
228 /* Error check. We return false if things aren't set up right, or
229 there isn't enough buffer space left. */
231 if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
232 return NULL;
235 /* Return an index into the static buffer */
237 result = *buffer;
238 *buffer += len;
239 *buflen -= len;
241 return result;
244 /****************************************************************************
245 gethostbyname() - we ignore any domain portion of the name and only
246 handle names that are at most 15 characters long
247 **************************************************************************/
248 _PUBLIC_ON_LINUX_
249 NSS_STATUS
250 _nss_wins_gethostbyname_r(const char *hostname,
251 struct hostent *he,
252 char *buffer,
253 size_t buflen,
254 int *errnop,
255 int *h_errnop)
257 NSS_STATUS nss_status = NSS_STATUS_SUCCESS;
258 char *ip;
259 struct in_addr in;
260 int i;
261 fstring name;
262 size_t namelen;
263 int rc;
265 #ifdef HAVE_PTHREAD
266 pthread_mutex_lock(&wins_nss_mutex);
267 #endif
269 memset(he, '\0', sizeof(*he));
270 fstrcpy(name, hostname);
272 /* Do lookup */
274 ip = lookup_byname_backend(name);
275 if (ip == NULL) {
276 *h_errnop = HOST_NOT_FOUND;
277 nss_status = NSS_STATUS_NOTFOUND;
278 goto out;
281 rc = inet_pton(AF_INET, ip, &in);
282 wbcFreeMemory(ip);
283 if (rc == 0) {
284 *errnop = errno;
285 *h_errnop = NETDB_INTERNAL;
286 nss_status = NSS_STATUS_TRYAGAIN;
287 goto out;
290 /* Copy h_name */
292 namelen = strlen(name) + 1;
294 if ((he->h_name = get_static(&buffer, &buflen, namelen)) == NULL) {
295 *errnop = EAGAIN;
296 *h_errnop = NETDB_INTERNAL;
297 nss_status = NSS_STATUS_TRYAGAIN;
298 goto out;
301 memcpy(he->h_name, name, namelen);
303 /* Copy h_addr_list, align to pointer boundary first */
305 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
306 i = sizeof(char*) - i;
308 if (get_static(&buffer, &buflen, i) == NULL) {
309 *errnop = EAGAIN;
310 *h_errnop = NETDB_INTERNAL;
311 nss_status = NSS_STATUS_TRYAGAIN;
312 goto out;
315 if ((he->h_addr_list = (char **)get_static(
316 &buffer, &buflen, 2 * sizeof(char *))) == NULL) {
317 *errnop = EAGAIN;
318 *h_errnop = NETDB_INTERNAL;
319 nss_status = NSS_STATUS_TRYAGAIN;
320 goto out;
323 if ((he->h_addr_list[0] = get_static(&buffer, &buflen,
324 INADDRSZ)) == NULL) {
325 *errnop = EAGAIN;
326 *h_errnop = NETDB_INTERNAL;
327 nss_status = NSS_STATUS_TRYAGAIN;
328 goto out;
331 memcpy(he->h_addr_list[0], &in, INADDRSZ);
333 he->h_addr_list[1] = NULL;
335 /* Set h_addr_type and h_length */
337 he->h_addrtype = AF_INET;
338 he->h_length = INADDRSZ;
340 /* Set h_aliases */
342 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
343 i = sizeof(char*) - i;
345 if (get_static(&buffer, &buflen, i) == NULL) {
346 *errnop = EAGAIN;
347 *h_errnop = NETDB_INTERNAL;
348 nss_status = NSS_STATUS_TRYAGAIN;
349 goto out;
352 if ((he->h_aliases = (char **)get_static(
353 &buffer, &buflen, sizeof(char *))) == NULL) {
354 *errnop = EAGAIN;
355 *h_errnop = NETDB_INTERNAL;
356 nss_status = NSS_STATUS_TRYAGAIN;
357 goto out;
360 he->h_aliases[0] = NULL;
362 *h_errnop = NETDB_SUCCESS;
363 nss_status = NSS_STATUS_SUCCESS;
365 out:
367 #ifdef HAVE_PTHREAD
368 pthread_mutex_unlock(&wins_nss_mutex);
369 #endif
370 return nss_status;
374 _PUBLIC_ON_LINUX_
375 NSS_STATUS
376 _nss_wins_gethostbyname2_r(const char *name,
377 int af,
378 struct hostent *he,
379 char *buffer,
380 size_t buflen,
381 int *errnop,
382 int *h_errnop)
384 NSS_STATUS nss_status;
386 if(af!=AF_INET) {
387 *errnop = EAFNOSUPPORT;
388 *h_errnop = NO_DATA;
389 nss_status = NSS_STATUS_UNAVAIL;
390 } else {
391 nss_status = _nss_wins_gethostbyname_r(name,
393 buffer,
394 buflen,
395 errnop,
396 h_errnop);
398 return nss_status;
400 #endif