2 * Copyright (c) 2006-2013 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 OR NON-INFRINGEMENT. Please see the
18 * License for the specific language governing rights and limitations
21 * @APPLE_LICENSE_HEADER_END@
25 #include <sys/types.h>
26 #include <sys/socket.h>
34 #include <dispatch/dispatch.h>
35 #include <os/assumes.h>
39 #include <asl_private.h>
41 static uint8_t *b64charset
= (uint8_t *)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
44 asl_is_utf8_char(const unsigned char *p
, int *state
, int *ctype
)
55 if ((*p
>= 0xc2) && (*p
<= 0xdf)) *ctype
= 1;
56 else if (*p
== 0xe0) *ctype
= 2;
57 else if ((*p
>= 0xe1) && (*p
<= 0xef)) *ctype
= 3;
58 else if (*p
== 0xf0) *ctype
= 4;
59 else if ((*p
>= 0xf1) && (*p
<= 0xf3)) *ctype
= 5;
60 else if (*p
== 0xf4) *ctype
= 6;
73 if ((*p
>= 0x80) && (*p
<= 0xbf)) *state
= 0;
80 if ((*p
>= 0xa0) && (*p
<= 0xbf)) *state
= 2;
87 if ((*p
>= 0x80) && (*p
<= 0xbf)) *state
= 2;
94 if ((*p
>= 0x90) && (*p
<= 0xbf)) *state
= 2;
101 if ((*p
>= 0x80) && (*p
<= 0xbf)) *state
= 2;
108 if ((*p
>= 0x80) && (*p
<= 0x8f)) *state
= 2;
121 if ((*ctype
>= 2) && (*ctype
<= 3))
123 if ((*p
>= 0x80) && (*p
<= 0xbf)) *state
= 0;
126 else if ((*ctype
>= 4) && (*ctype
<= 6))
128 if ((*p
>= 0x80) && (*p
<= 0xbf)) *state
= 3;
141 if ((*ctype
>= 4) && (*ctype
<= 6))
143 if ((*p
>= 0x80) && (*p
<= 0xbf)) *state
= 0;
160 __private_extern__
int
161 asl_is_utf8(const char *str
)
163 const unsigned char *p
;
168 if (str
== NULL
) return flag
;
170 for (p
= (const unsigned char *)str
; (*p
!= '\0') && (flag
== 1); p
++)
172 flag
= asl_is_utf8_char(p
, &state
, &ctype
);
178 __private_extern__
uint8_t *
179 asl_b64_encode(const uint8_t *buf
, size_t len
)
183 size_t i0
, i1
, i2
, j
, outlen
;
185 if (buf
== NULL
) return NULL
;
186 if (len
== 0) return NULL
;
188 outlen
= ((len
+ 2) / 3) * 4;
189 out
= (uint8_t *)malloc(outlen
+ 1);
206 out
[j
++] = b64charset
[b
];
208 b
= ((buf
[i0
] & 0x03) << 4) | (buf
[i1
] >> 4);
209 out
[j
++] = b64charset
[b
];
211 b
= ((buf
[i1
] & 0x0f) << 2) | ((buf
[i2
] & 0xc0) >> 6);
212 out
[j
++] = b64charset
[b
];
215 out
[j
++] = b64charset
[b
];
225 out
[j
++] = b64charset
[b
];
227 b
= (buf
[i0
] & 0x03) << 4;
229 if (i1
< len
) b
|= (buf
[i1
] >> 4);
230 out
[j
++] = b64charset
[b
];
239 b
= (buf
[i1
] & 0x0f) << 2;
240 out
[j
++] = b64charset
[b
];
248 asl_syslog_faciliy_name_to_num(const char *name
)
250 if (name
== NULL
) return -1;
252 if (strcaseeq(name
, "auth")) return LOG_AUTH
;
253 if (strcaseeq(name
, "authpriv")) return LOG_AUTHPRIV
;
254 if (strcaseeq(name
, "cron")) return LOG_CRON
;
255 if (strcaseeq(name
, "daemon")) return LOG_DAEMON
;
256 if (strcaseeq(name
, "ftp")) return LOG_FTP
;
257 if (strcaseeq(name
, "install")) return LOG_INSTALL
;
258 if (strcaseeq(name
, "kern")) return LOG_KERN
;
259 if (strcaseeq(name
, "lpr")) return LOG_LPR
;
260 if (strcaseeq(name
, "mail")) return LOG_MAIL
;
261 if (strcaseeq(name
, "netinfo")) return LOG_NETINFO
;
262 if (strcaseeq(name
, "remoteauth")) return LOG_REMOTEAUTH
;
263 if (strcaseeq(name
, "news")) return LOG_NEWS
;
264 if (strcaseeq(name
, "security")) return LOG_AUTH
;
265 if (strcaseeq(name
, "syslog")) return LOG_SYSLOG
;
266 if (strcaseeq(name
, "user")) return LOG_USER
;
267 if (strcaseeq(name
, "uucp")) return LOG_UUCP
;
268 if (strcaseeq(name
, "local0")) return LOG_LOCAL0
;
269 if (strcaseeq(name
, "local1")) return LOG_LOCAL1
;
270 if (strcaseeq(name
, "local2")) return LOG_LOCAL2
;
271 if (strcaseeq(name
, "local3")) return LOG_LOCAL3
;
272 if (strcaseeq(name
, "local4")) return LOG_LOCAL4
;
273 if (strcaseeq(name
, "local5")) return LOG_LOCAL5
;
274 if (strcaseeq(name
, "local6")) return LOG_LOCAL6
;
275 if (strcaseeq(name
, "local7")) return LOG_LOCAL7
;
276 if (strcaseeq(name
, "launchd")) return LOG_LAUNCHD
;
282 asl_syslog_faciliy_num_to_name(int n
)
284 if (n
< 0) return NULL
;
286 if (n
== LOG_AUTH
) return "auth";
287 if (n
== LOG_AUTHPRIV
) return "authpriv";
288 if (n
== LOG_CRON
) return "cron";
289 if (n
== LOG_DAEMON
) return "daemon";
290 if (n
== LOG_FTP
) return "ftp";
291 if (n
== LOG_INSTALL
) return "install";
292 if (n
== LOG_KERN
) return "kern";
293 if (n
== LOG_LPR
) return "lpr";
294 if (n
== LOG_MAIL
) return "mail";
295 if (n
== LOG_NETINFO
) return "netinfo";
296 if (n
== LOG_REMOTEAUTH
) return "remoteauth";
297 if (n
== LOG_NEWS
) return "news";
298 if (n
== LOG_AUTH
) return "security";
299 if (n
== LOG_SYSLOG
) return "syslog";
300 if (n
== LOG_USER
) return "user";
301 if (n
== LOG_UUCP
) return "uucp";
302 if (n
== LOG_LOCAL0
) return "local0";
303 if (n
== LOG_LOCAL1
) return "local1";
304 if (n
== LOG_LOCAL2
) return "local2";
305 if (n
== LOG_LOCAL3
) return "local3";
306 if (n
== LOG_LOCAL4
) return "local4";
307 if (n
== LOG_LOCAL5
) return "local5";
308 if (n
== LOG_LOCAL6
) return "local6";
309 if (n
== LOG_LOCAL7
) return "local7";
310 if (n
== LOG_LAUNCHD
) return "launchd";
315 static xpc_connection_t
316 _create_aslmanager_connection(void)
318 xpc_connection_t connection
;
320 connection
= xpc_connection_create_mach_service(ASLMANAGER_SERVICE_NAME
, NULL
, XPC_CONNECTION_MACH_SERVICE_PRIVILEGED
);
321 xpc_connection_set_event_handler(connection
, ^(xpc_object_t xobj
) { if (xobj
!= NULL
) {}; });
322 xpc_connection_resume(connection
);
328 asl_trigger_aslmanager(void)
330 xpc_connection_t connection
= _create_aslmanager_connection();
331 if (connection
== NULL
) return -1;
333 xpc_object_t request
= xpc_dictionary_create(NULL
, NULL
, 0);
334 if (request
== NULL
) return -1;
336 xpc_object_t reply
= xpc_connection_send_message_with_reply_sync(connection
, request
);
338 if (reply
!= NULL
) xpc_release(reply
);
339 xpc_release(request
);
340 xpc_release(connection
);