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"
18 static pthread_mutex_t _pammodutil_mutex = PTHREAD_MUTEX_INITIALIZER;
19 static void _pammodutil_lock(void)
21 pthread_mutex_lock(&_pammodutil_mutex);
23 static void _pammodutil_unlock(void)
25 pthread_mutex_unlock(&_pammodutil_mutex);
28 static int intlen(int number)
38 struct passwd *_pammodutil_getpwnam(pam_handle_t *pamh, const char *user)
40 #ifdef HAVE_GETPWNAM_R
43 size_t length = PWD_INITIAL_LENGTH;
48 struct passwd *result = NULL;
50 new_buffer = realloc(buffer, sizeof(struct passwd) + length);
51 if (new_buffer == NULL) {
55 /* no memory for the user - so delete the memory */
63 /* make the re-entrant call to get the pwd structure */
64 status = getpwnam_r(user, buffer,
65 sizeof(struct passwd) + (char *) buffer,
67 if (!status && (result == buffer)) {
72 data_name = malloc(strlen("_pammodutil_getpwnam") + 1 +
73 strlen(user) + 1 + intlen(INT_MAX) + 1);
74 if ((pamh != NULL) && (data_name == NULL)) {
75 D(("was unable to register the data item [%s]",
76 pam_strerror(pamh, status)));
82 for (i = 0; i < INT_MAX; i++) {
83 sprintf(data_name, "_pammodutil_getpwnam_%s_%d", user, i);
85 status = PAM_NO_MODULE_DATA;
86 if (pam_get_data(pamh, data_name, &ignore) != PAM_SUCCESS) {
87 status = pam_set_data(pamh, data_name,
88 result, _pammodutil_cleanup);
91 if (status == PAM_SUCCESS) {
101 if (status == PAM_SUCCESS) {
106 D(("was unable to register the data item [%s]",
107 pam_strerror(pamh, status)));
116 } while (length < PWD_ABSURD_PWD_LENGTH);
118 D(("pwd structure took %u bytes or so of memory",
119 length+sizeof(struct passwd)));
124 #else /* ie. ifndef HAVE_GETPWNAM_R */
127 * Sorry, there does not appear to be a reentrant version of
128 * getpwnam(). So, we use the standard libc function.
131 return getpwnam(user);
133 #endif /* def HAVE_GETPWNAM_R */