17c478bd9Sstevel@tonic-gate /*
2*fe0e7ec4Smaheshvs * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
37c478bd9Sstevel@tonic-gate * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate */
57c478bd9Sstevel@tonic-gate
67c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
77c478bd9Sstevel@tonic-gate /* All Rights Reserved */
87c478bd9Sstevel@tonic-gate
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate * Copyright (c) 1983 Regents of the University of California.
117c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate */
147c478bd9Sstevel@tonic-gate
157c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
167c478bd9Sstevel@tonic-gate
177c478bd9Sstevel@tonic-gate #include "restore.h"
187c478bd9Sstevel@tonic-gate #include <ctype.h>
197c478bd9Sstevel@tonic-gate #include <errno.h>
207c478bd9Sstevel@tonic-gate #include <syslog.h>
217c478bd9Sstevel@tonic-gate #include <limits.h>
227c478bd9Sstevel@tonic-gate /* LINTED: this file really is necessary */
237c478bd9Sstevel@tonic-gate #include <euc.h>
247c478bd9Sstevel@tonic-gate #include <widec.h>
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate * Insure that all the components of a pathname exist. Note that
287c478bd9Sstevel@tonic-gate * lookupname() and addentry() both expect complex names as
297c478bd9Sstevel@tonic-gate * input arguments, so a double NULL needs to be added to each name.
307c478bd9Sstevel@tonic-gate */
317c478bd9Sstevel@tonic-gate void
pathcheck(char * name)32*fe0e7ec4Smaheshvs pathcheck(char *name)
337c478bd9Sstevel@tonic-gate {
347c478bd9Sstevel@tonic-gate char *cp, save;
357c478bd9Sstevel@tonic-gate struct entry *ep;
367c478bd9Sstevel@tonic-gate char *start;
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate start = strchr(name, '/');
397c478bd9Sstevel@tonic-gate if (start == 0)
407c478bd9Sstevel@tonic-gate return;
417c478bd9Sstevel@tonic-gate for (cp = start; *cp != '\0'; cp++) {
427c478bd9Sstevel@tonic-gate if (*cp != '/')
437c478bd9Sstevel@tonic-gate continue;
447c478bd9Sstevel@tonic-gate *cp = '\0';
457c478bd9Sstevel@tonic-gate save = *(cp+1);
467c478bd9Sstevel@tonic-gate *(cp+1) = '\0';
477c478bd9Sstevel@tonic-gate ep = lookupname(name);
487c478bd9Sstevel@tonic-gate if (ep == NIL) {
497c478bd9Sstevel@tonic-gate ep = addentry(name, psearch(name), NODE);
507c478bd9Sstevel@tonic-gate newnode(ep);
517c478bd9Sstevel@tonic-gate }
527c478bd9Sstevel@tonic-gate /* LINTED: result fits in a short */
537c478bd9Sstevel@tonic-gate ep->e_flags |= NEW|KEEP;
547c478bd9Sstevel@tonic-gate *cp = '/';
557c478bd9Sstevel@tonic-gate *(cp+1) = save;
567c478bd9Sstevel@tonic-gate }
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate * Change a name to a unique temporary name.
617c478bd9Sstevel@tonic-gate */
627c478bd9Sstevel@tonic-gate void
mktempname(struct entry * ep)63*fe0e7ec4Smaheshvs mktempname(struct entry *ep)
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate char *newname;
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate if (ep->e_flags & TMPNAME)
687c478bd9Sstevel@tonic-gate badentry(ep, gettext("mktempname: called with TMPNAME"));
697c478bd9Sstevel@tonic-gate /* LINTED: result fits in a short */
707c478bd9Sstevel@tonic-gate ep->e_flags |= TMPNAME;
717c478bd9Sstevel@tonic-gate newname = savename(gentempname(ep));
727c478bd9Sstevel@tonic-gate renameit(myname(ep), newname);
737c478bd9Sstevel@tonic-gate freename(ep->e_name);
747c478bd9Sstevel@tonic-gate ep->e_name = newname;
757c478bd9Sstevel@tonic-gate /* LINTED: savename guarantees strlen will fit */
767c478bd9Sstevel@tonic-gate ep->e_namlen = strlen(ep->e_name);
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate * Generate a temporary name for an entry.
817c478bd9Sstevel@tonic-gate */
827c478bd9Sstevel@tonic-gate char *
gentempname(struct entry * ep)83*fe0e7ec4Smaheshvs gentempname(struct entry *ep)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate static char name[MAXPATHLEN];
867c478bd9Sstevel@tonic-gate struct entry *np;
877c478bd9Sstevel@tonic-gate long i = 0;
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate for (np = lookupino(ep->e_ino); np != NIL && np != ep; np = np->e_links)
907c478bd9Sstevel@tonic-gate i++;
917c478bd9Sstevel@tonic-gate if (np == NIL)
927c478bd9Sstevel@tonic-gate badentry(ep, gettext("not on ino list"));
937c478bd9Sstevel@tonic-gate (void) snprintf(name, sizeof (name), "%s%ld%lu", TMPHDR, i, ep->e_ino);
947c478bd9Sstevel@tonic-gate return (name);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate * Rename a file or directory.
997c478bd9Sstevel@tonic-gate */
1007c478bd9Sstevel@tonic-gate void
renameit(char * fp,char * tp)101*fe0e7ec4Smaheshvs renameit(char *fp, char *tp)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate int fromfd, tofd;
1047c478bd9Sstevel@tonic-gate char *from, *to;
1057c478bd9Sstevel@tonic-gate char tobuf[MAXPATHLEN];
1067c478bd9Sstevel@tonic-gate char *pathend;
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate resolve(fp, &fromfd, &from);
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate * The to pointer argument is assumed to be either a fully
1117c478bd9Sstevel@tonic-gate * specified path (starting with "./") or a simple temporary
1127c478bd9Sstevel@tonic-gate * file name (starting with TMPHDR). If passed a simple temp
1137c478bd9Sstevel@tonic-gate * file name, we need to set up the descriptors explicitly.
1147c478bd9Sstevel@tonic-gate */
1157c478bd9Sstevel@tonic-gate if (strncmp(tp, TMPHDR, sizeof (TMPHDR) - 1) == 0) {
1167c478bd9Sstevel@tonic-gate tofd = fromfd;
1177c478bd9Sstevel@tonic-gate if ((pathend = strrchr(from, '/')) != NULL) {
1187c478bd9Sstevel@tonic-gate strncpy(tobuf, from, pathend - from + 1);
1197c478bd9Sstevel@tonic-gate tobuf[pathend - from + 1] = NULL;
1207c478bd9Sstevel@tonic-gate strlcat(tobuf, tp, sizeof (tobuf));
1217c478bd9Sstevel@tonic-gate to = tobuf;
1227c478bd9Sstevel@tonic-gate } else {
1237c478bd9Sstevel@tonic-gate to = tp;
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate } else
1267c478bd9Sstevel@tonic-gate resolve(tp, &tofd, &to);
1277c478bd9Sstevel@tonic-gate if (renameat(fromfd, from, tofd, to) < 0) {
1287c478bd9Sstevel@tonic-gate int saverr = errno;
1297c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1307c478bd9Sstevel@tonic-gate gettext("Warning: cannot rename %s to %s: %s\n"),
1317c478bd9Sstevel@tonic-gate from, to, strerror(saverr));
1327c478bd9Sstevel@tonic-gate (void) fflush(stderr);
1337c478bd9Sstevel@tonic-gate } else {
1347c478bd9Sstevel@tonic-gate vprintf(stdout, gettext("rename %s to %s\n"), from, to);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate if (fromfd != AT_FDCWD) (void) close(fromfd);
1377c478bd9Sstevel@tonic-gate if (tofd != AT_FDCWD) (void) close(tofd);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate * Create a new node (directory). Note that, because we have no
1427c478bd9Sstevel@tonic-gate * mkdirat() function, fchdir() must be used set up the appropriate
1437c478bd9Sstevel@tonic-gate * name space context prior to the call to mkdir() if we are
1447c478bd9Sstevel@tonic-gate * operating in attribute space.
1457c478bd9Sstevel@tonic-gate */
1467c478bd9Sstevel@tonic-gate void
newnode(struct entry * np)147*fe0e7ec4Smaheshvs newnode(struct entry *np)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate char *cp;
1507c478bd9Sstevel@tonic-gate int dfd;
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate if (np->e_type != NODE)
1537c478bd9Sstevel@tonic-gate badentry(np, gettext("newnode: not a node"));
1547c478bd9Sstevel@tonic-gate resolve(myname(np), &dfd, &cp);
1557c478bd9Sstevel@tonic-gate if (dfd != AT_FDCWD) {
1567c478bd9Sstevel@tonic-gate if (fchdir(dfd) < 0) {
1577c478bd9Sstevel@tonic-gate int saverr = errno;
1587c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1597c478bd9Sstevel@tonic-gate gettext("Warning: cannot create %s: %s"),
1607c478bd9Sstevel@tonic-gate cp, strerror(saverr));
1617c478bd9Sstevel@tonic-gate (void) fflush(stderr);
1627c478bd9Sstevel@tonic-gate (void) close(dfd);
1637c478bd9Sstevel@tonic-gate return;
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate if (mkdir(cp, 0777) < 0) {
1677c478bd9Sstevel@tonic-gate int saverr = errno;
1687c478bd9Sstevel@tonic-gate /* LINTED: result fits in a short */
1697c478bd9Sstevel@tonic-gate np->e_flags |= EXISTED;
1707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Warning: "));
1717c478bd9Sstevel@tonic-gate (void) fflush(stderr);
1727c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s\n", cp, strerror(saverr));
1737c478bd9Sstevel@tonic-gate } else {
1747c478bd9Sstevel@tonic-gate vprintf(stdout, gettext("Make node %s\n"), cp);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate if (dfd != AT_FDCWD) {
1777c478bd9Sstevel@tonic-gate fchdir(savepwd);
1787c478bd9Sstevel@tonic-gate (void) close(dfd);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate /*
1837c478bd9Sstevel@tonic-gate * Remove an old node (directory). See comment above on newnode()
1847c478bd9Sstevel@tonic-gate * for explanation of fchdir() use below.
1857c478bd9Sstevel@tonic-gate */
1867c478bd9Sstevel@tonic-gate void
removenode(struct entry * ep)187*fe0e7ec4Smaheshvs removenode(struct entry *ep)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate char *cp;
1907c478bd9Sstevel@tonic-gate int dfd;
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate if (ep->e_type != NODE)
1937c478bd9Sstevel@tonic-gate badentry(ep, gettext("removenode: not a node"));
1947c478bd9Sstevel@tonic-gate if (ep->e_entries != NIL)
1957c478bd9Sstevel@tonic-gate badentry(ep, gettext("removenode: non-empty directory"));
1967c478bd9Sstevel@tonic-gate /* LINTED: result fits in a short */
1977c478bd9Sstevel@tonic-gate ep->e_flags |= REMOVED;
1987c478bd9Sstevel@tonic-gate /* LINTED: result fits in a short */
1997c478bd9Sstevel@tonic-gate ep->e_flags &= ~TMPNAME;
2007c478bd9Sstevel@tonic-gate resolve(myname(ep), &dfd, &cp);
2017c478bd9Sstevel@tonic-gate if (dfd != AT_FDCWD) {
2027c478bd9Sstevel@tonic-gate if (fchdir(dfd) < 0) {
2037c478bd9Sstevel@tonic-gate int saverr = errno;
2047c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2057c478bd9Sstevel@tonic-gate gettext("Warning: cannot remove %s: %s"),
2067c478bd9Sstevel@tonic-gate cp, strerror(saverr));
2077c478bd9Sstevel@tonic-gate (void) fflush(stderr);
2087c478bd9Sstevel@tonic-gate (void) close(dfd);
2097c478bd9Sstevel@tonic-gate return;
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate if (rmdir(cp) < 0) { /* NOTE: could use unlinkat (..,REMOVEDIR) */
2137c478bd9Sstevel@tonic-gate int saverr = errno;
2147c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Warning: %s: %s\n"),
2157c478bd9Sstevel@tonic-gate cp, strerror(saverr));
2167c478bd9Sstevel@tonic-gate (void) fflush(stderr);
2177c478bd9Sstevel@tonic-gate } else {
2187c478bd9Sstevel@tonic-gate vprintf(stdout, gettext("Remove node %s\n"), cp);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate if (dfd != AT_FDCWD) {
2217c478bd9Sstevel@tonic-gate (void) fchdir(savepwd);
2227c478bd9Sstevel@tonic-gate (void) close(dfd);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate * Remove a leaf.
2287c478bd9Sstevel@tonic-gate */
2297c478bd9Sstevel@tonic-gate void
removeleaf(struct entry * ep)230*fe0e7ec4Smaheshvs removeleaf(struct entry *ep)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate char *cp;
2337c478bd9Sstevel@tonic-gate int dfd;
2347c478bd9Sstevel@tonic-gate
2357c478bd9Sstevel@tonic-gate if (ep->e_type != LEAF)
2367c478bd9Sstevel@tonic-gate badentry(ep, gettext("removeleaf: not a leaf"));
2377c478bd9Sstevel@tonic-gate /* LINTED: result fits in a short */
2387c478bd9Sstevel@tonic-gate ep->e_flags |= REMOVED;
2397c478bd9Sstevel@tonic-gate /* LINTED: result fits in a short */
2407c478bd9Sstevel@tonic-gate ep->e_flags &= ~TMPNAME;
2417c478bd9Sstevel@tonic-gate resolve(myname(ep), &dfd, &cp);
2427c478bd9Sstevel@tonic-gate if (unlinkat(dfd, cp, 0) < 0) {
2437c478bd9Sstevel@tonic-gate int saverr = errno;
2447c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Warning: %s: %s\n"),
2457c478bd9Sstevel@tonic-gate cp, strerror(saverr));
2467c478bd9Sstevel@tonic-gate (void) fflush(stderr);
2477c478bd9Sstevel@tonic-gate } else {
2487c478bd9Sstevel@tonic-gate vprintf(stdout, gettext("Remove leaf %s\n"), cp);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate if (dfd != AT_FDCWD)
2517c478bd9Sstevel@tonic-gate (void) close(dfd);
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate /*
2557c478bd9Sstevel@tonic-gate * Create a link.
2567c478bd9Sstevel@tonic-gate * This function assumes that the context has already been set
2577c478bd9Sstevel@tonic-gate * for the link file to be created (i.e., we have "fchdir-ed"
2587c478bd9Sstevel@tonic-gate * into attribute space already if this is an attribute link).
2597c478bd9Sstevel@tonic-gate */
260*fe0e7ec4Smaheshvs int
lf_linkit(char * existing,char * new,int type)261*fe0e7ec4Smaheshvs lf_linkit(char *existing, char *new, int type)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate char linkbuf[MAXPATHLEN];
2647c478bd9Sstevel@tonic-gate struct stat64 s1[1], s2[1];
2657c478bd9Sstevel@tonic-gate char *name;
2667c478bd9Sstevel@tonic-gate int dfd, l, result;
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate resolve(existing, &dfd, &name);
2697c478bd9Sstevel@tonic-gate if (dfd == -1) {
2707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
2717c478bd9Sstevel@tonic-gate "Warning: cannot restore %s link %s->%s\n"),
2727c478bd9Sstevel@tonic-gate (type == SYMLINK ? "symbolic" : "hard"), new, existing);
2737c478bd9Sstevel@tonic-gate result = FAIL;
2747c478bd9Sstevel@tonic-gate goto out;
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate if (type == SYMLINK) {
2777c478bd9Sstevel@tonic-gate if (symlink(name, new) < 0) {
2787c478bd9Sstevel@tonic-gate /* No trailing \0 from readlink(2) */
2797c478bd9Sstevel@tonic-gate if (((l = readlink(new, linkbuf, sizeof (linkbuf)))
2807c478bd9Sstevel@tonic-gate > 0) &&
2817c478bd9Sstevel@tonic-gate (l == strlen(name)) &&
2827c478bd9Sstevel@tonic-gate (strncmp(linkbuf, name, l) == 0)) {
2837c478bd9Sstevel@tonic-gate vprintf(stdout,
2847c478bd9Sstevel@tonic-gate gettext("Symbolic link %s->%s ok\n"),
2857c478bd9Sstevel@tonic-gate new, name);
2867c478bd9Sstevel@tonic-gate result = GOOD;
2877c478bd9Sstevel@tonic-gate goto out;
2887c478bd9Sstevel@tonic-gate } else {
2897c478bd9Sstevel@tonic-gate int saverr = errno;
2907c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
2917c478bd9Sstevel@tonic-gate "Warning: cannot create symbolic link %s->%s: %s"),
2927c478bd9Sstevel@tonic-gate new, name, strerror(saverr));
2937c478bd9Sstevel@tonic-gate (void) fflush(stderr);
2947c478bd9Sstevel@tonic-gate result = FAIL;
2957c478bd9Sstevel@tonic-gate goto out;
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate } else if (type == HARDLINK) {
2997c478bd9Sstevel@tonic-gate if (link(name, new) < 0) {
3007c478bd9Sstevel@tonic-gate int saverr = errno;
3017c478bd9Sstevel@tonic-gate if ((stat64(name, s1) == 0) &&
3027c478bd9Sstevel@tonic-gate (stat64(new, s2) == 0) &&
3037c478bd9Sstevel@tonic-gate (s1->st_dev == s2->st_dev) &&
3047c478bd9Sstevel@tonic-gate (s1->st_ino == s2->st_ino)) {
3057c478bd9Sstevel@tonic-gate vprintf(stdout,
3067c478bd9Sstevel@tonic-gate gettext("Hard link %s->%s ok\n"),
3077c478bd9Sstevel@tonic-gate new, name);
3087c478bd9Sstevel@tonic-gate result = GOOD;
3097c478bd9Sstevel@tonic-gate goto out;
3107c478bd9Sstevel@tonic-gate } else {
3117c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
3127c478bd9Sstevel@tonic-gate "Warning: cannot create hard link %s->%s: %s\n"),
3137c478bd9Sstevel@tonic-gate new, name, strerror(saverr));
3147c478bd9Sstevel@tonic-gate (void) fflush(stderr);
3157c478bd9Sstevel@tonic-gate result = FAIL;
3167c478bd9Sstevel@tonic-gate goto out;
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate } else {
3207c478bd9Sstevel@tonic-gate panic(gettext("%s: unknown type %d\n"), "linkit", type);
3217c478bd9Sstevel@tonic-gate result = FAIL;
3227c478bd9Sstevel@tonic-gate goto out;
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate result = GOOD;
3257c478bd9Sstevel@tonic-gate if (type == SYMLINK)
3267c478bd9Sstevel@tonic-gate vprintf(stdout, gettext("Create symbolic link %s->%s\n"),
3277c478bd9Sstevel@tonic-gate new, name);
3287c478bd9Sstevel@tonic-gate else
3297c478bd9Sstevel@tonic-gate vprintf(stdout, gettext("Create hard link %s->%s\n"),
3307c478bd9Sstevel@tonic-gate new, name);
3317c478bd9Sstevel@tonic-gate out:
3327c478bd9Sstevel@tonic-gate if (dfd != AT_FDCWD) {
3337c478bd9Sstevel@tonic-gate (void) close(dfd);
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate return (result);
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate
3387c478bd9Sstevel@tonic-gate /*
3397c478bd9Sstevel@tonic-gate * Find lowest-numbered inode (above "start") that needs to be extracted.
3407c478bd9Sstevel@tonic-gate * Caller knows that a return value of maxino means there's nothing left.
3417c478bd9Sstevel@tonic-gate */
3427c478bd9Sstevel@tonic-gate ino_t
lowerbnd(ino_t start)343*fe0e7ec4Smaheshvs lowerbnd(ino_t start)
3447c478bd9Sstevel@tonic-gate {
3457c478bd9Sstevel@tonic-gate struct entry *ep;
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate for (; start < maxino; start++) {
3487c478bd9Sstevel@tonic-gate ep = lookupino(start);
3497c478bd9Sstevel@tonic-gate if (ep == NIL || ep->e_type == NODE)
3507c478bd9Sstevel@tonic-gate continue;
3517c478bd9Sstevel@tonic-gate if (ep->e_flags & (NEW|EXTRACT))
3527c478bd9Sstevel@tonic-gate return (start);
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate return (start);
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate /*
3587c478bd9Sstevel@tonic-gate * Find highest-numbered inode (below "start") that needs to be extracted.
3597c478bd9Sstevel@tonic-gate */
3607c478bd9Sstevel@tonic-gate ino_t
upperbnd(ino_t start)361*fe0e7ec4Smaheshvs upperbnd(ino_t start)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate struct entry *ep;
3647c478bd9Sstevel@tonic-gate
3657c478bd9Sstevel@tonic-gate for (; start > ROOTINO; start--) {
3667c478bd9Sstevel@tonic-gate ep = lookupino(start);
3677c478bd9Sstevel@tonic-gate if (ep == NIL || ep->e_type == NODE)
3687c478bd9Sstevel@tonic-gate continue;
3697c478bd9Sstevel@tonic-gate if (ep->e_flags & (NEW|EXTRACT))
3707c478bd9Sstevel@tonic-gate return (start);
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate return (start);
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate
3757c478bd9Sstevel@tonic-gate /*
3767c478bd9Sstevel@tonic-gate * report on a badly formed entry
3777c478bd9Sstevel@tonic-gate */
3787c478bd9Sstevel@tonic-gate void
badentry(struct entry * ep,char * msg)379*fe0e7ec4Smaheshvs badentry(struct entry *ep, char *msg)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate
3827c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("bad entry: %s\n"), msg);
3837c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("name: %s\n"), myname(ep));
3847c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("parent name %s\n"),
3857c478bd9Sstevel@tonic-gate myname(ep->e_parent));
3867c478bd9Sstevel@tonic-gate if (ep->e_sibling != NIL)
3877c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("sibling name: %s\n"),
3887c478bd9Sstevel@tonic-gate myname(ep->e_sibling));
3897c478bd9Sstevel@tonic-gate if (ep->e_entries != NIL)
3907c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("next entry name: %s\n"),
3917c478bd9Sstevel@tonic-gate myname(ep->e_entries));
3927c478bd9Sstevel@tonic-gate if (ep->e_links != NIL)
3937c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("next link name: %s\n"),
3947c478bd9Sstevel@tonic-gate myname(ep->e_links));
3957c478bd9Sstevel@tonic-gate if (ep->e_xattrs != NIL)
3967c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("attribute root name: %s\n"),
3977c478bd9Sstevel@tonic-gate myname(ep->e_xattrs));
3987c478bd9Sstevel@tonic-gate if (ep->e_next != NIL)
3997c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("next hashchain name: %s\n"),
4007c478bd9Sstevel@tonic-gate myname(ep->e_next));
4017c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("entry type: %s\n"),
4027c478bd9Sstevel@tonic-gate ep->e_type == NODE ? gettext("NODE") : gettext("LEAF"));
4037c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("inode number: %lu\n"), ep->e_ino);
4047c478bd9Sstevel@tonic-gate panic(gettext("flags: %s\n"), flagvalues(ep));
4057c478bd9Sstevel@tonic-gate /* Our callers are expected to handle our returning. */
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate
4087c478bd9Sstevel@tonic-gate /*
4097c478bd9Sstevel@tonic-gate * Construct a string indicating the active flag bits of an entry.
4107c478bd9Sstevel@tonic-gate */
4117c478bd9Sstevel@tonic-gate char *
flagvalues(struct entry * ep)412*fe0e7ec4Smaheshvs flagvalues(struct entry *ep)
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate static char flagbuf[BUFSIZ];
4157c478bd9Sstevel@tonic-gate
4167c478bd9Sstevel@tonic-gate (void) strlcpy(flagbuf, gettext("|NIL"), sizeof (flagbuf));
4177c478bd9Sstevel@tonic-gate flagbuf[0] = '\0';
4187c478bd9Sstevel@tonic-gate if (ep->e_flags & REMOVED)
4197c478bd9Sstevel@tonic-gate (void) strlcat(flagbuf, gettext("|REMOVED"), sizeof (flagbuf));
4207c478bd9Sstevel@tonic-gate if (ep->e_flags & TMPNAME)
4217c478bd9Sstevel@tonic-gate (void) strlcat(flagbuf, gettext("|TMPNAME"), sizeof (flagbuf));
4227c478bd9Sstevel@tonic-gate if (ep->e_flags & EXTRACT)
4237c478bd9Sstevel@tonic-gate (void) strlcat(flagbuf, gettext("|EXTRACT"), sizeof (flagbuf));
4247c478bd9Sstevel@tonic-gate if (ep->e_flags & NEW)
4257c478bd9Sstevel@tonic-gate (void) strlcat(flagbuf, gettext("|NEW"), sizeof (flagbuf));
4267c478bd9Sstevel@tonic-gate if (ep->e_flags & KEEP)
4277c478bd9Sstevel@tonic-gate (void) strlcat(flagbuf, gettext("|KEEP"), sizeof (flagbuf));
4287c478bd9Sstevel@tonic-gate if (ep->e_flags & EXISTED)
4297c478bd9Sstevel@tonic-gate (void) strlcat(flagbuf, gettext("|EXISTED"), sizeof (flagbuf));
4307c478bd9Sstevel@tonic-gate if (ep->e_flags & XATTR)
4317c478bd9Sstevel@tonic-gate (void) strlcat(flagbuf, gettext("|XATTR"), sizeof (flagbuf));
4327c478bd9Sstevel@tonic-gate if (ep->e_flags & XATTRROOT)
4337c478bd9Sstevel@tonic-gate (void) strlcat(flagbuf, gettext("|XATTRROOT"),
4347c478bd9Sstevel@tonic-gate sizeof (flagbuf));
4357c478bd9Sstevel@tonic-gate return (&flagbuf[1]);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate
4387c478bd9Sstevel@tonic-gate /*
4397c478bd9Sstevel@tonic-gate * Check to see if a name is on a dump tape.
4407c478bd9Sstevel@tonic-gate */
4417c478bd9Sstevel@tonic-gate ino_t
dirlookup(char * name)442*fe0e7ec4Smaheshvs dirlookup(char *name)
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate ino_t ino;
4457c478bd9Sstevel@tonic-gate
4467c478bd9Sstevel@tonic-gate ino = psearch(name);
4477c478bd9Sstevel@tonic-gate if (ino == 0 || BIT(ino, dumpmap) == 0)
4487c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s is not on volume\n"), name);
4497c478bd9Sstevel@tonic-gate return (ino);
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate
4527c478bd9Sstevel@tonic-gate /*
4537c478bd9Sstevel@tonic-gate * Elicit a reply.
4547c478bd9Sstevel@tonic-gate */
455*fe0e7ec4Smaheshvs int
reply(char * question)456*fe0e7ec4Smaheshvs reply(char *question)
4577c478bd9Sstevel@tonic-gate {
4587c478bd9Sstevel@tonic-gate char *yesorno = gettext("yn"); /* must be two characters, "yes" first */
4597c478bd9Sstevel@tonic-gate int c;
4607c478bd9Sstevel@tonic-gate
4617c478bd9Sstevel@tonic-gate do {
4627c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s? [%s] ", question, yesorno);
4637c478bd9Sstevel@tonic-gate (void) fflush(stderr);
4647c478bd9Sstevel@tonic-gate c = getc(terminal);
4657c478bd9Sstevel@tonic-gate while (c != '\n' && getc(terminal) != '\n') {
4667c478bd9Sstevel@tonic-gate if (ferror(terminal)) {
4677c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
4687c478bd9Sstevel@tonic-gate "Error reading response\n"));
4697c478bd9Sstevel@tonic-gate (void) fflush(stderr);
4707c478bd9Sstevel@tonic-gate return (FAIL);
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate if (feof(terminal))
4737c478bd9Sstevel@tonic-gate return (FAIL);
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate if (isupper(c))
4767c478bd9Sstevel@tonic-gate c = tolower(c);
4777c478bd9Sstevel@tonic-gate } while (c != yesorno[0] && c != yesorno[1]);
4787c478bd9Sstevel@tonic-gate if (c == yesorno[0])
4797c478bd9Sstevel@tonic-gate return (GOOD);
4807c478bd9Sstevel@tonic-gate return (FAIL);
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate
4837c478bd9Sstevel@tonic-gate /*
4847c478bd9Sstevel@tonic-gate * handle unexpected inconsistencies
4857c478bd9Sstevel@tonic-gate */
4867c478bd9Sstevel@tonic-gate /*
4877c478bd9Sstevel@tonic-gate * Note that a panic w/ EOF on the tty means all panics will return...
4887c478bd9Sstevel@tonic-gate */
4897c478bd9Sstevel@tonic-gate #ifdef __STDC__
4907c478bd9Sstevel@tonic-gate #include <stdarg.h>
4917c478bd9Sstevel@tonic-gate
4927c478bd9Sstevel@tonic-gate /* VARARGS1 */
4937c478bd9Sstevel@tonic-gate void
panic(const char * msg,...)4947c478bd9Sstevel@tonic-gate panic(const char *msg, ...)
4957c478bd9Sstevel@tonic-gate {
4967c478bd9Sstevel@tonic-gate va_list args;
4977c478bd9Sstevel@tonic-gate
4987c478bd9Sstevel@tonic-gate va_start(args, msg);
4997c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, msg, args);
5007c478bd9Sstevel@tonic-gate va_end(args);
5017c478bd9Sstevel@tonic-gate if (reply(gettext("abort")) == GOOD) {
5027c478bd9Sstevel@tonic-gate if (reply(gettext("dump core")) == GOOD)
5037c478bd9Sstevel@tonic-gate abort();
5047c478bd9Sstevel@tonic-gate done(1);
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate }
5077c478bd9Sstevel@tonic-gate #else
5087c478bd9Sstevel@tonic-gate #include <varargs.h>
5097c478bd9Sstevel@tonic-gate
5107c478bd9Sstevel@tonic-gate /* VARARGS1 */
5117c478bd9Sstevel@tonic-gate void
panic(va_dcl)512*fe0e7ec4Smaheshvs panic(va_dcl)
5137c478bd9Sstevel@tonic-gate {
5147c478bd9Sstevel@tonic-gate va_list args;
5157c478bd9Sstevel@tonic-gate char *msg;
5167c478bd9Sstevel@tonic-gate
5177c478bd9Sstevel@tonic-gate va_start(args);
5187c478bd9Sstevel@tonic-gate msg = va_arg(args, char *);
5197c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, msg, args);
5207c478bd9Sstevel@tonic-gate va_end(args);
5217c478bd9Sstevel@tonic-gate if (reply(gettext("abort")) == GOOD) {
5227c478bd9Sstevel@tonic-gate if (reply(gettext("dump core")) == GOOD)
5237c478bd9Sstevel@tonic-gate abort();
5247c478bd9Sstevel@tonic-gate done(1);
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate #endif
5277c478bd9Sstevel@tonic-gate
5287c478bd9Sstevel@tonic-gate /*
5297c478bd9Sstevel@tonic-gate * Locale-specific version of ctime
5307c478bd9Sstevel@tonic-gate */
5317c478bd9Sstevel@tonic-gate char *
532*fe0e7ec4Smaheshvs lctime(time_t *tp)
5337c478bd9Sstevel@tonic-gate {
5347c478bd9Sstevel@tonic-gate static char buf[256];
5357c478bd9Sstevel@tonic-gate struct tm *tm;
5367c478bd9Sstevel@tonic-gate
5377c478bd9Sstevel@tonic-gate tm = localtime(tp);
5387c478bd9Sstevel@tonic-gate (void) strftime(buf, sizeof (buf), "%c\n", tm);
5397c478bd9Sstevel@tonic-gate return (buf);
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate
5427c478bd9Sstevel@tonic-gate static int
5437c478bd9Sstevel@tonic-gate statcmp(const struct stat *left, const struct stat *right)
5447c478bd9Sstevel@tonic-gate {
5457c478bd9Sstevel@tonic-gate int result = 1;
5467c478bd9Sstevel@tonic-gate
5477c478bd9Sstevel@tonic-gate if ((left->st_dev == right->st_dev) &&
5487c478bd9Sstevel@tonic-gate (left->st_ino == right->st_ino) &&
5497c478bd9Sstevel@tonic-gate (left->st_mode == right->st_mode) &&
5507c478bd9Sstevel@tonic-gate (left->st_nlink == right->st_nlink) &&
5517c478bd9Sstevel@tonic-gate (left->st_uid == right->st_uid) &&
5527c478bd9Sstevel@tonic-gate (left->st_gid == right->st_gid) &&
5537c478bd9Sstevel@tonic-gate (left->st_rdev == right->st_rdev) &&
5547c478bd9Sstevel@tonic-gate (left->st_ctim.tv_sec == right->st_ctim.tv_sec) &&
5557c478bd9Sstevel@tonic-gate (left->st_ctim.tv_nsec == right->st_ctim.tv_nsec) &&
5567c478bd9Sstevel@tonic-gate (left->st_mtim.tv_sec == right->st_mtim.tv_sec) &&
5577c478bd9Sstevel@tonic-gate (left->st_mtim.tv_nsec == right->st_mtim.tv_nsec) &&
5587c478bd9Sstevel@tonic-gate (left->st_blksize == right->st_blksize) &&
5597c478bd9Sstevel@tonic-gate (left->st_blocks == right->st_blocks)) {
5607c478bd9Sstevel@tonic-gate result = 0;
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate
5637c478bd9Sstevel@tonic-gate return (result);
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate
5667c478bd9Sstevel@tonic-gate /*
5677c478bd9Sstevel@tonic-gate * Safely open a file.
5687c478bd9Sstevel@tonic-gate */
5697c478bd9Sstevel@tonic-gate int
5707c478bd9Sstevel@tonic-gate safe_open(int dfd, const char *filename, int mode, int perms)
5717c478bd9Sstevel@tonic-gate {
5727c478bd9Sstevel@tonic-gate static int init_syslog = 1;
5737c478bd9Sstevel@tonic-gate int fd;
5747c478bd9Sstevel@tonic-gate int working_mode;
5757c478bd9Sstevel@tonic-gate int saverr;
5767c478bd9Sstevel@tonic-gate char *errtext;
5777c478bd9Sstevel@tonic-gate struct stat pre_stat, pre_lstat;
5787c478bd9Sstevel@tonic-gate struct stat post_stat, post_lstat;
5797c478bd9Sstevel@tonic-gate
5807c478bd9Sstevel@tonic-gate if (init_syslog) {
5817c478bd9Sstevel@tonic-gate openlog(progname, LOG_CONS, LOG_DAEMON);
5827c478bd9Sstevel@tonic-gate init_syslog = 0;
5837c478bd9Sstevel@tonic-gate }
5847c478bd9Sstevel@tonic-gate
5857c478bd9Sstevel@tonic-gate /*
5867c478bd9Sstevel@tonic-gate * Don't want to be spoofed into trashing something we
5877c478bd9Sstevel@tonic-gate * shouldn't, thus the following rigamarole. If it doesn't
5887c478bd9Sstevel@tonic-gate * exist, we create it and proceed. Otherwise, require that
5897c478bd9Sstevel@tonic-gate * what's there be a real file with no extraneous links and
5907c478bd9Sstevel@tonic-gate * owned by whoever ran us.
5917c478bd9Sstevel@tonic-gate *
5927c478bd9Sstevel@tonic-gate * The silliness with using both lstat() and fstat() is to avoid
5937c478bd9Sstevel@tonic-gate * race-condition games with someone replacing the file with a
5947c478bd9Sstevel@tonic-gate * symlink after we've opened it. If there was an flstat(),
5957c478bd9Sstevel@tonic-gate * we wouldn't need the fstat().
5967c478bd9Sstevel@tonic-gate *
5977c478bd9Sstevel@tonic-gate * The initial open with the hard-coded flags is ok even if we
5987c478bd9Sstevel@tonic-gate * are intending to open only for reading. If it succeeds,
5997c478bd9Sstevel@tonic-gate * then the file did not exist, and we'll synthesize an appropriate
6007c478bd9Sstevel@tonic-gate * complaint below. Otherwise, it does exist, so we won't be
6017c478bd9Sstevel@tonic-gate * truncating it with the open.
6027c478bd9Sstevel@tonic-gate */
6037c478bd9Sstevel@tonic-gate if ((fd = openat(dfd, filename,
6047c478bd9Sstevel@tonic-gate O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_LARGEFILE, perms)) < 0) {
6057c478bd9Sstevel@tonic-gate if (errno == EEXIST) {
6067c478bd9Sstevel@tonic-gate if (fstatat(dfd, filename, &pre_lstat,
6077c478bd9Sstevel@tonic-gate AT_SYMLINK_NOFOLLOW) < 0) {
6087c478bd9Sstevel@tonic-gate saverr = errno;
6097c478bd9Sstevel@tonic-gate (void) close(fd);
6107c478bd9Sstevel@tonic-gate errno = saverr;
6117c478bd9Sstevel@tonic-gate return (-1);
6127c478bd9Sstevel@tonic-gate }
6137c478bd9Sstevel@tonic-gate
6147c478bd9Sstevel@tonic-gate if (fstatat(dfd, filename, &pre_stat, 0) < 0) {
6157c478bd9Sstevel@tonic-gate saverr = errno;
6167c478bd9Sstevel@tonic-gate (void) close(fd);
6177c478bd9Sstevel@tonic-gate errno = saverr;
6187c478bd9Sstevel@tonic-gate return (-1);
6197c478bd9Sstevel@tonic-gate }
6207c478bd9Sstevel@tonic-gate
6217c478bd9Sstevel@tonic-gate working_mode = mode & (O_WRONLY|O_RDWR|O_RDONLY);
6227c478bd9Sstevel@tonic-gate working_mode |= O_LARGEFILE;
6237c478bd9Sstevel@tonic-gate
6247c478bd9Sstevel@tonic-gate if ((fd = openat(dfd, filename, working_mode)) < 0) {
6257c478bd9Sstevel@tonic-gate if (errno == ENOENT) {
6267c478bd9Sstevel@tonic-gate errtext = gettext(
6277c478bd9Sstevel@tonic-gate "Unexpected condition detected: %s used to exist, but doesn't any longer\n");
6287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, errtext,
6297c478bd9Sstevel@tonic-gate filename);
6307c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, errtext, filename);
6317c478bd9Sstevel@tonic-gate errno = ENOENT;
6327c478bd9Sstevel@tonic-gate }
6337c478bd9Sstevel@tonic-gate return (-1);
6347c478bd9Sstevel@tonic-gate }
6357c478bd9Sstevel@tonic-gate
6367c478bd9Sstevel@tonic-gate if (fstatat(fd, NULL, &post_lstat,
6377c478bd9Sstevel@tonic-gate AT_SYMLINK_NOFOLLOW) < 0) {
6387c478bd9Sstevel@tonic-gate saverr = errno;
6397c478bd9Sstevel@tonic-gate (void) close(fd);
6407c478bd9Sstevel@tonic-gate errno = saverr;
6417c478bd9Sstevel@tonic-gate return (-1);
6427c478bd9Sstevel@tonic-gate }
6437c478bd9Sstevel@tonic-gate
6447c478bd9Sstevel@tonic-gate if (fstatat(fd, NULL, &post_stat, 0) < 0) {
6457c478bd9Sstevel@tonic-gate saverr = errno;
6467c478bd9Sstevel@tonic-gate (void) close(fd);
6477c478bd9Sstevel@tonic-gate errno = saverr;
6487c478bd9Sstevel@tonic-gate return (-1);
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate
6517c478bd9Sstevel@tonic-gate if (statcmp(&pre_lstat, &post_lstat) != 0) {
6527c478bd9Sstevel@tonic-gate errtext = gettext(
6537c478bd9Sstevel@tonic-gate "Unexpected condition detected: %s's lstat(2) information changed\n");
6547c478bd9Sstevel@tonic-gate (void) fprintf(stderr, errtext, filename);
6557c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, errtext, filename);
6567c478bd9Sstevel@tonic-gate errno = EPERM;
6577c478bd9Sstevel@tonic-gate return (-1);
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate
6607c478bd9Sstevel@tonic-gate if (statcmp(&pre_stat, &post_stat) != 0) {
6617c478bd9Sstevel@tonic-gate errtext = gettext(
6627c478bd9Sstevel@tonic-gate "Unexpected condition detected: %s's stat(2) information changed\n");
6637c478bd9Sstevel@tonic-gate (void) fprintf(stderr, errtext, filename);
6647c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, errtext, filename);
6657c478bd9Sstevel@tonic-gate errno = EPERM;
6667c478bd9Sstevel@tonic-gate return (-1);
6677c478bd9Sstevel@tonic-gate }
6687c478bd9Sstevel@tonic-gate
6697c478bd9Sstevel@tonic-gate /*
6707c478bd9Sstevel@tonic-gate * If inode, device, or type are wrong, bail out.
6717c478bd9Sstevel@tonic-gate */
6727c478bd9Sstevel@tonic-gate if ((!S_ISREG(post_lstat.st_mode) ||
6737c478bd9Sstevel@tonic-gate (post_stat.st_ino != post_lstat.st_ino) ||
6747c478bd9Sstevel@tonic-gate (post_stat.st_dev != post_lstat.st_dev))) {
6757c478bd9Sstevel@tonic-gate errtext = gettext(
6767c478bd9Sstevel@tonic-gate "Unexpected condition detected: %s is not a regular file\n");
6777c478bd9Sstevel@tonic-gate (void) fprintf(stderr, errtext, filename);
6787c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, errtext, filename);
6797c478bd9Sstevel@tonic-gate (void) close(fd);
6807c478bd9Sstevel@tonic-gate errno = EPERM;
6817c478bd9Sstevel@tonic-gate return (-1);
6827c478bd9Sstevel@tonic-gate }
6837c478bd9Sstevel@tonic-gate
6847c478bd9Sstevel@tonic-gate /*
6857c478bd9Sstevel@tonic-gate * Bad link count implies someone's linked our
6867c478bd9Sstevel@tonic-gate * target to something else, which we probably
6877c478bd9Sstevel@tonic-gate * shouldn't step on.
6887c478bd9Sstevel@tonic-gate */
6897c478bd9Sstevel@tonic-gate if (post_lstat.st_nlink != 1) {
6907c478bd9Sstevel@tonic-gate errtext = gettext(
6917c478bd9Sstevel@tonic-gate "Unexpected condition detected: %s must have exactly one link\n");
6927c478bd9Sstevel@tonic-gate (void) fprintf(stderr, errtext, filename);
6937c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, errtext, filename);
6947c478bd9Sstevel@tonic-gate (void) close(fd);
6957c478bd9Sstevel@tonic-gate errno = EPERM;
6967c478bd9Sstevel@tonic-gate return (-1);
6977c478bd9Sstevel@tonic-gate }
6987c478bd9Sstevel@tonic-gate /*
6997c478bd9Sstevel@tonic-gate * Root might make a file, but non-root might
7007c478bd9Sstevel@tonic-gate * need to open it. If the permissions let us
7017c478bd9Sstevel@tonic-gate * get this far, then let it through.
7027c478bd9Sstevel@tonic-gate */
7037c478bd9Sstevel@tonic-gate if (post_lstat.st_uid != getuid() &&
7047c478bd9Sstevel@tonic-gate post_lstat.st_uid != 0) {
7057c478bd9Sstevel@tonic-gate errtext = gettext(
7067c478bd9Sstevel@tonic-gate "Unsupported condition detected: %s must be owned by uid %ld or 0\n");
7077c478bd9Sstevel@tonic-gate (void) fprintf(stderr, errtext, filename,
7087c478bd9Sstevel@tonic-gate (long)getuid());
7097c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, errtext, filename,
7107c478bd9Sstevel@tonic-gate (long)getuid());
7117c478bd9Sstevel@tonic-gate (void) close(fd);
7127c478bd9Sstevel@tonic-gate errno = EPERM;
7137c478bd9Sstevel@tonic-gate return (-1);
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate if (mode & (O_WRONLY|O_TRUNC)) {
7167c478bd9Sstevel@tonic-gate if (ftruncate(fd, (off_t)0) < 0) {
7177c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
7187c478bd9Sstevel@tonic-gate "ftruncate(%s): %s\n",
7197c478bd9Sstevel@tonic-gate filename, strerror(errno));
7207c478bd9Sstevel@tonic-gate (void) close(fd);
7217c478bd9Sstevel@tonic-gate return (-1);
7227c478bd9Sstevel@tonic-gate }
7237c478bd9Sstevel@tonic-gate }
7247c478bd9Sstevel@tonic-gate } else {
7257c478bd9Sstevel@tonic-gate /*
7267c478bd9Sstevel@tonic-gate * Didn't exist, but couldn't open it.
7277c478bd9Sstevel@tonic-gate */
7287c478bd9Sstevel@tonic-gate return (-1);
7297c478bd9Sstevel@tonic-gate }
7307c478bd9Sstevel@tonic-gate } else {
7317c478bd9Sstevel@tonic-gate /*
7327c478bd9Sstevel@tonic-gate * If truncating open succeeded for a read-only open,
7337c478bd9Sstevel@tonic-gate * bail out, as we really shouldn't have succeeded.
7347c478bd9Sstevel@tonic-gate */
7357c478bd9Sstevel@tonic-gate if (mode & O_RDONLY) {
7367c478bd9Sstevel@tonic-gate /* Undo the O_CREAT */
7377c478bd9Sstevel@tonic-gate (void) unlinkat(dfd, filename, 0);
7387c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "open(%s): %s\n",
7397c478bd9Sstevel@tonic-gate filename, strerror(ENOENT));
7407c478bd9Sstevel@tonic-gate (void) close(fd);
7417c478bd9Sstevel@tonic-gate errno = ENOENT;
7427c478bd9Sstevel@tonic-gate return (-1);
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate }
7457c478bd9Sstevel@tonic-gate
7467c478bd9Sstevel@tonic-gate return (fd);
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate
7497c478bd9Sstevel@tonic-gate /*
7507c478bd9Sstevel@tonic-gate * STDIO version of safe_open. Equivalent to fopen64(...).
7517c478bd9Sstevel@tonic-gate */
7527c478bd9Sstevel@tonic-gate FILE *
7537c478bd9Sstevel@tonic-gate safe_fopen(const char *filename, const char *smode, int perms)
7547c478bd9Sstevel@tonic-gate {
7557c478bd9Sstevel@tonic-gate int fd;
7567c478bd9Sstevel@tonic-gate int bmode;
7577c478bd9Sstevel@tonic-gate
7587c478bd9Sstevel@tonic-gate /*
7597c478bd9Sstevel@tonic-gate * accepts only modes "r", "r+", and "w"
7607c478bd9Sstevel@tonic-gate */
7617c478bd9Sstevel@tonic-gate if (smode[0] == 'r') {
7627c478bd9Sstevel@tonic-gate if (smode[1] == '\0') {
7637c478bd9Sstevel@tonic-gate bmode = O_RDONLY;
7647c478bd9Sstevel@tonic-gate } else if ((smode[1] == '+') && (smode[2] == '\0')) {
7657c478bd9Sstevel@tonic-gate bmode = O_RDWR;
7667c478bd9Sstevel@tonic-gate }
7677c478bd9Sstevel@tonic-gate } else if ((smode[0] == 'w') && (smode[1] == '\0')) {
7687c478bd9Sstevel@tonic-gate bmode = O_WRONLY;
7697c478bd9Sstevel@tonic-gate } else {
7707c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
7717c478bd9Sstevel@tonic-gate gettext("internal error: safe_fopen: invalid mode `%s'\n"),
7727c478bd9Sstevel@tonic-gate smode);
7737c478bd9Sstevel@tonic-gate return (NULL);
7747c478bd9Sstevel@tonic-gate }
7757c478bd9Sstevel@tonic-gate
7767c478bd9Sstevel@tonic-gate fd = safe_open(AT_FDCWD, filename, bmode, perms);
7777c478bd9Sstevel@tonic-gate
7787c478bd9Sstevel@tonic-gate /*
7797c478bd9Sstevel@tonic-gate * caller is expected to report error.
7807c478bd9Sstevel@tonic-gate */
7817c478bd9Sstevel@tonic-gate if (fd >= 0)
7827c478bd9Sstevel@tonic-gate return (fdopen(fd, smode));
7837c478bd9Sstevel@tonic-gate
7847c478bd9Sstevel@tonic-gate return ((FILE *)NULL);
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate
7877c478bd9Sstevel@tonic-gate /*
7887c478bd9Sstevel@tonic-gate * Read the contents of a directory.
7897c478bd9Sstevel@tonic-gate */
7907c478bd9Sstevel@tonic-gate int
791*fe0e7ec4Smaheshvs mkentry(char *name, ino_t ino, struct arglist *ap)
7927c478bd9Sstevel@tonic-gate {
7937c478bd9Sstevel@tonic-gate struct afile *fp;
7947c478bd9Sstevel@tonic-gate
7957c478bd9Sstevel@tonic-gate if (ap->base == NULL) {
7967c478bd9Sstevel@tonic-gate ap->nent = 20;
7977c478bd9Sstevel@tonic-gate ap->base = (struct afile *)calloc((unsigned)ap->nent,
7987c478bd9Sstevel@tonic-gate sizeof (*(ap->base)));
7997c478bd9Sstevel@tonic-gate if (ap->base == NULL) {
8007c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
8017c478bd9Sstevel@tonic-gate gettext("%s: out of memory\n"), ap->cmd);
8027c478bd9Sstevel@tonic-gate return (FAIL);
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate }
8057c478bd9Sstevel@tonic-gate if (ap->head == NULL)
8067c478bd9Sstevel@tonic-gate ap->head = ap->last = ap->base;
8077c478bd9Sstevel@tonic-gate fp = ap->last;
8087c478bd9Sstevel@tonic-gate fp->fnum = ino;
8097c478bd9Sstevel@tonic-gate fp->fname = savename(name);
8107c478bd9Sstevel@tonic-gate fp++;
8117c478bd9Sstevel@tonic-gate if (fp == ap->head + ap->nent) {
8127c478bd9Sstevel@tonic-gate ap->base = (struct afile *)realloc((char *)ap->base,
8137c478bd9Sstevel@tonic-gate (size_t)(2 * ap->nent * (size_t)sizeof (*(ap->base))));
8147c478bd9Sstevel@tonic-gate if (ap->base == NULL) {
8157c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
8167c478bd9Sstevel@tonic-gate gettext("%s: out of memory\n"), ap->cmd);
8177c478bd9Sstevel@tonic-gate return (FAIL);
8187c478bd9Sstevel@tonic-gate }
8197c478bd9Sstevel@tonic-gate ap->head = ap->base;
8207c478bd9Sstevel@tonic-gate fp = ap->head + ap->nent;
8217c478bd9Sstevel@tonic-gate ap->nent *= 2;
8227c478bd9Sstevel@tonic-gate }
8237c478bd9Sstevel@tonic-gate ap->last = fp;
8247c478bd9Sstevel@tonic-gate return (GOOD);
8257c478bd9Sstevel@tonic-gate }
8267c478bd9Sstevel@tonic-gate
8277c478bd9Sstevel@tonic-gate #ifdef __STDC__
8287c478bd9Sstevel@tonic-gate static int gmatch(wchar_t *, wchar_t *);
8297c478bd9Sstevel@tonic-gate static int addg(struct direct *, char *, char *, struct arglist *);
8307c478bd9Sstevel@tonic-gate #else
8317c478bd9Sstevel@tonic-gate static int gmatch();
8327c478bd9Sstevel@tonic-gate static int addg();
8337c478bd9Sstevel@tonic-gate #endif
8347c478bd9Sstevel@tonic-gate
8357c478bd9Sstevel@tonic-gate /*
8367c478bd9Sstevel@tonic-gate * XXX This value is ASCII (but not language) dependent. In
8377c478bd9Sstevel@tonic-gate * ASCII, it is the DEL character (unlikely to appear in paths).
8387c478bd9Sstevel@tonic-gate * If you are compiling on an EBCDIC-based machine, re-define
8397c478bd9Sstevel@tonic-gate * this (0x7f is '"') to be something like 0x7 (DEL). It's
8407c478bd9Sstevel@tonic-gate * either this hack or re-write the expand() algorithm...
8417c478bd9Sstevel@tonic-gate */
8427c478bd9Sstevel@tonic-gate #define DELIMCHAR ((char)0x7f)
8437c478bd9Sstevel@tonic-gate
8447c478bd9Sstevel@tonic-gate /*
8457c478bd9Sstevel@tonic-gate * Expand a file name.
8467c478bd9Sstevel@tonic-gate * "as" is the pattern to expand.
8477c478bd9Sstevel@tonic-gate * "rflg" non-zero indicates that we're recursing.
8487c478bd9Sstevel@tonic-gate * "ap" is where to put the results of the expansion.
8497c478bd9Sstevel@tonic-gate *
8507c478bd9Sstevel@tonic-gate * Our caller guarantees that "as" is at least the string ".".
8517c478bd9Sstevel@tonic-gate */
8527c478bd9Sstevel@tonic-gate int
853*fe0e7ec4Smaheshvs expand(char *as, int rflg, struct arglist *ap)
8547c478bd9Sstevel@tonic-gate {
8557c478bd9Sstevel@tonic-gate int count, size;
8567c478bd9Sstevel@tonic-gate char dir = 0;
8577c478bd9Sstevel@tonic-gate char *rescan = 0;
8587c478bd9Sstevel@tonic-gate RST_DIR *dirp;
8597c478bd9Sstevel@tonic-gate char *s, *cs;
8607c478bd9Sstevel@tonic-gate int sindex, rindexa, lindex;
8617c478bd9Sstevel@tonic-gate struct direct *dp;
8627c478bd9Sstevel@tonic-gate char slash;
8637c478bd9Sstevel@tonic-gate char *rs;
8647c478bd9Sstevel@tonic-gate char c;
8657c478bd9Sstevel@tonic-gate wchar_t w_fname[PATH_MAX+1];
8667c478bd9Sstevel@tonic-gate wchar_t w_pname[PATH_MAX+1];
8677c478bd9Sstevel@tonic-gate
8687c478bd9Sstevel@tonic-gate /*
8697c478bd9Sstevel@tonic-gate * check for meta chars
8707c478bd9Sstevel@tonic-gate */
8717c478bd9Sstevel@tonic-gate s = cs = as;
8727c478bd9Sstevel@tonic-gate slash = 0;
8737c478bd9Sstevel@tonic-gate while (*cs != '*' && *cs != '?' && *cs != '[') {
8747c478bd9Sstevel@tonic-gate if (*cs++ == 0) {
8757c478bd9Sstevel@tonic-gate if (rflg && slash)
8767c478bd9Sstevel@tonic-gate break;
8777c478bd9Sstevel@tonic-gate else
8787c478bd9Sstevel@tonic-gate return (0);
8797c478bd9Sstevel@tonic-gate } else if (*cs == '/') {
8807c478bd9Sstevel@tonic-gate slash++;
8817c478bd9Sstevel@tonic-gate }
8827c478bd9Sstevel@tonic-gate }
8837c478bd9Sstevel@tonic-gate for (;;) {
8847c478bd9Sstevel@tonic-gate if (cs == s) {
8857c478bd9Sstevel@tonic-gate s = "";
8867c478bd9Sstevel@tonic-gate break;
8877c478bd9Sstevel@tonic-gate } else if (*--cs == '/') {
8887c478bd9Sstevel@tonic-gate *cs = 0;
8897c478bd9Sstevel@tonic-gate if (s == cs)
8907c478bd9Sstevel@tonic-gate s = "/";
8917c478bd9Sstevel@tonic-gate break;
8927c478bd9Sstevel@tonic-gate }
8937c478bd9Sstevel@tonic-gate }
8947c478bd9Sstevel@tonic-gate if ((dirp = rst_opendir(s)) != NULL)
8957c478bd9Sstevel@tonic-gate dir++;
8967c478bd9Sstevel@tonic-gate count = 0;
8977c478bd9Sstevel@tonic-gate if (*cs == 0)
8987c478bd9Sstevel@tonic-gate *cs++ = DELIMCHAR;
8997c478bd9Sstevel@tonic-gate if (dir) {
9007c478bd9Sstevel@tonic-gate /*
9017c478bd9Sstevel@tonic-gate * check for rescan
9027c478bd9Sstevel@tonic-gate */
9037c478bd9Sstevel@tonic-gate rs = cs;
9047c478bd9Sstevel@tonic-gate do {
9057c478bd9Sstevel@tonic-gate if (*rs == '/') {
9067c478bd9Sstevel@tonic-gate rescan = rs;
9077c478bd9Sstevel@tonic-gate *rs = 0;
9087c478bd9Sstevel@tonic-gate }
9097c478bd9Sstevel@tonic-gate } while (*rs++);
9107c478bd9Sstevel@tonic-gate /* LINTED: result fits into an int */
9117c478bd9Sstevel@tonic-gate sindex = (int)(ap->last - ap->head);
9127c478bd9Sstevel@tonic-gate (void) mbstowcs(w_pname, cs, PATH_MAX);
9137c478bd9Sstevel@tonic-gate w_pname[PATH_MAX - 1] = 0;
9147c478bd9Sstevel@tonic-gate while ((dp = rst_readdir(dirp)) != NULL && dp->d_ino != 0) {
9157c478bd9Sstevel@tonic-gate if (!dflag && BIT(dp->d_ino, dumpmap) == 0)
9167c478bd9Sstevel@tonic-gate continue;
9177c478bd9Sstevel@tonic-gate if ((*dp->d_name == '.' && *cs != '.'))
9187c478bd9Sstevel@tonic-gate continue;
9197c478bd9Sstevel@tonic-gate (void) mbstowcs(w_fname, dp->d_name, PATH_MAX);
9207c478bd9Sstevel@tonic-gate w_fname[PATH_MAX - 1] = 0;
9217c478bd9Sstevel@tonic-gate if (gmatch(w_fname, w_pname)) {
9227c478bd9Sstevel@tonic-gate if (addg(dp, s, rescan, ap) < 0) {
9237c478bd9Sstevel@tonic-gate rst_closedir(dirp);
9247c478bd9Sstevel@tonic-gate return (-1);
9257c478bd9Sstevel@tonic-gate }
9267c478bd9Sstevel@tonic-gate count++;
9277c478bd9Sstevel@tonic-gate }
9287c478bd9Sstevel@tonic-gate }
9297c478bd9Sstevel@tonic-gate if (rescan) {
9307c478bd9Sstevel@tonic-gate rindexa = sindex;
9317c478bd9Sstevel@tonic-gate /* LINTED: result fits into an int */
9327c478bd9Sstevel@tonic-gate lindex = (int)(ap->last - ap->head);
9337c478bd9Sstevel@tonic-gate if (count) {
9347c478bd9Sstevel@tonic-gate count = 0;
9357c478bd9Sstevel@tonic-gate while (rindexa < lindex) {
9367c478bd9Sstevel@tonic-gate size = expand(ap->head[rindexa].fname,
9377c478bd9Sstevel@tonic-gate 1, ap);
9387c478bd9Sstevel@tonic-gate if (size < 0) {
9397c478bd9Sstevel@tonic-gate rst_closedir(dirp);
9407c478bd9Sstevel@tonic-gate return (size);
9417c478bd9Sstevel@tonic-gate }
9427c478bd9Sstevel@tonic-gate count += size;
9437c478bd9Sstevel@tonic-gate rindexa++;
9447c478bd9Sstevel@tonic-gate }
9457c478bd9Sstevel@tonic-gate }
9467c478bd9Sstevel@tonic-gate /* LINTED: lint is confused about pointer size/type */
9477c478bd9Sstevel@tonic-gate bcopy((void *)(&ap->head[lindex]),
9487c478bd9Sstevel@tonic-gate (void *)(&ap->head[sindex]),
9497c478bd9Sstevel@tonic-gate (size_t)((ap->last - &ap->head[rindexa])) *
9507c478bd9Sstevel@tonic-gate sizeof (*ap->head));
9517c478bd9Sstevel@tonic-gate ap->last -= lindex - sindex;
9527c478bd9Sstevel@tonic-gate *rescan = '/';
9537c478bd9Sstevel@tonic-gate }
9547c478bd9Sstevel@tonic-gate rst_closedir(dirp);
9557c478bd9Sstevel@tonic-gate }
9567c478bd9Sstevel@tonic-gate s = as;
9577c478bd9Sstevel@tonic-gate while ((c = *s) != '\0')
9587c478bd9Sstevel@tonic-gate *s++ = (c != DELIMCHAR ? c : '/');
9597c478bd9Sstevel@tonic-gate
9607c478bd9Sstevel@tonic-gate return (count);
9617c478bd9Sstevel@tonic-gate }
9627c478bd9Sstevel@tonic-gate
9637c478bd9Sstevel@tonic-gate /*
9647c478bd9Sstevel@tonic-gate * Check for a name match
9657c478bd9Sstevel@tonic-gate */
9667c478bd9Sstevel@tonic-gate static int
967*fe0e7ec4Smaheshvs gmatch(wchar_t *s, wchar_t *p)
9687c478bd9Sstevel@tonic-gate {
9697c478bd9Sstevel@tonic-gate long scc; /* source character to text */
9707c478bd9Sstevel@tonic-gate wchar_t c; /* pattern character to match */
9717c478bd9Sstevel@tonic-gate char ok; /* [x-y] range match status */
9727c478bd9Sstevel@tonic-gate long lc; /* left character of [x-y] range */
9737c478bd9Sstevel@tonic-gate
9747c478bd9Sstevel@tonic-gate scc = *s++;
9757c478bd9Sstevel@tonic-gate switch (c = *p++) {
9767c478bd9Sstevel@tonic-gate
9777c478bd9Sstevel@tonic-gate case '[':
9787c478bd9Sstevel@tonic-gate ok = 0;
9797c478bd9Sstevel@tonic-gate lc = -1;
9807c478bd9Sstevel@tonic-gate while (c = *p++) {
9817c478bd9Sstevel@tonic-gate if (c == ']') {
9827c478bd9Sstevel@tonic-gate return (ok ? gmatch(s, p) : 0);
9837c478bd9Sstevel@tonic-gate } else if (c == '-') {
9847c478bd9Sstevel@tonic-gate wchar_t rc = *p++;
9857c478bd9Sstevel@tonic-gate /*
9867c478bd9Sstevel@tonic-gate * Check both ends must belong to
9877c478bd9Sstevel@tonic-gate * the same codeset.
9887c478bd9Sstevel@tonic-gate */
9897c478bd9Sstevel@tonic-gate if (wcsetno(lc) != wcsetno(rc)) {
9907c478bd9Sstevel@tonic-gate /*
9917c478bd9Sstevel@tonic-gate * If not, ignore the '-'
9927c478bd9Sstevel@tonic-gate * operator and [x-y] is
9937c478bd9Sstevel@tonic-gate * treated as if it were
9947c478bd9Sstevel@tonic-gate * [xy].
9957c478bd9Sstevel@tonic-gate */
9967c478bd9Sstevel@tonic-gate if (scc == lc)
9977c478bd9Sstevel@tonic-gate ok++;
9987c478bd9Sstevel@tonic-gate if (scc == (lc = rc))
9997c478bd9Sstevel@tonic-gate ok++;
10007c478bd9Sstevel@tonic-gate } else if (lc <= scc && scc <= rc)
10017c478bd9Sstevel@tonic-gate ok++;
10027c478bd9Sstevel@tonic-gate } else {
10037c478bd9Sstevel@tonic-gate lc = c;
10047c478bd9Sstevel@tonic-gate if (scc == lc)
10057c478bd9Sstevel@tonic-gate ok++;
10067c478bd9Sstevel@tonic-gate }
10077c478bd9Sstevel@tonic-gate }
10087c478bd9Sstevel@tonic-gate /* No closing bracket => failure */
10097c478bd9Sstevel@tonic-gate return (0);
10107c478bd9Sstevel@tonic-gate
10117c478bd9Sstevel@tonic-gate default:
10127c478bd9Sstevel@tonic-gate if (c != scc)
10137c478bd9Sstevel@tonic-gate return (0);
10147c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/
10157c478bd9Sstevel@tonic-gate
10167c478bd9Sstevel@tonic-gate case '?':
10177c478bd9Sstevel@tonic-gate return (scc ? gmatch(s, p) : 0);
10187c478bd9Sstevel@tonic-gate
10197c478bd9Sstevel@tonic-gate case '*':
10207c478bd9Sstevel@tonic-gate if (*p == 0)
10217c478bd9Sstevel@tonic-gate return (1);
10227c478bd9Sstevel@tonic-gate s--;
10237c478bd9Sstevel@tonic-gate while (*s) {
10247c478bd9Sstevel@tonic-gate if (gmatch(s++, p))
10257c478bd9Sstevel@tonic-gate return (1);
10267c478bd9Sstevel@tonic-gate }
10277c478bd9Sstevel@tonic-gate return (0);
10287c478bd9Sstevel@tonic-gate
10297c478bd9Sstevel@tonic-gate case 0:
10307c478bd9Sstevel@tonic-gate return (scc == 0);
10317c478bd9Sstevel@tonic-gate }
10327c478bd9Sstevel@tonic-gate }
10337c478bd9Sstevel@tonic-gate
10347c478bd9Sstevel@tonic-gate /*
10357c478bd9Sstevel@tonic-gate * Construct a matched name.
10367c478bd9Sstevel@tonic-gate */
10377c478bd9Sstevel@tonic-gate static int
1038*fe0e7ec4Smaheshvs addg(struct direct *dp, char *as1, char *as3, struct arglist *ap)
10397c478bd9Sstevel@tonic-gate {
10407c478bd9Sstevel@tonic-gate char *s1, *s2, *limit;
10417c478bd9Sstevel@tonic-gate int c;
10427c478bd9Sstevel@tonic-gate char buf[MAXPATHLEN + 1];
10437c478bd9Sstevel@tonic-gate
10447c478bd9Sstevel@tonic-gate s2 = buf;
10457c478bd9Sstevel@tonic-gate limit = buf + sizeof (buf) - 1;
10467c478bd9Sstevel@tonic-gate s1 = as1;
10477c478bd9Sstevel@tonic-gate while ((c = *s1++) != '\0' && s2 < limit) {
10487c478bd9Sstevel@tonic-gate if (c == DELIMCHAR) {
10497c478bd9Sstevel@tonic-gate *s2++ = '/';
10507c478bd9Sstevel@tonic-gate break;
10517c478bd9Sstevel@tonic-gate }
10527c478bd9Sstevel@tonic-gate /* LINTED narrowing cast */
10537c478bd9Sstevel@tonic-gate *s2++ = (char)c;
10547c478bd9Sstevel@tonic-gate }
10557c478bd9Sstevel@tonic-gate s1 = dp->d_name;
10567c478bd9Sstevel@tonic-gate while ((*s2 = *s1++) != '\0' && s2 < limit)
10577c478bd9Sstevel@tonic-gate s2++;
10587c478bd9Sstevel@tonic-gate s1 = as3;
10597c478bd9Sstevel@tonic-gate if (s1 != NULL && s2 < limit) {
10607c478bd9Sstevel@tonic-gate *s2++ = '/';
10617c478bd9Sstevel@tonic-gate
10627c478bd9Sstevel@tonic-gate while ((*s2++ = *++s1) != '\0' && s2 < limit) {
10637c478bd9Sstevel@tonic-gate continue;
10647c478bd9Sstevel@tonic-gate /*LINTED [empty loop body]*/
10657c478bd9Sstevel@tonic-gate }
10667c478bd9Sstevel@tonic-gate }
10677c478bd9Sstevel@tonic-gate *s2 = '\0';
10687c478bd9Sstevel@tonic-gate if (mkentry(buf, dp->d_ino, ap) == FAIL)
10697c478bd9Sstevel@tonic-gate return (-1);
10707c478bd9Sstevel@tonic-gate return (0);
10717c478bd9Sstevel@tonic-gate }
10727c478bd9Sstevel@tonic-gate
10737c478bd9Sstevel@tonic-gate
10747c478bd9Sstevel@tonic-gate /*
10757c478bd9Sstevel@tonic-gate * Resolve a "complex" pathname (as generated by myname()) into
10767c478bd9Sstevel@tonic-gate * a file descriptor and a relative path. The file descriptor
10777c478bd9Sstevel@tonic-gate * will reference the hidden directory containing the attribute
10787c478bd9Sstevel@tonic-gate * named by the relative path. If the provided path is not
10797c478bd9Sstevel@tonic-gate * complex, the returned file descriptor will be AT_FDCWD and rpath
10807c478bd9Sstevel@tonic-gate * will equal path.
10817c478bd9Sstevel@tonic-gate *
10827c478bd9Sstevel@tonic-gate * This function is intended to be used to transform a complex
10837c478bd9Sstevel@tonic-gate * pathname into a pair of handles that can be used to actually
10847c478bd9Sstevel@tonic-gate * manipulate the named file. Since extended attributes have
10857c478bd9Sstevel@tonic-gate * an independant name space, a file descriptor for a directory
10867c478bd9Sstevel@tonic-gate * in the attribute name space is necessary to actually manipulate
10877c478bd9Sstevel@tonic-gate * the attribute file (via the path-relative xxxat() system calls
10887c478bd9Sstevel@tonic-gate * or a call to fchdir()).
10897c478bd9Sstevel@tonic-gate *
10907c478bd9Sstevel@tonic-gate * In the event of an error, the returned file descriptor will be
10917c478bd9Sstevel@tonic-gate * -1. It is expected that callers will either check for this
10927c478bd9Sstevel@tonic-gate * condition directly, or attempt to use the descriptor, fail, and
10937c478bd9Sstevel@tonic-gate * generate an appropriate context-specific error message.
10947c478bd9Sstevel@tonic-gate *
10957c478bd9Sstevel@tonic-gate * This function is pretty much a no-op for "simple" (non-attribute)
10967c478bd9Sstevel@tonic-gate * paths.
10977c478bd9Sstevel@tonic-gate */
10987c478bd9Sstevel@tonic-gate void
1099*fe0e7ec4Smaheshvs resolve(char *path, int *fd, char **rpath)
11007c478bd9Sstevel@tonic-gate {
11017c478bd9Sstevel@tonic-gate int tfd;
11027c478bd9Sstevel@tonic-gate
11037c478bd9Sstevel@tonic-gate *fd = tfd = AT_FDCWD;
11047c478bd9Sstevel@tonic-gate *rpath = path;
11057c478bd9Sstevel@tonic-gate path = *rpath + strlen(*rpath) +1;
11067c478bd9Sstevel@tonic-gate while (*path != '\0' &&
11077c478bd9Sstevel@tonic-gate (*fd = openat64(tfd, *rpath, O_RDONLY)) > 0) {
11087c478bd9Sstevel@tonic-gate if (tfd != AT_FDCWD) (void) close(tfd);
11097c478bd9Sstevel@tonic-gate tfd = *fd;
11107c478bd9Sstevel@tonic-gate *rpath = path;
11117c478bd9Sstevel@tonic-gate path = *rpath + strlen(*rpath) +1;
11127c478bd9Sstevel@tonic-gate }
11137c478bd9Sstevel@tonic-gate if (*fd == AT_FDCWD)
11147c478bd9Sstevel@tonic-gate return;
11157c478bd9Sstevel@tonic-gate if (*fd < 0 || (*fd = openat64(tfd, ".", O_RDONLY|O_XATTR)) < 0) {
11167c478bd9Sstevel@tonic-gate int saverr = errno;
11177c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
11187c478bd9Sstevel@tonic-gate gettext("Warning: cannot fully resolve %s: %s"),
11197c478bd9Sstevel@tonic-gate path, strerror(saverr));
11207c478bd9Sstevel@tonic-gate (void) fflush(stderr);
11217c478bd9Sstevel@tonic-gate }
11227c478bd9Sstevel@tonic-gate if (tfd != AT_FDCWD) (void) close(tfd);
11237c478bd9Sstevel@tonic-gate }
11247c478bd9Sstevel@tonic-gate
11257c478bd9Sstevel@tonic-gate /*
11267c478bd9Sstevel@tonic-gate * Copy a complex pathname to another string. Note that the
11277c478bd9Sstevel@tonic-gate * length returned by this function is the number of characters
11287c478bd9Sstevel@tonic-gate * up to (but not including) the final NULL.
11297c478bd9Sstevel@tonic-gate */
11307c478bd9Sstevel@tonic-gate int
1131*fe0e7ec4Smaheshvs complexcpy(char *s1, char *s2, int max)
11327c478bd9Sstevel@tonic-gate {
11337c478bd9Sstevel@tonic-gate int nullseen = 0;
11347c478bd9Sstevel@tonic-gate int len = 0;
11357c478bd9Sstevel@tonic-gate
11367c478bd9Sstevel@tonic-gate while (len++ < max) {
11377c478bd9Sstevel@tonic-gate *s1++ = *s2;
11387c478bd9Sstevel@tonic-gate if (*s2++ == '\0') {
11397c478bd9Sstevel@tonic-gate if (nullseen)
11407c478bd9Sstevel@tonic-gate return (len-1);
11417c478bd9Sstevel@tonic-gate else
11427c478bd9Sstevel@tonic-gate nullseen = 1;
11437c478bd9Sstevel@tonic-gate } else {
11447c478bd9Sstevel@tonic-gate nullseen = 0;
11457c478bd9Sstevel@tonic-gate }
11467c478bd9Sstevel@tonic-gate }
11477c478bd9Sstevel@tonic-gate *s1 = '\0';
11487c478bd9Sstevel@tonic-gate if (nullseen == 0)
11497c478bd9Sstevel@tonic-gate *--s1 = '\0';
11507c478bd9Sstevel@tonic-gate fprintf(stderr,
11517c478bd9Sstevel@tonic-gate gettext("Warning: unterminated source string in complexcpy\n"));
11527c478bd9Sstevel@tonic-gate return (max-1);
11537c478bd9Sstevel@tonic-gate }
1154