1/*
2 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25#include <TargetConditionals.h>
26
27#if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))
28
29#include <CoreFoundation/CoreFoundation.h>
30#include <AssertMacros.h>
31#include <utilities/SecCFWrappers.h>
32
33#include <sys/types.h>
34#include <pwd.h>
35#include <uuid/uuid.h>
36#include "iOSforOSX.h"
37#include <pwd.h>
38#include <unistd.h>
39
40#include ".././libsecurity_keychain/lib/SecBase64P.c"
41
42CFURLRef SecCopyKeychainDirectoryFile(CFStringRef file)
43{
44    struct passwd *passwd = getpwuid(getuid());
45    if (!passwd)
46        return NULL;
47
48    CFURLRef pathURL = NULL;
49    CFURLRef fileURL = NULL;
50    CFStringRef home = NULL;
51    CFStringRef filePath = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%s/%@"), "Library/Keychains", file);
52    require(filePath, xit);
53
54    if (passwd->pw_dir)
55        home = CFStringCreateWithCString(NULL, passwd->pw_dir, kCFStringEncodingUTF8);
56
57    pathURL = CFURLCreateWithFileSystemPath(NULL, home?home:CFSTR("/"), kCFURLPOSIXPathStyle, true);
58    if (pathURL)
59        fileURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault, pathURL, filePath, false);
60
61xit:
62    CFReleaseSafe(filePath);
63    CFReleaseSafe(pathURL);
64    CFReleaseSafe(home);
65    return fileURL;
66}
67
68// XXX: do we still need this?  see securityd_files?
69CFURLRef PortableCFCopyHomeDirectoryURL(void)
70{
71    char *path = getenv("HOME");
72    if (!path) {
73        struct passwd *pw = getpwuid(getuid());
74        path = pw->pw_dir;
75    }
76    CFStringRef path_cf = CFStringCreateWithCStringNoCopy(NULL, path, kCFStringEncodingUTF8, kCFAllocatorNull);
77    CFURLRef path_url = CFURLCreateWithFileSystemPath(NULL, path_cf, kCFURLPOSIXPathStyle, true);
78
79    CFRelease(path_cf);
80    return path_url;
81}
82
83#endif
84