authorization_util.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_MAC_AUTHORIZATION_UTIL_H_
  5. #define BASE_MAC_AUTHORIZATION_UTIL_H_
  6. // AuthorizationExecuteWithPrivileges fork()s and exec()s the tool, but it
  7. // does not wait() for it. It also doesn't provide the caller with access to
  8. // the forked pid. If used irresponsibly, zombie processes will accumulate.
  9. //
  10. // Apple's really gotten us between a rock and a hard place, here.
  11. //
  12. // Fortunately, AuthorizationExecuteWithPrivileges does give access to the
  13. // tool's stdout (and stdin) via a FILE* pipe. The tool can output its pid
  14. // to this pipe, and the main program can read it, and then have something
  15. // that it can wait() for.
  16. //
  17. // The contract is that any tool executed by the wrappers declared in this
  18. // file must print its pid to stdout on a line by itself before doing anything
  19. // else.
  20. //
  21. // http://developer.apple.com/library/mac/#samplecode/BetterAuthorizationSample/Listings/BetterAuthorizationSampleLib_c.html
  22. // (Look for "What's This About Zombies?")
  23. #include <CoreFoundation/CoreFoundation.h>
  24. #include <Security/Authorization.h>
  25. #include <stdio.h>
  26. #include <sys/types.h>
  27. #include "base/base_export.h"
  28. namespace base {
  29. namespace mac {
  30. // Obtains an AuthorizationRef for the rights indicated by |rights|. If
  31. // necessary, prompts the user for authentication. If the user is prompted,
  32. // |prompt| will be used as the prompt string and an icon appropriate for the
  33. // application will be displayed in a prompt dialog. Note that the system
  34. // appends its own text to the prompt string. |extraFlags| will be ORed
  35. // together with the default flags. Returns NULL on failure.
  36. BASE_EXPORT
  37. AuthorizationRef GetAuthorizationRightsWithPrompt(
  38. AuthorizationRights* rights,
  39. CFStringRef prompt,
  40. AuthorizationFlags extraFlags);
  41. // Obtains an AuthorizationRef (using |GetAuthorizationRightsWithPrompt|) that
  42. // can be used to run commands as root.
  43. BASE_EXPORT
  44. AuthorizationRef AuthorizationCreateToRunAsRoot(CFStringRef prompt);
  45. // Calls straight through to AuthorizationExecuteWithPrivileges. If that
  46. // call succeeds, |pid| will be set to the pid of the executed tool. If the
  47. // pid can't be determined, |pid| will be set to -1. |pid| must not be NULL.
  48. // |pipe| may be NULL, but the tool will always be executed with a pipe in
  49. // order to read the pid from its stdout.
  50. BASE_EXPORT
  51. OSStatus ExecuteWithPrivilegesAndGetPID(AuthorizationRef authorization,
  52. const char* tool_path,
  53. AuthorizationFlags options,
  54. const char** arguments,
  55. FILE** pipe,
  56. pid_t* pid);
  57. // Calls ExecuteWithPrivilegesAndGetPID, and if that call succeeds, calls
  58. // waitpid() to wait for the process to exit. If waitpid() succeeds, the
  59. // exit status is placed in |exit_status|, otherwise, -1 is stored.
  60. // |exit_status| may be NULL and this function will still wait for the process
  61. // to exit.
  62. BASE_EXPORT
  63. OSStatus ExecuteWithPrivilegesAndWait(AuthorizationRef authorization,
  64. const char* tool_path,
  65. AuthorizationFlags options,
  66. const char** arguments,
  67. FILE** pipe,
  68. int* exit_status);
  69. } // namespace mac
  70. } // namespace base
  71. #endif // BASE_MAC_AUTHORIZATION_UTIL_H_