[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH] loginutils: add portable groupadd applet
From:       Natanael Copa <ncopa () alpinelinux ! org>
Date:       2016-04-21 12:12:12
Message-ID: 1461240732-25862-1-git-send-email-ncopa () alpinelinux ! org
[Download RAW message or body]

Add groupadd applet which aims to be portable with groupadd from
shadow-utils/pkg-shadow. groupadd exists in both debian/ubuntu and
fedora/centos while addgroup only exists on debian/ubuntu.

We add the groupadd applet to improve portability, while keeping
addgroup to be backwards compatible.

function                                             old     new   delta
new_group                                              -     214    +214
groupadd_main                                          -     166    +166
.rodata                                            93576   93643     +67
packed_usage                                       19378   19413     +35
groupadd_longopts                                      -      16     +16
applet_names                                        1632    1641      +9
applet_main                                         1984    1992      +8
bbconfig_config_bz2                                 5543    5544      +1
applet_suid                                           62      63      +1
applet_install_loc                                   124     125      +1
addgroup_main                                        503     303    -200
------------------------------------------------------------------------------
(add/remove: 3/0 grow/shrink: 7/1 up/down: 518/-200)          Total: 318
bytes
   text    data     bss     dec     hex filename
 593356   11443    3096  607895   94697 busybox_old
 593627   11451    3096  608174   947ae busybox_unstripped

Signed-off-by: Natanael Copa <ncopa@alpinelinux.org>
---

I kept the addgroup applet for the following reasons:
 - We may need the backward compatible addgroup for a migration period to
   not break things.
 - the `shadow` groupadd can not add users to a group. We need keep addgroup
   til we have an applet that can add users to a group.

 libbb/Kbuild.src      |  1 +
 loginutils/addgroup.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 69 insertions(+), 1 deletion(-)

diff --git a/libbb/Kbuild.src b/libbb/Kbuild.src
index b08ce11..b2a5fdc 100644
--- a/libbb/Kbuild.src
+++ b/libbb/Kbuild.src
@@ -140,6 +140,7 @@ lib-$(CONFIG_ADDGROUP) += update_passwd.o
 lib-$(CONFIG_ADDUSER) += update_passwd.o
 lib-$(CONFIG_DELGROUP) += update_passwd.o
 lib-$(CONFIG_DELUSER) += update_passwd.o
+lib-$(CONFIG_GROUPADD) += update_passwd.o
 
 lib-$(CONFIG_FTPD) += correct_password.o
 lib-$(CONFIG_PASSWD) += pw_encrypt.o update_passwd.o obscure.o
diff --git a/loginutils/addgroup.c b/loginutils/addgroup.c
index 4d4fc3f..bc0d56c 100644
--- a/loginutils/addgroup.c
+++ b/loginutils/addgroup.c
@@ -31,9 +31,25 @@
 //config:	  addgroup will add an existing user to an
 //config:	  existing group.
 
+//config:config GROUPADD
+//config:	bool "groupadd"
+//config:	default n
+//config:	help
+//config:	  A more portable utility for creating a new group account.
+//config:
+//config:config FEATURE_GROUPADD_LONG_OPTIONS
+//config:	bool "Enable long options"
+//config:	default y
+//config:	depends on GROUPADD && LONG_OPTS
+//config:	help
+//config:	  Support long options for the groupadd applet.
+//config:
+
 //applet:IF_ADDGROUP(APPLET(addgroup, BB_DIR_USR_SBIN, BB_SUID_DROP))
+//applet:IF_GROUPADD(APPLET(groupadd, BB_DIR_USR_SBIN, BB_SUID_DROP))
 
 //kbuild:lib-$(CONFIG_ADDGROUP) += addgroup.o
+//kbuild:lib-$(CONFIG_GROUPADD) += addgroup.o
 
 //usage:#define addgroup_trivial_usage
 //usage:       "[-g GID] [-S] " IF_FEATURE_ADDUSER_TO_GROUP("[USER] ") "GROUP"
@@ -42,6 +58,13 @@
 //usage:     "\n	-g GID	Group id"
 //usage:     "\n	-S	Create a system group"
 
+//usage:#define groupadd_trivial_usage
+//usage:       "[-g GID] [-r] GROUP"
+//usage:#define groupadd_full_usage "\n\n"
+//usage:       "Add a group\n"
+//usage:     "\n	-g GID	Group id"
+//usage:     "\n	-r	Create a system group"
+
 #include "libbb.h"
 
 #if CONFIG_LAST_SYSTEM_ID < CONFIG_FIRST_SYSTEM_ID
@@ -101,6 +124,8 @@ static void new_group(char *group, gid_t gid)
 	struct group gr;
 	char *p;
 
+	die_if_bad_username(group);
+
 	/* make sure gid and group haven't already been allocated */
 	gr.gr_gid = gid;
 	gr.gr_name = group;
@@ -147,6 +172,7 @@ static const char addgroup_longopts[] ALIGN1 =
  * If called with two non-option arguments, addgroup
  * will add an existing user to an existing group.
  */
+#if ENABLE_ADDGROUP
 int addgroup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int addgroup_main(int argc UNUSED_PARAM, char **argv)
 {
@@ -200,9 +226,50 @@ int addgroup_main(int argc UNUSED_PARAM, char **argv)
 	} else
 #endif /* ENABLE_FEATURE_ADDUSER_TO_GROUP */
 	{
-		die_if_bad_username(argv[0]);
 		new_group(argv[0], xatou_range(gid, 0, CONFIG_LAST_ID));
 	}
 	/* Reached only on success */
 	return EXIT_SUCCESS;
 }
+#endif /* ENABLE_ADDGROUP */
+
+
+#if ENABLE_FEATURE_GROUPADD_LONG_OPTIONS
+static const char groupadd_longopts[] ALIGN1 =
+		"gid\0"                 Required_argument "g"
+		"system\0"              No_argument       "r"
+		;
+#endif
+
+/*
+ * groupadd will take a login_name as its first parameter.
+ *
+ * gid can be customized via command-line parameters.
+ */
+#if ENABLE_GROUPADD
+int groupadd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int groupadd_main(int argc UNUSED_PARAM, char **argv)
+{
+	unsigned opts;
+	const char *gid = "0";
+
+	/* need to be root */
+	if (geteuid()) {
+		bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
+	}
+#if ENABLE_FEATURE_GROUPADD_LONG_OPTIONS
+	applet_long_options = groupadd_longopts;
+#endif
+	/* Syntax:
+	 *  groupadd group
+	 *  groupadd -g num group
+	 * Check for min, max and missing args */
+	opt_complementary = "=1";
+	opts = getopt32(argv, "g:r", &gid);
+
+	new_group(argv[optind], xatou_range(gid, 0, CONFIG_LAST_ID));
+
+	/* Reached only on success */
+	return EXIT_SUCCESS;
+}
+#endif /* ENABLE_GROUPADD */
-- 
2.8.1

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic