2 * nss-icmp or libnss_icmp - GNU C Library NSS module to query host
4 * Copyright (C) 2005 Fredrik Tolf <fredrik@dolda2000.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 #include <sys/socket.h>
29 #include <netinet/in.h>
31 #include <arpa/inet.h>
33 #include <sys/types.h>
36 #define CONFIGFILE "/etc/nss-icmp.conf"
38 #define DEBUGP(format...) fprintf(stderr, "nss-icmp: " format);
40 #define DEBUGP(format...)
44 struct cache *next, *prev;
53 static int inited = 0;
54 static int timeout = -1;
55 static int usecache = 1;
56 static time_t nfttl = 300;
57 static struct cache *cache = NULL;
59 static void readconfig(void)
65 if((f = fopen(CONFIGFILE, "r")) == NULL)
68 while(fgets(linebuf, sizeof(linebuf), f) != NULL) {
71 if((p = strchr(linebuf, '\n')) != NULL)
73 if((p = strchr(linebuf, ' ')) != NULL) {
77 if(!strcmp(linebuf, "timeout")) {
82 if(!strcmp(linebuf, "ttlnotfound")) {
87 if(!strcmp(linebuf, "nocache")) {
95 static void freecache(struct cache *cc)
100 cc->next->prev = cc->prev;
102 cc->prev->next = cc->next;
107 if(cc->names != NULL) {
108 for(i = 0; cc->names[i] != NULL; i++)
115 static void cachenotfound(const void *addr, socklen_t len, int af, time_t ttl)
119 for(cc = cache; cc != NULL; cc = cc->next) {
120 if((cc->af == af) && (cc->addrlen == len) && !memcmp(cc->addr, addr, len))
124 if((cc = malloc(sizeof(*cc))) == NULL)
126 memset(cc, 0, sizeof(*cc));
127 if((cc->addr = malloc(len)) == NULL) {
131 memcpy(cc->addr, addr, len);
146 static void updatecache(const void *addr, socklen_t len, int af, char **names, time_t ttl)
151 for(cc = cache; cc != NULL; cc = cc->next) {
152 if((cc->af == af) && (cc->addrlen == len) && !memcmp(cc->addr, addr, len))
156 if((cc = malloc(sizeof(*cc))) == NULL)
158 memset(cc, 0, sizeof(*cc));
159 if((cc->addr = malloc(len)) == NULL) {
163 memcpy(cc->addr, addr, len);
169 for(i = 0; names[i] != NULL; i++);
170 if((cc->names = malloc(sizeof(*(cc->names)) * (i + 1))) == NULL) {
174 memset(cc->names, 0, sizeof(*(cc->names)) * (i + 1));
175 for(i = 0; names[i] != NULL; i++) {
176 if((cc->names[i] = malloc(strlen(names[i]) + 1)) == NULL) {
180 strcpy(cc->names[i], names[i]);
190 static void expirecache(void)
192 struct cache *cc, *next;
196 for(cc = cache; cc != NULL; cc = next) {
198 if(now - cc->at > cc->ttl) {
205 enum nss_status _nss_icmp_gethostbyaddr_r(const void *addr, socklen_t len, int af, struct hostent *result, char *buffer, size_t buflen, int *errnop, int *h_errnop)
214 int an, thislen, ttl;
227 retbuf = (struct retstruct *)buffer;
228 if((buflen < sizeof(*retbuf)) || (len > sizeof(retbuf->retaddr))) {
230 *h_errnop = NETDB_INTERNAL;
231 return(NSS_STATUS_UNAVAIL);
234 DEBUGP("starting lookup\n");
238 for(cc = cache; cc != NULL; cc = cc->next) {
239 if((cc->af == af) && (cc->addrlen == len) && !memcmp(cc->addr, addr, len))
247 DEBUGP("address not in cache, looking up for real\n");
248 ap = (u_int8_t *)addr;
249 if(inet_ntop(af, addr, addrbuf, sizeof(addrbuf)) == NULL) {
251 *h_errnop = NETDB_INTERNAL;
252 return(NSS_STATUS_UNAVAIL);
254 DEBUGP("address is %s\n", addrbuf);
258 *h_errnop = NETDB_INTERNAL;
259 return(NSS_STATUS_UNAVAIL);
261 /* I honestly don't know if it is considered OK to fork in other
262 * people's programs. We need a SUID worker, though, so there's
263 * little choice that I can see. */
264 if((child = fork()) < 0) {
266 *h_errnop = NETDB_INTERNAL;
267 return(NSS_STATUS_UNAVAIL);
272 char timeoutbuf[128];
274 if((fd = open("/dev/null", O_WRONLY)) < 0)
279 for(i = 3; i < FD_SETSIZE; i++)
283 snprintf(timeoutbuf, sizeof(timeoutbuf), "%i", timeout);
284 execlp("idnlookup", "idnlookup", "-Tt", timeoutbuf, addrbuf, NULL);
286 execlp("idnlookup", "idnlookup", "-T", addrbuf, NULL);
295 ret = read(pfd[0], addrbuf + rl, sizeof(addrbuf) - rl);
298 *h_errnop = NETDB_INTERNAL;
300 return(NSS_STATUS_UNAVAIL);
303 if(rl >= sizeof(addrbuf) - 1) {
305 *h_errnop = NETDB_INTERNAL;
307 return(NSS_STATUS_UNAVAIL);
313 if((p = strchr(addrbuf, '\n')) == NULL) {
315 cachenotfound(addr, len, af, nfttl);
316 *h_errnop = TRY_AGAIN; /* XXX: Is this correct? */
317 return(NSS_STATUS_NOTFOUND);
323 p3 = buffer + sizeof(*retbuf);
324 while((p2 = strchr(p, '\n')) != NULL) {
329 if((p3 - buffer) + thislen + 1 > buflen) {
331 *h_errnop = NETDB_INTERNAL;
332 return(NSS_STATUS_UNAVAIL);
334 memcpy(p3, p, thislen + 1);
335 retbuf->aliaslist[an] = p3;
340 *h_errnop = NETDB_INTERNAL;
341 return(NSS_STATUS_UNAVAIL);
346 cachenotfound(addr, len, af, nfttl);
347 *h_errnop = TRY_AGAIN; /* XXX: Is this correct? */
348 return(NSS_STATUS_NOTFOUND);
350 retbuf->aliaslist[an] = NULL;
353 updatecache(addr, len, af, retbuf->aliaslist, ttl);
355 DEBUGP("address found in cache\n");
357 *h_errnop = TRY_AGAIN; /* XXX: Is this correct? */
358 return(NSS_STATUS_NOTFOUND);
361 p3 = buffer + sizeof(*retbuf);
362 for(i = 0; cc->names[i] != NULL; i++) {
363 thislen = strlen(cc->names[i]);
364 DEBUGP("filling in address %s, length %i\n", cc->names[i], thislen);
365 if((p3 - buffer) + thislen + 1 > buflen) {
367 *h_errnop = NETDB_INTERNAL;
368 return(NSS_STATUS_UNAVAIL);
370 memcpy(p3, cc->names[i], thislen + 1);
371 retbuf->aliaslist[i] = p3;
374 retbuf->aliaslist[i] = NULL;
377 DEBUGP("returning hostent\n");
378 memcpy(retbuf->retaddr, addr, len);
379 retbuf->addrlist[0] = retbuf->retaddr;
380 retbuf->addrlist[1] = NULL;
381 result->h_name = retbuf->aliaslist[0];
382 result->h_aliases = retbuf->aliaslist;
383 result->h_addr_list = retbuf->addrlist;
384 result->h_addrtype = af;
385 result->h_length = len;
387 *h_errnop = NETDB_SUCCESS;
388 DEBUGP("returning\n");
389 return(NSS_STATUS_SUCCESS);
394 * compile-command: "gcc -shared -Wall -g -o libnss_icmp.so.2 nss-icmp.c"