4440f8f1318fd7736fd01f054cb884f5e8b78155
[shibboleth/sp.git] / util / mdquery.cpp
1 /*\r
2  *  Copyright 2001-2007 Internet2\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 /**\r
18  * mdquery.cpp\r
19  * \r
20  * SAML Metadata Query tool layered on SP configuration\r
21  */\r
22 \r
23 #if defined (_MSC_VER) || defined(__BORLANDC__)\r
24 # include "config_win32.h"\r
25 #else\r
26 # include "config.h"\r
27 #endif\r
28 \r
29 #ifdef WIN32\r
30 # define _CRT_NONSTDC_NO_DEPRECATE 1\r
31 # define _CRT_SECURE_NO_DEPRECATE 1\r
32 #endif\r
33 \r
34 #include <shibsp/Application.h>\r
35 #include <shibsp/exceptions.h>\r
36 #include <shibsp/SPConfig.h>\r
37 #include <shibsp/ServiceProvider.h>\r
38 #include <shibsp/util/SPConstants.h>\r
39 #include <saml/saml2/metadata/Metadata.h>\r
40 #include <xmltooling/logging.h>\r
41 \r
42 using namespace shibsp;\r
43 using namespace opensaml::saml2md;\r
44 using namespace opensaml;\r
45 using namespace xmltooling::logging;\r
46 using namespace xmltooling;\r
47 using namespace std;\r
48 \r
49 int main(int argc,char* argv[])\r
50 {\r
51     char* entityID = NULL;\r
52     char* appID = "default";\r
53     bool strict = true;\r
54 \r
55     for (int i=1; i<argc; i++) {\r
56         if (!strcmp(argv[i],"-e") && i+1<argc)\r
57             entityID=argv[++i];\r
58         else if (!strcmp(argv[i],"-a") && i+1<argc)\r
59             appID=argv[++i];\r
60         else if (!strcmp(argv[i],"--nostrict"))\r
61             strict = false;\r
62     }\r
63 \r
64     if (!entityID) {\r
65         cerr << "usage: mdquery -e <entityID> [-a <application id> --nostrict]" << endl;\r
66         exit(0);\r
67     }\r
68 \r
69     char* path=getenv("SHIBSP_SCHEMAS");\r
70     if (!path)\r
71         path=SHIBSP_SCHEMAS;\r
72     char* config=getenv("SHIBSP_CONFIG");\r
73     if (!config)\r
74         config=SHIBSP_CONFIG;\r
75 \r
76     XMLToolingConfig::getConfig().log_config(getenv("SHIBSP_LOGGING") ? getenv("SHIBSP_LOGGING") : SHIBSP_LOGGING);\r
77 \r
78     SPConfig& conf=SPConfig::getConfig();\r
79     conf.setFeatures(SPConfig::Metadata | SPConfig::OutOfProcess);\r
80     if (!conf.init(path))\r
81         return -1;\r
82 \r
83     try {\r
84         static const XMLCh _path[] = UNICODE_LITERAL_4(p,a,t,h);\r
85         static const XMLCh validate[] = UNICODE_LITERAL_8(v,a,l,i,d,a,t,e);\r
86         xercesc::DOMDocument* dummydoc=XMLToolingConfig::getConfig().getParser().newDocument();\r
87         XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);\r
88         xercesc::DOMElement* dummy = dummydoc->createElementNS(NULL,_path);\r
89         auto_ptr_XMLCh src(config);\r
90         dummy->setAttributeNS(NULL,_path,src.get());\r
91         dummy->setAttributeNS(NULL,validate,xmlconstants::XML_ONE);\r
92         conf.setServiceProvider(conf.ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER,dummy));\r
93         conf.getServiceProvider()->init();\r
94     }\r
95     catch (exception&) {\r
96         conf.term();\r
97         return -2;\r
98     }\r
99 \r
100     ServiceProvider* sp=conf.getServiceProvider();\r
101     sp->lock();\r
102 \r
103     Category& log = Category::getInstance(SHIBSP_LOGCAT".Utility.MDQuery");\r
104 \r
105     const Application* app = sp->getApplication(appID);\r
106     if (!app) {\r
107         log.error("unknown application ID (%s)", appID);\r
108         sp->unlock();\r
109         conf.term();\r
110         return -3;\r
111     }\r
112 \r
113     app->getMetadataProvider()->lock();\r
114     const EntityDescriptor* entity = app->getMetadataProvider()->getEntityDescriptor(entityID, strict);\r
115     if (entity) {\r
116         XMLHelper::serialize(entity->marshall(), cout, true);\r
117     }\r
118     else {\r
119         log.error("no metadata found for (%s)", entityID);\r
120     }\r
121 \r
122     app->getMetadataProvider()->unlock();\r
123 \r
124     sp->unlock();\r
125     conf.term();\r
126     return 0;\r
127 }\r