~ubuntu-branches/ubuntu/hoary/postfix/hoary-security

« back to all changes in this revision

Viewing changes to src/util/dict_open.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-10-06 11:50:33 UTC
  • Revision ID: james.westby@ubuntu.com-20041006115033-9oky44ylqmhjy7eq
Tags: 2.1.3-1ubuntu17
* Deliver man pages for master.cf services in section 8postfix.
  Remove smtpd.8.gz diversion. Closes: #274777
* Clean up postfix-mysql documentation (created README.Debian files).
  Closes: Warty#2022
* Fix typo in postmap man page.  Closes: #271369

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
/*      dict_open_register(type, open)
43
43
/*      char    *type;
44
44
/*      DICT    *(*open) (const char *, int, int);
 
45
/*
 
46
/*      ARGV   *dict_mapnames()
 
47
/*
 
48
/*      void (*)() dict_mkmap_func(const char *dict_type)
45
49
/* DESCRIPTION
46
50
/*      This module implements a low-level interface to multiple
47
51
/*      physical dictionary types.
135
139
/*      associated data structures.
136
140
/*
137
141
/*      dict_open_register() adds support for a new dictionary type.
 
142
/*
 
143
/*      dict_mapnames() returns an ARGV list containing all of the known
 
144
/*      map types, including dynamic maps.
 
145
/*
 
146
/*      dict_mkmap_func() returns a pointer to the mkmap setup function
 
147
/*      for the given map type, as given in /etc/dynamicmaps.cf
 
148
/*
138
149
/* DIAGNOSTICS
139
150
/*      Fatal error: open error, unsupported dictionary type, attempt to
140
151
/*      update non-writable dictionary.
158
169
#include <strings.h>
159
170
#endif
160
171
 
 
172
#include <sys/stat.h>
 
173
#include <unistd.h>
 
174
 
161
175
/* Utility library. */
162
176
 
163
177
#include <argv.h>
180
194
#include <split_at.h>
181
195
#include <htable.h>
182
196
 
 
197
#ifndef NO_DYNAMIC_MAPS
 
198
#include <load_lib.h>
 
199
#include <vstring.h>
 
200
#include <vstream.h>
 
201
#include <vstring_vstream.h>
 
202
#include <mvect.h>
 
203
 
 
204
 /*
 
205
  * Interface for dynamic map loading.
 
206
  */
 
207
typedef struct {
 
208
    const char  *pattern;
 
209
    const char  *soname;
 
210
    const char  *openfunc;
 
211
    const char  *mkmapfunc;
 
212
} DLINFO;
 
213
 
 
214
static DLINFO *dict_dlinfo;
 
215
static DLINFO *dict_open_dlfind(const char *type);
 
216
#endif
 
217
 
183
218
 /*
184
219
  * lookup table for available map types.
185
220
  */
210
245
#ifdef HAS_NETINFO
211
246
    DICT_TYPE_NETINFO, dict_ni_open,
212
247
#endif
 
248
#ifndef MAX_DYNAMIC_MAPS
213
249
#ifdef HAS_PCRE
214
250
    DICT_TYPE_PCRE, dict_pcre_open,
215
251
#endif
 
252
#endif /* MAX_DYNAMIC_MAPS */
216
253
#ifdef HAS_POSIX_REGEXP
217
254
    DICT_TYPE_REGEXP, dict_regexp_open,
218
255
#endif
267
304
 
268
305
    if (dict_open_hash == 0)
269
306
        dict_open_init();
270
 
    if ((dp = (DICT_OPEN_INFO *) htable_find(dict_open_hash, dict_type)) == 0)
271
 
        msg_fatal("unsupported dictionary type: %s", dict_type);
 
307
    if ((dp = (DICT_OPEN_INFO *) htable_find(dict_open_hash, dict_type)) == 0) {
 
308
#ifdef NO_DYNAMIC_MAPS
 
309
        msg_fatal("%s: unsupported dictionary type: %s", myname, dict_type);
 
310
#else
 
311
        struct stat st;
 
312
        LIB_FN fn[2];
 
313
        DICT *(*open) (const char *, int, int);
 
314
        DLINFO *dl=dict_open_dlfind(dict_type);
 
315
        if (!dl)
 
316
            msg_fatal("%s: unsupported dictionary type: %s:  Is the postfix-%s package installed?", myname, dict_type, dict_type);
 
317
        if (stat(dl->soname,&st) < 0) {
 
318
            msg_fatal("%s: unsupported dictionary type: %s (%s not found.  Is the postfix-%s package installed?)",
 
319
                myname, dict_type, dl->soname, dict_type);
 
320
        }
 
321
        fn[0].name = dl->openfunc;
 
322
        fn[0].ptr  = (void**)&open;
 
323
        fn[1].name = NULL;
 
324
        load_library_symbols(dl->soname, fn, NULL);
 
325
        dict_open_register(dict_type, open);
 
326
        dp = (DICT_OPEN_INFO *) htable_find(dict_open_hash, dict_type);
 
327
#endif
 
328
    }
 
329
    if (msg_verbose>1) {
 
330
        msg_info("%s: calling %s open routine",myname,dict_type);
 
331
    }
272
332
    if ((dict = dp->open(dict_name, open_flags, dict_flags)) == 0)
273
333
        msg_fatal("opening %s:%s %m", dict_type, dict_name);
274
334
    if (msg_verbose)
276
336
    return (dict);
277
337
}
278
338
 
 
339
dict_mkmap_func_t dict_mkmap_func(const char *dict_type)
 
340
{
 
341
    char   *myname="dict_mkmap_func";
 
342
    struct stat st;
 
343
    LIB_FN fn[2];
 
344
    dict_mkmap_func_t mkmap;
 
345
    DLINFO *dl;
 
346
#ifndef NO_DYNAMIC_MAPS
 
347
    if (!dict_dlinfo)
 
348
        msg_fatal("dlinfo==NULL");
 
349
    dl=dict_open_dlfind(dict_type);
 
350
    if (!dl)
 
351
        msg_fatal("%s: unsupported dictionary type: %s:  Is the postfix-%s package installed?", myname, dict_type, dict_type);
 
352
    if (stat(dl->soname,&st) < 0) {
 
353
        msg_fatal("%s: unsupported dictionary type: %s (%s not found.  Is the postfix-%s package installed?)",
 
354
            myname, dict_type, dl->soname, dict_type);
 
355
    }
 
356
    fn[0].name = dl->mkmapfunc;
 
357
    fn[0].ptr  = (void**)&mkmap;
 
358
    fn[1].name = NULL;
 
359
    load_library_symbols(dl->soname, fn, NULL);
 
360
    return mkmap;
 
361
#else
 
362
    return (void(*)())NULL;
 
363
#endif
 
364
}
 
365
 
279
366
/* dict_open_register - register dictionary type */
280
367
 
281
368
void    dict_open_register(const char *type,
302
389
    HTABLE_INFO **ht;
303
390
    DICT_OPEN_INFO *dp;
304
391
    ARGV   *mapnames;
 
392
#ifndef NO_DYNAMIC_MAPS
 
393
    DLINFO *dlp;
 
394
#endif
305
395
 
306
396
    if (dict_open_hash == 0)
307
397
        dict_open_init();
310
400
        dp = (DICT_OPEN_INFO *) ht[0]->value;
311
401
        argv_add(mapnames, dp->type, ARGV_END);
312
402
    }
 
403
#ifndef NO_DYNAMIC_MAPS
 
404
    if (!dict_dlinfo)
 
405
        msg_fatal("dlinfo==NULL");
 
406
    for (dlp=dict_dlinfo; dlp->pattern; dlp++) {
 
407
        argv_add(mapnames, dlp->pattern, ARGV_END);
 
408
    }
 
409
#endif
313
410
    myfree((char *) ht_info);
314
411
    argv_terminate(mapnames);
315
412
    return mapnames;
316
413
}
317
414
 
 
415
#ifndef NO_DYNAMIC_MAPS
 
416
#define STREQ(x,y) (x == y || (x[0] == y[0] && strcmp(x,y) == 0))
 
417
 
 
418
void dict_open_dlinfo(const char *path)
 
419
{
 
420
    char    *myname="dict_open_dlinfo";
 
421
    VSTREAM *conf_fp=vstream_fopen(path,O_RDONLY,0);
 
422
    VSTRING *buf = vstring_alloc(100);
 
423
    char    *cp;
 
424
    ARGV    *argv;
 
425
    MVECT    vector;
 
426
    int      nelm=0;
 
427
    int      linenum=0;
 
428
 
 
429
    dict_dlinfo=(DLINFO*)mvect_alloc(&vector,sizeof(DLINFO),3,NULL,NULL);
 
430
 
 
431
    if (!conf_fp) {
 
432
        msg_warn("%s: cannot open %s.  No dynamic maps will be allowed.",
 
433
                myname, path);
 
434
    } else {
 
435
        while (vstring_get_nonl(buf,conf_fp) != VSTREAM_EOF) {
 
436
            cp = vstring_str(buf);
 
437
            linenum++;
 
438
            if (*cp == '#' || *cp == '\0')
 
439
                continue;
 
440
            argv = argv_split(cp, " \t");
 
441
            if (argv->argc != 3 && argv->argc != 4) {
 
442
                msg_fatal("%s: Expected \"pattern .so-name open-function [mkmap-function]\" at line %d",
 
443
                          myname, linenum);
 
444
            }
 
445
            if (STREQ(argv->argv[0],"*")) {
 
446
                msg_warn("%s: wildcard dynamic map entry no longer supported.",
 
447
                          myname);
 
448
                continue;
 
449
            }
 
450
            if (argv->argv[1][0] != '/') {
 
451
                msg_fatal("%s: .so name must begin with a \"/\" at line %d",
 
452
                          myname, linenum);
 
453
            }
 
454
            if (nelm >= vector.nelm) {
 
455
                dict_dlinfo=(DLINFO*)mvect_realloc(&vector,vector.nelm+3);
 
456
            }
 
457
            dict_dlinfo[nelm].pattern  = mystrdup(argv->argv[0]);
 
458
            dict_dlinfo[nelm].soname   = mystrdup(argv->argv[1]);
 
459
            dict_dlinfo[nelm].openfunc = mystrdup(argv->argv[2]);
 
460
            if (argv->argc==4)
 
461
                dict_dlinfo[nelm].mkmapfunc = mystrdup(argv->argv[3]);
 
462
            else
 
463
                dict_dlinfo[nelm].mkmapfunc = NULL;
 
464
            nelm++;
 
465
            argv_free(argv);
 
466
        }
 
467
    }
 
468
    if (nelm >= vector.nelm) {
 
469
        dict_dlinfo=(DLINFO*)mvect_realloc(&vector,vector.nelm+1);
 
470
    }
 
471
    dict_dlinfo[nelm].pattern  = NULL;
 
472
    dict_dlinfo[nelm].soname   = NULL;
 
473
    dict_dlinfo[nelm].openfunc = NULL;
 
474
    dict_dlinfo[nelm].mkmapfunc = NULL;
 
475
    if (conf_fp)
 
476
        vstream_fclose(conf_fp);
 
477
    vstring_free(buf);
 
478
}
 
479
 
 
480
static DLINFO *dict_open_dlfind(const char *type)
 
481
{
 
482
    DLINFO *dp;
 
483
 
 
484
    if (!dict_dlinfo)
 
485
        return NULL;
 
486
 
 
487
    for (dp=dict_dlinfo; dp->pattern; dp++) {
 
488
        if (STREQ(dp->pattern,type))
 
489
            return dp;
 
490
    }
 
491
    return NULL;
 
492
}
 
493
 
 
494
#endif /* !NO_DYNAMIC_MAPS */
 
495
 
318
496
#ifdef TEST
319
497
 
320
498
 /*