ctdb-scripts: Avoid shellcheck warning SC2017 (arithmetic precision)
[Samba.git] / nsswitch / wins.c
blobfc65c03063e712924868d98a0be94b80c8e0ad35
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_nss.h"
23 #include "nsswitch/libwbclient/wbclient.h"
25 #ifdef HAVE_NS_API_H
27 #include <ns_daemon.h>
28 #endif
30 #if HAVE_PTHREAD_H
31 #include <pthread.h>
32 #endif
34 #if HAVE_PTHREAD
35 static pthread_mutex_t wins_nss_mutex = PTHREAD_MUTEX_INITIALIZER;
36 #endif
38 #ifndef INADDRSZ
39 #define INADDRSZ 4
40 #endif
42 NSS_STATUS _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
43 char *buffer, size_t buflen, int *h_errnop);
44 NSS_STATUS _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
45 char *buffer, size_t buflen, int *h_errnop);
47 static char *lookup_byname_backend(const char *name)
49 const char *p;
50 char *ip, *ipp;
51 size_t nbt_len;
52 wbcErr result;
54 nbt_len = strlen(name);
55 if (nbt_len > MAX_NETBIOSNAME_LEN - 1) {
56 return NULL;
58 p = strchr(name, '.');
59 if (p != NULL) {
60 return NULL;
63 result = wbcResolveWinsByName(name, &ip);
64 if (result != WBC_ERR_SUCCESS) {
65 return NULL;
68 ipp = strchr(ip, '\t');
69 if (ipp != NULL) {
70 *ipp = '\0';
73 return ip;
76 #ifdef HAVE_NS_API_H
78 static char *lookup_byaddr_backend(const char *ip)
80 wbcErr result;
81 char *name = NULL;
83 result = wbcResolveWinsByIP(ip, &name);
84 if (result != WBC_ERR_SUCCESS) {
85 return NULL;
88 return name;
91 /* IRIX version */
93 int init(void)
95 bool ok;
97 nsd_logprintf(NSD_LOG_MIN, "entering init (wins)\n");
99 ok = nss_wins_init();
100 if (!ok) {
101 return NSD_ERROR;
104 return NSD_OK;
107 int lookup(nsd_file_t *rq)
109 char *map;
110 char *key;
111 char *addr;
112 int i, count, len, size;
113 char response[1024];
114 bool found = False;
116 nsd_logprintf(NSD_LOG_MIN, "entering lookup (wins)\n");
117 if (! rq)
118 return NSD_ERROR;
120 map = nsd_attr_fetch_string(rq->f_attrs, "table", (char*)0);
121 if (! map) {
122 rq->f_status = NS_FATAL;
123 return NSD_ERROR;
126 key = nsd_attr_fetch_string(rq->f_attrs, "key", (char*)0);
127 if (! key || ! *key) {
128 rq->f_status = NS_FATAL;
129 return NSD_ERROR;
132 response[0] = '\0';
133 len = sizeof(response) - 2;
136 * response needs to be a string of the following format
137 * ip_address[ ip_address]*\tname[ alias]*
139 if (strcasecmp_m(map,"hosts.byaddr") == 0) {
140 char *name;
142 name = lookup_byaddr_backend(key);
143 if (name != NULL) {
144 size = strlen(key) + 1;
145 if (size > len) {
146 return NSD_ERROR;
148 len -= size;
149 strncat(response,key,size);
150 strncat(response,"\t",1);
152 size = strlen(name) + 1;
153 if (size > len) {
154 return NSD_ERROR;
156 len -= size;
157 strncat(response, name, size);
158 strncat(response, " ", 1);
159 found = True;
161 response[strlen(response)-1] = '\n';
162 } else if (strcasecmp_m(map,"hosts.byname") == 0) {
163 char *ip;
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;
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;
180 strncat(response,key,size);
181 strncat(response,"\n",1);
183 found = True;
187 if (found) {
188 nsd_logprintf(NSD_LOG_LOW, "lookup (wins %s) %s\n",map,response);
189 nsd_set_result(rq,NS_SUCCESS,response,strlen(response),VOLATILE);
190 return NSD_OK;
192 nsd_logprintf(NSD_LOG_LOW, "lookup (wins) not found\n");
193 rq->f_status = NS_NOTFOUND;
194 return NSD_NEXT;
197 #else
199 /* Allocate some space from the nss static buffer. The buffer and buflen
200 are the pointers passed in by the C library to the _nss_*_*
201 functions. */
203 static char *get_static(char **buffer, size_t *buflen, size_t len)
205 char *result;
207 /* Error check. We return false if things aren't set up right, or
208 there isn't enough buffer space left. */
210 if ((buffer == NULL) || (buflen == NULL) || (*buflen < len)) {
211 return NULL;
214 /* Return an index into the static buffer */
216 result = *buffer;
217 *buffer += len;
218 *buflen -= len;
220 return result;
223 /****************************************************************************
224 gethostbyname() - we ignore any domain portion of the name and only
225 handle names that are at most 15 characters long
226 **************************************************************************/
227 NSS_STATUS
228 _nss_wins_gethostbyname_r(const char *hostname, struct hostent *he,
229 char *buffer, size_t buflen, int *h_errnop)
231 NSS_STATUS nss_status = NSS_STATUS_SUCCESS;
232 char *ip;
233 struct in_addr in;
234 int i;
235 fstring name;
236 size_t namelen;
237 int rc;
239 #if HAVE_PTHREAD
240 pthread_mutex_lock(&wins_nss_mutex);
241 #endif
243 memset(he, '\0', sizeof(*he));
244 fstrcpy(name, hostname);
246 /* Do lookup */
248 ip = lookup_byname_backend(name);
249 if (ip == NULL) {
250 nss_status = NSS_STATUS_NOTFOUND;
251 goto out;
254 rc = inet_pton(AF_INET, ip, &in);
255 wbcFreeMemory(ip);
256 if (rc == 0) {
257 nss_status = NSS_STATUS_TRYAGAIN;
258 goto out;
261 /* Copy h_name */
263 namelen = strlen(name) + 1;
265 if ((he->h_name = get_static(&buffer, &buflen, namelen)) == NULL) {
266 nss_status = NSS_STATUS_TRYAGAIN;
267 goto out;
270 memcpy(he->h_name, name, namelen);
272 /* Copy h_addr_list, align to pointer boundary first */
274 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
275 i = sizeof(char*) - i;
277 if (get_static(&buffer, &buflen, i) == NULL) {
278 nss_status = NSS_STATUS_TRYAGAIN;
279 goto out;
282 if ((he->h_addr_list = (char **)get_static(
283 &buffer, &buflen, 2 * sizeof(char *))) == NULL) {
284 nss_status = NSS_STATUS_TRYAGAIN;
285 goto out;
288 if ((he->h_addr_list[0] = get_static(&buffer, &buflen,
289 INADDRSZ)) == NULL) {
290 nss_status = NSS_STATUS_TRYAGAIN;
291 goto out;
294 memcpy(he->h_addr_list[0], &in, INADDRSZ);
296 he->h_addr_list[1] = NULL;
298 /* Set h_addr_type and h_length */
300 he->h_addrtype = AF_INET;
301 he->h_length = INADDRSZ;
303 /* Set h_aliases */
305 if ((i = (unsigned long)(buffer) % sizeof(char*)) != 0)
306 i = sizeof(char*) - i;
308 if (get_static(&buffer, &buflen, i) == NULL) {
309 nss_status = NSS_STATUS_TRYAGAIN;
310 goto out;
313 if ((he->h_aliases = (char **)get_static(
314 &buffer, &buflen, sizeof(char *))) == NULL) {
315 nss_status = NSS_STATUS_TRYAGAIN;
316 goto out;
319 he->h_aliases[0] = NULL;
321 nss_status = NSS_STATUS_SUCCESS;
323 out:
325 #if HAVE_PTHREAD
326 pthread_mutex_unlock(&wins_nss_mutex);
327 #endif
328 return nss_status;
332 NSS_STATUS
333 _nss_wins_gethostbyname2_r(const char *name, int af, struct hostent *he,
334 char *buffer, size_t buflen, int *h_errnop)
336 NSS_STATUS nss_status;
338 if(af!=AF_INET) {
339 *h_errnop = NO_DATA;
340 nss_status = NSS_STATUS_UNAVAIL;
341 } else {
342 nss_status = _nss_wins_gethostbyname_r(
343 name, he, buffer, buflen, h_errnop);
345 return nss_status;
347 #endif