Le *_*kin 3 cocoa sudo authorization root nstask
我是可可的初学者......
我只想在我的Cocoa App中启动Apache和其他进程.
这是我的代码:
OSStatus myStatus;
AuthorizationFlags myFlags = kAuthorizationFlagDefaults;
AuthorizationRef myAuthorizationRef;
FILE *pipe = NULL;
myStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, myFlags, &myAuthorizationRef);
AuthorizationItem myItems = {kAuthorizationRightExecute, 0, NULL, 0};
AuthorizationRights myRights = {1, &myItems};
myFlags = kAuthorizationFlagDefaults |
kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagPreAuthorize |
kAuthorizationFlagExtendRights;
myStatus = AuthorizationCopyPrivilegedReference (&myAuthorizationRef,kAuthorizationFlagDefaults);
myStatus = AuthorizationCopyRights (myAuthorizationRef,&myRights, NULL, myFlags, NULL );
char *tool = "/usr/sbin/apachectl";
char *args[] = { "start",NULL} ;
myStatus = AuthorizationExecuteWithPrivileges(myAuthorizationRef, tool, kAuthorizationFlagDefaults, args, &pipe);
char c[100];
int n=fread(c,1,100,pipe);
c[n] = '\0';
NSLog(@"%s\n",c);
Run Code Online (Sandbox Code Playgroud)
theResult: 这个操作需要root
当我运行'whoami'时,我是' root'但是当我运行getuid()时,我' 501' ......
我尝试使用setuid(0); 但它没有设置!!
你能帮助我吗?
谢谢
kou*_*ush 14
我有同样的问题.AuthorizationExecuteWithPrivileges允许您将权限升级到root,但不会自动执行(我想保留用户会话或其他任何内容).
我最终创建了一个通过AuthorizationExecuteWithPrivileges运行的通用可执行文件,然后该可执行文件将setuid为root,然后执行您实际想要以root身份运行的进程.
这是setuid包装器可执行文件的源代码:
#include <stdio.h>
int main(int argc, char** argv) {
if (argc < 2) {
printf("not enough arguments\n");
return -1;
}
if (0 != setuid(0)) {
printf("setuid failed.\n");
return -3;
}
int i;
char** argvz = (char**)malloc(sizeof(char*) * (argc - 1));
for (i = 1; i < argc; i++) {
argvz[i - 1] = argv[i];
}
execv(argv[1], argvz);
printf("execv returned?\n");
return -2;
}
Run Code Online (Sandbox Code Playgroud)
然后,基本上运行(通过AuthorizationExecuteWithPrivileges调用它)为:
setuid my-program-to-run and arguments to pass
Run Code Online (Sandbox Code Playgroud)
它将以root身份setuid,然后使用给定的args运行有问题的程序.
请注意,您必须从AuthorizationExecuteWithPrivileges调用setuid因为只有AuthorizationExecuteWithPrivileges创建的pid才具有升级的权限(并且setuid将执行并用您自己的进程替换该进程).
| 归档时间: |
|
| 查看次数: |
4194 次 |
| 最近记录: |