17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
23*bc54f855SJohn Levon * Copyright (c) 1994 by Sun Microsystems, Inc.
24*bc54f855SJohn Levon * Copyright (c) 2018, Joyent, Inc.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate * Copyright 1991, 1992 by Mortice Kern Systems Inc. All rights reserved.
297c478bd9Sstevel@tonic-gate *
307c478bd9Sstevel@tonic-gate * Standards Conformance :
317c478bd9Sstevel@tonic-gate * P1003.2/D11.2
327c478bd9Sstevel@tonic-gate *
337c478bd9Sstevel@tonic-gate */
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate * Original ident string for reference
367c478bd9Sstevel@tonic-gate * ident "$Id: pathchk.c,v 1.29 1994/05/24 15:51:19 mark Exp $"
377c478bd9Sstevel@tonic-gate */
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate #include <locale.h>
407c478bd9Sstevel@tonic-gate #include <libintl.h>
417c478bd9Sstevel@tonic-gate #include <limits.h>
427c478bd9Sstevel@tonic-gate #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <fcntl.h> /* for creat() prototype */
447c478bd9Sstevel@tonic-gate #include <string.h>
457c478bd9Sstevel@tonic-gate #include <errno.h>
467c478bd9Sstevel@tonic-gate #include <stdlib.h>
477c478bd9Sstevel@tonic-gate #include <stdio.h>
487c478bd9Sstevel@tonic-gate #include <ctype.h>
497c478bd9Sstevel@tonic-gate #include <unistd.h>
507c478bd9Sstevel@tonic-gate #include <stdlib.h>
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate * These are the characters in the portable filename character set defined
547c478bd9Sstevel@tonic-gate * in POSIX P1003.2.
557c478bd9Sstevel@tonic-gate */
567c478bd9Sstevel@tonic-gate static char portfsset[] = \
577c478bd9Sstevel@tonic-gate "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-";
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate #ifndef M_FSDELIM
617c478bd9Sstevel@tonic-gate #define M_FSDELIM(c) ((c) == '/')
627c478bd9Sstevel@tonic-gate #endif
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate static char *nametoolong = "%s: component too long.\n";
657c478bd9Sstevel@tonic-gate static char *pathtoolong = "%s: pathname too long.\n";
667c478bd9Sstevel@tonic-gate static char *notsrch = "%s: Not searchable.\n";
677c478bd9Sstevel@tonic-gate static char *badchar = "%s: Nonportable character '%c' (%#02X) found.\n";
687c478bd9Sstevel@tonic-gate static char *badbyte = "%s: Nonportable byte %#02X found.\n";
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate static char *pathconfprob = "pathchk: warning: \
717c478bd9Sstevel@tonic-gate pathconf(\"%s\", %s) returns '%s'. Using %s = %d\n";
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate static int printWarnings = 1;
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate static int checkpathname(char *, int);
777c478bd9Sstevel@tonic-gate static void usage(void);
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate * mainline for pathchk
817c478bd9Sstevel@tonic-gate */
827c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)837c478bd9Sstevel@tonic-gate main(int argc, char **argv)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate int c;
867c478bd9Sstevel@tonic-gate int errors;
877c478bd9Sstevel@tonic-gate int pflag = 0;
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
907c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
917c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
927c478bd9Sstevel@tonic-gate #endif
937c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "pw")) != EOF) {
977c478bd9Sstevel@tonic-gate switch (c) {
987c478bd9Sstevel@tonic-gate case 'p':
997c478bd9Sstevel@tonic-gate pflag = 1;
1007c478bd9Sstevel@tonic-gate break;
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate case 'w':
1037c478bd9Sstevel@tonic-gate /* turn off warning messages */
1047c478bd9Sstevel@tonic-gate printWarnings = 0;
1057c478bd9Sstevel@tonic-gate break;
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate default:
1087c478bd9Sstevel@tonic-gate usage();
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate argv += optind;
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate if (*argv == 0) {
1157c478bd9Sstevel@tonic-gate usage();
1167c478bd9Sstevel@tonic-gate /* NOTREACHED */
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate errors = 0;
1207c478bd9Sstevel@tonic-gate while (*argv) {
1217c478bd9Sstevel@tonic-gate errors += checkpathname(*argv, pflag);
1227c478bd9Sstevel@tonic-gate argv += 1;
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate return (errors);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate * checkPathConf(const char *, int, long *)
1307c478bd9Sstevel@tonic-gate *
1317c478bd9Sstevel@tonic-gate * Calls pathconf(), and returns 1 if pathconf failed, zero
1327c478bd9Sstevel@tonic-gate * otherwise. If pathconf() succeeded, then *valp contains the
1337c478bd9Sstevel@tonic-gate * value returned
1347c478bd9Sstevel@tonic-gate */
1357c478bd9Sstevel@tonic-gate static int
checkPathConf(const char * path,int type,long * valp)1367c478bd9Sstevel@tonic-gate checkPathConf(const char *path, int type, long *valp)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate errno = 0;
1397c478bd9Sstevel@tonic-gate *valp = pathconf(path, type);
1407c478bd9Sstevel@tonic-gate if ((*valp == -1) && (errno != 0) && (errno != EACCES)) {
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate * pathconf() is not supported on some mounted filesystems
1437c478bd9Sstevel@tonic-gate * (e.g NFS mounts) and pathconf() is known to fail.
1447c478bd9Sstevel@tonic-gate * So, we print a warning and use the POSIX default values.
1457c478bd9Sstevel@tonic-gate */
1467c478bd9Sstevel@tonic-gate if (type == _PC_PATH_MAX)
1477c478bd9Sstevel@tonic-gate *valp = _POSIX_PATH_MAX;
1487c478bd9Sstevel@tonic-gate else
1497c478bd9Sstevel@tonic-gate *valp = _POSIX_NAME_MAX;
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate if (printWarnings) {
1527c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(pathconfprob), path,
1537c478bd9Sstevel@tonic-gate type == _PC_PATH_MAX?"_PC_PATH_MAX" :
1547c478bd9Sstevel@tonic-gate "_PC_NAME_MAX", strerror(errno),
1557c478bd9Sstevel@tonic-gate type == _PC_PATH_MAX ? "PATH_MAX" : "NAME_MAX",
1567c478bd9Sstevel@tonic-gate *valp);
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate return ((*valp == -1) && (errno != 0));
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate #define UPDATE_LIMITS(buf)\
1647c478bd9Sstevel@tonic-gate {\
1657c478bd9Sstevel@tonic-gate if (pflag) {\
1667c478bd9Sstevel@tonic-gate nameMax = _POSIX_NAME_MAX;\
1677c478bd9Sstevel@tonic-gate pathMax = _POSIX_PATH_MAX;\
1687c478bd9Sstevel@tonic-gate } else if (checkPathConf((buf), _PC_PATH_MAX, &pathMax) || \
1697c478bd9Sstevel@tonic-gate checkPathConf((buf), _PC_NAME_MAX, &nameMax)) {\
1707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(notsrch), buf);\
1717c478bd9Sstevel@tonic-gate return (1);\
1727c478bd9Sstevel@tonic-gate }\
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate * checkpathname(char *pname)
1777c478bd9Sstevel@tonic-gate * pathchk a single pathname.
1787c478bd9Sstevel@tonic-gate */
1797c478bd9Sstevel@tonic-gate int
checkpathname(char * path,int pflag)1807c478bd9Sstevel@tonic-gate checkpathname(char *path, int pflag)
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate int checkStat;
1837c478bd9Sstevel@tonic-gate long nameMax;
1847c478bd9Sstevel@tonic-gate long pathMax;
1857c478bd9Sstevel@tonic-gate char *scomp;
1867c478bd9Sstevel@tonic-gate char *ecomp;
1877c478bd9Sstevel@tonic-gate register char *p;
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate p = path;
1907c478bd9Sstevel@tonic-gate checkStat = 1;
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate /*
1937c478bd9Sstevel@tonic-gate * Get the initial NAME_MAX and PATH_MAX values
1947c478bd9Sstevel@tonic-gate */
1957c478bd9Sstevel@tonic-gate if (M_FSDELIM(*p)) {
1967c478bd9Sstevel@tonic-gate char buf[2];
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate buf[0] = *p;
1997c478bd9Sstevel@tonic-gate buf[1] = '\0';
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate UPDATE_LIMITS(buf);
2027c478bd9Sstevel@tonic-gate } else {
2037c478bd9Sstevel@tonic-gate /*
2047c478bd9Sstevel@tonic-gate * This is a relative pathname, initial values
2057c478bd9Sstevel@tonic-gate * are relative to the current directory
2067c478bd9Sstevel@tonic-gate */
2077c478bd9Sstevel@tonic-gate UPDATE_LIMITS(".");
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate /*
2117c478bd9Sstevel@tonic-gate * Check to make sure that the pathname doesn't exceed the
2127c478bd9Sstevel@tonic-gate * current PATH_MAX
2137c478bd9Sstevel@tonic-gate */
2147c478bd9Sstevel@tonic-gate if (pathMax != -1 && strlen(p) > (size_t)pathMax) {
2157c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(pathtoolong), path);
2167c478bd9Sstevel@tonic-gate return (1);
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate /*
2217c478bd9Sstevel@tonic-gate * Now spin around checking all the prefixes of
2227c478bd9Sstevel@tonic-gate * the pathname, until we hit the end of the
2237c478bd9Sstevel@tonic-gate * argument
2247c478bd9Sstevel@tonic-gate */
2257c478bd9Sstevel@tonic-gate while (*p != '\0') {
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate * Find the beginning of the next
2287c478bd9Sstevel@tonic-gate * component. Assume that
2297c478bd9Sstevel@tonic-gate * M_FSDELIM('\0') == 0
2307c478bd9Sstevel@tonic-gate */
2317c478bd9Sstevel@tonic-gate while (M_FSDELIM(*p))
2327c478bd9Sstevel@tonic-gate p += 1;
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate if (*p == '\0') {
2357c478bd9Sstevel@tonic-gate /*
2367c478bd9Sstevel@tonic-gate * There were trailing fsdelim chars on
2377c478bd9Sstevel@tonic-gate * the path provided, so we were
2387c478bd9Sstevel@tonic-gate * finished, we just didn't know it.
2397c478bd9Sstevel@tonic-gate */
2407c478bd9Sstevel@tonic-gate return (0);
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate scomp = p;
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate /*
2467c478bd9Sstevel@tonic-gate * Find the end of the current component
2477c478bd9Sstevel@tonic-gate * and check for valid characters in the component
2487c478bd9Sstevel@tonic-gate */
249*bc54f855SJohn Levon while (*p != '\0' && !M_FSDELIM(*p)) {
2507c478bd9Sstevel@tonic-gate /*
2517c478bd9Sstevel@tonic-gate * for pflag: check for PFCS characters
2527c478bd9Sstevel@tonic-gate * otherwise assume all characters are valid
2537c478bd9Sstevel@tonic-gate */
2547c478bd9Sstevel@tonic-gate if (pflag && (strchr(portfsset, *p) == 0)) {
2557c478bd9Sstevel@tonic-gate if (isprint(*p)) {
2567c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2577c478bd9Sstevel@tonic-gate gettext(badchar), path, *p, *p);
2587c478bd9Sstevel@tonic-gate } else {
2597c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2607c478bd9Sstevel@tonic-gate gettext(badbyte), path, *p);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate return (1);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate p += 1;
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate ecomp = p;
2687c478bd9Sstevel@tonic-gate
2697c478bd9Sstevel@tonic-gate /*
2707c478bd9Sstevel@tonic-gate * Make sure that this component does not exceed
2717c478bd9Sstevel@tonic-gate * NAME_MAX in the current prefix directory
2727c478bd9Sstevel@tonic-gate */
2737c478bd9Sstevel@tonic-gate if ((nameMax != -1) && (ecomp - scomp > nameMax)) {
2747c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(nametoolong), scomp);
2757c478bd9Sstevel@tonic-gate return (1);
2767c478bd9Sstevel@tonic-gate } else if (!pflag && checkStat) {
2777c478bd9Sstevel@tonic-gate /*
2787c478bd9Sstevel@tonic-gate * Perform the extra checks that
2797c478bd9Sstevel@tonic-gate * are required when not just
2807c478bd9Sstevel@tonic-gate * checking for portability.
2817c478bd9Sstevel@tonic-gate */
282*bc54f855SJohn Levon struct stat sb;
283*bc54f855SJohn Levon char fsdelim;
2847c478bd9Sstevel@tonic-gate
285*bc54f855SJohn Levon fsdelim = *ecomp;
286*bc54f855SJohn Levon *ecomp = '\0';
2877c478bd9Sstevel@tonic-gate
288*bc54f855SJohn Levon if (stat(path, &sb) == -1) {
2897c478bd9Sstevel@tonic-gate /*
2907c478bd9Sstevel@tonic-gate * We error out if an
2917c478bd9Sstevel@tonic-gate * intermediate component
2927c478bd9Sstevel@tonic-gate * is a file, when we
2937c478bd9Sstevel@tonic-gate * were expecting a
2947c478bd9Sstevel@tonic-gate * directory, or it is an
2957c478bd9Sstevel@tonic-gate * unsearchable directory.
2967c478bd9Sstevel@tonic-gate */
2977c478bd9Sstevel@tonic-gate if ((errno == ENOTDIR && fsdelim != '\0') ||
2987c478bd9Sstevel@tonic-gate (errno == EACCES)) {
2997c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(notsrch),
3007c478bd9Sstevel@tonic-gate path);
3017c478bd9Sstevel@tonic-gate return (1);
3027c478bd9Sstevel@tonic-gate } else if (errno == ENOENT) {
3037c478bd9Sstevel@tonic-gate checkStat = 0;
3047c478bd9Sstevel@tonic-gate }
305*bc54f855SJohn Levon } else if (S_ISDIR(sb.st_mode)) {
3067c478bd9Sstevel@tonic-gate /*
3077c478bd9Sstevel@tonic-gate * If the current prefix is a
3087c478bd9Sstevel@tonic-gate * directory, then we need to
3097c478bd9Sstevel@tonic-gate * update the limits for NAME_MAX
3107c478bd9Sstevel@tonic-gate * for the next component and the suffix.
3117c478bd9Sstevel@tonic-gate */
3127c478bd9Sstevel@tonic-gate if (checkPathConf(path, _PC_NAME_MAX,
3137c478bd9Sstevel@tonic-gate &nameMax)) {
3147c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
3157c478bd9Sstevel@tonic-gate gettext(notsrch), path);
3167c478bd9Sstevel@tonic-gate return (1);
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate /*
3217c478bd9Sstevel@tonic-gate * restore the fsdelim char that we
3227c478bd9Sstevel@tonic-gate * stomped to produce a prefix.
3237c478bd9Sstevel@tonic-gate */
3247c478bd9Sstevel@tonic-gate *ecomp = fsdelim;
3257c478bd9Sstevel@tonic-gate } /* if (we need to stat the path) */
3267c478bd9Sstevel@tonic-gate } /* while (more of this path to check) */
3277c478bd9Sstevel@tonic-gate
3287c478bd9Sstevel@tonic-gate /*
3297c478bd9Sstevel@tonic-gate * We successfully traversed the whole pathname
3307c478bd9Sstevel@tonic-gate */
3317c478bd9Sstevel@tonic-gate return (0);
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate
3347c478bd9Sstevel@tonic-gate void
usage()3357c478bd9Sstevel@tonic-gate usage()
3367c478bd9Sstevel@tonic-gate {
3377c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("usage: pathchk [-p] pathname ..."));
3387c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\n");
3397c478bd9Sstevel@tonic-gate exit(2);
3407c478bd9Sstevel@tonic-gate }
341