VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/checksum/manifest.cpp@ 28800

Last change on this file since 28800 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.8 KB
Line 
1/* $Id: manifest.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT - Manifest file handling.
4 */
5
6/*
7 * Copyright (C) 2009 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include "internal/iprt.h"
32#include <iprt/manifest.h>
33
34#include <iprt/err.h>
35#include <iprt/file.h>
36#include <iprt/mem.h>
37#include <iprt/path.h>
38#include <iprt/sha.h>
39#include <iprt/stream.h>
40#include <iprt/string.h>
41
42
43/*******************************************************************************
44* Structures and Typedefs *
45*******************************************************************************/
46/**
47 * Internal per file structure used by RTManifestVerify
48 */
49typedef struct RTMANIFESTFILEENTRY
50{
51 char *pszManifestFile;
52 char *pszManifestDigest;
53 PRTMANIFESTTEST pTestPattern;
54} RTMANIFESTFILEENTRY;
55typedef RTMANIFESTFILEENTRY* PRTMANIFESTFILEENTRY;
56
57
58
59RTR3DECL(int) RTManifestVerify(const char *pszManifestFile, PRTMANIFESTTEST paTests, size_t cTests, size_t *piFailed)
60{
61 /* Validate input */
62 AssertPtrReturn(pszManifestFile, VERR_INVALID_POINTER);
63 AssertPtrReturn(paTests, VERR_INVALID_POINTER);
64 AssertReturn(cTests > 0, VERR_INVALID_PARAMETER);
65
66 /* Open the manifest file */
67 PRTSTREAM pStream;
68 int rc = RTStrmOpen(pszManifestFile, "r", &pStream);
69 if (RT_FAILURE(rc))
70 return rc;
71
72 PRTMANIFESTFILEENTRY paFiles = (PRTMANIFESTFILEENTRY)RTMemTmpAllocZ(sizeof(RTMANIFESTFILEENTRY) * cTests);
73 if (!paFiles)
74 {
75 RTStrmClose(pStream);
76 return VERR_NO_MEMORY;
77 }
78
79 /* Fill our compare list */
80 for (size_t i = 0; i < cTests; ++i)
81 paFiles[i].pTestPattern = &paTests[i];
82
83 /* Parse the manifest file line by line */
84 char szLine[1024];
85 for (;;)
86 {
87 rc = RTStrmGetLine(pStream, szLine, sizeof(szLine));
88 if (RT_FAILURE(rc))
89 break;
90 size_t cch = strlen(szLine);
91
92 /* Skip empty lines */
93 if (cch == 0)
94 continue;
95
96 /** @todo r=bird:
97 * -# The SHA1 test should probably include a blank space check.
98 * -# If there is a specific order to the elements in the string, it would be
99 * good if the delimiter searching checked for it.
100 * -# Deal with filenames containing delimiter characters.
101 */
102
103 /* Check for the digest algorithm */
104 if ( cch < 4
105 || !( szLine[0] == 'S'
106 && szLine[1] == 'H'
107 && szLine[2] == 'A'
108 && szLine[3] == '1'))
109 {
110 /* Digest unsupported */
111 rc = VERR_MANIFEST_UNSUPPORTED_DIGEST_TYPE;
112 break;
113 }
114
115 /* Try to find the filename */
116 char *pszNameStart = strchr(szLine, '(');
117 if (!pszNameStart)
118 {
119 rc = VERR_MANIFEST_WRONG_FILE_FORMAT;
120 break;
121 }
122 char *pszNameEnd = strchr(szLine, ')');
123 if (!pszNameEnd)
124 {
125 rc = VERR_MANIFEST_WRONG_FILE_FORMAT;
126 break;
127 }
128
129 /* Copy the filename part */
130 size_t cchName = pszNameEnd - pszNameStart - 1;
131 char *pszName = (char *)RTMemTmpAlloc(cchName + 1);
132 if (!pszName)
133 {
134 rc = VERR_NO_MEMORY;
135 break;
136 }
137 memcpy(pszName, pszNameStart + 1, cchName);
138 pszName[cchName] = '\0';
139
140 /* Try to find the digest sum */
141 char *pszDigestStart = strchr(szLine, '=');
142 if (!pszDigestStart)
143 {
144 RTMemTmpFree(pszName);
145 rc = VERR_MANIFEST_WRONG_FILE_FORMAT;
146 break;
147 }
148 char *pszDigest = ++pszDigestStart;
149
150 /* Check our file list against the extracted data */
151 bool fFound = false;
152 for (size_t i = 0; i < cTests; ++i)
153 {
154 if (!RTStrCmp(RTPathFilename(paFiles[i].pTestPattern->pszTestFile), RTStrStrip(pszName)))
155 {
156 /* Add the data of the manifest file to the file list */
157 paFiles[i].pszManifestFile = RTStrDup(RTStrStrip(pszName));
158 paFiles[i].pszManifestDigest = RTStrDup(RTStrStrip(pszDigest));
159 fFound = true;
160 break;
161 }
162 }
163 RTMemTmpFree(pszName);
164 if (!fFound)
165 {
166 /* There have to be an entry in the file list */
167 rc = VERR_MANIFEST_FILE_MISMATCH;
168 break;
169 }
170 }
171 RTStrmClose(pStream);
172
173 if ( rc == VINF_SUCCESS
174 || rc == VERR_EOF)
175 {
176 rc = VINF_SUCCESS;
177 for (size_t i = 0; i < cTests; ++i)
178 {
179 /* If there is an entry in the file list, which hasn't an
180 * equivalent in the manifest file, its an error. */
181 if ( !paFiles[i].pszManifestFile
182 || !paFiles[i].pszManifestDigest)
183 {
184 rc = VERR_MANIFEST_FILE_MISMATCH;
185 break;
186 }
187
188 /* Do the manifest SHA1 digest match against the actual digest? */
189 if (RTStrICmp(paFiles[i].pszManifestDigest, paFiles[i].pTestPattern->pszTestDigest))
190 {
191 if (piFailed)
192 *piFailed = i;
193 rc = VERR_MANIFEST_DIGEST_MISMATCH;
194 break;
195 }
196 }
197 }
198
199 /* Cleanup */
200 for (size_t i = 0; i < cTests; ++i)
201 {
202 if (paFiles[i].pszManifestFile)
203 RTStrFree(paFiles[i].pszManifestFile);
204 if (paFiles[i].pszManifestDigest)
205 RTStrFree(paFiles[i].pszManifestDigest);
206 }
207 RTMemTmpFree(paFiles);
208
209 return rc;
210}
211
212
213RTR3DECL(int) RTManifestVerifyFiles(const char *pszManifestFile, const char * const *papszFiles, size_t cFiles, size_t *piFailed)
214{
215 /* Validate input */
216 AssertPtrReturn(pszManifestFile, VERR_INVALID_POINTER);
217 AssertPtrReturn(papszFiles, VERR_INVALID_POINTER);
218
219 /* Create our compare list */
220 PRTMANIFESTTEST paFiles = (PRTMANIFESTTEST)RTMemTmpAllocZ(sizeof(RTMANIFESTTEST) * cFiles);
221 if (!paFiles)
222 return VERR_NO_MEMORY;
223
224 /* Fill our compare list */
225 int rc = VINF_SUCCESS;
226 for (size_t i = 0; i < cFiles; ++i)
227 {
228 char *pszDigest;
229 rc = RTSha1Digest(papszFiles[i], &pszDigest);
230 if (RT_FAILURE(rc))
231 break;
232 paFiles[i].pszTestFile = (char*)papszFiles[i];
233 paFiles[i].pszTestDigest = pszDigest;
234 }
235
236 /* Do the verification */
237 if (RT_SUCCESS(rc))
238 rc = RTManifestVerify(pszManifestFile, paFiles, cFiles, piFailed);
239
240 /* Cleanup */
241 for (size_t i = 0; i < cFiles; ++i)
242 {
243 if (paFiles[i].pszTestDigest)
244 RTStrFree(paFiles[i].pszTestDigest);
245 }
246 RTMemTmpFree(paFiles);
247
248 return rc;
249}
250
251
252RTR3DECL(int) RTManifestWriteFiles(const char *pszManifestFile, const char * const *papszFiles, size_t cFiles)
253{
254 /* Validate input */
255 AssertPtrReturn(pszManifestFile, VERR_INVALID_POINTER);
256 AssertPtrReturn(papszFiles, VERR_INVALID_POINTER);
257
258 /* Open a file to stream in */
259 PRTSTREAM pStream;
260 int rc = RTStrmOpen(pszManifestFile, "w", &pStream);
261 if (RT_FAILURE(rc))
262 return rc;
263
264 for (size_t i = 0; i < cFiles; ++i)
265 {
266 /* Calculate the SHA1 digest of every file */
267 char *pszDigest;
268 rc = RTSha1Digest(papszFiles[i], &pszDigest);
269 if (RT_FAILURE(rc))
270 break;
271
272 /* Add the entry to the manifest file */
273 int cch = RTStrmPrintf(pStream, "SHA1 (%s)= %s\n", RTPathFilename(papszFiles[i]), pszDigest);
274 RTStrFree(pszDigest);
275 if (RT_UNLIKELY(cch < 0))
276 {
277 rc = VERR_INTERNAL_ERROR;
278 break;
279 }
280 }
281 int rc2 = RTStrmClose(pStream);
282 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
283 rc2 = rc;
284
285 /* Delete the manifest file on failure */
286 if (RT_FAILURE(rc))
287 RTFileDelete(pszManifestFile);
288
289 return rc;
290}
291
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette