17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51f1b4534Scasper * Common Development and Distribution License (the "License"). 61f1b4534Scasper * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*70cbfe41SPhilippe Jung * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #include <sys/stat.h> 287c478bd9Sstevel@tonic-gate #include <sys/types.h> 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #include <assert.h> 317c478bd9Sstevel@tonic-gate #include <ctype.h> 327c478bd9Sstevel@tonic-gate #include <errno.h> 331f1b4534Scasper #include <fcntl.h> 347c478bd9Sstevel@tonic-gate #include <libintl.h> 357c478bd9Sstevel@tonic-gate #include <libscf.h> 367c478bd9Sstevel@tonic-gate #include <libuutil.h> 377c478bd9Sstevel@tonic-gate #include <limits.h> 387c478bd9Sstevel@tonic-gate #include <md5.h> 397c478bd9Sstevel@tonic-gate #include <pthread.h> 407c478bd9Sstevel@tonic-gate #include <stdio.h> 417c478bd9Sstevel@tonic-gate #include <stdlib.h> 427c478bd9Sstevel@tonic-gate #include <string.h> 437c478bd9Sstevel@tonic-gate #include <strings.h> 441f1b4534Scasper #include <unistd.h> 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate #include <manifest_hash.h> 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate /* 497c478bd9Sstevel@tonic-gate * Translate a file name to property name. Return an allocated string or NULL 50*70cbfe41SPhilippe Jung * if realpath() fails. If deathrow is true, realpath() is skipped. This 51*70cbfe41SPhilippe Jung * allows to return the property name even if the file doesn't exist. 527c478bd9Sstevel@tonic-gate */ 537c478bd9Sstevel@tonic-gate char * 54*70cbfe41SPhilippe Jung mhash_filename_to_propname(const char *in, boolean_t deathrow) 557c478bd9Sstevel@tonic-gate { 567c478bd9Sstevel@tonic-gate char *out, *cp, *base; 577c478bd9Sstevel@tonic-gate size_t len, piece_len; 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate out = uu_zalloc(PATH_MAX + 1); 60*70cbfe41SPhilippe Jung if (deathrow) { 61*70cbfe41SPhilippe Jung /* used only for service deathrow handling */ 62*70cbfe41SPhilippe Jung if (strlcpy(out, in, PATH_MAX + 1) >= (PATH_MAX + 1)) { 63*70cbfe41SPhilippe Jung uu_free(out); 64*70cbfe41SPhilippe Jung return (NULL); 65*70cbfe41SPhilippe Jung } 66*70cbfe41SPhilippe Jung } else { 67*70cbfe41SPhilippe Jung if (realpath(in, out) == NULL) { 68*70cbfe41SPhilippe Jung uu_free(out); 69*70cbfe41SPhilippe Jung return (NULL); 70*70cbfe41SPhilippe Jung } 717c478bd9Sstevel@tonic-gate } 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate base = getenv("PKG_INSTALL_ROOT"); 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate /* 767c478bd9Sstevel@tonic-gate * We copy-shift over the basedir and the leading slash, since it's 777c478bd9Sstevel@tonic-gate * not relevant to when we boot with this repository. 787c478bd9Sstevel@tonic-gate */ 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate cp = out + ((base != NULL)? strlen(base) : 0); 817c478bd9Sstevel@tonic-gate if (*cp == '/') 827c478bd9Sstevel@tonic-gate cp++; 837c478bd9Sstevel@tonic-gate (void) memmove(out, cp, strlen(cp) + 1); 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate len = strlen(out); 867c478bd9Sstevel@tonic-gate if (len > scf_limit(SCF_LIMIT_MAX_NAME_LENGTH)) { 877c478bd9Sstevel@tonic-gate /* Use the first half and the second half. */ 887c478bd9Sstevel@tonic-gate piece_len = (scf_limit(SCF_LIMIT_MAX_NAME_LENGTH) - 3) / 2; 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate (void) strncpy(out + piece_len, "__", 2); 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate (void) memmove(out + piece_len + 2, out + (len - piece_len), 937c478bd9Sstevel@tonic-gate piece_len + 1); 947c478bd9Sstevel@tonic-gate } 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate /* 977c478bd9Sstevel@tonic-gate * Translate non-property characters to '_', first making sure that 987c478bd9Sstevel@tonic-gate * we don't begin with '_'. 997c478bd9Sstevel@tonic-gate */ 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate if (!isalpha(*out)) 1027c478bd9Sstevel@tonic-gate *out = 'A'; 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate for (cp = out + 1; *cp != '\0'; ++cp) { 1057c478bd9Sstevel@tonic-gate if (!(isalnum(*cp) || *cp == '_' || *cp == '-')) 1067c478bd9Sstevel@tonic-gate *cp = '_'; 1077c478bd9Sstevel@tonic-gate } 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate return (out); 1107c478bd9Sstevel@tonic-gate } 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate int 1137c478bd9Sstevel@tonic-gate mhash_retrieve_entry(scf_handle_t *hndl, const char *name, uchar_t *hash) 1147c478bd9Sstevel@tonic-gate { 1157c478bd9Sstevel@tonic-gate scf_scope_t *scope; 1167c478bd9Sstevel@tonic-gate scf_service_t *svc; 1177c478bd9Sstevel@tonic-gate scf_propertygroup_t *pg; 1187c478bd9Sstevel@tonic-gate scf_property_t *prop; 1197c478bd9Sstevel@tonic-gate scf_value_t *val; 1207c478bd9Sstevel@tonic-gate ssize_t szret; 1217c478bd9Sstevel@tonic-gate int result = 0; 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate /* 1247c478bd9Sstevel@tonic-gate * In this implementation the hash for name is the opaque value of 1257c478bd9Sstevel@tonic-gate * svc:/MHASH_SVC/:properties/name/MHASH_PROP 1267c478bd9Sstevel@tonic-gate */ 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate if ((scope = scf_scope_create(hndl)) == NULL || 1297c478bd9Sstevel@tonic-gate (svc = scf_service_create(hndl)) == NULL || 1307c478bd9Sstevel@tonic-gate (pg = scf_pg_create(hndl)) == NULL || 1317c478bd9Sstevel@tonic-gate (prop = scf_property_create(hndl)) == NULL || 1327c478bd9Sstevel@tonic-gate (val = scf_value_create(hndl)) == NULL) { 1337c478bd9Sstevel@tonic-gate result = -1; 1347c478bd9Sstevel@tonic-gate goto out; 1357c478bd9Sstevel@tonic-gate } 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate if (scf_handle_get_local_scope(hndl, scope) < 0) { 1387c478bd9Sstevel@tonic-gate result = -1; 1397c478bd9Sstevel@tonic-gate goto out; 1407c478bd9Sstevel@tonic-gate } 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate if (scf_scope_get_service(scope, MHASH_SVC, svc) < 0) { 1437c478bd9Sstevel@tonic-gate result = -1; 1447c478bd9Sstevel@tonic-gate goto out; 1457c478bd9Sstevel@tonic-gate } 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate if (scf_service_get_pg(svc, name, pg) != SCF_SUCCESS) { 1487c478bd9Sstevel@tonic-gate result = -1; 1497c478bd9Sstevel@tonic-gate goto out; 1507c478bd9Sstevel@tonic-gate } 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate if (scf_pg_get_property(pg, MHASH_PROP, prop) != SCF_SUCCESS) { 1537c478bd9Sstevel@tonic-gate result = -1; 1547c478bd9Sstevel@tonic-gate goto out; 1557c478bd9Sstevel@tonic-gate } 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate if (scf_property_get_value(prop, val) != SCF_SUCCESS) { 1587c478bd9Sstevel@tonic-gate result = -1; 1597c478bd9Sstevel@tonic-gate goto out; 1607c478bd9Sstevel@tonic-gate } 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate szret = scf_value_get_opaque(val, hash, MHASH_SIZE); 1637c478bd9Sstevel@tonic-gate if (szret < 0) { 1647c478bd9Sstevel@tonic-gate result = -1; 1657c478bd9Sstevel@tonic-gate goto out; 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate 1681f1b4534Scasper /* 1691f1b4534Scasper * Make sure that the old hash is returned with 1701f1b4534Scasper * remainder of the bytes zeroed. 1711f1b4534Scasper */ 1721f1b4534Scasper if (szret == MHASH_SIZE_OLD) { 1731f1b4534Scasper (void) memset(hash + MHASH_SIZE_OLD, 0, 1741f1b4534Scasper MHASH_SIZE - MHASH_SIZE_OLD); 1751f1b4534Scasper } else if (szret != MHASH_SIZE) { 1767c478bd9Sstevel@tonic-gate scf_value_destroy(val); 1777c478bd9Sstevel@tonic-gate result = -1; 1787c478bd9Sstevel@tonic-gate goto out; 1797c478bd9Sstevel@tonic-gate } 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate out: 1827c478bd9Sstevel@tonic-gate (void) scf_value_destroy(val); 1837c478bd9Sstevel@tonic-gate scf_property_destroy(prop); 1847c478bd9Sstevel@tonic-gate scf_pg_destroy(pg); 1857c478bd9Sstevel@tonic-gate scf_service_destroy(svc); 1867c478bd9Sstevel@tonic-gate scf_scope_destroy(scope); 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate return (result); 1897c478bd9Sstevel@tonic-gate } 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate int 1927c478bd9Sstevel@tonic-gate mhash_store_entry(scf_handle_t *hndl, const char *name, uchar_t *hash, 1937c478bd9Sstevel@tonic-gate char **errstr) 1947c478bd9Sstevel@tonic-gate { 1957c478bd9Sstevel@tonic-gate scf_scope_t *scope = NULL; 1967c478bd9Sstevel@tonic-gate scf_service_t *svc = NULL; 1977c478bd9Sstevel@tonic-gate scf_propertygroup_t *pg = NULL; 1987c478bd9Sstevel@tonic-gate scf_property_t *prop = NULL; 1997c478bd9Sstevel@tonic-gate scf_value_t *val = NULL; 2007c478bd9Sstevel@tonic-gate scf_transaction_t *tx = NULL; 2017c478bd9Sstevel@tonic-gate scf_transaction_entry_t *e = NULL; 2027c478bd9Sstevel@tonic-gate int ret, result = 0; 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate int i; 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate if ((scope = scf_scope_create(hndl)) == NULL || 2077c478bd9Sstevel@tonic-gate (svc = scf_service_create(hndl)) == NULL || 2087c478bd9Sstevel@tonic-gate (pg = scf_pg_create(hndl)) == NULL || 2097c478bd9Sstevel@tonic-gate (prop = scf_property_create(hndl)) == NULL) { 2107c478bd9Sstevel@tonic-gate if (errstr != NULL) 2117c478bd9Sstevel@tonic-gate *errstr = gettext("Could not create scf objects"); 2127c478bd9Sstevel@tonic-gate result = -1; 2137c478bd9Sstevel@tonic-gate goto out; 2147c478bd9Sstevel@tonic-gate } 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate if (scf_handle_get_local_scope(hndl, scope) != SCF_SUCCESS) { 2177c478bd9Sstevel@tonic-gate if (errstr != NULL) 2187c478bd9Sstevel@tonic-gate *errstr = gettext("Could not get local scope"); 2197c478bd9Sstevel@tonic-gate result = -1; 2207c478bd9Sstevel@tonic-gate goto out; 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate for (i = 0; i < 5; ++i) { 2247c478bd9Sstevel@tonic-gate scf_error_t err; 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate if (scf_scope_get_service(scope, MHASH_SVC, svc) == 2277c478bd9Sstevel@tonic-gate SCF_SUCCESS) 2287c478bd9Sstevel@tonic-gate break; 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND) { 2317c478bd9Sstevel@tonic-gate if (errstr != NULL) 2327c478bd9Sstevel@tonic-gate *errstr = gettext("Could not get manifest hash " 2337c478bd9Sstevel@tonic-gate "service"); 2347c478bd9Sstevel@tonic-gate result = -1; 2357c478bd9Sstevel@tonic-gate goto out; 2367c478bd9Sstevel@tonic-gate } 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate if (scf_scope_add_service(scope, MHASH_SVC, svc) == 2397c478bd9Sstevel@tonic-gate SCF_SUCCESS) 2407c478bd9Sstevel@tonic-gate break; 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate err = scf_error(); 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate if (err == SCF_ERROR_EXISTS) 2457c478bd9Sstevel@tonic-gate /* Try again. */ 2467c478bd9Sstevel@tonic-gate continue; 2477c478bd9Sstevel@tonic-gate else if (err == SCF_ERROR_PERMISSION_DENIED) { 2487c478bd9Sstevel@tonic-gate if (errstr != NULL) 2497c478bd9Sstevel@tonic-gate *errstr = gettext("Could not store file hash: " 2507c478bd9Sstevel@tonic-gate "permission denied.\n"); 2517c478bd9Sstevel@tonic-gate result = -1; 2527c478bd9Sstevel@tonic-gate goto out; 2537c478bd9Sstevel@tonic-gate } 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate if (errstr != NULL) 2567c478bd9Sstevel@tonic-gate *errstr = gettext("Could not add manifest hash " 2577c478bd9Sstevel@tonic-gate "service"); 2587c478bd9Sstevel@tonic-gate result = -1; 2597c478bd9Sstevel@tonic-gate goto out; 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate if (i == 5) { 2637c478bd9Sstevel@tonic-gate if (errstr != NULL) 2647c478bd9Sstevel@tonic-gate *errstr = gettext("Could not store file hash: " 2657c478bd9Sstevel@tonic-gate "service addition contention.\n"); 2667c478bd9Sstevel@tonic-gate result = -1; 2677c478bd9Sstevel@tonic-gate goto out; 2687c478bd9Sstevel@tonic-gate } 2697c478bd9Sstevel@tonic-gate 2707c478bd9Sstevel@tonic-gate for (i = 0; i < 5; ++i) { 2717c478bd9Sstevel@tonic-gate scf_error_t err; 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate if (scf_service_get_pg(svc, name, pg) == SCF_SUCCESS) 2747c478bd9Sstevel@tonic-gate break; 2757c478bd9Sstevel@tonic-gate 2767c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND) { 2777c478bd9Sstevel@tonic-gate if (errstr != NULL) 2787c478bd9Sstevel@tonic-gate *errstr = gettext("Could not get service's " 2797c478bd9Sstevel@tonic-gate "hash record)"); 2807c478bd9Sstevel@tonic-gate result = -1; 2817c478bd9Sstevel@tonic-gate goto out; 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate if (scf_service_add_pg(svc, name, MHASH_PG_TYPE, 2857c478bd9Sstevel@tonic-gate MHASH_PG_FLAGS, pg) == SCF_SUCCESS) 2867c478bd9Sstevel@tonic-gate break; 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate err = scf_error(); 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate if (err == SCF_ERROR_EXISTS) 2917c478bd9Sstevel@tonic-gate /* Try again. */ 2927c478bd9Sstevel@tonic-gate continue; 2937c478bd9Sstevel@tonic-gate else if (err == SCF_ERROR_PERMISSION_DENIED) { 2947c478bd9Sstevel@tonic-gate if (errstr != NULL) 2957c478bd9Sstevel@tonic-gate *errstr = gettext("Could not store file hash: " 2967c478bd9Sstevel@tonic-gate "permission denied.\n"); 2977c478bd9Sstevel@tonic-gate result = -1; 2987c478bd9Sstevel@tonic-gate goto out; 2997c478bd9Sstevel@tonic-gate } 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate if (errstr != NULL) 3027c478bd9Sstevel@tonic-gate *errstr = gettext("Could not store file hash"); 3037c478bd9Sstevel@tonic-gate result = -1; 3047c478bd9Sstevel@tonic-gate goto out; 3057c478bd9Sstevel@tonic-gate } 3067c478bd9Sstevel@tonic-gate if (i == 5) { 3077c478bd9Sstevel@tonic-gate if (errstr != NULL) 3087c478bd9Sstevel@tonic-gate *errstr = gettext("Could not store file hash: " 3097c478bd9Sstevel@tonic-gate "property group addition contention.\n"); 3107c478bd9Sstevel@tonic-gate result = -1; 3117c478bd9Sstevel@tonic-gate goto out; 3127c478bd9Sstevel@tonic-gate } 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate if ((e = scf_entry_create(hndl)) == NULL || 3157c478bd9Sstevel@tonic-gate (val = scf_value_create(hndl)) == NULL) { 3167c478bd9Sstevel@tonic-gate if (errstr != NULL) 3177c478bd9Sstevel@tonic-gate *errstr = gettext("Could not store file hash: " 3187c478bd9Sstevel@tonic-gate "permission denied.\n"); 3197c478bd9Sstevel@tonic-gate result = -1; 3207c478bd9Sstevel@tonic-gate goto out; 3217c478bd9Sstevel@tonic-gate } 3227c478bd9Sstevel@tonic-gate 3237c478bd9Sstevel@tonic-gate ret = scf_value_set_opaque(val, hash, MHASH_SIZE); 3247c478bd9Sstevel@tonic-gate assert(ret == SCF_SUCCESS); 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate tx = scf_transaction_create(hndl); 3277c478bd9Sstevel@tonic-gate if (tx == NULL) { 3287c478bd9Sstevel@tonic-gate if (errstr != NULL) 3297c478bd9Sstevel@tonic-gate *errstr = gettext("Could not create transaction"); 3307c478bd9Sstevel@tonic-gate result = -1; 3317c478bd9Sstevel@tonic-gate goto out; 3327c478bd9Sstevel@tonic-gate } 3337c478bd9Sstevel@tonic-gate 3347c478bd9Sstevel@tonic-gate do { 3357c478bd9Sstevel@tonic-gate if (scf_pg_update(pg) == -1) { 3367c478bd9Sstevel@tonic-gate if (errstr != NULL) 3377c478bd9Sstevel@tonic-gate *errstr = gettext("Could not update hash " 3387c478bd9Sstevel@tonic-gate "entry"); 3397c478bd9Sstevel@tonic-gate result = -1; 3407c478bd9Sstevel@tonic-gate goto out; 3417c478bd9Sstevel@tonic-gate } 3427c478bd9Sstevel@tonic-gate if (scf_transaction_start(tx, pg) != SCF_SUCCESS) { 3437c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_PERMISSION_DENIED) { 3447c478bd9Sstevel@tonic-gate if (errstr != NULL) 3457c478bd9Sstevel@tonic-gate *errstr = gettext("Could not start " 3467c478bd9Sstevel@tonic-gate "hash transaction.\n"); 3477c478bd9Sstevel@tonic-gate result = -1; 3487c478bd9Sstevel@tonic-gate goto out; 3497c478bd9Sstevel@tonic-gate } 3507c478bd9Sstevel@tonic-gate 3517c478bd9Sstevel@tonic-gate if (errstr != NULL) 3527c478bd9Sstevel@tonic-gate *errstr = gettext("Could not store file hash: " 3537c478bd9Sstevel@tonic-gate "permission denied.\n"); 3547c478bd9Sstevel@tonic-gate result = -1; 3557c478bd9Sstevel@tonic-gate 3567c478bd9Sstevel@tonic-gate scf_transaction_destroy(tx); 3577c478bd9Sstevel@tonic-gate (void) scf_entry_destroy(e); 3587c478bd9Sstevel@tonic-gate goto out; 3597c478bd9Sstevel@tonic-gate } 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate if (scf_transaction_property_new(tx, e, MHASH_PROP, 3627c478bd9Sstevel@tonic-gate SCF_TYPE_OPAQUE) != SCF_SUCCESS && 3637c478bd9Sstevel@tonic-gate scf_transaction_property_change_type(tx, e, MHASH_PROP, 3647c478bd9Sstevel@tonic-gate SCF_TYPE_OPAQUE) != SCF_SUCCESS) { 3657c478bd9Sstevel@tonic-gate if (errstr != NULL) 3667c478bd9Sstevel@tonic-gate *errstr = gettext("Could not modify hash " 3677c478bd9Sstevel@tonic-gate "entry"); 3687c478bd9Sstevel@tonic-gate result = -1; 3697c478bd9Sstevel@tonic-gate goto out; 3707c478bd9Sstevel@tonic-gate } 3717c478bd9Sstevel@tonic-gate 3727c478bd9Sstevel@tonic-gate ret = scf_entry_add_value(e, val); 3737c478bd9Sstevel@tonic-gate assert(ret == SCF_SUCCESS); 3747c478bd9Sstevel@tonic-gate 3757c478bd9Sstevel@tonic-gate ret = scf_transaction_commit(tx); 3767c478bd9Sstevel@tonic-gate 3777c478bd9Sstevel@tonic-gate if (ret == 0) 3787c478bd9Sstevel@tonic-gate scf_transaction_reset(tx); 3797c478bd9Sstevel@tonic-gate } while (ret == 0); 3807c478bd9Sstevel@tonic-gate 3817c478bd9Sstevel@tonic-gate if (ret < 0) { 3827c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_PERMISSION_DENIED) { 3837c478bd9Sstevel@tonic-gate if (errstr != NULL) 3847c478bd9Sstevel@tonic-gate *errstr = gettext("Could not store file hash: " 3857c478bd9Sstevel@tonic-gate "permission denied.\n"); 3867c478bd9Sstevel@tonic-gate result = -1; 3877c478bd9Sstevel@tonic-gate goto out; 3887c478bd9Sstevel@tonic-gate } 3897c478bd9Sstevel@tonic-gate 3907c478bd9Sstevel@tonic-gate if (errstr != NULL) 3917c478bd9Sstevel@tonic-gate *errstr = gettext("Could not commit transaction"); 3927c478bd9Sstevel@tonic-gate result = -1; 3937c478bd9Sstevel@tonic-gate } 3947c478bd9Sstevel@tonic-gate 3957c478bd9Sstevel@tonic-gate scf_transaction_destroy(tx); 3967c478bd9Sstevel@tonic-gate (void) scf_entry_destroy(e); 3977c478bd9Sstevel@tonic-gate 3987c478bd9Sstevel@tonic-gate out: 3997c478bd9Sstevel@tonic-gate (void) scf_value_destroy(val); 4007c478bd9Sstevel@tonic-gate scf_property_destroy(prop); 4017c478bd9Sstevel@tonic-gate scf_pg_destroy(pg); 4027c478bd9Sstevel@tonic-gate scf_service_destroy(svc); 4037c478bd9Sstevel@tonic-gate scf_scope_destroy(scope); 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate return (result); 4067c478bd9Sstevel@tonic-gate } 4077c478bd9Sstevel@tonic-gate 4081f1b4534Scasper /* 4091f1b4534Scasper * Generate the md5 hash of a file; manifest files are smallish 4101f1b4534Scasper * so we can read them in one gulp. 4111f1b4534Scasper */ 4121f1b4534Scasper static int 4131f1b4534Scasper md5_hash_file(const char *file, off64_t sz, uchar_t *hash) 4141f1b4534Scasper { 4151f1b4534Scasper char *buf; 4161f1b4534Scasper int fd; 4171f1b4534Scasper ssize_t res; 4181f1b4534Scasper int ret; 4191f1b4534Scasper 4201f1b4534Scasper fd = open(file, O_RDONLY); 4211f1b4534Scasper if (fd < 0) 4221f1b4534Scasper return (-1); 4231f1b4534Scasper 4241f1b4534Scasper buf = malloc(sz); 4251f1b4534Scasper if (buf == NULL) { 4261f1b4534Scasper (void) close(fd); 4271f1b4534Scasper return (-1); 4281f1b4534Scasper } 4291f1b4534Scasper 4301f1b4534Scasper res = read(fd, buf, (size_t)sz); 4311f1b4534Scasper 4321f1b4534Scasper (void) close(fd); 4331f1b4534Scasper 4341f1b4534Scasper if (res == sz) { 4351f1b4534Scasper ret = 0; 4361f1b4534Scasper md5_calc(hash, (uchar_t *)buf, (unsigned int) sz); 4371f1b4534Scasper } else { 4381f1b4534Scasper ret = -1; 4391f1b4534Scasper } 4401f1b4534Scasper 4411f1b4534Scasper free(buf); 4421f1b4534Scasper return (ret); 4431f1b4534Scasper } 4441f1b4534Scasper 4457c478bd9Sstevel@tonic-gate /* 4467c478bd9Sstevel@tonic-gate * int mhash_test_file(scf_handle_t *, const char *, uint_t, char **, uchar_t *) 4477c478bd9Sstevel@tonic-gate * Test the given filename against the hashed metadata in the repository. 4487c478bd9Sstevel@tonic-gate * The behaviours for import and apply are slightly different. For imports, 4497c478bd9Sstevel@tonic-gate * if the hash value is absent or different, then the import operation 4507c478bd9Sstevel@tonic-gate * continues. For profile application, the operation continues only if the 4517c478bd9Sstevel@tonic-gate * hash value for the file is absent. 4527c478bd9Sstevel@tonic-gate * 453449975fdScasper * We keep two hashes: one which can be quickly test: the metadata hash, 454449975fdScasper * and one which is more expensive to test: the file contents hash. 455449975fdScasper * 456449975fdScasper * If either hash matches, the file does not need to be re-read. 457449975fdScasper * If only one of the hashes matches, a side effect of this function 458449975fdScasper * is to store the newly computed hash. 459449975fdScasper * If neither hash matches, the hash computed for the new file is returned 460449975fdScasper * and not stored. 461449975fdScasper * 462449975fdScasper * Return values: 463449975fdScasper * MHASH_NEWFILE - the file no longer matches the hash or no hash existed 464449975fdScasper * ONLY in this case we return the new file's hash. 465449975fdScasper * MHASH_FAILURE - an internal error occurred, or the file was not found. 466449975fdScasper * MHASH_RECONCILED- based on the metadata/file hash, the file does 467449975fdScasper * not need to be re-read; if necessary, 468449975fdScasper * the hash was upgraded or reconciled. 469449975fdScasper * 470449975fdScasper * NOTE: no hash is returned UNLESS MHASH_NEWFILE is returned. 4717c478bd9Sstevel@tonic-gate */ 4727c478bd9Sstevel@tonic-gate int 4737c478bd9Sstevel@tonic-gate mhash_test_file(scf_handle_t *hndl, const char *file, uint_t is_profile, 474449975fdScasper char **pnamep, uchar_t *hashbuf) 4757c478bd9Sstevel@tonic-gate { 4767c478bd9Sstevel@tonic-gate boolean_t do_hash; 4777c478bd9Sstevel@tonic-gate struct stat64 st; 4787c478bd9Sstevel@tonic-gate char *cp; 4797c478bd9Sstevel@tonic-gate char *data; 4807c478bd9Sstevel@tonic-gate uchar_t stored_hash[MHASH_SIZE]; 481449975fdScasper uchar_t hash[MHASH_SIZE]; 4827c478bd9Sstevel@tonic-gate char *pname; 4837c478bd9Sstevel@tonic-gate int ret; 484449975fdScasper int hashash; 485449975fdScasper int metahashok = 0; 4867c478bd9Sstevel@tonic-gate 4877c478bd9Sstevel@tonic-gate /* 4887c478bd9Sstevel@tonic-gate * In the case where we are doing automated imports, we reduce the UID, 4897c478bd9Sstevel@tonic-gate * the GID, the size, and the mtime into a string (to eliminate 4907c478bd9Sstevel@tonic-gate * endianness) which we then make opaque as a single MD5 digest. 4917c478bd9Sstevel@tonic-gate * 4927c478bd9Sstevel@tonic-gate * The previous hash was composed of the inode number, the UID, the file 4937c478bd9Sstevel@tonic-gate * size, and the mtime. This formulation was found to be insufficiently 4947c478bd9Sstevel@tonic-gate * portable for use in highly replicated deployments. The current 4957c478bd9Sstevel@tonic-gate * algorithm will allow matches of this "v1" hash, but always returns 4967c478bd9Sstevel@tonic-gate * the effective "v2" hash, such that updates result in the more 4977c478bd9Sstevel@tonic-gate * portable hash being used. 4987c478bd9Sstevel@tonic-gate * 4991f1b4534Scasper * An unwanted side effect of a hash based solely on the file 5001f1b4534Scasper * meta data is the fact that we pay no attention to the contents 5011f1b4534Scasper * which may remain the same despite meta data changes. This happens 5021f1b4534Scasper * with (live) upgrades. We extend the V2 hash with an additional 5031f1b4534Scasper * digest of the file contents and the code retrieving the hash 5041f1b4534Scasper * from the repository zero fills the remainder so we can detect 5051f1b4534Scasper * it is missing. 5061f1b4534Scasper * 5071f1b4534Scasper * If the the V2 digest matches, we check for the presence of 5081f1b4534Scasper * the contents digest and compute and store it if missing. 5091f1b4534Scasper * 5101f1b4534Scasper * If the V2 digest doesn't match but we also have a non-zero 5111f1b4534Scasper * file hash, we match the file content digest. If it matches, 5121f1b4534Scasper * we compute and store the new complete hash so that later 5131f1b4534Scasper * checks will find the meta data digest correct. 5141f1b4534Scasper * 5151f1b4534Scasper * If the above matches fail and the V1 hash doesn't match either, 5161f1b4534Scasper * we consider the test to have failed, implying that some aspect 5171f1b4534Scasper * of the manifest has changed. 5187c478bd9Sstevel@tonic-gate */ 5197c478bd9Sstevel@tonic-gate 5207c478bd9Sstevel@tonic-gate cp = getenv("SVCCFG_CHECKHASH"); 5217c478bd9Sstevel@tonic-gate do_hash = (cp != NULL && *cp != '\0'); 5227c478bd9Sstevel@tonic-gate if (!do_hash) { 523449975fdScasper if (pnamep != NULL) 524449975fdScasper *pnamep = NULL; 525449975fdScasper return (MHASH_NEWFILE); 526449975fdScasper } 527449975fdScasper 528*70cbfe41SPhilippe Jung pname = mhash_filename_to_propname(file, B_FALSE); 529449975fdScasper if (pname == NULL) 530449975fdScasper return (MHASH_FAILURE); 531449975fdScasper 532449975fdScasper hashash = mhash_retrieve_entry(hndl, pname, stored_hash) == 0; 533449975fdScasper 534449975fdScasper /* Never reread a profile. */ 535449975fdScasper if (hashash && is_profile) { 536449975fdScasper uu_free(pname); 537449975fdScasper return (MHASH_RECONCILED); 538449975fdScasper } 539449975fdScasper 540449975fdScasper /* 541449975fdScasper * No hash and not interested in one, then don't bother computing it. 542449975fdScasper * We also skip returning the property name in that case. 543449975fdScasper */ 544449975fdScasper if (!hashash && hashbuf == NULL) { 545449975fdScasper uu_free(pname); 546449975fdScasper return (MHASH_NEWFILE); 5477c478bd9Sstevel@tonic-gate } 5487c478bd9Sstevel@tonic-gate 549*70cbfe41SPhilippe Jung do { 5507c478bd9Sstevel@tonic-gate ret = stat64(file, &st); 551*70cbfe41SPhilippe Jung } while (ret < 0 && errno == EINTR); 5527c478bd9Sstevel@tonic-gate if (ret < 0) { 553449975fdScasper uu_free(pname); 554449975fdScasper return (MHASH_FAILURE); 5557c478bd9Sstevel@tonic-gate } 5567c478bd9Sstevel@tonic-gate 5577c478bd9Sstevel@tonic-gate data = uu_msprintf(MHASH_FORMAT_V2, st.st_uid, st.st_gid, 5587c478bd9Sstevel@tonic-gate st.st_size, st.st_mtime); 5597c478bd9Sstevel@tonic-gate if (data == NULL) { 560449975fdScasper uu_free(pname); 561449975fdScasper return (MHASH_FAILURE); 5627c478bd9Sstevel@tonic-gate } 5637c478bd9Sstevel@tonic-gate 5641f1b4534Scasper (void) memset(hash, 0, MHASH_SIZE); 5657c478bd9Sstevel@tonic-gate md5_calc(hash, (uchar_t *)data, strlen(data)); 5667c478bd9Sstevel@tonic-gate 5677c478bd9Sstevel@tonic-gate uu_free(data); 5687c478bd9Sstevel@tonic-gate 569449975fdScasper /* 570449975fdScasper * Verify the meta data hash. 571449975fdScasper */ 572449975fdScasper if (hashash && memcmp(hash, stored_hash, MD5_DIGEST_LENGTH) == 0) { 573449975fdScasper int i; 5747c478bd9Sstevel@tonic-gate 575449975fdScasper metahashok = 1; 5767c478bd9Sstevel@tonic-gate /* 577449975fdScasper * The metadata hash matches; now we see if there was a 578449975fdScasper * content hash; if not, we will continue on and compute and 579449975fdScasper * store the updated hash. 580449975fdScasper * If there was no content hash, mhash_retrieve_entry() 581449975fdScasper * will have zero filled it. 5827c478bd9Sstevel@tonic-gate */ 583449975fdScasper for (i = 0; i < MD5_DIGEST_LENGTH; i++) { 584449975fdScasper if (stored_hash[MD5_DIGEST_LENGTH+i] != 0) { 585449975fdScasper uu_free(pname); 586449975fdScasper return (MHASH_RECONCILED); 5871f1b4534Scasper } 5881f1b4534Scasper } 589449975fdScasper } 5901f1b4534Scasper 591449975fdScasper /* 592449975fdScasper * Compute the file hash as we can no longer avoid having to know it. 593449975fdScasper * Note: from this point on "hash" contains the full, current, hash. 594449975fdScasper */ 595449975fdScasper if (md5_hash_file(file, st.st_size, &hash[MHASH_SIZE_OLD]) != 0) { 596449975fdScasper uu_free(pname); 597449975fdScasper return (MHASH_FAILURE); 598449975fdScasper } 599449975fdScasper if (hashash) { 600449975fdScasper uchar_t hash_v1[MHASH_SIZE_OLD]; 601449975fdScasper 602449975fdScasper if (metahashok || 6031f1b4534Scasper memcmp(&hash[MHASH_SIZE_OLD], &stored_hash[MHASH_SIZE_OLD], 604449975fdScasper MD5_DIGEST_LENGTH) == 0) { 6051f1b4534Scasper 606449975fdScasper /* 607449975fdScasper * Reconcile entry: we get here when either the 608449975fdScasper * meta data hash matches or the content hash matches; 609449975fdScasper * we then update the database with the complete 610449975fdScasper * new hash so we can be a bit quicker next time. 611449975fdScasper */ 6121f1b4534Scasper (void) mhash_store_entry(hndl, pname, hash, NULL); 6131f1b4534Scasper uu_free(pname); 614449975fdScasper return (MHASH_RECONCILED); 6151f1b4534Scasper } 6167c478bd9Sstevel@tonic-gate 6177c478bd9Sstevel@tonic-gate /* 618449975fdScasper * No match on V2 hash or file content; compare V1 hash. 6197c478bd9Sstevel@tonic-gate */ 6207c478bd9Sstevel@tonic-gate data = uu_msprintf(MHASH_FORMAT_V1, st.st_ino, st.st_uid, 6217c478bd9Sstevel@tonic-gate st.st_size, st.st_mtime); 6221f1b4534Scasper if (data == NULL) { 6231f1b4534Scasper uu_free(pname); 624449975fdScasper return (MHASH_FAILURE); 6251f1b4534Scasper } 6267c478bd9Sstevel@tonic-gate 6277c478bd9Sstevel@tonic-gate md5_calc(hash_v1, (uchar_t *)data, strlen(data)); 6287c478bd9Sstevel@tonic-gate 6297c478bd9Sstevel@tonic-gate uu_free(data); 6307c478bd9Sstevel@tonic-gate 6311f1b4534Scasper if (memcmp(hash_v1, stored_hash, MD5_DIGEST_LENGTH) == 0) { 632449975fdScasper /* 633449975fdScasper * Update the new entry so we don't have to go through 634449975fdScasper * all this trouble next time. 635449975fdScasper */ 636449975fdScasper (void) mhash_store_entry(hndl, pname, hash, NULL); 6371f1b4534Scasper uu_free(pname); 638449975fdScasper return (MHASH_RECONCILED); 6391f1b4534Scasper } 6407c478bd9Sstevel@tonic-gate } 6417c478bd9Sstevel@tonic-gate 642449975fdScasper if (pnamep != NULL) 643449975fdScasper *pnamep = pname; 644449975fdScasper else 645449975fdScasper uu_free(pname); 646449975fdScasper 647449975fdScasper if (hashbuf != NULL) 648449975fdScasper (void) memcpy(hashbuf, hash, MHASH_SIZE); 6497c478bd9Sstevel@tonic-gate 650449975fdScasper return (MHASH_NEWFILE); 6517c478bd9Sstevel@tonic-gate } 652