This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
+ the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
static void *find_fn(const char *name)
{
- char s[1024];
+ char *s;
static void *h;
void *res;
- snprintf(s,sizeof(s), "_nss_%s_%s", nss_name, name);
+ if (asprintf(&s, "_nss_%s_%s", nss_name, name) < 0) {
+ exit(1);
+ }
if (!h) {
- h = sys_dlopen(so_path, RTLD_LAZY);
+ h = dlopen(so_path, RTLD_LAZY);
}
if (!h) {
printf("Can't open shared library %s\n", so_path);
exit(1);
}
- res = sys_dlsym(h, s);
+ res = dlsym(h, s);
if (!res) {
printf("Can't find function %s\n", s);
total_errors++;
+ SAFE_FREE(s);
return NULL;
}
+ SAFE_FREE(s);
return res;
}
static struct passwd *nss_getpwent(void)
{
NSS_STATUS (*_nss_getpwent_r)(struct passwd *, char *,
- size_t , int *) = find_fn("getpwent_r");
+ size_t , int *) =
+ (NSS_STATUS (*)(struct passwd *, char *,
+ size_t, int *))find_fn("getpwent_r");
static struct passwd pwd;
static char buf[1000];
NSS_STATUS status;
static struct passwd *nss_getpwnam(const char *name)
{
NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *,
- size_t , int *) = find_fn("getpwnam_r");
+ size_t , int *) =
+ (NSS_STATUS (*)(const char *, struct passwd *, char *,
+ size_t, int *))find_fn("getpwnam_r");
static struct passwd pwd;
static char buf[1000];
NSS_STATUS status;
static struct passwd *nss_getpwuid(uid_t uid)
{
NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *,
- size_t , int *) = find_fn("getpwuid_r");
+ size_t , int *) =
+ (NSS_STATUS (*)(uid_t, struct passwd *, char *,
+ size_t, int *))find_fn("getpwuid_r");
static struct passwd pwd;
static char buf[1000];
NSS_STATUS status;
static void nss_setpwent(void)
{
- NSS_STATUS (*_nss_setpwent)(void) = find_fn("setpwent");
+ NSS_STATUS (*_nss_setpwent)(void) =
+ (NSS_STATUS(*)(void))find_fn("setpwent");
NSS_STATUS status;
if (!_nss_setpwent)
static void nss_endpwent(void)
{
- NSS_STATUS (*_nss_endpwent)(void) = find_fn("endpwent");
+ NSS_STATUS (*_nss_endpwent)(void) =
+ (NSS_STATUS (*)(void))find_fn("endpwent");
NSS_STATUS status;
if (!_nss_endpwent)
static struct group *nss_getgrent(void)
{
NSS_STATUS (*_nss_getgrent_r)(struct group *, char *,
- size_t , int *) = find_fn("getgrent_r");
+ size_t , int *) =
+ (NSS_STATUS (*)(struct group *, char *,
+ size_t, int *))find_fn("getgrent_r");
static struct group grp;
static char *buf;
static int buflen = 1024;
return NULL;
if (!buf)
- buf = malloc(buflen);
+ buf = SMB_MALLOC_ARRAY(char, buflen);
again:
status = _nss_getgrent_r(&grp, buf, buflen, &nss_errno);
if (status == NSS_STATUS_TRYAGAIN) {
buflen *= 2;
- buf = realloc(buf, buflen);
+ buf = SMB_REALLOC_ARRAY(buf, char, buflen);
+ if (!buf) {
+ return NULL;
+ }
goto again;
}
if (status == NSS_STATUS_NOTFOUND) {
+ SAFE_FREE(buf);
return NULL;
}
if (status != NSS_STATUS_SUCCESS) {
report_nss_error("getgrent", status);
+ SAFE_FREE(buf);
return NULL;
}
return &grp;
static struct group *nss_getgrnam(const char *name)
{
NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *,
- size_t , int *) = find_fn("getgrnam_r");
+ size_t , int *) =
+ (NSS_STATUS (*)(const char *, struct group *, char *,
+ size_t, int *))find_fn("getgrnam_r");
static struct group grp;
static char *buf;
static int buflen = 1000;
return NULL;
if (!buf)
- buf = malloc(buflen);
+ buf = SMB_MALLOC_ARRAY(char, buflen);
again:
status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
if (status == NSS_STATUS_TRYAGAIN) {
buflen *= 2;
- buf = realloc(buf, buflen);
+ buf = SMB_REALLOC_ARRAY(buf, char, buflen);
+ if (!buf) {
+ return NULL;
+ }
goto again;
}
if (status == NSS_STATUS_NOTFOUND) {
+ SAFE_FREE(buf);
return NULL;
}
if (status != NSS_STATUS_SUCCESS) {
report_nss_error("getgrnam", status);
+ SAFE_FREE(buf);
return NULL;
}
return &grp;
static struct group *nss_getgrgid(gid_t gid)
{
NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *,
- size_t , int *) = find_fn("getgrgid_r");
+ size_t , int *) =
+ (NSS_STATUS (*)(gid_t, struct group *, char *,
+ size_t, int *))find_fn("getgrgid_r");
static struct group grp;
static char *buf;
static int buflen = 1000;
return NULL;
if (!buf)
- buf = malloc(buflen);
+ buf = SMB_MALLOC_ARRAY(char, buflen);
again:
status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
if (status == NSS_STATUS_TRYAGAIN) {
buflen *= 2;
- buf = realloc(buf, buflen);
+ buf = SMB_REALLOC_ARRAY(buf, char, buflen);
+ if (!buf) {
+ return NULL;
+ }
goto again;
}
if (status == NSS_STATUS_NOTFOUND) {
+ SAFE_FREE(buf);
return NULL;
}
if (status != NSS_STATUS_SUCCESS) {
report_nss_error("getgrgid", status);
+ SAFE_FREE(buf);
return NULL;
}
return &grp;
static void nss_setgrent(void)
{
- NSS_STATUS (*_nss_setgrent)(void) = find_fn("setgrent");
+ NSS_STATUS (*_nss_setgrent)(void) =
+ (NSS_STATUS (*)(void))find_fn("setgrent");
NSS_STATUS status;
if (!_nss_setgrent)
static void nss_endgrent(void)
{
- NSS_STATUS (*_nss_endgrent)(void) = find_fn("endgrent");
+ NSS_STATUS (*_nss_endgrent)(void) =
+ (NSS_STATUS (*)(void))find_fn("endgrent");
NSS_STATUS status;
if (!_nss_endgrent)
{
NSS_STATUS (*_nss_initgroups)(char *, gid_t , long int *,
long int *, gid_t **, long int , int *) =
- find_fn("initgroups_dyn");
+ (NSS_STATUS (*)(char *, gid_t, long int *,
+ long int *, gid_t **,
+ long int, int *))find_fn("initgroups_dyn");
NSS_STATUS status;
if (!_nss_initgroups)
static void print_passwd(struct passwd *pwd)
{
- printf("%s:%s:%d:%d:%s:%s:%s\n",
+ printf("%s:%s:%lu:%lu:%s:%s:%s\n",
pwd->pw_name,
pwd->pw_passwd,
- pwd->pw_uid,
- pwd->pw_gid,
+ (unsigned long)pwd->pw_uid,
+ (unsigned long)pwd->pw_gid,
pwd->pw_gecos,
pwd->pw_dir,
pwd->pw_shell);
static void print_group(struct group *grp)
{
int i;
- printf("%s:%s:%d: ",
+ printf("%s:%s:%lu: ",
grp->gr_name,
grp->gr_passwd,
- grp->gr_gid);
+ (unsigned long)grp->gr_gid);
if (!grp->gr_mem[0]) {
printf("\n");
printf("%s\n", grp->gr_mem[i]);
}
-static void nss_test_winbind_initgroups(char *name, gid_t gid)
+static void nss_test_initgroups(char *name, gid_t gid)
{
long int size = 16;
long int start = 1;
int i;
NSS_STATUS status;
- groups = (gid_t *)malloc(size * sizeof(gid_t));
+ groups = SMB_MALLOC_ARRAY(gid_t, size);
groups[0] = gid;
status = nss_initgroups(name, gid, &groups, &start, &size);
}
for (i=0; i<start-1; i++) {
- printf("%d, ", groups[i]);
+ printf("%lu, ", (unsigned long)groups[i]);
}
- printf("%d\n", groups[i]);
+ printf("%lu\n", (unsigned long)groups[i]);
}
-static void nss_test_winbind_users(void)
+static void nss_test_users(void)
{
struct passwd *pwd;
continue;
}
printf("getpwnam: "); print_passwd(pwd);
- printf("initgroups: "); nss_test_winbind_initgroups(pwd->pw_name, pwd->pw_gid);
+ printf("initgroups: "); nss_test_initgroups(pwd->pw_name, pwd->pw_gid);
printf("\n");
}
nss_endpwent();
}
-static void nss_test_winbind_groups(void)
+static void nss_test_groups(void)
{
struct group *grp;
nss_endgrent();
}
-static void nss_test_winbind_errors(void)
+static void nss_test_errors(void)
{
struct passwd *pwd;
struct group *grp;
}
}
-static struct hostent *nss_gethostbyname(const char *name)
-{
- NSS_STATUS (*_nss_gethostbyname_r)(const char *, struct hostent *,
- char *, size_t, int *, int *) =
- find_fn("gethostbyname_r");
- NSS_STATUS status;
- static struct hostent he;
- static char *buf;
- static int buflen = 1000;
- int h_nss_errnop; /* "Other" error information stored here */
-
- if (!_nss_gethostbyname_r)
- return NULL;
-
- if (!buf)
- buf = malloc(buflen);
-
-again:
- status = _nss_gethostbyname_r(
- name, &he, buf, buflen, &nss_errno, &h_nss_errnop) ;
-
- if (status == NSS_STATUS_TRYAGAIN) {
- buflen *= 2;
- buf = realloc(buf, buflen);
- goto again;
- }
-
- if (status == NSS_STATUS_NOTFOUND)
- return NULL;
-
-
- if (status != NSS_STATUS_SUCCESS) {
- report_nss_error("gethostbyname", status);
- return NULL;
- }
-
- return &he;
-}
-
-static struct hostent *nss_gethostbyname2(const char *name, int af)
-{
- NSS_STATUS (*_nss_gethostbyname2_r)(const char *, int,
- struct hostent *, char *, size_t,
- int *, int *) =
- find_fn("gethostbyname2_r");
- NSS_STATUS status;
- static struct hostent he;
- static char *buf;
- static int buflen = 1000;
- int h_nss_errnop; /* "Other" error information stored here */
-
- if (!_nss_gethostbyname2_r)
- return NULL;
-
- if (!buf)
- buf = malloc(buflen);
-
-again:
- status = _nss_gethostbyname2_r(
- name, af, &he, buf, buflen, &nss_errno, &h_nss_errnop) ;
-
- if (status == NSS_STATUS_TRYAGAIN) {
- buflen *= 2;
- buf = realloc(buf, buflen);
- goto again;
- }
-
- if (status == NSS_STATUS_NOTFOUND)
- return NULL;
-
-
- if (status != NSS_STATUS_SUCCESS) {
- report_nss_error("gethostbyname2", status);
- return NULL;
- }
-
- return &he;
-}
-
-static void nss_test_wins(void)
-{
- fstring myname;
- struct hostent *he;
-
- /* Try testing with our hostname. This will probably work if
- Samba is/was running on the local machine. */
-
- memset(myname, 0, sizeof(myname));
- if (gethostname(myname, sizeof(myname) - 1) == -1) {
- printf("gethostname() failed!");
- total_errors++;
- return;
- }
-
- /* Test gethostbyname */
-
- if ((he = nss_gethostbyname(myname))) {
- struct in_addr **ip_list = (struct in_addr **)he->h_addr_list;
-
- while (*ip_list) {
- printf("gethostbyname: %s has ip %s\n", myname,
- inet_ntoa(**ip_list));
- *ip_list++;
- }
- } else
- printf("hostname %s not found with gethostbyname\n", myname);
-
- /* Test gethostbyname2 */
-
- if ((he = nss_gethostbyname2(myname, AF_INET))) {
- struct in_addr **ip_list = (struct in_addr **)he->h_addr_list;
-
- while (*ip_list) {
- printf("gethostbyname2: %s has ip %s\n", myname,
- inet_ntoa(**ip_list));
- *ip_list++;
- }
- } else
- printf("hostname %s not found with gethostbyname2\n", myname);
-}
-
int main(int argc, char *argv[])
{
if (argc > 1) so_path = argv[1];
if (argc > 2) nss_name = argv[2];
- if (strequal(nss_name, "winbind")) {
- nss_test_winbind_users();
- nss_test_winbind_groups();
- nss_test_winbind_errors();
- goto done;
- }
+ nss_test_users();
+ nss_test_groups();
+ nss_test_errors();
- if (strequal(nss_name, "wins")) {
- nss_test_wins();
- goto done;
- }
-
-done:
printf("total_errors=%d\n", total_errors);
return total_errors;