summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiloslav Trmac <mitr@volny.cz>2006-05-14 01:34:30 +0000
committerMiloslav Trmac <mitr@volny.cz>2006-05-14 01:34:30 +0000
commit599631bda00f650cdfc6e3f39d112efc0bb7c6bf (patch)
treef2139af6236f9fc2027276b8cf3994f0d9cb8aff
parent906238c891d3f02b4c706457783694e3a01d91ce (diff)
Modify IPV6TO4_ROUTING to also add addresses on the interfaces
Add RFC 3041 (IPv6 privacy) support (patch for both by Peter Bieringer <pb@bieringer.de>)
-rw-r--r--ppp/ip-down.ipv6to459
-rw-r--r--ppp/ip-up.ipv6to428
-rw-r--r--sysconfig.txt9
-rwxr-xr-xsysconfig/network-scripts/ifdown-ipv684
-rwxr-xr-xsysconfig/network-scripts/ifdown-sit12
-rwxr-xr-xsysconfig/network-scripts/ifup-ipv632
-rwxr-xr-xsysconfig/network-scripts/ifup-sit15
-rw-r--r--sysconfig/network-scripts/network-functions-ipv635
8 files changed, 165 insertions, 109 deletions
diff --git a/ppp/ip-down.ipv6to4 b/ppp/ip-down.ipv6to4
index 9ca7250a..850005d8 100644
--- a/ppp/ip-down.ipv6to4
+++ b/ppp/ip-down.ipv6to4
@@ -4,12 +4,12 @@
#
#
# Taken from:
-# (P) & (C) 2000-2002 by Peter Bieringer <pb@bieringer.de>
+# (P) & (C) 2000-2005 by Peter Bieringer <pb@bieringer.de>
#
-# You will find more information in the IPv6-HowTo for Linux at
-# http://www.bieringer.de/linux/IPv6/
+# You will find more information on the initscripts-ipv6 homepage at
+# http://www.deepspace6.net/projects/initscripts-ipv6.html
#
-# Version 2002-10-30
+# Version 2005-09-22
#
# Calling parameters:
# $1: interface name
@@ -26,7 +26,7 @@
#
# Uses following information from /etc/sysconfig/network-scripts/ifcfg-$1:
# IPV6TO4INIT=yes|no: controls configuration
-# IPV6TO4_ROUTING="<device>-<suffix>/<prefix length> ...": information to setup local subnetting
+# IPV6TO4_ROUTING="<device>-<suffix>/<prefix length> ...": information to setup additional interfaces
#
# IPV6_CONTROL_RADVD=yes|no: controls radvd triggering
# IPV6_RADVD_PIDFILE=<file>: PID file of radvd for sending signals, default is "/var/run/radvd/radvd.pid"
@@ -40,11 +40,11 @@ if [ -z "$1" ]; then
fi
# Get global network configuration
-. /etc/sysconfig/network
+. /etc/sysconfig/network
# Source IPv4 helper functions
cd /etc/sysconfig/network-scripts
-. network-functions
+. network-functions
CONFIG=$1
[ -f "$CONFIG" ] || CONFIG=ifcfg-$CONFIG
@@ -67,33 +67,38 @@ fi
# Run basic IPv6 test, if not ok, skip IPv6 initialization
ipv6_test testonly || exit 0
-# Test device status
+# Test status of ppp device
ipv6_test_device_status $DEVICE
if [ $? != 0 -a $? != 11 ]; then
# device doesn't exist or other problem occurs
exit 1
fi
-# Shutdown of 6to4, if configured
-valid6to4config="yes"
-
-# Get IPv4 address from interface
-ipv4addr="`ipv6_get_ipv4addr_of_device $DEVICE`"
-if [ -z "$ipv4addr" ]; then
- # Has no IPv4 address
- valid6to4config="no"
-fi
+# Test status of tun6to4 device
+ipv6_test_device_status tun6to4
+if [ $? = 0 -o $? = 11 ]; then
+ # Device exists
+ valid6to4config="yes"
+
+ # Get IPv4 address from interface
+ ipv4addr="`ipv6_get_ipv4addr_of_device $DEVICE`"
+ if [ -z "$ipv4addr" ]; then
+ # Has no IPv4 address
+ valid6to4config="no"
+ fi
-# Get local IPv4 address of dedicated tunnel
-ipv4addr6to4local="`ipv6_get_ipv4addr_of_tunnel tun6to4 local`"
+ # Get local IPv4 address of dedicated tunnel
+ ipv4addr6to4local="`ipv6_get_ipv4addr_of_tunnel tun6to4 local`"
-# IPv6to4 not enabled on this interface?
-if [ $IPV6TO4INIT != "yes" ]; then
- # Check against configured 6to4 tunnel to see if this interface was regardless used before
- if [ "$ipv4addr" != "$ipv4addr6to4local" ]; then
- # IPv4 address of interface does't match local tunnel address, interface was not used for current 6to4 setup
- valid6to4config="no"
+ # IPv6to4 not enabled on this interface?
+ if [ $IPV6TO4INIT != "yes" ]; then
+ # Check against configured 6to4 tunnel to see if this interface was regardless used before
+ if [ "$ipv4addr" != "$ipv4addr6to4local" ]; then
+ # IPv4 address of interface does't match local tunnel address, interface was not used for current 6to4 setup
+ valid6to4config="no"
+ fi
fi
+
fi
if [ "$valid6to4config" = "yes" ]; then
@@ -106,10 +111,10 @@ if [ "$valid6to4config" = "yes" ]; then
# Delete routes to local networks
for devsuf in $IPV6TO4_ROUTING; do
dev="`echo $devsuf | awk -F- '{ print $1 }'`"
- ipv6_cleanup_routes $dev ::
+ ipv6_cleanup_6to4_device $dev
done
fi
# Delete all configured 6to4 address
- ipv6_cleanup_6to4_tunnels tun6to4
+ ipv6_cleanup_6to4_tunnels tun6to4
fi
diff --git a/ppp/ip-up.ipv6to4 b/ppp/ip-up.ipv6to4
index 609e02c4..26f6f671 100644
--- a/ppp/ip-up.ipv6to4
+++ b/ppp/ip-up.ipv6to4
@@ -4,12 +4,12 @@
#
#
# Taken from:
-# (P) & (C) 2000-2002 by Peter Bieringer <pb@bieringer.de>
-#
-# You will find more information in the IPv6-HowTo for Linux at
-# http://www.bieringer.de/linux/IPv6/
+# (P) & (C) 2000-2005 by Peter Bieringer <pb@bieringer.de>
#
-# Version 2002-11-14
+# You will find more information on the initscripts-ipv6 homepage at
+# http://www.deepspace6.net/projects/initscripts-ipv6.html
+#
+# Version 2005-09-22
#
# Calling parameters:
# $1: interface name
@@ -28,8 +28,8 @@
# IPV6TO4_IPV4ADDR=<IPv4 address>: special local address for 6to4 tunneling (only needed behind a NAT gateway)
# IPV6TO4_RELAY=<IPv4 address>: remote 6to4 relay router address (default: 192.88.99.1)
# IPV6TO4_MTU=<MTU for IPv6>: controls IPv6 MTU for the 6to4 link (optional, default is MTU of interface - 20)
-# IPV6TO4_ROUTING="<device>-<suffix>/<prefix length> ...": information to setup local subnetting
-# Example: IPV6TO4_ROUTING="eth0-:f101::0/64 eth1-:f102::0/64"
+# IPV6TO4_ROUTING="<device>-<suffix>/<prefix length> ...": information to setup additional interfaces
+# Example: IPV6TO4_ROUTING="eth0-:f101::1/64 eth1-:f102::1/64"
#
# IPV6_CONTROL_RADVD=yes|no: controls radvd triggering
# IPV6_RADVD_PIDFILE=<file>: PID file of radvd for sending signals, default is "/var/run/radvd/radvd.pid"
@@ -63,9 +63,9 @@ REALDEVICE=`echo ${DEVICE} | sed 's/:.*//g'`
# Test whether IPv6 should be configured, else stop
[ "$NETWORKING_IPV6" = "yes" ] || exit 0
-if [ ! -f /etc/sysconfig/network-scripts/network-functions-ipv6 ]; then
- exit 1
-fi
+if [ ! -f /etc/sysconfig/network-scripts/network-functions-ipv6 ]; then
+ exit 1
+fi
. /etc/sysconfig/network-scripts/network-functions-ipv6
@@ -106,12 +106,12 @@ if [ "$IPV6TO4INIT" = "yes" ]; then
if [ $? -ne 0 ]; then
valid6to4config="no"
fi
-
+
if [ "$valid6to4config" = "yes" ]; then
# Delete routes to local networks
for devsuf in $IPV6TO4_ROUTING; do
dev="`echo $devsuf | awk -F- '{ print $1 }'`"
- ipv6_cleanup_routes $dev ::
+ ipv6_cleanup_6to4_device $dev
done
# Cleanup all old data (needed, if "ip-down.ipv6to4" wasn't executed), delete all configured 6to4 address
@@ -182,11 +182,11 @@ if [ "$IPV6TO4INIT" = "yes" ]; then
# Generate 6to4 address
ipv6to4prefix="`ipv6_create_6to4_prefix $ipv4addr`"
if [ -n "$ipv6to4prefix" ]; then
- # Add route to local networks
+ # Add IPv6 address to interface (required interface route will be set automatically)
for devsuf in $IPV6TO4_ROUTING; do
dev="`echo $devsuf | awk -F- '{ print $1 }'`"
suf="`echo $devsuf | awk -F- '{ print $2 }'`"
- ipv6_add_route ${ipv6to4prefix}$suf :: $dev
+ ipv6_add_addr_on_device ${dev} ${ipv6to4prefix}${suf}
done
else
echo $"Error occured while calculating the IPv6to4 prefix"
diff --git a/sysconfig.txt b/sysconfig.txt
index ff136580..1b224d9c 100644
--- a/sysconfig.txt
+++ b/sysconfig.txt
@@ -590,6 +590,9 @@ Files in /etc/sysconfig/network-scripts/
Note: Must be greater or equal to 1280.
Example:
IPV6_MTU="1280"
+ IPV6_PRIVACY=rfc3041
+ Enables RFC 3041 IPv6 privacy support if set.
+ Default: RFC 3041 support disabled
Special configuration options for multi-homed hosts etc.
IPV6_ROUTER=yes|no: Controls IPv6 autoconfiguration
@@ -615,10 +618,10 @@ Files in /etc/sysconfig/network-scripts/
IPV6TO4_MTU="1280"
Default: MTU of master device - 20
IPV6TO4_ROUTING="<device>-<suffix>/<prefix length> ..." (optional)
- A list of routing tokens to setup proper IPv6 routes on the LAN
+ A list of routing tokens to setup proper IPv6 interfaces on the LAN
Example:
- IPV6TO4_ROUTING="eth0-:0004::0/64 eth1-:0005::0/64"
- Will create one route per eth0 and eth1, taking given SLA
+ IPV6TO4_ROUTING="eth0-:0004::1/64 eth1-:0005::1/64"
+ Will create one address per eth0 and eth1, taking given SLA
Optional settings for a 6to4 tunnel or a ppp link
IPV6_CONTROL_RADVD=yes|no (optional)
diff --git a/sysconfig/network-scripts/ifdown-ipv6 b/sysconfig/network-scripts/ifdown-ipv6
index b9120134..64ee246d 100755
--- a/sysconfig/network-scripts/ifdown-ipv6
+++ b/sysconfig/network-scripts/ifdown-ipv6
@@ -11,7 +11,7 @@
#
# RHL integration assistance by Pekka Savola <pekkas@netcore.fi>
#
-# Version 2004-09-02
+# Version 2005-09-22
#
# Note: if called as (like normally) by /etc/sysconfig/network-scripts/ifdown
# exit codes aren't handled by "ifdown"
@@ -25,7 +25,7 @@
#
# Optional for 6to4 tunneling:
# IPV6TO4_RELAY=<IPv4 address>: IPv4 address of the remote 6to4 relay [default: 192.88.99.1]
-# IPV6TO4_ROUTING="<device>-<suffix>/<prefix length> ...": information to setup local subnetting
+# IPV6TO4_ROUTING="<device>-<suffix>/<prefix length> ...": information to setup internal interfaces
#
# Optional for 6to4 tunneling links to trigger radvd:
# IPV6_CONTROL_RADVD=yes|no: controls radvd triggering [optional]
@@ -39,10 +39,10 @@
#
-. /etc/sysconfig/network
+. /etc/sysconfig/network
cd /etc/sysconfig/network-scripts
-. network-functions
+. network-functions
CONFIG=$1
[ -f "$CONFIG" ] || CONFIG=ifcfg-$CONFIG
@@ -74,45 +74,51 @@ ipv6_exec_sysctl -w net.ipv6.conf.$DEVICE.forwarding=0 >/dev/null 2>&1
ipv6_exec_sysctl -w net.ipv6.conf.$DEVICE.accept_ra=0 >/dev/null 2>&1
ipv6_exec_sysctl -w net.ipv6.conf.$DEVICE.accept_redirects=0 >/dev/null 2>&1
-# Shutdown of 6to4, if configured
-valid6to4config="yes"
-if [ -z "$IPV6TO4_RELAY" ]; then
- IPV6TO4_RELAY="192.88.99.1"
-fi
-
-# Get IPv4 address from interface
-if [ -n "$IPV6TO4_IPV4ADDR" ]; then
- # Take special configured from config file (precedence 1)
- ipv4addr="$IPV6TO4_IPV4ADDR"
+# Test status of tun6to4 device
+ipv6_test_device_status tun6to4
+if [ $? = 0 -o $? = 11 ]; then
+ # Device exists
+ valid6to4config="yes"
- # Get IPv4 address from interface first
- ipv4addrlocal="`ipv6_get_ipv4addr_of_device $DEVICE`"
- if [ -z "$ipv4addrlocal" ]; then
- # Take configured from config file
- ipv4addrlocal="$IPADDR"
+ if [ -z "$IPV6TO4_RELAY" ]; then
+ IPV6TO4_RELAY="192.88.99.1"
fi
-else
- # Get IPv4 address from interface first (has precedence 2)
- ipv4addr="`ipv6_get_ipv4addr_of_device $DEVICE`"
- if [ -z "$ipv4addr" ]; then
- # Take configured from config file (precedence 3)
- ipv4addr="$IPADDR"
+
+ # Get IPv4 address from interface
+ if [ -n "$IPV6TO4_IPV4ADDR" ]; then
+ # Take special configured from config file (precedence 1)
+ ipv4addr="$IPV6TO4_IPV4ADDR"
+
+ # Get IPv4 address from interface first
+ ipv4addrlocal="`ipv6_get_ipv4addr_of_device $DEVICE`"
+ if [ -z "$ipv4addrlocal" ]; then
+ # Take configured from config file
+ ipv4addrlocal="$IPADDR"
+ fi
+ else
+ # Get IPv4 address from interface first (has precedence 2)
+ ipv4addr="`ipv6_get_ipv4addr_of_device $DEVICE`"
+ if [ -z "$ipv4addr" ]; then
+ # Take configured from config file (precedence 3)
+ ipv4addr="$IPADDR"
+ fi
+ ipv4addrlocal="$ipv4addr"
fi
- ipv4addrlocal="$ipv4addr"
-fi
-# Get local IPv4 address of dedicated tunnel
-ipv4addr6to4local="`ipv6_get_ipv4addr_of_tunnel tun6to4 local`"
+ # Get local IPv4 address of dedicated tunnel
+ ipv4addr6to4local="`ipv6_get_ipv4addr_of_tunnel tun6to4 local`"
-if [ -z "$ipv4addrlocal" -o -z "$ipv4addr6to4local" ]; then
- # no IPv4 addresses given, 6to4 sure not configured
- valid6to4config="no"
-else
- # Check against configured 6to4 tunnel to see if this interface was used before
- if [ "$ipv4addrlocal" != "$ipv4addr6to4local" ]; then
- # IPv4 address of interface does't match local tunnel address, interface was not used for current 6to4 setup
+ if [ -z "$ipv4addrlocal" -o -z "$ipv4addr6to4local" ]; then
+ # no IPv4 addresses given, 6to4 sure not configured
valid6to4config="no"
+ else
+ # Check against configured 6to4 tunnel to see if this interface was used before
+ if [ "$ipv4addrlocal" != "$ipv4addr6to4local" ]; then
+ # IPv4 address of interface does't match local tunnel address, interface was not used for current 6to4 setup
+ valid6to4config="no"
+ fi
fi
+
fi
# Shutdown of 6to4, if configured
@@ -121,16 +127,16 @@ if [ "$valid6to4config" = "yes" ]; then
# Delete routes to local networks
for devsuf in $IPV6TO4_ROUTING; do
dev="`echo $devsuf | awk -F- '{ print $1 }'`"
- ipv6_cleanup_routes $dev ::
+ ipv6_cleanup_6to4_device $dev
done
fi
# Delete all configured 6to4 address
- ipv6_cleanup_6to4_tunnels tun6to4
+ ipv6_cleanup_6to4_tunnels tun6to4
# Control running radvd
ipv6_trigger_radvd down "$IPV6_RADVD_TRIGGER_ACTION" $IPV6_RADVD_PIDFILE
fi
-# Delete all current configured IPv6 addresses on this interface
+# Delete all current configured IPv6 addresses on this interface
ipv6_cleanup_device $DEVICE
diff --git a/sysconfig/network-scripts/ifdown-sit b/sysconfig/network-scripts/ifdown-sit
index 2059f96a..36603177 100755
--- a/sysconfig/network-scripts/ifdown-sit
+++ b/sysconfig/network-scripts/ifdown-sit
@@ -4,10 +4,10 @@
#
#
# Taken from:
-# (P) & (C) 2000-2002 by Peter Bieringer <pb@bieringer.de>
+# (P) & (C) 2000-2003 by Peter Bieringer <pb@bieringer.de>
#
-# You will find more information in the IPv6-HowTo for Linux at
-# http://www.bieringer.de/linux/IPv6/
+# You will find more information on the initscripts-ipv6 homepage at
+# http://www.deepspace6.net/projects/initscripts-ipv6.html
#
# RHL integration assistance by Pekka Savola <pekkas@netcore.fi>
#
@@ -21,10 +21,10 @@
#
-. /etc/sysconfig/network
+. /etc/sysconfig/network
cd /etc/sysconfig/network-scripts
-. network-functions
+. network-functions
CONFIG=$1
[ -f "$CONFIG" ] || CONFIG=ifcfg-$CONFIG
@@ -58,4 +58,4 @@ if [ $? != 0 -a $? != 11 ]; then
fi
# Cleanup and shut down IPv6-in-IPv4 tunnel device
-ipv6_del_tunnel_device $DEVICE
+ipv6_del_tunnel_device $DEVICE
diff --git a/sysconfig/network-scripts/ifup-ipv6 b/sysconfig/network-scripts/ifup-ipv6
index 6ff924b0..8762129f 100755
--- a/sysconfig/network-scripts/ifup-ipv6
+++ b/sysconfig/network-scripts/ifup-ipv6
@@ -4,17 +4,17 @@
#
#
# Taken from:
-# (P) & (C) 2000-2004 by Peter Bieringer <pb@bieringer.de>
+# (P) & (C) 2000-2005 by Peter Bieringer <pb@bieringer.de>
#
# You will find more information on the initscripts-ipv6 homepage at
# http://www.deepspace6.net/projects/initscripts-ipv6.html
#
# RHL integration assistance by Pekka Savola <pekkas@netcore.fi>
#
-# Version 2004-03-21
+# Version 2005-09-22
#
# Note: if called (like normally) by /etc/sysconfig/network-scripts/ifup
-# exit codes aren't handled by "ifup"
+# exit codes aren't handled by "ifup"
#
# Uses following information from "/etc/sysconfig/network":
# NETWORKING_IPV6=yes|no: controls IPv6 initialization (global setting)
@@ -31,14 +31,16 @@
# IPV6FORWARDING=yes: IPV6_AUTOCONF=no, IPV6_ROUTER=yes
# IPV6FORWARDING=no: IPV6_AUTOCONF=yes
# IPV6_MTU=<MTU for IPv6>: controls IPv6 MTU for this link (optional)
+# IPV6_PRIVACY="rfc3041": control IPv6 privacy (optional)
+# This script only supports "rfc3041" (if kernel supports it)
#
# Optional for 6to4 tunneling (hardwired name of tunnel device is "tun6to4"):
# IPV6TO4INIT=yes|no: controls 6to4 tunneling setup
# IPV6TO4_RELAY=<IPv4 address>: IPv4 address of the remote 6to4 relay (default: 192.88.99.1)
# IPV6TO4_MTU=<MTU for IPv6>: controls IPv6 MTU for the 6to4 link (optional, default is MTU of interface - 20)
# IPV6TO4_IPV4ADDR=<IPv4 address>: overwrite local IPv4 address (optional)
-# IPV6TO4_ROUTING="<device>-<suffix>/<prefix length> ...": information to setup local subnetting
-# Example: IPV6TO4_ROUTING="eth0-:f101::0/64 eth1-:f102::0/64"
+# IPV6TO4_ROUTING="<device>-<suffix>/<prefix length> ...": information to setup additional interfaces
+# Example: IPV6TO4_ROUTING="eth0-:f101::1/64 eth1-:f102::1/64"
#
# Optional for 6to4 tunneling to trigger radvd:
# IPV6_CONTROL_RADVD=yes|no: controls radvd triggering (optional)
@@ -48,18 +50,18 @@
# Required version of radvd to use 6to4 prefix recalculation
# 0.6.2p3 or newer supporting option "Base6to4Interface"
# Required version of radvd to use dynamic ppp links
-# 0.7.0 + fixes or newer
+# 0.7.0 + fixes or newer
#
-. /etc/sysconfig/network
+. /etc/sysconfig/network
cd /etc/sysconfig/network-scripts
-. network-functions
+. network-functions
CONFIG=$1
[ -f "$CONFIG" ] || CONFIG=ifcfg-$CONFIG
-source_config
+source_config
# IPv6 don't need aliases anymore, config is skipped
REALDEVICE=`echo ${DEVICE} | sed 's/:.*//g'`
@@ -83,7 +85,7 @@ ipv6_test_device_status $DEVICE
if [ $? != 0 -a $? != 11 ]; then
# device doesn't exist or other problem occurs
exit 1
-fi
+fi
# Setup IPv6 address on specified interface
if ! [ -z "$IPV6ADDR" ]; then
@@ -142,6 +144,14 @@ if [ -n "$IPV6ADDR_SECONDARIES" ]; then
done
fi
+# Enable IPv6 RFC3041 privacy extensions if desired
+if [ "$IPV6_PRIVACY" = "rfc3041" ]; then
+ ipv6_exec_sysctl -w net.ipv6.conf.$DEVICE.use_tempaddr=2 >/dev/null 2>&1
+ if [ $? -ne 0 ]; then
+ echo $"Cannot enable IPv6 privacy method '$IPV6_PRIVACY', not supported by kernel"
+ fi
+fi
+
# Setup default IPv6 route, check are done by function
if [ -n "$IPV6_DEFAULTDEV" -o -n "$IPV6_DEFAULTGW" ]; then
ipv6_set_default_route "$IPV6_DEFAULTGW" "$IPV6_DEFAULTDEV" "$DEVICE"
@@ -284,7 +294,7 @@ if [ "$IPV6TO4INIT" = "yes" ]; then
for devsuf in $IPV6TO4_ROUTING; do
dev="`echo $devsuf | awk -F- '{ print $1 }'`"
suf="`echo $devsuf | awk -F- '{ print $2 }'`"
- ipv6_add_route ${ipv6to4prefix}$suf :: $dev
+ ipv6_add_addr_on_device ${dev} ${ipv6to4prefix}${suf}
done
else
echo $"Error occurred while calculating the IPv6to4 prefix"
diff --git a/sysconfig/network-scripts/ifup-sit b/sysconfig/network-scripts/ifup-sit
index 80d5b963..4fa78eb5 100755
--- a/sysconfig/network-scripts/ifup-sit
+++ b/sysconfig/network-scripts/ifup-sit
@@ -4,11 +4,14 @@
#
#
# Taken from:
-# (P) & (C) 2000-2002 by Peter Bieringer <pb@bieringer.de>
+# (P) & (C) 2000-2003 by Peter Bieringer <pb@bieringer.de>
+#
+# You will find more information on the initscripts-ipv6 homepage at
+# http://www.deepspace6.net/projects/initscripts-ipv6.html
#
# RHL integration assistance by Pekka Savola <pekkas@netcore.fi>
#
-# Version 2002-12-04
+# Version 2003-09-08
#
# Uses following information from /etc/sysconfig/network:
# NETWORKING_IPV6=yes|no: controls IPv6 initialization (global setting)
@@ -19,8 +22,8 @@
# DEVICE=<device>
# IPV6INIT=yes|no: controls IPv6 configuration for this interface
# IPV6_MTU=<MTU for IPv6>: controls IPv6 MTU for this link (optional)
-#
-# For static tunnels
+#
+# For static tunnels
# IPV6TUNNELIPV4=<IPv4 address>: IPv4 address of remote tunnel endpoint
# IPV6TUNNELIPV4LOCAL=<IPv4 address>: (optional) local IPv4 address of tunnel
# IPV6ADDR=<IPv6 address>[/<prefix length>]: (optional) local IPv6 address of a numbered tunnel
@@ -28,10 +31,10 @@
#
-. /etc/sysconfig/network
+. /etc/sysconfig/network
cd /etc/sysconfig/network-scripts
-. network-functions
+. network-functions
CONFIG=$1
[ -f "$CONFIG" ] || CONFIG=ifcfg-$CONFIG
diff --git a/sysconfig/network-scripts/network-functions-ipv6 b/sysconfig/network-scripts/network-functions-ipv6
index 153c05ea..5ee91187 100644
--- a/sysconfig/network-scripts/network-functions-ipv6
+++ b/sysconfig/network-scripts/network-functions-ipv6
@@ -8,7 +8,7 @@
# You will find more information on the initscripts-ipv6 homepage at
# http://www.deepspace6.net/projects/initscripts-ipv6.html
#
-# Version: 2005-03-03
+# Version: 2005-09-22
#
#
@@ -419,12 +419,12 @@ ipv6_cleanup_routes() {
if [ -n "$gatewaymatch" ]; then
# Get all IPv6 routes (except default link-local and multicast) through given interface via a given gateway and remove them
ipv6_exec_ip -6 route show dev $device via $gatewaymatch | LC_ALL=C grep -v -w expires | LC_ALL=C egrep -v "^fe80::/64|^ff00::/8" | while read ipv6net dummy; do
- ipv6_exec_ip -6 route del $ipv6net via $gatewaymatch dev $iface
+ ipv6_del_route $ipv6net $gatewaymatch $device
done
else
# Get all IPv6 routes (except default link-local and multicast) through given interface and remove them
ipv6_exec_ip -6 route show dev $device | LC_ALL=C grep -v -w expires | LC_ALL=C egrep -v "^fe80::/64|^ff00::/8" | while read ipv6net dummy; do
- ipv6_exec_ip -6 route del $ipv6net dev $iface
+ ipv6_del_route $ipv6net :: $device
done
fi
@@ -594,6 +594,35 @@ ipv6_cleanup_device() {
}
+## Remove all IPv6 6to4 related routes and addresses on given interface
+# $1: <Interface>
+# return code: 0=ok 1=argument error 2=IPv6 test fails 3=major problem
+ipv6_cleanup_6to4_device() {
+ local fn="ipv6_cleanup_6to4_device"
+
+ local device=$1
+
+ if [ -z "$device" ]; then
+ ipv6_log $"Missing parameter 'device' (arg 1)" err $fn
+ return 1
+ fi
+
+ ipv6_test testonly || return 2
+
+ # Cleanup 6to4 addresses on this device
+ ipv6_exec_ip -6 addr show dev $dev scope global permanent | LC_ALL=C grep -w inet6 | awk '{ print $2}' | LC_ALL=C grep "^2002:" | while read addr; do
+ ipv6_del_addr_on_device ${dev} ${addr}
+ done
+
+ # Get all IPv6 routes through given interface via a given gateway and remove them
+ ipv6_exec_ip -6 route show dev $device | LC_ALL=C grep "^2002:" | while read ipv6net dummy; do
+ ipv6_del_route $ipv6net :: $device
+ done
+
+ return 0
+}
+
+
## Remove an IPv6 address on given interface
# $1: <Interface>
# $2: <IPv6 address>
9'>1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378
/*
   SSSD

   Proxy Module

   Copyright (C) Simo Sorce <ssorce@redhat.com>	2008-2009

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <nss.h>
#include <errno.h>
#include <pwd.h>
#include <grp.h>
#include <dlfcn.h>

#include <security/pam_appl.h>
#include <security/pam_modules.h>

#include "util/util.h"
#include "providers/dp_backend.h"
#include "db/sysdb.h"

struct proxy_nss_ops {
    enum nss_status (*getpwnam_r)(const char *name, struct passwd *result,
                                  char *buffer, size_t buflen, int *errnop);
    enum nss_status (*getpwuid_r)(uid_t uid, struct passwd *result,
                                  char *buffer, size_t buflen, int *errnop);
    enum nss_status (*setpwent)(void);
    enum nss_status (*getpwent_r)(struct passwd *result,
                                  char *buffer, size_t buflen, int *errnop);
    enum nss_status (*endpwent)(void);

    enum nss_status (*getgrnam_r)(const char *name, struct group *result,
                                  char *buffer, size_t buflen, int *errnop);
    enum nss_status (*getgrgid_r)(gid_t gid, struct group *result,
                                  char *buffer, size_t buflen, int *errnop);
    enum nss_status (*setgrent)(void);
    enum nss_status (*getgrent_r)(struct group *result,
                                  char *buffer, size_t buflen, int *errnop);
    enum nss_status (*endgrent)(void);
    enum nss_status (*initgroups_dyn)(const char *user, gid_t group,
                                      long int *start, long int *size,
                                      gid_t **groups, long int limit,
                                      int *errnop);
};

struct proxy_ctx {
    struct be_ctx *be;
    struct proxy_nss_ops ops;
};

struct proxy_auth_ctx {
    struct be_ctx *be;
    char *pam_target;
};

struct authtok_conv {
    uint32_t authtok_size;
    uint8_t *authtok;
};

static int proxy_internal_conv(int num_msg, const struct pam_message **msgm,
                            struct pam_response **response,
                            void *appdata_ptr) {
    int i;
    struct pam_response *reply;
    struct authtok_conv *auth_data;

    auth_data = talloc_get_type(appdata_ptr, struct authtok_conv);

    if (num_msg <= 0) return PAM_CONV_ERR;

    reply = (struct pam_response *) calloc(num_msg,
                                           sizeof(struct pam_response));
    if (reply == NULL) return PAM_CONV_ERR;

    for (i=0; i < num_msg; i++) {
        switch( msgm[i]->msg_style ) {
            case PAM_PROMPT_ECHO_OFF:
                DEBUG(4, ("Conversation message: [%s]\n", msgm[i]->msg));
                reply[i].resp_retcode = 0;
                reply[i].resp = calloc(auth_data->authtok_size + 1,
                                       sizeof(char));
                if (reply[i].resp == NULL) goto failed;
                memcpy(reply[i].resp, auth_data->authtok, auth_data->authtok_size);

                break;
            default:
                DEBUG(1, ("Conversation style %d not supported.\n",
                           msgm[i]->msg_style));
                goto failed;
        }
    }

    *response = reply;
    reply = NULL;

    return PAM_SUCCESS;

failed:
    free(reply);
    return PAM_CONV_ERR;
}

static void proxy_pam_handler_cache_done(struct tevent_req *treq);
static void proxy_reply(struct be_req *req,
                        int error, const char *errstr);

static void proxy_pam_handler(struct be_req *req) {
    int ret;
    int pam_status;
    pam_handle_t *pamh=NULL;
    struct authtok_conv *auth_data;
    struct pam_conv conv;
    struct pam_data *pd;
    struct proxy_auth_ctx *ctx;;
    bool cache_auth_data = false;

    ctx = talloc_get_type(req->be_ctx->bet_info[BET_AUTH].pvt_bet_data, struct proxy_auth_ctx);
    pd = talloc_get_type(req->req_data, struct pam_data);

    conv.conv=proxy_internal_conv;
    auth_data = talloc_zero(req, struct authtok_conv);
    conv.appdata_ptr=auth_data;

    ret = pam_start(ctx->pam_target, pd->user, &conv, &pamh);
    if (ret == PAM_SUCCESS) {
        DEBUG(1, ("Pam transaction started.\n"));
        pam_set_item(pamh, PAM_TTY, pd->tty);
        if (ret != PAM_SUCCESS) {
            DEBUG(1, ("Setting PAM_TTY failed: %s.\n", pam_strerror(pamh, ret)));
        }
        pam_set_item(pamh, PAM_RUSER, pd->ruser);
        if (ret != PAM_SUCCESS) {
            DEBUG(1, ("Setting PAM_RUSER failed: %s.\n", pam_strerror(pamh, ret)));
        }
        pam_set_item(pamh, PAM_RHOST, pd->rhost);
        if (ret != PAM_SUCCESS) {
            DEBUG(1, ("Setting PAM_RHOST failed: %s.\n", pam_strerror(pamh, ret)));
        }
        switch (pd->cmd) {
            case SSS_PAM_AUTHENTICATE:
                auth_data->authtok_size = pd->authtok_size;
                auth_data->authtok = pd->authtok;
                pam_status = pam_authenticate(pamh, 0);
                if ((pam_status == PAM_SUCCESS) &&
                    (req->be_ctx->domain->cache_credentials)) {
                    cache_auth_data = true;
                }
                break;
            case SSS_PAM_SETCRED:
                pam_status=pam_setcred(pamh, 0);
                break;
            case SSS_PAM_ACCT_MGMT:
                pam_status=pam_acct_mgmt(pamh, 0);
                break;
            case SSS_PAM_OPEN_SESSION:
                pam_status=pam_open_session(pamh, 0);
                break;
            case SSS_PAM_CLOSE_SESSION:
                pam_status=pam_close_session(pamh, 0);
                break;
            case SSS_PAM_CHAUTHTOK:
                if (pd->priv != 1) {
                    auth_data->authtok_size = pd->authtok_size;
                    auth_data->authtok = pd->authtok;
                    pam_status = pam_authenticate(pamh, 0);
                    if (pam_status != PAM_SUCCESS) break;
                }
                auth_data->authtok_size = pd->newauthtok_size;
                auth_data->authtok = pd->newauthtok;
                pam_status = pam_chauthtok(pamh, 0);
                if ((pam_status == PAM_SUCCESS) &&
                    (req->be_ctx->domain->cache_credentials)) {
                    cache_auth_data = true;
                }
                break;
            default:
                DEBUG(1, ("unknown PAM call"));
                pam_status=PAM_ABORT;
        }

        DEBUG(4, ("Pam result: [%d][%s]\n", pam_status, pam_strerror(pamh, pam_status)));

        if (pam_status == PAM_AUTHINFO_UNAVAIL) {
            be_mark_offline(req->be_ctx);
        }

        ret = pam_end(pamh, pam_status);
        if (ret != PAM_SUCCESS) {
            pamh=NULL;
            DEBUG(1, ("Cannot terminate pam transaction.\n"));
        }

    } else {
        DEBUG(1, ("Failed to initialize pam transaction.\n"));
        pam_status = PAM_SYSTEM_ERR;
    }

    pd->pam_status = pam_status;

    if (cache_auth_data) {
        struct tevent_req *subreq;
        char *password;

        password = talloc_size(req, auth_data->authtok_size + 1);
        if (!password) {
            /* password caching failures are not fatal errors */
            return proxy_reply(req, EOK, NULL);
        }
        memcpy(password, auth_data->authtok, auth_data->authtok_size);
        password[auth_data->authtok_size] = '\0';
        talloc_set_destructor((TALLOC_CTX *)password, password_destructor);

        subreq = sysdb_cache_password_send(req, req->be_ctx->ev,
                                           req->be_ctx->sysdb, NULL,
                                           req->be_ctx->domain,
                                           pd->user, password);
        if (!subreq) {
            /* password caching failures are not fatal errors */
            return proxy_reply(req, EOK, NULL);
        }
        tevent_req_set_callback(subreq, proxy_pam_handler_cache_done, req);
    }

    proxy_reply(req, EOK, NULL);
}

static void proxy_pam_handler_cache_done(struct tevent_req *subreq)
{
    struct be_req *req = tevent_req_callback_data(subreq, struct be_req);
    int ret;

    /* password caching failures are not fatal errors */
    ret = sysdb_cache_password_recv(subreq);
    talloc_zfree(subreq);

    /* so we just log it any return */
    if (ret) {
        DEBUG(2, ("Failed to cache password (%d)[%s]!?\n",
                  ret, strerror(ret)));
    }

    return proxy_reply(req, EOK, NULL);
}

static void proxy_reply(struct be_req *req, int error, const char *errstr)
{
    if (error && !errstr) errstr = "Operation failed";
    return req->fn(req, error, errstr);
}

/* =Common-proxy-tevent_req-utils=========================================*/

#define DEFAULT_BUFSIZE 4096
#define MAX_BUF_SIZE 1024*1024 /* max 1MiB */

struct proxy_state {
    struct tevent_context *ev;
    struct proxy_ctx *ctx;
    struct sysdb_ctx *sysdb;
    struct sss_domain_info *domain;
    const char *name;

    struct sysdb_handle *handle;
    struct passwd *pwd;
    struct group *grp;
    uid_t uid;
    gid_t gid;
};

static void proxy_default_done(struct tevent_req *subreq)
{
    struct tevent_req *req = tevent_req_callback_data(subreq,
                                                      struct tevent_req);
    int ret;

    ret = sysdb_transaction_commit_recv(subreq);
    talloc_zfree(subreq);
    if (ret) {
        tevent_req_error(req, ret);
        return;
    }

    tevent_req_done(req);
}

static int proxy_default_recv(struct tevent_req *req)
{
    enum tevent_req_state tstate;
    uint64_t err = 0;

    if (tevent_req_is_error(req, &tstate, &err)) {
        if (err != 0) return err;
        return EIO;
    }

    return EOK;
}


/* =Getpwnam-wrapper======================================================*/

static void get_pw_name_process(struct tevent_req *subreq);
static void get_pw_name_remove_done(struct tevent_req *subreq);
static void get_pw_name_add_done(struct tevent_req *subreq);

static struct tevent_req *get_pw_name_send(TALLOC_CTX *mem_ctx,
                                           struct tevent_context *ev,
                                           struct proxy_ctx *ctx,
                                           struct sysdb_ctx *sysdb,
                                           struct sss_domain_info *domain,
                                           const char *name)
{
    struct tevent_req *req, *subreq;
    struct proxy_state *state;

    req = tevent_req_create(mem_ctx, &state, struct proxy_state);
    if (!req) return NULL;

    memset(state, 0, sizeof(struct proxy_state));

    state->ev = ev;
    state->ctx = ctx;
    state->sysdb = sysdb;
    state->domain = domain;
    state->name = name;

    subreq = sysdb_transaction_send(state, state->ev, state->sysdb);
    if (!subreq) {
        talloc_zfree(req);
        return NULL;
    }
    tevent_req_set_callback(subreq, get_pw_name_process, req);

    return req;
}

static void get_pw_name_process(struct tevent_req *subreq)
{
    struct tevent_req *req = tevent_req_callback_data(subreq,
                                                      struct tevent_req);
    struct proxy_state *state = tevent_req_data(req,
                                                struct proxy_state);
    struct proxy_ctx *ctx = state->ctx;
    enum nss_status status;
    char *buffer;
    size_t buflen;
    bool delete_user = false;
    int ret;

    DEBUG(7, ("Searching user by name (%s)\n", state->name));

    ret = sysdb_transaction_recv(subreq, state, &state->handle);
    if (ret) {
        tevent_req_error(req, ret);
        return;
    }
    talloc_zfree(subreq);

    state->pwd = talloc(state, struct passwd);
    if (!state->pwd) {
        tevent_req_error(req, ENOMEM);
        return;
    }

    buflen = DEFAULT_BUFSIZE;
    buffer = talloc_size(state, buflen);
    if (!buffer) {
        tevent_req_error(req, ENOMEM);
        return;
    }

    /* FIXME: should we move this call outside the transaction to keep the
     * transaction as short as possible ? */
    status = ctx->ops.getpwnam_r(state->name, state->pwd,
                                 buffer, buflen, &ret);

    switch (status) {
    case NSS_STATUS_NOTFOUND:

        DEBUG(7, ("User %s not found.\n", state->name));
        delete_user = true;
        break;

    case NSS_STATUS_SUCCESS:

        DEBUG(7, ("User %s found: (%s, %d, %d)\n",
                  state->name, state->pwd->pw_name,
                  state->pwd->pw_uid, state->pwd->pw_gid));

        /* uid=0 or gid=0 are invalid values */
        if (state->pwd->pw_uid == 0 || state->pwd->pw_gid == 0) {
            delete_user = true;
            break;
        }

        subreq = sysdb_store_user_send(state, state->ev, state->handle,
                                       state->domain,
                                       state->pwd->pw_name,
                                       state->pwd->pw_passwd,
                                       state->pwd->pw_uid,
                                       state->pwd->pw_gid,
                                       state->pwd->pw_gecos,
                                       state->pwd->pw_dir,
                                       state->pwd->pw_shell, NULL);
        if (!subreq) {
            tevent_req_error(req, ENOMEM);
            return;
        }
        tevent_req_set_callback(subreq, get_pw_name_add_done, req);
        return;

    case NSS_STATUS_UNAVAIL:
        /* "remote" backend unavailable. Enter offline mode */
        tevent_req_error(req, ENXIO);
        return;

    default:
        DEBUG(2, ("proxy -> getpwnam_r failed for '%s' <%d>\n",
                  state->name, status));
        tevent_req_error(req, EIO);
        return;
    }

    if (delete_user) {
        struct ldb_dn *dn;

        DEBUG(7, ("User %s does not exist (or is invalid) on remote server,"
                  " deleting!\n", state->name));

        dn = sysdb_user_dn(state->sysdb, state,
                           state->domain->name, state->name);
        if (!dn) {
            tevent_req_error(req, ENOMEM);
            return;
        }

        subreq = sysdb_delete_entry_send(state, state->ev, state->handle, dn, true);
        if (!subreq) {
            tevent_req_error(req, ENOMEM);
            return;
        }
        tevent_req_set_callback(subreq, get_pw_name_remove_done, req);
    }
}

static void get_pw_name_add_done(struct tevent_req *subreq)
{
    struct tevent_req *req = tevent_req_callback_data(subreq,
                                                      struct tevent_req);
    struct proxy_state *state = tevent_req_data(req,
                                                struct proxy_state);
    int ret;

    ret = sysdb_store_user_recv(subreq);
    talloc_zfree(subreq);
    if (ret) {
        tevent_req_error(req, ret);
        return;
    }

    subreq = sysdb_transaction_commit_send(state, state->ev, state->handle);
    if (!subreq) {
        tevent_req_error(req, ENOMEM);
        return;
    }
    tevent_req_set_callback(subreq, proxy_default_done, req);
}

static void get_pw_name_remove_done(struct tevent_req *subreq)
{
    struct tevent_req *req = tevent_req_callback_data(subreq,
                                                      struct tevent_req);
    struct proxy_state *state = tevent_req_data(req,
                                                struct proxy_state);
    int ret;

    ret = sysdb_delete_entry_recv(subreq);
    talloc_zfree(subreq);
    if (ret) {
        tevent_req_error(req, ret);
        return;
    }

    subreq = sysdb_transaction_commit_send(state, state->ev, state->handle);
    if (!subreq) {
        tevent_req_error(req, ENOMEM);
        return;
    }
    tevent_req_set_callback(subreq, proxy_default_done, req);
}

/* =Getpwuid-wrapper======================================================*/

static void get_pw_uid_process(struct tevent_req *subreq);
static void get_pw_uid_remove_done(struct tevent_req *subreq);

static struct tevent_req *get_pw_uid_send(TALLOC_CTX *mem_ctx,
                                           struct tevent_context *ev,
                                           struct proxy_ctx *ctx,
                                           struct sysdb_ctx *sysdb,
                                           struct sss_domain_info *domain,
                                           uid_t uid)
{
    struct tevent_req *req, *subreq;
    struct proxy_state *state;

    req = tevent_req_create(mem_ctx, &state, struct proxy_state);
    if (!req) return NULL;

    memset(state, 0, sizeof(struct proxy_state));

    state->ev = ev;
    state->ctx = ctx;
    state->sysdb = sysdb;
    state->domain = domain;
    state->uid = uid;

    subreq = sysdb_transaction_send(state, state->ev, state->sysdb);
    if (!subreq) {
        talloc_zfree(req);
        return NULL;
    }
    tevent_req_set_callback(subreq, get_pw_uid_process, req);

    return req;
}

static void get_pw_uid_process(struct tevent_req *subreq)
{
    struct tevent_req *req = tevent_req_callback_data(subreq,
                                                      struct tevent_req);
    struct proxy_state *state = tevent_req_data(req,
                                                struct proxy_state);
    struct proxy_ctx *ctx = state->ctx;
    enum nss_status status;
    char *buffer;
    size_t buflen;
    bool delete_user = false;
    int ret;

    DEBUG(7, ("Searching user by uid (%d)\n", state->uid));

    ret = sysdb_transaction_recv(subreq, state, &state->handle);
    if (ret) {
        tevent_req_error(req, ret);
        return;
    }
    talloc_zfree(subreq);

    state->pwd = talloc(state, struct passwd);
    if (!state->pwd) {
        tevent_req_error(req, ENOMEM);
        return;
    }

    buflen = DEFAULT_BUFSIZE;
    buffer = talloc_size(state, buflen);
    if (!buffer) {
        tevent_req_error(req, ENOMEM);
        return;
    }

    /* always zero out the pwd structure */
    memset(state->pwd, 0, sizeof(struct passwd));

    status = ctx->ops.getpwuid_r(state->uid, state->pwd,
                                 buffer, buflen, &ret);

    switch (status) {
    case NSS_STATUS_NOTFOUND:

        DEBUG(7, ("User %d not found.\n", state->uid));
        delete_user = true;
        break;

    case NSS_STATUS_SUCCESS:

        DEBUG(7, ("User %d found (%s, %d, %d)\n",
                  state->uid, state->pwd->pw_name,
                  state->pwd->pw_uid, state->pwd->pw_gid));

        /* uid=0 or gid=0 are invalid values */
        if (state->pwd->pw_uid == 0 || state->pwd->pw_gid == 0) {
            delete_user = true;
            break;
        }

        subreq = sysdb_store_user_send(state, state->ev, state->handle,
                                       state->domain,
                                       state->pwd->pw_name,
                                       state->pwd->pw_passwd,
                                       state->pwd->pw_uid,
                                       state->pwd->pw_gid,
                                       state->pwd->pw_gecos,
                                       state->pwd->pw_dir,
                                       state->pwd->pw_shell, NULL);
        if (!subreq) {
            tevent_req_error(req, ENOMEM);
            return;
        }
        tevent_req_set_callback(subreq, get_pw_name_add_done, req);
        return;

    case NSS_STATUS_UNAVAIL:
        /* "remote" backend unavailable. Enter offline mode */
        tevent_req_error(req, ENXIO);
        return;

    default:
        DEBUG(2, ("proxy -> getpwnam_r failed for '%s' <%d>\n",
                  state->name, status));
        tevent_req_error(req, EIO);
        return;
    }

    if (delete_user) {
        DEBUG(7, ("User %d does not exist (or is invalid) on remote server,"
                  " deleting!\n", state->uid));

        subreq = sysdb_delete_user_by_uid_send(state, state->ev,
                                               state->handle,
                                               state->domain,
                                               state->uid,
                                               true);
        if (!subreq) {
            tevent_req_error(req, ENOMEM);
            return;
        }
        tevent_req_set_callback(subreq, get_pw_uid_remove_done, req);
    }
}

static void get_pw_uid_remove_done(struct tevent_req *subreq)
{
    struct tevent_req *req = tevent_req_callback_data(subreq,
                                                      struct tevent_req);
    struct proxy_state *state = tevent_req_data(req,
                                                struct proxy_state);
    int ret;

    ret = sysdb_delete_user_by_uid_recv(subreq);
    talloc_zfree(subreq);
    if (ret) {
        tevent_req_error(req, ret);
        return;
    }

    subreq = sysdb_transaction_commit_send(state, state->ev, state->handle);
    if (!subreq) {
        tevent_req_error(req, ENOMEM);
        return;
    }
    tevent_req_set_callback(subreq, proxy_default_done, req);
}

/* =Getpwent-wrapper======================================================*/

struct enum_users_state {
    struct tevent_context *ev;
    struct proxy_ctx *ctx;
    struct sysdb_ctx *sysdb;
    struct sss_domain_info *domain;
    struct sysdb_handle *handle;

    struct passwd *pwd;

    size_t buflen;
    char *buffer;

    bool in_transaction;
};

static void enum_users_process(struct tevent_req *subreq);

static struct tevent_req *enum_users_send(TALLOC_CTX *mem_ctx,
                                          struct tevent_context *ev,
                                          struct proxy_ctx *ctx,
                                          struct sysdb_ctx *sysdb,
                                          struct sss_domain_info *domain)
{
    struct tevent_req *req, *subreq;
    struct enum_users_state *state;
    enum nss_status status;

    DEBUG(7, ("Enumerating users\n"));

    req = tevent_req_create(mem_ctx, &state, struct enum_users_state);
    if (!req) return NULL;

    state->ev = ev;
    state->ctx = ctx;
    state->sysdb = sysdb;
    state->domain = domain;
    state->handle = NULL;

    state->pwd = talloc(state, struct passwd);
    if (!state->pwd) {
        tevent_req_error(req, ENOMEM);
        goto fail;
    }

    state->buflen = DEFAULT_BUFSIZE;
    state->buffer = talloc_size(state, state->buflen);
    if (!state->buffer) {
        tevent_req_error(req, ENOMEM);
        goto fail;
    }

    state->in_transaction = false;

    status = ctx->ops.setpwent();
    if (status != NSS_STATUS_SUCCESS) {
        tevent_req_error(req, EIO);
        goto fail;
    }

    subreq = sysdb_transaction_send(state, state->ev, state->sysdb);
    if (!subreq) {
        tevent_req_error(req, ENOMEM);
        goto fail;
    }
    tevent_req_set_callback(subreq, enum_users_process, req);

    return req;

fail:
    tevent_req_post(req, ev);
    return req;
}

static void enum_users_process(struct tevent_req *subreq)
{
    struct tevent_req *req = tevent_req_callback_data(subreq,
                                                      struct tevent_req);
    struct enum_users_state *state = tevent_req_data(req,
                                                struct enum_users_state);
    struct proxy_ctx *ctx = state->ctx;
    enum nss_status status;
    char *newbuf;
    int ret;

    if (!state->in_transaction) {
        ret = sysdb_transaction_recv(subreq, state, &state->handle);
        if (ret) {
            goto fail;
        }
        talloc_zfree(subreq);

        state->in_transaction = true;
    } else {
        ret = sysdb_store_user_recv(subreq);
        if (ret) {
            /* Do not fail completely on errors.
             * Just report the failure to save and go on */
            DEBUG(2, ("Failed to store user. Ignoring.\n"));
        }
        talloc_zfree(subreq);
    }

again:
    /* always zero out the pwd structure */
    memset(state->pwd, 0, sizeof(struct passwd));

    /* get entry */
    status = ctx->ops.getpwent_r(state->pwd,
                                 state->buffer, state->buflen, &ret);

    switch (status) {
    case NSS_STATUS_TRYAGAIN:
        /* buffer too small ? */
        if (state->buflen < MAX_BUF_SIZE) {
            state->buflen *= 2;
        }
        if (state->buflen > MAX_BUF_SIZE) {
            state->buflen = MAX_BUF_SIZE;
        }
        newbuf = talloc_realloc_size(state, state->buffer, state->buflen);
        if (!newbuf) {
            ret = ENOMEM;
            goto fail;
        }
        state->buffer = newbuf;
        goto again;

    case NSS_STATUS_NOTFOUND:

        /* we are done here */
        DEBUG(7, ("Enumeration completed.\n"));

        ctx->ops.endpwent();
        subreq = sysdb_transaction_commit_send(state, state->ev,
                                               state->handle);
        if (!subreq) {
            tevent_req_error(req, ENOMEM);
            return;
        }
        tevent_req_set_callback(subreq, proxy_default_done, req);
        return;

    case NSS_STATUS_SUCCESS:

        DEBUG(7, ("User found (%s, %d, %d)\n", state->pwd->pw_name,
                  state->pwd->pw_uid, state->pwd->pw_gid));

        /* uid=0 or gid=0 are invalid values */
        if (state->pwd->pw_uid == 0 || state->pwd->pw_gid == 0) {
            goto again; /* skip */
        }

        subreq = sysdb_store_user_send(state, state->ev, state->handle,
                                       state->domain,
                                       state->pwd->pw_name,
                                       state->pwd->pw_passwd,
                                       state->pwd->pw_uid,
                                       state->pwd->pw_gid,
                                       state->pwd->pw_gecos,
                                       state->pwd->pw_dir,
                                       state->pwd->pw_shell, NULL);
        if (!subreq) {
            tevent_req_error(req, ENOMEM);
            return;
        }
        tevent_req_set_callback(subreq, enum_users_process, req);
        return;

    case NSS_STATUS_UNAVAIL:
        /* "remote" backend unavailable. Enter offline mode */
        ret = ENXIO;
        goto fail;

    default:
        DEBUG(2, ("proxy -> getpwent_r failed (%d)[%s]\n",
                  ret, strerror(ret)));
        goto fail;
    }

fail:
    ctx->ops.endpwent();
    tevent_req_error(req, ret);
}

/* =Getgrnam-wrapper======================================================*/

#define DEBUG_GR_MEM(level, state) \
    do { \
        if (debug_level >= level) { \
            if (!state->grp->gr_mem || !state->grp->gr_mem[0]) { \
                DEBUG(level, ("Group %s has no members!\n", \
                              state->grp->gr_name)); \
            } else { \
                int i = 0; \
                while (state->grp->gr_mem[i]) { \
                    /* count */ \
                    i++; \
                } \
                DEBUG(level, ("Group %s has %d members!\n", \
                              state->grp->gr_name, i)); \
            } \
        } \
    } while(0)

static void get_gr_name_process(struct tevent_req *subreq);
static void get_gr_name_remove_done(struct tevent_req *subreq);
static void get_gr_name_add_done(struct tevent_req *subreq);

static struct tevent_req *get_gr_name_send(TALLOC_CTX *mem_ctx,
                                           struct tevent_context *ev,
                                           struct proxy_ctx *ctx,
                                           struct sysdb_ctx *sysdb,
                                           struct sss_domain_info *domain,
                                           const char *name)
{
    struct tevent_req *req, *subreq;
    struct proxy_state *state;

    req = tevent_req_create(mem_ctx, &state, struct proxy_state);
    if (!req) return NULL;

    memset(state, 0, sizeof(struct proxy_state));

    state->ev = ev;
    state->ctx = ctx;
    state->sysdb = sysdb;
    state->domain = domain;
    state->name = name;

    subreq = sysdb_transaction_send(state, state->ev, state->sysdb);
    if (!subreq) {
        talloc_zfree(req);
        return NULL;
    }
    tevent_req_set_callback(subreq, get_gr_name_process, req);

    return req;
}

static void get_gr_name_process(struct tevent_req *subreq)
{
    struct tevent_req *req = tevent_req_callback_data(subreq,
                                                      struct tevent_req);
    struct proxy_state *state = tevent_req_data(req,
                                                struct proxy_state);
    struct proxy_ctx *ctx = state->ctx;
    enum nss_status status;
    char *buffer;
    char *newbuf;
    size_t buflen;
    bool delete_group = false;
    const char **members;
    int ret;

    DEBUG(7, ("Searching group by name (%s)\n", state->name));

    ret = sysdb_transaction_recv(subreq, state, &state->handle);
    if (ret) {
        tevent_req_error(req, ret);
        return;
    }
    talloc_zfree(subreq);

    state->grp = talloc(state, struct group);
    if (!state->grp) {
        tevent_req_error(req, ENOMEM);
        return;
    }

    buflen = DEFAULT_BUFSIZE;
    buffer = talloc_size(state, buflen);
    if (!buffer) {
        tevent_req_error(req, ENOMEM);
        return;
    }

    /* FIXME: should we move this call outside the transaction to keep the
     * transaction as short as possible ? */
again:
    /* always zero out the grp structure */
    memset(state->grp, 0, sizeof(struct group));

    status = ctx->ops.getgrnam_r(state->name, state->grp,
                                 buffer, buflen, &ret);

    switch (status) {
    case NSS_STATUS_TRYAGAIN:
        /* buffer too small ? */
        if (buflen < MAX_BUF_SIZE) {
            buflen *= 2;
        }
        if (buflen > MAX_BUF_SIZE) {
            buflen = MAX_BUF_SIZE;
        }
        newbuf = talloc_realloc_size(state, buffer, buflen);
        if (!newbuf) {
            tevent_req_error(req, ENOMEM);
            return;
        }
        buffer = newbuf;
        goto again;

    case NSS_STATUS_NOTFOUND:

        DEBUG(7, ("Group %s not found.\n", state->name));
        delete_group = true;
        break;

    case NSS_STATUS_SUCCESS:

        DEBUG(7, ("Group %s found: (%s, %d)\n", state->name,
                  state->grp->gr_name, state->grp->gr_gid));

        /* gid=0 is an invalid value */
        if (state->grp->gr_gid == 0) {
            delete_group = true;
            break;
        }

        DEBUG_GR_MEM(7, state);

        if (state->grp->gr_mem && state->grp->gr_mem[0]) {