]>
git.saurik.com Git - apple/syslog.git/blob - syslogd.tproj/asl_action.c
2 * Copyright (c) 2004-2008 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, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <sys/types.h>
26 #include <sys/socket.h>
39 #define _PATH_ASL_CONF "/etc/asl.conf"
40 #define MY_ID "asl_action"
42 #define ASL_KEY_FACILITY "Facility"
43 #define IndexNull ((uint32_t)-1)
44 #define forever for(;;)
46 static asl_msg_t
*query
= NULL
;
55 TAILQ_ENTRY(action_rule
) entries
;
58 static TAILQ_HEAD(cr
, action_rule
) asl_action_rule
;
60 int asl_action_close();
61 static int _parse_notify_file(const char *);
67 _parse_notify_file(_PATH_ASL_CONF
);
72 * Q [k v] [k v] ... action args...
82 if (p
== NULL
) return NULL
;
83 if (*p
!= 'Q') return NULL
;
90 while ((*p
== ' ') || (*p
== '\t')) p
++;
92 if (*p
== '\0') return NULL
;
93 if (*p
!= '[') return p
;
95 /* skip to closing ] */
116 struct action_rule
*out
;
118 if (s
== NULL
) return -1;
119 while ((*s
== ' ') || (*s
== '\t')) s
++;
120 if (*s
== '#') return -1;
122 act
= _find_action(s
);
124 if (act
== NULL
) return -1;
125 out
= (struct action_rule
*)calloc(1, sizeof(struct action_rule
));
126 if (out
== NULL
) return -1;
128 p
= strchr(act
, ' ');
129 if (p
!= NULL
) *p
= '\0';
130 out
->action
= strdup(act
);
132 if (out
->action
== NULL
)
140 out
->options
= strdup(p
+1);
142 if (out
->options
== NULL
)
153 out
->query
= asl_msg_from_string(s
);
155 if (out
->query
== NULL
)
158 if (out
->options
!= NULL
) free(out
->options
);
163 TAILQ_INSERT_TAIL(&asl_action_rule
, out
, entries
);
172 char *a
, *p
, *e
, *out
;
175 if (s
== NULL
) return NULL
;
176 if (*s
== NULL
) return NULL
;
204 if (quote
== 0) quote
= 1;
208 if (((*p
== ' ') || (*p
== '\t')) && (quote
== 0))
221 if (len
== 0) return NULL
;
223 out
= malloc(len
+ 1);
224 if (out
== NULL
) return NULL
;
233 _act_notify(struct action_rule
*r
)
235 if (r
== NULL
) return;
236 if (r
->options
== NULL
) return;
237 notify_post(r
->options
);
241 _act_access_control(struct action_rule
*r
, asl_msg_t
*msg
)
246 ruid
= atoi(r
->options
);
248 p
= strchr(r
->options
, ' ');
249 if (p
== NULL
) p
= strchr(r
->options
, '\t');
257 if (ruid
!= -1) asl_set((aslmsg
)msg
, ASL_KEY_READ_UID
, r
->options
);
260 if (rgid
!= -1) asl_set((aslmsg
)msg
, ASL_KEY_READ_GID
, p
);
268 asl_action_sendmsg(asl_msg_t
*msg
, const char *outid
)
270 struct action_rule
*r
;
278 if (msg
== NULL
) return -1;
280 for (r
= asl_action_rule
.tqh_first
; r
!= NULL
; r
= r
->entries
.tqe_next
)
282 if (asl_msg_cmp(r
->query
, msg
) == 1)
284 if (r
->action
== NULL
) continue;
285 else if (!strcmp(r
->action
, "access")) _act_access_control(r
, msg
);
286 else if (!strcmp(r
->action
, "notify")) _act_notify(r
);
294 _parse_notify_file(const char *name
)
299 cf
= fopen(name
, "r");
300 if (cf
== NULL
) return 1;
302 while (NULL
!= (line
= get_line_from_file(cf
)))
314 asl_action_init(void)
316 asldebug("%s: init\n", MY_ID
);
318 TAILQ_INIT(&asl_action_rule
);
320 query
= asl_new(ASL_TYPE_QUERY
);
321 aslevent_addmatch(query
, MY_ID
);
322 aslevent_addoutput(asl_action_sendmsg
, MY_ID
);
324 _parse_notify_file(_PATH_ASL_CONF
);
329 asl_action_reset(void)
336 asl_action_close(void)
338 struct action_rule
*r
, *n
;
341 for (r
= asl_action_rule
.tqh_first
; r
!= NULL
; r
= n
)
343 n
= r
->entries
.tqe_next
;
345 if (r
->query
!= NULL
) asl_free(r
->query
);
346 if (r
->action
!= NULL
) free(r
->action
);
347 if (r
->options
!= NULL
) free(r
->options
);
349 TAILQ_REMOVE(&asl_action_rule
, r
, entries
);