Exemple #1
0
Boolean ProcessCimElement(CIMRepository& repository, XmlParser& parser)
{
    XmlEntry entry;

    if (!parser.next(entry) || entry.type != XmlEntry::XML_DECLARATION)
    {
	throw(parser.getLine(), "expected XML declaration");
    }

    if (!XmlReader::testStartTag(parser, entry, "CIM"))
	return false;

    String cimVersion;

    if (!entry.getAttributeValue("CIMVERSION", cimVersion))
    {
	throw XmlValidationError(parser.getLine(), 
	    "missing CIM.CIMVERSION attribute");
    }

    String dtdVersion;

    if (!entry.getAttributeValue("DTDVERSION", dtdVersion))
    {
	throw XmlValidationError(parser.getLine(), 
	    "missing CIM.DTDVERSION attribute");
    }

    if (!ProcessDeclarationElement(repository, parser))
    {
	throw XmlValidationError(parser.getLine(), 
	    "Expected DECLARATION element");
    }

    XmlReader::expectEndTag(parser, "CIM");

    return true;
}
Exemple #2
0
PEGASUS_NAMESPACE_BEGIN

/**

    Encapsulates the XML request in an HTTP M-POST or POST request message.
    Generates the appropriate HTTP extension headers corresponding to the
    XML request, in accordance with Specifications for CIM Operations over
    HTTP, Version 1.0, Section 3.  This method should be called only when
    the current parser location is the xml declaration.  If the xml
    declaration is not found, but the first token in the input is
    HTTP_METHOD_MPOST or HTTP_METHOD_POST, it is assumed that the request is
    already encapsulated in an HTTP request, and the message is returned
    unchanged.  No attempt is made to validate the complete HTTP request.
    If the useMPost parameter is TRUE, the headers are generated for an
    M-POST request.  Otherwise, the headers are generated for a POST
    request.  If the useHTTP11 parameter is TRUE, the headers are generated
    for an HTTP/1.1 request.  Otherwise, the headers are generated for an
    HTTP/1.0 request.  The combination of useMPost true and useHTTP11 false
    is invalid, but this function does not check for this case.  The XML
    request is examined only as much as necessary to generate the required
    headers.  This method does not attempt to validate the entire XML request.

    @param   parser              XmlParser instance corresponding to the
                                 XML request
    @param   hostName            host name to be used in HTTP Host header
    @param   useMPost            Boolean indicating that headers should be
                                 generated for an M-POST request
    @param   useHTTP11           Boolean indicating that headers should be
                                 generated for an HTTP/1.1 request
    @param   content             Buffer containing XML request
    @param   httpHeaders         Buffer returning the HTTP headers

    @return  Buffer containing the XML request encapsulated in an
             HTTP request message

    @exception  XmlValidationError  if the XML input is invalid
    @exception  XmlSemanticError    if the XML input contains a semantic error
    @exception  WbemExecException   if the input contains neither XML nor HTTP
                                    M-POST or POST method request

 */
Buffer XMLProcess::encapsulate( XmlParser& parser,
                                       const String& hostName,
                                       Boolean useMPost,
                                       Boolean useHTTP11,
                                       Buffer& content,
                                       Buffer& httpHeaders
                                       )
throw (XmlValidationError, XmlSemanticError, WbemExecException,
               XmlException, Exception)
{
    XmlEntry                     entry;
    Buffer                    message;
    String                       messageId;
    const char*                  cimVersion            = 0;
    const char*                  dtdVersion            = 0;
    String                       protocolVersion;
    CIMName                      className;
    CIMName                      methodName;
    CIMObjectPath                objectName;
    Buffer                    encoded;
    String                    objPath;
    Array<CIMKeyBinding>         keyBindings;
    Boolean                      multireq              = false;
    static Uint32                BUFFERSIZE            = 1024;
    Boolean hasXmlDeclaration = false;

    //
    //  xml declaration
    //
    if (!(hasXmlDeclaration = XmlReader::testXmlDeclaration (parser, entry)))
    {
        //
        //  No xml declaration
        //  Request may already be encapsulated in HTTP
        //  Check for HTTP method
        //
        char tmp [8];
        char* tmpp = & (tmp [0]);
        strncpy (tmpp, entry.text, 8);
        tmpp[7] = 0;
#if !defined(PEGASUS_COMPILER_MSVC) && !defined(PEGASUS_OS_ZOS)
        char *last;
        char* p = strtok_r (tmpp, HTTP_SP, &last);
#else
        char* p = strtok (tmpp, HTTP_SP);
#endif
        if (p != NULL)
        {
            if ((strcmp (p, HTTP_METHOD_MPOST) == 0) ||
                (strcmp (p, HTTP_METHOD_POST) == 0) ||
            //  This is a special request used for testing.
            //  It includes the HTTP header.
            //  Return the message as is.
                (strcmp (p, HTTP_METHOD_BOGUS) == 0))
            {
                return content;
            }
        }
     }

#ifdef PEGASUS_ENABLE_PROTOCOL_WSMAN

     // Check if the next tag is a SOAP envelope.
     if (parser.next(entry) &&
             entry.type == XmlEntry::START_TAG &&
             strcmp(entry.localName, "Envelope") == 0)
     {
          //
          //  Set WSMAN headers and content.
          //
          message << HTTP_METHOD_POST << HTTP_SP
                  << "/wsman" << HTTP_SP
                  << HTTP_PROTOCOL << HTTP_VERSION_11 << HTTP_CRLF;
          message << HEADER_NAME_HOST << HEADER_SEPARATOR
                  << HTTP_SP << hostName << HTTP_CRLF;
          message << HEADER_NAME_CONTENTTYPE << HEADER_SEPARATOR
                  << HTTP_SP << WSMAN_HEADER_VALUE_CONTENTTYPE
                  << HTTP_CRLF;
          message << HEADER_NAME_CONTENTLENGTH << HEADER_SEPARATOR
                  << HTTP_SP << (Uint32)content.size () << HTTP_CRLF;

          httpHeaders << message;
          message << HTTP_CRLF;
          message << content;
          return message;
     }
     else
     {
         parser.putBack(entry);
     }

#endif

    if (!hasXmlDeclaration)
    {
        //
        //  Input contains neither XML declaration nor HTTP M-POST or
        //  POST method request
        //
        throw WbemExecException(WbemExecException::INVALID_INPUT);
    }

    //
    //  CIM element
    //
    XmlReader::getCimStartTag (parser, cimVersion, dtdVersion);


    //
    //  MESSAGE element
    //
    if (!XmlReader::getMessageStartTag (parser, messageId, protocolVersion))
    {
        throw XmlValidationError(parser.getLine(), "expected MESSAGE element");

        // l10n TODO
        //MessageLoaderParms mlParms(
        //  "Server.CIMOperationRequestDecoder.EXPECTED_MESSAGE_ELEMENT",
        //  "expected MESSAGE element");
        //throw XmlValidationError(parser.getLine(), mlParms);
    }

    //
    //  MULTIREQ or SIMPLEREQ element
    //
    if (XmlReader::testStartTag (parser, entry, XML_ELEMENT_MULTIREQ))
    {
        multireq = true;
    }
    else if (!XmlReader::testStartTag (parser, entry, XML_ELEMENT_SIMPLEREQ))
    {
        // l10n TODO
        throw XmlValidationError (parser.getLine (), MISSING_ELEMENT_REQ);
    }

    //
    //  SIMPLEREQ element
    //
    else
    {
        //
        //  IMETHODCALL element
        //
        if (XmlReader::testStartTag (parser, entry, XML_ELEMENT_IMETHODCALL))
        {
            //
            //  Get NAME attribute of IMETHODCALL element
            //
            methodName = XmlReader::getCimNameAttribute (parser.getLine (),
                entry, XML_ELEMENT_IMETHODCALL);

            //
            //  Construct the object path from the LOCALNAMESPACEPATH
            //  subelements (NAMESPACE elements)
            //
            String namespaceName;

            if (!XmlReader::getLocalNameSpacePathElement(parser, namespaceName))
            {
                throw XmlValidationError(parser.getLine(),
                    "expected LOCALNAMESPACEPATH element");

                // l10n TODO
                //MessageLoaderParms mlParms(
                //    "Server.CIMOperationRequestDecoder."
                //        "EXPECTED_LOCALNAMESPACEPATH_ELEMENT",
                //    "expected LOCALNAMESPACEPATH element");
                //throw XmlValidationError(parser.getLine(),mlParms);
            }
            objPath.append(namespaceName);
        }

        //
        //  METHODCALL element
        //
        else if (XmlReader::testStartTag (parser, entry,
            XML_ELEMENT_METHODCALL))
        {
            //
            //  Get NAME attribute of METHODCALL element
            //
            methodName = XmlReader::getCimNameAttribute (parser.getLine (),
                entry, XML_ELEMENT_METHODCALL);

            //
            //  LOCALCLASSPATH or LOCALINSTANCEPATH element
            //
            if (XmlReader::getLocalClassPathElement (parser, objectName))
            {
                objPath.append(objectName.toString());
            }
            else if (XmlReader::getLocalInstancePathElement (parser,
                objectName))
            {
                objPath.append(objectName.toString());
            }
            else
            {
                // l10n TODO - Use CIMOperationRequestDecoder message when it
                // is available (or perhaps use Common.XmlReader.
                // EXPECTED_LOCALINSTANCEPATH_OR_LOCALCLASSPATH_ELEMENT)
                throw XmlValidationError (parser.getLine (),
                    MISSING_ELEMENT_LOCALPATH);
            }
        }
        else
        {
            throw XmlValidationError(parser.getLine(),
                "expected IMETHODCALL or METHODCALL element");

            // l10n TODO
            //MessageLoaderParms mlParms(
            // "Server.CIMOperationRequestDecoder.EXPECTED_IMETHODCALL_ELEMENT",
            // "expected IMETHODCALL or METHODCALL element");
            //throw XmlValidationError(parser.getLine(),mlParms);
        }
    }

    //
    //  Set headers
    //
    message.reserveCapacity (BUFFERSIZE);

    //
    //  Generate header prefix
    //
    srand(TimeValue::getCurrentTime().toMilliseconds() & 0xFFFFFFFF);
    char nn[3];
    nn[0] = '0' + (rand() % 10);
    nn[1] = '0' + (rand() % 10);
    nn[2] = 0;

    if (useMPost)
    {
        message << HTTP_METHOD_MPOST << HTTP_SP << HTTP_REQUEST_URI_CIMOM
                << HTTP_SP << HTTP_PROTOCOL << HTTP_VERSION_11 << HTTP_CRLF;
    }
    else
    {
        message << HTTP_METHOD_POST << HTTP_SP << HTTP_REQUEST_URI_CIMOM
                << HTTP_SP << HTTP_PROTOCOL;
        if (useHTTP11)
        {
            message << HTTP_VERSION_11;
        }
        else
        {
            message << HTTP_VERSION_10;
        }
        message << HTTP_CRLF;
    }
    message << HEADER_NAME_HOST << HEADER_SEPARATOR << HTTP_SP << hostName
            << HTTP_CRLF;
    message << HEADER_NAME_CONTENTTYPE << HEADER_SEPARATOR << HTTP_SP
            << HEADER_VALUE_CONTENTTYPE
            << HTTP_CRLF;
    message << HEADER_NAME_CONTENTLENGTH << HEADER_SEPARATOR << HTTP_SP
            << (Uint32)content.size () << HTTP_CRLF;

    if (useMPost)
    {
        message << HEADER_NAME_MAN << HEADER_SEPARATOR << HTTP_SP
                << HEADER_VALUE_MAN << nn << HTTP_CRLF;
        message << nn << HEADER_PREFIX_DELIMITER
                << HEADER_NAME_CIMPROTOCOLVERSION << HEADER_SEPARATOR
                << HTTP_SP << protocolVersion << HTTP_CRLF;
        message << nn << HEADER_PREFIX_DELIMITER << HEADER_NAME_CIMOPERATION
                << HEADER_SEPARATOR << HTTP_SP << HEADER_VALUE_CIMOPERATION
                << HTTP_CRLF;
        if (multireq)
        {
            message << nn << HEADER_PREFIX_DELIMITER << HEADER_NAME_CIMBATCH
                << HEADER_SEPARATOR << HTTP_CRLF;
        }
        else
        {
            message << nn << HEADER_PREFIX_DELIMITER << HEADER_NAME_CIMMETHOD
                << HEADER_SEPARATOR << HTTP_SP
                << XmlWriter::encodeURICharacters(methodName.getString())
                << HTTP_CRLF;
            message << nn << HEADER_PREFIX_DELIMITER << HEADER_NAME_CIMOBJECT
                << HEADER_SEPARATOR << HTTP_SP
                << XmlWriter::encodeURICharacters(objPath) << HTTP_CRLF;
        }
    }
    else
    {
        message << HEADER_NAME_CIMPROTOCOLVERSION << HEADER_SEPARATOR
                << HTTP_SP << protocolVersion << HTTP_CRLF;
        message << HEADER_NAME_CIMOPERATION << HEADER_SEPARATOR << HTTP_SP
                << HEADER_VALUE_CIMOPERATION << HTTP_CRLF;
        if (multireq)
        {
            message << HEADER_NAME_CIMBATCH << HEADER_SEPARATOR << HTTP_CRLF;
        }
        else
        {
            message << HEADER_NAME_CIMMETHOD << HEADER_SEPARATOR << HTTP_SP
                    << XmlWriter::encodeURICharacters(methodName.getString())
                    << HTTP_CRLF;
            message << HEADER_NAME_CIMOBJECT << HEADER_SEPARATOR << HTTP_SP
                    << XmlWriter::encodeURICharacters(objPath) << HTTP_CRLF;
        }
    }

    httpHeaders << message;
    message << HTTP_CRLF;
    message << content;

    return message;
}
void CIMExportRequestDecoder::handleMethodRequest(
    Uint32 queueId,
    HttpMethod httpMethod,
    char* content,
    const String& requestUri,
    const char* cimProtocolVersionInHeader,
    const char* cimExportMethodInHeader,
    const String& userName,
    const String& ipAddress,
    const AcceptLanguageList& httpAcceptLanguages,
    const ContentLanguageList& httpContentLanguages,
    Boolean closeConnect)
{
    // Set the Accept-Language into the thread for this service.
    // This will allow all code in this thread to get
    // the languages for the messages returned to the client.
    Thread::setLanguages(httpAcceptLanguages);

    //
    // If CIM Listener is shutting down, return error response
    //
    if (_serverTerminating)
    {
        sendHttpError(
            queueId,
            HTTP_STATUS_SERVICEUNAVAILABLE,
            String::EMPTY,
            "CIM Listener is shutting down.",
            closeConnect);
        return;
    }

    // Create a parser:

    XmlParser parser(content);
    XmlEntry entry;
    String messageId;
    const char* cimExportMethodName = "";
    AutoPtr<CIMExportIndicationRequestMessage> request;

    try
    {
        //
        // Process <?xml ... >
        //

        // These values are currently unused
        const char* xmlVersion = 0;
        const char* xmlEncoding = 0;

        XmlReader::getXmlDeclaration(parser, xmlVersion, xmlEncoding);

        // Expect <CIM ...>

        const char* cimVersion = 0;
        const char* dtdVersion = 0;

        XmlReader::getCimStartTag(parser, cimVersion, dtdVersion);

        if (!XmlReader::isSupportedCIMVersion(cimVersion))
        {
            sendHttpError(
                queueId,
                HTTP_STATUS_NOTIMPLEMENTED,
                "unsupported-cim-version",
                String::EMPTY,
                closeConnect);
            return;
        }

        if (!XmlReader::isSupportedDTDVersion(dtdVersion))
        {
            sendHttpError(
                queueId,
                HTTP_STATUS_NOTIMPLEMENTED,
                "unsupported-dtd-version",
                String::EMPTY,
                closeConnect);
            return;
        }

        // Expect <MESSAGE ...>

        String protocolVersion;
        if (!XmlReader::getMessageStartTag(
                    parser, messageId, protocolVersion))
        {
            MessageLoaderParms mlParms(
                "ExportServer.CIMExportRequestDecoder.EXPECTED_MESSAGE_ELEMENT",
                "expected MESSAGE element");

            throw XmlValidationError(parser.getLine(), mlParms);
        }

        // Validate that the protocol version in the header matches the XML

        if (!String::equalNoCase(protocolVersion, cimProtocolVersionInHeader))
        {
            sendHttpError(
                queueId,
                HTTP_STATUS_BADREQUEST,
                "header-mismatch",
                String::EMPTY,
                closeConnect);
            return;
        }

        if (!XmlReader::isSupportedProtocolVersion(protocolVersion))
        {
            // See Specification for CIM Operations over HTTP section 4.3
            sendHttpError(
                queueId,
                HTTP_STATUS_NOTIMPLEMENTED,
                "unsupported-protocol-version",
                String::EMPTY,
                closeConnect);
            return;
        }

        if (XmlReader::testStartTag(parser, entry, "MULTIEXPREQ"))
        {
            // We wouldn't have gotten here if CIMExportBatch header was
            // specified, so this must be indicative of a header mismatch
            sendHttpError(
                queueId,
                HTTP_STATUS_BADREQUEST,
                "header-mismatch",
                String::EMPTY,
                closeConnect);
            return;
            // Future: When MULTIEXPREQ is supported, must ensure
            // CIMExportMethod header is absent, and CIMExportBatch header
            // is present.
        }

        // Expect <SIMPLEEXPREQ ...>

        XmlReader::expectStartTag(parser, entry, "SIMPLEEXPREQ");

        // Expect <EXPMETHODCALL ...>

        if (!XmlReader::getEMethodCallStartTag(parser, cimExportMethodName))
        {
            MessageLoaderParms mlParms(
                "ExportServer.CIMExportRequestDecoder."
                "EXPECTED_EXPMETHODCALL_ELEMENT",
                "expected EXPMETHODCALL element");

            throw XmlValidationError(parser.getLine(), mlParms);
        }

        // The Specification for CIM Operations over HTTP reads:
        //     3.3.9. CIMExportMethod
        //
        //     This header MUST be present in any CIM Export Request
        //     message that contains a Simple Export Request.
        //
        //     It MUST NOT be present in any CIM Export Response message,
        //     nor in any CIM Export Request message that is not a
        //     Simple Export Request. It MUST NOT be present in any CIM
        //     Operation Request or Response message.
        //
        //     The name of the CIM export method within a Simple Export
        //     Request is defined to be the value of the NAME attribute
        //     of the <EXPMETHODCALL> element.
        //
        //     If a CIM Listener receives a CIM Export Request for which
        //     either:
        //
        //     - The CIMExportMethod header is present but has an invalid
        //       value, or;
        //     - The CIMExportMethod header is not present but the Export
        //       Request Message is a Simple Export Request, or;
        //     - The CIMExportMethod header is present but the Export
        //       Request Message is not a Simple Export Request, or;
        //     - The CIMExportMethod header is present, the Export Request
        //       Message is a Simple Export Request, but the CIMIdentifier
        //       value (when unencoded) does not match the unique method
        //       name within the Simple Export Request,
        //
        //     then it MUST fail the request and return a status of
        //     "400 Bad Request" (and MUST include a CIMError header in the
        //     response with a value of header-mismatch), subject to the
        //     considerations specified in Errors.
        if (System::strcasecmp(
                    cimExportMethodName, cimExportMethodInHeader) != 0)
        {
            // ATTN-RK-P3-20020404: How to decode cimExportMethodInHeader?
            sendHttpError(
                queueId,
                HTTP_STATUS_BADREQUEST,
                "header-mismatch",
                String::EMPTY,
                closeConnect);
            return;
        }

        // This try block only catches CIMExceptions, because they must be
        // responded to with a proper EMETHODRESPONSE.  Other exceptions are
        // caught in the outer try block.
        try
        {
            // Delegate to appropriate method to handle:

            if (System::strcasecmp(
                        cimExportMethodName, "ExportIndication") == 0)
            {
                request.reset(decodeExportIndicationRequest(
                                  queueId, parser, messageId, requestUri));
            }
            else
            {
                throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_NOT_SUPPORTED,
                                              MessageLoaderParms(
                                                  "ExportServer.CIMExportRequestDecoder."
                                                  "UNRECOGNIZED_EXPORT_METHOD",
                                                  "Unrecognized export method: $0",
                                                  cimExportMethodName));
            }
        }
        catch (CIMException& e)
        {
            sendEMethodError(
                queueId,
                httpMethod,
                messageId,
                cimExportMethodName,
                e,
                closeConnect);

            return;
        }

        // Expect </EXPMETHODCALL>

        XmlReader::expectEndTag(parser, "EXPMETHODCALL");

        // Expect </SIMPLEEXPREQ>

        XmlReader::expectEndTag(parser, "SIMPLEEXPREQ");

        // Expect </MESSAGE>

        XmlReader::expectEndTag(parser, "MESSAGE");

        // Expect </CIM>

        XmlReader::expectEndTag(parser, "CIM");
    }
    catch (XmlValidationError& e)
    {
        PEG_TRACE((TRC_XML,Tracer::LEVEL1,
                   "CIMExportRequestDecoder::handleMethodRequest - "
                   "XmlValidationError exception has occurred. Message: %s",
                   (const char*) e.getMessage().getCString()));

        sendHttpError(
            queueId,
            HTTP_STATUS_BADREQUEST,
            "request-not-valid",
            e.getMessage(),
            closeConnect);
        return;
    }
    catch (XmlSemanticError& e)
    {
        PEG_TRACE((TRC_XML,Tracer::LEVEL1,
                   "CIMExportRequestDecoder::handleMethodRequest - "
                   "XmlSemanticError exception has occurred. Message: %s",
                   (const char*) e.getMessage().getCString()));
        // ATTN-RK-P2-20020404: Is this the correct response for these errors?
        sendHttpError(
            queueId,
            HTTP_STATUS_BADREQUEST,
            "request-not-valid",
            e.getMessage(),
            closeConnect);
        return;
    }
    catch (XmlException& e)
    {
        PEG_TRACE((TRC_XML,Tracer::LEVEL1,
                   "CIMExportRequestDecoder::handleMethodRequest - "
                   "XmlException has occurred. Message: %s",
                   (const char*) e.getMessage().getCString()));

        sendHttpError(
            queueId,
            HTTP_STATUS_BADREQUEST,
            "request-not-well-formed",
            e.getMessage(),
            closeConnect);
        return;
    }
    catch (Exception& e)
    {
        // Don't know why I got this exception.  Seems like a bad thing.
        // Any exceptions we're expecting should be caught separately and
        // dealt with appropriately.  This is a last resort.
        sendHttpError(
            queueId,
            HTTP_STATUS_INTERNALSERVERERROR,
            String::EMPTY,
            e.getMessage(),
            closeConnect);
        return;
    }
    catch (...)
    {
        // Don't know why I got whatever this is.  Seems like a bad thing.
        // Any exceptions we're expecting should be caught separately and
        // dealt with appropriately.  This is a last resort.
        sendHttpError(
            queueId,
            HTTP_STATUS_INTERNALSERVERERROR,
            String::EMPTY,
            String::EMPTY,
            closeConnect);
        return;
    }

// l10n TODO - might want to move A-L and C-L to Message
// to make this more maintainable
    // Add the language headers to the request.
    // Note: Since the text of an export error response will be ignored
    // by the export client, ignore Accept-Language in the export request.
    // This will cause any export error response message to be sent in the
    // default language.
    request->operationContext.insert(IdentityContainer(userName));
    request->operationContext.set(
        ContentLanguageListContainer(httpContentLanguages));
    request->operationContext.set(
        AcceptLanguageListContainer(AcceptLanguageList()));

    request->ipAddress = ipAddress;

    request->setCloseConnect(closeConnect);

    _outputQueue->enqueue(request.release());
}