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*85bcc4e5SSean Wilcox  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/stat.h>
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <assert.h>
307c478bd9Sstevel@tonic-gate #include <ctype.h>
317c478bd9Sstevel@tonic-gate #include <errno.h>
321f1b4534Scasper #include <fcntl.h>
337c478bd9Sstevel@tonic-gate #include <libintl.h>
347c478bd9Sstevel@tonic-gate #include <libscf.h>
357c478bd9Sstevel@tonic-gate #include <libuutil.h>
367c478bd9Sstevel@tonic-gate #include <limits.h>
377c478bd9Sstevel@tonic-gate #include <md5.h>
387c478bd9Sstevel@tonic-gate #include <pthread.h>
397c478bd9Sstevel@tonic-gate #include <stdio.h>
407c478bd9Sstevel@tonic-gate #include <stdlib.h>
417c478bd9Sstevel@tonic-gate #include <string.h>
427c478bd9Sstevel@tonic-gate #include <strings.h>
431f1b4534Scasper #include <unistd.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <manifest_hash.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * Translate a file name to property name.  Return an allocated string or NULL
4970cbfe41SPhilippe Jung  * if realpath() fails. If deathrow is true, realpath() is skipped. This
5070cbfe41SPhilippe Jung  * allows to return the property name even if the file doesn't exist.
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate char *
5370cbfe41SPhilippe Jung mhash_filename_to_propname(const char *in, boolean_t deathrow)
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate 	char *out, *cp, *base;
567c478bd9Sstevel@tonic-gate 	size_t len, piece_len;
57*85bcc4e5SSean Wilcox 	size_t base_sz = 0;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	out = uu_zalloc(PATH_MAX + 1);
6070cbfe41SPhilippe Jung 	if (deathrow) {
6170cbfe41SPhilippe Jung 		/* used only for service deathrow handling */
6270cbfe41SPhilippe Jung 		if (strlcpy(out, in, PATH_MAX + 1) >= (PATH_MAX + 1)) {
6370cbfe41SPhilippe Jung 			uu_free(out);
6470cbfe41SPhilippe Jung 			return (NULL);
6570cbfe41SPhilippe Jung 		}
6670cbfe41SPhilippe Jung 	} else {
6770cbfe41SPhilippe Jung 		if (realpath(in, out) == NULL) {
6870cbfe41SPhilippe Jung 			uu_free(out);
6970cbfe41SPhilippe Jung 			return (NULL);
7070cbfe41SPhilippe 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 
80*85bcc4e5SSean Wilcox 	if (base != NULL && strncmp(out, base, strlen(base)) == 0)
81*85bcc4e5SSean Wilcox 		base_sz = strlen(base);
82*85bcc4e5SSean Wilcox 
83*85bcc4e5SSean Wilcox 	cp = out + base_sz;
847c478bd9Sstevel@tonic-gate 	if (*cp == '/')
857c478bd9Sstevel@tonic-gate 		cp++;
867c478bd9Sstevel@tonic-gate 	(void) memmove(out, cp, strlen(cp) + 1);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	len = strlen(out);
897c478bd9Sstevel@tonic-gate 	if (len > scf_limit(SCF_LIMIT_MAX_NAME_LENGTH)) {
907c478bd9Sstevel@tonic-gate 		/* Use the first half and the second half. */
917c478bd9Sstevel@tonic-gate 		piece_len = (scf_limit(SCF_LIMIT_MAX_NAME_LENGTH) - 3) / 2;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 		(void) strncpy(out + piece_len, "__", 2);
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 		(void) memmove(out + piece_len + 2, out + (len - piece_len),
967c478bd9Sstevel@tonic-gate 		    piece_len + 1);
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	/*
1007c478bd9Sstevel@tonic-gate 	 * Translate non-property characters to '_', first making sure that
1017c478bd9Sstevel@tonic-gate 	 * we don't begin with '_'.
1027c478bd9Sstevel@tonic-gate 	 */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if (!isalpha(*out))
1057c478bd9Sstevel@tonic-gate 		*out = 'A';
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	for (cp = out + 1; *cp != '\0'; ++cp) {
1087c478bd9Sstevel@tonic-gate 		if (!(isalnum(*cp) || *cp == '_' || *cp == '-'))
1097c478bd9Sstevel@tonic-gate 			*cp = '_';
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	return (out);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate int
1169444c26fSTom Whitten mhash_retrieve_entry(scf_handle_t *hndl, const char *name, uchar_t *hash,
1179444c26fSTom Whitten     apply_action_t *action)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	scf_scope_t *scope;
1207c478bd9Sstevel@tonic-gate 	scf_service_t *svc;
1217c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
1227c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
1237c478bd9Sstevel@tonic-gate 	scf_value_t *val;
1249444c26fSTom Whitten 	scf_error_t err;
1257c478bd9Sstevel@tonic-gate 	ssize_t szret;
1267c478bd9Sstevel@tonic-gate 	int result = 0;
1277c478bd9Sstevel@tonic-gate 
1289444c26fSTom Whitten 	if (action)
1299444c26fSTom Whitten 		*action = APPLY_NONE;
1309444c26fSTom Whitten 
1317c478bd9Sstevel@tonic-gate 	/*
1327c478bd9Sstevel@tonic-gate 	 * In this implementation the hash for name is the opaque value of
1337c478bd9Sstevel@tonic-gate 	 * svc:/MHASH_SVC/:properties/name/MHASH_PROP
1347c478bd9Sstevel@tonic-gate 	 */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	if ((scope = scf_scope_create(hndl)) == NULL ||
1377c478bd9Sstevel@tonic-gate 	    (svc = scf_service_create(hndl)) == NULL ||
1387c478bd9Sstevel@tonic-gate 	    (pg = scf_pg_create(hndl)) == NULL ||
1397c478bd9Sstevel@tonic-gate 	    (prop = scf_property_create(hndl)) == NULL ||
1407c478bd9Sstevel@tonic-gate 	    (val = scf_value_create(hndl)) == NULL) {
1417c478bd9Sstevel@tonic-gate 		result = -1;
1427c478bd9Sstevel@tonic-gate 		goto out;
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	if (scf_handle_get_local_scope(hndl, scope) < 0) {
1467c478bd9Sstevel@tonic-gate 		result = -1;
1477c478bd9Sstevel@tonic-gate 		goto out;
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	if (scf_scope_get_service(scope, MHASH_SVC, svc) < 0) {
1517c478bd9Sstevel@tonic-gate 		result = -1;
1527c478bd9Sstevel@tonic-gate 		goto out;
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	if (scf_service_get_pg(svc, name, pg) != SCF_SUCCESS) {
1567c478bd9Sstevel@tonic-gate 		result = -1;
1577c478bd9Sstevel@tonic-gate 		goto out;
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, MHASH_PROP, prop) != SCF_SUCCESS) {
1617c478bd9Sstevel@tonic-gate 		result = -1;
1627c478bd9Sstevel@tonic-gate 		goto out;
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
1667c478bd9Sstevel@tonic-gate 		result = -1;
1677c478bd9Sstevel@tonic-gate 		goto out;
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	szret = scf_value_get_opaque(val, hash, MHASH_SIZE);
1717c478bd9Sstevel@tonic-gate 	if (szret < 0) {
1727c478bd9Sstevel@tonic-gate 		result = -1;
1737c478bd9Sstevel@tonic-gate 		goto out;
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
1761f1b4534Scasper 	/*
1771f1b4534Scasper 	 * Make sure that the old hash is returned with
1781f1b4534Scasper 	 * remainder of the bytes zeroed.
1791f1b4534Scasper 	 */
1801f1b4534Scasper 	if (szret == MHASH_SIZE_OLD) {
1811f1b4534Scasper 		(void) memset(hash + MHASH_SIZE_OLD, 0,
1821f1b4534Scasper 		    MHASH_SIZE - MHASH_SIZE_OLD);
1831f1b4534Scasper 	} else if (szret != MHASH_SIZE) {
1847c478bd9Sstevel@tonic-gate 		scf_value_destroy(val);
1857c478bd9Sstevel@tonic-gate 		result = -1;
1867c478bd9Sstevel@tonic-gate 		goto out;
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1899444c26fSTom Whitten 	/*
1909444c26fSTom Whitten 	 * If caller has requested the apply_last property, read the
1919444c26fSTom Whitten 	 * property if it exists.
1929444c26fSTom Whitten 	 */
1939444c26fSTom Whitten 	if (action != NULL) {
1949444c26fSTom Whitten 		uint8_t apply_value;
1959444c26fSTom Whitten 
1969444c26fSTom Whitten 		if (scf_pg_get_property(pg, MHASH_APPLY_PROP, prop) !=
1979444c26fSTom Whitten 		    SCF_SUCCESS) {
1989444c26fSTom Whitten 			err = scf_error();
1999444c26fSTom Whitten 			if ((err != SCF_ERROR_DELETED) &&
2009444c26fSTom Whitten 			    (err != SCF_ERROR_NOT_FOUND)) {
2019444c26fSTom Whitten 				result = -1;
2029444c26fSTom Whitten 			}
2039444c26fSTom Whitten 			goto out;
2049444c26fSTom Whitten 		}
2059444c26fSTom Whitten 		if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
2069444c26fSTom Whitten 			err = scf_error();
2079444c26fSTom Whitten 			if ((err != SCF_ERROR_DELETED) &&
2089444c26fSTom Whitten 			    (err != SCF_ERROR_NOT_FOUND)) {
2099444c26fSTom Whitten 				result = -1;
2109444c26fSTom Whitten 			}
2119444c26fSTom Whitten 			goto out;
2129444c26fSTom Whitten 		}
2139444c26fSTom Whitten 		if (scf_value_get_boolean(val, &apply_value) != SCF_SUCCESS) {
2149444c26fSTom Whitten 			result = -1;
2159444c26fSTom Whitten 			goto out;
2169444c26fSTom Whitten 		}
2179444c26fSTom Whitten 		if (apply_value)
2189444c26fSTom Whitten 			*action = APPLY_LATE;
2199444c26fSTom Whitten 	}
2209444c26fSTom Whitten 
2217c478bd9Sstevel@tonic-gate out:
2227c478bd9Sstevel@tonic-gate 	(void) scf_value_destroy(val);
2237c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
2247c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
2257c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
2267c478bd9Sstevel@tonic-gate 	scf_scope_destroy(scope);
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	return (result);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate int
23223294c7dSSean Wilcox mhash_store_entry(scf_handle_t *hndl, const char *name, const char *fname,
2339444c26fSTom Whitten     uchar_t *hash, apply_action_t apply_late, char **errstr)
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate 	scf_scope_t *scope = NULL;
2367c478bd9Sstevel@tonic-gate 	scf_service_t *svc = NULL;
2377c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
2387c478bd9Sstevel@tonic-gate 	scf_property_t *prop = NULL;
2399444c26fSTom Whitten 	scf_value_t *aval = NULL;
2407c478bd9Sstevel@tonic-gate 	scf_value_t *val = NULL;
24123294c7dSSean Wilcox 	scf_value_t *fval = NULL;
2427c478bd9Sstevel@tonic-gate 	scf_transaction_t *tx = NULL;
2439444c26fSTom Whitten 	scf_transaction_entry_t *ae = NULL;
2447c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t *e = NULL;
24523294c7dSSean Wilcox 	scf_transaction_entry_t *fe = NULL;
2469444c26fSTom Whitten 	scf_error_t err;
2477c478bd9Sstevel@tonic-gate 	int ret, result = 0;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	int i;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	if ((scope = scf_scope_create(hndl)) == NULL ||
2527c478bd9Sstevel@tonic-gate 	    (svc = scf_service_create(hndl)) == NULL ||
2537c478bd9Sstevel@tonic-gate 	    (pg = scf_pg_create(hndl)) == NULL ||
2547c478bd9Sstevel@tonic-gate 	    (prop = scf_property_create(hndl)) == NULL) {
2557c478bd9Sstevel@tonic-gate 		if (errstr != NULL)
2567c478bd9Sstevel@tonic-gate 			*errstr = gettext("Could not create scf objects");
2577c478bd9Sstevel@tonic-gate 		result = -1;
2587c478bd9Sstevel@tonic-gate 		goto out;
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	if (scf_handle_get_local_scope(hndl, scope) != SCF_SUCCESS) {
2627c478bd9Sstevel@tonic-gate 		if (errstr != NULL)
2637c478bd9Sstevel@tonic-gate 			*errstr = gettext("Could not get local scope");
2647c478bd9Sstevel@tonic-gate 		result = -1;
2657c478bd9Sstevel@tonic-gate 		goto out;
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	for (i = 0; i < 5; ++i) {
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		if (scf_scope_get_service(scope, MHASH_SVC, svc) ==
2717c478bd9Sstevel@tonic-gate 		    SCF_SUCCESS)
2727c478bd9Sstevel@tonic-gate 			break;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND) {
2757c478bd9Sstevel@tonic-gate 			if (errstr != NULL)
2767c478bd9Sstevel@tonic-gate 				*errstr = gettext("Could not get manifest hash "
2777c478bd9Sstevel@tonic-gate 				    "service");
2787c478bd9Sstevel@tonic-gate 			result = -1;
2797c478bd9Sstevel@tonic-gate 			goto out;
2807c478bd9Sstevel@tonic-gate 		}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 		if (scf_scope_add_service(scope, MHASH_SVC, svc) ==
2837c478bd9Sstevel@tonic-gate 		    SCF_SUCCESS)
2847c478bd9Sstevel@tonic-gate 			break;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 		err = scf_error();
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 		if (err == SCF_ERROR_EXISTS)
2897c478bd9Sstevel@tonic-gate 			/* Try again. */
2907c478bd9Sstevel@tonic-gate 			continue;
2917c478bd9Sstevel@tonic-gate 		else if (err == SCF_ERROR_PERMISSION_DENIED) {
2927c478bd9Sstevel@tonic-gate 			if (errstr != NULL)
2937c478bd9Sstevel@tonic-gate 				*errstr = gettext("Could not store file hash: "
2947c478bd9Sstevel@tonic-gate 				    "permission denied.\n");
2957c478bd9Sstevel@tonic-gate 			result = -1;
2967c478bd9Sstevel@tonic-gate 			goto out;
2977c478bd9Sstevel@tonic-gate 		}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 		if (errstr != NULL)
3007c478bd9Sstevel@tonic-gate 			*errstr = gettext("Could not add manifest hash "
3017c478bd9Sstevel@tonic-gate 			    "service");
3027c478bd9Sstevel@tonic-gate 		result = -1;
3037c478bd9Sstevel@tonic-gate 		goto out;
3047c478bd9Sstevel@tonic-gate 	}
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 			    "service addition contention.\n");
3107c478bd9Sstevel@tonic-gate 		result = -1;
3117c478bd9Sstevel@tonic-gate 		goto out;
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	for (i = 0; i < 5; ++i) {
3157c478bd9Sstevel@tonic-gate 		if (scf_service_get_pg(svc, name, pg) == SCF_SUCCESS)
3167c478bd9Sstevel@tonic-gate 			break;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND) {
3197c478bd9Sstevel@tonic-gate 			if (errstr != NULL)
3207c478bd9Sstevel@tonic-gate 				*errstr = gettext("Could not get service's "
3217c478bd9Sstevel@tonic-gate 				    "hash record)");
3227c478bd9Sstevel@tonic-gate 			result = -1;
3237c478bd9Sstevel@tonic-gate 			goto out;
3247c478bd9Sstevel@tonic-gate 		}
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 		if (scf_service_add_pg(svc, name, MHASH_PG_TYPE,
3277c478bd9Sstevel@tonic-gate 		    MHASH_PG_FLAGS, pg) == SCF_SUCCESS)
3287c478bd9Sstevel@tonic-gate 			break;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 		err = scf_error();
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 		if (err == SCF_ERROR_EXISTS)
3337c478bd9Sstevel@tonic-gate 			/* Try again. */
3347c478bd9Sstevel@tonic-gate 			continue;
3357c478bd9Sstevel@tonic-gate 		else if (err == SCF_ERROR_PERMISSION_DENIED) {
3367c478bd9Sstevel@tonic-gate 			if (errstr != NULL)
3377c478bd9Sstevel@tonic-gate 				*errstr = gettext("Could not store file hash: "
3387c478bd9Sstevel@tonic-gate 				    "permission denied.\n");
3397c478bd9Sstevel@tonic-gate 			result = -1;
3407c478bd9Sstevel@tonic-gate 			goto out;
3417c478bd9Sstevel@tonic-gate 		}
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 		if (errstr != NULL)
3447c478bd9Sstevel@tonic-gate 			*errstr = gettext("Could not store file hash");
3457c478bd9Sstevel@tonic-gate 		result = -1;
3467c478bd9Sstevel@tonic-gate 		goto out;
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 	if (i == 5) {
3497c478bd9Sstevel@tonic-gate 		if (errstr != NULL)
3507c478bd9Sstevel@tonic-gate 			*errstr = gettext("Could not store file hash: "
3517c478bd9Sstevel@tonic-gate 			    "property group addition contention.\n");
3527c478bd9Sstevel@tonic-gate 		result = -1;
3537c478bd9Sstevel@tonic-gate 		goto out;
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	if ((e = scf_entry_create(hndl)) == NULL ||
35723294c7dSSean Wilcox 	    (val = scf_value_create(hndl)) == NULL ||
35823294c7dSSean Wilcox 	    (fe = scf_entry_create(hndl)) == NULL ||
3599444c26fSTom Whitten 	    (fval = scf_value_create(hndl)) == NULL ||
3609444c26fSTom Whitten 	    (ae = scf_entry_create(hndl)) == NULL ||
3619444c26fSTom Whitten 	    (aval = scf_value_create(hndl)) == NULL) {
3627c478bd9Sstevel@tonic-gate 		if (errstr != NULL)
3637c478bd9Sstevel@tonic-gate 			*errstr = gettext("Could not store file hash: "
3647c478bd9Sstevel@tonic-gate 			    "permission denied.\n");
3657c478bd9Sstevel@tonic-gate 		result = -1;
3667c478bd9Sstevel@tonic-gate 		goto out;
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	ret = scf_value_set_opaque(val, hash, MHASH_SIZE);
3707c478bd9Sstevel@tonic-gate 	assert(ret == SCF_SUCCESS);
37123294c7dSSean Wilcox 	ret = scf_value_set_astring(fval, fname);
37223294c7dSSean Wilcox 	assert(ret == SCF_SUCCESS);
3739444c26fSTom Whitten 	if (apply_late == APPLY_LATE) {
3749444c26fSTom Whitten 		scf_value_set_boolean(aval, 1);
3759444c26fSTom Whitten 	}
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	tx = scf_transaction_create(hndl);
3787c478bd9Sstevel@tonic-gate 	if (tx == NULL) {
3797c478bd9Sstevel@tonic-gate 		if (errstr != NULL)
3807c478bd9Sstevel@tonic-gate 			*errstr = gettext("Could not create transaction");
3817c478bd9Sstevel@tonic-gate 		result = -1;
3827c478bd9Sstevel@tonic-gate 		goto out;
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	do {
3867c478bd9Sstevel@tonic-gate 		if (scf_pg_update(pg) == -1) {
3877c478bd9Sstevel@tonic-gate 			if (errstr != NULL)
3887c478bd9Sstevel@tonic-gate 				*errstr = gettext("Could not update hash "
3897c478bd9Sstevel@tonic-gate 				    "entry");
3907c478bd9Sstevel@tonic-gate 			result = -1;
3917c478bd9Sstevel@tonic-gate 			goto out;
3927c478bd9Sstevel@tonic-gate 		}
3937c478bd9Sstevel@tonic-gate 		if (scf_transaction_start(tx, pg) != SCF_SUCCESS) {
3947c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_PERMISSION_DENIED) {
3957c478bd9Sstevel@tonic-gate 				if (errstr != NULL)
3967c478bd9Sstevel@tonic-gate 					*errstr = gettext("Could not start "
3977c478bd9Sstevel@tonic-gate 					    "hash transaction.\n");
3987c478bd9Sstevel@tonic-gate 				result = -1;
3997c478bd9Sstevel@tonic-gate 				goto out;
4007c478bd9Sstevel@tonic-gate 			}
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 			if (errstr != NULL)
4037c478bd9Sstevel@tonic-gate 				*errstr = gettext("Could not store file hash: "
4047c478bd9Sstevel@tonic-gate 				    "permission denied.\n");
4057c478bd9Sstevel@tonic-gate 			result = -1;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 			scf_transaction_destroy(tx);
4087c478bd9Sstevel@tonic-gate 			(void) scf_entry_destroy(e);
4097c478bd9Sstevel@tonic-gate 			goto out;
4107c478bd9Sstevel@tonic-gate 		}
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 		if (scf_transaction_property_new(tx, e, MHASH_PROP,
4137c478bd9Sstevel@tonic-gate 		    SCF_TYPE_OPAQUE) != SCF_SUCCESS &&
4147c478bd9Sstevel@tonic-gate 		    scf_transaction_property_change_type(tx, e, MHASH_PROP,
4157c478bd9Sstevel@tonic-gate 		    SCF_TYPE_OPAQUE) != SCF_SUCCESS) {
4167c478bd9Sstevel@tonic-gate 			if (errstr != NULL)
4177c478bd9Sstevel@tonic-gate 				*errstr = gettext("Could not modify hash "
4187c478bd9Sstevel@tonic-gate 				    "entry");
4197c478bd9Sstevel@tonic-gate 			result = -1;
4207c478bd9Sstevel@tonic-gate 			goto out;
4217c478bd9Sstevel@tonic-gate 		}
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 		ret = scf_entry_add_value(e, val);
4247c478bd9Sstevel@tonic-gate 		assert(ret == SCF_SUCCESS);
4257c478bd9Sstevel@tonic-gate 
4269444c26fSTom Whitten 		if (scf_transaction_property_new(tx, fe, MHASH_FILE_PROP,
42723294c7dSSean Wilcox 		    SCF_TYPE_ASTRING) != SCF_SUCCESS &&
4289444c26fSTom Whitten 		    scf_transaction_property_change_type(tx, fe,
4299444c26fSTom Whitten 		    MHASH_FILE_PROP, SCF_TYPE_ASTRING) != SCF_SUCCESS) {
43023294c7dSSean Wilcox 			if (errstr != NULL)
43123294c7dSSean Wilcox 				*errstr = gettext("Could not modify file "
43223294c7dSSean Wilcox 				    "entry");
43323294c7dSSean Wilcox 			result = -1;
43423294c7dSSean Wilcox 			goto out;
43523294c7dSSean Wilcox 		}
43623294c7dSSean Wilcox 
43723294c7dSSean Wilcox 		ret = scf_entry_add_value(fe, fval);
43823294c7dSSean Wilcox 		assert(ret == SCF_SUCCESS);
43923294c7dSSean Wilcox 
4409444c26fSTom Whitten 		switch (apply_late) {
4419444c26fSTom Whitten 		case APPLY_NONE:
4429444c26fSTom Whitten 			if (scf_transaction_property_delete(tx, ae,
4439444c26fSTom Whitten 			    MHASH_APPLY_PROP) != 0) {
4449444c26fSTom Whitten 				err = scf_error();
4459444c26fSTom Whitten 				if ((err != SCF_ERROR_DELETED) &&
4469444c26fSTom Whitten 				    (err != SCF_ERROR_NOT_FOUND)) {
4479444c26fSTom Whitten 					if (errstr != NULL) {
4489444c26fSTom Whitten 						*errstr = gettext("Could not "
4499444c26fSTom Whitten 						    "delete apply_late "
4509444c26fSTom Whitten 						    "property");
4519444c26fSTom Whitten 					}
4529444c26fSTom Whitten 					result = -1;
4539444c26fSTom Whitten 					goto out;
4549444c26fSTom Whitten 				}
4559444c26fSTom Whitten 			}
4569444c26fSTom Whitten 			break;
4579444c26fSTom Whitten 		case APPLY_LATE:
4589444c26fSTom Whitten 			if ((scf_transaction_property_new(tx, ae,
4599444c26fSTom Whitten 			    MHASH_APPLY_PROP,
4609444c26fSTom Whitten 			    SCF_TYPE_BOOLEAN) != SCF_SUCCESS) &&
4619444c26fSTom Whitten 			    (scf_transaction_property_change_type(tx, ae,
4629444c26fSTom Whitten 			    MHASH_APPLY_PROP, SCF_TYPE_BOOLEAN) !=
4639444c26fSTom Whitten 			    SCF_SUCCESS)) {
4649444c26fSTom Whitten 				if (errstr != NULL) {
4659444c26fSTom Whitten 					*errstr = gettext("Could not modify "
4669444c26fSTom Whitten 					    "apply_late property");
4679444c26fSTom Whitten 				}
4689444c26fSTom Whitten 				result = -1;
4699444c26fSTom Whitten 				goto out;
4709444c26fSTom Whitten 			}
4719444c26fSTom Whitten 
4729444c26fSTom Whitten 			ret = scf_entry_add_value(ae, aval);
4739444c26fSTom Whitten 			assert(ret == SCF_SUCCESS);
4749444c26fSTom Whitten 			break;
4759444c26fSTom Whitten 		default:
4769444c26fSTom Whitten 			abort();
4779444c26fSTom Whitten 		};
4789444c26fSTom Whitten 
4797c478bd9Sstevel@tonic-gate 		ret = scf_transaction_commit(tx);
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 		if (ret == 0)
4827c478bd9Sstevel@tonic-gate 			scf_transaction_reset(tx);
4837c478bd9Sstevel@tonic-gate 	} while (ret == 0);
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	if (ret < 0) {
4867c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_PERMISSION_DENIED) {
4877c478bd9Sstevel@tonic-gate 			if (errstr != NULL)
4887c478bd9Sstevel@tonic-gate 				*errstr = gettext("Could not store file hash: "
4897c478bd9Sstevel@tonic-gate 				    "permission denied.\n");
4907c478bd9Sstevel@tonic-gate 			result = -1;
4917c478bd9Sstevel@tonic-gate 			goto out;
4927c478bd9Sstevel@tonic-gate 		}
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 		if (errstr != NULL)
4957c478bd9Sstevel@tonic-gate 			*errstr = gettext("Could not commit transaction");
4967c478bd9Sstevel@tonic-gate 		result = -1;
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	scf_transaction_destroy(tx);
5007c478bd9Sstevel@tonic-gate 	(void) scf_entry_destroy(e);
50123294c7dSSean Wilcox 	(void) scf_entry_destroy(fe);
5029444c26fSTom Whitten 	(void) scf_entry_destroy(ae);
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate out:
5057c478bd9Sstevel@tonic-gate 	(void) scf_value_destroy(val);
50623294c7dSSean Wilcox 	(void) scf_value_destroy(fval);
5079444c26fSTom Whitten 	(void) scf_value_destroy(aval);
5087c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
5097c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
5107c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
5117c478bd9Sstevel@tonic-gate 	scf_scope_destroy(scope);
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	return (result);
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate 
5161f1b4534Scasper /*
5171f1b4534Scasper  * Generate the md5 hash of a file; manifest files are smallish
5181f1b4534Scasper  * so we can read them in one gulp.
5191f1b4534Scasper  */
5201f1b4534Scasper static int
5211f1b4534Scasper md5_hash_file(const char *file, off64_t sz, uchar_t *hash)
5221f1b4534Scasper {
5231f1b4534Scasper 	char *buf;
5241f1b4534Scasper 	int fd;
5251f1b4534Scasper 	ssize_t res;
5261f1b4534Scasper 	int ret;
5271f1b4534Scasper 
5281f1b4534Scasper 	fd = open(file, O_RDONLY);
5291f1b4534Scasper 	if (fd < 0)
5301f1b4534Scasper 		return (-1);
5311f1b4534Scasper 
5321f1b4534Scasper 	buf = malloc(sz);
5331f1b4534Scasper 	if (buf == NULL) {
5341f1b4534Scasper 		(void) close(fd);
5351f1b4534Scasper 		return (-1);
5361f1b4534Scasper 	}
5371f1b4534Scasper 
5381f1b4534Scasper 	res = read(fd, buf, (size_t)sz);
5391f1b4534Scasper 
5401f1b4534Scasper 	(void) close(fd);
5411f1b4534Scasper 
5421f1b4534Scasper 	if (res == sz) {
5431f1b4534Scasper 		ret = 0;
5441f1b4534Scasper 		md5_calc(hash, (uchar_t *)buf, (unsigned int) sz);
5451f1b4534Scasper 	} else {
5461f1b4534Scasper 		ret = -1;
5471f1b4534Scasper 	}
5481f1b4534Scasper 
5491f1b4534Scasper 	free(buf);
5501f1b4534Scasper 	return (ret);
5511f1b4534Scasper }
5521f1b4534Scasper 
5537c478bd9Sstevel@tonic-gate /*
5547c478bd9Sstevel@tonic-gate  * int mhash_test_file(scf_handle_t *, const char *, uint_t, char **, uchar_t *)
5557c478bd9Sstevel@tonic-gate  *   Test the given filename against the hashed metadata in the repository.
5567c478bd9Sstevel@tonic-gate  *   The behaviours for import and apply are slightly different.  For imports,
5577c478bd9Sstevel@tonic-gate  *   if the hash value is absent or different, then the import operation
5587c478bd9Sstevel@tonic-gate  *   continues.  For profile application, the operation continues only if the
5597c478bd9Sstevel@tonic-gate  *   hash value for the file is absent.
5607c478bd9Sstevel@tonic-gate  *
561449975fdScasper  *   We keep two hashes: one which can be quickly test: the metadata hash,
562449975fdScasper  *   and one which is more expensive to test: the file contents hash.
563449975fdScasper  *
564449975fdScasper  *   If either hash matches, the file does not need to be re-read.
565449975fdScasper  *   If only one of the hashes matches, a side effect of this function
566449975fdScasper  *   is to store the newly computed hash.
567449975fdScasper  *   If neither hash matches, the hash computed for the new file is returned
568449975fdScasper  *   and not stored.
569449975fdScasper  *
570449975fdScasper  *   Return values:
571449975fdScasper  *	MHASH_NEWFILE	- the file no longer matches the hash or no hash existed
572449975fdScasper  *			  ONLY in this case we return the new file's hash.
573449975fdScasper  *	MHASH_FAILURE	- an internal error occurred, or the file was not found.
574449975fdScasper  *	MHASH_RECONCILED- based on the metadata/file hash, the file does
575449975fdScasper  *			  not need to be re-read; if necessary,
576449975fdScasper  *			  the hash was upgraded or reconciled.
577449975fdScasper  *
578449975fdScasper  * NOTE: no hash is returned UNLESS MHASH_NEWFILE is returned.
5797c478bd9Sstevel@tonic-gate  */
5807c478bd9Sstevel@tonic-gate int
5817c478bd9Sstevel@tonic-gate mhash_test_file(scf_handle_t *hndl, const char *file, uint_t is_profile,
582449975fdScasper     char **pnamep, uchar_t *hashbuf)
5837c478bd9Sstevel@tonic-gate {
5849444c26fSTom Whitten 	apply_action_t action;
5857c478bd9Sstevel@tonic-gate 	boolean_t do_hash;
5867c478bd9Sstevel@tonic-gate 	struct stat64 st;
5877c478bd9Sstevel@tonic-gate 	char *cp;
5887c478bd9Sstevel@tonic-gate 	char *data;
5897c478bd9Sstevel@tonic-gate 	uchar_t stored_hash[MHASH_SIZE];
590449975fdScasper 	uchar_t hash[MHASH_SIZE];
5917c478bd9Sstevel@tonic-gate 	char *pname;
5927c478bd9Sstevel@tonic-gate 	int ret;
593449975fdScasper 	int hashash;
594449975fdScasper 	int metahashok = 0;
5957c478bd9Sstevel@tonic-gate 
5969444c26fSTom Whitten 	if (pnamep)
5979444c26fSTom Whitten 		*pnamep = NULL;
5989444c26fSTom Whitten 
5997c478bd9Sstevel@tonic-gate 	/*
6007c478bd9Sstevel@tonic-gate 	 * In the case where we are doing automated imports, we reduce the UID,
6017c478bd9Sstevel@tonic-gate 	 * the GID, the size, and the mtime into a string (to eliminate
6027c478bd9Sstevel@tonic-gate 	 * endianness) which we then make opaque as a single MD5 digest.
6037c478bd9Sstevel@tonic-gate 	 *
6047c478bd9Sstevel@tonic-gate 	 * The previous hash was composed of the inode number, the UID, the file
6057c478bd9Sstevel@tonic-gate 	 * size, and the mtime.  This formulation was found to be insufficiently
6067c478bd9Sstevel@tonic-gate 	 * portable for use in highly replicated deployments.  The current
6077c478bd9Sstevel@tonic-gate 	 * algorithm will allow matches of this "v1" hash, but always returns
6087c478bd9Sstevel@tonic-gate 	 * the effective "v2" hash, such that updates result in the more
6097c478bd9Sstevel@tonic-gate 	 * portable hash being used.
6107c478bd9Sstevel@tonic-gate 	 *
6111f1b4534Scasper 	 * An unwanted side effect of a hash based solely on the file
6121f1b4534Scasper 	 * meta data is the fact that we pay no attention to the contents
6131f1b4534Scasper 	 * which may remain the same despite meta data changes.  This happens
6141f1b4534Scasper 	 * with (live) upgrades.  We extend the V2 hash with an additional
6151f1b4534Scasper 	 * digest of the file contents and the code retrieving the hash
6161f1b4534Scasper 	 * from the repository zero fills the remainder so we can detect
6171f1b4534Scasper 	 * it is missing.
6181f1b4534Scasper 	 *
6191f1b4534Scasper 	 * If the the V2 digest matches, we check for the presence of
6201f1b4534Scasper 	 * the contents digest and compute and store it if missing.
6211f1b4534Scasper 	 *
6221f1b4534Scasper 	 * If the V2 digest doesn't match but we also have a non-zero
6231f1b4534Scasper 	 * file hash, we match the file content digest.  If it matches,
6241f1b4534Scasper 	 * we compute and store the new complete hash so that later
6251f1b4534Scasper 	 * checks will find the meta data digest correct.
6261f1b4534Scasper 	 *
6271f1b4534Scasper 	 * If the above matches fail and the V1 hash doesn't match either,
6281f1b4534Scasper 	 * we consider the test to have failed, implying that some aspect
6291f1b4534Scasper 	 * of the manifest has changed.
6307c478bd9Sstevel@tonic-gate 	 */
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	cp = getenv("SVCCFG_CHECKHASH");
6337c478bd9Sstevel@tonic-gate 	do_hash = (cp != NULL && *cp != '\0');
6347c478bd9Sstevel@tonic-gate 	if (!do_hash) {
635449975fdScasper 		return (MHASH_NEWFILE);
636449975fdScasper 	}
637449975fdScasper 
63870cbfe41SPhilippe Jung 	pname = mhash_filename_to_propname(file, B_FALSE);
639449975fdScasper 	if (pname == NULL)
640449975fdScasper 		return (MHASH_FAILURE);
641449975fdScasper 
6429444c26fSTom Whitten 	hashash = mhash_retrieve_entry(hndl, pname, stored_hash, &action) == 0;
6439444c26fSTom Whitten 	if (is_profile == 0) {
6449444c26fSTom Whitten 		/* Actions other than APPLY_NONE are restricted to profiles. */
6459444c26fSTom Whitten 		assert(action == APPLY_NONE);
6469444c26fSTom Whitten 	}
647449975fdScasper 
6489444c26fSTom Whitten 	/*
6499444c26fSTom Whitten 	 * As a general rule, we do not reread a profile.  The exception to
6509444c26fSTom Whitten 	 * this rule is when we are running as part of the manifest import
6519444c26fSTom Whitten 	 * service and the apply_late property is set to true.
6529444c26fSTom Whitten 	 */
653449975fdScasper 	if (hashash && is_profile) {
6549444c26fSTom Whitten 		cp = getenv("SMF_FMRI");
6559444c26fSTom Whitten 		if ((cp == NULL) ||
6569444c26fSTom Whitten 		    (strcmp(cp, SCF_INSTANCE_MI) != 0) ||
6579444c26fSTom Whitten 		    (action != APPLY_LATE)) {
6589444c26fSTom Whitten 			uu_free(pname);
6599444c26fSTom Whitten 			return (MHASH_RECONCILED);
6609444c26fSTom Whitten 		}
661449975fdScasper 	}
662449975fdScasper 
663449975fdScasper 	/*
664449975fdScasper 	 * No hash and not interested in one, then don't bother computing it.
665449975fdScasper 	 * We also skip returning the property name in that case.
666449975fdScasper 	 */
667449975fdScasper 	if (!hashash && hashbuf == NULL) {
668449975fdScasper 		uu_free(pname);
669449975fdScasper 		return (MHASH_NEWFILE);
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate 
67270cbfe41SPhilippe Jung 	do {
6737c478bd9Sstevel@tonic-gate 		ret = stat64(file, &st);
67470cbfe41SPhilippe Jung 	} while (ret < 0 && errno == EINTR);
6757c478bd9Sstevel@tonic-gate 	if (ret < 0) {
676449975fdScasper 		uu_free(pname);
677449975fdScasper 		return (MHASH_FAILURE);
6787c478bd9Sstevel@tonic-gate 	}
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	data = uu_msprintf(MHASH_FORMAT_V2, st.st_uid, st.st_gid,
6817c478bd9Sstevel@tonic-gate 	    st.st_size, st.st_mtime);
6827c478bd9Sstevel@tonic-gate 	if (data == NULL) {
683449975fdScasper 		uu_free(pname);
684449975fdScasper 		return (MHASH_FAILURE);
6857c478bd9Sstevel@tonic-gate 	}
6867c478bd9Sstevel@tonic-gate 
6871f1b4534Scasper 	(void) memset(hash, 0, MHASH_SIZE);
6887c478bd9Sstevel@tonic-gate 	md5_calc(hash, (uchar_t *)data, strlen(data));
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	uu_free(data);
6917c478bd9Sstevel@tonic-gate 
692449975fdScasper 	/*
693449975fdScasper 	 * Verify the meta data hash.
694449975fdScasper 	 */
695449975fdScasper 	if (hashash && memcmp(hash, stored_hash, MD5_DIGEST_LENGTH) == 0) {
696449975fdScasper 		int i;
6977c478bd9Sstevel@tonic-gate 
698449975fdScasper 		metahashok = 1;
6997c478bd9Sstevel@tonic-gate 		/*
700449975fdScasper 		 * The metadata hash matches; now we see if there was a
701449975fdScasper 		 * content hash; if not, we will continue on and compute and
702449975fdScasper 		 * store the updated hash.
703449975fdScasper 		 * If there was no content hash, mhash_retrieve_entry()
704449975fdScasper 		 * will have zero filled it.
7057c478bd9Sstevel@tonic-gate 		 */
706449975fdScasper 		for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
707449975fdScasper 			if (stored_hash[MD5_DIGEST_LENGTH+i] != 0) {
7089444c26fSTom Whitten 				if (action == APPLY_LATE) {
7099444c26fSTom Whitten 					if (pnamep != NULL)
7109444c26fSTom Whitten 						*pnamep = pname;
7119444c26fSTom Whitten 					ret = MHASH_NEWFILE;
7129444c26fSTom Whitten 				} else {
7139444c26fSTom Whitten 					uu_free(pname);
7149444c26fSTom Whitten 					ret = MHASH_RECONCILED;
7159444c26fSTom Whitten 				}
7169444c26fSTom Whitten 				return (ret);
7171f1b4534Scasper 			}
7181f1b4534Scasper 		}
719449975fdScasper 	}
7201f1b4534Scasper 
721449975fdScasper 	/*
722449975fdScasper 	 * Compute the file hash as we can no longer avoid having to know it.
723449975fdScasper 	 * Note: from this point on "hash" contains the full, current, hash.
724449975fdScasper 	 */
725449975fdScasper 	if (md5_hash_file(file, st.st_size, &hash[MHASH_SIZE_OLD]) != 0) {
726449975fdScasper 		uu_free(pname);
727449975fdScasper 		return (MHASH_FAILURE);
728449975fdScasper 	}
729449975fdScasper 	if (hashash) {
730449975fdScasper 		uchar_t hash_v1[MHASH_SIZE_OLD];
731449975fdScasper 
732449975fdScasper 		if (metahashok ||
7331f1b4534Scasper 		    memcmp(&hash[MHASH_SIZE_OLD], &stored_hash[MHASH_SIZE_OLD],
734449975fdScasper 		    MD5_DIGEST_LENGTH) == 0) {
7351f1b4534Scasper 
736449975fdScasper 			/*
737449975fdScasper 			 * Reconcile entry: we get here when either the
738449975fdScasper 			 * meta data hash matches or the content hash matches;
739449975fdScasper 			 * we then update the database with the complete
740449975fdScasper 			 * new hash so we can be a bit quicker next time.
741449975fdScasper 			 */
7429444c26fSTom Whitten 			(void) mhash_store_entry(hndl, pname, file, hash,
7439444c26fSTom Whitten 			    APPLY_NONE, NULL);
7449444c26fSTom Whitten 			if (action == APPLY_LATE) {
7459444c26fSTom Whitten 				if (pnamep != NULL)
7469444c26fSTom Whitten 					*pnamep = pname;
7479444c26fSTom Whitten 				ret = MHASH_NEWFILE;
7489444c26fSTom Whitten 			} else {
7499444c26fSTom Whitten 				uu_free(pname);
7509444c26fSTom Whitten 				ret = MHASH_RECONCILED;
7519444c26fSTom Whitten 			}
7529444c26fSTom Whitten 			return (ret);
7531f1b4534Scasper 		}
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 		/*
756449975fdScasper 		 * No match on V2 hash or file content; compare V1 hash.
7577c478bd9Sstevel@tonic-gate 		 */
7587c478bd9Sstevel@tonic-gate 		data = uu_msprintf(MHASH_FORMAT_V1, st.st_ino, st.st_uid,
7597c478bd9Sstevel@tonic-gate 		    st.st_size, st.st_mtime);
7601f1b4534Scasper 		if (data == NULL) {
7611f1b4534Scasper 			uu_free(pname);
762449975fdScasper 			return (MHASH_FAILURE);
7631f1b4534Scasper 		}
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 		md5_calc(hash_v1, (uchar_t *)data, strlen(data));
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 		uu_free(data);
7687c478bd9Sstevel@tonic-gate 
7691f1b4534Scasper 		if (memcmp(hash_v1, stored_hash, MD5_DIGEST_LENGTH) == 0) {
770449975fdScasper 			/*
771449975fdScasper 			 * Update the new entry so we don't have to go through
772449975fdScasper 			 * all this trouble next time.
773449975fdScasper 			 */
7749444c26fSTom Whitten 			(void) mhash_store_entry(hndl, pname, file, hash,
7759444c26fSTom Whitten 			    APPLY_NONE, NULL);
7761f1b4534Scasper 			uu_free(pname);
777449975fdScasper 			return (MHASH_RECONCILED);
7781f1b4534Scasper 		}
7797c478bd9Sstevel@tonic-gate 	}
7807c478bd9Sstevel@tonic-gate 
781449975fdScasper 	if (pnamep != NULL)
782449975fdScasper 		*pnamep = pname;
783449975fdScasper 	else
784449975fdScasper 		uu_free(pname);
785449975fdScasper 
786449975fdScasper 	if (hashbuf != NULL)
787449975fdScasper 		(void) memcpy(hashbuf, hash, MHASH_SIZE);
7887c478bd9Sstevel@tonic-gate 
789449975fdScasper 	return (MHASH_NEWFILE);
7907c478bd9Sstevel@tonic-gate }
791