2 * Copyright (c) 2000-2004 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
33 AuthHostInstance::AuthHostInstance(Session
&session
, AuthHostType host
) :
36 secdebug("authhost", "authhost born (%p)", this);
38 session
.addReference(*this);
41 AuthHostInstance::~AuthHostInstance()
43 secdebug("authhost", "authhost died (%p)", this);
46 servicePort ().destroy ();
49 Session
&AuthHostInstance::session() const
51 return referent
<Session
>();
55 AuthHostInstance::childAction()
57 // Setup the environment for the SecurityAgent
62 // close down any files that might have been open at this point
63 int maxDescriptors
= getdtablesize ();
66 int devnull
= open(_PATH_DEVNULL
, O_RDWR
, 0);
67 if (devnull
>= 0) for (i
= 0; i
< 3; ++i
)
72 for (i
= 3; i
< maxDescriptors
; ++i
)
77 // construct path to SecurityAgent
78 char agentExecutable
[PATH_MAX
+ 1];
79 const char *path
= getenv("SECURITYAGENT");
81 path
= "/System/Library/CoreServices/SecurityAgent.app";
83 if ((mHostType
== userAuthHost
) || (mHostType
== privilegedAuthHost
))
85 snprintf(agentExecutable
, sizeof(agentExecutable
), "%s/Contents/Resources/authorizationhost", path
);
87 secdebug("AuthHostInstance", "execl(%s)", agentExecutable
);
88 execl(agentExecutable
, agentExecutable
, NULL
);
92 snprintf(agentExecutable
, sizeof(agentExecutable
), "%s/Contents/MacOS/SecurityAgent", path
);
94 struct group
*agentGroup
= getgrnam("securityagent");
95 gid_t agentGID
= static_cast<gid_t
>(-2);
98 agentGID
= agentGroup
->gr_gid
;
102 struct passwd
*agentUser
= getpwnam("securityagent");
103 uid_t agentUID
= static_cast<uid_t
>(-2);
106 agentUID
= agentUser
->pw_uid
;
113 CFRef
<CFDataRef
> userPrefs(session().copyUserPrefs());
115 FILE *mbox
= tmpfile();
117 if (userPrefs
&& mbox
)
119 if (fwrite(CFDataGetBytePtr(userPrefs
), CFDataGetLength(userPrefs
), 1, mbox
) != 1)
123 char mboxFdString
[20];
125 if ((int)sizeof(mboxFdString
) > snprintf(mboxFdString
, sizeof(mboxFdString
), "%d", fileno(mbox
)))
126 setenv("SECURITYAGENT_USERPREFS_FD", mboxFdString
, 1);
130 secdebug("AuthHostInstance", "execl(%s) as user (%d,%d)", agentExecutable
, agentUID
, agentGID
);
131 execl(agentExecutable
, agentExecutable
, NULL
);
134 secdebug("AuthHostInstance", "execl failed, errno=%d", errno
);
135 // Unconditional suicide follows.
136 // See comments below on why we can't use abort()
140 // NOTE: OS X abort() is implemented as kill(getuid()), which fails
141 // for a setuid-root process that has setuid'd. Go back to root to die...
148 AuthHostInstance::activate()
150 StLock
<Mutex
> _(*this);
151 if (state() != alive
)
153 if (!(session().attributes() & sessionHasGraphicAccess
))
154 CssmError::throwMe(CSSM_ERRCODE_NO_USER_INTERACTION
);
156 Security::MachPlusPlus::StBootstrap
bootSaver(session().bootstrapPort());
159 switch (ServerChild::state()) {
161 secdebug("AuthHostInstance", "%p (pid %d) has launched", this, pid());
164 secdebug("AuthHostInstance", "%p (pid %d) failed on startup", this, pid());
172 CssmError::throwMe(CSSM_ERRCODE_NO_USER_INTERACTION
);
174 return servicePort();