1 /* Template generic NSS service provider. See nss_test.h for usage.
2 Copyright (C) 2017-2023 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 spwd
*spwd_data
= NULL
;
70 static int nspwd_data
= 0;
72 static struct hostent
*host_data
= NULL
;
73 static int nhost_data
= 0;
75 /* This function will get called, and once per session, look back into
76 the test case's executable for an init hook function, and call
79 static int initted
= 0;
90 memset (&t
, 0, sizeof (t
));
95 pwd_data
= t
.pwd_table
;
96 for (i
=0; ! PWD_ISLAST(& pwd_data
[i
]); i
++)
103 grp_data
= t
.grp_table
;
104 for (i
=0; ! GRP_ISLAST(& grp_data
[i
]); i
++)
110 spwd_data
= t
.spwd_table
;
111 for (i
=0; ! SPWD_ISLAST(& spwd_data
[i
]); i
++)
117 host_data
= t
.host_table
;
118 for (i
=0; ! HOST_ISLAST(& host_data
[i
]); i
++)
126 /* -------------------------------------------------- */
127 /* Password handling. */
129 static size_t pwd_iter
;
130 #define CURPWD pwd_data[pwd_iter]
132 static pthread_mutex_t pwd_lock
= PTHREAD_MUTEX_INITIALIZER
;
135 NAME(setpwent
) (int stayopen
)
139 return NSS_STATUS_SUCCESS
;
144 NAME(endpwent
) (void)
147 return NSS_STATUS_SUCCESS
;
150 static enum nss_status
151 copy_passwd (struct passwd
*result
, struct passwd
*local
,
152 char *buffer
, size_t buflen
, int *errnop
)
154 struct alloc_buffer buf
= alloc_buffer_create (buffer
, buflen
);
156 result
->pw_name
= alloc_buffer_maybe_copy_string (&buf
, local
->pw_name
);
157 result
->pw_passwd
= alloc_buffer_maybe_copy_string (&buf
, local
->pw_passwd
);
158 result
->pw_uid
= local
->pw_uid
;
159 result
->pw_gid
= local
->pw_gid
;
160 result
->pw_gecos
= alloc_buffer_maybe_copy_string (&buf
, local
->pw_gecos
);
161 result
->pw_dir
= alloc_buffer_maybe_copy_string (&buf
, local
->pw_dir
);
162 result
->pw_shell
= alloc_buffer_maybe_copy_string (&buf
, local
->pw_shell
);
164 if (alloc_buffer_has_failed (&buf
))
167 return NSS_STATUS_TRYAGAIN
;
170 return NSS_STATUS_SUCCESS
;
174 NAME(getpwent_r
) (struct passwd
*result
, char *buffer
, size_t buflen
,
177 int res
= NSS_STATUS_SUCCESS
;
180 pthread_mutex_lock (&pwd_lock
);
182 if (pwd_iter
>= npwd_data
)
183 res
= NSS_STATUS_NOTFOUND
;
186 res
= copy_passwd (result
, &CURPWD
, buffer
, buflen
, errnop
);
190 pthread_mutex_unlock (&pwd_lock
);
197 NAME(getpwuid_r
) (uid_t uid
, struct passwd
*result
, char *buffer
,
198 size_t buflen
, int *errnop
)
201 for (size_t idx
= 0; idx
< npwd_data
; ++idx
)
202 if (pwd_data
[idx
].pw_uid
== uid
)
203 return copy_passwd (result
, &pwd_data
[idx
], buffer
, buflen
, errnop
);
205 return NSS_STATUS_NOTFOUND
;
210 NAME(getpwnam_r
) (const char *name
, struct passwd
*result
, char *buffer
,
211 size_t buflen
, int *errnop
)
214 for (size_t idx
= 0; idx
< npwd_data
; ++idx
)
215 if (strcmp (pwd_data
[idx
].pw_name
, name
) == 0)
216 return copy_passwd (result
, &pwd_data
[idx
], buffer
, buflen
, errnop
);
218 return NSS_STATUS_NOTFOUND
;
221 /* -------------------------------------------------- */
222 /* Group handling. */
224 static size_t grp_iter
;
225 #define CURGRP grp_data[grp_iter]
227 static pthread_mutex_t grp_lock
= PTHREAD_MUTEX_INITIALIZER
;
230 NAME(setgrent
) (int stayopen
)
234 return NSS_STATUS_SUCCESS
;
239 NAME(endgrent
) (void)
242 return NSS_STATUS_SUCCESS
;
245 static enum nss_status
246 copy_group (struct group
*result
, struct group
*local
,
247 char *buffer
, size_t buflen
, int *errnop
)
249 struct alloc_buffer buf
= alloc_buffer_create (buffer
, buflen
);
256 while (local
->gr_mem
[i
])
259 memlist
= alloc_buffer_alloc_array (&buf
, char *, i
+ 1);
262 for (i
= 0; local
->gr_mem
[i
]; ++i
)
263 memlist
[i
] = alloc_buffer_maybe_copy_string (&buf
, local
->gr_mem
[i
]);
267 result
->gr_mem
= memlist
;
270 result
->gr_mem
= NULL
;
272 result
->gr_name
= alloc_buffer_maybe_copy_string (&buf
, local
->gr_name
);
273 result
->gr_passwd
= alloc_buffer_maybe_copy_string (&buf
, local
->gr_passwd
);
274 result
->gr_gid
= local
->gr_gid
;
276 if (alloc_buffer_has_failed (&buf
))
279 return NSS_STATUS_TRYAGAIN
;
282 return NSS_STATUS_SUCCESS
;
287 NAME(getgrent_r
) (struct group
*result
, char *buffer
, size_t buflen
,
290 int res
= NSS_STATUS_SUCCESS
;
293 pthread_mutex_lock (&grp_lock
);
295 if (grp_iter
>= ngrp_data
)
296 res
= NSS_STATUS_NOTFOUND
;
299 res
= copy_group (result
, &CURGRP
, buffer
, buflen
, errnop
);
303 pthread_mutex_unlock (&grp_lock
);
310 NAME(getgrgid_r
) (gid_t gid
, struct group
*result
, char *buffer
,
311 size_t buflen
, int *errnop
)
314 for (size_t idx
= 0; idx
< ngrp_data
; ++idx
)
315 if (grp_data
[idx
].gr_gid
== gid
)
316 return copy_group (result
, &grp_data
[idx
], buffer
, buflen
, errnop
);
318 return NSS_STATUS_NOTFOUND
;
323 NAME(getgrnam_r
) (const char *name
, struct group
*result
, char *buffer
,
324 size_t buflen
, int *errnop
)
327 for (size_t idx
= 0; idx
< ngrp_data
; ++idx
)
328 if (strcmp (pwd_data
[idx
].pw_name
, name
) == 0)
330 return copy_group (result
, &grp_data
[idx
], buffer
, buflen
, errnop
);
333 return NSS_STATUS_NOTFOUND
;
336 /* -------------------------------------------------- */
337 /* Shadow password handling. */
339 static size_t spwd_iter
;
340 #define CURSPWD spwd_data[spwd_iter]
342 static pthread_mutex_t spwd_lock
= PTHREAD_MUTEX_INITIALIZER
;
345 NAME(setspent
) (int stayopen
)
349 return NSS_STATUS_SUCCESS
;
354 NAME(endspwent
) (void)
357 return NSS_STATUS_SUCCESS
;
360 static enum nss_status
361 copy_shadow (struct spwd
*result
, struct spwd
*local
,
362 char *buffer
, size_t buflen
, int *errnop
)
364 struct alloc_buffer buf
= alloc_buffer_create (buffer
, buflen
);
366 result
->sp_namp
= alloc_buffer_maybe_copy_string (&buf
, local
->sp_namp
);
367 result
->sp_pwdp
= alloc_buffer_maybe_copy_string (&buf
, local
->sp_pwdp
);
368 result
->sp_lstchg
= local
->sp_lstchg
;
369 result
->sp_min
= local
->sp_min
;
370 result
->sp_max
= local
->sp_max
;
371 result
->sp_warn
= local
->sp_warn
;
372 result
->sp_inact
= local
->sp_inact
;
373 result
->sp_expire
= local
->sp_expire
;
374 result
->sp_flag
= local
->sp_flag
;
376 if (alloc_buffer_has_failed (&buf
))
379 return NSS_STATUS_TRYAGAIN
;
382 return NSS_STATUS_SUCCESS
;
386 NAME(getspent_r
) (struct spwd
*result
, char *buffer
, size_t buflen
,
389 int res
= NSS_STATUS_SUCCESS
;
392 pthread_mutex_lock (&spwd_lock
);
394 if (spwd_iter
>= nspwd_data
)
395 res
= NSS_STATUS_NOTFOUND
;
398 res
= copy_shadow (result
, &CURSPWD
, buffer
, buflen
, errnop
);
402 pthread_mutex_unlock (&spwd_lock
);
408 NAME(getspnam_r
) (const char *name
, struct spwd
*result
, char *buffer
,
409 size_t buflen
, int *errnop
)
412 for (size_t idx
= 0; idx
< nspwd_data
; ++idx
)
413 if (strcmp (spwd_data
[idx
].sp_namp
, name
) == 0)
414 return copy_shadow (result
, &spwd_data
[idx
], buffer
, buflen
, errnop
);
416 return NSS_STATUS_NOTFOUND
;
419 /* -------------------------------------------------- */
422 static size_t host_iter
;
423 #define CURHOST host_data[host_iter]
425 static pthread_mutex_t host_lock
= PTHREAD_MUTEX_INITIALIZER
;
428 NAME(sethostent
) (int stayopen
)
432 return NSS_STATUS_SUCCESS
;
437 NAME(endhostent
) (void)
440 return NSS_STATUS_SUCCESS
;
443 static enum nss_status
444 copy_host (struct hostent
*result
, struct hostent
*local
,
445 char *buffer
, size_t buflen
, int *errnop
)
447 struct alloc_buffer buf
= alloc_buffer_create (buffer
, buflen
);
451 if (local
->h_addr_list
)
454 while (local
->h_addr_list
[i
])
457 memlist
= alloc_buffer_alloc_array (&buf
, char *, i
+ 1);
460 for (j
= 0; j
< i
; ++j
)
461 memlist
[j
] = alloc_buffer_maybe_copy_string (&buf
, local
->h_addr_list
[j
]);
465 result
->h_addr_list
= memlist
;
469 result
->h_addr_list
= NULL
;
472 result
->h_aliases
= NULL
;
473 result
->h_addrtype
= AF_INET
;
474 result
->h_length
= 4;
475 result
->h_name
= alloc_buffer_maybe_copy_string (&buf
, local
->h_name
);
477 if (alloc_buffer_has_failed (&buf
))
480 return NSS_STATUS_TRYAGAIN
;
483 return NSS_STATUS_SUCCESS
;
488 NAME(gethostent_r
) (struct hostent
*ret
, char *buffer
, size_t buflen
,
489 struct hostent
**result
, int *errnop
)
491 int res
= NSS_STATUS_SUCCESS
;
494 pthread_mutex_lock (&host_lock
);
496 if (host_iter
>= nhost_data
)
498 res
= NSS_STATUS_NOTFOUND
;
503 res
= copy_host (ret
, &CURHOST
, buffer
, buflen
, errnop
);
508 pthread_mutex_unlock (&host_lock
);
514 NAME(gethostbyname3_r
) (const char *name
, int af
, struct hostent
*ret
,
515 char *buffer
, size_t buflen
, int *errnop
,
516 int *h_errnop
, int32_t *ttlp
, char **canonp
)
520 for (size_t idx
= 0; idx
< nhost_data
; ++idx
)
521 if (strcmp (host_data
[idx
].h_name
, name
) == 0)
522 return copy_host (ret
, & host_data
[idx
], buffer
, buflen
, h_errnop
);
524 return NSS_STATUS_NOTFOUND
;
528 NAME(gethostbyname_r
) (const char *name
, struct hostent
*result
,
529 char *buffer
, size_t buflen
,
530 int *errnop
, int *h_errnop
)
532 return NAME(gethostbyname3_r
) (name
, AF_INET
, result
, buffer
, buflen
,
533 errnop
, h_errnop
, NULL
, NULL
);
537 NAME(gethostbyname2_r
) (const char *name
, int af
, struct hostent
*result
,
538 char *buffer
, size_t buflen
,
539 int *errnop
, int *h_errnop
)
541 return NAME(gethostbyname3_r
) (name
, af
, result
, buffer
, buflen
,
542 errnop
, h_errnop
, NULL
, NULL
);
546 NAME(gethostbyaddr2_r
) (const void *addr
, socklen_t len
, int af
,
547 struct hostent
*result
, char *buffer
, size_t buflen
,
548 int *errnop
, int *h_errnop
, int32_t *ttlp
)
552 /* Support this later. */
554 return NSS_STATUS_NOTFOUND
;
556 for (size_t idx
= 0; idx
< nhost_data
; ++idx
)
557 if (memcmp (host_data
[idx
].h_addr
, addr
, len
) == 0)
558 return copy_host (result
, & host_data
[idx
], buffer
, buflen
, h_errnop
);
560 return NSS_STATUS_NOTFOUND
;
563 /* Note: only the first address is supported, intentionally. */
565 NAME(gethostbyaddr_r
) (const void *addr
, socklen_t len
, int af
,
566 struct hostent
*result
, char *buffer
, size_t buflen
,
567 int *errnop
, int *h_errnop
)
569 return NAME(gethostbyaddr2_r
) (addr
, len
, af
, result
, buffer
, buflen
,
570 errnop
, h_errnop
, NULL
);