diff --git a/ChangeLog b/ChangeLog index 0d2d4900..45fde2b0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2009-05-07 Nicolas François + + * libmisc/non_interactive_pam_conv.c, + libmisc/pam_pass_non_interractive.c, libmisc/Makefile.am: Renamed. + * libmisc/pam_pass_non_interractive.c, lib/prototypes.h: + non_interactive_password and non_interactive_pam_conv do not need + to be externally visible. + * libmisc/pam_pass_non_interractive.c: Added declaration of + ni_conv. + * libmisc/pam_pass_non_interractive.c: Only compile ifdef USE_PAM. + * libmisc/pam_pass_non_interractive.c, lib/prototypes.h: + Added do_pam_passwd_non_interractive(). + * src/chpasswd.c: Use do_pam_passwd_non_interractive(). + 2009-05-07 Nicolas François * libmisc/pam_pass.c: Removed comment regarding pam_misc. This is diff --git a/lib/prototypes.h b/lib/prototypes.h index dfeedfec..f2c7ebbb 100644 --- a/lib/prototypes.h +++ b/lib/prototypes.h @@ -238,9 +238,10 @@ extern void motd (void); /* myname.c */ extern /*@null@*/struct passwd *get_my_pwent (void); -/* non_interactive_pam_conv.c */ -/*@null@*/ /*@only@*/extern char *non_interactive_password; -extern struct pam_conv non_interactive_pam_conv; +/* pam_pass_non_interractive.c */ +extern int do_pam_passwd_non_interractive (const char *pam_service, + const char *username, + const char* password) /* obscure.c */ #ifndef USE_PAM diff --git a/libmisc/Makefile.am b/libmisc/Makefile.am index 12ee065a..8b577b29 100644 --- a/libmisc/Makefile.am +++ b/libmisc/Makefile.am @@ -38,9 +38,9 @@ libmisc_a_SOURCES = \ mail.c \ motd.c \ myname.c \ - non_interactive_pam_conv.c \ obscure.c \ pam_pass.c \ + pam_pass_non_interractive.c \ pwd2spwd.c \ pwdcheck.c \ pwd_init.c \ diff --git a/libmisc/non_interactive_pam_conv.c b/libmisc/pam_pass_non_interractive.c similarity index 73% rename from libmisc/non_interactive_pam_conv.c rename to libmisc/pam_pass_non_interractive.c index e1f2dcb4..7c597249 100644 --- a/libmisc/non_interactive_pam_conv.c +++ b/libmisc/pam_pass_non_interractive.c @@ -31,6 +31,7 @@ #ident "$Id:$" +#ifdef USE_PAM #include #include #include @@ -38,13 +39,23 @@ #include #include "prototypes.h" -/*@null@*/ /*@only@*/char *non_interactive_password = NULL; +/*@null@*/ /*@only@*/static char *non_interactive_password = NULL; +static int ni_conv (int num_msg, + const struct pam_message **msg, + struct pam_response **resp, + unused void *appdata_ptr); +static struct pam_conv non_interactive_pam_conv = { + ni_conv, + NULL +}; + static int ni_conv (int num_msg, const struct pam_message **msg, struct pam_response **resp, - unused void *appdata_ptr) { + unused void *appdata_ptr) +{ struct pam_response *responses; int count; @@ -117,8 +128,38 @@ failed_conversation: return PAM_CONV_ERR; } -struct pam_conv non_interactive_pam_conv = { - ni_conv, - NULL -}; +/* + * Change non interactively the user's password using PAM. + * + * Return 0 on success, 1 on failure. + */ +int do_pam_passwd_non_interractive (const char *pam_service, + const char *username, + const char* password) +{ + pam_handle_t *pamh = NULL; + int ret; + + ret = pam_start (pam_service, username, &non_interactive_pam_conv, &pamh); + if (ret != PAM_SUCCESS) { + fprintf (stderr, + _("%s: (user %s) pam_start failure %d\n"), + Prog, username, ret); + return 1; + } + + non_interactive_password = password; + ret = pam_chauthtok (pamh, 0); + if (ret != PAM_SUCCESS) { + fprintf (stderr, + _("%s: (user %s) pam_chauthtok() failed, error:\n" + "%s\n"), + Prog, username, pam_strerror (pamh, ret)); + } + + (void) pam_end (pamh, PAM_SUCCESS); +} +#else /* !USE_PAM */ +extern int errno; /* warning: ANSI C forbids an empty source file */ +#endif /* !USE_PAM */ diff --git a/src/chpasswd.c b/src/chpasswd.c index 53ef8a33..8bb5797b 100644 --- a/src/chpasswd.c +++ b/src/chpasswd.c @@ -465,35 +465,12 @@ int main (int argc, char **argv) newpwd = cp; #ifdef USE_PAM - pam_handle_t *pamh = NULL; - int ret; - ret = pam_start ("chpasswd", name, &non_interactive_pam_conv, &pamh); - if (ret != PAM_SUCCESS) { + if (do_pam_passwd_non_interractive ("chpasswd", name, newpwd) != 0) { fprintf (stderr, - _("chpasswd: (user %s) pam_start failure %d\n"), - name, ret); - fprintf (stderr, - _("chpasswd: (user %s) password unchanged\n"), - name); - errors++; - continue; + _("%s: (line %d, user %s) password not changed\n"), + Prog, line, name); + error++; } - - non_interactive_password = newpwd; - ret = pam_chauthtok (pamh, 0); - if (ret != PAM_SUCCESS) { - fprintf (stderr, _("chpasswd: (line %d, user %s) pam_chauthtok() failed, error:\n" - " %s\n"), - line, name, pam_strerror (pamh, ret)); - fprintf (stderr, - _("chpasswd: (line %d, user %s) password unchanged\n"), - line, name); - errors++; - continue; - } - - (void) pam_end (pamh, PAM_SUCCESS); - #else /* !USE_PAM */ if ( !eflg && ( (NULL == crypt_method)