X-Git-Url: https://git.piment-noir.org/?p=deb_shairplay.git;a=blobdiff_plain;f=src%2Flib%2Fraop.c;h=0c640b5bb67d85535607f078b148ce0dcb2f0358;hp=484b3f4774c2ef9cd3ed030048e43dbdb57352ce;hb=8c3f8f7a7c12c3dbd1da30dc37bab2797024c3b3;hpb=1b4a582b04fc39d9d4d930acb4d0803bdedfb32e diff --git a/src/lib/raop.c b/src/lib/raop.c index 484b3f4..0c640b5 100644 --- a/src/lib/raop.c +++ b/src/lib/raop.c @@ -1,3 +1,17 @@ +/** + * Copyright (C) 2011-2012 Juho Vähä-Herttua + * + * 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. + */ + #include #include #include @@ -6,6 +20,7 @@ #include "raop.h" #include "raop_rtp.h" #include "rsakey.h" +#include "digest.h" #include "httpd.h" #include "sdp.h" @@ -13,16 +28,23 @@ #include "utils.h" #include "netutils.h" #include "logger.h" +#include "compat.h" /* Actually 345 bytes for 2048-bit key */ #define MAX_SIGNATURE_LEN 512 +/* Let's just decide on some length */ +#define MAX_PASSWORD_LEN 64 + +/* MD5 as hex fits here */ +#define MAX_NONCE_LEN 32 + struct raop_s { /* Callbacks for audio */ raop_callbacks_t callbacks; /* Logger instance */ - logger_t logger; + logger_t *logger; /* HTTP daemon and RSA key */ httpd_t *httpd; @@ -31,6 +53,9 @@ struct raop_s { /* Hardware address information */ unsigned char hwaddr[MAX_HWADDR_LEN]; int hwaddrlen; + + /* Password information */ + char password[MAX_PASSWORD_LEN+1]; }; struct raop_conn_s { @@ -42,6 +67,8 @@ struct raop_conn_s { unsigned char *remote; int remotelen; + + char nonce[MAX_NONCE_LEN+1]; }; typedef struct raop_conn_s raop_conn_t; @@ -49,7 +76,6 @@ static void * conn_init(void *opaque, unsigned char *local, int locallen, unsigned char *remote, int remotelen) { raop_conn_t *conn; - int i; conn = calloc(1, sizeof(raop_conn_t)); if (!conn) { @@ -58,16 +84,26 @@ conn_init(void *opaque, unsigned char *local, int locallen, unsigned char *remot conn->raop = opaque; conn->raop_rtp = NULL; - logger_log(&conn->raop->logger, LOGGER_INFO, "Local: "); - for (i=0; iraop->logger, LOGGER_INFO, "%02x", local[i]); + if (locallen == 4) { + logger_log(conn->raop->logger, LOGGER_INFO, + "Local: %d.%d.%d.%d", + local[0], local[1], local[2], local[3]); + } else if (locallen == 16) { + logger_log(conn->raop->logger, LOGGER_INFO, + "Local: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", + local[0], local[1], local[2], local[3], local[4], local[5], local[6], local[7], + local[8], local[9], local[10], local[11], local[12], local[13], local[14], local[15]); } - logger_log(&conn->raop->logger, LOGGER_INFO, "\n"); - logger_log(&conn->raop->logger, LOGGER_INFO, "Remote: "); - for (i=0; iraop->logger, LOGGER_INFO, "%02x", remote[i]); + if (remotelen == 4) { + logger_log(conn->raop->logger, LOGGER_INFO, + "Remote: %d.%d.%d.%d", + remote[0], remote[1], remote[2], remote[3]); + } else if (remotelen == 16) { + logger_log(conn->raop->logger, LOGGER_INFO, + "Remote: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", + remote[0], remote[1], remote[2], remote[3], remote[4], remote[5], remote[6], remote[7], + remote[8], remote[9], remote[10], remote[11], remote[12], remote[13], remote[14], remote[15]); } - logger_log(&conn->raop->logger, LOGGER_INFO, "\n"); conn->local = malloc(locallen); assert(conn->local); @@ -79,12 +115,15 @@ conn_init(void *opaque, unsigned char *local, int locallen, unsigned char *remot conn->locallen = locallen; conn->remotelen = remotelen; + + digest_generate_nonce(conn->nonce, sizeof(conn->nonce)); return conn; } static void conn_request(void *ptr, http_request_t *request, http_response_t **response) { + const char realm[] = "airplay"; raop_conn_t *conn = ptr; raop_t *raop = conn->raop; @@ -92,6 +131,7 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response) const char *method; const char *cseq; const char *challenge; + int require_auth = 0; method = http_request_get_method(request); cseq = http_request_get_header(request, "CSeq"); @@ -100,20 +140,63 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response) } res = http_response_init("RTSP/1.0", 200, "OK"); + + /* We need authorization for everything else than OPTIONS request */ + if (strcmp(method, "OPTIONS") != 0 && strlen(raop->password)) { + const char *authorization; + + authorization = http_request_get_header(request, "Authorization"); + if (authorization) { + logger_log(conn->raop->logger, LOGGER_DEBUG, "Our nonce: %s", conn->nonce); + logger_log(conn->raop->logger, LOGGER_DEBUG, "Authorization: %s", authorization); + } + if (!digest_is_valid(realm, raop->password, conn->nonce, method, http_request_get_url(request), authorization)) { + char *authstr; + int authstrlen; + + /* Allocate the authenticate string */ + authstrlen = sizeof("Digest realm=\"\", nonce=\"\"") + sizeof(realm) + sizeof(conn->nonce) + 1; + authstr = malloc(authstrlen); + + /* Concatenate the authenticate string */ + memset(authstr, 0, authstrlen); + strcat(authstr, "Digest realm=\""); + strcat(authstr, realm); + strcat(authstr, "\", nonce=\""); + strcat(authstr, conn->nonce); + strcat(authstr, "\""); + + /* Construct a new response */ + require_auth = 1; + http_response_destroy(res); + res = http_response_init("RTSP/1.0", 401, "Unauthorized"); + http_response_add_header(res, "WWW-Authenticate", authstr); + free(authstr); + logger_log(conn->raop->logger, LOGGER_DEBUG, "Authentication unsuccessful, sending Unauthorized"); + } else { + logger_log(conn->raop->logger, LOGGER_DEBUG, "Authentication successful!"); + } + } + http_response_add_header(res, "CSeq", cseq); http_response_add_header(res, "Apple-Jack-Status", "connected; type=analog"); challenge = http_request_get_header(request, "Apple-Challenge"); - if (challenge) { + if (!require_auth && challenge) { char signature[MAX_SIGNATURE_LEN]; memset(signature, 0, sizeof(signature)); rsakey_sign(raop->rsakey, signature, sizeof(signature), challenge, conn->local, conn->locallen, raop->hwaddr, raop->hwaddrlen); - logger_log(&conn->raop->logger, LOGGER_DEBUG, "Got signature: %s\n", signature); http_response_add_header(res, "Apple-Response", signature); + + logger_log(conn->raop->logger, LOGGER_DEBUG, "Got challenge: %s", challenge); + logger_log(conn->raop->logger, LOGGER_DEBUG, "Got response: %s", signature); } - if (!strcmp(method, "OPTIONS")) { + + if (require_auth) { + /* Do nothing in case of authentication request */ + } else if (!strcmp(method, "OPTIONS")) { http_response_add_header(res, "Public", "ANNOUNCE, SETUP, RECORD, PAUSE, FLUSH, TEARDOWN, OPTIONS, GET_PARAMETER, SET_PARAMETER"); } else if (!strcmp(method, "ANNOUNCE")) { const char *data; @@ -125,21 +208,41 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response) data = http_request_get_data(request, &datalen); if (data) { - sdp_t *sdp = sdp_init(data, datalen); - logger_log(&conn->raop->logger, LOGGER_DEBUG, "rsaaeskey: %s\n", sdp_get_rsaaeskey(sdp)); - logger_log(&conn->raop->logger, LOGGER_DEBUG, "aesiv: %s\n", sdp_get_aesiv(sdp)); - - aeskeylen = rsakey_decrypt(raop->rsakey, aeskey, sizeof(aeskey), - sdp_get_rsaaeskey(sdp)); - aesivlen = rsakey_parseiv(raop->rsakey, aesiv, sizeof(aesiv), - sdp_get_aesiv(sdp)); - logger_log(&conn->raop->logger, LOGGER_DEBUG, "aeskeylen: %d\n", aeskeylen); - logger_log(&conn->raop->logger, LOGGER_DEBUG, "aesivlen: %d\n", aesivlen); - - conn->raop_rtp = raop_rtp_init(&raop->logger, &raop->callbacks, sdp_get_fmtp(sdp), aeskey, aesiv); + sdp_t *sdp; + const char *remotestr, *rtpmapstr, *fmtpstr, *aeskeystr, *aesivstr; + + sdp = sdp_init(data, datalen); + remotestr = sdp_get_connection(sdp); + rtpmapstr = sdp_get_rtpmap(sdp); + fmtpstr = sdp_get_fmtp(sdp); + aeskeystr = sdp_get_rsaaeskey(sdp); + aesivstr = sdp_get_aesiv(sdp); + + logger_log(conn->raop->logger, LOGGER_DEBUG, "connection: %s", remotestr); + logger_log(conn->raop->logger, LOGGER_DEBUG, "rtpmap: %s", rtpmapstr); + logger_log(conn->raop->logger, LOGGER_DEBUG, "fmtp: %s", fmtpstr); + logger_log(conn->raop->logger, LOGGER_DEBUG, "rsaaeskey: %s", aeskeystr); + logger_log(conn->raop->logger, LOGGER_DEBUG, "aesiv: %s", aesivstr); + + aeskeylen = rsakey_decrypt(raop->rsakey, aeskey, sizeof(aeskey), aeskeystr); + aesivlen = rsakey_parseiv(raop->rsakey, aesiv, sizeof(aesiv), aesivstr); + logger_log(conn->raop->logger, LOGGER_DEBUG, "aeskeylen: %d", aeskeylen); + logger_log(conn->raop->logger, LOGGER_DEBUG, "aesivlen: %d", aesivlen); + + if (conn->raop_rtp) { + /* This should never happen */ + raop_rtp_destroy(conn->raop_rtp); + conn->raop_rtp = NULL; + } + conn->raop_rtp = raop_rtp_init(raop->logger, &raop->callbacks, remotestr, rtpmapstr, fmtpstr, aeskey, aesiv); + if (!conn->raop_rtp) { + logger_log(conn->raop->logger, LOGGER_ERR, "Error initializing the audio decoder"); + http_response_set_disconnect(res, 1); + } sdp_destroy(sdp); } } else if (!strcmp(method, "SETUP")) { + unsigned short remote_cport=0, remote_tport=0; unsigned short cport=0, tport=0, dport=0; const char *transport; char buffer[1024]; @@ -148,38 +251,85 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response) transport = http_request_get_header(request, "Transport"); assert(transport); - logger_log(&conn->raop->logger, LOGGER_INFO, "Transport: %s\n", transport); + logger_log(conn->raop->logger, LOGGER_INFO, "Transport: %s", transport); use_udp = strncmp(transport, "RTP/AVP/TCP", 11); - - /* FIXME: Should use the parsed ports for resend */ - raop_rtp_start(conn->raop_rtp, use_udp, 1234, 1234, &cport, &tport, &dport); + if (use_udp) { + char *original, *current, *tmpstr; + + current = original = strdup(transport); + if (original) { + while ((tmpstr = utils_strsep(¤t, ";")) != NULL) { + unsigned short value; + int ret; + + ret = sscanf(tmpstr, "control_port=%hu", &value); + if (ret == 1) { + logger_log(conn->raop->logger, LOGGER_DEBUG, "Found remote control port: %hu", value); + remote_cport = value; + } + ret = sscanf(tmpstr, "timing_port=%hu", &value); + if (ret == 1) { + logger_log(conn->raop->logger, LOGGER_DEBUG, "Found remote timing port: %hu", value); + remote_tport = value; + } + } + } + free(original); + } + if (conn->raop_rtp) { + raop_rtp_start(conn->raop_rtp, use_udp, remote_cport, remote_tport, &cport, &tport, &dport); + } else { + logger_log(conn->raop->logger, LOGGER_ERR, "RAOP not initialized at SETUP, playing will fail!"); + http_response_set_disconnect(res, 1); + } memset(buffer, 0, sizeof(buffer)); if (use_udp) { snprintf(buffer, sizeof(buffer)-1, - "RTP/AVP/UDP;unicast;mode=record;timing_port=%u;events;control_port=%u;server_port=%u", + "RTP/AVP/UDP;unicast;mode=record;timing_port=%hu;events;control_port=%hu;server_port=%hu", tport, cport, dport); } else { snprintf(buffer, sizeof(buffer)-1, "RTP/AVP/TCP;unicast;interleaved=0-1;mode=record;server_port=%u", dport); } - logger_log(&conn->raop->logger, LOGGER_INFO, "Responding with %s\n", buffer); + logger_log(conn->raop->logger, LOGGER_INFO, "Responding with %s", buffer); http_response_add_header(res, "Transport", buffer); http_response_add_header(res, "Session", "DEADBEEF"); } else if (!strcmp(method, "SET_PARAMETER")) { + const char *content_type; const char *data; int datalen; - char *datastr; + content_type = http_request_get_header(request, "Content-Type"); data = http_request_get_data(request, &datalen); - datastr = calloc(1, datalen+1); - if (datastr) { - memcpy(datastr, data, datalen); - if (!strncmp(datastr, "volume: ", 8)) { - float vol = 0.0; - sscanf(data+8, "%f", &vol); - raop_rtp_set_volume(conn->raop_rtp, vol); + if (!strcmp(content_type, "text/parameters")) { + char *datastr; + datastr = calloc(1, datalen+1); + if (data && datastr && conn->raop_rtp) { + memcpy(datastr, data, datalen); + if (!strncmp(datastr, "volume: ", 8)) { + float vol = 0.0; + sscanf(datastr+8, "%f", &vol); + raop_rtp_set_volume(conn->raop_rtp, vol); + } + } else if (!conn->raop_rtp) { + logger_log(conn->raop->logger, LOGGER_WARNING, "RAOP not initialized at SET_PARAMETER volume"); + } + free(datastr); + } else if (!strcmp(content_type, "image/jpeg")) { + logger_log(conn->raop->logger, LOGGER_INFO, "Got image data of %d bytes", datalen); + if (conn->raop_rtp) { + raop_rtp_set_coverart(conn->raop_rtp, data, datalen); + } else { + logger_log(conn->raop->logger, LOGGER_WARNING, "RAOP not initialized at SET_PARAMETER coverart"); + } + } else if (!strcmp(content_type, "application/x-dmap-tagged")) { + logger_log(conn->raop->logger, LOGGER_INFO, "Got metadata of %d bytes", datalen); + if (conn->raop_rtp) { + raop_rtp_set_metadata(conn->raop_rtp, data, datalen); + } else { + logger_log(conn->raop->logger, LOGGER_WARNING, "RAOP not initialized at SET_PARAMETER metadata"); } } } else if (!strcmp(method, "FLUSH")) { @@ -187,22 +337,29 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response) int next_seq = -1; rtpinfo = http_request_get_header(request, "RTP-Info"); - assert(rtpinfo); - - logger_log(&conn->raop->logger, LOGGER_INFO, "RTP-Info: %s\n", rtpinfo); - if (!strncmp(rtpinfo, "seq=", 4)) { - next_seq = strtol(rtpinfo+4, NULL, 10); + if (rtpinfo) { + logger_log(conn->raop->logger, LOGGER_INFO, "Flush with RTP-Info: %s", rtpinfo); + if (!strncmp(rtpinfo, "seq=", 4)) { + next_seq = strtol(rtpinfo+4, NULL, 10); + } + } + if (conn->raop_rtp) { + raop_rtp_flush(conn->raop_rtp, next_seq); + } else { + logger_log(conn->raop->logger, LOGGER_WARNING, "RAOP not initialized at FLUSH"); } - raop_rtp_flush(conn->raop_rtp, next_seq); } else if (!strcmp(method, "TEARDOWN")) { http_response_add_header(res, "Connection", "close"); - raop_rtp_stop(conn->raop_rtp); - raop_rtp_destroy(conn->raop_rtp); - conn->raop_rtp = NULL; + if (conn->raop_rtp) { + /* Destroy our RTP session */ + raop_rtp_stop(conn->raop_rtp); + raop_rtp_destroy(conn->raop_rtp); + conn->raop_rtp = NULL; + } } http_response_finish(res, NULL, 0); - logger_log(&conn->raop->logger, LOGGER_DEBUG, "Got request %s with URL %s\n", method, http_request_get_url(request)); + logger_log(conn->raop->logger, LOGGER_DEBUG, "Handled request %s with URL %s", method, http_request_get_url(request)); *response = res; } @@ -212,6 +369,7 @@ conn_destroy(void *ptr) raop_conn_t *conn = ptr; if (conn->raop_rtp) { + /* This is done in case TEARDOWN was not called */ raop_rtp_destroy(conn->raop_rtp); } free(conn->local); @@ -220,7 +378,7 @@ conn_destroy(void *ptr) } raop_t * -raop_init(raop_callbacks_t *callbacks, const char *pemkey, const char *hwaddr, int hwaddrlen) +raop_init(int max_clients, raop_callbacks_t *callbacks, const char *pemkey, int *error) { raop_t *raop; httpd_t *httpd; @@ -228,8 +386,9 @@ raop_init(raop_callbacks_t *callbacks, const char *pemkey, const char *hwaddr, i httpd_callbacks_t httpd_cbs; assert(callbacks); + assert(max_clients > 0); + assert(max_clients < 100); assert(pemkey); - assert(hwaddr); /* Initialize the network */ if (netutils_init() < 0) { @@ -237,17 +396,12 @@ raop_init(raop_callbacks_t *callbacks, const char *pemkey, const char *hwaddr, i } /* Validate the callbacks structure */ - if (!callbacks->audio_init || !callbacks->audio_set_volume || - !callbacks->audio_process || !callbacks->audio_flush || + if (!callbacks->audio_init || + !callbacks->audio_process || !callbacks->audio_destroy) { return NULL; } - /* Validate hardware address */ - if (hwaddrlen > MAX_HWADDR_LEN) { - return NULL; - } - /* Allocate the raop_t structure */ raop = calloc(1, sizeof(raop_t)); if (!raop) { @@ -255,7 +409,7 @@ raop_init(raop_callbacks_t *callbacks, const char *pemkey, const char *hwaddr, i } /* Initialize the logger */ - logger_init(&raop->logger); + raop->logger = logger_init(); /* Set HTTP callbacks to our handlers */ memset(&httpd_cbs, 0, sizeof(httpd_cbs)); @@ -265,7 +419,7 @@ raop_init(raop_callbacks_t *callbacks, const char *pemkey, const char *hwaddr, i httpd_cbs.conn_destroy = &conn_destroy; /* Initialize the http daemon */ - httpd = httpd_init(&raop->logger, &httpd_cbs, 10, 1); + httpd = httpd_init(raop->logger, &httpd_cbs, max_clients); if (!httpd) { free(raop); return NULL; @@ -285,15 +439,11 @@ raop_init(raop_callbacks_t *callbacks, const char *pemkey, const char *hwaddr, i raop->httpd = httpd; raop->rsakey = rsakey; - /* Copy hwaddr to resulting structure */ - memcpy(raop->hwaddr, hwaddr, hwaddrlen); - raop->hwaddrlen = hwaddrlen; - return raop; } raop_t * -raop_init_from_keyfile(raop_callbacks_t *callbacks, const char *keyfile, const char *hwaddr, int hwaddrlen) +raop_init_from_keyfile(int max_clients, raop_callbacks_t *callbacks, const char *keyfile, int *error) { raop_t *raop; char *pemstr; @@ -301,7 +451,7 @@ raop_init_from_keyfile(raop_callbacks_t *callbacks, const char *keyfile, const c if (utils_read_file(&pemstr, keyfile) < 0) { return NULL; } - raop = raop_init(callbacks, pemstr, hwaddr, hwaddrlen); + raop = raop_init(max_clients, callbacks, pemstr, error); free(pemstr); return raop; } @@ -314,6 +464,7 @@ raop_destroy(raop_t *raop) httpd_destroy(raop->httpd); rsakey_destroy(raop->rsakey); + logger_destroy(raop->logger); free(raop); /* Cleanup the network */ @@ -322,10 +473,55 @@ raop_destroy(raop_t *raop) } int -raop_start(raop_t *raop, unsigned short *port) +raop_is_running(raop_t *raop) +{ + assert(raop); + + return httpd_is_running(raop->httpd); +} + +void +raop_set_log_level(raop_t *raop, int level) +{ + assert(raop); + + logger_set_level(raop->logger, level); +} + +void +raop_set_log_callback(raop_t *raop, raop_log_callback_t callback, void *cls) +{ + assert(raop); + + logger_set_callback(raop->logger, callback, cls); +} + +int +raop_start(raop_t *raop, unsigned short *port, const char *hwaddr, int hwaddrlen, const char *password) { assert(raop); assert(port); + assert(hwaddr); + + /* Validate hardware address */ + if (hwaddrlen > MAX_HWADDR_LEN) { + return -1; + } + + memset(raop->password, 0, sizeof(raop->password)); + if (password) { + /* Validate password */ + if (strlen(password) > MAX_PASSWORD_LEN) { + return -1; + } + + /* Copy password to the raop structure */ + strncpy(raop->password, password, MAX_PASSWORD_LEN); + } + + /* Copy hwaddr to the raop structure */ + memcpy(raop->hwaddr, hwaddr, hwaddrlen); + raop->hwaddrlen = hwaddrlen; return httpd_start(raop->httpd, port); }