]>
git.saurik.com Git - apple/security.git/blob - SecurityTool/translocate.c
2 * Copyright (c) 2016 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@
27 #include <CoreFoundation/CoreFoundation.h>
29 #include <Security/SecTranslocate.h>
31 #include "security_tool.h"
32 #include "translocate.h"
34 static CFURLRef
CFURLfromPath(const char * path
, Boolean isDir
)
36 return CFURLCreateFromFileSystemRepresentation(NULL
, (UInt8
*)path
, strlen(path
), isDir
);
39 static char * PathFromCFURL(CFURLRef url
)
41 char* path
= malloc(PATH_MAX
);
48 if (!CFURLGetFileSystemRepresentation(url
, true, (UInt8
*)path
, PATH_MAX
))
58 static Boolean
PathIsDir(const char * path
)
60 Boolean result
= false;
67 DIR* d
= opendir(path
);
79 static void SafeCFRelease(CFTypeRef ref
)
87 /* return 2 = bad args, anything else is ignored */
89 int translocate_create(int argc
, char * const *argv
)
95 return SHOW_USAGE_MESSAGE
;
98 CFURLRef inUrl
= CFURLfromPath(argv
[1], PathIsDir(argv
[1]));
99 CFURLRef outUrl
= NULL
;
100 CFErrorRef error
= NULL
;
101 char* outPath
= NULL
;
105 printf("Error: failed to create url for: %s\n", argv
[1]);
109 outUrl
= SecTranslocateCreateSecureDirectoryForURL(inUrl
, NULL
, &error
);
113 int err
= (int)CFErrorGetCode(error
);
114 printf("Error: failed while trying to translocate %s (errno: %d, %s)\n", argv
[1], err
, strerror(err
));
118 outPath
= PathFromCFURL(outUrl
);
122 printf("Error: failed to convert out url to string for %s\n", argv
[1]);
126 printf("Translocation point: (note if this is what you passed in then that path should not be translocated)\n\t%s\n",outPath
);
132 SafeCFRelease(inUrl
);
133 SafeCFRelease(outUrl
);
134 SafeCFRelease(error
);
139 int translocate_policy(int argc
, char * const *argv
)
145 return SHOW_USAGE_MESSAGE
;
148 CFURLRef inUrl
= CFURLfromPath(argv
[1], PathIsDir(argv
[1]));
150 CFErrorRef error
= NULL
;
154 printf("Error: failed to create url for: %s\n", argv
[1]);
158 if (!SecTranslocateURLShouldRunTranslocated(inUrl
, &should
, &error
))
160 int err
= (int)CFErrorGetCode(error
);
161 printf("Error: failed while trying to check policy for %s (errno: %d, %s)\n", argv
[1], err
, strerror(err
));
165 printf("\t%s\n", should
? "Would translocate": "Would not translocate");
170 SafeCFRelease(inUrl
);
171 SafeCFRelease(error
);
176 int translocate_check(int argc
, char * const *argv
)
182 return SHOW_USAGE_MESSAGE
;
185 CFURLRef inUrl
= CFURLfromPath(argv
[1], PathIsDir(argv
[1]));
187 CFErrorRef error
= NULL
;
191 printf("Error: failed to create url for: %s\n", argv
[1]);
195 if (!SecTranslocateIsTranslocatedURL(inUrl
, &is
, &error
))
197 int err
= (int)CFErrorGetCode(error
);
198 printf("Error: failed while trying to check status for %s (errno: %d, %s)\n", argv
[1], err
, strerror(err
));
202 printf("\t%s\n", is
? "TRANSLOCATED": "NOT TRANSLOCATED");
207 SafeCFRelease(inUrl
);
208 SafeCFRelease(error
);
213 int translocate_original_path(int argc
, char * const * argv
)
219 return SHOW_USAGE_MESSAGE
;
222 CFURLRef inUrl
= CFURLfromPath(argv
[1], PathIsDir(argv
[1]));
223 CFURLRef outUrl
= NULL
;
224 CFErrorRef error
= NULL
;
225 char* outPath
= NULL
;
229 printf("Error: failed to create url for: %s\n", argv
[1]);
233 outUrl
= SecTranslocateCreateOriginalPathForURL(inUrl
, &error
);
237 int err
= (int)CFErrorGetCode(error
);
238 printf("Error: failed while trying to find original path for %s (errno: %d, %s)\n", argv
[1], err
, strerror(err
));
242 outPath
= PathFromCFURL(outUrl
);
246 printf("Error: failed to convert out url to string for %s\n", argv
[1]);
250 printf("Original Path: (note if this is what you passed in then that path is not translocated)\n\t%s\n",outPath
);
256 SafeCFRelease(inUrl
);
257 SafeCFRelease(outUrl
);
258 SafeCFRelease(error
);