432468 Improve command CGI path handling

This commit is contained in:
Greg Wilkins 2014-04-11 10:48:24 +10:00
parent ef400675aa
commit 5a0811b328
1 changed files with 119 additions and 79 deletions

View File

@ -47,32 +47,33 @@ import org.eclipse.jetty.util.log.Logger;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** /**
* CGI Servlet. * CGI Servlet.
* <p/> * <p>
* The cgi bin directory can be set with the "cgibinResourceBase" init parameter or it will default to the resource base of the context. If the *
* "cgibinResourceBaseIsRelative" init parameter is set the resource base is relative to the webapp. For example "WEB-INF/cgi" would work. * The following init parameters are used to configure this servlet:
* <br/> * <dl>
* Not that this only works for extracted war files as "jar cf" will not reserve the execute permissions on the cgi files. * <dt>cgibinResourceBase</dt><dd>Path to the cgi bin directory if set or it will default to the resource base of the context.</dd>
* <p/> * <dt>resourceBase</dt><dd>An alias for cgibinResourceBase.</dd>
* The "commandPrefix" init parameter may be used to set a prefix to all commands passed to exec. This can be used on systems that need assistance to execute a * <dt>cgibinResourceBaseIsRelative</dt><dd>If true then cgibinResourceBase is relative to the webapp (eg "WEB-INF/cgi")</dd>
* particular file type. For example on windows this can be set to "perl" so that perl scripts are executed. * <dt>commandPrefix</dt><dd>may be used to set a prefix to all commands passed to exec. This can be used on systems that need assistance to execute a
* <p/> * particular file type. For example on windows this can be set to "perl" so that perl scripts are executed.</dd>
* The "Path" init param is passed to the exec environment as PATH. Note: Must be run unpacked somewhere in the filesystem. * <dt>Path</dt><dd>passed to the exec environment as PATH.</dd>
* <p/> * <dt>ENV_*</dt><dd>used to set an arbitrary environment variable with the name stripped of the leading ENV_ and using the init parameter value</dd>
* Any initParameter that starts with ENV_ is used to set an environment variable with the name stripped of the leading ENV_ and using the init parameter value. * <dt>useFullPath</dt><dd>If true, the full URI path within the context is used for the exec command, otherwise a search is done for a partial URL that matches an exec Command</dd>
* </dl>
*
*/ */
public class CGI extends HttpServlet public class CGI extends HttpServlet
{ {
/** private static final long serialVersionUID = -6182088932884791074L;
*
*/
private static final long serialVersionUID = -6182088932884791073L;
private static final Logger LOG = Log.getLogger(CGI.class); private static final Logger LOG = Log.getLogger(CGI.class);
private boolean _ok; private boolean _ok;
private File _docRoot; private File _docRoot;
private boolean _cgiBinProvided;
private String _path; private String _path;
private String _cmdPrefix; private String _cmdPrefix;
private boolean _useFullPath;
private EnvList _env; private EnvList _env;
private boolean _ignoreExitState; private boolean _ignoreExitState;
private boolean _relative; private boolean _relative;
@ -83,16 +84,22 @@ public class CGI extends HttpServlet
{ {
_env = new EnvList(); _env = new EnvList();
_cmdPrefix = getInitParameter("commandPrefix"); _cmdPrefix = getInitParameter("commandPrefix");
_useFullPath = Boolean.parseBoolean(getInitParameter("useFullPath"));
_relative = Boolean.parseBoolean(getInitParameter("cgibinResourceBaseIsRelative")); _relative = Boolean.parseBoolean(getInitParameter("cgibinResourceBaseIsRelative"));
String tmp = getInitParameter("cgibinResourceBase"); String tmp = getInitParameter("cgibinResourceBase");
if (tmp == null) if (tmp != null)
_cgiBinProvided = true;
else
{ {
tmp = getInitParameter("resourceBase"); tmp = getInitParameter("resourceBase");
if (tmp == null) if (tmp != null)
_cgiBinProvided = true;
else
tmp = getServletContext().getRealPath("/"); tmp = getServletContext().getRealPath("/");
} }
else if (_relative)
if (_relative && _cgiBinProvided)
{ {
tmp = getServletContext().getRealPath(tmp); tmp = getServletContext().getRealPath(tmp);
} }
@ -137,10 +144,10 @@ public class CGI extends HttpServlet
_env.set("PATH",_path); _env.set("PATH",_path);
_ignoreExitState = "true".equalsIgnoreCase(getInitParameter("ignoreExitState")); _ignoreExitState = "true".equalsIgnoreCase(getInitParameter("ignoreExitState"));
Enumeration e = getInitParameterNames(); Enumeration<String> e = getInitParameterNames();
while (e.hasMoreElements()) while (e.hasMoreElements())
{ {
String n = (String)e.nextElement(); String n = e.nextElement();
if (n != null && n.startsWith("ENV_")) if (n != null && n.startsWith("ENV_"))
_env.set(n.substring(4),getInitParameter(n)); _env.set(n.substring(4),getInitParameter(n));
} }
@ -166,7 +173,6 @@ public class CGI extends HttpServlet
return; return;
} }
String pathInContext = (_relative?"":StringUtil.nonNull(req.getServletPath())) + StringUtil.nonNull(req.getPathInfo());
if (LOG.isDebugEnabled()) if (LOG.isDebugEnabled())
{ {
LOG.debug("CGI: ContextPath : " + req.getContextPath()); LOG.debug("CGI: ContextPath : " + req.getContextPath());
@ -180,63 +186,69 @@ public class CGI extends HttpServlet
// pathInContext may actually comprises scriptName/pathInfo...We will // pathInContext may actually comprises scriptName/pathInfo...We will
// walk backwards up it until we find the script - the rest must // walk backwards up it until we find the script - the rest must
// be the pathInfo; // be the pathInfo;
String pathInContext = (_relative ? "" : StringUtil.nonNull(req.getServletPath())) + StringUtil.nonNull(req.getPathInfo());
File execCmd = new File(_docRoot, pathInContext);
String pathInfo = pathInContext;
String both = pathInContext; if(!_useFullPath)
String first = both;
String last = "";
File exe = new File(_docRoot,first);
while ((first.endsWith("/") || !exe.exists()) && first.length() >= 0)
{ {
int index = first.lastIndexOf('/'); String path = pathInContext;
String info = "";
first = first.substring(0,index); // Search docroot for a matching execCmd
last = both.substring(index,both.length()); while (path.endsWith("/") && path.length() >= 0)
exe = new File(_docRoot,first);
}
if (first.length() == 0 || !exe.exists() || exe.isDirectory() || !exe.getCanonicalPath().equals(exe.getAbsolutePath()))
{
res.sendError(404);
}
else
{
if (LOG.isDebugEnabled())
{ {
LOG.debug("CGI: script is " + exe); if(!execCmd.exists())
LOG.debug("CGI: pathInfo is " + last); break;
int index = path.lastIndexOf('/');
path = path.substring(0,index);
info = pathInContext.substring(index,pathInContext.length());
execCmd = new File(_docRoot,path);
} }
exec(exe,last,req,res);
if (path.length() == 0 || !execCmd.exists() || execCmd.isDirectory() || !execCmd.getCanonicalPath().equals(execCmd.getAbsolutePath()))
{
res.sendError(404);
}
pathInfo = info;
} }
exec(execCmd,pathInfo,req,res);
} }
/* ------------------------------------------------------------ */ /** executes the CGI process
/* /*
* @param root @param path @param req @param res @exception IOException * @param command the command to execute, this command is prefixed by
* the context parameter "commandPrefix".
* @param pathInfo The PATH_INFO to process,
* see http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getPathInfo%28%29. Cannot be null
* @param req
* @param res
* @exception IOException
*/ */
private void exec(File command, String pathInfo, HttpServletRequest req, HttpServletResponse res) throws IOException private void exec(File command, String pathInfo, HttpServletRequest req, HttpServletResponse res) throws IOException
{ {
String path = command.getAbsolutePath(); assert req != null;
File dir = command.getParentFile(); assert res != null;
String scriptName = req.getRequestURI().substring(0,req.getRequestURI().length() - pathInfo.length()); assert pathInfo != null;
String scriptPath = getServletContext().getRealPath(scriptName); assert command != null;
String pathTranslated = req.getPathTranslated();
int len = req.getContentLength(); if (LOG.isDebugEnabled())
if (len < 0) {
len = 0; LOG.debug("CGI: script is " + command);
if ((pathTranslated == null) || (pathTranslated.length() == 0)) LOG.debug("CGI: pathInfo is " + pathInfo);
pathTranslated = path; }
String bodyFormEncoded = null; String bodyFormEncoded = null;
if ((HttpMethod.POST.equals(req.getMethod()) || HttpMethod.PUT.equals(req.getMethod())) && "application/x-www-form-urlencoded".equals(req.getContentType())) if ((HttpMethod.POST.equals(req.getMethod()) || HttpMethod.PUT.equals(req.getMethod())) && "application/x-www-form-urlencoded".equals(req.getContentType()))
{ {
MultiMap<String> parameterMap = new MultiMap<String>(); MultiMap<String> parameterMap = new MultiMap<String>();
Enumeration names = req.getParameterNames(); Enumeration<String> names = req.getParameterNames();
while (names.hasMoreElements()) while (names.hasMoreElements())
{ {
String parameterName = (String)names.nextElement(); String parameterName = names.nextElement();
parameterMap.addValues(parameterName, req.getParameterValues(parameterName)); parameterMap.addValues(parameterName, req.getParameterValues(parameterName));
} }
bodyFormEncoded = UrlEncoded.encode(parameterMap, Charset.forName(req.getCharacterEncoding()), true); bodyFormEncoded = UrlEncoded.encode(parameterMap, Charset.forName(req.getCharacterEncoding()), true);
@ -247,24 +259,33 @@ public class CGI extends HttpServlet
// look at : // look at :
// http://Web.Golux.Com/coar/cgi/draft-coar-cgi-v11-03-clean.html#6.1.1 // http://Web.Golux.Com/coar/cgi/draft-coar-cgi-v11-03-clean.html#6.1.1
env.set("AUTH_TYPE", req.getAuthType()); env.set("AUTH_TYPE", req.getAuthType());
int contentLen = req.getContentLength();
if (contentLen < 0)
contentLen = 0;
if (bodyFormEncoded != null) if (bodyFormEncoded != null)
{ {
env.set("CONTENT_LENGTH", Integer.toString(bodyFormEncoded.length())); env.set("CONTENT_LENGTH", Integer.toString(bodyFormEncoded.length()));
} }
else else
{ {
env.set("CONTENT_LENGTH", Integer.toString(len)); env.set("CONTENT_LENGTH", Integer.toString(contentLen));
} }
env.set("CONTENT_TYPE", req.getContentType()); env.set("CONTENT_TYPE", req.getContentType());
env.set("GATEWAY_INTERFACE", "CGI/1.1"); env.set("GATEWAY_INTERFACE", "CGI/1.1");
if ((pathInfo != null) && (pathInfo.length() > 0)) if (pathInfo.length() > 0)
{ {
env.set("PATH_INFO", pathInfo); env.set("PATH_INFO", pathInfo);
} }
String pathTranslated = req.getPathTranslated();
if ((pathTranslated == null) || (pathTranslated.length() == 0))
pathTranslated = pathInfo;
env.set("PATH_TRANSLATED", pathTranslated); env.set("PATH_TRANSLATED", pathTranslated);
env.set("QUERY_STRING", req.getQueryString()); env.set("QUERY_STRING", req.getQueryString());
env.set("REMOTE_ADDR", req.getRemoteAddr()); env.set("REMOTE_ADDR", req.getRemoteAddr());
env.set("REMOTE_HOST", req.getRemoteHost()); env.set("REMOTE_HOST", req.getRemoteHost());
// The identity information reported about the connection by a // The identity information reported about the connection by a
// RFC 1413 [11] request to the remote agent, if // RFC 1413 [11] request to the remote agent, if
// available. Servers MAY choose not to support this feature, or // available. Servers MAY choose not to support this feature, or
@ -272,17 +293,33 @@ public class CGI extends HttpServlet
// "REMOTE_IDENT" => "NYI" // "REMOTE_IDENT" => "NYI"
env.set("REMOTE_USER", req.getRemoteUser()); env.set("REMOTE_USER", req.getRemoteUser());
env.set("REQUEST_METHOD", req.getMethod()); env.set("REQUEST_METHOD", req.getMethod());
env.set("SCRIPT_NAME", scriptName);
String scriptPath;
String scriptName;
// use docRoot for scriptPath, too
if(_cgiBinProvided)
{
scriptPath = command.getAbsolutePath();
scriptName = scriptPath.substring(_docRoot.getAbsolutePath().length());
}
else
{
String requestURI = req.getRequestURI();
scriptName = requestURI.substring(0,requestURI.length() - pathInfo.length());
scriptPath = getServletContext().getRealPath(scriptName);
}
env.set("SCRIPT_FILENAME", scriptPath); env.set("SCRIPT_FILENAME", scriptPath);
env.set("SCRIPT_NAME", scriptName);
env.set("SERVER_NAME", req.getServerName()); env.set("SERVER_NAME", req.getServerName());
env.set("SERVER_PORT", Integer.toString(req.getServerPort())); env.set("SERVER_PORT", Integer.toString(req.getServerPort()));
env.set("SERVER_PROTOCOL", req.getProtocol()); env.set("SERVER_PROTOCOL", req.getProtocol());
env.set("SERVER_SOFTWARE", getServletContext().getServerInfo()); env.set("SERVER_SOFTWARE", getServletContext().getServerInfo());
Enumeration enm = req.getHeaderNames(); Enumeration<String> enm = req.getHeaderNames();
while (enm.hasMoreElements()) while (enm.hasMoreElements())
{ {
String name = (String)enm.nextElement(); String name = enm.nextElement();
String value = req.getHeader(name); String value = req.getHeader(name);
env.set("HTTP_" + name.toUpperCase(Locale.ENGLISH).replace('-','_'),value); env.set("HTTP_" + name.toUpperCase(Locale.ENGLISH).replace('-','_'),value);
} }
@ -293,29 +330,30 @@ public class CGI extends HttpServlet
// "SERVER_URL" => "NYI - http://us0245", // "SERVER_URL" => "NYI - http://us0245",
// "TZ" => System.getProperty("user.timezone"), // "TZ" => System.getProperty("user.timezone"),
// are we meant to decode args here ? or does the script get them // are we meant to decode args here? or does the script get them
// via PATH_INFO ? if we are, they should be decoded and passed // via PATH_INFO? if we are, they should be decoded and passed
// into exec here... // into exec here...
String execCmd = path; String absolutePath = command.getAbsolutePath();
if ((execCmd.charAt(0) != '"') && (execCmd.indexOf(" ") >= 0)) String execCmd = absolutePath;
// escape the execCommand
if (execCmd.length() > 0 && execCmd.charAt(0) != '"' && execCmd.indexOf(" ") >= 0)
execCmd = "\"" + execCmd + "\""; execCmd = "\"" + execCmd + "\"";
if (_cmdPrefix != null) if (_cmdPrefix != null)
execCmd = _cmdPrefix + " " + execCmd; execCmd = _cmdPrefix + " " + execCmd;
assert execCmd != null;
LOG.debug("Environment: " + env.getExportString()); LOG.debug("Environment: " + env.getExportString());
LOG.debug("Command: " + execCmd); LOG.debug("Command: " + execCmd);
final Process p; final Process p = Runtime.getRuntime().exec(execCmd, env.getEnvArray(), _docRoot);
if (dir == null)
p = Runtime.getRuntime().exec(execCmd, env.getEnvArray());
else
p = Runtime.getRuntime().exec(execCmd, env.getEnvArray(), dir);
// hook processes input to browser's output (async) // hook processes input to browser's output (async)
if (bodyFormEncoded != null) if (bodyFormEncoded != null)
writeProcessInput(p, bodyFormEncoded); writeProcessInput(p, bodyFormEncoded);
else if (len > 0) else if (contentLen > 0)
writeProcessInput(p, req.getInputStream(), len); writeProcessInput(p, req.getInputStream(), contentLen);
// hook processes output to browser's input (sync) // hook processes output to browser's input (sync)
// if browser closes stream, we should detect it and kill process... // if browser closes stream, we should detect it and kill process...
@ -383,7 +421,7 @@ public class CGI extends HttpServlet
int exitValue = p.exitValue(); int exitValue = p.exitValue();
if (0 != exitValue) if (0 != exitValue)
{ {
LOG.warn("Non-zero exit status (" + exitValue + ") from CGI program: " + path); LOG.warn("Non-zero exit status (" + exitValue + ") from CGI program: " + absolutePath);
if (!res.isCommitted()) if (!res.isCommitted())
res.sendError(500,"Failed to exec CGI"); res.sendError(500,"Failed to exec CGI");
} }
@ -393,7 +431,7 @@ public class CGI extends HttpServlet
{ {
// browser has probably closed its input stream - we // browser has probably closed its input stream - we
// terminate and clean up... // terminate and clean up...
LOG.debug("CGI: Client closed connection!"); LOG.debug("CGI: Client closed connection!", e);
} }
catch (InterruptedException ie) catch (InterruptedException ie)
{ {
@ -422,6 +460,7 @@ public class CGI extends HttpServlet
{ {
new Thread(new Runnable() new Thread(new Runnable()
{ {
@Override
public void run() public void run()
{ {
try try
@ -445,6 +484,7 @@ public class CGI extends HttpServlet
new Thread(new Runnable() new Thread(new Runnable()
{ {
@Override
public void run() public void run()
{ {
try try