4 * This function provides a thread safer version of getpwnam() for use
5 * with PAM modules that care about this sort of thing.
7 * XXX - or at least it should provide a thread-safe alternative.
10 #include "pammodutil.h"
15 struct passwd *_pammodutil_getpwnam(pam_handle_t *pamh, const char *user)
17 #ifdef HAVE_GETPWNAM_R
20 size_t length = PWD_INITIAL_LENGTH;
25 struct passwd *result = NULL;
27 new_buffer = realloc(buffer, sizeof(struct passwd) + length);
28 if (new_buffer == NULL) {
32 /* no memory for the user - so delete the memory */
40 /* make the re-entrant call to get the pwd structure */
41 status = getpwnam_r(user, buffer,
42 sizeof(struct passwd) + (char *) buffer,
44 if (!status && result) {
45 status = pam_set_data(pamh, "_pammodutil_getpwnam", result,
47 if (status == PAM_SUCCESS) {
52 D(("was unable to register the data item [%s]",
53 pam_strerror(pamh, status)));
62 } while (length < PWD_ABSURD_PWD_LENGTH);
64 D(("pwd structure took %u bytes or so of memory",
65 length+sizeof(struct passwd)));
70 #else /* ie. ifndef HAVE_GETPWNAM_R */
73 * Sorry, there does not appear to be a reentrant version of
74 * getpwnam(). So, we use the standard libc function.
77 return getpwnam(user);
79 #endif /* def HAVE_GETPWNAM_R */