remove SCGI support (i have opinions...)

Signed-off-by: kim (grufwub) <grufwub@gmail.com>
development
kim (grufwub) 4 years ago
parent d473872bd0
commit 07b5ea1d7c

@ -1,66 +1,26 @@
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 {
// 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()
}
env := make([]string, 12)
env = append(env, "SERVER_SOFTWARE=Gophi "+Version)
env = append(env, "SERVER_PROTOCOL="+protocol)
env = append(env, "SERVER_NAME="+Hostname)
env = append(env, "SERVER_PORT="+FwdPort)
env = append(env, "DOCUMENT_ROOT="+Root)
env = append(env, "GATEWAY_INTERFACE=CGI/1.1")
env = append(env, "PATH="+safePath)
// 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
}
@ -74,14 +34,6 @@ func generateCGIEnv(client *Client, request *Request) []string {
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("REQUEST_URI", request.Path().Selector()))
return env
}
// ExecuteCGIScript executes a CGI script, responding with stdout to client
func ExecuteCGIScript(client *Client, request *Request) *errors.Error {
// Create cmd object
@ -156,52 +108,3 @@ 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 root path
scgiSocket := request.Path().Root()
// Connect to SCGI socket, defer close
conn, err := net.Dial("unix", scgiSocket)
if err != nil {
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 (we don't bother wrapping in buffered
// writer the request env will be of limited size, and we send it
// all at once). Set deadline for this single write
conn.SetWriteDeadline(time.Now().Add(scgiWriteDeadline))
_, err = conn.Write(envBytes)
if err != nil {
return errors.WrapError(SCGIWriteErr, err)
}
// Wrap conn in BufferedReader, defer putting back
br := scgiBufferedReaderPool.Get(conn)
defer scgiBufferedReaderPool.Put(br)
// Set conn read deadline
conn.SetReadDeadline(time.Now().Add(scgiReadDeadline))
// Connect client directly to socket output
return client.Conn().ReadFrom(br)
}

@ -44,10 +44,6 @@ 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(&scgiReadDeadline, scgiReadTimeoutFlagStr, time.Duration(time.Second*1), scgiReadTimeoutDescStr)
flag.DurationVar(&scgiWriteDeadline, scgiWriteTimeoutFlagStr, time.Duration(time.Second*3), scgiWriteTimeoutDescStr)
flag.DurationVar(&maxCGIRunTime, maxCGITimeFlagStr, time.Duration(time.Second*3), maxCGITimeDescStr)
safePath := flag.String(safePathFlagStr, "/bin:/usr/bin", safePathDescStr)
flag.StringVar(&userDir, userDirFlagStr, "", userDirDescStr)
@ -155,18 +151,6 @@ 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,6 +23,4 @@ var (
InvalidRequestErr = errors.NewBaseError(invalidRequestErrStr)
CGIStartErr = errors.NewBaseError(cgiStartErrStr)
CGIExitCodeErr = errors.NewBaseError(cgiExitCodeErrStr)
SCGIDialErr = errors.NewBaseError(scgiDialErrStr)
SCGIWriteErr = errors.NewBaseError(scgiWriteErrStr)
)

@ -109,37 +109,6 @@ 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())
@ -208,32 +177,3 @@ 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
}

@ -61,18 +61,12 @@ var (
cgiEnv []string
maxCGIRunTime time.Duration
// SCGI related globals
scgiEnv []*envVar
scgiReadDeadline time.Duration
scgiWriteDeadline time.Duration
// Global OS signal channel
sigChannel chan os.Signal
// Buffer pools
connBufferedReaderPool *bufpools.BufferedReaderPool
connBufferedWriterPool *bufpools.BufferedWriterPool
scgiBufferedReaderPool *bufpools.BufferedReaderPool
fileBufferedReaderPool *bufpools.BufferedReaderPool
fileBufferPool *bufpools.BufferPool
@ -81,7 +75,6 @@ var (
restrictedPaths []*regexp.Regexp
hiddenPaths []*regexp.Regexp
requestRemaps []*RequestRemap
scgiMaps []*RequestRemap
// WithinCGIDir returns whether a path is within the server's specified CGI scripts directory
WithinCGIDir func(*Path) bool
@ -135,11 +128,8 @@ func HandleClient(client *Client, request *Request, newFileContents func(*Path)
return errors.NewError(RestrictedPathErr)
}
// 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 {
// Try remap if necessary. If remapped, log!
if ok := RemapRequest(request); ok {
client.LogInfo(requestRemappedStr, request.Path().Selector(), request.Params())
}

@ -62,18 +62,6 @@ 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 unix SCGI socket) as new-line separated list of map statements (see documentation)"
scgiReadBufFlagStr = "scgi-read-buf"
scgiReadBufDescStr = "SCGI connection read buffer size (bytes)"
scgiReadTimeoutFlagStr = "scgi-read-timeout"
scgiReadTimeoutDescStr = "SCGI connection read timeout"
scgiWriteTimeoutFlagStr = "scgi-write-timeout"
scgiWriteTimeoutDescStr = "SCGI connection write timeout"
cgiDirFlagStr = "cgi-dir"
cgiDirDescStr = "CGI scripts directory (empty to disable)"
@ -120,13 +108,6 @@ 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"
@ -165,6 +146,4 @@ const (
invalidRequestErrStr = "Invalid request"
cgiStartErrStr = "CGI start error"
cgiExitCodeErrStr = "CGI non-zero exit code"
scgiDialErrStr = "SCGI socket dial error"
scgiWriteErrStr = "SCGI write error"
)

@ -90,29 +90,6 @@ e.g. scripts within cgi-bin to the root directory:
Entries are parsed, compiled, and so matched-against in order. They are matched against
relative paths so please bear this in mind if you have user directories enabled.
# SCGI Request Mapping Regex
Request mapping is parsed as a new-line separated list of remap statements of form:
`/regex/matched/against -> /var/run/gopher_scgi.sock`
Internally, the request remapping statements are parsed as (shown as Python code):
`/match_statement -> /template_statement.sock`
```Python
# Where the match statement is compiled using
regex = "(?m)" + "/match_statement".removeprefix("/") + "$"
# And the template is stored as
template = "/template_statement.sock"
```
e.g. everything under scgi to the SCGI socket:
`/scgi/(?P<uri>[^/]+) -> /var/run/gopher_scgi.sock`
# User server spaces
When user server spaces are enabled, they are accessed via the following gopher URL:

Loading…
Cancel
Save