2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-22 01:49:11 +00:00

Add getgids utility to simular "id -G" using sudo_getgrouplist2()

This commit is contained in:
Todd C. Miller 2021-06-07 14:18:09 -06:00
parent 3104d8ba0b
commit dfaa9b95f1
3 changed files with 106 additions and 3 deletions

View File

@ -237,6 +237,7 @@ lib/util/regress/fnmatch/fnm_test.in
lib/util/regress/fuzz/fuzz_sudo_conf.c
lib/util/regress/fuzz/fuzz_sudo_conf.dict
lib/util/regress/getdelim/getdelim_test.c
lib/util/regress/getgrouplist/getgids.c
lib/util/regress/getgrouplist/getgrouplist_test.c
lib/util/regress/glob/files
lib/util/regress/glob/globtest.c

View File

@ -105,7 +105,7 @@ PVS_LOG_OPTS = -a 'GA:1,2' -e -t errorfile -d $(PVS_IGNORE)
# Regression tests
TEST_PROGS = conf_test hltq_test parseln_test progname_test strsplit_test \
strtobool_test strtoid_test strtomode_test strtonum_test \
parse_gids_test getgrouplist_test @COMPAT_TEST_PROGS@
parse_gids_test getgids getgrouplist_test @COMPAT_TEST_PROGS@
TEST_LIBS = @LIBS@
TEST_LDFLAGS = @LDFLAGS@
@ -170,6 +170,8 @@ STRSPLIT_TEST_OBJS = strsplit_test.lo strsplit.lo
PARSE_GIDS_TEST_OBJS = parse_gids_test.lo gidlist.lo
GETGIDS_OBJS = getgids.lo getgrouplist.lo
GETGROUPLIST_TEST_OBJS = getgrouplist_test.lo getgrouplist.lo
STRSIG_TEST_OBJS = strsig_test.lo sig2str.lo str2sig.lo @SIGNAME@
@ -269,6 +271,9 @@ progname_test: $(PROGNAME_TEST_OBJS)
parse_gids_test: $(PARSE_GIDS_TEST_OBJS) libsudo_util.la
$(LIBTOOL) $(LTFLAGS) --mode=link $(CC) -o $@ $(PARSE_GIDS_TEST_OBJS) libsudo_util.la $(ASAN_LDFLAGS) $(PIE_LDFLAGS) $(SSP_LDFLAGS) $(TEST_LDFLAGS) $(TEST_LIBS)
getgids: $(GETGIDS_OBJS) libsudo_util.la
$(LIBTOOL) $(LTFLAGS) --mode=link $(CC) -o $@ $(GETGIDS_OBJS) libsudo_util.la $(ASAN_LDFLAGS) $(PIE_LDFLAGS) $(SSP_LDFLAGS) $(TEST_LDFLAGS) $(TEST_LIBS)
getgrouplist_test: $(GETGROUPLIST_TEST_OBJS) libsudo_util.la
$(LIBTOOL) $(LTFLAGS) --mode=link $(CC) -o $@ $(GETGROUPLIST_TEST_OBJS) libsudo_util.la $(ASAN_LDFLAGS) $(PIE_LDFLAGS) $(SSP_LDFLAGS) $(TEST_LDFLAGS) $(TEST_LIBS)
@ -781,12 +786,26 @@ getentropy.i: $(srcdir)/getentropy.c $(incdir)/sudo_compat.h \
$(CC) -E -o $@ $(CPPFLAGS) $<
getentropy.plog: getentropy.i
rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/getentropy.c --i-file $< --output-file $@
getgids.lo: $(srcdir)/regress/getgrouplist/getgids.c \
$(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \
$(incdir)/sudo_fatal.h $(incdir)/sudo_plugin.h \
$(incdir)/sudo_util.h $(top_builddir)/config.h
$(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(srcdir)/regress/getgrouplist/getgids.c
getgids.i: $(srcdir)/regress/getgrouplist/getgids.c \
$(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \
$(incdir)/sudo_fatal.h $(incdir)/sudo_plugin.h \
$(incdir)/sudo_util.h $(top_builddir)/config.h
$(CC) -E -o $@ $(CPPFLAGS) $<
getgids.plog: getgids.i
rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/regress/getgrouplist/getgids.c --i-file $< --output-file $@
getgrouplist.lo: $(srcdir)/getgrouplist.c $(incdir)/compat/nss_dbdefs.h \
$(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \
$(incdir)/sudo_debug.h $(incdir)/sudo_queue.h \
$(incdir)/sudo_util.h $(top_builddir)/config.h
$(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(srcdir)/getgrouplist.c
getgrouplist.i: $(srcdir)/getgrouplist.c $(incdir)/compat/nss_dbdefs.h \
$(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \
$(incdir)/sudo_debug.h $(incdir)/sudo_queue.h \
$(incdir)/sudo_util.h $(top_builddir)/config.h
$(CC) -E -o $@ $(CPPFLAGS) $<
getgrouplist.plog: getgrouplist.i
@ -1126,10 +1145,12 @@ pwrite.i: $(srcdir)/pwrite.c $(incdir)/sudo_compat.h $(top_builddir)/config.h
pwrite.plog: pwrite.i
rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/pwrite.c --i-file $< --output-file $@
rcstr.lo: $(srcdir)/rcstr.c $(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \
$(incdir)/sudo_util.h $(top_builddir)/config.h
$(incdir)/sudo_debug.h $(incdir)/sudo_queue.h $(incdir)/sudo_util.h \
$(top_builddir)/config.h
$(LIBTOOL) $(LTFLAGS) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(ASAN_CFLAGS) $(PIE_CFLAGS) $(SSP_CFLAGS) $(srcdir)/rcstr.c
rcstr.i: $(srcdir)/rcstr.c $(incdir)/compat/stdbool.h $(incdir)/sudo_compat.h \
$(incdir)/sudo_util.h $(top_builddir)/config.h
$(incdir)/sudo_debug.h $(incdir)/sudo_queue.h $(incdir)/sudo_util.h \
$(top_builddir)/config.h
$(CC) -E -o $@ $(CPPFLAGS) $<
rcstr.plog: rcstr.i
rm -f $@; pvs-studio --cfg $(PVS_CFG) --sourcetree-root $(top_srcdir) --skip-cl-exe yes --source-file $(srcdir)/rcstr.c --i-file $< --output-file $@

View File

@ -0,0 +1,81 @@
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2021 Todd C. Miller <Todd.Miller@sudo.ws>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STDBOOL_H
# include <stdbool.h>
#else
# include "compat/stdbool.h"
#endif
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#define SUDO_ERROR_WRAP 0
#include "sudo_compat.h"
#include "sudo_fatal.h"
#include "sudo_util.h"
sudo_dso_public int main(int argc, char *argv[]);
/*
* Implement "id -G" using sudo_getgrouplist2().
*/
int
main(int argc, char *argv[])
{
char *username = NULL;
GETGROUPS_T *groups = NULL;
struct passwd *pw;
int i, ngroups;
gid_t basegid;
initprogname(argc > 0 ? argv[0] : "getgids");
if (getopt(argc, argv, "") != -1) {
fprintf(stderr, "usage: %s [user]\n", getprogname());
return EXIT_FAILURE;
}
if (argc > 1)
username = argv[1];
if (username != NULL) {
if ((pw = getpwnam(username)) == NULL)
sudo_fatalx("unknown user name %s", username);
} else {
if ((pw = getpwuid(getuid())) == NULL)
sudo_fatalx("unknown user ID %u", (unsigned int)getuid());
}
basegid = pw->pw_gid;
if ((username = strdup(pw->pw_name)) == NULL)
sudo_fatal(NULL);
if (sudo_getgrouplist2(username, basegid, &groups, &ngroups) == -1)
sudo_fatal("sudo_getgroulist2");
for (i = 0; i < ngroups; i++) {
printf("%s%u", i ? " " : "", (unsigned int)groups[i]);
}
putchar('\n');
return EXIT_SUCCESS;
}