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>
41 #define CONFIGFILE "/etc/nss-icmp.conf"
45 #define DEBUGP(format...) fprintf(stderr, "nss-icmp: " format);
47 #define DEBUGP(format...)
51 struct cache *next, *prev;
60 static int inited = 0;
61 static int timeout = -1;
62 static int usecache = 1;
63 static time_t nfttl = 300;
64 static struct cache *cache = NULL;
66 static void readconfig(void)
72 if((f = fopen(CONFIGFILE, "r")) == NULL)
75 while(fgets(linebuf, sizeof(linebuf), f) != NULL) {
78 if((p = strchr(linebuf, '\n')) != NULL)
80 if((p = strchr(linebuf, ' ')) != NULL) {
84 if(!strcmp(linebuf, "timeout")) {
89 if(!strcmp(linebuf, "ttlnotfound")) {
94 if(!strcmp(linebuf, "nocache")) {
102 static void freecache(struct cache *cc)
107 cc->next->prev = cc->prev;
109 cc->prev->next = cc->next;
114 if(cc->names != NULL) {
115 for(i = 0; cc->names[i] != NULL; i++)
122 static void cachenotfound(const void *addr, socklen_t len, int af, time_t ttl)
126 for(cc = cache; cc != NULL; cc = cc->next) {
127 if((cc->af == af) && (cc->addrlen == len) && !memcmp(cc->addr, addr, len))
131 if((cc = malloc(sizeof(*cc))) == NULL)
133 memset(cc, 0, sizeof(*cc));
134 if((cc->addr = malloc(len)) == NULL) {
138 memcpy(cc->addr, addr, len);
153 static void updatecache(const void *addr, socklen_t len, int af, char **names, time_t ttl)
158 for(cc = cache; cc != NULL; cc = cc->next) {
159 if((cc->af == af) && (cc->addrlen == len) && !memcmp(cc->addr, addr, len))
163 if((cc = malloc(sizeof(*cc))) == NULL)
165 memset(cc, 0, sizeof(*cc));
166 if((cc->addr = malloc(len)) == NULL) {
170 memcpy(cc->addr, addr, len);
176 for(i = 0; names[i] != NULL; i++);
177 if((cc->names = malloc(sizeof(*(cc->names)) * (i + 1))) == NULL) {
181 memset(cc->names, 0, sizeof(*(cc->names)) * (i + 1));
182 for(i = 0; names[i] != NULL; i++) {
183 if((cc->names[i] = malloc(strlen(names[i]) + 1)) == NULL) {
187 strcpy(cc->names[i], names[i]);
197 static void expirecache(void)
199 struct cache *cc, *next;
203 for(cc = cache; cc != NULL; cc = next) {
205 if(now - cc->at > cc->ttl) {
212 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)
221 int an, thislen, ttl;
235 retbuf = (struct retstruct *)buffer;
236 if((buflen < sizeof(*retbuf)) || (len > sizeof(retbuf->retaddr))) {
238 *h_errnop = NETDB_INTERNAL;
239 return(NSS_STATUS_UNAVAIL);
242 DEBUGP("starting lookup\n");
246 for(cc = cache; cc != NULL; cc = cc->next) {
247 if((cc->af == af) && (cc->addrlen == len) && !memcmp(cc->addr, addr, len))
255 DEBUGP("address not in cache, looking up for real\n");
256 ap = (u_int8_t *)addr;
257 if(inet_ntop(af, addr, addrbuf, sizeof(addrbuf)) == NULL) {
259 *h_errnop = NETDB_INTERNAL;
260 return(NSS_STATUS_UNAVAIL);
262 DEBUGP("address is %s\n", addrbuf);
266 *h_errnop = NETDB_INTERNAL;
267 return(NSS_STATUS_UNAVAIL);
269 /* I honestly don't know if it is considered OK to fork in other
270 * people's programs. We need a SUID worker, though, so there's
271 * little choice that I can see. */
272 if((child = fork()) < 0) {
274 *h_errnop = NETDB_INTERNAL;
275 return(NSS_STATUS_UNAVAIL);
280 char timeoutbuf[128];
282 if((fd = open("/dev/null", O_WRONLY)) < 0)
287 for(i = 3; i < FD_SETSIZE; i++)
291 snprintf(timeoutbuf, sizeof(timeoutbuf), "%i", timeout);
292 execlp("idnlookup", "idnlookup", "-Tt", timeoutbuf, addrbuf, NULL);
294 execlp("idnlookup", "idnlookup", "-T", addrbuf, NULL);
303 ret = read(pfd[0], addrbuf + rl, sizeof(addrbuf) - rl);
306 *h_errnop = NETDB_INTERNAL;
308 return(NSS_STATUS_UNAVAIL);
311 if(rl >= sizeof(addrbuf) - 1) {
313 *h_errnop = NETDB_INTERNAL;
315 return(NSS_STATUS_UNAVAIL);
321 waitpid(child, &status, 0);
323 if((p = strchr(addrbuf, '\n')) == NULL) {
325 cachenotfound(addr, len, af, nfttl);
326 *h_errnop = TRY_AGAIN; /* XXX: Is this correct? */
327 return(NSS_STATUS_NOTFOUND);
333 p3 = buffer + sizeof(*retbuf);
334 while((p2 = strchr(p, '\n')) != NULL) {
339 if((p3 - buffer) + thislen + 1 > buflen) {
341 *h_errnop = NETDB_INTERNAL;
342 return(NSS_STATUS_UNAVAIL);
344 memcpy(p3, p, thislen + 1);
345 retbuf->aliaslist[an] = p3;
350 *h_errnop = NETDB_INTERNAL;
351 return(NSS_STATUS_UNAVAIL);
356 cachenotfound(addr, len, af, nfttl);
357 *h_errnop = TRY_AGAIN; /* XXX: Is this correct? */
358 return(NSS_STATUS_NOTFOUND);
360 retbuf->aliaslist[an] = NULL;
363 updatecache(addr, len, af, retbuf->aliaslist, ttl);
365 DEBUGP("address found in cache\n");
367 *h_errnop = TRY_AGAIN; /* XXX: Is this correct? */
368 return(NSS_STATUS_NOTFOUND);
371 p3 = buffer + sizeof(*retbuf);
372 for(i = 0; cc->names[i] != NULL; i++) {
373 thislen = strlen(cc->names[i]);
374 DEBUGP("filling in address %s, length %i\n", cc->names[i], thislen);
375 if((p3 - buffer) + thislen + 1 > buflen) {
377 *h_errnop = NETDB_INTERNAL;
378 return(NSS_STATUS_UNAVAIL);
380 memcpy(p3, cc->names[i], thislen + 1);
381 retbuf->aliaslist[i] = p3;
384 retbuf->aliaslist[i] = NULL;
387 DEBUGP("returning hostent\n");
388 memcpy(retbuf->retaddr, addr, len);
389 retbuf->addrlist[0] = retbuf->retaddr;
390 retbuf->addrlist[1] = NULL;
391 result->h_name = retbuf->aliaslist[0];
392 result->h_aliases = retbuf->aliaslist;
393 result->h_addr_list = retbuf->addrlist;
394 result->h_addrtype = af;
395 result->h_length = len;
397 *h_errnop = NETDB_SUCCESS;
398 DEBUGP("returning\n");
399 return(NSS_STATUS_SUCCESS);
404 * compile-command: "gcc -shared -Wall -g -o libnss_icmp.so.2 nss-icmp.c"