1 /* Copyright (c) 2014 Percona LLC and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or
4    modify it under the terms of the GNU General Public License
5    as published by the Free Software Foundation; version 2 of
6    the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
16 
17 #include <syslog.h>
18 #include <string.h>
19 #include "audit_handler.h"
20 #include "audit_log.h"
21 
22 typedef struct audit_handler_syslog_data_struct audit_handler_syslog_data_t;
23 
24 struct audit_handler_syslog_data_struct
25 {
26   size_t struct_size;
27   int priority;
28   logger_prolog_func_t header;
29   logger_epilog_func_t footer;
30 };
31 
32 static int audit_handler_syslog_write(audit_handler_t *handler,
33                                       const char *buf, size_t len);
34 static int audit_handler_syslog_flush(audit_handler_t *handler);
35 static int audit_handler_syslog_close(audit_handler_t *handler);
36 
37 
audit_handler_syslog_open(audit_handler_syslog_config_t * opts)38 audit_handler_t *audit_handler_syslog_open(audit_handler_syslog_config_t *opts)
39 {
40   audit_handler_t *handler= (audit_handler_t*)
41     my_malloc(key_memory_audit_log_handler,
42               sizeof(audit_handler_t) + sizeof(audit_handler_syslog_data_t),
43               MY_ZEROFILL);
44   if (handler != NULL)
45   {
46     audit_handler_syslog_data_t *data=
47                                    (audit_handler_syslog_data_t*) (handler + 1);
48     MY_STAT stat_arg;
49 
50     data->struct_size= sizeof(audit_handler_syslog_data_t);
51     data->priority= opts->priority;
52     data->header= opts->header;
53     data->footer= opts->footer;
54     openlog(opts->ident, 0, opts->facility);
55     memset(&stat_arg, 0, sizeof(stat_arg));
56     opts->header(&stat_arg, NULL, 0);
57     handler->data= data;
58     handler->write= audit_handler_syslog_write;
59     handler->flush= audit_handler_syslog_flush;
60     handler->close= audit_handler_syslog_close;
61   }
62   return handler;
63 }
64 
65 static
audit_handler_syslog_write(audit_handler_t * handler,const char * buf,size_t len)66 int audit_handler_syslog_write(audit_handler_t *handler,
67                                const char *buf, size_t len)
68 {
69   audit_handler_syslog_data_t *data=
70                                    (audit_handler_syslog_data_t*) handler->data;
71   assert(data->struct_size == sizeof(audit_handler_syslog_data_t));
72   syslog(data->priority, "%s", buf);
73   return len;
74 }
75 
76 static
audit_handler_syslog_flush(audit_handler_t * handler)77 int audit_handler_syslog_flush(audit_handler_t *handler)
78 {
79   audit_handler_syslog_data_t *data=
80                                    (audit_handler_syslog_data_t*) handler->data;
81   MY_STAT stat_arg;
82   memset(&stat_arg, 0, sizeof(stat_arg));
83   data->header(&stat_arg, NULL, 0);
84   data->footer(NULL, 0);
85   return 0;
86 }
87 
88 static
audit_handler_syslog_close(audit_handler_t * handler)89 int audit_handler_syslog_close(audit_handler_t *handler)
90 {
91   audit_handler_syslog_data_t *data=
92                                    (audit_handler_syslog_data_t*) handler->data;
93   data->footer(NULL, 0);
94   closelog();
95   my_free(handler);
96   return 0;
97 }
98