1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate *
24*7c478bd9Sstevel@tonic-gate * Copyright (c) 1994 by Sun Microsystems, Inc.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate * Copyright 1991, 1992 by Mortice Kern Systems Inc. All rights reserved.
29*7c478bd9Sstevel@tonic-gate *
30*7c478bd9Sstevel@tonic-gate * Standards Conformance :
31*7c478bd9Sstevel@tonic-gate * P1003.2/D11.2
32*7c478bd9Sstevel@tonic-gate *
33*7c478bd9Sstevel@tonic-gate */
34*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
35*7c478bd9Sstevel@tonic-gate /*
36*7c478bd9Sstevel@tonic-gate * Original ident string for reference
37*7c478bd9Sstevel@tonic-gate * ident "$Id: pathchk.c,v 1.29 1994/05/24 15:51:19 mark Exp $"
38*7c478bd9Sstevel@tonic-gate */
39*7c478bd9Sstevel@tonic-gate
40*7c478bd9Sstevel@tonic-gate #include <locale.h>
41*7c478bd9Sstevel@tonic-gate #include <libintl.h>
42*7c478bd9Sstevel@tonic-gate #include <limits.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
44*7c478bd9Sstevel@tonic-gate #include <fcntl.h> /* for creat() prototype */
45*7c478bd9Sstevel@tonic-gate #include <string.h>
46*7c478bd9Sstevel@tonic-gate #include <errno.h>
47*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
48*7c478bd9Sstevel@tonic-gate #include <stdio.h>
49*7c478bd9Sstevel@tonic-gate #include <ctype.h>
50*7c478bd9Sstevel@tonic-gate #include <unistd.h>
51*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
52*7c478bd9Sstevel@tonic-gate
53*7c478bd9Sstevel@tonic-gate /*
54*7c478bd9Sstevel@tonic-gate * These are the characters in the portable filename character set defined
55*7c478bd9Sstevel@tonic-gate * in POSIX P1003.2.
56*7c478bd9Sstevel@tonic-gate */
57*7c478bd9Sstevel@tonic-gate static char portfsset[] = \
58*7c478bd9Sstevel@tonic-gate "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-";
59*7c478bd9Sstevel@tonic-gate
60*7c478bd9Sstevel@tonic-gate
61*7c478bd9Sstevel@tonic-gate #ifndef M_FSDELIM
62*7c478bd9Sstevel@tonic-gate #define M_FSDELIM(c) ((c) == '/')
63*7c478bd9Sstevel@tonic-gate #endif
64*7c478bd9Sstevel@tonic-gate
65*7c478bd9Sstevel@tonic-gate static char *nametoolong = "%s: component too long.\n";
66*7c478bd9Sstevel@tonic-gate static char *pathtoolong = "%s: pathname too long.\n";
67*7c478bd9Sstevel@tonic-gate static char *notsrch = "%s: Not searchable.\n";
68*7c478bd9Sstevel@tonic-gate static char *badchar = "%s: Nonportable character '%c' (%#02X) found.\n";
69*7c478bd9Sstevel@tonic-gate static char *badbyte = "%s: Nonportable byte %#02X found.\n";
70*7c478bd9Sstevel@tonic-gate
71*7c478bd9Sstevel@tonic-gate static char *pathconfprob = "pathchk: warning: \
72*7c478bd9Sstevel@tonic-gate pathconf(\"%s\", %s) returns '%s'. Using %s = %d\n";
73*7c478bd9Sstevel@tonic-gate
74*7c478bd9Sstevel@tonic-gate
75*7c478bd9Sstevel@tonic-gate static int printWarnings = 1;
76*7c478bd9Sstevel@tonic-gate
77*7c478bd9Sstevel@tonic-gate static int checkpathname(char *, int);
78*7c478bd9Sstevel@tonic-gate static void usage(void);
79*7c478bd9Sstevel@tonic-gate
80*7c478bd9Sstevel@tonic-gate /*
81*7c478bd9Sstevel@tonic-gate * mainline for pathchk
82*7c478bd9Sstevel@tonic-gate */
83*7c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)84*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
85*7c478bd9Sstevel@tonic-gate {
86*7c478bd9Sstevel@tonic-gate int c;
87*7c478bd9Sstevel@tonic-gate int errors;
88*7c478bd9Sstevel@tonic-gate int pflag = 0;
89*7c478bd9Sstevel@tonic-gate
90*7c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
91*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
92*7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
93*7c478bd9Sstevel@tonic-gate #endif
94*7c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
95*7c478bd9Sstevel@tonic-gate
96*7c478bd9Sstevel@tonic-gate
97*7c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "pw")) != EOF) {
98*7c478bd9Sstevel@tonic-gate switch (c) {
99*7c478bd9Sstevel@tonic-gate case 'p':
100*7c478bd9Sstevel@tonic-gate pflag = 1;
101*7c478bd9Sstevel@tonic-gate break;
102*7c478bd9Sstevel@tonic-gate
103*7c478bd9Sstevel@tonic-gate case 'w':
104*7c478bd9Sstevel@tonic-gate /* turn off warning messages */
105*7c478bd9Sstevel@tonic-gate printWarnings = 0;
106*7c478bd9Sstevel@tonic-gate break;
107*7c478bd9Sstevel@tonic-gate
108*7c478bd9Sstevel@tonic-gate default:
109*7c478bd9Sstevel@tonic-gate usage();
110*7c478bd9Sstevel@tonic-gate }
111*7c478bd9Sstevel@tonic-gate }
112*7c478bd9Sstevel@tonic-gate
113*7c478bd9Sstevel@tonic-gate argv += optind;
114*7c478bd9Sstevel@tonic-gate
115*7c478bd9Sstevel@tonic-gate if (*argv == 0) {
116*7c478bd9Sstevel@tonic-gate usage();
117*7c478bd9Sstevel@tonic-gate /* NOTREACHED */
118*7c478bd9Sstevel@tonic-gate }
119*7c478bd9Sstevel@tonic-gate
120*7c478bd9Sstevel@tonic-gate errors = 0;
121*7c478bd9Sstevel@tonic-gate while (*argv) {
122*7c478bd9Sstevel@tonic-gate errors += checkpathname(*argv, pflag);
123*7c478bd9Sstevel@tonic-gate argv += 1;
124*7c478bd9Sstevel@tonic-gate }
125*7c478bd9Sstevel@tonic-gate
126*7c478bd9Sstevel@tonic-gate return (errors);
127*7c478bd9Sstevel@tonic-gate }
128*7c478bd9Sstevel@tonic-gate
129*7c478bd9Sstevel@tonic-gate /*
130*7c478bd9Sstevel@tonic-gate * checkPathConf(const char *, int, long *)
131*7c478bd9Sstevel@tonic-gate *
132*7c478bd9Sstevel@tonic-gate * Calls pathconf(), and returns 1 if pathconf failed, zero
133*7c478bd9Sstevel@tonic-gate * otherwise. If pathconf() succeeded, then *valp contains the
134*7c478bd9Sstevel@tonic-gate * value returned
135*7c478bd9Sstevel@tonic-gate */
136*7c478bd9Sstevel@tonic-gate static int
checkPathConf(const char * path,int type,long * valp)137*7c478bd9Sstevel@tonic-gate checkPathConf(const char *path, int type, long *valp)
138*7c478bd9Sstevel@tonic-gate {
139*7c478bd9Sstevel@tonic-gate errno = 0;
140*7c478bd9Sstevel@tonic-gate *valp = pathconf(path, type);
141*7c478bd9Sstevel@tonic-gate if ((*valp == -1) && (errno != 0) && (errno != EACCES)) {
142*7c478bd9Sstevel@tonic-gate /*
143*7c478bd9Sstevel@tonic-gate * pathconf() is not supported on some mounted filesystems
144*7c478bd9Sstevel@tonic-gate * (e.g NFS mounts) and pathconf() is known to fail.
145*7c478bd9Sstevel@tonic-gate * So, we print a warning and use the POSIX default values.
146*7c478bd9Sstevel@tonic-gate */
147*7c478bd9Sstevel@tonic-gate if (type == _PC_PATH_MAX)
148*7c478bd9Sstevel@tonic-gate *valp = _POSIX_PATH_MAX;
149*7c478bd9Sstevel@tonic-gate else
150*7c478bd9Sstevel@tonic-gate *valp = _POSIX_NAME_MAX;
151*7c478bd9Sstevel@tonic-gate
152*7c478bd9Sstevel@tonic-gate if (printWarnings) {
153*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(pathconfprob), path,
154*7c478bd9Sstevel@tonic-gate type == _PC_PATH_MAX?"_PC_PATH_MAX" :
155*7c478bd9Sstevel@tonic-gate "_PC_NAME_MAX", strerror(errno),
156*7c478bd9Sstevel@tonic-gate type == _PC_PATH_MAX ? "PATH_MAX" : "NAME_MAX",
157*7c478bd9Sstevel@tonic-gate *valp);
158*7c478bd9Sstevel@tonic-gate }
159*7c478bd9Sstevel@tonic-gate }
160*7c478bd9Sstevel@tonic-gate return ((*valp == -1) && (errno != 0));
161*7c478bd9Sstevel@tonic-gate }
162*7c478bd9Sstevel@tonic-gate
163*7c478bd9Sstevel@tonic-gate
164*7c478bd9Sstevel@tonic-gate #define UPDATE_LIMITS(buf)\
165*7c478bd9Sstevel@tonic-gate {\
166*7c478bd9Sstevel@tonic-gate if (pflag) {\
167*7c478bd9Sstevel@tonic-gate nameMax = _POSIX_NAME_MAX;\
168*7c478bd9Sstevel@tonic-gate pathMax = _POSIX_PATH_MAX;\
169*7c478bd9Sstevel@tonic-gate } else if (checkPathConf((buf), _PC_PATH_MAX, &pathMax) || \
170*7c478bd9Sstevel@tonic-gate checkPathConf((buf), _PC_NAME_MAX, &nameMax)) {\
171*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(notsrch), buf);\
172*7c478bd9Sstevel@tonic-gate return (1);\
173*7c478bd9Sstevel@tonic-gate }\
174*7c478bd9Sstevel@tonic-gate }
175*7c478bd9Sstevel@tonic-gate
176*7c478bd9Sstevel@tonic-gate /*
177*7c478bd9Sstevel@tonic-gate * checkpathname(char *pname)
178*7c478bd9Sstevel@tonic-gate * pathchk a single pathname.
179*7c478bd9Sstevel@tonic-gate */
180*7c478bd9Sstevel@tonic-gate int
checkpathname(char * path,int pflag)181*7c478bd9Sstevel@tonic-gate checkpathname(char *path, int pflag)
182*7c478bd9Sstevel@tonic-gate {
183*7c478bd9Sstevel@tonic-gate int checkStat;
184*7c478bd9Sstevel@tonic-gate long nameMax;
185*7c478bd9Sstevel@tonic-gate long pathMax;
186*7c478bd9Sstevel@tonic-gate char *scomp;
187*7c478bd9Sstevel@tonic-gate char *ecomp;
188*7c478bd9Sstevel@tonic-gate register char *p;
189*7c478bd9Sstevel@tonic-gate
190*7c478bd9Sstevel@tonic-gate p = path;
191*7c478bd9Sstevel@tonic-gate checkStat = 1;
192*7c478bd9Sstevel@tonic-gate
193*7c478bd9Sstevel@tonic-gate /*
194*7c478bd9Sstevel@tonic-gate * Get the initial NAME_MAX and PATH_MAX values
195*7c478bd9Sstevel@tonic-gate */
196*7c478bd9Sstevel@tonic-gate if (M_FSDELIM(*p)) {
197*7c478bd9Sstevel@tonic-gate char buf[2];
198*7c478bd9Sstevel@tonic-gate
199*7c478bd9Sstevel@tonic-gate buf[0] = *p;
200*7c478bd9Sstevel@tonic-gate buf[1] = '\0';
201*7c478bd9Sstevel@tonic-gate
202*7c478bd9Sstevel@tonic-gate UPDATE_LIMITS(buf);
203*7c478bd9Sstevel@tonic-gate } else {
204*7c478bd9Sstevel@tonic-gate /*
205*7c478bd9Sstevel@tonic-gate * This is a relative pathname, initial values
206*7c478bd9Sstevel@tonic-gate * are relative to the current directory
207*7c478bd9Sstevel@tonic-gate */
208*7c478bd9Sstevel@tonic-gate UPDATE_LIMITS(".");
209*7c478bd9Sstevel@tonic-gate }
210*7c478bd9Sstevel@tonic-gate
211*7c478bd9Sstevel@tonic-gate /*
212*7c478bd9Sstevel@tonic-gate * Check to make sure that the pathname doesn't exceed the
213*7c478bd9Sstevel@tonic-gate * current PATH_MAX
214*7c478bd9Sstevel@tonic-gate */
215*7c478bd9Sstevel@tonic-gate if (pathMax != -1 && strlen(p) > (size_t)pathMax) {
216*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(pathtoolong), path);
217*7c478bd9Sstevel@tonic-gate return (1);
218*7c478bd9Sstevel@tonic-gate }
219*7c478bd9Sstevel@tonic-gate
220*7c478bd9Sstevel@tonic-gate
221*7c478bd9Sstevel@tonic-gate /*
222*7c478bd9Sstevel@tonic-gate * Now spin around checking all the prefixes of
223*7c478bd9Sstevel@tonic-gate * the pathname, until we hit the end of the
224*7c478bd9Sstevel@tonic-gate * argument
225*7c478bd9Sstevel@tonic-gate */
226*7c478bd9Sstevel@tonic-gate while (*p != '\0') {
227*7c478bd9Sstevel@tonic-gate /*
228*7c478bd9Sstevel@tonic-gate * Find the beginning of the next
229*7c478bd9Sstevel@tonic-gate * component. Assume that
230*7c478bd9Sstevel@tonic-gate * M_FSDELIM('\0') == 0
231*7c478bd9Sstevel@tonic-gate */
232*7c478bd9Sstevel@tonic-gate while (M_FSDELIM(*p))
233*7c478bd9Sstevel@tonic-gate p += 1;
234*7c478bd9Sstevel@tonic-gate
235*7c478bd9Sstevel@tonic-gate if (*p == '\0') {
236*7c478bd9Sstevel@tonic-gate /*
237*7c478bd9Sstevel@tonic-gate * There were trailing fsdelim chars on
238*7c478bd9Sstevel@tonic-gate * the path provided, so we were
239*7c478bd9Sstevel@tonic-gate * finished, we just didn't know it.
240*7c478bd9Sstevel@tonic-gate */
241*7c478bd9Sstevel@tonic-gate return (0);
242*7c478bd9Sstevel@tonic-gate }
243*7c478bd9Sstevel@tonic-gate
244*7c478bd9Sstevel@tonic-gate scomp = p;
245*7c478bd9Sstevel@tonic-gate
246*7c478bd9Sstevel@tonic-gate /*
247*7c478bd9Sstevel@tonic-gate * Find the end of the current component
248*7c478bd9Sstevel@tonic-gate * and check for valid characters in the component
249*7c478bd9Sstevel@tonic-gate */
250*7c478bd9Sstevel@tonic-gate while (*p != '\0' && !M_FSDELIM(*p)) {
251*7c478bd9Sstevel@tonic-gate /*
252*7c478bd9Sstevel@tonic-gate * for pflag: check for PFCS characters
253*7c478bd9Sstevel@tonic-gate * otherwise assume all characters are valid
254*7c478bd9Sstevel@tonic-gate */
255*7c478bd9Sstevel@tonic-gate if (pflag && (strchr(portfsset, *p) == 0)) {
256*7c478bd9Sstevel@tonic-gate if (isprint(*p)) {
257*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
258*7c478bd9Sstevel@tonic-gate gettext(badchar), path, *p, *p);
259*7c478bd9Sstevel@tonic-gate } else {
260*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
261*7c478bd9Sstevel@tonic-gate gettext(badbyte), path, *p);
262*7c478bd9Sstevel@tonic-gate }
263*7c478bd9Sstevel@tonic-gate return (1);
264*7c478bd9Sstevel@tonic-gate }
265*7c478bd9Sstevel@tonic-gate p += 1;
266*7c478bd9Sstevel@tonic-gate }
267*7c478bd9Sstevel@tonic-gate
268*7c478bd9Sstevel@tonic-gate ecomp = p;
269*7c478bd9Sstevel@tonic-gate
270*7c478bd9Sstevel@tonic-gate /*
271*7c478bd9Sstevel@tonic-gate * Make sure that this component does not exceed
272*7c478bd9Sstevel@tonic-gate * NAME_MAX in the current prefix directory
273*7c478bd9Sstevel@tonic-gate */
274*7c478bd9Sstevel@tonic-gate if ((nameMax != -1) && (ecomp - scomp > nameMax)) {
275*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(nametoolong), scomp);
276*7c478bd9Sstevel@tonic-gate return (1);
277*7c478bd9Sstevel@tonic-gate } else if (!pflag && checkStat) {
278*7c478bd9Sstevel@tonic-gate /*
279*7c478bd9Sstevel@tonic-gate * Perform the extra checks that
280*7c478bd9Sstevel@tonic-gate * are required when not just
281*7c478bd9Sstevel@tonic-gate * checking for portability.
282*7c478bd9Sstevel@tonic-gate */
283*7c478bd9Sstevel@tonic-gate struct stat sb;
284*7c478bd9Sstevel@tonic-gate char fsdelim;
285*7c478bd9Sstevel@tonic-gate
286*7c478bd9Sstevel@tonic-gate fsdelim = *ecomp;
287*7c478bd9Sstevel@tonic-gate *ecomp = '\0';
288*7c478bd9Sstevel@tonic-gate
289*7c478bd9Sstevel@tonic-gate if (stat(path, &sb) == -1) {
290*7c478bd9Sstevel@tonic-gate /*
291*7c478bd9Sstevel@tonic-gate * We error out if an
292*7c478bd9Sstevel@tonic-gate * intermediate component
293*7c478bd9Sstevel@tonic-gate * is a file, when we
294*7c478bd9Sstevel@tonic-gate * were expecting a
295*7c478bd9Sstevel@tonic-gate * directory, or it is an
296*7c478bd9Sstevel@tonic-gate * unsearchable directory.
297*7c478bd9Sstevel@tonic-gate */
298*7c478bd9Sstevel@tonic-gate if ((errno == ENOTDIR && fsdelim != '\0') ||
299*7c478bd9Sstevel@tonic-gate (errno == EACCES)) {
300*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(notsrch),
301*7c478bd9Sstevel@tonic-gate path);
302*7c478bd9Sstevel@tonic-gate return (1);
303*7c478bd9Sstevel@tonic-gate } else if (errno == ENOENT) {
304*7c478bd9Sstevel@tonic-gate checkStat = 0;
305*7c478bd9Sstevel@tonic-gate }
306*7c478bd9Sstevel@tonic-gate } else if (S_ISDIR(sb.st_mode)) {
307*7c478bd9Sstevel@tonic-gate /*
308*7c478bd9Sstevel@tonic-gate * If the current prefix is a
309*7c478bd9Sstevel@tonic-gate * directory, then we need to
310*7c478bd9Sstevel@tonic-gate * update the limits for NAME_MAX
311*7c478bd9Sstevel@tonic-gate * for the next component and the suffix.
312*7c478bd9Sstevel@tonic-gate */
313*7c478bd9Sstevel@tonic-gate if (checkPathConf(path, _PC_NAME_MAX,
314*7c478bd9Sstevel@tonic-gate &nameMax)) {
315*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
316*7c478bd9Sstevel@tonic-gate gettext(notsrch), path);
317*7c478bd9Sstevel@tonic-gate return (1);
318*7c478bd9Sstevel@tonic-gate }
319*7c478bd9Sstevel@tonic-gate }
320*7c478bd9Sstevel@tonic-gate
321*7c478bd9Sstevel@tonic-gate /*
322*7c478bd9Sstevel@tonic-gate * restore the fsdelim char that we
323*7c478bd9Sstevel@tonic-gate * stomped to produce a prefix.
324*7c478bd9Sstevel@tonic-gate */
325*7c478bd9Sstevel@tonic-gate *ecomp = fsdelim;
326*7c478bd9Sstevel@tonic-gate } /* if (we need to stat the path) */
327*7c478bd9Sstevel@tonic-gate } /* while (more of this path to check) */
328*7c478bd9Sstevel@tonic-gate
329*7c478bd9Sstevel@tonic-gate /*
330*7c478bd9Sstevel@tonic-gate * We successfully traversed the whole pathname
331*7c478bd9Sstevel@tonic-gate */
332*7c478bd9Sstevel@tonic-gate return (0);
333*7c478bd9Sstevel@tonic-gate }
334*7c478bd9Sstevel@tonic-gate
335*7c478bd9Sstevel@tonic-gate void
usage()336*7c478bd9Sstevel@tonic-gate usage()
337*7c478bd9Sstevel@tonic-gate {
338*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("usage: pathchk [-p] pathname ..."));
339*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\n");
340*7c478bd9Sstevel@tonic-gate exit(2);
341*7c478bd9Sstevel@tonic-gate }
342