1 1.1 roy # Generate /etc/resolv.conf 2 1.1 roy # Support resolvconf(8) if available 3 1.1 roy # We can merge other dhcpcd resolv.conf files into one like resolvconf, 4 1.1 roy # but resolvconf is preferred as other applications like VPN clients 5 1.1 roy # can readily hook into it. 6 1.1 roy # Also, resolvconf can configure local nameservers such as bind 7 1.1 roy # or dnsmasq. This is important as the libc resolver isn't that powerful. 8 1.1 roy 9 1.1 roy resolv_conf_dir="$state_dir/resolv.conf" 10 1.1 roy NL=" 11 1.1 roy " 12 1.1 roy : ${resolvconf:=resolvconf} 13 1.1 roy 14 1.1 roy build_resolv_conf() 15 1.1 roy { 16 1.2 kre cf="$state_dir/resolv.conf.$ifname" 17 1.1 roy 18 1.1 roy # Build a list of interfaces 19 1.1 roy interfaces=$(list_interfaces "$resolv_conf_dir") 20 1.1 roy 21 1.1 roy # Build the resolv.conf 22 1.3 roy header= 23 1.1 roy if [ -n "$interfaces" ]; then 24 1.1 roy # Build the header 25 1.1 roy for x in ${interfaces}; do 26 1.1 roy header="$header${header:+, }$x" 27 1.1 roy done 28 1.1 roy 29 1.1 roy # Build the search list 30 1.1 roy domain=$(cd "$resolv_conf_dir"; \ 31 1.1 roy key_get_value "domain " ${interfaces}) 32 1.1 roy search=$(cd "$resolv_conf_dir"; \ 33 1.1 roy key_get_value "search " ${interfaces}) 34 1.1 roy set -- ${domain} 35 1.1 roy domain="$1" 36 1.1 roy [ -n "$2" ] && search="$search $*" 37 1.1 roy [ -n "$search" ] && search="$(uniqify $search)" 38 1.1 roy [ "$domain" = "$search" ] && search= 39 1.1 roy [ -n "$domain" ] && domain="domain $domain$NL" 40 1.1 roy [ -n "$search" ] && search="search $search$NL" 41 1.1 roy 42 1.1 roy # Build the nameserver list 43 1.1 roy srvs=$(cd "$resolv_conf_dir"; \ 44 1.1 roy key_get_value "nameserver " ${interfaces}) 45 1.4 roy for x in $(uniqify $srvs); do 46 1.1 roy servers="${servers}nameserver $x$NL" 47 1.1 roy done 48 1.1 roy fi 49 1.1 roy header="$signature_base${header:+ $from }$header" 50 1.1 roy 51 1.1 roy # Assemble resolv.conf using our head and tail files 52 1.1 roy [ -f "$cf" ] && rm -f "$cf" 53 1.1 roy [ -d "$resolv_conf_dir" ] || mkdir -p "$resolv_conf_dir" 54 1.1 roy echo "$header" > "$cf" 55 1.1 roy if [ -f /etc/resolv.conf.head ]; then 56 1.1 roy cat /etc/resolv.conf.head >> "$cf" 57 1.1 roy else 58 1.1 roy echo "# /etc/resolv.conf.head can replace this line" >> "$cf" 59 1.1 roy fi 60 1.1 roy printf %s "$domain$search$servers" >> "$cf" 61 1.1 roy if [ -f /etc/resolv.conf.tail ]; then 62 1.1 roy cat /etc/resolv.conf.tail >> "$cf" 63 1.1 roy else 64 1.1 roy echo "# /etc/resolv.conf.tail can replace this line" >> "$cf" 65 1.1 roy fi 66 1.1 roy if change_file /etc/resolv.conf "$cf"; then 67 1.1 roy chmod 644 /etc/resolv.conf 68 1.1 roy fi 69 1.1 roy rm -f "$cf" 70 1.1 roy } 71 1.1 roy 72 1.1 roy # Extract any ND DNS options from the RA 73 1.3 roy # Obey the lifetimes 74 1.1 roy eval_nd_dns() 75 1.1 roy { 76 1.3 roy 77 1.3 roy eval rdnsstime=\$nd${i}_rdnss${j}_lifetime 78 1.3 roy [ -z "$rdnsstime" ] && return 1 79 1.3 roy ltime=$(($rdnsstime - $offset)) 80 1.3 roy if [ "$ltime" -gt 0 ]; then 81 1.1 roy eval rdnss=\$nd${i}_rdnss${j}_servers 82 1.3 roy [ -n "$rdnss" ] && new_rdnss="$new_rdnss${new_rdnss:+ }$rdnss" 83 1.1 roy fi 84 1.3 roy 85 1.3 roy eval dnssltime=\$nd${i}_dnssl${j}_lifetime 86 1.3 roy [ -z "$dnssltime" ] && return 1 87 1.3 roy ltime=$(($dnssltime - $offset)) 88 1.3 roy if [ "$ltime" -gt 0 ]; then 89 1.1 roy eval dnssl=\$nd${i}_dnssl${j}_search 90 1.3 roy [ -n "$dnssl" ] && new_dnssl="$new_dnssl${new_dnssl:+ }$dnssl" 91 1.1 roy fi 92 1.1 roy 93 1.1 roy j=$(($j + 1)) 94 1.1 roy return 0 95 1.1 roy } 96 1.1 roy 97 1.1 roy add_resolv_conf() 98 1.1 roy { 99 1.2 kre conf="$signature$NL" 100 1.2 kre warn=true 101 1.1 roy 102 1.1 roy # Loop to extract the ND DNS options using our indexed shell values 103 1.1 roy i=1 104 1.1 roy j=1 105 1.1 roy while true; do 106 1.3 roy eval acquired=\$nd${i}_acquired 107 1.3 roy [ -z "$acquired" ] && break 108 1.3 roy eval now=\$nd${i}_now 109 1.3 roy [ -z "$now" ] && break 110 1.3 roy offset=$(($now - $acquired)) 111 1.1 roy while true; do 112 1.1 roy eval_nd_dns || break 113 1.1 roy done 114 1.1 roy i=$(($i + 1)) 115 1.1 roy j=1 116 1.1 roy done 117 1.1 roy [ -n "$new_rdnss" ] && \ 118 1.1 roy new_domain_name_servers="$new_domain_name_servers${new_domain_name_servers:+ }$new_rdnss" 119 1.1 roy [ -n "$new_dnssl" ] && \ 120 1.1 roy new_domain_search="$new_domain_search${new_domain_search:+ }$new_dnssl" 121 1.1 roy 122 1.1 roy # Derive a new domain from our various hostname options 123 1.1 roy if [ -z "$new_domain_name" ]; then 124 1.1 roy if [ "$new_dhcp6_fqdn" != "${new_dhcp6_fqdn#*.}" ]; then 125 1.1 roy new_domain_name="${new_dhcp6_fqdn#*.}" 126 1.1 roy elif [ "$new_fqdn" != "${new_fqdn#*.}" ]; then 127 1.1 roy new_domain_name="${new_fqdn#*.}" 128 1.1 roy elif [ "$new_host_name" != "${new_host_name#*.}" ]; then 129 1.1 roy new_domain_name="${new_host_name#*.}" 130 1.1 roy fi 131 1.1 roy fi 132 1.1 roy 133 1.1 roy # If we don't have any configuration, remove it 134 1.2 kre if [ -z "$new_domain_name_servers" ] && 135 1.2 kre [ -z "$new_domain_name" ] && 136 1.2 kre [ -z "$new_domain_search" ]; then 137 1.1 roy remove_resolv_conf 138 1.1 roy return $? 139 1.1 roy fi 140 1.1 roy 141 1.1 roy if [ -n "$new_domain_name" ]; then 142 1.1 roy set -- $new_domain_name 143 1.1 roy if valid_domainname "$1"; then 144 1.1 roy conf="${conf}domain $1$NL" 145 1.1 roy else 146 1.1 roy syslog err "Invalid domain name: $1" 147 1.1 roy fi 148 1.1 roy # If there is no search this, make this one 149 1.1 roy if [ -z "$new_domain_search" ]; then 150 1.1 roy new_domain_search="$new_domain_name" 151 1.1 roy [ "$new_domain_name" = "$1" ] && warn=true 152 1.1 roy fi 153 1.1 roy fi 154 1.1 roy if [ -n "$new_domain_search" ]; then 155 1.4 roy new_domain_search=$(uniqify $new_domain_search) 156 1.1 roy if valid_domainname_list $new_domain_search; then 157 1.1 roy conf="${conf}search $new_domain_search$NL" 158 1.1 roy elif ! $warn; then 159 1.1 roy syslog err "Invalid domain name in list:" \ 160 1.1 roy "$new_domain_search" 161 1.1 roy fi 162 1.1 roy fi 163 1.4 roy new_domain_name_servers=$(uniqify $new_domain_name_servers) 164 1.1 roy for x in ${new_domain_name_servers}; do 165 1.1 roy conf="${conf}nameserver $x$NL" 166 1.1 roy done 167 1.1 roy if type "$resolvconf" >/dev/null 2>&1; then 168 1.1 roy [ -n "$ifmetric" ] && export IF_METRIC="$ifmetric" 169 1.1 roy printf %s "$conf" | "$resolvconf" -a "$ifname" 170 1.1 roy return $? 171 1.1 roy fi 172 1.1 roy 173 1.1 roy if [ -e "$resolv_conf_dir/$ifname" ]; then 174 1.1 roy rm -f "$resolv_conf_dir/$ifname" 175 1.1 roy fi 176 1.1 roy [ -d "$resolv_conf_dir" ] || mkdir -p "$resolv_conf_dir" 177 1.1 roy printf %s "$conf" > "$resolv_conf_dir/$ifname" 178 1.1 roy build_resolv_conf 179 1.1 roy } 180 1.1 roy 181 1.1 roy remove_resolv_conf() 182 1.1 roy { 183 1.1 roy if type "$resolvconf" >/dev/null 2>&1; then 184 1.1 roy "$resolvconf" -d "$ifname" -f 185 1.1 roy else 186 1.1 roy if [ -e "$resolv_conf_dir/$ifname" ]; then 187 1.1 roy rm -f "$resolv_conf_dir/$ifname" 188 1.1 roy fi 189 1.1 roy build_resolv_conf 190 1.1 roy fi 191 1.1 roy } 192 1.1 roy 193 1.1 roy # For ease of use, map DHCP6 names onto our DHCP4 names 194 1.1 roy case "$reason" in 195 1.1 roy BOUND6|RENEW6|REBIND6|REBOOT6|INFORM6) 196 1.1 roy new_domain_name_servers="$new_dhcp6_name_servers" 197 1.1 roy new_domain_search="$new_dhcp6_domain_search" 198 1.1 roy ;; 199 1.1 roy esac 200 1.1 roy 201 1.5 roy if $if_configured; then 202 1.5 roy if $if_up || [ "$reason" = ROUTERADVERT ]; then 203 1.5 roy add_resolv_conf 204 1.5 roy elif $if_down; then 205 1.5 roy remove_resolv_conf 206 1.5 roy fi 207 1.1 roy fi 208