lwconfig.c revision d8c8722f28ca439b9ca46f109e2804a7eb33a1ac
dc3d68d6fe0c0dc0d43015ed6b651f407b38ee25Tinderbox User * Copyright (C) 2000 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff * copyright notice and this permission notice appear in all copies.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
23daf8948b2e5462ccea082a441e9759dffa50cdMark Andrews/* $Id: lwconfig.c,v 1.22 2000/07/07 18:58:46 bwelling Exp $ */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence *** Module for parsing resolv.conf files.
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff *** entry points are:
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff *** lwres_conf_init(lwres_context_t *ctx)
07072c9456f1112705db701bb35f12dbdcb217b0Mark Andrews *** intializes data structure for subsequent config parsing.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 *** lwres_conf_parse(lwres_context_t *ctx, const char *filename)
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff *** parses a file and fills in the data structure.
a8dcebd0419f27234664e89b9cd48bc54cad08a7Michael Graff *** lwres_conf_print(lwres_context_t *ctx, FILE *fp)
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff *** prints the config data structure to the FILE.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence *** lwres_conf_clear(lwres_context_t *ctx)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 *** frees up all the internal memory used by the config data
54489ba1672458382e8dfb5490fcfc7230f8ea85Evan Hunt *** structure, returning it to the lwres_context_t.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinextern int lwres_net_pton(int af, const char *src, void *dst);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinextern const char *lwres_net_ntop(int af, const void *src, char *dst,
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrewslwres_conf_parsenameserver(lwres_context_t *ctx, FILE *fp);
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Grafflwres_conf_parsedomain(lwres_context_t *ctx, FILE *fp);
e21d199dca95aff5d50f133d6b064309e209af00Brian Wellingtonlwres_conf_parsesearch(lwres_context_t *ctx, FILE *fp);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉lwres_conf_parsesortlist(lwres_context_t *ctx, FILE *fp);
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Grafflwres_conf_parseoption(lwres_context_t *ctx, FILE *fp);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉lwres_create_addr(const char *buff, lwres_addr_t *addr);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * Eat characters from FP until EOL or EOF. Returns EOF or '\n'
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff * Eats white space up to next newline or non-whitespace character (of
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff * EOF). Returns the last character read. Comments are considered white
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff while (ch != '\n' && ch != EOF && isspace((unsigned char)ch))
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff * Skip over any leading whitespace and then read in the next sequence of
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * non-whitespace characters. In this context newline is not considered
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * whitespace. Returns EOF on end-of-file, or the character
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * that caused the reading to stop.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉getword(FILE *fp, char *buffer, size_t size) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (ch == EOF || isspace((unsigned char)ch))
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 else if ((size_t) (p - buffer) == size - 1)
e2fe1fda755f24f593406dc26fed87e8ea1bb502Tatuya JINMEI 神明達哉 *p++ = (char)ch;
96b3cb85d3b06d99323a6ea7ae04f4eb3d74e8bcMark Andrews } while (1);
96b3cb85d3b06d99323a6ea7ae04f4eb3d74e8bcMark Andrewsstatic char *
96b3cb85d3b06d99323a6ea7ae04f4eb3d74e8bcMark Andrewslwres_strdup(lwres_context_t *ctx, const char *str) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein for (i = 0 ; i < LWRES_CONFMAXNAMESERVERS ; i++)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 lwres_resetaddr(&confdata->nameservers[i]);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein for (i = 0 ; i < LWRES_CONFMAXSEARCH ; i++)
54489ba1672458382e8dfb5490fcfc7230f8ea85Evan Hunt for (i = 0 ; i < LWRES_CONFMAXSORTLIST ; i++) {
f98d6edb191348477c9c5a156003df627d9bc42cBrian Wellington lwres_resetaddr(&confdata->nameservers[i]);
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews for (i = 0 ; i < LWRES_CONFMAXSORTLIST ; i++) {
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrewslwres_conf_parsenameserver(lwres_context_t *ctx, FILE *fp) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (confdata->nsnext == LWRES_CONFMAXNAMESERVERS)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return (LWRES_R_FAILURE); /* Nothing on line. */
e2fe1fda755f24f593406dc26fed87e8ea1bb502Tatuya JINMEI 神明達哉 return (LWRES_R_FAILURE); /* Extra junk on line. */
35f06ab0e6d5ad26176b7584de7b4d405272ba68Brian Wellington &confdata->nameservers[confdata->nsnext++]);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉lwres_conf_parsedomain(lwres_context_t *ctx, FILE *fp) {
0a9fedafec59fd3ec2eeadc3f123db163e71c0fbMichael Graff return (LWRES_R_FAILURE); /* Nothing else on line. */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return (LWRES_R_FAILURE); /* Extra junk on line. */
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff * Search and domain are mutually exclusive.
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt for (i = 0 ; i < LWRES_CONFMAXSEARCH ; i++) {
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafssonlwres_conf_parsesearch(lwres_context_t *ctx, FILE *fp) {
f6579931b38b357013736ce3efc79a34480af5ceBrian Wellington * Search and domain are mutually exclusive.
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff * Remove any previous search definitions.
f19e6ef1eb7d5abf547fc1af24e561bdc5d75b22Mark Andrews for (idx = 0 ; idx < LWRES_CONFMAXSEARCH ; idx++) {
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt return (LWRES_R_FAILURE); /* Nothing else on line. */
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson if (confdata->searchnxt == LWRES_CONFMAXSEARCH)
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff confdata->search[idx] = lwres_strdup(ctx, word);
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafssonlwres_create_addr(const char *buffer, lwres_addr_t *addr) {
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff if (lwres_net_pton(AF_INET, buffer, &addrbuff) == 1) {
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff } else if (lwres_net_pton(AF_INET6, buffer, &addrbuff) == 1) {
f6579931b38b357013736ce3efc79a34480af5ceBrian Wellington return (LWRES_R_FAILURE); /* Unrecognised format. */
ec46482ef3c0c3e9747f0074cb9263adb8aef961Andreas Gustafssonlwres_conf_parsesortlist(lwres_context_t *ctx, FILE *fp) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return (LWRES_R_FAILURE); /* Empty line after keyword. */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (confdata->sortlistnxt == LWRES_CONFMAXSORTLIST)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return (LWRES_R_FAILURE); /* Too many values. */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 res = lwres_create_addr(word, &confdata->sortlist[idx].addr);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * Make up a mask.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 memset(&confdata->sortlist[idx].mask.address, 0xff,
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉lwres_conf_parseoption(lwres_context_t *ctx, FILE *fp) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return (LWRES_R_FAILURE); /* Empty line after keyword. */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 } else if (strcmp("no_tld_query", word) == 0) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (ndots < 0 || ndots > 0xff) /* Out of range. */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉lwres_conf_parse(lwres_context_t *ctx, const char *filename) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 stopchar = getword(fp, word, sizeof(word));
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 else if (strcmp(word, "nameserver") == 0)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 rval = lwres_conf_parsenameserver(ctx, fp);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 rval = lwres_conf_parsesortlist(ctx, fp);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /* unrecognised word. Ignore entire line */
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff if (ret == LWRES_R_SUCCESS && rval != LWRES_R_SUCCESS)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉lwres_conf_print(lwres_context_t *ctx, FILE *fp) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 const char *p;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 REQUIRE(confdata->nsnext <= LWRES_CONFMAXNAMESERVERS);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 for (i = 0 ; i < confdata->nsnext ; i++) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 af = lwresaddr2af(confdata->nameservers[i].family);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 p = lwres_net_ntop(af, confdata->nameservers[i].address,
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff fprintf(fp, "domain %s\n", confdata->domainname);
0941f35ad9b4f48a5324af405a730c3cb3e0aad8Michael Graff REQUIRE(confdata->searchnxt <= LWRES_CONFMAXSEARCH);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 for (i = 0 ; i < confdata->searchnxt ; i++)
0941f35ad9b4f48a5324af405a730c3cb3e0aad8Michael Graff REQUIRE(confdata->sortlistnxt <= LWRES_CONFMAXSORTLIST);
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews for (i = 0 ; i < confdata->sortlistnxt ; i++) {
0941f35ad9b4f48a5324af405a730c3cb3e0aad8Michael Graff af = lwresaddr2af(confdata->sortlist[i].addr.family);
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff memset(&tmpaddr.address, 0xff, tmpaddr.length);
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graff fprintf(fp, "options ndots:%d\n", confdata->ndots);