début de réorganisation des fonctions

This commit is contained in:
Jephté Clain 2017-02-13 08:15:35 +04:00
parent b266663d8a
commit fa3d3b48b1
3 changed files with 471 additions and 451 deletions

View File

@ -79,319 +79,6 @@ function service_enable() {
__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS=
__DEBIAN_NETWORK_INTERFACES=/etc/network/interfaces
function __network_parse_confbr() {
local br; local -a ifaces
splitpair "$1" br ifaces
array_split ifaces "$ifaces" ,
__npc_destbr="$br"
array_copy __npc_destifaces ifaces
}
function network_parse_confbr() {
# network_parse_confbr "$confbr" br ifaces
local __npc_destbr; local -a __npc_destifaces
__network_parse_confbr "$1"
set_var "${2:-br}" "$__npc_destbr"
array_copy "${3:-ifaces}" __npc_destifaces
}
function network_format_confbr() {
# network_format_confbr "$br" ifaces --> "br:ifaces"
echo "$1:$(array_join "${2:-ifaces}" ,)"
}
function __network_parse_confip() {
local tmpig iface gateway; local -a ipsuffixes
splitfsep2 "$1" : tmpig ipsuffixes
splitfsep "$tmpig" // iface gateway
array_split ipsuffixes "$ipsuffixes" ,
__npc_destiface="$iface"
__npc_destgateway="$gateway"
array_copy __npc_destipsuffixes ipsuffixes
}
function network_parse_confip() {
# network_parse_confip "$confip" iface gateway ipsuffixes
local __npc_destiface __npc_destgateway; local -a __npc_destipsuffixes
__network_parse_confip "$1"
set_var "${2:-iface}" "$__npc_destiface"
set_var "${3:-gateway}" "$__npc_destgateway"
array_copy "${4:-ipsuffixes}" __npc_destipsuffixes
}
function network_parse_ipsuffix() {
# network_parse_ipsuffix "$ipsuffix" ip suffix
splitfsep "$1" / "${2:-ip}" "${3:-suffix}"
}
function network_format_confip() {
# network_format_confip "$iface" "$gateway" ipsuffixes --> "iface//gateway:ipsuffixes"
local tmpig="$1${2:+//$2}"
echo "${tmpig:+$tmpig:}$(array_join "${3:-ipsuffixes}" ,)"
}
function network_format_ipsuffix() {
# network_format_ipsuffix "$ip" "$suffix" --> "ip/suffix"
local ip="$(strlower "$1")"
if [ "$ip" == "dhcp" ]; then
echo "$ip"
else
echo "$1${2:+/$2}"
fi
}
function __network_fix_confbrs() {
local -a confbrs ifaces brs ips
local confbr br iface
# recenser les bridges et créer les tableaux __BR_ifaces
for confbr in "${__nfc_confbrs[@]}"; do
network_parse_confbr "$confbr" br ifaces
array_addu brs "$br"
eval "local -a ${br}_ifaces"
done
# puis constuire la liste des interfaces associées à chaque bridge
for confbr in "${__nfc_confbrs[@]}"; do
network_parse_confbr "$confbr" br ifaces
array_extendu "${br}_ifaces" ifaces
done
# puis construire le tableau final
array_new confbrs
for br in "${brs[@]}"; do
array_add confbrs "$(network_format_confbr "$br" "${br}_ifaces")"
done
array_copy __nfc_destconfbrs confbrs
}
function network_fix_confbrs() {
# normaliser le tableau $1(=confbrs): fusionner les doublons
local -a __nfc_confbrs __nfc_destconfbrs
array_copy __nfc_confbrs "${1:-confbrs}"
__network_fix_confbrs
array_copy "${1:-confbrs}" __nfc_destconfbrs
}
function __network_fix_confips() {
local -a confips ipsuffixes ifaces ips
local confip iface gateway network ip suffix mainip
local mainiface="$1"
# recenser les interfaces et créer les tableaux __IFACE_ipspecs
for confip in "${__nfc_confips[@]}"; do
network_parse_confip "$confip" iface gateway ipsuffixes
if [ -n "$iface" ]; then
network="${iface}_network"
if ! array_contains ifaces "$iface"; then
array_add ifaces "$iface"
eval "local ${iface}_gateway $network; local -a ${iface}_ipsuffixes"
fi
if [ -z "${!network}" -a -n "${ipsuffixes[0]}" ]; then
setv "$network" "$(ipcalc_network "${ipsuffixes[0]}")"
fi
fi
done
# puis construire la liste des adresses IP associées à chaque interface
for confip in "${__nfc_confips[@]}"; do
network_parse_confip "$confip" iface gateway ipsuffixes
# si aucune interface n'est spécifiée, sélectionner celle correspondant
# à la même adresse de réseau. sinon prendre $mainiface
if [ -z "$iface" ]; then
network="$(ipcalc_network "${ipsuffixes[0]}")"
local ip_iface ip_network
for ip_iface in "${ifaces[@]}"; do
ip_network="${ip_iface}_network"
if [ "${!ip_network}" == "$network" ]; then
iface="$ip_iface"
break
fi
done
fi
[ -n "$iface" ] || iface="$mainiface"
# si la passerelle a déjà été spécifiée, la récupérer
local tmpgw="${iface}_gateway"
[ -n "${!tmpgw}" ] && gateway="${!tmpgw}"
# calculer l'adresse ip principale, pour pouvoir traiter le cas où
# l'adresse ip principale est l'adresse de la passerelle.
mainip=
for ipsuffix in "${ipsuffixes[@]}"; do
network_parse_ipsuffix "$ipsuffix" ip suffix
if ! array_contains ips "$ip"; then
[ -n "$suffix" ] || suffix=24
if [ -z "$mainip" ]; then
[ -n "$gateway" ] || gateway="$(ipcalc_gateway "$ip" "$suffix")"
mainip="$ip"
fi
array_add "${iface}_ipsuffixes" "$(network_format_ipsuffix "$ip" "$suffix")"
array_add ips "$ip"
fi
done
[ "$gateway" == "$mainip" ] && gateway=
# si l'adresse ip principale est obtenue par dhcp, il ne faut pas
# spécifier la passerelle: elle sera fournie par le serveur DHCP.
# Utiliser le marqueur "none" pour que la valeur ne soit pas modifiée.
[ "${ipsuffixes[0]}" == "dhcp" ] && gateway=none
set_var "${iface}_gateway" "$gateway"
done
# puis construire le tableau final
array_new confips
for iface in "${ifaces[@]}"; do
gateway="${iface}_gateway"; gateway="${!gateway}"
[ "$gateway" == "none" ] && gateway=
array_add confips "$(network_format_confip "$iface" "$gateway" "${iface}_ipsuffixes")"
done
array_copy __nfc_destconfips confips
}
function network_fix_confips() {
# normaliser le tableau $1(=confips): fusionner les doublons, spécifier le
# suffixe /24 par défaut, etc. $2 est le cas échéant l'interface associée
# aux adresses ip non qualifiées
local -a __nfc_confips __nfc_destconfips
array_copy __nfc_confips "${1:-confips}"
__network_fix_confips "$2"
array_copy "${1:-confips}" __nfc_destconfips
}
function __network_fix_mainiface() {
local -a confips ifaces ipsuffixes
local br iface gateway confip mainconfip
local mainiface="$1"
# déterminer mainiface
if [ -z "$mainiface" -a -n "${__nfm_confbrs[0]}" ]; then
network_parse_confbr "${__nfm_confbrs[0]}" br ifaces
mainiface="$br"
fi
if [ -z "$mainiface" -a -n "${__nfm_confips[0]}" ]; then
network_parse_confip "${__nfm_confips[0]}" iface gateway ipsuffixes
mainiface="$iface"
fi
[ -n "$mainiface" ] || mainiface=eth0
# ensuite, il faut reécrire confips avec la valeur de mainiface
array_new confips
for confip in "${__nfm_confips[@]}"; do
network_parse_confip "$confip" iface gateway ipsuffixes
[ -n "$iface" ] || iface="$mainiface"
confip="$(network_format_confip "$iface" "$gateway" ipsuffixes)"
if [ "$iface" == "$mainiface" ]; then
mainconfip="$confip"
else
array_add confips "$confip"
fi
done
[ -n "$mainconfip" ] && array_ins confips "$mainconfip"
array_copy __nfm_destconfips confips
}
function network_fix_mainiface() {
# A partir des valeurs des tableaux $1(=confbrs) et $2(=confips), et de
# l'interface principale $3, déterminer l'interface principale. Si $3 est
# spécifié, c'est la valeur sélectionnée. Sinon, si un bridge existe, c'est
# le premier bridge qui est sélectionné. Sinon, la première interface est
# sélectionnée. Sinon, on prend eth0.
# Ensuite, réorganiser les tableaux de façon que confips[0] devienne la
# configuration ip de l'interface principale.
local -a __nfm_confbrs __nfm_confips __nfm_destconfips
array_copy __nfm_confbrs "${1:-confbrs}"
array_copy __nfm_confips "${2:-confips}"
__network_fix_mainiface "$3"
array_copy "${2:-confips}" __nfm_destconfips
}
function network_fix_confs() {
network_fix_confbrs "${1:-confbrs}"
network_fix_confips "${2:-confips}"
network_fix_mainiface "${1:-confbrs}" "${2:-confips}" "$3"
}
function __network_valid_ifaces() {
ip link | awk '/<.*>/ {
flags = $0; sub(/^.*</, ",", flags); sub(/>.*$/, ",", flags)
if (flags ~ /,LOOPBACK,/) next
if (flags !~ /,UP,/) next
sub(/:$/, "", $2); print $2
}'
}
function __network_set_confbrs() {
local -a ifaces tmpconfbrs confbrs
local confbr br iface
array_from_lines ifaces "$(__network_valid_ifaces)"
array_from_lines tmpconfbrs "$(brctl show 2>/dev/null | awk '
BEGIN { confbrs = "" }
NR == 1 { next }
{
if ($1 != "" && $2 != "") {
if (confbr != "") print confbr
confbr = $1 ":" $4
} else {
confbr = confbr "," $1
}
}
END {
if (confbr != "") print confbr
}
')"
for confbr in "${tmpconfbrs[@]}"; do
splitpair "$confbr" br iface
array_contains ifaces "$br" || continue
array_add confbrs "$confbr"
done
array_copy __nsc_destconfbrs confbrs
}
function network_set_confbrs() {
# initialiser $1(=confbrs) avec l'état des bridges sur le système courant
local -a __nsc_destconfbrs
__network_set_confbrs
array_copy "${1:-confbrs}" __nsc_destconfbrs
}
function __network_set_gateway() {
# initialiser la variable gateway avec la passerelle associée à l'interface
# $1
gateway="$(route -n | awk -v iface="$1" '$1 == "0.0.0.0" && $8 == iface { print $2 }')"
}
function __network_set_confip() {
# initialiser la variable confip avec l'état de l'interface $1, en assumant
# que la passerelle vaut $2.
# retourner 1 si l'interface n'a pas d'adresse ip associée
local -a ipsuffixes
array_from_lines ipsuffixes "$(ip addr show dev "$1" | awk '$1 == "inet" { print $2 }')"
confip="$(network_format_confip "$1" "$2" ipsuffixes)"
[ "${#ipsuffixes[*]}" -gt 0 ]
}
function __network_set_confips() {
local -a confips ifaces
local iface gateway
array_from_lines ifaces "$(__network_valid_ifaces)"
for iface in "${ifaces[@]}"; do
__network_set_gateway "$iface"
[ -n "$gateway" ] || gateway=-
__network_set_confip "$iface" "$gateway" || continue
array_add confips "$confip"
done
array_copy __nsc_destconfips confips
}
function network_set_confips() {
# initialiser le tableau $1(=confips) avec l'état des interfaces sur le
# système courant
local -a __nsc_destconfips
__network_set_confips
array_copy "${1:-confips}" __nsc_destconfips
}
function network_interfaces_check_confbr() {
# Vérifier que la configuration du bridge $1, dont les membres sont les
# interfaces du tableau $2(=ifaces) est faite dans le fichier
@ -783,17 +470,11 @@ iface $iface inet manual"
bridge_maxwait 0"
}
function __network_backup() {
local file="$1"
local backup="$file.orig$(date +%Y%m%d)"
[ -f "$backup" ] || cat "$file" >"$backup" 2>/dev/null
}
function network_fix_hostname() {
local hostname="${1%%.*}"
local modified
[ -f /etc/hostname ] || touch /etc/hostname
if [ -n "$__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS" ]; then
if [ -n "$__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS" -o -n "$__NETWORK_DEVEL_SHOW_MODIFS" ]; then
uecho "Setting /etc/hostname to $hostname"
elif [ "$(</etc/hostname)" != "$hostname" ]; then
__network_backup /etc/hostname
@ -812,7 +493,7 @@ function network_fix_mailname() {
local host="$1"
local modified
[ -f /etc/mailname ] || touch /etc/mailname
if [ -n "$__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS" ]; then
if [ -n "$__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS" -o -n "$__NETWORK_DEVEL_SHOW_MODIFS" ]; then
uecho "Setting /etc/mailname to $host"
elif [ "$(</etc/mailname)" != "$host" ]; then
__network_backup /etc/mailname
@ -827,130 +508,6 @@ function network_fix_mailname() {
[ -n "$modified" ]
}
function network_fix_exim4() {
local host="$1" oldhost="$2"
local tmpfile; ac_set_tmpfile tmpfile
local modified
sed </etc/exim4/update-exim4.conf.conf >"$tmpfile" "
/^dc_other_hostnames=/c\\
dc_other_hostnames='$host'"
if [ -n "$__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS" ]; then
if testdiff "$tmpfile" /etc/exim4/update-exim4.conf.conf; then
uecho "Setting /etc/exim4/update-exim4.conf.conf to:"
cat "$tmpfile" | sed 's/^/ /g' 1>&2
else
uecho "/etc/exim4/update-exim4.conf.conf: pas de modifications"
fi
elif testdiff "$tmpfile" /etc/exim4/update-exim4.conf.conf; then
__network_backup /etc/exim4/update-exim4.conf.conf
if show_debug; then
edebug "Setting /etc/exim4/update-exim4.conf.conf to:"
cat "$tmpfile" | sed 's/^/ /g' 1>&2
else
estep /etc/exim4/update-exim4.conf.conf
fi
cat "$tmpfile" >/etc/exim4/update-exim4.conf.conf
update-exim4.conf
modified=1
fi
ac_clean "$tmpfile"
[ -n "$modified" ]
}
function network_fix_postfix() {
local host="$1" oldhost="$2"
local tmpfile; ac_set_tmpfile tmpfile
local modified
awkrun </etc/postfix/main.cf >"$tmpfile" host="$host" oldhost="$oldhost" '
/^myhostname *=/ { $0 = "myhostname = " host }
/^mydestination *=/ {
gsub(" *" oldhost " *,? *", "")
if ($0 !~ (" *" host " *,? *")) {
sub(/^mydestination *= */, "mydestination = " host ", ")
}
}
{ print }'
if [ -n "$__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS" ]; then
if testdiff "$tmpfile" /etc/postfix/main.cf; then
uecho "Setting /etc/postfix/main.cf to:"
cat "$tmpfile" | sed 's/^/ /g' 1>&2
else
uecho "/etc/postfix/main.cf: pas de modifications"
fi
elif testdiff "$tmpfile" /etc/postfix/main.cf; then
__network_backup /etc/postfix/main.cf
if show_debug; then
edebug "Setting /etc/postfix/main.cf to:"
cat "$tmpfile" | sed 's/^/ /g' 1>&2
else
estep /etc/postfix/main.cf
fi
cat "$tmpfile" >/etc/postfix/main.cf
modified=1
fi
ac_clean "$tmpfile"
[ -n "$modified" ]
}
function network_fix_hosts() {
local host="$1" hostname="${1%%.*}"; local shost="${host//./\\.}"
local ip="$2"; local sip="${ip//./\\.}"
local oldhost="$3" oldhostname="${3%%.*}"; soldhost="${host//./\\.}"
local tmpfile; ac_set_tmpfile tmpfile
local modified
awkrun </etc/hosts >"$tmpfile" \
host="$host" hostname="$hostname" \
ip="$ip" \
oldhost="$oldhost" oldhostname="$oldhostname" \
'
/^[^# \t]/ {
gsub("[ \\t]+" host "[ \\t]*", " ")
gsub("[ \\t]+" hostname "[ \\t]*", " ")
gsub("[ \\t]+" oldhost "[ \\t]*", " ")
gsub("[ \\t]+" oldhostname "[ \\t]*", " ")
if ($0 ~ /^[^ \t]+[ \t]*$/) next
gsub(/[ \t]*$/, "")
}
{ print }'
if quietgrep "^$sip[ $TAB]\\+$shost[ $TAB]\\+$hostname" "$tmpfile"; then
sed -i "\
s/\$/ /
/^[ $TAB]$oldhost[ $TAB]/d
/^[ $TAB]$oldhostname[ $TAB]/d
s/ \$//
" "$tmpfile"
fi
if ! quietgrep "^$sip[ $TAB]\\+$shost[ $TAB]\\+$hostname" "$tmpfile"; then
sed -i "/^$sip[ $TAB]/d" "$tmpfile"
sed -i "1i\\
$ip$TAB$host $hostname" "$tmpfile"
fi
if [ -n "$__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS" ]; then
if testdiff "$tmpfile" /etc/hosts; then
uecho "Setting /etc/hosts to:"
cat "$tmpfile" | sed 's/^/ /g' 1>&2
else
uecho "/etc/hosts: pas de modifications"
fi
elif testdiff "$tmpfile" /etc/hosts; then
__network_backup /etc/hosts
if show_debug; then
edebug "Setting /etc/hosts to:"
cat "$tmpfile" | sed 's/^/ /g' 1>&2
else
estep /etc/hosts
fi
cat "$tmpfile" >/etc/hosts
modified=1
fi
ac_clean "$tmpfile"
[ -n "$modified" ]
}
function network_config() {
# (Re)configurer le réseau sur l'hôte courant. Des efforts sont faits pour
# ne mettre à jour les fichiers que si c'est nécessaire. Si un des arguments
@ -1096,7 +653,7 @@ iface lo inet loopback
done
# Fin de traitement
if [ -n "$__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS" ]; then
if [ -n "$__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS" -o -n "$__NETWORK_DEVEL_SHOW_MODIFS" ]; then
if testdiff "$interfaces" "$nifile"; then
uecho "Setting $nifile to:"
cat "$interfaces" | sed 's/^/ /g' 1>&2

View File

@ -222,3 +222,451 @@ function ipcalc_fqdn_maybe() {
ipcalc_fqdn "$1"
fi
}
################################################################################
# Fonctions génériques pour la gestion du réseau
__NETWORK_DEVEL_SHOW_MODIFS=
function __network_parse_confbr() {
local br; local -a ifaces
splitpair "$1" br ifaces
array_split ifaces "$ifaces" ,
__npc_destbr="$br"
array_copy __npc_destifaces ifaces
}
function network_parse_confbr() {
# network_parse_confbr "$confbr" br ifaces
local __npc_destbr; local -a __npc_destifaces
__network_parse_confbr "$1"
set_var "${2:-br}" "$__npc_destbr"
array_copy "${3:-ifaces}" __npc_destifaces
}
function network_format_confbr() {
# network_format_confbr "$br" ifaces --> "br:ifaces"
echo "$1:$(array_join "${2:-ifaces}" ,)"
}
function __network_parse_confip() {
local tmpig iface gateway; local -a ipsuffixes
splitfsep2 "$1" : tmpig ipsuffixes
splitfsep "$tmpig" // iface gateway
array_split ipsuffixes "$ipsuffixes" ,
__npc_destiface="$iface"
__npc_destgateway="$gateway"
array_copy __npc_destipsuffixes ipsuffixes
}
function network_parse_confip() {
# network_parse_confip "$confip" iface gateway ipsuffixes
local __npc_destiface __npc_destgateway; local -a __npc_destipsuffixes
__network_parse_confip "$1"
set_var "${2:-iface}" "$__npc_destiface"
set_var "${3:-gateway}" "$__npc_destgateway"
array_copy "${4:-ipsuffixes}" __npc_destipsuffixes
}
function network_parse_ipsuffix() {
# network_parse_ipsuffix "$ipsuffix" ip suffix
splitfsep "$1" / "${2:-ip}" "${3:-suffix}"
}
function network_format_confip() {
# network_format_confip "$iface" "$gateway" ipsuffixes --> "iface//gateway:ipsuffixes"
local tmpig="$1${2:+//$2}"
echo "${tmpig:+$tmpig:}$(array_join "${3:-ipsuffixes}" ,)"
}
function network_format_ipsuffix() {
# network_format_ipsuffix "$ip" "$suffix" --> "ip/suffix"
local ip="$(strlower "$1")"
if [ "$ip" == "dhcp" ]; then
echo "$ip"
else
echo "$1${2:+/$2}"
fi
}
function __network_fix_confbrs() {
local -a confbrs ifaces brs ips
local confbr br iface
# recenser les bridges et créer les tableaux __BR_ifaces
for confbr in "${__nfc_confbrs[@]}"; do
network_parse_confbr "$confbr" br ifaces
array_addu brs "$br"
eval "local -a ${br}_ifaces"
done
# puis constuire la liste des interfaces associées à chaque bridge
for confbr in "${__nfc_confbrs[@]}"; do
network_parse_confbr "$confbr" br ifaces
array_extendu "${br}_ifaces" ifaces
done
# puis construire le tableau final
array_new confbrs
for br in "${brs[@]}"; do
array_add confbrs "$(network_format_confbr "$br" "${br}_ifaces")"
done
array_copy __nfc_destconfbrs confbrs
}
function network_fix_confbrs() {
# normaliser le tableau $1(=confbrs): fusionner les doublons
local -a __nfc_confbrs __nfc_destconfbrs
array_copy __nfc_confbrs "${1:-confbrs}"
__network_fix_confbrs
array_copy "${1:-confbrs}" __nfc_destconfbrs
}
function __network_fix_confips() {
local -a confips ipsuffixes ifaces ips
local confip iface gateway network ip suffix mainip
local mainiface="$1"
# recenser les interfaces et créer les tableaux __IFACE_ipspecs
for confip in "${__nfc_confips[@]}"; do
network_parse_confip "$confip" iface gateway ipsuffixes
if [ -n "$iface" ]; then
network="${iface}_network"
if ! array_contains ifaces "$iface"; then
array_add ifaces "$iface"
eval "local ${iface}_gateway $network; local -a ${iface}_ipsuffixes"
fi
if [ -z "${!network}" -a -n "${ipsuffixes[0]}" ]; then
setv "$network" "$(ipcalc_network "${ipsuffixes[0]}")"
fi
fi
done
# puis construire la liste des adresses IP associées à chaque interface
for confip in "${__nfc_confips[@]}"; do
network_parse_confip "$confip" iface gateway ipsuffixes
# si aucune interface n'est spécifiée, sélectionner celle correspondant
# à la même adresse de réseau. sinon prendre $mainiface
if [ -z "$iface" ]; then
network="$(ipcalc_network "${ipsuffixes[0]}")"
local ip_iface ip_network
for ip_iface in "${ifaces[@]}"; do
ip_network="${ip_iface}_network"
if [ "${!ip_network}" == "$network" ]; then
iface="$ip_iface"
break
fi
done
fi
[ -n "$iface" ] || iface="$mainiface"
# si la passerelle a déjà été spécifiée, la récupérer
local tmpgw="${iface}_gateway"
[ -n "${!tmpgw}" ] && gateway="${!tmpgw}"
# calculer l'adresse ip principale, pour pouvoir traiter le cas où
# l'adresse ip principale est l'adresse de la passerelle.
mainip=
for ipsuffix in "${ipsuffixes[@]}"; do
network_parse_ipsuffix "$ipsuffix" ip suffix
if ! array_contains ips "$ip"; then
[ -n "$suffix" ] || suffix=24
if [ -z "$mainip" ]; then
[ -n "$gateway" ] || gateway="$(ipcalc_gateway "$ip" "$suffix")"
mainip="$ip"
fi
array_add "${iface}_ipsuffixes" "$(network_format_ipsuffix "$ip" "$suffix")"
array_add ips "$ip"
fi
done
[ "$gateway" == "$mainip" ] && gateway=
# si l'adresse ip principale est obtenue par dhcp, il ne faut pas
# spécifier la passerelle: elle sera fournie par le serveur DHCP.
# Utiliser le marqueur "none" pour que la valeur ne soit pas modifiée.
[ "${ipsuffixes[0]}" == "dhcp" ] && gateway=none
set_var "${iface}_gateway" "$gateway"
done
# puis construire le tableau final
array_new confips
for iface in "${ifaces[@]}"; do
gateway="${iface}_gateway"; gateway="${!gateway}"
[ "$gateway" == "none" ] && gateway=
array_add confips "$(network_format_confip "$iface" "$gateway" "${iface}_ipsuffixes")"
done
array_copy __nfc_destconfips confips
}
function network_fix_confips() {
# normaliser le tableau $1(=confips): fusionner les doublons, spécifier le
# suffixe /24 par défaut, etc. $2 est le cas échéant l'interface associée
# aux adresses ip non qualifiées
local -a __nfc_confips __nfc_destconfips
array_copy __nfc_confips "${1:-confips}"
__network_fix_confips "$2"
array_copy "${1:-confips}" __nfc_destconfips
}
function __network_fix_mainiface() {
local -a confips ifaces ipsuffixes
local br iface gateway confip mainconfip
local mainiface="$1"
# déterminer mainiface
if [ -z "$mainiface" -a -n "${__nfm_confbrs[0]}" ]; then
network_parse_confbr "${__nfm_confbrs[0]}" br ifaces
mainiface="$br"
fi
if [ -z "$mainiface" -a -n "${__nfm_confips[0]}" ]; then
network_parse_confip "${__nfm_confips[0]}" iface gateway ipsuffixes
mainiface="$iface"
fi
[ -n "$mainiface" ] || mainiface=eth0
# ensuite, il faut reécrire confips avec la valeur de mainiface
array_new confips
for confip in "${__nfm_confips[@]}"; do
network_parse_confip "$confip" iface gateway ipsuffixes
[ -n "$iface" ] || iface="$mainiface"
confip="$(network_format_confip "$iface" "$gateway" ipsuffixes)"
if [ "$iface" == "$mainiface" ]; then
mainconfip="$confip"
else
array_add confips "$confip"
fi
done
[ -n "$mainconfip" ] && array_ins confips "$mainconfip"
array_copy __nfm_destconfips confips
}
function network_fix_mainiface() {
# A partir des valeurs des tableaux $1(=confbrs) et $2(=confips), et de
# l'interface principale $3, déterminer l'interface principale. Si $3 est
# spécifié, c'est la valeur sélectionnée. Sinon, si un bridge existe, c'est
# le premier bridge qui est sélectionné. Sinon, la première interface est
# sélectionnée. Sinon, on prend eth0.
# Ensuite, réorganiser les tableaux de façon que confips[0] devienne la
# configuration ip de l'interface principale.
local -a __nfm_confbrs __nfm_confips __nfm_destconfips
array_copy __nfm_confbrs "${1:-confbrs}"
array_copy __nfm_confips "${2:-confips}"
__network_fix_mainiface "$3"
array_copy "${2:-confips}" __nfm_destconfips
}
function network_fix_confs() {
network_fix_confbrs "${1:-confbrs}"
network_fix_confips "${2:-confips}"
network_fix_mainiface "${1:-confbrs}" "${2:-confips}" "$3"
}
function __network_valid_ifaces() {
ip link | awk '/<.*>/ {
flags = $0; sub(/^.*</, ",", flags); sub(/>.*$/, ",", flags)
if (flags ~ /,LOOPBACK,/) next
if (flags !~ /,UP,/) next
sub(/:$/, "", $2); print $2
}'
}
function __network_set_confbrs() {
local -a ifaces tmpconfbrs confbrs
local confbr br iface
array_from_lines ifaces "$(__network_valid_ifaces)"
array_from_lines tmpconfbrs "$(brctl show 2>/dev/null | awk '
BEGIN { confbrs = "" }
NR == 1 { next }
{
if ($1 != "" && $2 != "") {
if (confbr != "") print confbr
confbr = $1 ":" $4
} else {
confbr = confbr "," $1
}
}
END {
if (confbr != "") print confbr
}
')"
for confbr in "${tmpconfbrs[@]}"; do
splitpair "$confbr" br iface
array_contains ifaces "$br" || continue
array_add confbrs "$confbr"
done
array_copy __nsc_destconfbrs confbrs
}
function network_set_confbrs() {
# initialiser $1(=confbrs) avec l'état des bridges sur le système courant
local -a __nsc_destconfbrs
__network_set_confbrs
array_copy "${1:-confbrs}" __nsc_destconfbrs
}
function __network_set_gateway() {
# initialiser la variable gateway avec la passerelle associée à l'interface
# $1
gateway="$(route -n | awk -v iface="$1" '$1 == "0.0.0.0" && $8 == iface { print $2 }')"
}
function __network_set_confip() {
# initialiser la variable confip avec l'état de l'interface $1, en assumant
# que la passerelle vaut $2.
# retourner 1 si l'interface n'a pas d'adresse ip associée
local -a ipsuffixes
array_from_lines ipsuffixes "$(ip addr show dev "$1" | awk '$1 == "inet" { print $2 }')"
confip="$(network_format_confip "$1" "$2" ipsuffixes)"
[ "${#ipsuffixes[*]}" -gt 0 ]
}
function __network_set_confips() {
local -a confips ifaces
local iface gateway
array_from_lines ifaces "$(__network_valid_ifaces)"
for iface in "${ifaces[@]}"; do
__network_set_gateway "$iface"
[ -n "$gateway" ] || gateway=-
__network_set_confip "$iface" "$gateway" || continue
array_add confips "$confip"
done
array_copy __nsc_destconfips confips
}
function network_set_confips() {
# initialiser le tableau $1(=confips) avec l'état des interfaces sur le
# système courant
local -a __nsc_destconfips
__network_set_confips
array_copy "${1:-confips}" __nsc_destconfips
}
function __network_backup() {
local file="$1"
local backup="$file.orig$(date +%Y%m%d)"
[ -f "$backup" ] || cat "$file" >"$backup" 2>/dev/null
}
function network_fix_exim4() {
local host="$1" oldhost="$2"
local tmpfile; ac_set_tmpfile tmpfile
local modified
sed </etc/exim4/update-exim4.conf.conf >"$tmpfile" "
/^dc_other_hostnames=/c\\
dc_other_hostnames='$host'"
if [ -n "$__NETWORK_DEVEL_SHOW_MODIFS" ]; then
if testdiff "$tmpfile" /etc/exim4/update-exim4.conf.conf; then
uecho "Setting /etc/exim4/update-exim4.conf.conf to:"
cat "$tmpfile" | sed 's/^/ /g' 1>&2
else
uecho "/etc/exim4/update-exim4.conf.conf: pas de modifications"
fi
elif testdiff "$tmpfile" /etc/exim4/update-exim4.conf.conf; then
__network_backup /etc/exim4/update-exim4.conf.conf
if show_debug; then
edebug "Setting /etc/exim4/update-exim4.conf.conf to:"
cat "$tmpfile" | sed 's/^/ /g' 1>&2
else
estep /etc/exim4/update-exim4.conf.conf
fi
cat "$tmpfile" >/etc/exim4/update-exim4.conf.conf
update-exim4.conf
modified=1
fi
ac_clean "$tmpfile"
[ -n "$modified" ]
}
function network_fix_postfix() {
local host="$1" oldhost="$2"
local tmpfile; ac_set_tmpfile tmpfile
local modified
awkrun </etc/postfix/main.cf >"$tmpfile" host="$host" oldhost="$oldhost" '
/^myhostname *=/ { $0 = "myhostname = " host }
/^mydestination *=/ {
gsub(" *" oldhost " *,? *", "")
if ($0 !~ (" *" host " *,? *")) {
sub(/^mydestination *= */, "mydestination = " host ", ")
}
}
{ print }'
if [ -n "$__NETWORK_DEVEL_SHOW_MODIFS" ]; then
if testdiff "$tmpfile" /etc/postfix/main.cf; then
uecho "Setting /etc/postfix/main.cf to:"
cat "$tmpfile" | sed 's/^/ /g' 1>&2
else
uecho "/etc/postfix/main.cf: pas de modifications"
fi
elif testdiff "$tmpfile" /etc/postfix/main.cf; then
__network_backup /etc/postfix/main.cf
if show_debug; then
edebug "Setting /etc/postfix/main.cf to:"
cat "$tmpfile" | sed 's/^/ /g' 1>&2
else
estep /etc/postfix/main.cf
fi
cat "$tmpfile" >/etc/postfix/main.cf
modified=1
fi
ac_clean "$tmpfile"
[ -n "$modified" ]
}
function network_fix_hosts() {
local host="$1" hostname="${1%%.*}"; local shost="${host//./\\.}"
local ip="$2"; local sip="${ip//./\\.}"
local oldhost="$3" oldhostname="${3%%.*}"; soldhost="${host//./\\.}"
local tmpfile; ac_set_tmpfile tmpfile
local modified
awkrun </etc/hosts >"$tmpfile" \
host="$host" hostname="$hostname" \
ip="$ip" \
oldhost="$oldhost" oldhostname="$oldhostname" \
'
/^[^# \t]/ {
gsub("[ \\t]+" host "[ \\t]*", " ")
gsub("[ \\t]+" hostname "[ \\t]*", " ")
gsub("[ \\t]+" oldhost "[ \\t]*", " ")
gsub("[ \\t]+" oldhostname "[ \\t]*", " ")
if ($0 ~ /^[^ \t]+[ \t]*$/) next
gsub(/[ \t]*$/, "")
}
{ print }'
if quietgrep "^$sip[ $TAB]\\+$shost[ $TAB]\\+$hostname" "$tmpfile"; then
sed -i "\
s/\$/ /
/^[ $TAB]$oldhost[ $TAB]/d
/^[ $TAB]$oldhostname[ $TAB]/d
s/ \$//
" "$tmpfile"
fi
if ! quietgrep "^$sip[ $TAB]\\+$shost[ $TAB]\\+$hostname" "$tmpfile"; then
sed -i "/^$sip[ $TAB]/d" "$tmpfile"
sed -i "1i\\
$ip$TAB$host $hostname" "$tmpfile"
fi
if [ -n "$__NETWORK_DEVEL_SHOW_MODIFS" ]; then
if testdiff "$tmpfile" /etc/hosts; then
uecho "Setting /etc/hosts to:"
cat "$tmpfile" | sed 's/^/ /g' 1>&2
else
uecho "/etc/hosts: pas de modifications"
fi
elif testdiff "$tmpfile" /etc/hosts; then
__network_backup /etc/hosts
if show_debug; then
edebug "Setting /etc/hosts to:"
cat "$tmpfile" | sed 's/^/ /g' 1>&2
else
estep /etc/hosts
fi
cat "$tmpfile" >/etc/hosts
modified=1
fi
ac_clean "$tmpfile"
[ -n "$modified" ]
}

View File

@ -5,7 +5,7 @@
##@require sysinfos
##@require service
uprovide redhat
urequire base sysinfos service
urequire base sysinfos service ipcalc
################################################################################
# Gestion des packages
@ -71,8 +71,23 @@ function service_enable() {
################################################################################
# Gestion des interfaces réseau
function create_bridge() {
# Créer un nouveau pont nommé $1 avec les paramètres $2
eerror "non implémenté"
return 1
__REDHAT_NETWORK_DEVEL_SHOW_MODIFS=
function network_fix_hostname() {
local hostname="${1%%.*}"
local modified
[ -f /etc/hostname ] || touch /etc/hostname
if [ -n "$__REDHAT_NETWORK_DEVEL_SHOW_MODIFS" -o -n "$__NETWORK_DEVEL_SHOW_MODIFS" ]; then
uecho "Setting /etc/hostname to $hostname"
elif [ "$(</etc/hostname)" != "$hostname" ]; then
__network_backup /etc/hostname
if show_debug; then
edebug "Setting /etc/hostname to $hostname"
else
estep /etc/hostname
fi
echo "$hostname" >/etc/hostname
modified=1
fi
[ -n "$modified" ]
}