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