xref: /titanic_44/usr/src/cmd/rm/rm.c (revision 996aa81675f6b63ed02041243b97e61ee7bd51d2)
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
579033acbSas145665  * Common Development and Distribution License (the "License").
679033acbSas145665  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
2379033acbSas145665  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27014a7923Sas145665 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28014a7923Sas145665 /*	All Rights Reserved   */
29014a7923Sas145665 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * rm [-fiRr] file ...
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <fcntl.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/stat.h>
417c478bd9Sstevel@tonic-gate #include <dirent.h>
427c478bd9Sstevel@tonic-gate #include <limits.h>
437c478bd9Sstevel@tonic-gate #include <locale.h>
447c478bd9Sstevel@tonic-gate #include <langinfo.h>
457c478bd9Sstevel@tonic-gate #include <unistd.h>
467c478bd9Sstevel@tonic-gate #include <stdlib.h>
477c478bd9Sstevel@tonic-gate #include <errno.h>
487c478bd9Sstevel@tonic-gate #include <sys/resource.h>
49014a7923Sas145665 #include <sys/avl.h>
50014a7923Sas145665 #include <libcmdutils.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #define	ARGCNT		5		/* Number of arguments */
537c478bd9Sstevel@tonic-gate #define	CHILD		0
547c478bd9Sstevel@tonic-gate #define	DIRECTORY	((buffer.st_mode&S_IFMT) == S_IFDIR)
557c478bd9Sstevel@tonic-gate #define	SYMLINK		((buffer.st_mode&S_IFMT) == S_IFLNK)
567c478bd9Sstevel@tonic-gate #define	FAIL		-1
577c478bd9Sstevel@tonic-gate #define	MAXFORK		100		/* Maximum number of forking attempts */
587c478bd9Sstevel@tonic-gate #define	NAMESIZE	MAXNAMLEN + 1	/* "/" + (file name size) */
597c478bd9Sstevel@tonic-gate #define	TRUE		1
607c478bd9Sstevel@tonic-gate #define	FALSE		0
617c478bd9Sstevel@tonic-gate #define	WRITE		02
627c478bd9Sstevel@tonic-gate #define	SEARCH		07
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static	int	errcode;
657c478bd9Sstevel@tonic-gate static	int interactive, recursive, silent; /* flags for command line options */
667c478bd9Sstevel@tonic-gate 
67*996aa816Ssn199410 static	int	rm(char *, int);
68*996aa816Ssn199410 static	int	undir(char *, int, dev_t, ino_t);
697c478bd9Sstevel@tonic-gate static	int	yes(void);
707c478bd9Sstevel@tonic-gate static	int	mypath(dev_t, ino_t);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static	char	yeschr[SCHAR_MAX + 2];
737c478bd9Sstevel@tonic-gate static	char	nochr[SCHAR_MAX + 2];
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static char *fullpath;
7612a9e0efSsn199410 static int initdirfd;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate static void push_name(char *name, int first);
79*996aa816Ssn199410 static int pop_name(int first);
807c478bd9Sstevel@tonic-gate static void force_chdir(char *);
817c478bd9Sstevel@tonic-gate static void ch_dir(char *);
827c478bd9Sstevel@tonic-gate static char *get_filename(char *name);
8312a9e0efSsn199410 static void chdir_init(void);
8412a9e0efSsn199410 static void check_initdir(void);
857c478bd9Sstevel@tonic-gate static void cleanup(void);
867c478bd9Sstevel@tonic-gate 
8712a9e0efSsn199410 static char 	*cwd;		/* pathname of init dir, from getcwd() */
887c478bd9Sstevel@tonic-gate static rlim_t	maxfiles;	/* maximum number of open files */
897c478bd9Sstevel@tonic-gate static int	first_dir = 1;	/* flag set when first trying to remove a dir */
907c478bd9Sstevel@tonic-gate 	/* flag set when can't get dev/inode of a parent dir */
917c478bd9Sstevel@tonic-gate static int	parent_err = 0;
92014a7923Sas145665 static avl_tree_t *tree;	/* tree to keep track of nodes visited */
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate struct dir_id {
957c478bd9Sstevel@tonic-gate 	dev_t	dev;
967c478bd9Sstevel@tonic-gate 	ino_t	inode;
977c478bd9Sstevel@tonic-gate 	struct dir_id *next;
987c478bd9Sstevel@tonic-gate };
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	/*
10112a9e0efSsn199410 	 * initdir is the first of a linked list of structures
1027c478bd9Sstevel@tonic-gate 	 * containing unique identifying device and inode numbers for
10312a9e0efSsn199410 	 * each directory, from the initial dir up to the root.
10412a9e0efSsn199410 	 * current_dir is a pointer to the most recent directory pushed
10512a9e0efSsn199410 	 * on during a recursive rm() call.
1067c478bd9Sstevel@tonic-gate 	 */
10712a9e0efSsn199410 static struct dir_id initdir, *current_dir;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate int
1107c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	extern int	optind;
1137c478bd9Sstevel@tonic-gate 	int	errflg = 0;
1147c478bd9Sstevel@tonic-gate 	int	c;
1157c478bd9Sstevel@tonic-gate 	struct rlimit rl;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1187c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1197c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
1207c478bd9Sstevel@tonic-gate #endif
1217c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	(void) strncpy(yeschr, nl_langinfo(YESSTR), SCHAR_MAX + 1);
1247c478bd9Sstevel@tonic-gate 	(void) strncpy(nochr, nl_langinfo(NOSTR), SCHAR_MAX + 1);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "frRi")) != EOF)
1277c478bd9Sstevel@tonic-gate 		switch (c) {
1287c478bd9Sstevel@tonic-gate 		case 'f':
1297c478bd9Sstevel@tonic-gate 			silent = TRUE;
1307c478bd9Sstevel@tonic-gate #ifdef XPG4
1317c478bd9Sstevel@tonic-gate 			interactive = FALSE;
1327c478bd9Sstevel@tonic-gate #endif
1337c478bd9Sstevel@tonic-gate 			break;
1347c478bd9Sstevel@tonic-gate 		case 'i':
1357c478bd9Sstevel@tonic-gate 			interactive = TRUE;
1367c478bd9Sstevel@tonic-gate #ifdef XPG4
1377c478bd9Sstevel@tonic-gate 			silent = FALSE;
1387c478bd9Sstevel@tonic-gate #endif
1397c478bd9Sstevel@tonic-gate 			break;
1407c478bd9Sstevel@tonic-gate 		case 'r':
1417c478bd9Sstevel@tonic-gate 		case 'R':
1427c478bd9Sstevel@tonic-gate 			recursive = TRUE;
1437c478bd9Sstevel@tonic-gate 			break;
1447c478bd9Sstevel@tonic-gate 		case '?':
1457c478bd9Sstevel@tonic-gate 			errflg = 1;
1467c478bd9Sstevel@tonic-gate 			break;
1477c478bd9Sstevel@tonic-gate 		}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/*
1507c478bd9Sstevel@tonic-gate 	 * For BSD compatibility allow '-' to delimit the end
1517c478bd9Sstevel@tonic-gate 	 * of options.  However, if options were already explicitly
1527c478bd9Sstevel@tonic-gate 	 * terminated with '--', then treat '-' literally: otherwise,
1537c478bd9Sstevel@tonic-gate 	 * "rm -- -" won't remove '-'.
1547c478bd9Sstevel@tonic-gate 	 */
1557c478bd9Sstevel@tonic-gate 	if (optind < argc &&
1567c478bd9Sstevel@tonic-gate 	    strcmp(argv[optind], "-") == 0 &&
1577c478bd9Sstevel@tonic-gate 	    strcmp(argv[optind - 1], "--") != 0)
1587c478bd9Sstevel@tonic-gate 		optind++;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	argc -= optind;
1617c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if ((argc < 1 && !silent) || errflg) {
1647c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1657c478bd9Sstevel@tonic-gate 			gettext("usage: rm [-fiRr] file ...\n"));
1667c478bd9Sstevel@tonic-gate 		exit(2);
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rl)) {
1707c478bd9Sstevel@tonic-gate 		perror("getrlimit");
1717c478bd9Sstevel@tonic-gate 		exit(2);
1727c478bd9Sstevel@tonic-gate 	} else
1737c478bd9Sstevel@tonic-gate 		maxfiles = rl.rlim_cur - 2;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	while (argc-- > 0) {
176014a7923Sas145665 		tree = NULL;
177*996aa816Ssn199410 		/* Retry if rm() fails due to bad chdir */
178*996aa816Ssn199410 		while (rm(*argv, 1) < 0)
179*996aa816Ssn199410 			;
1807c478bd9Sstevel@tonic-gate 		argv++;
181014a7923Sas145665 		destroy_tree(tree);
1827c478bd9Sstevel@tonic-gate 	}
18312a9e0efSsn199410 
1847c478bd9Sstevel@tonic-gate 	cleanup();
1857c478bd9Sstevel@tonic-gate 	return (errcode ? 2 : 0);
1867c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
189*996aa816Ssn199410 static int
1907c478bd9Sstevel@tonic-gate rm(char *path, int first)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate 	struct stat buffer;
1937c478bd9Sstevel@tonic-gate 	char	*filepath;
1947c478bd9Sstevel@tonic-gate 	char	*p;
1957c478bd9Sstevel@tonic-gate 	char	resolved_path[PATH_MAX];
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/*
1987c478bd9Sstevel@tonic-gate 	 * Check file to see if it exists.
1997c478bd9Sstevel@tonic-gate 	 */
2007c478bd9Sstevel@tonic-gate 	if (lstat(path, &buffer) == FAIL) {
2017c478bd9Sstevel@tonic-gate 		if (!silent) {
2027c478bd9Sstevel@tonic-gate 			perror(path);
2037c478bd9Sstevel@tonic-gate 			++errcode;
2047c478bd9Sstevel@tonic-gate 		}
205*996aa816Ssn199410 		return (0);
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/* prevent removal of / but allow removal of sym-links */
2097c478bd9Sstevel@tonic-gate 	if (!S_ISLNK(buffer.st_mode) && realpath(path, resolved_path) != NULL &&
2107c478bd9Sstevel@tonic-gate 	    strcmp(resolved_path, "/") == 0) {
2117c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2127c478bd9Sstevel@tonic-gate 		    gettext("rm of %s is not allowed\n"), resolved_path);
2137c478bd9Sstevel@tonic-gate 		errcode++;
214*996aa816Ssn199410 		return (0);
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	/* prevent removal of . or .. (directly) */
2187c478bd9Sstevel@tonic-gate 	if (p = strrchr(path, '/'))
2197c478bd9Sstevel@tonic-gate 		p++;
2207c478bd9Sstevel@tonic-gate 	else
2217c478bd9Sstevel@tonic-gate 		p = path;
2227c478bd9Sstevel@tonic-gate 	if (strcmp(".", p) == 0 || strcmp("..", p) == 0) {
2237c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2247c478bd9Sstevel@tonic-gate 			gettext("rm of %s is not allowed\n"), path);
2257c478bd9Sstevel@tonic-gate 		errcode++;
226*996aa816Ssn199410 		return (0);
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 	/*
2297c478bd9Sstevel@tonic-gate 	 * If it's a directory, remove its contents.
2307c478bd9Sstevel@tonic-gate 	 */
2317c478bd9Sstevel@tonic-gate 	if (DIRECTORY) {
2327c478bd9Sstevel@tonic-gate 		/*
2337c478bd9Sstevel@tonic-gate 		 * If "-r" wasn't specified, trying to remove directories
2347c478bd9Sstevel@tonic-gate 		 * is an error.
2357c478bd9Sstevel@tonic-gate 		 */
2367c478bd9Sstevel@tonic-gate 		if (!recursive) {
2377c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2387c478bd9Sstevel@tonic-gate 			    gettext("rm: %s is a directory\n"), path);
2397c478bd9Sstevel@tonic-gate 			++errcode;
240*996aa816Ssn199410 			return (0);
2417c478bd9Sstevel@tonic-gate 		}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 		if (first_dir) {
24412a9e0efSsn199410 			check_initdir();
24512a9e0efSsn199410 			current_dir = NULL;
2467c478bd9Sstevel@tonic-gate 			first_dir = 0;
2477c478bd9Sstevel@tonic-gate 		}
2487c478bd9Sstevel@tonic-gate 
249*996aa816Ssn199410 		return (undir(path, first, buffer.st_dev, buffer.st_ino));
2507c478bd9Sstevel@tonic-gate 	}
25112a9e0efSsn199410 
2527c478bd9Sstevel@tonic-gate 	filepath = get_filename(path);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	/*
2557c478bd9Sstevel@tonic-gate 	 * If interactive, ask for acknowledgement.
2567c478bd9Sstevel@tonic-gate 	 *
2577c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE - The following message will contain the
2587c478bd9Sstevel@tonic-gate 	 * first character of the strings for "yes" and "no" defined
2597c478bd9Sstevel@tonic-gate 	 * in the file "nl_langinfo.po".  After substitution, the
2607c478bd9Sstevel@tonic-gate 	 * message will appear as follows:
2617c478bd9Sstevel@tonic-gate 	 *	rm: remove <filename> (y/n)?
2627c478bd9Sstevel@tonic-gate 	 * For example, in German, this will appear as
2637c478bd9Sstevel@tonic-gate 	 *	rm: l�schen <filename> (j/n)?
2647c478bd9Sstevel@tonic-gate 	 * where j=ja, n=nein, <filename>=the file to be removed
2657c478bd9Sstevel@tonic-gate 	 *
2667c478bd9Sstevel@tonic-gate 	 */
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	if (interactive) {
2707c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("rm: remove %s (%s/%s)? "),
2717c478bd9Sstevel@tonic-gate 			filepath, yeschr, nochr);
2727c478bd9Sstevel@tonic-gate 		if (!yes()) {
2737c478bd9Sstevel@tonic-gate 			free(filepath);
274*996aa816Ssn199410 			return (0);
2757c478bd9Sstevel@tonic-gate 		}
2767c478bd9Sstevel@tonic-gate 	} else if (!silent) {
2777c478bd9Sstevel@tonic-gate 		/*
2787c478bd9Sstevel@tonic-gate 		 * If not silent, and stdin is a terminal, and there's
2797c478bd9Sstevel@tonic-gate 		 * no write access, and the file isn't a symbolic link,
2807c478bd9Sstevel@tonic-gate 		 * ask for permission.
2817c478bd9Sstevel@tonic-gate 		 *
2827c478bd9Sstevel@tonic-gate 		 * TRANSLATION_NOTE - The following message will contain the
2837c478bd9Sstevel@tonic-gate 		 * first character of the strings for "yes" and "no" defined
2847c478bd9Sstevel@tonic-gate 		 * in the file "nl_langinfo.po".  After substitution, the
2857c478bd9Sstevel@tonic-gate 		 * message will appear as follows:
2867c478bd9Sstevel@tonic-gate 		 * 	rm: <filename>: override protection XXX (y/n)?
2877c478bd9Sstevel@tonic-gate 		 * where XXX is the permission mode bits of the file in octal
2887c478bd9Sstevel@tonic-gate 		 * and <filename> is the file to be removed
2897c478bd9Sstevel@tonic-gate 		 *
2907c478bd9Sstevel@tonic-gate 		 */
2917c478bd9Sstevel@tonic-gate 		if (!SYMLINK && access(path, W_OK) == FAIL &&
2927c478bd9Sstevel@tonic-gate 		    isatty(fileno(stdin))) {
2937c478bd9Sstevel@tonic-gate 			(void) printf(
2947c478bd9Sstevel@tonic-gate 			    gettext("rm: %s: override protection %o (%s/%s)? "),
2957c478bd9Sstevel@tonic-gate 			    filepath, buffer.st_mode & 0777, yeschr, nochr);
2967c478bd9Sstevel@tonic-gate 			/*
2977c478bd9Sstevel@tonic-gate 			 * If permission isn't given, skip the file.
2987c478bd9Sstevel@tonic-gate 			 */
2997c478bd9Sstevel@tonic-gate 			if (!yes()) {
3007c478bd9Sstevel@tonic-gate 				free(filepath);
301*996aa816Ssn199410 				return (0);
3027c478bd9Sstevel@tonic-gate 			}
3037c478bd9Sstevel@tonic-gate 		}
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	/*
3077c478bd9Sstevel@tonic-gate 	 * If the unlink fails, inform the user. For /usr/bin/rm, only inform
3087c478bd9Sstevel@tonic-gate 	 * the user if interactive or not silent.
3097c478bd9Sstevel@tonic-gate 	 * If unlink fails with errno = ENOENT because file was removed
3107c478bd9Sstevel@tonic-gate 	 * in between the lstat call and unlink don't inform the user and
3117c478bd9Sstevel@tonic-gate 	 * don't change errcode.
3127c478bd9Sstevel@tonic-gate 	 */
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	if (unlink(path) == FAIL) {
3157c478bd9Sstevel@tonic-gate 		if (errno == ENOENT) {
3167c478bd9Sstevel@tonic-gate 			free(filepath);
317*996aa816Ssn199410 			return (0);
3187c478bd9Sstevel@tonic-gate 		}
3197c478bd9Sstevel@tonic-gate #ifndef XPG4
3207c478bd9Sstevel@tonic-gate 		if (!silent || interactive) {
3217c478bd9Sstevel@tonic-gate #endif
3227c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3237c478bd9Sstevel@tonic-gate 				    gettext("rm: %s not removed: "), filepath);
3247c478bd9Sstevel@tonic-gate 				perror("");
3257c478bd9Sstevel@tonic-gate #ifndef XPG4
3267c478bd9Sstevel@tonic-gate 		}
3277c478bd9Sstevel@tonic-gate #endif
3287c478bd9Sstevel@tonic-gate 		++errcode;
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	free(filepath);
332*996aa816Ssn199410 	return (0);
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate 
335*996aa816Ssn199410 static int
3367c478bd9Sstevel@tonic-gate undir(char *path, int first, dev_t dev, ino_t ino)
3377c478bd9Sstevel@tonic-gate {
3387c478bd9Sstevel@tonic-gate 	char	*newpath;
3397c478bd9Sstevel@tonic-gate 	DIR	*name;
3407c478bd9Sstevel@tonic-gate 	struct dirent *direct;
3417c478bd9Sstevel@tonic-gate 	int	ismypath;
342014a7923Sas145665 	int	ret;
3437c478bd9Sstevel@tonic-gate 	int	chdir_failed = 0;
344*996aa816Ssn199410 	int	bad_chdir = 0;
3457c478bd9Sstevel@tonic-gate 	size_t	len;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	push_name(path, first);
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/*
3507c478bd9Sstevel@tonic-gate 	 * If interactive and this file isn't in the path of the
3517c478bd9Sstevel@tonic-gate 	 * current working directory, ask for acknowledgement.
3527c478bd9Sstevel@tonic-gate 	 *
3537c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE - The following message will contain the
3547c478bd9Sstevel@tonic-gate 	 * first character of the strings for "yes" and "no" defined
3557c478bd9Sstevel@tonic-gate 	 * in the file "nl_langinfo.po".  After substitution, the
3567c478bd9Sstevel@tonic-gate 	 * message will appear as follows:
3577c478bd9Sstevel@tonic-gate 	 *	rm: examine files in directory <directoryname> (y/n)?
3587c478bd9Sstevel@tonic-gate 	 * where <directoryname> is the directory to be removed
3597c478bd9Sstevel@tonic-gate 	 *
3607c478bd9Sstevel@tonic-gate 	 */
3617c478bd9Sstevel@tonic-gate 	ismypath = mypath(dev, ino);
3627c478bd9Sstevel@tonic-gate 	if (interactive) {
3637c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3647c478bd9Sstevel@tonic-gate 		    gettext("rm: examine files in directory %s (%s/%s)? "),
3657c478bd9Sstevel@tonic-gate 			fullpath, yeschr, nochr);
3667c478bd9Sstevel@tonic-gate 		/*
3677c478bd9Sstevel@tonic-gate 		 * If the answer is no, skip the directory.
3687c478bd9Sstevel@tonic-gate 		 */
369*996aa816Ssn199410 		if (!yes())
370*996aa816Ssn199410 			return (pop_name(first));
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate #ifdef XPG4
3747c478bd9Sstevel@tonic-gate 	/*
3757c478bd9Sstevel@tonic-gate 	 * XCU4 and POSIX.2: If not interactive and file is not in the
3767c478bd9Sstevel@tonic-gate 	 * path of the current working directory, check to see whether
3777c478bd9Sstevel@tonic-gate 	 * or not directory is readable or writable and if not,
3787c478bd9Sstevel@tonic-gate 	 * prompt user for response.
3797c478bd9Sstevel@tonic-gate 	 */
3807c478bd9Sstevel@tonic-gate 	if (!interactive && !ismypath &&
3817c478bd9Sstevel@tonic-gate 	    (access(path, W_OK|X_OK) == FAIL) && isatty(fileno(stdin))) {
3827c478bd9Sstevel@tonic-gate 		if (!silent) {
3837c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3847c478bd9Sstevel@tonic-gate 			    gettext(
3857c478bd9Sstevel@tonic-gate 				"rm: examine files in directory %s (%s/%s)? "),
3867c478bd9Sstevel@tonic-gate 			    fullpath, yeschr, nochr);
3877c478bd9Sstevel@tonic-gate 			/*
3887c478bd9Sstevel@tonic-gate 			 * If the answer is no, skip the directory.
3897c478bd9Sstevel@tonic-gate 			 */
390*996aa816Ssn199410 			if (!yes())
391*996aa816Ssn199410 				return (pop_name(first));
3927c478bd9Sstevel@tonic-gate 		}
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate #endif
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	/*
397014a7923Sas145665 	 * Add this node to the search tree so we don't
398014a7923Sas145665 	 * get into a endless loop. If the add fails then
399014a7923Sas145665 	 * we have visited this node before.
400014a7923Sas145665 	 */
401014a7923Sas145665 	ret = add_tnode(&tree, dev, ino);
402014a7923Sas145665 	if (ret != 1) {
403014a7923Sas145665 		if (ret == 0) {
404014a7923Sas145665 			(void) fprintf(stderr,
405014a7923Sas145665 			    gettext("rm: cycle detected for %s\n"),
406014a7923Sas145665 			    fullpath);
407014a7923Sas145665 		} else if (ret == -1) {
408014a7923Sas145665 			perror("rm");
409014a7923Sas145665 		}
410014a7923Sas145665 		errcode++;
411*996aa816Ssn199410 		return (pop_name(first));
412014a7923Sas145665 	}
413014a7923Sas145665 
414014a7923Sas145665 	/*
4157c478bd9Sstevel@tonic-gate 	 * Open the directory for reading.
4167c478bd9Sstevel@tonic-gate 	 */
4177c478bd9Sstevel@tonic-gate 	if ((name = opendir(path)) == NULL) {
4187c478bd9Sstevel@tonic-gate 		int	saveerrno = errno;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 		/*
4217c478bd9Sstevel@tonic-gate 		 * If interactive, ask for acknowledgement.
4227c478bd9Sstevel@tonic-gate 		 */
4237c478bd9Sstevel@tonic-gate 		if (interactive) {
4247c478bd9Sstevel@tonic-gate 			/*
4257c478bd9Sstevel@tonic-gate 			 * Print an error message that
4267c478bd9Sstevel@tonic-gate 			 * we could not read the directory
4277c478bd9Sstevel@tonic-gate 			 * as the user wanted to examine
4287c478bd9Sstevel@tonic-gate 			 * files in the directory.  Only
4297c478bd9Sstevel@tonic-gate 			 * affect the error status if
4307c478bd9Sstevel@tonic-gate 			 * user doesn't want to remove the
4317c478bd9Sstevel@tonic-gate 			 * directory as we still may be able
4327c478bd9Sstevel@tonic-gate 			 * remove the directory successfully.
4337c478bd9Sstevel@tonic-gate 			 */
4347c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4357c478bd9Sstevel@tonic-gate 			    "rm: cannot read directory %s: "),
4367c478bd9Sstevel@tonic-gate 			    fullpath);
4377c478bd9Sstevel@tonic-gate 			errno = saveerrno;
4387c478bd9Sstevel@tonic-gate 			perror("");
4397c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
4407c478bd9Sstevel@tonic-gate 			    "rm: remove %s: (%s/%s)? "),
4417c478bd9Sstevel@tonic-gate 			    fullpath, yeschr, nochr);
4427c478bd9Sstevel@tonic-gate 			if (!yes()) {
4437c478bd9Sstevel@tonic-gate 				++errcode;
444*996aa816Ssn199410 				return (pop_name(first));
4457c478bd9Sstevel@tonic-gate 			}
4467c478bd9Sstevel@tonic-gate 		}
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 		/*
4497c478bd9Sstevel@tonic-gate 		 * If the directory is empty, we may be able to
4507c478bd9Sstevel@tonic-gate 		 * go ahead and remove it.
4517c478bd9Sstevel@tonic-gate 		 */
4527c478bd9Sstevel@tonic-gate 		if (rmdir(path) == FAIL) {
4537c478bd9Sstevel@tonic-gate 			if (interactive) {
4547c478bd9Sstevel@tonic-gate 				int	rmdirerr = errno;
4557c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
4567c478bd9Sstevel@tonic-gate 				    "rm: Unable to remove directory %s: "),
4577c478bd9Sstevel@tonic-gate 				    fullpath);
4587c478bd9Sstevel@tonic-gate 				errno = rmdirerr;
4597c478bd9Sstevel@tonic-gate 				perror("");
4607c478bd9Sstevel@tonic-gate 			} else {
4617c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
4627c478bd9Sstevel@tonic-gate 				    "rm: cannot read directory %s: "),
4637c478bd9Sstevel@tonic-gate 				    fullpath);
4647c478bd9Sstevel@tonic-gate 				errno = saveerrno;
4657c478bd9Sstevel@tonic-gate 				perror("");
4667c478bd9Sstevel@tonic-gate 			}
4677c478bd9Sstevel@tonic-gate 			++errcode;
4687c478bd9Sstevel@tonic-gate 		}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 		/* Continue to next file/directory rather than exit */
471*996aa816Ssn199410 		return (pop_name(first));
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	/*
4757c478bd9Sstevel@tonic-gate 	 * XCU4 requires that rm -r descend the directory
4767c478bd9Sstevel@tonic-gate 	 * hierarchy without regard to PATH_MAX.  If we can't
4777c478bd9Sstevel@tonic-gate 	 * chdir() do not increment error counter and do not
4787c478bd9Sstevel@tonic-gate 	 * print message.
4797c478bd9Sstevel@tonic-gate 	 *
4807c478bd9Sstevel@tonic-gate 	 * However, if we cannot chdir because someone has taken away
4817c478bd9Sstevel@tonic-gate 	 * execute access we may still be able to delete the directory
4827c478bd9Sstevel@tonic-gate 	 * if it's empty. The old rm could do this.
4837c478bd9Sstevel@tonic-gate 	 */
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	if (chdir(path) == -1) {
4867c478bd9Sstevel@tonic-gate 		chdir_failed = 1;
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	/*
4907c478bd9Sstevel@tonic-gate 	 * Read every directory entry.
4917c478bd9Sstevel@tonic-gate 	 */
4927c478bd9Sstevel@tonic-gate 	while ((direct = readdir(name)) != NULL) {
4937c478bd9Sstevel@tonic-gate 		/*
4947c478bd9Sstevel@tonic-gate 		 * Ignore "." and ".." entries.
4957c478bd9Sstevel@tonic-gate 		 */
4967c478bd9Sstevel@tonic-gate 		if (strcmp(direct->d_name, ".") == 0 ||
4977c478bd9Sstevel@tonic-gate 		    strcmp(direct->d_name, "..") == 0)
4987c478bd9Sstevel@tonic-gate 			continue;
4997c478bd9Sstevel@tonic-gate 		/*
5007c478bd9Sstevel@tonic-gate 		 * Try to remove the file.
5017c478bd9Sstevel@tonic-gate 		 */
5027c478bd9Sstevel@tonic-gate 		len = strlen(direct->d_name) + 1;
5037c478bd9Sstevel@tonic-gate 		if (chdir_failed) {
5047c478bd9Sstevel@tonic-gate 			len += strlen(path) + 2;
5057c478bd9Sstevel@tonic-gate 		}
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 		newpath = malloc(len);
5087c478bd9Sstevel@tonic-gate 		if (newpath == NULL) {
5097c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5107c478bd9Sstevel@tonic-gate 			    gettext("rm: Insufficient memory.\n"));
5117c478bd9Sstevel@tonic-gate 			cleanup();
5127c478bd9Sstevel@tonic-gate 			exit(1);
5137c478bd9Sstevel@tonic-gate 		}
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 		if (!chdir_failed) {
5167c478bd9Sstevel@tonic-gate 			(void) strcpy(newpath, direct->d_name);
5177c478bd9Sstevel@tonic-gate 		} else {
5187c478bd9Sstevel@tonic-gate 			(void) snprintf(newpath, len, "%s/%s",
5197c478bd9Sstevel@tonic-gate 			    path, direct->d_name);
5207c478bd9Sstevel@tonic-gate 		}
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 		/*
5247c478bd9Sstevel@tonic-gate 		 * If a spare file descriptor is available, just call the
5257c478bd9Sstevel@tonic-gate 		 * "rm" function with the file name; otherwise close the
5267c478bd9Sstevel@tonic-gate 		 * directory and reopen it when the child is removed.
5277c478bd9Sstevel@tonic-gate 		 */
5287c478bd9Sstevel@tonic-gate 		if (name->dd_fd >= maxfiles) {
5297c478bd9Sstevel@tonic-gate 			(void) closedir(name);
530*996aa816Ssn199410 			if (rm(newpath, 0) < 0)
531*996aa816Ssn199410 				bad_chdir = -1;
5327c478bd9Sstevel@tonic-gate 			if (!chdir_failed)
5337c478bd9Sstevel@tonic-gate 				name = opendir(".");
5347c478bd9Sstevel@tonic-gate 			else
5357c478bd9Sstevel@tonic-gate 				name = opendir(path);
5367c478bd9Sstevel@tonic-gate 			if (name == NULL) {
5377c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
5387c478bd9Sstevel@tonic-gate 				    gettext("rm: cannot read directory %s: "),
5397c478bd9Sstevel@tonic-gate 				    fullpath);
5407c478bd9Sstevel@tonic-gate 				perror("");
5417c478bd9Sstevel@tonic-gate 				cleanup();
5427c478bd9Sstevel@tonic-gate 				exit(2);
5437c478bd9Sstevel@tonic-gate 			}
544*996aa816Ssn199410 		} else if (rm(newpath, 0) < 0)
545*996aa816Ssn199410 			bad_chdir = -1;
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 		free(newpath);
548*996aa816Ssn199410 		if (bad_chdir)
549*996aa816Ssn199410 			break;
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	/*
5537c478bd9Sstevel@tonic-gate 	 * Close the directory we just finished reading.
5547c478bd9Sstevel@tonic-gate 	 */
5557c478bd9Sstevel@tonic-gate 	(void) closedir(name);
556*996aa816Ssn199410 	if (bad_chdir)
557*996aa816Ssn199410 		return (-1);
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	/*
5607c478bd9Sstevel@tonic-gate 	 * The contents of the directory have been removed.  If the
5617c478bd9Sstevel@tonic-gate 	 * directory itself is in the path of the current working
5627c478bd9Sstevel@tonic-gate 	 * directory, don't try to remove it.
5637c478bd9Sstevel@tonic-gate 	 * When the directory itself is the current working directory, mypath()
5647c478bd9Sstevel@tonic-gate 	 * has a return code == 2.
5657c478bd9Sstevel@tonic-gate 	 *
5667c478bd9Sstevel@tonic-gate 	 * XCU4: Because we've descended the directory hierarchy in order
5677c478bd9Sstevel@tonic-gate 	 * to avoid PATH_MAX limitation, we must now start ascending
5687c478bd9Sstevel@tonic-gate 	 * one level at a time and remove files/directories.
5697c478bd9Sstevel@tonic-gate 	 */
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	if (!chdir_failed) {
5727c478bd9Sstevel@tonic-gate 		if (first)
57312a9e0efSsn199410 			chdir_init();
5747c478bd9Sstevel@tonic-gate 		else if (chdir("..") == -1) {
5757c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5767c478bd9Sstevel@tonic-gate 			    gettext("rm: cannot change to parent of "
5777c478bd9Sstevel@tonic-gate 				    "directory %s: "),
5787c478bd9Sstevel@tonic-gate 			    fullpath);
5797c478bd9Sstevel@tonic-gate 			perror("");
5807c478bd9Sstevel@tonic-gate 			cleanup();
5817c478bd9Sstevel@tonic-gate 			exit(2);
5827c478bd9Sstevel@tonic-gate 		}
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	switch (ismypath) {
5867c478bd9Sstevel@tonic-gate 	case 3:
587*996aa816Ssn199410 		return (pop_name(first));
5887c478bd9Sstevel@tonic-gate 	case 2:
5897c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5907c478bd9Sstevel@tonic-gate 		    gettext("rm: Cannot remove any directory in the path "
5917c478bd9Sstevel@tonic-gate 			"of the current working directory\n%s\n"), fullpath);
5927c478bd9Sstevel@tonic-gate 		++errcode;
593*996aa816Ssn199410 		return (pop_name(first));
5947c478bd9Sstevel@tonic-gate 	case 1:
5957c478bd9Sstevel@tonic-gate 		++errcode;
596*996aa816Ssn199410 		return (pop_name(first));
5977c478bd9Sstevel@tonic-gate 	case 0:
5987c478bd9Sstevel@tonic-gate 		break;
5997c478bd9Sstevel@tonic-gate 	}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	/*
6027c478bd9Sstevel@tonic-gate 	 * If interactive, ask for acknowledgement.
6037c478bd9Sstevel@tonic-gate 	 */
6047c478bd9Sstevel@tonic-gate 	if (interactive) {
6057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("rm: remove %s: (%s/%s)? "),
6067c478bd9Sstevel@tonic-gate 			fullpath, yeschr, nochr);
607*996aa816Ssn199410 		if (!yes())
608*996aa816Ssn199410 			return (pop_name(first));
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate 	if (rmdir(path) == FAIL) {
6117c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6127c478bd9Sstevel@tonic-gate 			gettext("rm: Unable to remove directory %s: "),
6137c478bd9Sstevel@tonic-gate 			fullpath);
6147c478bd9Sstevel@tonic-gate 		perror("");
6157c478bd9Sstevel@tonic-gate 		++errcode;
6167c478bd9Sstevel@tonic-gate 	}
617*996aa816Ssn199410 	return (pop_name(first));
6187c478bd9Sstevel@tonic-gate }
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate static int
6227c478bd9Sstevel@tonic-gate yes(void)
6237c478bd9Sstevel@tonic-gate {
6247c478bd9Sstevel@tonic-gate 	int	i, b;
6257c478bd9Sstevel@tonic-gate 	char	ans[SCHAR_MAX + 1];
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	for (i = 0; ; i++) {
6287c478bd9Sstevel@tonic-gate 		b = getchar();
6297c478bd9Sstevel@tonic-gate 		if (b == '\n' || b == '\0' || b == EOF) {
6307c478bd9Sstevel@tonic-gate 			ans[i] = 0;
6317c478bd9Sstevel@tonic-gate 			break;
6327c478bd9Sstevel@tonic-gate 		}
6337c478bd9Sstevel@tonic-gate 		if (i < SCHAR_MAX)
6347c478bd9Sstevel@tonic-gate 			ans[i] = b;
6357c478bd9Sstevel@tonic-gate 	}
6367c478bd9Sstevel@tonic-gate 	if (i >= SCHAR_MAX) {
6377c478bd9Sstevel@tonic-gate 		i = SCHAR_MAX;
6387c478bd9Sstevel@tonic-gate 		ans[SCHAR_MAX] = 0;
6397c478bd9Sstevel@tonic-gate 	}
6407c478bd9Sstevel@tonic-gate 	if ((i == 0) | (strncmp(yeschr, ans, i)))
6417c478bd9Sstevel@tonic-gate 		return (0);
6427c478bd9Sstevel@tonic-gate 	return (1);
6437c478bd9Sstevel@tonic-gate }
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate static int
6477c478bd9Sstevel@tonic-gate mypath(dev_t dev, ino_t ino)
6487c478bd9Sstevel@tonic-gate {
6497c478bd9Sstevel@tonic-gate 	struct dir_id *curdir;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	/*
6527c478bd9Sstevel@tonic-gate 	 * Check to see if this is our current directory
6537c478bd9Sstevel@tonic-gate 	 * Indicated by return 2;
6547c478bd9Sstevel@tonic-gate 	 */
65512a9e0efSsn199410 	if (dev == initdir.dev && ino == initdir.inode) {
6567c478bd9Sstevel@tonic-gate 		return (2);
6577c478bd9Sstevel@tonic-gate 	}
6587c478bd9Sstevel@tonic-gate 
65912a9e0efSsn199410 	curdir = initdir.next;
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	while (curdir != NULL) {
6627c478bd9Sstevel@tonic-gate 		/*
6637c478bd9Sstevel@tonic-gate 		 * If we find a match, the directory (dev, ino) passed to
6647c478bd9Sstevel@tonic-gate 		 * mypath() is an ancestor of ours. Indicated by return 3.
6657c478bd9Sstevel@tonic-gate 		 */
6667c478bd9Sstevel@tonic-gate 		if (curdir->dev == dev && curdir->inode == ino)
6677c478bd9Sstevel@tonic-gate 			return (3);
6687c478bd9Sstevel@tonic-gate 		curdir = curdir->next;
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate 	/*
6717c478bd9Sstevel@tonic-gate 	 * parent_err indicates we couldn't stat or chdir to
6727c478bd9Sstevel@tonic-gate 	 * one of our parent dirs, so the linked list of dir_id structs
6737c478bd9Sstevel@tonic-gate 	 * is incomplete
6747c478bd9Sstevel@tonic-gate 	 */
6757c478bd9Sstevel@tonic-gate 	if (parent_err) {
6767c478bd9Sstevel@tonic-gate #ifndef XPG4
6777c478bd9Sstevel@tonic-gate 		if (!silent || interactive) {
6787c478bd9Sstevel@tonic-gate #endif
6797c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("rm: cannot determine "
6807c478bd9Sstevel@tonic-gate 			    "if this is an ancestor of the current "
6817c478bd9Sstevel@tonic-gate 			    "working directory\n%s\n"), fullpath);
6827c478bd9Sstevel@tonic-gate #ifndef XPG4
6837c478bd9Sstevel@tonic-gate 		}
6847c478bd9Sstevel@tonic-gate #endif
6857c478bd9Sstevel@tonic-gate 		/* assume it is. least dangerous */
6867c478bd9Sstevel@tonic-gate 		return (1);
6877c478bd9Sstevel@tonic-gate 	}
6887c478bd9Sstevel@tonic-gate 	return (0);
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate static int maxlen;
6927c478bd9Sstevel@tonic-gate static int curlen;
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate static char *
6957c478bd9Sstevel@tonic-gate get_filename(char *name)
6967c478bd9Sstevel@tonic-gate {
6977c478bd9Sstevel@tonic-gate 	char *path;
6987c478bd9Sstevel@tonic-gate 	size_t len;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	if (fullpath == NULL || *fullpath == '\0') {
7017c478bd9Sstevel@tonic-gate 		path = strdup(name);
7027c478bd9Sstevel@tonic-gate 		if (path == NULL) {
7037c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
7047c478bd9Sstevel@tonic-gate 			    gettext("rm: Insufficient memory.\n"));
7057c478bd9Sstevel@tonic-gate 			cleanup();
7067c478bd9Sstevel@tonic-gate 			exit(1);
7077c478bd9Sstevel@tonic-gate 		}
7087c478bd9Sstevel@tonic-gate 	} else {
7097c478bd9Sstevel@tonic-gate 		len = strlen(fullpath) + strlen(name) + 2;
7107c478bd9Sstevel@tonic-gate 		path = malloc(len);
7117c478bd9Sstevel@tonic-gate 		if (path == NULL) {
7127c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
7137c478bd9Sstevel@tonic-gate 			    gettext("rm: Insufficient memory.\n"));
7147c478bd9Sstevel@tonic-gate 			cleanup();
7157c478bd9Sstevel@tonic-gate 			exit(1);
7167c478bd9Sstevel@tonic-gate 		}
7177c478bd9Sstevel@tonic-gate 		(void) snprintf(path, len, "%s/%s", fullpath, name);
7187c478bd9Sstevel@tonic-gate 	}
7197c478bd9Sstevel@tonic-gate 	return (path);
7207c478bd9Sstevel@tonic-gate }
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate static void
7237c478bd9Sstevel@tonic-gate push_name(char *name, int first)
7247c478bd9Sstevel@tonic-gate {
7257c478bd9Sstevel@tonic-gate 	int	namelen;
72612a9e0efSsn199410 	struct	stat buffer;
72712a9e0efSsn199410 	struct	dir_id *newdir;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	namelen = strlen(name) + 1; /* 1 for "/" */
7307c478bd9Sstevel@tonic-gate 	if ((curlen + namelen) >= maxlen) {
7317c478bd9Sstevel@tonic-gate 		maxlen += PATH_MAX;
7327c478bd9Sstevel@tonic-gate 		fullpath = (char *)realloc(fullpath, (size_t)(maxlen + 1));
7337c478bd9Sstevel@tonic-gate 	}
7347c478bd9Sstevel@tonic-gate 	if (first) {
7357c478bd9Sstevel@tonic-gate 		(void) strcpy(fullpath, name);
7367c478bd9Sstevel@tonic-gate 	} else {
7377c478bd9Sstevel@tonic-gate 		(void) strcat(fullpath, "/");
7387c478bd9Sstevel@tonic-gate 		(void) strcat(fullpath, name);
7397c478bd9Sstevel@tonic-gate 	}
7407c478bd9Sstevel@tonic-gate 	curlen = strlen(fullpath);
74112a9e0efSsn199410 
74212a9e0efSsn199410 	if (stat(".", &buffer) == -1) {
74312a9e0efSsn199410 		(void) fprintf(stderr,
74412a9e0efSsn199410 		    gettext("rm: cannot stat current directory: "));
74512a9e0efSsn199410 		perror("");
74612a9e0efSsn199410 		exit(2);
74712a9e0efSsn199410 	}
74812a9e0efSsn199410 	if ((newdir = malloc(sizeof (struct dir_id))) == NULL) {
74912a9e0efSsn199410 		(void) fprintf(stderr,
75012a9e0efSsn199410 		    gettext("rm: Insufficient memory.\n"));
75112a9e0efSsn199410 		cleanup();
75212a9e0efSsn199410 		exit(1);
75312a9e0efSsn199410 	}
75412a9e0efSsn199410 
75512a9e0efSsn199410 	newdir->dev = buffer.st_dev;
75612a9e0efSsn199410 	newdir->inode = buffer.st_ino;
75712a9e0efSsn199410 	newdir->next = current_dir;
75812a9e0efSsn199410 	current_dir = newdir;
7597c478bd9Sstevel@tonic-gate }
7607c478bd9Sstevel@tonic-gate 
761*996aa816Ssn199410 static int
7627c478bd9Sstevel@tonic-gate pop_name(int first)
7637c478bd9Sstevel@tonic-gate {
764*996aa816Ssn199410 	int	retval = 0;
7657c478bd9Sstevel@tonic-gate 	char *slash;
76612a9e0efSsn199410 	struct	stat buffer;
76712a9e0efSsn199410 	struct	dir_id *remove_dir;
76812a9e0efSsn199410 
7697c478bd9Sstevel@tonic-gate 	if (first) {
7707c478bd9Sstevel@tonic-gate 		*fullpath = '\0';
771*996aa816Ssn199410 		return (0);
7727c478bd9Sstevel@tonic-gate 	}
7737c478bd9Sstevel@tonic-gate 	slash = strrchr(fullpath, '/');
7747c478bd9Sstevel@tonic-gate 	if (slash)
7757c478bd9Sstevel@tonic-gate 		*slash = '\0';
7767c478bd9Sstevel@tonic-gate 	else
7777c478bd9Sstevel@tonic-gate 		*fullpath = '\0';
7787c478bd9Sstevel@tonic-gate 	curlen = strlen(fullpath);
77912a9e0efSsn199410 
78012a9e0efSsn199410 	if (stat(".", &buffer) == -1) {
78112a9e0efSsn199410 		(void) fprintf(stderr,
78212a9e0efSsn199410 		    gettext("rm: cannot stat current directory: "));
78312a9e0efSsn199410 		perror("");
78412a9e0efSsn199410 		exit(2);
78512a9e0efSsn199410 	}
78612a9e0efSsn199410 
78712a9e0efSsn199410 	/*
78812a9e0efSsn199410 	 * For each pop operation, verify that the device and inode numbers
78912a9e0efSsn199410 	 * of "." match the numbers recorded before the chdir was done into
79012a9e0efSsn199410 	 * the directory. If they do not match, it is an indication of
79112a9e0efSsn199410 	 * possible malicious activity and rm has chdir to an unintended
79212a9e0efSsn199410 	 * directory
79312a9e0efSsn199410 	 */
79412a9e0efSsn199410 	if ((current_dir->inode != buffer.st_ino) || (current_dir->dev !=
79512a9e0efSsn199410 	    buffer.st_dev)) {
79612a9e0efSsn199410 		(void) fprintf(stderr, gettext("rm: WARNING: "
79712a9e0efSsn199410 		    "A subdirectory of %s was moved or linked to "
79812a9e0efSsn199410 		    "another directory during the execution of rm\n"),
79912a9e0efSsn199410 		    fullpath);
800*996aa816Ssn199410 		retval = -1;
80112a9e0efSsn199410 	}
80212a9e0efSsn199410 	remove_dir = current_dir;
80312a9e0efSsn199410 	current_dir = current_dir->next;
80412a9e0efSsn199410 	free(remove_dir);
805*996aa816Ssn199410 	return (retval);
8067c478bd9Sstevel@tonic-gate }
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate static void
8097c478bd9Sstevel@tonic-gate force_chdir(char *dirname)
8107c478bd9Sstevel@tonic-gate {
8117c478bd9Sstevel@tonic-gate 	char 	*pathname, *mp, *tp;
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	/* use pathname instead of dirname, so dirname won't be modified */
8147c478bd9Sstevel@tonic-gate 	if ((pathname = strdup(dirname)) == NULL) {
8157c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("rm: strdup: "));
8167c478bd9Sstevel@tonic-gate 		perror("");
8177c478bd9Sstevel@tonic-gate 		cleanup();
8187c478bd9Sstevel@tonic-gate 		exit(2);
8197c478bd9Sstevel@tonic-gate 	}
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	/* pathname is an absolute full path from getcwd() */
8227c478bd9Sstevel@tonic-gate 	mp = pathname;
8237c478bd9Sstevel@tonic-gate 	while (mp) {
8247c478bd9Sstevel@tonic-gate 		tp = strchr(mp, '/');
8257c478bd9Sstevel@tonic-gate 		if (strlen(mp) >= PATH_MAX) {
8267c478bd9Sstevel@tonic-gate 			/*
8277c478bd9Sstevel@tonic-gate 			 * after the first iteration through this
8287c478bd9Sstevel@tonic-gate 			 * loop, the below will NULL out the '/'
8297c478bd9Sstevel@tonic-gate 			 * which follows the first dir on pathname
8307c478bd9Sstevel@tonic-gate 			 */
8317c478bd9Sstevel@tonic-gate 			*tp = 0;
8327c478bd9Sstevel@tonic-gate 			tp++;
8337c478bd9Sstevel@tonic-gate 			if (*mp == NULL)
8347c478bd9Sstevel@tonic-gate 				ch_dir("/");
8357c478bd9Sstevel@tonic-gate 			else
8367c478bd9Sstevel@tonic-gate 				/*
8377c478bd9Sstevel@tonic-gate 				 * mp points to the start of a dirname,
8387c478bd9Sstevel@tonic-gate 				 * terminated by NULL, so ch_dir()
8397c478bd9Sstevel@tonic-gate 				 * here will move down one directory
8407c478bd9Sstevel@tonic-gate 				 */
8417c478bd9Sstevel@tonic-gate 				ch_dir(mp);
8427c478bd9Sstevel@tonic-gate 			/*
8437c478bd9Sstevel@tonic-gate 			 * reset mp to the start of the dirname
8447c478bd9Sstevel@tonic-gate 			 * which follows the one we just chdir'd to
8457c478bd9Sstevel@tonic-gate 			 */
8467c478bd9Sstevel@tonic-gate 			mp = tp;
8477c478bd9Sstevel@tonic-gate 			continue;	/* probably can remove this */
8487c478bd9Sstevel@tonic-gate 		} else {
8497c478bd9Sstevel@tonic-gate 			ch_dir(mp);
8507c478bd9Sstevel@tonic-gate 			break;
8517c478bd9Sstevel@tonic-gate 		}
8527c478bd9Sstevel@tonic-gate 	}
8537c478bd9Sstevel@tonic-gate 	free(pathname);
8547c478bd9Sstevel@tonic-gate }
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate static void
8577c478bd9Sstevel@tonic-gate ch_dir(char *dirname)
8587c478bd9Sstevel@tonic-gate {
8597c478bd9Sstevel@tonic-gate 	if (chdir(dirname) == -1) {
8607c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
8617c478bd9Sstevel@tonic-gate 		gettext("rm: cannot change to %s directory: "), dirname);
8627c478bd9Sstevel@tonic-gate 			perror("");
8637c478bd9Sstevel@tonic-gate 			cleanup();
8647c478bd9Sstevel@tonic-gate 			exit(2);
8657c478bd9Sstevel@tonic-gate 	}
8667c478bd9Sstevel@tonic-gate }
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate static void
86912a9e0efSsn199410 chdir_init(void)
8707c478bd9Sstevel@tonic-gate {
8717c478bd9Sstevel@tonic-gate 	/*
87212a9e0efSsn199410 	 * Go back to init dir--the dir from where rm was executed--using
8737c478bd9Sstevel@tonic-gate 	 * one of two methods, depending on which method works
87412a9e0efSsn199410 	 * for the given permissions of the init dir and its
8757c478bd9Sstevel@tonic-gate 	 * parent directories.
8767c478bd9Sstevel@tonic-gate 	 */
87712a9e0efSsn199410 	if (initdirfd != -1) {
87812a9e0efSsn199410 		if (fchdir(initdirfd) == -1) {
8797c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
8807c478bd9Sstevel@tonic-gate 			    gettext("rm: cannot change to starting "
8817c478bd9Sstevel@tonic-gate 			    "directory: "));
8827c478bd9Sstevel@tonic-gate 			perror("");
8837c478bd9Sstevel@tonic-gate 			cleanup();
8847c478bd9Sstevel@tonic-gate 			exit(2);
8857c478bd9Sstevel@tonic-gate 		}
8867c478bd9Sstevel@tonic-gate 	} else {
8877c478bd9Sstevel@tonic-gate 		if (strlen(cwd) < PATH_MAX)
8887c478bd9Sstevel@tonic-gate 			ch_dir(cwd);
8897c478bd9Sstevel@tonic-gate 		else
8907c478bd9Sstevel@tonic-gate 			force_chdir(cwd);
8917c478bd9Sstevel@tonic-gate 	}
8927c478bd9Sstevel@tonic-gate }
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate /*
89512a9e0efSsn199410  * check_initdir -
8967c478bd9Sstevel@tonic-gate  * is only called the first time rm tries to
8977c478bd9Sstevel@tonic-gate  * remove a directory.  It saves the current directory, i.e.,
89812a9e0efSsn199410  * init dir, so we can go back to it after traversing elsewhere.
8997c478bd9Sstevel@tonic-gate  * It also saves all the device and inode numbers of each
90012a9e0efSsn199410  * dir from the initial dir back to the root in a linked list, so we
9017c478bd9Sstevel@tonic-gate  * can later check, via mypath(), if we are trying to remove our current
9027c478bd9Sstevel@tonic-gate  * dir or an ancestor.
9037c478bd9Sstevel@tonic-gate  */
9047c478bd9Sstevel@tonic-gate static void
90512a9e0efSsn199410 check_initdir(void)
9067c478bd9Sstevel@tonic-gate {
90712a9e0efSsn199410 	int	size;	/* size allocated for pathname of init dir (cwd) */
9087c478bd9Sstevel@tonic-gate 	struct stat buffer;
9097c478bd9Sstevel@tonic-gate 	struct dir_id *lastdir, *curdir;
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	/*
91212a9e0efSsn199410 	 * We need to save where we currently are (the "init dir") so
9137c478bd9Sstevel@tonic-gate 	 * we can return after traversing down directories we're
9147c478bd9Sstevel@tonic-gate 	 * removing.  Two methods are attempted:
9157c478bd9Sstevel@tonic-gate 	 *
91612a9e0efSsn199410 	 * 1) open() the initial dir so we can use the fd
9177c478bd9Sstevel@tonic-gate 	 *    to fchdir() back.  This requires read permission
91812a9e0efSsn199410 	 *    on the initial dir.
9197c478bd9Sstevel@tonic-gate 	 *
9207c478bd9Sstevel@tonic-gate 	 * 2) getcwd() so we can chdir() to go back.  This
92112a9e0efSsn199410 	 *    requires search (x) permission on the init dir,
9227c478bd9Sstevel@tonic-gate 	 *    and read and search permission on all parent dirs.  Also,
92312a9e0efSsn199410 	 *    getcwd() will not work if the init dir is > 341
9247c478bd9Sstevel@tonic-gate 	 *    directories deep (see open bugid 4033182 - getcwd needs
9257c478bd9Sstevel@tonic-gate 	 *    to work for pathnames of any depth).
9267c478bd9Sstevel@tonic-gate 	 *
9277c478bd9Sstevel@tonic-gate 	 * If neither method works, we can't remove any directories
9287c478bd9Sstevel@tonic-gate 	 * and rm will fail.
9297c478bd9Sstevel@tonic-gate 	 *
9307c478bd9Sstevel@tonic-gate 	 * For future enhancement, a possible 3rd option to use
9317c478bd9Sstevel@tonic-gate 	 * would be to fork a process to remove a directory,
93212a9e0efSsn199410 	 * eliminating the need to chdir back to the initial directory
93312a9e0efSsn199410 	 * and eliminating the permission restrictions on the initial dir
9347c478bd9Sstevel@tonic-gate 	 * or its parent dirs.
9357c478bd9Sstevel@tonic-gate 	 */
93612a9e0efSsn199410 	initdirfd = open(".", O_RDONLY);
93712a9e0efSsn199410 	if (initdirfd == -1) {
9387c478bd9Sstevel@tonic-gate 		size = PATH_MAX;
9397c478bd9Sstevel@tonic-gate 		while ((cwd = getcwd(NULL, size)) == NULL) {
9407c478bd9Sstevel@tonic-gate 			if (errno == ERANGE) {
9417c478bd9Sstevel@tonic-gate 				size = PATH_MAX + size;
9427c478bd9Sstevel@tonic-gate 				continue;
9437c478bd9Sstevel@tonic-gate 			} else {
9447c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
9457c478bd9Sstevel@tonic-gate 				    gettext("rm: cannot open starting "
9467c478bd9Sstevel@tonic-gate 				    "directory: "));
9477c478bd9Sstevel@tonic-gate 				perror("pwd");
9487c478bd9Sstevel@tonic-gate 				exit(2);
9497c478bd9Sstevel@tonic-gate 			}
9507c478bd9Sstevel@tonic-gate 		}
9517c478bd9Sstevel@tonic-gate 	}
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	/*
9547c478bd9Sstevel@tonic-gate 	 * since we exit on error here, we're guaranteed to at least
95512a9e0efSsn199410 	 * have info in the first dir_id struct, initdir
9567c478bd9Sstevel@tonic-gate 	 */
9577c478bd9Sstevel@tonic-gate 	if (stat(".", &buffer) == -1) {
9587c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
9597c478bd9Sstevel@tonic-gate 		    gettext("rm: cannot stat current directory: "));
9607c478bd9Sstevel@tonic-gate 		perror("");
9617c478bd9Sstevel@tonic-gate 		exit(2);
9627c478bd9Sstevel@tonic-gate 	}
96312a9e0efSsn199410 	initdir.dev = buffer.st_dev;
96412a9e0efSsn199410 	initdir.inode = buffer.st_ino;
96512a9e0efSsn199410 	initdir.next = NULL;
9667c478bd9Sstevel@tonic-gate 
96712a9e0efSsn199410 	lastdir = &initdir;
9687c478bd9Sstevel@tonic-gate 	/*
9697c478bd9Sstevel@tonic-gate 	 * Starting from current working directory, walk toward the
9707c478bd9Sstevel@tonic-gate 	 * root, looking at each directory along the way.
9717c478bd9Sstevel@tonic-gate 	 */
9727c478bd9Sstevel@tonic-gate 	for (;;) {
9737c478bd9Sstevel@tonic-gate 		if (chdir("..") == -1 || lstat(".", &buffer) == -1) {
9747c478bd9Sstevel@tonic-gate 			parent_err = 1;
9757c478bd9Sstevel@tonic-gate 			break;
9767c478bd9Sstevel@tonic-gate 		}
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 		if ((lastdir->next = malloc(sizeof (struct dir_id))) ==
9797c478bd9Sstevel@tonic-gate 		    NULL) {
9807c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9817c478bd9Sstevel@tonic-gate 			    gettext("rm: Insufficient memory.\n"));
9827c478bd9Sstevel@tonic-gate 			cleanup();
9837c478bd9Sstevel@tonic-gate 			exit(1);
9847c478bd9Sstevel@tonic-gate 		}
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 		curdir = lastdir->next;
9877c478bd9Sstevel@tonic-gate 		curdir->dev = buffer.st_dev;
9887c478bd9Sstevel@tonic-gate 		curdir->inode = buffer.st_ino;
9897c478bd9Sstevel@tonic-gate 		curdir->next = NULL;
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 		/*
9927c478bd9Sstevel@tonic-gate 		 * Stop when we reach the root; note that chdir("..")
9937c478bd9Sstevel@tonic-gate 		 * at the root dir will stay in root. Get rid of
9947c478bd9Sstevel@tonic-gate 		 * the redundant dir_id struct for root.
9957c478bd9Sstevel@tonic-gate 		 */
9967c478bd9Sstevel@tonic-gate 		if (curdir->dev == lastdir->dev && curdir->inode ==
9977c478bd9Sstevel@tonic-gate 		    lastdir->inode) {
9987c478bd9Sstevel@tonic-gate 			lastdir->next = NULL;
9997c478bd9Sstevel@tonic-gate 			free(curdir);
10007c478bd9Sstevel@tonic-gate 			break;
10017c478bd9Sstevel@tonic-gate 		}
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 			/* loop again to go back another level */
10047c478bd9Sstevel@tonic-gate 		lastdir = curdir;
10057c478bd9Sstevel@tonic-gate 	}
100612a9e0efSsn199410 		/* go back to init directory */
100712a9e0efSsn199410 	chdir_init();
10087c478bd9Sstevel@tonic-gate }
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate /*
10117c478bd9Sstevel@tonic-gate  * cleanup the dynamically-allocated list of device numbers and inodes,
101212a9e0efSsn199410  * if any.  If initdir was never used, it is external and static so
101312a9e0efSsn199410  * it is guaranteed initialized to zero, thus initdir.next would be NULL.
10147c478bd9Sstevel@tonic-gate  */
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate static void
10177c478bd9Sstevel@tonic-gate cleanup(void) {
101812a9e0efSsn199410 
10197c478bd9Sstevel@tonic-gate 	struct dir_id *lastdir, *curdir;
10207c478bd9Sstevel@tonic-gate 
102112a9e0efSsn199410 	curdir = initdir.next;
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	while (curdir != NULL) {
10247c478bd9Sstevel@tonic-gate 		lastdir = curdir;
10257c478bd9Sstevel@tonic-gate 		curdir = curdir->next;
10267c478bd9Sstevel@tonic-gate 		free(lastdir);
10277c478bd9Sstevel@tonic-gate 	}
10287c478bd9Sstevel@tonic-gate }
1029