]> granicus.if.org Git - shadow/commitdiff
* lib/prototypes.h, lib/Makefile.am, po/POTFILES.in,
authornekral-guest <nekral-guest@5a98b0ae-9ef6-0310-add3-de5d479b70d7>
Fri, 9 Dec 2011 22:13:02 +0000 (22:13 +0000)
committernekral-guest <nekral-guest@5a98b0ae-9ef6-0310-add3-de5d479b70d7>
Fri, 9 Dec 2011 22:13:02 +0000 (22:13 +0000)
libmisc/copydir.c, lib/selinux.c: Move set_selinux_file_context()
and reset_selinux_file_context() from libmisc/copydir.c to
lib/selinux.c.
* lib/commonio.c: Use set_selinux_file_context() and
reset_selinux_file_context() instead of using the existing
database SELinux context to set the context for the newly created
files.

ChangeLog
lib/Makefile.am
lib/commonio.c
lib/prototypes.h
lib/selinux.c [new file with mode: 0644]
libmisc/copydir.c
po/POTFILES.in

index 2c76703cdd073202290788f3bce19aadae15fe00..dc4a739a2b5dcf323397450c8edb4918be15ccdb 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2011-12-09  Peter Vrabec  <pvrabec@redhat.com>
+
+       * lib/prototypes.h, lib/Makefile.am, po/POTFILES.in,
+       libmisc/copydir.c, lib/selinux.c: Move set_selinux_file_context()
+       and reset_selinux_file_context() from libmisc/copydir.c to
+       lib/selinux.c.
+       * lib/commonio.c: Use set_selinux_file_context() and
+       reset_selinux_file_context() instead of using the existing
+       database SELinux context to set the context for the newly created
+       files.
+
 2011-12-09  Nicolas François  <nicolas.francois@centraliens.net>
 
        * src/vipw.c: Do not use a hardcoded program name in the usage
index 8a1df3bd7f0c014bc20bff35cdd66a9c2f58ceb7..c448dd30281aae8e69e32397de40882373f75116 100644 (file)
@@ -39,6 +39,7 @@ libshadow_la_SOURCES = \
        pwio.c \
        pwio.h \
        pwmem.c \
+       selinux.c \
        semanage.c \
        sgetgrent.c \
        sgetpwent.c \
index dbc0990a239f40d138d964b039a5aebf72685f41..11e46cd00f4e308654512a7442ddf9cb378b5381 100644 (file)
@@ -45,9 +45,6 @@
 #include <stdio.h>
 #include <signal.h>
 #include "nscd.h"
-#ifdef WITH_SELINUX
-#include <selinux/selinux.h>
-#endif                         /* WITH_SELINUX */
 #ifdef WITH_TCB
 #include <tcb.h>
 #endif                         /* WITH_TCB */
@@ -652,15 +649,6 @@ int commonio_open (struct commonio_db *db, int mode)
        /* Do not inherit fd in spawned processes (e.g. nscd) */
        fcntl (fileno (db->fp), F_SETFD, FD_CLOEXEC);
 
-#ifdef WITH_SELINUX
-       db->scontext = NULL;
-       if ((is_selinux_enabled () > 0) && (!db->readonly)) {
-               if (fgetfilecon (fileno (db->fp), &db->scontext) < 0) {
-                       goto cleanup_errno;
-               }
-       }
-#endif                         /* WITH_SELINUX */
-
        buflen = BUFLEN;
        buf = (char *) malloc (buflen);
        if (NULL == buf) {
@@ -745,12 +733,6 @@ int commonio_open (struct commonio_db *db, int mode)
       cleanup_errno:
        saved_errno = errno;
        free_linked_list (db);
-#ifdef WITH_SELINUX
-       if (db->scontext != NULL) {
-               freecon (db->scontext);
-               db->scontext = NULL;
-       }
-#endif                         /* WITH_SELINUX */
        fclose (db->fp);
        db->fp = NULL;
        errno = saved_errno;
@@ -932,10 +914,6 @@ int commonio_close (struct commonio_db *db)
        int errors = 0;
        struct stat sb;
 
-#ifdef WITH_SELINUX
-       /*@null@*/security_context_t old_context = NULL;
-#endif                         /* WITH_SELINUX */
-
        if (!db->isopen) {
                errno = EINVAL;
                return 0;
@@ -959,23 +937,17 @@ int commonio_close (struct commonio_db *db)
                        db->fp = NULL;
                        goto fail;
                }
-#ifdef WITH_SELINUX
-               if (db->scontext != NULL) {
-                       if (getfscreatecon (&old_context) < 0) {
-                               errors++;
-                               goto fail;
-                       }
-                       if (setfscreatecon (db->scontext) < 0) {
-                               errors++;
-                               goto fail;
-                       }
-               }
-#endif                         /* WITH_SELINUX */
+
                /*
                 * Create backup file.
                 */
                snprintf (buf, sizeof buf, "%s-", db->filename);
 
+#ifdef WITH_SELINUX
+               if (set_selinux_file_context (buf) != 0) {
+                       errors++;
+               }
+#endif
                if (create_backup (buf, db->fp) != 0) {
                        errors++;
                }
@@ -984,6 +956,11 @@ int commonio_close (struct commonio_db *db)
                        errors++;
                }
 
+#ifdef WITH_SELINUX
+               if (reset_selinux_file_context () != 0) {
+                       errors++;
+               }
+#endif
                if (errors != 0) {
                        db->fp = NULL;
                        goto fail;
@@ -1040,19 +1017,6 @@ int commonio_close (struct commonio_db *db)
        errors++;
       success:
 
-#ifdef WITH_SELINUX
-       if (db->scontext != NULL) {
-               if (NULL != old_context) {
-               if (setfscreatecon (old_context) < 0) {
-                       errors++;
-               }
-                       freecon (old_context);
-                       old_context = NULL;
-               }
-               freecon (db->scontext);
-               db->scontext = NULL;
-       }
-#endif                         /* WITH_SELINUX */
        free_linked_list (db);
        return errors == 0;
 }
index 85758b8d23a3b7b00c4ef0e7e1c5caa416c8a081..f001a331d6acf23ecb80df7fc07745e5d7072232 100644 (file)
@@ -297,6 +297,10 @@ extern void process_root_flag (const char* short_opt, int argc, char **argv);
 /* salt.c */
 extern /*@observer@*/const char *crypt_make_salt (/*@null@*//*@observer@*/const char *meth, /*@null@*/void *arg);
 
+/* selinux.c */
+extern int set_selinux_file_context (const char *dst_name);
+extern int reset_selinux_file_context (void);
+
 /* semanage.c */
 extern int set_seuser(const char *login_name, const char *seuser_name);
 extern int del_seuser(const char *login_name);
diff --git a/lib/selinux.c b/lib/selinux.c
new file mode 100644 (file)
index 0000000..7c29f79
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2011       , Peter Vrabec <pvrabec@redhat.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the copyright holders or contributors may not be used to
+ *    endorse or promote products derived from this software without
+ *    specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
+ * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <config.h>
+
+#ifdef WITH_SELINUX
+
+#include "defines.h"
+
+#include <selinux/selinux.h>
+
+
+static bool selinux_checked = false;
+static bool selinux_enabled;
+
+/*
+ * set_selinux_file_context - Set the security context before any file or
+ *                            directory creation.
+ *
+ *     set_selinux_file_context () should be called before any creation
+ *     of file, symlink, directory, ...
+ *
+ *     Callers may have to Reset SELinux to create files with default
+ *     contexts with reset_selinux_file_context
+ */
+int set_selinux_file_context (const char *dst_name)
+{
+       /*@null@*/security_context_t scontext = NULL;
+
+       if (!selinux_checked) {
+               selinux_enabled = is_selinux_enabled () > 0;
+               selinux_checked = true;
+       }
+
+       if (selinux_enabled) {
+               /* Get the default security context for this file */
+               if (matchpathcon (dst_name, 0, &scontext) < 0) {
+                       if (security_getenforce () != 0) {
+                               return 1;
+                       }
+               }
+               /* Set the security context for the next created file */
+               if (setfscreatecon (scontext) < 0) {
+                       if (security_getenforce () != 0) {
+                               return 1;
+                       }
+               }
+               freecon (scontext);
+       }
+       return 0;
+}
+
+/*
+ * reset_selinux_file_context - Reset the security context to the default
+ *                              policy behavior
+ *
+ *     reset_selinux_file_context () should be called after the context
+ *     was changed with set_selinux_file_context ()
+ */
+int reset_selinux_file_context (void)
+{
+       if (!selinux_checked) {
+               selinux_enabled = is_selinux_enabled () > 0;
+               selinux_checked = true;
+       }
+       if (selinux_enabled) {
+               if (setfscreatecon (NULL) != 0) {
+                       return 1;
+               }
+       }
+       return 0;
+}
+
+#else                          /* !WITH_SELINUX */
+extern int errno;              /* warning: ANSI C forbids an empty source file */
+#endif                         /* !WITH_SELINUX */
index 93f2b4f20102b6dc7ece9883facaf60e9a055cc3..7cb5f568b8c2fa8dd7e0d345a85c4bf4cbe146d4 100644 (file)
 #include <attr/libattr.h>
 #endif                         /* WITH_ATTR */
 
-#ifdef WITH_SELINUX
-static bool selinux_checked = false;
-static bool selinux_enabled;
-#endif                         /* WITH_SELINUX */
 
 static /*@null@*/const char *src_orig;
 static /*@null@*/const char *dst_orig;
@@ -112,66 +108,6 @@ static int fchown_if_needed (int fdst, const struct stat *statp,
                              uid_t old_uid, uid_t new_uid,
                              gid_t old_gid, gid_t new_gid);
 
-#ifdef WITH_SELINUX
-/*
- * set_selinux_file_context - Set the security context before any file or
- *                            directory creation.
- *
- *     set_selinux_file_context () should be called before any creation
- *     of file, symlink, directory, ...
- *
- *     Callers may have to Reset SELinux to create files with default
- *     contexts with reset_selinux_file_context
- */
-int set_selinux_file_context (const char *dst_name)
-{
-       /*@null@*/security_context_t scontext = NULL;
-
-       if (!selinux_checked) {
-               selinux_enabled = is_selinux_enabled () > 0;
-               selinux_checked = true;
-       }
-
-       if (selinux_enabled) {
-               /* Get the default security context for this file */
-               if (matchpathcon (dst_name, 0, &scontext) < 0) {
-                       if (security_getenforce () != 0) {
-                               return 1;
-                       }
-               }
-               /* Set the security context for the next created file */
-               if (setfscreatecon (scontext) < 0) {
-                       if (security_getenforce () != 0) {
-                               return 1;
-                       }
-               }
-               freecon (scontext);
-       }
-       return 0;
-}
-
-/*
- * reset_selinux_file_context - Reset the security context to the default
- *                              policy behavior
- *
- *     reset_selinux_file_context () should be called after the context
- *     was changed with set_selinux_file_context ()
- */
-int reset_selinux_file_context (void)
-{
-       if (!selinux_checked) {
-               selinux_enabled = is_selinux_enabled () > 0;
-               selinux_checked = true;
-       }
-       if (selinux_enabled) {
-               if (setfscreatecon (NULL) != 0) {
-                       return 1;
-               }
-       }
-       return 0;
-}
-#endif                         /* WITH_SELINUX */
-
 #if defined(WITH_ACL) || defined(WITH_ATTR)
 /*
  * error_acl - format the error messages for the ACL and EQ libraries.
index eca476aec36f617c595d13dfdfc8e8c51f08214c..1758f49eb79b8c302b0bd77818e3a21976e13e6e 100644 (file)
@@ -17,6 +17,7 @@ lib/port.c
 lib/pwauth.c
 lib/pwio.c
 lib/pwmem.c
+lib/selinux.c
 lib/semanage.c
 lib/sgetgrent.c
 lib/sgetpwent.c