summaryrefslogtreecommitdiff
path: root/open-vm-tools/guestproxycerttool/cert_util.c
blob: ae0e4785e5664c151b07be8ba1183ffc84e6c1b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*********************************************************
 * Copyright (C) 2014-2015 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation version 2.1 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/

/*
 * certUtil.c --
 *
 *    Utilities to manage the certificates.
 */

#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <ctype.h>
#include "cert_util.h"

/*
 *----------------------------------------------------------------------
 *
 * CompareFile --
 *
 *    Check if two files are the same.
 *
 * Results:
 *    TRUE if file comparison is performed successfully, otherwise
 *    FALSE. When the returned value is TRUE, 'same' is TRUE if two
 *    input files are the same, otherwise FALSE. When the returned
 *    value is FALSE, 'same' is not defined.
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------
 */

static gboolean
CompareFile(const gchar *fname1,                 // IN
            const gchar *fname2,                 // IN
            gboolean *same)                      // OUT
{
   gsize num;
   gboolean ret = FALSE;
   GMappedFile *m1;
   GMappedFile *m2 = NULL;
   GError *error = NULL;

   m1 = g_mapped_file_new(fname1, FALSE, &error);
   if (m1 == NULL) {
      Error("Unable to map %s: %s.\n", fname1, error->message);
      goto exit;
   }

   m2 = g_mapped_file_new(fname2, FALSE, &error);
   if (m2 == NULL) {
      Error("Unable to map %s: %s.\n", fname2, error->message);
      goto exit;
   }

   ret = TRUE;
   *same = FALSE;

   num = g_mapped_file_get_length(m1);
   if (g_mapped_file_get_length(m2) == num) {
      if (num) {
         if (memcmp(g_mapped_file_get_contents(m1),
                    g_mapped_file_get_contents(m2), num) == 0) {
            *same = TRUE;
         }
      } else {
         /* Two empty files */
         *same = TRUE;
      }
   }

exit:
   g_clear_error(&error);
   if (m1) {
      g_mapped_file_unref(m1);
   }
   if (m2) {
      g_mapped_file_unref(m2);
   }

   return ret;
}


/*
 *----------------------------------------------------------------------
 *
 * CertUtil_CreateCertFileName --
 *
 *    A convenient function to make up the certificate file name based
 *    on the supplied guest proxy certificate store (certDir), subject
 *    name hash (hash), and certificate version (version).
 *
 * Results:
 *    Return the full path name of the certificate. Callers should free
 *    the returned string.
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------
 */

gchar *
CertUtil_CreateCertFileName(const gchar *certDir, // IN
                            const gchar *hash,    // IN
                            int version)          // IN
{
   gchar *ret;
   gchar *tmp;

   tmp = g_strdup_printf("%s.%d", hash, version);
   ret = g_build_filename(certDir, tmp, NULL);

   g_free(tmp);
   return ret;
}


/*
 *----------------------------------------------------------------------
 *
 * IntCmp --
 *
 *    This is an integer comparator, which is used to sort a list
 *    with ascending order.
 *
 * Results:
 *    The difference of ga and gb.
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------
 */

static gint
IntCmp(gconstpointer ga,                         // IN
       gconstpointer gb)                         // IN
{
   gint a = GPOINTER_TO_INT(ga);
   gint b = GPOINTER_TO_INT(gb);

   return (a - b);
}


/*
 *----------------------------------------------------------------------
 *
 * MatchFile --
 *
 *    Scan each file at the directory and collect file extensions of
 *    matched files. Sort the file extension list in ascending order.
 *
 * Results:
 *    Return a list of file extensions, their file names matching the
 *    regular expression. These extensions are file version numbers.
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------
 */

static GList *
MatchFile(GDir *dir,                             // IN
          GRegex *regExpr)                       // IN
{
   const gchar *fn, *cp;
   GList *list = NULL;

   while ((fn = g_dir_read_name(dir)) != NULL) {
      if (g_regex_match(regExpr, fn, 0, NULL)) {

         cp = strrchr(fn, '.');
         list = g_list_prepend(list, GINT_TO_POINTER(atoi(cp + 1)));
      }
   }

   list = g_list_sort(list, IntCmp);

   return list;
}


/*
 *----------------------------------------------------------------------
 *
 * SearchFile --
 *
 *    Search files with pattern (<fname>.[0-9]+) at a directory <path>.
 *
 * Results:
 *    Return TRUE if file search is performed successfully, otherwise
 *    FALSE. When the returned value is TRUE, 'list' is set to include
 *    a list of file extensions matching the pattern.
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------
 */

static gboolean
SearchFile(const gchar *path,                    // IN
           const gchar *fname,                   // IN
           GList **list)                         // OUT
{
   gboolean ret = FALSE;
   gchar *pattern;
   GRegex *regExpr;
   GDir *dir = NULL;
   GError *error = NULL;

   pattern = g_strdup_printf("%s.[0-9]+", fname);
   regExpr = g_regex_new(pattern, 0, 0, &error);
   if (!regExpr) {
      Error("Failed to compile %s: %s.\n", pattern, error->message);
      goto exit;
   }

   dir = g_dir_open(path, 0, &error);
   if (!dir) {
      Error("Failed to open %s: %s.\n", path, error->message);
      goto exit;
   }

   *list = MatchFile(dir, regExpr);
   ret = TRUE;

exit:
   g_free(pattern);
   g_clear_error(&error);
   if (dir) {
      g_dir_close(dir);
   }
   if (regExpr) {
      g_regex_unref(regExpr);
   }

   return ret;
}


/*
 *----------------------------------------------------------------------
 *
 * CertUtil_FindCert --
 *
 *    From the trusted certificate directory (certDir), check if
 *    there is any certificate file matching the contents of the
 *    supplied one. In general, certificate files are saved in the
 *    directory by the format of <hash>.[0-9]+.
 *
 * Results:
 *    Return TRUE if the function is successfully executed. Otherwise
 *    FALSE. When return TRUE, 'num' is set to the version of matching
 *    certificate file or -1 if no matching. For 'last', it is set to
 *    the highest version, or -1 if the store has no certificate file.
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------
 */

gboolean
CertUtil_FindCert(const gchar *certFile,          // IN
                  const gchar *certDir,           // IN
                  const gchar *hash,              // IN
                  int *num,                       // OUT
                  int *last)                      // OUT
{
   gboolean ret = FALSE;
   const GList *node;
   GList *list = NULL;
   gchar *path = NULL;

   *last = *num = -1;
   if (!SearchFile(certDir, hash, &list)) {
      goto exit;
   }

   ret = TRUE;
   if (!list) {
      goto exit;
   }

   /* *last = the highest file version */
   node = g_list_last(list);
   *last = GPOINTER_TO_INT(node->data);

   for (node = g_list_first(list); node; node = g_list_next(node)) {
      gboolean same = FALSE;
      int ext = GPOINTER_TO_INT(node->data);

      g_free(path);
      path = CertUtil_CreateCertFileName(certDir, hash, ext);

      if (!CompareFile(certFile, path, &same)) {
         ret = FALSE;
         goto exit;
      }

      if (same) {
         *num = ext;
         break;
      }
   }

exit:
   g_free(path);
   if (list) {
      g_list_free(list);
   }

   return ret;
}


/*
 *----------------------------------------------------------------------
 *
 * CertUtil_GetToolDir --
 *
 *    Get the VMware tool installation directory.
 *
 * Results:
 *    The VMware tool installation directory. Callers should not free
 *    the returned string.
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------
 */

const gchar *
CertUtil_GetToolDir(void)
{
   static gchar *path = NULL;

   if (!path) {
      path = g_build_filename(G_DIR_SEPARATOR_S, "etc", "vmware-tools", NULL);
   }

   return path;
}


/*
 *----------------------------------------------------------------------
 *
 * CertUtil_CopyFile --
 *
 *    Copy a file from source to destination.
 *
 * Results:
 *    TRUE if success, otherwise FALSE.
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------
 */

gboolean
CertUtil_CopyFile(const gchar *src,              // IN
                  const gchar *dst)              // IN
{
   gsize length;
   gboolean ret = FALSE;
   GMappedFile *smap;
   GError *error = NULL;
   FILE *file = NULL;
   const gchar *content;

   smap = g_mapped_file_new(src, FALSE, &error);
   if (!smap) {
      Error("Unable to map %s: %s.\n", src, error->message);
      goto exit;
   }

   file = fopen(dst, "w");
   if (!file) {
      Error("Failed to open %s: %s.\n", dst, strerror(errno));
      goto exit;
   }

   length = g_mapped_file_get_length(smap);
   content = g_mapped_file_get_contents(smap);
   if (fwrite(content, 1, length, file) < length) {
      Error("Failed to copy %s to %s: %s.\n", src, dst, strerror(errno));
      goto exit;
   }

   ret = TRUE;

exit:
   g_clear_error(&error);
   if (smap) {
      g_mapped_file_unref(smap);
   }
   if (file) {
      fclose(file);
   }

   return ret;
}