I've seen several threads where people want to change their password via a script by interfacing with /bin/passwd. Since /bin/passwd talks to a TTY, this usually means resorting to using something like expect.

This is a short script using the Authen::PAM module that works with STDIN/STDOUT, and therefore could be wrapped in a script more easily.

If you run it setuid, and uncomment the ruid=euid bit, folks can change their password without supplying their current password. Careful with that.

Caveat Emptor.

#!/usr/bin/perl $ENV{'PATH'} = '/bin:/usr/bin'; delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'}; use Authen::PAM; my($user,$pamh,$ret,$error); if (!defined($user=getpwuid($<)) ) { die("Can't find user for uid $<\n"); } ## THIS COULD OPEN UP UNWANTED BEHAVIOUR ## IF THIS SCRIPT IS RUNNING SETUID AND ## YOU UNCOMMENT IT. ## (if ruid == 0, then it won't prompt ## for the old password, just the new one) #$<=$>; # $pamh= new Authen::PAM("passwd", $user); $ret=pam_chauthtok($pamh); if ($ret != PAM_SUCCESS) { $error = pam_strerror($pamh, $ret); die("change password failed [$error]\n"); }

In reply to Non-tty based passwd by kschwab

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.