Authorization NSTask "sudo"

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Authorization NSTask "sudo"

    Hey liebes OS X Entwicklerforum!

    Ich habe ein Problem in einem Projekt, in dem ich per NSTask den apache starten will. Dafür brauche ich allerdings root-Rechte und daher habe ich mich der Apple Doc zur Authorization bedient. Leider funktioniert das ganze nicht ganz so wie gewünscht.. Man wird nach dem Passwort gefragt und ich gebe es ein, das funktioniert alles, aber wenn ich mir per NSAlert den Output des NSTasks ausgeben lasse bekomme ich nur: "This operation requires root" :(

    Hier mein Code:

    Quellcode

    1. @interface ApacheControlModel : NSObject {
    2. AuthorizationRef _authRef;
    3. }


    Quellcode

    1. - (void) awakeFromNib {
    2. OSStatus status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &_authRef);
    3. if(status != errAuthorizationSuccess) {
    4. assert(NO);
    5. _authRef = NULL;
    6. }
    7. }
    Alles anzeigen


    Quellcode

    1. - (NSString*) startApache {
    2. AuthorizationItem authItem;
    3. authItem.name = "de.PAT.ApacheControl.startApache";
    4. authItem.valueLength = 0;
    5. authItem.value = NULL;
    6. authItem.flags = 0;
    7. AuthorizationRights authRights = { 1, &authItem };
    8. AuthorizationFlags authFlags = kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights;
    9. OSStatus status = AuthorizationCopyRights(_authRef, &authRights, kAuthorizationEmptyEnvironment, authFlags, NULL);
    10. if(status != errAuthorizationSuccess) {
    11. /** ERR **/
    12. return false;
    13. } else {
    14. NSTask* task = [NSTask new];
    15. [task setLaunchPath: @"/usr/sbin/apachectl"];
    16. [task setArguments: [NSArray arrayWithObjects: @"start", nil]];
    17. NSPipe* pipe = [NSPipe pipe];
    18. [task setStandardOutput: pipe];
    19. [task launch];
    20. NSData* data = [[pipe fileHandleForReading] readDataToEndOfFile];
    21. [task waitUntilExit];
    22. NSString* output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
    23. return output;
    24. }
    25. }
    Alles anzeigen



    Was habe ich falsch gemacht?

    Liebe Grüße und Danke im Vorraus!
    ThePat
  • Funktioniert nicht.. ich hab es jetzt mit AuthorizationExecuteWithPrivileges probiert aber auch das funktioniert nicht.

    Hab den Code von hier: stackoverflow.com/questions/56…hprivileges-as-root-error

    Quellcode

    1. - (NSString*) stopApache {
    2. OSStatus myStatus;
    3. AuthorizationFlags myFlags = kAuthorizationFlagDefaults;
    4. AuthorizationRef myAuthorizationRef;
    5. FILE *pipe = NULL;
    6. myStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, myFlags, &myAuthorizationRef);
    7. AuthorizationItem myItems = {kAuthorizationRightExecute, 0, NULL, 0};
    8. AuthorizationRights myRights = {1, &myItems};
    9. myFlags = kAuthorizationFlagDefaults |
    10. kAuthorizationFlagInteractionAllowed |
    11. kAuthorizationFlagPreAuthorize |
    12. kAuthorizationFlagExtendRights;
    13. myStatus = AuthorizationCopyPrivilegedReference (&myAuthorizationRef,kAuthorizationFlagDefaults);
    14. myStatus = AuthorizationCopyRights (myAuthorizationRef,&myRights, NULL, myFlags, NULL );
    15. char *tool = "sudo /usr/sbin/apachectl";
    16. char *args[] = { "stop", NULL} ;
    17. myStatus = AuthorizationExecuteWithPrivileges(myAuthorizationRef, tool, kAuthorizationFlagDefaults, args, &pipe);
    18. if(pipe != NULL) {
    19. char c[100];
    20. int n=fread(c,1,100,pipe);
    21. c[n] = '\0';
    22. NSLog(@"%s\n",c);
    23. }
    Alles anzeigen


    Dies führte aber dazu, dass pipe = NULL ist und der Befehl nicht ausgeführt wird. Habe dann herausgefunden, nur "/usr/sbin/apachectl" funktioniert aber dann (wie in dem Post beschrieben) die Meldung kommt, dass man root-Privileges brauch. wenn ich "/usr/bin/sudo" mit den args = { "/usr/sbin/apachectl stop", NULL }; probiere kommt nur die Meldung:

    Quellcode

    1. sudo: no tty present and no askpass program specified
    Da kein PW übergeben wird. Es scheint alles nicht zu funktionieren..
  • Habe jetzt schon 3 sample codes ausprobiert, keiner hilft. status ist immer = errAuthorizationSuccess (sowohl nach dem Kopieren der Rechte, als auch nach dem Ausführen) und trotzdem gibt es die Ausgabe: This operation requires root.

    Hier nochmal der Code:

    [C] OSStatus status;
    AuthorizationRef authorizationRef;

    // AuthorizationCreate and pass NULL as the initial
    // AuthorizationRights set so that the AuthorizationRef gets created
    // successfully, and then later call AuthorizationCopyRights to
    // determine or extend the allowable rights.
    // http://developer.apple.com/qa/qa2001/qa1172.html
    status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
    kAuthorizationFlagDefaults, &authorizationRef);
    if (status != errAuthorizationSuccess)
    NSLog(@"Error Creating Initial Authorization: %d", status);

    // kAuthorizationRightExecute == "system.privilege.admin"
    AuthorizationItem right = {kAuthorizationRightExecute, 0, NULL, 0};
    AuthorizationRights rights = {1, &right};
    AuthorizationFlags flags = kAuthorizationFlagDefaults |
    kAuthorizationFlagInteractionAllowed |
    kAuthorizationFlagPreAuthorize |
    kAuthorizationFlagExtendRights;

    // Call AuthorizationCopyRights to determine or extend the allowable rights.
    status = AuthorizationCopyRights(authorizationRef, &rights, NULL, flags, NULL);
    if (status != errAuthorizationSuccess)
    NSLog(@"Copy Rights Unsuccessful: %d", status);

    NSLog(@"\n\n** %@ **\n\n", @"This command should work.");
    char *tool = "/usr/sbin/apachectl";
    char *args[] = {"start",NULL};
    FILE *pipe = NULL;

    status = AuthorizationExecuteWithPrivileges(authorizationRef, tool,
    kAuthorizationFlagDefaults, args, &pipe);
    if (status != errAuthorizationSuccess)
    NSLog(@"Error: %d", status);

    // The only way to guarantee that a credential acquired when you
    // request a right is not shared with other authorization instances is
    // to destroy the credential. To do so, call the AuthorizationFree
    // function with the flag kAuthorizationFlagDestroyRights.
    // http://developer.apple.com/documentation/Security/Conceptual/authorization_concepts/02authconcepts/chapter_2_section_7.html
    status = AuthorizationFree(authorizationRef, kAuthorizationFlagDestroyRights);

    if(pipe != NULL) {
    char c[100];
    int n=fread(c,1,100,pipe);

    c[n] = '\0';
    NSLog(@"%s\n",c);
    }[/C]