1 /*
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2026 Dag-Erling Smørgrav
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/param.h>
29 #include <sys/sysctl.h>
30
31 #include <errno.h>
32 #include <libutil.h>
33 #include <paths.h>
34 #include <stdbool.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37
38 #ifndef _PATH_LOCALBASE
39 #define _PATH_LOCALBASE "/usr/local"
40 #endif
41
42 /*
43 * Used in case of error; guaranteed to not be the start of a valid path
44 * name. Deliberately includes a trailing path separator so that any
45 * access will fail with ENOTDIR.
46 */
47 #define INVALID_PREFIX "/dev/null/"
48
49 /*
50 * Copies a path from src to dst, which may point to the same buffer,
51 * while deduplicating path separators. Returns false if the path is not
52 * absolute or the result (including the terminating NUL) exceeds len
53 * characters.
54 */
55 static bool
normalize(const char * src,char * dst,size_t len)56 normalize(const char *src, char *dst, size_t len)
57 {
58 if (*src != '/' || len == 0)
59 return (false);
60 while (*src == '/')
61 src++;
62 do {
63 *dst++ = '/';
64 if (--len == 0)
65 return (false);
66 while (*src != '\0' && *src != '/') {
67 *dst++ = *src++;
68 if (--len == 0)
69 return (false);
70 }
71 while (*src == '/')
72 src++;
73 } while (*src != '\0');
74 *dst = '\0';
75 return (true);
76 }
77
78 const char *
getlocalbase(void)79 getlocalbase(void)
80 {
81 static const int oid[2] = { CTL_USER, USER_LOCALBASE };
82 static char buf[MAXPATHLEN];
83 static const char *localbase = NULL;
84 char *path;
85 size_t len = sizeof(buf);
86
87 if (localbase != NULL)
88 return (localbase);
89
90 /* If not privileged, try the environment first */
91 if ((path = secure_getenv("LOCALBASE")) != NULL && *path != '\0') {
92 if (!normalize(path, buf, sizeof(buf)))
93 return (localbase = INVALID_PREFIX);
94 return (localbase = buf);
95 }
96
97 /* Next, try the sysctl variable */
98 if (sysctl(oid, 2, buf, &len, NULL, 0) != 0) {
99 if (errno == ENOMEM)
100 return (localbase = INVALID_PREFIX);
101 } else if (*buf != '\0') {
102 if (!normalize(buf, buf, sizeof(buf)))
103 return (localbase = INVALID_PREFIX);
104 return (localbase = buf);
105 }
106
107 /* Fall back to the hardcoded default */
108 return (localbase = _PATH_LOCALBASE);
109 }
110