Need to initialize libs before calling into Xerces.
[shibboleth/sp.git] / util / mdquery.cpp
1 /*
2  *  Copyright 2001-2010 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * mdquery.cpp
19  * 
20  * SAML Metadata Query tool layered on SP configuration.
21  */
22
23 #if defined (_MSC_VER) || defined(__BORLANDC__)
24 # include "config_win32.h"
25 #else
26 # include "config.h"
27 #endif
28
29 #ifdef WIN32
30 # define _CRT_NONSTDC_NO_DEPRECATE 1
31 # define _CRT_SECURE_NO_DEPRECATE 1
32 #endif
33
34 #include <shibsp/Application.h>
35 #include <shibsp/exceptions.h>
36 #include <shibsp/SPConfig.h>
37 #include <shibsp/ServiceProvider.h>
38 #include <shibsp/metadata/MetadataProviderCriteria.h>
39 #include <shibsp/util/SPConstants.h>
40 #include <saml/saml2/metadata/Metadata.h>
41 #include <xmltooling/logging.h>
42
43 using namespace shibsp;
44 using namespace opensaml::saml2md;
45 using namespace opensaml;
46 using namespace xmltooling::logging;
47 using namespace xmltooling;
48 using namespace std;
49
50 using xercesc::XMLString;
51
52 void usage()
53 {
54     cerr << "usage: mdquery -e <entityID> [-a <app id> -nostrict]" << endl;
55     cerr << "       mdquery -e <entityID> -r <role> -p <protocol> [-a <app id> -ns <namespace> -nostrict]" << endl;
56 }
57
58 int main(int argc,char* argv[])
59 {
60     char* entityID = nullptr;
61     char* appID = "default";
62     bool strict = true;
63     char* prot = nullptr;
64     const XMLCh* protocol = nullptr;
65     char* rname = nullptr;
66     char* rns = nullptr;
67
68     for (int i=1; i<argc; i++) {
69         if (!strcmp(argv[i],"-e") && i+1<argc)
70             entityID=argv[++i];
71         else if (!strcmp(argv[i],"-a") && i+1<argc)
72             appID=argv[++i];
73         else if (!strcmp(argv[i],"-p") && i+1<argc)
74             prot=argv[++i];
75         else if (!strcmp(argv[i],"-r") && i+1<argc)
76             rname=argv[++i];
77         else if (!strcmp(argv[i],"-ns") && i+1<argc)
78             rns=argv[++i];
79         else if (!strcmp(argv[i],"-saml10"))
80             protocol=samlconstants::SAML10_PROTOCOL_ENUM;
81         else if (!strcmp(argv[i],"-saml11"))
82             protocol=samlconstants::SAML11_PROTOCOL_ENUM;
83         else if (!strcmp(argv[i],"-saml2"))
84             protocol=samlconstants::SAML20P_NS;
85         else if (!strcmp(argv[i],"-idp"))
86             rname="IDPSSODescriptor";
87         else if (!strcmp(argv[i],"-aa"))
88             rname="AttributeAuthorityDescriptor";
89         else if (!strcmp(argv[i],"-pdp"))
90             rname="PDPDescriptor";
91         else if (!strcmp(argv[i],"-sp"))
92             rname="SPSSODescriptor";
93         else if (!strcmp(argv[i],"-nostrict"))
94             strict = false;
95     }
96
97     if (!entityID) {
98         usage();
99         return -10;
100     }
101
102     SPConfig& conf=SPConfig::getConfig();
103     conf.setFeatures(SPConfig::Metadata | SPConfig::Trust | SPConfig::OutOfProcess | SPConfig::Credentials);
104     if (!conf.init())
105         return -1;
106
107     if (rname) {
108         if (!protocol) {
109             if (prot)
110                 protocol = XMLString::transcode(prot);
111         }
112         if (!protocol) {
113             conf.term();
114             usage();
115             return -10;
116         }
117     }
118
119     if (!conf.instantiate()) {
120         conf.term();
121         return -2;
122     }
123
124     ServiceProvider* sp=conf.getServiceProvider();
125     sp->lock();
126
127     Category& log = Category::getInstance(SHIBSP_LOGCAT".Utility.MDQuery");
128
129     const Application* app = sp->getApplication(appID);
130     if (!app) {
131         log.error("unknown application ID (%s)", appID);
132         sp->unlock();
133         conf.term();
134         return -3;
135     }
136
137     app->getMetadataProvider()->lock();
138     MetadataProviderCriteria mc(*app, entityID, nullptr, nullptr, strict);
139     if (rname) {
140         const XMLCh* ns = rns ? XMLString::transcode(rns) : samlconstants::SAML20MD_NS;
141         auto_ptr_XMLCh n(rname);
142         QName q(ns, n.get());
143         mc.role = &q;
144         mc.protocol = protocol;
145         const RoleDescriptor* role = app->getMetadataProvider()->getEntityDescriptor(mc).second;
146         if (role)
147             XMLHelper::serialize(role->marshall(), cout, true);
148         else
149             log.error("compatible role %s not found for (%s)", q.toString().c_str(), entityID);
150     }
151     else {
152         const EntityDescriptor* entity = app->getMetadataProvider()->getEntityDescriptor(mc).first;
153         if (entity)
154             XMLHelper::serialize(entity->marshall(), cout, true);
155         else
156             log.error("no metadata found for (%s)", entityID);
157     }
158
159     app->getMetadataProvider()->unlock();
160
161     sp->unlock();
162     conf.term();
163     return 0;
164 }