[BusyBox] [feature patch] id -G support plus groups applet

Tony J. White tjw at webteam.net
Fri Mar 12 22:12:02 UTC 2004


Here's another one for the patches directory if it looks OK.

See URL for more information about the patch:
http://tjw.org/id/

-Tony

-------------- next part --------------
diff -urN busybox-1.00-pre8.orig/coreutils/Config.in busybox-1.00-pre8/coreutils/Config.in
--- busybox-1.00-pre8.orig/coreutils/Config.in	2004-01-27 03:22:20.000000000 -0600
+++ busybox-1.00-pre8/coreutils/Config.in	2004-03-12 15:50:06.000000000 -0600
@@ -218,6 +218,14 @@
 	help
 	  id displays the current user and group ID names.
 
+config CONFIG_FEATURE_ID_GROUPS_ALIAS
+        bool "  Support 'groups' as alias to 'id -Gn'"
+        default y
+        depends on CONFIG_ID
+        help
+	  Print the groups a user is in.  This is an alias to 'id -Gn' on
+	  most systems.
+
 config CONFIG_INSTALL
 	bool "install"
 	default n
diff -urN busybox-1.00-pre8.orig/coreutils/id.c busybox-1.00-pre8/coreutils/id.c
--- busybox-1.00-pre8.orig/coreutils/id.c	2003-07-03 05:06:58.000000000 -0500
+++ busybox-1.00-pre8/coreutils/id.c	2004-03-12 15:54:45.000000000 -0600
@@ -3,6 +3,7 @@
  * Mini id implementation for busybox
  *
  * Copyright (C) 2000 by Randolph Chung <tausq at debian.org>
+ * Copyright (C) 2004 by Tony J. White <tjw at tjw.org>
  *
  * 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
@@ -20,7 +21,6 @@
  *
  */
 
-/* BB_AUDIT SUSv3 _NOT_ compliant -- option -G is not currently supported. */
 
 #include "busybox.h"
 #include <stdio.h>
@@ -37,6 +37,40 @@
 #define JUST_GROUP        2
 #define PRINT_REAL        4
 #define NAME_NOT_NUMBER   8
+#define PRINT_GROUPS     16
+
+
+static int id_print_groups(char *user, char *group, int flags) {
+	FILE * etc_group;
+	struct group *grp;
+	int init = 0;
+	char **member;
+
+	etc_group = bb_xfopen(bb_path_group_file, "r");	
+	while((grp = fgetgrent(etc_group))) {
+		for(member = grp->gr_mem; *member; ++member) {
+			if(strcmp(*member, user)) continue;
+			if(flags & PRINT_GROUPS) {
+				/* don't print default group twice */
+				if(!(strcmp(grp->gr_name, group))) continue;
+				if(flags & NAME_NOT_NUMBER) 
+					printf(" %s", grp->gr_name);
+				else
+					printf(" %u", grp->gr_gid);
+			}
+			else {
+				if(!init) {
+					printf(" groups=");
+					init = 1;
+				}
+				else printf(",");
+				printf("%u(%s)", grp->gr_gid, grp->gr_name);
+			}
+		}
+	}
+	fclose(etc_group);
+	return 1;
+}
 
 extern int id_main(int argc, char **argv)
 {
@@ -48,10 +82,18 @@
 	int is_flask_enabled_flag = is_flask_enabled();
 #endif
 	
-	flags = bb_getopt_ulflags(argc, argv, "ugrn");
+	flags = bb_getopt_ulflags(argc, argv, "ugrnG");
+
+#ifdef CONFIG_FEATURE_ID_GROUPS_ALIAS
+	/* groups command is an alias for 'id -Gn' */
+	if(bb_applet_name[0] == 'g') 
+		flags |= (PRINT_GROUPS + NAME_NOT_NUMBER);
+#endif
 
 	if (((flags & (JUST_USER | JUST_GROUP)) == (JUST_USER | JUST_GROUP))
 		|| (argc > optind + 1)
+		|| (flags & PRINT_GROUPS && 
+			(flags & JUST_USER || flags & JUST_GROUP))
 	) {
 		bb_show_usage();
 	}
@@ -67,27 +109,30 @@
 		my_getpwuid(user, uid);
 	} else {
 		safe_strncpy(user, argv[optind], sizeof(user));
-	    gid = my_getpwnamegid(user);
+		gid = my_getpwnamegid(user);
 	}
 	my_getgrgid(group, gid);
 
 	pwnam=my_getpwnam(user);
 	grnam=my_getgrnam(group);
 
-	if (flags & (JUST_GROUP | JUST_USER)) {
+	if (flags & (JUST_GROUP | JUST_USER | PRINT_GROUPS)) {
 		char *s = group;
 		if (flags & JUST_USER) {
 			s = user;
 			grnam = pwnam;
 		}
 		if (flags & NAME_NOT_NUMBER) {
-			puts(s);
+			printf("%s", s);
 		} else {
-			printf("%ld\n", grnam);
+			printf("%ld", grnam);
 		}
+		if(flags & PRINT_GROUPS) id_print_groups(user, group, flags);
+		printf("\n");
 	} else {
-#ifdef CONFIG_SELINUX
 		printf("uid=%ld(%s) gid=%ld(%s)", pwnam, user, grnam, group);
+		id_print_groups(user, group, flags);
+#ifdef CONFIG_SELINUX
 		if(is_flask_enabled_flag)
 		{
 			security_id_t mysid = getsecsid();
@@ -98,13 +143,8 @@
 				strcpy(context, "unknown");
 			printf(" context=%s\n", context);
 		}
-		else
-			printf("\n");
-#else
-		printf("uid=%ld(%s) gid=%ld(%s)\n", pwnam, user, grnam, group);
 #endif
-
+		printf("\n");
 	}
-
 	bb_fflush_stdout_and_exit(0);
 }
diff -urN busybox-1.00-pre8.orig/include/applets.h busybox-1.00-pre8/include/applets.h
--- busybox-1.00-pre8.orig/include/applets.h	2004-01-27 03:22:20.000000000 -0600
+++ busybox-1.00-pre8/include/applets.h	2004-03-12 15:50:06.000000000 -0600
@@ -232,6 +232,9 @@
 #ifdef CONFIG_GREP
 	APPLET(grep, grep_main, _BB_DIR_BIN, _BB_SUID_NEVER)
 #endif
+#if defined(CONFIG_FEATURE_ID_GROUPS_ALIAS)
+	APPLET(groups, id_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
+#endif
 #ifdef CONFIG_GUNZIP
 	APPLET(gunzip, gunzip_main, _BB_DIR_BIN, _BB_SUID_NEVER)
 #endif
diff -urN busybox-1.00-pre8.orig/include/usage.h busybox-1.00-pre8/include/usage.h
--- busybox-1.00-pre8.orig/include/usage.h	2004-02-22 06:25:47.000000000 -0600
+++ busybox-1.00-pre8/include/usage.h	2004-03-12 15:50:06.000000000 -0600
@@ -794,6 +794,16 @@
 	"$ grep ^[rR]oo. /etc/passwd\n" \
 	"root:x:0:0:root:/root:/bin/bash\n"
 
+#define groups_trivial_usage \
+	" [USERNAME]"
+#define groups_full_usage \
+	"Print all group names that USERNAME is a member of." 
+#define groups_example_usage \
+	"$ groups\n" \
+	"andersen users\n" \
+	"$ groups tjw\n" \
+	"tjw users\n"
+
 #define gunzip_trivial_usage \
 	"[OPTION]... FILE"
 #define gunzip_full_usage \
@@ -1035,10 +1045,13 @@
 	"\t-u\tprints only the user ID\n" \
 	USAGE_SELINUX("\t-c\tprints only the security context\n", "") \
 	"\t-n\tprint a name instead of a number (with for -ug)\n" \
-	"\t-r\tprints the real user ID instead of the effective ID (with -ug)"
+	"\t-r\tprints the real user ID instead of the effective ID " \
+		"(with -ug)\n" \
+	"\t-G\tprints all groups the user belongs to"
 #define id_example_usage \
 	"$ id\n" \
-	"uid=1000(andersen) gid=1000(andersen)\n"
+	"uid=1000(andersen) gid=1000(andersen)" \
+		" groups=1000(andersen),100(users)\n"
 
 #ifdef CONFIG_FEATURE_IFCONFIG_SLIP
   #define USAGE_SIOCSKEEPALIVE(a) a
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.busybox.net/pipermail/busybox/attachments/20040312/648f4e81/attachment-0002.pgp 


More information about the busybox mailing list