lwconfig.c revision 03968c0f5eed4e5c1e17f7df5948acf9aa4870ee
5fa46bc91672ef5737aee6f99763161511566c24Tinderbox 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
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * 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
28a8f5b0de57d269cf2845c69cb6abe18cbd3b3aMark Andrews/* $Id: lwconfig.c,v 1.21 2000/07/07 18:28:58 bwelling Exp $ */
0c310d16b05ee94743d33f6920907edee6084fc8Michael Graff *** Module for parsing resolv.conf files.
de153390f5a1f6d4fa86af91d4cae772d9846ca0Mark Andrews *** entry points are:
0c310d16b05ee94743d33f6920907edee6084fc8Michael Graff *** lwres_conf_init(lwres_context_t *ctx)
822f6cdabb1edd44472c7a758b5cae71376fa9beBrian Wellington *** intializes data structure for subsequent config parsing.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews *** lwres_conf_parse(lwres_context_t *ctx, const char *filename)
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence *** parses a file and fills in the data structure.
25a66b4e41e2b0a2af4840749bac80ae78c678bfMark Andrews *** lwres_conf_print(lwres_context_t *ctx, FILE *fp)
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence *** prints the config data structure to the FILE.
973a19342597823f111fce6a8cd5adfd0e2e7c0dMark Andrews *** lwres_conf_clear(lwres_context_t *ctx)
0c310d16b05ee94743d33f6920907edee6084fc8Michael Graff *** frees up all the internal memory used by the config data
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence *** structure, returning it to the lwres_context_t.
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrenceextern int lwres_net_pton(int af, const char *src, void *dst);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsextern const char *lwres_net_ntop(int af, const void *src, char *dst,
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencelwres_conf_parsenameserver(lwres_context_t *ctx, FILE *fp);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencelwres_conf_parsedomain(lwres_context_t *ctx, FILE *fp);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencelwres_conf_parsesearch(lwres_context_t *ctx, FILE *fp);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencelwres_conf_parsesortlist(lwres_context_t *ctx, FILE *fp);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencelwres_conf_parseoption(lwres_context_t *ctx, FILE *fp);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencelwres_create_addr(const char *buff, lwres_addr_t *addr);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * Eat characters from FP until EOL or EOF. Returns EOF or '\n'
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont * Eats white space up to next newline or non-whitespace character (of
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * EOF). Returns the last character read. Comments are considered white
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence while (ch != '\n' && ch != EOF && isspace((unsigned char)ch))
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * Skip over any leading whitespace and then read in the next sequence of
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * non-whitespace characters. In this context newline is not considered
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * whitespace. Returns EOF on end-of-file, or the character
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * that caused the reading to stop.
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrencegetword(FILE *fp, char *buffer, size_t size) {
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence *p++ = (char)ch;
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson memset(addr->address, 0, LWRES_ADDR_MAXLEN);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencelwres_strdup(lwres_context_t *ctx, const char *str) {
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews for (i = 0 ; i < LWRES_CONFMAXNAMESERVERS ; i++)
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews for (i = 0 ; i < LWRES_CONFMAXSEARCH ; i++)
b6a0341bcb113e93bd0bc41a9f9a1fc117444da6Mark Andrews for (i = 0 ; i < LWRES_CONFMAXSORTLIST ; i++) {
aa05bbdef7f7827dde158dcc913f4dade84c8511Brian Wellington lwres_resetaddr(&confdata->sortlist[i].mask);
ffe74cc719aa0f10c38fbc1f2f3ea7db0960cb8fMark Andrews for (i = 0 ; i < LWRES_CONFMAXSORTLIST ; i++) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewslwres_conf_parsenameserver(lwres_context_t *ctx, FILE *fp) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (confdata->nsnext == LWRES_CONFMAXNAMESERVERS)
368b37b616234fce3d23099eb180f1dd38e1fb62Mark Andrews return (LWRES_R_FAILURE); /* Nothing on line. */
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews return (LWRES_R_FAILURE); /* Extra junk on line. */
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewslwres_conf_parsedomain(lwres_context_t *ctx, FILE *fp) {
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews return (LWRES_R_FAILURE); /* Nothing else on line. */
29c818c7d40fc8898b062903ec703851328a4deaMark Andrews return (LWRES_R_FAILURE); /* Extra junk on line. */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Search and domain are mutually exclusive.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews for (i = 0 ; i < LWRES_CONFMAXSEARCH ; i++) {
735ca24fa61ad9cd0285776c2fc4c14032f8f194Mark Andrews confdata->domainname = lwres_strdup(ctx, word);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewslwres_conf_parsesearch(lwres_context_t *ctx, FILE *fp) {
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * Search and domain are mutually exclusive.
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews * Remove any previous search definitions.
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews for (idx = 0 ; idx < LWRES_CONFMAXSEARCH ; idx++) {
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews return (LWRES_R_FAILURE); /* Nothing else on line. */
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews if (confdata->searchnxt == LWRES_CONFMAXSEARCH)
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews confdata->search[idx] = lwres_strdup(ctx, word);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrencelwres_create_addr(const char *buffer, lwres_addr_t *addr) {
f1cae4bcb7ee3060d893f5ab3ba55c1820bf3e4aBrian Wellington unsigned int len;
19d365e4448f1782611280b020987988b7ac3210Mark Andrews if (lwres_net_pton(AF_INET, buffer, &addrbuff) == 1) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews } else if (lwres_net_pton(AF_INET6, buffer, &addrbuff) == 1) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (LWRES_R_FAILURE); /* Unrecognised format. */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewslwres_conf_parsesortlist(lwres_context_t *ctx, FILE *fp) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (LWRES_R_FAILURE); /* Empty line after keyword. */
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews if (confdata->sortlistnxt == LWRES_CONFMAXSORTLIST)
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews return (LWRES_R_FAILURE); /* Too many values. */
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews *p++ = '\0';
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews res = lwres_create_addr(word, &confdata->sortlist[idx].addr);
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews * Make up a mask.
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews memset(&confdata->sortlist[idx].mask.address, 0xff,
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrewslwres_conf_parseoption(lwres_context_t *ctx, FILE *fp) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (LWRES_R_FAILURE); /* Empty line after keyword. */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews } else if (strcmp("no_tld_query", word) == 0) {
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington if (ndots < 0 || ndots > 0xff) /* Out of range. */
693ddf84daa745a0ea8ca311a8154dfa03eabc43Andreas Gustafssonlwres_conf_parse(lwres_context_t *ctx, const char *filename) {
7e4cda9965e2edf2ec43c57967eec8eff7061ab0Andreas Gustafsson else if (strcmp(word, "nameserver") == 0)
3a16668468060842e847ea6556e80e1405d35cd6Brian Wellington rval = lwres_conf_parsenameserver(ctx, fp);
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington /* unrecognised word. Ignore entire line */
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellingtonlwres_conf_print(lwres_context_t *ctx, FILE *fp) {
5eb91bd90e3ad3426e5e3213031556a737cf3809Mark Andrews char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington const char *p;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington REQUIRE(confdata->nsnext <= LWRES_CONFMAXNAMESERVERS);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington af = lwresaddr2af(confdata->nameservers[i].family);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington p = lwres_net_ntop(af, confdata->nameservers[i].address,
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington fprintf(fp, "domain %s\n", confdata->domainname);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington REQUIRE(confdata->searchnxt <= LWRES_CONFMAXSEARCH);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington for (i = 0 ; i < confdata->searchnxt ; i++)
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington REQUIRE(confdata->sortlistnxt <= LWRES_CONFMAXSORTLIST);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews for (i = 0 ; i < confdata->sortlistnxt ; i++) {
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews af = lwresaddr2af(confdata->sortlist[i].addr.family);
8dd5237a27e2e824d18f835dc711573aeb23a173Mark Andrews memset(&tmpaddr.address, 0xff, tmpaddr.length);
228c679d7a269423019f7c528db92e855f08240bMark Andrews fprintf(fp, "options ndots:%d\n", confdata->ndots);