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 
21 typedef struct audit_handler_syslog_data_struct audit_handler_syslog_data_t;
22 
23 struct audit_handler_syslog_data_struct
24 {
25   size_t struct_size;
26   int priority;
27   logger_prolog_func_t header;
28   logger_epilog_func_t footer;
29 };
30 
31 static int audit_handler_syslog_write(audit_handler_t *handler,
32                                       const char *buf, size_t len);
33 static int audit_handler_syslog_flush(audit_handler_t *handler);
34 static int audit_handler_syslog_close(audit_handler_t *handler);
35 
36 
audit_handler_syslog_open(audit_handler_syslog_config_t * opts)37 audit_handler_t *audit_handler_syslog_open(audit_handler_syslog_config_t *opts)
38 {
39   audit_handler_t *handler= (audit_handler_t*)
40        calloc(sizeof(audit_handler_t) + sizeof(audit_handler_syslog_data_t), 1);
41   if (handler != NULL)
42   {
43     audit_handler_syslog_data_t *data=
44                                    (audit_handler_syslog_data_t*) (handler + 1);
45     MY_STAT stat_arg;
46 
47     data->struct_size= sizeof(audit_handler_syslog_data_t);
48     data->priority= opts->priority;
49     data->header= opts->header;
50     data->footer= opts->footer;
51     openlog(opts->ident, 0, opts->facility);
52     memset(&stat_arg, 0, sizeof(stat_arg));
53     opts->header(&stat_arg, NULL, 0);
54     handler->data= data;
55     handler->write= audit_handler_syslog_write;
56     handler->flush= audit_handler_syslog_flush;
57     handler->close= audit_handler_syslog_close;
58   }
59   return handler;
60 }
61 
62 static
audit_handler_syslog_write(audit_handler_t * handler,const char * buf,size_t len)63 int audit_handler_syslog_write(audit_handler_t *handler,
64                                const char *buf, size_t len)
65 {
66   audit_handler_syslog_data_t *data=
67                                    (audit_handler_syslog_data_t*) handler->data;
68   DBUG_ASSERT(data->struct_size == sizeof(audit_handler_syslog_data_t));
69   syslog(data->priority, "%s", buf);
70   return len;
71 }
72 
73 static
audit_handler_syslog_flush(audit_handler_t * handler)74 int audit_handler_syslog_flush(audit_handler_t *handler)
75 {
76   audit_handler_syslog_data_t *data=
77                                    (audit_handler_syslog_data_t*) handler->data;
78   MY_STAT stat_arg;
79   memset(&stat_arg, 0, sizeof(stat_arg));
80   data->header(&stat_arg, NULL, 0);
81   data->footer(NULL, 0);
82   return 0;
83 }
84 
85 static
audit_handler_syslog_close(audit_handler_t * handler)86 int audit_handler_syslog_close(audit_handler_t *handler)
87 {
88   audit_handler_syslog_data_t *data=
89                                    (audit_handler_syslog_data_t*) handler->data;
90   data->footer(NULL, 0);
91   closelog();
92   free(handler);
93   return 0;
94 }
95