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

List:       busybox
Subject:    Re: [BusyBox] [PATCH] add -G to id]
From:       "Tony J. White" <tjw () webteam ! net>
Date:       2004-12-15 16:17:22
Message-ID: 20041215161722.GA29387 () morbo ! tjw ! org
[Download RAW message or body]

It looks like mailman ate this message when I attached a pgp signature.

----- Forwarded message from "Tony J. White" <tjw@webteam.net> -----
On Thu, Dec 09, 2004 at 11:55:52AM +0300, Vladimir N. Oleynik wrote:
> >    bb_opt_complementaly = "u~g:g~u:G~u:u~G:G~g:g~G";
> Or not. ;) (25 bytes with '\0' aling to 28 bytes)
> But
> bb_opt_complementaly = "u~g:g~u:G~ug:u~G:g~G";
> 21 bytes aligned to 24 bytes and may be save some space and identicaly.

Here is a patch that makes use of bb_opt_complementaly.  I've verified
that it does work as expected.

The new patch is here too: http://tjw.org/id/

-Tony


diff -urN busybox-1.00.orig/coreutils/Config.in busybox-1.00/coreutils/Config.in
--- busybox-1.00.orig/coreutils/Config.in	2004-08-10 21:45:47.000000000 -0500
+++ busybox-1.00/coreutils/Config.in	2004-12-15 09:28:34.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.orig/coreutils/id.c busybox-1.00/coreutils/id.c
--- busybox-1.00.orig/coreutils/id.c	2004-09-14 22:04:07.000000000 -0500
+++ busybox-1.00/coreutils/id.c	2004-12-15 09:29:20.000000000 -0600
@@ -3,6 +3,7 @@
  * Mini id implementation for busybox
  *
  * Copyright (C) 2000 by Randolph Chung <tausq@debian.org>
+ * Copyright (C) 2004 by Tony J. White <tjw@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. */
 /* Hacked by Tito Ragusa (C) 2004 to handle usernames of whatever length and to
  * be more similar to GNU id. 
  */
@@ -29,6 +29,7 @@
 #include "pwd_.h"
 #include <stdio.h>
 #include <unistd.h>
+#include <string.h>
 #include <sys/types.h>
 
 #ifdef CONFIG_SELINUX
@@ -40,6 +41,43 @@
 #define NAME_NOT_NUMBER   2
 #define JUST_USER         4
 #define JUST_GROUP        8
+#define PRINT_GROUPS     16
+
+static short id_print_groups(uid_t uid, gid_t gid, unsigned long flags) {
+	FILE * etc_group;
+	struct group *grp;
+	int init = 0;
+	char **member;
+	char *user;
+
+	user = my_getpwuid(NULL, uid, -1 ); 
+
+	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(grp->gr_gid == gid) continue;
+				if(flags & NAME_NOT_NUMBER)
+					bb_printf(" %s", grp->gr_name);
+				else
+					bb_printf(" %u", grp->gr_gid);
+			}
+			else {
+				if(!init) {
+					bb_printf(" groups=");
+					init = 1;
+				}
+				else printf(",");
+				bb_printf("%u(%s)", grp->gr_gid, grp->gr_name);
+			}
+		}
+	}
+	fclose(etc_group);
+	return EXIT_SUCCESS; 
+}
+
 
 static short printf_full(unsigned int id, const char *arg, const char prefix)
 {	
@@ -65,8 +103,14 @@
 	int is_flask_enabled_flag = is_flask_enabled();
 #endif
 
-	bb_opt_complementaly = "u~g:g~u";
-	flags = bb_getopt_ulflags(argc, argv, "rnug");
+	bb_opt_complementaly = "u~g:g~u:G~ug:u~G:g~G";
+	flags = bb_getopt_ulflags(argc, argv, "rnugG");
+
+#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 & 0x80000000UL)
 	/* Don't allow -n -r -nr */
@@ -91,14 +135,19 @@
 		/* in this case PRINT_REAL is the same */ 
 	}
 
-	if(flags & (JUST_GROUP | JUST_USER)) {
-		/* JUST_GROUP and JUST_USER are mutually exclusive */
+	if(flags & (JUST_GROUP | JUST_USER | PRINT_GROUPS)) {
+		/* JUST_GROUP and JUST_USER and PRINT_GROUPS are mutually exclusive */
 		if(flags & NAME_NOT_NUMBER) {
-			/* my_getpwuid and my_getgrgid exit on failure so puts cannot segfault */
-			puts((flags & JUST_USER) ? my_getpwuid(NULL, uid, -1 ) : my_getgrgid(NULL, gid, -1 ));
+			/* my_getpwuid and my_getgrgid exit on failure so printf cannot 
+			 * segfault */
+			bb_printf("%s", ((flags & JUST_USER) ? 
+				my_getpwuid(NULL, uid, -1 ) : my_getgrgid(NULL, gid, -1 )));
 		} else {
-			bb_printf("%u\n",(flags & JUST_USER) ? uid : gid);
+			bb_printf("%u",(flags & JUST_USER) ? uid : gid);
 		}
+		/* print any member groups */
+		if(flags & PRINT_GROUPS) id_print_groups(uid, gid, flags);
+		putchar('\n');
 		/* exit */ 
 		bb_fflush_stdout_and_exit(EXIT_SUCCESS);
 	}
@@ -107,8 +156,13 @@
 	/* my_getpwuid doesn't exit on failure here */
 	status=printf_full(uid, my_getpwuid(NULL, uid, 0), 'u');
 	putchar(' ');
+
 	/* my_getgrgid doesn't exit on failure here */
 	status|=printf_full(gid, my_getgrgid(NULL, gid, 0), 'g');
+
+	/* print all member groups */
+	status|=id_print_groups(uid, gid, flags);
+
 #ifdef CONFIG_SELINUX
 	if(is_flask_enabled_flag) {
 		security_id_t mysid = getsecsid();
diff -urN busybox-1.00.orig/include/applets.h busybox-1.00/include/applets.h
--- busybox-1.00.orig/include/applets.h	2004-08-26 18:01:34.000000000 -0500
+++ busybox-1.00/include/applets.h	2004-12-15 09:28:34.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.orig/include/usage.h busybox-1.00/include/usage.h
--- busybox-1.00.orig/include/usage.h	2004-09-14 11:23:56.000000000 -0500
+++ busybox-1.00/include/usage.h	2004-12-15 09:28:34.000000000 -0600
@@ -805,6 +805,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 \
@@ -1052,7 +1062,8 @@
 	"\t-g\tprints only the group ID\n" \
 	"\t-u\tprints only the user ID\n" \
 	"\t-n\tprint a name instead of a number\n" \
-	"\t-r\tprints the real user ID instead of the effective ID"
+	"\t-r\tprints the real user ID instead of the effective ID\n" \
+	"\t-G\tprints all groups the user belongs to"
 #define id_example_usage \
 	"$ id\n" \
 	"uid=1000(andersen) gid=1000(andersen)\n"






_______________________________________________
busybox mailing list
busybox@mail.busybox.net
http://codepoet.org/mailman/listinfo/busybox


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

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