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.
|
|
|
|
*/
|
|
|
|
|
2011-09-27 06:58:49 +04:00
|
|
|
#include <freerdp/utils/file.h>
|
2011-08-29 02:22:24 +04:00
|
|
|
#include <freerdp/utils/certstore.h>
|
|
|
|
|
|
|
|
static char cert_dir[] = "freerdp";
|
|
|
|
static char cert_loc[] = "cacert";
|
|
|
|
static char certstore_file[] = "known_hosts";
|
|
|
|
|
2011-09-27 06:58:49 +04:00
|
|
|
void certstore_create(rdpCertStore* certstore)
|
2011-08-29 02:22:24 +04:00
|
|
|
{
|
2011-09-27 06:58:49 +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-31 12:35:50 +04:00
|
|
|
|
2011-08-29 02:22:24 +04:00
|
|
|
fflush(certstore->fp);
|
|
|
|
}
|
|
|
|
|
2011-09-27 06:58:49 +04:00
|
|
|
void certstore_load(rdpCertStore* certstore)
|
2011-08-29 02:22:24 +04:00
|
|
|
{
|
2011-08-31 12:35:50 +04:00
|
|
|
certstore->fp = fopen((char*) certstore->file, "r+");
|
2011-08-29 02:22:24 +04:00
|
|
|
}
|
|
|
|
|
2011-09-27 06:58:49 +04:00
|
|
|
void certstore_open(rdpCertStore* certstore)
|
2011-08-29 02:22:24 +04:00
|
|
|
{
|
|
|
|
struct stat stat_info;
|
|
|
|
|
2011-08-31 12:35:50 +04:00
|
|
|
if (stat((char*) certstore->file, &stat_info) != 0)
|
2011-08-29 02:22:24 +04:00
|
|
|
certstore_create(certstore);
|
|
|
|
else
|
|
|
|
certstore_load(certstore);
|
|
|
|
}
|
|
|
|
|
2011-09-27 06:58:49 +04:00
|
|
|
void certstore_close(rdpCertStore* certstore)
|
2011-08-29 02:22:24 +04:00
|
|
|
{
|
|
|
|
if (certstore->fp != NULL)
|
|
|
|
fclose(certstore->fp);
|
|
|
|
}
|
|
|
|
|
2011-09-27 21:40:29 +04:00
|
|
|
char* get_local_certloc(char* home_path)
|
2011-08-29 02:22:24 +04:00
|
|
|
{
|
2011-08-31 12:35:50 +04:00
|
|
|
char* certloc;
|
2011-08-29 02:22:24 +04:00
|
|
|
struct stat stat_info;
|
2011-09-27 06:58:49 +04:00
|
|
|
|
2011-09-27 21:40:29 +04:00
|
|
|
if (home_path == NULL)
|
|
|
|
home_path = getenv("HOME");
|
2011-08-31 12:35:50 +04:00
|
|
|
|
2011-09-27 21:40:29 +04:00
|
|
|
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);
|
2011-08-31 12:35:50 +04:00
|
|
|
|
|
|
|
if(stat((char*) certloc, &stat_info) != 0)
|
2011-09-27 06:58:49 +04:00
|
|
|
freerdp_mkdir(certloc);
|
2011-08-31 12:35:50 +04:00
|
|
|
|
2011-08-29 02:22:24 +04:00
|
|
|
return certloc;
|
|
|
|
}
|
|
|
|
|
2011-09-27 06:58:49 +04:00
|
|
|
void certstore_init(rdpCertStore* certstore)
|
2011-08-29 02:22:24 +04:00
|
|
|
{
|
|
|
|
int length;
|
|
|
|
char* home_path;
|
|
|
|
struct stat stat_info;
|
|
|
|
|
2011-09-27 06:58:49 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-09-27 06:58:49 +04:00
|
|
|
certstore->home_path = (char*) xstrdup(home_path);
|
2011-08-29 02:22:24 +04:00
|
|
|
|
2011-09-27 06:58:49 +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)
|
|
|
|
{
|
2011-09-27 06:58:49 +04:00
|
|
|
freerdp_mkdir(certstore->path);
|
2011-08-29 02:22:24 +04:00
|
|
|
printf("creating directory %s\n", certstore->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
length = strlen(certstore->path);
|
2011-09-27 06:58:49 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-09-27 06:58:49 +04:00
|
|
|
rdpCertData* certdata_new(char* hostname, char* fingerprint)
|
2011-08-29 02:22:24 +04:00
|
|
|
{
|
2011-09-27 06:58:49 +04:00
|
|
|
rdpCertData* certdata;
|
2011-08-31 12:35:50 +04:00
|
|
|
|
2011-09-27 06:58:49 +04:00
|
|
|
certdata = (rdpCertData*) xzalloc(sizeof(rdpCertData));
|
2011-08-31 12:35:50 +04:00
|
|
|
|
2011-09-27 06:58:49 +04:00
|
|
|
if (certdata != NULL)
|
2011-08-29 02:22:24 +04:00
|
|
|
{
|
2011-09-27 06:58:49 +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-31 12:35:50 +04:00
|
|
|
|
2011-08-29 02:22:24 +04:00
|
|
|
return certdata;
|
|
|
|
}
|
|
|
|
|
2011-09-27 06:58:49 +04:00
|
|
|
void certdata_free(rdpCertData* certdata)
|
2011-08-29 02:22:24 +04:00
|
|
|
{
|
|
|
|
if(certdata != NULL)
|
|
|
|
{
|
|
|
|
xfree(certdata->hostname);
|
2011-09-27 06:58:49 +04:00
|
|
|
xfree(certdata->fingerprint);
|
2011-08-29 02:22:24 +04:00
|
|
|
xfree(certdata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-27 06:58:49 +04:00
|
|
|
rdpCertStore* certstore_new(rdpCertData* certdata, char* home_path)
|
2011-08-29 02:22:24 +04:00
|
|
|
{
|
2011-09-27 06:58:49 +04:00
|
|
|
rdpCertStore* certstore;
|
2011-08-31 12:35:50 +04:00
|
|
|
|
2011-09-27 06:58:49 +04:00
|
|
|
certstore = (rdpCertStore*) xzalloc(sizeof(rdpCertStore));
|
2011-08-29 02:22:24 +04:00
|
|
|
|
|
|
|
if (certstore != NULL)
|
|
|
|
{
|
2011-09-27 06:58:49 +04:00
|
|
|
certstore->home_path = home_path;
|
2011-08-29 02:22:24 +04:00
|
|
|
certstore->certdata = certdata;
|
|
|
|
certstore_init(certstore);
|
|
|
|
}
|
|
|
|
|
|
|
|
return certstore;
|
|
|
|
}
|
|
|
|
|
2011-09-27 06:58:49 +04:00
|
|
|
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);
|
2011-09-27 06:58:49 +04:00
|
|
|
xfree(certstore->home_path);
|
2011-08-29 02:22:24 +04:00
|
|
|
certdata_free(certstore->certdata);
|
|
|
|
xfree(certstore);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-27 06:58:49 +04:00
|
|
|
int cert_data_match(rdpCertStore* certstore)
|
2011-08-29 02:22:24 +04:00
|
|
|
{
|
2011-09-01 20:29:23 +04:00
|
|
|
FILE* fp;
|
2011-08-29 02:22:24 +04:00
|
|
|
int length;
|
2011-09-01 20:29:23 +04:00
|
|
|
char* data;
|
|
|
|
char* pline;
|
|
|
|
long int size;
|
2011-09-27 06:58:49 +04:00
|
|
|
rdpCertData* cert_data;
|
2011-09-01 20:29:23 +04:00
|
|
|
|
|
|
|
fp = certstore->fp;
|
2011-08-31 12:35:50 +04:00
|
|
|
cert_data = certstore->certdata;
|
2011-09-01 20:29:23 +04:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2011-09-01 20:29:23 +04:00
|
|
|
length = strlen(pline);
|
2011-08-31 12:35:50 +04:00
|
|
|
|
2011-09-01 20:29:23 +04:00
|
|
|
if (length > 0)
|
2011-08-29 02:22:24 +04:00
|
|
|
{
|
2011-09-01 20:29:23 +04:00
|
|
|
length = strcspn(pline, " \t");
|
|
|
|
pline[length] = '\0';
|
2011-08-31 12:35:50 +04:00
|
|
|
|
2011-09-01 20:29:23 +04:00
|
|
|
if (strcmp(pline, cert_data->hostname) == 0)
|
|
|
|
{
|
|
|
|
pline = &pline[length + 1];
|
2011-08-31 12:35:50 +04:00
|
|
|
|
2011-09-27 06:58:49 +04:00
|
|
|
if (strcmp(pline, cert_data->fingerprint) == 0)
|
2011-09-01 20:29:23 +04:00
|
|
|
certstore->match = 0;
|
|
|
|
else
|
|
|
|
certstore->match = -1;
|
2011-08-29 02:22:24 +04:00
|
|
|
break;
|
2011-09-01 20:29:23 +04:00
|
|
|
}
|
2011-08-29 09:55:16 +04:00
|
|
|
}
|
2011-09-01 20:29:23 +04:00
|
|
|
|
|
|
|
pline = strtok(NULL, "\n");
|
2011-08-29 02:22:24 +04:00
|
|
|
}
|
2011-09-01 20:29:23 +04:00
|
|
|
xfree(data);
|
2011-08-31 12:35:50 +04:00
|
|
|
|
2011-08-29 02:22:24 +04:00
|
|
|
return certstore->match;
|
|
|
|
}
|
2011-09-01 20:29:23 +04:00
|
|
|
|
2011-09-27 06:58:49 +04:00
|
|
|
void cert_data_print(rdpCertStore* certstore)
|
2011-08-29 02:22:24 +04:00
|
|
|
{
|
2011-09-27 06:58:49 +04:00
|
|
|
fseek(certstore->fp, 0, SEEK_END);
|
|
|
|
fprintf(certstore->fp,"%s %s\n", certstore->certdata->hostname, certstore->certdata->fingerprint);
|
2011-08-29 02:22:24 +04:00
|
|
|
}
|