
Showing paste #kNu4:
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
/*
============================================================================
function loadconfig()
loads a config.cfg file that contains a api key and secret
here is an example of config.cfg content:
; configuration file start
Key: API_KEY
Secret: SECRET
; configuration file end
============================================================================
*/
char * loadconfig()
{
FILE *f = fopen("config.cfg", "r");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET); /* same as rewind(f); */
char *string = malloc(fsize + 1);
fread(string, fsize, 1, f);
fclose(f);
string[fsize] = 0;
return string;
}
void parsetoken(char str[], char ** key, char ** secret)
{
int iskey = 0;
int issecret = 0;
char * pch;
pch = strtok (str,": \n");
while (pch != NULL)
{
if(iskey == 1)
{
int length = strlen(pch);
*key = (char*)malloc((length+1)*sizeof(char));
sprintf(*key, "%s", pch);
(*key)[length] = '\0';
iskey = 0;
}
else if(issecret == 1)
{
int length = strlen(pch);
*secret = (char*)malloc((length+1)*sizeof(char));
sprintf(*secret, "%s", pch);
(*secret)[length] = '\0';
issecret = 0;
}
if(strcmp(pch, "Key") == 0)
{
iskey = 1;
}
else if(strcmp(pch, "Secret") == 0)
{
issecret = 1;
}
pch = strtok (NULL, ": \n");
}
}
int main(int argc, char * argv[])
{
CURL *curl;
CURLcode res;
char * config = loadconfig();
char * key = NULL;
char * secret = NULL;
parsetoken(config, &key, &secret);
char authorization_template_string[] = "Authorization: sso-key %s:%s";
int authorization_length = strlen(key)+strlen(secret)+strlen(authorization_template_string);
char * authorization = (char*)malloc((authorization_length+1)*sizeof(char));
sprintf(authorization, authorization_template_string, key, secret);
authorization[authorization_length-1] = '\0';
if(argc > 1)
{
if(strcmp(argv[1], "setup") == 0 && argc == 4)
{
static const char * data_template_string = "[\
{\
\"name\": \"_acme-challenge\",\
\"data\": \"%s\",\
\"port\": 65535,\
\"priority\": 0,\
\"protocol\": \"\",\
\"service\": \"\",\
\"ttl\": 600,\
\"type\": \"TXT\",\
\"weight\": 0\
}\
]";
static const char * url_template_string = "https://api.godaddy.com/v1/domains/%s/records";
char data[1024];
char url[4096];
sprintf(data, data_template_string, argv[3]);
sprintf(url, url_template_string, argv[2]);
struct curl_slist * header = NULL;
header = curl_slist_append(header, authorization);
header = curl_slist_append(header, "Content-Type: application/json");
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
/* if we do not provide POSTFIELDSIZE, libcurl will strlen() by
itself */
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(data));
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
curl_slist_free_all(header); /* free the list again */
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
}
else if(strcmp(argv[1], "teardown") == 0 && argc == 3)
{
static const char * url_template_string = "https://api.godaddy.com/v1/domains/%s/records/TXT/_acme-challenge";
char url[4096];
sprintf(url, url_template_string, argv[2]);
struct curl_slist * header = NULL;
header = curl_slist_append(header, authorization);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
curl_slist_free_all(header); /* free the list again */
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
}
}
return 0;
}
Make a new paste