From 8daa83a594a2e98f39d764422bfbdbc62c9efd44 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 19:20:00 +0200 Subject: Adding upstream version 2:4.20.0+dfsg. Signed-off-by: Daniel Baumann --- examples/nss/wbtest.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 examples/nss/wbtest.c (limited to 'examples/nss/wbtest.c') diff --git a/examples/nss/wbtest.c b/examples/nss/wbtest.c new file mode 100644 index 0000000..14265bd --- /dev/null +++ b/examples/nss/wbtest.c @@ -0,0 +1,111 @@ +/* + nss sample code for extended winbindd functionality + + Copyright (C) Andrew Tridgell (tridge@samba.org) + + you are free to use this code in any way you see fit, including + without restriction, using this code in your own products. You do + not need to give any attribution. +*/ + +/* + compile like this: + + cc -o wbtest wbtest.c nss_winbind.c -ldl + + and run like this: + + ./wbtest /lib/libnss_winbind.so +*/ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "nss_winbind.h" + +static int nss_test_users(struct nss_state *nss) +{ + struct passwd pwd; + + if (nss_setpwent(nss) != 0) { + perror("setpwent"); + return -1; + } + + /* loop over all users */ + while ((nss_getpwent(nss, &pwd) == 0)) { + char *sid, **group_sids, *name2; + int i; + + printf("User %s\n", pwd.pw_name); + if (nss_nametosid(nss, pwd.pw_name, &sid) != 0) { + perror("nametosid"); + return -1; + } + printf("\tSID %s\n", sid); + + if (nss_sidtoname(nss, sid, &name2) != 0) { + perror("sidtoname"); + return -1; + } + printf("\tSID->name %s\n", name2); + + if (nss_getusersids(nss, sid, &group_sids) != 0) { + perror("getusersids"); + return -1; + } + + printf("\tGroups:\n"); + for (i=0; group_sids[i]; i++) { + printf("\t\t%s\n", group_sids[i]); + free(group_sids[i]); + } + + free(sid); + free(name2); + free(group_sids); + } + + + if (nss_endpwent(nss) != 0) { + perror("endpwent"); + return -1; + } + + return 0; +} + + +/* + main program. It lists all users, listing user SIDs for each user + */ +int main(int argc, char *argv[]) +{ + struct nss_state nss; + const char *so_path = "/lib/libnss_winbind.so"; + int ret; + + if (argc > 1) { + so_path = argv[1]; + } + + if (nss_open(&nss, so_path) != 0) { + perror("nss_open"); + exit(1); + } + + ret = nss_test_users(&nss); + + nss_close(&nss); + + return ret; +} -- cgit v1.2.3