diff options
author | Jiří Klimeš <jklimes@redhat.com> | 2014-03-31 14:32:32 +0200 |
---|---|---|
committer | Jiří Klimeš <jklimes@redhat.com> | 2014-03-31 16:52:58 +0200 |
commit | 70f32fe3f30527e6cb91b22d87a68c13d3a59546 (patch) | |
tree | 806f11a87099b1707031b231e7859e1cbab8b5ca | |
parent | bcb253b1939243f29540c00a0ff67d9f1bcf1749 (diff) | |
download | NetworkManager-70f32fe3f30527e6cb91b22d87a68c13d3a59546.tar.gz |
utils: add nm_utils_file_set_contents()
nm_utils_file_set_contents() wraps g_file_set_contents() but preserves SELinux
label on the file.
-rw-r--r-- | src/Makefile.am | 8 | ||||
-rw-r--r-- | src/NetworkManagerUtils.c | 50 | ||||
-rw-r--r-- | src/NetworkManagerUtils.h | 5 |
3 files changed, 62 insertions, 1 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index f3fcfba98e..b11978f2ea 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -360,6 +360,10 @@ if WITH_TEAMDCTL AM_CPPFLAGS += ${LIBTEAMDCTL_CFLAGS} endif +if HAVE_SELINUX +AM_CPPFLAGS += ${SELINUX_CFLAGS} +endif + libNetworkManager_la_SOURCES = \ $(nm_sources) \ $(glue_sources) @@ -384,6 +388,10 @@ if WITH_TEAMDCTL libNetworkManager_la_LIBADD += $(LIBTEAMDCTL_LIBS) endif +if HAVE_SELINUX +libNetworkManager_la_LIBADD += $(SELINUX_LIBS) +endif + NetworkManager_LDFLAGS = -rdynamic dbusservicedir = $(DBUS_SYS_DIR) diff --git a/src/NetworkManagerUtils.c b/src/NetworkManagerUtils.c index fbd69129b9..431daa286f 100644 --- a/src/NetworkManagerUtils.c +++ b/src/NetworkManagerUtils.c @@ -15,10 +15,11 @@ * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * - * Copyright (C) 2004 - 2012 Red Hat, Inc. + * Copyright (C) 2004 - 2014 Red Hat, Inc. * Copyright (C) 2005 - 2008 Novell, Inc. */ +#include <config.h> #include <glib.h> #include <errno.h> #include <fcntl.h> @@ -26,6 +27,12 @@ #include <unistd.h> #include <stdlib.h> #include <resolv.h> +#include <sys/types.h> +#include <sys/stat.h> + +#ifdef HAVE_SELINUX +#include <selinux/selinux.h> +#endif #include "NetworkManagerUtils.h" #include "nm-utils.h" @@ -1051,3 +1058,44 @@ nm_utils_ip6_property_path (const char *ifname, const char *property) return path; } + +/** + * nm_utils_set_file_contents: + * @filename: name of a file to write contents to, yyin the GLib file name encoding + * @contents: string to write to the file + * @length: length of contents, or -1 if contents is a nul-terminated string + * @error: return location for a GError, or NULL + * + * A wrapper for g_file_set_file_contents(), but preserves SELinux content of the file. + * + * Returns: %TRUE on success, %FALSE if an error occurred + */ +gboolean +nm_utils_file_set_contents (const gchar *filename, + const gchar *contents, + gssize length, + GError **error) +{ + gboolean ret; +#if HAVE_SELINUX + security_context_t se_ctx_prev, se_ctx = NULL; + struct stat file_stat = { .st_mode = 0 }; + + /* Get default context for filename and set it for fscreate */ + stat (filename, &file_stat); + matchpathcon (filename, file_stat.st_mode, &se_ctx); + matchpathcon_fini (); + getfscreatecon (&se_ctx_prev); + setfscreatecon (se_ctx); +#endif + + ret = g_file_set_contents (filename, contents, length, error); + +#if HAVE_SELINUX + /* Restore previous context and cleanup */ + setfscreatecon (se_ctx_prev); + freecon (se_ctx); + freecon (se_ctx_prev); +#endif + return ret; +} diff --git a/src/NetworkManagerUtils.h b/src/NetworkManagerUtils.h index f9583f5f5d..3203f5ad43 100644 --- a/src/NetworkManagerUtils.h +++ b/src/NetworkManagerUtils.h @@ -107,4 +107,9 @@ gint32 nm_utils_get_monotonic_timestamp_s (void); const char *nm_utils_ip6_property_path (const char *ifname, const char *property); +gboolean nm_utils_file_set_contents (const gchar *filename, + const gchar *contents, + gssize length, + GError **error); + #endif /* NETWORK_MANAGER_UTILS_H */ |