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?

Kristopher Johnson
  • 81,409
  • 55
  • 245
  • 302

2 Answers2

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.

toma
  • 1,471
  • 12
  • 20
1

Use the passwd shell command.

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • 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. – Bill K Dec 09 '08 at 18:39
  • beware, passwd(1) does not change the password the the user's keychain! –  Dec 09 '08 at 18:53