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