2
* FreeRDP: A Remote Desktop Protocol Client
5
* Copyright 2011 Jiten Pathy
7
* Licensed under the Apache License, Version 2.0 (the "License");
8
* you may not use this file except in compliance with the License.
9
* You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
20
#include <freerdp/utils/file.h>
21
#include <freerdp/utils/certstore.h>
23
static const char cert_dir[] = "freerdp";
24
static const char cert_loc[] = "cacert";
25
static const char certstore_file[] = "known_hosts";
27
void certstore_create(rdpCertStore* certstore)
29
certstore->fp = fopen((char*) certstore->file, "w+");
31
if (certstore->fp == NULL)
33
printf("certstore_create: error opening [%s] for writing\n", certstore->file);
37
fflush(certstore->fp);
40
void certstore_load(rdpCertStore* certstore)
42
certstore->fp = fopen((char*) certstore->file, "r+");
45
void certstore_open(rdpCertStore* certstore)
47
struct stat stat_info;
49
if (stat((char*) certstore->file, &stat_info) != 0)
50
certstore_create(certstore);
52
certstore_load(certstore);
55
void certstore_close(rdpCertStore* certstore)
57
if (certstore->fp != NULL)
58
fclose(certstore->fp);
61
char* get_local_certloc(char* home_path)
64
struct stat stat_info;
66
if (home_path == NULL)
67
home_path = getenv("HOME");
69
certloc = (char*) xmalloc(strlen(home_path) + 2 + strlen(cert_dir) + 1 + strlen(cert_loc) + 1);
70
sprintf(certloc, "%s/.%s/%s", home_path, cert_dir, cert_loc);
72
if(stat((char*) certloc, &stat_info) != 0)
73
freerdp_mkdir(certloc);
78
void certstore_init(rdpCertStore* certstore)
82
struct stat stat_info;
86
if (certstore->home_path == NULL)
87
home_path = getenv("HOME");
89
home_path = certstore->home_path;
91
if (home_path == NULL)
93
printf("could not get home path\n");
97
certstore->home_path = (char*) xstrdup(home_path);
99
certstore->path = (char*) xmalloc(strlen(certstore->home_path) + 2 + strlen(cert_dir) + 1);
100
sprintf(certstore->path, "%s/.%s", certstore->home_path, cert_dir);
102
if (stat(certstore->path, &stat_info) != 0)
104
freerdp_mkdir(certstore->path);
105
printf("creating directory %s\n", certstore->path);
108
length = strlen(certstore->path);
109
certstore->file = (char*) xmalloc(strlen(certstore->path) + 1 + strlen(certstore_file) + 1);
110
sprintf(certstore->file, "%s/%s", certstore->path, certstore_file);
112
certstore_open(certstore);
115
rdpCertData* certdata_new(char* hostname, char* fingerprint)
117
rdpCertData* certdata;
119
certdata = (rdpCertData*) xzalloc(sizeof(rdpCertData));
121
if (certdata != NULL)
123
certdata->hostname = xzalloc(strlen(hostname) + 1);
124
certdata->fingerprint = xzalloc(strlen(fingerprint) + 1);
125
sprintf(certdata->hostname, "%s", hostname);
126
sprintf(certdata->fingerprint, "%s", fingerprint);
132
void certdata_free(rdpCertData* certdata)
136
xfree(certdata->hostname);
137
xfree(certdata->fingerprint);
142
rdpCertStore* certstore_new(rdpCertData* certdata, char* home_path)
144
rdpCertStore* certstore;
146
certstore = (rdpCertStore*) xzalloc(sizeof(rdpCertStore));
148
if (certstore != NULL)
150
certstore->home_path = home_path;
151
certstore->certdata = certdata;
152
certstore_init(certstore);
158
void certstore_free(rdpCertStore* certstore)
160
if (certstore != NULL)
162
certstore_close(certstore);
163
xfree(certstore->path);
164
xfree(certstore->file);
165
xfree(certstore->home_path);
166
certdata_free(certstore->certdata);
171
int cert_data_match(rdpCertStore* certstore)
178
rdpCertData* cert_data;
181
cert_data = certstore->certdata;
184
return certstore->match;
186
fseek(fp, 0, SEEK_END);
188
fseek(fp, 0, SEEK_SET);
190
data = (char*) xmalloc(size + 1);
191
length = fread(data, size, 1, fp);
194
return certstore->match;
197
pline = strtok(data, "\n");
199
while (pline != NULL)
201
length = strlen(pline);
205
length = strcspn(pline, " \t");
206
pline[length] = '\0';
208
if (strcmp(pline, cert_data->hostname) == 0)
210
pline = &pline[length + 1];
212
if (strcmp(pline, cert_data->fingerprint) == 0)
213
certstore->match = 0;
215
certstore->match = -1;
220
pline = strtok(NULL, "\n");
224
return certstore->match;
227
void cert_data_print(rdpCertStore* certstore)
231
/* reopen in append mode */
232
fp = fopen(certstore->file, "a");
237
fprintf(certstore->fp,"%s %s\n", certstore->certdata->hostname, certstore->certdata->fingerprint);