Cocoa AuthorizationExecuteWithPrivileges()作为根错误

Cocoa AuthorizationExecuteWithPrivileges()作为根错误,cocoa,authorization,root,sudo,nstask,Cocoa,Authorization,Root,Sudo,Nstask,我是可可的初学者 我只想在我的Cocoa应用程序中启动Apache和其他进程 这是我的密码: OSStatus myStatus; AuthorizationFlags myFlags = kAuthorizationFlagDefaults; AuthorizationRef myAuthorizationRef; FILE *pipe = NULL; myStatus = AuthorizationCreate(NULL, kAuthorization

我是可可的初学者

我只想在我的Cocoa应用程序中启动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);
结果:此操作需要root
当我运行'whoami'时,我是'root',但当我运行getuid()时,我是'501'

我尝试使用setuid(0);但是它没有设定

你能帮我吗
谢谢

我将在/etc/sudoers中为uid=501的用户定义对/usr/sbin/apachectl的访问权限,并在代码中执行“sudo/usr/sbin/apachectl”而不是/usr/sbin/apachectl。

我面临着完全相同的问题! whoami返回“root”,root命令返回“root required”,这太疯狂了!!
sudoers的解决方案可能有效,但我认为我们必须搜索setuid位,正如这里所写的那样

我遇到了完全相同的问题。AuthorizationExecuteWithPrivileges允许您将权限升级到root,但不会自动执行(我猜是为了保留用户会话或其他)

我最终生成了一个通用可执行文件,它将通过AuthorizationExecuteWithPrivileges运行,然后该可执行文件将uid设置为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;
}
它将setuid设置为root,然后使用给定的参数运行有问题的程序


请注意,您必须从AuthorizationExecuteWithPrivileges调用setuid,因为只有AuthorizationExecuteWithPrivileges创建的pid将具有升级权限(并且setuid将执行并用您自己的进程替换该进程)。

我得到了解决方案。。。您只需创建一个简单的程序,使用一个调用apache命令的NStask。并且该程序必须以setuid(0)开头;然后构建它,并通过AuthorizationExecuteWithPrivileges运行它。我尝试了,但没有成功。你能给我举个小例子吗。。。谢谢我编译了这个:然后,我把它放进去了,谢谢你的回答,我把它剪下来粘贴进去了!一个错误:execv不知道需要多少个参数,因此需要将argvz的大小增加一个,并添加一个指向结束argvz[argc-1]=null的null指针;还可以包括和以消除编译器警告。
setuid my-program-to-run and arguments to pass