1 /* Template generic NSS service provider. See nss_test.h for usage.
2 Copyright (C) 2017-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
24 #include <alloc_buffer.h>
27 /* We need to be able to handle NULLs "properly" within the testsuite,
28 to test known bad data. */
29 #define alloc_buffer_maybe_copy_string(b,s) s ? alloc_buffer_copy_string (b, s) : NULL;
31 /* This file is the master template. Other instances of this test
32 module should define NAME(x) to have their name instead of "test1",
33 then include this file.
35 #define NAME_(x,n) _nss_##n##_##x
37 #define NAME(x) NAME_(x,test1)
39 #define NAMESTR__(x) #x
40 #define NAMESTR_(x) NAMESTR__(x)
41 #define NAMESTR(x) NAMESTR_(NAME(x))
45 /* -------------------------------------------------- */
48 static struct passwd default_pwd_data
[] =
51 { .pw_name = (char *) "name" #u, .pw_passwd = (char *) "*", .pw_uid = u, \
52 .pw_gid = 100, .pw_gecos = (char *) "*", .pw_dir = (char *) "*", \
53 .pw_shell = (char *) "*" }
60 #define default_npwd_data \
61 (sizeof (default_pwd_data) / sizeof (default_pwd_data[0]))
63 static struct passwd
*pwd_data
= default_pwd_data
;
64 static int npwd_data
= default_npwd_data
;
66 static struct group
*grp_data
= NULL
;
67 static int ngrp_data
= 0;
69 static struct hostent
*host_data
= NULL
;
70 static int nhost_data
= 0;
72 /* This function will get called, and once per session, look back into
73 the test case's executable for an init hook function, and call
76 static int initted
= 0;
87 memset (&t
, 0, sizeof (t
));
92 pwd_data
= t
.pwd_table
;
93 for (i
=0; ! PWD_ISLAST(& pwd_data
[i
]); i
++)
100 grp_data
= t
.grp_table
;
101 for (i
=0; ! GRP_ISLAST(& grp_data
[i
]); i
++)
107 host_data
= t
.host_table
;
108 for (i
=0; ! HOST_ISLAST(& host_data
[i
]); i
++)
116 /* -------------------------------------------------- */
117 /* Password handling. */
119 static size_t pwd_iter
;
120 #define CURPWD pwd_data[pwd_iter]
122 static pthread_mutex_t pwd_lock
= PTHREAD_MUTEX_INITIALIZER
;
125 NAME(setpwent
) (int stayopen
)
129 return NSS_STATUS_SUCCESS
;
134 NAME(endpwent
) (void)
137 return NSS_STATUS_SUCCESS
;
140 static enum nss_status
141 copy_passwd (struct passwd
*result
, struct passwd
*local
,
142 char *buffer
, size_t buflen
, int *errnop
)
144 struct alloc_buffer buf
= alloc_buffer_create (buffer
, buflen
);
146 result
->pw_name
= alloc_buffer_maybe_copy_string (&buf
, local
->pw_name
);
147 result
->pw_passwd
= alloc_buffer_maybe_copy_string (&buf
, local
->pw_passwd
);
148 result
->pw_uid
= local
->pw_uid
;
149 result
->pw_gid
= local
->pw_gid
;
150 result
->pw_gecos
= alloc_buffer_maybe_copy_string (&buf
, local
->pw_gecos
);
151 result
->pw_dir
= alloc_buffer_maybe_copy_string (&buf
, local
->pw_dir
);
152 result
->pw_shell
= alloc_buffer_maybe_copy_string (&buf
, local
->pw_shell
);
154 if (alloc_buffer_has_failed (&buf
))
157 return NSS_STATUS_TRYAGAIN
;
160 return NSS_STATUS_SUCCESS
;
164 NAME(getpwent_r
) (struct passwd
*result
, char *buffer
, size_t buflen
,
167 int res
= NSS_STATUS_SUCCESS
;
170 pthread_mutex_lock (&pwd_lock
);
172 if (pwd_iter
>= npwd_data
)
173 res
= NSS_STATUS_NOTFOUND
;
176 res
= copy_passwd (result
, &CURPWD
, buffer
, buflen
, errnop
);
180 pthread_mutex_unlock (&pwd_lock
);
187 NAME(getpwuid_r
) (uid_t uid
, struct passwd
*result
, char *buffer
,
188 size_t buflen
, int *errnop
)
191 for (size_t idx
= 0; idx
< npwd_data
; ++idx
)
192 if (pwd_data
[idx
].pw_uid
== uid
)
193 return copy_passwd (result
, &pwd_data
[idx
], buffer
, buflen
, errnop
);
195 return NSS_STATUS_NOTFOUND
;
200 NAME(getpwnam_r
) (const char *name
, struct passwd
*result
, char *buffer
,
201 size_t buflen
, int *errnop
)
204 for (size_t idx
= 0; idx
< npwd_data
; ++idx
)
205 if (strcmp (pwd_data
[idx
].pw_name
, name
) == 0)
206 return copy_passwd (result
, &pwd_data
[idx
], buffer
, buflen
, errnop
);
208 return NSS_STATUS_NOTFOUND
;
211 /* -------------------------------------------------- */
212 /* Group handling. */
214 static size_t grp_iter
;
215 #define CURGRP grp_data[grp_iter]
217 static pthread_mutex_t grp_lock
= PTHREAD_MUTEX_INITIALIZER
;
220 NAME(setgrent
) (int stayopen
)
224 return NSS_STATUS_SUCCESS
;
229 NAME(endgrent
) (void)
232 return NSS_STATUS_SUCCESS
;
235 static enum nss_status
236 copy_group (struct group
*result
, struct group
*local
,
237 char *buffer
, size_t buflen
, int *errnop
)
239 struct alloc_buffer buf
= alloc_buffer_create (buffer
, buflen
);
246 while (local
->gr_mem
[i
])
249 memlist
= alloc_buffer_alloc_array (&buf
, char *, i
+ 1);
252 for (i
= 0; local
->gr_mem
[i
]; ++i
)
253 memlist
[i
] = alloc_buffer_maybe_copy_string (&buf
, local
->gr_mem
[i
]);
257 result
->gr_mem
= memlist
;
260 result
->gr_mem
= NULL
;
262 result
->gr_name
= alloc_buffer_maybe_copy_string (&buf
, local
->gr_name
);
263 result
->gr_passwd
= alloc_buffer_maybe_copy_string (&buf
, local
->gr_passwd
);
264 result
->gr_gid
= local
->gr_gid
;
266 if (alloc_buffer_has_failed (&buf
))
269 return NSS_STATUS_TRYAGAIN
;
272 return NSS_STATUS_SUCCESS
;
277 NAME(getgrent_r
) (struct group
*result
, char *buffer
, size_t buflen
,
280 int res
= NSS_STATUS_SUCCESS
;
283 pthread_mutex_lock (&grp_lock
);
285 if (grp_iter
>= ngrp_data
)
286 res
= NSS_STATUS_NOTFOUND
;
289 res
= copy_group (result
, &CURGRP
, buffer
, buflen
, errnop
);
293 pthread_mutex_unlock (&grp_lock
);
300 NAME(getgrgid_r
) (gid_t gid
, struct group
*result
, char *buffer
,
301 size_t buflen
, int *errnop
)
304 for (size_t idx
= 0; idx
< ngrp_data
; ++idx
)
305 if (grp_data
[idx
].gr_gid
== gid
)
306 return copy_group (result
, &grp_data
[idx
], buffer
, buflen
, errnop
);
308 return NSS_STATUS_NOTFOUND
;
313 NAME(getgrnam_r
) (const char *name
, struct group
*result
, char *buffer
,
314 size_t buflen
, int *errnop
)
317 for (size_t idx
= 0; idx
< ngrp_data
; ++idx
)
318 if (strcmp (pwd_data
[idx
].pw_name
, name
) == 0)
320 return copy_group (result
, &grp_data
[idx
], buffer
, buflen
, errnop
);
323 return NSS_STATUS_NOTFOUND
;
326 /* -------------------------------------------------- */
329 static size_t host_iter
;
330 #define CURHOST host_data[host_iter]
332 static pthread_mutex_t host_lock
= PTHREAD_MUTEX_INITIALIZER
;
335 NAME(sethostent
) (int stayopen
)
339 return NSS_STATUS_SUCCESS
;
344 NAME(endhostent
) (void)
347 return NSS_STATUS_SUCCESS
;
350 static enum nss_status
351 copy_host (struct hostent
*result
, struct hostent
*local
,
352 char *buffer
, size_t buflen
, int *errnop
)
354 struct alloc_buffer buf
= alloc_buffer_create (buffer
, buflen
);
358 if (local
->h_addr_list
)
361 while (local
->h_addr_list
[i
])
364 memlist
= alloc_buffer_alloc_array (&buf
, char *, i
+ 1);
367 for (j
= 0; j
< i
; ++j
)
368 memlist
[j
] = alloc_buffer_maybe_copy_string (&buf
, local
->h_addr_list
[j
]);
372 result
->h_addr_list
= memlist
;
376 result
->h_addr_list
= NULL
;
379 result
->h_aliases
= NULL
;
380 result
->h_addrtype
= AF_INET
;
381 result
->h_length
= 4;
382 result
->h_name
= alloc_buffer_maybe_copy_string (&buf
, local
->h_name
);
384 if (alloc_buffer_has_failed (&buf
))
387 return NSS_STATUS_TRYAGAIN
;
390 return NSS_STATUS_SUCCESS
;
395 NAME(gethostent_r
) (struct hostent
*ret
, char *buffer
, size_t buflen
,
396 struct hostent
**result
, int *errnop
)
398 int res
= NSS_STATUS_SUCCESS
;
401 pthread_mutex_lock (&host_lock
);
403 if (host_iter
>= nhost_data
)
405 res
= NSS_STATUS_NOTFOUND
;
410 res
= copy_host (ret
, &CURHOST
, buffer
, buflen
, errnop
);
415 pthread_mutex_unlock (&host_lock
);
421 NAME(gethostbyname3_r
) (const char *name
, int af
, struct hostent
*ret
,
422 char *buffer
, size_t buflen
, int *errnop
,
423 int *h_errnop
, int32_t *ttlp
, char **canonp
)
427 for (size_t idx
= 0; idx
< nhost_data
; ++idx
)
428 if (strcmp (host_data
[idx
].h_name
, name
) == 0)
429 return copy_host (ret
, & host_data
[idx
], buffer
, buflen
, h_errnop
);
431 return NSS_STATUS_NOTFOUND
;
435 NAME(gethostbyname_r
) (const char *name
, struct hostent
*result
,
436 char *buffer
, size_t buflen
,
437 int *errnop
, int *h_errnop
)
439 return NAME(gethostbyname3_r
) (name
, AF_INET
, result
, buffer
, buflen
,
440 errnop
, h_errnop
, NULL
, NULL
);
444 NAME(gethostbyname2_r
) (const char *name
, int af
, struct hostent
*result
,
445 char *buffer
, size_t buflen
,
446 int *errnop
, int *h_errnop
)
448 return NAME(gethostbyname3_r
) (name
, af
, result
, buffer
, buflen
,
449 errnop
, h_errnop
, NULL
, NULL
);
453 NAME(gethostbyaddr2_r
) (const void *addr
, socklen_t len
, int af
,
454 struct hostent
*result
, char *buffer
, size_t buflen
,
455 int *errnop
, int *h_errnop
, int32_t *ttlp
)
459 /* Support this later. */
461 return NSS_STATUS_NOTFOUND
;
463 for (size_t idx
= 0; idx
< nhost_data
; ++idx
)
464 if (memcmp (host_data
[idx
].h_addr
, addr
, len
) == 0)
465 return copy_host (result
, & host_data
[idx
], buffer
, buflen
, h_errnop
);
467 return NSS_STATUS_NOTFOUND
;
470 /* Note: only the first address is supported, intentionally. */
472 NAME(gethostbyaddr_r
) (const void *addr
, socklen_t len
, int af
,
473 struct hostent
*result
, char *buffer
, size_t buflen
,
474 int *errnop
, int *h_errnop
)
476 return NAME(gethostbyaddr2_r
) (addr
, len
, af
, result
, buffer
, buflen
,
477 errnop
, h_errnop
, NULL
);