xref: /xnu-11215.41.3/libsyscall/wrappers/persona.c (revision 33de042d024d46de5ff4e89f2471de6608e37fa4) !
1 /*
2  * Copyright (c) 2015 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 #include <sys/cdefs.h>
24 #include <sys/types.h>
25 #include <stdarg.h>
26 #include <stdint.h>
27 #include <sys/persona.h>
28 
29 #include "strings.h"
30 
31 /* syscall entry point */
32 int __persona(uint32_t operation, uint32_t flags, struct kpersona_info *info, uid_t *id, size_t *idlen, char *path);
33 
34 int
kpersona_alloc(struct kpersona_info * info,uid_t * id)35 kpersona_alloc(struct kpersona_info *info, uid_t *id)
36 {
37 	size_t idlen = 1;
38 	return __persona(PERSONA_OP_ALLOC, 0, info, id, &idlen, NULL);
39 }
40 
41 int
kpersona_palloc(struct kpersona_info * info,uid_t * id,char path[MAXPATHLEN])42 kpersona_palloc(struct kpersona_info *info, uid_t *id, char path[MAXPATHLEN])
43 {
44 	size_t idlen = 1;
45 	return __persona(PERSONA_OP_PALLOC, 0, info, id, &idlen, path);
46 }
47 
48 int
kpersona_dealloc(uid_t id)49 kpersona_dealloc(uid_t id)
50 {
51 	size_t idlen = 1;
52 	return __persona(PERSONA_OP_DEALLOC, 0, NULL, &id, &idlen, NULL);
53 }
54 
55 int
kpersona_get(uid_t * id)56 kpersona_get(uid_t *id)
57 {
58 	size_t idlen = 1;
59 	return __persona(PERSONA_OP_GET, 0, NULL, id, &idlen, NULL);
60 }
61 
62 int
kpersona_getpath(uid_t id,char path[MAXPATHLEN])63 kpersona_getpath(uid_t id, char path[MAXPATHLEN])
64 {
65 	size_t idlen = 1;
66 	return __persona(PERSONA_OP_GETPATH, 0, NULL, &id, &idlen, path);
67 }
68 
69 int
kpersona_info(uid_t id,struct kpersona_info * info)70 kpersona_info(uid_t id, struct kpersona_info *info)
71 {
72 	size_t idlen = 1;
73 	return __persona(PERSONA_OP_INFO, 0, info, &id, &idlen, NULL);
74 }
75 
76 int
kpersona_pidinfo(pid_t pid,struct kpersona_info * info)77 kpersona_pidinfo(pid_t pid, struct kpersona_info *info)
78 {
79 	size_t idlen = 1;
80 	uid_t id = (uid_t)pid;
81 	return __persona(PERSONA_OP_PIDINFO, 0, info, &id, &idlen, NULL);
82 }
83 
84 int
kpersona_find(const char * name,uid_t uid,uid_t * id,size_t * idlen)85 kpersona_find(const char *name, uid_t uid, uid_t *id, size_t *idlen)
86 {
87 	int ret;
88 	struct kpersona_info kinfo;
89 	kinfo.persona_info_version = PERSONA_INFO_V1;
90 	kinfo.persona_id = uid;
91 	kinfo.persona_type = 0;
92 	kinfo.persona_gid = 0;
93 	kinfo.persona_ngroups = 0;
94 	kinfo.persona_groups[0] = 0;
95 	kinfo.persona_name[0] = 0;
96 	if (name) {
97 		strlcpy(kinfo.persona_name, name, sizeof(kinfo.persona_name));
98 	}
99 	ret = __persona(PERSONA_OP_FIND, 0, &kinfo, id, idlen, NULL);
100 	if (ret < 0) {
101 		return ret;
102 	}
103 	return (int)(*idlen);
104 }
105 
106 int
kpersona_find_by_type(int persona_type,uid_t * id,size_t * idlen)107 kpersona_find_by_type(int persona_type, uid_t *id, size_t *idlen)
108 {
109 	int ret;
110 	struct kpersona_info kinfo;
111 	kinfo.persona_info_version = PERSONA_INFO_V1;
112 	kinfo.persona_type = persona_type;
113 	kinfo.persona_id = -1;
114 	kinfo.persona_gid = 0;
115 	kinfo.persona_ngroups = 0;
116 	kinfo.persona_groups[0] = 0;
117 	kinfo.persona_name[0] = 0;
118 	ret = __persona(PERSONA_OP_FIND_BY_TYPE, 0, &kinfo, id, idlen, NULL);
119 	if (ret < 0) {
120 		return ret;
121 	}
122 	return (int)(*idlen);
123 }
124