dmap-share.c 53.9 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
	PROP_SERVER,
53 54 55 56
	PROP_NAME,
	PROP_PASSWORD,
	PROP_REVISION_NUMBER,
	PROP_AUTH_METHOD,
57 58
	PROP_DB,
	PROP_CONTAINER_DB,
59 60
	PROP_TRANSCODE_MIMETYPE,
	PROP_TXT_RECORDS
61 62
};

W. Michael Petullo's avatar
W. Michael Petullo committed
63 64
struct DMAPSharePrivate
{
65 66 67
	gchar *name;
	guint port;
	char *password;
68 69 70 71

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

74 75
	DMAPShareAuthMethod auth_method;

76
	/* mDNS/DNS-SD publishing things */
77 78
	gboolean server_active;
	gboolean published;
79
	DMAPMdnsPublisher *publisher;
80

81
	/* HTTP server things */
82
	SoupServer *server;
83 84
	guint revision_number;

85 86 87
	/* The media database */
	DMAPDb *db;
	DMAPContainerDb *container_db;
W. Michael Petullo's avatar
W. Michael Petullo committed
88

89 90
	/* TXT-RECORDS published by mDNS */
	gchar **txt_records;
91

92 93 94
	GHashTable *session_ids;
};

95 96 97
typedef void (*ShareBitwiseDestroyFunc) (void *);
typedef DMAPRecord *(*ShareBitwiseLookupByIdFunc) (void *db, guint id);

98
/* FIXME: name this something else, as it is more than just share/bitwise now */
W. Michael Petullo's avatar
W. Michael Petullo committed
99 100
struct share_bitwise_t
{
101
	DMAPShare *share;
102
	struct MLCL_Bits mb;
103
	GSList *id_list;
104
	guint32 size;
105 106 107 108 109 110 111

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

	void (*destroy) (void *);
112 113
};

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

G_DEFINE_ABSTRACT_TYPE (DMAPShare, dmap_share, G_TYPE_OBJECT)
#define DMAP_SHARE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
119 120
				   DMAP_TYPE_SHARE, DMAPSharePrivate));

W. Michael Petullo's avatar
W. Michael Petullo committed
121 122 123 124 125 126
     static gboolean
	     _dmap_share_soup_auth_callback (SoupAuthDomain * auth_domain,
					     SoupMessage * msg,
					     const char *username,
					     gpointer password,
					     DMAPShare * share)
127
{
W. Michael Petullo's avatar
W. Michael Petullo committed
128
	gboolean allowed;
129 130 131 132 133 134 135 136 137 138 139
	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
140 141 142 143 144 145
static void
server_info_adapter (SoupServer * server,
		     SoupMessage * message,
		     const char *path,
		     GHashTable * query,
		     SoupClientContext * context, DMAPShare * share)
146 147 148 149
{
	DMAP_SHARE_GET_CLASS (share)->server_info (share,
						   server,
						   message,
W. Michael Petullo's avatar
W. Michael Petullo committed
150
						   path, query, context);
151 152
}

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

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

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

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

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

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

228 229
gboolean
_dmap_share_server_start (DMAPShare *share)
230
{
231 232 233 234 235 236 237 238 239
	guint desired_port = DMAP_SHARE_GET_CLASS (share)->get_desired_port (share);
	gboolean password_required;
	GError *error = NULL;
	GSList *listening_uri_list;
	SoupURI *listening_uri;

	share->priv->server = soup_server_new (NULL, NULL);

	password_required = (share->priv->auth_method != DMAP_SHARE_AUTH_METHOD_NONE);
240 241 242 243

	if (password_required) {
		SoupAuthDomain *auth_domain;

W. Michael Petullo's avatar
W. Michael Petullo committed
244 245 246 247 248 249 250 251 252 253 254 255
		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);
256
		soup_auth_domain_basic_set_auth_callback (auth_domain,
W. Michael Petullo's avatar
W. Michael Petullo committed
257 258 259 260
							  (SoupAuthDomainBasicAuthCallback)
							  _dmap_share_soup_auth_callback,
							  g_object_ref
							  (share),
261
							  g_object_unref);
262
		soup_server_add_auth_domain (share->priv->server, auth_domain);
263 264
	}

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

287
	soup_server_listen_all (share->priv->server, desired_port, 0, &error);
288

289 290 291 292
	if (error != NULL) {
		g_warning ("Unable to start music sharing server on port %d: %s. "
		           "Trying any open IPv6 port", desired_port, error->message);
		g_clear_error (&error);
293

294 295
		soup_server_listen_all (share->priv->server, SOUP_ADDRESS_ANY_PORT,
		                        SOUP_SERVER_LISTEN_IPV6_ONLY, &error);
296 297
	}

298
	listening_uri_list = soup_server_get_uris (share->priv->server);
299 300
	if (listening_uri_list == NULL) {
		g_warning ("Unable to start music sharing server on any port.");
301
		return FALSE;
302 303
	}

304 305 306 307 308 309 310
	/* We can only expose one port, so no point checking more than one URI
	 * here. Maybe it somehow is listening on a different port for IPv4 vs.
	 * IPv6, but there's not much we can do.
	 */
	listening_uri = listening_uri_list->data;
	share->priv->port = soup_uri_get_port (listening_uri);
	g_slist_free_full (listening_uri_list, (GDestroyNotify) soup_uri_free);
311

312
	g_debug ("Started DMAP server on port %u", share->priv->port);
313 314

	/* using direct since there is no g_uint_hash or g_uint_equal */
W. Michael Petullo's avatar
W. Michael Petullo committed
315 316 317
	share->priv->session_ids =
		g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL,
				       g_free);
318 319 320 321 322 323 324

	share->priv->server_active = TRUE;

	return TRUE;
}

static gboolean
W. Michael Petullo's avatar
W. Michael Petullo committed
325
_dmap_share_server_stop (DMAPShare * share)
326
{
W. Michael Petullo's avatar
W. Michael Petullo committed
327 328
	g_debug ("Stopping music sharing server on port %d",
		 share->priv->port);
329

330 331 332 333
	if (share->priv->server) {
		soup_server_disconnect (share->priv->server);
		g_object_unref (share->priv->server);
		share->priv->server = NULL;
334 335 336 337 338 339 340 341 342 343 344 345 346
	}

	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
347
_dmap_share_publish_start (DMAPShare * share)
348
{
W. Michael Petullo's avatar
W. Michael Petullo committed
349
	GError *error;
350 351 352
	gboolean res;
	gboolean password_required;

W. Michael Petullo's avatar
W. Michael Petullo committed
353 354
	password_required =
		(share->priv->auth_method != DMAP_SHARE_AUTH_METHOD_NONE);
355 356 357

	error = NULL;
	res = dmap_mdns_publisher_publish (share->priv->publisher,
358
					   share->priv->name,
W. Michael Petullo's avatar
W. Michael Petullo committed
359 360 361 362 363
					   share->priv->port,
					   DMAP_SHARE_GET_CLASS (share)->
					   get_type_of_service (share),
					   password_required,
					   share->priv->txt_records, &error);
364 365 366

	if (res == FALSE) {
		if (error != NULL) {
W. Michael Petullo's avatar
W. Michael Petullo committed
367 368 369
			g_warning
				("Unable to notify network of media sharing: %s",
				 error->message);
370 371
			g_error_free (error);
		} else {
W. Michael Petullo's avatar
W. Michael Petullo committed
372 373
			g_warning
				("Unable to notify network of media sharing");
374 375 376 377 378 379 380 381 382 383
		}
		return FALSE;
	} else {
		g_debug ("Published DMAP server information to mdns");
	}

	return TRUE;
}

static gboolean
W. Michael Petullo's avatar
W. Michael Petullo committed
384
_dmap_share_publish_stop (DMAPShare * share)
385 386 387
{
	if (share->priv->publisher) {
		gboolean res;
W. Michael Petullo's avatar
W. Michael Petullo committed
388 389
		GError *error;

390
		error = NULL;
W. Michael Petullo's avatar
W. Michael Petullo committed
391 392 393
		res = dmap_mdns_publisher_withdraw (share->priv->publisher,
						    share->priv->port,
						    &error);
394
		if (error != NULL) {
W. Michael Petullo's avatar
W. Michael Petullo committed
395 396 397
			g_warning
				("Unable to withdraw music sharing service: %s",
				 error->message);
398 399 400 401 402 403 404 405 406 407
			g_error_free (error);
		}
		return res;
	}

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

static void
W. Michael Petullo's avatar
W. Michael Petullo committed
408
_dmap_share_restart (DMAPShare * share)
409 410 411
{
	gboolean res;

412 413
	_dmap_share_server_stop (share);
	res = _dmap_share_server_start (share);
414 415
	if (res) {
		/* To update information just publish again */
416
		_dmap_share_publish_start (share);
417
	} else {
418
		_dmap_share_publish_stop (share);
419 420 421 422
	}
}

static void
W. Michael Petullo's avatar
W. Michael Petullo committed
423
_dmap_share_maybe_restart (DMAPShare * share)
424 425
{
	if (share->priv->published) {
426
		_dmap_share_restart (share);
427 428 429 430
	}
}

static void
W. Michael Petullo's avatar
W. Michael Petullo committed
431
_dmap_share_set_name (DMAPShare * share, const char *name)
432 433 434 435 436 437 438 439
{
	GError *error;

	g_return_if_fail (share != NULL);

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

440 441
	if (share->priv->published) {
		error = NULL;
442 443 444 445 446
		dmap_mdns_publisher_rename_at_port (share->priv->
						    publisher,
						    share->priv->port,
						    name,
						    &error);
447 448 449 450 451
		if (error != NULL) {
			g_warning ("Unable to change MDNS service name: %s",
				   error->message);
			g_error_free (error);
		}
452 453 454 455
	}
}

static void
W. Michael Petullo's avatar
W. Michael Petullo committed
456
_dmap_share_set_password (DMAPShare * share, const char *password)
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
{
	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;
	}

473
	_dmap_share_maybe_restart (share);
474 475 476
}

static void
W. Michael Petullo's avatar
W. Michael Petullo committed
477 478 479
_dmap_share_set_property (GObject * object,
			  guint prop_id,
			  const GValue * value, GParamSpec * pspec)
480 481 482 483 484
{
	DMAPShare *share = DMAP_SHARE (object);

	switch (prop_id) {
	case PROP_NAME:
485
		_dmap_share_set_name (share, g_value_get_string (value));
486 487
		break;
	case PROP_PASSWORD:
488
		_dmap_share_set_password (share, g_value_get_string (value));
489
		break;
490
	case PROP_DB:
W. Michael Petullo's avatar
W. Michael Petullo committed
491 492 493 494 495 496 497
		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:
498
		/* FIXME: get or dup? */
W. Michael Petullo's avatar
W. Michael Petullo committed
499 500 501 502 503
		share->priv->transcode_mimetype = g_value_dup_string (value);
		break;
	case PROP_TXT_RECORDS:
		share->priv->txt_records = g_value_dup_boxed (value);
		break;
504 505 506 507 508 509 510
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
W. Michael Petullo's avatar
W. Michael Petullo committed
511 512
_dmap_share_get_property (GObject * object,
			  guint prop_id, GValue * value, GParamSpec * pspec)
513 514 515 516
{
	DMAPShare *share = DMAP_SHARE (object);

	switch (prop_id) {
517 518
	case PROP_SERVER:
		g_value_set_object (value, share->priv->server);
519
		return;
520 521 522 523 524 525 526 527
	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,
528
				  _dmap_share_get_revision_number
W. Michael Petullo's avatar
W. Michael Petullo committed
529
				  (DMAP_SHARE (object)));
530 531 532
		break;
	case PROP_AUTH_METHOD:
		g_value_set_uint (value,
533
				  _dmap_share_get_auth_method
W. Michael Petullo's avatar
W. Michael Petullo committed
534
				  (DMAP_SHARE (object)));
535
		break;
536
	case PROP_DB:
W. Michael Petullo's avatar
W. Michael Petullo committed
537 538 539 540 541 542 543 544 545 546 547
		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;
548 549 550 551 552 553 554
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
W. Michael Petullo's avatar
W. Michael Petullo committed
555
_dmap_share_finalize (GObject * object)
556 557 558
{
	DMAPShare *share = DMAP_SHARE (object);

559 560
	g_debug ("Finalizing DMAPShare");

561
	if (share->priv->published) {
562
		_dmap_share_publish_stop (share);
563 564 565
	}

	if (share->priv->server_active) {
566
		_dmap_share_server_stop (share);
567 568 569
	}

	g_free (share->priv->name);
570 571 572 573
	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
574

575
	g_strfreev (share->priv->txt_records);
576 577 578 579 580

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

581
	G_OBJECT_CLASS (dmap_share_parent_class)->finalize (object);
582 583 584
}

static void
W. Michael Petullo's avatar
W. Michael Petullo committed
585
dmap_share_class_init (DMAPShareClass * klass)
586 587 588
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

589 590
	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
591
	object_class->finalize = _dmap_share_finalize;
592 593

	/* Pure virtual methods: */
W. Michael Petullo's avatar
W. Michael Petullo committed
594 595
	klass->get_desired_port = NULL;
	klass->get_type_of_service = NULL;
596
	klass->message_add_standard_headers = NULL;
W. Michael Petullo's avatar
W. Michael Petullo committed
597 598
	klass->get_meta_data_map = NULL;
	klass->add_entry_to_mlcl = NULL;
599
	klass->databases_browse_xxx = NULL;
W. Michael Petullo's avatar
W. Michael Petullo committed
600
	klass->databases_items_xxx = NULL;
601 602

	/* Virtual methods: */
W. Michael Petullo's avatar
W. Michael Petullo committed
603 604 605 606 607
	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;
608
	klass->name_collision = _dmap_share_name_collision;
W. Michael Petullo's avatar
W. Michael Petullo committed
609 610
	klass->databases = _dmap_share_databases;
	klass->ctrl_int = _dmap_share_ctrl_int;
611

612
	g_object_class_install_property (object_class,
613 614
					 PROP_SERVER,
					 g_param_spec_object ("server",
615 616 617 618
							      "Soup Server",
							      "Soup server",
							      SOUP_TYPE_SERVER,
							      G_PARAM_READABLE));
W. Michael Petullo's avatar
W. Michael Petullo committed
619

620 621 622
	g_object_class_install_property (object_class,
					 PROP_NAME,
					 g_param_spec_string ("name",
W. Michael Petullo's avatar
W. Michael Petullo committed
623 624 625 626
							      "Name",
							      "Share Name",
							      NULL,
							      G_PARAM_READWRITE));
627 628 629
	g_object_class_install_property (object_class,
					 PROP_PASSWORD,
					 g_param_spec_string ("password",
W. Michael Petullo's avatar
W. Michael Petullo committed
630 631 632 633
							      "Authentication password",
							      "Authentication password",
							      NULL,
							      G_PARAM_READWRITE));
634 635 636 637

	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
638
							    "Revision number",
639 640 641 642 643 644 645 646 647
							    "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
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
							    "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));

663
	g_object_class_install_property (object_class,
W. Michael Petullo's avatar
W. Michael Petullo committed
664 665 666 667 668 669 670
					 PROP_CONTAINER_DB,
					 g_param_spec_pointer ("container-db",
							       "Container DB",
							       "Container DB object",
							       G_PARAM_READWRITE
							       |
							       G_PARAM_CONSTRUCT_ONLY));
671

672
	g_object_class_install_property (object_class,
W. Michael Petullo's avatar
W. Michael Petullo committed
673 674 675 676 677 678 679 680
					 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));
681 682

	g_object_class_install_property (object_class,
W. Michael Petullo's avatar
W. Michael Petullo committed
683 684 685 686 687 688
					 PROP_TXT_RECORDS,
					 g_param_spec_boxed ("txt-records",
							     "TXT-Records",
							     "Set TXT-Records used for MDNS publishing",
							     G_TYPE_STRV,
							     G_PARAM_READWRITE));
689 690 691 692

	g_type_class_add_private (klass, sizeof (DMAPSharePrivate));
}

W. Michael Petullo's avatar
W. Michael Petullo committed
693 694 695
static void
published_adapter (DMAPMdnsPublisher * publisher,
		   const char *name, DMAPShare * share)
696 697 698 699
{
	DMAP_SHARE_GET_CLASS (share)->published (share, publisher, name);
}

W. Michael Petullo's avatar
W. Michael Petullo committed
700 701 702
static void
name_collision_adapter (DMAPMdnsPublisher * publisher,
			const char *name, DMAPShare * share)
703 704 705 706 707
{
	DMAP_SHARE_GET_CLASS (share)->name_collision (share, publisher, name);
}

static void
W. Michael Petullo's avatar
W. Michael Petullo committed
708
dmap_share_init (DMAPShare * share)
709 710 711 712 713 714 715 716 717
{
	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
718
				 G_CALLBACK (published_adapter), share, 0);
719 720 721 722 723 724 725
	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
726
_dmap_share_get_auth_method (DMAPShare * share)
727 728 729 730 731
{
	return share->priv->auth_method;
}

guint
W. Michael Petullo's avatar
W. Michael Petullo committed
732
_dmap_share_get_revision_number (DMAPShare * share)
733 734 735 736 737
{
	return share->priv->revision_number;
}

static gboolean
W. Michael Petullo's avatar
W. Michael Petullo committed
738
get_session_id (GHashTable * query, guint32 * id)
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
{
	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
757 758
_dmap_share_get_revision_number_from_query (GHashTable * query,
					    guint * number)
759 760 761 762 763 764
{
	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
765 766
		g_warning
			("Client asked for an update without a rev. number");
767 768 769 770 771 772 773 774 775 776 777
		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
778 779 780 781
_dmap_share_session_id_validate (DMAPShare * share,
				 SoupClientContext * context,
				 SoupMessage * message,
				 GHashTable * query, guint32 * id)
782
{
W. Michael Petullo's avatar
W. Michael Petullo committed
783 784
	guint32 session_id;
	gboolean res;
785 786 787 788 789 790 791 792
	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
793
	if (!res) {
794 795 796 797 798 799 800 801
		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
802 803 804
		g_warning
			("Validation failed: Unable to lookup session id %u",
			 session_id);
805 806 807 808 809
		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
810
		 session_id, remote_address, addr);
811
	if (remote_address == NULL || strcmp (addr, remote_address) != 0) {
W. Michael Petullo's avatar
W. Michael Petullo committed
812 813
		g_warning
			("Validation failed: Remote address does not match stored address");
814 815 816 817 818 819 820 821 822 823 824
		return FALSE;
	}

	if (id) {
		*id = session_id;
	}

	return TRUE;
}

static guint32
W. Michael Petullo's avatar
W. Michael Petullo committed
825
session_id_generate (DMAPShare * share, SoupClientContext * context)
826 827 828 829 830 831 832 833 834
{
	guint32 id;

	id = g_random_int ();

	return id;
}

guint32
W. Michael Petullo's avatar
W. Michael Petullo committed
835
_dmap_share_session_id_create (DMAPShare * share, SoupClientContext * context)
836
{
W. Michael Petullo's avatar
W. Michael Petullo committed
837
	guint32 id;
838
	const char *addr;
W. Michael Petullo's avatar
W. Michael Petullo committed
839
	char *remote_address;
840 841 842 843 844 845 846 847 848

	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
849
	} while (addr != NULL);
850 851

	/* store session id and remote address */
852 853 854 855
	/* FIXME, warning, this fails against libsoup-2.33.90-1.fc15.x86_64 with:
	 * (dmapd:12917): libsoup-CRITICAL **: soup_address_get_physical: assertion `SOUP_IS_ADDRESS (addr)' failed
	 * Is this a bug in libsoup or libdmapsharing?
	 */
856 857 858 859 860 861 862 863
	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
864 865
_dmap_share_session_id_remove (DMAPShare * share,
			       SoupClientContext * context, guint32 id)
866 867 868 869 870
{
	g_hash_table_remove (share->priv->session_ids, GUINT_TO_POINTER (id));
}

void
W. Michael Petullo's avatar
W. Michael Petullo committed
871 872 873
_dmap_share_message_set_from_dmap_structure (DMAPShare * share,
					     SoupMessage * message,
					     GNode * structure)
874 875 876 877 878 879 880 881 882 883 884 885 886 887
{
	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);

888 889
	DMAP_SHARE_GET_CLASS (share)->message_add_standard_headers (share,
								    message);
890 891 892 893 894

	soup_message_set_status (message, SOUP_STATUS_OK);
}

gboolean
W. Michael Petullo's avatar
W. Michael Petullo committed
895
_dmap_share_client_requested (bitwise bits, gint field)
896 897 898 899 900
{
	return 0 != (bits & (((bitwise) 1) << field));
}

gboolean
901
_dmap_share_uri_is_local (const char *text_uri)
902
{
W. Michael Petullo's avatar
W. Michael Petullo committed
903
	return g_str_has_prefix (text_uri, "file://");
904 905 906
}

gboolean
W. Michael Petullo's avatar
W. Michael Petullo committed
907 908
_dmap_share_soup_auth_filter (SoupAuthDomain * auth_domain,
			      SoupMessage * msg, gpointer user_data)
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
{
	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
927 928
_dmap_share_published (DMAPShare * share,
		       DMAPMdnsPublisher * publisher, const char *name)
929
{
930
	if (share->priv->name == NULL || name == NULL) {
W. Michael Petullo's avatar
W. Michael Petullo committed
931 932
		return;
	}
933

934
	if (strcmp (share->priv->name, name) == 0) {
W. Michael Petullo's avatar
W. Michael Petullo committed
935 936 937
		g_debug ("mDNS publish successful");
		share->priv->published = TRUE;
	}
938 939 940
}

void
W. Michael Petullo's avatar
W. Michael Petullo committed
941 942
_dmap_share_name_collision (DMAPShare * share,
			    DMAPMdnsPublisher * publisher, const char *name)
943
{
W. Michael Petullo's avatar
W. Michael Petullo committed
944
	char *new_name = "FIXME";
945

946
	if (share->priv->name == NULL || name == NULL) {
W. Michael Petullo's avatar
W. Michael Petullo committed
947 948
		return;
	}
949

950
	if (strcmp (share->priv->name, name) == 0) {
W. Michael Petullo's avatar
W. Michael Petullo committed
951
		g_warning ("Duplicate share name on mDNS");
952

W. Michael Petullo's avatar
W. Michael Petullo committed
953 954 955
		_dmap_share_set_name (DMAP_SHARE (share), new_name);
		g_free (new_name);
	}
956

W. Michael Petullo's avatar
W. Michael Petullo committed
957
	return;
958 959 960
}

void
W. Michael Petullo's avatar
W. Michael Petullo committed
961 962 963 964 965
_dmap_share_content_codes (DMAPShare * share,
			   SoupServer * server,
			   SoupMessage * message,
			   const char *path,
			   GHashTable * query, SoupClientContext * context)
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
{
/* 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
992 993 994 995
		dmap_structure_add (mdcl, DMAP_CC_MCNM,
				    dmap_content_code_string_as_int32 (defs
								       [i].
								       string));
996
		dmap_structure_add (mdcl, DMAP_CC_MCNA, defs[i].name);
W. Michael Petullo's avatar
W. Michael Petullo committed
997 998
		dmap_structure_add (mdcl, DMAP_CC_MCTY,
				    (gint32) defs[i].type);
999 1000
	}

1001
	_dmap_share_message_set_from_dmap_structure (share, message, mccr);
1002 1003 1004 1005
	dmap_structure_destroy (mccr);
}

void
W. Michael Petullo's avatar
W. Michael Petullo committed
1006 1007 1008 1009 1010
_dmap_share_login (DMAPShare * share,
		   SoupServer * server,
		   SoupMessage * message,
		   const char *path,
		   GHashTable * query, SoupClientContext * context)
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
{
/* MLOG login response
 * 	MSTT status
 * 	MLID session id
 */
	GNode *mlog;
	guint32 session_id;

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

1021
	session_id = _dmap_share_session_id_create (share, context);
1022 1023 1024 1025 1026

	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);

1027
	_dmap_share_message_set_from_dmap_structure (share, message, mlog);
1028 1029 1030 1031
	dmap_structure_destroy (mlog);
}

void
W. Michael Petullo's avatar
W. Michael Petullo committed
1032 1033 1034 1035 1036
_dmap_share_logout (DMAPShare * share,
		    SoupServer * server,
		    SoupMessage * message,
		    const char *path,
		    GHashTable * query, SoupClientContext * context)
1037
{
W. Michael Petullo's avatar
W. Michael Petullo committed
1038
	int status;
1039 1040 1041 1042
	guint32 id;

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

W. Michael Petullo's avatar
W. Michael Petullo committed
1043 1044
	if (_dmap_share_session_id_validate
	    (share, context, message, query, &id)) {
1045
		_dmap_share_session_id_remove (share, context, id);
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055

		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
1056 1057 1058 1059 1060
_dmap_share_update (DMAPShare * share,
		    SoupServer * server,
		    SoupMessage * message,
		    const char *path,
		    GHashTable * query, SoupClientContext * context)
1061
{
W. Michael Petullo's avatar
W. Michael Petullo committed
1062
	guint revision_number;
1063 1064 1065 1066
	gboolean res;

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

W. Michael Petullo's avatar
W. Michael Petullo committed
1067 1068
	res = _dmap_share_get_revision_number_from_query (query,
							  &revision_number);
1069

1070
	if (res && revision_number != _dmap_share_get_revision_number (share)) {
1071
		/* MUPD update response
W. Michael Petullo's avatar
W. Michael Petullo committed
1072 1073
		 *      MSTT status
		 *      MUSR server revision
1074 1075 1076 1077
		 */
		GNode *mupd;

		mupd = dmap_structure_add (NULL, DMAP_CC_MUPD);
W. Michael Petullo's avatar
W. Michael Petullo committed
1078 1079 1080 1081 1082 1083 1084 1085
		dmap_structure_add (mupd, DMAP_CC_MSTT,
				    (gint32) DMAP_STATUS_OK);
		dmap_structure_add (mupd, DMAP_CC_MUSR,
				    (gint32)
				    _dmap_share_get_revision_number (share));

		_dmap_share_message_set_from_dmap_structure (share, message,
							     mupd);
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
		dmap_structure_destroy (mupd);
	} else {
		/* FIXME: This seems like a bug. It just leaks the
		 * message (and socket) without ever replying.
		 */
		g_object_ref (message);
		soup_server_pause_message (server, message);
	}
}

bitwise
1097
_dmap_share_parse_meta_str (const char *attrs, struct DMAPMetaDataMap *mdm)
1098 1099 1100 1101 1102 1103 1104 1105 1106
{
	guint i;
	bitwise bits = 0;

	/* iTunes 8 uses meta=all for /databases/1/items query: */
	if (strcmp (attrs, "all") == 0) {
		bits = ~0;
	} else {
		gchar **attrsv;
W. Michael Petullo's avatar
W. Michael Petullo committed
1107

1108 1109 1110 1111
		attrsv = g_strsplit (attrs, ",", -1);

		for (i = 0; attrsv[i]; i++) {
			guint j;
1112
			gboolean found = FALSE;
1113

1114
			for (j = 0; mdm[j].tag; j++) {
1115 1116
				if (strcmp (mdm[j].tag, attrsv[i]) == 0) {
					bits |= (((bitwise) 1) << mdm[j].md);
1117
					found = TRUE;
1118 1119
				}
			}
1120 1121

			if (found == FALSE)
W. Michael Petullo's avatar
W. Michael Petullo committed
1122 1123
				g_debug ("Unknown meta request: %s",
					 attrsv[i]);
1124 1125
		}
		g_strfreev (attrsv);