xref: /netbsd-src/external/bsd/am-utils/dist/libamu/mtab.c (revision 8bae5d409deb915cf7c8f0539fae22ff2cb8a313)
1*8bae5d40Schristos /*	$NetBSD: mtab.c,v 1.1.1.3 2015/01/17 16:34:18 christos Exp $	*/
2a53f50b9Schristos 
3a53f50b9Schristos /*
4*8bae5d40Schristos  * Copyright (c) 1997-2014 Erez Zadok
5a53f50b9Schristos  * Copyright (c) 1989 Jan-Simon Pendry
6a53f50b9Schristos  * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
7a53f50b9Schristos  * Copyright (c) 1989 The Regents of the University of California.
8a53f50b9Schristos  * All rights reserved.
9a53f50b9Schristos  *
10a53f50b9Schristos  * This code is derived from software contributed to Berkeley by
11a53f50b9Schristos  * Jan-Simon Pendry at Imperial College, London.
12a53f50b9Schristos  *
13a53f50b9Schristos  * Redistribution and use in source and binary forms, with or without
14a53f50b9Schristos  * modification, are permitted provided that the following conditions
15a53f50b9Schristos  * are met:
16a53f50b9Schristos  * 1. Redistributions of source code must retain the above copyright
17a53f50b9Schristos  *    notice, this list of conditions and the following disclaimer.
18a53f50b9Schristos  * 2. Redistributions in binary form must reproduce the above copyright
19a53f50b9Schristos  *    notice, this list of conditions and the following disclaimer in the
20a53f50b9Schristos  *    documentation and/or other materials provided with the distribution.
21*8bae5d40Schristos  * 3. Neither the name of the University nor the names of its contributors
22a53f50b9Schristos  *    may be used to endorse or promote products derived from this software
23a53f50b9Schristos  *    without specific prior written permission.
24a53f50b9Schristos  *
25a53f50b9Schristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26a53f50b9Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27a53f50b9Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28a53f50b9Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29a53f50b9Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30a53f50b9Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31a53f50b9Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32a53f50b9Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33a53f50b9Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34a53f50b9Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35a53f50b9Schristos  * SUCH DAMAGE.
36a53f50b9Schristos  *
37a53f50b9Schristos  *
38a53f50b9Schristos  * File: am-utils/libamu/mtab.c
39a53f50b9Schristos  *
40a53f50b9Schristos  */
41a53f50b9Schristos 
42a53f50b9Schristos #ifdef HAVE_CONFIG_H
43a53f50b9Schristos # include <config.h>
44a53f50b9Schristos #endif /* HAVE_CONFIG_H */
45a53f50b9Schristos #include <am_defs.h>
46a53f50b9Schristos #include <amu.h>
47a53f50b9Schristos 
48a53f50b9Schristos 
49a53f50b9Schristos /*
50a53f50b9Schristos  * Firewall /etc/mtab entries
51a53f50b9Schristos  */
52a53f50b9Schristos void
mnt_free(mntent_t * mp)53a53f50b9Schristos mnt_free(mntent_t *mp)
54a53f50b9Schristos {
55a53f50b9Schristos   XFREE(mp->mnt_fsname);
56a53f50b9Schristos   XFREE(mp->mnt_dir);
57a53f50b9Schristos   XFREE(mp->mnt_type);
58a53f50b9Schristos   XFREE(mp->mnt_opts);
59a53f50b9Schristos 
60a53f50b9Schristos #ifdef HAVE_MNTENT_T_MNT_TIME
61a53f50b9Schristos # ifdef HAVE_MNTENT_T_MNT_TIME_STRING
62a53f50b9Schristos   XFREE(mp->mnt_time);
63a53f50b9Schristos # endif /* HAVE_MNTENT_T_MNT_TIME_STRING */
64a53f50b9Schristos #endif /* HAVE_MNTENT_T_MNT_TIME */
65a53f50b9Schristos 
66a53f50b9Schristos   XFREE(mp);
67a53f50b9Schristos }
68a53f50b9Schristos 
69a53f50b9Schristos 
70a53f50b9Schristos /*
71a53f50b9Schristos  * Discard memory allocated for mount list
72a53f50b9Schristos  */
73a53f50b9Schristos void
discard_mntlist(mntlist * mp)74a53f50b9Schristos discard_mntlist(mntlist *mp)
75a53f50b9Schristos {
76a53f50b9Schristos   mntlist *mp2;
77a53f50b9Schristos 
78a53f50b9Schristos   while ((mp2 = mp)) {
79a53f50b9Schristos     mp = mp->mnext;
80a53f50b9Schristos     if (mp2->mnt)
81a53f50b9Schristos       mnt_free(mp2->mnt);
82a53f50b9Schristos     XFREE(mp2);
83a53f50b9Schristos   }
84a53f50b9Schristos }
85a53f50b9Schristos 
86a53f50b9Schristos 
87a53f50b9Schristos /*
88a53f50b9Schristos  * Throw away a mount list
89a53f50b9Schristos  */
90a53f50b9Schristos void
free_mntlist(mntlist * mp)91a53f50b9Schristos free_mntlist(mntlist *mp)
92a53f50b9Schristos {
93a53f50b9Schristos   discard_mntlist(mp);
94a53f50b9Schristos #ifdef MOUNT_TABLE_ON_FILE
95a53f50b9Schristos   unlock_mntlist();
96a53f50b9Schristos #endif /* MOUNT_TABLE_ON_FILE */
97a53f50b9Schristos }
98a53f50b9Schristos 
99a53f50b9Schristos 
100a53f50b9Schristos /*
101a53f50b9Schristos  * Utility routine which returns a pointer to whatever follows an = in a
102a53f50b9Schristos  * string.  Returns null if = is not found in the string.
103a53f50b9Schristos  */
104a53f50b9Schristos char *
haseq(char * instr)105a53f50b9Schristos haseq(char *instr)
106a53f50b9Schristos {
107a53f50b9Schristos   if (instr) {
108a53f50b9Schristos     char *eq = strchr(instr, '=');
109a53f50b9Schristos     if (eq) return ++eq;
110a53f50b9Schristos   }
111a53f50b9Schristos   return NULL;
112a53f50b9Schristos }
113a53f50b9Schristos 
114a53f50b9Schristos 
115a53f50b9Schristos /*
116a53f50b9Schristos  * Utility routine which returns a pointer to whatever
117a53f50b9Schristos  * follows an = in a mount option.  Returns null if option
118a53f50b9Schristos  * doesn't exist or doesn't have an '='.  Won't fail for opt,foo=.
119a53f50b9Schristos  */
120a53f50b9Schristos char *
hasmnteq(mntent_t * mnt,char * opt)121a53f50b9Schristos hasmnteq(mntent_t *mnt, char *opt)
122a53f50b9Schristos {
123a53f50b9Schristos   if (mnt && opt) {		/* disallow null input pointers */
124a53f50b9Schristos     if ( *opt ) {		/* disallow the null string as an opt */
125a53f50b9Schristos       char *str = amu_hasmntopt(mnt, opt);
126a53f50b9Schristos       if ( str ) {		/* option was there */
127a53f50b9Schristos 	char *eq = str + strlen(opt); /* Look at char just after option */
128a53f50b9Schristos 	if (*eq == '=')		/* Is it '=' ? */
129a53f50b9Schristos 	  return ++eq;		/* If so, return pointer to remaining str */
130a53f50b9Schristos       }
131a53f50b9Schristos     }
132a53f50b9Schristos   }
133a53f50b9Schristos   return NULL;
134a53f50b9Schristos }
135a53f50b9Schristos 
136a53f50b9Schristos 
137a53f50b9Schristos /*
138a53f50b9Schristos  * Wrapper around hasmntvalerr(), which retains backwards compatibiliy with
139a53f50b9Schristos  * older use of hasmntval().
140a53f50b9Schristos  *
141a53f50b9Schristos  * XXX: eventually, all use of hasmntval() should be replaced with
142a53f50b9Schristos  * hasmntvalerr().
143a53f50b9Schristos  */
144a53f50b9Schristos int
hasmntval(mntent_t * mnt,char * opt)145a53f50b9Schristos hasmntval(mntent_t *mnt, char *opt)
146a53f50b9Schristos {
147a53f50b9Schristos   int err, val = 0;
148a53f50b9Schristos 
149a53f50b9Schristos   err = hasmntvalerr(mnt, opt, &val);
150a53f50b9Schristos   if (err)	   /* if there was an error (hasmntvalerr returned 1) */
151a53f50b9Schristos     return 0;	   /* redundant: val==0 above, but leave here for clarity */
152a53f50b9Schristos   /* otherwise there was no error */
153a53f50b9Schristos   return val;
154a53f50b9Schristos }
155a53f50b9Schristos 
156a53f50b9Schristos 
157a53f50b9Schristos /*
158a53f50b9Schristos  * Utility routine which determines the value of a numeric option in the
159a53f50b9Schristos  * mount options (such as port=%d), and fills in the value in the argument
160a53f50b9Schristos  * valp (argument won't be touched if no value is set, for example due to an
161a53f50b9Schristos  * error).
162a53f50b9Schristos  *
163a53f50b9Schristos  * Returns non-zero (1) on error; returns 0 on success.
164a53f50b9Schristos  *
165a53f50b9Schristos  * XXX: eventually, all use of hasmntval() should be replaced with
166a53f50b9Schristos  * hasmntvalerr().
167a53f50b9Schristos  */
168a53f50b9Schristos unsigned int
hasmntvalerr(mntent_t * mnt,char * opt,int * valp)169a53f50b9Schristos hasmntvalerr(mntent_t *mnt, char *opt, int *valp)
170a53f50b9Schristos {
171a53f50b9Schristos   char *str = amu_hasmntopt(mnt, opt);
172a53f50b9Schristos   int err = 1;		     /* 1 means no good value was set (an error) */
173a53f50b9Schristos   char *eq, *endptr;
174a53f50b9Schristos   long int i;
175a53f50b9Schristos 
176a53f50b9Schristos   /* exit if no option specificed */
177a53f50b9Schristos   if (!str) {
178a53f50b9Schristos     goto out;
179a53f50b9Schristos   }
180a53f50b9Schristos 
181a53f50b9Schristos   eq = hasmnteq(mnt, opt);
182a53f50b9Schristos 
183a53f50b9Schristos   if (!eq) {		  /* no argument to option ('=' sign was missing) */
184a53f50b9Schristos     plog(XLOG_MAP, "numeric option to \"%s\" missing", opt);
185a53f50b9Schristos     goto out;
186a53f50b9Schristos   }
187a53f50b9Schristos 
188a53f50b9Schristos   /* if got here, then we had an '=' after option name */
189a53f50b9Schristos   endptr = NULL;
190a53f50b9Schristos   i = strtol(eq, &endptr, 0); /* hex and octal allowed ;-) */
191a53f50b9Schristos   if (!endptr ||
192a53f50b9Schristos       (endptr != eq && (*endptr == ',' || *endptr == '\0'))) {
193a53f50b9Schristos       /*
194a53f50b9Schristos        * endptr set means strtol saw a non-digit.  If the non-digit is a
195a53f50b9Schristos        * comma, it's probably the start of the next option.  If the comma is
196a53f50b9Schristos        * the first char though, complain about it (foo=,bar is made
197a53f50b9Schristos        * noticeable by this).
198a53f50b9Schristos        *
199a53f50b9Schristos        * Similar reasoning for '\0' instead of comma, it's the end of the
200a53f50b9Schristos        * string.
201a53f50b9Schristos        */
202a53f50b9Schristos     *valp = (int) i;		/* set good value */
203a53f50b9Schristos     err = 0;			/* no error */
204a53f50b9Schristos   } else {
205a53f50b9Schristos     /* whatever was after the '=' sign wasn't a number */
206a53f50b9Schristos     plog(XLOG_MAP, "invalid numeric option in \"%s\": \"%s\"", opt, str);
207a53f50b9Schristos     /* fall through to error/exit processing */
208a53f50b9Schristos   }
209a53f50b9Schristos 
210a53f50b9Schristos  out:
211a53f50b9Schristos   return err;
212a53f50b9Schristos }
213a53f50b9Schristos 
214a53f50b9Schristos 
215a53f50b9Schristos /*
216a53f50b9Schristos  * Utility routine which returns the string value of
217a53f50b9Schristos  * an option in the mount options (such as proto=udp).
218a53f50b9Schristos  * Returns NULL if the option is not specified.
219a53f50b9Schristos  * Returns malloc'ed string (caller must free!)
220a53f50b9Schristos  */
221a53f50b9Schristos char *
hasmntstr(mntent_t * mnt,char * opt)222a53f50b9Schristos hasmntstr(mntent_t *mnt, char *opt)
223a53f50b9Schristos {
224a53f50b9Schristos   char *str = amu_hasmntopt(mnt, opt);
225a53f50b9Schristos 
226a53f50b9Schristos   if (str) { /* The option was there */
227a53f50b9Schristos 
228a53f50b9Schristos     char *eq = hasmnteq(mnt, opt);
229a53f50b9Schristos 
230a53f50b9Schristos     if (eq) { /* and had an = after it */
231a53f50b9Schristos 
232a53f50b9Schristos       char *endptr = strchr(eq, ',');
233a53f50b9Schristos 
234*8bae5d40Schristos       /* if saw no comma, return xstrdup'd string */
235a53f50b9Schristos       if (!endptr)
236*8bae5d40Schristos 	return xstrdup(eq);
237a53f50b9Schristos       else {
238a53f50b9Schristos 	/* else we need to copy only the chars needed */
239a53f50b9Schristos 	int len = endptr - eq;
240a53f50b9Schristos 	char *buf = xmalloc(len + 1);
241a53f50b9Schristos 	strncpy(buf, eq, len);
242a53f50b9Schristos 	buf[len] = '\0';
243a53f50b9Schristos 	return buf;
244a53f50b9Schristos       }
245a53f50b9Schristos     }
246a53f50b9Schristos   }
247a53f50b9Schristos   return NULL;
248a53f50b9Schristos }
249