~ubuntu-branches/ubuntu/feisty/clamav/feisty

« back to all changes in this revision

Viewing changes to clamd/dazukofs.c

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2007-02-20 10:33:44 UTC
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20070220103344-zgcu2psnx9d98fpa
Tags: upstream-0.90
ImportĀ upstreamĀ versionĀ 0.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* userspace library to interface with dazukofs
2
 
 
3
 
   Copyright (C) 2008-2009 John Ogness
4
 
     Author: John Ogness <dazukocode@ogness.net>
5
 
 
6
 
   This library is free software; you can redistribute it and/or
7
 
   modify it under the terms of the GNU Lesser General Public
8
 
   License as published by the Free Software Foundation; either
9
 
   version 2.1 of the License, or (at your option) any later version.
10
 
 
11
 
   This library 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 Lesser General Public License for more details.
15
 
 
16
 
   You should have received a copy of the GNU Lesser General Public
17
 
   License along with this library; if not, write to the Free Software
18
 
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
 
*/
20
 
 
21
 
#include <stdio.h>
22
 
#include <stdlib.h>
23
 
#include <string.h>
24
 
#include <fcntl.h>
25
 
#include <unistd.h>
26
 
 
27
 
#include <sys/types.h>
28
 
#include <sys/stat.h>
29
 
 
30
 
#include "dazukofs.h"
31
 
 
32
 
struct dazukofs_handle
33
 
{
34
 
        int dev_fd;
35
 
        unsigned long event_id;
36
 
        char *group_name;
37
 
};
38
 
 
39
 
#define DAZUKOFS_ALLOWED_GROUPCHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-"
40
 
static int check_group_name(const char *gname)
41
 
{
42
 
        size_t len = strlen(gname);
43
 
        const char *p;
44
 
 
45
 
        if (len > 20)
46
 
                return -1;
47
 
 
48
 
        for (p = gname; *p; p++) {
49
 
                if (strchr(DAZUKOFS_ALLOWED_GROUPCHARS, *p) == NULL)
50
 
                        return -1;
51
 
        }
52
 
 
53
 
        return 0;
54
 
}
55
 
 
56
 
dazukofs_handle_t dazukofs_open(const char *gname, int flags)
57
 
{
58
 
        struct dazukofs_handle *hndl = NULL;
59
 
        char key[25];
60
 
        char buf[256];
61
 
        char *p;
62
 
        int gid;
63
 
        int fd;
64
 
 
65
 
        if (check_group_name(gname) != 0)
66
 
                goto error_out;
67
 
 
68
 
        fd = open("/dev/dazukofs.ctrl", O_RDWR);
69
 
        if (fd == -1) {
70
 
                /* try to read at least
71
 
                 * (maybe the group already exists) */
72
 
                fd = open("/dev/dazukofs.ctrl", O_RDONLY);
73
 
                if (fd == -1)
74
 
                        goto error_out;
75
 
        } else {
76
 
                memset(buf, 0, sizeof(buf));
77
 
 
78
 
                if (flags & DAZUKOFS_TRACK_GROUP)
79
 
                        snprintf(buf, sizeof(buf) - 1, "addtrack=%s", gname);
80
 
                else
81
 
                        snprintf(buf, sizeof(buf) - 1, "add=%s", gname);
82
 
 
83
 
                if (write(fd, buf, strlen(buf)) == -1)
84
 
                        goto error_out_close;
85
 
 
86
 
                lseek(fd, 0, SEEK_SET);
87
 
        }
88
 
 
89
 
        memset(buf, 0, sizeof(buf));
90
 
        if (read(fd, buf, sizeof(buf)-1) == -1)
91
 
                goto error_out_close;
92
 
 
93
 
        memset(key, 0, sizeof(key));
94
 
        snprintf(key, sizeof(key) - 1, ":%s\n", gname);
95
 
 
96
 
        p = strstr(buf, key);
97
 
        if (!p || p == buf)
98
 
                goto error_out_close;
99
 
 
100
 
        p--;
101
 
        gid = *p - '0';
102
 
        if (gid < 0 || gid > 9)
103
 
                goto error_out_close;
104
 
 
105
 
        hndl = malloc(sizeof(struct dazukofs_handle));
106
 
        if (!hndl)
107
 
                goto error_out_close;
108
 
        memset(hndl, 0, sizeof(struct dazukofs_handle));
109
 
 
110
 
        hndl->group_name = strdup(gname);
111
 
        if (!hndl->group_name)
112
 
                goto error_out_free;
113
 
 
114
 
        memset(key, 0, sizeof(key));
115
 
        snprintf(key, sizeof(key) - 1, "/dev/dazukofs.%d", gid);
116
 
 
117
 
        hndl->dev_fd = open(key, O_RDWR);
118
 
        if (hndl->dev_fd == -1)
119
 
                goto error_out_free;
120
 
 
121
 
        close(fd);
122
 
 
123
 
        return hndl;
124
 
 
125
 
error_out_free:
126
 
        if (hndl->group_name)
127
 
                free(hndl->group_name);
128
 
        free(hndl);
129
 
        hndl = NULL;
130
 
error_out_close:
131
 
        close(fd);
132
 
error_out:
133
 
        return hndl;
134
 
}
135
 
 
136
 
int dazukofs_close(dazukofs_handle_t hndl, int flags)
137
 
{
138
 
        char buf[48];
139
 
        int fd;
140
 
        int ret = -1;
141
 
 
142
 
        if (flags & DAZUKOFS_REMOVE_GROUP) {
143
 
                fd = open("/dev/dazukofs.ctrl", O_WRONLY);
144
 
                if (fd == -1)
145
 
                        goto error_out;
146
 
 
147
 
                memset(buf, 0, sizeof(buf));
148
 
                snprintf(buf, sizeof(buf) - 1, "del=%s", hndl->group_name);
149
 
 
150
 
                if (write(fd, buf, strlen(buf)) == -1) {
151
 
                        close(fd);
152
 
                        goto error_out;
153
 
                }
154
 
 
155
 
                close(fd);
156
 
        }
157
 
 
158
 
        ret = close(hndl->dev_fd);
159
 
        if (ret != 0)
160
 
                goto error_out;
161
 
 
162
 
        free(hndl->group_name);
163
 
        free(hndl);
164
 
 
165
 
        return 0;
166
 
 
167
 
error_out:
168
 
        return ret;
169
 
}
170
 
 
171
 
int dazukofs_get_access(dazukofs_handle_t hndl, struct dazukofs_access *acc)
172
 
{
173
 
        char buf[48];
174
 
        char *p;
175
 
        int err = -1;
176
 
 
177
 
        memset(buf, 0, sizeof(buf));
178
 
        if (read(hndl->dev_fd, buf, sizeof(buf)-1) == -1)
179
 
                goto out;
180
 
 
181
 
        p = strstr(buf, "id=");
182
 
        if (!p)
183
 
                goto out;
184
 
        p += 3;
185
 
        hndl->event_id = strtoul(p, &p, 10);
186
 
 
187
 
        p = strstr(p, "fd=");
188
 
        if (!p)
189
 
                goto out;
190
 
        p += 3;
191
 
        acc->fd = (int)strtol(p, &p, 10);
192
 
 
193
 
        p = strstr(p, "pid=");
194
 
        if (!p)
195
 
                goto out;
196
 
        p += 4;
197
 
        acc->pid = strtoul(p, NULL, 10);
198
 
 
199
 
        acc->deny = 0;
200
 
 
201
 
        err = 0;
202
 
out:
203
 
        return err;
204
 
}
205
 
 
206
 
int dazukofs_return_access(dazukofs_handle_t hndl, struct dazukofs_access *acc)
207
 
{
208
 
        char buf[48];
209
 
        int err = -1;
210
 
 
211
 
        if (close(acc->fd) != 0)
212
 
                goto out;
213
 
        snprintf(buf, sizeof(buf)-1, "id=%lu r=%d", hndl->event_id,
214
 
                 acc->deny ? 1 : 0);
215
 
        buf[sizeof(buf)-1] = 0;
216
 
 
217
 
        if (write(hndl->dev_fd, buf, strlen(buf)) == -1)
218
 
                goto out;
219
 
        lseek(hndl->dev_fd, 0, SEEK_SET);
220
 
        err = 0;
221
 
out:
222
 
        return err;
223
 
}
224
 
 
225
 
int dazukofs_get_filename(struct dazukofs_access *acc, char *buf, size_t bufsiz)
226
 
{
227
 
        char proc[48];
228
 
        int ret;
229
 
 
230
 
        memset(proc, 0, sizeof(proc));
231
 
        snprintf(proc, sizeof(proc) - 1, "/proc/self/fd/%d", acc->fd);
232
 
        ret = readlink(proc, buf, bufsiz - 1);
233
 
        buf[bufsiz - 1] = 0;
234
 
        if (ret > 0)
235
 
                buf[ret] = 0;
236
 
 
237
 
        return ret;
238
 
}