| OLD | NEW |
| 1 #include "lookup.h" | 1 #include "lookup.h" |
| 2 #include "stdio_impl.h" | 2 #include "stdio_impl.h" |
| 3 #include <ctype.h> | 3 #include <ctype.h> |
| 4 #include <errno.h> | 4 #include <errno.h> |
| 5 #include <string.h> | 5 #include <string.h> |
| 6 #include <netinet/in.h> | 6 #include <netinet/in.h> |
| 7 | 7 |
| 8 int __get_resolv_conf(struct resolvconf *conf, char *search, size_t search_sz) | 8 int __get_resolv_conf(struct resolvconf* conf, char* search, size_t search_sz) { |
| 9 { | 9 char line[256]; |
| 10 » char line[256]; | 10 unsigned char _buf[256]; |
| 11 » unsigned char _buf[256]; | 11 FILE *f, _f; |
| 12 » FILE *f, _f; | 12 int nns = 0; |
| 13 » int nns = 0; | |
| 14 | 13 |
| 15 » conf->ndots = 1; | 14 conf->ndots = 1; |
| 16 » conf->timeout = 5; | 15 conf->timeout = 5; |
| 17 » conf->attempts = 2; | 16 conf->attempts = 2; |
| 18 » if (search) *search = 0; | 17 if (search) |
| 18 *search = 0; |
| 19 | 19 |
| 20 » f = __fopen_rb_ca("/etc/resolv.conf", &_f, _buf, sizeof _buf); | 20 f = __fopen_rb_ca("/etc/resolv.conf", &_f, _buf, sizeof _buf); |
| 21 » if (!f) switch (errno) { | 21 if (!f) |
| 22 » case ENOENT: | 22 switch (errno) { |
| 23 » case ENOTDIR: | 23 case ENOENT: |
| 24 » case EACCES: | 24 case ENOTDIR: |
| 25 » » goto no_resolv_conf; | 25 case EACCES: |
| 26 » default: | 26 goto no_resolv_conf; |
| 27 » » return -1; | 27 default: |
| 28 » } | 28 return -1; |
| 29 } |
| 29 | 30 |
| 30 » while (fgets(line, sizeof line, f)) { | 31 while (fgets(line, sizeof line, f)) { |
| 31 » » char *p, *z; | 32 char *p, *z; |
| 32 » » if (!strchr(line, '\n') && !feof(f)) { | 33 if (!strchr(line, '\n') && !feof(f)) { |
| 33 » » » /* Ignore lines that get truncated rather than | 34 /* Ignore lines that get truncated rather than |
| 34 » » » * potentially misinterpreting them. */ | 35 * potentially misinterpreting them. */ |
| 35 » » » int c; | 36 int c; |
| 36 » » » do c = getc(f); | 37 do |
| 37 » » » while (c != '\n' && c != EOF); | 38 c = getc(f); |
| 38 » » » continue; | 39 while (c != '\n' && c != EOF); |
| 39 » » } | 40 continue; |
| 40 » » if (!strncmp(line, "options", 7) && isspace(line[7])) { | 41 } |
| 41 » » » p = strstr(line, "ndots:"); | 42 if (!strncmp(line, "options", 7) && isspace(line[7])) { |
| 42 » » » if (p && isdigit(p[6])) { | 43 p = strstr(line, "ndots:"); |
| 43 » » » » p += 6; | 44 if (p && isdigit(p[6])) { |
| 44 » » » » unsigned long x = strtoul(p, &z, 10); | 45 p += 6; |
| 45 » » » » if (z != p) conf->ndots = x > 15 ? 15 : x; | 46 unsigned long x = strtoul(p, &z, 10); |
| 46 » » » } | 47 if (z != p) |
| 47 » » » p = strstr(line, "attempts:"); | 48 conf->ndots = x > 15 ? 15 : x; |
| 48 » » » if (p && isdigit(p[6])) { | 49 } |
| 49 » » » » p += 6; | 50 p = strstr(line, "attempts:"); |
| 50 » » » » unsigned long x = strtoul(p, &z, 10); | 51 if (p && isdigit(p[6])) { |
| 51 » » » » if (z != p) conf->attempts = x > 10 ? 10 : x; | 52 p += 6; |
| 52 » » » } | 53 unsigned long x = strtoul(p, &z, 10); |
| 53 » » » p = strstr(line, "timeout:"); | 54 if (z != p) |
| 54 » » » if (p && (isdigit(p[8]) || p[8]=='.')) { | 55 conf->attempts = x > 10 ? 10 : x; |
| 55 » » » » p += 8; | 56 } |
| 56 » » » » unsigned long x = strtoul(p, &z, 10); | 57 p = strstr(line, "timeout:"); |
| 57 » » » » if (z != p) conf->timeout = x > 60 ? 60 : x; | 58 if (p && (isdigit(p[8]) || p[8] == '.')) { |
| 58 » » » } | 59 p += 8; |
| 59 » » » continue; | 60 unsigned long x = strtoul(p, &z, 10); |
| 60 » » } | 61 if (z != p) |
| 61 » » if (!strncmp(line, "nameserver", 10) && isspace(line[10])) { | 62 conf->timeout = x > 60 ? 60 : x; |
| 62 » » » if (nns >= MAXNS) continue; | 63 } |
| 63 » » » for (p=line+11; isspace(*p); p++); | 64 continue; |
| 64 » » » for (z=p; *z && !isspace(*z); z++); | 65 } |
| 65 » » » *z=0; | 66 if (!strncmp(line, "nameserver", 10) && isspace(line[10])) { |
| 66 » » » if (__lookup_ipliteral(conf->ns+nns, p, AF_UNSPEC) > 0) | 67 if (nns >= MAXNS) |
| 67 » » » » nns++; | 68 continue; |
| 68 » » » continue; | 69 for (p = line + 11; isspace(*p); p++) |
| 69 » » } | 70 ; |
| 71 for (z = p; *z && !isspace(*z); z++) |
| 72 ; |
| 73 *z = 0; |
| 74 if (__lookup_ipliteral(conf->ns + nns, p, AF_UNSPEC) > 0) |
| 75 nns++; |
| 76 continue; |
| 77 } |
| 70 | 78 |
| 71 » » if (!search) continue; | 79 if (!search) |
| 72 » » if ((strncmp(line, "domain", 6) && strncmp(line, "search", 6)) | 80 continue; |
| 73 » » || !isspace(line[6])) | 81 if ((strncmp(line, "domain", 6) && strncmp(line, "search", 6)) || |
| 74 » » » continue; | 82 !isspace(line[6])) |
| 75 » » for (p=line+7; isspace(*p); p++); | 83 continue; |
| 76 » » size_t l = strlen(p); | 84 for (p = line + 7; isspace(*p); p++) |
| 77 » » /* This can never happen anyway with chosen buffer sizes. */ | 85 ; |
| 78 » » if (l >= search_sz) continue; | 86 size_t l = strlen(p); |
| 79 » » memcpy(search, p, l+1); | 87 /* This can never happen anyway with chosen buffer sizes. */ |
| 80 » } | 88 if (l >= search_sz) |
| 89 continue; |
| 90 memcpy(search, p, l + 1); |
| 91 } |
| 81 | 92 |
| 82 » __fclose_ca(f); | 93 __fclose_ca(f); |
| 83 | 94 |
| 84 no_resolv_conf: | 95 no_resolv_conf: |
| 85 » if (!nns) { | 96 if (!nns) { |
| 86 » » __lookup_ipliteral(conf->ns, "127.0.0.1", AF_UNSPEC); | 97 __lookup_ipliteral(conf->ns, "127.0.0.1", AF_UNSPEC); |
| 87 » » nns = 1; | 98 nns = 1; |
| 88 » } | 99 } |
| 89 | 100 |
| 90 » conf->nns = nns; | 101 conf->nns = nns; |
| 91 | 102 |
| 92 » return 0; | 103 return 0; |
| 93 } | 104 } |
| OLD | NEW |