2010-01-15 17:01:11 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Julien BLACHE <jb@jblache.org>
|
|
|
|
*
|
|
|
|
* iTunes - Remote pairing hash function published by Michael Paul Bailey
|
|
|
|
* <http://jinxidoru.blogspot.com/2009/06/itunes-remote-pairing-code.html>
|
2010-02-05 12:39:03 -05:00
|
|
|
* Simplified version using standard MD5 published by Jeff Sharkey
|
|
|
|
* <http://jsharkey.org/blog/2009/06/21/itunes-dacp-pairing-hash-is-broken/>
|
2010-01-15 17:01:11 -05:00
|
|
|
*
|
|
|
|
* Pairing process based on the work by
|
|
|
|
* - Michael Croes
|
|
|
|
* <http://blog.mycroes.nl/2008/08/pairing-itunes-remote-app-with-your-own.html>
|
|
|
|
* - Jeffrey Sharkey
|
|
|
|
* <http://dacp.jsharkey.org/>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdint.h>
|
2010-09-18 11:16:29 -04:00
|
|
|
#include <inttypes.h>
|
2010-01-15 17:01:11 -05:00
|
|
|
#include <errno.h>
|
|
|
|
|
2017-01-06 03:44:18 -05:00
|
|
|
#ifdef HAVE_EVENTFD
|
2010-02-04 12:52:13 -05:00
|
|
|
# include <sys/eventfd.h>
|
|
|
|
#endif
|
|
|
|
|
2015-10-19 15:15:29 -04:00
|
|
|
#include <event2/event.h>
|
|
|
|
#include <event2/buffer.h>
|
|
|
|
#include <event2/http.h>
|
2010-01-15 17:01:11 -05:00
|
|
|
|
2010-02-05 12:39:03 -05:00
|
|
|
#include <gcrypt.h>
|
|
|
|
|
2010-01-15 17:01:11 -05:00
|
|
|
#include "logger.h"
|
2020-03-15 06:28:21 -04:00
|
|
|
#include "commands.h"
|
2010-01-15 17:01:11 -05:00
|
|
|
#include "conffile.h"
|
2010-09-18 10:29:06 -04:00
|
|
|
#include "mdns.h"
|
2010-01-15 17:01:11 -05:00
|
|
|
#include "misc.h"
|
2010-01-30 11:06:32 -05:00
|
|
|
#include "db.h"
|
2010-01-15 17:01:11 -05:00
|
|
|
#include "remote_pairing.h"
|
2017-08-16 15:32:46 -04:00
|
|
|
#include "listener.h"
|
2010-01-15 17:01:11 -05:00
|
|
|
|
|
|
|
|
|
|
|
struct remote_info {
|
2010-01-30 11:06:32 -05:00
|
|
|
struct pairing_info pi;
|
2010-01-15 17:01:11 -05:00
|
|
|
|
|
|
|
char *paircode;
|
|
|
|
char *pin;
|
|
|
|
|
2010-05-08 03:02:56 -04:00
|
|
|
unsigned short v4_port;
|
|
|
|
unsigned short v6_port;
|
|
|
|
char *v4_address;
|
|
|
|
char *v6_address;
|
2010-01-15 17:01:11 -05:00
|
|
|
|
|
|
|
struct evhttp_connection *evcon;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-01-25 11:55:11 -05:00
|
|
|
/* Main event base, from main.c */
|
|
|
|
extern struct event_base *evbase_main;
|
|
|
|
|
2017-01-21 10:11:20 -05:00
|
|
|
static pthread_mutex_t remote_lck;
|
2017-03-25 02:40:07 -04:00
|
|
|
static struct remote_info *remote_info;
|
2010-01-15 17:01:11 -05:00
|
|
|
|
2020-03-15 06:28:21 -04:00
|
|
|
static struct commands_base *cmdbase;
|
2010-01-15 17:01:11 -05:00
|
|
|
|
|
|
|
/* iTunes - Remote pairing hash */
|
|
|
|
static char *
|
|
|
|
itunes_pairing_hash(char *paircode, char *pin)
|
|
|
|
{
|
|
|
|
char hash[33];
|
2010-02-05 12:39:03 -05:00
|
|
|
char ebuf[64];
|
|
|
|
uint8_t *hash_bytes;
|
|
|
|
size_t hashlen;
|
|
|
|
gcry_md_hd_t hd;
|
|
|
|
gpg_error_t gc_err;
|
|
|
|
int i;
|
2010-01-15 17:01:11 -05:00
|
|
|
|
|
|
|
if (strlen(paircode) != 16)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Paircode length != 16, cannot compute pairing hash\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(pin) != 4)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Pin length != 4, cannot compute pairing hash\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-02-05 12:39:03 -05:00
|
|
|
gc_err = gcry_md_open(&hd, GCRY_MD_MD5, 0);
|
|
|
|
if (gc_err != GPG_ERR_NO_ERROR)
|
|
|
|
{
|
|
|
|
gpg_strerror_r(gc_err, ebuf, sizeof(ebuf));
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Could not open MD5: %s\n", ebuf);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-02-22 11:49:24 -05:00
|
|
|
gcry_md_write(hd, paircode, 16);
|
|
|
|
/* Add pin code characters on 16 bits - remember Mac OS X is
|
|
|
|
* all UTF-16 (wchar_t).
|
|
|
|
*/
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
gcry_md_write(hd, pin + i, 1);
|
|
|
|
gcry_md_write(hd, "\0", 1);
|
|
|
|
}
|
2010-02-05 12:39:03 -05:00
|
|
|
|
|
|
|
hash_bytes = gcry_md_read(hd, GCRY_MD_MD5);
|
|
|
|
if (!hash_bytes)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Could not read MD5 hash\n");
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
hashlen = gcry_md_get_algo_dlen(GCRY_MD_MD5);
|
|
|
|
|
|
|
|
for (i = 0; i < hashlen; i++)
|
|
|
|
sprintf(hash + (2 * i), "%02X", hash_bytes[i]);
|
|
|
|
|
|
|
|
gcry_md_close(hd);
|
2010-01-15 17:01:11 -05:00
|
|
|
|
|
|
|
return strdup(hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Operations on the remote list must happen
|
|
|
|
* with the list lock held by the caller
|
|
|
|
*/
|
|
|
|
static struct remote_info *
|
2017-03-25 02:40:07 -04:00
|
|
|
create_remote(void)
|
2010-01-15 17:01:11 -05:00
|
|
|
{
|
|
|
|
struct remote_info *ri;
|
|
|
|
|
2017-03-25 02:40:07 -04:00
|
|
|
ri = calloc(1, sizeof(struct remote_info));
|
2010-01-15 17:01:11 -05:00
|
|
|
if (!ri)
|
|
|
|
{
|
|
|
|
DPRINTF(E_WARN, L_REMOTE, "Out of memory for struct remote_info\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ri;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
unlink_remote(struct remote_info *ri)
|
|
|
|
{
|
2017-03-25 02:40:07 -04:00
|
|
|
if (ri == remote_info)
|
|
|
|
remote_info = NULL;
|
2010-01-15 17:01:11 -05:00
|
|
|
else
|
2017-03-25 02:40:07 -04:00
|
|
|
DPRINTF(E_LOG, L_REMOTE, "WARNING: struct remote_info not found in list; BUG!\n");
|
2010-01-15 17:01:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_remote(struct remote_info *ri)
|
|
|
|
{
|
|
|
|
if (ri->paircode)
|
|
|
|
free(ri->paircode);
|
|
|
|
|
|
|
|
if (ri->pin)
|
|
|
|
free(ri->pin);
|
|
|
|
|
2010-05-08 03:02:56 -04:00
|
|
|
if (ri->v4_address)
|
|
|
|
free(ri->v4_address);
|
|
|
|
|
|
|
|
if (ri->v6_address)
|
|
|
|
free(ri->v6_address);
|
2010-01-15 17:01:11 -05:00
|
|
|
|
2010-01-30 11:06:32 -05:00
|
|
|
free_pi(&ri->pi, 1);
|
|
|
|
|
2010-01-15 17:01:11 -05:00
|
|
|
free(ri);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
remove_remote(struct remote_info *ri)
|
|
|
|
{
|
|
|
|
unlink_remote(ri);
|
|
|
|
|
|
|
|
free_remote(ri);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-05-08 03:02:56 -04:00
|
|
|
remove_remote_address_byid(const char *id, int family)
|
2010-01-15 17:01:11 -05:00
|
|
|
{
|
2017-03-25 02:40:07 -04:00
|
|
|
struct remote_info *ri = NULL;
|
2010-01-15 17:01:11 -05:00
|
|
|
|
2017-03-25 02:40:07 -04:00
|
|
|
if (remote_info && strcmp(remote_info->pi.remote_id, id) == 0)
|
|
|
|
ri = remote_info;
|
2010-01-15 17:01:11 -05:00
|
|
|
|
|
|
|
if (!ri)
|
|
|
|
{
|
|
|
|
DPRINTF(E_WARN, L_REMOTE, "Remote %s not found in list\n", id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-08 03:02:56 -04:00
|
|
|
switch (family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
if (ri->v4_address)
|
|
|
|
{
|
|
|
|
free(ri->v4_address);
|
|
|
|
ri->v4_address = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AF_INET6:
|
|
|
|
if (ri->v6_address)
|
|
|
|
{
|
|
|
|
free(ri->v6_address);
|
|
|
|
ri->v6_address = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ri->v4_address && !ri->v6_address)
|
|
|
|
remove_remote(ri);
|
2010-01-15 17:01:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-05-08 03:02:56 -04:00
|
|
|
add_remote_mdns_data(const char *id, int family, const char *address, int port, char *name, char *paircode)
|
2010-01-15 17:01:11 -05:00
|
|
|
{
|
2010-05-08 03:02:56 -04:00
|
|
|
char *check_addr;
|
2010-01-15 17:01:11 -05:00
|
|
|
int ret;
|
|
|
|
|
2017-03-25 02:40:07 -04:00
|
|
|
if (remote_info && strcmp(remote_info->pi.remote_id, id) == 0)
|
2010-01-15 17:01:11 -05:00
|
|
|
{
|
2017-03-25 02:40:07 -04:00
|
|
|
DPRINTF(E_DBG, L_REMOTE, "Remote id %s found\n", id);
|
|
|
|
free_pi(&remote_info->pi, 1);
|
|
|
|
ret = 1;
|
2010-01-15 17:01:11 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-25 02:40:07 -04:00
|
|
|
DPRINTF(E_DBG, L_REMOTE, "Remote id %s not known, adding\n", id);
|
|
|
|
if (remote_info)
|
2010-05-08 03:02:56 -04:00
|
|
|
{
|
2017-03-25 02:40:07 -04:00
|
|
|
DPRINTF(E_DBG, L_REMOTE, "Removing existing remote with id %s\n", remote_info->pi.remote_id);
|
|
|
|
remove_remote(remote_info);
|
2010-05-08 03:02:56 -04:00
|
|
|
}
|
2010-01-15 17:01:11 -05:00
|
|
|
|
2017-03-25 02:40:07 -04:00
|
|
|
remote_info = create_remote();
|
|
|
|
ret = 0;
|
2010-01-15 17:01:11 -05:00
|
|
|
}
|
|
|
|
|
2017-03-25 02:40:07 -04:00
|
|
|
free(remote_info->paircode);
|
|
|
|
free(remote_info->pi.remote_id);
|
|
|
|
remote_info->pi.remote_id = strdup(id);
|
2010-01-15 17:01:11 -05:00
|
|
|
|
2010-05-08 03:02:56 -04:00
|
|
|
switch (family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
2017-03-25 02:40:07 -04:00
|
|
|
free(remote_info->v4_address);
|
|
|
|
remote_info->v4_address = strdup(address);
|
|
|
|
remote_info->v4_port = port;
|
2010-05-08 03:02:56 -04:00
|
|
|
|
2017-03-25 02:40:07 -04:00
|
|
|
check_addr = remote_info->v4_address;
|
2010-05-08 03:02:56 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AF_INET6:
|
2017-03-25 02:40:07 -04:00
|
|
|
free(remote_info->v6_address);
|
|
|
|
remote_info->v6_address = strdup(address);
|
|
|
|
remote_info->v6_port = port;
|
2010-05-08 03:02:56 -04:00
|
|
|
|
2017-03-25 02:40:07 -04:00
|
|
|
check_addr = remote_info->v6_address;
|
2010-05-08 03:02:56 -04:00
|
|
|
break;
|
2011-04-30 05:36:03 -04:00
|
|
|
|
|
|
|
default:
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Unknown address family %d\n", family);
|
|
|
|
|
|
|
|
check_addr = NULL;
|
|
|
|
break;
|
2010-05-08 03:02:56 -04:00
|
|
|
}
|
|
|
|
|
2017-03-25 02:40:07 -04:00
|
|
|
if (!remote_info->pi.remote_id || !check_addr)
|
2010-01-15 17:01:11 -05:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Out of memory for remote pairing data\n");
|
|
|
|
|
2017-03-25 02:40:07 -04:00
|
|
|
remove_remote(remote_info);
|
2010-01-15 17:01:11 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-03-25 02:40:07 -04:00
|
|
|
remote_info->pi.name = name;
|
|
|
|
remote_info->paircode = paircode;
|
2010-01-15 17:01:11 -05:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-03-25 06:07:39 -04:00
|
|
|
add_remote_pin_data(const char *pin)
|
2010-01-15 17:01:11 -05:00
|
|
|
{
|
2017-03-25 06:07:39 -04:00
|
|
|
if (!remote_info)
|
2010-01-15 17:01:11 -05:00
|
|
|
{
|
2017-03-25 06:07:39 -04:00
|
|
|
DPRINTF(E_LOG, L_REMOTE, "No remote known from mDNS, ignoring\n");
|
2010-01-15 17:01:11 -05:00
|
|
|
|
2020-03-15 06:28:21 -04:00
|
|
|
return REMOTE_ERROR;
|
2010-01-15 17:01:11 -05:00
|
|
|
}
|
|
|
|
|
2017-03-25 06:07:39 -04:00
|
|
|
DPRINTF(E_DBG, L_REMOTE, "Adding pin to remote '%s'\n", remote_info->pi.name);
|
2010-01-15 17:01:11 -05:00
|
|
|
|
2017-03-25 06:07:39 -04:00
|
|
|
free(remote_info->pin);
|
|
|
|
remote_info->pin = strdup(pin);
|
2010-01-15 17:01:11 -05:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-25 11:55:11 -05:00
|
|
|
/* Thread: main (pairing) */
|
2010-01-15 17:01:11 -05:00
|
|
|
static void
|
|
|
|
pairing_request_cb(struct evhttp_request *req, void *arg)
|
|
|
|
{
|
|
|
|
struct remote_info *ri;
|
2014-05-29 17:22:00 -04:00
|
|
|
struct evbuffer *input_buffer;
|
2010-01-30 11:07:22 -05:00
|
|
|
uint8_t *response;
|
|
|
|
char guid[17];
|
2015-10-19 15:15:29 -04:00
|
|
|
int buflen;
|
2014-05-29 17:22:00 -04:00
|
|
|
int response_code;
|
2010-01-30 11:07:22 -05:00
|
|
|
int len;
|
|
|
|
int i;
|
|
|
|
int ret;
|
2010-01-15 17:01:11 -05:00
|
|
|
|
|
|
|
ri = (struct remote_info *)arg;
|
|
|
|
|
|
|
|
if (!req)
|
2014-01-27 14:36:54 -05:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Empty pairing request callback\n");
|
|
|
|
|
2020-03-15 06:28:21 -04:00
|
|
|
ret = REMOTE_ERROR;
|
2014-01-27 14:36:54 -05:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2010-01-15 17:01:11 -05:00
|
|
|
|
2014-05-29 17:22:00 -04:00
|
|
|
response_code = evhttp_request_get_response_code(req);
|
|
|
|
if (response_code != HTTP_OK)
|
2010-01-15 17:01:11 -05:00
|
|
|
{
|
2017-10-28 15:23:21 -04:00
|
|
|
if (ri->v6_address)
|
|
|
|
{
|
|
|
|
if (response_code != 0)
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Pairing failed with '%s' ([%s]:%d), HTTP response code %d\n", ri->pi.name, ri->v6_address, ri->v6_port, response_code);
|
|
|
|
else
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Pairing failed with '%s' ([%s]:%d), no reply from Remote\n", ri->pi.name, ri->v6_address, ri->v6_port);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (response_code != 0)
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Pairing failed with '%s' (%s:%d), HTTP response code %d\n", ri->pi.name, ri->v4_address, ri->v4_port, response_code);
|
|
|
|
else
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Pairing failed with '%s' (%s:%d), no reply from Remote\n", ri->pi.name, ri->v4_address, ri->v4_port);
|
|
|
|
}
|
2010-01-15 17:01:11 -05:00
|
|
|
|
2020-03-15 06:28:21 -04:00
|
|
|
ret = REMOTE_INVALID_PIN;
|
2010-01-15 17:01:11 -05:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2014-05-29 17:22:00 -04:00
|
|
|
input_buffer = evhttp_request_get_input_buffer(req);
|
|
|
|
|
2015-10-19 15:15:29 -04:00
|
|
|
buflen = evbuffer_get_length(input_buffer);
|
|
|
|
if (buflen < 8)
|
2010-01-30 11:07:22 -05:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Remote %s/%s: pairing response too short\n", ri->pi.remote_id, ri->pi.name);
|
|
|
|
|
2020-03-15 06:28:21 -04:00
|
|
|
ret = REMOTE_ERROR;
|
2010-01-30 11:07:22 -05:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2015-10-19 15:15:29 -04:00
|
|
|
response = evbuffer_pullup(input_buffer, -1);
|
2010-01-30 11:07:22 -05:00
|
|
|
|
|
|
|
if ((response[0] != 'c') || (response[1] != 'm') || (response[2] != 'p') || (response[3] != 'a'))
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Remote %s/%s: unknown pairing response, expected cmpa\n", ri->pi.remote_id, ri->pi.name);
|
|
|
|
|
2020-03-15 06:28:21 -04:00
|
|
|
ret = REMOTE_ERROR;
|
2010-01-30 11:07:22 -05:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = (response[4] << 24) | (response[5] << 16) | (response[6] << 8) | (response[7]);
|
2015-10-19 15:15:29 -04:00
|
|
|
if (buflen < 8 + len)
|
2010-01-30 11:07:22 -05:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Remote %s/%s: pairing response truncated (got %d expected %d)\n",
|
2015-10-19 15:15:29 -04:00
|
|
|
ri->pi.remote_id, ri->pi.name, buflen, len + 8);
|
2010-01-30 11:07:22 -05:00
|
|
|
|
2020-03-15 06:28:21 -04:00
|
|
|
ret = REMOTE_ERROR;
|
2010-01-30 11:07:22 -05:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
response += 8;
|
|
|
|
|
|
|
|
for (; len > 0; len--, response++)
|
|
|
|
{
|
|
|
|
if ((response[0] != 'c') || (response[1] != 'm') || (response[2] != 'p') || (response[3] != 'g'))
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len -= 8;
|
|
|
|
response += 8;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len < 8)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Remote %s/%s: cmpg truncated in pairing response\n", ri->pi.remote_id, ri->pi.name);
|
|
|
|
|
2020-03-15 06:28:21 -04:00
|
|
|
ret = REMOTE_ERROR;
|
2010-01-30 11:07:22 -05:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
sprintf(guid + (2 * i), "%02X", response[i]);
|
|
|
|
|
|
|
|
ri->pi.guid = strdup(guid);
|
|
|
|
|
2016-07-10 14:38:03 -04:00
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Pairing succeeded with Remote '%s' (id %s), GUID: %s\n", ri->pi.name, ri->pi.remote_id, guid);
|
2010-01-30 11:07:22 -05:00
|
|
|
|
|
|
|
ret = db_pairing_add(&ri->pi);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Failed to register pairing!\n");
|
|
|
|
|
2020-03-15 06:28:21 -04:00
|
|
|
ret = REMOTE_ERROR;
|
2010-01-30 11:07:22 -05:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2010-01-15 17:01:11 -05:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
evhttp_connection_free(ri->evcon);
|
|
|
|
free_remote(ri);
|
2020-03-15 06:28:21 -04:00
|
|
|
listener_notify(LISTENER_PAIRING);
|
|
|
|
commands_exec_end(cmdbase, ret);
|
2010-01-15 17:01:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-08 03:02:56 -04:00
|
|
|
/* Thread: main (pairing) */
|
|
|
|
static int
|
|
|
|
send_pairing_request(struct remote_info *ri, char *req_uri, int family)
|
|
|
|
{
|
|
|
|
struct evhttp_connection *evcon;
|
|
|
|
struct evhttp_request *req;
|
|
|
|
char *address;
|
|
|
|
unsigned short port;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
switch (family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
if (!ri->v4_address)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
address = ri->v4_address;
|
|
|
|
port = ri->v4_port;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AF_INET6:
|
|
|
|
if (!ri->v6_address)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
address = ri->v6_address;
|
|
|
|
port = ri->v6_port;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-05-29 17:22:00 -04:00
|
|
|
evcon = evhttp_connection_base_new(evbase_main, NULL, address, port);
|
2010-05-08 03:02:56 -04:00
|
|
|
if (!evcon)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Could not create connection for pairing with %s\n", ri->pi.name);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
req = evhttp_request_new(pairing_request_cb, ri);
|
|
|
|
if (!req)
|
|
|
|
{
|
|
|
|
DPRINTF(E_WARN, L_REMOTE, "Could not create HTTP request for pairing\n");
|
|
|
|
|
|
|
|
goto request_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = evhttp_make_request(evcon, req, EVHTTP_REQ_GET, req_uri);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_WARN, L_REMOTE, "Could not make pairing request\n");
|
|
|
|
|
|
|
|
goto request_fail;
|
|
|
|
}
|
|
|
|
|
2014-01-27 14:36:54 -05:00
|
|
|
DPRINTF(E_DBG, L_REMOTE, "Pairing requested to %s\n", req_uri);
|
|
|
|
|
2010-05-08 03:02:56 -04:00
|
|
|
ri->evcon = evcon;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
request_fail:
|
|
|
|
evhttp_connection_free(evcon);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-01-25 11:55:11 -05:00
|
|
|
/* Thread: main (pairing) */
|
2020-03-15 06:28:21 -04:00
|
|
|
static int
|
2010-01-15 17:01:11 -05:00
|
|
|
do_pairing(struct remote_info *ri)
|
|
|
|
{
|
|
|
|
char req_uri[128];
|
|
|
|
char *pairing_hash;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
pairing_hash = itunes_pairing_hash(ri->paircode, ri->pin);
|
|
|
|
if (!pairing_hash)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Could not compute pairing hash!\n");
|
|
|
|
|
|
|
|
goto hash_fail;
|
|
|
|
}
|
|
|
|
|
2010-01-30 11:06:32 -05:00
|
|
|
DPRINTF(E_DBG, L_REMOTE, "Pairing hash for %s/%s: %s\n", ri->pi.remote_id, ri->pi.name, pairing_hash);
|
2010-01-15 17:01:11 -05:00
|
|
|
|
|
|
|
/* Prepare request URI */
|
|
|
|
/* The servicename variable is the mDNS service group name; currently it's
|
|
|
|
* a hash of the library name, but in iTunes the service name and the library
|
|
|
|
* ID (DbId) are different (see comment in main.c).
|
|
|
|
* Remote uses the service name to perform mDNS lookups.
|
|
|
|
*/
|
2010-01-25 11:47:37 -05:00
|
|
|
ret = snprintf(req_uri, sizeof(req_uri), "/pair?pairingcode=%s&servicename=%016" PRIX64, pairing_hash, libhash);
|
2010-01-15 17:01:11 -05:00
|
|
|
free(pairing_hash);
|
|
|
|
if ((ret < 0) || (ret >= sizeof(req_uri)))
|
|
|
|
{
|
|
|
|
DPRINTF(E_WARN, L_REMOTE, "Request URI for pairing exceeds buffer size\n");
|
|
|
|
|
|
|
|
goto req_uri_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fire up the request */
|
2010-05-08 03:02:56 -04:00
|
|
|
if (ri->v6_address)
|
2010-01-15 17:01:11 -05:00
|
|
|
{
|
2010-05-08 03:02:56 -04:00
|
|
|
ret = send_pairing_request(ri, req_uri, AF_INET6);
|
|
|
|
if (ret == 0)
|
2020-03-15 06:28:21 -04:00
|
|
|
return 0;
|
2010-01-15 17:01:11 -05:00
|
|
|
|
2010-05-08 03:02:56 -04:00
|
|
|
DPRINTF(E_WARN, L_REMOTE, "Could not send pairing request on IPv6\n");
|
2017-10-28 15:23:21 -04:00
|
|
|
|
|
|
|
free(ri->v6_address);
|
|
|
|
ri->v6_address = NULL;
|
2010-01-15 17:01:11 -05:00
|
|
|
}
|
|
|
|
|
2010-05-08 03:02:56 -04:00
|
|
|
ret = send_pairing_request(ri, req_uri, AF_INET);
|
2010-01-15 17:01:11 -05:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2010-05-08 03:02:56 -04:00
|
|
|
DPRINTF(E_WARN, L_REMOTE, "Could not send pairing request on IPv4\n");
|
2010-01-15 17:01:11 -05:00
|
|
|
|
2010-05-08 03:02:56 -04:00
|
|
|
goto pairing_fail;
|
2010-01-15 17:01:11 -05:00
|
|
|
}
|
|
|
|
|
2020-03-15 06:28:21 -04:00
|
|
|
return 0;
|
2010-01-15 17:01:11 -05:00
|
|
|
|
2010-05-08 03:02:56 -04:00
|
|
|
pairing_fail:
|
2010-01-15 17:01:11 -05:00
|
|
|
req_uri_fail:
|
|
|
|
hash_fail:
|
|
|
|
free_remote(ri);
|
2020-03-15 06:28:21 -04:00
|
|
|
return -1;
|
2010-01-15 17:01:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-06 15:19:33 -04:00
|
|
|
/* Thread: main (mdns) */
|
|
|
|
static void
|
2010-09-18 11:16:29 -04:00
|
|
|
touch_remote_cb(const char *name, const char *type, const char *domain, const char *hostname, int family, const char *address, int port, struct keyval *txt)
|
2010-09-06 15:19:33 -04:00
|
|
|
{
|
2010-09-18 11:16:29 -04:00
|
|
|
const char *p;
|
2010-09-06 15:19:33 -04:00
|
|
|
char *devname;
|
|
|
|
char *paircode;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (port < 0)
|
|
|
|
{
|
|
|
|
/* If Remote stops advertising itself, the pairing either succeeded or
|
|
|
|
* failed; any subsequent attempt will need a new pairing pin, so
|
|
|
|
* we can just forget everything we know about the remote.
|
|
|
|
*/
|
2017-01-21 10:11:20 -05:00
|
|
|
CHECK_ERR(L_REMOTE, pthread_mutex_lock(&remote_lck));
|
2010-09-06 15:19:33 -04:00
|
|
|
|
|
|
|
remove_remote_address_byid(name, family);
|
|
|
|
|
2017-01-21 10:11:20 -05:00
|
|
|
CHECK_ERR(L_REMOTE, pthread_mutex_unlock(&remote_lck));
|
2010-09-06 15:19:33 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Get device name (DvNm field in TXT record) */
|
2010-09-18 11:16:29 -04:00
|
|
|
p = keyval_get(txt, "DvNm");
|
2010-09-06 15:19:33 -04:00
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Remote %s: no DvNm in TXT record!\n", name);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-09-18 11:16:29 -04:00
|
|
|
if (*p == '\0')
|
2010-09-06 15:19:33 -04:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Remote %s: DvNm has no value\n", name);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-09-18 11:16:29 -04:00
|
|
|
devname = strdup(p);
|
2010-09-06 15:19:33 -04:00
|
|
|
if (!devname)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Out of memory for device name\n");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get pairing code (Pair field in TXT record) */
|
2010-09-18 11:16:29 -04:00
|
|
|
p = keyval_get(txt, "Pair");
|
2010-09-06 15:19:33 -04:00
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Remote %s: no Pair in TXT record!\n", name);
|
|
|
|
|
|
|
|
free(devname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-09-18 11:16:29 -04:00
|
|
|
if (*p == '\0')
|
2010-09-06 15:19:33 -04:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Remote %s: Pair has no value\n", name);
|
|
|
|
|
|
|
|
free(devname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-09-18 11:16:29 -04:00
|
|
|
paircode = strdup(p);
|
2010-09-06 15:19:33 -04:00
|
|
|
if (!paircode)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Out of memory for paircode\n");
|
|
|
|
|
|
|
|
free(devname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-09 17:01:21 -05:00
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Discovered remote '%s' (id %s) at %s:%d, paircode %s\n", devname, name, address, port, paircode);
|
2010-09-06 15:19:33 -04:00
|
|
|
|
|
|
|
/* Add the data to the list, adding the remote to the list if needed */
|
2017-01-21 10:11:20 -05:00
|
|
|
CHECK_ERR(L_REMOTE, pthread_mutex_lock(&remote_lck));
|
2010-09-06 15:19:33 -04:00
|
|
|
|
|
|
|
ret = add_remote_mdns_data(name, family, address, port, devname, paircode);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_WARN, L_REMOTE, "Could not add Remote mDNS data, id %s\n", name);
|
|
|
|
|
|
|
|
free(devname);
|
|
|
|
free(paircode);
|
|
|
|
}
|
|
|
|
|
2017-01-21 10:11:20 -05:00
|
|
|
CHECK_ERR(L_REMOTE, pthread_mutex_unlock(&remote_lck));
|
2010-09-06 15:19:33 -04:00
|
|
|
}
|
2017-08-16 15:32:46 -04:00
|
|
|
|
|
|
|
listener_notify(LISTENER_PAIRING);
|
2010-09-06 15:19:33 -04:00
|
|
|
}
|
|
|
|
|
2017-08-16 15:32:46 -04:00
|
|
|
/*
|
|
|
|
* Returns the remote name of the current active pairing request as an allocated string (needs to be freed by the caller)
|
|
|
|
* or NULL in case there is no active pairing request.
|
|
|
|
*
|
|
|
|
* Thread: httpd
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
remote_pairing_get_name(void)
|
|
|
|
{
|
|
|
|
char *remote_name;
|
|
|
|
|
|
|
|
DPRINTF(E_DBG, L_REMOTE, "Get pairing remote name\n");
|
|
|
|
|
|
|
|
CHECK_ERR(L_REMOTE, pthread_mutex_lock(&remote_lck));
|
|
|
|
|
|
|
|
if (remote_info)
|
|
|
|
remote_name = strdup(remote_info->pi.name);
|
|
|
|
else
|
|
|
|
remote_name = NULL;
|
|
|
|
|
|
|
|
CHECK_ERR(L_REMOTE, pthread_mutex_unlock(&remote_lck));
|
|
|
|
|
|
|
|
return remote_name;
|
|
|
|
}
|
|
|
|
|
2020-03-15 06:28:21 -04:00
|
|
|
/* Thread: main (pairing) */
|
|
|
|
static enum command_state
|
|
|
|
pairing_pair(void *arg, int *retval)
|
|
|
|
{
|
|
|
|
char *pin = arg;
|
|
|
|
struct remote_info *ri;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
CHECK_ERR(L_REMOTE, pthread_mutex_lock(&remote_lck));
|
|
|
|
|
|
|
|
ret = add_remote_pin_data(pin);
|
|
|
|
if (ret == 0 && remote_info && remote_info->paircode && remote_info->pin)
|
|
|
|
{
|
|
|
|
ri = remote_info;
|
|
|
|
unlink_remote(ri);
|
|
|
|
ret = do_pairing(ri);
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_ERR(L_REMOTE, pthread_mutex_unlock(&remote_lck));
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
*retval = ret;
|
|
|
|
return COMMAND_END;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're async if we have a successful pairing request
|
|
|
|
*retval = 1;
|
|
|
|
return COMMAND_PENDING;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Thread: filescanner, mpd */
|
|
|
|
void
|
|
|
|
remote_pairing_kickoff(char **arglist)
|
|
|
|
{
|
|
|
|
char *cmdarg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = strlen(arglist[0]);
|
|
|
|
if (ret != 4)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Kickoff pairing failed, first line did not contain a 4-digit pin (got %d)\n", ret);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdarg = strdup(arglist[0]);
|
|
|
|
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Kickoff pairing with pin '%s'\n", arglist[0]);
|
|
|
|
|
|
|
|
commands_exec_async(cmdbase, pairing_pair, cmdarg);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
remote_pairing_pair(const char *pin)
|
|
|
|
{
|
|
|
|
char cmdarg[5];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = strlen(pin);
|
|
|
|
if (ret != 4)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_REMOTE, "Pairing failed, not a 4-digit pin (got %d)\n", ret);
|
|
|
|
return REMOTE_INVALID_PIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(cmdarg, pin, sizeof(cmdarg));
|
|
|
|
|
|
|
|
return commands_exec_sync(cmdbase, pairing_pair, NULL, &cmdarg);
|
|
|
|
}
|
|
|
|
|
2010-09-06 15:19:33 -04:00
|
|
|
|
2010-01-15 17:01:11 -05:00
|
|
|
/* Thread: main */
|
|
|
|
int
|
|
|
|
remote_pairing_init(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2017-03-25 02:40:07 -04:00
|
|
|
remote_info = NULL;
|
2010-01-15 17:01:11 -05:00
|
|
|
|
2017-01-21 10:11:20 -05:00
|
|
|
CHECK_ERR(L_REMOTE, mutex_init(&remote_lck));
|
|
|
|
|
2020-03-15 06:28:21 -04:00
|
|
|
cmdbase = commands_base_new(evbase_main, NULL);
|
2010-01-15 17:01:11 -05:00
|
|
|
|
2016-10-12 16:24:58 -04:00
|
|
|
// No ipv6 for remote at the moment
|
2018-10-14 16:10:28 -04:00
|
|
|
ret = mdns_browse("_touch-remote._tcp", AF_INET, touch_remote_cb, 0);
|
2010-01-15 17:01:11 -05:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_REMOTE, "Could not browse for Remote services\n");
|
|
|
|
|
|
|
|
goto mdns_browse_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
mdns_browse_fail:
|
2020-03-15 06:28:21 -04:00
|
|
|
commands_base_free(cmdbase);
|
2010-01-15 17:01:11 -05:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Thread: main */
|
|
|
|
void
|
|
|
|
remote_pairing_deinit(void)
|
|
|
|
{
|
2017-03-25 02:40:07 -04:00
|
|
|
if (remote_info)
|
|
|
|
free_remote(remote_info);
|
2010-01-28 13:19:16 -05:00
|
|
|
|
2020-03-15 06:28:21 -04:00
|
|
|
commands_base_free(cmdbase);
|
2017-01-21 10:11:20 -05:00
|
|
|
|
|
|
|
CHECK_ERR(L_REMOTE, pthread_mutex_destroy(&remote_lck));
|
2010-01-15 17:01:11 -05:00
|
|
|
}
|