2011-01-11 23:17:54 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2010 Wim Taymans <wim.taymans at gmail.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-04 00:14:25 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2011-01-11 23:17:54 +00:00
|
|
|
*/
|
2013-07-11 14:57:14 +00:00
|
|
|
/**
|
|
|
|
* SECTION:rtsp-auth
|
|
|
|
* @short_description: Authentication and authorization
|
|
|
|
* @see_also: #GstRTSPPermission, #GstRTSPtoken
|
|
|
|
*
|
2013-07-16 10:32:51 +00:00
|
|
|
* The #GstRTSPAuth object is responsible for checking if the current user is
|
|
|
|
* allowed to perform requested actions. The default implementation has some
|
|
|
|
* reasonable checks but subclasses can implement custom security policies.
|
|
|
|
*
|
|
|
|
* A new auth object is made with gst_rtsp_auth_new(). It is usually configured
|
|
|
|
* on the #GstRTSPServer object.
|
|
|
|
*
|
|
|
|
* The RTSP server will call gst_rtsp_auth_check() with a string describing the
|
|
|
|
* check to perform. The possible checks are prefixed with
|
|
|
|
* #GST_RTSP_AUTH_CHECK_*. Depending on the check, the default implementation
|
|
|
|
* will use the current #GstRTSPToken, #GstRTSPClientState and
|
|
|
|
* #GstRTSPPermissions on the object to check if an operation is allowed.
|
|
|
|
*
|
|
|
|
* The default #GstRTSPAuth object has support for basic authentication. With
|
|
|
|
* gst_rtsp_auth_add_basic() you can add a basic authentication string together
|
|
|
|
* with the #GstRTSPToken that will become active when successfully
|
|
|
|
* authenticated.
|
|
|
|
*
|
|
|
|
* When a TLS certificate has been set with gst_rtsp_auth_set_tls_certificate(),
|
|
|
|
* the default auth object will require the client to connect with a TLS
|
|
|
|
* connection.
|
|
|
|
*
|
|
|
|
* Last reviewed on 2013-07-16 (1.0.0)
|
2013-07-11 14:57:14 +00:00
|
|
|
*/
|
2011-01-11 23:17:54 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "rtsp-auth.h"
|
|
|
|
|
2012-11-29 10:11:05 +00:00
|
|
|
#define GST_RTSP_AUTH_GET_PRIVATE(obj) \
|
|
|
|
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_AUTH, GstRTSPAuthPrivate))
|
|
|
|
|
|
|
|
struct _GstRTSPAuthPrivate
|
|
|
|
{
|
|
|
|
GMutex lock;
|
2013-07-12 10:41:52 +00:00
|
|
|
|
|
|
|
/* the TLS certificate */
|
|
|
|
GTlsCertificate *certificate;
|
2013-07-04 12:33:59 +00:00
|
|
|
GHashTable *basic; /* protected by lock */
|
2012-11-29 10:11:05 +00:00
|
|
|
GstRTSPMethod methods;
|
|
|
|
};
|
|
|
|
|
2011-01-11 23:17:54 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_LAST
|
|
|
|
};
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (rtsp_auth_debug);
|
|
|
|
#define GST_CAT_DEFAULT rtsp_auth_debug
|
|
|
|
|
|
|
|
static void gst_rtsp_auth_get_property (GObject * object, guint propid,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_rtsp_auth_set_property (GObject * object, guint propid,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_rtsp_auth_finalize (GObject * obj);
|
|
|
|
|
2013-07-05 18:48:18 +00:00
|
|
|
static gboolean default_authenticate (GstRTSPAuth * auth,
|
2013-07-09 14:04:35 +00:00
|
|
|
GstRTSPClientState * state);
|
|
|
|
static gboolean default_check (GstRTSPAuth * auth, GstRTSPClientState * state,
|
|
|
|
const gchar * check);
|
2011-01-11 23:17:54 +00:00
|
|
|
|
|
|
|
G_DEFINE_TYPE (GstRTSPAuth, gst_rtsp_auth, G_TYPE_OBJECT);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtsp_auth_class_init (GstRTSPAuthClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
|
2012-11-29 10:11:05 +00:00
|
|
|
g_type_class_add_private (klass, sizeof (GstRTSPAuthPrivate));
|
|
|
|
|
2011-01-11 23:17:54 +00:00
|
|
|
gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
gobject_class->get_property = gst_rtsp_auth_get_property;
|
|
|
|
gobject_class->set_property = gst_rtsp_auth_set_property;
|
|
|
|
gobject_class->finalize = gst_rtsp_auth_finalize;
|
|
|
|
|
2013-07-05 18:48:18 +00:00
|
|
|
klass->authenticate = default_authenticate;
|
2013-07-05 10:08:36 +00:00
|
|
|
klass->check = default_check;
|
2011-01-11 23:17:54 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (rtsp_auth_debug, "rtspauth", 0, "GstRTSPAuth");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtsp_auth_init (GstRTSPAuth * auth)
|
|
|
|
{
|
2013-07-04 12:33:59 +00:00
|
|
|
GstRTSPAuthPrivate *priv;
|
|
|
|
|
|
|
|
auth->priv = priv = GST_RTSP_AUTH_GET_PRIVATE (auth);
|
|
|
|
|
|
|
|
g_mutex_init (&priv->lock);
|
|
|
|
|
2013-07-05 18:48:18 +00:00
|
|
|
priv->basic = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
|
|
|
|
(GDestroyNotify) gst_rtsp_token_unref);
|
2012-11-29 10:11:05 +00:00
|
|
|
|
2011-01-11 23:17:54 +00:00
|
|
|
/* bitwise or of all methods that need authentication */
|
2013-07-15 09:56:06 +00:00
|
|
|
priv->methods = 0;
|
2011-01-11 23:17:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtsp_auth_finalize (GObject * obj)
|
|
|
|
{
|
|
|
|
GstRTSPAuth *auth = GST_RTSP_AUTH (obj);
|
2012-11-29 10:11:05 +00:00
|
|
|
GstRTSPAuthPrivate *priv = auth->priv;
|
2011-01-11 23:17:54 +00:00
|
|
|
|
|
|
|
GST_INFO ("finalize auth %p", auth);
|
2013-07-12 10:41:52 +00:00
|
|
|
|
|
|
|
if (priv->certificate)
|
|
|
|
g_object_unref (priv->certificate);
|
2013-07-04 12:33:59 +00:00
|
|
|
g_hash_table_unref (priv->basic);
|
2012-11-29 10:11:05 +00:00
|
|
|
g_mutex_clear (&priv->lock);
|
2011-01-11 23:17:54 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (gst_rtsp_auth_parent_class)->finalize (obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtsp_auth_get_property (GObject * object, guint propid,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
switch (propid) {
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtsp_auth_set_property (GObject * object, guint propid,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
switch (propid) {
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_rtsp_auth_new:
|
|
|
|
*
|
|
|
|
* Create a new #GstRTSPAuth instance.
|
|
|
|
*
|
|
|
|
* Returns: a new #GstRTSPAuth
|
|
|
|
*/
|
|
|
|
GstRTSPAuth *
|
|
|
|
gst_rtsp_auth_new (void)
|
|
|
|
{
|
|
|
|
GstRTSPAuth *result;
|
|
|
|
|
|
|
|
result = g_object_new (GST_TYPE_RTSP_AUTH, NULL);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-07-12 10:41:52 +00:00
|
|
|
/**
|
|
|
|
* gst_rtsp_auth_set_tls_certificate:
|
|
|
|
* @auth: a #GstRTSPAuth
|
|
|
|
* @cert: (allow none): a #GTlsCertificate
|
|
|
|
*
|
|
|
|
* Set the TLS certificate for the auth. Client connections will only
|
|
|
|
* be accepted when TLS is negotiated.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_rtsp_auth_set_tls_certificate (GstRTSPAuth * auth, GTlsCertificate * cert)
|
|
|
|
{
|
|
|
|
GstRTSPAuthPrivate *priv;
|
|
|
|
GTlsCertificate *old;
|
|
|
|
|
|
|
|
g_return_if_fail (GST_IS_RTSP_AUTH (auth));
|
|
|
|
|
|
|
|
priv = auth->priv;
|
|
|
|
|
|
|
|
if (cert)
|
|
|
|
g_object_ref (cert);
|
|
|
|
|
|
|
|
g_mutex_lock (&priv->lock);
|
|
|
|
old = priv->certificate;
|
|
|
|
priv->certificate = cert;
|
|
|
|
g_mutex_unlock (&priv->lock);
|
|
|
|
|
|
|
|
if (old)
|
|
|
|
g_object_unref (old);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_rtsp_auth_get_tls_certificate:
|
|
|
|
* @auth: a #GstRTSPAuth
|
|
|
|
*
|
|
|
|
* Get the #GTlsCertificate used for negotiating TLS @auth.
|
|
|
|
*
|
|
|
|
* Returns: (transfer full): the #GTlsCertificate of @auth. g_object_unref() after
|
|
|
|
* usage.
|
|
|
|
*/
|
|
|
|
GTlsCertificate *
|
|
|
|
gst_rtsp_auth_get_tls_certificate (GstRTSPAuth * auth)
|
|
|
|
{
|
|
|
|
GstRTSPAuthPrivate *priv;
|
|
|
|
GTlsCertificate *result;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GST_IS_RTSP_AUTH (auth), NULL);
|
|
|
|
|
|
|
|
priv = auth->priv;
|
|
|
|
|
|
|
|
g_mutex_lock (&priv->lock);
|
|
|
|
if ((result = priv->certificate))
|
|
|
|
g_object_ref (result);
|
|
|
|
g_mutex_unlock (&priv->lock);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-11 23:17:54 +00:00
|
|
|
/**
|
2013-07-04 12:33:59 +00:00
|
|
|
* gst_rtsp_auth_add_basic:
|
2011-01-11 23:17:54 +00:00
|
|
|
* @auth: a #GstRTSPAuth
|
|
|
|
* @basic: the basic token
|
2013-07-11 10:18:26 +00:00
|
|
|
* @token: authorisation token
|
2011-01-11 23:17:54 +00:00
|
|
|
*
|
2013-07-04 12:33:59 +00:00
|
|
|
* Add a basic token for the default authentication algorithm that
|
2013-07-11 10:18:26 +00:00
|
|
|
* enables the client with privileges listed in @token.
|
2011-01-11 23:17:54 +00:00
|
|
|
*/
|
|
|
|
void
|
2013-07-04 12:33:59 +00:00
|
|
|
gst_rtsp_auth_add_basic (GstRTSPAuth * auth, const gchar * basic,
|
2013-07-05 18:48:18 +00:00
|
|
|
GstRTSPToken * token)
|
2011-01-11 23:17:54 +00:00
|
|
|
{
|
2012-11-29 10:11:05 +00:00
|
|
|
GstRTSPAuthPrivate *priv;
|
|
|
|
|
2012-11-12 15:03:21 +00:00
|
|
|
g_return_if_fail (GST_IS_RTSP_AUTH (auth));
|
2013-07-04 12:33:59 +00:00
|
|
|
g_return_if_fail (basic != NULL);
|
2013-07-05 18:48:18 +00:00
|
|
|
g_return_if_fail (GST_IS_RTSP_TOKEN (token));
|
2012-11-12 15:03:21 +00:00
|
|
|
|
2012-11-29 10:11:05 +00:00
|
|
|
priv = auth->priv;
|
|
|
|
|
|
|
|
g_mutex_lock (&priv->lock);
|
2013-07-05 18:48:18 +00:00
|
|
|
g_hash_table_replace (priv->basic, g_strdup (basic),
|
|
|
|
gst_rtsp_token_ref (token));
|
2013-07-04 12:33:59 +00:00
|
|
|
g_mutex_unlock (&priv->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_rtsp_auth_remove_basic:
|
|
|
|
* @auth: a #GstRTSPAuth
|
|
|
|
* @basic: (transfer none): the basic token
|
|
|
|
*
|
|
|
|
* Add a basic token for the default authentication algorithm that
|
|
|
|
* enables the client qith privileges from @authgroup.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_rtsp_auth_remove_basic (GstRTSPAuth * auth, const gchar * basic)
|
|
|
|
{
|
|
|
|
GstRTSPAuthPrivate *priv;
|
|
|
|
|
|
|
|
g_return_if_fail (GST_IS_RTSP_AUTH (auth));
|
|
|
|
g_return_if_fail (basic != NULL);
|
|
|
|
|
|
|
|
priv = auth->priv;
|
|
|
|
|
|
|
|
g_mutex_lock (&priv->lock);
|
|
|
|
g_hash_table_remove (priv->basic, basic);
|
2012-11-29 10:11:05 +00:00
|
|
|
g_mutex_unlock (&priv->lock);
|
2011-01-11 23:17:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2013-07-09 14:04:35 +00:00
|
|
|
default_authenticate (GstRTSPAuth * auth, GstRTSPClientState * state)
|
2011-01-11 23:17:54 +00:00
|
|
|
{
|
2012-11-29 10:11:05 +00:00
|
|
|
GstRTSPAuthPrivate *priv = auth->priv;
|
2011-01-11 23:17:54 +00:00
|
|
|
GstRTSPResult res;
|
2013-07-05 10:08:36 +00:00
|
|
|
gchar *authorization;
|
2011-01-11 23:17:54 +00:00
|
|
|
|
2013-07-05 18:48:18 +00:00
|
|
|
GST_DEBUG_OBJECT (auth, "authenticate");
|
2011-01-11 23:17:54 +00:00
|
|
|
|
2013-07-05 10:08:36 +00:00
|
|
|
res =
|
|
|
|
gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_AUTHORIZATION,
|
|
|
|
&authorization, 0);
|
|
|
|
if (res < 0)
|
|
|
|
goto no_auth;
|
2011-01-11 23:17:54 +00:00
|
|
|
|
2013-07-05 10:08:36 +00:00
|
|
|
/* parse type */
|
|
|
|
if (g_ascii_strncasecmp (authorization, "basic ", 6) == 0) {
|
2013-07-05 18:48:18 +00:00
|
|
|
GstRTSPToken *token;
|
2013-07-05 10:08:36 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (auth, "check Basic auth");
|
|
|
|
g_mutex_lock (&priv->lock);
|
2013-07-05 18:48:18 +00:00
|
|
|
if ((token = g_hash_table_lookup (priv->basic, &authorization[6]))) {
|
|
|
|
GST_DEBUG_OBJECT (auth, "setting token %p", token);
|
|
|
|
state->token = token;
|
2013-07-05 10:08:36 +00:00
|
|
|
}
|
|
|
|
g_mutex_unlock (&priv->lock);
|
|
|
|
} else if (g_ascii_strncasecmp (authorization, "digest ", 7) == 0) {
|
|
|
|
GST_DEBUG_OBJECT (auth, "check Digest auth");
|
|
|
|
/* not implemented yet */
|
|
|
|
}
|
|
|
|
return TRUE;
|
2011-01-11 23:17:54 +00:00
|
|
|
|
2013-07-05 10:08:36 +00:00
|
|
|
no_auth:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (auth, "no authorization header found");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
2013-07-04 12:33:59 +00:00
|
|
|
|
2013-07-15 09:56:06 +00:00
|
|
|
static void
|
|
|
|
send_response (GstRTSPAuth * auth, GstRTSPStatusCode code,
|
|
|
|
GstRTSPClientState * state)
|
|
|
|
{
|
|
|
|
gst_rtsp_message_init_response (state->response, code,
|
|
|
|
gst_rtsp_status_as_text (code), state->request);
|
|
|
|
|
|
|
|
if (code == GST_RTSP_STS_UNAUTHORIZED) {
|
|
|
|
/* we only have Basic for now */
|
|
|
|
gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_WWW_AUTHENTICATE,
|
|
|
|
"Basic realm=\"GStreamer RTSP Server\"");
|
|
|
|
}
|
|
|
|
gst_rtsp_client_send_message (state->client, state->session, state->response);
|
|
|
|
}
|
|
|
|
|
2013-07-05 10:08:36 +00:00
|
|
|
static gboolean
|
2013-07-09 14:04:35 +00:00
|
|
|
ensure_authenticated (GstRTSPAuth * auth, GstRTSPClientState * state)
|
2013-07-05 10:08:36 +00:00
|
|
|
{
|
|
|
|
GstRTSPAuthClass *klass;
|
|
|
|
|
|
|
|
klass = GST_RTSP_AUTH_GET_CLASS (auth);
|
|
|
|
|
2013-07-09 14:04:35 +00:00
|
|
|
/* we need a token to check */
|
|
|
|
if (state->token == NULL) {
|
|
|
|
if (klass->authenticate) {
|
|
|
|
if (!klass->authenticate (auth, state))
|
|
|
|
goto authenticate_failed;
|
2011-01-11 23:17:54 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-09 14:04:35 +00:00
|
|
|
if (state->token == NULL)
|
|
|
|
goto no_auth;
|
|
|
|
|
|
|
|
return TRUE;
|
2011-01-11 23:17:54 +00:00
|
|
|
|
2013-07-09 14:04:35 +00:00
|
|
|
/* ERRORS */
|
2013-07-05 18:48:18 +00:00
|
|
|
authenticate_failed:
|
2013-07-05 10:08:36 +00:00
|
|
|
{
|
2013-07-08 14:29:01 +00:00
|
|
|
GST_DEBUG_OBJECT (auth, "authenticate failed");
|
2013-07-15 09:56:06 +00:00
|
|
|
send_response (auth, GST_RTSP_STS_UNAUTHORIZED, state);
|
2013-07-05 10:08:36 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-01-11 23:17:54 +00:00
|
|
|
no_auth:
|
|
|
|
{
|
2013-07-08 14:29:01 +00:00
|
|
|
GST_DEBUG_OBJECT (auth, "no authorization token found");
|
2013-07-15 09:56:06 +00:00
|
|
|
send_response (auth, GST_RTSP_STS_UNAUTHORIZED, state);
|
2011-01-11 23:17:54 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-12 13:19:29 +00:00
|
|
|
/* new connection */
|
2013-07-09 14:04:35 +00:00
|
|
|
static gboolean
|
2013-07-12 13:19:29 +00:00
|
|
|
check_connect (GstRTSPAuth * auth, GstRTSPClientState * state,
|
2013-07-09 14:04:35 +00:00
|
|
|
const gchar * check)
|
|
|
|
{
|
|
|
|
GstRTSPAuthPrivate *priv = auth->priv;
|
2013-07-12 13:19:29 +00:00
|
|
|
|
|
|
|
if (priv->certificate) {
|
|
|
|
GTlsConnection *tls;
|
|
|
|
|
|
|
|
/* configure the connection */
|
|
|
|
tls = gst_rtsp_connection_get_tls (state->conn, NULL);
|
|
|
|
g_tls_connection_set_certificate (tls, priv->certificate);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check url and methods */
|
|
|
|
static gboolean
|
|
|
|
check_url (GstRTSPAuth * auth, GstRTSPClientState * state, const gchar * check)
|
|
|
|
{
|
|
|
|
GstRTSPAuthPrivate *priv = auth->priv;
|
|
|
|
|
|
|
|
if ((state->method & priv->methods) != 0)
|
|
|
|
if (!ensure_authenticated (auth, state))
|
|
|
|
goto not_authenticated;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
not_authenticated:
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check access to media factory */
|
|
|
|
static gboolean
|
|
|
|
check_factory (GstRTSPAuth * auth, GstRTSPClientState * state,
|
|
|
|
const gchar * check)
|
|
|
|
{
|
|
|
|
const gchar *role;
|
|
|
|
GstRTSPPermissions *perms;
|
|
|
|
|
2013-07-15 09:56:06 +00:00
|
|
|
if (!ensure_authenticated (auth, state))
|
|
|
|
return FALSE;
|
|
|
|
|
2013-07-12 13:19:29 +00:00
|
|
|
if (!(role = gst_rtsp_token_get_string (state->token,
|
2013-07-16 10:32:51 +00:00
|
|
|
GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE)))
|
2013-07-12 13:19:29 +00:00
|
|
|
goto no_media_role;
|
|
|
|
if (!(perms = gst_rtsp_media_factory_get_permissions (state->factory)))
|
|
|
|
goto no_permissions;
|
|
|
|
|
|
|
|
if (g_str_equal (check, "auth.check.media.factory.access")) {
|
|
|
|
if (!gst_rtsp_permissions_is_allowed (perms, role,
|
2013-07-16 10:32:51 +00:00
|
|
|
GST_RTSP_PERM_MEDIA_FACTORY_ACCESS))
|
2013-07-12 13:19:29 +00:00
|
|
|
goto no_access;
|
|
|
|
} else if (g_str_equal (check, "auth.check.media.factory.construct")) {
|
2013-07-12 14:01:14 +00:00
|
|
|
if (!gst_rtsp_permissions_is_allowed (perms, role,
|
2013-07-16 10:32:51 +00:00
|
|
|
GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT))
|
2013-07-12 13:19:29 +00:00
|
|
|
goto no_construct;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
no_media_role:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (auth, "no media factory role found");
|
|
|
|
send_response (auth, GST_RTSP_STS_UNAUTHORIZED, state);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
no_permissions:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (auth, "no permissions on media factory found");
|
|
|
|
send_response (auth, GST_RTSP_STS_UNAUTHORIZED, state);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
no_access:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (auth, "no permissions to access media factory");
|
|
|
|
send_response (auth, GST_RTSP_STS_NOT_FOUND, state);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
no_construct:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (auth, "no permissions to construct media factory");
|
|
|
|
send_response (auth, GST_RTSP_STS_UNAUTHORIZED, state);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-12 15:07:53 +00:00
|
|
|
static gboolean
|
|
|
|
check_client_settings (GstRTSPAuth * auth, GstRTSPClientState * state,
|
|
|
|
const gchar * check)
|
|
|
|
{
|
2013-07-15 09:56:06 +00:00
|
|
|
if (!ensure_authenticated (auth, state))
|
|
|
|
return FALSE;
|
|
|
|
|
2013-07-12 15:07:53 +00:00
|
|
|
return gst_rtsp_token_is_allowed (state->token,
|
2013-07-16 10:32:51 +00:00
|
|
|
GST_RTSP_TOKEN_TRANSPORT_CLIENT_SETTINGS);
|
2013-07-12 15:07:53 +00:00
|
|
|
}
|
|
|
|
|
2013-07-12 13:19:29 +00:00
|
|
|
static gboolean
|
|
|
|
default_check (GstRTSPAuth * auth, GstRTSPClientState * state,
|
|
|
|
const gchar * check)
|
|
|
|
{
|
2013-07-09 14:04:35 +00:00
|
|
|
gboolean res = FALSE;
|
|
|
|
|
2013-07-12 13:19:29 +00:00
|
|
|
/* FIXME, use hastable or so */
|
2013-07-12 10:41:52 +00:00
|
|
|
if (g_str_equal (check, GST_RTSP_AUTH_CHECK_CONNECT)) {
|
2013-07-12 13:19:29 +00:00
|
|
|
res = check_connect (auth, state, check);
|
2013-07-12 10:41:52 +00:00
|
|
|
} else if (g_str_equal (check, GST_RTSP_AUTH_CHECK_URL)) {
|
2013-07-12 13:19:29 +00:00
|
|
|
res = check_url (auth, state, check);
|
2013-07-09 14:04:35 +00:00
|
|
|
} else if (g_str_has_prefix (check, "auth.check.media.factory.")) {
|
2013-07-12 13:19:29 +00:00
|
|
|
res = check_factory (auth, state, check);
|
2013-07-12 15:07:53 +00:00
|
|
|
} else if (g_str_equal (check, GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS)) {
|
|
|
|
res = check_client_settings (auth, state, check);
|
2013-07-09 14:04:35 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-07-15 14:47:07 +00:00
|
|
|
static gboolean
|
|
|
|
no_auth_check (const gchar * check)
|
|
|
|
{
|
|
|
|
gboolean res;
|
|
|
|
|
|
|
|
if (g_str_equal (check, GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS))
|
|
|
|
res = FALSE;
|
|
|
|
else
|
|
|
|
res = TRUE;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2011-01-11 23:17:54 +00:00
|
|
|
/**
|
2012-04-13 13:27:22 +00:00
|
|
|
* gst_rtsp_auth_check:
|
2013-07-08 14:29:01 +00:00
|
|
|
* @check: the item to check
|
2011-01-11 23:17:54 +00:00
|
|
|
*
|
2013-07-09 14:04:35 +00:00
|
|
|
* Check if @check is allowed in the current context.
|
2011-01-11 23:17:54 +00:00
|
|
|
*
|
2013-07-05 10:08:36 +00:00
|
|
|
* Returns: FALSE if check failed.
|
2011-01-11 23:17:54 +00:00
|
|
|
*/
|
|
|
|
gboolean
|
2013-07-09 14:04:35 +00:00
|
|
|
gst_rtsp_auth_check (const gchar * check)
|
2011-01-11 23:17:54 +00:00
|
|
|
{
|
|
|
|
gboolean result = FALSE;
|
|
|
|
GstRTSPAuthClass *klass;
|
2013-07-09 14:04:35 +00:00
|
|
|
GstRTSPClientState *state;
|
|
|
|
GstRTSPAuth *auth;
|
2011-01-11 23:17:54 +00:00
|
|
|
|
2013-07-09 14:04:35 +00:00
|
|
|
g_return_val_if_fail (check != NULL, FALSE);
|
|
|
|
|
|
|
|
if (!(state = gst_rtsp_client_state_get_current ()))
|
|
|
|
goto no_state;
|
|
|
|
|
|
|
|
/* no auth, we don't need to check */
|
|
|
|
if (!(auth = state->auth))
|
2013-07-15 14:47:07 +00:00
|
|
|
return no_auth_check (check);
|
2012-11-12 15:03:21 +00:00
|
|
|
|
2011-01-11 23:17:54 +00:00
|
|
|
klass = GST_RTSP_AUTH_GET_CLASS (auth);
|
|
|
|
|
2013-07-10 13:28:35 +00:00
|
|
|
GST_DEBUG_OBJECT (auth, "check authorization '%s'", check);
|
2011-01-11 23:17:54 +00:00
|
|
|
|
2013-07-05 10:08:36 +00:00
|
|
|
if (klass->check)
|
2013-07-09 14:04:35 +00:00
|
|
|
result = klass->check (auth, state, check);
|
2011-01-11 23:17:54 +00:00
|
|
|
|
|
|
|
return result;
|
2013-07-09 14:04:35 +00:00
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
no_state:
|
|
|
|
{
|
|
|
|
GST_ERROR ("no clientstate found");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-01-11 23:17:54 +00:00
|
|
|
}
|
|
|
|
|
2011-01-12 09:41:42 +00:00
|
|
|
/**
|
|
|
|
* gst_rtsp_auth_make_basic:
|
|
|
|
* @user: a userid
|
|
|
|
* @pass: a password
|
|
|
|
*
|
|
|
|
* Construct a Basic authorisation token from @user and @pass.
|
|
|
|
*
|
|
|
|
* Returns: the base64 encoding of the string @user:@pass. g_free()
|
|
|
|
* after usage.
|
|
|
|
*/
|
2011-01-11 23:17:54 +00:00
|
|
|
gchar *
|
|
|
|
gst_rtsp_auth_make_basic (const gchar * user, const gchar * pass)
|
|
|
|
{
|
|
|
|
gchar *user_pass;
|
|
|
|
gchar *result;
|
|
|
|
|
2012-11-12 15:03:21 +00:00
|
|
|
g_return_val_if_fail (user != NULL, NULL);
|
|
|
|
g_return_val_if_fail (pass != NULL, NULL);
|
|
|
|
|
2011-01-11 23:17:54 +00:00
|
|
|
user_pass = g_strjoin (":", user, pass, NULL);
|
|
|
|
result = g_base64_encode ((guchar *) user_pass, strlen (user_pass));
|
|
|
|
g_free (user_pass);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|