| 1 | /* | 
|---|
| 2 | nss sample code for extended winbindd functionality | 
|---|
| 3 |  | 
|---|
| 4 | Copyright (C) Andrew Tridgell (tridge@samba.org) | 
|---|
| 5 |  | 
|---|
| 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 | */ | 
|---|
| 10 |  | 
|---|
| 11 | /* | 
|---|
| 12 | compile like this: | 
|---|
| 13 |  | 
|---|
| 14 | cc -o wbtest wbtest.c nss_winbind.c -ldl | 
|---|
| 15 |  | 
|---|
| 16 | and run like this: | 
|---|
| 17 |  | 
|---|
| 18 | ./wbtest /lib/libnss_winbind.so | 
|---|
| 19 | */ | 
|---|
| 20 |  | 
|---|
| 21 | #define _GNU_SOURCE | 
|---|
| 22 |  | 
|---|
| 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> | 
|---|
| 32 |  | 
|---|
| 33 | #include "nss_winbind.h" | 
|---|
| 34 |  | 
|---|
| 35 | static int nss_test_users(struct nss_state *nss) | 
|---|
| 36 | { | 
|---|
| 37 | struct passwd pwd; | 
|---|
| 38 |  | 
|---|
| 39 | if (nss_setpwent(nss) != 0) { | 
|---|
| 40 | perror("setpwent"); | 
|---|
| 41 | return -1; | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 | /* loop over all users */ | 
|---|
| 45 | while ((nss_getpwent(nss, &pwd) == 0)) { | 
|---|
| 46 | char *sid, **group_sids, *name2; | 
|---|
| 47 | int i; | 
|---|
| 48 |  | 
|---|
| 49 | printf("User %s\n", pwd.pw_name); | 
|---|
| 50 | if (nss_nametosid(nss, pwd.pw_name, &sid) != 0) { | 
|---|
| 51 | perror("nametosid"); | 
|---|
| 52 | return -1; | 
|---|
| 53 | } | 
|---|
| 54 | printf("\tSID %s\n", sid); | 
|---|
| 55 |  | 
|---|
| 56 | if (nss_sidtoname(nss, sid, &name2) != 0) { | 
|---|
| 57 | perror("sidtoname"); | 
|---|
| 58 | return -1; | 
|---|
| 59 | } | 
|---|
| 60 | printf("\tSID->name %s\n", name2); | 
|---|
| 61 |  | 
|---|
| 62 | if (nss_getusersids(nss, sid, &group_sids) != 0) { | 
|---|
| 63 | perror("getusersids"); | 
|---|
| 64 | return -1; | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 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]); | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | free(sid); | 
|---|
| 74 | free(name2); | 
|---|
| 75 | free(group_sids); | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 |  | 
|---|
| 79 | if (nss_endpwent(nss) != 0) { | 
|---|
| 80 | perror("endpwent"); | 
|---|
| 81 | return -1; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | return 0; | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 |  | 
|---|
| 88 | /* | 
|---|
| 89 | main program. It lists all users, listing user SIDs for each user | 
|---|
| 90 | */ | 
|---|
| 91 | int main(int argc, char *argv[]) | 
|---|
| 92 | { | 
|---|
| 93 | struct nss_state nss; | 
|---|
| 94 | const char *so_path = "/lib/libnss_winbind.so"; | 
|---|
| 95 | int ret; | 
|---|
| 96 |  | 
|---|
| 97 | if (argc > 1) { | 
|---|
| 98 | so_path = argv[1]; | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | if (nss_open(&nss, so_path) != 0) { | 
|---|
| 102 | perror("nss_open"); | 
|---|
| 103 | exit(1); | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | ret = nss_test_users(&nss); | 
|---|
| 107 |  | 
|---|
| 108 | nss_close(&nss); | 
|---|
| 109 |  | 
|---|
| 110 | return ret; | 
|---|
| 111 | } | 
|---|