1//
2//  iOSforOSX.c
3//  utilities
4//
5//  Created by J Osborne on 11/13/12.
6//  Copyright (c) 2012 Apple Inc. All rights reserved.
7//
8
9#include <TargetConditionals.h>
10
11#if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))
12
13#include <CoreFoundation/CoreFoundation.h>
14#include <AssertMacros.h>
15#include <utilities/SecCFWrappers.h>
16
17#include <sys/types.h>
18#include <pwd.h>
19#include <uuid/uuid.h>
20#include "iOSforOSX.h"
21#include <pwd.h>
22#include <unistd.h>
23
24#include ".././libsecurity_keychain/lib/SecBase64P.c"
25
26CFURLRef SecCopyKeychainDirectoryFile(CFStringRef file)
27{
28    struct passwd *passwd = getpwuid(getuid());
29    if (!passwd)
30        return NULL;
31
32    CFURLRef pathURL = NULL;
33    CFURLRef fileURL = NULL;
34    CFStringRef home = NULL;
35    CFStringRef filePath = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%s/%@"), "Library/Keychains", file);
36    require(filePath, xit);
37
38    if (passwd->pw_dir)
39        home = CFStringCreateWithCString(NULL, passwd->pw_dir, kCFStringEncodingUTF8);
40
41    pathURL = CFURLCreateWithFileSystemPath(NULL, home?home:CFSTR("/"), kCFURLPOSIXPathStyle, true);
42    if (pathURL)
43        fileURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault, pathURL, filePath, false);
44
45xit:
46    CFReleaseSafe(filePath);
47    CFReleaseSafe(pathURL);
48    CFReleaseSafe(home);
49    return fileURL;
50}
51
52// XXX: do we still need this?  see securityd_files?
53CFURLRef PortableCFCopyHomeDirectoryURL(void)
54{
55    char *path = getenv("HOME");
56    if (!path) {
57        struct passwd *pw = getpwuid(getuid());
58        path = pw->pw_dir;
59    }
60    CFStringRef path_cf = CFStringCreateWithCStringNoCopy(NULL, path, kCFStringEncodingUTF8, kCFAllocatorNull);
61    CFURLRef path_url = CFURLCreateWithFileSystemPath(NULL, path_cf, kCFURLPOSIXPathStyle, true);
62
63    CFRelease(path_cf);
64    return path_url;
65}
66
67#endif
68