1391 lines
		
	
	
		
			40 KiB
		
	
	
	
		
			Bash
		
	
	
	
	
	
			
		
		
	
	
			1391 lines
		
	
	
		
			40 KiB
		
	
	
	
		
			Bash
		
	
	
	
	
	
| ##@cooked comments # -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
 | |
| ## Outils pour les distributions debian
 | |
| ##@cooked nocomments
 | |
| ##@require base
 | |
| ##@require sysinfos
 | |
| ##@require service
 | |
| uprovide debian
 | |
| urequire base sysinfos service ipcalc
 | |
| 
 | |
| ################################################################################
 | |
| # Gestion des packages
 | |
| 
 | |
| function pkg_check() {
 | |
|     # Vérifier que les packages sont installés sur le système
 | |
|     local pkg status
 | |
|     for pkg in "$@"; do
 | |
|         status="$(LANG=C dpkg -s "$pkg" 2>/dev/null | grep '^Status:')" || return 1
 | |
|         [[ "$status" == *not-installed* ]] && return 1
 | |
|         [[ "$status" == *deinstall* ]] && return 1
 | |
|     done
 | |
|     return 0
 | |
| }
 | |
| 
 | |
| function pkg_update() {
 | |
|     # Mettre à jour la liste des packages silencieusement sans confirmation
 | |
|     LANG=C apt-get update -qy
 | |
| }
 | |
| 
 | |
| function pkg_upgrade() {
 | |
|     # Mettre à jour la liste des packages silencieusement sans confirmation
 | |
|     LANG=C apt-get upgrade -qy
 | |
| }
 | |
| 
 | |
| function pkg_install() {
 | |
|     # Installer les packages silencieusement et sans confirmation
 | |
|     LANG=C apt-get install -qy "$@"
 | |
| }
 | |
| 
 | |
| function pkg_installm() {
 | |
|     # Installer les packages silencieusement et sans confirmation
 | |
|     # Retourner 0 si au moins un des packages a été installé. Sinon, les
 | |
|     # packages n'ont pas été installés, soit parce qu'ils sont déjà installé,
 | |
|     # soit parce qu'il y a eu une erreur.
 | |
|     if ! pkg_check "$@"; then
 | |
|         # essayer d'installer les packages
 | |
|         pkg_install "$@"
 | |
|     else
 | |
|         # aucun package n'a été installé
 | |
|         return 1
 | |
|     fi
 | |
| }
 | |
| 
 | |
| ################################################################################
 | |
| # Gestion des services
 | |
| 
 | |
| function service_disable() {
 | |
|     # Désactiver le service $1 pour qu'il ne se lance pas automatiquement au
 | |
|     # démarrage
 | |
|     update-rc.d -f "$1" remove
 | |
| }
 | |
| 
 | |
| function service_enable() {
 | |
|     # Activer le service $1 pour qu'il se lance automatiquement au démarrage
 | |
|     update-rc.d "$1" defaults
 | |
| }
 | |
| 
 | |
| ################################################################################
 | |
| # Gestion des interfaces réseau
 | |
| 
 | |
| __DEBIAN_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"
 | |
|     if [ "${1,,}" == "dhcp" ]; then
 | |
|         echo "${1,,}"
 | |
|     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 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
 | |
|         [ -n "$iface" ] || iface="$mainiface"
 | |
|         array_addu ifaces "$iface"
 | |
|         eval "local ${iface}_gateway; local -a ${iface}_ipsuffixes"
 | |
|     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
 | |
|         [ -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"
 | |
|         __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() {
 | |
|     awkrun <"$nifile" -f br="$br" ifaces[@]=__nicc_ifaces '
 | |
| BEGIN {
 | |
|   array_new(iface_have_autos)
 | |
|   array_new(iface_have_hotplugs)
 | |
|   array_new(iface_have_manuals)
 | |
|   for (i = 1; i <= ifaces_count; i++) {
 | |
|     iface_have_autos[i] = 0
 | |
|     iface_have_hotplugs[i] = 0
 | |
|     iface_have_manuals[i] = 0
 | |
|   }
 | |
|   br_have_auto = 0
 | |
|   br_have_hotplug = 0
 | |
|   have_br = 0
 | |
| }
 | |
| function get_iface_index(iface,                 i) {
 | |
|   for (i = 1; i <= ifaces_count; i++) {
 | |
|     if (ifaces[i] == iface) return i
 | |
|   }
 | |
|   return 0
 | |
| }
 | |
| 
 | |
| $1 ~ /^(allow-)?auto$/ {
 | |
|   for (j = 2; j <= NF; j++) {
 | |
|     i = get_iface_index($j)
 | |
|     if (i != 0) iface_have_autos[i] = 1
 | |
|     if ($i == br) br_have_auto = 1
 | |
|   }
 | |
| }
 | |
| $1 == "allow-hotplug" {
 | |
|   for (j = 2; j <= NF; j++) {
 | |
|     i = get_iface_index($j)
 | |
|     if (i != 0) iface_have_hotplugs[i] = 1
 | |
|     if ($i == br) br_have_hotplug = 1
 | |
|   }
 | |
| }
 | |
| $1 == "iface" && $3 == "inet" && $4 == "manual" {
 | |
|   i = get_iface_index($2)
 | |
|   if (i != 0) iface_have_manuals[i] = 1
 | |
| }
 | |
| $1 == "iface" && $2 == br && $3 == "inet" {
 | |
|   have_br = 1
 | |
| }
 | |
| END {
 | |
|   check_auto_or_hotplug = br_have_auto || br_have_hotplug
 | |
|   check_manuals = 1
 | |
|   if (check_auto_or_hotplug) {
 | |
|     for (i = 1; i <= ifaces_count; i++) {
 | |
|       if (!iface_have_autos[i] && !iface_have_hotplugs[i]) {
 | |
|         check_auto_or_hotplug = 0
 | |
|         break
 | |
|       }
 | |
|       if (!iface_have_manuals[i]) {
 | |
|         check_manuals = 0
 | |
|         break
 | |
|       }
 | |
|     }
 | |
|   }
 | |
|   if (!check_auto_or_hotplug || !check_manuals || !have_br) {
 | |
|     exit 1
 | |
|   }
 | |
|   exit 0
 | |
| }
 | |
| '
 | |
| }
 | |
| 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
 | |
|     # $3(=/etc/network/interfaces)
 | |
|     local -a __nicc_ifaces
 | |
|     array_copy __nicc_ifaces "${2:-ifaces}"
 | |
|     local br="$1" nifile="${3:-/etc/network/interfaces}"
 | |
|     __network_interfaces_check_confbr
 | |
| }
 | |
| 
 | |
| function __network_interfaces_check_confip() {
 | |
|     awkrun <"$nifile" -f iface="$iface" gateway="$gateway" ipsuffixes[@]=__nicc_ipsuffixes '
 | |
| BEGIN {
 | |
|   have_auto = 0
 | |
|   have_hotplug = 0
 | |
|   have_iface = 0
 | |
|   have_method = 0 # static ou dhcp
 | |
|   have_gateway = ipsuffixes[1] == "dhcp" || gateway == ""
 | |
|   array_new(have_ips)
 | |
|   array_new(have_ip_ups)
 | |
|   array_new(have_ip_downs)
 | |
|   for (i = 1; i <= ipsuffixes_count; i++) {
 | |
|     have_ips[i] = 1
 | |
|     have_ip_ups[i] = 0
 | |
|     have_ip_downs[i] = 0
 | |
|   }
 | |
|   have_ips[1] = 0
 | |
|   have_ip_ups[1] = 1
 | |
|   have_ip_downs[1] = 1
 | |
|   in_iface = 0
 | |
| }
 | |
| function get_ipsuffix_index(ipsuffix,                 i) {
 | |
|   ip0 = ipsuffix; sub(/\/[0-9]+$/, "", ip0)
 | |
|   for (i = 1; i <= ipsuffixes_count; i++) {
 | |
|     # XXX ne pas comparer les suffixes pour le moment
 | |
|     ipi = ipsuffixes[i]; sub(/\/[0-9]+$/, "", ipi)
 | |
|     if (ipi == ip0) return i
 | |
|   }
 | |
|   return 0
 | |
| }
 | |
| 
 | |
| $1 == "iface" && $2 == iface && $3 == "inet" {
 | |
|   have_iface = 1
 | |
|   if (ipsuffixes[1] == "dhcp") {
 | |
|     have_method = $4 == "dhcp"
 | |
|   } else {
 | |
|     have_method = $4 == "static"
 | |
|   }
 | |
|   in_iface = 1
 | |
|   next
 | |
| }
 | |
| in_iface && $1 == "gateway" && $2 == gateway {
 | |
|   have_gateway = 1
 | |
|   next
 | |
| }
 | |
| in_iface && $1 == "address" && get_ipsuffix_index($2) == 1 {
 | |
|   have_ips[1] = 1
 | |
|   next
 | |
| }
 | |
| in_iface && $0 ~ "^[ \\t]*(post-)?up (/sbin/)?ip addr add " {
 | |
|   if (match($0, "ip addr add (.*/.*) dev " iface " ", vs) != 0) {
 | |
|     i = get_ipsuffix_index(vs[1])
 | |
|     if (i > 1) {
 | |
|       have_ip_ups[i] = 1
 | |
|     }
 | |
|   }
 | |
|   next
 | |
| }
 | |
| in_iface && $0 ~ "^[ \\t]*(pre-)?down (/sbin/)?ip addr del " {
 | |
|   if (match($0, "ip addr del (.*/.*) dev " iface " ", vs) != 0) {
 | |
|     i = get_ipsuffix_index(vs[1])
 | |
|     if (i > 1) {
 | |
|       have_ip_downs[i] = 1
 | |
|     }
 | |
|   }
 | |
|   next
 | |
| }
 | |
| $1 == "iface" {
 | |
|   in_iface = 0
 | |
|   next
 | |
| }
 | |
| $1 ~ /^(allow-)?auto$/ {
 | |
|   for (i = 2; i <= NF; i++) {
 | |
| print "auto: " $i ", iface=" iface
 | |
|     if ($i == iface) {
 | |
|       have_auto = 1
 | |
|     }
 | |
|   }
 | |
|   in_iface = 0
 | |
|   next
 | |
| }
 | |
| $1 == "allow-hotplug" {
 | |
|   for (i = 2; i <= NF; i++) {
 | |
| print "hotplug: " $i ", iface=" iface
 | |
|     if ($i == iface) {
 | |
|       have_hotplug = 1
 | |
|     }
 | |
|   }
 | |
|   in_iface = 0
 | |
|   next
 | |
| }
 | |
| 
 | |
| END {
 | |
|   check_auto_or_hotplug = have_auto || have_hotplug
 | |
|   check_iface = have_iface && have_method && have_gateway
 | |
|   check_ips = 1
 | |
|   for (i = 1; i <= ipsuffixes_count; i++) {
 | |
|     if (!have_ips[i] || !have_ip_ups[i] || !have_ip_downs[i]) {
 | |
|       check_ips = 0
 | |
|       break
 | |
|     }
 | |
|   }
 | |
|   if (!check_auto_or_hotplug || !check_iface || !check_ips) {
 | |
|     exit 1
 | |
|   }
 | |
|   exit 0
 | |
| }
 | |
| '
 | |
| }
 | |
| function network_interfaces_check_confip() {
 | |
|     # Vérifier que la configuration de l'interface $1, avec la passerelle $2,
 | |
|     # avec les adresses IP du tabbleau $3(=ipsuffixes) est faite dans le fichier
 | |
|     # $4(=/etc/network/interfaces)
 | |
|     local -a __nicc_ipsuffixes
 | |
|     array_copy __nicc_ipsuffixes "${3:-ipsuffixes}"
 | |
|     local iface="$1" gateway="$2" nifile="${4:-/etc/network/interfaces}"
 | |
|     __network_interfaces_check_confip
 | |
| }
 | |
| 
 | |
| function network_interfaces_remove_iface() {
 | |
|     :
 | |
| }
 | |
| 
 | |
| function network_interfaces_add_confbr() {
 | |
|     :
 | |
| }
 | |
| 
 | |
| function network_interfaces_add_confip() {
 | |
|     :
 | |
| }
 | |
| 
 | |
| #XXX
 | |
| function __old_network_backup() {
 | |
|     local file="$1"
 | |
|     local backup="$file.orig$(date +%Y%m%d)"
 | |
|     [ -f "$backup" ] || cat "$file" >"$backup" 2>/dev/null
 | |
| }
 | |
| 
 | |
| function __old_network_hostname() {
 | |
|     local hostname="${1%%.*}"
 | |
|     [ -f /etc/hostname ] || touch /etc/hostname
 | |
|     if [ -n "$__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS" ]; then
 | |
|         uecho "Setting /etc/hostname to $hostname"
 | |
|     elif [ "$(</etc/hostname)" != "$hostname" ]; then
 | |
|         __old_network_backup /etc/hostname
 | |
|         if show_debug; then
 | |
|             edebug "Setting /etc/hostname to $hostname"
 | |
|         else
 | |
|             estep /etc/hostname
 | |
|         fi
 | |
|         echo "$hostname" >/etc/hostname
 | |
|     fi
 | |
| }
 | |
| 
 | |
| function __old_network_mailname() {
 | |
|     local host="$1"
 | |
|     [ -f /etc/mailname ] || touch /etc/mailname
 | |
|     if [ -n "$__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS" ]; then
 | |
|         uecho "Setting /etc/mailname to $host"
 | |
|     elif [ "$(</etc/mailname)" != "$host" ]; then
 | |
|         __old_network_backup /etc/mailname
 | |
|         if show_debug; then
 | |
|             edebug "Setting /etc/mailname to $host"
 | |
|         else
 | |
|             estep /etc/mailname
 | |
|         fi
 | |
|         echo "$host" >/etc/mailname
 | |
|     fi
 | |
| }
 | |
| 
 | |
| function __old_network_fix_exim4() {
 | |
|     local host="$1" oldhost="$2"
 | |
|     local tmpfile; ac_set_tmpfile tmpfile
 | |
| 
 | |
|     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
 | |
|         __old_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
 | |
|     fi
 | |
|     ac_clean "$tmpfile"
 | |
| }
 | |
| 
 | |
| function __old_network_fix_postfix() {
 | |
|     local host="$1" oldhost="$2"
 | |
|     local tmpfile; ac_set_tmpfile tmpfile
 | |
| 
 | |
|     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
 | |
|         __old_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
 | |
|     fi
 | |
|     ac_clean "$tmpfile"
 | |
| }
 | |
| 
 | |
| function __old_network_hosts() {
 | |
|     local host="$1" hostname="${1%%.*}" ip="$2"
 | |
|     local shost="${host//./\\.}"
 | |
|     local tmpfile; ac_set_tmpfile tmpfile
 | |
|     awkrun </etc/hosts >"$tmpfile" host="$host" hostname="$hostname" ip="$ip" '
 | |
| /^[^# \t]/ {
 | |
|   gsub("[ \\t]+" host "[ \\t]*", " ")
 | |
|   gsub("[ \\t]+" hostname "[ \\t]*", " ")
 | |
|   if ($0 ~ /^[^ \t]+[ \t]*$/) next
 | |
|   gsub(/[ \t]*$/, "")
 | |
| }
 | |
| { print }'
 | |
|     local sip="${ip//./\\.}" shost="${host//./\\.}"
 | |
|     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
 | |
|         __old_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
 | |
|     fi
 | |
|     ac_clean "$tmpfile"
 | |
| }
 | |
| 
 | |
| __DEBIAN_NETWORK_UPDATE_BASE_SCRIPT='
 | |
| BEGIN {
 | |
|   method = "dhcp"
 | |
|   array_new(have_iface_hotplugs)
 | |
|   array_new(have_iface_autos)
 | |
|   array_new(have_iface_manuals)
 | |
|   array_new(ips)
 | |
|   array_new(suffixes)
 | |
|   array_new(gateways)
 | |
|   array_new(have_ip_ups)
 | |
|   array_new(have_ip_downs)
 | |
| }
 | |
| function indexof_iface(iface,            i) {
 | |
|   for (i = 1; i <= ifaces_count; i++) {
 | |
|     if (ifaces[i] == iface) {
 | |
|       return i
 | |
|     }
 | |
|   }
 | |
|   return 0
 | |
| }
 | |
| function indexof_ip(ip,            i) {
 | |
|   for (i = 1; i <= ipspecs_count; i++) {
 | |
|     if (ips[i] == ip) {
 | |
|       return i
 | |
|     }
 | |
|   }
 | |
|   return 0
 | |
| }
 | |
| function get_netmask(suffix) {
 | |
|   if (suffix == 0) return "0.0.0.0"
 | |
|   else if (suffix == 8) return "255.0.0.0"
 | |
|   else if (suffix == 16) return "255.255.0.0"
 | |
|   else if (suffix == 24) return "255.255.255.0"
 | |
|   else if (suffix == 32) return "255.255.255.255"
 | |
|   else return suffix
 | |
| }
 | |
| function remove_hotplug_iface(iface, line) {
 | |
|   if (line == "") line = $0
 | |
|   if (line !~ / $/) line = line " "
 | |
|   gsub(" " iface " ", " ", line)
 | |
|   return line
 | |
| }
 | |
| function print_hotplug_iface(line) {
 | |
|   if (line ~ /^allow-hotplug *$/) {
 | |
|     # une seule interface sur la ligne: ne pas l"afficher
 | |
|   } else {
 | |
|     # supprimer l"interface de la ligne
 | |
|     sub(/ *$/, "", line)
 | |
|     print line
 | |
|   }
 | |
| }
 | |
| function remove_auto_iface(iface, line) {
 | |
|   if (line == "") line = $0 " "
 | |
|   gsub(" " iface " ", " ", line)
 | |
|   return line
 | |
| }
 | |
| function print_auto_iface(line) {
 | |
|   if (line ~ /^(allow-)?auto *$/) {
 | |
|     # une seule interface sur la ligne: ne pas l"afficher
 | |
|   } else {
 | |
|     # supprimer l"interface de la ligne
 | |
|     sub(/ *$/, "", line)
 | |
|     print line
 | |
|   }
 | |
| }
 | |
| '
 | |
| __DEBIAN_NETWORK_UPDATE_BRIDGE_SCRIPT='
 | |
| BEGIN {
 | |
|   for (i = 1; i <= ifaces_count; i++) {
 | |
|     have_iface_hotplugs[i] = 0
 | |
|     have_iface_autos[i] = 0
 | |
|     have_iface_manuals[i] = 0
 | |
|   }
 | |
|   for (i = 1; i <= ipspecs_count; i++) {
 | |
|     if (match(ipspecs[i], "^([^/]*)(/([^+]*))?(\\+(.*))?$", vs) != 0) {
 | |
|       ip = vs[1]
 | |
|       suffix = vs[3]
 | |
|       gateway = vs[5]
 | |
|     } else {
 | |
|       ip = ""
 | |
|       suffix = ""
 | |
|       gateway = ""
 | |
|     }
 | |
|     if (i == 1) {
 | |
|       if (ip == "dhcp") {
 | |
|         method = "dhcp"
 | |
|       } else {
 | |
|         method = "static"
 | |
|         mainip = ip
 | |
|       }
 | |
|     }
 | |
|     if (suffix == "") suffix = "24"
 | |
|     ips[i] = ip
 | |
|     suffixes[i] = suffix
 | |
|     gateways[i] = gateway
 | |
|     have_ip_ups[i] = i == 1
 | |
|     have_ip_downs[i] = i == 1
 | |
|   }
 | |
| }
 | |
| '
 | |
| __DEBIAN_NETWORK_UPDATE_IFACE_SCRIPT='
 | |
| BEGIN {
 | |
|   for (i = 1; i <= ipspecs_count; i++) {
 | |
|     if (match(ipspecs[i], "^([^/]*)(/([^+]*))?(\\+(.*))?$", vs) != 0) {
 | |
|       ip = vs[1]
 | |
|       suffix = vs[3]
 | |
|       gateway = vs[5]
 | |
|     } else {
 | |
|       ip = ""
 | |
|       suffix = ""
 | |
|       gateway = ""
 | |
|     }
 | |
|     if (i == 1) {
 | |
|       if (ip == "dhcp") {
 | |
|         method = "dhcp"
 | |
|       } else {
 | |
|         method = "static"
 | |
|         mainip = ip
 | |
|       }
 | |
|     }
 | |
|     if (suffix == "") suffix = "24"
 | |
|     ips[i] = ip
 | |
|     suffixes[i] = suffix
 | |
|     gateways[i] = gateway
 | |
|     have_ip_ups[i] = i == 1
 | |
|     have_ip_downs[i] = i == 1
 | |
|   }
 | |
| }
 | |
| '
 | |
| 
 | |
| function __old_network_update_bridge() {
 | |
|     # vérifier la configuration (interfaces de ifaces[@] en manual, présence du
 | |
|     # bridge, bridge en auto, adresse ip principale statique ou en dhcp,
 | |
|     # adresses ip supplémentaires), puis si nécessaire, supprimer l'ancienne
 | |
|     # configuration et créer la nouvelle.
 | |
|     local inf="$1"; shift
 | |
|     local outf="$1"; shift
 | |
|     awkrun <"$inf" -f iface="$1" ipspecs[@]="${2:-ipspecs}" ifaces[@]="${3:-ifaces}" '
 | |
| BEGIN {
 | |
|   have_hotplug = 0
 | |
|   have_auto = 0
 | |
|   have_iface = 0
 | |
|   have_method = 0 # static ou dhcp
 | |
|   in_iface = 0
 | |
|   have_mainip = 0
 | |
| }
 | |
| '"
 | |
| $__DEBIAN_NETWORK_UPDATE_BASE_SCRIPT
 | |
| $__DEBIAN_NETWORK_UPDATE_BRIDGE_SCRIPT
 | |
| "'
 | |
| 
 | |
| $1 == "iface" && $2 == iface && $3 == "inet" {
 | |
|   have_iface = 1
 | |
|   if (($0 " ") ~ (" " method " ")) have_method = 1
 | |
|   in_iface = 1
 | |
|   next
 | |
| }
 | |
| in_iface && ($0 " ") ~ "^[ \\t]*address " mainip " " { have_mainip = 1 }
 | |
| in_iface && $0 ~ "^[ \\t]*up (/sbin/)?ip addr add " {
 | |
|   if (match($0, "ip addr add (.*)/.* dev " iface, vs) != 0) {
 | |
|     i = indexof_ip(vs[1])
 | |
|     if (i != 0) have_ip_ups[i] = 1
 | |
|   }
 | |
|   next
 | |
| }
 | |
| in_iface && $0 ~ "^[ \\t]*down (/sbin/)?ip addr del " {
 | |
|   if (match($0, "ip addr del (.*)/.* dev " iface, vs) != 0) {
 | |
|     i = indexof_ip(vs[1])
 | |
|     if (i != 0) have_ip_downs[i] = 1
 | |
|   }
 | |
|   next
 | |
| }
 | |
| $1 == "iface" {
 | |
|   i = indexof_iface($2)
 | |
|   if (i != 0 && $3 == "inet" && $4 == "manual") {
 | |
|     have_iface_manuals[i] = 1
 | |
|   }
 | |
|   in_iface = 0
 | |
|   next
 | |
| }
 | |
| $1 == "allow-hotplug" {
 | |
|   if (($0 " ") ~ (" " iface " ")) have_hotplug = 1
 | |
|   for (i = 1; i <= ifaces_count; i++) {
 | |
|     if (($0 " ") ~ (" " ifaces[i] " ")) iface_have_hotplugs[i] = 1
 | |
|   }
 | |
|   in_iface = 0
 | |
|   next
 | |
| }
 | |
| $1 ~ /^(allow-)?auto$/ {
 | |
|   if (($0 " ") ~ (" " iface " ")) have_auto = 1
 | |
|   for (i = 1; i <= ifaces_count; i++) {
 | |
|     if (($0 " ") ~ (" " ifaces[i] " ")) have_iface_autos[i] = 1
 | |
|   }
 | |
|   in_iface = 0
 | |
|   next
 | |
| }
 | |
| 
 | |
| END {
 | |
|   check_hotplug = !have_hotplug
 | |
|   if (check_hotplug) for (i = 1; i <= ifaces_count; i++) {
 | |
|     if (iface_have_hotplugs[i]) {
 | |
|       check_hotplug = 0
 | |
|       break
 | |
|     }
 | |
|   }
 | |
|   check_auto = have_auto
 | |
|   if (check_auto) for (i = 1; i <= ifaces_count; i++) {
 | |
|     if (!have_iface_autos[i]) {
 | |
|       check_auto = 0
 | |
|       break
 | |
|     }
 | |
|   }
 | |
|   check_iface = have_iface && have_method && have_mainip
 | |
|   check_supplips = 1
 | |
|   for (i = 1; i <= ipspecs_count; i++) {
 | |
|     if (!have_ip_ups[i] || !have_ip_downs[i]) {
 | |
|       check_supplips = 0
 | |
|       break
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   if (!check_hotplug || !check_auto || !check_iface || !check_supplips) {
 | |
|     exit 1 # il faut refaire la configuration
 | |
|   }
 | |
|   exit 0 # tout est ok
 | |
| }
 | |
| ' && return 1
 | |
| 
 | |
|     # il faut refaire la configuration
 | |
|     awkrun <"$inf" >"$outf" -f iface="$1" ipspecs[@]="${2:-ipspecs}" ifaces[@]="${3:-ifaces}" '
 | |
| BEGIN {
 | |
|   have_auto = 0
 | |
|   remove_auto = 0
 | |
|   array_new(remove_iface_autos)
 | |
|   for (i = 1; i <= ifaces_count; i++) {
 | |
|     remove_iface_autos[i] = 0
 | |
|   }
 | |
|   replace_conf = 1
 | |
|   in_iface = 0
 | |
|   found_iface = 0
 | |
|   remove_old_iface = 0
 | |
| }
 | |
| '"
 | |
| $__DEBIAN_NETWORK_UPDATE_BASE_SCRIPT
 | |
| $__DEBIAN_NETWORK_UPDATE_BRIDGE_SCRIPT
 | |
| "'
 | |
| function write_conf() {
 | |
|   replace_conf = 0
 | |
| 
 | |
|   for (i = 1; i <= ifaces_count; i++) {
 | |
|     printed = 0
 | |
|     if (!have_iface_autos[i]) {
 | |
|       print "auto " ifaces[i]
 | |
|       printed = 1
 | |
|     }
 | |
|     if (!have_iface_manuals[i]) {
 | |
|       print "iface " ifaces[i] " inet manual"
 | |
|       printed = 1
 | |
|     }
 | |
|     if (printed) print ""
 | |
|   }
 | |
| 
 | |
|   if (!have_auto) print "auto " iface
 | |
|   print "iface " iface " inet " method
 | |
|   if (ipspecs_count > 0 && method == "static") {
 | |
|     print "    address " ips[1]
 | |
|     print "    netmask " get_netmask(suffixes[1])
 | |
|     if (gateways[1] != "" && gateways[1] != ips[1]) {
 | |
|       print "    gateway " gateways[1]
 | |
|     }
 | |
|   }
 | |
|   ports = "    bridge_ports"
 | |
|   if (ifaces_count == 0) {
 | |
|     ports = ports " none"
 | |
|   } else {
 | |
|     for (i = 1; i <= ifaces_count; i++) {
 | |
|       ports = ports " " ifaces[i]
 | |
|     }
 | |
|   }
 | |
|   print ports
 | |
|   print "    bridge_stp off"
 | |
|   print "    bridge_fd 2"
 | |
|   print "    bridge_maxwait 0"
 | |
|   serial = 0
 | |
|   for (i = 2; i <= ipspecs_count; i++) {
 | |
|     print "    up ip addr add " ips[i] "/" suffixes[i] " dev " iface " label " iface ":" serial
 | |
|     print "    down ip addr del " ips[i] "/" suffixes[i] " dev " iface " label " iface ":" serial
 | |
|     serial++
 | |
|   }
 | |
|   print ""
 | |
| }
 | |
| 
 | |
| remove_old_iface && $1 == "iface" {
 | |
|   remove_old_iface = 0
 | |
| }
 | |
| !remove_old_iface && replace_conf && !in_iface && $1 == "iface" && $2 == iface && $3 == "inet" {
 | |
|   in_iface = 1
 | |
|   found_iface = 1
 | |
|   next
 | |
| }
 | |
| !remove_old_iface && $1 == "iface" {
 | |
|   in_iface = 0
 | |
|   for (i = 1; i <= ifaces_count; i++) {
 | |
|     if ($2 == ifaces[i]) {
 | |
|       if ($3 == "inet" && $4 == "manual") {
 | |
|         have_iface_manuals[i] = 1
 | |
|         break
 | |
|       } else {
 | |
|         remove_old_iface = 1
 | |
|       }
 | |
|     }
 | |
|   }
 | |
|   if (replace_conf && found_iface) write_conf()
 | |
|   if (!remove_old_iface) print
 | |
|   next
 | |
| }
 | |
| $1 == "allow-hotplug" {
 | |
|   in_iface = 0
 | |
|   remove_old_iface = 0
 | |
|   if (replace_conf && found_iface) write_conf()
 | |
| 
 | |
|   line = $0
 | |
|   if (($0 " ") ~ (" " iface " ")) line = remove_hotplug_iface(iface, line)
 | |
|   for (i = 1; i <= ifaces_count; i++) {
 | |
|     if (($0 " ") ~ (" " ifaces[i] " ")) line = remove_hotplug_iface(ifaces[i], line)
 | |
|   }
 | |
|   print_hotplug_iface(line)
 | |
|   next
 | |
| }
 | |
| $1 ~ /(allow-)?auto/ {
 | |
|   in_iface = 0
 | |
|   remove_old_iface = 0
 | |
|   if (replace_conf && found_iface) write_conf()
 | |
| 
 | |
|   line = $0
 | |
|   if (($0 " ") ~ (" " iface " ")) {
 | |
|     if (!remove_auto) {
 | |
|       have_auto = 1
 | |
|       remove_auto = 1
 | |
|     } else {
 | |
|       line = remove_auto_iface(iface, line)
 | |
|     }
 | |
|   }
 | |
|   for (i = 1; i <= ifaces_count; i++) {
 | |
|     if (($0 " ") ~ (" " ifaces[i] " ")) {
 | |
|       if (!remove_iface_autos[i]) {
 | |
|         have_iface_autos[i] = 1
 | |
|         remove_iface_autos[i] = 1
 | |
|       } else {
 | |
|         line = remove_auto_iface(ifaces[i], line)
 | |
|       }
 | |
|     }
 | |
|   }
 | |
|   print_auto_iface(line)
 | |
|   next
 | |
| }
 | |
| { print }
 | |
| END {
 | |
|   if (replace_conf) write_conf()
 | |
| }
 | |
| '
 | |
|     return 0
 | |
| }
 | |
| 
 | |
| function __old_network_update_iface() {
 | |
|     # vérifier la configuration (présence de l'interface, interface en auto,
 | |
|     # adresse ip principale statique ou en dhcp, adresses ip supplémentaires),
 | |
|     # puis si nécessaire, supprimer l'ancienne configuration et créer la
 | |
|     # nouvelle.
 | |
|     local inf="$1"; shift
 | |
|     local outf="$1"; shift
 | |
|     awkrun <"$inf" -f iface="$1" ipspecs[@]="${2:-ipspecs}" '
 | |
| BEGIN {
 | |
|   have_hotplug = 0
 | |
|   have_auto = 0
 | |
|   have_iface = 0
 | |
|   have_method = 0 # static ou dhcp
 | |
|   in_iface = 0
 | |
|   have_mainip = 0
 | |
| }
 | |
| '"
 | |
| $__DEBIAN_NETWORK_UPDATE_BASE_SCRIPT
 | |
| $__DEBIAN_NETWORK_UPDATE_IFACE_SCRIPT
 | |
| "'
 | |
| 
 | |
| $1 == "iface" && $2 == iface && $3 == "inet" {
 | |
|   have_iface = 1
 | |
|   if (($0 " ") ~ (" " method " ")) have_method = 1
 | |
|   in_iface = 1
 | |
|   next
 | |
| }
 | |
| in_iface && ($0 " ") ~ "^[ \\t]*address " mainip " " { have_mainip = 1 }
 | |
| in_iface && $0 ~ "^[ \\t]*up (/sbin/)?ip addr add " {
 | |
|   if (match($0, "ip addr add (.*)/.* dev " iface, vs) != 0) {
 | |
|     i = indexof_ip(vs[1])
 | |
|     if (i != 0) have_ip_ups[i] = 1
 | |
|   }
 | |
|   next
 | |
| }
 | |
| in_iface && $0 ~ "^[ \\t]*down (/sbin/)?ip addr del " {
 | |
|   if (match($0, "ip addr del (.*)/.* dev " iface, vs) != 0) {
 | |
|     i = indexof_ip(vs[1])
 | |
|     if (i != 0) have_ip_downs[i] = 1
 | |
|   }
 | |
|   next
 | |
| }
 | |
| $1 == "iface" {
 | |
|   in_iface = 0
 | |
|   next
 | |
| }
 | |
| $1 == "allow-hotplug" {
 | |
|   if (($0 " ") ~ (" " iface " ")) have_hotplug = 1
 | |
|   in_iface = 0
 | |
|   next
 | |
| }
 | |
| $1 ~ /^(allow-)?auto$/ {
 | |
|   if (($0 " ") ~ (" " iface " ")) have_auto = 1
 | |
|   in_iface = 0
 | |
|   next
 | |
| }
 | |
| 
 | |
| END {
 | |
|   check_hotplug = !have_hotplug
 | |
|   check_auto = have_auto
 | |
|   check_iface = have_iface && have_method && have_mainip
 | |
|   check_supplips = 1
 | |
|   for (i = 1; i <= ipspecs_count; i++) {
 | |
|     if (!have_ip_ups[i] || !have_ip_downs[i]) {
 | |
|       check_supplips = 0
 | |
|       break
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   if (!check_hotplug || !check_auto || !check_iface || !check_supplips) {
 | |
|     exit 1 # il faut refaire la configuration
 | |
|   }
 | |
|   exit 0 # tout est ok
 | |
| }
 | |
| ' && return 1
 | |
| 
 | |
|     # il faut refaire la configuration
 | |
|     awkrun <"$inf" >"$outf" -f iface="$1" ipspecs[@]="${2:-ipspecs}" '
 | |
| BEGIN {
 | |
|   have_auto = 0
 | |
|   remove_auto = 0
 | |
|   replace_conf = 1
 | |
|   in_iface = 0
 | |
|   found_iface = 0
 | |
| }
 | |
| '"
 | |
| $__DEBIAN_NETWORK_UPDATE_BASE_SCRIPT
 | |
| $__DEBIAN_NETWORK_UPDATE_IFACE_SCRIPT
 | |
| "'
 | |
| function write_conf() {
 | |
|   replace_conf = 0
 | |
|   if (!have_auto) print "auto " iface
 | |
|   print "iface " iface " inet " method
 | |
|   if (ipspecs_count > 0 && method == "static") {
 | |
|     print "    address " ips[1]
 | |
|     print "    netmask " get_netmask(suffixes[1])
 | |
|     if (gateways[1] != "" && gateways[1] != ips[1]) {
 | |
|       print "    gateway " gateways[1]
 | |
|     }
 | |
|   }
 | |
|   serial = 0
 | |
|   for (i = 2; i <= ipspecs_count; i++) {
 | |
|     print "    up ip addr add " ips[i] "/" suffixes[i] " dev " iface " label " iface ":" serial
 | |
|     print "    down ip addr del " ips[i] "/" suffixes[i] " dev " iface " label " iface ":" serial
 | |
|     serial++
 | |
|   }
 | |
|   print ""
 | |
| }
 | |
| 
 | |
| ($0 " ") ~ ("^allow-hotplug.* " iface " ") { print_hotplug_iface(remove_hotplug_iface(iface)); next; }
 | |
| !remove_auto && ($0 " ") ~ ("^(allow-)?auto.* " iface " ") { have_auto = 1; remove_auto = 1; next; }
 | |
| remove_auto && ($0 " ") ~ ("^(allow-)?auto.* " iface " ") { print_auto_iface(remove_auto_iface(iface)); next; }
 | |
| replace_conf && !in_iface && $0 ~ ("^iface " iface " inet ") { in_iface = 1; found_iface = 1; next; }
 | |
| replace_conf && found_iface && in_iface && $0 ~ "^iface " { print; in_iface = 0; write_conf(); next; }
 | |
| { print }
 | |
| END {
 | |
|   if (replace_conf) write_conf()
 | |
| }
 | |
| '
 | |
|     return 0
 | |
| }
 | |
| 
 | |
| function network_config() {
 | |
|     # (Re)configurer le réseau sur l'hôte courant.
 | |
|     # $1 (host) est le nom d'hôte.
 | |
|     # $2 (confips) est le nom d'un tableau contenant la configuration des
 | |
|     # adresses ips pour les interfaces.
 | |
|     # $3 (confbrs) est le nom d'un tableau contenant la configuration des
 | |
|     # bridges à créer/mettre à jour.
 | |
|     # $4 (mainiface) est le nom de l'interface principale, c'est à dire
 | |
|     # l'interface qui est sélectionnée si une adresse ip n'est pas préfixée de
 | |
|     # son interface. En principe, l'interface principale est le premier bridge
 | |
|     # défini ou la première interface définie.
 | |
|     # $5 (reset_interfaces) spécifie de ne pas chercher à mettre à jour le
 | |
|     # fichier /etc/network/interfaces, mais de le recréer depuis zéro.
 | |
|     # Si un des arguments n'est pas spécifié, il est ignoré.
 | |
|     # Le tableau confips doit contenir des définitions d'une des formes
 | |
|     # suivantes:
 | |
|     #     [iface:]address[/suffix][+gateway],...
 | |
|     #     [iface:]dhcp
 | |
|     # La deuxième forme est pour spécifier qu'une interface est configurée par
 | |
|     # DHCP. iface vaut par défaut eth0, sauf si une définition de bridge
 | |
|     # existe, auquel cas il s'agit du premier bridge défini. Pour chaque
 | |
|     # interface, seule la première spécification d'adresse IP tient compte de
 | |
|     # l'argument gateway. Les autres spécifications définissent des adresses IP
 | |
|     # supplémentaires pour l'interface.
 | |
|     # Le tableau brs doit contenir des définitions de la forme suivante:
 | |
|     #     br:ifaces,...
 | |
|     # br est le nom du bridge, e.g. br0. ifaces est une liste d'interfaces
 | |
|     # séparées par une virgule. e.g. br0:eth0,eth1
 | |
|     # Bien que ce soit techniquement possible, ce script interdit que l'on
 | |
|     # définisse une adresse IP pour une interface faisant partie d'un bridge.
 | |
|     urequire ipcalc conf
 | |
| 
 | |
|     local -a __nc_confips __nc_confbrs
 | |
|     [ -n "$2" ] && array_copy __nc_confips "$2"
 | |
|     [ -n "$3" ] && array_copy __nc_confbrs "$3"
 | |
| 
 | |
|     local host="$1" mainiface="$4" reset_interfaces="$5"
 | |
|     local -a confips confbrs
 | |
|     array_copy confips __nc_confips
 | |
|     array_copy confbrs __nc_confbrs
 | |
| 
 | |
|     __old_network_fix_confips confips
 | |
|     __old_network_fix_confbrs confbrs
 | |
| 
 | |
|     local mainbr # bridge principal
 | |
|     local mainip # adresse IP principale de l'interface principale
 | |
|     local confbr confip br iface suffix gateway
 | |
|     local -a ipspecs
 | |
| 
 | |
|     __old_network_resolve_mainiface mainiface "${confbrs[0]}" "${confips[0]}"
 | |
|     __old_network_parse_confbr "${confbrs[0]}" mainbr
 | |
|     [ -n "$mainbr" ] || mainbr=br0
 | |
| 
 | |
|     if [ -n "${confips[*]}" -o -n "${confbrs[*]}" ]; then
 | |
|         # Vérifier qu'une interface avec une adresse IP ne figure pas dans les
 | |
|         # bridges à configurer. en profiter pour classer les interfaces par
 | |
|         # type: standard et bridges
 | |
|         etitle "Configuration des interfaces réseau"
 | |
| 
 | |
|         local -a ifaces_with_ips ifaces
 | |
|         local -a brifaces
 | |
|         ifaces_with_ips=()
 | |
|         for confip in "${confips[@]}"; do
 | |
|             __old_network_parse_confip "$confip" iface
 | |
|             array_addu ifaces_with_ips "$iface"
 | |
|         done
 | |
|         for confbr in "${confbrs[@]}"; do
 | |
|             __old_network_parse_confbr "$confbr" br ifaces
 | |
|             array_add brifaces "$br"
 | |
|             for iface in "${ifaces[@]}"; do
 | |
|                 if array_contains ifaces_with_ips "$iface"; then
 | |
|                     eerror "$iface: Impossible de configurer une interface dans un bridge ET avec une adresse IP"
 | |
|                     return 1
 | |
|                 fi
 | |
|             done
 | |
|         done
 | |
|         local -a confstdips confbrips
 | |
|         for confip in "${confips[@]}"; do
 | |
|             __old_network_parse_confip "$confip" iface
 | |
|             [ -n "$iface" ] || iface="$mainiface"
 | |
|             if array_contains brifaces "$iface"; then
 | |
|                 array_add confbrips "$confip"
 | |
|             else
 | |
|                 array_add confstdips "$confip"
 | |
|             fi
 | |
|         done
 | |
| 
 | |
|         # maintenant, configurer /etc/network/interfaces: faire une copie de travail
 | |
|         local interfaces workfile
 | |
|         ac_set_tmpfile interfaces
 | |
|         ac_set_tmpfile workfile
 | |
|         cat /etc/network/interfaces >"$interfaces"
 | |
| 
 | |
|         if [ -n "$reset_interfaces" ]; then
 | |
|             echo >"$interfaces" "\
 | |
| # This file describes the network interfaces available on your system
 | |
| # and how to activate them. For more information, see interfaces(5).
 | |
| 
 | |
| # The loopback network interface
 | |
| auto lo
 | |
| iface lo inet loopback
 | |
| "
 | |
|         fi
 | |
| 
 | |
|         # configurer chaque bridge
 | |
|         local -a tmpifaces
 | |
|         local tmpbr
 | |
|         for confip in "${confbrips[@]}"; do
 | |
|             __old_network_parse_confip "$confip" br ipspecs
 | |
|             [ -n "$br" ] || br="$mainbr"
 | |
| 
 | |
|             if [ -z "$mainip" -a "$br" == "$mainiface" ]; then
 | |
|                 __old_network_parse_ipspec "${ipspecs[0]}" mainip suffix gateway
 | |
|             fi
 | |
| 
 | |
|             ifaces=()
 | |
|             for confbr in "${confbrs[@]}"; do
 | |
|                 __old_network_parse_confbr "$confbr" tmpbr tmpifaces
 | |
|                 if [ "$tmpbr" == "$br" ]; then
 | |
|                     array_copy ifaces tmpifaces
 | |
|                     break
 | |
|                 fi
 | |
|             done
 | |
| 
 | |
|             if __old_network_update_bridge "$interfaces" "$workfile" "$br" ipspecs ifaces; then
 | |
|                 cat "$workfile" >"$interfaces"
 | |
|             fi
 | |
|         done
 | |
| 
 | |
|         # configurer chaque interface classique
 | |
|         for confip in "${confstdips[@]}"; do
 | |
|             __old_network_parse_confip "$confip" iface ipspecs
 | |
|             [ -n "$iface" ] || iface="$mainiface"
 | |
| 
 | |
|             if [ -z "$mainip" -a "$iface" == "$mainiface" ]; then
 | |
|                 __old_network_parse_ipspec "${ipspecs[0]}" mainip suffix gateway
 | |
|             fi
 | |
| 
 | |
|             if __old_network_update_iface "$interfaces" "$workfile" "$iface" ipspecs; then
 | |
|                 cat "$workfile" >"$interfaces"
 | |
|             fi
 | |
|         done
 | |
| 
 | |
|         # Fin de traitement
 | |
|         if [ -n "$__DEBIAN_NETWORK_DEVEL_SHOW_MODIFS" ]; then
 | |
|             if testdiff "$interfaces" /etc/network/interfaces; then
 | |
|                 uecho "Setting /etc/network/interfaces to:"
 | |
|                 cat "$interfaces" | sed 's/^/    /g' 1>&2
 | |
|             else
 | |
|                 uecho "/etc/network/interfaces: pas de modifications"
 | |
|             fi
 | |
|         elif testdiff "$interfaces" /etc/network/interfaces; then
 | |
|             __old_network_backup /etc/network/interfaces
 | |
|             if show_debug; then
 | |
|                 edebug "Setting /etc/network/interfaces to:"
 | |
|                 cat "$interfaces" | sed 's/^/    /g' 1>&2
 | |
|             else
 | |
|                 estep /etc/network/interfaces
 | |
|             fi
 | |
|             cat "$interfaces" >/etc/network/interfaces
 | |
|         fi
 | |
|         ac_clean "$interfaces" "$workfile"
 | |
| 
 | |
|         eend
 | |
|     fi
 | |
| 
 | |
|     # configurer le nom d'hôte
 | |
|     if [ -n "$host" ]; then
 | |
|         host="$(ipcalc_fqdn_maybe "$host")"
 | |
|         etitle "Configuration du nom d'hôte: $host"
 | |
| 
 | |
|         # si on ne spécifie pas l'adresse ip principale, la résoudre ici. ceci
 | |
|         # est nécessaire pour mettre à jour /etc/hosts
 | |
|         if [ "$mainip" == dhcp ]; then
 | |
|             # si l'adresse obtenue est en DHCP, ne pas mettre à jour /etc/hosts
 | |
|             mainip=
 | |
|         elif [ -z "$mainip" ]; then
 | |
|             local -a mainips
 | |
|             resolv_ips mainips "$host"
 | |
|             [ -n "${mainips[*]}" ] || {
 | |
|                 eerror "$host: Impossible de résoudre ce nom d'hôte"
 | |
|                 return 1
 | |
|             }
 | |
|             mainip="${mainips[0]}"
 | |
|         fi
 | |
| 
 | |
|         __old_network_hostname "$host"
 | |
|         __old_network_mailname "$host"
 | |
|         if [ -n "$mainip" ]; then
 | |
|             __old_network_hosts "$host" "$mainip"
 | |
|         fi
 | |
| 
 | |
|         eend
 | |
|     fi
 | |
| 
 | |
|     return 0
 | |
| }
 |