Index: lib/libutil/Makefile =================================================================== --- lib/libutil/Makefile +++ lib/libutil/Makefile @@ -12,7 +12,8 @@ LIB= util SHLIB_MAJOR= 9 -SRCS= _secure_path.c auth.c expand_number.c flopen.c fparseln.c gr_util.c \ +SRCS= _secure_path.c auth.c expand_number.c flopen.c fparseln.c \ + getlocalbase.c gr_util.c \ hexdump.c humanize_number.c kinfo_getfile.c \ kinfo_getallproc.c kinfo_getproc.c kinfo_getvmmap.c \ kinfo_getvmobject.c kld.c \ @@ -30,7 +31,7 @@ CFLAGS+= -I${.CURDIR} -I${SRCTOP}/lib/libc/gen/ -MAN+= expand_number.3 flopen.3 fparseln.3 hexdump.3 \ +MAN+= expand_number.3 flopen.3 fparseln.3 getlocalbase.3 hexdump.3 \ humanize_number.3 kinfo_getallproc.3 kinfo_getfile.3 \ kinfo_getproc.3 kinfo_getvmmap.3 kinfo_getvmobject.3 kld.3 \ login_auth.3 login_cap.3 \ Index: lib/libutil/getlocalbase.3 =================================================================== --- /dev/null +++ lib/libutil/getlocalbase.3 @@ -0,0 +1,103 @@ +.\" +.\" SPDX-License-Identifier: BSD-2-Clause-FreeBSD +.\" +.\" Copyright 2020 Scott Long +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 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. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd Oct 28, 2020 +.Dt GETLOCALBASE 3 +.Os +.Sh NAME +.Nm getlocalbase +.Nd "return the path to the local software directory" +.Sh LIBRARY +.Lb libutil +.Sh SYNOPSIS +.In libutil.h +.Ft int +.Fn getlocalbase char* ssize_t +.Sh DESCRIPTION +The +.Fn getlocalbase +function returns the path to the local software base directory. +Normally this is the +.Pa /usr/local +directory. +First the +.Ev LOCALBASE +environment variable checked. +If that doesn't exist then the +.Em user.localbase +sysctl is checked. +If that also doesn't exist then the value of the +.Em _PATH_LOCALBASE +compile-time variable is used. +If that is undefined then the default of +.Pa /usr/local +is used. +.Pp +The +.Fn getlocalbase +function takes two arguments: +.Fa path +and +.Fa len . +The +.Fa path +argument points to a caller-supplied buffer to hold the resulting pathname. +The +.Fa len +argument is a pointer to the length of the caller-supplied buffer. At +return it will be updated with the actual length of the string copied into +the +.Fa path +buffer. +.Sh IMPLEMENTATION NOTES +Calls to +.Fn getlocalpath +will perform a setugid check on the running binary before checking the +environment. +.Sh RETURN VALUES +.Rv -std getlocalbase +.Sh ENVIRONMENT +The +.Fn getlocalbase +library function retrieves the +.Ev LOCALBASE +environment variable. +.El +.Sh SEE ALSO +.Xr env 1 , +.Xr src.conf 5 +.Sh HISTORY +The +.Nm +manual page example first appeared in +.Fx 13.0 . +.Pp +.Sh AUTHORS +This +manual page was written by +.An Scott Long Aq Mt scottl@FreeBSD.org Index: lib/libutil/getlocalbase.c =================================================================== --- /dev/null +++ lib/libutil/getlocalbase.c @@ -0,0 +1,75 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright 2020 Scott Long + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include + +size_t +getlocalbase(char *path, size_t pathlen) +{ + size_t tmplen; + const char *tmppath; + + if ((pathlen == 0) || (path == NULL)) { + errno = EINVAL; + return (-1); + } + + tmppath = NULL; + tmplen = pathlen; + if (issetugid() == 0) + tmppath = getenv("LOCALBASE"); + + if ((tmppath == NULL) && + (sysctlbyname("user.localbase", path, &tmplen, NULL, 0) == 0)) { + return (tmplen); + } + + if (tmppath == NULL) +#ifdef _PATH_LOCALBASE + tmppath = _PATH_LOCALBASE; +#else + tmppath = "/usr/local"; +#endif + + tmplen = strlcpy(path, tmppath, pathlen); + if ((tmplen < 0) || (tmplen >= pathlen)) { + errno = ENOMEM; + tmplen = -1; + } + + return (tmplen); +} Index: lib/libutil/libutil.h =================================================================== --- lib/libutil/libutil.h +++ lib/libutil/libutil.h @@ -98,6 +98,7 @@ int flopenat(int _dirfd, const char *_path, int _flags, ...); int forkpty(int *_amaster, char *_name, struct termios *_termp, struct winsize *_winp); +size_t getlocalbase(char *path, size_t pathlen); void hexdump(const void *_ptr, int _length, const char *_hdr, int _flags); int humanize_number(char *_buf, size_t _len, int64_t _number, const char *_suffix, int _scale, int _flags); Index: sbin/nvmecontrol/comnd.h =================================================================== --- sbin/nvmecontrol/comnd.h +++ sbin/nvmecontrol/comnd.h @@ -96,7 +96,7 @@ int arg_parse(int argc, char * const *argv, const struct cmd *f); void arg_help(int argc, char * const *argv, const struct cmd *f); void cmd_init(void); -void cmd_load_dir(const char *dir, cmd_load_cb_t *cb, void *argp); +void cmd_load_dir(char *dir, cmd_load_cb_t *cb, void *argp); int cmd_dispatch(int argc, char *argv[], const struct cmd *); #endif /* COMND_H */ Index: sbin/nvmecontrol/comnd.c =================================================================== --- sbin/nvmecontrol/comnd.c +++ sbin/nvmecontrol/comnd.c @@ -286,7 +286,7 @@ * Loads all the .so's from the specified directory. */ void -cmd_load_dir(const char *dir __unused, cmd_load_cb_t cb __unused, void *argp __unused) +cmd_load_dir(char *dir, cmd_load_cb_t cb, void *argp) { DIR *d; struct dirent *dent; Index: sbin/nvmecontrol/nvmecontrol.c =================================================================== --- sbin/nvmecontrol/nvmecontrol.c +++ sbin/nvmecontrol/nvmecontrol.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -175,11 +176,17 @@ int main(int argc, char *argv[]) { + char locallib[MAXPATHLEN]; + size_t len; cmd_init(); - cmd_load_dir("/lib/nvmecontrol", NULL, NULL); - cmd_load_dir(_PATH_LOCALBASE "/lib/nvmecontrol", NULL, NULL); + snprintf(locallib, MAXPATHLEN, "/lib/nvmecontrol"); + cmd_load_dir(locallib, NULL, NULL); + if ((len = getlocalbase(locallib, MAXPATHLEN)) > 0) { + strlcat(locallib, "/lib/nvmecontrol", MAXPATHLEN - len); + cmd_load_dir(locallib, NULL, NULL); + } cmd_dispatch(argc, argv, NULL); Index: usr.sbin/mailwrapper/mailwrapper.c =================================================================== --- usr.sbin/mailwrapper/mailwrapper.c +++ usr.sbin/mailwrapper/mailwrapper.c @@ -105,8 +105,10 @@ initarg(&al); addarg(&al, argv[0]); - snprintf(localmailerconf, MAXPATHLEN, "%s/etc/mail/mailer.conf", - getenv("LOCALBASE") ? getenv("LOCALBASE") : _PATH_LOCALBASE); + if ((len = getlocalbase(localmailerconf, MAXPATHLEN)) != 0) + err(EX_OSERR, "cannot determine local path"); + + strlcat(localmailerconf, "/etc/mail/mailer.conf", MAXPATHLEN - len); mailerconf = localmailerconf; if ((config = fopen(localmailerconf, "r")) == NULL) Index: usr.sbin/pkg/Makefile =================================================================== --- usr.sbin/pkg/Makefile +++ usr.sbin/pkg/Makefile @@ -25,6 +25,6 @@ CFLAGS+=-I${SRCTOP}/contrib/libucl/include .PATH: ${SRCTOP}/contrib/libucl/include -LIBADD= archive fetch ucl sbuf crypto ssl +LIBADD= archive fetch ucl sbuf crypto ssl util .include Index: usr.sbin/pkg/pkg.c =================================================================== --- usr.sbin/pkg/pkg.c +++ usr.sbin/pkg/pkg.c @@ -43,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -1037,6 +1038,7 @@ { char pkgpath[MAXPATHLEN]; const char *pkgarg; + size_t len; int i; bool bootstrap_only, force, yes; @@ -1045,8 +1047,11 @@ pkgarg = NULL; yes = false; - snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg", - getenv("LOCALBASE") ? getenv("LOCALBASE") : _PATH_LOCALBASE); + if ((len = getlocalbase(pkgpath, MAXPATHLEN)) != 0) { + fprintf(stderr, "Cannot determine local path\n"); + exit(EXIT_FAILURE); + } + strlcat(pkgpath, "/sbin/pkg", MAXPATHLEN - len); if (argc > 1 && strcmp(argv[1], "bootstrap") == 0) { bootstrap_only = true;