2

I need to be able to change a user's password from a cron task or from an ssh session. Is there an easy way to do that with a bash script? If not, what's the easiest way to do it in Cocoa?

2 Answers 2

2

Apple introduced CSIdentitySetPassword API in Mac OS 10.5 that allows to change password as follows:

#import <Collaboration/Collaboration.h>

    AuthorizationRef authRef = NULL; // You have to initialize authRef

    CBIdentityAuthority *authority = [CBIdentityAuthority defaultIdentityAuthority];
    CSIdentityRef identity = [CBIdentity identityWithName:user authority:authority].CSIdentity;
    if (CSIdentityGetClass(identity) == kCSIdentityClassUser) {
        CSIdentitySetPassword(identity, (__bridge CFStringRef)newPassword);
        CSIdentityCommit(identity, authRef, NULL);
    }

AuthenticationRef can be initialized like int this response.

Sign up to request clarification or add additional context in comments.

Comments

1

Use the passwd shell command.

2 Comments

Doesn't passwd block redirecting input? If it doesn't, you could do this but you'd have to write all three lines out to a file (original, new, new) and then redirect it, but I thought that wasn't supposed to work (for safety). Could be wrong.
beware, passwd(1) does not change the password the the user's keychain!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.