2 * Copyright (c) 2007 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
27 #include <membership.h>
31 * Message ID generation
33 static uint64_t _asl_core_msg_next_id
= 1;
34 static pthread_mutex_t msg_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
36 #define mix(a, b, c) \
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); \
50 * Hash is used to improve string search.
53 asl_core_string_hash(const char *s
, uint32_t inlen
)
55 uint32_t a
, b
, c
, l
, len
;
57 if (s
== NULL
) return 0;
62 if (s
[0] == '\0') return 0;
72 a
+= (s
[0] + ((uint32_t)s
[1]<<8) + ((uint32_t)s
[ 2]<<16) + ((uint32_t)s
[ 3]<<24));
73 b
+= (s
[4] + ((uint32_t)s
[5]<<8) + ((uint32_t)s
[ 6]<<16) + ((uint32_t)s
[ 7]<<24));
74 c
+= (s
[8] + ((uint32_t)s
[9]<<8) + ((uint32_t)s
[10]<<16) + ((uint32_t)s
[11]<<24));
85 case 11: c
+= ((uint32_t)s
[10]<<24);
86 case 10: c
+= ((uint32_t)s
[9]<<16);
87 case 9 : c
+= ((uint32_t)s
[8]<<8);
89 case 8 : b
+= ((uint32_t)s
[7]<<24);
90 case 7 : b
+= ((uint32_t)s
[6]<<16);
91 case 6 : b
+= ((uint32_t)s
[5]<<8);
94 case 4 : a
+= ((uint32_t)s
[3]<<24);
95 case 3 : a
+= ((uint32_t)s
[2]<<16);
96 case 2 : a
+= ((uint32_t)s
[1]<<8);
107 asl_core_error(uint32_t code
)
111 case ASL_STATUS_OK
: return "Operation Succeeded";
112 case ASL_STATUS_INVALID_ARG
: return "Invalid Argument";
113 case ASL_STATUS_INVALID_STORE
: return "Invalid Data Store";
114 case ASL_STATUS_INVALID_STRING
: return "Invalid String";
115 case ASL_STATUS_INVALID_ID
: return "Invalid ID Number";
116 case ASL_STATUS_INVALID_MESSAGE
: return "Invalid Message";
117 case ASL_STATUS_NOT_FOUND
: return "Not Found";
118 case ASL_STATUS_READ_FAILED
: return "Read Operation Failed";
119 case ASL_STATUS_WRITE_FAILED
: return "Write Operation Failed";
120 case ASL_STATUS_NO_MEMORY
: return "System Memory Allocation Failed";
121 case ASL_STATUS_ACCESS_DENIED
: return "Access Denied";
122 case ASL_STATUS_READ_ONLY
: return "Read Only Access";
123 case ASL_STATUS_WRITE_ONLY
: return "Write Only Access";
124 case ASL_STATUS_MATCH_FAILED
: return "Match Failed";
125 case ASL_STATUS_NO_RECORDS
: return "No More Records";
128 return "Operation Failed";
132 asl_core_check_user_access(int32_t msgu
, int32_t readu
)
134 /* -1 means anyone may read */
135 if (msgu
== -1) return ASL_STATUS_OK
;
137 /* Check for exact match */
138 if (msgu
== readu
) return ASL_STATUS_OK
;
140 return ASL_STATUS_ACCESS_DENIED
;
144 asl_core_check_group_access(int32_t msgg
, int32_t readu
, int32_t readg
)
149 /* -1 means anyone may read */
150 if (msgg
== -1) return ASL_STATUS_OK
;
152 /* Check for exact match */
153 if (msgg
== readg
) return ASL_STATUS_OK
;
155 /* Check if user (u) is in read group (msgg) */
156 mbr_uid_to_uuid(readu
, uu
);
157 mbr_gid_to_uuid(msgg
, gu
);
160 mbr_check_membership(uu
, gu
, &check
);
161 if (check
!= 0) return ASL_STATUS_OK
;
163 return ASL_STATUS_ACCESS_DENIED
;
167 asl_core_check_access(int32_t msgu
, int32_t msgg
, int32_t readu
, int32_t readg
, uint16_t flags
)
171 /* root (uid 0) may always read */
172 if (readu
== 0) return ASL_STATUS_OK
;
174 uset
= flags
& ASL_MSG_FLAG_READ_UID_SET
;
175 gset
= flags
& ASL_MSG_FLAG_READ_GID_SET
;
177 /* if no access controls are set, anyone may read */
178 if ((uset
| gset
) == 0) return ASL_STATUS_OK
;
180 /* if only uid is set, then access is only by uid match */
181 if ((uset
!= 0) && (gset
== 0)) return asl_core_check_user_access(msgu
, readu
);
183 /* if only gid is set, then access is only by gid match */
184 if ((uset
== 0) && (gset
!= 0)) return asl_core_check_group_access(msgg
, readu
, readg
);
186 /* both uid and gid are set - check user, then group */
187 if ((asl_core_check_user_access(msgu
, readu
)) == ASL_STATUS_OK
) return ASL_STATUS_OK
;
188 return asl_core_check_group_access(msgg
, readu
, readg
);
192 asl_core_htonq(uint64_t n
)
194 #ifdef __BIG_ENDIAN__
206 x
.l
[0] = htonl(x
.l
[1]);
214 asl_core_ntohq(uint64_t n
)
216 #ifdef __BIG_ENDIAN__
228 x
.l
[0] = ntohl(x
.l
[1]);
236 asl_core_new_msg_id(uint64_t start
)
240 pthread_mutex_lock(&msg_id_lock
);
242 if (start
!= 0) _asl_core_msg_next_id
= start
;
244 out
= _asl_core_msg_next_id
;
245 _asl_core_msg_next_id
++;
247 pthread_mutex_unlock(&msg_id_lock
);