diff options
Diffstat (limited to 'callouts/nm-dispatcher-utils.c')
| -rw-r--r-- | callouts/nm-dispatcher-utils.c | 165 |
1 files changed, 62 insertions, 103 deletions
diff --git a/callouts/nm-dispatcher-utils.c b/callouts/nm-dispatcher-utils.c index e5240a7d..b1d11daf 100644 --- a/callouts/nm-dispatcher-utils.c +++ b/callouts/nm-dispatcher-utils.c @@ -30,7 +30,7 @@ #include <nm-setting-ip6-config.h> #include <nm-setting-connection.h> -#include "nm-dispatcher-action.h" +#include "nm-dispatcher-api.h" #include "nm-utils.h" #include "nm-dispatcher-utils.h" @@ -96,6 +96,8 @@ construct_ip4_items (GSList *items, GHashTable *ip4_config, const char *prefix) guint32 num, i; GString *tmp; GValue *val; + char str_addr[INET_ADDRSTRLEN]; + char str_gw[INET_ADDRSTRLEN]; if (ip4_config == NULL) return items; @@ -110,30 +112,19 @@ construct_ip4_items (GSList *items, GHashTable *ip4_config, const char *prefix) for (iter = addresses, num = 0; iter; iter = g_slist_next (iter)) { NMIP4Address *addr = (NMIP4Address *) iter->data; - char str_addr[INET_ADDRSTRLEN + 1]; - char str_gw[INET_ADDRSTRLEN + 1]; - struct in_addr tmp_addr; guint32 ip_prefix = nm_ip4_address_get_prefix (addr); char *addrtmp; - memset (str_addr, 0, sizeof (str_addr)); - tmp_addr.s_addr = nm_ip4_address_get_address (addr); - if (!inet_ntop (AF_INET, &tmp_addr, str_addr, sizeof (str_addr))) - continue; - - memset (str_gw, 0, sizeof (str_gw)); - tmp_addr.s_addr = nm_ip4_address_get_gateway (addr); - inet_ntop (AF_INET, &tmp_addr, str_gw, sizeof (str_gw)); + nm_utils_inet4_ntop (nm_ip4_address_get_address (addr), str_addr); + nm_utils_inet4_ntop (nm_ip4_address_get_gateway (addr), str_gw); addrtmp = g_strdup_printf ("%sIP4_ADDRESS_%d=%s/%d %s", prefix, num++, str_addr, ip_prefix, str_gw); items = g_slist_prepend (items, addrtmp); } if (num) items = g_slist_prepend (items, g_strdup_printf ("%sIP4_NUM_ADDRESSES=%d", prefix, num)); - if (addresses) { - g_slist_foreach (addresses, (GFunc) nm_ip4_address_unref, NULL); - g_slist_free (addresses); - } + if (addresses) + g_slist_free_full (addresses, (GDestroyNotify) nm_ip4_address_unref); /* DNS servers */ val = g_hash_table_lookup (ip4_config, "nameservers"); @@ -146,17 +137,13 @@ construct_ip4_items (GSList *items, GHashTable *ip4_config, const char *prefix) tmp = g_string_new (NULL); g_string_append_printf (tmp, "%sIP4_NAMESERVERS=", prefix); for (i = 0; i < dns->len; i++) { - struct in_addr addr; - char buf[INET_ADDRSTRLEN + 1]; - - addr.s_addr = g_array_index (dns, guint32, i); - memset (buf, 0, sizeof (buf)); - if (inet_ntop (AF_INET, &addr, buf, sizeof (buf))) { - if (!first) - g_string_append_c (tmp, ' '); - g_string_append (tmp, buf); - first = FALSE; - } + guint32 addr; + + addr = g_array_index (dns, guint32, i); + if (!first) + g_string_append_c (tmp, ' '); + g_string_append (tmp, nm_utils_inet4_ntop (addr, NULL)); + first = FALSE; } items = g_slist_prepend (items, tmp->str); g_string_free (tmp, FALSE); @@ -176,17 +163,13 @@ construct_ip4_items (GSList *items, GHashTable *ip4_config, const char *prefix) tmp = g_string_new (NULL); g_string_append_printf (tmp, "%sIP4_WINS_SERVERS=", prefix); for (i = 0; i < wins->len; i++) { - struct in_addr addr; - char buf[INET_ADDRSTRLEN + 1]; - - addr.s_addr = g_array_index (wins, guint32, i); - memset (buf, 0, sizeof (buf)); - if (inet_ntop (AF_INET, &addr, buf, sizeof (buf))) { - if (!first) - g_string_append_c (tmp, ' '); - g_string_append (tmp, buf); - first = FALSE; - } + guint32 addr; + + addr = g_array_index (wins, guint32, i); + if (!first) + g_string_append_c (tmp, ' '); + g_string_append (tmp, nm_utils_inet4_ntop (addr, NULL)); + first = FALSE; } items = g_slist_prepend (items, tmp->str); g_string_free (tmp, FALSE); @@ -199,30 +182,19 @@ construct_ip4_items (GSList *items, GHashTable *ip4_config, const char *prefix) for (iter = routes, num = 0; iter; iter = g_slist_next (iter)) { NMIP4Route *route = (NMIP4Route *) iter->data; - char str_addr[INET_ADDRSTRLEN + 1]; - char str_nh[INET_ADDRSTRLEN + 1]; - struct in_addr tmp_addr; guint32 ip_prefix = nm_ip4_route_get_prefix (route); guint32 metric = nm_ip4_route_get_metric (route); char *routetmp; - memset (str_addr, 0, sizeof (str_addr)); - tmp_addr.s_addr = nm_ip4_route_get_dest (route); - if (!inet_ntop (AF_INET, &tmp_addr, str_addr, sizeof (str_addr))) - continue; + nm_utils_inet4_ntop (nm_ip4_route_get_dest (route), str_addr); + nm_utils_inet4_ntop (nm_ip4_route_get_next_hop (route), str_gw); - memset (str_nh, 0, sizeof (str_nh)); - tmp_addr.s_addr = nm_ip4_route_get_next_hop (route); - inet_ntop (AF_INET, &tmp_addr, str_nh, sizeof (str_nh)); - - routetmp = g_strdup_printf ("%sIP4_ROUTE_%d=%s/%d %s %d", prefix, num++, str_addr, ip_prefix, str_nh, metric); + routetmp = g_strdup_printf ("%sIP4_ROUTE_%d=%s/%d %s %d", prefix, num++, str_addr, ip_prefix, str_gw, metric); items = g_slist_prepend (items, routetmp); } items = g_slist_prepend (items, g_strdup_printf ("%sIP4_NUM_ROUTES=%d", prefix, num)); - if (routes) { - g_slist_foreach (routes, (GFunc) nm_ip4_route_unref, NULL); - g_slist_free (routes); - } + if (routes) + g_slist_free_full (routes, (GDestroyNotify) nm_ip4_route_unref); return items; } @@ -255,6 +227,8 @@ construct_ip6_items (GSList *items, GHashTable *ip6_config, const char *prefix) guint32 num; GString *tmp; GValue *val; + char str_addr[INET6_ADDRSTRLEN]; + char str_gw[INET6_ADDRSTRLEN]; if (ip6_config == NULL) return items; @@ -269,27 +243,19 @@ construct_ip6_items (GSList *items, GHashTable *ip6_config, const char *prefix) for (iter = addresses, num = 0; iter; iter = g_slist_next (iter)) { NMIP6Address *addr = (NMIP6Address *) iter->data; - char str_addr[INET6_ADDRSTRLEN]; - char str_gw[INET6_ADDRSTRLEN]; - const struct in6_addr *tmp_addr; guint32 ip_prefix = nm_ip6_address_get_prefix (addr); char *addrtmp; - tmp_addr = nm_ip6_address_get_address (addr); - inet_ntop (AF_INET6, tmp_addr, str_addr, sizeof (str_addr)); - - tmp_addr = nm_ip6_address_get_gateway (addr); - inet_ntop (AF_INET6, tmp_addr, str_gw, sizeof (str_gw)); + nm_utils_inet6_ntop (nm_ip6_address_get_address (addr), str_addr); + nm_utils_inet6_ntop (nm_ip6_address_get_gateway (addr), str_gw); addrtmp = g_strdup_printf ("%sIP6_ADDRESS_%d=%s/%d %s", prefix, num++, str_addr, ip_prefix, str_gw); items = g_slist_prepend (items, addrtmp); } if (num) items = g_slist_prepend (items, g_strdup_printf ("%sIP6_NUM_ADDRESSES=%d", prefix, num)); - if (addresses) { - g_slist_foreach (addresses, (GFunc) nm_ip6_address_unref, NULL); - g_slist_free (addresses); - } + if (addresses) + g_slist_free_full (addresses, (GDestroyNotify) nm_ip6_address_unref); /* DNS servers */ val = g_hash_table_lookup (ip6_config, "nameservers"); @@ -297,23 +263,20 @@ construct_ip6_items (GSList *items, GHashTable *ip6_config, const char *prefix) dns = nm_utils_ip6_dns_from_gvalue (val); if (g_slist_length (dns)) { + gboolean first = TRUE; + tmp = g_string_new (NULL); g_string_append_printf (tmp, "%sIP6_NAMESERVERS=", prefix); for (iter = dns; iter; iter = g_slist_next (iter)) { const struct in6_addr *addr = iter->data; - gboolean first = TRUE; - char buf[INET6_ADDRSTRLEN + 1]; - - memset (buf, 0, sizeof (buf)); - if (inet_ntop (AF_INET6, addr, buf, sizeof (buf))) { - if (!first) - g_string_append_c (tmp, ' '); - g_string_append (tmp, buf); - first = FALSE; - } + + if (!first) + g_string_append_c (tmp, ' '); + g_string_append (tmp, nm_utils_inet6_ntop (addr, NULL)); + first = FALSE; } - + items = g_slist_prepend (items, tmp->str); g_string_free (tmp, FALSE); } @@ -328,31 +291,20 @@ construct_ip6_items (GSList *items, GHashTable *ip6_config, const char *prefix) for (iter = routes, num = 0; iter; iter = g_slist_next (iter)) { NMIP6Route *route = (NMIP6Route *) iter->data; - char str_addr[INET6_ADDRSTRLEN + 1]; - char str_nh[INET6_ADDRSTRLEN + 1]; - const struct in6_addr *tmp_addr; guint32 ip_prefix = nm_ip6_route_get_prefix (route); guint32 metric = nm_ip6_route_get_metric (route); char *routetmp; - memset (str_addr, 0, sizeof (str_addr)); - tmp_addr = nm_ip6_route_get_dest (route); - if (!inet_ntop (AF_INET6, &tmp_addr, str_addr, sizeof (str_addr))) - continue; + nm_utils_inet6_ntop (nm_ip6_route_get_dest (route), str_addr); + nm_utils_inet6_ntop (nm_ip6_route_get_next_hop (route), str_gw); - memset (str_nh, 0, sizeof (str_nh)); - tmp_addr = nm_ip6_route_get_next_hop (route); - inet_ntop (AF_INET6, &tmp_addr, str_nh, sizeof (str_nh)); - - routetmp = g_strdup_printf ("%sIP6_ROUTE_%d=%s/%d %s %d", prefix, num++, str_addr, ip_prefix, str_nh, metric); + routetmp = g_strdup_printf ("%sIP6_ROUTE_%d=%s/%d %s %d", prefix, num++, str_addr, ip_prefix, str_gw, metric); items = g_slist_prepend (items, routetmp); } if (num) items = g_slist_prepend (items, g_strdup_printf ("%sIP6_NUM_ROUTES=%d", prefix, num)); - if (routes) { - g_slist_foreach (routes, (GFunc) nm_ip6_route_unref, NULL); - g_slist_free (routes); - } + if (routes) + g_slist_free_full (routes, (GDestroyNotify) nm_ip6_route_unref); return items; } @@ -393,10 +345,10 @@ nm_dispatcher_utils_construct_envp (const char *action, char **out_iface) { const char *iface = NULL, *ip_iface = NULL; - const char *uuid = NULL, *id = NULL; + const char *uuid = NULL, *id = NULL, *path; NMDeviceState dev_state = NM_DEVICE_STATE_UNKNOWN; GValue *value; - char **envp = NULL; + char **envp = NULL, *path_item; GSList *items = NULL, *iter; guint i; GHashTable *con_setting_hash; @@ -407,7 +359,7 @@ nm_dispatcher_utils_construct_envp (const char *action, /* Hostname changes don't require a device nor contain a connection */ if (!strcmp (action, "hostname")) - return g_new0 (char *, 1); + goto done; /* Canonicalize the VPN interface name; "" is used when passing it through * D-Bus so make sure that's fixed up here. @@ -453,7 +405,7 @@ nm_dispatcher_utils_construct_envp (const char *action, return NULL; } ip_iface = g_value_get_string (value); - } + } /* Device type */ value = g_hash_table_lookup (device_props, NMD_DEVICE_PROPS_TYPE); @@ -493,12 +445,6 @@ nm_dispatcher_utils_construct_envp (const char *action, items = construct_ip6_items (items, vpn_ip6_props, "VPN_"); } - /* Convert the list to an environment pointer */ - envp = g_new0 (char *, g_slist_length (items) + 1); - for (iter = items, i = 0; iter; iter = g_slist_next (iter), i++) - envp[i] = (char *) iter->data; - g_slist_free (items); - /* Backwards compat: 'iface' is set in this order: * 1) VPN interface name * 2) Device IP interface name @@ -511,6 +457,19 @@ nm_dispatcher_utils_construct_envp (const char *action, else *out_iface = g_strdup (iface); + done: + path = g_getenv ("PATH"); + if (path) { + path_item = g_strdup_printf ("PATH=%s", path); + items = g_slist_prepend (items, path_item); + } + + /* Convert the list to an environment pointer */ + envp = g_new0 (char *, g_slist_length (items) + 1); + for (iter = items, i = 0; iter; iter = g_slist_next (iter), i++) + envp[i] = (char *) iter->data; + g_slist_free (items); + return envp; } |