add initial support for SCGI

Signed-off-by: kim (grufwub) <grufwub@gmail.com>
development
kim (grufwub) 4 years ago
parent e36f1ecdb9
commit 145f54b85d

@ -1,40 +1,86 @@
package core
import (
"net"
"os/exec"
"strconv"
"syscall"
"time"
"github.com/grufwub/go-errors"
)
type envVar struct {
key string
value string
}
func newEnvVar(key, value string) *envVar {
return &envVar{key, value}
}
func (v *envVar) String() string {
return v.key + "=" + v.value
}
// getDefaultEnv gets the default (not typed for CGI or SCGI) environment to execute
// under, as a slice of envVar that can easily be used for either
func getDefaultEnv() []*envVar {
env := make([]*envVar, 0)
env = append(env, newEnvVar("SERVER_SOFTWARE", "Gophi "+Version))
env = append(env, newEnvVar("SERVER_PROTOCOL", protocol))
env = append(env, newEnvVar("SERVER_NAME", Hostname))
env = append(env, newEnvVar("SERVER_PORT", FwdPort))
env = append(env, newEnvVar("DOCUMENT_ROOT", Root))
return env
}
// setupInitialCGIEnv takes a safe PATH, uses other server variables and returns a slice of constant CGI environment variables
func setupInitialCGIEnv(safePath string) []string {
env := make([]string, 0)
SystemLog.Infof(cgiPathStr, safePath)
env = append(env, "GATEWAY_INTERFACE=CGI/1.1")
env = append(env, "SERVER_SOFTWARE=Gophi "+Version)
env = append(env, "SERVER_PROTOCOL="+protocol)
env = append(env, "REQUEST_METHOD=GET") // always GET (in HTTP terms anywho)
env = append(env, "CONTENT_LENGTH=0") // always 0
env = append(env, "PATH="+safePath)
env = append(env, "SERVER_NAME="+Hostname)
env = append(env, "SERVER_PORT="+FwdPort)
env = append(env, "DOCUMENT_ROOT="+Root)
// Append the default environment
env := getDefaultEnv()
env = append(env, newEnvVar("GATEWAY_INTERFACE", "CGI/1.1"))
env = append(env, newEnvVar("PATH", safePath))
// Convert to a string slice
envStrs := make([]string, len(env))
for i := range env {
envStrs[i] = env[i].String()
}
// Return string slice of environment variables
return envStrs
}
// setupInitialSCGIEnv uses server variables and returns a slice of constant SCGI environment variables
func setupInitialSCGIEnv() []*envVar {
// Append the default environment
env := getDefaultEnv()
env = append(env, newEnvVar("SCGI", "1"))
// Return this
return env
}
// generateCGIEnv takes a Client, and Request object, the global constant slice and generates a full set of CGI environment variables
// generateCGIEnv takes a Client, and Request object, uses the global constant slice and generates a full set of CGI environment variables
func generateCGIEnv(client *Client, request *Request) []string {
env := append(cgiEnv, "REMOTE_ADDR="+client.IP())
env = append(env, "QUERY_STRING="+request.Params())
env = append(env, "SCRIPT_NAME="+request.Path().Relative())
env = append(env, "SCRIPT_FILENAME="+request.Path().Absolute())
env = append(env, "SELECTOR="+request.Path().Selector())
env = append(env, "REQUEST_URI="+request.Path().Selector())
return env
}
// generateSCGIEnv takes a Client, and Request object, uses the global constant slice and generates a full set of SCGI environment variables
func generateSCGIEnv(client *Client, request *Request) []*envVar {
env := append(scgiEnv, newEnvVar("REMOTE_ADDR", client.IP()))
env = append(env, newEnvVar("QUERY_STRING", request.Params()))
env = append(env, newEnvVar("SCRIPT_NAME", request.Path().Relative()))
env = append(env, newEnvVar("SCRIPT_FILENAME", request.Path().Absolute()))
env = append(env, newEnvVar("REQUEST_URI", request.Path().Selector()))
return env
}
@ -112,3 +158,48 @@ func ExecuteCGIScript(client *Client, request *Request) *errors.Error {
// Exit fine!
return nil
}
// ExecuteSCGI sends an SCGI env request to request SCGI socket
func ExecuteSCGI(client *Client, request *Request) *errors.Error {
// SCGI socket is stored as request absolute path
scgiSocket := request.Path().Absolute()
// Connect to SCGI socket, defer close
conn, err := net.Dial("unix", scgiSocket)
if err != nil {
SystemLog.Error("SCGI socket dial error")
return errors.WrapError(SCGIDialErr, err)
}
defer conn.Close()
// Get SCGI variables + calculate total length
env := generateSCGIEnv(client, request)
envBytes := []byte{}
length := 0
for _, v := range env {
// Add the key + zero byte padding, calculate length
envBytes = append(envBytes, []byte(v.key+"\x00")...)
length += len(v.key) + 1
// Add the value + zero byte padding, calculate length
envBytes = append(envBytes, []byte(v.value+"\x00")...)
length += len(v.value) + 1
}
// Prepend envBytes with expected length, then append ','
envBytes = append([]byte(strconv.Itoa(length)+":"), envBytes...)
envBytes = append(envBytes, ',')
// Send SCGI environment
_, err = conn.Write(envBytes)
if err != nil {
SystemLog.Error("SCGI socket write error")
return errors.WrapError(SCGIWriteErr, err)
}
// Wrap conn in BufferedReader, defer putting back
br := scgiBufferedReaderPool.Get(conn)
defer scgiBufferedReaderPool.Put(br)
// Connect client directly to socket output
return client.Conn().ReadFrom(br)
}

@ -44,6 +44,8 @@ func ParseFlagsAndSetup(proto string) {
hiddenPathsList := flag.String(hiddenPathsFlagStr, "", hiddenPathsDescStr)
remapRequestsList := flag.String(remapRequestsFlagStr, "", remapRequestsDescStr)
cgiDir := flag.String(cgiDirFlagStr, "", cgiDirDescStr)
scgiPathsList := flag.String(scgiPathsFlagStr, "", scgiPathsDescStr)
scgiReadBuf := flag.Uint(scgiReadBufFlagStr, 1024, scgiReadBufDescStr)
flag.DurationVar(&maxCGIRunTime, maxCGITimeFlagStr, time.Duration(time.Second*3), maxCGITimeDescStr)
safePath := flag.String(safePathFlagStr, "/bin:/usr/bin", safePathDescStr)
flag.StringVar(&userDir, userDirFlagStr, "", userDirDescStr)
@ -151,6 +153,18 @@ func ParseFlagsAndSetup(proto string) {
WithinCGIDir = withinCGIDirEnabled
}
// If no SCGI paths provided, set to disabled function. Else, compile and enable
if *scgiPathsList == "" {
SystemLog.Info(scgiSupportDisabledStr)
MapSCGIRequest = mapSCGIRequestDisabled
} else {
SystemLog.Info(scgiSupportEnabledStr)
scgiMaps = compileSCGIMapRegex(*scgiPathsList)
scgiEnv = setupInitialSCGIEnv()
MapSCGIRequest = mapSCGIRequestEnabled
scgiBufferedReaderPool = bufpools.NewBufferedReaderPool(int(*scgiReadBuf))
}
// If no user dir supplied, set to disabled function. Else, set user dir and enable
if userDir == "" {
SystemLog.Info(userDirDisabledStr)

@ -23,4 +23,6 @@ var (
InvalidRequestErr = errors.NewBaseError(invalidRequestErrStr)
CGIStartErr = errors.NewBaseError(cgiStartErrStr)
CGIExitCodeErr = errors.NewBaseError(cgiExitCodeErrStr)
SCGIDialErr = errors.NewBaseError(scgiDialErrStr)
SCGIWriteErr = errors.NewBaseError(scgiWriteErrStr)
)

@ -27,6 +27,12 @@ func (p *Path) Remap(newRel string) {
p.rel = sanitizeRawPath(p.root, newRel)
}
// RemapDirect remaps a Path to a new absolute path, keeping previous selector
func (p *Path) RemapDirect(newAbs string) {
p.root = newAbs
p.rel = ""
}
// Root returns file's root directory
func (p *Path) Root() string {
return p.root

@ -78,7 +78,7 @@ func compileHiddenPathsRegex(hidden string) []*regexp.Regexp {
return regexes
}
// compil RequestRemapRegex turns a string of remapped paths into a slice of compiled RequestRemap structures
// compileRequestRemapRegex turns a string of remapped paths into a slice of compiled RequestRemap structures
func compileRequestRemapRegex(remaps string) []*RequestRemap {
requestRemaps := make([]*RequestRemap, 0)
@ -109,6 +109,37 @@ func compileRequestRemapRegex(remaps string) []*RequestRemap {
return requestRemaps
}
// compileSCGIMapRegex turns a string of mapped paths to SCGI into a slice of compiled RequestRemap structures
func compileSCGIMapRegex(maps string) []*RequestRemap {
requestMaps := make([]*RequestRemap, 0)
// Split maps string by new lines
for _, expr := range strings.Split(maps, "\n") {
// Skip empty expressions
if len(expr) == 0 {
continue
}
// Split into alias and remap
split := strings.Split(expr, requestRemapSeparatorStr)
if len(split) != 2 {
SystemLog.Fatalf(scgiMapRegexInvalidStr, expr)
}
// Compile the regular expression
regex, err := regexp.Compile("(?m)" + strings.TrimPrefix(split[0], "/") + "$")
if err != nil {
SystemLog.Fatalf(scgiMapRegexCompileFailedStr, expr)
}
// Append RequestRemap and log
requestMaps = append(requestMaps, &RequestRemap{regex, split[1]})
SystemLog.Infof(scgiMapRegexCompiledStr, expr)
}
return requestMaps
}
// withinCGIDirEnabled returns whether a Path's absolute value matches within the CGI dir
func withinCGIDirEnabled(p *Path) bool {
return cgiDirRegex.MatchString(p.Absolute())
@ -177,3 +208,32 @@ func remapRequestEnabled(request *Request) bool {
func remapRequestDisabled(request *Request) bool {
return false
}
// mapSCGIRequestEnabled tries to map a request to SCGI path, returning a bool as to success
func mapSCGIRequestEnabled(request *Request) bool {
for _, remap := range scgiMaps {
// No match, gotta keep looking
if !remap.Regex.MatchString(request.Path().Selector()) {
continue
}
// Create new request from template and submatches
raw := make([]byte, 0)
for _, submatches := range remap.Regex.FindAllStringSubmatchIndex(request.Path().Selector(), -1) {
raw = remap.Regex.ExpandString(raw, remap.Template, request.Path().Selector(), submatches)
}
// Split to new path and parameters again
path, params := SplitBy(string(raw), "?")
// Remap request, log and return
request.RemapDirect(path, params)
return true
}
return false
}
// mapSCGIRequestDisabled always returns false, there are no mapped SCGI paths
func mapSCGIRequestDisabled(request *Request) bool {
return false
}

@ -32,3 +32,11 @@ func (r *Request) Remap(rel, params string) {
}
r.p.Remap(rel)
}
// RemapDirect modifies a request to use new absolute path, and accomodate supplied extra parameters
func (r *Request) RemapDirect(abs, params string) {
if len(r.params) > 0 {
r.params = params + "&" + r.params
}
r.p.RemapDirect(abs)
}

@ -65,6 +65,7 @@ var (
// Buffer pools
connBufferedReaderPool *bufpools.BufferedReaderPool
connBufferedWriterPool *bufpools.BufferedWriterPool
scgiBufferedReaderPool *bufpools.BufferedReaderPool
fileBufferedReaderPool *bufpools.BufferedReaderPool
fileBufferPool *bufpools.BufferPool
@ -73,6 +74,9 @@ var (
// cgiEnv holds the global slice of constant CGI environment variables
cgiEnv []string
// scgiEnv holds the global slice of constant SCGI environment variables
scgiEnv []*envVar
// maxCGIRunTime specifies the maximum time a CGI script can run for
maxCGIRunTime time.Duration
@ -105,6 +109,12 @@ var (
// RemapRequest is the global function to remap a request
RemapRequest func(*Request) bool
// scgiMaps is the global slice of mapped SCGI paths
scgiMaps []*RequestRemap
// MapSCGIRequest is the global function to map a request to SCGI
MapSCGIRequest func(*Request) bool
)
// Start begins operation of the server
@ -143,9 +153,11 @@ func HandleClient(client *Client, request *Request, newFileContents func(*Path)
return errors.NewError(RestrictedPathErr)
}
// Try remap request, log if so
ok := RemapRequest(request)
if ok {
// If SCGI path map to socket, else try remap request
if ok := MapSCGIRequest(request); ok {
client.LogInfo(scgiRequestMappedStr, request.Path().Selector(), request.Params())
return ExecuteSCGI(client, request)
} else if ok := RemapRequest(request); ok {
client.LogInfo(requestRemappedStr, request.Path().Selector(), request.Params())
}

@ -62,6 +62,12 @@ const (
remapRequestsFlagStr = "remap-requests"
remapRequestsDescStr = "Remap requests as new-line separated list of remap statements (see documenation)"
scgiPathsFlagStr = "scgi-maps"
scgiPathsDescStr = "SCGI request maps (to local SCGI socket) as new-line separated list of map statements (see documentation)"
scgiReadBufFlagStr = "scgi-read-buf"
scgiReadBufDescStr = "SCGI connection read buffer size (bytes)"
cgiDirFlagStr = "cgi-dir"
cgiDirDescStr = "CGI scripts directory (empty to disable)"
@ -108,6 +114,13 @@ const (
requestRemapRegexCompiledStr = "Compiled path remap regex: %s"
requestRemappedStr = "Remapped request: %s %s"
scgiSupportEnabledStr = "SCGI support enabled"
scgiSupportDisabledStr = "SCGI support disabled"
scgiMapRegexInvalidStr = "Invalid SCGI request map regex: %s"
scgiMapRegexCompileFailedStr = "Failed compiling SCGI request map regex: %s"
scgiMapRegexCompiledStr = "Compiled SCGI path map regex: %s"
scgiRequestMappedStr = "SCGI mapped request: %s %s"
cgiPathStr = "CGI safe path: %s"
cgiSupportEnabledStr = "CGI script support enabled"
cgiSupportDisabledStr = "CGI script support disabled"
@ -146,4 +159,6 @@ const (
invalidRequestErrStr = "Invalid request"
cgiStartErrStr = "CGI start error"
cgiExitCodeErrStr = "CGI non-zero exit code"
scgiDialErrStr = "SCGI socket dial error"
scgiWriteErrStr = "SCGI write error"
)

Loading…
Cancel
Save