[PATCH] implement groups applet (v3)

Tito farmatito at tiscali.it
Mon Jun 20 20:58:59 UTC 2011


On Monday 20 June 2011 18:28:21 Tanguy Pruvot wrote:
> From e76c1e9268c7bb28598ff1dbc36b0ca7e1729abb Mon Sep 17 00:00:00 2001
> From: Tanguy Pruvot <tanguy.pruvot at gmail.com>
> Date: Mon, 20 Jun 2011 16:07:31 +0200
> Subject: [PATCH] implement groups applet (v2)
> 
> Signed-off-by: Tanguy Pruvot <tanguy.pruvot at gmail.com>
> ---
>  coreutils/Config.src  |    7 +++++++
>  coreutils/Kbuild.src  |    1 +
>  coreutils/groups.c    |   40 ++++++++++++++++++++++++++++++++++++++++
>  include/applets.src.h |    1 +
>  4 files changed, 49 insertions(+), 0 deletions(-)
>  create mode 100644 coreutils/groups.c
> 
> diff --git a/coreutils/Config.src b/coreutils/Config.src
> index 1843e8b..a234b38 100644
> --- a/coreutils/Config.src
> +++ b/coreutils/Config.src
> @@ -269,6 +269,13 @@ config FEATURE_FANCY_HEAD
>  	help
>  	  This enables the head options (-c, -q, and -v).
> 
> +config GROUPS
> +	bool "groups"
> +	default y
> +	depends on ID
> +	help
> +	  Print the group names associated with the current user id
> +
>  config HOSTID
>  	bool "hostid"
>  	default y
> diff --git a/coreutils/Kbuild.src b/coreutils/Kbuild.src
> index 6a41c83..361f3e6 100644
> --- a/coreutils/Kbuild.src
> +++ b/coreutils/Kbuild.src
> @@ -44,6 +44,7 @@ lib-$(CONFIG_LN)        += ln.o
>  lib-$(CONFIG_LOGNAME)   += logname.o
>  lib-$(CONFIG_LS)        += ls.o
>  lib-$(CONFIG_FTPD)      += ls.o
> +lib-$(CONFIG_GROUPS)    += groups.o
>  lib-$(CONFIG_MD5SUM)    += md5_sha1_sum.o
>  lib-$(CONFIG_MKDIR)     += mkdir.o
>  lib-$(CONFIG_MKFIFO)    += mkfifo.o
> diff --git a/coreutils/groups.c b/coreutils/groups.c
> new file mode 100644
> index 0000000..ab07ee2
> --- /dev/null
> +++ b/coreutils/groups.c
> @@ -0,0 +1,40 @@
> +/* vi: set sw=4 ts=4: */
> +/*
> + * Mini groups implementation for busybox
> + *
> + * Copyright (C) 2011 Tanguy Pruvot <tanguy.pruvot at gmail.com>.
> + *
> + * Licensed under GPLv2 or later, see file LICENSE in this source tree.
> + */
> +
> +/* BB_AUDIT SUSv3 N/A -- Matches GNU behavior. */
> +
> +//usage:#define groups_trivial_usage
> +//usage:       ""
> +//usage:#define groups_full_usage "\n\n"
> +//usage:       "Print the group names associated with the current user id"
> +
> +#include "libbb.h"
> +
> +extern int id_main(int argc, char **argv);
> +
> +int groups_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
> +int groups_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
> +{
> +	char *id_argv[3];
> +	int ret;
> +
> +	if (argv[1])
> +		bb_show_usage();
> +
> +	id_argv[0] = xstrdup("id");
> +	id_argv[1] = xstrdup("-Gn");
> +	id_argv[2] = 0;
> +
> +	ret = id_main(2, id_argv);
> +
> +	free(id_argv[1]);
> +	free(id_argv[0]);
> +
> +	return ret;
> +}
> diff --git a/include/applets.src.h b/include/applets.src.h
> index 133f376..c3775fb 100644
> --- a/include/applets.src.h
> +++ b/include/applets.src.h
> @@ -170,6 +170,7 @@ IF_GETENFORCE(APPLET(getenforce, BB_DIR_USR_SBIN,
> BB_SUID_DROP))
>  IF_GETOPT(APPLET(getopt, BB_DIR_BIN, BB_SUID_DROP))
>  IF_GETSEBOOL(APPLET(getsebool, BB_DIR_USR_SBIN, BB_SUID_DROP))
>  IF_GETTY(APPLET(getty, BB_DIR_SBIN, BB_SUID_DROP))
> +IF_GROUPS(APPLET_NOEXEC(groups, groups, BB_DIR_USR_BIN, BB_SUID_DROP, groups))
>  IF_GUNZIP(APPLET(gunzip, BB_DIR_BIN, BB_SUID_DROP))
>  IF_GZIP(APPLET(gzip, BB_DIR_BIN, BB_SUID_DROP))
>  IF_HD(APPLET_NOEXEC(hd, hexdump, BB_DIR_USR_BIN, BB_SUID_DROP, hd))

Hi, 
just for fun one improved version of your groups patch:

pros: accepts username on the commandline
         uses id.c directly
contras: Denys will say its obfuscated ;-)

Ciao,
Tito

--- coreutils/id.c.original	2011-06-14 01:18:56.000000000 +0200
+++ coreutils/id.c	2011-06-20 22:42:04.000000000 +0200
@@ -33,6 +33,14 @@
 //usage:       "$ id\n"
 //usage:       "uid=1000(andersen) gid=1000(andersen)\n"
 
+//usage:#define groups_trivial_usage
+//usage:       "[USER]"
+//usage:#define groups_full_usage "\n\n"
+//usage:       "Print the group memberships of USER or for the current process"
+//usage:#define groups_example_usage
+//usage:       "$ groups\n"
+//usage:       "andersen lp dialout cdrom floppy\n"
+
 #include "libbb.h"
 
 /* This is a NOEXEC applet. Be very careful! */
@@ -140,7 +148,12 @@
 	/* Don't allow more than one username */
 	opt_complementary = "?1:u--g:g--u:G--u:u--G:g--G:G--g:r?ugG:n?ugG"
 			 IF_SELINUX(":u--Z:Z--u:g--Z:Z--g:G--Z:Z--G");
-	opt = getopt32(argv, "rnugG" IF_SELINUX("Z"));
+
+	if (ENABLE_GROUPS && applet_name[0] == 'g') {
+		option_mask32 = opt = getopt32(argv, "") | JUST_ALL_GROUPS | NAME_NOT_NUMBER;
+	} else {
+		opt = getopt32(argv, "rnugG" IF_SELINUX("Z"));
+	}
 
 	username = argv[optind];
 	if (username) {
--- coreutils/Config.src.original	2011-06-20 22:43:30.000000000 +0200
+++ coreutils/Config.src	2011-06-20 22:46:34.000000000 +0200
@@ -269,6 +269,12 @@
 	help
 	  This enables the head options (-c, -q, and -v).
 
+config GROUPS
+	bool "groups"
+	default y
+	help
+	  Print the group names associated with username or with the current user id
+
 config HOSTID
 	bool "hostid"
 	default y
--- coreutils/Kbuild.src.original	2011-06-20 22:48:27.000000000 +0200
+++ coreutils/Kbuild.src	2011-06-20 21:38:35.000000000 +0200
@@ -44,6 +44,7 @@
 lib-$(CONFIG_LOGNAME)   += logname.o
 lib-$(CONFIG_LS)        += ls.o
 lib-$(CONFIG_FTPD)      += ls.o
+lib-$(CONFIG_GROUPS)    += id.o
 lib-$(CONFIG_MD5SUM)    += md5_sha1_sum.o
 lib-$(CONFIG_MKDIR)     += mkdir.o
 lib-$(CONFIG_MKFIFO)    += mkfifo.o
--- include/applets.src.h.original	2011-06-20 22:49:38.000000000 +0200
+++ include/applets.src.h	2011-06-20 21:45:41.000000000 +0200
@@ -170,6 +170,7 @@
 IF_GETOPT(APPLET(getopt, BB_DIR_BIN, BB_SUID_DROP))
 IF_GETSEBOOL(APPLET(getsebool, BB_DIR_USR_SBIN, BB_SUID_DROP))
 IF_GETTY(APPLET(getty, BB_DIR_SBIN, BB_SUID_DROP))
+IF_GROUPS(APPLET_ODDNAME(groups, id, BB_DIR_USR_BIN, BB_SUID_DROP, groups))
 IF_GUNZIP(APPLET(gunzip, BB_DIR_BIN, BB_SUID_DROP))
 IF_GZIP(APPLET(gzip, BB_DIR_BIN, BB_SUID_DROP))
 IF_HD(APPLET_NOEXEC(hd, hexdump, BB_DIR_USR_BIN, BB_SUID_DROP, hd))

-------------- next part --------------
A non-text attachment was scrubbed...
Name: groups.patch
Type: text/x-diff
Size: 2525 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20110620/5d13dc65/attachment.bin>


More information about the busybox mailing list