PageRenderTime 78ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/usr/src/cmd/backup/restore/utilities.c

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