1 /************************************************
3 -------------------------------------------------
4 based on nsstest.c by a. tridgell
6 -------------------------------------------------
7 get user and group info for different nss modules
8 ************************************************/
17 typedef enum nss_status NSS_STATUS;
20 void *find_fn(const char *nss_name, const char *funct_name)
26 // create the library name
27 asprintf(&so_path, "libnss_%s.so.2", nss_name);
28 h = dlopen(so_path, RTLD_LAZY);
35 // create the function name
36 asprintf(&s, "_nss_%s_%s", nss_name, funct_name);
38 // and find it in the library
47 // free the strings from asprintf
53 struct passwd *nss_getpwent(const char *nss_name)
55 NSS_STATUS (*_nss_getpwent_r)(struct passwd *, char *,
56 size_t , int *) = find_fn(nss_name, "getpwent_r");
57 static struct passwd pwd;
58 static char buf[1000];
61 status = _nss_getpwent_r(&pwd, buf, sizeof(buf), &nss_errno);
63 if (status != NSS_STATUS_SUCCESS)
69 struct passwd *nss_getpwnam(const char *nss_name, const char *name)
71 NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *,
72 size_t , int *) = find_fn(nss_name, "getpwnam_r");
73 static struct passwd pwd;
74 static char buf[1000];
77 status = _nss_getpwnam_r(name, &pwd, buf, sizeof(buf), &nss_errno);
79 if (status != NSS_STATUS_SUCCESS)
85 struct passwd *nss_getpwuid(const char *nss_name, uid_t uid)
87 NSS_STATUS (*_nss_getpwuid_r)(uid_t, struct passwd *, char *,
88 size_t , int *) = find_fn(nss_name, "getpwuid_r");
89 static struct passwd pwd;
90 static char buf[1000];
93 status = _nss_getpwuid_r(uid, &pwd, buf, sizeof(buf), &nss_errno);
95 if (status != NSS_STATUS_SUCCESS)
101 NSS_STATUS nss_setpwent(const char *nss_name)
103 NSS_STATUS (*_nss_setpwent)(void) = find_fn(nss_name, "setpwent");
104 return( _nss_setpwent() );
107 NSS_STATUS nss_endpwent(const char *nss_name)
109 NSS_STATUS (*_nss_endpwent)(void) = find_fn(nss_name, "endpwent");
110 return( _nss_endpwent() );
113 struct group *nss_getgrent(const char *nss_name)
115 NSS_STATUS (*_nss_getgrent_r)(struct group *, char *,
116 size_t , int *) = find_fn(nss_name, "getgrent_r");
117 static struct group grp;
119 static char *buf = NULL;
120 static int buflen = 1024;
124 if((buf = malloc(buflen)) == NULL)
129 status = _nss_getgrent_r(&grp, buf, buflen, &my_errno);
130 if (status == NSS_STATUS_TRYAGAIN) {
133 if( (buf = malloc(buflen)) == NULL) {
140 if (status != NSS_STATUS_SUCCESS) {
149 struct group *nss_getgrnam(const char *nss_name, const char *name)
151 NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *,
152 size_t , int *) = find_fn(nss_name, "getgrnam_r");
153 static struct group grp;
154 static char *buf = NULL;
155 static int buflen = 1024;
159 if( (buf = malloc(buflen)) == NULL)
164 status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
165 if (status == NSS_STATUS_TRYAGAIN) {
168 if ( (buf = malloc(buflen)) == NULL) {
175 if (status != NSS_STATUS_SUCCESS) {
184 struct group *nss_getgrgid(const char *nss_name, gid_t gid)
186 NSS_STATUS (*_nss_getgrgid_r)(gid_t, struct group *, char *,
187 size_t , int *) = find_fn(nss_name, "getgrgid_r");
188 static struct group grp;
189 static char *buf = NULL;
190 static int buflen = 1024;
194 if( (buf = malloc(buflen)) == NULL)
199 status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
200 if (status == NSS_STATUS_TRYAGAIN) {
203 if( (buf = malloc(buflen)) == NULL) {
210 if (status != NSS_STATUS_SUCCESS) {
220 NSS_STATUS nss_setgrent(const char *nss_name)
222 NSS_STATUS (*_nss_setgrent)(void) = find_fn(nss_name, "setgrent");
223 return( _nss_setgrent() );
226 NSS_STATUS nss_endgrent(const char *nss_name)
228 NSS_STATUS (*_nss_endgrent)(void) = find_fn(nss_name, "endgrent");
229 return( _nss_endgrent() );
233 this trick resets the internal glibc cached copy of the NIS domain name
234 read the glibc nis code (in particular the __ypdomain[] static array) to
235 understand whats going on
237 static void nis_reset_domain(void)
239 void (*fn_yp_get_default_domain)(char **);
240 void *h = dlopen("libnss_nis.so.2", RTLD_LAZY);
243 fn_yp_get_default_domain = dlsym(h, "yp_get_default_domain");
245 if (!fn_yp_get_default_domain) return;
247 fn_yp_get_default_domain(&domain);
265 while (nss_getpwent("nis")) i++;