diff options
author | Andrew G. Morgan <morgan@kernel.org> | 2001-12-09 22:15:11 +0000 |
---|---|---|
committer | Andrew G. Morgan <morgan@kernel.org> | 2001-12-09 22:15:11 +0000 |
commit | da67a7d6126846939fd43b1ddb5aa8c06ee09301 (patch) | |
tree | e9df9e69023b0e8584ee85cd8a44daf210bee75d /modules/pammodutil/modutil_getpwnam.c | |
parent | cb7734d4080f3673a34594ee4c6e7b02dcd89f33 (diff) | |
download | pam-da67a7d6126846939fd43b1ddb5aa8c06ee09301.tar.gz pam-da67a7d6126846939fd43b1ddb5aa8c06ee09301.tar.bz2 pam-da67a7d6126846939fd43b1ddb5aa8c06ee09301.zip |
Relevant BUGIDs: 490938
Purpose of commit: new feature
Commit summary:
---------------
Added libpammodutil and link it with every module as its built.
The issue here is that there is a lot of code that the various modules
use in common, and this staic library can be used to help make this code
more maintainable. I do not intend to make this library dynamic. Especially
right now, as I want to be free to chop and change the API and don't want
to deal with revision control and third party modules.
This checkin makes the pam_rhost_auth module make some use of this new
library. I don't intend to add support for any other module prior to
releasing 0.76.
Diffstat (limited to 'modules/pammodutil/modutil_getpwnam.c')
-rw-r--r-- | modules/pammodutil/modutil_getpwnam.c | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/modules/pammodutil/modutil_getpwnam.c b/modules/pammodutil/modutil_getpwnam.c new file mode 100644 index 00000000..287dc065 --- /dev/null +++ b/modules/pammodutil/modutil_getpwnam.c @@ -0,0 +1,80 @@ +/* + * $Id$ + * + * This function provides a thread safer version of getpwnam() for use + * with PAM modules that care about this sort of thing. + * + * XXX - or at least it should provide a thread-safe alternative. + */ + +#include "pammodutil.h" + +#include <pwd.h> +#include <stdlib.h> + +struct passwd *_pammodutil_getpwnam(pam_handle_t *pamh, const char *user) +{ +#ifdef HAVE_GETPWNAM_R + + void *buffer=NULL; + size_t length = PWD_INITIAL_LENGTH; + + do { + int status; + void *new_buffer; + struct passwd *result = NULL; + + new_buffer = realloc(buffer, sizeof(struct passwd) + length); + if (new_buffer == NULL) { + + D(("out of memory")); + + /* no memory for the user - so delete the memory */ + if (buffer) { + free(buffer); + } + return NULL; + } + buffer = new_buffer; + + /* make the re-entrant call to get the pwd structure */ + status = getpwnam_r(user, buffer, + sizeof(struct passwd) + (char *) buffer, + length, &result); + if (!status && result) { + status = pam_set_data(pamh, "_pammodutil_getpwnam", result, + _pammodutil_cleanup); + if (status == PAM_SUCCESS) { + D(("success")); + return result; + } + + D(("was unable to register the data item [%s]", + pam_strerror(pamh, status))); + + free(buffer); + return NULL; + + } + + length <<= 1; + + } while (length < PWD_ABSURD_PWD_LENGTH); + + D(("pwd structure took %u bytes or so of memory", + length+sizeof(struct passwd))); + + free(buffer); + return NULL; + +#else /* ie. ifndef HAVE_GETPWNAM_R */ + + /* + * Sorry, there does not appear to be a reentrant version of + * getpwnam(). So, we use the standard libc function. + */ + + return getpwnam(user); + +#endif /* def HAVE_GETPWNAM_R */ +} |