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