1/*
2 * Copyright (c) 2012 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#include <stdlib.h>
25#include <string.h>
26#include <xpc/private.h>
27#include <sys/stat.h>
28#include <TargetConditionals.h>
29#include "configuration_profile.h"
30
31#define NOTIFY_PATH_SERVICE "com.apple.system.notify.service.path:0x87:"
32#define CPROF_PATH "/Library/Managed Preferences/mobile"
33
34char *
35configuration_profile_create_notification_key(const char *ident)
36{
37	char *out = NULL;
38
39	if (ident == NULL) return NULL;
40
41	if (ident[0] == '/')
42	{
43		asprintf(&out, "%s%s", NOTIFY_PATH_SERVICE, ident);
44		return out;
45	}
46
47#if TARGET_OS_EMBEDDED
48	if (strchr(ident + 1, '/') != NULL) return NULL;
49	asprintf(&out, "%s%s/%s.plist", NOTIFY_PATH_SERVICE, CPROF_PATH, ident);
50#endif
51
52	return out;
53}
54
55xpc_object_t
56configuration_profile_copy_property_list(const char *ident)
57{
58	char path[MAXPATHLEN];
59	void *data;
60	int fd;
61	struct stat sb;
62	xpc_object_t out = NULL;
63
64	if (ident == NULL) return NULL;
65
66	path[0] = '\0';
67	if (ident[0] == '/')
68	{
69		snprintf(path, sizeof(path), "%s", ident);
70	}
71#if TARGET_OS_EMBEDDED
72	else
73	{
74		if (strchr(ident + 1, '/') != NULL) return NULL;
75		snprintf(path, sizeof(path), "%s/%s.plist", CPROF_PATH, ident);
76	}
77#endif
78
79	if (path[0] == '\0') return NULL;
80
81	fd = open(path, O_RDONLY, 0);
82	if (fd < 0) return NULL;
83
84	memset(&sb, 0, sizeof(struct stat));
85	if (fstat(fd, &sb) < 0)
86	{
87		close(fd);
88		return NULL;
89	}
90
91	data = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
92	if (data != NULL) out = xpc_create_from_plist(data, sb.st_size);
93
94	munmap(data, sb.st_size);
95	close(fd);
96
97	return out;
98}
99