Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package utils
import (
"bufio"
"errors"
"fmt"
"io"
"io/fs"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"syscall"
)
const (
dirMode = 0700
rootUID = 0
maxPathDepth = 20
maxPathLength = 1024
DefaultWriteFileMode = 0022
ldSplitLen = 2
ldLibNameIndex = 0
ldLibPathIndex = 1
ldCommand = "/sbin/ldconfig"
ldParam = "--print-cache"
LdLibPath = "LD_LIBRARY_PATH"
grepCommand = "/bin/grep"
)
func IsDir(path string) bool {
if path == "" {
return false
}
if !IsExist(path) {
return path[len(path)-1:] == "/"
}
s, err := os.Stat(path)
if err != nil {
return false
}
return s.IsDir()
}
func IsFile(path string) bool {
if path == "" {
return false
}
return !IsDir(path)
}
func IsSoftlink(path string) (bool, error) {
file, err := os.Open(path)
if err != nil {
return false, err
}
defer file.Close()
fileInfo, err := file.Stat()
if err != nil {
return false, err
}
if (fileInfo.Mode() & fs.ModeSymlink) != 0 {
return true, nil
}
return false, nil
}
func IsExist(filePath string) bool {
_, err := os.Stat(filePath)
if err == nil {
return true
}
if os.IsExist(err) {
return true
}
return false
}
func IsLexist(filePath string) bool {
_, err := os.Lstat(filePath)
if err == nil {
return true
}
if os.IsExist(err) {
return true
}
return false
}
func CheckPath(path string) (string, error) {
if path == "" {
return path, nil
}
origin := path
for !IsLexist(path) {
path = filepath.Dir(path)
if path == "." {
return "", os.ErrNotExist
}
}
absPath, err := filepath.Abs(path)
if err != nil {
return "", fmt.Errorf("get the absolute path failed: %v", err)
}
resoledPath, err := filepath.EvalSymlinks(absPath)
if err != nil {
if strings.Contains(err.Error(), "no such file or directory") {
return "", os.ErrNotExist
}
return "", fmt.Errorf("get the symlinks path failed: %v", err)
}
if absPath != resoledPath {
return "", errors.New("can't support symlinks")
}
absOrigin, err := filepath.Abs(origin)
if err != nil {
return "", fmt.Errorf("get the absolute path failed: %v", err)
}
return absOrigin, nil
}
func MakeSureDir(path string) error {
dir := filepath.Dir(path)
if IsExist(dir) {
return nil
}
if err := os.MkdirAll(dir, dirMode); err != nil {
return fmt.Errorf("create directory failed: %v", err)
}
return nil
}
func CheckMode(mode os.FileMode, optional ...os.FileMode) bool {
var targetMode os.FileMode
if len(optional) > 0 {
targetMode = optional[0]
} else {
targetMode = DefaultWriteFileMode
}
checkMode := uint32(mode) & uint32(targetMode)
return checkMode == 0
}
func CheckOwnerAndPermission(verifyPath string, mode os.FileMode, uid uint32) (string, error) {
if verifyPath == "" {
return verifyPath, errors.New("empty path")
}
absPath, err := filepath.Abs(verifyPath)
if err != nil {
return "", fmt.Errorf("abs failed %v", err)
}
resoledPath, err := filepath.EvalSymlinks(absPath)
if err != nil {
return "", fmt.Errorf("evalSymlinks failed %v", err)
}
if absPath != resoledPath {
pathInfo, err := os.Lstat(absPath)
if err != nil {
return "", fmt.Errorf("lstat failed, %v", err)
}
stat, ok := pathInfo.Sys().(*syscall.Stat_t)
if !ok || stat.Uid != uid {
return "", errors.New("symlinks owner may not root")
}
}
pathInfo, err := os.Stat(resoledPath)
if err != nil {
return "", fmt.Errorf("stat failed %v", err)
}
stat, ok := pathInfo.Sys().(*syscall.Stat_t)
if !ok || stat.Uid != uid || !CheckMode(pathInfo.Mode(), mode) {
return "", errors.New("check uid or mode failed")
}
return resoledPath, nil
}
func DoCheckOwnerAndPermission(path string, mode os.FileMode, uid uint32) error {
if !IsExist(path) {
return nil
}
pathInfo, err := os.Stat(path)
if err != nil {
return fmt.Errorf("stat failed %v", err)
}
stat, ok := pathInfo.Sys().(*syscall.Stat_t)
if !ok || stat.Uid != uid || !CheckMode(pathInfo.Mode(), mode) {
return fmt.Errorf("check uid or mode failed : %v", path)
}
return nil
}
func checkAbsPath(libPath string) (string, error) {
absLibPath, err := CheckOwnerAndPermission(libPath, DefaultWriteFileMode, rootUID)
if err != nil {
return "", fmt.Errorf("%s: %v", libPath, err)
}
count := 0
fPath := absLibPath
for {
if count >= maxPathDepth {
break
}
count++
if fPath == "/" {
return absLibPath, nil
}
fPath = filepath.Dir(fPath)
if _, err := CheckOwnerAndPermission(fPath, DefaultWriteFileMode, rootUID); err != nil {
return "", fmt.Errorf("%s: %v", fPath, err)
}
}
return "", errors.New("absolute path check failed")
}
func checkLibsPath(libraryPaths []string) (string, error) {
errs := make([]string, 0, len(libraryPaths))
for _, libraryAbsName := range libraryPaths {
absLibPath, err := checkAbsPath(libraryAbsName)
if err == nil {
return absLibPath, nil
}
errs = append(errs, fmt.Sprintf("%s;", err.Error()))
}
return "", fmt.Errorf("lib path is invalid, %v", errs)
}
func getLibFromEnv(libraryName string) (string, error) {
ldLibraryPath := os.Getenv(LdLibPath)
if len(ldLibraryPath) > maxPathLength {
return "", fmt.Errorf("invalid library path env")
}
libraryPaths := strings.Split(ldLibraryPath, ":")
targetLibs := make([]string, 0, len(ldLibraryPath))
for _, libraryPath := range libraryPaths {
libraryAbsName := path.Join(libraryPath, libraryName)
if len(libraryAbsName) > maxPathLength || !IsLexist(libraryAbsName) {
continue
}
targetLibs = append(targetLibs, libraryAbsName)
}
if len(libraryPaths) == 0 {
return "", errors.New("file path no exist or too long")
}
return checkLibsPath(targetLibs)
}
func trimSpaceTable(data string) string {
data = strings.Replace(data, " ", "", -1)
data = strings.Replace(data, "\t", "", -1)
data = strings.Replace(data, "\n", "", -1)
return data
}
func parserLibPath(line, libraryName string) string {
ldInfo := strings.Split(line, "=>")
if len(ldInfo) < ldSplitLen {
return ""
}
libNames := strings.Split(ldInfo[ldLibNameIndex], " ")
for index, libName := range libNames {
if index >= maxPathDepth {
break
}
if len(libName) == 0 {
continue
}
if name := trimSpaceTable(libName); name != libraryName {
continue
}
return trimSpaceTable(ldInfo[ldLibPathIndex])
}
return ""
}
func parseLibFromLdCmd(libraryName string) (string, error) {
ldCmd := exec.Command(ldCommand, ldParam)
grepCmd := exec.Command(grepCommand, libraryName)
ldCmdStdout, err := ldCmd.StdoutPipe()
if err != nil {
return "", fmt.Errorf("command exec failed: %v", err)
}
grepCmd.Stdin = ldCmdStdout
stdout, err := grepCmd.StdoutPipe()
if err != nil {
return "", fmt.Errorf("get pipe failed: %v", err)
}
if err = grepCmd.Start(); err != nil {
return "", fmt.Errorf("command exec failed: %v", err)
}
if err = ldCmd.Run(); err != nil {
return "", fmt.Errorf("command exec failed: %v", err)
}
defer func() {
if err = grepCmd.Wait(); err != nil {
log.Printf("command exec failed, %v", err)
}
}()
reader := bufio.NewReader(stdout)
count := 0
line := ""
for {
if count >= maxPathLength {
err = errors.New("too many items in command stdout")
break
}
count++
line, err = reader.ReadString('\n')
if err != nil || io.EOF == err {
break
}
if libPath := parserLibPath(line, libraryName); libPath != "" {
return libPath, nil
}
}
return "", fmt.Errorf("can't find valid lib: %v", err)
}
func getLibFromLdCmd(libraryName string) (string, error) {
libraryAbsName, err := parseLibFromLdCmd(libraryName)
if err != nil {
return "", err
}
var absLibPath string
if absLibPath, err = checkAbsPath(libraryAbsName); err == nil {
return absLibPath, nil
}
return "", fmt.Errorf("driver lib is not exist or it's permission is invalid, %v", err)
}
func GetDriverLibPath(libraryName string) (string, error) {
var libPath string
var envErr, cmdErr error
if libPath, envErr = getLibFromEnv(libraryName); envErr == nil {
return libPath, nil
}
if libPath, cmdErr = getLibFromLdCmd(libraryName); cmdErr == nil {
return libPath, nil
}
return "", fmt.Errorf("cannot found valid driver lib, fromEnv: %v, fromLdCmd: %v", envErr, cmdErr)
}