xref: /onnv-gate/usr/src/cmd/backup/restore/utilities.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
7*0Sstevel@tonic-gate /*	  All Rights Reserved	*/
8*0Sstevel@tonic-gate 
9*0Sstevel@tonic-gate /*
10*0Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
11*0Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
12*0Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
13*0Sstevel@tonic-gate  */
14*0Sstevel@tonic-gate 
15*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
16*0Sstevel@tonic-gate 
17*0Sstevel@tonic-gate #include "restore.h"
18*0Sstevel@tonic-gate #include <ctype.h>
19*0Sstevel@tonic-gate #include <errno.h>
20*0Sstevel@tonic-gate #include <syslog.h>
21*0Sstevel@tonic-gate #include <limits.h>
22*0Sstevel@tonic-gate /* LINTED: this file really is necessary */
23*0Sstevel@tonic-gate #include <euc.h>
24*0Sstevel@tonic-gate #include <widec.h>
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Insure that all the components of a pathname exist. Note that
28*0Sstevel@tonic-gate  * lookupname() and addentry() both expect complex names as
29*0Sstevel@tonic-gate  * input arguments, so a double NULL needs to be added to each name.
30*0Sstevel@tonic-gate  */
31*0Sstevel@tonic-gate void
32*0Sstevel@tonic-gate pathcheck(name)
33*0Sstevel@tonic-gate 	char *name;
34*0Sstevel@tonic-gate {
35*0Sstevel@tonic-gate 	char *cp, save;
36*0Sstevel@tonic-gate 	struct entry *ep;
37*0Sstevel@tonic-gate 	char *start;
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate 	start = strchr(name, '/');
40*0Sstevel@tonic-gate 	if (start == 0)
41*0Sstevel@tonic-gate 		return;
42*0Sstevel@tonic-gate 	for (cp = start; *cp != '\0'; cp++) {
43*0Sstevel@tonic-gate 		if (*cp != '/')
44*0Sstevel@tonic-gate 			continue;
45*0Sstevel@tonic-gate 		*cp = '\0';
46*0Sstevel@tonic-gate 		save = *(cp+1);
47*0Sstevel@tonic-gate 		*(cp+1) = '\0';
48*0Sstevel@tonic-gate 		ep = lookupname(name);
49*0Sstevel@tonic-gate 		if (ep == NIL) {
50*0Sstevel@tonic-gate 			ep = addentry(name, psearch(name), NODE);
51*0Sstevel@tonic-gate 			newnode(ep);
52*0Sstevel@tonic-gate 		}
53*0Sstevel@tonic-gate 		/* LINTED: result fits in a short */
54*0Sstevel@tonic-gate 		ep->e_flags |= NEW|KEEP;
55*0Sstevel@tonic-gate 		*cp = '/';
56*0Sstevel@tonic-gate 		*(cp+1) = save;
57*0Sstevel@tonic-gate 	}
58*0Sstevel@tonic-gate }
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate /*
61*0Sstevel@tonic-gate  * Change a name to a unique temporary name.
62*0Sstevel@tonic-gate  */
63*0Sstevel@tonic-gate void
64*0Sstevel@tonic-gate mktempname(ep)
65*0Sstevel@tonic-gate 	struct entry *ep;
66*0Sstevel@tonic-gate {
67*0Sstevel@tonic-gate 	char *newname;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	if (ep->e_flags & TMPNAME)
70*0Sstevel@tonic-gate 		badentry(ep, gettext("mktempname: called with TMPNAME"));
71*0Sstevel@tonic-gate 	/* LINTED: result fits in a short */
72*0Sstevel@tonic-gate 	ep->e_flags |= TMPNAME;
73*0Sstevel@tonic-gate 	newname = savename(gentempname(ep));
74*0Sstevel@tonic-gate 	renameit(myname(ep), newname);
75*0Sstevel@tonic-gate 	freename(ep->e_name);
76*0Sstevel@tonic-gate 	ep->e_name = newname;
77*0Sstevel@tonic-gate 	/* LINTED: savename guarantees strlen will fit */
78*0Sstevel@tonic-gate 	ep->e_namlen = strlen(ep->e_name);
79*0Sstevel@tonic-gate }
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate /*
82*0Sstevel@tonic-gate  * Generate a temporary name for an entry.
83*0Sstevel@tonic-gate  */
84*0Sstevel@tonic-gate char *
85*0Sstevel@tonic-gate gentempname(ep)
86*0Sstevel@tonic-gate 	struct entry *ep;
87*0Sstevel@tonic-gate {
88*0Sstevel@tonic-gate 	static char name[MAXPATHLEN];
89*0Sstevel@tonic-gate 	struct entry *np;
90*0Sstevel@tonic-gate 	long i = 0;
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	for (np = lookupino(ep->e_ino); np != NIL && np != ep; np = np->e_links)
93*0Sstevel@tonic-gate 		i++;
94*0Sstevel@tonic-gate 	if (np == NIL)
95*0Sstevel@tonic-gate 		badentry(ep, gettext("not on ino list"));
96*0Sstevel@tonic-gate 	(void) snprintf(name, sizeof (name), "%s%ld%lu", TMPHDR, i, ep->e_ino);
97*0Sstevel@tonic-gate 	return (name);
98*0Sstevel@tonic-gate }
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate /*
101*0Sstevel@tonic-gate  * Rename a file or directory.
102*0Sstevel@tonic-gate  */
103*0Sstevel@tonic-gate void
104*0Sstevel@tonic-gate renameit(fp, tp)
105*0Sstevel@tonic-gate 	char *fp;
106*0Sstevel@tonic-gate 	char *tp;
107*0Sstevel@tonic-gate {
108*0Sstevel@tonic-gate 	int fromfd, tofd;
109*0Sstevel@tonic-gate 	char *from, *to;
110*0Sstevel@tonic-gate 	char tobuf[MAXPATHLEN];
111*0Sstevel@tonic-gate 	char *pathend;
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	resolve(fp, &fromfd, &from);
114*0Sstevel@tonic-gate 	/*
115*0Sstevel@tonic-gate 	 * The to pointer argument is assumed to be either a fully
116*0Sstevel@tonic-gate 	 * specified path (starting with "./") or a simple temporary
117*0Sstevel@tonic-gate 	 * file name (starting with TMPHDR).  If passed a simple temp
118*0Sstevel@tonic-gate 	 * file name, we need to set up the descriptors explicitly.
119*0Sstevel@tonic-gate 	 */
120*0Sstevel@tonic-gate 	if (strncmp(tp, TMPHDR, sizeof (TMPHDR) - 1) == 0) {
121*0Sstevel@tonic-gate 		tofd = fromfd;
122*0Sstevel@tonic-gate 		if ((pathend = strrchr(from, '/')) != NULL) {
123*0Sstevel@tonic-gate 			strncpy(tobuf, from, pathend - from + 1);
124*0Sstevel@tonic-gate 			tobuf[pathend - from + 1] = NULL;
125*0Sstevel@tonic-gate 			strlcat(tobuf, tp, sizeof (tobuf));
126*0Sstevel@tonic-gate 			to = tobuf;
127*0Sstevel@tonic-gate 		} else {
128*0Sstevel@tonic-gate 			to = tp;
129*0Sstevel@tonic-gate 		}
130*0Sstevel@tonic-gate 	} else
131*0Sstevel@tonic-gate 		resolve(tp, &tofd, &to);
132*0Sstevel@tonic-gate 	if (renameat(fromfd, from, tofd, to) < 0) {
133*0Sstevel@tonic-gate 		int saverr = errno;
134*0Sstevel@tonic-gate 		(void) fprintf(stderr,
135*0Sstevel@tonic-gate 		    gettext("Warning: cannot rename %s to %s: %s\n"),
136*0Sstevel@tonic-gate 		    from, to, strerror(saverr));
137*0Sstevel@tonic-gate 		(void) fflush(stderr);
138*0Sstevel@tonic-gate 	} else {
139*0Sstevel@tonic-gate 		vprintf(stdout, gettext("rename %s to %s\n"), from, to);
140*0Sstevel@tonic-gate 	}
141*0Sstevel@tonic-gate 	if (fromfd != AT_FDCWD) (void) close(fromfd);
142*0Sstevel@tonic-gate 	if (tofd != AT_FDCWD) (void) close(tofd);
143*0Sstevel@tonic-gate }
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate /*
146*0Sstevel@tonic-gate  * Create a new node (directory). Note that, because we have no
147*0Sstevel@tonic-gate  * mkdirat() function, fchdir() must be used set up the appropriate
148*0Sstevel@tonic-gate  * name space context prior to the call to mkdir() if we are
149*0Sstevel@tonic-gate  * operating in attribute space.
150*0Sstevel@tonic-gate  */
151*0Sstevel@tonic-gate void
152*0Sstevel@tonic-gate newnode(np)
153*0Sstevel@tonic-gate 	struct entry *np;
154*0Sstevel@tonic-gate {
155*0Sstevel@tonic-gate 	char *cp;
156*0Sstevel@tonic-gate 	int dfd;
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	if (np->e_type != NODE)
159*0Sstevel@tonic-gate 		badentry(np, gettext("newnode: not a node"));
160*0Sstevel@tonic-gate 	resolve(myname(np), &dfd, &cp);
161*0Sstevel@tonic-gate 	if (dfd != AT_FDCWD) {
162*0Sstevel@tonic-gate 		if (fchdir(dfd) < 0) {
163*0Sstevel@tonic-gate 			int saverr = errno;
164*0Sstevel@tonic-gate 			(void) fprintf(stderr,
165*0Sstevel@tonic-gate 				gettext("Warning: cannot create %s: %s"),
166*0Sstevel@tonic-gate 				cp, strerror(saverr));
167*0Sstevel@tonic-gate 			(void) fflush(stderr);
168*0Sstevel@tonic-gate 			(void) close(dfd);
169*0Sstevel@tonic-gate 			return;
170*0Sstevel@tonic-gate 		}
171*0Sstevel@tonic-gate 	}
172*0Sstevel@tonic-gate 	if (mkdir(cp, 0777) < 0) {
173*0Sstevel@tonic-gate 		int saverr = errno;
174*0Sstevel@tonic-gate 		/* LINTED: result fits in a short */
175*0Sstevel@tonic-gate 		np->e_flags |= EXISTED;
176*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Warning: "));
177*0Sstevel@tonic-gate 		(void) fflush(stderr);
178*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s\n", cp, strerror(saverr));
179*0Sstevel@tonic-gate 	} else {
180*0Sstevel@tonic-gate 		vprintf(stdout, gettext("Make node %s\n"), cp);
181*0Sstevel@tonic-gate 	}
182*0Sstevel@tonic-gate 	if (dfd != AT_FDCWD) {
183*0Sstevel@tonic-gate 		fchdir(savepwd);
184*0Sstevel@tonic-gate 		(void) close(dfd);
185*0Sstevel@tonic-gate 	}
186*0Sstevel@tonic-gate }
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate /*
189*0Sstevel@tonic-gate  * Remove an old node (directory). See comment above on newnode()
190*0Sstevel@tonic-gate  * for explanation of fchdir() use below.
191*0Sstevel@tonic-gate  */
192*0Sstevel@tonic-gate void
193*0Sstevel@tonic-gate removenode(ep)
194*0Sstevel@tonic-gate 	struct entry *ep;
195*0Sstevel@tonic-gate {
196*0Sstevel@tonic-gate 	char *cp;
197*0Sstevel@tonic-gate 	int dfd;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	if (ep->e_type != NODE)
200*0Sstevel@tonic-gate 		badentry(ep, gettext("removenode: not a node"));
201*0Sstevel@tonic-gate 	if (ep->e_entries != NIL)
202*0Sstevel@tonic-gate 		badentry(ep, gettext("removenode: non-empty directory"));
203*0Sstevel@tonic-gate 	/* LINTED: result fits in a short */
204*0Sstevel@tonic-gate 	ep->e_flags |= REMOVED;
205*0Sstevel@tonic-gate 	/* LINTED: result fits in a short */
206*0Sstevel@tonic-gate 	ep->e_flags &= ~TMPNAME;
207*0Sstevel@tonic-gate 	resolve(myname(ep), &dfd, &cp);
208*0Sstevel@tonic-gate 	if (dfd != AT_FDCWD) {
209*0Sstevel@tonic-gate 		if (fchdir(dfd) < 0) {
210*0Sstevel@tonic-gate 			int saverr = errno;
211*0Sstevel@tonic-gate 			(void) fprintf(stderr,
212*0Sstevel@tonic-gate 				gettext("Warning: cannot remove %s: %s"),
213*0Sstevel@tonic-gate 				cp, strerror(saverr));
214*0Sstevel@tonic-gate 			(void) fflush(stderr);
215*0Sstevel@tonic-gate 			(void) close(dfd);
216*0Sstevel@tonic-gate 			return;
217*0Sstevel@tonic-gate 		}
218*0Sstevel@tonic-gate 	}
219*0Sstevel@tonic-gate 	if (rmdir(cp) < 0) {	/* NOTE: could use unlinkat (..,REMOVEDIR) */
220*0Sstevel@tonic-gate 		int saverr = errno;
221*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Warning: %s: %s\n"),
222*0Sstevel@tonic-gate 			cp, strerror(saverr));
223*0Sstevel@tonic-gate 		(void) fflush(stderr);
224*0Sstevel@tonic-gate 	} else {
225*0Sstevel@tonic-gate 		vprintf(stdout, gettext("Remove node %s\n"), cp);
226*0Sstevel@tonic-gate 	}
227*0Sstevel@tonic-gate 	if (dfd != AT_FDCWD) {
228*0Sstevel@tonic-gate 		(void) fchdir(savepwd);
229*0Sstevel@tonic-gate 		(void) close(dfd);
230*0Sstevel@tonic-gate 	}
231*0Sstevel@tonic-gate }
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate /*
234*0Sstevel@tonic-gate  * Remove a leaf.
235*0Sstevel@tonic-gate  */
236*0Sstevel@tonic-gate void
237*0Sstevel@tonic-gate removeleaf(ep)
238*0Sstevel@tonic-gate 	struct entry *ep;
239*0Sstevel@tonic-gate {
240*0Sstevel@tonic-gate 	char *cp;
241*0Sstevel@tonic-gate 	int dfd;
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	if (ep->e_type != LEAF)
244*0Sstevel@tonic-gate 		badentry(ep, gettext("removeleaf: not a leaf"));
245*0Sstevel@tonic-gate 	/* LINTED: result fits in a short */
246*0Sstevel@tonic-gate 	ep->e_flags |= REMOVED;
247*0Sstevel@tonic-gate 	/* LINTED: result fits in a short */
248*0Sstevel@tonic-gate 	ep->e_flags &= ~TMPNAME;
249*0Sstevel@tonic-gate 	resolve(myname(ep), &dfd, &cp);
250*0Sstevel@tonic-gate 	if (unlinkat(dfd, cp, 0) < 0) {
251*0Sstevel@tonic-gate 		int saverr = errno;
252*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Warning: %s: %s\n"),
253*0Sstevel@tonic-gate 			cp, strerror(saverr));
254*0Sstevel@tonic-gate 		(void) fflush(stderr);
255*0Sstevel@tonic-gate 	} else {
256*0Sstevel@tonic-gate 		vprintf(stdout, gettext("Remove leaf %s\n"), cp);
257*0Sstevel@tonic-gate 	}
258*0Sstevel@tonic-gate 	if (dfd != AT_FDCWD)
259*0Sstevel@tonic-gate 		(void) close(dfd);
260*0Sstevel@tonic-gate }
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate /*
263*0Sstevel@tonic-gate  * Create a link.
264*0Sstevel@tonic-gate  *	This function assumes that the context has already been set
265*0Sstevel@tonic-gate  *	for the link file to be created (i.e., we have "fchdir-ed"
266*0Sstevel@tonic-gate  *	into attribute space already if this is an attribute link).
267*0Sstevel@tonic-gate  */
268*0Sstevel@tonic-gate lf_linkit(existing, new, type)
269*0Sstevel@tonic-gate 	char *existing, *new;
270*0Sstevel@tonic-gate 	int type;
271*0Sstevel@tonic-gate {
272*0Sstevel@tonic-gate 	char linkbuf[MAXPATHLEN];
273*0Sstevel@tonic-gate 	struct stat64 s1[1], s2[1];
274*0Sstevel@tonic-gate 	char *name;
275*0Sstevel@tonic-gate 	int dfd, l, result;
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 	resolve(existing, &dfd, &name);
278*0Sstevel@tonic-gate 	if (dfd == -1) {
279*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
280*0Sstevel@tonic-gate 			"Warning: cannot restore %s link %s->%s\n"),
281*0Sstevel@tonic-gate 			(type == SYMLINK ? "symbolic" : "hard"), new, existing);
282*0Sstevel@tonic-gate 		result = FAIL;
283*0Sstevel@tonic-gate 		goto out;
284*0Sstevel@tonic-gate 	}
285*0Sstevel@tonic-gate 	if (type == SYMLINK) {
286*0Sstevel@tonic-gate 		if (symlink(name, new) < 0) {
287*0Sstevel@tonic-gate 			/* No trailing \0 from readlink(2) */
288*0Sstevel@tonic-gate 			if (((l = readlink(new, linkbuf, sizeof (linkbuf)))
289*0Sstevel@tonic-gate 			    > 0) &&
290*0Sstevel@tonic-gate 			    (l == strlen(name)) &&
291*0Sstevel@tonic-gate 			    (strncmp(linkbuf, name, l) == 0)) {
292*0Sstevel@tonic-gate 				vprintf(stdout,
293*0Sstevel@tonic-gate 				    gettext("Symbolic link %s->%s ok\n"),
294*0Sstevel@tonic-gate 				    new, name);
295*0Sstevel@tonic-gate 				result = GOOD;
296*0Sstevel@tonic-gate 				goto out;
297*0Sstevel@tonic-gate 			} else {
298*0Sstevel@tonic-gate 				int saverr = errno;
299*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
300*0Sstevel@tonic-gate 			    "Warning: cannot create symbolic link %s->%s: %s"),
301*0Sstevel@tonic-gate 				    new, name, strerror(saverr));
302*0Sstevel@tonic-gate 				(void) fflush(stderr);
303*0Sstevel@tonic-gate 				result = FAIL;
304*0Sstevel@tonic-gate 				goto out;
305*0Sstevel@tonic-gate 			}
306*0Sstevel@tonic-gate 		}
307*0Sstevel@tonic-gate 	} else if (type == HARDLINK) {
308*0Sstevel@tonic-gate 		if (link(name, new) < 0) {
309*0Sstevel@tonic-gate 			int saverr = errno;
310*0Sstevel@tonic-gate 			if ((stat64(name, s1) == 0) &&
311*0Sstevel@tonic-gate 			    (stat64(new, s2) == 0) &&
312*0Sstevel@tonic-gate 			    (s1->st_dev == s2->st_dev) &&
313*0Sstevel@tonic-gate 			    (s1->st_ino == s2->st_ino)) {
314*0Sstevel@tonic-gate 				vprintf(stdout,
315*0Sstevel@tonic-gate 				    gettext("Hard link %s->%s ok\n"),
316*0Sstevel@tonic-gate 				    new, name);
317*0Sstevel@tonic-gate 				result = GOOD;
318*0Sstevel@tonic-gate 				goto out;
319*0Sstevel@tonic-gate 			} else {
320*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
321*0Sstevel@tonic-gate 			    "Warning: cannot create hard link %s->%s: %s\n"),
322*0Sstevel@tonic-gate 				    new, name, strerror(saverr));
323*0Sstevel@tonic-gate 				(void) fflush(stderr);
324*0Sstevel@tonic-gate 				result = FAIL;
325*0Sstevel@tonic-gate 				goto out;
326*0Sstevel@tonic-gate 			}
327*0Sstevel@tonic-gate 		}
328*0Sstevel@tonic-gate 	} else {
329*0Sstevel@tonic-gate 		panic(gettext("%s: unknown type %d\n"), "linkit", type);
330*0Sstevel@tonic-gate 		result = FAIL;
331*0Sstevel@tonic-gate 		goto out;
332*0Sstevel@tonic-gate 	}
333*0Sstevel@tonic-gate 	result = GOOD;
334*0Sstevel@tonic-gate 	if (type == SYMLINK)
335*0Sstevel@tonic-gate 		vprintf(stdout, gettext("Create symbolic link %s->%s\n"),
336*0Sstevel@tonic-gate 		    new, name);
337*0Sstevel@tonic-gate 	else
338*0Sstevel@tonic-gate 		vprintf(stdout, gettext("Create hard link %s->%s\n"),
339*0Sstevel@tonic-gate 		    new, name);
340*0Sstevel@tonic-gate out:
341*0Sstevel@tonic-gate 	if (dfd != AT_FDCWD) {
342*0Sstevel@tonic-gate 		(void) close(dfd);
343*0Sstevel@tonic-gate 	}
344*0Sstevel@tonic-gate 	return (result);
345*0Sstevel@tonic-gate }
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate /*
348*0Sstevel@tonic-gate  * Find lowest-numbered inode (above "start") that needs to be extracted.
349*0Sstevel@tonic-gate  * Caller knows that a return value of maxino means there's nothing left.
350*0Sstevel@tonic-gate  */
351*0Sstevel@tonic-gate ino_t
352*0Sstevel@tonic-gate lowerbnd(start)
353*0Sstevel@tonic-gate 	ino_t start;
354*0Sstevel@tonic-gate {
355*0Sstevel@tonic-gate 	struct entry *ep;
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 	for (; start < maxino; start++) {
358*0Sstevel@tonic-gate 		ep = lookupino(start);
359*0Sstevel@tonic-gate 		if (ep == NIL || ep->e_type == NODE)
360*0Sstevel@tonic-gate 			continue;
361*0Sstevel@tonic-gate 		if (ep->e_flags & (NEW|EXTRACT))
362*0Sstevel@tonic-gate 			return (start);
363*0Sstevel@tonic-gate 	}
364*0Sstevel@tonic-gate 	return (start);
365*0Sstevel@tonic-gate }
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate /*
368*0Sstevel@tonic-gate  * Find highest-numbered inode (below "start") that needs to be extracted.
369*0Sstevel@tonic-gate  */
370*0Sstevel@tonic-gate ino_t
371*0Sstevel@tonic-gate upperbnd(start)
372*0Sstevel@tonic-gate 	ino_t start;
373*0Sstevel@tonic-gate {
374*0Sstevel@tonic-gate 	struct entry *ep;
375*0Sstevel@tonic-gate 
376*0Sstevel@tonic-gate 	for (; start > ROOTINO; start--) {
377*0Sstevel@tonic-gate 		ep = lookupino(start);
378*0Sstevel@tonic-gate 		if (ep == NIL || ep->e_type == NODE)
379*0Sstevel@tonic-gate 			continue;
380*0Sstevel@tonic-gate 		if (ep->e_flags & (NEW|EXTRACT))
381*0Sstevel@tonic-gate 			return (start);
382*0Sstevel@tonic-gate 	}
383*0Sstevel@tonic-gate 	return (start);
384*0Sstevel@tonic-gate }
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate /*
387*0Sstevel@tonic-gate  * report on a badly formed entry
388*0Sstevel@tonic-gate  */
389*0Sstevel@tonic-gate void
390*0Sstevel@tonic-gate badentry(ep, msg)
391*0Sstevel@tonic-gate 	struct entry *ep;
392*0Sstevel@tonic-gate 	char *msg;
393*0Sstevel@tonic-gate {
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("bad entry: %s\n"), msg);
396*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("name: %s\n"), myname(ep));
397*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("parent name %s\n"),
398*0Sstevel@tonic-gate 		myname(ep->e_parent));
399*0Sstevel@tonic-gate 	if (ep->e_sibling != NIL)
400*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("sibling name: %s\n"),
401*0Sstevel@tonic-gate 		    myname(ep->e_sibling));
402*0Sstevel@tonic-gate 	if (ep->e_entries != NIL)
403*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("next entry name: %s\n"),
404*0Sstevel@tonic-gate 		    myname(ep->e_entries));
405*0Sstevel@tonic-gate 	if (ep->e_links != NIL)
406*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("next link name: %s\n"),
407*0Sstevel@tonic-gate 		    myname(ep->e_links));
408*0Sstevel@tonic-gate 	if (ep->e_xattrs != NIL)
409*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("attribute root name: %s\n"),
410*0Sstevel@tonic-gate 		    myname(ep->e_xattrs));
411*0Sstevel@tonic-gate 	if (ep->e_next != NIL)
412*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("next hashchain name: %s\n"),
413*0Sstevel@tonic-gate 		    myname(ep->e_next));
414*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("entry type: %s\n"),
415*0Sstevel@tonic-gate 	    ep->e_type == NODE ? gettext("NODE") : gettext("LEAF"));
416*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("inode number: %lu\n"), ep->e_ino);
417*0Sstevel@tonic-gate 	panic(gettext("flags: %s\n"), flagvalues(ep));
418*0Sstevel@tonic-gate 	/* Our callers are expected to handle our returning. */
419*0Sstevel@tonic-gate }
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate /*
422*0Sstevel@tonic-gate  * Construct a string indicating the active flag bits of an entry.
423*0Sstevel@tonic-gate  */
424*0Sstevel@tonic-gate char *
425*0Sstevel@tonic-gate flagvalues(ep)
426*0Sstevel@tonic-gate 	struct entry *ep;
427*0Sstevel@tonic-gate {
428*0Sstevel@tonic-gate 	static char flagbuf[BUFSIZ];
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate 	(void) strlcpy(flagbuf, gettext("|NIL"), sizeof (flagbuf));
431*0Sstevel@tonic-gate 	flagbuf[0] = '\0';
432*0Sstevel@tonic-gate 	if (ep->e_flags & REMOVED)
433*0Sstevel@tonic-gate 		(void) strlcat(flagbuf, gettext("|REMOVED"), sizeof (flagbuf));
434*0Sstevel@tonic-gate 	if (ep->e_flags & TMPNAME)
435*0Sstevel@tonic-gate 		(void) strlcat(flagbuf, gettext("|TMPNAME"), sizeof (flagbuf));
436*0Sstevel@tonic-gate 	if (ep->e_flags & EXTRACT)
437*0Sstevel@tonic-gate 		(void) strlcat(flagbuf, gettext("|EXTRACT"), sizeof (flagbuf));
438*0Sstevel@tonic-gate 	if (ep->e_flags & NEW)
439*0Sstevel@tonic-gate 		(void) strlcat(flagbuf, gettext("|NEW"), sizeof (flagbuf));
440*0Sstevel@tonic-gate 	if (ep->e_flags & KEEP)
441*0Sstevel@tonic-gate 		(void) strlcat(flagbuf, gettext("|KEEP"), sizeof (flagbuf));
442*0Sstevel@tonic-gate 	if (ep->e_flags & EXISTED)
443*0Sstevel@tonic-gate 		(void) strlcat(flagbuf, gettext("|EXISTED"), sizeof (flagbuf));
444*0Sstevel@tonic-gate 	if (ep->e_flags & XATTR)
445*0Sstevel@tonic-gate 		(void) strlcat(flagbuf, gettext("|XATTR"), sizeof (flagbuf));
446*0Sstevel@tonic-gate 	if (ep->e_flags & XATTRROOT)
447*0Sstevel@tonic-gate 		(void) strlcat(flagbuf, gettext("|XATTRROOT"),
448*0Sstevel@tonic-gate 						sizeof (flagbuf));
449*0Sstevel@tonic-gate 	return (&flagbuf[1]);
450*0Sstevel@tonic-gate }
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate /*
453*0Sstevel@tonic-gate  * Check to see if a name is on a dump tape.
454*0Sstevel@tonic-gate  */
455*0Sstevel@tonic-gate ino_t
456*0Sstevel@tonic-gate dirlookup(name)
457*0Sstevel@tonic-gate 	char *name;
458*0Sstevel@tonic-gate {
459*0Sstevel@tonic-gate 	ino_t ino;
460*0Sstevel@tonic-gate 
461*0Sstevel@tonic-gate 	ino = psearch(name);
462*0Sstevel@tonic-gate 	if (ino == 0 || BIT(ino, dumpmap) == 0)
463*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not on volume\n"), name);
464*0Sstevel@tonic-gate 	return (ino);
465*0Sstevel@tonic-gate }
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate /*
468*0Sstevel@tonic-gate  * Elicit a reply.
469*0Sstevel@tonic-gate  */
470*0Sstevel@tonic-gate reply(question)
471*0Sstevel@tonic-gate 	char *question;
472*0Sstevel@tonic-gate {
473*0Sstevel@tonic-gate 	char *yesorno = gettext("yn"); /* must be two characters, "yes" first */
474*0Sstevel@tonic-gate 	int c;
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate 	do	{
477*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s? [%s] ", question, yesorno);
478*0Sstevel@tonic-gate 		(void) fflush(stderr);
479*0Sstevel@tonic-gate 		c = getc(terminal);
480*0Sstevel@tonic-gate 		while (c != '\n' && getc(terminal) != '\n') {
481*0Sstevel@tonic-gate 			if (ferror(terminal)) {
482*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
483*0Sstevel@tonic-gate 					"Error reading response\n"));
484*0Sstevel@tonic-gate 				(void) fflush(stderr);
485*0Sstevel@tonic-gate 				return (FAIL);
486*0Sstevel@tonic-gate 			}
487*0Sstevel@tonic-gate 			if (feof(terminal))
488*0Sstevel@tonic-gate 				return (FAIL);
489*0Sstevel@tonic-gate 		}
490*0Sstevel@tonic-gate 		if (isupper(c))
491*0Sstevel@tonic-gate 			c = tolower(c);
492*0Sstevel@tonic-gate 	} while (c != yesorno[0] && c != yesorno[1]);
493*0Sstevel@tonic-gate 	if (c == yesorno[0])
494*0Sstevel@tonic-gate 		return (GOOD);
495*0Sstevel@tonic-gate 	return (FAIL);
496*0Sstevel@tonic-gate }
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate /*
499*0Sstevel@tonic-gate  * handle unexpected inconsistencies
500*0Sstevel@tonic-gate  */
501*0Sstevel@tonic-gate /*
502*0Sstevel@tonic-gate  * Note that a panic w/ EOF on the tty means all panics will return...
503*0Sstevel@tonic-gate  */
504*0Sstevel@tonic-gate #ifdef __STDC__
505*0Sstevel@tonic-gate #include <stdarg.h>
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate /* VARARGS1 */
508*0Sstevel@tonic-gate void
509*0Sstevel@tonic-gate panic(const char *msg, ...)
510*0Sstevel@tonic-gate {
511*0Sstevel@tonic-gate 	va_list	args;
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate 	va_start(args, msg);
514*0Sstevel@tonic-gate 	(void) vfprintf(stderr, msg, args);
515*0Sstevel@tonic-gate 	va_end(args);
516*0Sstevel@tonic-gate 	if (reply(gettext("abort")) == GOOD) {
517*0Sstevel@tonic-gate 		if (reply(gettext("dump core")) == GOOD)
518*0Sstevel@tonic-gate 			abort();
519*0Sstevel@tonic-gate 		done(1);
520*0Sstevel@tonic-gate 	}
521*0Sstevel@tonic-gate }
522*0Sstevel@tonic-gate #else
523*0Sstevel@tonic-gate #include <varargs.h>
524*0Sstevel@tonic-gate 
525*0Sstevel@tonic-gate /* VARARGS1 */
526*0Sstevel@tonic-gate void
527*0Sstevel@tonic-gate panic(va_alist)
528*0Sstevel@tonic-gate 	va_dcl
529*0Sstevel@tonic-gate {
530*0Sstevel@tonic-gate 	va_list	args;
531*0Sstevel@tonic-gate 	char	*msg;
532*0Sstevel@tonic-gate 
533*0Sstevel@tonic-gate 	va_start(args);
534*0Sstevel@tonic-gate 	msg = va_arg(args, char *);
535*0Sstevel@tonic-gate 	(void) vfprintf(stderr, msg, args);
536*0Sstevel@tonic-gate 	va_end(args);
537*0Sstevel@tonic-gate 	if (reply(gettext("abort")) == GOOD) {
538*0Sstevel@tonic-gate 		if (reply(gettext("dump core")) == GOOD)
539*0Sstevel@tonic-gate 			abort();
540*0Sstevel@tonic-gate 		done(1);
541*0Sstevel@tonic-gate 	}
542*0Sstevel@tonic-gate #endif
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate /*
545*0Sstevel@tonic-gate  * Locale-specific version of ctime
546*0Sstevel@tonic-gate  */
547*0Sstevel@tonic-gate char *
548*0Sstevel@tonic-gate lctime(tp)
549*0Sstevel@tonic-gate 	time_t	*tp;
550*0Sstevel@tonic-gate {
551*0Sstevel@tonic-gate 	static char buf[256];
552*0Sstevel@tonic-gate 	struct tm *tm;
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate 	tm = localtime(tp);
555*0Sstevel@tonic-gate 	(void) strftime(buf, sizeof (buf), "%c\n", tm);
556*0Sstevel@tonic-gate 	return (buf);
557*0Sstevel@tonic-gate }
558*0Sstevel@tonic-gate 
559*0Sstevel@tonic-gate static int
560*0Sstevel@tonic-gate statcmp(const struct stat *left, const struct stat *right)
561*0Sstevel@tonic-gate {
562*0Sstevel@tonic-gate 	int result = 1;
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate 	if ((left->st_dev == right->st_dev) &&
565*0Sstevel@tonic-gate 	    (left->st_ino == right->st_ino) &&
566*0Sstevel@tonic-gate 	    (left->st_mode == right->st_mode) &&
567*0Sstevel@tonic-gate 	    (left->st_nlink == right->st_nlink) &&
568*0Sstevel@tonic-gate 	    (left->st_uid == right->st_uid) &&
569*0Sstevel@tonic-gate 	    (left->st_gid == right->st_gid) &&
570*0Sstevel@tonic-gate 	    (left->st_rdev == right->st_rdev) &&
571*0Sstevel@tonic-gate 	    (left->st_ctim.tv_sec == right->st_ctim.tv_sec) &&
572*0Sstevel@tonic-gate 	    (left->st_ctim.tv_nsec == right->st_ctim.tv_nsec) &&
573*0Sstevel@tonic-gate 	    (left->st_mtim.tv_sec == right->st_mtim.tv_sec) &&
574*0Sstevel@tonic-gate 	    (left->st_mtim.tv_nsec == right->st_mtim.tv_nsec) &&
575*0Sstevel@tonic-gate 	    (left->st_blksize == right->st_blksize) &&
576*0Sstevel@tonic-gate 	    (left->st_blocks == right->st_blocks)) {
577*0Sstevel@tonic-gate 		result = 0;
578*0Sstevel@tonic-gate 	}
579*0Sstevel@tonic-gate 
580*0Sstevel@tonic-gate 	return (result);
581*0Sstevel@tonic-gate }
582*0Sstevel@tonic-gate 
583*0Sstevel@tonic-gate /*
584*0Sstevel@tonic-gate  * Safely open a file.
585*0Sstevel@tonic-gate  */
586*0Sstevel@tonic-gate int
587*0Sstevel@tonic-gate safe_open(int dfd, const char *filename, int mode, int perms)
588*0Sstevel@tonic-gate {
589*0Sstevel@tonic-gate 	static int init_syslog = 1;
590*0Sstevel@tonic-gate 	int fd;
591*0Sstevel@tonic-gate 	int working_mode;
592*0Sstevel@tonic-gate 	int saverr;
593*0Sstevel@tonic-gate 	char *errtext;
594*0Sstevel@tonic-gate 	struct stat pre_stat, pre_lstat;
595*0Sstevel@tonic-gate 	struct stat post_stat, post_lstat;
596*0Sstevel@tonic-gate 
597*0Sstevel@tonic-gate 	if (init_syslog) {
598*0Sstevel@tonic-gate 		openlog(progname, LOG_CONS, LOG_DAEMON);
599*0Sstevel@tonic-gate 		init_syslog = 0;
600*0Sstevel@tonic-gate 	}
601*0Sstevel@tonic-gate 
602*0Sstevel@tonic-gate 	/*
603*0Sstevel@tonic-gate 	 * Don't want to be spoofed into trashing something we
604*0Sstevel@tonic-gate 	 * shouldn't, thus the following rigamarole.  If it doesn't
605*0Sstevel@tonic-gate 	 * exist, we create it and proceed.  Otherwise, require that
606*0Sstevel@tonic-gate 	 * what's there be a real file with no extraneous links and
607*0Sstevel@tonic-gate 	 * owned by whoever ran us.
608*0Sstevel@tonic-gate 	 *
609*0Sstevel@tonic-gate 	 * The silliness with using both lstat() and fstat() is to avoid
610*0Sstevel@tonic-gate 	 * race-condition games with someone replacing the file with a
611*0Sstevel@tonic-gate 	 * symlink after we've opened it.  If there was an flstat(),
612*0Sstevel@tonic-gate 	 * we wouldn't need the fstat().
613*0Sstevel@tonic-gate 	 *
614*0Sstevel@tonic-gate 	 * The initial open with the hard-coded flags is ok even if we
615*0Sstevel@tonic-gate 	 * are intending to open only for reading.  If it succeeds,
616*0Sstevel@tonic-gate 	 * then the file did not exist, and we'll synthesize an appropriate
617*0Sstevel@tonic-gate 	 * complaint below.  Otherwise, it does exist, so we won't be
618*0Sstevel@tonic-gate 	 * truncating it with the open.
619*0Sstevel@tonic-gate 	 */
620*0Sstevel@tonic-gate 	if ((fd = openat(dfd, filename,
621*0Sstevel@tonic-gate 	    O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_LARGEFILE, perms)) < 0) {
622*0Sstevel@tonic-gate 		if (errno == EEXIST) {
623*0Sstevel@tonic-gate 			if (fstatat(dfd, filename, &pre_lstat,
624*0Sstevel@tonic-gate 						AT_SYMLINK_NOFOLLOW) < 0) {
625*0Sstevel@tonic-gate 				saverr = errno;
626*0Sstevel@tonic-gate 				(void) close(fd);
627*0Sstevel@tonic-gate 				errno = saverr;
628*0Sstevel@tonic-gate 				return (-1);
629*0Sstevel@tonic-gate 			}
630*0Sstevel@tonic-gate 
631*0Sstevel@tonic-gate 			if (fstatat(dfd, filename, &pre_stat, 0) < 0) {
632*0Sstevel@tonic-gate 				saverr = errno;
633*0Sstevel@tonic-gate 				(void) close(fd);
634*0Sstevel@tonic-gate 				errno = saverr;
635*0Sstevel@tonic-gate 				return (-1);
636*0Sstevel@tonic-gate 			}
637*0Sstevel@tonic-gate 
638*0Sstevel@tonic-gate 			working_mode = mode & (O_WRONLY|O_RDWR|O_RDONLY);
639*0Sstevel@tonic-gate 			working_mode |= O_LARGEFILE;
640*0Sstevel@tonic-gate 
641*0Sstevel@tonic-gate 			if ((fd = openat(dfd, filename, working_mode)) < 0) {
642*0Sstevel@tonic-gate 				if (errno == ENOENT) {
643*0Sstevel@tonic-gate 					errtext = gettext(
644*0Sstevel@tonic-gate "Unexpected condition detected: %s used to exist, but doesn't any longer\n");
645*0Sstevel@tonic-gate 					(void) fprintf(stderr, errtext,
646*0Sstevel@tonic-gate 					    filename);
647*0Sstevel@tonic-gate 					syslog(LOG_WARNING, errtext, filename);
648*0Sstevel@tonic-gate 					errno = ENOENT;
649*0Sstevel@tonic-gate 				}
650*0Sstevel@tonic-gate 				return (-1);
651*0Sstevel@tonic-gate 			}
652*0Sstevel@tonic-gate 
653*0Sstevel@tonic-gate 			if (fstatat(fd, NULL, &post_lstat,
654*0Sstevel@tonic-gate 						AT_SYMLINK_NOFOLLOW) < 0) {
655*0Sstevel@tonic-gate 				saverr = errno;
656*0Sstevel@tonic-gate 				(void) close(fd);
657*0Sstevel@tonic-gate 				errno = saverr;
658*0Sstevel@tonic-gate 				return (-1);
659*0Sstevel@tonic-gate 			}
660*0Sstevel@tonic-gate 
661*0Sstevel@tonic-gate 			if (fstatat(fd, NULL, &post_stat, 0) < 0) {
662*0Sstevel@tonic-gate 				saverr = errno;
663*0Sstevel@tonic-gate 				(void) close(fd);
664*0Sstevel@tonic-gate 				errno = saverr;
665*0Sstevel@tonic-gate 				return (-1);
666*0Sstevel@tonic-gate 			}
667*0Sstevel@tonic-gate 
668*0Sstevel@tonic-gate 			if (statcmp(&pre_lstat, &post_lstat) != 0) {
669*0Sstevel@tonic-gate 				errtext = gettext(
670*0Sstevel@tonic-gate "Unexpected condition detected: %s's lstat(2) information changed\n");
671*0Sstevel@tonic-gate 				(void) fprintf(stderr, errtext, filename);
672*0Sstevel@tonic-gate 				syslog(LOG_WARNING, errtext, filename);
673*0Sstevel@tonic-gate 				errno = EPERM;
674*0Sstevel@tonic-gate 				return (-1);
675*0Sstevel@tonic-gate 			}
676*0Sstevel@tonic-gate 
677*0Sstevel@tonic-gate 			if (statcmp(&pre_stat, &post_stat) != 0) {
678*0Sstevel@tonic-gate 				errtext = gettext(
679*0Sstevel@tonic-gate "Unexpected condition detected: %s's stat(2) information changed\n");
680*0Sstevel@tonic-gate 				(void) fprintf(stderr, errtext, filename);
681*0Sstevel@tonic-gate 				syslog(LOG_WARNING, errtext, filename);
682*0Sstevel@tonic-gate 				errno = EPERM;
683*0Sstevel@tonic-gate 				return (-1);
684*0Sstevel@tonic-gate 			}
685*0Sstevel@tonic-gate 
686*0Sstevel@tonic-gate 			/*
687*0Sstevel@tonic-gate 			 * If inode, device, or type are wrong, bail out.
688*0Sstevel@tonic-gate 			 */
689*0Sstevel@tonic-gate 			if ((!S_ISREG(post_lstat.st_mode) ||
690*0Sstevel@tonic-gate 			    (post_stat.st_ino != post_lstat.st_ino) ||
691*0Sstevel@tonic-gate 			    (post_stat.st_dev != post_lstat.st_dev))) {
692*0Sstevel@tonic-gate 				errtext = gettext(
693*0Sstevel@tonic-gate 	    "Unexpected condition detected: %s is not a regular file\n");
694*0Sstevel@tonic-gate 				(void) fprintf(stderr, errtext, filename);
695*0Sstevel@tonic-gate 				syslog(LOG_WARNING, errtext, filename);
696*0Sstevel@tonic-gate 				(void) close(fd);
697*0Sstevel@tonic-gate 				errno = EPERM;
698*0Sstevel@tonic-gate 				return (-1);
699*0Sstevel@tonic-gate 			}
700*0Sstevel@tonic-gate 
701*0Sstevel@tonic-gate 			/*
702*0Sstevel@tonic-gate 			 * Bad link count implies someone's linked our
703*0Sstevel@tonic-gate 			 * target to something else, which we probably
704*0Sstevel@tonic-gate 			 * shouldn't step on.
705*0Sstevel@tonic-gate 			 */
706*0Sstevel@tonic-gate 			if (post_lstat.st_nlink != 1) {
707*0Sstevel@tonic-gate 				errtext = gettext(
708*0Sstevel@tonic-gate 	    "Unexpected condition detected: %s must have exactly one link\n");
709*0Sstevel@tonic-gate 				(void) fprintf(stderr, errtext, filename);
710*0Sstevel@tonic-gate 				syslog(LOG_WARNING, errtext, filename);
711*0Sstevel@tonic-gate 				(void) close(fd);
712*0Sstevel@tonic-gate 				errno = EPERM;
713*0Sstevel@tonic-gate 				return (-1);
714*0Sstevel@tonic-gate 			}
715*0Sstevel@tonic-gate 			/*
716*0Sstevel@tonic-gate 			 * Root might make a file, but non-root might
717*0Sstevel@tonic-gate 			 * need to open it.  If the permissions let us
718*0Sstevel@tonic-gate 			 * get this far, then let it through.
719*0Sstevel@tonic-gate 			 */
720*0Sstevel@tonic-gate 			if (post_lstat.st_uid != getuid() &&
721*0Sstevel@tonic-gate 			    post_lstat.st_uid != 0) {
722*0Sstevel@tonic-gate 				errtext = gettext(
723*0Sstevel@tonic-gate "Unsupported condition detected: %s must be owned by uid %ld or 0\n");
724*0Sstevel@tonic-gate 				(void) fprintf(stderr, errtext, filename,
725*0Sstevel@tonic-gate 				    (long)getuid());
726*0Sstevel@tonic-gate 				syslog(LOG_WARNING, errtext, filename,
727*0Sstevel@tonic-gate 				    (long)getuid());
728*0Sstevel@tonic-gate 				(void) close(fd);
729*0Sstevel@tonic-gate 				errno = EPERM;
730*0Sstevel@tonic-gate 				return (-1);
731*0Sstevel@tonic-gate 			}
732*0Sstevel@tonic-gate 			if (mode & (O_WRONLY|O_TRUNC)) {
733*0Sstevel@tonic-gate 				if (ftruncate(fd, (off_t)0) < 0) {
734*0Sstevel@tonic-gate 					(void) fprintf(stderr,
735*0Sstevel@tonic-gate 					    "ftruncate(%s): %s\n",
736*0Sstevel@tonic-gate 					    filename, strerror(errno));
737*0Sstevel@tonic-gate 					(void) close(fd);
738*0Sstevel@tonic-gate 					return (-1);
739*0Sstevel@tonic-gate 				}
740*0Sstevel@tonic-gate 			}
741*0Sstevel@tonic-gate 		} else {
742*0Sstevel@tonic-gate 			/*
743*0Sstevel@tonic-gate 			 * Didn't exist, but couldn't open it.
744*0Sstevel@tonic-gate 			 */
745*0Sstevel@tonic-gate 			return (-1);
746*0Sstevel@tonic-gate 		}
747*0Sstevel@tonic-gate 	} else {
748*0Sstevel@tonic-gate 		/*
749*0Sstevel@tonic-gate 		 * If truncating open succeeded for a read-only open,
750*0Sstevel@tonic-gate 		 * bail out, as we really shouldn't have succeeded.
751*0Sstevel@tonic-gate 		 */
752*0Sstevel@tonic-gate 		if (mode & O_RDONLY) {
753*0Sstevel@tonic-gate 			/* Undo the O_CREAT */
754*0Sstevel@tonic-gate 			(void) unlinkat(dfd, filename, 0);
755*0Sstevel@tonic-gate 			(void) fprintf(stderr, "open(%s): %s\n",
756*0Sstevel@tonic-gate 			    filename, strerror(ENOENT));
757*0Sstevel@tonic-gate 			(void) close(fd);
758*0Sstevel@tonic-gate 			errno = ENOENT;
759*0Sstevel@tonic-gate 			return (-1);
760*0Sstevel@tonic-gate 		}
761*0Sstevel@tonic-gate 	}
762*0Sstevel@tonic-gate 
763*0Sstevel@tonic-gate 	return (fd);
764*0Sstevel@tonic-gate }
765*0Sstevel@tonic-gate 
766*0Sstevel@tonic-gate /*
767*0Sstevel@tonic-gate  * STDIO version of safe_open.  Equivalent to fopen64(...).
768*0Sstevel@tonic-gate  */
769*0Sstevel@tonic-gate FILE *
770*0Sstevel@tonic-gate safe_fopen(const char *filename, const char *smode, int perms)
771*0Sstevel@tonic-gate {
772*0Sstevel@tonic-gate 	int fd;
773*0Sstevel@tonic-gate 	int bmode;
774*0Sstevel@tonic-gate 
775*0Sstevel@tonic-gate 	/*
776*0Sstevel@tonic-gate 	 * accepts only modes  "r", "r+", and "w"
777*0Sstevel@tonic-gate 	 */
778*0Sstevel@tonic-gate 	if (smode[0] == 'r') {
779*0Sstevel@tonic-gate 		if (smode[1] == '\0') {
780*0Sstevel@tonic-gate 			bmode = O_RDONLY;
781*0Sstevel@tonic-gate 		} else if ((smode[1] == '+') && (smode[2] == '\0')) {
782*0Sstevel@tonic-gate 			bmode = O_RDWR;
783*0Sstevel@tonic-gate 		}
784*0Sstevel@tonic-gate 	} else if ((smode[0] == 'w') && (smode[1] == '\0')) {
785*0Sstevel@tonic-gate 		bmode = O_WRONLY;
786*0Sstevel@tonic-gate 	} else {
787*0Sstevel@tonic-gate 		(void) fprintf(stderr,
788*0Sstevel@tonic-gate 		    gettext("internal error: safe_fopen: invalid mode `%s'\n"),
789*0Sstevel@tonic-gate 		    smode);
790*0Sstevel@tonic-gate 		return (NULL);
791*0Sstevel@tonic-gate 	}
792*0Sstevel@tonic-gate 
793*0Sstevel@tonic-gate 	fd = safe_open(AT_FDCWD, filename, bmode, perms);
794*0Sstevel@tonic-gate 
795*0Sstevel@tonic-gate 	/*
796*0Sstevel@tonic-gate 	 * caller is expected to report error.
797*0Sstevel@tonic-gate 	 */
798*0Sstevel@tonic-gate 	if (fd >= 0)
799*0Sstevel@tonic-gate 		return (fdopen(fd, smode));
800*0Sstevel@tonic-gate 
801*0Sstevel@tonic-gate 	return ((FILE *)NULL);
802*0Sstevel@tonic-gate }
803*0Sstevel@tonic-gate 
804*0Sstevel@tonic-gate /*
805*0Sstevel@tonic-gate  * Read the contents of a directory.
806*0Sstevel@tonic-gate  */
807*0Sstevel@tonic-gate int
808*0Sstevel@tonic-gate mkentry(name, ino, ap)
809*0Sstevel@tonic-gate 	char *name;
810*0Sstevel@tonic-gate 	ino_t ino;
811*0Sstevel@tonic-gate 	struct arglist *ap;
812*0Sstevel@tonic-gate {
813*0Sstevel@tonic-gate 	struct afile *fp;
814*0Sstevel@tonic-gate 
815*0Sstevel@tonic-gate 	if (ap->base == NULL) {
816*0Sstevel@tonic-gate 		ap->nent = 20;
817*0Sstevel@tonic-gate 		ap->base = (struct afile *)calloc((unsigned)ap->nent,
818*0Sstevel@tonic-gate 			sizeof (*(ap->base)));
819*0Sstevel@tonic-gate 		if (ap->base == NULL) {
820*0Sstevel@tonic-gate 			(void) fprintf(stderr,
821*0Sstevel@tonic-gate 				gettext("%s: out of memory\n"), ap->cmd);
822*0Sstevel@tonic-gate 			return (FAIL);
823*0Sstevel@tonic-gate 		}
824*0Sstevel@tonic-gate 	}
825*0Sstevel@tonic-gate 	if (ap->head == NULL)
826*0Sstevel@tonic-gate 		ap->head = ap->last = ap->base;
827*0Sstevel@tonic-gate 	fp = ap->last;
828*0Sstevel@tonic-gate 	fp->fnum = ino;
829*0Sstevel@tonic-gate 	fp->fname = savename(name);
830*0Sstevel@tonic-gate 	fp++;
831*0Sstevel@tonic-gate 	if (fp == ap->head + ap->nent) {
832*0Sstevel@tonic-gate 		ap->base = (struct afile *)realloc((char *)ap->base,
833*0Sstevel@tonic-gate 		    (size_t)(2 * ap->nent * (size_t)sizeof (*(ap->base))));
834*0Sstevel@tonic-gate 		if (ap->base == NULL) {
835*0Sstevel@tonic-gate 			(void) fprintf(stderr,
836*0Sstevel@tonic-gate 				gettext("%s: out of memory\n"), ap->cmd);
837*0Sstevel@tonic-gate 			return (FAIL);
838*0Sstevel@tonic-gate 		}
839*0Sstevel@tonic-gate 		ap->head = ap->base;
840*0Sstevel@tonic-gate 		fp = ap->head + ap->nent;
841*0Sstevel@tonic-gate 		ap->nent *= 2;
842*0Sstevel@tonic-gate 	}
843*0Sstevel@tonic-gate 	ap->last = fp;
844*0Sstevel@tonic-gate 	return (GOOD);
845*0Sstevel@tonic-gate }
846*0Sstevel@tonic-gate 
847*0Sstevel@tonic-gate #ifdef __STDC__
848*0Sstevel@tonic-gate static int gmatch(wchar_t *, wchar_t *);
849*0Sstevel@tonic-gate static int addg(struct direct *, char *, char *, struct arglist *);
850*0Sstevel@tonic-gate #else
851*0Sstevel@tonic-gate static int gmatch();
852*0Sstevel@tonic-gate static int addg();
853*0Sstevel@tonic-gate #endif
854*0Sstevel@tonic-gate 
855*0Sstevel@tonic-gate /*
856*0Sstevel@tonic-gate  * XXX  This value is ASCII (but not language) dependent.  In
857*0Sstevel@tonic-gate  * ASCII, it is the DEL character (unlikely to appear in paths).
858*0Sstevel@tonic-gate  * If you are compiling on an EBCDIC-based machine, re-define
859*0Sstevel@tonic-gate  * this (0x7f is '"') to be something like 0x7 (DEL).  It's
860*0Sstevel@tonic-gate  * either this hack or re-write the expand() algorithm...
861*0Sstevel@tonic-gate  */
862*0Sstevel@tonic-gate #define	DELIMCHAR	((char)0x7f)
863*0Sstevel@tonic-gate 
864*0Sstevel@tonic-gate /*
865*0Sstevel@tonic-gate  * Expand a file name.
866*0Sstevel@tonic-gate  * "as" is the pattern to expand.
867*0Sstevel@tonic-gate  * "rflg" non-zero indicates that we're recursing.
868*0Sstevel@tonic-gate  * "ap" is where to put the results of the expansion.
869*0Sstevel@tonic-gate  *
870*0Sstevel@tonic-gate  * Our caller guarantees that "as" is at least the string ".".
871*0Sstevel@tonic-gate  */
872*0Sstevel@tonic-gate int
873*0Sstevel@tonic-gate expand(as, rflg, ap)
874*0Sstevel@tonic-gate 	char *as;
875*0Sstevel@tonic-gate 	int rflg;
876*0Sstevel@tonic-gate 	struct arglist *ap;
877*0Sstevel@tonic-gate {
878*0Sstevel@tonic-gate 	int		count, size;
879*0Sstevel@tonic-gate 	char		dir = 0;
880*0Sstevel@tonic-gate 	char		*rescan = 0;
881*0Sstevel@tonic-gate 	RST_DIR		*dirp;
882*0Sstevel@tonic-gate 	char		*s, *cs;
883*0Sstevel@tonic-gate 	int		sindex, rindexa, lindex;
884*0Sstevel@tonic-gate 	struct direct	*dp;
885*0Sstevel@tonic-gate 	char		slash;
886*0Sstevel@tonic-gate 	char		*rs;
887*0Sstevel@tonic-gate 	char		c;
888*0Sstevel@tonic-gate 	wchar_t 	w_fname[PATH_MAX+1];
889*0Sstevel@tonic-gate 	wchar_t		w_pname[PATH_MAX+1];
890*0Sstevel@tonic-gate 
891*0Sstevel@tonic-gate 	/*
892*0Sstevel@tonic-gate 	 * check for meta chars
893*0Sstevel@tonic-gate 	 */
894*0Sstevel@tonic-gate 	s = cs = as;
895*0Sstevel@tonic-gate 	slash = 0;
896*0Sstevel@tonic-gate 	while (*cs != '*' && *cs != '?' && *cs != '[') {
897*0Sstevel@tonic-gate 		if (*cs++ == 0) {
898*0Sstevel@tonic-gate 			if (rflg && slash)
899*0Sstevel@tonic-gate 				break;
900*0Sstevel@tonic-gate 			else
901*0Sstevel@tonic-gate 				return (0);
902*0Sstevel@tonic-gate 		} else if (*cs == '/') {
903*0Sstevel@tonic-gate 			slash++;
904*0Sstevel@tonic-gate 		}
905*0Sstevel@tonic-gate 	}
906*0Sstevel@tonic-gate 	for (;;) {
907*0Sstevel@tonic-gate 		if (cs == s) {
908*0Sstevel@tonic-gate 			s = "";
909*0Sstevel@tonic-gate 			break;
910*0Sstevel@tonic-gate 		} else if (*--cs == '/') {
911*0Sstevel@tonic-gate 			*cs = 0;
912*0Sstevel@tonic-gate 			if (s == cs)
913*0Sstevel@tonic-gate 				s = "/";
914*0Sstevel@tonic-gate 			break;
915*0Sstevel@tonic-gate 		}
916*0Sstevel@tonic-gate 	}
917*0Sstevel@tonic-gate 	if ((dirp = rst_opendir(s)) != NULL)
918*0Sstevel@tonic-gate 		dir++;
919*0Sstevel@tonic-gate 	count = 0;
920*0Sstevel@tonic-gate 	if (*cs == 0)
921*0Sstevel@tonic-gate 		*cs++ = DELIMCHAR;
922*0Sstevel@tonic-gate 	if (dir) {
923*0Sstevel@tonic-gate 		/*
924*0Sstevel@tonic-gate 		 * check for rescan
925*0Sstevel@tonic-gate 		 */
926*0Sstevel@tonic-gate 		rs = cs;
927*0Sstevel@tonic-gate 		do {
928*0Sstevel@tonic-gate 			if (*rs == '/') {
929*0Sstevel@tonic-gate 				rescan = rs;
930*0Sstevel@tonic-gate 				*rs = 0;
931*0Sstevel@tonic-gate 			}
932*0Sstevel@tonic-gate 		} while (*rs++);
933*0Sstevel@tonic-gate 		/* LINTED: result fits into an int */
934*0Sstevel@tonic-gate 		sindex = (int)(ap->last - ap->head);
935*0Sstevel@tonic-gate 		(void) mbstowcs(w_pname, cs, PATH_MAX);
936*0Sstevel@tonic-gate 		w_pname[PATH_MAX - 1] = 0;
937*0Sstevel@tonic-gate 		while ((dp = rst_readdir(dirp)) != NULL && dp->d_ino != 0) {
938*0Sstevel@tonic-gate 			if (!dflag && BIT(dp->d_ino, dumpmap) == 0)
939*0Sstevel@tonic-gate 				continue;
940*0Sstevel@tonic-gate 			if ((*dp->d_name == '.' && *cs != '.'))
941*0Sstevel@tonic-gate 				continue;
942*0Sstevel@tonic-gate 			(void) mbstowcs(w_fname, dp->d_name, PATH_MAX);
943*0Sstevel@tonic-gate 			w_fname[PATH_MAX - 1] = 0;
944*0Sstevel@tonic-gate 			if (gmatch(w_fname, w_pname)) {
945*0Sstevel@tonic-gate 				if (addg(dp, s, rescan, ap) < 0) {
946*0Sstevel@tonic-gate 					rst_closedir(dirp);
947*0Sstevel@tonic-gate 					return (-1);
948*0Sstevel@tonic-gate 				}
949*0Sstevel@tonic-gate 				count++;
950*0Sstevel@tonic-gate 			}
951*0Sstevel@tonic-gate 		}
952*0Sstevel@tonic-gate 		if (rescan) {
953*0Sstevel@tonic-gate 			rindexa = sindex;
954*0Sstevel@tonic-gate 			/* LINTED: result fits into an int */
955*0Sstevel@tonic-gate 			lindex = (int)(ap->last - ap->head);
956*0Sstevel@tonic-gate 			if (count) {
957*0Sstevel@tonic-gate 				count = 0;
958*0Sstevel@tonic-gate 				while (rindexa < lindex) {
959*0Sstevel@tonic-gate 					size = expand(ap->head[rindexa].fname,
960*0Sstevel@tonic-gate 					    1, ap);
961*0Sstevel@tonic-gate 					if (size < 0) {
962*0Sstevel@tonic-gate 						rst_closedir(dirp);
963*0Sstevel@tonic-gate 						return (size);
964*0Sstevel@tonic-gate 					}
965*0Sstevel@tonic-gate 					count += size;
966*0Sstevel@tonic-gate 					rindexa++;
967*0Sstevel@tonic-gate 				}
968*0Sstevel@tonic-gate 			}
969*0Sstevel@tonic-gate 			/* LINTED: lint is confused about pointer size/type */
970*0Sstevel@tonic-gate 			bcopy((void *)(&ap->head[lindex]),
971*0Sstevel@tonic-gate 			    (void *)(&ap->head[sindex]),
972*0Sstevel@tonic-gate 			    (size_t)((ap->last - &ap->head[rindexa])) *
973*0Sstevel@tonic-gate 			    sizeof (*ap->head));
974*0Sstevel@tonic-gate 			ap->last -= lindex - sindex;
975*0Sstevel@tonic-gate 			*rescan = '/';
976*0Sstevel@tonic-gate 		}
977*0Sstevel@tonic-gate 		rst_closedir(dirp);
978*0Sstevel@tonic-gate 	}
979*0Sstevel@tonic-gate 	s = as;
980*0Sstevel@tonic-gate 	while ((c = *s) != '\0')
981*0Sstevel@tonic-gate 		*s++ = (c != DELIMCHAR ? c : '/');
982*0Sstevel@tonic-gate 
983*0Sstevel@tonic-gate 	return (count);
984*0Sstevel@tonic-gate }
985*0Sstevel@tonic-gate 
986*0Sstevel@tonic-gate /*
987*0Sstevel@tonic-gate  * Check for a name match
988*0Sstevel@tonic-gate  */
989*0Sstevel@tonic-gate static int
990*0Sstevel@tonic-gate gmatch(s, p)
991*0Sstevel@tonic-gate 	wchar_t	*s;	/* string to test */
992*0Sstevel@tonic-gate 	wchar_t	*p;	/* pattern to match against */
993*0Sstevel@tonic-gate {
994*0Sstevel@tonic-gate 	long	scc;	/* source character to text */
995*0Sstevel@tonic-gate 	wchar_t	c;	/* pattern character to match */
996*0Sstevel@tonic-gate 	char	ok;	/* [x-y] range match status */
997*0Sstevel@tonic-gate 	long	lc;	/* left character of [x-y] range */
998*0Sstevel@tonic-gate 
999*0Sstevel@tonic-gate 	scc = *s++;
1000*0Sstevel@tonic-gate 	switch (c = *p++) {
1001*0Sstevel@tonic-gate 
1002*0Sstevel@tonic-gate 	case '[':
1003*0Sstevel@tonic-gate 		ok = 0;
1004*0Sstevel@tonic-gate 		lc = -1;
1005*0Sstevel@tonic-gate 		while (c = *p++) {
1006*0Sstevel@tonic-gate 			if (c == ']') {
1007*0Sstevel@tonic-gate 				return (ok ? gmatch(s, p) : 0);
1008*0Sstevel@tonic-gate 			} else if (c == '-') {
1009*0Sstevel@tonic-gate 				wchar_t rc = *p++;
1010*0Sstevel@tonic-gate 				/*
1011*0Sstevel@tonic-gate 				 * Check both ends must belong to
1012*0Sstevel@tonic-gate 				 * the same codeset.
1013*0Sstevel@tonic-gate 				 */
1014*0Sstevel@tonic-gate 				if (wcsetno(lc) != wcsetno(rc)) {
1015*0Sstevel@tonic-gate 					/*
1016*0Sstevel@tonic-gate 					 * If not, ignore the '-'
1017*0Sstevel@tonic-gate 					 * operator and [x-y] is
1018*0Sstevel@tonic-gate 					 * treated as if it were
1019*0Sstevel@tonic-gate 					 * [xy].
1020*0Sstevel@tonic-gate 					 */
1021*0Sstevel@tonic-gate 					if (scc == lc)
1022*0Sstevel@tonic-gate 						ok++;
1023*0Sstevel@tonic-gate 					if (scc == (lc = rc))
1024*0Sstevel@tonic-gate 						ok++;
1025*0Sstevel@tonic-gate 				} else if (lc <= scc && scc <= rc)
1026*0Sstevel@tonic-gate 					ok++;
1027*0Sstevel@tonic-gate 			} else {
1028*0Sstevel@tonic-gate 				lc = c;
1029*0Sstevel@tonic-gate 				if (scc == lc)
1030*0Sstevel@tonic-gate 					ok++;
1031*0Sstevel@tonic-gate 			}
1032*0Sstevel@tonic-gate 		}
1033*0Sstevel@tonic-gate 		/* No closing bracket => failure */
1034*0Sstevel@tonic-gate 		return (0);
1035*0Sstevel@tonic-gate 
1036*0Sstevel@tonic-gate 	default:
1037*0Sstevel@tonic-gate 		if (c != scc)
1038*0Sstevel@tonic-gate 			return (0);
1039*0Sstevel@tonic-gate 		/*FALLTHROUGH*/
1040*0Sstevel@tonic-gate 
1041*0Sstevel@tonic-gate 	case '?':
1042*0Sstevel@tonic-gate 		return (scc ? gmatch(s, p) : 0);
1043*0Sstevel@tonic-gate 
1044*0Sstevel@tonic-gate 	case '*':
1045*0Sstevel@tonic-gate 		if (*p == 0)
1046*0Sstevel@tonic-gate 			return (1);
1047*0Sstevel@tonic-gate 		s--;
1048*0Sstevel@tonic-gate 		while (*s) {
1049*0Sstevel@tonic-gate 			if (gmatch(s++, p))
1050*0Sstevel@tonic-gate 				return (1);
1051*0Sstevel@tonic-gate 		}
1052*0Sstevel@tonic-gate 		return (0);
1053*0Sstevel@tonic-gate 
1054*0Sstevel@tonic-gate 	case 0:
1055*0Sstevel@tonic-gate 		return (scc == 0);
1056*0Sstevel@tonic-gate 	}
1057*0Sstevel@tonic-gate }
1058*0Sstevel@tonic-gate 
1059*0Sstevel@tonic-gate /*
1060*0Sstevel@tonic-gate  * Construct a matched name.
1061*0Sstevel@tonic-gate  */
1062*0Sstevel@tonic-gate static int
1063*0Sstevel@tonic-gate addg(dp, as1, as3, ap)
1064*0Sstevel@tonic-gate 	struct direct	*dp;	/* The directory containing the name */
1065*0Sstevel@tonic-gate 	char		*as1;	/* The current directory */
1066*0Sstevel@tonic-gate 	char		*as3;	/* The file name in dp */
1067*0Sstevel@tonic-gate 	struct arglist	*ap;	/* Where to append the new name */
1068*0Sstevel@tonic-gate {
1069*0Sstevel@tonic-gate 	char	*s1, *s2, *limit;
1070*0Sstevel@tonic-gate 	int	c;
1071*0Sstevel@tonic-gate 	char	buf[MAXPATHLEN + 1];
1072*0Sstevel@tonic-gate 
1073*0Sstevel@tonic-gate 	s2 = buf;
1074*0Sstevel@tonic-gate 	limit = buf + sizeof (buf) - 1;
1075*0Sstevel@tonic-gate 	s1 = as1;
1076*0Sstevel@tonic-gate 	while ((c = *s1++) != '\0' && s2 < limit) {
1077*0Sstevel@tonic-gate 		if (c == DELIMCHAR) {
1078*0Sstevel@tonic-gate 			*s2++ = '/';
1079*0Sstevel@tonic-gate 			break;
1080*0Sstevel@tonic-gate 		}
1081*0Sstevel@tonic-gate 		/* LINTED narrowing cast */
1082*0Sstevel@tonic-gate 		*s2++ = (char)c;
1083*0Sstevel@tonic-gate 	}
1084*0Sstevel@tonic-gate 	s1 = dp->d_name;
1085*0Sstevel@tonic-gate 	while ((*s2 = *s1++) != '\0' && s2 < limit)
1086*0Sstevel@tonic-gate 		s2++;
1087*0Sstevel@tonic-gate 	s1 = as3;
1088*0Sstevel@tonic-gate 	if (s1 != NULL && s2 < limit) {
1089*0Sstevel@tonic-gate 		*s2++ = '/';
1090*0Sstevel@tonic-gate 
1091*0Sstevel@tonic-gate 		while ((*s2++ = *++s1) != '\0' && s2 < limit) {
1092*0Sstevel@tonic-gate 			continue;
1093*0Sstevel@tonic-gate 			/*LINTED [empty loop body]*/
1094*0Sstevel@tonic-gate 		}
1095*0Sstevel@tonic-gate 	}
1096*0Sstevel@tonic-gate 	*s2 = '\0';
1097*0Sstevel@tonic-gate 	if (mkentry(buf, dp->d_ino, ap) == FAIL)
1098*0Sstevel@tonic-gate 		return (-1);
1099*0Sstevel@tonic-gate 	return (0);
1100*0Sstevel@tonic-gate }
1101*0Sstevel@tonic-gate 
1102*0Sstevel@tonic-gate 
1103*0Sstevel@tonic-gate /*
1104*0Sstevel@tonic-gate  * Resolve a "complex" pathname (as generated by myname()) into
1105*0Sstevel@tonic-gate  * a file descriptor and a relative path.  The file descriptor
1106*0Sstevel@tonic-gate  * will reference the hidden directory containing the attribute
1107*0Sstevel@tonic-gate  * named by the relative path.  If the provided path is not
1108*0Sstevel@tonic-gate  * complex, the returned file descriptor will be AT_FDCWD and rpath
1109*0Sstevel@tonic-gate  * will equal path.
1110*0Sstevel@tonic-gate  *
1111*0Sstevel@tonic-gate  * This function is intended to be used to transform a complex
1112*0Sstevel@tonic-gate  * pathname into a pair of handles that can be used to actually
1113*0Sstevel@tonic-gate  * manipulate the named file.  Since extended attributes have
1114*0Sstevel@tonic-gate  * an independant name space, a file descriptor for a directory
1115*0Sstevel@tonic-gate  * in the attribute name space is necessary to actually manipulate
1116*0Sstevel@tonic-gate  * the attribute file (via the path-relative xxxat() system calls
1117*0Sstevel@tonic-gate  * or a call to fchdir()).
1118*0Sstevel@tonic-gate  *
1119*0Sstevel@tonic-gate  * In the event of an error, the returned file descriptor will be
1120*0Sstevel@tonic-gate  * -1.  It is expected that callers will either check for this
1121*0Sstevel@tonic-gate  * condition directly, or attempt to use the descriptor, fail, and
1122*0Sstevel@tonic-gate  * generate an appropriate context-specific error message.
1123*0Sstevel@tonic-gate  *
1124*0Sstevel@tonic-gate  * This function is pretty much a no-op for "simple" (non-attribute)
1125*0Sstevel@tonic-gate  * paths.
1126*0Sstevel@tonic-gate  */
1127*0Sstevel@tonic-gate void
1128*0Sstevel@tonic-gate resolve(path, fd, rpath)
1129*0Sstevel@tonic-gate 	char	*path;
1130*0Sstevel@tonic-gate 	int	*fd;
1131*0Sstevel@tonic-gate 	char	**rpath;
1132*0Sstevel@tonic-gate {
1133*0Sstevel@tonic-gate 	int	tfd;
1134*0Sstevel@tonic-gate 
1135*0Sstevel@tonic-gate 	*fd = tfd = AT_FDCWD;
1136*0Sstevel@tonic-gate 	*rpath = path;
1137*0Sstevel@tonic-gate 	path = *rpath + strlen(*rpath) +1;
1138*0Sstevel@tonic-gate 	while (*path != '\0' &&
1139*0Sstevel@tonic-gate 		(*fd = openat64(tfd, *rpath, O_RDONLY)) > 0) {
1140*0Sstevel@tonic-gate 		if (tfd != AT_FDCWD) (void) close(tfd);
1141*0Sstevel@tonic-gate 		tfd = *fd;
1142*0Sstevel@tonic-gate 		*rpath = path;
1143*0Sstevel@tonic-gate 		path = *rpath + strlen(*rpath) +1;
1144*0Sstevel@tonic-gate 	}
1145*0Sstevel@tonic-gate 	if (*fd == AT_FDCWD)
1146*0Sstevel@tonic-gate 		return;
1147*0Sstevel@tonic-gate 	if (*fd < 0 || (*fd = openat64(tfd, ".", O_RDONLY|O_XATTR)) < 0) {
1148*0Sstevel@tonic-gate 		int saverr = errno;
1149*0Sstevel@tonic-gate 		(void) fprintf(stderr,
1150*0Sstevel@tonic-gate 			gettext("Warning: cannot fully resolve %s: %s"),
1151*0Sstevel@tonic-gate 			path, strerror(saverr));
1152*0Sstevel@tonic-gate 		(void) fflush(stderr);
1153*0Sstevel@tonic-gate 	}
1154*0Sstevel@tonic-gate 	if (tfd != AT_FDCWD) (void) close(tfd);
1155*0Sstevel@tonic-gate }
1156*0Sstevel@tonic-gate 
1157*0Sstevel@tonic-gate /*
1158*0Sstevel@tonic-gate  * Copy a complex pathname to another string.  Note that the
1159*0Sstevel@tonic-gate  * length returned by this function is the number of characters
1160*0Sstevel@tonic-gate  * up to (but not including) the final NULL.
1161*0Sstevel@tonic-gate  */
1162*0Sstevel@tonic-gate int
1163*0Sstevel@tonic-gate complexcpy(s1, s2, max)
1164*0Sstevel@tonic-gate 	char	*s1;
1165*0Sstevel@tonic-gate 	char	*s2;
1166*0Sstevel@tonic-gate 	int	max;
1167*0Sstevel@tonic-gate {
1168*0Sstevel@tonic-gate 	int	nullseen = 0;
1169*0Sstevel@tonic-gate 	int	len = 0;
1170*0Sstevel@tonic-gate 
1171*0Sstevel@tonic-gate 	while (len++ < max) {
1172*0Sstevel@tonic-gate 		*s1++ = *s2;
1173*0Sstevel@tonic-gate 		if (*s2++ == '\0') {
1174*0Sstevel@tonic-gate 			if (nullseen)
1175*0Sstevel@tonic-gate 				return (len-1);
1176*0Sstevel@tonic-gate 			else
1177*0Sstevel@tonic-gate 				nullseen = 1;
1178*0Sstevel@tonic-gate 		} else {
1179*0Sstevel@tonic-gate 			nullseen = 0;
1180*0Sstevel@tonic-gate 		}
1181*0Sstevel@tonic-gate 	}
1182*0Sstevel@tonic-gate 	*s1 = '\0';
1183*0Sstevel@tonic-gate 	if (nullseen == 0)
1184*0Sstevel@tonic-gate 		*--s1 = '\0';
1185*0Sstevel@tonic-gate 	fprintf(stderr,
1186*0Sstevel@tonic-gate 		gettext("Warning: unterminated source string in complexcpy\n"));
1187*0Sstevel@tonic-gate 	return (max-1);
1188*0Sstevel@tonic-gate }
1189