/* rd_addlog.c * * Implementation of the AddLog Rivendell Access Library * * (C) Copyright 2017 Todd Baker * (C) Copyright 2018 Fred Gleason * * 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 #include "rd_addlog.h" #include "rd_getuseragent.h" int RD_AddLog(const char hostname[], const char username[], const char passwd[], const char ticket[], const char logname[], const char servicename[], const char user_agent[]) { char url[1500]; CURL *curl=NULL; 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; if((curl=curl_easy_init())==NULL) { curl_easy_cleanup(curl); return -1; } /* * Setup the CURL call */ snprintf(url,1500,"http://%s/rd-bin/rdxport.cgi",hostname); curl_formadd(&first, &last, CURLFORM_PTRNAME, "COMMAND", CURLFORM_COPYCONTENTS, "29", 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); curl_formadd(&first, &last, CURLFORM_PTRNAME, "LOG_NAME", CURLFORM_COPYCONTENTS, logname, CURLFORM_END); curl_formadd(&first, &last, CURLFORM_PTRNAME, "SERVICE_NAME", CURLFORM_COPYCONTENTS, servicename, CURLFORM_END); 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_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) { //Success return 0; } return (int)response_code; }