]>
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 "translocate.h"
33 static CFURLRef
CFURLfromPath(const char * path
, Boolean isDir
)
35 return CFURLCreateFromFileSystemRepresentation(NULL
, (UInt8
*)path
, strlen(path
), isDir
);
38 static char * PathFromCFURL(CFURLRef url
)
40 char* path
= malloc(PATH_MAX
);
47 if (!CFURLGetFileSystemRepresentation(url
, true, (UInt8
*)path
, PATH_MAX
))
57 static Boolean
PathIsDir(const char * path
)
59 Boolean result
= false;
66 DIR* d
= opendir(path
);
78 static void SafeCFRelease(CFTypeRef ref
)
86 /* return 2 = bad args, anything else is ignored */
88 int translocate_create(int argc
, char * const *argv
)
97 CFURLRef inUrl
= CFURLfromPath(argv
[1], PathIsDir(argv
[1]));
98 CFURLRef outUrl
= NULL
;
99 CFErrorRef error
= NULL
;
100 char* outPath
= NULL
;
104 printf("Error: failed to create url for: %s\n", argv
[1]);
108 outUrl
= SecTranslocateCreateSecureDirectoryForURL(inUrl
, NULL
, &error
);
112 int err
= (int)CFErrorGetCode(error
);
113 printf("Error: failed while trying to translocate %s (errno: %d, %s)\n", argv
[1], err
, strerror(err
));
117 outPath
= PathFromCFURL(outUrl
);
121 printf("Error: failed to convert out url to string for %s\n", argv
[1]);
125 printf("Translocation point: (note if this is what you passed in then that path should not be translocated)\n\t%s\n",outPath
);
131 SafeCFRelease(inUrl
);
132 SafeCFRelease(outUrl
);
133 SafeCFRelease(error
);
138 int translocate_policy(int argc
, char * const *argv
)
147 CFURLRef inUrl
= CFURLfromPath(argv
[1], PathIsDir(argv
[1]));
149 CFErrorRef error
= NULL
;
153 printf("Error: failed to create url for: %s\n", argv
[1]);
157 if (!SecTranslocateURLShouldRunTranslocated(inUrl
, &should
, &error
))
159 int err
= (int)CFErrorGetCode(error
);
160 printf("Error: failed while trying to check policy for %s (errno: %d, %s)\n", argv
[1], err
, strerror(err
));
164 printf("\t%s\n", should
? "Would translocate": "Would not translocate");
169 SafeCFRelease(inUrl
);
170 SafeCFRelease(error
);
175 int translocate_check(int argc
, char * const *argv
)
184 CFURLRef inUrl
= CFURLfromPath(argv
[1], PathIsDir(argv
[1]));
186 CFErrorRef error
= NULL
;
190 printf("Error: failed to create url for: %s\n", argv
[1]);
194 if (!SecTranslocateIsTranslocatedURL(inUrl
, &is
, &error
))
196 int err
= (int)CFErrorGetCode(error
);
197 printf("Error: failed while trying to check status for %s (errno: %d, %s)\n", argv
[1], err
, strerror(err
));
201 printf("\t%s\n", is
? "TRANSLOCATED": "NOT TRANSLOCATED");
206 SafeCFRelease(inUrl
);
207 SafeCFRelease(error
);
212 int translocate_original_path(int argc
, char * const * argv
)
221 CFURLRef inUrl
= CFURLfromPath(argv
[1], PathIsDir(argv
[1]));
222 CFURLRef outUrl
= NULL
;
223 CFErrorRef error
= NULL
;
224 char* outPath
= NULL
;
228 printf("Error: failed to create url for: %s\n", argv
[1]);
232 outUrl
= SecTranslocateCreateOriginalPathForURL(inUrl
, &error
);
236 int err
= (int)CFErrorGetCode(error
);
237 printf("Error: failed while trying to find original path for %s (errno: %d, %s)\n", argv
[1], err
, strerror(err
));
241 outPath
= PathFromCFURL(outUrl
);
245 printf("Error: failed to convert out url to string for %s\n", argv
[1]);
249 printf("Original Path: (note if this is what you passed in then that path is not translocated)\n\t%s\n",outPath
);
255 SafeCFRelease(inUrl
);
256 SafeCFRelease(outUrl
);
257 SafeCFRelease(error
);