add support for disabling CGI support

Signed-off-by: kim (grufwub) <grufwub@gmail.com>
master
kim (grufwub) 4 years ago
parent 9f32233d28
commit 4ea4280cd0

@ -14,6 +14,7 @@ type ServerConfig struct {
/* Executable Settings */
Env []string
CgiEnv []string
CgiEnabled bool
/* Content settings */
CharSet string

@ -30,6 +30,7 @@ const (
BufferReadErr ErrorCode = iota
CommandStartErr ErrorCode = iota
CommandExitCodeErr ErrorCode = iota
CgiDisabledErr ErrorCode = iota
/* Error Response Codes */
ErrorResponse200 ErrorResponseCode = iota
@ -88,6 +89,8 @@ func (e *GophorError) Error() string {
str = "command start fail"
case CommandExitCodeErr:
str = "command exit code non-zero"
case CgiDisabledErr:
str = "ignoring /cgi-bin request, CGI disabled"
default:
str = "Unknown"
@ -138,6 +141,8 @@ func gophorErrorToResponseCode(code ErrorCode) ErrorResponseCode {
return ErrorResponse500
case CommandExitCodeErr:
return ErrorResponse500
case CgiDisabledErr:
return ErrorResponse404
default:
return ErrorResponse503

@ -192,8 +192,10 @@ func readGophermap(request *FileSystemRequest) ([]GophermapSection, *GophorError
subRequest := parseLineRequestString(request, line[1:])
if !subRequest.HasAbsPathPrefix("/") {
/* Special case here where command must be in path, return GophermapExecCommand */
sections = append(sections, &GophermapExecCommand{ subRequest })
if Config.CgiEnabled {
/* Special case here where command must be in path, return GophermapExecCommand */
sections = append(sections, &GophermapExecCommand{ subRequest })
}
} else if subRequest.RelPath() == "" {
/* path cleaning failed */
break
@ -212,7 +214,7 @@ func readGophermap(request *FileSystemRequest) ([]GophermapSection, *GophorError
/* Check if we've been supplied subgophermap or regular file */
if subRequest.HasAbsPathSuffix("/"+GophermapFileStr) {
/* If executable, store as GophermapExecutable, else readGophermap() */
if stat.Mode().Perm() & 0100 != 0 {
if stat.Mode().Perm() & 0100 != 0 && Config.CgiEnabled {
sections = append(sections, &GophermapExecFile { subRequest })
} else {
/* Treat as any other gophermap! */
@ -223,7 +225,7 @@ func readGophermap(request *FileSystemRequest) ([]GophermapSection, *GophorError
}
} else {
/* If stored in cgi-bin store as GophermapExecutable, else read into GophermapText */
if subRequest.HasRelPathPrefix(CgiBinDirStr) {
if subRequest.HasRelPathPrefix(CgiBinDirStr) && Config.CgiEnabled {
sections = append(sections, &GophermapExecCgi{ subRequest })
} else {
fileContents, gophorErr := readIntoGophermap(subRequest.AbsPath())

@ -57,7 +57,7 @@ func (fs *FileSystem) HandleRequest(request *FileSystemRequest) ([]byte, *Gophor
switch {
/* Directory */
case stat.Mode() & os.ModeDir != 0:
/* Ignore cgi-bin directory */
/* Ignore anything under cgi-bin directory */
if request.HasRelPathPrefix(CgiBinDirStr) {
return nil, &GophorError{ IllegalPathErr, nil }
}
@ -92,9 +92,13 @@ func (fs *FileSystem) HandleRequest(request *FileSystemRequest) ([]byte, *Gophor
/* Regular file */
case stat.Mode() & os.ModeType == 0:
/* If cgi-bin, return executed contents. Else, fetch */
/* If cgi-bin and CGI enabled, return executed contents. Else, fetch */
if request.HasRelPathPrefix(CgiBinDirStr) {
return executeCgi(request)
if Config.CgiEnabled {
return executeCgi(request)
} else {
return nil, &GophorError{ CgiDisabledErr, nil }
}
} else {
return fs.FetchFile(request)
}

@ -71,6 +71,7 @@ func setupServer() []*GophorListener {
pageWidth := flag.Int("page-width", 80, "Change page width used when formatting output.")
restrictedFiles := flag.String("restrict-files", "", "New-line separated list of regex statements restricting files from showing in directory listings.")
disableCgi := flag.Bool("disable-cgi", false, "Disable CGI and all executable support.")
/* Logging settings */
systemLogPath := flag.String("system-log", "", "Change server system log file (blank outputs to stderr).")
@ -95,7 +96,8 @@ func setupServer() []*GophorListener {
/* Setup the server configuration instance and enter as much as we can right now */
Config = new(ServerConfig)
Config.PageWidth = *pageWidth
Config.PageWidth = *pageWidth
Config.CgiEnabled = !*disableCgi
/* Have to be set AFTER page width variable set */
Config.FooterText = formatGophermapFooter(*footerText, !*footerSeparator)

Loading…
Cancel
Save