1 1.2 joerg /* $NetBSD: pathchk.c,v 1.2 2011/09/16 15:39:28 joerg Exp $ */ 2 1.1 jdolecek 3 1.1 jdolecek /*- 4 1.1 jdolecek * Copyright (c) 2002 Tim J. Robbins. 5 1.1 jdolecek * All rights reserved. 6 1.1 jdolecek * 7 1.1 jdolecek * Redistribution and use in source and binary forms, with or without 8 1.1 jdolecek * modification, are permitted provided that the following conditions 9 1.1 jdolecek * are met: 10 1.1 jdolecek * 1. Redistributions of source code must retain the above copyright 11 1.1 jdolecek * notice, this list of conditions and the following disclaimer. 12 1.1 jdolecek * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 jdolecek * notice, this list of conditions and the following disclaimer in the 14 1.1 jdolecek * documentation and/or other materials provided with the distribution. 15 1.1 jdolecek * 16 1.1 jdolecek * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 1.1 jdolecek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 1.1 jdolecek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 1.1 jdolecek * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 1.1 jdolecek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 1.1 jdolecek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 1.1 jdolecek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 1.1 jdolecek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 1.1 jdolecek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 jdolecek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 jdolecek * SUCH DAMAGE. 27 1.1 jdolecek * 28 1.1 jdolecek * from FreeBSD: pathchk.c,v 1.4 2002/12/15 00:40:47 tjr Exp 29 1.1 jdolecek */ 30 1.1 jdolecek 31 1.1 jdolecek #include <sys/cdefs.h> 32 1.1 jdolecek #ifndef lint 33 1.2 joerg __RCSID("$NetBSD: pathchk.c,v 1.2 2011/09/16 15:39:28 joerg Exp $"); 34 1.1 jdolecek #endif /* !lint */ 35 1.1 jdolecek 36 1.1 jdolecek #include <sys/types.h> 37 1.1 jdolecek #include <sys/stat.h> 38 1.1 jdolecek 39 1.1 jdolecek #include <err.h> 40 1.1 jdolecek #include <errno.h> 41 1.1 jdolecek #include <limits.h> 42 1.1 jdolecek #include <stdio.h> 43 1.1 jdolecek #include <stdlib.h> 44 1.1 jdolecek #include <string.h> 45 1.1 jdolecek #include <unistd.h> 46 1.1 jdolecek 47 1.1 jdolecek static int check(const char *); 48 1.1 jdolecek static int portable(const char *); 49 1.2 joerg __dead static void usage(void); 50 1.1 jdolecek 51 1.1 jdolecek static int pflag; /* Perform portability checks */ 52 1.1 jdolecek 53 1.1 jdolecek int 54 1.1 jdolecek main(int argc, char *argv[]) 55 1.1 jdolecek { 56 1.1 jdolecek int ch, rval; 57 1.1 jdolecek const char *arg; 58 1.1 jdolecek 59 1.1 jdolecek while ((ch = getopt(argc, argv, "p")) > 0) { 60 1.1 jdolecek switch (ch) { 61 1.1 jdolecek case 'p': 62 1.1 jdolecek pflag = 1; 63 1.1 jdolecek break; 64 1.1 jdolecek default: 65 1.1 jdolecek usage(); 66 1.1 jdolecek /*NOTREACHED*/ 67 1.1 jdolecek } 68 1.1 jdolecek } 69 1.1 jdolecek argc -= optind; 70 1.1 jdolecek argv += optind; 71 1.1 jdolecek 72 1.1 jdolecek if (argc == 0) 73 1.1 jdolecek usage(); 74 1.1 jdolecek 75 1.1 jdolecek rval = 0; 76 1.1 jdolecek while ((arg = *argv++) != NULL) 77 1.1 jdolecek rval |= check(arg); 78 1.1 jdolecek 79 1.1 jdolecek exit(rval); 80 1.1 jdolecek } 81 1.1 jdolecek 82 1.1 jdolecek static void 83 1.1 jdolecek usage(void) 84 1.1 jdolecek { 85 1.1 jdolecek 86 1.1 jdolecek fprintf(stderr, "usage: pathchk [-p] pathname...\n"); 87 1.1 jdolecek exit(1); 88 1.1 jdolecek } 89 1.1 jdolecek 90 1.1 jdolecek static int 91 1.1 jdolecek check(const char *path) 92 1.1 jdolecek { 93 1.1 jdolecek struct stat sb; 94 1.1 jdolecek long complen, namemax, pathmax, svnamemax; 95 1.1 jdolecek int badch, last; 96 1.1 jdolecek char *end, *p, *pathd; 97 1.1 jdolecek 98 1.1 jdolecek if ((pathd = strdup(path)) == NULL) 99 1.1 jdolecek err(1, "strdup"); 100 1.1 jdolecek 101 1.1 jdolecek p = pathd; 102 1.1 jdolecek 103 1.1 jdolecek if (!pflag) { 104 1.1 jdolecek errno = 0; 105 1.1 jdolecek namemax = pathconf(*p == '/' ? "/" : ".", _PC_NAME_MAX); 106 1.1 jdolecek if (namemax == -1 && errno != 0) 107 1.1 jdolecek namemax = NAME_MAX; 108 1.1 jdolecek } else 109 1.1 jdolecek namemax = _POSIX_NAME_MAX; 110 1.1 jdolecek 111 1.1 jdolecek for (;;) { 112 1.1 jdolecek p += strspn(p, "/"); 113 1.1 jdolecek complen = (long)strcspn(p, "/"); 114 1.1 jdolecek end = p + complen; 115 1.1 jdolecek last = *end == '\0'; 116 1.1 jdolecek *end = '\0'; 117 1.1 jdolecek 118 1.1 jdolecek if (namemax != -1 && complen > namemax) { 119 1.1 jdolecek warnx("%s: %s: component too long (limit %ld)", path, 120 1.1 jdolecek p, namemax); 121 1.1 jdolecek goto bad; 122 1.1 jdolecek } 123 1.1 jdolecek 124 1.1 jdolecek if (!pflag && stat(pathd, &sb) == -1 && errno != ENOENT) { 125 1.1 jdolecek warn("%s: %.*s", path, (int)(strlen(pathd) - 126 1.1 jdolecek complen - 1), pathd); 127 1.1 jdolecek goto bad; 128 1.1 jdolecek } 129 1.1 jdolecek 130 1.1 jdolecek if (pflag && (badch = portable(p)) >= 0) { 131 1.1 jdolecek warnx("%s: %s: component contains non-portable " 132 1.1 jdolecek "character `%c'", path, p, badch); 133 1.1 jdolecek goto bad; 134 1.1 jdolecek } 135 1.1 jdolecek 136 1.1 jdolecek if (last) 137 1.1 jdolecek break; 138 1.1 jdolecek 139 1.1 jdolecek if (!pflag) { 140 1.1 jdolecek errno = 0; 141 1.1 jdolecek svnamemax = namemax; 142 1.1 jdolecek namemax = pathconf(pathd, _PC_NAME_MAX); 143 1.1 jdolecek if (namemax == -1 && errno != 0) 144 1.1 jdolecek namemax = svnamemax; 145 1.1 jdolecek } 146 1.1 jdolecek 147 1.1 jdolecek *end = '/'; 148 1.1 jdolecek p = end + 1; 149 1.1 jdolecek } 150 1.1 jdolecek 151 1.1 jdolecek if (!pflag) { 152 1.1 jdolecek errno = 0; 153 1.1 jdolecek pathmax = pathconf(path, _PC_PATH_MAX); 154 1.1 jdolecek if (pathmax == -1 && errno != 0) 155 1.1 jdolecek pathmax = PATH_MAX; 156 1.1 jdolecek } else 157 1.1 jdolecek pathmax = _POSIX_PATH_MAX; 158 1.1 jdolecek if (pathmax != -1 && strlen(path) >= (size_t)pathmax) { 159 1.1 jdolecek warnx("%s: path too long (limit %ld)", path, pathmax - 1); 160 1.1 jdolecek goto bad; 161 1.1 jdolecek } 162 1.1 jdolecek 163 1.1 jdolecek free(pathd); 164 1.1 jdolecek return (0); 165 1.1 jdolecek 166 1.1 jdolecek bad: free(pathd); 167 1.1 jdolecek return (1); 168 1.1 jdolecek } 169 1.1 jdolecek 170 1.1 jdolecek /* 171 1.1 jdolecek * Check whether a path component contains only portable characters. Return 172 1.1 jdolecek * the first non-portable character found. 173 1.1 jdolecek */ 174 1.1 jdolecek static int 175 1.1 jdolecek portable(const char *path) 176 1.1 jdolecek { 177 1.1 jdolecek static const char charset[] = 178 1.1 jdolecek "abcdefghijklmnopqrstuvwxyz" 179 1.1 jdolecek "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 180 1.1 jdolecek "0123456789._-"; 181 1.1 jdolecek long s; 182 1.1 jdolecek 183 1.1 jdolecek if (*path == '-') 184 1.1 jdolecek return (*path); 185 1.1 jdolecek 186 1.1 jdolecek s = strspn(path, charset); 187 1.1 jdolecek if (path[s] != '\0') 188 1.1 jdolecek return (path[s]); 189 1.1 jdolecek 190 1.1 jdolecek return (-1); 191 1.1 jdolecek } 192