coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[coreutils] [patch] Re: Install enhancement request: capabilities


From: Yaron Sheffer
Subject: [coreutils] [patch] Re: Install enhancement request: capabilities
Date: Thu, 04 Nov 2010 12:43:32 +0200
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.12) Gecko/20101027 Lightning/1.0b2 Thunderbird/3.1.6

Now with a patch.

Thanks,
    Yaron

On 11/03/2010 12:44 PM, Yaron Sheffer wrote:
Hi,


Posix capabilities have been in the kernel for some time, but userspace support is lagging. "Install" is one such missing piece.


I suggest to add a "--capability" flag, with syntax taken from setcap. E.g.:


sudo setcap cap_net_raw+ep /bin/ping


would become


(sudo) install --capability cap_net_raw+ep ping /bin/ping


Thanks,

    Yaron

From dac0c76ace266f482854b9d28288a7b841ce7294 Mon Sep 17 00:00:00 2001
From: Yaron Sheffer <address@hidden>
Date: Thu, 4 Nov 2010 12:11:08 +0200
Subject: [PATCH] Enable "install" to set a file's POSIX capabilities.
 Signed-off-by: Yaron Sheffer <address@hidden>

---
 doc/coreutils.texi |   12 +++++++++++
 src/Makefile.am    |    1 +
src/install.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 67 insertions(+), 1 deletions(-)

diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index be5999f..b469646 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -8242,6 +8242,18 @@ This option is useful if you want to use the last modification times
 of installed files to keep track of when they were last built as opposed
 to when they were last installed.

+@item -P @var{capability-string}
+@itemx --capabilities=@var{capability-string}
+@opindex -P
+@opindex --capabilities
+@cindex POSIX capabilities of installed files, setting
+@cindex capabilities
+If @command{install} has appropriate privileges (is run as root,
+or has the cap_setfcap capability), set the
+POSIX capabilities of installed files or directories to @var{capability-string}.
+See @code{cap_from_text(3)} for the string's syntax.
+Use @code{getcap(8)} to view the installed file capabilities.
+
 @item -s
 @itemx --strip
 @opindex -s
diff --git a/src/Makefile.am b/src/Makefile.am
index 00c7ff7..9a4607d 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -334,6 +334,7 @@ dd_LDADD += $(LIB_GETHRXTIME)

 # for cap_get_file
 ls_LDADD += $(LIB_CAP)
+ginstall_LDADD += $(LIB_CAP)

 # for fdatasync
 dd_LDADD += $(LIB_FDATASYNC)
diff --git a/src/install.c b/src/install.c
index 467e500..5f36904 100644
--- a/src/install.c
+++ b/src/install.c
@@ -25,6 +25,11 @@
 #include <grp.h>
 #include <selinux/selinux.h>

+#ifdef HAVE_CAP
+/* capability.h must be last, see comment on ls.c */
+#include <sys/capability.h>
+#endif /* HAVE_CAP */
+
 #include "system.h"
 #include "backupfile.h"
 #include "error.h"
@@ -71,6 +76,7 @@ static bool use_default_selinux_context = true;

static bool change_timestamps (struct stat const *from_sb, char const *to);
 static bool change_attributes (char const *name);
+static bool set_capabilities (char const *name, char const *caapability_string);
 static bool copy_file (const char *from, const char *to,
                        const struct cp_options *x);
 static bool install_file_in_file_parents (char const *from, char *to,
@@ -115,6 +121,9 @@ static mode_t dir_mode = DEFAULT_MODE;
    or S_ISGID bits.  */
 static mode_t dir_mode_bits = CHMOD_MODE_BITS;

+/* A POSIX capability specification string, see cap_to_text(3). */
+static char *capability_string;
+
 /* Compare files before installing (-C) */
 static bool copy_only_if_needed;

@@ -151,6 +160,9 @@ static struct option const long_options[] =
   /* --preserve_context was silently supported until Apr 2009.
      FIXME: disable altogether in a year or so.  */
{"preserve_context", no_argument, NULL, PRESERVE_CONTEXT_OPTION_DEPRECATED},
+#ifdef HAVE_CAP
+  {"capabilities", required_argument, NULL, 'P'},
+#endif /* HAVE_CAP */
   {"strip", no_argument, NULL, 's'},
   {"strip-program", required_argument, NULL, STRIP_PROGRAM_OPTION},
   {"suffix", required_argument, NULL, 'S'},
@@ -452,6 +464,7 @@ main (int argc, char **argv)

   owner_name = NULL;
   group_name = NULL;
+  capability_string = NULL;
   strip_files = false;
   dir_arg = false;
   umask (0);
@@ -460,7 +473,7 @@ main (int argc, char **argv)
      we'll actually use backup_suffix_string.  */
   backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");

- while ((optc = getopt_long (argc, argv, "bcCsDdg:m:o:pt:TvS:Z:", long_options, + while ((optc = getopt_long (argc, argv, "bcCsDdg:m:o:pP:t:TvS:Z:", long_options,
                               NULL)) != -1)
     {
       switch (optc)
@@ -507,6 +520,11 @@ main (int argc, char **argv)
         case 'p':
           x.preserve_timestamps = true;
           break;
+#ifdef HAVE_CAP
+    case 'P':
+      capability_string = optarg;
+      break;
+#endif /* HAVE_CAP */
         case 'S':
           make_backups = true;
           backup_suffix_string = optarg;
@@ -808,6 +826,11 @@ change_attributes (char const *name)
   else
     ok = true;

+#if HAVE_CAP
+  if (capability_string)
+    set_capabilities (name, capability_string);
+#endif /* HAVE_CAP */
+
   if (use_default_selinux_context)
     setdefaultfilecon (name);

@@ -817,6 +840,29 @@ change_attributes (char const *name)
 /* Set the timestamps of file TO to match those of file FROM.
    Return true if successful.  */

+#ifdef HAVE_CAP
+static bool
+set_capabilities (char const *name, char const *capability_string)
+{
+  cap_t caps;
+
+  caps = cap_from_text (capability_string);
+  if (caps == NULL) {
+ error (EXIT_FAILURE, errno, _("invalid capability string for %s"), quote (name));
+      return false;
+  }
+
+  if (cap_set_file (name, caps) == -1) {
+ error (EXIT_FAILURE, errno, _("cannot set capabilities on %s"), quote (name));
+      cap_free (caps);
+      return false;
+  }
+
+  cap_free (caps);
+  return true;
+}
+#endif /* HAVE_CAP */
+
 static bool
 change_timestamps (struct stat const *from_sb, char const *to)
 {
@@ -977,6 +1023,13 @@ Mandatory arguments to long options are mandatory for short options too.\n\
       fputs (_("\
-p, --preserve-timestamps apply access/modification times of SOURCE files\n\
                         to corresponding destination files\n\
+"), stdout);
+#ifdef HAVE_CAP
+      fputs (_("\
+ -P, --capabilities set POSIX capabilities, to allow granular permissions\n\
+"), stdout);
+#endif /* HAVE_CAP */
+      fputs (_("\
   -s, --strip         strip symbol tables\n\
       --strip-program=PROGRAM  program used to strip binaries\n\
   -S, --suffix=SUFFIX  override the usual backup suffix\n\
--
1.7.1





reply via email to

[Prev in Thread] Current Thread [Next in Thread]