2014-05-13 13:34:30 -04:00
|
|
|
package symlink
|
2013-11-07 15:34:01 -05:00
|
|
|
|
|
|
|
import (
|
2013-12-11 20:12:53 -05:00
|
|
|
"fmt"
|
2013-11-07 15:34:01 -05:00
|
|
|
"os"
|
2014-05-15 18:25:38 -04:00
|
|
|
"path"
|
2013-11-07 15:34:01 -05:00
|
|
|
"path/filepath"
|
2013-12-11 19:31:02 -05:00
|
|
|
"strings"
|
2013-11-07 15:34:01 -05:00
|
|
|
)
|
|
|
|
|
2014-05-15 18:25:38 -04:00
|
|
|
const maxLoopCounter = 100
|
|
|
|
|
2013-12-11 19:31:02 -05:00
|
|
|
// FollowSymlink will follow an existing link and scope it to the root
|
|
|
|
// path provided.
|
2014-10-28 17:18:45 -04:00
|
|
|
// The role of this function is to return an absolute path in the root
|
|
|
|
// or normalize to the root if the symlink leads to a path which is
|
|
|
|
// outside of the root.
|
|
|
|
// Errors encountered while attempting to follow the symlink in path
|
|
|
|
// will be reported.
|
|
|
|
// Normalizations to the root don't constitute errors.
|
2013-12-11 19:31:02 -05:00
|
|
|
func FollowSymlinkInScope(link, root string) (string, error) {
|
|
|
|
root, err := filepath.Abs(root)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-12-11 20:12:53 -05:00
|
|
|
|
|
|
|
link, err = filepath.Abs(link)
|
2013-12-11 19:31:02 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-12-11 20:12:53 -05:00
|
|
|
|
2014-06-24 12:50:44 -04:00
|
|
|
if link == root {
|
|
|
|
return root, nil
|
|
|
|
}
|
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
if !strings.HasPrefix(filepath.Dir(link), root) {
|
2014-11-19 11:27:34 -05:00
|
|
|
return "", fmt.Errorf("%s is not within %s", link, root)
|
2013-12-11 20:12:53 -05:00
|
|
|
}
|
2013-12-11 19:31:02 -05:00
|
|
|
|
2014-06-24 12:50:44 -04:00
|
|
|
prev := "/"
|
2014-06-24 12:53:53 -04:00
|
|
|
|
2013-12-11 19:31:02 -05:00
|
|
|
for _, p := range strings.Split(link, "/") {
|
|
|
|
prev = filepath.Join(prev, p)
|
|
|
|
|
2014-05-15 18:25:38 -04:00
|
|
|
loopCounter := 0
|
2013-12-11 20:12:53 -05:00
|
|
|
for {
|
2014-05-15 18:25:38 -04:00
|
|
|
loopCounter++
|
|
|
|
|
|
|
|
if loopCounter >= maxLoopCounter {
|
|
|
|
return "", fmt.Errorf("loopCounter reached MAX: %v", loopCounter)
|
|
|
|
}
|
|
|
|
|
2014-04-10 18:47:53 -04:00
|
|
|
if !strings.HasPrefix(prev, root) {
|
|
|
|
// Don't resolve symlinks outside of root. For example,
|
|
|
|
// we don't have to check /home in the below.
|
|
|
|
//
|
|
|
|
// /home -> usr/home
|
|
|
|
// FollowSymlinkInScope("/home/bob/foo/bar", "/home/bob/foo")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
stat, err := os.Lstat(prev)
|
2013-12-11 19:31:02 -05:00
|
|
|
if err != nil {
|
2013-12-11 20:12:53 -05:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
break
|
|
|
|
}
|
2013-12-11 19:31:02 -05:00
|
|
|
return "", err
|
|
|
|
}
|
2013-12-11 20:12:53 -05:00
|
|
|
|
2014-10-28 17:18:45 -04:00
|
|
|
// let's break if we're not dealing with a symlink
|
|
|
|
if stat.Mode()&os.ModeSymlink != os.ModeSymlink {
|
|
|
|
break
|
|
|
|
}
|
2013-12-11 19:31:02 -05:00
|
|
|
|
2014-10-28 17:18:45 -04:00
|
|
|
// process the symlink
|
|
|
|
dest, err := os.Readlink(prev)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if path.IsAbs(dest) {
|
|
|
|
prev = filepath.Join(root, dest)
|
2013-12-11 20:12:53 -05:00
|
|
|
} else {
|
2014-10-28 17:18:45 -04:00
|
|
|
prev, _ = filepath.Abs(prev)
|
|
|
|
|
|
|
|
dir := filepath.Dir(prev)
|
|
|
|
prev = filepath.Join(dir, dest)
|
|
|
|
if dir == root && !strings.HasPrefix(prev, root) {
|
|
|
|
prev = root
|
|
|
|
}
|
|
|
|
if len(prev) < len(root) || (len(prev) == len(root) && prev != root) {
|
|
|
|
prev = filepath.Join(root, filepath.Base(dest))
|
|
|
|
}
|
2013-12-11 19:31:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-28 17:18:45 -04:00
|
|
|
if prev == "/" {
|
|
|
|
prev = root
|
|
|
|
}
|
2013-12-11 19:31:02 -05:00
|
|
|
return prev, nil
|
|
|
|
}
|