7 #include <sys/socket.h>
8 #include <netinet/in.h>
10 #include <arpa/inet.h>
12 #include <sys/types.h>
15 #define CONFIGFILE "/etc/nss-icmp.conf"
18 struct cache *next, *prev;
27 static int inited = 0;
28 static int timeout = -1;
29 static int usecache = 1;
30 static time_t nfttl = 300;
31 static struct cache *cache = NULL;
33 static void readconfig(void)
39 if((f = fopen(CONFIGFILE, "r")) == NULL)
42 while(fgets(linebuf, sizeof(linebuf), f) != NULL) {
45 if((p = strchr(linebuf, '\n')) != NULL)
47 if((p = strchr(linebuf, ' ')) != NULL) {
51 if(!strcmp(linebuf, "timeout")) {
56 if(!strcmp(linebuf, "ttlnotfound")) {
61 if(!strcmp(linebuf, "nocache")) {
69 static void freecache(struct cache *cc)
74 cc->next->prev = cc->prev;
76 cc->prev->next = cc->next;
81 if(cc->names != NULL) {
82 for(i = 0; cc->names[i] != NULL; i++)
89 static void cachenotfound(const void *addr, socklen_t len, int af, time_t ttl)
93 for(cc = cache; cc != NULL; cc = cc->next) {
94 if((cc->af == af) && (cc->addrlen == len) && !memcmp(cc->addr, addr, len))
98 if((cc = malloc(sizeof(*cc))) == NULL)
100 memset(cc, 0, sizeof(*cc));
101 if((cc->addr = malloc(len)) == NULL) {
105 memcpy(cc->addr, addr, len);
120 static void updatecache(const void *addr, socklen_t len, int af, char **names, time_t ttl)
125 for(cc = cache; cc != NULL; cc = cc->next) {
126 if((cc->af == af) && (cc->addrlen == len) && !memcmp(cc->addr, addr, len))
130 if((cc = malloc(sizeof(*cc))) == NULL)
132 memset(cc, 0, sizeof(*cc));
133 if((cc->addr = malloc(len)) == NULL) {
137 memcpy(cc->addr, addr, len);
143 for(i = 0; names[i] != NULL; i++);
144 if((cc->names = malloc(sizeof(*(cc->names)) * (i + 1))) == NULL) {
148 memset(cc->names, 0, sizeof(*(cc->names)) * (i + 1));
149 for(i = 0; names[i] != NULL; i++) {
150 if((cc->names[i] = malloc(strlen(names[i]) + 1)) == NULL) {
154 strcpy(cc->names[i], names[i]);
164 static void expirecache(void)
166 struct cache *cc, *next;
170 for(cc = cache; cc != NULL; cc = next) {
172 if(now - cc->at > cc->ttl) {
179 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)
188 int an, thislen, ttl;
201 retbuf = (struct retstruct *)buffer;
202 if((buflen < sizeof(*retbuf)) || (len > sizeof(retbuf->retaddr))) {
204 *h_errnop = NETDB_INTERNAL;
205 return(NSS_STATUS_UNAVAIL);
210 for(cc = cache; cc != NULL; cc = cc->next) {
211 if((cc->af == af) && (cc->addrlen == len) && !memcmp(cc->addr, addr, len))
219 ap = (u_int8_t *)addr;
220 if(inet_ntop(af, addr, addrbuf, sizeof(addrbuf)) == NULL) {
222 *h_errnop = NETDB_INTERNAL;
223 return(NSS_STATUS_UNAVAIL);
228 *h_errnop = NETDB_INTERNAL;
229 return(NSS_STATUS_UNAVAIL);
231 /* I honestly don't know if it is considered OK to fork in other
232 * people's programs. We need a SUID worker, though, so there's
233 * little choice that I can see. */
234 if((child = fork()) < 0) {
236 *h_errnop = NETDB_INTERNAL;
237 return(NSS_STATUS_UNAVAIL);
242 char timeoutbuf[128];
244 if((fd = open("/dev/null", O_WRONLY)) < 0)
249 for(i = 3; i < FD_SETSIZE; i++)
253 snprintf(timeoutbuf, sizeof(timeoutbuf), "%i", timeout);
254 execlp("idnlookup", "idnlookup", "-Tt", timeoutbuf, addrbuf, NULL);
256 execlp("idnlookup", "idnlookup", "-T", addrbuf, NULL);
265 ret = read(pfd[0], addrbuf + rl, sizeof(addrbuf) - rl);
268 *h_errnop = NETDB_INTERNAL;
270 return(NSS_STATUS_UNAVAIL);
273 if(rl >= sizeof(addrbuf) - 1) {
275 *h_errnop = NETDB_INTERNAL;
277 return(NSS_STATUS_UNAVAIL);
283 if((p = strchr(addrbuf, '\n')) == NULL) {
285 cachenotfound(addr, len, af, nfttl);
286 *h_errnop = TRY_AGAIN; /* XXX: Is this correct? */
287 return(NSS_STATUS_NOTFOUND);
293 p3 = buffer + sizeof(*retbuf);
294 while((p2 = strchr(p, '\n')) != NULL) {
299 if((p3 - buffer) + thislen + 1 > buflen) {
301 *h_errnop = NETDB_INTERNAL;
302 return(NSS_STATUS_UNAVAIL);
304 memcpy(p3, p, thislen + 1);
305 retbuf->aliaslist[an] = p3;
310 *h_errnop = NETDB_INTERNAL;
311 return(NSS_STATUS_UNAVAIL);
316 cachenotfound(addr, len, af, nfttl);
317 *h_errnop = TRY_AGAIN; /* XXX: Is this correct? */
318 return(NSS_STATUS_NOTFOUND);
320 retbuf->aliaslist[an] = NULL;
323 updatecache(addr, len, af, retbuf->aliaslist, ttl);
326 *h_errnop = TRY_AGAIN; /* XXX: Is this correct? */
327 return(NSS_STATUS_NOTFOUND);
330 p3 = buffer + sizeof(*retbuf);
331 for(i = 0; cc->names[i] != NULL; i++) {
332 thislen = strlen(cc->names[i]);
333 if((p3 - buffer) + thislen + 1 > buflen) {
335 *h_errnop = NETDB_INTERNAL;
336 return(NSS_STATUS_UNAVAIL);
338 memcpy(p3, cc->names[i], thislen + 1);
339 retbuf->aliaslist[an] = p3;
343 *h_errnop = NETDB_INTERNAL;
344 return(NSS_STATUS_UNAVAIL);
349 memcpy(retbuf->retaddr, addr, len);
350 retbuf->addrlist[0] = retbuf->retaddr;
351 retbuf->addrlist[1] = NULL;
352 result->h_name = retbuf->aliaslist[0];
353 result->h_aliases = retbuf->aliaslist;
354 result->h_addr_list = retbuf->addrlist;
355 result->h_addrtype = af;
356 result->h_length = len;
358 *h_errnop = NETDB_SUCCESS;
359 return(NSS_STATUS_SUCCESS);