~ubuntu-branches/ubuntu/oneiric/jabberd2/oneiric-security

« back to all changes in this revision

Viewing changes to sm/storage_fs.c

  • Committer: Bazaar Package Importer
  • Author(s): Nicolai Spohrer
  • Date: 2008-08-12 16:13:43 UTC
  • mfrom: (1.1.3 upstream) (0.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20080812161343-6trz3r97dtevxd17
Tags: 2.2.1-1ubuntu1
* Merge with Debian unstable (LP: #257130), remaining changes:
  - debian/control:
    + Modify Maintainer field as per spec
    + Depend on libdb4.6-dev instead of libdb4.4-dev
    + Added Conflicts and Replaces: ..., jabber for jabberd2
  - debian/rules: Added libtoolize call (jabberd2 ships with
     an older ltmain.sh version that conflicts with the
     current libtool version)
  - debian/init: create /var/run/jabber directory with correct
     permissions
* Dropped changes:
  - Debian already depends on libpq-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * jabberd - Jabber Open Source Server
3
 
 * Copyright (c) 2002-2003 Jeremie Miller, Thomas Muldowney,
4
 
 *                         Ryan Eatmon, Robert Norris
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or
9
 
 * (at your option) any 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; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
19
 
 */
20
 
 
21
 
/** @file sm/storage_fs.c
22
 
  * @brief filesystem storage module
23
 
  * @author Robert Norris
24
 
  * $Date: 2005/01/19 18:29:38 $
25
 
  * $Revision: 1.8.2.8 $
26
 
  */
27
 
 
28
 
/*
29
 
 * WARNING: this uses lots of static buffers, and doesn't do all the bounds
30
 
 * checking that it should. it should not be used for anything other than
31
 
 * testing
32
 
 *
33
 
 * !!! fix everything that makes this a problem
34
 
 */
35
 
 
36
 
#include "sm.h"
37
 
 
38
 
#ifdef STORAGE_FS
39
 
 
40
 
#include <ctype.h>
41
 
 
42
 
#ifdef HAVE_DIRENT_H
43
 
# include <dirent.h>
44
 
# define NAMELEN(dirent) strlen((dirent)->d_name)
45
 
#else
46
 
# define dirent direct
47
 
# define NAMELEN(dirent) (dirent)->d_namelen
48
 
# ifdef HAVE_SYS_NDIR_H
49
 
#  include <sys/ndir.h>
50
 
# endif
51
 
# ifdef HAVE_SYS_DIR_H
52
 
#  include <sys/dir.h>
53
 
# endif
54
 
# ifdef HAVE_NDIR_H
55
 
#  include <ndir.h>
56
 
# endif
57
 
#endif
58
 
 
59
 
/** internal structure, holds our data */
60
 
typedef struct drvdata_st {
61
 
    char *path;
62
 
} *drvdata_t;
63
 
 
64
 
static st_ret_t _st_fs_add_type(st_driver_t drv, const char *type) {
65
 
    drvdata_t data = (drvdata_t) drv->private;
66
 
    char path[1024];
67
 
    struct stat sbuf;
68
 
    int ret;
69
 
 
70
 
    snprintf(path, 1024, "%s/%s", data->path, type);
71
 
    ret = stat(path, &sbuf);
72
 
    if(ret < 0) {
73
 
        if(errno != ENOENT) {
74
 
            log_write(drv->st->sm->log, LOG_ERR, "fs: couldn't stat '%s': %s", path, strerror(errno));
75
 
            return st_FAILED;
76
 
        }
77
 
 
78
 
        log_debug(ZONE, "creating new type dir '%s'", path);
79
 
 
80
 
        ret = mkdir(path, 0755);
81
 
        if(ret < 0) {
82
 
            log_write(drv->st->sm->log, LOG_ERR, "fs: couldn't create directory '%s': %s", path, strerror(errno));
83
 
            return st_FAILED;
84
 
        }
85
 
    }
86
 
 
87
 
    return st_SUCCESS;
88
 
}
89
 
 
90
 
static st_ret_t _st_fs_put(st_driver_t drv, const char *type, const char *owner, os_t os) {
91
 
    drvdata_t data = (drvdata_t) drv->private;
92
 
    char path[1024];
93
 
    struct stat sbuf;
94
 
    int ret;
95
 
    int file;
96
 
    FILE *f;
97
 
    os_object_t o;
98
 
    char *key;
99
 
    void *val;
100
 
    os_type_t ot;
101
 
    char *xml;
102
 
    int len;
103
 
 
104
 
    if(os_count(os) == 0)
105
 
        return st_SUCCESS;
106
 
 
107
 
    snprintf(path, 1024, "%s/%s", data->path, type);
108
 
    ret = stat(path, &sbuf);
109
 
    if(ret < 0) {
110
 
        log_write(drv->st->sm->log, LOG_ERR, "fs: couldn't stat '%s': %s", path, strerror(errno));
111
 
        return st_FAILED;
112
 
    }
113
 
 
114
 
    snprintf(path, 1024, "%s/%s/%s", data->path, type, owner);
115
 
    ret = stat(path, &sbuf);
116
 
    if(ret < 0) {
117
 
        if(errno != ENOENT) {
118
 
            log_write(drv->st->sm->log, LOG_ERR, "fs: couldn't stat '%s': %s", path, strerror(errno));
119
 
            return st_FAILED;
120
 
        }
121
 
 
122
 
        log_debug(ZONE, "creating new collection dir '%s'", path);
123
 
 
124
 
        ret = mkdir(path, 0755);
125
 
        if(ret < 0) {
126
 
            log_write(drv->st->sm->log, LOG_ERR, "fs: couldn't create directory '%s': %s", path, strerror(errno));
127
 
            return st_FAILED;
128
 
        }
129
 
    }
130
 
 
131
 
    file = -1;
132
 
 
133
 
    if(os_iter_first(os))
134
 
        do {
135
 
            for(file++; file < 999999; file++) {
136
 
                snprintf(path, 1024, "%s/%s/%s/%d", data->path, type, owner, file);
137
 
 
138
 
                ret = stat(path, &sbuf);
139
 
                if(ret < 0 && errno == ENOENT)
140
 
                    break;
141
 
 
142
 
                if(ret < 0) {
143
 
                    log_write(drv->st->sm->log, LOG_ERR, "fs: couldn't stat '%s': %s", path, strerror(errno));
144
 
                    return st_FAILED;
145
 
                }
146
 
            }
147
 
                
148
 
            log_debug(ZONE, "will store object to %s", path);
149
 
 
150
 
            f = fopen(path, "w");
151
 
            if(f == NULL) {
152
 
                log_write(drv->st->sm->log, LOG_ERR, "fs: couldn't open '%s' for writing: %s", path, strerror(errno));
153
 
                return st_FAILED;
154
 
            }
155
 
 
156
 
            o = os_iter_object(os);
157
 
 
158
 
            if(os_object_iter_first(o))
159
 
                do {
160
 
                    os_object_iter_get(o, &key, &val, &ot);
161
 
 
162
 
                    log_debug(ZONE, "writing field %s type %d", key, ot);
163
 
 
164
 
                    switch(ot) {
165
 
                        case os_type_BOOLEAN:
166
 
                            fprintf(f, "%s %d %d\n", key, ot, (int) val == 0 ? 0 : 1);
167
 
                            break;
168
 
                            
169
 
                        case os_type_INTEGER:
170
 
                            fprintf(f, "%s %d %d\n", key, ot, (int) val);
171
 
                            break;
172
 
 
173
 
                        case os_type_STRING:
174
 
                            fprintf(f, "%s %d %s\n", key, ot, (char *) val);
175
 
                            break;
176
 
 
177
 
                        case os_type_NAD:
178
 
                            nad_print((nad_t) val, 0, &xml, &len);
179
 
                            fprintf(f, "%s %d %.*s\n", key, ot, len, xml);
180
 
                            break;
181
 
 
182
 
                        case os_type_UNKNOWN:
183
 
                            break;
184
 
                    }
185
 
 
186
 
                } while(os_object_iter_next(o));
187
 
 
188
 
            fclose(f);
189
 
 
190
 
        } while(os_iter_next(os));
191
 
 
192
 
    return st_SUCCESS;
193
 
}
194
 
 
195
 
static st_ret_t _st_fs_get(st_driver_t drv, const char *type, const char *owner, const char *filter, os_t *os) {
196
 
    drvdata_t data = (drvdata_t) drv->private;
197
 
    char path[1024], file[1024];
198
 
    struct stat sbuf;
199
 
    int ret;
200
 
    DIR *dir;
201
 
    struct dirent *dirent;
202
 
    FILE *f;
203
 
    char buf[4096], *otc, *val, *c;
204
 
    os_object_t o;
205
 
    os_type_t ot;
206
 
    int i;
207
 
    nad_t nad;
208
 
    st_filter_t sf;
209
 
 
210
 
    snprintf(path, 1024, "%s/%s/%s", data->path, type, owner);
211
 
    ret = stat(path, &sbuf);
212
 
    if(ret < 0) {
213
 
        if(errno == ENOENT)
214
 
            return st_NOTFOUND;
215
 
        log_write(drv->st->sm->log, LOG_ERR, "fs: couldn't stat '%s': %s", path, strerror(errno));
216
 
        return st_FAILED;
217
 
    }
218
 
 
219
 
    dir = opendir(path);
220
 
    if(dir == NULL) {
221
 
        log_write(drv->st->sm->log, LOG_ERR, "fs: couldn't open directory '%s': %s", path, strerror(errno));
222
 
        return st_FAILED;
223
 
    }
224
 
 
225
 
    *os = os_new();
226
 
 
227
 
    errno = 0;
228
 
    while((dirent = readdir(dir)) != NULL) {
229
 
        if(!(isdigit(dirent->d_name[0])))
230
 
            continue;
231
 
 
232
 
        snprintf(file, 1024, "%s/%s", path, dirent->d_name);
233
 
        f = fopen(file, "r");
234
 
        if(f == NULL) {
235
 
            log_write(drv->st->sm->log, LOG_ERR, "fs: couldn't open '%s' for reading: %s", path, strerror(errno));
236
 
            os_free(*os);
237
 
            closedir(dir);
238
 
            return st_FAILED;
239
 
        }
240
 
 
241
 
        o = os_object_new(*os);
242
 
 
243
 
        while(fgets(buf, 4096, f) != NULL) {
244
 
            otc = strchr(buf, ' ');
245
 
            *otc = '\0'; otc++;
246
 
 
247
 
            val = strchr(otc, ' ');
248
 
            *val = '\0'; val++;
249
 
 
250
 
            ot = (os_type_t) atoi(otc);
251
 
 
252
 
            switch(ot) {
253
 
                case os_type_BOOLEAN:
254
 
                case os_type_INTEGER:
255
 
                    i = atoi(val);
256
 
                    os_object_put(o, buf, &i, ot);
257
 
 
258
 
                    break;
259
 
 
260
 
                case os_type_STRING:
261
 
                    c = strchr(val, '\n');
262
 
                    if(c != NULL) *c = '\0';
263
 
                    os_object_put(o, buf, val, ot);
264
 
 
265
 
                    break;
266
 
 
267
 
                case os_type_NAD:
268
 
                    nad = nad_parse(drv->st->sm->router->nad_cache, val, 0);
269
 
                    if(nad == NULL) {
270
 
                        log_write(drv->st->sm->log, LOG_ERR, "fs: unable to parse stored XML; type=%s, owner=%s", type, owner);
271
 
                        os_free(*os);
272
 
                        fclose(f);
273
 
                        closedir(dir);
274
 
                        return st_FAILED;
275
 
                    }
276
 
                    os_object_put(o, buf, nad, ot);
277
 
                    nad_free(nad);
278
 
 
279
 
                    break;
280
 
 
281
 
                case os_type_UNKNOWN:
282
 
                    break;
283
 
            }
284
 
        }
285
 
 
286
 
        if(!feof(f)) {
287
 
            log_write(drv->st->sm->log, LOG_ERR, "fs: couldn't read from '%s': %s", path, strerror(errno));
288
 
            os_free(*os);
289
 
            fclose(f);
290
 
            closedir(dir);
291
 
            return st_FAILED;
292
 
        }
293
 
 
294
 
        fclose(f);
295
 
 
296
 
        errno = 0;
297
 
    }
298
 
 
299
 
    if(errno != 0) {
300
 
        log_write(drv->st->sm->log, LOG_ERR, "fs: couldn't read from directory '%s': %s", path, strerror(errno));
301
 
        closedir(dir);
302
 
        os_free(*os);
303
 
        return st_FAILED;
304
 
    }
305
 
 
306
 
    closedir(dir);
307
 
    
308
 
    sf = storage_filter(filter);
309
 
 
310
 
    if(os_iter_first(*os))
311
 
        do {
312
 
            o = os_iter_object(*os);
313
 
            if(!storage_match(sf, o, *os))
314
 
                os_object_free(o);
315
 
        } while(os_iter_next(*os));
316
 
 
317
 
    if(sf != NULL) pool_free(sf->p);
318
 
 
319
 
    return st_SUCCESS;
320
 
}
321
 
 
322
 
static st_ret_t _st_fs_delete(st_driver_t drv, const char *type, const char *owner, const char *filter) {
323
 
    drvdata_t data = (drvdata_t) drv->private;
324
 
    char path[1024], file[1024];
325
 
    struct stat sbuf;
326
 
    int ret;
327
 
    DIR *dir;
328
 
    os_t os;
329
 
    struct dirent *dirent;
330
 
    FILE *f;
331
 
    char buf[4096], *otc, *val, *c;
332
 
    os_object_t o;
333
 
    os_type_t ot;
334
 
    int i;
335
 
    nad_t nad;
336
 
    st_filter_t sf;
337
 
 
338
 
    snprintf(path, 1024, "%s/%s/%s", data->path, type, owner);
339
 
    ret = stat(path, &sbuf);
340
 
    if(ret < 0) {
341
 
        if(errno == ENOENT)
342
 
            return st_NOTFOUND;
343
 
        log_write(drv->st->sm->log, LOG_ERR, "fs: couldn't stat '%s': %s", path, strerror(errno));
344
 
        return st_FAILED;
345
 
    }
346
 
 
347
 
    dir = opendir(path);
348
 
    if(dir == NULL) {
349
 
        log_write(drv->st->sm->log, LOG_ERR, "fs: couldn't open directory '%s': %s", path, strerror(errno));
350
 
        return st_FAILED;
351
 
    }
352
 
 
353
 
    os = os_new();
354
 
 
355
 
    sf = storage_filter(filter);
356
 
 
357
 
    errno = 0;
358
 
    while((dirent = readdir(dir)) != NULL) {
359
 
        if(!(isdigit(dirent->d_name[0])))
360
 
            continue;
361
 
 
362
 
        snprintf(file, 1024, "%s/%s", path, dirent->d_name);
363
 
        f = fopen(file, "r");
364
 
        if(f == NULL) {
365
 
            log_write(drv->st->sm->log, LOG_ERR, "fs: couldn't open '%s' for reading: %s", path, strerror(errno));
366
 
            os_free(os);
367
 
            closedir(dir);
368
 
            return st_FAILED;
369
 
        }
370
 
 
371
 
        o = os_object_new(os);
372
 
 
373
 
        while(fgets(buf, 4096, f) != NULL) {
374
 
            otc = strchr(buf, ' ');
375
 
            *otc = '\0'; otc++;
376
 
 
377
 
            val = strchr(otc, ' ');
378
 
            *val = '\0'; val++;
379
 
 
380
 
            ot = (os_type_t) atoi(otc);
381
 
 
382
 
            switch(ot) {
383
 
                case os_type_BOOLEAN:
384
 
                case os_type_INTEGER:
385
 
                    i = atoi(val);
386
 
                    os_object_put(o, buf, &i, ot);
387
 
 
388
 
                    break;
389
 
 
390
 
                case os_type_STRING:
391
 
                    c = strchr(val, '\n');
392
 
                    if(c != NULL) *c = '\0';
393
 
                    os_object_put(o, buf, val, ot);
394
 
 
395
 
                    break;
396
 
 
397
 
                case os_type_NAD:
398
 
                    nad = nad_parse(drv->st->sm->router->nad_cache, val, 0);
399
 
                    if(nad == NULL)
400
 
                        log_write(drv->st->sm->log, LOG_ERR, "fs: unable to parse stored XML; type=%s, owner=%s", type, owner);
401
 
                    else {
402
 
                        os_object_put(o, buf, nad, ot);
403
 
                        nad_free(nad);
404
 
                    }
405
 
 
406
 
                    break;
407
 
 
408
 
                case os_type_UNKNOWN:
409
 
                    break;
410
 
            }
411
 
        }
412
 
 
413
 
        if(!feof(f)) {
414
 
            log_write(drv->st->sm->log, LOG_ERR, "fs: couldn't read from '%s': %s", path, strerror(errno));
415
 
            os_free(os);
416
 
            fclose(f);
417
 
            closedir(dir);
418
 
            return st_FAILED;
419
 
        }
420
 
 
421
 
        fclose(f);
422
 
 
423
 
        if(storage_match(sf, o, os)) {
424
 
            ret = unlink(file);
425
 
            if(ret < 0) {
426
 
                log_write(drv->st->sm->log, LOG_ERR, "fs: couldn't unlink '%s': %s", path, strerror(errno));
427
 
                if(sf != NULL) pool_free(sf->p);
428
 
                os_free(os);
429
 
                closedir(dir);
430
 
                return st_FAILED;
431
 
            }
432
 
        }
433
 
 
434
 
        errno = 0;
435
 
    }
436
 
 
437
 
    if(errno != 0) {
438
 
        log_write(drv->st->sm->log, LOG_ERR, "fs: couldn't read from directory '%s': %s", path, strerror(errno));
439
 
        closedir(dir);
440
 
        os_free(os);
441
 
        return st_FAILED;
442
 
    }
443
 
 
444
 
    if(sf != NULL) pool_free(sf->p);
445
 
 
446
 
    os_free(os);
447
 
 
448
 
    closedir(dir);
449
 
 
450
 
    return st_SUCCESS;
451
 
}
452
 
 
453
 
static st_ret_t _st_fs_replace(st_driver_t drv, const char *type, const char *owner, const char *filter, os_t os) {
454
 
    st_ret_t ret;
455
 
 
456
 
    ret = _st_fs_delete(drv, type, owner, filter);
457
 
    if(ret == st_SUCCESS || ret == st_NOTFOUND)
458
 
        ret = _st_fs_put(drv, type, owner, os);
459
 
 
460
 
    return ret;
461
 
}
462
 
 
463
 
static void _st_fs_free(st_driver_t drv) {
464
 
    drvdata_t data = (drvdata_t) drv->private;
465
 
 
466
 
    free(data);
467
 
}
468
 
 
469
 
st_ret_t st_fs_init(st_driver_t drv) {
470
 
    char *path;
471
 
    struct stat sbuf;
472
 
    int ret;
473
 
    drvdata_t data;
474
 
 
475
 
    path = config_get_one(drv->st->sm->config, "storage.fs.path", 0);
476
 
    if(path == NULL) {
477
 
        log_write(drv->st->sm->log, LOG_ERR, "fs: no path specified in config file");
478
 
        return st_FAILED;
479
 
    }
480
 
 
481
 
    ret = stat(path, &sbuf);
482
 
    if(ret < 0) {
483
 
        log_write(drv->st->sm->log, LOG_ERR, "fs: couldn't stat path '%s': %s", path, strerror(errno));
484
 
        return st_FAILED;
485
 
    }
486
 
 
487
 
    data = (drvdata_t) malloc(sizeof(struct drvdata_st));
488
 
    memset(data, 0, sizeof(struct drvdata_st));
489
 
 
490
 
    data->path = path;
491
 
 
492
 
    drv->private = (void *) data;
493
 
 
494
 
    drv->add_type = _st_fs_add_type;
495
 
    drv->put = _st_fs_put;
496
 
    drv->get = _st_fs_get;
497
 
    drv->delete = _st_fs_delete;
498
 
    drv->replace = _st_fs_replace;
499
 
    drv->free = _st_fs_free;
500
 
 
501
 
    log_write(drv->st->sm->log, LOG_WARNING, "fs: the filesystem storage driver should only be used for testing!");
502
 
 
503
 
    return st_SUCCESS;
504
 
}
505
 
 
506
 
#endif