aboutsummaryrefslogtreecommitdiffstats
path: root/modules/lookup_multi.c
blob: 062e4d73d414ed2c1c483cfd18b1034f7e90ef75 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#ident "$Id$"
/* ----------------------------------------------------------------------- *
 *   
 *  lookup_multi.c - module for Linux automount to seek multiple lookup
 *                   methods in succession
 *
 *   Copyright 1999-2000 Transmeta Corporation - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
 *   USA; either version 2 of the License, or (at your option) any later
 *   version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>

#define MODULE_LOOKUP
#include "automount.h"

#define MODPREFIX "lookup(multi): "

#define MAPENT_MAX_LEN 4095

struct module_info {
  int argc;
  const char * const *argv;
  struct lookup_mod *mod;
};

struct lookup_context {
  int n;
  const char **argl;
  struct module_info *m;
};

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

int lookup_init(const char *my_mapfmt, int argc, const char * const *argv,
		void **context)
{
  struct lookup_context *ctxt;
  char *map, *mapfmt;
  int i, j, an;

  if ( !(*context = ctxt = malloc(sizeof(struct lookup_context))) )
    goto nomem;
  memset(ctxt, 0, sizeof(struct lookup_context));

  if ( argc < 1 ) {
    syslog(LOG_CRIT, MODPREFIX "No map list");
    return 1;
  }
  ctxt->n = 1;			/* Always at least one map */
  for ( i = 0 ; i < argc ; i++ ) {
    if ( !strcmp(argv[i], "--") ) /* -- separates maps */
      ctxt->n++;
  }

  if ( !(ctxt->m = malloc(ctxt->n*sizeof(struct module_info))) ||
       !(ctxt->argl = malloc((argc+1)*sizeof(const char *))) )
    goto nomem;
  memset(ctxt->m, 0, ctxt->n*sizeof(struct module_info));

  memcpy(ctxt->argl, argv, (argc+1)*sizeof(const char *));

  for ( i = j = an = 0 ; ctxt->argl[an] ; an++ ) {
    if ( ctxt->m[i].argc == 0 )
      ctxt->m[i].argv = &ctxt->argl[an];
    if ( !strcmp(ctxt->argl[an], "--") ) {
      ctxt->argl[an] = NULL;
      i++;
    } else {
      ctxt->m[i].argc++;
    }
  }

  for ( i = 0 ; i < ctxt->n ; i++ ) {
    if ( !ctxt->m[i].argv[0] ) {
      syslog(LOG_CRIT, MODPREFIX "missing module name");
      return 1;
    }
    map = strdup(ctxt->m[i].argv[0]);
    if ( !map )
      goto nomem;

    if ( (mapfmt = strchr(map, ',')) )
      *(mapfmt++) = '\0';
    
    if ( !(ctxt->m[i].mod = open_lookup(map, MODPREFIX,
					mapfmt ? mapfmt : my_mapfmt,
					ctxt->m[i].argc - 1,
					ctxt->m[i].argv + 1)) )
      return 1;
  }

  *context = ctxt;
  return 0;

 nomem:
  syslog(LOG_CRIT, MODPREFIX "malloc: %m");
  return 1;
}

int lookup_mount(const char *root, const char *name, int name_len,
		 void *context)
{
  struct lookup_context *ctxt = (struct lookup_context *) context;
  int i;

  for ( i = 0 ; i < ctxt->n ; i++ ) {
    if ( ctxt->m[i].mod->lookup_mount(root,name,name_len,
				      ctxt->m[i].mod->context) == 0 )
      return 0;
  }
  return 1;			/* No module succeeded */
}

int lookup_done(void *context)
{
  struct lookup_context *ctxt = (struct lookup_context *) context;
  int i, rv = 0;

  for ( i = 0 ; i < ctxt->n ; i++ ) {
    rv = rv || close_lookup(ctxt->m[i].mod);
  }

  free(ctxt->argl);
  free(ctxt->m);
  free(ctxt);

  return rv;
}