File: lookup_file.c

package info (click to toggle)
autofs 0.3.14-6
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 308 kB
  • ctags: 223
  • sloc: ansic: 2,132; sh: 288; makefile: 128
file content (211 lines) | stat: -rw-r--r-- 4,726 bytes parent folder | download
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * lookup_file.c
 *
 * Module for Linux automountd to access a plain file automount map
 */

#include <stdio.h>
#include <malloc.h>
#include <errno.h>
#include <unistd.h>
#include <ctype.h>
#include <syslog.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>

#define MODULE_LOOKUP
#include "automount.h"

#define MAPFMT_DEFAULT "sun"

#define MODPREFIX "lookup(file): "

#define MAPENT_MAX_LEN 4095

struct lookup_context {
  char *mapname;
  struct parse_mod *parse;
};

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

int lookup_init(char *mapfmt, int argc, char **argv, void **context)
{
  struct lookup_context *ctxt;

  if ( !(*context = ctxt = malloc(sizeof(struct lookup_context))) ) {
    syslog(LOG_CRIT, MODPREFIX "malloc: %m");
    return 1;
  }
  if ( argc < 1 ) {
    syslog(LOG_CRIT, MODPREFIX "No map name");
    return 1;
  }
  ctxt->mapname = argv[0];

  if (ctxt->mapname[0] != '/') {
    syslog(LOG_CRIT, MODPREFIX "file map %s is not an absolute pathname",
	   ctxt->mapname);
    return 1;
  }

  if ( access(ctxt->mapname, R_OK) ) {
    syslog(LOG_WARNING, MODPREFIX "file map %s missing or not readable",
	   ctxt->mapname);
  }

  if ( !mapfmt )
    mapfmt = MAPFMT_DEFAULT;

  return !(ctxt->parse = open_parse(mapfmt,MODPREFIX,argc-1,argv+1));
}

int lookup_mount(char *root, char *name, int name_len, void *context)
{
  struct lookup_context *ctxt = (struct lookup_context *) context;
  int ch, nch;
  char mapent[MAPENT_MAX_LEN+1], *p, *nptr;
  int mapent_len;
  FILE *f;
  enum {
    st_begin, st_compare, st_star, st_badent, st_entspc, st_getent
  } state;
  enum { got_nothing, got_star, got_real } getting, gotten;
  enum { esc_none, esc_char, esc_val } escape;

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

  chdir("/");			/* If this is not here the filesystem stays
				   busy, for some reason... */
  f = fopen(ctxt->mapname, "r");
  if ( !f ) {
    syslog(LOG_ERR, MODPREFIX "could not open map file %s", ctxt->mapname);
    return 1;
  }
  
  state = st_begin;
  gotten = got_nothing;

  /* Shut up gcc */
  p = nptr = NULL;
  mapent_len = 0;
  getting = got_nothing;
  escape = esc_none;

  /* This is ugly.  We can't just remove \ escape sequences in the value
     portion of an entry, because the parsing routine may need it. */

  while ( (ch = getc(f)) != EOF ) {
    switch ( escape ) {
    case esc_none:
      if ( ch == '\\' ) {
	/* Handle continuation lines */
	if ( (nch = getc(f)) == '\n' )
	  goto next_char;
	ungetc(nch,f);
	escape = esc_char;
      }
      break;

    case esc_char:
      escape = esc_val;
      break;

    case esc_val:
      escape = esc_none;
      break;
    }     

    switch(state) {
    case st_begin:
      if ( isspace(ch) && !escape )
	;
      else if ( escape == esc_char )
	;
      else if ( ch == '#' && !escape )
	state = st_badent;
      else if ( (char)ch == name[0] ) {
	state = st_compare;
	nptr = name+1;
      }	else if ( ch == '*' && !escape )
	state = st_star;
      else
	state = st_badent;
      break;

    case st_compare:
      if ( ch == '\n' )
	state = st_begin;
      else if ( isspace(ch) && !*nptr && !escape ) {
	getting = got_real;
	state = st_entspc;
      } else if ( escape == esc_char )
	;
      else if ( (char)ch != *(nptr++) )
	state = st_badent;
      break;

    case st_star:
      if ( ch == '\n' )
	state = st_begin;
      else if ( isspace(ch) && gotten < got_star && !escape ) {
	getting = got_star;
	state = st_entspc;
      } else if ( escape != esc_char )
	state = st_badent;
      break;

    case st_badent:
      if ( ch == '\n' )
	state = st_begin;
      break;

    case st_entspc:
      if ( ch == '\n' )
	state = st_begin;
      else if ( !isspace(ch) || escape ) {
	state = st_getent;
	p = mapent;
	gotten = getting;
	*(p++) = ch;
	mapent_len = 1;
      }
      break;

    case st_getent:
      if ( ch == '\n' ) {
	state = st_begin;
	if ( gotten == got_real )
	  goto got_it;		/* No point in parsing the rest of the file */
      } else if ( mapent_len < MAPENT_MAX_LEN ) {
	mapent_len++;
	*(p++) = ch;
      }
      break;
    }
  next_char:			/* End of loop, since we can't continue;
				   inside a switch */
  }
  
got_it:
  fclose(f);

  if ( gotten == got_nothing ) {
    syslog(LOG_NOTICE, MODPREFIX "lookup for %s failed", name);
    return 1;			/* Didn't found anything */
  }
  *p = '\0';			/* Null-terminate */

  syslog(LOG_DEBUG, MODPREFIX "%s -> %s", name, mapent);

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

int lookup_done(void *context)
{
  struct lookup_context *ctxt = (struct lookup_context *) context;
  int rv = close_parse(ctxt->parse);
  free(ctxt);
  return rv;
}