以编程方式或通过脚本更改Mac OS X用户密码

时间:2008-12-09 18:09:28

标签: cocoa macos passwords

我需要能够从cron任务或ssh会话更改用户的密码。有一个简单的方法来使用bash脚本吗?如果没有,在Cocoa中最简单的方法是什么?

2 个答案:

答案 0 :(得分:1)

使用passwd shell命令。

答案 1 :(得分:1)

Apple在Mac OS 10.5中引入了CSIdentitySetPassword API,允许更改密码,如下所示:

#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可以像int this response一样初始化。