unsafe_symlink_target.c 1009 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  4. */
  5. #include "libbb.h"
  6. #include "bb_archive.h"
  7. void FAST_FUNC create_or_remember_link(llist_t **link_placeholders,
  8. const char *target,
  9. const char *linkname,
  10. int hard_link)
  11. {
  12. if (hard_link || target[0] == '/' || strstr(target, "..")) {
  13. llist_add_to_end(link_placeholders,
  14. xasprintf("%c%s%c%s", hard_link, linkname, '\0', target)
  15. );
  16. return;
  17. }
  18. if (symlink(target, linkname) != 0) {
  19. /* shared message */
  20. bb_perror_msg_and_die("can't create %slink '%s' to '%s'",
  21. "sym", linkname, target
  22. );
  23. }
  24. }
  25. void FAST_FUNC create_links_from_list(llist_t *list)
  26. {
  27. while (list) {
  28. char *target;
  29. target = list->data + 1 + strlen(list->data + 1) + 1;
  30. if ((*list->data ? link : symlink) (target, list->data + 1)) {
  31. /* shared message */
  32. bb_error_msg_and_die("can't create %slink '%s' to '%s'",
  33. *list->data ? "hard" : "sym",
  34. list->data + 1, target
  35. );
  36. }
  37. list = list->link;
  38. }
  39. }