title: Obtaining admin privileges to delete files using rm from a Cocoa app
body: <p>I am making a small app that deletes log files. I am using an NSTask instance which runs rm and srm (secure rm) to delete files.</p>
  
  <p>I want to be able to delete files in:</p>
  
  <ul>
  <li>/Library/Logs</li>
  <li>~/Library/Logs</li>
  </ul>
  
  <p>The issue is that the user account does not have permissions to access some files in the system library folder, such as the Adobe logs subfolder and others. For example, only the "system" user (group?) has r/w permissions for the Adobe logs folder and its contents, and the current user doesn't even have an entry in the permissions shown in the Get Info window for the folder.</p>
  
  <p>What I want to be able to do exactly:</p>
  
  <ol>
  <li>Obtain admin privileges.</li>
  <li>Store the password in the Keychain so the app doesn't have to nag the user each time (Is the storage of the password a bad idea? Is it possible?)</li>
  <li>Delete a file whatever the file permissions may be.</li>
  </ol>
  
  <p>I am using NSTask because it offers notifications for task completion, getting text output from the task itself, etc. Would I need to use something else? If so, how could I replicate NSTask's completion notifications and output file handle while running rm and srm with admin privileges?</p>
  
  <p>I am looking for the most secure way to handle the situation. i.e. I don't want my application to become a doorway for privilege escalation attacks.</p>
  
  <p>I looked at the Authorization Services Programming Guide but I am not sure which case fits. At first I thought that <code>AuthorizationExecuteWithPrivileges</code> would be a good idea but after reading more on the subject it looks like this method is not recommended for security reasons.</p>
  
  <p>A detailed answer would be very welcome. I'm sure some of you already had to do something similar and have some code and knowledge to share.</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I am now able to make the authentication dialog pop up and obtain privileges, like so:</p>
  
  <pre><code>OSStatus status;
  AuthorizationRef authRef;
      status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &amp;authRef);
  
  AuthorizationRights authRights;
  AuthorizationItem authItems[1];
  
  authItems[0].name = kAuthorizationRightExecute;
  
  authRights.count = sizeof(authItems) / sizeof(authItems[0]);
  authRights.items = authItems;
  
  AuthorizationFlags authFlags = kAuthorizationFlagDefaults | kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed;
  
  status = AuthorizationCopyRights(authRef, &amp;authRights, kAuthorizationEmptyEnvironment, authFlags, NULL);
  </code></pre>
  
  <p>From the looks of it, it seems that the "Factored Application" method looks the most appropriate. The thing is that, to me, <em>rm already seems like an external helper tool</em>. I'm not sure I get the setuid alternative suggested in the documentation. Could I set the setuid bit on rm and run it using the NSTask method I already implemented? This would mean that I wouldn't need to create my own helper tool. Could somebody elaborate on this subject?</p>
  
  <p>I also looked at the BetterAuthorizationSample which is suggested as a more secure and recent alternative to the setuid bit method, but found it awfully complex for such as simple behavior. Any hints?</p>
  
  <p>Thanks in advance for any help!</p>