rtsp-auth: Add support for parsing .htdigest files
[platform/upstream/gstreamer.git] / examples / test-auth-digest.c
1 /* GStreamer
2  * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <gst/gst.h>
21
22 #include <gst/rtsp-server/rtsp-server.h>
23
24 static gchar *htdigest_path = NULL;
25 static gchar *realm = NULL;
26
27 static GOptionEntry entries[] = {
28   {"htdigest-path", 'h', 0, G_OPTION_ARG_STRING, &htdigest_path,
29       "Path to an htdigest file to parse (default: None)", "PATH"},
30   {"realm", 'r', 0, G_OPTION_ARG_STRING, &realm,
31       "Authentication realm (default: None)", "REALM"},
32   {NULL}
33 };
34
35
36 static gboolean
37 remove_func (GstRTSPSessionPool * pool, GstRTSPSession * session,
38     GstRTSPServer * server)
39 {
40   return GST_RTSP_FILTER_REMOVE;
41 }
42
43 static gboolean
44 remove_sessions (GstRTSPServer * server)
45 {
46   GstRTSPSessionPool *pool;
47
48   g_print ("removing all sessions\n");
49   pool = gst_rtsp_server_get_session_pool (server);
50   gst_rtsp_session_pool_filter (pool,
51       (GstRTSPSessionPoolFilterFunc) remove_func, server);
52   g_object_unref (pool);
53
54   return FALSE;
55 }
56
57 static gboolean
58 timeout (GstRTSPServer * server)
59 {
60   GstRTSPSessionPool *pool;
61
62   pool = gst_rtsp_server_get_session_pool (server);
63   gst_rtsp_session_pool_cleanup (pool);
64   g_object_unref (pool);
65
66   return TRUE;
67 }
68
69 int
70 main (int argc, char *argv[])
71 {
72   GMainLoop *loop;
73   GstRTSPServer *server;
74   GstRTSPMountPoints *mounts;
75   GstRTSPMediaFactory *factory;
76   GstRTSPAuth *auth;
77   GstRTSPToken *token;
78   GOptionContext *optctx;
79   GError *error = NULL;
80
81   optctx = g_option_context_new (NULL);
82   g_option_context_add_main_entries (optctx, entries, NULL);
83   g_option_context_add_group (optctx, gst_init_get_option_group ());
84   if (!g_option_context_parse (optctx, &argc, &argv, &error)) {
85     g_printerr ("Error parsing options: %s\n", error->message);
86     g_option_context_free (optctx);
87     g_clear_error (&error);
88     return -1;
89   }
90   g_option_context_free (optctx);
91
92   loop = g_main_loop_new (NULL, FALSE);
93
94   /* create a server instance */
95   server = gst_rtsp_server_new ();
96
97   /* get the mounts for this server, every server has a default mapper object
98    * that be used to map uri mount points to media factories */
99   mounts = gst_rtsp_server_get_mount_points (server);
100
101
102   /* make a media factory for a test stream. The default media factory can use
103    * gst-launch syntax to create pipelines. 
104    * any launch line works as long as it contains elements named pay%d. Each
105    * element with pay%d names will be a stream */
106   factory = gst_rtsp_media_factory_new ();
107   gst_rtsp_media_factory_set_launch (factory, "( "
108       "videotestsrc ! video/x-raw,width=352,height=288,framerate=15/1 ! "
109       "x264enc ! rtph264pay name=pay0 pt=96 "
110       "audiotestsrc ! audio/x-raw,rate=8000 ! "
111       "alawenc ! rtppcmapay name=pay1 pt=97 " ")");
112   /* attach the test factory to the /test url */
113   gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
114
115   /* allow user and admin to access this resource */
116   gst_rtsp_media_factory_add_role (factory, "user",
117       GST_RTSP_PERM_MEDIA_FACTORY_ACCESS, G_TYPE_BOOLEAN, TRUE,
118       GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, G_TYPE_BOOLEAN, TRUE, NULL);
119   gst_rtsp_media_factory_add_role (factory, "admin",
120       GST_RTSP_PERM_MEDIA_FACTORY_ACCESS, G_TYPE_BOOLEAN, TRUE,
121       GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, G_TYPE_BOOLEAN, TRUE, NULL);
122   /* admin2 can look at the media but not construct so he gets a
123    * 401 Unauthorized */
124   gst_rtsp_media_factory_add_role (factory, "admin2",
125       GST_RTSP_PERM_MEDIA_FACTORY_ACCESS, G_TYPE_BOOLEAN, TRUE,
126       GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, G_TYPE_BOOLEAN, FALSE, NULL);
127   /* Anonymous user can do the same things as admin2 on this resource */
128   gst_rtsp_media_factory_add_role (factory, "anonymous",
129       GST_RTSP_PERM_MEDIA_FACTORY_ACCESS, G_TYPE_BOOLEAN, TRUE,
130       GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, G_TYPE_BOOLEAN, FALSE, NULL);
131
132   /* make another factory */
133   factory = gst_rtsp_media_factory_new ();
134   gst_rtsp_media_factory_set_launch (factory, "( "
135       "videotestsrc ! video/x-raw,width=352,height=288,framerate=30/1 ! "
136       "x264enc ! rtph264pay name=pay0 pt=96 )");
137   /* attach the test factory to the /test url */
138   gst_rtsp_mount_points_add_factory (mounts, "/test2", factory);
139
140   /* allow admin2 to access this resource */
141   /* user and admin have no permissions so they can't even see the
142    * media and get a 404 Not Found */
143   gst_rtsp_media_factory_add_role (factory, "admin2",
144       GST_RTSP_PERM_MEDIA_FACTORY_ACCESS, G_TYPE_BOOLEAN, TRUE,
145       GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, G_TYPE_BOOLEAN, TRUE, NULL);
146
147   /* don't need the ref to the mapper anymore */
148   g_object_unref (mounts);
149
150   /* make a new authentication manager */
151   auth = gst_rtsp_auth_new ();
152   gst_rtsp_auth_set_supported_methods (auth, GST_RTSP_AUTH_DIGEST);
153
154   /* make default token, it has no permissions */
155   token =
156       gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
157       "anonymous", NULL);
158   gst_rtsp_auth_set_default_token (auth, token);
159   gst_rtsp_token_unref (token);
160
161   /* make user token */
162   token =
163       gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
164       "user", NULL);
165   gst_rtsp_auth_add_digest (auth, "user", "password", token);
166   gst_rtsp_token_unref (token);
167
168   if (htdigest_path) {
169     token =
170         gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
171         "user", NULL);
172
173     if (!gst_rtsp_auth_parse_htdigest (auth, htdigest_path, token)) {
174       g_printerr ("Could not parse htdigest at %s\n", htdigest_path);
175       gst_rtsp_token_unref (token);
176       goto failed;
177     }
178
179     gst_rtsp_token_unref (token);
180   }
181
182   if (realm)
183     gst_rtsp_auth_set_realm (auth, realm);
184
185   /* make admin token */
186   token =
187       gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
188       "admin", NULL);
189   gst_rtsp_auth_add_digest (auth, "admin", "power", token);
190   gst_rtsp_token_unref (token);
191
192   /* make admin2 token */
193   token =
194       gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
195       "admin2", NULL);
196   gst_rtsp_auth_add_digest (auth, "admin2", "power2", token);
197   gst_rtsp_token_unref (token);
198
199   /* set as the server authentication manager */
200   gst_rtsp_server_set_auth (server, auth);
201   g_object_unref (auth);
202
203   /* attach the server to the default maincontext */
204   if (gst_rtsp_server_attach (server, NULL) == 0)
205     goto failed;
206
207   g_timeout_add_seconds (2, (GSourceFunc) timeout, server);
208   g_timeout_add_seconds (10, (GSourceFunc) remove_sessions, server);
209
210   /* start serving */
211   g_print ("stream with user:password ready at rtsp://127.0.0.1:8554/test\n");
212   g_print ("stream with admin:power ready at rtsp://127.0.0.1:8554/test\n");
213   g_print ("stream with admin2:power2 ready at rtsp://127.0.0.1:8554/test2\n");
214
215   if (htdigest_path)
216     g_print ("stream with htdigest users ready at rtsp://127.0.0.1:8554/test\n");
217
218   g_main_loop_run (loop);
219
220   return 0;
221
222   /* ERRORS */
223 failed:
224   {
225     g_print ("failed to attach the server\n");
226     return -1;
227   }
228 }