2
* $Id: pam_modutil_getpwnam.c,v 1.3 2008/05/14 12:55:02 t8m Exp $
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 "pam_modutil_private.h"
18
static int intlen(int number)
29
pam_modutil_getpwnam(pam_handle_t *pamh, const char *user)
31
#ifdef HAVE_GETPWNAM_R
34
size_t length = PWD_INITIAL_LENGTH;
39
struct passwd *result = NULL;
41
new_buffer = realloc(buffer, sizeof(struct passwd) + length);
42
if (new_buffer == NULL) {
46
/* no memory for the user - so delete the memory */
54
/* make the re-entrant call to get the pwd structure */
56
status = getpwnam_r(user, buffer,
57
sizeof(struct passwd) + (char *) buffer,
59
if (!status && (result == buffer)) {
64
data_name = malloc(strlen("_pammodutil_getpwnam") + 1 +
65
strlen(user) + 1 + intlen(INT_MAX) + 1);
66
if ((pamh != NULL) && (data_name == NULL)) {
67
D(("was unable to register the data item [%s]",
68
pam_strerror(pamh, status)));
74
for (i = 0; i < INT_MAX; i++) {
75
sprintf(data_name, "_pammodutil_getpwnam_%s_%d", user, i);
76
status = PAM_NO_MODULE_DATA;
77
if (pam_get_data(pamh, data_name, &ignore) != PAM_SUCCESS) {
78
status = pam_set_data(pamh, data_name,
79
result, pam_modutil_cleanup);
81
if (status == PAM_SUCCESS) {
91
if (status == PAM_SUCCESS) {
96
D(("was unable to register the data item [%s]",
97
pam_strerror(pamh, status)));
102
} else if (errno != ERANGE && errno != EINTR) {
103
/* no sense in repeating the call */
107
length <<= PWD_LENGTH_SHIFT;
109
} while (length < PWD_ABSURD_PWD_LENGTH);
111
D(("pwd structure took %u bytes or so of memory",
112
length+sizeof(struct passwd)));
117
#else /* ie. ifndef HAVE_GETPWNAM_R */
120
* Sorry, there does not appear to be a reentrant version of
121
* getpwnam(). So, we use the standard libc function.
124
return getpwnam(user);
126
#endif /* def HAVE_GETPWNAM_R */