~ubuntu-branches/ubuntu/maverick/audit/maverick

« back to all changes in this revision

Viewing changes to new_audispd/audispd-builtins.c

  • Committer: Bazaar Package Importer
  • Author(s): Mathias Gug
  • Date: 2007-06-29 13:05:14 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070629130514-z798cz4lebiahj5w
Tags: 1.5.4-0ubuntu1
* New upstream version.
* debian/patches/audit-1.5.1-dist.patch:
  * update so that it applies for 1.5.4.
* debian/control:
  * update Maintainer and XSBC-Original-Maintainer fields.
* debian/rules:
  * enable apparmor support: add --with-apparmor to configure options.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* audispd-builtins.c - some common builtin plugins
 
3
* Copyright (c) 2007 Red Hat Inc., Durham, North Carolina.
 
4
* All Rights Reserved. 
 
5
*
 
6
* This software may be freely redistributed and/or modified under the
 
7
* terms of the GNU General Public License as published by the Free
 
8
* Software Foundation; either version 2, or (at your option) any
 
9
* later version.
 
10
*
 
11
* This program is distributed in the hope that it will be useful,
 
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
* GNU General Public License for more details.
 
15
*
 
16
* You should have received a copy of the GNU General Public License
 
17
* along with this program; see the file COPYING. If not, write to the
 
18
* Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
*
 
20
* Authors:
 
21
*   Steve Grubb <sgrubb@redhat.com>
 
22
*/
 
23
 
 
24
#include "config.h"
 
25
#include <string.h>
 
26
#include <dirent.h>
 
27
#include <libgen.h>
 
28
#include <ctype.h>
 
29
#include <errno.h>
 
30
#include <stdlib.h>
 
31
#include <unistd.h>
 
32
#include <sys/un.h>
 
33
#include <sys/stat.h>
 
34
#include <fcntl.h>
 
35
#include "audispd-pconfig.h"
 
36
#include "audispd-builtins.h"
 
37
 
 
38
// Local data
 
39
static int sock = -1, conn = -1;
 
40
static int syslog_started = 0, priority;
 
41
static char *path = NULL;
 
42
 
 
43
// Local prototypes
 
44
static void init_af_unix(const plugin_conf_t *conf);
 
45
static void init_syslog(const plugin_conf_t *conf);
 
46
 
 
47
 
 
48
void start_builtin(plugin_conf_t *conf)
 
49
{
 
50
        if (strcasecmp("builtin_af_unix", conf->path) == 0)
 
51
                init_af_unix(conf);
 
52
        else if (strcasecmp("builtin_syslog", conf->path) == 0)
 
53
                init_syslog(conf);
 
54
        else
 
55
                syslog(LOG_ERR, "Unknown builtin %s", conf->path);
 
56
}
 
57
 
 
58
static void init_af_unix(const plugin_conf_t *conf)
 
59
{
 
60
        int i = 1, mode = -1;
 
61
        char *base = NULL;
 
62
 
 
63
        // while args
 
64
        while (conf->args[i]) {
 
65
                int rc, bad = 0;
 
66
 
 
67
                // is all nums - do mode
 
68
                base = conf->args[i];
 
69
                while (*base) {
 
70
                        if (!isdigit(*base)) {
 
71
                                bad = 1;
 
72
                                break;
 
73
                        }
 
74
                        base++;
 
75
                }
 
76
                if (!bad) {
 
77
                        errno = 0;
 
78
                        mode = strtoul(conf->args[i], NULL, 8);
 
79
                        if (errno) {
 
80
                                syslog(LOG_ERR, "Error converting %s (%s)",
 
81
                                        conf->args[i], strerror(errno));
 
82
                                mode = -1;
 
83
                                bad = 1;
 
84
                        } else if (path) {
 
85
                                rc = chmod(path, mode);
 
86
                                if (rc < 0) {
 
87
                                        syslog(LOG_ERR,
 
88
                                            "Couldn't chmod %s to %04o (%s)",
 
89
                                                conf->args[i], mode,
 
90
                                                strerror(errno));
 
91
                                        destroy_af_unix();
 
92
                                        return;
 
93
                                }
 
94
                        }
 
95
                } else {
 
96
                        // else check for '/'
 
97
                        base = strchr(conf->args[i], '/');
 
98
                        if (base) {
 
99
                                // get dirname
 
100
                                DIR *d;
 
101
                                char *dir = strdup(conf->args[i]);
 
102
                                base = dirname(dir);
 
103
                                d = opendir(base);
 
104
                                if (d) {
 
105
                                        struct sockaddr_un addr;
 
106
                                        socklen_t len;
 
107
                                        int cmd;
 
108
 
 
109
                                        closedir(d);
 
110
                                        unlink(conf->args[i]);
 
111
                                        sock = socket(PF_UNIX, SOCK_STREAM, 0);
 
112
                                        if (sock < 0) {
 
113
                                                syslog(LOG_ERR, 
 
114
                                            "Couldn't open af_unix socket (%s)",
 
115
                                                strerror(errno));
 
116
                                                return;
 
117
                                        }
 
118
                                        memset(&addr, 0, sizeof(addr));
 
119
                                        addr.sun_family = AF_UNIX;
 
120
                                        strcpy(&addr.sun_path[0],conf->args[i]);
 
121
                                        len = sizeof(addr);
 
122
                                        rc = bind(sock, 
 
123
                                                (const struct sockaddr *)&addr,
 
124
                                                len);
 
125
                                        if (rc < 0) {
 
126
                                                syslog(LOG_ERR, 
 
127
                                            "Couldn't bind af_unix socket (%s)",
 
128
                                                strerror(errno));
 
129
                                                destroy_af_unix();
 
130
                                                return;
 
131
                                        }
 
132
                                        if (mode != -1) { 
 
133
                                                rc = chmod(conf->args[i], mode);
 
134
                                                if (rc < 0) {
 
135
                                                        syslog(LOG_ERR,
 
136
                                            "Couldn't chmod %s to %04o (%s)",
 
137
                                                                conf->args[i],
 
138
                                                                mode,
 
139
                                                                strerror(
 
140
                                                                        errno));
 
141
                                                        destroy_af_unix();
 
142
                                                        return;
 
143
                                                }
 
144
                                        }
 
145
                                        rc = listen(sock, 5);
 
146
                                        if (rc) {
 
147
                                                syslog(LOG_ERR, 
 
148
                                    "Couldn't listen on af_unix socket (%s)",
 
149
                                                strerror(errno));
 
150
                                                destroy_af_unix();
 
151
                                                return;
 
152
                                        }
 
153
                                        // Put socket in nonblock mode
 
154
                                        cmd = fcntl(sock, F_GETFL);
 
155
                                        fcntl(sock, F_SETFL, cmd|FNDELAY);
 
156
                                        // don't leak the descriptor
 
157
                                        cmd = fcntl(sock, F_GETFD);
 
158
                                        fcntl(sock, F_SETFD, cmd|FD_CLOEXEC);
 
159
                                        path = strdup(conf->args[i]);
 
160
                                        bad = 0;
 
161
                                } else
 
162
                                        syslog(LOG_ERR, "Couldn't open %s (%s)",
 
163
                                                base, strerror(errno));
 
164
                                free(dir);
 
165
                        } else 
 
166
                                syslog(LOG_ERR, "Malformed path %s",
 
167
                                                conf->args[i]);
 
168
                }
 
169
                if (bad) {
 
170
                        destroy_af_unix();
 
171
                        return;
 
172
                }
 
173
                i++;
 
174
        }
 
175
        syslog(LOG_INFO, "af_unix plugin initialized");
 
176
}
 
177
 
 
178
void send_af_unix(const char *s)
 
179
{
 
180
        if (sock < 0)
 
181
                return;
 
182
        if (conn < 0) {
 
183
                struct sockaddr_un peer_addr;
 
184
                socklen_t peer_addr_size = sizeof(peer_addr);
 
185
                conn = accept(sock, &peer_addr, &peer_addr_size);
 
186
        }
 
187
        if (conn >= 0) {
 
188
                write(sock, s, strlen(s));
 
189
        }
 
190
}
 
191
 
 
192
void destroy_af_unix(void)
 
193
{
 
194
        if (conn >= 0) {
 
195
                close(conn);
 
196
                conn = -1;
 
197
        }
 
198
        if (sock >= 0) {
 
199
                close(sock);
 
200
                sock = -1;
 
201
        }
 
202
        if (path) {
 
203
                unlink(path);
 
204
                free(path);
 
205
                path = NULL;
 
206
        }
 
207
}
 
208
 
 
209
static void init_syslog(const plugin_conf_t *conf)
 
210
{
 
211
        if (conf->args[1]) {
 
212
                if (strcasecmp(conf->args[1], "LOG_DEBUG") == 0)
 
213
                        priority = LOG_DEBUG;
 
214
                else if (strcasecmp(conf->args[1], "LOG_INFO") == 0)
 
215
                        priority = LOG_INFO;
 
216
                else if (strcasecmp(conf->args[1], "LOG_NOTICE") == 0)
 
217
                        priority = LOG_NOTICE;
 
218
                else if (strcasecmp(conf->args[1], "LOG_WARNING") == 0)
 
219
                        priority = LOG_WARNING;
 
220
                else if (strcasecmp(conf->args[1], "LOG_ERR") == 0)
 
221
                        priority = LOG_ERR;
 
222
                else if (strcasecmp(conf->args[1], "LOG_CRIT") == 0)
 
223
                        priority = LOG_CRIT;
 
224
                else if (strcasecmp(conf->args[1], "LOG_ALERT") == 0)
 
225
                        priority = LOG_ALERT;
 
226
                else if (strcasecmp(conf->args[1], "LOG_EMERG") == 0)
 
227
                        priority = LOG_EMERG;
 
228
                else {
 
229
                        syslog(LOG_ERR, "Unknown log priority %s",
 
230
                                conf->args[1]);
 
231
                        syslog_started = 0;
 
232
                        return;
 
233
                }
 
234
        } else
 
235
                priority = LOG_INFO;
 
236
        syslog_started = 1;
 
237
        syslog(LOG_INFO, "syslog plugin initialized");
 
238
}
 
239
 
 
240
void send_syslog(const char *s)
 
241
{
 
242
        if (syslog_started) {
 
243
                syslog(priority, "%s", s);
 
244
        }
 
245
}
 
246
 
 
247
void destroy_syslog(void)
 
248
{
 
249
        syslog_started = 0;
 
250
}
 
251