]> git.saurik.com Git - apple/syslog.git/blame - libsystem_asl.tproj/src/asl_util.c
syslog-349.1.1.tar.gz
[apple/syslog.git] / libsystem_asl.tproj / src / asl_util.c
CommitLineData
81582353 1/*
f3df4c03 2 * Copyright (c) 2006-2013 Apple Inc. All rights reserved.
81582353
A
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 OR NON-INFRINGEMENT. Please see the
18 * License for the specific language governing rights and limitations
19 * under the License."
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#include <string.h>
25#include <sys/types.h>
26#include <sys/socket.h>
27#include <sys/un.h>
28#include <fcntl.h>
29#include <stdint.h>
30#include <stdlib.h>
31#include <errno.h>
32#include <unistd.h>
33#include <Block.h>
34#include <dispatch/dispatch.h>
35#include <os/assumes.h>
36#include <xpc/xpc.h>
f3df4c03 37#include <syslog.h>
5222c21d 38#include <asl_core.h>
81582353
A
39#include <asl_private.h>
40
41static uint8_t *b64charset = (uint8_t *)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
42
43static int
44asl_is_utf8_char(const unsigned char *p, int *state, int *ctype)
45{
46 switch (*state)
47 {
48 case 0:
49 {
50 *ctype = 0;
51
52 if (*p >= 0x80)
53 {
54 *state = 1;
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;
61 else return 0;
62 }
63
64 break;
65 }
66
67 case 1:
68 {
69 switch (*ctype)
70 {
71 case 1:
72 {
73 if ((*p >= 0x80) && (*p <= 0xbf)) *state = 0;
74 else return 0;
75 break;
76 }
77
78 case 2:
79 {
80 if ((*p >= 0xa0) && (*p <= 0xbf)) *state = 2;
81 else return 0;
82 break;
83 }
84
85 case 3:
86 {
87 if ((*p >= 0x80) && (*p <= 0xbf)) *state = 2;
88 else return 0;
89 break;
90 }
91
92 case 4:
93 {
94 if ((*p >= 0x90) && (*p <= 0xbf)) *state = 2;
95 else return 0;
96 break;
97 }
98
99 case 5:
100 {
101 if ((*p >= 0x80) && (*p <= 0xbf)) *state = 2;
102 else return 0;
103 break;
104 }
105
106 case 6:
107 {
108 if ((*p >= 0x80) && (*p <= 0x8f)) *state = 2;
109 else return 0;
110 break;
111 }
112
113 default: return 0;
114 }
115
116 break;
117 }
118
119 case 2:
120 {
121 if ((*ctype >= 2) && (*ctype <= 3))
122 {
123 if ((*p >= 0x80) && (*p <= 0xbf)) *state = 0;
124 else return 0;
125 }
126 else if ((*ctype >= 4) && (*ctype <= 6))
127 {
128 if ((*p >= 0x80) && (*p <= 0xbf)) *state = 3;
129 else return 0;
130 }
131 else
132 {
133 return 0;
134 }
135
136 break;
137 }
138
139 case 3:
140 {
141 if ((*ctype >= 4) && (*ctype <= 6))
142 {
143 if ((*p >= 0x80) && (*p <= 0xbf)) *state = 0;
144 else return 0;
145 }
146 else
147 {
148 return 0;
149 }
150
151 break;
152 }
153
154 default: return 0;
155 }
156
157 return 1;
158}
159
160__private_extern__ int
161asl_is_utf8(const char *str)
162{
163 const unsigned char *p;
164 int flag = 1;
165 int state = 0;
166 int ctype = 0;
167
168 if (str == NULL) return flag;
169
170 for (p = (const unsigned char *)str; (*p != '\0') && (flag == 1); p++)
171 {
172 flag = asl_is_utf8_char(p, &state, &ctype);
173 }
174
175 return flag;
176}
177
178__private_extern__ uint8_t *
179asl_b64_encode(const uint8_t *buf, size_t len)
180{
181 uint8_t *out;
182 uint8_t b;
183 size_t i0, i1, i2, j, outlen;
f3df4c03 184
81582353
A
185 if (buf == NULL) return NULL;
186 if (len == 0) return NULL;
f3df4c03 187
81582353
A
188 outlen = ((len + 2) / 3) * 4;
189 out = (uint8_t *)malloc(outlen + 1);
190 if (out == NULL)
191 {
192 errno = ENOMEM;
193 return NULL;
194 }
f3df4c03 195
81582353 196 out[outlen] = 0;
f3df4c03 197
81582353
A
198 i0 = 0;
199 i1 = 1;
200 i2 = 2;
201 j = 0;
f3df4c03 202
81582353
A
203 while (i2 < len)
204 {
205 b = buf[i0] >> 2;
206 out[j++] = b64charset[b];
f3df4c03 207
81582353
A
208 b = ((buf[i0] & 0x03) << 4) | (buf[i1] >> 4);
209 out[j++] = b64charset[b];
f3df4c03 210
81582353
A
211 b = ((buf[i1] & 0x0f) << 2) | ((buf[i2] & 0xc0) >> 6);
212 out[j++] = b64charset[b];
f3df4c03 213
81582353
A
214 b = buf[i2] & 0x3f;
215 out[j++] = b64charset[b];
f3df4c03 216
81582353
A
217 i0 += 3;
218 i1 = i0 + 1;
219 i2 = i1 + 1;
220 }
f3df4c03 221
81582353
A
222 if (i0 < len)
223 {
224 b = buf[i0] >> 2;
225 out[j++] = b64charset[b];
f3df4c03 226
81582353 227 b = (buf[i0] & 0x03) << 4;
f3df4c03 228
81582353
A
229 if (i1 < len) b |= (buf[i1] >> 4);
230 out[j++] = b64charset[b];
f3df4c03 231
81582353
A
232 if (i1 >= len)
233 {
234 out[j++] = '=';
235 out[j++] = '=';
236 return out;
237 }
f3df4c03 238
81582353
A
239 b = (buf[i1] & 0x0f) << 2;
240 out[j++] = b64charset[b];
241 out[j++] = '=';
242 }
f3df4c03 243
81582353
A
244 return out;
245}
246
f3df4c03
A
247int
248asl_syslog_faciliy_name_to_num(const char *name)
249{
250 if (name == NULL) return -1;
251
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;
277
278 return -1;
279}
280
281const char *
282asl_syslog_faciliy_num_to_name(int n)
283{
284 if (n < 0) return NULL;
285
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";
311
312 return NULL;
313}
314
81582353
A
315static xpc_connection_t
316_create_aslmanager_connection(void)
317{
318 xpc_connection_t connection;
319
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);
323
324 return connection;
325}
326
327int
328asl_trigger_aslmanager(void)
329{
330 xpc_connection_t connection = _create_aslmanager_connection();
331 if (connection == NULL) return -1;
332
333 xpc_object_t request = xpc_dictionary_create(NULL, NULL, 0);
334 if (request == NULL) return -1;
335
336 xpc_object_t reply = xpc_connection_send_message_with_reply_sync(connection, request);
337
338 if (reply != NULL) xpc_release(reply);
339 xpc_release(request);
340 xpc_release(connection);
341 return 0;
342}