# dhcpcd and dnscache [SOLVED]

## wumpyr

So there's a dhcpcd nerd knob (-R) to not clobber resolv.conf, but is there an an easy way to have dhcpcd update the external dnscache servers?  I want to move to djbdns/dnscache running a forwarding cache on my laptop.Last edited by wumpyr on Tue May 23, 2006 12:11 pm; edited 1 time in total

----------

## jmbsvicetto

Hi and welcome to the forums.

Have you taken a look at the -F and -h options?

----------

## wumpyr

 *jmbsvicetto wrote:*   

> Hi and welcome to the forums.
> 
> Have you taken a look at the -F and -h options?

 

Hi.  I'm not looking to update external dns entries.  What I need to do is update the what djb calls "external DNS cache" entries (what I'd simply call upstream dns) in /etc/dnscache/root/servers/@ with whatever dns addresses are returned in the lease, so there is a forwarding cache setup as per http://cr.yp.to/djbdns/run-cache-home.html

i.e how do you do step 3 when using dhcpcd at the above url?

----------

## UberLord

emerge net-misc/reslovconf-gentoo 

emerge --config resovconf-gentoo

cd /etc/resolvconf/update.d

wget http://dev.gentoo.org/~uberlord/resolvconf-gentoo/djbdns

Now, ensure that your dhcp client supports resolvconf, dhcpcd-2.0.5 and higher do.

Don't pass the -R option anymore and voila. dhcpcd will send it's resolv.conf to resolvconf which will update djbdns and not the actual /etc/resolv.conf file if /etc/resolv.conf points to nameserver 127.0.0.1

----------

## wumpyr

 *UberLord wrote:*   

> 
> 
> wget http://dev.gentoo.org/~uberlord/resolvconf-gentoo/djbdns
> 
> 

 

Thank you for your post, I'm trying it now however I get a 403 forbidden on this file, can you fix the permissions please?

----------

## UberLord

 *wumpyr wrote:*   

> Thank you for your post, I'm trying it now however I get a 403 forbidden on this file, can you fix the permissions please?

 

Fixed. Sorry about that.

----------

## wumpyr

Close but no cigar. Also created the symlink for /var/dnscache to /etc/resolvconf/dnscache as per the djbdns file above.  Now the /etc/resolv.conf symlink is deleted when getting a lease.  Looking at it a bit further, when I stop eth0, the resolv.conf symlink is removed and replaced with a resolv.conf file so this breaks when starting up the interface again.  If I stop eth0, run emerge --config resolvconf-gentoo and start the interface, the symlink is retained.

net-misc/dhcpcd-2.0.5

net-misc/resolvconf-gentoo-0.5

sys-apps/baselayout-1.11.14-r8

```
voyager ~ # ls -la /etc/resolv.conf

lrwxrwxrwx 1 root root 26 2006-05-23 13:52 /etc/resolv.conf -> resolvconf/run/resolv.conf

voyager ~ # cat /etc/resolv.conf

# Generated by dhcpcd for interface eth0

domain test.com

nameserver 127.0.0.1

voyager ~ # 

voyager ~ # /etc/init.d/net.eth0 restart

 * Stopping eth0

 *   Bringing down eth0

 *     Stopping dhcpcd on eth0 ...                                                                                   [ ok ]

 *     Shutting down eth0 ...                                                                                        [ ok ]

 * Starting eth0

 *   Bringing up eth0

 *     Configuration not set for eth0 - assuming dhcp

 *     dhcp

 *       Running dhcpcd ...                                                                                          [ ok ]

 *       eth0 received address 192.168.0.58

voyager ~ # ls -la /etc/resolv.conf

ls: /etc/resolv.conf: No such file or directory

voyager ~ #

voyager ~ # ps -ef|grep dhcpcd

root     16523     1  0 14:01 ?        00:00:00 /sbin/dhcpcd -h host -t 30 eth0

root     17702  2736  0 14:05 pts/3    00:00:00 grep dhcpcd

voyager ~ #
```

Clobbered symlink:

```
voyager ~ # ls -la /etc/resolv.conf

lrwxrwxrwx 1 root root 26 2006-05-23 14:07 /etc/resolv.conf -> resolvconf/run/resolv.conf

voyager ~ # /etc/init.d/net.eth0 stop

 * Stopping eth0

 *   Bringing down eth0

 *     Stopping dhcpcd on eth0 ...                                                                                   [ ok ]

 *     Shutting down eth0 ...                                                                                        [ ok ]

voyager ~ # ls -la /etc/resolv.conf

-rw-r--r-- 1 root root 110 2006-05-19 16:45 /etc/resolv.conf

voyager ~ #
```

----------

## UberLord

Hmmm, looks like you'll need baselayout-1.12

----------

## wumpyr

I upgraded to baselayout-1.12.0_pre19-r2.  This fixes the clobbering resolv.conf issue, however still no go.

If I have a resolv.conf with 127.0.0.1 and then get a lease /etc/resolv.conf is rewritten with the nameserver IPs from the lease as if there was no djbdns instead of leaving it as 127.0.0.1 and /var/dnscache/root/servers/@ is not being rewritten.

----------

## UberLord

I think you need todo this in conf.d/net

```
dns_servers_lo=( "127.0.0.1" )
```

Then restart net.lo and it should start working then

----------

## wumpyr

 *UberLord wrote:*   

> I think you need todo this in conf.d/net
> 
> ```
> dns_servers_lo=( "127.0.0.1" )
> ```
> ...

 

Yes, this change along with the changes above solved the issue.  Works great.  Thank you.

----------

## plq

i thought the most elegant solution would be to add a command line option like --export-dns to dhcpcd.

so i did, and here's the patch [against dhcpcd-3.0.9]:

```

diff dhcpcd-3.0.9/configure.c dhcpcd-godo/configure.c

156a157,178

> static int export_dns (const options_t *options, const dhcp_t *dhcp)

> {

>   FILE *f;

>   address_t *address;

>

>   logger (LOG_DEBUG, "writing "RESOLVFILE);

>   if (! (f = fopen(options->export_dns, "w")))

>       logger (LOG_ERR, "fopen `%s': %s", options->export_dns, strerror (errno));

>

>   if (f)

>     {

>       for (address = dhcp->dnsservers; address; address = address->next)

>         fprintf (f, "%s\n", inet_ntoa (address->address));

>

>       fclose (f);

>     }

>   else

>     return -1;

>

>   return 0;

> }

>

498a521,523

>   if (options->export_dns && strlen(options->export_dns) && dhcp->dnsservers)

>       export_dns(options,dhcp);

>

diff dhcpcd-3.0.9/dhcpcd.8 dhcpcd-godo/dhcpcd.8

20a21

> \%[\-E\ filename]

185a187,189

> .BI \-E \ filename

> Exports received dns servers to the specified file name.

> .TP

diff dhcpcd-3.0.9/dhcpcd.c dhcpcd-godo/dhcpcd.c

100c100,101

<         "              [-u userclass] [-F [none | ptr | both]] [-I clientID]\n");

---

>         "              [-u userclass] [-F [none | ptr | both]] [-I clientID]\n"

>         "              [-E filename]\n");

137a139

>       {"export-dns", required_argument, NULL, 'e'},

167c169

<   while ((ch = getopt_long(argc, argv, "ac:dh:i:kl:m:nps:t:u:F:GHI:NRY", longopts,

---

>   while ((ch = getopt_long(argc, argv, "ac:dh:i:kl:m:nps:t:u:E:F:GHI:NRY", longopts,

260a263,272

>       case 'E':

>       if (strlen (optarg) > EXPORT_DNS_MAX_LEN)

>         {

>           logger (LOG_ERR, "`%s' is too long for the name of the file to hold the dns information, max is %d",

>                   optarg, EXPORT_DNS_MAX_LEN);

>           exit (EXIT_FAILURE);

>         }

>       else

>         sprintf(options.export_dns, "%s", optarg);

>       break;

diff dhcpcd-3.0.9/dhcpcd.h dhcpcd-godo/dhcpcd.h

39c39

<

---

> #define EXPORT_DNS_MAX_LEN      1024

46a47

>   char export_dns[EXPORT_DNS_MAX_LEN];

```

and here's a /etc/conf.d/net:

```

config_eth0=("dhcp")

dhcp_eth0="nodns"

dhcpcd_eth0="-E /service/dnscache/root/servers/@"

```

Last edited by plq on Fri Jan 26, 2007 11:13 am; edited 1 time in total

----------

## UberLord

Not bad, but I'll reject that patch.

A better solution is to install resolvconf-gentoo and then use the updater file for djbdns found here

https://bugs.gentoo.org/show_bug.cgi?id=134133

That enables every IPv4 dhcp client in portage to update dnscache  :Smile: 

----------

## plq

i know, i know, i would not even attempt to patch it if the code wasn't that clean, but i just couldn't resist it  :Twisted Evil:  nice job!

just let me know when you discover a serious security hole or something  :Wink: 

----------

## wumpyr

Hi Uberlord

I'm using http://dev.gentoo.org/~uberlord/resolvconf-gentoo/djbdns and I have a couple of issues.

1) when I attach to a different network and get a new search domain from dhcp it creates a new file such as /var/dnscache/root/servers/xyz.com which is fine but the permissions are wrong and dnscache can't read the file whenever it is loaded.  I fixed this by adding 

```
umask 022
```

 to the top of the above script.

2) When it does create this new file, split dns for that domain doesn't kick in straight away, i have to svc -h /service/dnscache to get it to reread the new config - this is not automated anywhere?

Thanks

----------

## UberLord

Hi.

I don't use this, or write the script.

However, I have modified it based on your post, so see if the new one works for you. The last line in the script is the one that triggers the update - not sure if it works or not. Let me know  :Smile: 

----------

## wumpyr

Hi Uberlord - yes, seems to work ok now. Thanks.

----------

## wumpyr

Ok, so I have another problem.

Split dns files are hanging around in /var/dnscache/root/servers/

So say xyz.com is created and I am on that local network, fine.  Then I move to a completely different network and should be using the default dns servers (@) for that domain.  Doesn't work because the split dns file xyz.com was dangling around.  So I have to delete it and svc -h /service/dnscache.

----------

## UberLord

Someone else will have to fix that as I said I don't use the script or djbdns.

Maybe you can fix it yourself? Learn shell scripting and become a dev  :Smile: 

----------

## wumpyr

ok sure, this works so far for me.

```
# diff -C5 djbdns.orig djbdns

*** djbdns.orig 2007-04-08 03:47:29.000000000 +1000

--- djbdns      2007-04-08 03:59:04.000000000 +1000

***************

*** 7,17 ****

  # the place where dnscache is installed is a moving target,

  # so the user is expected to do a symlink between his dnscache

  # install dir and /etc/resolvconf/dnscache

! # Otherwise djbns won't read the files...

  umask 022

  DJBDNSCONF="/etc/resolvconf/dnscache/root/servers"

  # sanity checks

--- 7,17 ----

  # the place where dnscache is installed is a moving target,

  # so the user is expected to do a symlink between his dnscache

  # install dir and /etc/resolvconf/dnscache

! # Otherwise djbdns won't read the files...

  umask 022

  DJBDNSCONF="/etc/resolvconf/dnscache/root/servers"

  # sanity checks

***************

*** 23,32 ****

--- 23,41 ----

  if ! grep 1 ${DJBDNSCONF}/../../env/FORWARDONLY &>/dev/null; then

        echo -e "env/FORWARDONLY should be 1\nin order for the djbdns-resolv.conf updater to make sense\nexiting"

        exit 1

  fi

+ # Clean up dangling split dns domains unknown to resolvconf

+ CURSEARCH=`ls ${DJBDNSCONF} | grep -v '@'`

+

+ for N in ${CURSEARCH}; do

+       if ! resolvconf -l | grep -q "${N}" ; then

+               rm -f "${DJBDNSCONF}/${N}"

+       fi

+ done

+

  # load our variables from resolvconf

  VARS="$(resolvconf -v)"

  eval "${VARS}"

  # set up the forward zones

```

----------

## UberLord

Added to the script - thanks

----------

