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 #pragma ident "%Z%%M% %I% %E% SMI" 23*7c478bd9Sstevel@tonic-gate 24*7c478bd9Sstevel@tonic-gate /* 25*7c478bd9Sstevel@tonic-gate * Copyright (c) 1987 by Sun Microsystems, Inc. 26*7c478bd9Sstevel@tonic-gate */ 27*7c478bd9Sstevel@tonic-gate 28*7c478bd9Sstevel@tonic-gate #include <strings.h> 29*7c478bd9Sstevel@tonic-gate #include <sys/param.h> 30*7c478bd9Sstevel@tonic-gate #include <errno.h> 31*7c478bd9Sstevel@tonic-gate 32*7c478bd9Sstevel@tonic-gate extern char *getwd(); 33*7c478bd9Sstevel@tonic-gate 34*7c478bd9Sstevel@tonic-gate /* LINTLIBRARY */ 35*7c478bd9Sstevel@tonic-gate 36*7c478bd9Sstevel@tonic-gate /* 37*7c478bd9Sstevel@tonic-gate * Input name in raw, canonicalized pathname output to canon. If dosymlinks 38*7c478bd9Sstevel@tonic-gate * is nonzero, resolves all symbolic links encountered during canonicalization 39*7c478bd9Sstevel@tonic-gate * into an equivalent symlink-free form. Returns 0 on success, -1 on failure. 40*7c478bd9Sstevel@tonic-gate * The routine fails if the current working directory can't be obtained or if 41*7c478bd9Sstevel@tonic-gate * either of the arguments is NULL. 42*7c478bd9Sstevel@tonic-gate * 43*7c478bd9Sstevel@tonic-gate * Sets errno on failure. 44*7c478bd9Sstevel@tonic-gate */ 45*7c478bd9Sstevel@tonic-gate int 46*7c478bd9Sstevel@tonic-gate pathcanon(raw, canon, dosymlinks) 47*7c478bd9Sstevel@tonic-gate char *raw, 48*7c478bd9Sstevel@tonic-gate *canon; 49*7c478bd9Sstevel@tonic-gate int dosymlinks; 50*7c478bd9Sstevel@tonic-gate { 51*7c478bd9Sstevel@tonic-gate register char *s, 52*7c478bd9Sstevel@tonic-gate *d; 53*7c478bd9Sstevel@tonic-gate register char *limit = canon + MAXPATHLEN; 54*7c478bd9Sstevel@tonic-gate char *modcanon; 55*7c478bd9Sstevel@tonic-gate int nlink = 0; 56*7c478bd9Sstevel@tonic-gate 57*7c478bd9Sstevel@tonic-gate /* 58*7c478bd9Sstevel@tonic-gate * Do a bit of sanity checking. 59*7c478bd9Sstevel@tonic-gate */ 60*7c478bd9Sstevel@tonic-gate if (raw == NULL || canon == NULL) { 61*7c478bd9Sstevel@tonic-gate errno = EINVAL; 62*7c478bd9Sstevel@tonic-gate return (-1); 63*7c478bd9Sstevel@tonic-gate } 64*7c478bd9Sstevel@tonic-gate 65*7c478bd9Sstevel@tonic-gate /* 66*7c478bd9Sstevel@tonic-gate * If the path in raw is not already absolute, convert it to that form. 67*7c478bd9Sstevel@tonic-gate * In any case, initialize canon with the absolute form of raw. Make 68*7c478bd9Sstevel@tonic-gate * sure that none of the operations overflow the corresponding buffers. 69*7c478bd9Sstevel@tonic-gate * The code below does the copy operations by hand so that it can easily 70*7c478bd9Sstevel@tonic-gate * keep track of whether overflow is about to occur. 71*7c478bd9Sstevel@tonic-gate */ 72*7c478bd9Sstevel@tonic-gate s = raw; 73*7c478bd9Sstevel@tonic-gate d = canon; 74*7c478bd9Sstevel@tonic-gate if (*s != '/') { 75*7c478bd9Sstevel@tonic-gate /* Relative; prepend the working directory. */ 76*7c478bd9Sstevel@tonic-gate if (getwd(d) == NULL) { 77*7c478bd9Sstevel@tonic-gate /* Use whatever errno value getwd may have left around. */ 78*7c478bd9Sstevel@tonic-gate return (-1); 79*7c478bd9Sstevel@tonic-gate } 80*7c478bd9Sstevel@tonic-gate d += strlen(d); 81*7c478bd9Sstevel@tonic-gate /* Add slash to separate working directory from relative part. */ 82*7c478bd9Sstevel@tonic-gate if (d < limit) 83*7c478bd9Sstevel@tonic-gate *d++ = '/'; 84*7c478bd9Sstevel@tonic-gate modcanon = d; 85*7c478bd9Sstevel@tonic-gate } else 86*7c478bd9Sstevel@tonic-gate modcanon = canon; 87*7c478bd9Sstevel@tonic-gate while (d < limit && *s) 88*7c478bd9Sstevel@tonic-gate *d++ = *s++; 89*7c478bd9Sstevel@tonic-gate 90*7c478bd9Sstevel@tonic-gate /* Add a trailing slash to simplify the code below. */ 91*7c478bd9Sstevel@tonic-gate s = "/"; 92*7c478bd9Sstevel@tonic-gate while (d < limit && (*d++ = *s++)) 93*7c478bd9Sstevel@tonic-gate continue; 94*7c478bd9Sstevel@tonic-gate 95*7c478bd9Sstevel@tonic-gate 96*7c478bd9Sstevel@tonic-gate /* 97*7c478bd9Sstevel@tonic-gate * Canonicalize the path. The strategy is to update in place, with 98*7c478bd9Sstevel@tonic-gate * d pointing to the end of the canonicalized portion and s to the 99*7c478bd9Sstevel@tonic-gate * current spot from which we're copying. This works because 100*7c478bd9Sstevel@tonic-gate * canonicalization doesn't increase path length, except as discussed 101*7c478bd9Sstevel@tonic-gate * below. Note also that the path has had a slash added at its end. 102*7c478bd9Sstevel@tonic-gate * This greatly simplifies the treatment of boundary conditions. 103*7c478bd9Sstevel@tonic-gate */ 104*7c478bd9Sstevel@tonic-gate d = s = modcanon; 105*7c478bd9Sstevel@tonic-gate while (d < limit && *s) { 106*7c478bd9Sstevel@tonic-gate if ((*d++ = *s++) == '/' && d > canon + 1) { 107*7c478bd9Sstevel@tonic-gate register char *t = d - 2; 108*7c478bd9Sstevel@tonic-gate 109*7c478bd9Sstevel@tonic-gate switch (*t) { 110*7c478bd9Sstevel@tonic-gate case '/': 111*7c478bd9Sstevel@tonic-gate /* Found // in the name. */ 112*7c478bd9Sstevel@tonic-gate d--; 113*7c478bd9Sstevel@tonic-gate continue; 114*7c478bd9Sstevel@tonic-gate case '.': 115*7c478bd9Sstevel@tonic-gate switch (*--t) { 116*7c478bd9Sstevel@tonic-gate case '/': 117*7c478bd9Sstevel@tonic-gate /* Found /./ in the name. */ 118*7c478bd9Sstevel@tonic-gate d -= 2; 119*7c478bd9Sstevel@tonic-gate continue; 120*7c478bd9Sstevel@tonic-gate case '.': 121*7c478bd9Sstevel@tonic-gate if (*--t == '/') { 122*7c478bd9Sstevel@tonic-gate /* Found /../ in the name. */ 123*7c478bd9Sstevel@tonic-gate while (t > canon && *--t != '/') 124*7c478bd9Sstevel@tonic-gate continue; 125*7c478bd9Sstevel@tonic-gate d = t + 1; 126*7c478bd9Sstevel@tonic-gate } 127*7c478bd9Sstevel@tonic-gate continue; 128*7c478bd9Sstevel@tonic-gate default: 129*7c478bd9Sstevel@tonic-gate break; 130*7c478bd9Sstevel@tonic-gate } 131*7c478bd9Sstevel@tonic-gate break; 132*7c478bd9Sstevel@tonic-gate default: 133*7c478bd9Sstevel@tonic-gate break; 134*7c478bd9Sstevel@tonic-gate } 135*7c478bd9Sstevel@tonic-gate /* 136*7c478bd9Sstevel@tonic-gate * We're at the end of a component. If dosymlinks is set 137*7c478bd9Sstevel@tonic-gate * see whether the component is a symbolic link. If so, 138*7c478bd9Sstevel@tonic-gate * replace it by its contents. 139*7c478bd9Sstevel@tonic-gate */ 140*7c478bd9Sstevel@tonic-gate if (dosymlinks) { 141*7c478bd9Sstevel@tonic-gate char link[MAXPATHLEN + 1]; 142*7c478bd9Sstevel@tonic-gate register int llen; 143*7c478bd9Sstevel@tonic-gate 144*7c478bd9Sstevel@tonic-gate /* 145*7c478bd9Sstevel@tonic-gate * See whether it's a symlink by trying to read it. 146*7c478bd9Sstevel@tonic-gate * 147*7c478bd9Sstevel@tonic-gate * Start by isolating it. 148*7c478bd9Sstevel@tonic-gate */ 149*7c478bd9Sstevel@tonic-gate *(d - 1) = '\0'; 150*7c478bd9Sstevel@tonic-gate if ((llen = readlink(canon, link, sizeof link)) >= 0) { 151*7c478bd9Sstevel@tonic-gate /* Make sure that there are no circular links. */ 152*7c478bd9Sstevel@tonic-gate nlink++; 153*7c478bd9Sstevel@tonic-gate if (nlink > MAXSYMLINKS) { 154*7c478bd9Sstevel@tonic-gate errno = ELOOP; 155*7c478bd9Sstevel@tonic-gate return (-1); 156*7c478bd9Sstevel@tonic-gate } 157*7c478bd9Sstevel@tonic-gate /* 158*7c478bd9Sstevel@tonic-gate * The component is a symlink. Since its value can be 159*7c478bd9Sstevel@tonic-gate * of arbitrary size, we can't continue copying in place. 160*7c478bd9Sstevel@tonic-gate * Instead, form the new path suffix in the link buffer 161*7c478bd9Sstevel@tonic-gate * and then copy it back to its proper spot in canon. 162*7c478bd9Sstevel@tonic-gate */ 163*7c478bd9Sstevel@tonic-gate t = link + llen; 164*7c478bd9Sstevel@tonic-gate *t++ = '/'; 165*7c478bd9Sstevel@tonic-gate /* 166*7c478bd9Sstevel@tonic-gate * Copy the remaining unresolved portion to the end 167*7c478bd9Sstevel@tonic-gate * of the symlink. If the sum of the unresolved part and 168*7c478bd9Sstevel@tonic-gate * the readlink exceeds MAXPATHLEN, the extra bytes 169*7c478bd9Sstevel@tonic-gate * will be dropped off. Too bad! 170*7c478bd9Sstevel@tonic-gate */ 171*7c478bd9Sstevel@tonic-gate (void) strncpy(t, s, sizeof link - llen - 1); 172*7c478bd9Sstevel@tonic-gate link[sizeof link - 1] = '\0'; 173*7c478bd9Sstevel@tonic-gate /* 174*7c478bd9Sstevel@tonic-gate * If the link's contents are absolute, copy it back 175*7c478bd9Sstevel@tonic-gate * to the start of canon, otherwise to the beginning of 176*7c478bd9Sstevel@tonic-gate * the link's position in the path. 177*7c478bd9Sstevel@tonic-gate */ 178*7c478bd9Sstevel@tonic-gate if (link[0] == '/') { 179*7c478bd9Sstevel@tonic-gate /* Absolute. */ 180*7c478bd9Sstevel@tonic-gate (void) strcpy(canon, link); 181*7c478bd9Sstevel@tonic-gate d = s = canon; 182*7c478bd9Sstevel@tonic-gate } 183*7c478bd9Sstevel@tonic-gate else { 184*7c478bd9Sstevel@tonic-gate /* 185*7c478bd9Sstevel@tonic-gate * Relative: find beginning of component and copy. 186*7c478bd9Sstevel@tonic-gate */ 187*7c478bd9Sstevel@tonic-gate --d; 188*7c478bd9Sstevel@tonic-gate while (d > canon && *--d != '/') 189*7c478bd9Sstevel@tonic-gate continue; 190*7c478bd9Sstevel@tonic-gate s = ++d; 191*7c478bd9Sstevel@tonic-gate /* 192*7c478bd9Sstevel@tonic-gate * If the sum of the resolved part, the readlink 193*7c478bd9Sstevel@tonic-gate * and the remaining unresolved part exceeds 194*7c478bd9Sstevel@tonic-gate * MAXPATHLEN, the extra bytes will be dropped off. 195*7c478bd9Sstevel@tonic-gate */ 196*7c478bd9Sstevel@tonic-gate if (strlen(link) >= (limit - s)) { 197*7c478bd9Sstevel@tonic-gate (void) strncpy(s, link, limit - s); 198*7c478bd9Sstevel@tonic-gate *(limit - 1) = '\0'; 199*7c478bd9Sstevel@tonic-gate } else { 200*7c478bd9Sstevel@tonic-gate (void) strcpy(s, link); 201*7c478bd9Sstevel@tonic-gate } 202*7c478bd9Sstevel@tonic-gate } 203*7c478bd9Sstevel@tonic-gate continue; 204*7c478bd9Sstevel@tonic-gate } else { 205*7c478bd9Sstevel@tonic-gate /* 206*7c478bd9Sstevel@tonic-gate * readlink call failed. It can be because it was 207*7c478bd9Sstevel@tonic-gate * not a link (i.e. a file, dir etc.) or because the 208*7c478bd9Sstevel@tonic-gate * the call actually failed. 209*7c478bd9Sstevel@tonic-gate */ 210*7c478bd9Sstevel@tonic-gate if (errno != EINVAL) 211*7c478bd9Sstevel@tonic-gate return (-1); 212*7c478bd9Sstevel@tonic-gate *(d - 1) = '/'; /* Restore it */ 213*7c478bd9Sstevel@tonic-gate } 214*7c478bd9Sstevel@tonic-gate } /* if (dosymlinks) */ 215*7c478bd9Sstevel@tonic-gate } 216*7c478bd9Sstevel@tonic-gate } /* while */ 217*7c478bd9Sstevel@tonic-gate 218*7c478bd9Sstevel@tonic-gate /* Remove the trailing slash that was added above. */ 219*7c478bd9Sstevel@tonic-gate if (*(d - 1) == '/' && d > canon + 1) 220*7c478bd9Sstevel@tonic-gate d--; 221*7c478bd9Sstevel@tonic-gate *d = '\0'; 222*7c478bd9Sstevel@tonic-gate return (0); 223*7c478bd9Sstevel@tonic-gate } 224*7c478bd9Sstevel@tonic-gate 225*7c478bd9Sstevel@tonic-gate /* 226*7c478bd9Sstevel@tonic-gate * Canonicalize the path given in raw, resolving away all symbolic link 227*7c478bd9Sstevel@tonic-gate * components. Store the result into the buffer named by canon, which 228*7c478bd9Sstevel@tonic-gate * must be long enough (MAXPATHLEN bytes will suffice). Returns NULL 229*7c478bd9Sstevel@tonic-gate * on failure and canon on success. 230*7c478bd9Sstevel@tonic-gate * 231*7c478bd9Sstevel@tonic-gate * The routine indirectly invokes the readlink() system call and getwd() 232*7c478bd9Sstevel@tonic-gate * so it inherits the possibility of hanging due to inaccessible file 233*7c478bd9Sstevel@tonic-gate * system resources. 234*7c478bd9Sstevel@tonic-gate */ 235*7c478bd9Sstevel@tonic-gate char * 236*7c478bd9Sstevel@tonic-gate realpath(raw, canon) 237*7c478bd9Sstevel@tonic-gate char *raw; 238*7c478bd9Sstevel@tonic-gate char *canon; 239*7c478bd9Sstevel@tonic-gate { 240*7c478bd9Sstevel@tonic-gate return (pathcanon(raw, canon, 1) < 0 ? NULL : canon); 241*7c478bd9Sstevel@tonic-gate } 242