1 /* Template generic NSS service provider. See nss_test.h for usage.
2 Copyright (C) 2017 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 <http://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 (sizeof (pwd_data) / sizeof (pwd_data[0]))
62 static struct passwd
*pwd_data
= default_pwd_data
;
63 static int npwd_data
= default_npwd_data
;
65 static struct group
*grp_data
= NULL
;
66 static int ngrp_data
= 0;
68 /* This function will get called, and once per session, look back into
69 the test case's executable for an init hook function, and call
72 static int initted
= 0;
83 memset (&t
, 0, sizeof(t
));
88 pwd_data
= t
.pwd_table
;
89 for (i
=0; ! PWD_ISLAST(& pwd_data
[i
]); i
++)
96 grp_data
= t
.grp_table
;
97 for (i
=0; ! GRP_ISLAST(& grp_data
[i
]); i
++)
105 /* -------------------------------------------------- */
106 /* Password handling. */
108 static size_t pwd_iter
;
109 #define CURPWD pwd_data[pwd_iter]
111 static pthread_mutex_t pwd_lock
= PTHREAD_MUTEX_INITIALIZER
;
114 NAME(setpwent
) (int stayopen
)
118 return NSS_STATUS_SUCCESS
;
123 NAME(endpwent
) (void)
126 return NSS_STATUS_SUCCESS
;
129 static enum nss_status
130 copy_passwd (struct passwd
*result
, struct passwd
*local
,
131 char *buffer
, size_t buflen
, int *errnop
)
133 struct alloc_buffer buf
= alloc_buffer_create (buffer
, buflen
);
135 result
->pw_name
= alloc_buffer_maybe_copy_string (&buf
, local
->pw_name
);
136 result
->pw_passwd
= alloc_buffer_maybe_copy_string (&buf
, local
->pw_passwd
);
137 result
->pw_uid
= local
->pw_uid
;
138 result
->pw_gid
= local
->pw_gid
;
139 result
->pw_gecos
= alloc_buffer_maybe_copy_string (&buf
, local
->pw_gecos
);
140 result
->pw_dir
= alloc_buffer_maybe_copy_string (&buf
, local
->pw_dir
);
141 result
->pw_shell
= alloc_buffer_maybe_copy_string (&buf
, local
->pw_shell
);
143 if (alloc_buffer_has_failed (&buf
))
146 return NSS_STATUS_TRYAGAIN
;
149 return NSS_STATUS_SUCCESS
;
153 NAME(getpwent_r
) (struct passwd
*result
, char *buffer
, size_t buflen
,
156 int res
= NSS_STATUS_SUCCESS
;
159 pthread_mutex_lock (&pwd_lock
);
161 if (pwd_iter
>= npwd_data
)
162 res
= NSS_STATUS_NOTFOUND
;
165 res
= copy_passwd (result
, &CURPWD
, buffer
, buflen
, errnop
);
169 pthread_mutex_unlock (&pwd_lock
);
176 NAME(getpwuid_r
) (uid_t uid
, struct passwd
*result
, char *buffer
,
177 size_t buflen
, int *errnop
)
180 for (size_t idx
= 0; idx
< npwd_data
; ++idx
)
181 if (pwd_data
[idx
].pw_uid
== uid
)
182 return copy_passwd (result
, &pwd_data
[idx
], buffer
, buflen
, errnop
);
184 return NSS_STATUS_NOTFOUND
;
189 NAME(getpwnam_r
) (const char *name
, struct passwd
*result
, char *buffer
,
190 size_t buflen
, int *errnop
)
193 for (size_t idx
= 0; idx
< npwd_data
; ++idx
)
194 if (strcmp (pwd_data
[idx
].pw_name
, name
) == 0)
195 return copy_passwd (result
, &pwd_data
[idx
], buffer
, buflen
, errnop
);
197 return NSS_STATUS_NOTFOUND
;
200 /* -------------------------------------------------- */
201 /* Group handling. */
203 static size_t grp_iter
;
204 #define CURGRP grp_data[grp_iter]
206 static pthread_mutex_t grp_lock
= PTHREAD_MUTEX_INITIALIZER
;
209 NAME(setgrent
) (int stayopen
)
213 return NSS_STATUS_SUCCESS
;
218 NAME(endgrent
) (void)
221 return NSS_STATUS_SUCCESS
;
224 static enum nss_status
225 copy_group (struct group
*result
, struct group
*local
,
226 char *buffer
, size_t buflen
, int *errnop
)
228 struct alloc_buffer buf
= alloc_buffer_create (buffer
, buflen
);
235 while (local
->gr_mem
[i
])
238 memlist
= alloc_buffer_alloc_array (&buf
, char *, i
+ 1);
241 for (i
= 0; local
->gr_mem
[i
]; ++i
)
242 memlist
[i
] = alloc_buffer_maybe_copy_string (&buf
, local
->gr_mem
[i
]);
246 result
->gr_mem
= memlist
;
249 result
->gr_mem
= NULL
;
251 result
->gr_name
= alloc_buffer_maybe_copy_string (&buf
, local
->gr_name
);
252 result
->gr_passwd
= alloc_buffer_maybe_copy_string (&buf
, local
->gr_passwd
);
253 result
->gr_gid
= local
->gr_gid
;
255 if (alloc_buffer_has_failed (&buf
))
258 return NSS_STATUS_TRYAGAIN
;
261 return NSS_STATUS_SUCCESS
;
266 NAME(getgrent_r
) (struct group
*result
, char *buffer
, size_t buflen
,
269 int res
= NSS_STATUS_SUCCESS
;
272 pthread_mutex_lock (&grp_lock
);
274 if (grp_iter
>= ngrp_data
)
275 res
= NSS_STATUS_NOTFOUND
;
278 res
= copy_group (result
, &CURGRP
, buffer
, buflen
, errnop
);
282 pthread_mutex_unlock (&pwd_lock
);
289 NAME(getgrgid_r
) (gid_t gid
, struct group
*result
, char *buffer
,
290 size_t buflen
, int *errnop
)
293 for (size_t idx
= 0; idx
< ngrp_data
; ++idx
)
294 if (grp_data
[idx
].gr_gid
== gid
)
295 return copy_group (result
, &grp_data
[idx
], buffer
, buflen
, errnop
);
297 return NSS_STATUS_NOTFOUND
;
302 NAME(getgrnam_r
) (const char *name
, struct group
*result
, char *buffer
,
303 size_t buflen
, int *errnop
)
306 for (size_t idx
= 0; idx
< ngrp_data
; ++idx
)
307 if (strcmp (pwd_data
[idx
].pw_name
, name
) == 0)
309 return copy_group (result
, &grp_data
[idx
], buffer
, buflen
, errnop
);
312 return NSS_STATUS_NOTFOUND
;