Change Mac OS X User Password Programmatically or via Script

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 this with a bash script? If not, what's the easiest way to do it in Cocoa?

+1


source to share


2 answers


Use a shell command passwd

.



+1


source


Apple introduced the CSIdentitySetPassword API in Mac OS 10.5, which allows you to change your password like this:

#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 as int in this answer.

+1


source







All Articles