reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
ReproAuthenticatorFactory.cxx
Go to the documentation of this file.
1
2#include <iostream>
3#include <fstream>
4#include <memory>
5#include <stdexcept>
6#include <utility>
7
8#if defined(HAVE_CONFIG_H)
9 #include "config.h"
10#endif
11
12#include "rutil/Logger.hxx"
13
14#include "AclStore.hxx"
19#include "UserAuthGrabber.hxx"
24
25using namespace std;
26using namespace resip;
27using namespace repro;
28
29#define RESIPROCATE_SUBSYSTEM resip::Subsystem::REPRO
30
32 : mProxyConfig(proxyConfig),
33 mSipStack(sipStack),
34 mDum(dum),
35 mEnableCertAuth(mProxyConfig.getConfigBool("EnableCertificateAuthenticator", false)),
36 mEnableDigestAuth(!mProxyConfig.getConfigBool("DisableAuth", false)),
37 mEnableRADIUS(mProxyConfig.getConfigBool("EnableRADIUS", false)),
38 mRADIUSConfiguration(mProxyConfig.getConfigData("RADIUSConfiguration", "")),
39 mStaticRealm(mProxyConfig.getConfigData("StaticRealm", "")),
40 mDigestChallengeThirdParties(!mEnableCertAuth),
41 mCertificateAuthManager((DumFeature*)0),
42 mServerAuthManager((ServerAuthManager*)0)
43{
44 std::vector<resip::Data> digestTypeStrings;
45 if (mProxyConfig.getConfigValue("DigestChallengeAlgorithms", digestTypeStrings))
46 {
47 DigestType digestType;
48 for (auto it = digestTypeStrings.begin(); it != digestTypeStrings.end(); ++it)
49 {
50 if (Helper::isDigestAlgorithmSupported(*it, digestType))
51 {
52 InfoLog(<< "Adding algorithm=" << *it << " for digest challenges");
53 mChallengeDigestTypes.push_back(digestType);
54 }
55 }
56 }
57}
58
62
63void
64ReproAuthenticatorFactory::init()
65{
66 if(!mAuthRequestDispatcher.get())
67 {
68 int numAuthGrabberWorkerThreads = mProxyConfig.getConfigInt("NumAuthGrabberWorkerThreads", 2);
69 if(numAuthGrabberWorkerThreads < 1)
70 {
71 numAuthGrabberWorkerThreads = 1; // must have at least one thread
72 }
73 std::unique_ptr<Worker> grabber(new UserAuthGrabber(*mProxyConfig.getDataStore()));
74 mAuthRequestDispatcher.reset(new Dispatcher(std::move(grabber), &mSipStack, numAuthGrabberWorkerThreads));
75 }
76
77 // TODO: should be implemented using AbstractDb
78 loadCommonNameMappings();
79}
80
81void
82ReproAuthenticatorFactory::loadCommonNameMappings()
83{
84 // Already loaded?
85 if(!mCommonNameMappings.empty())
86 return;
87
88 Data mappingsFileName = mProxyConfig.getConfigData("CommonNameMappings", "");
89 if(mappingsFileName.empty())
90 return;
91
92 InfoLog(<< "trying to load common name mappings from file: " << mappingsFileName);
93
94 ifstream mappingsFile(mappingsFileName.c_str());
95 if(!mappingsFile)
96 {
97 ErrLog(<< "failed to open mappings file: " << mappingsFileName << ", aborting");
98 throw std::runtime_error("Error opening/reading mappings file");
99 }
100
101 string sline;
102 while(getline(mappingsFile, sline))
103 {
104 Data line(sline);
105 Data cn;
106 PermittedFromAddresses permitted;
107 ParseBuffer pb(line);
108
109 pb.skipWhitespace();
110 const char * anchor = pb.position();
111 if(pb.eof() || *anchor == '#') continue; // if line is a comment or blank then skip it
112
113 // Look for end of name
114 pb.skipToOneOf("\t");
115 pb.data(cn, anchor);
116 if(mCommonNameMappings.find(cn) != mCommonNameMappings.end())
117 {
118 ErrLog(<< "CN '" << cn << "' repeated in mappings file");
119 throw std::runtime_error("CN repeated in mappings file");
120 }
121 pb.skipChar('\t');
122
123 while(!pb.eof())
124 {
125 pb.skipWhitespace();
126 if(pb.eof())
127 continue;
128
129 Data value;
130 anchor = pb.position();
131 pb.skipToOneOf(",\r\n ");
132 pb.data(value, anchor);
133 if(!value.empty())
134 {
135 StackLog(<< "Loading CN '" << cn << "', found mapping '" << value << "'");
136 permitted.insert(value);
137 }
138 if(!pb.eof())
139 pb.skipChar();
140 }
141
142 DebugLog(<< "Loaded mapping for CN '" << cn << "', " << permitted.size() << " mapping(s)");
143 mCommonNameMappings[cn] = permitted;
144 }
145}
146
147std::shared_ptr<DumFeature>
149{
150 init();
151 if(!mCertificateAuthManager.get())
152 {
153 Store *db = mProxyConfig.getDataStore();
154 resip_assert(db);
155 AclStore& aclStore = db->mAclStore;
156 mCertificateAuthManager.reset(new ReproTlsPeerAuthManager(*mDum, mDum->dumIncomingTarget(), getDispatcher(), aclStore, true, mCommonNameMappings));
157 }
158 return mCertificateAuthManager;
159}
160
161std::unique_ptr<Processor>
163{
164 init();
165 Store *db = mProxyConfig.getDataStore();
166 resip_assert(db);
167 AclStore& aclStore = db->mAclStore;
168 return std::unique_ptr<Processor>(new CertificateAuthenticator(mProxyConfig, getDispatcher(), &mSipStack, aclStore, true, mCommonNameMappings));
169}
170
171std::shared_ptr<ServerAuthManager>
173{
174 init();
175 if(!mServerAuthManager.get())
176 {
177 if(mEnableRADIUS)
178 {
179#ifdef USE_RADIUS_CLIENT
180 mServerAuthManager.reset(new ReproRADIUSServerAuthManager(*mDum,
181 mProxyConfig.getDataStore()->mAclStore,
182 !mProxyConfig.getConfigBool("DisableAuthInt", false) /*useAuthInt*/,
183 mProxyConfig.getConfigBool("RejectBadNonces", false),
184 mRADIUSConfiguration,
185 mDigestChallengeThirdParties,
186 mStaticRealm,
187 mChallengeDigestTypes));
188#else
189 ErrLog(<<"can't create ReproRADIUSServerAuthManager, not compiled with RADIUS support");
190#endif
191 }
192 else
193 {
194 mServerAuthManager.reset(new ReproServerAuthManager(*mDum,
196 mProxyConfig.getDataStore()->mAclStore,
197 !mProxyConfig.getConfigBool("DisableAuthInt", false) /*useAuthInt*/,
198 mProxyConfig.getConfigBool("RejectBadNonces", false),
199 mDigestChallengeThirdParties,
200 mStaticRealm,
201 mChallengeDigestTypes));
202 }
203 }
204 return mServerAuthManager;
205}
206
207std::unique_ptr<Processor>
209{
210 init();
211 if(mEnableRADIUS)
212 {
213#ifdef USE_RADIUS_CLIENT
214 return std::unique_ptr<Processor>(new RADIUSAuthenticator(mProxyConfig, mRADIUSConfiguration, mStaticRealm));
215#else
216 ErrLog(<<"can't create RADIUSAuthenticator, not compiled with RADIUS support");
217 return nullptr;
218#endif
219 }
220 else
221 {
222 return std::unique_ptr<Processor>(new DigestAuthenticator(mProxyConfig, getDispatcher(), mStaticRealm, mChallengeDigestTypes));
223 }
224}
225
228{
229 init();
230 return mAuthRequestDispatcher.get();
231}
232
233/* ====================================================================
234 * BSD License
235 *
236 * Copyright (c) 2026 SIP Spectrum, Inc. https://www.sipspectrum.com
237 * Copyright (c) 2013 Daniel Pocock http://danielpocock.com All rights reserved.
238 *
239 * Redistribution and use in source and binary forms, with or without
240 * modification, are permitted provided that the following conditions
241 * are met:
242 *
243 * 1. Redistributions of source code must retain the above copyright
244 * notice, this list of conditions and the following disclaimer.
245 *
246 * 2. Redistributions in binary form must reproduce the above copyright
247 * notice, this list of conditions and the following disclaimer in
248 * the documentation and/or other materials provided with the
249 * distribution.
250 *
251 * 3. Neither the name of the author(s) nor the names of any contributors
252 * may be used to endorse or promote products derived from this software
253 * without specific prior written permission.
254 *
255 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS "AS IS" AND
256 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
257 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
258 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE
259 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
260 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
263 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
264 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
265 * SUCH DAMAGE.
266 *
267 * ====================================================================
268 *
269 */
#define DebugLog(args_)
Definition Logger.hxx:128
#define StackLog(args_)
Definition Logger.hxx:125
#define InfoLog(args_)
Definition Logger.hxx:131
#define ErrLog(args_)
Definition Logger.hxx:137
#define resip_assert(x)
This file needs to work for both C and C++ code.
virtual std::shared_ptr< resip::DumFeature > getCertificateAuthManager()
virtual resip::Dispatcher * getDispatcher()
ReproAuthenticatorFactory(ProxyConfig &proxyConfig, resip::SipStack &sipStack, resip::DialogUsageManager *dum)
virtual std::shared_ptr< resip::ServerAuthManager > getServerAuthManager()
virtual std::unique_ptr< Processor > getCertificateAuthenticator()
virtual std::unique_ptr< Processor > getDigestAuthenticator()
The Store contains differnt types of stores such as user, route, etc.
Definition Store.hxx:21
AclStore mAclStore
Definition Store.hxx:37
int getConfigInt(const resip::Data &name, int defaultValue) const
resip::Data getConfigData(const resip::Data &name, const resip::Data &defaultValue, bool useDefaultIfEmpty=false) const
bool getConfigBool(const resip::Data &name, bool defaultValue) const
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
const char * c_str() const
Returns a null-terminated string representing.
Definition Data.cxx:1097
bool empty() const noexcept
Checks whether the Data is empty.
Definition Data.hxx:591
const char * data() const noexcept
Returns a pointer to the contents of this Data.
Definition Data.hxx:608
TargetCommand::Target & dumIncomingTarget()
A generic thread-bank class.
static bool isDigestAlgorithmSupported(const Auth &auth, DigestType &digestType)
Definition Helper.cxx:742
Provides string-parsing functionality with protection from buffer overflow.
bool getConfigValue(const resip::Data &name, resip::Uri &value)
This is the primary point of interaction between the app-layer and the stack.
Definition SipStack.hxx:160
This Class is used to implement a primitive form of RPC using loose XML formatting.
dcm! – add UnusedChecking(_enum) below;
std::set< Data > PermittedFromAddresses