5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * CDDL HEADER START
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * The contents of this file are subject to the terms of the
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Common Development and Distribution License (the "License").
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * You may not use this file except in compliance with the License.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * or http://www.opensolaris.org/os/licensing.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * See the License for the specific language governing permissions
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * and limitations under the License.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * When distributing Covered Code, include this CDDL HEADER in each
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * If applicable, add the following below this CDDL HEADER, with the
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * fields enclosed by brackets "[]" replaced with your own identifying
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * information: Portions Copyright [yyyy] [name of copyright owner]
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * CDDL HEADER END
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Use is subject to license terms.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland/* All Rights Reserved */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland#define WRN_SCARYLINK "WARNING: <%s>, target of symlink <%s>, does not exist."
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland#define ERR_PATHLONG "path argument too long"
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland#define ERR_CLASSLONG "classname argument too long"
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland#define ERR_CLASSCHAR "bad character in classname"
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland#define ERR_POPEN "unable to create pipe to <%s>"
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland#define ERR_PCLOSE "unable to close pipe to <%s>"
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland#define ERR_RDLINK "unable to read link for <%s>"
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland#define ERR_MEMORY "memory allocation failure, errno=%d"
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic struct link *firstlink = (struct link *)0;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic struct link *lastlink = (struct link *)0;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic char *scan_raw_ln(char *targ_name, char *link_name);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic int xflag = 0; /* confirm contents of files */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic char construction[PATH_MAX], mylocal[PATH_MAX];
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic void findlink(struct cfent *ept, char *path, char *svpath);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic void output(char *path, int n, char *local);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic void usage(void);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland while ((c = getopt(argc, argv, "xnic:?")) != EOF) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* validate that classname is acceptable */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* follow symlinks */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* bug id 4244631, not ABI compliant */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland abi_sym_ptr = getenv("PKG_NONABI_SYMLINKS");
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (abi_sym_ptr && strncasecmp(abi_sym_ptr, "TRUE", 4) == 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* take path list from stdin */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland while (fgets(path, sizeof (path), stdin) != (char *)NULL) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * remove any trailing newline characters from the end of path
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland while ((len > 0) && (path[len-1] == '\n')) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) strlcpy(entry.pkg_class, def_class, sizeof (entry.pkg_class));
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) strlcpy(entry.path, path, PATH_MAX);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) strlcpy(entry.ainfo.owner, BADOWNER, sizeof (entry.ainfo.owner));
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) strlcpy(entry.ainfo.group, BADGROUP, sizeof (entry.ainfo.group));
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (cverify(0, &entry.ftype, path, &entry.cinfo, 1)) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Use averify to figure out the attributes. This has trouble
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * divining the identity of a symlink which points to a
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * non-existant target. For that reason, if it comes back as
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * an existence problem, we fake in a symlink and see if averify
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * likes that. If it does, all we have is a risky symlink.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if ((s = averify(0, &entry.ftype, path, &entry.ainfo)) == VE_EXIST &&
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland entry.ftype = 's'; /* try again assuming symlink */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* try to read what it points to */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if ((s = readlink(path, mylocal, PATH_MAX)) > 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (averify(0, &entry.ftype, path, &entry.ainfo)) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* It's a link to a file not in this package. */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland } else if (s != 0 && s != VE_CONT)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* replace first n characters with 'local' */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) strlcpy(entry.ainfo.local, entry.path,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) strlcpy(entry.path, local, PATH_MAX);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland n = ((unsigned int)pt - (unsigned int)path - 1);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) snprintf(cmd, sizeof (cmd), "find %s -print", path);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland while (fscanf(pp, "%[^\n]\n", newpath) == 1)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Scan a raw link for origination errors. Given
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * we don't want the link to be verbatim since link_name must be relative
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * to it's source. This functions checks for identical directory paths
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * and if it's clearly a misplaced relative path, the duplicate
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * directories are stripped. This is necessary because pkgadd is actually
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * in the source directory (hlink/path) when it creates the link.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * NOTE : The buffer we get with targ_name is going to be used later
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * and cannot be modified. That's why we have yet another PATH_MAX
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * size buffer in this function.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandscan_raw_ln(char *targ_name, char *link_name)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland char *file_name; /* name of the file in link_name */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland char *this_dir; /* current directory in targ_name */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland char *next_dir; /* next directory in targ_name */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland char *targ_ptr; /* current character in targ_name */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland const_ptr = targ_name; /* Point to here 'til we know it's different. */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * If the link is absolute or it is in the current directory, no
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * further testing necessary.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (file_name = strrchr(link_name, '/')) != NULL) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * This will be walked down to the highest directory
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * not common to both the link and the target.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * At this point targ_name is a relative path through at
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * least one directory.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland this_dir = targ_ptr; /* first directory in targ_name */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland file_name++; /* point to the name not the '/' */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Scan across the pathname until we reach a different
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * directory or the final file name.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland else /* point to the end of the string */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* length to compare */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland str_size = ((ptrdiff_t)next_dir - (ptrdiff_t)this_dir);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * If both paths begin with the same directory, then
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * skip that common directory in both the link and
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * the target.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (strncmp(this_dir, link_name, str_size) == 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* point to the target so far */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* Skip past it in the target */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* Skip past it in the link */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * If these directories don't match then the
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * directory above is the lowest common directory. We
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * need to construct a relative path from the lowest
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * child up to that directory.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* Count the intermediate directories. */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland while ((dptr = strchr(dptr, '/')) != NULL) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Now targ_ptr is pointing to the fork in
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * the path and dptr is pointing to the lowest
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * child in the link. We now insert the
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * appropriate number of "../'s" to get to
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * the first common directory. We'll
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * construct this in the construction
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland while (d--) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland break; /* done */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland } while (link_name != file_name); /* at file name */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandfindlink(struct cfent *ept, char *path, char *svpath)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if ((statbuf.st_mode & S_IFMT) == S_IFLNK) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (n <= 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland for (link = firstlink; link; link = link->next) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if ((new = (struct link *)calloc(1, sizeof (struct link))) == NULL) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland gettext("usage: %s [-i] [-c class] [path ...]\n"), get_prog_name());
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /*NOTREACHED*/