From 96fea781285603d192ecaa5eaa6e56a5d11918c5 Mon Sep 17 00:00:00 2001 From: Peter Avalos Date: Thu, 26 Feb 2009 05:47:37 -0500 Subject: [PATCH] Add getfstab(3) and setfstab(3). * Use PATH_FSTAB instead of hard-coded value. * Change function name in dump(8) that was conflicting. Obtained-from: FreeBSD --- include/fstab.h | 13 +++++---- lib/libc/gen/Makefile.inc | 3 ++- lib/libc/gen/fstab.c | 57 +++++++++++++++++++++++++++++---------- lib/libc/gen/getfsent.3 | 56 ++++++++++++++++++++++++++++++-------- sbin/dump/dump.h | 2 +- sbin/dump/main.c | 2 +- sbin/dump/optr.c | 4 +-- 7 files changed, 102 insertions(+), 35 deletions(-) diff --git a/include/fstab.h b/include/fstab.h index 8af569f30e..258078d9bf 100644 --- a/include/fstab.h +++ b/include/fstab.h @@ -31,6 +31,7 @@ * SUCH DAMAGE. * * @(#)fstab.h 8.1 (Berkeley) 6/2/93 + * $FreeBSD: src/include/fstab.h,v 1.4 2003/04/07 12:54:59 mdodd Exp $ * $DragonFly: src/include/fstab.h,v 1.2 2003/11/14 01:01:43 dillon Exp $ */ @@ -70,11 +71,13 @@ struct fstab { #include __BEGIN_DECLS -struct fstab *getfsent (void); -struct fstab *getfsspec (const char *); -struct fstab *getfsfile (const char *); -int setfsent (void); -void endfsent (void); +void endfsent(void); +struct fstab *getfsent(void); +struct fstab *getfsfile(const char *); +struct fstab *getfsspec(const char *); +const char *getfstab(void); +int setfsent(void); +void setfstab(const char *); __END_DECLS #endif /* !_FSTAB_H_ */ diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc index b05c20968b..5e232f05a8 100644 --- a/lib/libc/gen/Makefile.inc +++ b/lib/libc/gen/Makefile.inc @@ -106,7 +106,8 @@ MLINKS+=getcap.3 cgetcap.3 getcap.3 cgetclose.3 getcap.3 cgetent.3 \ MLINKS+=getcwd.3 getwd.3 MLINKS+=getdomainname.3 setdomainname.3 MLINKS+=getfsent.3 endfsent.3 getfsent.3 getfsfile.3 getfsent.3 getfsspec.3 \ - getfsent.3 getfstype.3 getfsent.3 setfsent.3 + getfsent.3 getfstab.3 getfsent.3 getfstype.3 getfsent.3 setfsent.3 \ + getfsent.3 setfstab.3 MLINKS+=getgrent.3 endgrent.3 getgrent.3 getgrgid.3 getgrent.3 getgrnam.3 \ getgrent.3 setgrent.3 getgrent.3 setgroupent.3 MLINKS+=gethostname.3 sethostname.3 diff --git a/lib/libc/gen/fstab.c b/lib/libc/gen/fstab.c index 0ca594b23d..2ce1f5dd6b 100644 --- a/lib/libc/gen/fstab.c +++ b/lib/libc/gen/fstab.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -30,11 +26,9 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $FreeBSD: src/lib/libc/gen/fstab.c,v 1.8 2000/01/27 23:06:15 jasone Exp $ - * $DragonFly: src/lib/libc/gen/fstab.c,v 1.6 2005/11/13 00:07:42 swildner Exp $ - * * @(#)fstab.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/gen/fstab.c,v 1.8 2000/01/27 23:06:15 jasone Exp $ + * $FreeBSD: src/lib/libc/gen/fstab.c,v 1.15 2007/01/09 00:27:53 imp Exp $ + * $DragonFly: src/lib/libc/gen/fstab.c,v 1.6 2005/11/13 00:07:42 swildner Exp $ */ #include "namespace.h" @@ -54,10 +48,37 @@ static FILE *_fs_fp; static struct fstab _fs_fstab; static int LineNo = 0; +static char *path_fstab; +static char fstab_path[PATH_MAX]; +static int fsp_set = 0; + +static void error(int); +static void fixfsfile(void); +static int fstabscan(void); + +void +setfstab(const char *file) +{ + + if (file == NULL) { + path_fstab = _PATH_FSTAB; + } else { + strncpy(fstab_path, file, PATH_MAX); + fstab_path[PATH_MAX - 1] = '\0'; + path_fstab = fstab_path; + } + fsp_set = 1; +} + +const char * +getfstab(void) +{ -static void error (int); -static void fixfsfile (void); -static int fstabscan (void); + if (fsp_set) + return (path_fstab); + else + return (_PATH_FSTAB); +} static void fixfsfile(void) @@ -216,7 +237,7 @@ getfsfile(const char *name) return((struct fstab *)NULL); } -int +int setfsent(void) { if (_fs_fp) { @@ -224,7 +245,13 @@ setfsent(void) LineNo = 0; return(1); } - if ((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL) { + if (fsp_set == 0) { + if (issetugid()) + setfstab(NULL); + else + setfstab(getenv("PATH_FSTAB")); + } + if ((_fs_fp = fopen(path_fstab, "r")) != NULL) { LineNo = 0; return(1); } @@ -239,6 +266,8 @@ endfsent(void) fclose(_fs_fp); _fs_fp = NULL; } + + fsp_set = 0; } static void @@ -248,7 +277,7 @@ error(int err) char num[30]; _write(STDERR_FILENO, "fstab: ", 7); - _write(STDERR_FILENO, _PATH_FSTAB, sizeof(_PATH_FSTAB) - 1); + _write(STDERR_FILENO, path_fstab, strlen(path_fstab)); _write(STDERR_FILENO, ":", 1); sprintf(num, "%d: ", LineNo); _write(STDERR_FILENO, num, strlen(num)); diff --git a/lib/libc/gen/getfsent.3 b/lib/libc/gen/getfsent.3 index 6d9517cdc4..c9be210687 100644 --- a/lib/libc/gen/getfsent.3 +++ b/lib/libc/gen/getfsent.3 @@ -9,10 +9,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -30,10 +26,10 @@ .\" SUCH DAMAGE. .\" .\" @(#)getfsent.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/gen/getfsent.3,v 1.5.2.4 2001/12/14 18:33:51 ru Exp $ +.\" $FreeBSD: src/lib/libc/gen/getfsent.3,v 1.17 2007/01/09 00:27:53 imp Exp $ .\" $DragonFly: src/lib/libc/gen/getfsent.3,v 1.3 2006/05/26 19:39:36 swildner Exp $ .\" -.Dd June 4, 1993 +.Dd April 7, 2003 .Dt GETFSENT 3 .Os .Sh NAME @@ -41,22 +37,28 @@ .Nm getfsspec , .Nm getfsfile , .Nm setfsent , -.Nm endfsent +.Nm endfsent , +.Nm setfstab , +.Nm getfstab .Nd get file system descriptor file entry .Sh LIBRARY .Lb libc .Sh SYNOPSIS .In fstab.h -.Ft struct fstab * +.Ft "struct fstab *" .Fn getfsent void -.Ft struct fstab * +.Ft "struct fstab *" .Fn getfsspec "const char *spec" -.Ft struct fstab * +.Ft "struct fstab *" .Fn getfsfile "const char *file" .Ft int .Fn setfsent void .Ft void .Fn endfsent void +.Ft void +.Fn setfstab "const char *file" +.Ft "const char *" +.Fn getfstab void .Sh DESCRIPTION The .Fn getfsent , @@ -95,6 +97,18 @@ function closes the file. .Pp The +.Fn setfstab +function sets the file to be used by subsequent operations. +The value set by +.Fn setfstab +does not persist across calls to +.Fn endfsent . +.Pp +The +.Fn getfstab +function returns the name of the file that will be used. +.Pp +The .Fn getfsspec and .Fn getfsfile @@ -129,6 +143,20 @@ The .Fn endfsent function returns nothing. +.Sh ENVIRONMENT +.Bl -tag -width ".Ev PATH_FSTAB" +.It Ev PATH_FSTAB +If the environment variable +.Ev PATH_FSTAB +is set, all operations are performed against the specified file. +.Ev PATH_FSTAB +will not be honored if the process environment or memory address space is +considered +.Dq tainted . +(See +.Xr issetugid 2 +for more information.) +.El .Sh FILES .Bl -tag -width /etc/fstab -compact .It Pa /etc/fstab @@ -147,7 +175,13 @@ the and .Fn setfsent functions appeared in -.Bx 4.3 . +.Bx 4.3 ; +the +.Fn setfstab +and +.Fn getfstab +functions appeared in +.Fx 5.1 . .Sh BUGS These functions use static data storage; if the data is needed for future use, it should be diff --git a/sbin/dump/dump.h b/sbin/dump/dump.h index eb30b981db..edcae5a7df 100644 --- a/sbin/dump/dump.h +++ b/sbin/dump/dump.h @@ -134,7 +134,7 @@ void writerec(const void *, int); void Exit(int) __dead2; void dumpabort(int); -void getfstab(void); +void dump_getfstab(void); char *rawname(char *); struct ufs1_dinode *getino(ufs1_ino_t); diff --git a/sbin/dump/main.c b/sbin/dump/main.c index 461f242be9..5ef4d0c902 100644 --- a/sbin/dump/main.c +++ b/sbin/dump/main.c @@ -290,7 +290,7 @@ main(int argc, char **argv) if (signal(SIGINT, interrupt) == SIG_IGN) signal(SIGINT, SIG_IGN); - getfstab(); /* /etc/fstab snarfed */ + dump_getfstab(); /* /etc/fstab snarfed */ /* * disk can be either the full special file name, * the suffix of the special file name, diff --git a/sbin/dump/optr.c b/sbin/dump/optr.c index a5a84f2b5f..b286e5904e 100644 --- a/sbin/dump/optr.c +++ b/sbin/dump/optr.c @@ -289,7 +289,7 @@ struct pfstab { static SLIST_HEAD(, pfstab) table; void -getfstab(void) +dump_getfstab(void) { struct fstab *fs; struct pfstab *pf; @@ -364,7 +364,7 @@ lastdump(int arg) struct tm *tlast; time(&tnow); - getfstab(); /* /etc/fstab input */ + dump_getfstab(); /* /etc/fstab input */ initdumptimes(); /* /etc/dumpdates input */ qsort(ddatev, nddates, sizeof(struct dumpdates *), datesort); -- 2.41.0