I wrote a short shell script that changes the password of the account that the user chooses using the passwd command. I wanted to make some sort of clean and nice gui for it
Running the
passwd
command from a GUI app is tricky because that command expects to have
stdin
connected to a terminal device. You can do this, using a
pseudoterminal, but it’s a challenge.
A better option would be to use an API to change the user’s password, and the preferred API for this is the OpenDirectory framework. This isn’t easy, but it’s a lot easier than messing with pseudoterminals.
There’s two parts to this:
Tracking down the user’s OD record
Changing the password in that record
Part 1 is the hard part, but you can find some code to do it in this post.
Note That code uses
kODNodeTypeLocalNodes
, which means it’ll only operate on local users. If you want to support any user, including users from a remote directory service, use
kODNodeTypeAuthentication
.
Part 2 is surprisingly easy. Once you have the OD record, you can just call
changePassword(_:toPassword:)
.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"