1 /* Utilities for reading/writing fstab, mtab, etc.
2    Copyright (C) 1995-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <alloca.h>
20 #include <mntent.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdio_ext.h>
24 #include <string.h>
25 #include <sys/types.h>
26 
27 #define flockfile(s) _IO_flockfile (s)
28 #define funlockfile(s) _IO_funlockfile (s)
29 
30 #undef __setmntent
31 #undef __endmntent
32 #undef __getmntent_r
33 
34 /* Prepare to begin reading and/or writing mount table entries from the
35    beginning of FILE.  MODE is as for `fopen'.  */
36 FILE *
__setmntent(const char * file,const char * mode)37 __setmntent (const char *file, const char *mode)
38 {
39   /* Extend the mode parameter with "c" to disable cancellation in the
40      I/O functions and "e" to set FD_CLOEXEC.  */
41   size_t modelen = strlen (mode);
42   char newmode[modelen + 3];
43   memcpy (mempcpy (newmode, mode, modelen), "ce", 3);
44   FILE *result = fopen (file, newmode);
45 
46   if (result != NULL)
47     /* We do the locking ourselves.  */
48     __fsetlocking (result, FSETLOCKING_BYCALLER);
49 
50   return result;
51 }
52 libc_hidden_def (__setmntent)
weak_alias(__setmntent,setmntent)53 weak_alias (__setmntent, setmntent)
54 
55 
56 /* Close a stream opened with `setmntent'.  */
57 int
58 __endmntent (FILE *stream)
59 {
60   if (stream)		/* SunOS 4.x allows for NULL stream */
61     fclose (stream);
62   return 1;		/* SunOS 4.x says to always return 1 */
63 }
64 libc_hidden_def (__endmntent)
weak_alias(__endmntent,endmntent)65 weak_alias (__endmntent, endmntent)
66 
67 
68 /* Since the values in a line are separated by spaces, a name cannot
69    contain a space.  Therefore some programs encode spaces in names
70    by the strings "\040".  We undo the encoding when reading an entry.
71    The decoding happens in place.  */
72 static char *
73 decode_name (char *buf)
74 {
75   char *rp = buf;
76   char *wp = buf;
77 
78   do
79     if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '4' && rp[3] == '0')
80       {
81 	/* \040 is a SPACE.  */
82 	*wp++ = ' ';
83 	rp += 3;
84       }
85     else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '1')
86       {
87 	/* \011 is a TAB.  */
88 	*wp++ = '\t';
89 	rp += 3;
90       }
91     else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '2')
92       {
93 	/* \012 is a NEWLINE.  */
94 	*wp++ = '\n';
95 	rp += 3;
96       }
97     else if (rp[0] == '\\' && rp[1] == '\\')
98       {
99 	/* We have to escape \\ to be able to represent all characters.  */
100 	*wp++ = '\\';
101 	rp += 1;
102       }
103     else if (rp[0] == '\\' && rp[1] == '1' && rp[2] == '3' && rp[3] == '4')
104       {
105 	/* \134 is also \\.  */
106 	*wp++ = '\\';
107 	rp += 3;
108       }
109     else
110       *wp++ = *rp;
111   while (*rp++ != '\0');
112 
113   return buf;
114 }
115 
116 static bool
get_mnt_entry(FILE * stream,struct mntent * mp,char * buffer,int bufsiz)117 get_mnt_entry (FILE *stream, struct mntent *mp, char *buffer, int bufsiz)
118 {
119   char *cp;
120   char *head;
121 
122   do
123     {
124       char *end_ptr;
125 
126       if (__fgets_unlocked (buffer, bufsiz, stream) == NULL)
127 	  return false;
128 
129       end_ptr = strchr (buffer, '\n');
130       if (end_ptr != NULL)	/* chop newline */
131 	{
132 	  /* Do not walk past the start of buffer if it's all whitespace.  */
133 	  while (end_ptr != buffer
134 		 && (end_ptr[-1] == ' ' || end_ptr[-1] == '\t'))
135             end_ptr--;
136 	  *end_ptr = '\0';
137 	}
138       else
139 	{
140 	  /* Not the whole line was read.  Do it now but forget it.  */
141 	  char tmp[1024];
142 	  while (__fgets_unlocked (tmp, sizeof tmp, stream) != NULL)
143 	    if (strchr (tmp, '\n') != NULL)
144 	      break;
145 	}
146 
147       head = buffer + strspn (buffer, " \t");
148       /* skip empty lines and comment lines:  */
149     }
150   while (head[0] == '\0' || head[0] == '#');
151 
152   cp = __strsep (&head, " \t");
153   mp->mnt_fsname = cp != NULL ? decode_name (cp) : (char *) "";
154   if (head)
155     head += strspn (head, " \t");
156   cp = __strsep (&head, " \t");
157   mp->mnt_dir = cp != NULL ? decode_name (cp) : (char *) "";
158   if (head)
159     head += strspn (head, " \t");
160   cp = __strsep (&head, " \t");
161   mp->mnt_type = cp != NULL ? decode_name (cp) : (char *) "";
162   if (head)
163     head += strspn (head, " \t");
164   cp = __strsep (&head, " \t");
165   mp->mnt_opts = cp != NULL ? decode_name (cp) : (char *) "";
166   switch (head ? sscanf (head, " %d %d ", &mp->mnt_freq, &mp->mnt_passno) : 0)
167     {
168     case 0:
169       mp->mnt_freq = 0;
170       /* Fall through.  */
171     case 1:
172       mp->mnt_passno = 0;
173       /* Fall through.  */
174     case 2:
175       break;
176     }
177 
178   return true;
179 }
180 
181 /* Read one mount table entry from STREAM.  Returns a pointer to storage
182    reused on the next call, or null for EOF or error (use feof/ferror to
183    check).  */
184 struct mntent *
__getmntent_r(FILE * stream,struct mntent * mp,char * buffer,int bufsiz)185 __getmntent_r (FILE *stream, struct mntent *mp, char *buffer, int bufsiz)
186 {
187   struct mntent *result;
188 
189   flockfile (stream);
190   while (true)
191     if (get_mnt_entry (stream, mp, buffer, bufsiz))
192       {
193 	/* If the file system is autofs look for a mount option hint
194 	   ("ignore") to skip the entry.  */
195 	if (strcmp (mp->mnt_type, "autofs") == 0 && __hasmntopt (mp, "ignore"))
196 	  memset (mp, 0, sizeof (*mp));
197 	else
198 	  {
199 	    result = mp;
200 	    break;
201 	  }
202       }
203     else
204       {
205 	result = NULL;
206 	break;
207       }
208   funlockfile (stream);
209 
210   return result;
211 }
212 libc_hidden_def (__getmntent_r)
weak_alias(__getmntent_r,getmntent_r)213 weak_alias (__getmntent_r, getmntent_r)
214 
215 /* Write STR into STREAM, escaping whitespaces as we go.  Do not check for
216    errors here; we check the stream status in __ADDMNTENT.  */
217 static void
218 write_string (FILE *stream, const char *str)
219 {
220   char c;
221   const char *encode_chars = " \t\n\\";
222 
223   while ((c = *str++) != '\0')
224     {
225       if (strchr (encode_chars, c) == NULL)
226 	__putc_unlocked (c, stream);
227       else
228 	{
229 	  __putc_unlocked ('\\', stream);
230 	  __putc_unlocked (((c & 0xc0) >> 6) + '0', stream);
231 	  __putc_unlocked (((c & 0x38) >> 3) + '0', stream);
232 	  __putc_unlocked (((c & 0x07) >> 0) + '0', stream);
233 	}
234     }
235   __putc_unlocked (' ', stream);
236 }
237 
238 /* Write the mount table entry described by MNT to STREAM.
239    Return zero on success, nonzero on failure.  */
240 int
__addmntent(FILE * stream,const struct mntent * mnt)241 __addmntent (FILE *stream, const struct mntent *mnt)
242 {
243   int ret = 1;
244 
245   if (fseek (stream, 0, SEEK_END))
246     return ret;
247 
248   flockfile (stream);
249 
250   write_string (stream, mnt->mnt_fsname);
251   write_string (stream, mnt->mnt_dir);
252   write_string (stream, mnt->mnt_type);
253   write_string (stream, mnt->mnt_opts);
254   fprintf (stream, "%d %d\n", mnt->mnt_freq, mnt->mnt_passno);
255 
256   ret = __ferror_unlocked (stream) != 0 || fflush (stream) != 0;
257 
258   funlockfile (stream);
259 
260   return ret;
261 }
weak_alias(__addmntent,addmntent)262 weak_alias (__addmntent, addmntent)
263 
264 
265 /* Search MNT->mnt_opts for an option matching OPT.
266    Returns the address of the substring, or null if none found.  */
267 char *
268 __hasmntopt (const struct mntent *mnt, const char *opt)
269 {
270   const size_t optlen = strlen (opt);
271   char *rest = mnt->mnt_opts, *p;
272 
273   while ((p = strstr (rest, opt)) != NULL)
274     {
275       if ((p == rest || p[-1] == ',')
276 	  && (p[optlen] == '\0' || p[optlen] == '=' || p[optlen] == ','))
277 	return p;
278 
279       rest = strchr (p, ',');
280       if (rest == NULL)
281 	break;
282       ++rest;
283     }
284 
285   return NULL;
286 }
287 libc_hidden_def (__hasmntopt)
288 weak_alias (__hasmntopt, hasmntopt)
289