summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2014-06-05 23:16:23 +0200
committerArthur de Jong <arthur@arthurdejong.org>2014-06-05 23:16:23 +0200
commit13483f9f91c1fe4f3257f1d6b42eb1d01b3c6ba3 (patch)
treeb3170a651f2eb0c566e396f0ef2cf19a0425bbb1 /tests
parent3d29861e86e2044dc4dfbf6f2615c3e567c7a4f6 (diff)
Introduce lookup_groupbyuser test command
This command can be used to perform a lookup using getgrouplist() to present a list of returned numeric group ids. This can be used to avoid the additional lookups that are done with the id and groups commands.
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am5
-rw-r--r--tests/lookup_groupbyuser.c74
2 files changed, 78 insertions, 1 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index cf1009e..49e9d81 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -33,7 +33,8 @@ AM_TESTS_ENVIRONMENT = PYTHON='@PYTHON@'; export PYTHON; \
check_PROGRAMS = test_dict test_set test_tio test_expr test_getpeercred \
test_cfg test_attmap test_myldap test_common test_clock \
- test_tio_timeout lookup_netgroup lookup_shadow
+ test_tio_timeout lookup_netgroup lookup_shadow \
+ lookup_groupbyuser
EXTRA_DIST = README nslcd-test.conf usernames.txt testenv.sh test_myldap.sh \
test_nsscmds.sh test_pamcmds.sh test_pamcmds.expect \
@@ -92,3 +93,5 @@ test_tio_timeout_SOURCES = test_tio_timeout.c ../common/tio.h
lookup_netgroup_SOURCES = lookup_netgroup.c
lookup_shadow_SOURCES = lookup_shadow.c
+
+lookup_groupbyuser_SOURCES = lookup_groupbyuser.c
diff --git a/tests/lookup_groupbyuser.c b/tests/lookup_groupbyuser.c
new file mode 100644
index 0000000..a3ade26
--- /dev/null
+++ b/tests/lookup_groupbyuser.c
@@ -0,0 +1,74 @@
+/*
+ lookup_groupbyuser.c - simple lookup for groups by user
+
+ Copyright (C) 2014 Arthur de Jong
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301 USA
+*/
+
+#include "config.h"
+
+#include <string.h>
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+
+#include <grp.h>
+
+#ifndef HAVE_GETGROUPLIST
+/* dummy implementation that for systems without getgrouplist() */
+int main(int argc,char *argv[])
+{
+ fprintf(stderr, "%s: getgrouplist() not available\n", argv[0]);
+ return 1;
+}
+#else /* HAVE_GETGROUPLIST */
+
+/* the main program... */
+int main(int argc,char *argv[])
+{
+ gid_t groups[1024];
+ int ngroups = sizeof(groups);
+ int i;
+ /* check arguments */
+ if ((argc != 1) && (argc != 2))
+ {
+ fprintf(stderr, "Usage: %s [USERNAME]\n", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+ /* start lookup */
+ if (getgrouplist(argv[1], (gid_t)-1, groups, &ngroups) < 0)
+ {
+ fprintf(stderr, "getgrouplist() failed (%d entries would be returned)\n",
+ ngroups);
+ exit(EXIT_FAILURE);
+ }
+ /* print results */
+ printf("user=%s groups=", argv[1]);
+ for (i = 0; i < ngroups; i++)
+ {
+ if (groups[i] != (gid_t)-1)
+ {
+ if (i > 0)
+ printf(",");
+ printf("%d", groups[i]);
+ }
+ }
+ printf("\n");
+ return 0;
+}
+
+#endif /* HAVE_GETGROUPLIST */