Initial commit
This commit is contained in:
91
src/main/phpselector.go
Normal file
91
src/main/phpselector.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/user"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
const (
|
||||
PATH_TO_USER_CONFIG = "/usr/local/hestia/data/users/%s/user.conf"
|
||||
)
|
||||
|
||||
func isExecOther(mode os.FileMode) bool {
|
||||
return mode&0001 != 0
|
||||
}
|
||||
|
||||
func getPHPVerFromConf(path string) (string, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("user config reading error %s", err)
|
||||
}
|
||||
defer f.Close()
|
||||
scanner := bufio.NewScanner(f)
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
formatedLine := strings.SplitN(strings.TrimSpace(line), "=", 2)
|
||||
if len(formatedLine) < 2 {
|
||||
return "", fmt.Errorf("incorrect string formatting in config %s", line)
|
||||
}
|
||||
if strings.TrimSpace(formatedLine[0]) == "PHPCLI" {
|
||||
result := strings.Trim(formatedLine[1], "' ")
|
||||
return result, nil
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return "", fmt.Errorf("user config reading error %s", err)
|
||||
}
|
||||
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func getUserPHPVer() (string, error) {
|
||||
user, err := user.Current()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("user information error %s", err)
|
||||
}
|
||||
pathToConf := fmt.Sprintf(PATH_TO_USER_CONFIG, user.Username)
|
||||
phpVer, err := getPHPVerFromConf(pathToConf)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
phpPath := "/usr/bin/php" + phpVer
|
||||
if finfo, err := os.Stat(phpPath); err != nil {
|
||||
return "", err
|
||||
} else {
|
||||
fmode := finfo.Mode()
|
||||
if !isExecOther(fmode) {
|
||||
return "", fmt.Errorf("not executable by others %s", phpPath)
|
||||
}
|
||||
if strings.Contains(finfo.Name(), "hestiacp-php-selector") {
|
||||
return "", fmt.Errorf("infinite symbolic link")
|
||||
}
|
||||
}
|
||||
return phpPath, nil
|
||||
}
|
||||
|
||||
func inner_main() int {
|
||||
phpPath, err := getUserPHPVer()
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
return 1
|
||||
}
|
||||
if phpPath == "" {
|
||||
fmt.Fprintln(os.Stderr, "no php supported")
|
||||
return 1
|
||||
}
|
||||
err = syscall.Exec(phpPath, os.Args, os.Environ())
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
func main() {
|
||||
os.Exit(inner_main())
|
||||
}
|
||||
Reference in New Issue
Block a user