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 /*
2285bcc4e5SSean Wilcox  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
23*b1e7e97dSAndy Fiddaman  * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
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
5070cbfe41SPhilippe Jung  * if realpath() fails. If deathrow is true, realpath() is skipped. This
5170cbfe41SPhilippe Jung  * allows to return the property name even if the file doesn't exist.
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate char *
mhash_filename_to_propname(const char * in,boolean_t deathrow)5470cbfe41SPhilippe 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;
5885bcc4e5SSean Wilcox 	size_t base_sz = 0;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	out = uu_zalloc(PATH_MAX + 1);
6170cbfe41SPhilippe Jung 	if (deathrow) {
6270cbfe41SPhilippe Jung 		/* used only for service deathrow handling */
6370cbfe41SPhilippe Jung 		if (strlcpy(out, in, PATH_MAX + 1) >= (PATH_MAX + 1)) {
6470cbfe41SPhilippe Jung 			uu_free(out);
6570cbfe41SPhilippe Jung 			return (NULL);
6670cbfe41SPhilippe Jung 		}
6770cbfe41SPhilippe Jung 	} else {
6870cbfe41SPhilippe Jung 		if (realpath(in, out) == NULL) {
6970cbfe41SPhilippe Jung 			uu_free(out);
7070cbfe41SPhilippe Jung 			return (NULL);
7170cbfe41SPhilippe Jung 		}
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	base = getenv("PKG_INSTALL_ROOT");
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	/*
777c478bd9Sstevel@tonic-gate 	 * We copy-shift over the basedir and the leading slash, since it's
787c478bd9Sstevel@tonic-gate 	 * not relevant to when we boot with this repository.
797c478bd9Sstevel@tonic-gate 	 */
807c478bd9Sstevel@tonic-gate 
8185bcc4e5SSean Wilcox 	if (base != NULL && strncmp(out, base, strlen(base)) == 0)
8285bcc4e5SSean Wilcox 		base_sz = strlen(base);
8385bcc4e5SSean Wilcox 
8485bcc4e5SSean Wilcox 	cp = out + base_sz;
857c478bd9Sstevel@tonic-gate 	if (*cp == '/')
867c478bd9Sstevel@tonic-gate 		cp++;
877c478bd9Sstevel@tonic-gate 	(void) memmove(out, cp, strlen(cp) + 1);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	len = strlen(out);
907c478bd9Sstevel@tonic-gate 	if (len > scf_limit(SCF_LIMIT_MAX_NAME_LENGTH)) {
917c478bd9Sstevel@tonic-gate 		/* Use the first half and the second half. */
927c478bd9Sstevel@tonic-gate 		piece_len = (scf_limit(SCF_LIMIT_MAX_NAME_LENGTH) - 3) / 2;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 		(void) strncpy(out + piece_len, "__", 2);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 		(void) memmove(out + piece_len + 2, out + (len - piece_len),
977c478bd9Sstevel@tonic-gate 		    piece_len + 1);
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	/*
1017c478bd9Sstevel@tonic-gate 	 * Translate non-property characters to '_', first making sure that
1027c478bd9Sstevel@tonic-gate 	 * we don't begin with '_'.
1037c478bd9Sstevel@tonic-gate 	 */
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	if (!isalpha(*out))
1067c478bd9Sstevel@tonic-gate 		*out = 'A';
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	for (cp = out + 1; *cp != '\0'; ++cp) {
1097c478bd9Sstevel@tonic-gate 		if (!(isalnum(*cp) || *cp == '_' || *cp == '-'))
1107c478bd9Sstevel@tonic-gate 			*cp = '_';
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	return (out);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate int
mhash_retrieve_entry(scf_handle_t * hndl,const char * name,uchar_t * hash,apply_action_t * action)1179444c26fSTom Whitten mhash_retrieve_entry(scf_handle_t *hndl, const char *name, uchar_t *hash,
1189444c26fSTom Whitten     apply_action_t *action)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	scf_scope_t *scope;
1217c478bd9Sstevel@tonic-gate 	scf_service_t *svc;
1227c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
1237c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
1247c478bd9Sstevel@tonic-gate 	scf_value_t *val;
1259444c26fSTom Whitten 	scf_error_t err;
1267c478bd9Sstevel@tonic-gate 	ssize_t szret;
1277c478bd9Sstevel@tonic-gate 	int result = 0;
1287c478bd9Sstevel@tonic-gate 
1299444c26fSTom Whitten 	if (action)
1309444c26fSTom Whitten 		*action = APPLY_NONE;
1319444c26fSTom Whitten 
1327c478bd9Sstevel@tonic-gate 	/*
1337c478bd9Sstevel@tonic-gate 	 * In this implementation the hash for name is the opaque value of
1347c478bd9Sstevel@tonic-gate 	 * svc:/MHASH_SVC/:properties/name/MHASH_PROP
1357c478bd9Sstevel@tonic-gate 	 */
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	if ((scope = scf_scope_create(hndl)) == NULL ||
1387c478bd9Sstevel@tonic-gate 	    (svc = scf_service_create(hndl)) == NULL ||
1397c478bd9Sstevel@tonic-gate 	    (pg = scf_pg_create(hndl)) == NULL ||
1407c478bd9Sstevel@tonic-gate 	    (prop = scf_property_create(hndl)) == NULL ||
1417c478bd9Sstevel@tonic-gate 	    (val = scf_value_create(hndl)) == NULL) {
1427c478bd9Sstevel@tonic-gate 		result = -1;
1437c478bd9Sstevel@tonic-gate 		goto out;
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	if (scf_handle_get_local_scope(hndl, scope) < 0) {
1477c478bd9Sstevel@tonic-gate 		result = -1;
1487c478bd9Sstevel@tonic-gate 		goto out;
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if (scf_scope_get_service(scope, MHASH_SVC, svc) < 0) {
1527c478bd9Sstevel@tonic-gate 		result = -1;
1537c478bd9Sstevel@tonic-gate 		goto out;
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	if (scf_service_get_pg(svc, name, pg) != SCF_SUCCESS) {
1577c478bd9Sstevel@tonic-gate 		result = -1;
1587c478bd9Sstevel@tonic-gate 		goto out;
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, MHASH_PROP, prop) != SCF_SUCCESS) {
1627c478bd9Sstevel@tonic-gate 		result = -1;
1637c478bd9Sstevel@tonic-gate 		goto out;
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
1677c478bd9Sstevel@tonic-gate 		result = -1;
1687c478bd9Sstevel@tonic-gate 		goto out;
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	szret = scf_value_get_opaque(val, hash, MHASH_SIZE);
1727c478bd9Sstevel@tonic-gate 	if (szret < 0) {
1737c478bd9Sstevel@tonic-gate 		result = -1;
1747c478bd9Sstevel@tonic-gate 		goto out;
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 
1771f1b4534Scasper 	/*
1781f1b4534Scasper 	 * Make sure that the old hash is returned with
1791f1b4534Scasper 	 * remainder of the bytes zeroed.
1801f1b4534Scasper 	 */
1811f1b4534Scasper 	if (szret == MHASH_SIZE_OLD) {
1821f1b4534Scasper 		(void) memset(hash + MHASH_SIZE_OLD, 0,
1831f1b4534Scasper 		    MHASH_SIZE - MHASH_SIZE_OLD);
1841f1b4534Scasper 	} else if (szret != MHASH_SIZE) {
1857c478bd9Sstevel@tonic-gate 		scf_value_destroy(val);
1867c478bd9Sstevel@tonic-gate 		result = -1;
1877c478bd9Sstevel@tonic-gate 		goto out;
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 
1909444c26fSTom Whitten 	/*
1919444c26fSTom Whitten 	 * If caller has requested the apply_last property, read the
1929444c26fSTom Whitten 	 * property if it exists.
1939444c26fSTom Whitten 	 */
1949444c26fSTom Whitten 	if (action != NULL) {
1959444c26fSTom Whitten 		uint8_t apply_value;
1969444c26fSTom Whitten 
1979444c26fSTom Whitten 		if (scf_pg_get_property(pg, MHASH_APPLY_PROP, prop) !=
1989444c26fSTom Whitten 		    SCF_SUCCESS) {
1999444c26fSTom Whitten 			err = scf_error();
2009444c26fSTom Whitten 			if ((err != SCF_ERROR_DELETED) &&
2019444c26fSTom Whitten 			    (err != SCF_ERROR_NOT_FOUND)) {
2029444c26fSTom Whitten 				result = -1;
2039444c26fSTom Whitten 			}
2049444c26fSTom Whitten 			goto out;
2059444c26fSTom Whitten 		}
2069444c26fSTom Whitten 		if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
2079444c26fSTom Whitten 			err = scf_error();
2089444c26fSTom Whitten 			if ((err != SCF_ERROR_DELETED) &&
2099444c26fSTom Whitten 			    (err != SCF_ERROR_NOT_FOUND)) {
2109444c26fSTom Whitten 				result = -1;
2119444c26fSTom Whitten 			}
2129444c26fSTom Whitten 			goto out;
2139444c26fSTom Whitten 		}
2149444c26fSTom Whitten 		if (scf_value_get_boolean(val, &apply_value) != SCF_SUCCESS) {
2159444c26fSTom Whitten 			result = -1;
2169444c26fSTom Whitten 			goto out;
2179444c26fSTom Whitten 		}
2189444c26fSTom Whitten 		if (apply_value)
2199444c26fSTom Whitten 			*action = APPLY_LATE;
2209444c26fSTom Whitten 	}
2219444c26fSTom Whitten 
2227c478bd9Sstevel@tonic-gate out:
2237c478bd9Sstevel@tonic-gate 	(void) scf_value_destroy(val);
2247c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
2257c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
2267c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
2277c478bd9Sstevel@tonic-gate 	scf_scope_destroy(scope);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	return (result);
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate int
mhash_store_entry(scf_handle_t * hndl,const char * name,const char * fname,uchar_t * hash,apply_action_t apply_late,char ** errstr)23323294c7dSSean Wilcox mhash_store_entry(scf_handle_t *hndl, const char *name, const char *fname,
2349444c26fSTom Whitten     uchar_t *hash, apply_action_t apply_late, char **errstr)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	scf_scope_t *scope = NULL;
2377c478bd9Sstevel@tonic-gate 	scf_service_t *svc = NULL;
2387c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
2397c478bd9Sstevel@tonic-gate 	scf_property_t *prop = NULL;
2409444c26fSTom Whitten 	scf_value_t *aval = NULL;
2417c478bd9Sstevel@tonic-gate 	scf_value_t *val = NULL;
24223294c7dSSean Wilcox 	scf_value_t *fval = NULL;
2437c478bd9Sstevel@tonic-gate 	scf_transaction_t *tx = NULL;
2449444c26fSTom Whitten 	scf_transaction_entry_t *ae = NULL;
2457c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t *e = NULL;
24623294c7dSSean Wilcox 	scf_transaction_entry_t *fe = NULL;
2479444c26fSTom Whitten 	scf_error_t err;
2487c478bd9Sstevel@tonic-gate 	int ret, result = 0;
249*b1e7e97dSAndy Fiddaman 	char *base;
250*b1e7e97dSAndy Fiddaman 	size_t base_sz = 0;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	int i;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	if ((scope = scf_scope_create(hndl)) == NULL ||
2557c478bd9Sstevel@tonic-gate 	    (svc = scf_service_create(hndl)) == NULL ||
2567c478bd9Sstevel@tonic-gate 	    (pg = scf_pg_create(hndl)) == NULL ||
2577c478bd9Sstevel@tonic-gate 	    (prop = scf_property_create(hndl)) == NULL) {
2587c478bd9Sstevel@tonic-gate 		if (errstr != NULL)
2597c478bd9Sstevel@tonic-gate 			*errstr = gettext("Could not create scf objects");
2607c478bd9Sstevel@tonic-gate 		result = -1;
2617c478bd9Sstevel@tonic-gate 		goto out;
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	if (scf_handle_get_local_scope(hndl, scope) != SCF_SUCCESS) {
2657c478bd9Sstevel@tonic-gate 		if (errstr != NULL)
2667c478bd9Sstevel@tonic-gate 			*errstr = gettext("Could not get local scope");
2677c478bd9Sstevel@tonic-gate 		result = -1;
2687c478bd9Sstevel@tonic-gate 		goto out;
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	for (i = 0; i < 5; ++i) {
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 		if (scf_scope_get_service(scope, MHASH_SVC, svc) ==
2747c478bd9Sstevel@tonic-gate 		    SCF_SUCCESS)
2757c478bd9Sstevel@tonic-gate 			break;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND) {
2787c478bd9Sstevel@tonic-gate 			if (errstr != NULL)
2797c478bd9Sstevel@tonic-gate 				*errstr = gettext("Could not get manifest hash "
2807c478bd9Sstevel@tonic-gate 				    "service");
2817c478bd9Sstevel@tonic-gate 			result = -1;
2827c478bd9Sstevel@tonic-gate 			goto out;
2837c478bd9Sstevel@tonic-gate 		}
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 		if (scf_scope_add_service(scope, MHASH_SVC, svc) ==
2867c478bd9Sstevel@tonic-gate 		    SCF_SUCCESS)
2877c478bd9Sstevel@tonic-gate 			break;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 		err = scf_error();
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 		if (err == SCF_ERROR_EXISTS)
2927c478bd9Sstevel@tonic-gate 			/* Try again. */
2937c478bd9Sstevel@tonic-gate 			continue;
2947c478bd9Sstevel@tonic-gate 		else if (err == SCF_ERROR_PERMISSION_DENIED) {
2957c478bd9Sstevel@tonic-gate 			if (errstr != NULL)
2967c478bd9Sstevel@tonic-gate 				*errstr = gettext("Could not store file hash: "
2977c478bd9Sstevel@tonic-gate 				    "permission denied.\n");
2987c478bd9Sstevel@tonic-gate 			result = -1;
2997c478bd9Sstevel@tonic-gate 			goto out;
3007c478bd9Sstevel@tonic-gate 		}
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 		if (errstr != NULL)
3037c478bd9Sstevel@tonic-gate 			*errstr = gettext("Could not add manifest hash "
3047c478bd9Sstevel@tonic-gate 			    "service");
3057c478bd9Sstevel@tonic-gate 		result = -1;
3067c478bd9Sstevel@tonic-gate 		goto out;
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	if (i == 5) {
3107c478bd9Sstevel@tonic-gate 		if (errstr != NULL)
3117c478bd9Sstevel@tonic-gate 			*errstr = gettext("Could not store file hash: "
3127c478bd9Sstevel@tonic-gate 			    "service addition contention.\n");
3137c478bd9Sstevel@tonic-gate 		result = -1;
3147c478bd9Sstevel@tonic-gate 		goto out;
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	for (i = 0; i < 5; ++i) {
3187c478bd9Sstevel@tonic-gate 		if (scf_service_get_pg(svc, name, pg) == SCF_SUCCESS)
3197c478bd9Sstevel@tonic-gate 			break;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND) {
3227c478bd9Sstevel@tonic-gate 			if (errstr != NULL)
3237c478bd9Sstevel@tonic-gate 				*errstr = gettext("Could not get service's "
3247c478bd9Sstevel@tonic-gate 				    "hash record)");
3257c478bd9Sstevel@tonic-gate 			result = -1;
3267c478bd9Sstevel@tonic-gate 			goto out;
3277c478bd9Sstevel@tonic-gate 		}
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 		if (scf_service_add_pg(svc, name, MHASH_PG_TYPE,
3307c478bd9Sstevel@tonic-gate 		    MHASH_PG_FLAGS, pg) == SCF_SUCCESS)
3317c478bd9Sstevel@tonic-gate 			break;
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 		err = scf_error();
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 		if (err == SCF_ERROR_EXISTS)
3367c478bd9Sstevel@tonic-gate 			/* Try again. */
3377c478bd9Sstevel@tonic-gate 			continue;
3387c478bd9Sstevel@tonic-gate 		else if (err == SCF_ERROR_PERMISSION_DENIED) {
3397c478bd9Sstevel@tonic-gate 			if (errstr != NULL)
3407c478bd9Sstevel@tonic-gate 				*errstr = gettext("Could not store file hash: "
3417c478bd9Sstevel@tonic-gate 				    "permission denied.\n");
3427c478bd9Sstevel@tonic-gate 			result = -1;
3437c478bd9Sstevel@tonic-gate 			goto out;
3447c478bd9Sstevel@tonic-gate 		}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 		if (errstr != NULL)
3477c478bd9Sstevel@tonic-gate 			*errstr = gettext("Could not store file hash");
3487c478bd9Sstevel@tonic-gate 		result = -1;
3497c478bd9Sstevel@tonic-gate 		goto out;
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate 	if (i == 5) {
3527c478bd9Sstevel@tonic-gate 		if (errstr != NULL)
3537c478bd9Sstevel@tonic-gate 			*errstr = gettext("Could not store file hash: "
3547c478bd9Sstevel@tonic-gate 			    "property group addition contention.\n");
3557c478bd9Sstevel@tonic-gate 		result = -1;
3567c478bd9Sstevel@tonic-gate 		goto out;
3577c478bd9Sstevel@tonic-gate 	}
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	if ((e = scf_entry_create(hndl)) == NULL ||
36023294c7dSSean Wilcox 	    (val = scf_value_create(hndl)) == NULL ||
36123294c7dSSean Wilcox 	    (fe = scf_entry_create(hndl)) == NULL ||
3629444c26fSTom Whitten 	    (fval = scf_value_create(hndl)) == NULL ||
3639444c26fSTom Whitten 	    (ae = scf_entry_create(hndl)) == NULL ||
3649444c26fSTom Whitten 	    (aval = scf_value_create(hndl)) == NULL) {
3657c478bd9Sstevel@tonic-gate 		if (errstr != NULL)
3667c478bd9Sstevel@tonic-gate 			*errstr = gettext("Could not store file hash: "
3677c478bd9Sstevel@tonic-gate 			    "permission denied.\n");
3687c478bd9Sstevel@tonic-gate 		result = -1;
3697c478bd9Sstevel@tonic-gate 		goto out;
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate 
372*b1e7e97dSAndy Fiddaman 	/*
373*b1e7e97dSAndy Fiddaman 	 * Remove any PKG_INSTALL_ROOT from the manifest filename so that it
374*b1e7e97dSAndy Fiddaman 	 * points to the correct location following installation.
375*b1e7e97dSAndy Fiddaman 	 */
376*b1e7e97dSAndy Fiddaman 	base = getenv("PKG_INSTALL_ROOT");
377*b1e7e97dSAndy Fiddaman 	if (base != NULL && strncmp(fname, base, strlen(base)) == 0)
378*b1e7e97dSAndy Fiddaman 		base_sz = strlen(base);
379*b1e7e97dSAndy Fiddaman 
3807c478bd9Sstevel@tonic-gate 	ret = scf_value_set_opaque(val, hash, MHASH_SIZE);
3817c478bd9Sstevel@tonic-gate 	assert(ret == SCF_SUCCESS);
382*b1e7e97dSAndy Fiddaman 	ret = scf_value_set_astring(fval, fname + base_sz);
38323294c7dSSean Wilcox 	assert(ret == SCF_SUCCESS);
3849444c26fSTom Whitten 	if (apply_late == APPLY_LATE) {
3859444c26fSTom Whitten 		scf_value_set_boolean(aval, 1);
3869444c26fSTom Whitten 	}
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	tx = scf_transaction_create(hndl);
3897c478bd9Sstevel@tonic-gate 	if (tx == NULL) {
3907c478bd9Sstevel@tonic-gate 		if (errstr != NULL)
3917c478bd9Sstevel@tonic-gate 			*errstr = gettext("Could not create transaction");
3927c478bd9Sstevel@tonic-gate 		result = -1;
3937c478bd9Sstevel@tonic-gate 		goto out;
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	do {
3977c478bd9Sstevel@tonic-gate 		if (scf_pg_update(pg) == -1) {
3987c478bd9Sstevel@tonic-gate 			if (errstr != NULL)
3997c478bd9Sstevel@tonic-gate 				*errstr = gettext("Could not update hash "
4007c478bd9Sstevel@tonic-gate 				    "entry");
4017c478bd9Sstevel@tonic-gate 			result = -1;
4027c478bd9Sstevel@tonic-gate 			goto out;
4037c478bd9Sstevel@tonic-gate 		}
4047c478bd9Sstevel@tonic-gate 		if (scf_transaction_start(tx, pg) != SCF_SUCCESS) {
4057c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_PERMISSION_DENIED) {
4067c478bd9Sstevel@tonic-gate 				if (errstr != NULL)
4077c478bd9Sstevel@tonic-gate 					*errstr = gettext("Could not start "
4087c478bd9Sstevel@tonic-gate 					    "hash transaction.\n");
4097c478bd9Sstevel@tonic-gate 				result = -1;
4107c478bd9Sstevel@tonic-gate 				goto out;
4117c478bd9Sstevel@tonic-gate 			}
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 			if (errstr != NULL)
4147c478bd9Sstevel@tonic-gate 				*errstr = gettext("Could not store file hash: "
4157c478bd9Sstevel@tonic-gate 				    "permission denied.\n");
4167c478bd9Sstevel@tonic-gate 			result = -1;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 			scf_transaction_destroy(tx);
4197c478bd9Sstevel@tonic-gate 			(void) scf_entry_destroy(e);
4207c478bd9Sstevel@tonic-gate 			goto out;
4217c478bd9Sstevel@tonic-gate 		}
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 		if (scf_transaction_property_new(tx, e, MHASH_PROP,
4247c478bd9Sstevel@tonic-gate 		    SCF_TYPE_OPAQUE) != SCF_SUCCESS &&
4257c478bd9Sstevel@tonic-gate 		    scf_transaction_property_change_type(tx, e, MHASH_PROP,
4267c478bd9Sstevel@tonic-gate 		    SCF_TYPE_OPAQUE) != SCF_SUCCESS) {
4277c478bd9Sstevel@tonic-gate 			if (errstr != NULL)
4287c478bd9Sstevel@tonic-gate 				*errstr = gettext("Could not modify hash "
4297c478bd9Sstevel@tonic-gate 				    "entry");
4307c478bd9Sstevel@tonic-gate 			result = -1;
4317c478bd9Sstevel@tonic-gate 			goto out;
4327c478bd9Sstevel@tonic-gate 		}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 		ret = scf_entry_add_value(e, val);
4357c478bd9Sstevel@tonic-gate 		assert(ret == SCF_SUCCESS);
4367c478bd9Sstevel@tonic-gate 
4379444c26fSTom Whitten 		if (scf_transaction_property_new(tx, fe, MHASH_FILE_PROP,
43823294c7dSSean Wilcox 		    SCF_TYPE_ASTRING) != SCF_SUCCESS &&
4399444c26fSTom Whitten 		    scf_transaction_property_change_type(tx, fe,
4409444c26fSTom Whitten 		    MHASH_FILE_PROP, SCF_TYPE_ASTRING) != SCF_SUCCESS) {
44123294c7dSSean Wilcox 			if (errstr != NULL)
44223294c7dSSean Wilcox 				*errstr = gettext("Could not modify file "
44323294c7dSSean Wilcox 				    "entry");
44423294c7dSSean Wilcox 			result = -1;
44523294c7dSSean Wilcox 			goto out;
44623294c7dSSean Wilcox 		}
44723294c7dSSean Wilcox 
44823294c7dSSean Wilcox 		ret = scf_entry_add_value(fe, fval);
44923294c7dSSean Wilcox 		assert(ret == SCF_SUCCESS);
45023294c7dSSean Wilcox 
4519444c26fSTom Whitten 		switch (apply_late) {
4529444c26fSTom Whitten 		case APPLY_NONE:
4539444c26fSTom Whitten 			if (scf_transaction_property_delete(tx, ae,
4549444c26fSTom Whitten 			    MHASH_APPLY_PROP) != 0) {
4559444c26fSTom Whitten 				err = scf_error();
4569444c26fSTom Whitten 				if ((err != SCF_ERROR_DELETED) &&
4579444c26fSTom Whitten 				    (err != SCF_ERROR_NOT_FOUND)) {
4589444c26fSTom Whitten 					if (errstr != NULL) {
4599444c26fSTom Whitten 						*errstr = gettext("Could not "
4609444c26fSTom Whitten 						    "delete apply_late "
4619444c26fSTom Whitten 						    "property");
4629444c26fSTom Whitten 					}
4639444c26fSTom Whitten 					result = -1;
4649444c26fSTom Whitten 					goto out;
4659444c26fSTom Whitten 				}
4669444c26fSTom Whitten 			}
4679444c26fSTom Whitten 			break;
4689444c26fSTom Whitten 		case APPLY_LATE:
4699444c26fSTom Whitten 			if ((scf_transaction_property_new(tx, ae,
4709444c26fSTom Whitten 			    MHASH_APPLY_PROP,
4719444c26fSTom Whitten 			    SCF_TYPE_BOOLEAN) != SCF_SUCCESS) &&
4729444c26fSTom Whitten 			    (scf_transaction_property_change_type(tx, ae,
4739444c26fSTom Whitten 			    MHASH_APPLY_PROP, SCF_TYPE_BOOLEAN) !=
4749444c26fSTom Whitten 			    SCF_SUCCESS)) {
4759444c26fSTom Whitten 				if (errstr != NULL) {
4769444c26fSTom Whitten 					*errstr = gettext("Could not modify "
4779444c26fSTom Whitten 					    "apply_late property");
4789444c26fSTom Whitten 				}
4799444c26fSTom Whitten 				result = -1;
4809444c26fSTom Whitten 				goto out;
4819444c26fSTom Whitten 			}
4829444c26fSTom Whitten 
4839444c26fSTom Whitten 			ret = scf_entry_add_value(ae, aval);
4849444c26fSTom Whitten 			assert(ret == SCF_SUCCESS);
4859444c26fSTom Whitten 			break;
4869444c26fSTom Whitten 		default:
4879444c26fSTom Whitten 			abort();
4889444c26fSTom Whitten 		};
4899444c26fSTom Whitten 
4907c478bd9Sstevel@tonic-gate 		ret = scf_transaction_commit(tx);
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 		if (ret == 0)
4937c478bd9Sstevel@tonic-gate 			scf_transaction_reset(tx);
4947c478bd9Sstevel@tonic-gate 	} while (ret == 0);
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	if (ret < 0) {
4977c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_PERMISSION_DENIED) {
4987c478bd9Sstevel@tonic-gate 			if (errstr != NULL)
4997c478bd9Sstevel@tonic-gate 				*errstr = gettext("Could not store file hash: "
5007c478bd9Sstevel@tonic-gate 				    "permission denied.\n");
5017c478bd9Sstevel@tonic-gate 			result = -1;
5027c478bd9Sstevel@tonic-gate 			goto out;
5037c478bd9Sstevel@tonic-gate 		}
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 		if (errstr != NULL)
5067c478bd9Sstevel@tonic-gate 			*errstr = gettext("Could not commit transaction");
5077c478bd9Sstevel@tonic-gate 		result = -1;
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	scf_transaction_destroy(tx);
5117c478bd9Sstevel@tonic-gate 	(void) scf_entry_destroy(e);
51223294c7dSSean Wilcox 	(void) scf_entry_destroy(fe);
5139444c26fSTom Whitten 	(void) scf_entry_destroy(ae);
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate out:
5167c478bd9Sstevel@tonic-gate 	(void) scf_value_destroy(val);
51723294c7dSSean Wilcox 	(void) scf_value_destroy(fval);
5189444c26fSTom Whitten 	(void) scf_value_destroy(aval);
5197c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
5207c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
5217c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
5227c478bd9Sstevel@tonic-gate 	scf_scope_destroy(scope);
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	return (result);
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate 
5271f1b4534Scasper /*
5281f1b4534Scasper  * Generate the md5 hash of a file; manifest files are smallish
5291f1b4534Scasper  * so we can read them in one gulp.
5301f1b4534Scasper  */
5311f1b4534Scasper static int
md5_hash_file(const char * file,off64_t sz,uchar_t * hash)5321f1b4534Scasper md5_hash_file(const char *file, off64_t sz, uchar_t *hash)
5331f1b4534Scasper {
5341f1b4534Scasper 	char *buf;
5351f1b4534Scasper 	int fd;
5361f1b4534Scasper 	ssize_t res;
5371f1b4534Scasper 	int ret;
5381f1b4534Scasper 
5391f1b4534Scasper 	fd = open(file, O_RDONLY);
5401f1b4534Scasper 	if (fd < 0)
5411f1b4534Scasper 		return (-1);
5421f1b4534Scasper 
5431f1b4534Scasper 	buf = malloc(sz);
5441f1b4534Scasper 	if (buf == NULL) {
5451f1b4534Scasper 		(void) close(fd);
5461f1b4534Scasper 		return (-1);
5471f1b4534Scasper 	}
5481f1b4534Scasper 
5491f1b4534Scasper 	res = read(fd, buf, (size_t)sz);
5501f1b4534Scasper 
5511f1b4534Scasper 	(void) close(fd);
5521f1b4534Scasper 
5531f1b4534Scasper 	if (res == sz) {
5541f1b4534Scasper 		ret = 0;
5551f1b4534Scasper 		md5_calc(hash, (uchar_t *)buf, (unsigned int) sz);
5561f1b4534Scasper 	} else {
5571f1b4534Scasper 		ret = -1;
5581f1b4534Scasper 	}
5591f1b4534Scasper 
5601f1b4534Scasper 	free(buf);
5611f1b4534Scasper 	return (ret);
5621f1b4534Scasper }
5631f1b4534Scasper 
5647c478bd9Sstevel@tonic-gate /*
5657c478bd9Sstevel@tonic-gate  * int mhash_test_file(scf_handle_t *, const char *, uint_t, char **, uchar_t *)
5667c478bd9Sstevel@tonic-gate  *   Test the given filename against the hashed metadata in the repository.
5677c478bd9Sstevel@tonic-gate  *   The behaviours for import and apply are slightly different.  For imports,
5687c478bd9Sstevel@tonic-gate  *   if the hash value is absent or different, then the import operation
5697c478bd9Sstevel@tonic-gate  *   continues.  For profile application, the operation continues only if the
5707c478bd9Sstevel@tonic-gate  *   hash value for the file is absent.
5717c478bd9Sstevel@tonic-gate  *
572449975fdScasper  *   We keep two hashes: one which can be quickly test: the metadata hash,
573449975fdScasper  *   and one which is more expensive to test: the file contents hash.
574449975fdScasper  *
575449975fdScasper  *   If either hash matches, the file does not need to be re-read.
576449975fdScasper  *   If only one of the hashes matches, a side effect of this function
577449975fdScasper  *   is to store the newly computed hash.
578449975fdScasper  *   If neither hash matches, the hash computed for the new file is returned
579449975fdScasper  *   and not stored.
580449975fdScasper  *
581449975fdScasper  *   Return values:
582449975fdScasper  *	MHASH_NEWFILE	- the file no longer matches the hash or no hash existed
583449975fdScasper  *			  ONLY in this case we return the new file's hash.
584449975fdScasper  *	MHASH_FAILURE	- an internal error occurred, or the file was not found.
585449975fdScasper  *	MHASH_RECONCILED- based on the metadata/file hash, the file does
586449975fdScasper  *			  not need to be re-read; if necessary,
587449975fdScasper  *			  the hash was upgraded or reconciled.
588449975fdScasper  *
589449975fdScasper  * NOTE: no hash is returned UNLESS MHASH_NEWFILE is returned.
5907c478bd9Sstevel@tonic-gate  */
5917c478bd9Sstevel@tonic-gate int
mhash_test_file(scf_handle_t * hndl,const char * file,uint_t is_profile,char ** pnamep,uchar_t * hashbuf)5927c478bd9Sstevel@tonic-gate mhash_test_file(scf_handle_t *hndl, const char *file, uint_t is_profile,
593449975fdScasper     char **pnamep, uchar_t *hashbuf)
5947c478bd9Sstevel@tonic-gate {
5959444c26fSTom Whitten 	apply_action_t action;
5967c478bd9Sstevel@tonic-gate 	boolean_t do_hash;
5977c478bd9Sstevel@tonic-gate 	struct stat64 st;
5987c478bd9Sstevel@tonic-gate 	char *cp;
5997c478bd9Sstevel@tonic-gate 	char *data;
6007c478bd9Sstevel@tonic-gate 	uchar_t stored_hash[MHASH_SIZE];
601449975fdScasper 	uchar_t hash[MHASH_SIZE];
6027c478bd9Sstevel@tonic-gate 	char *pname;
6037c478bd9Sstevel@tonic-gate 	int ret;
604449975fdScasper 	int hashash;
605449975fdScasper 	int metahashok = 0;
6067c478bd9Sstevel@tonic-gate 
6079444c26fSTom Whitten 	if (pnamep)
6089444c26fSTom Whitten 		*pnamep = NULL;
6099444c26fSTom Whitten 
6107c478bd9Sstevel@tonic-gate 	/*
6117c478bd9Sstevel@tonic-gate 	 * In the case where we are doing automated imports, we reduce the UID,
6127c478bd9Sstevel@tonic-gate 	 * the GID, the size, and the mtime into a string (to eliminate
6137c478bd9Sstevel@tonic-gate 	 * endianness) which we then make opaque as a single MD5 digest.
6147c478bd9Sstevel@tonic-gate 	 *
6157c478bd9Sstevel@tonic-gate 	 * The previous hash was composed of the inode number, the UID, the file
6167c478bd9Sstevel@tonic-gate 	 * size, and the mtime.  This formulation was found to be insufficiently
6177c478bd9Sstevel@tonic-gate 	 * portable for use in highly replicated deployments.  The current
6187c478bd9Sstevel@tonic-gate 	 * algorithm will allow matches of this "v1" hash, but always returns
6197c478bd9Sstevel@tonic-gate 	 * the effective "v2" hash, such that updates result in the more
6207c478bd9Sstevel@tonic-gate 	 * portable hash being used.
6217c478bd9Sstevel@tonic-gate 	 *
6221f1b4534Scasper 	 * An unwanted side effect of a hash based solely on the file
6231f1b4534Scasper 	 * meta data is the fact that we pay no attention to the contents
6241f1b4534Scasper 	 * which may remain the same despite meta data changes.  This happens
6251f1b4534Scasper 	 * with (live) upgrades.  We extend the V2 hash with an additional
6261f1b4534Scasper 	 * digest of the file contents and the code retrieving the hash
6271f1b4534Scasper 	 * from the repository zero fills the remainder so we can detect
6281f1b4534Scasper 	 * it is missing.
6291f1b4534Scasper 	 *
6301f1b4534Scasper 	 * If the the V2 digest matches, we check for the presence of
6311f1b4534Scasper 	 * the contents digest and compute and store it if missing.
6321f1b4534Scasper 	 *
6331f1b4534Scasper 	 * If the V2 digest doesn't match but we also have a non-zero
6341f1b4534Scasper 	 * file hash, we match the file content digest.  If it matches,
6351f1b4534Scasper 	 * we compute and store the new complete hash so that later
6361f1b4534Scasper 	 * checks will find the meta data digest correct.
6371f1b4534Scasper 	 *
6381f1b4534Scasper 	 * If the above matches fail and the V1 hash doesn't match either,
6391f1b4534Scasper 	 * we consider the test to have failed, implying that some aspect
6401f1b4534Scasper 	 * of the manifest has changed.
6417c478bd9Sstevel@tonic-gate 	 */
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	cp = getenv("SVCCFG_CHECKHASH");
6447c478bd9Sstevel@tonic-gate 	do_hash = (cp != NULL && *cp != '\0');
6457c478bd9Sstevel@tonic-gate 	if (!do_hash) {
646449975fdScasper 		return (MHASH_NEWFILE);
647449975fdScasper 	}
648449975fdScasper 
64970cbfe41SPhilippe Jung 	pname = mhash_filename_to_propname(file, B_FALSE);
650449975fdScasper 	if (pname == NULL)
651449975fdScasper 		return (MHASH_FAILURE);
652449975fdScasper 
6539444c26fSTom Whitten 	hashash = mhash_retrieve_entry(hndl, pname, stored_hash, &action) == 0;
6549444c26fSTom Whitten 	if (is_profile == 0) {
6559444c26fSTom Whitten 		/* Actions other than APPLY_NONE are restricted to profiles. */
6569444c26fSTom Whitten 		assert(action == APPLY_NONE);
6579444c26fSTom Whitten 	}
658449975fdScasper 
6599444c26fSTom Whitten 	/*
6609444c26fSTom Whitten 	 * As a general rule, we do not reread a profile.  The exception to
6619444c26fSTom Whitten 	 * this rule is when we are running as part of the manifest import
6629444c26fSTom Whitten 	 * service and the apply_late property is set to true.
6639444c26fSTom Whitten 	 */
664449975fdScasper 	if (hashash && is_profile) {
6659444c26fSTom Whitten 		cp = getenv("SMF_FMRI");
6669444c26fSTom Whitten 		if ((cp == NULL) ||
6679444c26fSTom Whitten 		    (strcmp(cp, SCF_INSTANCE_MI) != 0) ||
6689444c26fSTom Whitten 		    (action != APPLY_LATE)) {
6699444c26fSTom Whitten 			uu_free(pname);
6709444c26fSTom Whitten 			return (MHASH_RECONCILED);
6719444c26fSTom Whitten 		}
672449975fdScasper 	}
673449975fdScasper 
674449975fdScasper 	/*
675449975fdScasper 	 * No hash and not interested in one, then don't bother computing it.
676449975fdScasper 	 * We also skip returning the property name in that case.
677449975fdScasper 	 */
678449975fdScasper 	if (!hashash && hashbuf == NULL) {
679449975fdScasper 		uu_free(pname);
680449975fdScasper 		return (MHASH_NEWFILE);
6817c478bd9Sstevel@tonic-gate 	}
6827c478bd9Sstevel@tonic-gate 
68370cbfe41SPhilippe Jung 	do {
6847c478bd9Sstevel@tonic-gate 		ret = stat64(file, &st);
68570cbfe41SPhilippe Jung 	} while (ret < 0 && errno == EINTR);
6867c478bd9Sstevel@tonic-gate 	if (ret < 0) {
687449975fdScasper 		uu_free(pname);
688449975fdScasper 		return (MHASH_FAILURE);
6897c478bd9Sstevel@tonic-gate 	}
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	data = uu_msprintf(MHASH_FORMAT_V2, st.st_uid, st.st_gid,
6927c478bd9Sstevel@tonic-gate 	    st.st_size, st.st_mtime);
6937c478bd9Sstevel@tonic-gate 	if (data == NULL) {
694449975fdScasper 		uu_free(pname);
695449975fdScasper 		return (MHASH_FAILURE);
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 
6981f1b4534Scasper 	(void) memset(hash, 0, MHASH_SIZE);
6997c478bd9Sstevel@tonic-gate 	md5_calc(hash, (uchar_t *)data, strlen(data));
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	uu_free(data);
7027c478bd9Sstevel@tonic-gate 
703449975fdScasper 	/*
704449975fdScasper 	 * Verify the meta data hash.
705449975fdScasper 	 */
706449975fdScasper 	if (hashash && memcmp(hash, stored_hash, MD5_DIGEST_LENGTH) == 0) {
707449975fdScasper 		int i;
7087c478bd9Sstevel@tonic-gate 
709449975fdScasper 		metahashok = 1;
7107c478bd9Sstevel@tonic-gate 		/*
711449975fdScasper 		 * The metadata hash matches; now we see if there was a
712449975fdScasper 		 * content hash; if not, we will continue on and compute and
713449975fdScasper 		 * store the updated hash.
714449975fdScasper 		 * If there was no content hash, mhash_retrieve_entry()
715449975fdScasper 		 * will have zero filled it.
7167c478bd9Sstevel@tonic-gate 		 */
717449975fdScasper 		for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
718449975fdScasper 			if (stored_hash[MD5_DIGEST_LENGTH+i] != 0) {
7199444c26fSTom Whitten 				if (action == APPLY_LATE) {
7209444c26fSTom Whitten 					if (pnamep != NULL)
7219444c26fSTom Whitten 						*pnamep = pname;
7229444c26fSTom Whitten 					ret = MHASH_NEWFILE;
7239444c26fSTom Whitten 				} else {
7249444c26fSTom Whitten 					uu_free(pname);
7259444c26fSTom Whitten 					ret = MHASH_RECONCILED;
7269444c26fSTom Whitten 				}
7279444c26fSTom Whitten 				return (ret);
7281f1b4534Scasper 			}
7291f1b4534Scasper 		}
730449975fdScasper 	}
7311f1b4534Scasper 
732449975fdScasper 	/*
733449975fdScasper 	 * Compute the file hash as we can no longer avoid having to know it.
734449975fdScasper 	 * Note: from this point on "hash" contains the full, current, hash.
735449975fdScasper 	 */
736449975fdScasper 	if (md5_hash_file(file, st.st_size, &hash[MHASH_SIZE_OLD]) != 0) {
737449975fdScasper 		uu_free(pname);
738449975fdScasper 		return (MHASH_FAILURE);
739449975fdScasper 	}
740449975fdScasper 	if (hashash) {
741449975fdScasper 		uchar_t hash_v1[MHASH_SIZE_OLD];
742449975fdScasper 
743449975fdScasper 		if (metahashok ||
7441f1b4534Scasper 		    memcmp(&hash[MHASH_SIZE_OLD], &stored_hash[MHASH_SIZE_OLD],
745449975fdScasper 		    MD5_DIGEST_LENGTH) == 0) {
7461f1b4534Scasper 
747449975fdScasper 			/*
748449975fdScasper 			 * Reconcile entry: we get here when either the
749449975fdScasper 			 * meta data hash matches or the content hash matches;
750449975fdScasper 			 * we then update the database with the complete
751449975fdScasper 			 * new hash so we can be a bit quicker next time.
752449975fdScasper 			 */
7539444c26fSTom Whitten 			(void) mhash_store_entry(hndl, pname, file, hash,
7549444c26fSTom Whitten 			    APPLY_NONE, NULL);
7559444c26fSTom Whitten 			if (action == APPLY_LATE) {
7569444c26fSTom Whitten 				if (pnamep != NULL)
7579444c26fSTom Whitten 					*pnamep = pname;
7589444c26fSTom Whitten 				ret = MHASH_NEWFILE;
7599444c26fSTom Whitten 			} else {
7609444c26fSTom Whitten 				uu_free(pname);
7619444c26fSTom Whitten 				ret = MHASH_RECONCILED;
7629444c26fSTom Whitten 			}
7639444c26fSTom Whitten 			return (ret);
7641f1b4534Scasper 		}
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 		/*
767449975fdScasper 		 * No match on V2 hash or file content; compare V1 hash.
7687c478bd9Sstevel@tonic-gate 		 */
7697c478bd9Sstevel@tonic-gate 		data = uu_msprintf(MHASH_FORMAT_V1, st.st_ino, st.st_uid,
7707c478bd9Sstevel@tonic-gate 		    st.st_size, st.st_mtime);
7711f1b4534Scasper 		if (data == NULL) {
7721f1b4534Scasper 			uu_free(pname);
773449975fdScasper 			return (MHASH_FAILURE);
7741f1b4534Scasper 		}
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 		md5_calc(hash_v1, (uchar_t *)data, strlen(data));
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 		uu_free(data);
7797c478bd9Sstevel@tonic-gate 
7801f1b4534Scasper 		if (memcmp(hash_v1, stored_hash, MD5_DIGEST_LENGTH) == 0) {
781449975fdScasper 			/*
782449975fdScasper 			 * Update the new entry so we don't have to go through
783449975fdScasper 			 * all this trouble next time.
784449975fdScasper 			 */
7859444c26fSTom Whitten 			(void) mhash_store_entry(hndl, pname, file, hash,
7869444c26fSTom Whitten 			    APPLY_NONE, NULL);
7871f1b4534Scasper 			uu_free(pname);
788449975fdScasper 			return (MHASH_RECONCILED);
7891f1b4534Scasper 		}
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate 
792449975fdScasper 	if (pnamep != NULL)
793449975fdScasper 		*pnamep = pname;
794449975fdScasper 	else
795449975fdScasper 		uu_free(pname);
796449975fdScasper 
797449975fdScasper 	if (hashbuf != NULL)
798449975fdScasper 		(void) memcpy(hashbuf, hash, MHASH_SIZE);
7997c478bd9Sstevel@tonic-gate 
800449975fdScasper 	return (MHASH_NEWFILE);
8017c478bd9Sstevel@tonic-gate }
802