FreeRDP/libfreerdp-utils/certstore.c

240 lines
5.0 KiB
C
Raw Normal View History

2011-08-29 02:22:24 +04:00
/**
* FreeRDP: A Remote Desktop Protocol Client
* certstore Utils
*
* Copyright 2011 Jiten Pathy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <freerdp/utils/file.h>
2011-08-29 02:22:24 +04:00
#include <freerdp/utils/certstore.h>
static const char cert_dir[] = "freerdp";
static const char cert_loc[] = "cacert";
static const char certstore_file[] = "known_hosts";
2011-08-29 02:22:24 +04:00
void certstore_create(rdpCertStore* certstore)
2011-08-29 02:22:24 +04:00
{
certstore->fp = fopen((char*) certstore->file, "w+");
2011-08-29 02:22:24 +04:00
if (certstore->fp == NULL)
{
printf("certstore_create: error opening [%s] for writing\n", certstore->file);
return;
}
2011-08-29 02:22:24 +04:00
fflush(certstore->fp);
}
void certstore_load(rdpCertStore* certstore)
2011-08-29 02:22:24 +04:00
{
certstore->fp = fopen((char*) certstore->file, "r+");
2011-08-29 02:22:24 +04:00
}
void certstore_open(rdpCertStore* certstore)
2011-08-29 02:22:24 +04:00
{
struct stat stat_info;
if (stat((char*) certstore->file, &stat_info) != 0)
2011-08-29 02:22:24 +04:00
certstore_create(certstore);
else
certstore_load(certstore);
}
void certstore_close(rdpCertStore* certstore)
2011-08-29 02:22:24 +04:00
{
if (certstore->fp != NULL)
fclose(certstore->fp);
}
char* get_local_certloc(char* home_path)
2011-08-29 02:22:24 +04:00
{
char* certloc;
2011-08-29 02:22:24 +04:00
struct stat stat_info;
if (home_path == NULL)
home_path = getenv("HOME");
certloc = (char*) xmalloc(strlen(home_path) + 2 + strlen(cert_dir) + 1 + strlen(cert_loc) + 1);
sprintf(certloc, "%s/.%s/%s", home_path, cert_dir, cert_loc);
if(stat((char*) certloc, &stat_info) != 0)
freerdp_mkdir(certloc);
2011-08-29 02:22:24 +04:00
return certloc;
}
void certstore_init(rdpCertStore* certstore)
2011-08-29 02:22:24 +04:00
{
int length;
char* home_path;
struct stat stat_info;
certstore->match = 1;
if (certstore->home_path == NULL)
home_path = getenv("HOME");
else
home_path = certstore->home_path;
2011-08-29 02:22:24 +04:00
if (home_path == NULL)
{
printf("could not get home path\n");
return;
}
certstore->home_path = (char*) xstrdup(home_path);
2011-08-29 02:22:24 +04:00
certstore->path = (char*) xmalloc(strlen(certstore->home_path) + 2 + strlen(cert_dir) + 1);
sprintf(certstore->path, "%s/.%s", certstore->home_path, cert_dir);
2011-08-29 02:22:24 +04:00
if (stat(certstore->path, &stat_info) != 0)
{
freerdp_mkdir(certstore->path);
2011-08-29 02:22:24 +04:00
printf("creating directory %s\n", certstore->path);
}
length = strlen(certstore->path);
certstore->file = (char*) xmalloc(strlen(certstore->path) + 1 + strlen(certstore_file) + 1);
2011-08-29 02:22:24 +04:00
sprintf(certstore->file, "%s/%s", certstore->path, certstore_file);
certstore_open(certstore);
}
rdpCertData* certdata_new(char* hostname, char* fingerprint)
2011-08-29 02:22:24 +04:00
{
rdpCertData* certdata;
certdata = (rdpCertData*) xzalloc(sizeof(rdpCertData));
if (certdata != NULL)
2011-08-29 02:22:24 +04:00
{
certdata->hostname = xzalloc(strlen(hostname) + 1);
certdata->fingerprint = xzalloc(strlen(fingerprint) + 1);
sprintf(certdata->hostname, "%s", hostname);
sprintf(certdata->fingerprint, "%s", fingerprint);
2011-08-29 02:22:24 +04:00
}
2011-08-29 02:22:24 +04:00
return certdata;
}
void certdata_free(rdpCertData* certdata)
2011-08-29 02:22:24 +04:00
{
if(certdata != NULL)
{
xfree(certdata->hostname);
xfree(certdata->fingerprint);
2011-08-29 02:22:24 +04:00
xfree(certdata);
}
}
rdpCertStore* certstore_new(rdpCertData* certdata, char* home_path)
2011-08-29 02:22:24 +04:00
{
rdpCertStore* certstore;
certstore = (rdpCertStore*) xzalloc(sizeof(rdpCertStore));
2011-08-29 02:22:24 +04:00
if (certstore != NULL)
{
certstore->home_path = home_path;
2011-08-29 02:22:24 +04:00
certstore->certdata = certdata;
certstore_init(certstore);
}
return certstore;
}
void certstore_free(rdpCertStore* certstore)
2011-08-29 02:22:24 +04:00
{
if (certstore != NULL)
{
certstore_close(certstore);
xfree(certstore->path);
xfree(certstore->file);
xfree(certstore->home_path);
2011-08-29 02:22:24 +04:00
certdata_free(certstore->certdata);
xfree(certstore);
}
}
int cert_data_match(rdpCertStore* certstore)
2011-08-29 02:22:24 +04:00
{
FILE* fp;
2011-08-29 02:22:24 +04:00
int length;
char* data;
char* pline;
long int size;
rdpCertData* cert_data;
fp = certstore->fp;
cert_data = certstore->certdata;
if (!fp)
return certstore->match;
fseek(fp, 0, SEEK_END);
size = ftell(fp);
fseek(fp, 0, SEEK_SET);
data = (char*) xmalloc(size + 1);
length = fread(data, size, 1, fp);
if (size < 1)
return certstore->match;
data[size] = '\n';
pline = strtok(data, "\n");
while (pline != NULL)
2011-08-29 02:22:24 +04:00
{
length = strlen(pline);
if (length > 0)
2011-08-29 02:22:24 +04:00
{
length = strcspn(pline, " \t");
pline[length] = '\0';
if (strcmp(pline, cert_data->hostname) == 0)
{
pline = &pline[length + 1];
if (strcmp(pline, cert_data->fingerprint) == 0)
certstore->match = 0;
else
certstore->match = -1;
2011-08-29 02:22:24 +04:00
break;
}
2011-08-29 09:55:16 +04:00
}
pline = strtok(NULL, "\n");
2011-08-29 02:22:24 +04:00
}
xfree(data);
2011-08-29 02:22:24 +04:00
return certstore->match;
}
void cert_data_print(rdpCertStore* certstore)
2011-08-29 02:22:24 +04:00
{
FILE* fp;
/* reopen in append mode */
fp = fopen(certstore->file, "a");
if (!fp)
return;
fprintf(certstore->fp,"%s %s\n", certstore->certdata->hostname, certstore->certdata->fingerprint);
fclose(fp);
2011-08-29 02:22:24 +04:00
}