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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/stat.h>
#include <sys/types.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <libintl.h>
#include <libscf.h>
#include <libuutil.h>
#include <limits.h>
#include <md5.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <manifest_hash.h>
/*
* Translate a file name to property name. Return an allocated string or NULL
* if realpath() fails.
*/
char *
mhash_filename_to_propname(const char *in)
{
char *out, *cp, *base;
size_t len, piece_len;
out = uu_zalloc(PATH_MAX + 1);
if (realpath(in, out) == NULL) {
uu_free(out);
return (NULL);
}
base = getenv("PKG_INSTALL_ROOT");
/*
* We copy-shift over the basedir and the leading slash, since it's
* not relevant to when we boot with this repository.
*/
cp = out + ((base != NULL)? strlen(base) : 0);
if (*cp == '/')
cp++;
(void) memmove(out, cp, strlen(cp) + 1);
len = strlen(out);
if (len > scf_limit(SCF_LIMIT_MAX_NAME_LENGTH)) {
/* Use the first half and the second half. */
piece_len = (scf_limit(SCF_LIMIT_MAX_NAME_LENGTH) - 3) / 2;
(void) strncpy(out + piece_len, "__", 2);
(void) memmove(out + piece_len + 2, out + (len - piece_len),
piece_len + 1);
}
/*
* Translate non-property characters to '_', first making sure that
* we don't begin with '_'.
*/
if (!isalpha(*out))
*out = 'A';
for (cp = out + 1; *cp != '\0'; ++cp) {
if (!(isalnum(*cp) || *cp == '_' || *cp == '-'))
*cp = '_';
}
return (out);
}
int
mhash_retrieve_entry(scf_handle_t *hndl, const char *name, uchar_t *hash)
{
scf_scope_t *scope;
scf_service_t *svc;
scf_propertygroup_t *pg;
scf_property_t *prop;
scf_value_t *val;
ssize_t szret;
int result = 0;
/*
* In this implementation the hash for name is the opaque value of
* svc:/MHASH_SVC/:properties/name/MHASH_PROP
*/
if ((scope = scf_scope_create(hndl)) == NULL ||
(svc = scf_service_create(hndl)) == NULL ||
(pg = scf_pg_create(hndl)) == NULL ||
(prop = scf_property_create(hndl)) == NULL ||
(val = scf_value_create(hndl)) == NULL) {
result = -1;
goto out;
}
if (scf_handle_get_local_scope(hndl, scope) < 0) {
result = -1;
goto out;
}
if (scf_scope_get_service(scope, MHASH_SVC, svc) < 0) {
result = -1;
goto out;
}
if (scf_service_get_pg(svc, name, pg) != SCF_SUCCESS) {
result = -1;
goto out;
}
if (scf_pg_get_property(pg, MHASH_PROP, prop) != SCF_SUCCESS) {
result = -1;
goto out;
}
if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
result = -1;
goto out;
}
szret = scf_value_get_opaque(val, hash, MHASH_SIZE);
if (szret < 0) {
result = -1;
goto out;
}
if (szret != MHASH_SIZE) {
scf_value_destroy(val);
result = -1;
goto out;
}
out:
(void) scf_value_destroy(val);
scf_property_destroy(prop);
scf_pg_destroy(pg);
scf_service_destroy(svc);
scf_scope_destroy(scope);
return (result);
}
int
mhash_store_entry(scf_handle_t *hndl, const char *name, uchar_t *hash,
char **errstr)
{
scf_scope_t *scope = NULL;
scf_service_t *svc = NULL;
scf_propertygroup_t *pg = NULL;
scf_property_t *prop = NULL;
scf_value_t *val = NULL;
scf_transaction_t *tx = NULL;
scf_transaction_entry_t *e = NULL;
int ret, result = 0;
int i;
if ((scope = scf_scope_create(hndl)) == NULL ||
(svc = scf_service_create(hndl)) == NULL ||
(pg = scf_pg_create(hndl)) == NULL ||
(prop = scf_property_create(hndl)) == NULL) {
if (errstr != NULL)
*errstr = gettext("Could not create scf objects");
result = -1;
goto out;
}
if (scf_handle_get_local_scope(hndl, scope) != SCF_SUCCESS) {
if (errstr != NULL)
*errstr = gettext("Could not get local scope");
result = -1;
goto out;
}
for (i = 0; i < 5; ++i) {
scf_error_t err;
if (scf_scope_get_service(scope, MHASH_SVC, svc) ==
SCF_SUCCESS)
break;
if (scf_error() != SCF_ERROR_NOT_FOUND) {
if (errstr != NULL)
*errstr = gettext("Could not get manifest hash "
"service");
result = -1;
goto out;
}
if (scf_scope_add_service(scope, MHASH_SVC, svc) ==
SCF_SUCCESS)
break;
err = scf_error();
if (err == SCF_ERROR_EXISTS)
/* Try again. */
continue;
else if (err == SCF_ERROR_PERMISSION_DENIED) {
if (errstr != NULL)
*errstr = gettext("Could not store file hash: "
"permission denied.\n");
result = -1;
goto out;
}
if (errstr != NULL)
*errstr = gettext("Could not add manifest hash "
"service");
result = -1;
goto out;
}
if (i == 5) {
if (errstr != NULL)
*errstr = gettext("Could not store file hash: "
"service addition contention.\n");
result = -1;
goto out;
}
for (i = 0; i < 5; ++i) {
scf_error_t err;
if (scf_service_get_pg(svc, name, pg) == SCF_SUCCESS)
break;
if (scf_error() != SCF_ERROR_NOT_FOUND) {
if (errstr != NULL)
*errstr = gettext("Could not get service's "
"hash record)");
result = -1;
goto out;
}
if (scf_service_add_pg(svc, name, MHASH_PG_TYPE,
MHASH_PG_FLAGS, pg) == SCF_SUCCESS)
break;
err = scf_error();
if (err == SCF_ERROR_EXISTS)
/* Try again. */
continue;
else if (err == SCF_ERROR_PERMISSION_DENIED) {
if (errstr != NULL)
*errstr = gettext("Could not store file hash: "
"permission denied.\n");
result = -1;
goto out;
}
if (errstr != NULL)
*errstr = gettext("Could not store file hash");
result = -1;
goto out;
}
if (i == 5) {
if (errstr != NULL)
*errstr = gettext("Could not store file hash: "
"property group addition contention.\n");
result = -1;
goto out;
}
if ((e = scf_entry_create(hndl)) == NULL ||
(val = scf_value_create(hndl)) == NULL) {
if (errstr != NULL)
*errstr = gettext("Could not store file hash: "
"permission denied.\n");
result = -1;
goto out;
}
ret = scf_value_set_opaque(val, hash, MHASH_SIZE);
assert(ret == SCF_SUCCESS);
tx = scf_transaction_create(hndl);
if (tx == NULL) {
if (errstr != NULL)
*errstr = gettext("Could not create transaction");
result = -1;
goto out;
}
do {
if (scf_pg_update(pg) == -1) {
if (errstr != NULL)
*errstr = gettext("Could not update hash "
"entry");
result = -1;
goto out;
}
if (scf_transaction_start(tx, pg) != SCF_SUCCESS) {
if (scf_error() != SCF_ERROR_PERMISSION_DENIED) {
if (errstr != NULL)
*errstr = gettext("Could not start "
"hash transaction.\n");
result = -1;
goto out;
}
if (errstr != NULL)
*errstr = gettext("Could not store file hash: "
"permission denied.\n");
result = -1;
scf_transaction_destroy(tx);
(void) scf_entry_destroy(e);
goto out;
}
if (scf_transaction_property_new(tx, e, MHASH_PROP,
SCF_TYPE_OPAQUE) != SCF_SUCCESS &&
scf_transaction_property_change_type(tx, e, MHASH_PROP,
SCF_TYPE_OPAQUE) != SCF_SUCCESS) {
if (errstr != NULL)
*errstr = gettext("Could not modify hash "
"entry");
result = -1;
goto out;
}
ret = scf_entry_add_value(e, val);
assert(ret == SCF_SUCCESS);
ret = scf_transaction_commit(tx);
if (ret == 0)
scf_transaction_reset(tx);
} while (ret == 0);
if (ret < 0) {
if (scf_error() != SCF_ERROR_PERMISSION_DENIED) {
if (errstr != NULL)
*errstr = gettext("Could not store file hash: "
"permission denied.\n");
result = -1;
goto out;
}
if (errstr != NULL)
*errstr = gettext("Could not commit transaction");
result = -1;
}
scf_transaction_destroy(tx);
(void) scf_entry_destroy(e);
out:
(void) scf_value_destroy(val);
scf_property_destroy(prop);
scf_pg_destroy(pg);
scf_service_destroy(svc);
scf_scope_destroy(scope);
return (result);
}
/*
* int mhash_test_file(scf_handle_t *, const char *, uint_t, char **, uchar_t *)
* Test the given filename against the hashed metadata in the repository.
* The behaviours for import and apply are slightly different. For imports,
* if the hash value is absent or different, then the import operation
* continues. For profile application, the operation continues only if the
* hash value for the file is absent.
*
* Return non-zero if we should skip the file because it is unchanged or
* nonexistent.
*/
int
mhash_test_file(scf_handle_t *hndl, const char *file, uint_t is_profile,
char **pnamep, uchar_t *hash)
{
boolean_t do_hash;
struct stat64 st;
char *cp;
char *data;
uchar_t stored_hash[MHASH_SIZE];
char *pname;
int ret;
/*
* In the case where we are doing automated imports, we reduce the UID,
* the GID, the size, and the mtime into a string (to eliminate
* endianness) which we then make opaque as a single MD5 digest.
*
* The previous hash was composed of the inode number, the UID, the file
* size, and the mtime. This formulation was found to be insufficiently
* portable for use in highly replicated deployments. The current
* algorithm will allow matches of this "v1" hash, but always returns
* the effective "v2" hash, such that updates result in the more
* portable hash being used.
*
* If neither calculated digest matches the stored value, we consider
* the test to have failed, implying that some aspect of the manifest
* has changed.
*/
cp = getenv("SVCCFG_CHECKHASH");
do_hash = (cp != NULL && *cp != '\0');
if (!do_hash) {
*pnamep = NULL;
return (0);
}
do
ret = stat64(file, &st);
while (ret < 0 && errno == EINTR);
if (ret < 0) {
return (-1);
}
data = uu_msprintf(MHASH_FORMAT_V2, st.st_uid, st.st_gid,
st.st_size, st.st_mtime);
if (data == NULL) {
return (-1);
}
md5_calc(hash, (uchar_t *)data, strlen(data));
uu_free(data);
pname = mhash_filename_to_propname(file);
if (pname == NULL)
return (-1);
if (mhash_retrieve_entry(hndl, pname, stored_hash) == 0) {
uchar_t hash_v1[MHASH_SIZE];
if (is_profile) {
return (1);
}
/*
* Manifest import.
*/
if (memcmp(hash, stored_hash, MHASH_SIZE) == 0)
return (1);
/*
* No match on V2 hash; compare V1 hash.
*/
data = uu_msprintf(MHASH_FORMAT_V1, st.st_ino, st.st_uid,
st.st_size, st.st_mtime);
if (data == NULL)
return (-1);
md5_calc(hash_v1, (uchar_t *)data, strlen(data));
uu_free(data);
if (memcmp(hash_v1, stored_hash, MHASH_SIZE) == 0)
return (1);
}
*pnamep = pname;
return (0);
}
|