s3:winbindd: make use of samba_tevent_context_init()
[Samba/gebeck_regimport.git] / examples / nss / wbtest.c
blob14265bd54c1ad57f9bb78dc917ac2e06c672615f
1 /*
2 nss sample code for extended winbindd functionality
4 Copyright (C) Andrew Tridgell (tridge@samba.org)
6 you are free to use this code in any way you see fit, including
7 without restriction, using this code in your own products. You do
8 not need to give any attribution.
9 */
12 compile like this:
14 cc -o wbtest wbtest.c nss_winbind.c -ldl
16 and run like this:
18 ./wbtest /lib/libnss_winbind.so
21 #define _GNU_SOURCE
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <nss.h>
26 #include <dlfcn.h>
27 #include <pwd.h>
28 #include <grp.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <sys/types.h>
33 #include "nss_winbind.h"
35 static int nss_test_users(struct nss_state *nss)
37 struct passwd pwd;
39 if (nss_setpwent(nss) != 0) {
40 perror("setpwent");
41 return -1;
44 /* loop over all users */
45 while ((nss_getpwent(nss, &pwd) == 0)) {
46 char *sid, **group_sids, *name2;
47 int i;
49 printf("User %s\n", pwd.pw_name);
50 if (nss_nametosid(nss, pwd.pw_name, &sid) != 0) {
51 perror("nametosid");
52 return -1;
54 printf("\tSID %s\n", sid);
56 if (nss_sidtoname(nss, sid, &name2) != 0) {
57 perror("sidtoname");
58 return -1;
60 printf("\tSID->name %s\n", name2);
62 if (nss_getusersids(nss, sid, &group_sids) != 0) {
63 perror("getusersids");
64 return -1;
67 printf("\tGroups:\n");
68 for (i=0; group_sids[i]; i++) {
69 printf("\t\t%s\n", group_sids[i]);
70 free(group_sids[i]);
73 free(sid);
74 free(name2);
75 free(group_sids);
79 if (nss_endpwent(nss) != 0) {
80 perror("endpwent");
81 return -1;
84 return 0;
89 main program. It lists all users, listing user SIDs for each user
91 int main(int argc, char *argv[])
93 struct nss_state nss;
94 const char *so_path = "/lib/libnss_winbind.so";
95 int ret;
97 if (argc > 1) {
98 so_path = argv[1];
101 if (nss_open(&nss, so_path) != 0) {
102 perror("nss_open");
103 exit(1);
106 ret = nss_test_users(&nss);
108 nss_close(&nss);
110 return ret;