1*6818646aSjoerg /* $NetBSD: pathchk.c,v 1.2 2011/09/16 15:39:28 joerg Exp $ */
2938f344dSjdolecek
3938f344dSjdolecek /*-
4938f344dSjdolecek * Copyright (c) 2002 Tim J. Robbins.
5938f344dSjdolecek * All rights reserved.
6938f344dSjdolecek *
7938f344dSjdolecek * Redistribution and use in source and binary forms, with or without
8938f344dSjdolecek * modification, are permitted provided that the following conditions
9938f344dSjdolecek * are met:
10938f344dSjdolecek * 1. Redistributions of source code must retain the above copyright
11938f344dSjdolecek * notice, this list of conditions and the following disclaimer.
12938f344dSjdolecek * 2. Redistributions in binary form must reproduce the above copyright
13938f344dSjdolecek * notice, this list of conditions and the following disclaimer in the
14938f344dSjdolecek * documentation and/or other materials provided with the distribution.
15938f344dSjdolecek *
16938f344dSjdolecek * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17938f344dSjdolecek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18938f344dSjdolecek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19938f344dSjdolecek * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20938f344dSjdolecek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21938f344dSjdolecek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22938f344dSjdolecek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23938f344dSjdolecek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24938f344dSjdolecek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25938f344dSjdolecek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26938f344dSjdolecek * SUCH DAMAGE.
27938f344dSjdolecek *
28938f344dSjdolecek * from FreeBSD: pathchk.c,v 1.4 2002/12/15 00:40:47 tjr Exp
29938f344dSjdolecek */
30938f344dSjdolecek
31938f344dSjdolecek #include <sys/cdefs.h>
32938f344dSjdolecek #ifndef lint
33*6818646aSjoerg __RCSID("$NetBSD: pathchk.c,v 1.2 2011/09/16 15:39:28 joerg Exp $");
34938f344dSjdolecek #endif /* !lint */
35938f344dSjdolecek
36938f344dSjdolecek #include <sys/types.h>
37938f344dSjdolecek #include <sys/stat.h>
38938f344dSjdolecek
39938f344dSjdolecek #include <err.h>
40938f344dSjdolecek #include <errno.h>
41938f344dSjdolecek #include <limits.h>
42938f344dSjdolecek #include <stdio.h>
43938f344dSjdolecek #include <stdlib.h>
44938f344dSjdolecek #include <string.h>
45938f344dSjdolecek #include <unistd.h>
46938f344dSjdolecek
47938f344dSjdolecek static int check(const char *);
48938f344dSjdolecek static int portable(const char *);
49*6818646aSjoerg __dead static void usage(void);
50938f344dSjdolecek
51938f344dSjdolecek static int pflag; /* Perform portability checks */
52938f344dSjdolecek
53938f344dSjdolecek int
main(int argc,char * argv[])54938f344dSjdolecek main(int argc, char *argv[])
55938f344dSjdolecek {
56938f344dSjdolecek int ch, rval;
57938f344dSjdolecek const char *arg;
58938f344dSjdolecek
59938f344dSjdolecek while ((ch = getopt(argc, argv, "p")) > 0) {
60938f344dSjdolecek switch (ch) {
61938f344dSjdolecek case 'p':
62938f344dSjdolecek pflag = 1;
63938f344dSjdolecek break;
64938f344dSjdolecek default:
65938f344dSjdolecek usage();
66938f344dSjdolecek /*NOTREACHED*/
67938f344dSjdolecek }
68938f344dSjdolecek }
69938f344dSjdolecek argc -= optind;
70938f344dSjdolecek argv += optind;
71938f344dSjdolecek
72938f344dSjdolecek if (argc == 0)
73938f344dSjdolecek usage();
74938f344dSjdolecek
75938f344dSjdolecek rval = 0;
76938f344dSjdolecek while ((arg = *argv++) != NULL)
77938f344dSjdolecek rval |= check(arg);
78938f344dSjdolecek
79938f344dSjdolecek exit(rval);
80938f344dSjdolecek }
81938f344dSjdolecek
82938f344dSjdolecek static void
usage(void)83938f344dSjdolecek usage(void)
84938f344dSjdolecek {
85938f344dSjdolecek
86938f344dSjdolecek fprintf(stderr, "usage: pathchk [-p] pathname...\n");
87938f344dSjdolecek exit(1);
88938f344dSjdolecek }
89938f344dSjdolecek
90938f344dSjdolecek static int
check(const char * path)91938f344dSjdolecek check(const char *path)
92938f344dSjdolecek {
93938f344dSjdolecek struct stat sb;
94938f344dSjdolecek long complen, namemax, pathmax, svnamemax;
95938f344dSjdolecek int badch, last;
96938f344dSjdolecek char *end, *p, *pathd;
97938f344dSjdolecek
98938f344dSjdolecek if ((pathd = strdup(path)) == NULL)
99938f344dSjdolecek err(1, "strdup");
100938f344dSjdolecek
101938f344dSjdolecek p = pathd;
102938f344dSjdolecek
103938f344dSjdolecek if (!pflag) {
104938f344dSjdolecek errno = 0;
105938f344dSjdolecek namemax = pathconf(*p == '/' ? "/" : ".", _PC_NAME_MAX);
106938f344dSjdolecek if (namemax == -1 && errno != 0)
107938f344dSjdolecek namemax = NAME_MAX;
108938f344dSjdolecek } else
109938f344dSjdolecek namemax = _POSIX_NAME_MAX;
110938f344dSjdolecek
111938f344dSjdolecek for (;;) {
112938f344dSjdolecek p += strspn(p, "/");
113938f344dSjdolecek complen = (long)strcspn(p, "/");
114938f344dSjdolecek end = p + complen;
115938f344dSjdolecek last = *end == '\0';
116938f344dSjdolecek *end = '\0';
117938f344dSjdolecek
118938f344dSjdolecek if (namemax != -1 && complen > namemax) {
119938f344dSjdolecek warnx("%s: %s: component too long (limit %ld)", path,
120938f344dSjdolecek p, namemax);
121938f344dSjdolecek goto bad;
122938f344dSjdolecek }
123938f344dSjdolecek
124938f344dSjdolecek if (!pflag && stat(pathd, &sb) == -1 && errno != ENOENT) {
125938f344dSjdolecek warn("%s: %.*s", path, (int)(strlen(pathd) -
126938f344dSjdolecek complen - 1), pathd);
127938f344dSjdolecek goto bad;
128938f344dSjdolecek }
129938f344dSjdolecek
130938f344dSjdolecek if (pflag && (badch = portable(p)) >= 0) {
131938f344dSjdolecek warnx("%s: %s: component contains non-portable "
132938f344dSjdolecek "character `%c'", path, p, badch);
133938f344dSjdolecek goto bad;
134938f344dSjdolecek }
135938f344dSjdolecek
136938f344dSjdolecek if (last)
137938f344dSjdolecek break;
138938f344dSjdolecek
139938f344dSjdolecek if (!pflag) {
140938f344dSjdolecek errno = 0;
141938f344dSjdolecek svnamemax = namemax;
142938f344dSjdolecek namemax = pathconf(pathd, _PC_NAME_MAX);
143938f344dSjdolecek if (namemax == -1 && errno != 0)
144938f344dSjdolecek namemax = svnamemax;
145938f344dSjdolecek }
146938f344dSjdolecek
147938f344dSjdolecek *end = '/';
148938f344dSjdolecek p = end + 1;
149938f344dSjdolecek }
150938f344dSjdolecek
151938f344dSjdolecek if (!pflag) {
152938f344dSjdolecek errno = 0;
153938f344dSjdolecek pathmax = pathconf(path, _PC_PATH_MAX);
154938f344dSjdolecek if (pathmax == -1 && errno != 0)
155938f344dSjdolecek pathmax = PATH_MAX;
156938f344dSjdolecek } else
157938f344dSjdolecek pathmax = _POSIX_PATH_MAX;
158938f344dSjdolecek if (pathmax != -1 && strlen(path) >= (size_t)pathmax) {
159938f344dSjdolecek warnx("%s: path too long (limit %ld)", path, pathmax - 1);
160938f344dSjdolecek goto bad;
161938f344dSjdolecek }
162938f344dSjdolecek
163938f344dSjdolecek free(pathd);
164938f344dSjdolecek return (0);
165938f344dSjdolecek
166938f344dSjdolecek bad: free(pathd);
167938f344dSjdolecek return (1);
168938f344dSjdolecek }
169938f344dSjdolecek
170938f344dSjdolecek /*
171938f344dSjdolecek * Check whether a path component contains only portable characters. Return
172938f344dSjdolecek * the first non-portable character found.
173938f344dSjdolecek */
174938f344dSjdolecek static int
portable(const char * path)175938f344dSjdolecek portable(const char *path)
176938f344dSjdolecek {
177938f344dSjdolecek static const char charset[] =
178938f344dSjdolecek "abcdefghijklmnopqrstuvwxyz"
179938f344dSjdolecek "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
180938f344dSjdolecek "0123456789._-";
181938f344dSjdolecek long s;
182938f344dSjdolecek
183938f344dSjdolecek if (*path == '-')
184938f344dSjdolecek return (*path);
185938f344dSjdolecek
186938f344dSjdolecek s = strspn(path, charset);
187938f344dSjdolecek if (path[s] != '\0')
188938f344dSjdolecek return (path[s]);
189938f344dSjdolecek
190938f344dSjdolecek return (-1);
191938f344dSjdolecek }
192