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>
37 #define CONFIGFILE "/etc/nss-icmp.conf"
39 #define DEBUGP(format...) fprintf(stderr, "nss-icmp: " format);
41 #define DEBUGP(format...)
45 struct cache *next, *prev;
54 static int inited = 0;
55 static int timeout = -1;
56 static int usecache = 1;
57 static time_t nfttl = 300;
58 static struct cache *cache = NULL;
60 static void readconfig(void)
66 if((f = fopen(CONFIGFILE, "r")) == NULL)
69 while(fgets(linebuf, sizeof(linebuf), f) != NULL) {
72 if((p = strchr(linebuf, '\n')) != NULL)
74 if((p = strchr(linebuf, ' ')) != NULL) {
78 if(!strcmp(linebuf, "timeout")) {
83 if(!strcmp(linebuf, "ttlnotfound")) {
88 if(!strcmp(linebuf, "nocache")) {
96 static void freecache(struct cache *cc)
101 cc->next->prev = cc->prev;
103 cc->prev->next = cc->next;
108 if(cc->names != NULL) {
109 for(i = 0; cc->names[i] != NULL; i++)
116 static void cachenotfound(const void *addr, socklen_t len, int af, time_t ttl)
120 for(cc = cache; cc != NULL; cc = cc->next) {
121 if((cc->af == af) && (cc->addrlen == len) && !memcmp(cc->addr, addr, len))
125 if((cc = malloc(sizeof(*cc))) == NULL)
127 memset(cc, 0, sizeof(*cc));
128 if((cc->addr = malloc(len)) == NULL) {
132 memcpy(cc->addr, addr, len);
147 static void updatecache(const void *addr, socklen_t len, int af, char **names, time_t ttl)
152 for(cc = cache; cc != NULL; cc = cc->next) {
153 if((cc->af == af) && (cc->addrlen == len) && !memcmp(cc->addr, addr, len))
157 if((cc = malloc(sizeof(*cc))) == NULL)
159 memset(cc, 0, sizeof(*cc));
160 if((cc->addr = malloc(len)) == NULL) {
164 memcpy(cc->addr, addr, len);
170 for(i = 0; names[i] != NULL; i++);
171 if((cc->names = malloc(sizeof(*(cc->names)) * (i + 1))) == NULL) {
175 memset(cc->names, 0, sizeof(*(cc->names)) * (i + 1));
176 for(i = 0; names[i] != NULL; i++) {
177 if((cc->names[i] = malloc(strlen(names[i]) + 1)) == NULL) {
181 strcpy(cc->names[i], names[i]);
191 static void expirecache(void)
193 struct cache *cc, *next;
197 for(cc = cache; cc != NULL; cc = next) {
199 if(now - cc->at > cc->ttl) {
206 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)
215 int an, thislen, ttl;
229 retbuf = (struct retstruct *)buffer;
230 if((buflen < sizeof(*retbuf)) || (len > sizeof(retbuf->retaddr))) {
232 *h_errnop = NETDB_INTERNAL;
233 return(NSS_STATUS_UNAVAIL);
236 DEBUGP("starting lookup\n");
240 for(cc = cache; cc != NULL; cc = cc->next) {
241 if((cc->af == af) && (cc->addrlen == len) && !memcmp(cc->addr, addr, len))
249 DEBUGP("address not in cache, looking up for real\n");
250 ap = (u_int8_t *)addr;
251 if(inet_ntop(af, addr, addrbuf, sizeof(addrbuf)) == NULL) {
253 *h_errnop = NETDB_INTERNAL;
254 return(NSS_STATUS_UNAVAIL);
256 DEBUGP("address is %s\n", addrbuf);
260 *h_errnop = NETDB_INTERNAL;
261 return(NSS_STATUS_UNAVAIL);
263 /* I honestly don't know if it is considered OK to fork in other
264 * people's programs. We need a SUID worker, though, so there's
265 * little choice that I can see. */
266 if((child = fork()) < 0) {
268 *h_errnop = NETDB_INTERNAL;
269 return(NSS_STATUS_UNAVAIL);
274 char timeoutbuf[128];
276 if((fd = open("/dev/null", O_WRONLY)) < 0)
281 for(i = 3; i < FD_SETSIZE; i++)
285 snprintf(timeoutbuf, sizeof(timeoutbuf), "%i", timeout);
286 execlp("idnlookup", "idnlookup", "-Tt", timeoutbuf, addrbuf, NULL);
288 execlp("idnlookup", "idnlookup", "-T", addrbuf, NULL);
297 ret = read(pfd[0], addrbuf + rl, sizeof(addrbuf) - rl);
300 *h_errnop = NETDB_INTERNAL;
302 return(NSS_STATUS_UNAVAIL);
305 if(rl >= sizeof(addrbuf) - 1) {
307 *h_errnop = NETDB_INTERNAL;
309 return(NSS_STATUS_UNAVAIL);
315 waitpid(child, &status, 0);
317 if((p = strchr(addrbuf, '\n')) == NULL) {
319 cachenotfound(addr, len, af, nfttl);
320 *h_errnop = TRY_AGAIN; /* XXX: Is this correct? */
321 return(NSS_STATUS_NOTFOUND);
327 p3 = buffer + sizeof(*retbuf);
328 while((p2 = strchr(p, '\n')) != NULL) {
333 if((p3 - buffer) + thislen + 1 > buflen) {
335 *h_errnop = NETDB_INTERNAL;
336 return(NSS_STATUS_UNAVAIL);
338 memcpy(p3, p, thislen + 1);
339 retbuf->aliaslist[an] = p3;
344 *h_errnop = NETDB_INTERNAL;
345 return(NSS_STATUS_UNAVAIL);
350 cachenotfound(addr, len, af, nfttl);
351 *h_errnop = TRY_AGAIN; /* XXX: Is this correct? */
352 return(NSS_STATUS_NOTFOUND);
354 retbuf->aliaslist[an] = NULL;
357 updatecache(addr, len, af, retbuf->aliaslist, ttl);
359 DEBUGP("address found in cache\n");
361 *h_errnop = TRY_AGAIN; /* XXX: Is this correct? */
362 return(NSS_STATUS_NOTFOUND);
365 p3 = buffer + sizeof(*retbuf);
366 for(i = 0; cc->names[i] != NULL; i++) {
367 thislen = strlen(cc->names[i]);
368 DEBUGP("filling in address %s, length %i\n", cc->names[i], thislen);
369 if((p3 - buffer) + thislen + 1 > buflen) {
371 *h_errnop = NETDB_INTERNAL;
372 return(NSS_STATUS_UNAVAIL);
374 memcpy(p3, cc->names[i], thislen + 1);
375 retbuf->aliaslist[i] = p3;
378 retbuf->aliaslist[i] = NULL;
381 DEBUGP("returning hostent\n");
382 memcpy(retbuf->retaddr, addr, len);
383 retbuf->addrlist[0] = retbuf->retaddr;
384 retbuf->addrlist[1] = NULL;
385 result->h_name = retbuf->aliaslist[0];
386 result->h_aliases = retbuf->aliaslist;
387 result->h_addr_list = retbuf->addrlist;
388 result->h_addrtype = af;
389 result->h_length = len;
391 *h_errnop = NETDB_SUCCESS;
392 DEBUGP("returning\n");
393 return(NSS_STATUS_SUCCESS);
398 * compile-command: "gcc -shared -Wall -g -o libnss_icmp.so.2 nss-icmp.c"