]> git.saurik.com Git - apple/libc.git/blob - gen/asl_core.c
8d8bb059161bc097d2a97df7bea62d5a052b95e7
[apple/libc.git] / gen / asl_core.c
1 /*
2 * Copyright (c) 2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 2007 Apple Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25 #include <asl_core.h>
26 #include <string.h>
27 #include <membership.h>
28 #include <pthread.h>
29
30 /*
31 * Message ID generation
32 */
33 static uint64_t _asl_core_msg_next_id = 1;
34 static pthread_mutex_t msg_id_lock = PTHREAD_MUTEX_INITIALIZER;
35
36 #define mix(a, b, c) \
37 { \
38 a -= b; a -= c; a ^= (c>>13); \
39 b -= c; b -= a; b ^= (a<< 8); \
40 c -= a; c -= b; c ^= (b>>13); \
41 a -= b; a -= c; a ^= (c>>12); \
42 b -= c; b -= a; b ^= (a<<16); \
43 c -= a; c -= b; c ^= (b>> 5); \
44 a -= b; a -= c; a ^= (c>> 3); \
45 b -= c; b -= a; b ^= (a<<10); \
46 c -= a; c -= b; c ^= (b>>15); \
47 }
48
49 /*
50 * Hash is used to improve string search.
51 */
52 uint32_t
53 asl_core_string_hash(const char *s, uint32_t inlen)
54 {
55 uint32_t a, b, c, l, len;
56
57 if (s == NULL) return 0;
58
59 l = inlen;
60
61 len = l;
62 a = b = 0x9e3779b9;
63 c = 0;
64
65 while (len >= 12)
66 {
67 a += (s[0] + ((uint32_t)s[1]<<8) + ((uint32_t)s[ 2]<<16) + ((uint32_t)s[ 3]<<24));
68 b += (s[4] + ((uint32_t)s[5]<<8) + ((uint32_t)s[ 6]<<16) + ((uint32_t)s[ 7]<<24));
69 c += (s[8] + ((uint32_t)s[9]<<8) + ((uint32_t)s[10]<<16) + ((uint32_t)s[11]<<24));
70
71 mix(a, b, c);
72
73 s += 12;
74 len -= 12;
75 }
76
77 c += l;
78 switch(len)
79 {
80 case 11: c += ((uint32_t)s[10]<<24);
81 case 10: c += ((uint32_t)s[9]<<16);
82 case 9 : c += ((uint32_t)s[8]<<8);
83
84 case 8 : b += ((uint32_t)s[7]<<24);
85 case 7 : b += ((uint32_t)s[6]<<16);
86 case 6 : b += ((uint32_t)s[5]<<8);
87 case 5 : b += s[4];
88
89 case 4 : a += ((uint32_t)s[3]<<24);
90 case 3 : a += ((uint32_t)s[2]<<16);
91 case 2 : a += ((uint32_t)s[1]<<8);
92 case 1 : a += s[0];
93 }
94
95 mix(a, b, c);
96
97 if (c == 0) c = 1;
98 return c;
99 }
100
101 const char *
102 asl_core_error(uint32_t code)
103 {
104 switch (code)
105 {
106 case ASL_STATUS_OK: return "Operation Succeeded";
107 case ASL_STATUS_INVALID_ARG: return "Invalid Argument";
108 case ASL_STATUS_INVALID_STORE: return "Invalid Data Store";
109 case ASL_STATUS_INVALID_STRING: return "Invalid String";
110 case ASL_STATUS_INVALID_ID: return "Invalid ID Number";
111 case ASL_STATUS_INVALID_MESSAGE: return "Invalid Message";
112 case ASL_STATUS_NOT_FOUND: return "Not Found";
113 case ASL_STATUS_READ_FAILED: return "Read Operation Failed";
114 case ASL_STATUS_WRITE_FAILED: return "Write Operation Failed";
115 case ASL_STATUS_NO_MEMORY: return "System Memory Allocation Failed";
116 case ASL_STATUS_ACCESS_DENIED: return "Access Denied";
117 case ASL_STATUS_READ_ONLY: return "Read Only Access";
118 case ASL_STATUS_WRITE_ONLY: return "Write Only Access";
119 case ASL_STATUS_MATCH_FAILED: return "Match Failed";
120 case ASL_STATUS_NO_RECORDS: return "No More Records";
121 }
122
123 return "Operation Failed";
124 }
125
126 static uint32_t
127 asl_core_check_user_access(int32_t msgu, int32_t readu)
128 {
129 /* -1 means anyone may read */
130 if (msgu == -1) return ASL_STATUS_OK;
131
132 /* Check for exact match */
133 if (msgu == readu) return ASL_STATUS_OK;
134
135 return ASL_STATUS_ACCESS_DENIED;
136 }
137
138 static uint32_t
139 asl_core_check_group_access(int32_t msgg, int32_t readu, int32_t readg)
140 {
141 int check;
142 uuid_t uu, gu;
143
144 /* -1 means anyone may read */
145 if (msgg == -1) return ASL_STATUS_OK;
146
147 /* Check for exact match */
148 if (msgg == readg) return ASL_STATUS_OK;
149
150 /* Check if user (u) is in read group (msgg) */
151 mbr_uid_to_uuid(readu, uu);
152 mbr_gid_to_uuid(msgg, gu);
153
154 check = 0;
155 mbr_check_membership(uu, gu, &check);
156 if (check != 0) return ASL_STATUS_OK;
157
158 return ASL_STATUS_ACCESS_DENIED;
159 }
160
161 uint32_t
162 asl_core_check_access(int32_t msgu, int32_t msgg, int32_t readu, int32_t readg, uint16_t flags)
163 {
164 uint16_t uset, gset;
165
166 /* root (uid 0) may always read */
167 if (readu == 0) return ASL_STATUS_OK;
168
169 uset = flags & ASL_MSG_FLAG_READ_UID_SET;
170 gset = flags & ASL_MSG_FLAG_READ_GID_SET;
171
172 /* if no access controls are set, anyone may read */
173 if ((uset | gset) == 0) return ASL_STATUS_OK;
174
175 /* if only uid is set, then access is only by uid match */
176 if ((uset != 0) && (gset == 0)) return asl_core_check_user_access(msgu, readu);
177
178 /* if only gid is set, then access is only by gid match */
179 if ((uset == 0) && (gset != 0)) return asl_core_check_group_access(msgg, readu, readg);
180
181 /* both uid and gid are set - check user, then group */
182 if ((asl_core_check_user_access(msgu, readu)) == ASL_STATUS_OK) return ASL_STATUS_OK;
183 return asl_core_check_group_access(msgg, readu, readg);
184 }
185
186 uint64_t
187 asl_core_htonq(uint64_t n)
188 {
189 #ifdef __BIG_ENDIAN__
190 return n;
191 #else
192 u_int32_t t;
193 union
194 {
195 u_int64_t q;
196 u_int32_t l[2];
197 } x;
198
199 x.q = n;
200 t = x.l[0];
201 x.l[0] = htonl(x.l[1]);
202 x.l[1] = htonl(t);
203
204 return x.q;
205 #endif
206 }
207
208 uint64_t
209 asl_core_ntohq(uint64_t n)
210 {
211 #ifdef __BIG_ENDIAN__
212 return n;
213 #else
214 u_int32_t t;
215 union
216 {
217 u_int64_t q;
218 u_int32_t l[2];
219 } x;
220
221 x.q = n;
222 t = x.l[0];
223 x.l[0] = ntohl(x.l[1]);
224 x.l[1] = ntohl(t);
225
226 return x.q;
227 #endif
228 }
229
230 uint64_t
231 asl_core_new_msg_id(uint64_t start)
232 {
233 uint64_t out;
234
235 pthread_mutex_lock(&msg_id_lock);
236
237 if (start != 0) _asl_core_msg_next_id = start;
238
239 out = _asl_core_msg_next_id;
240 _asl_core_msg_next_id++;
241
242 pthread_mutex_unlock(&msg_id_lock);
243
244 return out;
245 }