xref: /freebsd/usr.bin/pathchk/pathchk.c (revision 5f4c09dd85bff675e0ca63c55ea3c517e0fddfcc)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2002 Tim J. Robbins.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * pathchk -- check pathnames
31  *
32  * Check whether files could be created with the names specified on the
33  * command line. If -p is specified, check whether the pathname is portable
34  * to all POSIX systems.
35  */
36 
37 #include <sys/cdefs.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 
41 #include <err.h>
42 #include <errno.h>
43 #include <limits.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 
49 static int	 check(const char *);
50 static int	 portable(const char *);
51 static void	 usage(void);
52 
53 static int	 pflag;			/* Perform portability checks */
54 static int	 Pflag;			/* Check for empty paths, leading '-' */
55 
56 int
57 main(int argc, char *argv[])
58 {
59 	int ch, rval;
60 	const char *arg;
61 
62 	while ((ch = getopt(argc, argv, "pP")) > 0) {
63 		switch (ch) {
64 		case 'p':
65 			pflag = 1;
66 			break;
67 		case 'P':
68 			Pflag = 1;
69 			break;
70 		default:
71 			usage();
72 			/*NOTREACHED*/
73 		}
74 	}
75 	argc -= optind;
76 	argv += optind;
77 
78 	if (argc == 0)
79 		usage();
80 
81 	rval = 0;
82 	while ((arg = *argv++) != NULL)
83 		rval |= check(arg);
84 
85 	exit(rval);
86 }
87 
88 static void
89 usage(void)
90 {
91 
92 	fprintf(stderr, "usage: pathchk [-Pp] pathname ...\n");
93 	exit(1);
94 }
95 
96 static int
97 check(const char *path)
98 {
99 	struct stat sb;
100 	long complen, namemax, pathmax, svnamemax;
101 	int last;
102 	char *end, *p, *pathd;
103 
104 	if ((pathd = strdup(path)) == NULL)
105 		err(1, "strdup");
106 
107 	p = pathd;
108 
109 	if (Pflag && *p == '\0') {
110 		warnx("%s: empty pathname", path);
111 		goto bad;
112 	}
113 	if ((Pflag || pflag) && (*p == '-' || strstr(p, "/-") != NULL)) {
114 		warnx("%s: contains a component starting with '-'", path);
115 		goto bad;
116 	}
117 
118 	if (!pflag) {
119 		errno = 0;
120 		namemax = pathconf(*p == '/' ? "/" : ".", _PC_NAME_MAX);
121 		if (namemax == -1 && errno != 0)
122 			namemax = NAME_MAX;
123 	} else
124 		namemax = _POSIX_NAME_MAX;
125 
126 	for (;;) {
127 		p += strspn(p, "/");
128 		complen = (long)strcspn(p, "/");
129 		end = p + complen;
130 		last = *end == '\0';
131 		*end = '\0';
132 
133 		if (namemax != -1 && complen > namemax) {
134 			warnx("%s: %s: component too long (limit %ld)", path,
135 			    p, namemax);
136 			goto bad;
137 		}
138 
139 		if (!pflag && stat(pathd, &sb) == -1 && errno != ENOENT) {
140 			warn("%s: %.*s", path, (int)(strlen(pathd) -
141 			    complen - 1), pathd);
142 			goto bad;
143 		}
144 
145 		if (pflag && !portable(p)) {
146 			warnx("%s: %s: component contains non-portable "
147 			    "character", path, p);
148 			goto bad;
149 		}
150 
151 		if (last)
152 			break;
153 
154 		if (!pflag) {
155 			errno = 0;
156 			svnamemax = namemax;
157 			namemax = pathconf(pathd, _PC_NAME_MAX);
158 			if (namemax == -1 && errno != 0)
159 				namemax = svnamemax;
160 		}
161 
162 		*end = '/';
163 		p = end + 1;
164 	}
165 
166 	if (!pflag) {
167 		errno = 0;
168 		pathmax = pathconf(path, _PC_PATH_MAX);
169 		if (pathmax == -1 && errno != 0)
170 			pathmax = PATH_MAX;
171 	} else
172 		pathmax = _POSIX_PATH_MAX;
173 	if (pathmax != -1 && strlen(path) >= (size_t)pathmax) {
174 		warnx("%s: path too long (limit %ld)", path, pathmax - 1);
175 		goto bad;
176 	}
177 
178 	free(pathd);
179 	return (0);
180 
181 bad:	free(pathd);
182 	return (1);
183 }
184 
185 /*
186  * Check whether a path component contains only portable characters.
187  */
188 static int
189 portable(const char *path)
190 {
191 	static const char charset[] =
192 	    "abcdefghijklmnopqrstuvwxyz"
193 	    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
194 	    "0123456789._-";
195 	long s;
196 
197 	s = strspn(path, charset);
198 	if (path[s] != '\0')
199 		return (0);
200 
201 	return (1);
202 }
203