summary refs log tree commit diff
path: root/src/settings/plugins/ifcfg-rh/utils.c
diff options
context:
space:
mode:
authorMichael Biebl <biebl@debian.org>2014-07-06 02:16:10 +0200
committerMichael Biebl <biebl@debian.org>2014-07-06 02:16:10 +0200
commit33491bc4279481db8ae47213e34a6d695a0e8830 (patch)
tree097d2b0fdff3fae6885381ae5e57a182cd8cbbba /src/settings/plugins/ifcfg-rh/utils.c
parent59c3714a494c3b3765657c0551ad82842d98a7d2 (diff)
Imported Upstream version 0.9.10.0 upstream/0.9.10.0
Diffstat (limited to 'src/settings/plugins/ifcfg-rh/utils.c')
-rw-r--r--src/settings/plugins/ifcfg-rh/utils.c144
1 files changed, 49 insertions, 95 deletions
diff --git a/src/settings/plugins/ifcfg-rh/utils.c b/src/settings/plugins/ifcfg-rh/utils.c
index 04df6671..8318a65d 100644
--- a/src/settings/plugins/ifcfg-rh/utils.c
+++ b/src/settings/plugins/ifcfg-rh/utils.c
@@ -25,97 +25,6 @@
 #include "shvar.h"
 
 /*
- * utils_bin2hexstr
- *
- * Convert a byte-array into a hexadecimal string.
- *
- * Code originally by Alex Larsson <alexl@redhat.com> and
- *  copyright Red Hat, Inc. under terms of the LGPL.
- *
- */
-char *
-utils_bin2hexstr (const char *bytes, int len, int final_len)
-{
-	static char hex_digits[] = "0123456789abcdef";
-	char *result;
-	int i;
-	gsize buflen = (len * 2) + 1;
-
-	g_return_val_if_fail (bytes != NULL, NULL);
-	g_return_val_if_fail (len > 0, NULL);
-	g_return_val_if_fail (len < 4096, NULL);   /* Arbitrary limit */
-	if (final_len > -1)
-		g_return_val_if_fail (final_len < buflen, NULL);
-
-	result = g_malloc0 (buflen);
-	for (i = 0; i < len; i++)
-	{
-		result[2*i] = hex_digits[(bytes[i] >> 4) & 0xf];
-		result[2*i+1] = hex_digits[bytes[i] & 0xf];
-	}
-	/* Cut converted key off at the correct length for this cipher type */
-	if (final_len > -1)
-		result[final_len] = '\0';
-	else
-		result[buflen - 1] = '\0';
-
-	return result;
-}
-
-/* From hostap, Copyright (c) 2002-2005, Jouni Malinen <jkmaline@cc.hut.fi> */
-
-static int hex2num (char c)
-{
-	if (c >= '0' && c <= '9')
-		return c - '0';
-	if (c >= 'a' && c <= 'f')
-		return c - 'a' + 10;
-	if (c >= 'A' && c <= 'F')
-		return c - 'A' + 10;
-	return -1;
-}
-
-static int hex2byte (const char *hex)
-{
-	int a, b;
-	a = hex2num(*hex++);
-	if (a < 0)
-		return -1;
-	b = hex2num(*hex++);
-	if (b < 0)
-		return -1;
-	return (a << 4) | b;
-}
-
-char *
-utils_hexstr2bin (const char *hex, size_t len)
-{
-	size_t       i;
-	int          a;
-	const char * ipos = hex;
-	char *       buf = NULL;
-	char *       opos;
-
-	/* Length must be a multiple of 2 */
-	if ((len % 2) != 0)
-		return NULL;
-
-	opos = buf = g_malloc0 ((len / 2) + 1);
-	for (i = 0; i < len; i += 2) {
-		a = hex2byte (ipos);
-		if (a < 0) {
-			g_free (buf);
-			return NULL;
-		}
-		*opos++ = a;
-		ipos += 2;
-	}
-	return buf;
-}
-
-/* End from hostap */
-
-/*
  * utils_single_quote_string
  *
  * Put string inside single quotes and remove CR, LF characters. If single quote
@@ -143,7 +52,6 @@ utils_single_quote_string (const char *str)
 			drop++;
 	}
 	new_str = g_malloc0 (slen + extra - drop + 4); /* 4 is for $''\0*/
-	if (!new_str) return NULL;
 
 	if (extra > 0)
 		new_str[j++] = '$';
@@ -179,7 +87,6 @@ utils_single_unquote_string (const char *str)
 
 	slen = strlen (str);
 	new_str = g_malloc0 (slen + 1);
-	if (!new_str) return NULL;
 
 	if (   (slen >= 2 && (str[0] == dq_char || str[0] == q_char) && str[0] == str[slen-1])
 	    || (slen >= 3 && str[0] == '$' && str[1] == q_char && str[1] == str[slen-1])) {
@@ -389,7 +296,7 @@ utils_get_extra_ifcfg (const char *parent, const char *tag, gboolean should_crea
 		ifcfg = svCreateFile (path);
 
 	if (!ifcfg)
-		ifcfg = svNewFile (path);
+		ifcfg = svOpenFile (path, NULL);
 
 	g_free (path);
 	return ifcfg;
@@ -455,8 +362,55 @@ utils_ignore_ip_config (NMConnection *connection)
 	 * scripts just ignore it if it's there.
 	 */
 	if (   nm_setting_connection_is_slave_type (s_con, NM_SETTING_BOND_SETTING_NAME)
-	    || nm_setting_connection_is_slave_type (s_con, NM_SETTING_BRIDGE_SETTING_NAME))
+	    || nm_setting_connection_is_slave_type (s_con, NM_SETTING_BRIDGE_SETTING_NAME)
+	    || nm_setting_connection_is_slave_type (s_con, NM_SETTING_TEAM_SETTING_NAME))
 		return TRUE;
 
 	return FALSE;
 }
+
+/* Find out if the 'alias' file name might be an alias file for 'ifcfg' file name,
+ * or any alias when 'ifcfg' is NULL. Does not check that it's actually a valid
+ * alias name; that happens in reader.c
+ */
+gboolean
+utils_is_ifcfg_alias_file (const char *alias, const char *ifcfg)
+{
+	g_return_val_if_fail (alias != NULL, FALSE);
+
+	if (strncmp (alias, IFCFG_TAG, strlen (IFCFG_TAG)))
+		return FALSE;
+
+	if (ifcfg) {
+		size_t len = strlen (ifcfg);
+
+		return (strncmp (alias, ifcfg, len) == 0 && alias[len] == ':');
+	} else {
+		return (strchr (alias, ':') != NULL);
+	}
+}
+
+char *
+utils_get_ifcfg_from_alias (const char *alias)
+{
+	char *base, *ptr, *ifcfg = NULL;
+
+	g_return_val_if_fail (alias != NULL, NULL);
+
+	base = g_path_get_basename (alias);
+	g_return_val_if_fail (base != NULL, NULL);
+
+	if (utils_is_ifcfg_alias_file (base, NULL)) {
+		ifcfg = g_strdup (alias);
+		ptr = strrchr (ifcfg, ':');
+		if (ptr)
+			*ptr = '\0';
+		else {
+			g_free (ifcfg);
+			ifcfg = NULL;
+		}
+	}
+
+	g_free (base);
+	return ifcfg;
+}