2 * Copyright (c) 1997-1998 Erez Zadok
3 * Copyright (c) 1989 Jan-Simon Pendry
4 * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1989 The Regents of the University of California.
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgment:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * $Id: opts.c,v 1.2 1998/12/27 06:24:48 ezk Exp $
47 #endif /* HAVE_CONFIG_H */
54 #define NLEN 16 /* Length of longest option name (conservative) */
55 #define S(x) (x) , (sizeof(x)-1)
57 * The BUFSPACE macros checks that there is enough space
58 * left in the expansion buffer. If there isn't then we
59 * give up completely. This is done to avoid crashing the
60 * automounter itself (which would be a bad thing to do).
62 #define BUFSPACE(ep, len) (((ep) + (len)) < expbuf+MAXPATHLEN)
67 typedef int (*IntFuncPtr) (char *);
68 typedef struct opt_apply opt_apply;
69 enum vs_opt { SelEQ, SelNE, VarAss };
75 char *name; /* Name of the option */
76 int nlen; /* Length of option name */
77 char **optp; /* Pointer to option value string */
78 char **sel_p; /* Pointer to selector value string */
79 int (*fxn_p)(char *); /* Pointer to boolean function */
80 int case_insensitive; /* How to do selector comparisons */
96 static int f_in_network(char *);
97 static int f_netgrp(char *);
98 static int f_exists(char *);
99 static int f_false(char *);
100 static int f_true(char *);
105 static struct am_opts fs_static; /* copy of the options to play with */
106 static char NullStr[] = "<NULL>";
107 static char nullstr[] = "";
108 static char *opt_dkey = NullStr;
109 static char *opt_host = nullstr; /* XXX: was the global hostname */
110 static char *opt_hostd = hostd;
111 static char *opt_key = nullstr;
112 static char *opt_keyd = nullstr;
113 static char *opt_map = nullstr;
114 static char *opt_path = nullstr;
115 static char *vars[8];
119 * Options in something corresponding to frequency of use so that
120 * first-match algorithm is sped up.
122 static struct opt opt_fields[] = {
124 Option str. Selector str. boolean fxn. flags */
126 &fs_static.opt_opts, 0, 0, FALSE },
128 0, &opt_host, 0, TRUE },
130 0, &opt_hostd, 0, TRUE },
132 &fs_static.opt_type, 0, 0, FALSE },
134 &fs_static.opt_rhost, 0, 0, TRUE },
136 &fs_static.opt_rfs, 0, 0, FALSE },
138 &fs_static.opt_fs, 0, 0, FALSE },
140 0, &opt_key, 0, FALSE },
142 0, &opt_map, 0, FALSE },
144 &fs_static.opt_sublink, 0, 0, FALSE },
146 0, &gopt.arch, 0, TRUE },
148 &fs_static.opt_dev, 0, 0, FALSE },
150 &fs_static.opt_pref, 0, 0, FALSE },
152 &fs_static.opt_autopref,0, 0, FALSE },
154 0, &opt_path, 0, FALSE },
156 0, &gopt.auto_dir, 0, FALSE },
158 &fs_static.opt_delay, 0, 0, FALSE },
160 0, &hostdomain, 0, TRUE },
162 0, &gopt.karch, 0, TRUE },
164 0, &gopt.cluster, 0, TRUE },
166 0, 0, f_in_network, TRUE },
168 0, 0, f_in_network, TRUE },
170 0, 0, f_in_network, TRUE },
172 0, &endian, 0, TRUE },
174 0, &gopt.op_sys, 0, TRUE },
176 0, &gopt.op_sys_ver, 0, TRUE },
178 &fs_static.opt_remopts, 0, 0, FALSE },
180 &fs_static.opt_mount, 0, 0, FALSE },
182 &fs_static.opt_unmount, 0, 0, FALSE },
184 &fs_static.opt_cache, 0, 0, FALSE },
186 &fs_static.opt_user, 0, 0, FALSE },
188 &fs_static.opt_group, 0, 0, FALSE },
190 0, &opt_dkey, 0, FALSE },
192 0, &opt_keyd, 0, FALSE },
194 &fs_static.opt_maptype, 0, 0, FALSE },
196 &fs_static.opt_cachedir, 0, 0, FALSE },
198 &fs_static.opt_addopts, 0, 0, FALSE },
200 &vars[0], 0, 0, FALSE },
202 &vars[1], 0, 0, FALSE },
204 &vars[2], 0, 0, FALSE },
206 &vars[3], 0, 0, FALSE },
208 &vars[4], 0, 0, FALSE },
210 &vars[5], 0, 0, FALSE },
212 &vars[6], 0, 0, FALSE },
214 &vars[7], 0, 0, FALSE },
215 { 0, 0, 0, 0, 0, FALSE },
218 static struct functable functable[] = {
219 { "in_network", f_in_network },
220 { "netgrp", f_netgrp },
221 { "exists", f_exists },
222 { "false", f_false },
228 * Specially expand the remote host name first
230 static opt_apply rhost_expansion[] =
232 {&fs_static.opt_rhost, "${host}"},
237 * List of options which need to be expanded
238 * Note that the order here _may_ be important.
240 static opt_apply expansions[] =
242 {&fs_static.opt_sublink, 0},
243 {&fs_static.opt_rfs, "${path}"},
244 {&fs_static.opt_fs, "${autodir}/${rhost}${rfs}"},
245 {&fs_static.opt_opts, "rw"},
246 {&fs_static.opt_remopts, "${opts}"},
247 {&fs_static.opt_mount, 0},
248 {&fs_static.opt_unmount, 0},
249 {&fs_static.opt_cachedir, 0},
250 {&fs_static.opt_addopts, 0},
255 * List of options which need to be free'ed before re-use
257 static opt_apply to_free[] =
259 {&fs_static.fs_glob, 0},
260 {&fs_static.fs_local, 0},
261 {&fs_static.fs_mtab, 0},
262 {&fs_static.opt_sublink, 0},
263 {&fs_static.opt_rfs, 0},
264 {&fs_static.opt_fs, 0},
265 {&fs_static.opt_rhost, 0},
266 {&fs_static.opt_opts, 0},
267 {&fs_static.opt_remopts, 0},
268 {&fs_static.opt_mount, 0},
269 {&fs_static.opt_unmount, 0},
270 {&fs_static.opt_cachedir, 0},
271 {&fs_static.opt_addopts, 0},
285 * expand backslash escape sequences
292 if ((*p)[1] == '\0') {
293 plog(XLOG_USER, "Empty backslash escape");
301 c = '\007'; /* Bell */
304 c = '\010'; /* Backspace */
307 c = '\011'; /* Horizontal Tab */
310 c = '\012'; /* New Line */
313 c = '\013'; /* Vertical Tab */
316 c = '\014'; /* Form Feed */
319 c = '\015'; /* Carriage Return */
322 c = '\033'; /* Escape */
335 for (cnt = 0, val = 0; cnt < 3; cnt++) {
337 if (ch < '0' || ch > '7') {
341 val = (val << 3) | (ch - '0');
344 if ((val & 0xffffff00) != 0)
346 "Too large character constant %u\n",
365 * Skip to next option in the string
375 while (*cp && *cp != ';') {
380 for (cp++; *cp && *cp != '"'; cp++)
382 *dp++ = backslash(&cp);
393 * Skip past any remaining ';'s
399 * If we have a zero length string
400 * and there are more fields, then
401 * parse the next one. This allows
402 * sequences of empty fields.
415 * These routines add a new style of selector; function-style boolean
416 * operators. To add new ones, just define functions as in true, false,
417 * exists (below) and add them to the functable, above.
419 * Usage example: Some people have X11R5 local, some go to a server. I do
422 * * exists(/usr/pkg/${key});type:=link;fs:=/usr/pkg/${key} || \
423 * -type:=nfs;rfs=/usr/pkg/${key} \
427 * -Rens Troost <rens@imsi.com>
430 functable_lookup(char *key)
432 struct functable *fp;
434 for (fp = functable; fp->name; fp++)
435 if (FSTREQ(fp->name, key))
437 return (IntFuncPtr) NULL;
442 eval_opts(char *opts, char *mapkey)
445 * Fill in the global structure fs_static by
446 * cracking the string opts. opts may be
447 * scribbled on at will.
453 * For each user-specified option
455 while (*(f = opt(&o))) {
457 enum vs_opt vs_opt = VarAss;
458 char *eq = strchr(f, '=');
463 if (!eq || eq[1] == '\0' || eq == f) {
465 * No value, is it a function call?
467 char *arg = strchr(f, '(');
469 if (!arg || arg[1] == '\0' || arg == f) {
473 plog(XLOG_USER, "key %s: No value component in \"%s\"", mapkey, f);
477 /* null-terminate the argument */
479 fx = strchr(arg, ')');
480 if (!arg || fx == arg) {
481 plog(XLOG_USER, "key %s: Malformed function in \"%s\"", mapkey, f);
487 * look up f in functable and pass it arg.
488 * func must return 0 on failure, and 1 on success.
490 if ((func = functable_lookup(f))) {
491 if (!(*func) (arg)) {
495 } else if (NSTREQ(f, "!", 1) && (func = functable_lookup(&f[1]))) {
496 /* then this is a negated prefixed function such as "!exists" */
497 plog(XLOG_USER, "executing negated function %s", &f[1]);
503 plog(XLOG_USER, "key %s: unknown function \"%s\"", mapkey, f);
510 * Check what type of operation is happening
515 if (eq[-1] == '!') { /* != */
519 } else if (eq[-1] == ':') { /* := */
523 } else if (eq[1] == '=') { /* == */
527 } else if (eq[1] == '!') { /* =! */
534 * For each recognized option
536 for (op = opt_fields; op->name; op++) {
538 * Check whether they match
540 if (FSTREQ(op->name, f)) {
545 if ((selok = (op->sel_p != NULL))) {
546 if (op->case_insensitive) {
547 selok = (STRCEQ(*op->sel_p, opt) == (vs_opt == SelNE));
549 selok = (STREQ(*op->sel_p, opt) == (vs_opt == SelNE));
553 plog(XLOG_MAP, "key %s: map selector %s (=%s) did not %smatch %s",
557 vs_opt == SelNE ? "mis" : "",
561 /* check if to apply a function */
563 ((*op->fxn_p)(opt) == (vs_opt == SelNE))) {
564 plog(XLOG_MAP, "key %s: map function %s did not %smatch %s",
567 vs_opt == SelNE ? "mis" : "",
575 plog(XLOG_USER, "key %s: Can't assign to a selector (%s)",
582 } /* end of "switch (vs_opt)" statement */
583 break; /* break out of for loop */
588 plog(XLOG_USER, "key %s: Unrecognized key/option \"%s\"", mapkey, f);
596 * Skip to next option in the string, but don't scribble over the string.
597 * However, *p gets repointed to the start of the next string past ';'.
600 opt_no_scribble(char **p)
607 while (*cp && *cp != ';') {
613 while (*cp && *cp != '\"')
623 * Skip past any remaining ';'s
629 * If we have a zero length string
630 * and there are more fields, then
631 * parse the next one. This allows
632 * sequences of empty fields.
643 * Strip any selectors from a string. Selectors are all assumed to be
644 * first in the string. This is used for the new /defaults method which will
645 * use selectors as well.
648 strip_selectors(char *opts, char *mapkey)
651 * Fill in the global structure fs_static by
652 * cracking the string opts. opts may be
653 * scribbled on at will.
660 * Scan options. Note that the opt() function scribbles on the opt string.
662 while (*(f = opt_no_scribble(&o))) {
663 enum vs_opt vs_opt = VarAss;
664 char *eq = strchr(f, '=');
666 if (!eq || eq[1] == '\0' || eq == f) {
668 * No option or assignment? Return as is.
670 plog(XLOG_USER, "key %s: No option or assignment in \"%s\"", mapkey, f);
674 * Check what type of operation is happening
679 if (eq[-1] == '!') { /* != */
681 } else if (eq[-1] == ':') { /* := */
683 } else if (eq[1] == '=') { /* == */
685 } else if (eq[1] == '!') { /* =! */
691 /* Skip this selector, maybe there's another one following it */
692 plog(XLOG_USER, "skipping selector to \"%s\"", o);
693 /* store previous match. it may have been the first assignment */
698 /* found the first assignment, return the string starting with it */
700 dlog("found first assignment past selectors \"%s\"", o);
706 /* return the same string by default. should not happen. */
711 /*****************************************************************************
712 *** BOOLEAN FUNCTIONS (return 0 if false, 1 if true): ***
713 *****************************************************************************/
715 /* test if arg is any of this host's network names or numbers */
717 f_in_network(char *arg)
724 status = is_network_member(arg);
726 plog(XLOG_USER, "%s is %son a local network",
727 arg, (status ? "" : "not "));
733 /* test if this host is in netgroup (arg) */
739 status = innetgr(arg, opt_host, NULL, NULL);
741 plog(XLOG_USER, "netgrp = %s status = %d host = %s", arg, status, opt_host);
747 /* test if file (arg) exists via lstat */
753 if (lstat(arg, &buf) < 0)
780 free_op(opt_apply *p, int b)
790 * Normalize slashes in the string.
793 normalize_slash(char *p)
795 char *f = strchr(p, '/');
801 /* assert(*f == '/'); */
802 if (f == f0 && f[0] == '/' && f[1] == '/') {
803 /* copy double slash iff first */
807 /* copy a single / across */
811 /* assert(f[-1] == '/'); */
812 /* skip past more /'s */
816 /* assert(*f != '/'); */
817 /* keep copying up to next / */
818 while (*f && *f != '/') {
822 /* assert(*f == 0 || *f == '/'); */
825 *t = 0; /* derived from fix by Steven Glassman */
831 * Macro-expand an option. Note that this does not
832 * handle recursive expansions. They will go badly wrong.
833 * If sel is true then old expand selectors, otherwise
834 * don't expand selectors.
837 expand_op(opt_apply *p, int sel_p)
839 static char expand_error[] = "No space to expand \"%s\"";
840 char expbuf[MAXPATHLEN + 1];
847 char *cp_orig = *p->opt;
850 while ((dp = strchr(cp, '$'))) {
853 * First copy up to the $
858 if (BUFSPACE(ep, len)) {
859 strncpy(ep, cp, len);
862 plog(XLOG_ERROR, expand_error, *p->opt);
870 if (BUFSPACE(ep, 1)) {
873 plog(XLOG_ERROR, expand_error, *p->opt);
876 } else if (ch == '{') {
879 E_All, E_Dir, E_File, E_Domain, E_Host
884 char *br_p = strchr(cp, '}');
894 plog(XLOG_USER, "No closing '}' in \"%s\"", *p->opt);
900 * Figure out which part of the variable to grab.
904 * Just take the last component
909 } else if (br_p[-1] == '/') {
911 * Take all but the last component
915 } else if (*cp == '.') {
922 } else if (br_p[-1] == '.') {
936 * Truncate if too long. Since it won't
937 * match anyway it doesn't matter that
938 * it has been cut short.
944 * Put the string into another buffer so
945 * we can do comparisons.
947 strncpy(nbuf, cp, len);
956 * Search the option array
958 for (op = opt_fields; op->name; op++) {
962 if (len == op->nlen && STREQ(op->name, nbuf)) {
966 * Found expansion. Copy
967 * the correct value field.
969 if (!(!op->sel_p == !sel_p)) {
971 * Copy the string across unexpanded
973 sprintf(xbuf, "${%s%s%s}",
974 todo == E_File ? "/" :
975 todo == E_Domain ? "." : "",
977 todo == E_Dir ? "/" :
978 todo == E_Host ? "." : "");
981 * Make sure expansion doesn't
985 } else if (op->sel_p) {
994 * ${/var} means take just the last part
995 * ${var/} means take all but the last part
996 * ${.var} means take all but first part
997 * ${var.} means take just the first part
998 * ${var} means take the whole lot
1000 int vlen = strlen(val);
1004 vptr = strrchr(val, '/');
1010 vptr = strrchr(val, '/');
1013 vlen = strlen(vptr);
1018 vptr = strchr(val, '.');
1021 vlen = strlen(vptr);
1028 vptr = strchr(val, '.');
1037 if (BUFSPACE(ep, vlen)) {
1041 plog(XLOG_ERROR, expand_error, *p->opt);
1046 * Done with this variable
1053 * Check that the search was successful
1057 * If it wasn't then scan the
1058 * environment for that name
1059 * and use any value found
1061 char *env = getenv(nbuf);
1064 int vlen = strlen(env);
1066 if (BUFSPACE(ep, vlen)) {
1070 plog(XLOG_ERROR, expand_error, *p->opt);
1075 plog(XLOG_DEBUG, "Environment gave \"%s\" -> \"%s\"", nbuf, env);
1078 plog(XLOG_USER, "Unknown sequence \"${%s}\"", nbuf);
1085 plog(XLOG_USER, "Unknown $ sequence in \"%s\"", *p->opt);
1091 * Handle common case - no expansion
1093 if (cp == *p->opt) {
1094 *p->opt = strdup(cp);
1097 * Finish off the expansion
1099 if (BUFSPACE(ep, strlen(cp))) {
1101 /* ep += strlen(ep); */
1103 plog(XLOG_ERROR, expand_error, *p->opt);
1107 * Save the expansion
1109 *p->opt = strdup(expbuf);
1112 normalize_slash(*p->opt);
1116 plog(XLOG_DEBUG, "Expansion of \"%s\"...", cp_orig);
1117 plog(XLOG_DEBUG, "... is \"%s\"", *p->opt);
1124 * Wrapper for expand_op
1127 expand_opts(opt_apply *p, int sel_p)
1130 expand_op(p, sel_p);
1131 } else if (p->val) {
1133 * Do double expansion, remembering
1134 * to free the string from the first
1137 char *s = *p->opt = expand_key(p->val);
1138 expand_op(p, sel_p);
1145 * Apply a function to a list of options
1148 apply_opts(void (*op) (opt_apply *, int), opt_apply ppp[], int b)
1152 for (pp = ppp; pp->opt; pp++)
1158 * Free the option table
1161 free_opts(am_opts *fo)
1164 * Copy in the structure we are playing with
1169 * Free previously allocated memory
1171 apply_opts(free_op, to_free, FALSE);
1179 expand_key(char *key)
1185 expand_opts(&oa, TRUE);
1192 * Remove trailing /'s from a string
1193 * unless the string is a single / (Steven Glassman)
1194 * or unless it is two slashes // (Kevin D. Bond)
1200 char *sl = s + strlen(s);
1202 while (*--sl == '/' && sl > s)
1209 eval_fs_opts(am_opts *fo, char *opts, char *g_opts, char *path, char *key, char *map)
1216 * Clear out the option table
1218 memset((voidp) &fs_static, 0, sizeof(fs_static));
1219 memset((voidp) vars, 0, sizeof(vars));
1220 memset((voidp) fo, 0, sizeof(*fo));
1223 opt_host = (char *) am_get_hostname();
1226 * Set key, map & path before expansion
1232 opt_dkey = strchr(key, '.');
1237 opt_keyd = strnsave(key, opt_dkey - key);
1239 if (*opt_dkey == '\0') /* check for 'host.' */
1244 * Expand global options
1246 fs_static.fs_glob = expand_key(g_opts);
1249 * Expand local options
1251 fs_static.fs_local = expand_key(opts);
1254 * Expand default (global) options
1256 if (!eval_opts(fs_static.fs_glob, key))
1260 * Expand local options
1262 if (ok && !eval_opts(fs_static.fs_local, key))
1266 * Normalize remote host name.
1267 * 1. Expand variables
1268 * 2. Normalize relative to host tables
1269 * 3. Strip local domains from the remote host
1270 * name before using it in other expansions.
1271 * This makes mount point names and other things
1272 * much shorter, while allowing cross domain
1273 * sharing of mount maps.
1275 apply_opts(expand_opts, rhost_expansion, FALSE);
1276 if (ok && fs_static.opt_rhost && *fs_static.opt_rhost)
1277 host_normalize(&fs_static.opt_rhost);
1280 * Macro expand the options.
1281 * Do this regardless of whether we are accepting
1282 * this mount - otherwise nasty things happen
1283 * with memory allocation.
1285 apply_opts(expand_opts, expansions, FALSE);
1288 * Strip trailing slashes from local pathname...
1290 deslashify(fs_static.opt_fs);
1293 * ok... copy the data back out.
1298 * Clear defined options
1300 if (opt_keyd != key && opt_keyd != nullstr)
1304 opt_key = opt_map = opt_path = nullstr;