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