Fred Gleason 5fbfed9d38 2018-08-31 Fred Gleason <fredg@paravelsystems.com>
* Fixed buffer overflow vulnerabilities in the rivwebcapi test
	harnesses.
	* Modified rd_createticket(7) in the rivwebcapi to process UTF-8
	strings correctly.
	* Modified rd_addcart(7) in the rivwebcapi to process UTF-8
	strings correctly.
	* Modified rd_addcut(7) in the rivwebcapi to process UTF-8
	strings correctly.
	* Modified rd_addlog(7) in the rivwebcapi to process UTF-8
	strings correctly.
	* Modified rd_assignschedcode(7) in the rivwebcapi to process UTF-8
	strings correctly.
	* Modified rd_audioinfo(7) in the rivwebcapi to process UTF-8
	strings correctly.
	* Modified rd_audiostore(7) in the rivwebcapi to process UTF-8
	strings correctly.
	* Modified rd_copyaudio(7) in the rivwebcapi to process UTF-8
	strings correctly.
	* Modified rd_deletelog(7) in the rivwebcapi to process UTF-8
	strings correctly.
	* Modified rd_editcart(7) in the rivwebcapi to process UTF-8
	strings correctly.
	* Modified rd_editcut(7) in the rivwebcapi to process UTF-8
	strings correctly.
	* Modified rd_export(7) in the rivwebcapi to process UTF-8
	strings correctly.
	* Modified rd_exportpeaks(7) in the rivwebcapi to process UTF-8
	strings correctly.
	* Modified rd_listcart(7) in the rivwebcapi to process UTF-8
	strings correctly.
	* Modified rd_listcarts(7) in the rivwebcapi to process UTF-8
	strings correctly.
	* Modified rd_listcartschedcodes(7) in the rivwebcapi to process UTF-8
	strings correctly.
	* Modified rd_listcut(7) in the rivwebcapi to process UTF-8
	strings correctly.
	* Modified rd_listcuts(7) in the rivwebcapi to process UTF-8
	strings correctly.
	* Modified rd_listgroup(7) in the rivwebcapi to process UTF-8
	strings correctly.
	* Modified rd_listgroups(7) in the rivwebcapi to process UTF-8
	strings correctly.
2018-08-31 14:28:26 -04:00

208 lines
5.4 KiB
C

/* rd_audiostore.c
*
* Implementation of the AudioStore Rivendell Access Library
*
* (C) Copyright 2015 Todd Baker <bakert@rfa.org>
* (C) Copyright 2018 Fred Gleason >fredg@paravelsystems.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <expat.h>
#include "rd_common.h"
#include "rd_audiostore.h"
#include "rd_getuseragent.h"
struct xml_data {
char elem_name[256];
char strbuf[1024];
struct rd_audiostore *audiostore;
};
static void XMLCALL __AudioStoreElementStart(void *data, const char *el,
const char **attr)
{
unsigned i;
struct xml_data *xml_data=(struct xml_data *)data;
if(strcasecmp(el,"audioStore")==0) { // Allocate a new audiostore entry
xml_data->audiostore=realloc(xml_data->audiostore,
sizeof(struct rd_audiostore));
}
strlcpy(xml_data->elem_name,el,256);
memset(xml_data->strbuf,0,1024);
}
static void XMLCALL __AudioStoreElementData(void *data,const XML_Char *s,
int len)
{
struct xml_data *xml_data=(struct xml_data *)data;
memcpy(xml_data->strbuf+strlen(xml_data->strbuf),s,len);
}
static void XMLCALL __AudioStoreElementEnd(void *data, const char *el)
{
struct xml_data *xml_data=(struct xml_data *)data;
struct rd_audiostore *audiostore=xml_data->audiostore;
if(strcasecmp(el,"freeBytes")==0) {
sscanf(xml_data->strbuf,"%lu",&audiostore->freebytes);
}
if(strcasecmp(el,"totalBytes")==0){
sscanf(xml_data->strbuf,"%lu",&audiostore->totalbytes);
}
}
size_t __AudioStoreCallback(void *ptr, size_t size, size_t nmemb, void *userdata)
{
XML_Parser p=(XML_Parser)userdata;
XML_Parse(p,ptr,size*nmemb,0);
return size*nmemb;
}
int RD_AudioStore(struct rd_audiostore *audiosto[],
const char hostname[],
const char username[],
const char passwd[],
const char ticket[],
const char user_agent[],
unsigned *numrecs)
{
char url[1500];
CURL *curl=NULL;
XML_Parser parser;
struct xml_data xml_data;
long response_code;
char errbuf[CURL_ERROR_SIZE];
CURLcode res;
char user_agent_string[255];
struct curl_httppost *first=NULL;
struct curl_httppost *last=NULL;
/* Set number of recs so if fail already set */
*numrecs = 0;
if((curl=curl_easy_init())==NULL) {
curl_easy_cleanup(curl);
return -1;
}
/*
* Setup the CURL call
*/
memset(&xml_data,0,sizeof(xml_data));
parser=XML_ParserCreate(NULL);
XML_SetUserData(parser,&xml_data);
XML_SetElementHandler(parser,__AudioStoreElementStart,
__AudioStoreElementEnd);
XML_SetCharacterDataHandler(parser,__AudioStoreElementData);
snprintf(url,1500,"http://%s/rd-bin/rdxport.cgi",hostname);
curl_formadd(&first,
&last,
CURLFORM_PTRNAME,
"COMMAND",
CURLFORM_COPYCONTENTS,
"23",
CURLFORM_END);
curl_formadd(&first,
&last,
CURLFORM_PTRNAME,
"LOGIN_NAME",
CURLFORM_COPYCONTENTS,
username,
CURLFORM_END);
curl_formadd(&first,
&last,
CURLFORM_PTRNAME,
"PASSWORD",
CURLFORM_COPYCONTENTS,
passwd,
CURLFORM_END);
curl_formadd(&first,
&last,
CURLFORM_PTRNAME,
"TICKET",
CURLFORM_COPYCONTENTS,
ticket,
CURLFORM_END);
//
// Check if User Agent Present otherwise set to default
if (strlen(user_agent)> 0){
curl_easy_setopt(curl, CURLOPT_USERAGENT,user_agent);
}
else
{
strcpy(user_agent_string, RD_GetUserAgent());
strcat(user_agent_string,VERSION);
curl_easy_setopt(curl, CURLOPT_USERAGENT,user_agent_string);
}
curl_easy_setopt(curl,CURLOPT_WRITEDATA,parser);
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,__AudioStoreCallback);
curl_easy_setopt(curl,CURLOPT_URL,url);
curl_easy_setopt(curl,CURLOPT_POST,1);
curl_easy_setopt(curl,CURLOPT_HTTPPOST,first);
curl_easy_setopt(curl,CURLOPT_NOPROGRESS,1);
curl_easy_setopt(curl,CURLOPT_ERRORBUFFER,errbuf);
// curl_easy_setopt(curl,CURLOPT_VERBOSE,1);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
size_t len = strlen(errbuf);
#ifdef RIVC_DEBUG_OUT
fprintf(stderr, "\nlibcurl error: (%d)", res);
if (len)
fprintf(stderr, "%s%s", errbuf,
((errbuf[len-1] != '\n') ? "\n" : ""));
else
fprintf(stderr, "%s\n", curl_easy_strerror(res));
#endif
curl_easy_cleanup(curl);
return -1;
}
/* The response OK - so figure out if we got what we wanted.. */
curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE,&response_code);
curl_formfree(first);
curl_easy_cleanup(curl);
if (response_code > 199 && response_code < 300) {
*audiosto=xml_data.audiostore;
*numrecs = 1;
return 0;
}
else {
#ifdef RIVC_DEBUG_OUT
fprintf(stderr," rd_audiostore Call Returned Error: %s\n",xml_data.strbuf);
#endif
return (int)response_code;
}
}