~paulliu/ubuntu/precise/freerdp/fixext

« back to all changes in this revision

Viewing changes to libfreerdp-utils/certstore.c

  • Committer: Package Import Robot
  • Author(s): Otavio Salvador
  • Date: 2012-02-11 10:34:05 UTC
  • mfrom: (1.2.2)
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: package-import@ubuntu.com-20120211103405-x2wgdb6x8plb7cdk
Tags: upstream-1.0.1
ImportĀ upstreamĀ versionĀ 1.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * FreeRDP: A Remote Desktop Protocol Client
3
 
 * certstore Utils
4
 
 *
5
 
 * Copyright 2011 Jiten Pathy
6
 
 *
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
10
 
 *
11
 
 *               http://www.apache.org/licenses/LICENSE-2.0
12
 
 *
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.
18
 
 */
19
 
 
20
 
#include <freerdp/utils/file.h>
21
 
#include <freerdp/utils/certstore.h>
22
 
 
23
 
static const char cert_dir[] = "freerdp";
24
 
static const char cert_loc[] = "cacert";
25
 
static const char certstore_file[] = "known_hosts";
26
 
 
27
 
void certstore_create(rdpCertStore* certstore)
28
 
{
29
 
        certstore->fp = fopen((char*) certstore->file, "w+");
30
 
 
31
 
        if (certstore->fp == NULL)
32
 
        {
33
 
                printf("certstore_create: error opening [%s] for writing\n", certstore->file);
34
 
                return;
35
 
        }
36
 
 
37
 
        fflush(certstore->fp);
38
 
}
39
 
 
40
 
void certstore_load(rdpCertStore* certstore)
41
 
{
42
 
        certstore->fp = fopen((char*) certstore->file, "r+");
43
 
}
44
 
 
45
 
void certstore_open(rdpCertStore* certstore)
46
 
{
47
 
        struct stat stat_info;
48
 
 
49
 
        if (stat((char*) certstore->file, &stat_info) != 0)
50
 
                certstore_create(certstore);
51
 
        else
52
 
                certstore_load(certstore);
53
 
}
54
 
 
55
 
void certstore_close(rdpCertStore* certstore)
56
 
{
57
 
        if (certstore->fp != NULL)
58
 
                fclose(certstore->fp);
59
 
}
60
 
 
61
 
char* get_local_certloc(char* home_path)
62
 
{
63
 
        char* certloc;
64
 
        struct stat stat_info;
65
 
 
66
 
        if (home_path == NULL)
67
 
                home_path = getenv("HOME");
68
 
 
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);
71
 
 
72
 
        if(stat((char*) certloc, &stat_info) != 0)
73
 
                freerdp_mkdir(certloc);
74
 
        
75
 
        return certloc;
76
 
}
77
 
 
78
 
void certstore_init(rdpCertStore* certstore)
79
 
{
80
 
        int length;
81
 
        char* home_path;
82
 
        struct stat stat_info;
83
 
        
84
 
        certstore->match = 1;
85
 
 
86
 
        if (certstore->home_path == NULL)
87
 
                home_path = getenv("HOME");
88
 
        else
89
 
                home_path = certstore->home_path;
90
 
 
91
 
        if (home_path == NULL)
92
 
        {
93
 
                printf("could not get home path\n");
94
 
                return;
95
 
        }
96
 
 
97
 
        certstore->home_path = (char*) xstrdup(home_path);
98
 
 
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);
101
 
 
102
 
        if (stat(certstore->path, &stat_info) != 0)
103
 
        {
104
 
                freerdp_mkdir(certstore->path);
105
 
                printf("creating directory %s\n", certstore->path);
106
 
        }
107
 
 
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);
111
 
 
112
 
        certstore_open(certstore);
113
 
}
114
 
 
115
 
rdpCertData* certdata_new(char* hostname, char* fingerprint)
116
 
{
117
 
        rdpCertData* certdata;
118
 
 
119
 
        certdata = (rdpCertData*) xzalloc(sizeof(rdpCertData));
120
 
 
121
 
        if (certdata != NULL)
122
 
        {
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);
127
 
        }
128
 
 
129
 
        return certdata;
130
 
}
131
 
 
132
 
void certdata_free(rdpCertData* certdata)
133
 
{
134
 
        if(certdata != NULL)
135
 
        {
136
 
                xfree(certdata->hostname);
137
 
                xfree(certdata->fingerprint);
138
 
                xfree(certdata);
139
 
        }
140
 
}
141
 
 
142
 
rdpCertStore* certstore_new(rdpCertData* certdata, char* home_path)
143
 
{
144
 
        rdpCertStore* certstore;
145
 
 
146
 
        certstore = (rdpCertStore*) xzalloc(sizeof(rdpCertStore));
147
 
 
148
 
        if (certstore != NULL)
149
 
        {
150
 
                certstore->home_path = home_path;
151
 
                certstore->certdata = certdata;
152
 
                certstore_init(certstore);
153
 
        }
154
 
 
155
 
        return certstore;
156
 
}
157
 
 
158
 
void certstore_free(rdpCertStore* certstore)
159
 
{
160
 
        if (certstore != NULL)
161
 
        {
162
 
                certstore_close(certstore);
163
 
                xfree(certstore->path);
164
 
                xfree(certstore->file);
165
 
                xfree(certstore->home_path);
166
 
                certdata_free(certstore->certdata);
167
 
                xfree(certstore);
168
 
        }
169
 
}
170
 
 
171
 
int cert_data_match(rdpCertStore* certstore)
172
 
{
173
 
        FILE* fp;
174
 
        int length;
175
 
        char* data;
176
 
        char* pline;
177
 
        long int size;
178
 
        rdpCertData* cert_data;
179
 
 
180
 
        fp = certstore->fp;
181
 
        cert_data = certstore->certdata;
182
 
 
183
 
        if (!fp)
184
 
                return certstore->match;
185
 
 
186
 
        fseek(fp, 0, SEEK_END);
187
 
        size = ftell(fp);
188
 
        fseek(fp, 0, SEEK_SET);
189
 
 
190
 
        data = (char*) xmalloc(size + 1);
191
 
        length = fread(data, size, 1, fp);
192
 
 
193
 
        if (size < 1)
194
 
                return certstore->match;
195
 
 
196
 
        data[size] = '\n';
197
 
        pline = strtok(data, "\n");
198
 
 
199
 
        while (pline != NULL)
200
 
        {
201
 
                length = strlen(pline);
202
 
 
203
 
                if (length > 0)
204
 
                {
205
 
                        length = strcspn(pline, " \t");
206
 
                        pline[length] = '\0';
207
 
 
208
 
                        if (strcmp(pline, cert_data->hostname) == 0)
209
 
                        {
210
 
                                pline = &pline[length + 1];
211
 
 
212
 
                                if (strcmp(pline, cert_data->fingerprint) == 0)
213
 
                                        certstore->match = 0;
214
 
                                else
215
 
                                        certstore->match = -1;
216
 
                                break;
217
 
                        }
218
 
                }
219
 
 
220
 
                pline = strtok(NULL, "\n");
221
 
        }
222
 
        xfree(data);
223
 
 
224
 
        return certstore->match;
225
 
}
226
 
 
227
 
void cert_data_print(rdpCertStore* certstore)
228
 
{
229
 
        FILE* fp;
230
 
 
231
 
        /* reopen in append mode */
232
 
        fp = fopen(certstore->file, "a");
233
 
 
234
 
        if (!fp)
235
 
                return;
236
 
 
237
 
        fprintf(certstore->fp,"%s %s\n", certstore->certdata->hostname, certstore->certdata->fingerprint);
238
 
        fclose(fp);
239
 
}