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