summaryrefslogtreecommitdiffstats
path: root/daemon/selinux.c
diff options
context:
space:
mode:
authorRichard Jones <rjones@trick.home.annexia.org>2009-08-12 16:56:09 +0100
committerRichard Jones <rjones@trick.home.annexia.org>2009-08-13 10:51:44 +0100
commit67a679afb17747b5ec392e56cf6121b085b38a3a (patch)
treef1df14f5e357459d0c95bb6deeb202222233211a /daemon/selinux.c
parent27566d8323e4a8af59f5649aeeaef97ebd55cbd0 (diff)
downloadlibguestfs-67a679afb17747b5ec392e56cf6121b085b38a3a.tar.gz
libguestfs-67a679afb17747b5ec392e56cf6121b085b38a3a.tar.xz
libguestfs-67a679afb17747b5ec392e56cf6121b085b38a3a.zip
Add 'setcon', 'getcon' commands to set and get the SELinux context.
Diffstat (limited to 'daemon/selinux.c')
-rw-r--r--daemon/selinux.c81
1 files changed, 81 insertions, 0 deletions
diff --git a/daemon/selinux.c b/daemon/selinux.c
new file mode 100644
index 00000000..6e2b347e
--- /dev/null
+++ b/daemon/selinux.c
@@ -0,0 +1,81 @@
+/* libguestfs - the guestfsd daemon
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * 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 2 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, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#ifdef HAVE_SELINUX_SELINUX_H
+#include <selinux/selinux.h>
+#endif
+
+#include "../src/guestfs_protocol.h"
+#include "daemon.h"
+#include "actions.h"
+
+#ifdef HAVE_LIBSELINUX
+
+/* setcon is only valid under the following circumstances:
+ * - single threaded
+ * - enforcing=0
+ */
+int
+do_setcon (char *context)
+{
+#ifdef HAVE_SETCON
+ if (setcon ((char *) context) == -1) {
+ reply_with_perror ("setcon");
+ return -1;
+ }
+
+ return 0;
+#else
+ reply_with_error ("%s is not available", __func__);
+ return -1;
+#endif
+}
+
+char *
+do_getcon (void)
+{
+#ifdef HAVE_GETCON
+ security_context_t context;
+ char *r;
+
+ if (getcon (&context) == -1) {
+ reply_with_perror ("getcon");
+ return NULL;
+ }
+
+ r = strdup (context);
+ freecon (context);
+ if (r == NULL) {
+ reply_with_perror ("strdup");
+ return NULL;
+ }
+
+ return r; /* caller frees */
+#else
+ reply_with_error ("%s is not available", __func__);
+ return -1;
+#endif
+}
+
+#endif /* HAVE_LIBSELINUX */