1 /* Template generic NSS service provider. See nss_test.h for usage.
2 Copyright (C) 2017-2018 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 \
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 /* This function will get called, and once per session, look back into
70 the test case's executable for an init hook function, and call
73 static int initted
= 0;
84 memset (&t
, 0, sizeof(t
));
89 pwd_data
= t
.pwd_table
;
90 for (i
=0; ! PWD_ISLAST(& pwd_data
[i
]); i
++)
97 grp_data
= t
.grp_table
;
98 for (i
=0; ! GRP_ISLAST(& grp_data
[i
]); i
++)
106 /* -------------------------------------------------- */
107 /* Password handling. */
109 static size_t pwd_iter
;
110 #define CURPWD pwd_data[pwd_iter]
112 static pthread_mutex_t pwd_lock
= PTHREAD_MUTEX_INITIALIZER
;
115 NAME(setpwent
) (int stayopen
)
119 return NSS_STATUS_SUCCESS
;
124 NAME(endpwent
) (void)
127 return NSS_STATUS_SUCCESS
;
130 static enum nss_status
131 copy_passwd (struct passwd
*result
, struct passwd
*local
,
132 char *buffer
, size_t buflen
, int *errnop
)
134 struct alloc_buffer buf
= alloc_buffer_create (buffer
, buflen
);
136 result
->pw_name
= alloc_buffer_maybe_copy_string (&buf
, local
->pw_name
);
137 result
->pw_passwd
= alloc_buffer_maybe_copy_string (&buf
, local
->pw_passwd
);
138 result
->pw_uid
= local
->pw_uid
;
139 result
->pw_gid
= local
->pw_gid
;
140 result
->pw_gecos
= alloc_buffer_maybe_copy_string (&buf
, local
->pw_gecos
);
141 result
->pw_dir
= alloc_buffer_maybe_copy_string (&buf
, local
->pw_dir
);
142 result
->pw_shell
= alloc_buffer_maybe_copy_string (&buf
, local
->pw_shell
);
144 if (alloc_buffer_has_failed (&buf
))
147 return NSS_STATUS_TRYAGAIN
;
150 return NSS_STATUS_SUCCESS
;
154 NAME(getpwent_r
) (struct passwd
*result
, char *buffer
, size_t buflen
,
157 int res
= NSS_STATUS_SUCCESS
;
160 pthread_mutex_lock (&pwd_lock
);
162 if (pwd_iter
>= npwd_data
)
163 res
= NSS_STATUS_NOTFOUND
;
166 res
= copy_passwd (result
, &CURPWD
, buffer
, buflen
, errnop
);
170 pthread_mutex_unlock (&pwd_lock
);
177 NAME(getpwuid_r
) (uid_t uid
, struct passwd
*result
, char *buffer
,
178 size_t buflen
, int *errnop
)
181 for (size_t idx
= 0; idx
< npwd_data
; ++idx
)
182 if (pwd_data
[idx
].pw_uid
== uid
)
183 return copy_passwd (result
, &pwd_data
[idx
], buffer
, buflen
, errnop
);
185 return NSS_STATUS_NOTFOUND
;
190 NAME(getpwnam_r
) (const char *name
, struct passwd
*result
, char *buffer
,
191 size_t buflen
, int *errnop
)
194 for (size_t idx
= 0; idx
< npwd_data
; ++idx
)
195 if (strcmp (pwd_data
[idx
].pw_name
, name
) == 0)
196 return copy_passwd (result
, &pwd_data
[idx
], buffer
, buflen
, errnop
);
198 return NSS_STATUS_NOTFOUND
;
201 /* -------------------------------------------------- */
202 /* Group handling. */
204 static size_t grp_iter
;
205 #define CURGRP grp_data[grp_iter]
207 static pthread_mutex_t grp_lock
= PTHREAD_MUTEX_INITIALIZER
;
210 NAME(setgrent
) (int stayopen
)
214 return NSS_STATUS_SUCCESS
;
219 NAME(endgrent
) (void)
222 return NSS_STATUS_SUCCESS
;
225 static enum nss_status
226 copy_group (struct group
*result
, struct group
*local
,
227 char *buffer
, size_t buflen
, int *errnop
)
229 struct alloc_buffer buf
= alloc_buffer_create (buffer
, buflen
);
236 while (local
->gr_mem
[i
])
239 memlist
= alloc_buffer_alloc_array (&buf
, char *, i
+ 1);
242 for (i
= 0; local
->gr_mem
[i
]; ++i
)
243 memlist
[i
] = alloc_buffer_maybe_copy_string (&buf
, local
->gr_mem
[i
]);
247 result
->gr_mem
= memlist
;
250 result
->gr_mem
= NULL
;
252 result
->gr_name
= alloc_buffer_maybe_copy_string (&buf
, local
->gr_name
);
253 result
->gr_passwd
= alloc_buffer_maybe_copy_string (&buf
, local
->gr_passwd
);
254 result
->gr_gid
= local
->gr_gid
;
256 if (alloc_buffer_has_failed (&buf
))
259 return NSS_STATUS_TRYAGAIN
;
262 return NSS_STATUS_SUCCESS
;
267 NAME(getgrent_r
) (struct group
*result
, char *buffer
, size_t buflen
,
270 int res
= NSS_STATUS_SUCCESS
;
273 pthread_mutex_lock (&grp_lock
);
275 if (grp_iter
>= ngrp_data
)
276 res
= NSS_STATUS_NOTFOUND
;
279 res
= copy_group (result
, &CURGRP
, buffer
, buflen
, errnop
);
283 pthread_mutex_unlock (&pwd_lock
);
290 NAME(getgrgid_r
) (gid_t gid
, struct group
*result
, char *buffer
,
291 size_t buflen
, int *errnop
)
294 for (size_t idx
= 0; idx
< ngrp_data
; ++idx
)
295 if (grp_data
[idx
].gr_gid
== gid
)
296 return copy_group (result
, &grp_data
[idx
], buffer
, buflen
, errnop
);
298 return NSS_STATUS_NOTFOUND
;
303 NAME(getgrnam_r
) (const char *name
, struct group
*result
, char *buffer
,
304 size_t buflen
, int *errnop
)
307 for (size_t idx
= 0; idx
< ngrp_data
; ++idx
)
308 if (strcmp (pwd_data
[idx
].pw_name
, name
) == 0)
310 return copy_group (result
, &grp_data
[idx
], buffer
, buflen
, errnop
);
313 return NSS_STATUS_NOTFOUND
;