2 * Copyright (C) 2010 Wim Taymans <wim.taymans at gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * @short_description: Authentication and authorization
22 * @see_also: #GstRTSPPermission, #GstRTSPtoken
24 * The #GstRTSPAuth object is responsible for checking if the current user is
25 * allowed to perform requested actions. The default implementation has some
26 * reasonable checks but subclasses can implement custom security policies.
28 * A new auth object is made with gst_rtsp_auth_new(). It is usually configured
29 * on the #GstRTSPServer object.
31 * The RTSP server will call gst_rtsp_auth_check() with a string describing the
32 * check to perform. The possible checks are prefixed with
33 * #GST_RTSP_AUTH_CHECK_*. Depending on the check, the default implementation
34 * will use the current #GstRTSPToken, #GstRTSPClientState and
35 * #GstRTSPPermissions on the object to check if an operation is allowed.
37 * The default #GstRTSPAuth object has support for basic authentication. With
38 * gst_rtsp_auth_add_basic() you can add a basic authentication string together
39 * with the #GstRTSPToken that will become active when successfully
42 * When a TLS certificate has been set with gst_rtsp_auth_set_tls_certificate(),
43 * the default auth object will require the client to connect with a TLS
46 * Last reviewed on 2013-07-16 (1.0.0)
51 #include "rtsp-auth.h"
53 #define GST_RTSP_AUTH_GET_PRIVATE(obj) \
54 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_AUTH, GstRTSPAuthPrivate))
56 struct _GstRTSPAuthPrivate
60 /* the TLS certificate */
61 GTlsCertificate *certificate;
62 GHashTable *basic; /* protected by lock */
63 GstRTSPMethod methods;
72 GST_DEBUG_CATEGORY_STATIC (rtsp_auth_debug);
73 #define GST_CAT_DEFAULT rtsp_auth_debug
75 static void gst_rtsp_auth_get_property (GObject * object, guint propid,
76 GValue * value, GParamSpec * pspec);
77 static void gst_rtsp_auth_set_property (GObject * object, guint propid,
78 const GValue * value, GParamSpec * pspec);
79 static void gst_rtsp_auth_finalize (GObject * obj);
81 static gboolean default_authenticate (GstRTSPAuth * auth,
82 GstRTSPClientState * state);
83 static gboolean default_check (GstRTSPAuth * auth, GstRTSPClientState * state,
86 G_DEFINE_TYPE (GstRTSPAuth, gst_rtsp_auth, G_TYPE_OBJECT);
89 gst_rtsp_auth_class_init (GstRTSPAuthClass * klass)
91 GObjectClass *gobject_class;
93 g_type_class_add_private (klass, sizeof (GstRTSPAuthPrivate));
95 gobject_class = G_OBJECT_CLASS (klass);
97 gobject_class->get_property = gst_rtsp_auth_get_property;
98 gobject_class->set_property = gst_rtsp_auth_set_property;
99 gobject_class->finalize = gst_rtsp_auth_finalize;
101 klass->authenticate = default_authenticate;
102 klass->check = default_check;
104 GST_DEBUG_CATEGORY_INIT (rtsp_auth_debug, "rtspauth", 0, "GstRTSPAuth");
108 gst_rtsp_auth_init (GstRTSPAuth * auth)
110 GstRTSPAuthPrivate *priv;
112 auth->priv = priv = GST_RTSP_AUTH_GET_PRIVATE (auth);
114 g_mutex_init (&priv->lock);
116 priv->basic = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
117 (GDestroyNotify) gst_rtsp_token_unref);
119 /* bitwise or of all methods that need authentication */
124 gst_rtsp_auth_finalize (GObject * obj)
126 GstRTSPAuth *auth = GST_RTSP_AUTH (obj);
127 GstRTSPAuthPrivate *priv = auth->priv;
129 GST_INFO ("finalize auth %p", auth);
131 if (priv->certificate)
132 g_object_unref (priv->certificate);
133 g_hash_table_unref (priv->basic);
134 g_mutex_clear (&priv->lock);
136 G_OBJECT_CLASS (gst_rtsp_auth_parent_class)->finalize (obj);
140 gst_rtsp_auth_get_property (GObject * object, guint propid,
141 GValue * value, GParamSpec * pspec)
145 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
150 gst_rtsp_auth_set_property (GObject * object, guint propid,
151 const GValue * value, GParamSpec * pspec)
155 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
162 * Create a new #GstRTSPAuth instance.
164 * Returns: a new #GstRTSPAuth
167 gst_rtsp_auth_new (void)
171 result = g_object_new (GST_TYPE_RTSP_AUTH, NULL);
177 * gst_rtsp_auth_set_tls_certificate:
178 * @auth: a #GstRTSPAuth
179 * @cert: (allow none): a #GTlsCertificate
181 * Set the TLS certificate for the auth. Client connections will only
182 * be accepted when TLS is negotiated.
185 gst_rtsp_auth_set_tls_certificate (GstRTSPAuth * auth, GTlsCertificate * cert)
187 GstRTSPAuthPrivate *priv;
188 GTlsCertificate *old;
190 g_return_if_fail (GST_IS_RTSP_AUTH (auth));
197 g_mutex_lock (&priv->lock);
198 old = priv->certificate;
199 priv->certificate = cert;
200 g_mutex_unlock (&priv->lock);
203 g_object_unref (old);
207 * gst_rtsp_auth_get_tls_certificate:
208 * @auth: a #GstRTSPAuth
210 * Get the #GTlsCertificate used for negotiating TLS @auth.
212 * Returns: (transfer full): the #GTlsCertificate of @auth. g_object_unref() after
216 gst_rtsp_auth_get_tls_certificate (GstRTSPAuth * auth)
218 GstRTSPAuthPrivate *priv;
219 GTlsCertificate *result;
221 g_return_val_if_fail (GST_IS_RTSP_AUTH (auth), NULL);
225 g_mutex_lock (&priv->lock);
226 if ((result = priv->certificate))
227 g_object_ref (result);
228 g_mutex_unlock (&priv->lock);
235 * gst_rtsp_auth_add_basic:
236 * @auth: a #GstRTSPAuth
237 * @basic: the basic token
238 * @token: authorisation token
240 * Add a basic token for the default authentication algorithm that
241 * enables the client with privileges listed in @token.
244 gst_rtsp_auth_add_basic (GstRTSPAuth * auth, const gchar * basic,
245 GstRTSPToken * token)
247 GstRTSPAuthPrivate *priv;
249 g_return_if_fail (GST_IS_RTSP_AUTH (auth));
250 g_return_if_fail (basic != NULL);
251 g_return_if_fail (GST_IS_RTSP_TOKEN (token));
255 g_mutex_lock (&priv->lock);
256 g_hash_table_replace (priv->basic, g_strdup (basic),
257 gst_rtsp_token_ref (token));
258 g_mutex_unlock (&priv->lock);
262 * gst_rtsp_auth_remove_basic:
263 * @auth: a #GstRTSPAuth
264 * @basic: (transfer none): the basic token
266 * Add a basic token for the default authentication algorithm that
267 * enables the client qith privileges from @authgroup.
270 gst_rtsp_auth_remove_basic (GstRTSPAuth * auth, const gchar * basic)
272 GstRTSPAuthPrivate *priv;
274 g_return_if_fail (GST_IS_RTSP_AUTH (auth));
275 g_return_if_fail (basic != NULL);
279 g_mutex_lock (&priv->lock);
280 g_hash_table_remove (priv->basic, basic);
281 g_mutex_unlock (&priv->lock);
285 default_authenticate (GstRTSPAuth * auth, GstRTSPClientState * state)
287 GstRTSPAuthPrivate *priv = auth->priv;
289 gchar *authorization;
291 GST_DEBUG_OBJECT (auth, "authenticate");
294 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_AUTHORIZATION,
300 if (g_ascii_strncasecmp (authorization, "basic ", 6) == 0) {
303 GST_DEBUG_OBJECT (auth, "check Basic auth");
304 g_mutex_lock (&priv->lock);
305 if ((token = g_hash_table_lookup (priv->basic, &authorization[6]))) {
306 GST_DEBUG_OBJECT (auth, "setting token %p", token);
307 state->token = token;
309 g_mutex_unlock (&priv->lock);
310 } else if (g_ascii_strncasecmp (authorization, "digest ", 7) == 0) {
311 GST_DEBUG_OBJECT (auth, "check Digest auth");
312 /* not implemented yet */
318 GST_DEBUG_OBJECT (auth, "no authorization header found");
324 send_response (GstRTSPAuth * auth, GstRTSPStatusCode code,
325 GstRTSPClientState * state)
327 gst_rtsp_message_init_response (state->response, code,
328 gst_rtsp_status_as_text (code), state->request);
330 if (code == GST_RTSP_STS_UNAUTHORIZED) {
331 /* we only have Basic for now */
332 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_WWW_AUTHENTICATE,
333 "Basic realm=\"GStreamer RTSP Server\"");
335 gst_rtsp_client_send_message (state->client, state->session, state->response);
339 ensure_authenticated (GstRTSPAuth * auth, GstRTSPClientState * state)
341 GstRTSPAuthClass *klass;
343 klass = GST_RTSP_AUTH_GET_CLASS (auth);
345 /* we need a token to check */
346 if (state->token == NULL) {
347 if (klass->authenticate) {
348 if (!klass->authenticate (auth, state))
349 goto authenticate_failed;
352 if (state->token == NULL)
360 GST_DEBUG_OBJECT (auth, "authenticate failed");
361 send_response (auth, GST_RTSP_STS_UNAUTHORIZED, state);
366 GST_DEBUG_OBJECT (auth, "no authorization token found");
367 send_response (auth, GST_RTSP_STS_UNAUTHORIZED, state);
374 check_connect (GstRTSPAuth * auth, GstRTSPClientState * state,
377 GstRTSPAuthPrivate *priv = auth->priv;
379 if (priv->certificate) {
382 /* configure the connection */
383 tls = gst_rtsp_connection_get_tls (state->conn, NULL);
384 g_tls_connection_set_certificate (tls, priv->certificate);
389 /* check url and methods */
391 check_url (GstRTSPAuth * auth, GstRTSPClientState * state, const gchar * check)
393 GstRTSPAuthPrivate *priv = auth->priv;
395 if ((state->method & priv->methods) != 0)
396 if (!ensure_authenticated (auth, state))
397 goto not_authenticated;
408 /* check access to media factory */
410 check_factory (GstRTSPAuth * auth, GstRTSPClientState * state,
414 GstRTSPPermissions *perms;
416 if (!ensure_authenticated (auth, state))
419 if (!(role = gst_rtsp_token_get_string (state->token,
420 GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE)))
422 if (!(perms = gst_rtsp_media_factory_get_permissions (state->factory)))
425 if (g_str_equal (check, "auth.check.media.factory.access")) {
426 if (!gst_rtsp_permissions_is_allowed (perms, role,
427 GST_RTSP_PERM_MEDIA_FACTORY_ACCESS))
429 } else if (g_str_equal (check, "auth.check.media.factory.construct")) {
430 if (!gst_rtsp_permissions_is_allowed (perms, role,
431 GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT))
439 GST_DEBUG_OBJECT (auth, "no media factory role found");
440 send_response (auth, GST_RTSP_STS_UNAUTHORIZED, state);
445 GST_DEBUG_OBJECT (auth, "no permissions on media factory found");
446 send_response (auth, GST_RTSP_STS_UNAUTHORIZED, state);
451 GST_DEBUG_OBJECT (auth, "no permissions to access media factory");
452 send_response (auth, GST_RTSP_STS_NOT_FOUND, state);
457 GST_DEBUG_OBJECT (auth, "no permissions to construct media factory");
458 send_response (auth, GST_RTSP_STS_UNAUTHORIZED, state);
464 check_client_settings (GstRTSPAuth * auth, GstRTSPClientState * state,
467 if (!ensure_authenticated (auth, state))
470 return gst_rtsp_token_is_allowed (state->token,
471 GST_RTSP_TOKEN_TRANSPORT_CLIENT_SETTINGS);
475 default_check (GstRTSPAuth * auth, GstRTSPClientState * state,
478 gboolean res = FALSE;
480 /* FIXME, use hastable or so */
481 if (g_str_equal (check, GST_RTSP_AUTH_CHECK_CONNECT)) {
482 res = check_connect (auth, state, check);
483 } else if (g_str_equal (check, GST_RTSP_AUTH_CHECK_URL)) {
484 res = check_url (auth, state, check);
485 } else if (g_str_has_prefix (check, "auth.check.media.factory.")) {
486 res = check_factory (auth, state, check);
487 } else if (g_str_equal (check, GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS)) {
488 res = check_client_settings (auth, state, check);
494 no_auth_check (const gchar * check)
498 if (g_str_equal (check, GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS))
507 * gst_rtsp_auth_check:
508 * @check: the item to check
510 * Check if @check is allowed in the current context.
512 * Returns: FALSE if check failed.
515 gst_rtsp_auth_check (const gchar * check)
517 gboolean result = FALSE;
518 GstRTSPAuthClass *klass;
519 GstRTSPClientState *state;
522 g_return_val_if_fail (check != NULL, FALSE);
524 if (!(state = gst_rtsp_client_state_get_current ()))
527 /* no auth, we don't need to check */
528 if (!(auth = state->auth))
529 return no_auth_check (check);
531 klass = GST_RTSP_AUTH_GET_CLASS (auth);
533 GST_DEBUG_OBJECT (auth, "check authorization '%s'", check);
536 result = klass->check (auth, state, check);
543 GST_ERROR ("no clientstate found");
549 * gst_rtsp_auth_make_basic:
553 * Construct a Basic authorisation token from @user and @pass.
555 * Returns: the base64 encoding of the string @user:@pass. g_free()
559 gst_rtsp_auth_make_basic (const gchar * user, const gchar * pass)
564 g_return_val_if_fail (user != NULL, NULL);
565 g_return_val_if_fail (pass != NULL, NULL);
567 user_pass = g_strjoin (":", user, pass, NULL);
568 result = g_base64_encode ((guchar *) user_pass, strlen (user_pass));