dmap-share.c 56 KB
Newer Older
1
/* Implmentation of DMAP (e.g., iTunes Music or iPhoto Picture) sharing
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * Copyright (C) 2005 Charles Schmidt <cschmidt2@emich.edu>
 *
 * Modifications Copyright (C) 2008 W. Michael Petullo <mike@flyn.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "config.h"

#include <time.h>
#include <string.h>
#include <stdlib.h>

#include <glib/gi18n.h>

31
#include <libdmapsharing/dmap.h>
32 33 34 35 36 37 38 39
#include <libdmapsharing/dmap-structure.h>

#define TYPE_OF_SERVICE "_daap._tcp"
#define STANDARD_DAAP_PORT 3689
#define STANDARD_DPAP_PORT 8770
#define DMAP_VERSION 2.0
#define DAAP_VERSION 3.0
#define DMAP_TIMEOUT 1800
40
#define DMAP_STATUS_OK 200
41

W. Michael Petullo's avatar
W. Michael Petullo committed
42 43 44
typedef enum
{
	DMAP_SHARE_AUTH_METHOD_NONE = 0,
45
	DMAP_SHARE_AUTH_METHOD_NAME_AND_PASSWORD = 1,
W. Michael Petullo's avatar
W. Michael Petullo committed
46
	DMAP_SHARE_AUTH_METHOD_PASSWORD = 2
47 48
} DMAPShareAuthMethod;

W. Michael Petullo's avatar
W. Michael Petullo committed
49 50
enum
{
51
	PROP_0,
52 53 54
	/* I'd be nice to unify these, once libsoup supports it. See:
	 * http://mail.gnome.org/archives/libsoup-list/2011-January/msg00000.html
	 */
55 56
	PROP_SERVER_IPV4,
	PROP_SERVER_IPV6,
57 58 59 60
	PROP_NAME,
	PROP_PASSWORD,
	PROP_REVISION_NUMBER,
	PROP_AUTH_METHOD,
61 62
	PROP_DB,
	PROP_CONTAINER_DB,
63 64
	PROP_TRANSCODE_MIMETYPE,
	PROP_TXT_RECORDS
65 66
};

W. Michael Petullo's avatar
W. Michael Petullo committed
67 68
struct DMAPSharePrivate
{
69 70 71
	gchar *name;
	guint port;
	char *password;
72 73 74 75

	/* FIXME: eventually, this should be determined dynamically, based
	 * on what client has connected and its supported mimetypes.
	 */
76
	char *transcode_mimetype;
77

78 79
	DMAPShareAuthMethod auth_method;

80
	/* mDNS/DNS-SD publishing things */
81 82
	gboolean server_active;
	gboolean published;
83
	DMAPMdnsPublisher *publisher;
84

85
	/* HTTP server things */
86 87
	SoupServer *server_ipv4;
	SoupServer *server_ipv6;
88 89
	guint revision_number;

90 91 92
	/* The media database */
	DMAPDb *db;
	DMAPContainerDb *container_db;
W. Michael Petullo's avatar
W. Michael Petullo committed
93

94 95
	/* TXT-RECORDS published by mDNS */
	gchar **txt_records;
96

97 98 99
	GHashTable *session_ids;
};

100
/* FIXME: name this something else, as it is more than just share/bitwise now */
W. Michael Petullo's avatar
W. Michael Petullo committed
101 102 103
struct share_bitwise_t
{
	SoupServer *server;	/* Also in share, but we need to know whether server_ipv6 or _ipv4. */
104
	struct MLCL_Bits mb;
105
	GSList *id_list;
106
	guint32 size;
107 108 109 110 111 112 113

	/* FIXME: ick, void * is DMAPDDb * or GHashTable * 
	 * in next two fields:*/
	void *db;
	DMAPRecord *(*lookup_by_id) (void *db, guint id);

	void (*destroy) (void *);
114 115
};

W. Michael Petullo's avatar
W. Michael Petullo committed
116 117
static void dmap_share_init (DMAPShare * share);
static void dmap_share_class_init (DMAPShareClass * klass);
118 119 120

G_DEFINE_ABSTRACT_TYPE (DMAPShare, dmap_share, G_TYPE_OBJECT)
#define DMAP_SHARE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
121
				   DMAP_TYPE_SHARE, DMAPSharePrivate))
W. Michael Petullo's avatar
W. Michael Petullo committed
122 123 124 125 126 127
     static gboolean
	     _dmap_share_soup_auth_callback (SoupAuthDomain * auth_domain,
					     SoupMessage * msg,
					     const char *username,
					     gpointer password,
					     DMAPShare * share)
128
{
W. Michael Petullo's avatar
W. Michael Petullo committed
129
	gboolean allowed;
130 131 132 133 134 135 136 137 138 139 140
	const char *path;

	path = soup_message_get_uri (msg)->path;
	g_debug ("Auth request for %s, user %s", path, username);

	allowed = !strcmp (password, share->priv->password);
	g_debug ("Auth request: %s", allowed ? "ALLOWED" : "DENIED");

	return allowed;
}

W. Michael Petullo's avatar
W. Michael Petullo committed
141 142 143 144 145 146
static void
server_info_adapter (SoupServer * server,
		     SoupMessage * message,
		     const char *path,
		     GHashTable * query,
		     SoupClientContext * context, DMAPShare * share)
147 148 149 150
{
	DMAP_SHARE_GET_CLASS (share)->server_info (share,
						   server,
						   message,
W. Michael Petullo's avatar
W. Michael Petullo committed
151
						   path, query, context);
152 153
}

W. Michael Petullo's avatar
W. Michael Petullo committed
154 155 156 157 158 159
static void
content_codes_adapter (SoupServer * server,
		       SoupMessage * message,
		       const char *path,
		       GHashTable * query,
		       SoupClientContext * context, DMAPShare * share)
160 161 162 163
{
	DMAP_SHARE_GET_CLASS (share)->content_codes (share,
						     server,
						     message,
W. Michael Petullo's avatar
W. Michael Petullo committed
164
						     path, query, context);
165 166
}

W. Michael Petullo's avatar
W. Michael Petullo committed
167 168 169 170 171 172
static void
login_adapter (SoupServer * server,
	       SoupMessage * message,
	       const char *path,
	       GHashTable * query,
	       SoupClientContext * context, DMAPShare * share)
173 174 175
{
	DMAP_SHARE_GET_CLASS (share)->login (share,
					     server,
W. Michael Petullo's avatar
W. Michael Petullo committed
176
					     message, path, query, context);
177 178
}

W. Michael Petullo's avatar
W. Michael Petullo committed
179 180 181 182 183 184
static void
logout_adapter (SoupServer * server,
		SoupMessage * message,
		const char *path,
		GHashTable * query,
		SoupClientContext * context, DMAPShare * share)
185 186 187
{
	DMAP_SHARE_GET_CLASS (share)->logout (share,
					      server,
W. Michael Petullo's avatar
W. Michael Petullo committed
188
					      message, path, query, context);
189 190
}

W. Michael Petullo's avatar
W. Michael Petullo committed
191 192 193 194 195 196
static void
update_adapter (SoupServer * server,
		SoupMessage * message,
		const char *path,
		GHashTable * query,
		SoupClientContext * context, DMAPShare * share)
197 198 199
{
	DMAP_SHARE_GET_CLASS (share)->update (share,
					      server,
W. Michael Petullo's avatar
W. Michael Petullo committed
200
					      message, path, query, context);
201 202
}

W. Michael Petullo's avatar
W. Michael Petullo committed
203 204 205 206 207 208
static void
databases_adapter (SoupServer * server,
		   SoupMessage * message,
		   const char *path,
		   GHashTable * query,
		   SoupClientContext * context, DMAPShare * share)
209 210 211 212
{
	DMAP_SHARE_GET_CLASS (share)->databases (share,
						 server,
						 message,
W. Michael Petullo's avatar
W. Michael Petullo committed
213
						 path, query, context);
214 215
}

W. Michael Petullo's avatar
W. Michael Petullo committed
216 217 218 219 220 221
static void
ctrl_int_adapter (SoupServer * server,
		  SoupMessage * message,
		  const char *path,
		  GHashTable * query,
		  SoupClientContext * context, DMAPShare * share)
222 223
{
	DMAP_SHARE_GET_CLASS (share)->ctrl_int (share,
W. Michael Petullo's avatar
W. Michael Petullo committed
224 225 226
						server,
						message,
						path, query, context);
227 228
}

229
static void
W. Michael Petullo's avatar
W. Michael Petullo committed
230
_dmap_share_server_setup_handlers (DMAPShare * share, SoupServer * server)
231
{
W. Michael Petullo's avatar
W. Michael Petullo committed
232 233
	gboolean password_required =
		(share->priv->auth_method != DMAP_SHARE_AUTH_METHOD_NONE);
234 235 236 237

	if (password_required) {
		SoupAuthDomain *auth_domain;

W. Michael Petullo's avatar
W. Michael Petullo committed
238 239 240 241 242 243 244 245 246 247 248 249
		auth_domain =
			soup_auth_domain_basic_new (SOUP_AUTH_DOMAIN_REALM,
						    "Music Sharing",
						    SOUP_AUTH_DOMAIN_ADD_PATH,
						    "/login",
						    SOUP_AUTH_DOMAIN_ADD_PATH,
						    "/update",
						    SOUP_AUTH_DOMAIN_ADD_PATH,
						    "/database",
						    SOUP_AUTH_DOMAIN_FILTER,
						    _dmap_share_soup_auth_filter,
						    NULL);
250
		soup_auth_domain_basic_set_auth_callback (auth_domain,
W. Michael Petullo's avatar
W. Michael Petullo committed
251 252 253 254
							  (SoupAuthDomainBasicAuthCallback)
							  _dmap_share_soup_auth_callback,
							  g_object_ref
							  (share),
255
							  g_object_unref);
256
		soup_server_add_auth_domain (server, auth_domain);
257 258
	}

259
	soup_server_add_handler (server, "/server-info",
260 261
				 (SoupServerCallback) server_info_adapter,
				 share, NULL);
262
	soup_server_add_handler (server, "/content-codes",
263 264
				 (SoupServerCallback) content_codes_adapter,
				 share, NULL);
265
	soup_server_add_handler (server, "/login",
266 267
				 (SoupServerCallback) login_adapter,
				 share, NULL);
268
	soup_server_add_handler (server, "/logout",
269 270
				 (SoupServerCallback) logout_adapter,
				 share, NULL);
271
	soup_server_add_handler (server, "/update",
272 273
				 (SoupServerCallback) update_adapter,
				 share, NULL);
274
	soup_server_add_handler (server, "/databases",
275 276
				 (SoupServerCallback) databases_adapter,
				 share, NULL);
277
	soup_server_add_handler (server, "/ctrl-int",
278 279
				 (SoupServerCallback) ctrl_int_adapter,
				 share, NULL);
280 281 282 283 284
	soup_server_run_async (server);

}

gboolean
W. Michael Petullo's avatar
W. Michael Petullo committed
285
_dmap_share_server_start (DMAPShare * share)
286 287 288 289 290
{
	SoupAddress *addr;
	guint port = DMAP_SHARE_GET_CLASS (share)->get_desired_port (share);

	addr = soup_address_new_any (SOUP_ADDRESS_FAMILY_IPV6, port);
W. Michael Petullo's avatar
W. Michael Petullo committed
291 292
	share->priv->server_ipv6 =
		soup_server_new (SOUP_SERVER_INTERFACE, addr, NULL);
293 294 295 296 297 298 299
	g_object_unref (addr);

	/* NOTE: On Linux, opening a socket may give a IPv6-wrapped IPv4 address.
	 * in this case, the server_ipv6 object will service requests from both
	 * IPv6 and IPv4 clients.
	 */
	if (share->priv->server_ipv6 == NULL) {
300
		g_debug
W. Michael Petullo's avatar
W. Michael Petullo committed
301 302 303 304 305 306
			("Unable to start music sharing server on port %d, trying any open port",
			 port);
		addr = soup_address_new_any (SOUP_ADDRESS_FAMILY_IPV6,
					     SOUP_ADDRESS_ANY_PORT);
		share->priv->server_ipv6 =
			soup_server_new (SOUP_SERVER_INTERFACE, addr, NULL);
307 308 309 310 311 312 313
		g_object_unref (addr);
	}

	if (share->priv->server_ipv6 != NULL) {
		/* Use same port for IPv4 as IPv6. */
		port = soup_server_get_port (share->priv->server_ipv6);
	} else {
314
		g_debug ("Unable to start music sharing server (IPv6)");
315 316 317 318 319 320 321
	}

	/* NOTE: In the case mentioned above, this will fail as the server_ipv6 is already
	 * servicing IPv4 requests. In this case server_ipv6 handles both IPv6 and IPv4
	 * and server_ipv4 is NULL.
	 */
	addr = soup_address_new_any (SOUP_ADDRESS_FAMILY_IPV4, port);
W. Michael Petullo's avatar
W. Michael Petullo committed
322 323
	share->priv->server_ipv4 =
		soup_server_new (SOUP_SERVER_INTERFACE, addr, NULL);
324 325 326 327
	g_object_unref (addr);

	/* Don't try any port on IPv4 unless IPv6 failed. We don't want to listen to one
	 * port on IPv6 and another on IPv4 */
W. Michael Petullo's avatar
W. Michael Petullo committed
328 329
	if (share->priv->server_ipv6 == NULL
	    && share->priv->server_ipv4 == NULL) {
330
		g_debug
W. Michael Petullo's avatar
W. Michael Petullo committed
331 332 333 334 335 336
			("Unable to start music sharing server on port %d, trying IPv4 only, any open port",
			 port);
		addr = soup_address_new_any (SOUP_ADDRESS_FAMILY_IPV4,
					     SOUP_ADDRESS_ANY_PORT);
		share->priv->server_ipv4 =
			soup_server_new (SOUP_SERVER_INTERFACE, addr, NULL);
337 338 339 340
		g_object_unref (addr);
	}

	if (share->priv->server_ipv4 == NULL) {
341
		g_debug ("Unable to start music sharing server (IPv4)");
342
		if (share->priv->server_ipv6 == NULL) {
343
			g_warning ("Unable to start music sharing server (both IPv4 and IPv6 failed)");
344 345 346 347 348
			return FALSE;
		}
	}

	if (share->priv->server_ipv6)
W. Michael Petullo's avatar
W. Michael Petullo committed
349 350 351
		share->priv->port =
			(guint) soup_server_get_port (share->priv->
						      server_ipv6);
352
	else
W. Michael Petullo's avatar
W. Michael Petullo committed
353 354 355
		share->priv->port =
			(guint) soup_server_get_port (share->priv->
						      server_ipv4);
356 357 358 359 360

	g_debug ("Started DMAP server on port %u (IPv6: %s, explicit IPv4: %s)",
	          share->priv->port,
		  share->priv->server_ipv6 ? "yes" : "no",
		  share->priv->server_ipv4 ? "yes" : "no");
361 362

	if (share->priv->server_ipv6)
W. Michael Petullo's avatar
W. Michael Petullo committed
363 364
		_dmap_share_server_setup_handlers (share,
						   share->priv->server_ipv6);
365 366

	if (share->priv->server_ipv4)
W. Michael Petullo's avatar
W. Michael Petullo committed
367 368
		_dmap_share_server_setup_handlers (share,
						   share->priv->server_ipv4);
369 370

	/* using direct since there is no g_uint_hash or g_uint_equal */
W. Michael Petullo's avatar
W. Michael Petullo committed
371 372 373
	share->priv->session_ids =
		g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL,
				       g_free);
374 375 376 377 378 379 380

	share->priv->server_active = TRUE;

	return TRUE;
}

static gboolean
W. Michael Petullo's avatar
W. Michael Petullo committed
381
_dmap_share_server_stop (DMAPShare * share)
382
{
W. Michael Petullo's avatar
W. Michael Petullo committed
383 384
	g_debug ("Stopping music sharing server on port %d",
		 share->priv->port);
385

386 387 388 389 390 391 392 393 394 395
	if (share->priv->server_ipv4) {
		soup_server_quit (share->priv->server_ipv4);
		g_object_unref (share->priv->server_ipv4);
		share->priv->server_ipv4 = NULL;
	}

	if (share->priv->server_ipv6) {
		soup_server_quit (share->priv->server_ipv6);
		g_object_unref (share->priv->server_ipv6);
		share->priv->server_ipv6 = NULL;
396 397 398 399 400 401 402 403 404 405 406 407 408
	}

	if (share->priv->session_ids) {
		g_hash_table_destroy (share->priv->session_ids);
		share->priv->session_ids = NULL;
	}

	share->priv->server_active = FALSE;

	return TRUE;
}

gboolean
W. Michael Petullo's avatar
W. Michael Petullo committed
409
_dmap_share_publish_start (DMAPShare * share)
410 411
{
	gchar *nameprop;
W. Michael Petullo's avatar
W. Michael Petullo committed
412
	GError *error;
413 414 415 416 417 418
	gboolean res;
	gboolean password_required;

	/* FIXME: this is done throughout dmap-share.c. Is this the best way? */
	g_object_get ((gpointer) share, "name", &nameprop, NULL);

W. Michael Petullo's avatar
W. Michael Petullo committed
419 420
	password_required =
		(share->priv->auth_method != DMAP_SHARE_AUTH_METHOD_NONE);
421 422 423

	error = NULL;
	res = dmap_mdns_publisher_publish (share->priv->publisher,
W. Michael Petullo's avatar
W. Michael Petullo committed
424 425 426 427 428 429
					   nameprop,
					   share->priv->port,
					   DMAP_SHARE_GET_CLASS (share)->
					   get_type_of_service (share),
					   password_required,
					   share->priv->txt_records, &error);
430 431 432

	if (res == FALSE) {
		if (error != NULL) {
W. Michael Petullo's avatar
W. Michael Petullo committed
433 434 435
			g_warning
				("Unable to notify network of media sharing: %s",
				 error->message);
436 437
			g_error_free (error);
		} else {
W. Michael Petullo's avatar
W. Michael Petullo committed
438 439
			g_warning
				("Unable to notify network of media sharing");
440 441 442 443 444 445 446 447 448 449 450 451
		}
		return FALSE;
	} else {
		g_debug ("Published DMAP server information to mdns");
	}

	g_free (nameprop);

	return TRUE;
}

static gboolean
W. Michael Petullo's avatar
W. Michael Petullo committed
452
_dmap_share_publish_stop (DMAPShare * share)
453 454 455
{
	if (share->priv->publisher) {
		gboolean res;
W. Michael Petullo's avatar
W. Michael Petullo committed
456 457
		GError *error;

458
		error = NULL;
W. Michael Petullo's avatar
W. Michael Petullo committed
459 460 461
		res = dmap_mdns_publisher_withdraw (share->priv->publisher,
						    share->priv->port,
						    &error);
462
		if (error != NULL) {
W. Michael Petullo's avatar
W. Michael Petullo committed
463 464 465
			g_warning
				("Unable to withdraw music sharing service: %s",
				 error->message);
466 467 468 469 470 471 472 473 474 475
			g_error_free (error);
		}
		return res;
	}

	share->priv->published = FALSE;
	return TRUE;
}

static void
W. Michael Petullo's avatar
W. Michael Petullo committed
476
_dmap_share_restart (DMAPShare * share)
477 478 479
{
	gboolean res;

480 481
	_dmap_share_server_stop (share);
	res = _dmap_share_server_start (share);
482 483
	if (res) {
		/* To update information just publish again */
484
		_dmap_share_publish_start (share);
485
	} else {
486
		_dmap_share_publish_stop (share);
487 488 489 490
	}
}

static void
W. Michael Petullo's avatar
W. Michael Petullo committed
491
_dmap_share_maybe_restart (DMAPShare * share)
492 493
{
	if (share->priv->published) {
494
		_dmap_share_restart (share);
495 496 497 498
	}
}

static void
W. Michael Petullo's avatar
W. Michael Petullo committed
499
_dmap_share_set_name (DMAPShare * share, const char *name)
500 501 502 503 504 505 506 507 508
{
	GError *error;
	gboolean res;

	g_return_if_fail (share != NULL);

	g_free (share->priv->name);
	share->priv->name = g_strdup (name);

509 510
	if (share->priv->published) {
		error = NULL;
W. Michael Petullo's avatar
W. Michael Petullo committed
511 512
		res = dmap_mdns_publisher_rename_at_port (share->priv->
							  publisher,
513
							  share->priv->port,
W. Michael Petullo's avatar
W. Michael Petullo committed
514
							  name, &error);
515 516 517 518 519
		if (error != NULL) {
			g_warning ("Unable to change MDNS service name: %s",
				   error->message);
			g_error_free (error);
		}
520 521 522 523
	}
}

static void
W. Michael Petullo's avatar
W. Michael Petullo committed
524
_dmap_share_set_password (DMAPShare * share, const char *password)
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
{
	g_return_if_fail (share != NULL);

	if (share->priv->password && password &&
	    strcmp (password, share->priv->password) == 0) {
		return;
	}

	g_free (share->priv->password);
	share->priv->password = g_strdup (password);
	if (password != NULL) {
		share->priv->auth_method = DMAP_SHARE_AUTH_METHOD_PASSWORD;
	} else {
		share->priv->auth_method = DMAP_SHARE_AUTH_METHOD_NONE;
	}

541
	_dmap_share_maybe_restart (share);
542 543 544
}

static void
W. Michael Petullo's avatar
W. Michael Petullo committed
545 546 547
_dmap_share_set_property (GObject * object,
			  guint prop_id,
			  const GValue * value, GParamSpec * pspec)
548 549 550 551 552
{
	DMAPShare *share = DMAP_SHARE (object);

	switch (prop_id) {
	case PROP_NAME:
553
		_dmap_share_set_name (share, g_value_get_string (value));
554 555
		break;
	case PROP_PASSWORD:
556
		_dmap_share_set_password (share, g_value_get_string (value));
557
		break;
558
	case PROP_DB:
W. Michael Petullo's avatar
W. Michael Petullo committed
559 560 561 562 563 564 565
		share->priv->db = (DMAPDb *) g_value_get_pointer (value);
		break;
	case PROP_CONTAINER_DB:
		share->priv->container_db =
			(DMAPContainerDb *) g_value_get_pointer (value);
		break;
	case PROP_TRANSCODE_MIMETYPE:
566
		/* FIXME: get or dup? */
W. Michael Petullo's avatar
W. Michael Petullo committed
567 568 569 570 571
		share->priv->transcode_mimetype = g_value_dup_string (value);
		break;
	case PROP_TXT_RECORDS:
		share->priv->txt_records = g_value_dup_boxed (value);
		break;
572 573 574 575 576 577 578
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
W. Michael Petullo's avatar
W. Michael Petullo committed
579 580
_dmap_share_get_property (GObject * object,
			  guint prop_id, GValue * value, GParamSpec * pspec)
581 582 583 584
{
	DMAPShare *share = DMAP_SHARE (object);

	switch (prop_id) {
585 586 587 588 589
	case PROP_SERVER_IPV4:
		g_value_set_object (value, share->priv->server_ipv4);
		return;
	case PROP_SERVER_IPV6:
		g_value_set_object (value, share->priv->server_ipv6);
590
		return;
591 592 593 594 595 596 597 598
	case PROP_NAME:
		g_value_set_string (value, share->priv->name);
		break;
	case PROP_PASSWORD:
		g_value_set_string (value, share->priv->password);
		break;
	case PROP_REVISION_NUMBER:
		g_value_set_uint (value,
599
				  _dmap_share_get_revision_number
W. Michael Petullo's avatar
W. Michael Petullo committed
600
				  (DMAP_SHARE (object)));
601 602 603
		break;
	case PROP_AUTH_METHOD:
		g_value_set_uint (value,
604
				  _dmap_share_get_auth_method
W. Michael Petullo's avatar
W. Michael Petullo committed
605
				  (DMAP_SHARE (object)));
606
		break;
607
	case PROP_DB:
W. Michael Petullo's avatar
W. Michael Petullo committed
608 609 610 611 612 613 614 615 616 617 618
		g_value_set_pointer (value, share->priv->db);
		break;
	case PROP_CONTAINER_DB:
		g_value_set_pointer (value, share->priv->container_db);
		break;
	case PROP_TRANSCODE_MIMETYPE:
		g_value_set_string (value, share->priv->transcode_mimetype);
		break;
	case PROP_TXT_RECORDS:
		g_value_set_boxed (value, share->priv->txt_records);
		break;
619 620 621 622 623 624 625
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
W. Michael Petullo's avatar
W. Michael Petullo committed
626
_dmap_share_finalize (GObject * object)
627 628 629
{
	DMAPShare *share = DMAP_SHARE (object);

630 631
	g_debug ("Finalizing DMAPShare");

632
	if (share->priv->published) {
633
		_dmap_share_publish_stop (share);
634 635 636
	}

	if (share->priv->server_active) {
637
		_dmap_share_server_stop (share);
638 639 640
	}

	g_free (share->priv->name);
641 642 643 644
	g_free (share->priv->password);

	g_object_unref (share->priv->db);
	g_object_unref (share->priv->container_db);
W. Michael Petullo's avatar
W. Michael Petullo committed
645

646
	g_strfreev (share->priv->txt_records);
647 648 649 650 651

	if (share->priv->publisher) {
		g_object_unref (share->priv->publisher);
	}

652
	G_OBJECT_CLASS (dmap_share_parent_class)->finalize (object);
653 654 655
}

static void
W. Michael Petullo's avatar
W. Michael Petullo committed
656
dmap_share_class_init (DMAPShareClass * klass)
657 658 659
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

660 661
	object_class->get_property = _dmap_share_get_property;
	object_class->set_property = _dmap_share_set_property;
W. Michael Petullo's avatar
W. Michael Petullo committed
662
	object_class->finalize = _dmap_share_finalize;
663 664

	/* Pure virtual methods: */
W. Michael Petullo's avatar
W. Michael Petullo committed
665 666
	klass->get_desired_port = NULL;
	klass->get_type_of_service = NULL;
667
	klass->message_add_standard_headers = NULL;
W. Michael Petullo's avatar
W. Michael Petullo committed
668 669
	klass->get_meta_data_map = NULL;
	klass->add_entry_to_mlcl = NULL;
670
	klass->databases_browse_xxx = NULL;
W. Michael Petullo's avatar
W. Michael Petullo committed
671
	klass->databases_items_xxx = NULL;
672 673

	/* Virtual methods: */
W. Michael Petullo's avatar
W. Michael Petullo committed
674 675 676 677 678
	klass->content_codes = _dmap_share_content_codes;
	klass->login = _dmap_share_login;
	klass->logout = _dmap_share_logout;
	klass->update = _dmap_share_update;
	klass->published = _dmap_share_published;
679
	klass->name_collision = _dmap_share_name_collision;
W. Michael Petullo's avatar
W. Michael Petullo committed
680 681
	klass->databases = _dmap_share_databases;
	klass->ctrl_int = _dmap_share_ctrl_int;
682

683
	g_object_class_install_property (object_class,
684 685 686 687 688 689
					 PROP_SERVER_IPV4,
					 g_param_spec_object ("server-ipv4",
							      "Soup Server",
							      "Soup server",
							      SOUP_TYPE_SERVER,
							      G_PARAM_READABLE));
W. Michael Petullo's avatar
W. Michael Petullo committed
690

691 692 693
	g_object_class_install_property (object_class,
					 PROP_SERVER_IPV6,
					 g_param_spec_object ("server-ipv6",
694 695 696 697
							      "Soup Server",
							      "Soup server",
							      SOUP_TYPE_SERVER,
							      G_PARAM_READABLE));
W. Michael Petullo's avatar
W. Michael Petullo committed
698

699 700 701
	g_object_class_install_property (object_class,
					 PROP_NAME,
					 g_param_spec_string ("name",
W. Michael Petullo's avatar
W. Michael Petullo committed
702 703 704 705
							      "Name",
							      "Share Name",
							      NULL,
							      G_PARAM_READWRITE));
706 707 708
	g_object_class_install_property (object_class,
					 PROP_PASSWORD,
					 g_param_spec_string ("password",
W. Michael Petullo's avatar
W. Michael Petullo committed
709 710 711 712
							      "Authentication password",
							      "Authentication password",
							      NULL,
							      G_PARAM_READWRITE));
713 714 715 716

	g_object_class_install_property (object_class,
					 PROP_REVISION_NUMBER,
					 g_param_spec_uint ("revision_number",
W. Michael Petullo's avatar
W. Michael Petullo committed
717
							    "Revision number",
718 719 720 721 722 723 724 725 726
							    "Revision number",
							    0,
							    G_MAXINT,
							    0,
							    G_PARAM_READWRITE));

	g_object_class_install_property (object_class,
					 PROP_AUTH_METHOD,
					 g_param_spec_uint ("auth_method",
W. Michael Petullo's avatar
W. Michael Petullo committed
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
							    "Authentication method",
							    "Authentication method",
							    DMAP_SHARE_AUTH_METHOD_NONE,
							    DMAP_SHARE_AUTH_METHOD_PASSWORD,
							    0,
							    G_PARAM_READWRITE));
	g_object_class_install_property (object_class,
					 PROP_DB,
					 g_param_spec_pointer ("db",
							       "DB",
							       "DB object",
							       G_PARAM_READWRITE
							       |
							       G_PARAM_CONSTRUCT_ONLY));

742
	g_object_class_install_property (object_class,
W. Michael Petullo's avatar
W. Michael Petullo committed
743 744 745 746 747 748 749
					 PROP_CONTAINER_DB,
					 g_param_spec_pointer ("container-db",
							       "Container DB",
							       "Container DB object",
							       G_PARAM_READWRITE
							       |
							       G_PARAM_CONSTRUCT_ONLY));
750

751
	g_object_class_install_property (object_class,
W. Michael Petullo's avatar
W. Michael Petullo committed
752 753 754 755 756 757 758 759
					 PROP_TRANSCODE_MIMETYPE,
					 g_param_spec_string
					 ("transcode-mimetype",
					  "Transcode mimetype",
					  "Set mimetype of stream after transcoding",
					  NULL,
					  G_PARAM_READWRITE |
					  G_PARAM_CONSTRUCT_ONLY));
760 761

	g_object_class_install_property (object_class,
W. Michael Petullo's avatar
W. Michael Petullo committed
762 763 764 765 766 767
					 PROP_TXT_RECORDS,
					 g_param_spec_boxed ("txt-records",
							     "TXT-Records",
							     "Set TXT-Records used for MDNS publishing",
							     G_TYPE_STRV,
							     G_PARAM_READWRITE));
768 769 770 771

	g_type_class_add_private (klass, sizeof (DMAPSharePrivate));
}

W. Michael Petullo's avatar
W. Michael Petullo committed
772 773 774
static void
published_adapter (DMAPMdnsPublisher * publisher,
		   const char *name, DMAPShare * share)
775 776 777 778
{
	DMAP_SHARE_GET_CLASS (share)->published (share, publisher, name);
}

W. Michael Petullo's avatar
W. Michael Petullo committed
779 780 781
static void
name_collision_adapter (DMAPMdnsPublisher * publisher,
			const char *name, DMAPShare * share)
782 783 784 785 786
{
	DMAP_SHARE_GET_CLASS (share)->name_collision (share, publisher, name);
}

static void
W. Michael Petullo's avatar
W. Michael Petullo committed
787
dmap_share_init (DMAPShare * share)
788 789 790 791 792 793 794 795 796
{
	share->priv = DMAP_SHARE_GET_PRIVATE (share);

	share->priv->revision_number = 5;
	share->priv->auth_method = DMAP_SHARE_AUTH_METHOD_NONE;
	share->priv->publisher = dmap_mdns_publisher_new ();

	g_signal_connect_object (share->priv->publisher,
				 "published",
W. Michael Petullo's avatar
W. Michael Petullo committed
797
				 G_CALLBACK (published_adapter), share, 0);
798 799 800 801 802 803 804
	g_signal_connect_object (share->priv->publisher,
				 "name-collision",
				 G_CALLBACK (name_collision_adapter),
				 share, 0);
}

guint
W. Michael Petullo's avatar
W. Michael Petullo committed
805
_dmap_share_get_auth_method (DMAPShare * share)
806 807 808 809 810
{
	return share->priv->auth_method;
}

guint
W. Michael Petullo's avatar
W. Michael Petullo committed
811
_dmap_share_get_revision_number (DMAPShare * share)
812 813 814 815 816
{
	return share->priv->revision_number;
}

static gboolean
W. Michael Petullo's avatar
W. Michael Petullo committed
817
get_session_id (GHashTable * query, guint32 * id)
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
{
	char *session_id_str;
	guint32 session_id;

	session_id_str = g_hash_table_lookup (query, "session-id");
	if (session_id_str == NULL) {
		g_warning ("Session id not found.");
		return FALSE;
	}

	session_id = (guint32) strtoul (session_id_str, NULL, 10);
	if (id != NULL) {
		*id = session_id;
	}
	return TRUE;
}

gboolean
W. Michael Petullo's avatar
W. Michael Petullo committed
836 837
_dmap_share_get_revision_number_from_query (GHashTable * query,
					    guint * number)
838 839 840 841 842 843
{
	char *revision_number_str;
	guint revision_number;

	revision_number_str = g_hash_table_lookup (query, "revision-number");
	if (revision_number_str == NULL) {
W. Michael Petullo's avatar
W. Michael Petullo committed
844 845
		g_warning
			("Client asked for an update without a rev. number");
846 847 848 849 850 851 852 853 854 855 856
		return FALSE;
	}

	revision_number = strtoul (revision_number_str, NULL, 10);
	if (number != NULL) {
		*number = revision_number;
	}
	return TRUE;
}

gboolean
W. Michael Petullo's avatar
W. Michael Petullo committed
857 858 859 860
_dmap_share_session_id_validate (DMAPShare * share,
				 SoupClientContext * context,
				 SoupMessage * message,
				 GHashTable * query, guint32 * id)
861
{
W. Michael Petullo's avatar
W. Michael Petullo committed
862 863
	guint32 session_id;
	gboolean res;
864 865 866 867 868 869 870 871
	const char *addr;
	const char *remote_address;

	if (id) {
		*id = 0;
	}

	res = get_session_id (query, &session_id);
W. Michael Petullo's avatar
W. Michael Petullo committed
872
	if (!res) {
873 874 875 876 877 878 879 880
		g_warning ("Validation failed: Unable to parse session id");
		return FALSE;
	}

	/* check hash for remote address */
	addr = g_hash_table_lookup (share->priv->session_ids,
				    GUINT_TO_POINTER (session_id));
	if (addr == NULL) {
W. Michael Petullo's avatar
W. Michael Petullo committed
881 882 883
		g_warning
			("Validation failed: Unable to lookup session id %u",
			 session_id);
884 885 886 887 888
		return FALSE;
	}

	remote_address = soup_client_context_get_host (context);
	g_debug ("Validating session id %u from %s matches %s",
W. Michael Petullo's avatar
W. Michael Petullo committed
889
		 session_id, remote_address, addr);
890
	if (remote_address == NULL || strcmp (addr, remote_address) != 0) {
W. Michael Petullo's avatar
W. Michael Petullo committed
891 892
		g_warning
			("Validation failed: Remote address does not match stored address");
893 894 895 896 897 898 899 900 901 902 903
		return FALSE;
	}

	if (id) {
		*id = session_id;
	}

	return TRUE;
}

static guint32
W. Michael Petullo's avatar
W. Michael Petullo committed
904
session_id_generate (DMAPShare * share, SoupClientContext * context)
905 906 907 908 909 910 911 912 913
{
	guint32 id;

	id = g_random_int ();

	return id;
}

guint32
W. Michael Petullo's avatar
W. Michael Petullo committed
914
_dmap_share_session_id_create (DMAPShare * share, SoupClientContext * context)
915
{
W. Michael Petullo's avatar
W. Michael Petullo committed
916
	guint32 id;
917
	const char *addr;
W. Michael Petullo's avatar
W. Michael Petullo committed
918
	char *remote_address;
919 920 921 922 923 924 925 926 927

	do {
		/* create a unique session id */
		id = session_id_generate (share, context);
		g_debug ("Generated session id %u", id);

		/* if already used, try again */
		addr = g_hash_table_lookup (share->priv->session_ids,
					    GUINT_TO_POINTER (id));
W. Michael Petullo's avatar
W. Michael Petullo committed
928
	} while (addr != NULL);
929 930 931 932 933 934 935 936 937 938

	/* store session id and remote address */
	remote_address = g_strdup (soup_client_context_get_host (context));
	g_hash_table_insert (share->priv->session_ids, GUINT_TO_POINTER (id),
			     remote_address);

	return id;
}

void
W. Michael Petullo's avatar
W. Michael Petullo committed
939 940
_dmap_share_session_id_remove (DMAPShare * share,
			       SoupClientContext * context, guint32 id)
941 942 943 944 945
{
	g_hash_table_remove (share->priv->session_ids, GUINT_TO_POINTER (id));
}

void
W. Michael Petullo's avatar
W. Michael Petullo committed
946 947 948
_dmap_share_message_set_from_dmap_structure (DMAPShare * share,
					     SoupMessage * message,
					     GNode * structure)
949 950 951 952 953 954 955 956 957 958 959 960 961 962
{
	gchar *resp;
	guint length;

	resp = dmap_structure_serialize (structure, &length);

	if (resp == NULL) {
		g_warning ("Serialize gave us null?\n");
		return;
	}

	soup_message_set_response (message, "application/x-dmap-tagged",
				   SOUP_MEMORY_TAKE, resp, length);

963 964
	DMAP_SHARE_GET_CLASS (share)->message_add_standard_headers (share,
								    message);
965 966 967 968 969

	soup_message_set_status (message, SOUP_STATUS_OK);
}

gboolean
W. Michael Petullo's avatar
W. Michael Petullo committed
970
_dmap_share_client_requested (bitwise bits, gint field)
971 972 973 974 975
{
	return 0 != (bits & (((bitwise) 1) << field));
}

gboolean
976
_dmap_share_uri_is_local (const char *text_uri)
977
{
W. Michael Petullo's avatar
W. Michael Petullo committed
978
	return g_str_has_prefix (text_uri, "file://");
979 980 981
}

gboolean
W. Michael Petullo's avatar
W. Michael Petullo committed
982 983
_dmap_share_soup_auth_filter (SoupAuthDomain * auth_domain,
			      SoupMessage * msg, gpointer user_data)
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
{
	const char *path;

	path = soup_message_get_uri (msg)->path;
	if (g_str_has_prefix (path, "/databases/")) {
		/* Subdirectories of /databases don't actually require
		 * authentication
		 */
		return FALSE;
	} else {
		/* Everything else in auth_domain's paths, including
		 * /databases itself, does require auth.
		 */
		return TRUE;
	}
}

void
W. Michael Petullo's avatar
W. Michael Petullo committed
1002 1003
_dmap_share_published (DMAPShare * share,
		       DMAPMdnsPublisher * publisher, const char *name)
1004 1005 1006 1007 1008
{
	gchar *nameprop;

	g_object_get ((gpointer) share, "name", &nameprop, NULL);

W. Michael Petullo's avatar
W. Michael Petullo committed
1009
	if (nameprop == NULL || name == NULL) {
1010
		g_free (nameprop);
W. Michael Petullo's avatar
W. Michael Petullo committed
1011 1012
		return;
	}
1013

W. Michael Petullo's avatar
W. Michael Petullo committed
1014 1015 1016 1017
	if (strcmp (nameprop, name) == 0) {
		g_debug ("mDNS publish successful");
		share->priv->published = TRUE;
	}
1018 1019 1020 1021 1022

	g_free (nameprop);
}

void
W. Michael Petullo's avatar
W. Michael Petullo committed
1023 1024
_dmap_share_name_collision (DMAPShare * share,
			    DMAPMdnsPublisher * publisher, const char *name)
1025 1026
{
	gchar *nameprop;
W. Michael Petullo's avatar
W. Michael Petullo committed
1027
	char *new_name = "FIXME";
1028 1029 1030

	g_object_get ((gpointer) share, "name", &nameprop, NULL);

W. Michael Petullo's avatar
W. Michael Petullo committed
1031
	if (nameprop == NULL || name == NULL) {
1032
		g_free (nameprop);
W. Michael Petullo's avatar
W. Michael Petullo committed
1033 1034
		return;
	}
1035

W. Michael Petullo's avatar
W. Michael Petullo committed
1036 1037
	if (strcmp (nameprop, name) == 0) {
		g_warning ("Duplicate share name on mDNS");
1038

W. Michael Petullo's avatar
W. Michael Petullo committed
1039 1040 1041
		_dmap_share_set_name (DMAP_SHARE (share), new_name);
		g_free (new_name);
	}
1042 1043 1044

	g_free (nameprop);

W. Michael Petullo's avatar
W. Michael Petullo committed
1045
	return;
1046 1047 1048
}

void
W. Michael Petullo's avatar
W. Michael Petullo committed
1049 1050 1051 1052 1053
_dmap_share_content_codes (DMAPShare * share,
			   SoupServer * server,
			   SoupMessage * message,
			   const char *path,
			   GHashTable * query, SoupClientContext * context)
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
{
/* MCCR content codes response
 * 	MSTT status
 * 	MDCL dictionary
 * 		MCNM content codes number
 * 		MCNA content codes name
 * 		MCTY content codes type
 * 	MDCL dictionary
 * 	...
 */
	const DMAPContentCodeDefinition *defs;
	guint num_defs = 0;
	guint i;
	GNode *mccr;

	g_debug ("Path is %s.", path);

	defs = dmap_content_codes (&num_defs);

	mccr = dmap_structure_add (NULL, DMAP_CC_MCCR);
	dmap_structure_add (mccr, DMAP_CC_MSTT, (gint32) DMAP_STATUS_OK);

	for (i = 0; i < num_defs; i++) {
		GNode *mdcl;

		mdcl = dmap_structure_add (mccr, DMAP_CC_MDCL);
W. Michael Petullo's avatar
W. Michael Petullo committed
1080 1081 1082 1083
		dmap_structure_add (mdcl, DMAP_CC_MCNM,
				    dmap_content_code_string_as_int32 (defs
								       [i].
								       string));
1084
		dmap_structure_add (mdcl, DMAP_CC_MCNA, defs[i].name);
W. Michael Petullo's avatar
W. Michael Petullo committed
1085 1086
		dmap_structure_add (mdcl, DMAP_CC_MCTY,
				    (gint32) defs[i].type);
1087 1088
	}

1089
	_dmap_share_message_set_from_dmap_structure (share, message, mccr);
1090 1091 1092 1093
	dmap_structure_destroy (mccr);
}

void
W. Michael Petullo's avatar
W. Michael Petullo committed
1094 1095 1096 1097 1098
_dmap_share_login (DMAPShare * share,
		   SoupServer * server,
		   SoupMessage * message,
		   const char *path,
		   GHashTable * query, SoupClientContext * context)
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
{
/* MLOG login response
 * 	MSTT status
 * 	MLID session id
 */
	GNode *mlog;
	guint32 session_id;

	g_debug ("Path is %s.", path);

1109
	session_id = _dmap_share_session_id_create (share, context);
1110 1111 1112 1113 1114

	mlog = dmap_structure_add (NULL, DMAP_CC_MLOG);
	dmap_structure_add (mlog, DMAP_CC_MSTT, (gint32) DMAP_STATUS_OK);
	dmap_structure_add (mlog, DMAP_CC_MLID, session_id);

1115
	_dmap_share_message_set_from_dmap_structure (share, message, mlog);
1116 1117 1118 1119
	dmap_structure_destroy (mlog);
}

void
W. Michael Petullo's avatar
W. Michael Petullo committed
1120 1121 1122 1123 1124
_dmap_share_logout (DMAPShare * share,
		    SoupServer * server,
		    SoupMessage * message,
		    const char *path,
		    GHashTable * query, SoupClientContext * context)
1125
{
W. Michael Petullo's avatar
W. Michael Petullo committed
1126
	int status;
1127 1128 1129 1130
	guint32 id;

	g_debug ("Path is %s.", path);

W. Michael Petullo's avatar
W. Michael Petullo committed
1131 1132
	if (_dmap_share_session_id_validate
	    (share, context, message, query, &id)) {
1133
		_dmap_share_session_id_remove (share, context, id);
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143

		status = SOUP_STATUS_NO_CONTENT;
	} else {
		status = SOUP_STATUS_FORBIDDEN;
	}

	soup_message_set_status (message, status);
}

void
W. Michael Petullo's avatar
W. Michael Petullo committed
1144 1145 1146 1147 1148
_dmap_share_update (DMAPShare