]>
git.saurik.com Git - apple/libsecurity_codesigning.git/blob - lib/bundlediskrep.cpp
ffaa4789a94fca1b556a8eee1ae6ef8726a914a7
2 * Copyright (c) 2006-2007 Apple 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@
23 #include "bundlediskrep.h"
24 #include <CoreFoundation/CFURLAccess.h>
25 #include <CoreFoundation/CFBundlePriv.h>
26 #include <security_codesigning/cfmunge.h>
31 namespace CodeSigning
{
33 using namespace UnixPlusPlus
;
37 // We make a CFBundleRef immediately, but everything else is lazy
39 BundleDiskRep::BundleDiskRep(const char *path
)
40 : mBundle(_CFBundleCreateIfMightBeBundle(NULL
, CFTempURL(path
)))
43 MacOSError::throwMe(errSecCSBadObjectFormat
);
44 mExecRep
= DiskRep::bestFileGuess(this->mainExecutablePath());
47 BundleDiskRep::BundleDiskRep(CFBundleRef ref
)
49 mBundle
= ref
; // retains
50 mExecRep
= DiskRep::bestFileGuess(this->mainExecutablePath());
55 // Create a path to a bundle signing resource, by name.
56 // If the BUNDLEDISKREP_DIRECTORY directory exists in the bundle's support directory, files
57 // will be read and written there. Otherwise, they go directly into the support directory.
59 string
BundleDiskRep::metaPath(const char *name
)
61 if (mMetaPath
.empty()) {
62 string support
= cfString(CFBundleCopySupportFilesDirectoryURL(mBundle
), true);
63 mMetaPath
= support
+ "/" BUNDLEDISKREP_DIRECTORY
;
64 if (::access(mMetaPath
.c_str(), F_OK
) == 0) {
71 return mMetaPath
+ "/" + name
;
76 // Try to create the meta-file directory in our bundle.
77 // Does nothing if the directory already exists.
78 // Throws if an error occurs.
80 void BundleDiskRep::createMeta()
82 string meta
= metaPath(BUNDLEDISKREP_DIRECTORY
);
84 if (::mkdir(meta
.c_str(), 0755) == 0) {
85 copyfile(cfString(canonicalPath(), true).c_str(), meta
.c_str(), NULL
, COPYFILE_SECURITY
);
88 } else if (errno
!= EEXIST
)
95 // Load and return a component, by slot number.
96 // Info.plist components come from the bundle, always (we don't look
97 // for Mach-O embedded versions).
98 // Everything else comes from the embedded blobs of a Mach-O image, or from
99 // files located in the Contents directory of the bundle.
101 CFDataRef
BundleDiskRep::component(CodeDirectory::SpecialSlot slot
)
104 // the Info.plist comes from the magic CFBundle-indicated place and ONLY from there
106 if (CFRef
<CFURLRef
> info
= _CFBundleCopyInfoPlistURL(mBundle
))
107 return cfLoadFile(info
);
110 // by default, we take components from the executable image or files
112 if (CFDataRef data
= mExecRep
->component(slot
))
115 // but the following always come from files
116 case cdResourceDirSlot
:
117 if (const char *name
= CodeDirectory::canonicalSlotName(slot
))
118 return metaData(name
);
126 // Various aspects of our DiskRep personality.
128 CFURLRef
BundleDiskRep::canonicalPath()
130 return CFBundleCopyBundleURL(mBundle
);
133 string
BundleDiskRep::recommendedIdentifier()
135 if (CFStringRef identifier
= CFBundleGetIdentifier(mBundle
))
136 return cfString(identifier
);
137 if (CFDictionaryRef infoDict
= CFBundleGetInfoDictionary(mBundle
))
138 if (CFStringRef identifier
= CFStringRef(CFDictionaryGetValue(infoDict
, kCFBundleNameKey
)))
139 return cfString(identifier
);
141 // fall back to using the $(basename) of the canonical path. Drop any .app suffix
142 string path
= cfString(this->canonicalPath(), true);
143 if (path
.substr(path
.size() - 4) == ".app")
144 path
= path
.substr(0, path
.size() - 4);
145 string::size_type p
= path
.rfind('/');
146 if (p
== string::npos
)
149 return path
.substr(p
+1);
152 string
BundleDiskRep::mainExecutablePath()
154 if (CFURLRef exec
= CFBundleCopyExecutableURL(mBundle
))
155 return cfString(exec
, true);
157 MacOSError::throwMe(errSecCSBadObjectFormat
);
160 string
BundleDiskRep::resourcesRootPath()
162 return cfString(CFBundleCopySupportFilesDirectoryURL(mBundle
), true);
165 CFDictionaryRef
BundleDiskRep::defaultResourceRules()
167 return cfmake
<CFDictionaryRef
>("{rules={"
168 "'^version.plist$' = #T"
170 "'^Resources/.*\\.lproj/' = {optional=#T, weight=1000}"
171 "'^Resources/.*\\.lproj/locversion.plist$' = {omit=#T, weight=1100}"
175 void BundleDiskRep::adjustResources(ResourceBuilder
&builder
)
177 // exclude entire contents of meta directory
178 builder
.addExclusion("^" BUNDLEDISKREP_DIRECTORY
"/");
180 // exclude the main executable file
181 string resources
= resourcesRootPath();
182 string executable
= mainExecutablePath();
183 if (!executable
.compare(0, resources
.length(), resources
, 0, resources
.length())) // is prefix
184 builder
.addExclusion(string("^") + executable
.substr(resources
.length() + 1) + "$");
188 const Requirements
*BundleDiskRep::defaultRequirements(const Architecture
*arch
)
190 return mExecRep
->defaultRequirements(arch
);
194 Universal
*BundleDiskRep::mainExecutableImage()
196 return mExecRep
->mainExecutableImage();
199 size_t BundleDiskRep::pageSize()
201 return mExecRep
->pageSize();
204 size_t BundleDiskRep::signingBase()
206 return mExecRep
->signingBase();
209 size_t BundleDiskRep::signingLimit()
211 return mExecRep
->signingLimit();
214 string
BundleDiskRep::format()
216 return string("bundle with ") + mExecRep
->format();
219 CFArrayRef
BundleDiskRep::modifiedFiles()
221 CFMutableArrayRef files
= CFArrayCreateMutableCopy(NULL
, 0, mExecRep
->modifiedFiles());
222 checkModifiedFile(files
, cdCodeDirectorySlot
);
223 checkModifiedFile(files
, cdSignatureSlot
);
224 checkModifiedFile(files
, cdResourceDirSlot
);
225 checkModifiedFile(files
, cdEntitlementSlot
);
229 void BundleDiskRep::checkModifiedFile(CFMutableArrayRef files
, CodeDirectory::SpecialSlot slot
)
231 if (CFDataRef data
= mExecRep
->component(slot
)) // provided by executable file
233 else if (const char *resourceName
= CodeDirectory::canonicalSlotName(slot
)) {
234 string file
= metaPath(resourceName
);
235 if (::access(file
.c_str(), F_OK
) == 0)
236 CFArrayAppendValue(files
, CFTempURL(file
));
240 FileDesc
&BundleDiskRep::fd()
242 return mExecRep
->fd();
245 void BundleDiskRep::flush()
254 DiskRep::Writer
*BundleDiskRep::writer()
256 return new Writer(this);
259 BundleDiskRep::Writer::Writer(BundleDiskRep
*r
)
260 : rep(r
), mMadeMetaDirectory(false)
262 execWriter
= rep
->mExecRep
->writer();
267 // Write a component.
268 // Note that this isn't concerned with Mach-O writing; this is handled at
269 // a much higher level. If we're called, we write to a file in the Bundle's meta directory.
271 void BundleDiskRep::Writer::component(CodeDirectory::SpecialSlot slot
, CFDataRef data
)
275 if (!execWriter
->attribute(writerLastResort
)) // willing to take the data...
276 return execWriter
->component(slot
, data
); // ... so hand it through
277 // execWriter doesn't want the data; store it as a resource file (below)
278 case cdResourceDirSlot
:
279 // the resource directory always goes into a bundle file
280 if (const char *name
= CodeDirectory::canonicalSlotName(slot
)) {
282 string path
= rep
->metaPath(name
);
283 AutoFileDesc
fd(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
284 fd
.writeAll(CFDataGetBytePtr(data
), CFDataGetLength(data
));
285 if (rep
->mMetaExists
) {
286 // leave a symlink in the support directory for pre-10.5.3 compatibility (but ignore errors)
287 string legacy
= cfString(CFBundleCopySupportFilesDirectoryURL(rep
->mBundle
), true) + "/" + name
;
288 // ::unlink(legacy.c_str()); // force-replace
289 ::symlink((string(BUNDLEDISKREP_DIRECTORY
"/") + name
).c_str(), legacy
.c_str());
292 MacOSError::throwMe(errSecCSBadObjectFormat
);
297 void BundleDiskRep::Writer::flush()
303 } // end namespace CodeSigning
304 } // end namespace Security