reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
WsCookieAuthManager.cxx
Go to the documentation of this file.
1#include "rutil/ResipAssert.h"
2
9#include "rutil/Logger.hxx"
11
12#define RESIPROCATE_SUBSYSTEM Subsystem::DUM
13
14using namespace resip;
15using namespace std;
16
21
23{
24 InfoLog(<< "~WsCookieAuthManager");
25}
26
27// !bwc! We absolutely, positively, MUST NOT throw here. This is because in
28// DialogUsageManager::process(), we do not know if a DumFeature has taken
29// ownership of msg until we get a return. If we throw, the ownership of msg
30// is unknown. This is unacceptable.
33{
34 SipMessage* sipMessage = dynamic_cast<SipMessage*>(msg);
35
36 if (sipMessage)
37 {
39 switch ( handle(sipMessage) )
40 {
42 InfoLog(<< "WsCookieAuth rejected request " << sipMessage->brief());
44 default: // includes Authorized, Skipped
46 }
47 }
48
49 // Catch-all (handles something that was not a SipMessage)
50 return FeatureDone;
51}
52
53bool
55{
56 return(
57 (isEqualNoCase(first.user(), second.user()) || first.user() == "*") &&
58 (isEqualNoCase(first.host(), second.host()) || first.host() == "*")
59 );
60}
61
62bool
64 const MethodTypes method,
65 const WsCookieContext &wsCookieContext,
66 const resip::Uri &fromUri,
67 const resip::Uri &toUri)
68{
69 if(difftime(wsCookieContext.getExpiresTime(), time(NULL)) < 0)
70 {
71 WarningLog(<< "Received expired cookie");
72 return false;
73 }
74
75 Uri wsFromUri = wsCookieContext.getWsFromUri();
76 Uri wsDestUri = wsCookieContext.getWsDestUri();
77 if(cookieUriMatch(wsFromUri, fromUri))
78 {
79 DebugLog(<< "Matched cookie source URI field" << wsFromUri << " against request From header field URI " << fromUri);
80 // When registering, "From" URI == "To" URI, so we can ignore the
81 // "To" URI restriction from the cookie when processing REGISTER
82 if(method == REGISTER && isEqualNoCase(fromUri.user(), toUri.user()) && isEqualNoCase(fromUri.host(), toUri.host()))
83 {
84 return true;
85 }
86 if(cookieUriMatch(wsDestUri, toUri))
87 {
88 DebugLog(<< "Matched cookie destination URI field" << wsDestUri << " against request To header field URI " << toUri);
89 return true;
90 }
91 }
92
93 // catch-all: access denied
94 return false;
95}
96
97// return true if request has been consumed
100{
101 // Only check message coming over WebSockets
102 if(!isWebSocket(sipMessage->getReceivedTransportTuple().getType()))
103 {
104 return Skipped;
105 }
106 //InfoLog( << "trying to do auth" );
107 if (!sipMessage->isRequest() ||
108 sipMessage->header(h_RequestLine).method() == ACK ||
109 sipMessage->header(h_RequestLine).method() == CANCEL)
110 {
111 // Do not inspect ACKs or CANCELs
112 return Skipped;
113 }
114
115 if(!sipMessage->header(h_From).isWellFormed() ||
116 sipMessage->header(h_From).isAllContacts() )
117 {
118 InfoLog(<<"Malformed From header: cannot verify against cookie. Rejecting.");
119 auto response = std::make_shared<SipMessage>();
120 Helper::makeResponse(*response, *sipMessage, 400, "Malformed From header");
121 mDum.send(response);
122 return Rejected;
123 }
124
125 const WsCookieContext &wsCookieContext = *(sipMessage->getWsCookieContext());
126 if (mDum.isMyDomain(sipMessage->header(h_From).uri().host()))
127 {
128 if (requiresAuthorization(*sipMessage))
129 {
130 if(authorizedForThisIdentity(sipMessage->header(h_RequestLine).method(), wsCookieContext, sipMessage->header(h_From).uri(), sipMessage->header(h_To).uri()))
131 {
132 return Authorized;
133 }
134 auto response = std::make_shared<SipMessage>();
135 Helper::makeResponse(*response, *sipMessage, 403, "Cookie-based authorization failed");
136 mDum.send(response);
137 return Rejected;
138 }
139 else
140 {
141 return Skipped;
142 }
143 }
144 else
145 {
146 auto response = std::make_shared<SipMessage>();
147 Helper::makeResponse(*response, *sipMessage, 403, "Cookie-based authorization failed");
148 mDum.send(response);
149 return Rejected;
150 }
151
152 InfoLog(<< "Skipping some message that we didn't explicitly handle");
153 return Skipped;
154}
155
156bool
158{
159 // everything must be authorized, over-ride this method
160 // to implement some other policy
161 return true;
162}
163
164
165/* ====================================================================
166 * BSD License
167 *
168 * Copyright (c) 2013 Catalin Constantin Usurelu All rights reserved.
169 *
170 * Redistribution and use in source and binary forms, with or without
171 * modification, are permitted provided that the following conditions
172 * are met:
173 *
174 * 1. Redistributions of source code must retain the above copyright
175 * notice, this list of conditions and the following disclaimer.
176 *
177 * 2. Redistributions in binary form must reproduce the above copyright
178 * notice, this list of conditions and the following disclaimer in
179 * the documentation and/or other materials provided with the
180 * distribution.
181 *
182 * 3. Neither the name of the author(s) nor the names of any contributors
183 * may be used to endorse or promote products derived from this software
184 * without specific prior written permission.
185 *
186 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS "AS IS" AND
187 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
189 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE
190 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
192 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
193 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
194 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
195 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
196 * SUCH DAMAGE.
197 *
198 * ====================================================================
199 *
200 */
#define DebugLog(args_)
Definition Logger.hxx:128
#define InfoLog(args_)
Definition Logger.hxx:131
#define WarningLog(args_)
Definition Logger.hxx:134
void send(std::shared_ptr< SipMessage > request)
DialogUsageManager & mDum
static void makeResponse(SipMessage &response, const SipMessage &request, int responseCode, const Data &reason=Data::Empty, const Data &hostname=Data::Empty, const Data &warning=Data::Empty)
Make a response to a provided request.
Definition Helper.cxx:412
bool isWellFormed() const
Returns true iff this element was parsed successfully, according to the implementation of parse().
The base-class used for message-passing.
Definition Message.hxx:21
virtual Brief brief() const
return a facet for brief encoding of message
Definition Message.cxx:16
const Uri & uri() const
MethodTypes & method()
Represents a SIP message.
bool isRequest() const
Returns true if message is a request, false otherwise.
const Tuple & getReceivedTransportTuple() const
const RequestLine & header(const RequestLineType &l) const
Returns the RequestLine. This is only valid for request messages.
std::shared_ptr< WsCookieContext > getWsCookieContext() const noexcept
bool isMyDomain(const Data &domain) const
Returns true iff domain matches one of the domains that this TransactionUser is responsible for.
TransportType getType() const
Definition Tuple.hxx:122
Represents the "SIP-URI" and "SIPS-URI" elements in the RFC 3261 grammar.
Definition Uri.hxx:22
Data & user()
Definition Uri.hxx:42
Data & host()
Definition Uri.hxx:40
virtual bool requiresAuthorization(const SipMessage &msg)
should return true if the request must be challenged The default is to challenge all requests - overr...
bool cookieUriMatch(const resip::Uri &first, const resip::Uri &second)
compares URI user and host, allows wildcards in first URI
bool authorizedForThisIdentity(const MethodTypes method, const WsCookieContext &wsCookieContext, const resip::Uri &fromUri, const resip::Uri &toUri)
should return true if the passed in user is authorized for the provided uri
WsCookieAuthManager(DialogUsageManager &dum, TargetCommand::Target &target)
virtual Result handle(SipMessage *sipMsg)
virtual ProcessingResult process(Message *msg)
time_t getExpiresTime() const
dcm! – add UnusedChecking(_enum) below;
bool isEqualNoCase(const Data &left, const Data &right)
Definition Data.hxx:1116
bool isWebSocket(TransportType type)
Returns true if passed in transport type is a WebSocket transport protocol.
RequestLineType h_RequestLine
Definition Headers.cxx:347