From 2916996f3ded96780df9eb44a06fda61f9d8a31f Mon Sep 17 00:00:00 2001 From: Jaroslav Kysela Date: Thu, 7 May 2015 15:09:51 +0200 Subject: [PATCH] IPTV RTSP: add basic authentication --- src/http.h | 4 +++ src/httpc.c | 44 ++++++++++++++++++++----------- src/input/mpegts/iptv/iptv_rtsp.c | 5 ++++ src/rtsp.c | 1 + 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/http.h b/src/http.h index ebd3ec8c0..4dd0c0686 100644 --- a/src/http.h +++ b/src/http.h @@ -320,6 +320,8 @@ struct http_client { int hc_rtp_multicast:1; long hc_rtsp_stream_id; int hc_rtp_timeout; + char *hc_rtsp_user; + char *hc_rtsp_pass; struct http_client_ssl *hc_ssl; /* ssl internals */ @@ -344,6 +346,8 @@ void http_client_close ( http_client_t *hc ); int http_client_send( http_client_t *hc, http_cmd_t cmd, const char *path, const char *query, http_arg_list_t *header, void *body, size_t body_size ); +void http_client_basic_auth( http_client_t *hc, http_arg_list_t *h, + const char *user, const char *pass ); int http_client_simple( http_client_t *hc, const url_t *url); int http_client_clear_state( http_client_t *hc ); int http_client_run( http_client_t *hc ); diff --git a/src/httpc.c b/src/httpc.c index 0995d506c..4bdbeb1d6 100644 --- a/src/httpc.c +++ b/src/httpc.c @@ -1036,6 +1036,31 @@ header: goto retry; } +/* + * + */ +void +http_client_basic_auth( http_client_t *hc, http_arg_list_t *h, + const char *user, const char *pass ) +{ + if (user && user[0] && pass && pass[0]) { +#define BASIC "Basic " + size_t plen = strlen(pass); + size_t ulen = strlen(user); + size_t len = BASE64_SIZE(plen + ulen + 1) + 1; + char *buf = alloca(ulen + 1 + plen + 1); + char *cbuf = alloca(len + sizeof(BASIC) + 1); + strcpy(buf, user); + strcat(buf, ":"); + strcat(buf, pass); + strcpy(cbuf, BASIC); + base64_encode(cbuf + sizeof(BASIC) - 1, len, + (uint8_t *)buf, ulen + 1 + plen); + http_arg_set(h, "Authorization", cbuf); +#undef BASIC + } +} + /* * Redirected */ @@ -1060,22 +1085,7 @@ http_client_basic_args ( http_client_t *hc, http_arg_list_t *h, const url_t *url } if (!keepalive) http_arg_set(h, "Connection", "close"); - if (url->user && url->user[0] && url->pass && url->pass[0]) { -#define BASIC "Basic " - size_t plen = strlen(url->pass); - size_t ulen = strlen(url->user); - size_t len = BASE64_SIZE(plen + ulen + 1) + 1; - char *buf = alloca(ulen + 1 + plen + 1); - char *cbuf = alloca(len + sizeof(BASIC) + 1); - strcpy(buf, url->user); - strcat(buf, ":"); - strcat(buf, url->pass); - strcpy(cbuf, BASIC); - base64_encode(cbuf + sizeof(BASIC) - 1, len, - (uint8_t *)buf, ulen + 1 + plen); - http_arg_set(h, "Authorization", cbuf); -#undef BASIC - } + http_client_basic_auth(hc, h, url->user, url->pass); } static int @@ -1398,6 +1408,8 @@ http_client_close ( http_client_t *hc ) free(hc->hc_host); free(hc->hc_scheme); free(hc->hc_bindaddr); + free(hc->hc_rtsp_user); + free(hc->hc_rtsp_pass); free(hc); } diff --git a/src/input/mpegts/iptv/iptv_rtsp.c b/src/input/mpegts/iptv/iptv_rtsp.c index 842468890..906867905 100644 --- a/src/input/mpegts/iptv/iptv_rtsp.c +++ b/src/input/mpegts/iptv/iptv_rtsp.c @@ -135,6 +135,11 @@ iptv_rtsp_start u->host, u->port, NULL))) return SM_CODE_TUNING_FAILED; + if (u->user) + hc->hc_rtsp_user = strdup(u->user); + if (u->pass) + hc->hc_rtsp_pass = strdup(u->pass); + if (udp_bind_double(&rtp, &rtpc, "IPTV", "rtp", "rtcp", NULL, 0, NULL, diff --git a/src/rtsp.c b/src/rtsp.c index 58a38b7c6..a851fa0d9 100644 --- a/src/rtsp.c +++ b/src/rtsp.c @@ -46,6 +46,7 @@ rtsp_send( http_client_t *hc, http_cmd_t cmd, } http_arg_set(hdr, "Session", hc->hc_rtsp_session); } + http_client_basic_auth(hc, hdr, hc->hc_rtsp_user, hc->hc_rtsp_pass); if (hc->hc_port != 554) snprintf(buf2, sizeof(buf2), ":%d", hc->hc_port); else -- 2.47.2