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