1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* NetworkManager system settings service
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * (C) Copyright 2008 - 2012 Red Hat, Inc.
27 #include "nm-core-internal.h"
28 #include "nm-utils-internal.h"
29 #include "NetworkManagerUtils.h"
35 * utils_single_quote_string
37 * Put string inside single quotes and remove CR, LF characters. If single quote
38 * is present, escape it with a backslash and prepend the whole string with $
39 * in order to have $'string'. That allows us to use single quote inside
40 * single quotes without breaking bash syntax. (man bash, section QUOTING).
42 * Caller is responsible for freeing the returned string.
45 utils_single_quote_string (const char *str)
47 static const char *drop_chars = "\r\n"; /* drop CR and LF */
48 static const char escape_char = '\\'; /* escape char is backslash */
49 static const char quote_char = '\''; /* quote char is single quote */
50 size_t i, slen, j = 0;
51 size_t drop = 0, extra = 0;
55 for (i = 0; i < slen; i++) {
56 if (str[i] == quote_char)
58 if (strchr (drop_chars, str[i]))
61 new_str = g_malloc0 (slen + extra - drop + 4); /* 4 is for $''\0*/
65 new_str[j++] = quote_char;
66 for (i = 0; i < slen; i++) {
67 if (strchr (drop_chars, str[i]))
69 if (str[i] == quote_char)
70 new_str[j++] = escape_char;
71 new_str[j++] = str[i];
73 new_str[j] = quote_char;
79 * utils_single_unquote_string
81 * Remove string from single (or double) quotes, and remove escaping of '.
82 * Also remove first $ if the string is in the form of $'string'.
84 * Caller is responsible for freeing the returned string.
87 utils_single_unquote_string (const char *str)
89 static const char escape_char = '\\'; /* escape char is backslash */
90 static const char q_char = '\''; /* quote char is single quote */
91 static const char dq_char = '"'; /* double quote char */
92 size_t i, slen, j = 0, quote = 0, dollar = 0;
96 new_str = g_malloc0 (slen + 1);
98 if ( (slen >= 2 && (str[0] == dq_char || str[0] == q_char) && str[0] == str[slen-1])
99 || (slen >= 3 && str[0] == '$' && str[1] == q_char && str[1] == str[slen-1])) {
101 if (str[0] == '$') dollar = 1;
105 while (i < slen - quote) {
106 if (str[i] == escape_char && str[i+1] == q_char && i+1 < slen-quote)
108 new_str[j++] = str[i++];
116 * Check ';[a-fA-F0-9]{8}' file suffix used for temporary files by rpm when
117 * installing packages.
119 * Implementation taken from upstart.
122 check_rpm_temp_suffix (const char *path)
126 g_return_val_if_fail (path != NULL, FALSE);
128 /* Matches *;[a-fA-F0-9]{8}; used by rpm */
129 ptr = strrchr (path, ';');
130 if (ptr && (strspn (ptr + 1, "abcdefABCDEF0123456789") == 8)
137 check_suffix (const char *base, const char *tag)
141 g_return_val_if_fail (base != NULL, TRUE);
142 g_return_val_if_fail (tag != NULL, TRUE);
145 tag_len = strlen (tag);
146 if ((len > tag_len) && !strcasecmp (base + len - tag_len, tag))
152 utils_should_ignore_file (const char *filename, gboolean only_ifcfg)
155 gboolean ignore = TRUE;
156 gboolean is_ifcfg = FALSE;
157 gboolean is_other = FALSE;
159 g_return_val_if_fail (filename != NULL, TRUE);
161 base = g_path_get_basename (filename);
162 g_return_val_if_fail (base != NULL, TRUE);
164 /* Only handle ifcfg, keys, and routes files */
165 if (!strncmp (base, IFCFG_TAG, strlen (IFCFG_TAG)))
168 if (only_ifcfg == FALSE) {
169 if ( !strncmp (base, KEYS_TAG, strlen (KEYS_TAG))
170 || !strncmp (base, ROUTE_TAG, strlen (ROUTE_TAG))
171 || !strncmp (base, ROUTE6_TAG, strlen (ROUTE6_TAG)))
175 /* But not those that have certain suffixes */
176 if ( (is_ifcfg || is_other)
177 && !check_suffix (base, BAK_TAG)
178 && !check_suffix (base, TILDE_TAG)
179 && !check_suffix (base, ORIG_TAG)
180 && !check_suffix (base, REJ_TAG)
181 && !check_suffix (base, RPMNEW_TAG)
182 && !check_suffix (base, AUGNEW_TAG)
183 && !check_suffix (base, AUGTMP_TAG)
184 && !check_rpm_temp_suffix (base))
192 utils_cert_path (const char *parent, const char *suffix)
197 g_return_val_if_fail (parent != NULL, NULL);
198 g_return_val_if_fail (suffix != NULL, NULL);
200 name = utils_get_ifcfg_name (parent, FALSE);
201 dir = g_path_get_dirname (parent);
202 path = g_strdup_printf ("%s/%s-%s", dir, name, suffix);
208 utils_get_ifcfg_name (const char *file, gboolean only_ifcfg)
212 g_return_val_if_fail (file != NULL, NULL);
214 name = strrchr (file, '/');
222 #define MATCH_TAG_AND_RETURN(name, TAG) \
224 if (strncmp (name, TAG, STRLEN (TAG)) == 0) { \
225 name += STRLEN (TAG); \
226 if (name[0] == '\0') \
233 MATCH_TAG_AND_RETURN (name, IFCFG_TAG);
235 MATCH_TAG_AND_RETURN (name, KEYS_TAG);
236 MATCH_TAG_AND_RETURN (name, ROUTE_TAG);
237 MATCH_TAG_AND_RETURN (name, ROUTE6_TAG);
243 /* Used to get any ifcfg/extra file path from any other ifcfg/extra path
244 * in the form <tag><name>.
247 utils_get_extra_path (const char *parent, const char *tag)
249 char *item_path = NULL, *dirname;
252 g_return_val_if_fail (parent != NULL, NULL);
253 g_return_val_if_fail (tag != NULL, NULL);
255 dirname = g_path_get_dirname (parent);
259 name = utils_get_ifcfg_name (parent, FALSE);
261 if (!strcmp (dirname, "."))
262 item_path = g_strdup_printf ("%s%s", tag, name);
264 item_path = g_strdup_printf ("%s/%s%s", dirname, tag, name);
272 utils_get_ifcfg_path (const char *parent)
274 return utils_get_extra_path (parent, IFCFG_TAG);
278 utils_get_keys_path (const char *parent)
280 return utils_get_extra_path (parent, KEYS_TAG);
284 utils_get_route_path (const char *parent)
286 return utils_get_extra_path (parent, ROUTE_TAG);
290 utils_get_route6_path (const char *parent)
292 return utils_get_extra_path (parent, ROUTE6_TAG);
296 utils_get_extra_ifcfg (const char *parent, const char *tag, gboolean should_create)
298 shvarFile *ifcfg = NULL;
301 path = utils_get_extra_path (parent, tag);
305 if (should_create && !g_file_test (path, G_FILE_TEST_EXISTS))
306 ifcfg = svCreateFile (path);
309 ifcfg = svOpenFile (path, NULL);
316 utils_get_keys_ifcfg (const char *parent, gboolean should_create)
318 return utils_get_extra_ifcfg (parent, KEYS_TAG, should_create);
322 utils_get_route_ifcfg (const char *parent, gboolean should_create)
324 return utils_get_extra_ifcfg (parent, ROUTE_TAG, should_create);
328 utils_get_route6_ifcfg (const char *parent, gboolean should_create)
330 return utils_get_extra_ifcfg (parent, ROUTE6_TAG, should_create);
333 /* Finds out if route file has new or older format
334 * Returns TRUE - new syntax (ADDRESS<n>=a.b.c.d ...), error opening file or empty
335 * FALSE - older syntax, i.e. argument to 'ip route add' (1.2.3.0/24 via 11.22.33.44)
338 utils_has_route_file_new_syntax (const char *filename)
340 char *contents = NULL;
342 gboolean ret = FALSE;
343 const char *pattern = "^[[:space:]]*ADDRESS[0-9]+=";
345 g_return_val_if_fail (filename != NULL, TRUE);
347 if (!g_file_get_contents (filename, &contents, &len, NULL))
355 if (g_regex_match_simple (pattern, contents, G_REGEX_MULTILINE, 0))
364 utils_has_complex_routes (const char *filename)
368 g_return_val_if_fail (filename != NULL, TRUE);
370 rules = utils_get_extra_path (filename, RULE_TAG);
371 if (g_file_test (rules, G_FILE_TEST_EXISTS)) {
377 rules = utils_get_extra_path (filename, RULE6_TAG);
378 if (g_file_test (rules, G_FILE_TEST_EXISTS)) {
388 utils_ignore_ip_config (NMConnection *connection)
390 NMSettingConnection *s_con;
392 s_con = nm_connection_get_setting_connection (connection);
395 /* bonding slaves have no IP configuration, and the system
396 * scripts just ignore it if it's there.
398 if ( nm_setting_connection_is_slave_type (s_con, NM_SETTING_BOND_SETTING_NAME)
399 || nm_setting_connection_is_slave_type (s_con, NM_SETTING_BRIDGE_SETTING_NAME)
400 || nm_setting_connection_is_slave_type (s_con, NM_SETTING_TEAM_SETTING_NAME))
406 /* Find out if the 'alias' file name might be an alias file for 'ifcfg' file name,
407 * or any alias when 'ifcfg' is NULL. Does not check that it's actually a valid
408 * alias name; that happens in reader.c
411 utils_is_ifcfg_alias_file (const char *alias, const char *ifcfg)
413 g_return_val_if_fail (alias != NULL, FALSE);
415 if (strncmp (alias, IFCFG_TAG, strlen (IFCFG_TAG)))
419 size_t len = strlen (ifcfg);
421 return (strncmp (alias, ifcfg, len) == 0 && alias[len] == ':');
423 return (strchr (alias, ':') != NULL);
428 utils_get_ifcfg_from_alias (const char *alias)
430 char *base, *ptr, *ifcfg = NULL;
432 g_return_val_if_fail (alias != NULL, NULL);
434 base = g_path_get_basename (alias);
435 g_return_val_if_fail (base != NULL, NULL);
437 if (utils_is_ifcfg_alias_file (base, NULL)) {
438 ifcfg = g_strdup (alias);
439 ptr = strrchr (ifcfg, ':');