aboutsummaryrefslogtreecommitdiffstats
path: root/modules/lookup_hesiod.c
blob: 231377ea4416e0e25fe990f6122f2051001eeeed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#ident "$Id$"
/*
 * lookup_hesiod.c
 *
 * Module for Linux automountd to access automount maps in hesiod filsys
 * entries.
 *
 */

#include <sys/types.h>
#include <ctype.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <hesiod.h>

#define MODULE_LOOKUP
#include "automount.h"

#define MAPFMT_DEFAULT "hesiod"

#define MODPREFIX "lookup(hesiod): "
#define HESIOD_LEN 512

struct lookup_context {
  struct parse_mod *parser;
};

int lookup_version = AUTOFS_LOOKUP_VERSION; /* Required by protocol */

/* This initializes a context (persistent non-global data) for queries to
   this module. */
int lookup_init(char *mapfmt, int argc, char **argv, void **context)
{
  struct lookup_context *ctxt = NULL;

  /* If we can't build a context, bail. */
  if((*context = ctxt = (struct lookup_context*) 
             malloc(sizeof(struct lookup_context))) == NULL) {
    syslog(LOG_CRIT, MODPREFIX "malloc: %m");
    return 1;
  }

  /* Initialize the resolver. */
  res_init();

  /* If a map type isn't explicitly given, parse it as hesiod entries. */
  if ( !mapfmt )
    mapfmt = MAPFMT_DEFAULT;

  /* Open the parser, if we can. */
  return !(ctxt->parser = open_parse(mapfmt,MODPREFIX,argc-1,argv+1));
}

/* Lookup and act on a filesystem name.  In this case, lookup the "filsys"
   record in hesiod.  If it's an AFS or NFS filesyste, parse it out.  If
   it's an ERR filesystem, it's an error message we should log.  Otherwise,
   assume it's something we know how to deal with already (generic). */
int lookup_mount(char *root, char *name, int name_len, void *context)
{
  char **hes_result, mapent[HESIOD_LEN + 1];
  struct lookup_context *ctxt = (struct lookup_context *) context;

  syslog(LOG_DEBUG, MODPREFIX "looking up root=\"%s\", name=\"%s\"",
        root, name);

  chdir("/");                  /* If this is not here the filesystem stays
                                  busy, for some reason... */

  hes_result = hes_resolve(name, "filsys");

  if(hes_result) {
    strncpy(mapent, hes_result[0], sizeof(mapent) - 1);
    syslog(LOG_DEBUG, MODPREFIX "lookup for \"%s\" gave \"%s\"",
           name, mapent);
    free(hes_result);
  }

  return ctxt->parser->parse_mount(root,name,name_len,mapent,ctxt->parser->context);
}

/* This destroys a context for queries to this module.  It releases the parser
   structure (unloading the module) and frees the memory used by the context. */
int lookup_done(void *context)
{
  struct lookup_context *ctxt = (struct lookup_context *) context;
  int rv = close_parse(ctxt->parser);
  free(ctxt);
  return rv;
}