Ejemplo n.º 1
0
int
xbps_pkgdb_lock(struct xbps_handle *xhp)
{
	mode_t prev_umask;
	int rv;
	/*
	 * Use a mandatory file lock to only allow one writer to pkgdb,
	 * other writers will block.
	 */
	xhp->pkgdb_plist = xbps_xasprintf("%s/%s", xhp->metadir, XBPS_PKGDB);
	if (xbps_pkgdb_init(xhp) == ENOENT) {
		/* if metadir does not exist, create it */
		if (access(xhp->metadir, R_OK|X_OK) == -1) {
			if (errno != ENOENT)
				return errno;

			if (xbps_mkpath(xhp->metadir, 0755) == -1) {
				rv = errno;
				xbps_dbg_printf(xhp, "[pkgdb] failed to create metadir "
				    "%s: %s\n", xhp->metadir, strerror(rv));
				return rv;
			}
		}
		/* if pkgdb is unexistent, create it with an empty dictionary */
		xhp->pkgdb = xbps_dictionary_create();
		if (!xbps_dictionary_externalize_to_file(xhp->pkgdb, xhp->pkgdb_plist)) {
			rv = errno;
			xbps_dbg_printf(xhp, "[pkgdb] failed to create pkgdb "
			    "%s: %s\n", xhp->pkgdb_plist, strerror(rv));
			return rv;
		}
	}

	prev_umask = umask(022);
	if ((pkgdb_fd = open(xhp->pkgdb_plist, O_CREAT|O_RDWR|O_CLOEXEC, 0664)) == -1) {
		rv = errno;
		xbps_dbg_printf(xhp, "[pkgdb] cannot open pkgdb for locking "
		    "%s: %s\n", xhp->pkgdb_plist, strerror(rv));
		free(xhp->pkgdb_plist);
		umask(prev_umask);
		return rv;
	}
	umask(prev_umask);

	if (lockf(pkgdb_fd, F_TLOCK, 0) == -1) {
		rv = errno;
		xbps_dbg_printf(xhp, "[pkgdb] cannot lock pkgdb: %s\n", strerror(rv));
		return rv;
	}
	return 0;
}
Ejemplo n.º 2
0
int
xbps_pkgdb_update(struct xbps_handle *xhp, bool flush, bool update)
{
	xbps_dictionary_t pkgdb_storage;
	static int cached_rv;
	int rv = 0;

	if (cached_rv && !flush)
		return cached_rv;

	if (xhp->pkgdb && update) {
		pkgdb_storage = xbps_dictionary_internalize_from_file(xhp->pkgdb_plist);
		if (pkgdb_storage == NULL ||
		    !xbps_dictionary_equals(xhp->pkgdb, pkgdb_storage)) {
			/* flush dictionary to storage */
			if (!xbps_dictionary_externalize_to_file(xhp->pkgdb, xhp->pkgdb_plist))
				return errno;
		}
		if (pkgdb_storage)
			xbps_object_release(pkgdb_storage);

		xbps_object_release(xhp->pkgdb);
		xhp->pkgdb = NULL;
		cached_rv = 0;
	}
	if (!update)
		return rv;

	/* update copy in memory */
	if ((xhp->pkgdb = xbps_dictionary_internalize_from_file(xhp->pkgdb_plist)) == NULL) {
		rv = errno;
		if (!rv)
			rv = EINVAL;

		if (rv == ENOENT)
			xhp->pkgdb = xbps_dictionary_create();
		else
			xbps_error_printf("cannot access to pkgdb: %s\n", strerror(rv));

		cached_rv = rv = errno;
	}

	return rv;
}
Ejemplo n.º 3
0
int
xbps_pkgdb_update(struct xbps_handle *xhp, bool flush)
{
	xbps_dictionary_t pkgdb_storage;
	char *plist;
	static int cached_rv;
	int rv = 0;

	if (cached_rv && !flush)
		return cached_rv;

	plist = xbps_xasprintf("%s/%s", xhp->metadir, XBPS_PKGDB);
	if (xhp->pkgdb && flush) {
		pkgdb_storage = xbps_dictionary_internalize_from_file(plist);
		if (pkgdb_storage == NULL ||
		    !xbps_dictionary_equals(xhp->pkgdb, pkgdb_storage)) {
			/* flush dictionary to storage */
			if (!xbps_dictionary_externalize_to_file(xhp->pkgdb, plist)) {
				free(plist);
				return errno;
			}
		}
		if (pkgdb_storage)
			xbps_object_release(pkgdb_storage);

		xbps_object_release(xhp->pkgdb);
		xhp->pkgdb = NULL;
		cached_rv = 0;
	}
	/* update copy in memory */
	if ((xhp->pkgdb = xbps_dictionary_internalize_from_file(plist)) == NULL) {
		if (errno == ENOENT)
			xhp->pkgdb = xbps_dictionary_create();
		else
			xbps_error_printf("cannot access to pkgdb: %s\n", strerror(errno));

		cached_rv = rv = errno;
	}
	free(plist);

	return rv;
}
Ejemplo n.º 4
0
int
xbps_repo_key_import(struct xbps_repo *repo)
{
	xbps_dictionary_t repokeyd = NULL;
	xbps_data_t pubkey = NULL;
	uint16_t pubkey_size = 0;
	const char *signedby = NULL;
	char *hexfp = NULL;
	char *p, *dbkeyd, *rkeyfile = NULL;
	int import, rv = 0;

	assert(repo);
	/*
	 * If repository does not have required metadata plist, ignore it.
	 */
	if (!xbps_dictionary_count(repo->idxmeta)) {
		xbps_dbg_printf(repo->xhp,
		    "[repo] `%s' unsigned repository!\n", repo->uri);
		return 0;
	}
	/*
	 * Check for required objects in index-meta:
	 * 	- signature-by (string)
	 * 	- public-key (data)
	 * 	- public-key-size (number)
	 */
	xbps_dictionary_get_cstring_nocopy(repo->idxmeta, "signature-by", &signedby);
	xbps_dictionary_get_uint16(repo->idxmeta, "public-key-size", &pubkey_size);
	pubkey = xbps_dictionary_get(repo->idxmeta, "public-key");

	if (signedby == NULL || pubkey_size == 0 ||
	    xbps_object_type(pubkey) != XBPS_TYPE_DATA) {
		xbps_dbg_printf(repo->xhp,
		    "[repo] `%s': incomplete signed repository "
		    "(missing objs)\n", repo->uri);
		rv = EINVAL;
		goto out;
	}
	hexfp = xbps_pubkey2fp(repo->xhp, pubkey);
	/*
	 * Check if the public key is alredy stored.
	 */
	rkeyfile = xbps_xasprintf("%s/keys/%s.plist", repo->xhp->metadir, hexfp);
	repokeyd = xbps_plist_dictionary_from_file(repo->xhp, rkeyfile);
	if (xbps_object_type(repokeyd) == XBPS_TYPE_DICTIONARY) {
		xbps_dbg_printf(repo->xhp,
		    "[repo] `%s' public key already stored.\n", repo->uri);
		goto out;
	}
	/*
	 * Notify the client and take appropiate action to import
	 * the repository public key. Pass back the public key openssh fingerprint
	 * to the client.
	 */
	import = xbps_set_cb_state(repo->xhp, XBPS_STATE_REPO_KEY_IMPORT, 0,
			hexfp, "`%s' repository has been RSA signed by \"%s\"",
			repo->uri, signedby);
	if (import <= 0) {
		rv = EAGAIN;
		goto out;
	}

	p = strdup(rkeyfile);
	dbkeyd = dirname(p);
	assert(dbkeyd);
	if (access(dbkeyd, R_OK|W_OK) == -1) {
		rv = errno;
		if (rv == ENOENT)
			rv = xbps_mkpath(dbkeyd, 0755);
		if (rv != 0) {
			rv = errno;
			xbps_dbg_printf(repo->xhp,
			    "[repo] `%s' cannot create %s: %s\n",
			    repo->uri, dbkeyd, strerror(errno));
			free(p);
			goto out;
		}
	}
	free(p);

	repokeyd = xbps_dictionary_create();
	xbps_dictionary_set(repokeyd, "public-key", pubkey);
	xbps_dictionary_set_uint16(repokeyd, "public-key-size", pubkey_size);
	xbps_dictionary_set_cstring_nocopy(repokeyd, "signature-by", signedby);

	if (!xbps_dictionary_externalize_to_file(repokeyd, rkeyfile)) {
		rv = errno;
		xbps_dbg_printf(repo->xhp,
		    "[repo] `%s' failed to externalize %s: %s\n",
		    repo->uri, rkeyfile, strerror(rv));
	}

out:
	if (hexfp)
		free(hexfp);
	if (repokeyd)
		xbps_object_release(repokeyd);
	if (rkeyfile)
		free(rkeyfile);
	return rv;
}
Ejemplo n.º 5
0
int HIDDEN
xbps_transaction_package_replace(struct xbps_handle *xhp)
{
	xbps_array_t replaces, unsorted;
	xbps_dictionary_t instd, reppkgd, filesd;
	xbps_object_t obj, obj2;
	xbps_object_iterator_t iter;
	const char *pattern, *pkgver, *curpkgver;
	char *buf, *pkgname, *curpkgname;
	bool instd_auto, sr;
	unsigned int i;

	unsorted = xbps_dictionary_get(xhp->transd, "unsorted_deps");

	for (i = 0; i < xbps_array_count(unsorted); i++) {
		obj = xbps_array_get(unsorted, i);
		replaces = xbps_dictionary_get(obj, "replaces");
		if (replaces == NULL || xbps_array_count(replaces) == 0)
			continue;

		iter = xbps_array_iterator(replaces);
		assert(iter);

		while ((obj2 = xbps_object_iterator_next(iter)) != NULL) {
			pattern = xbps_string_cstring_nocopy(obj2);
			/*
			 * Find the installed package that matches the pattern
			 * to be replaced.
			 */
			if (((instd = xbps_pkgdb_get_pkg(xhp, pattern)) == NULL) &&
			    ((instd = xbps_pkgdb_get_virtualpkg(xhp, pattern)) == NULL))
				continue;

			xbps_dictionary_get_cstring_nocopy(obj,
			    "pkgver", &pkgver);
			xbps_dictionary_get_cstring_nocopy(instd,
			    "pkgver", &curpkgver);
			pkgname = xbps_pkg_name(pkgver);
			assert(pkgname);
			curpkgname = xbps_pkg_name(curpkgver);
			assert(curpkgver);
			/*
			 * Check that we are not replacing the same package,
			 * due to virtual packages.
			 */
			if (strcmp(pkgname, curpkgname) == 0) {
				free(pkgname);
				free(curpkgname);
				continue;
			}

			xbps_dbg_printf(xhp,
			    "Package `%s' will be replaced by `%s', "
			    "matched with `%s'\n", curpkgver, pkgver, pattern);
			instd_auto = false;
			xbps_dictionary_get_bool(instd,
			    "automatic-install", &instd_auto);
			/*
			 * Package contains replaces="pkgpattern", but the
			 * package that should be replaced is also in the
			 * transaction and it's going to be updated.
			 */
			if ((reppkgd = xbps_find_pkg_in_array(unsorted, curpkgname))) {
				xbps_dbg_printf(xhp,
				    "found replaced pkg "
				    "in transaction\n");
				xbps_dictionary_set_bool(instd,
				    "remove-and-update", true);
				xbps_dictionary_set_bool(reppkgd,
				    "automatic-install", instd_auto);
				xbps_dictionary_set_bool(reppkgd,
				    "skip-obsoletes", true);
				xbps_array_replace_dict_by_name(unsorted,
				   reppkgd, curpkgname);
			}
			/*
			 * If new package is providing a virtual package to the
			 * package that we want to replace we should respect
			 * the automatic-install object.
			 */
			if (xbps_match_virtual_pkg_in_dict(obj,
			    pattern, true) ||
			    xbps_match_virtual_pkg_in_dict(instd,
			    pkgname, false)) {
				xbps_dictionary_set_bool(obj,
				    "automatic-install", instd_auto);
			}
			sr = false;
			xbps_dictionary_get_bool(obj, "softreplace", &sr);
			if (sr) {
				xbps_dictionary_set_bool(obj,
				    "automatic-install", instd_auto);
				xbps_dictionary_set_bool(instd,
				    "softreplace", true);
				buf = xbps_xasprintf("%s/.%s.plist",
				    xhp->metadir, curpkgname);
				filesd = xbps_dictionary_internalize_from_file(buf);
				free(buf);
				assert(filesd != NULL);
				buf = xbps_xasprintf("%s/.%s.plist",
				    xhp->metadir, pkgname);
				if (!xbps_dictionary_externalize_to_file(filesd, buf)) {
					free(buf);
					xbps_object_release(filesd);
					xbps_object_iterator_release(iter);
					free(pkgname);
					free(curpkgname);
					return errno;
				}
				xbps_object_release(filesd);
				free(buf);
			}
			/*
			 * Add package dictionary into the transaction and mark
			 * it as to be "removed".
			 */
			xbps_dictionary_set_cstring_nocopy(instd,
			    "transaction", "remove");
			xbps_array_add(unsorted, instd);
			free(pkgname);
			free(curpkgname);
		}
		xbps_object_iterator_release(iter);
	}

	return 0;
}
Ejemplo n.º 6
0
static int
create_pkg_metaplist(struct xbps_handle *xhp, const char *pkgname, const char *pkgver,
		     xbps_dictionary_t propsd, xbps_dictionary_t filesd,
		     const void *instbuf, const size_t instbufsiz,
		     const void *rembuf, const size_t rembufsiz)
{
	xbps_array_t array;
	xbps_dictionary_t pkg_metad;
	xbps_data_t data;
	char *buf;
	int rv = 0;

	xbps_dictionary_make_immutable(propsd);
	pkg_metad = xbps_dictionary_copy_mutable(propsd);

	/* Add objects from XBPS_PKGFILES */
	array = xbps_dictionary_get(filesd, "files");
	if (xbps_array_count(array))
		xbps_dictionary_set(pkg_metad, "files", array);
	array = xbps_dictionary_get(filesd, "conf_files");
	if (xbps_array_count(array))
		xbps_dictionary_set(pkg_metad, "conf_files", array);
	array = xbps_dictionary_get(filesd, "links");
	if (xbps_array_count(array))
		xbps_dictionary_set(pkg_metad, "links", array);
	array = xbps_dictionary_get(filesd, "dirs");
	if (xbps_array_count(array))
		xbps_dictionary_set(pkg_metad, "dirs", array);

	/* Add install/remove scripts data objects */
	if (instbuf != NULL) {
		data = xbps_data_create_data(instbuf, instbufsiz);
		assert(data);
		xbps_dictionary_set(pkg_metad, "install-script", data);
		xbps_object_release(data);
	}
	if (rembuf != NULL) {
		data = xbps_data_create_data(rembuf, rembufsiz);
		assert(data);
		xbps_dictionary_set(pkg_metad, "remove-script", data);
		xbps_object_release(data);
	}
	/* Remove unneeded objs from transaction */
	xbps_dictionary_remove(pkg_metad, "remove-and-update");
	xbps_dictionary_remove(pkg_metad, "transaction");
	xbps_dictionary_remove(pkg_metad, "state");
	xbps_dictionary_remove(pkg_metad, "pkgname");
	xbps_dictionary_remove(pkg_metad, "version");

	/*
	 * Externalize pkg dictionary to metadir.
	 */
	if (access(xhp->metadir, R_OK|X_OK) == -1) {
		if (errno == ENOENT) {
			xbps_mkpath(xhp->metadir, 0755);
		} else {
			return errno;
		}
	}
	buf = xbps_xasprintf("%s/.%s.plist", XBPS_META_PATH, pkgname);
	if (!xbps_dictionary_externalize_to_file(pkg_metad, buf)) {
		rv = errno;
		xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL,
		    errno, pkgver,
		    "%s: [unpack] failed to write metadata file `%s': %s",
		    pkgver, buf, strerror(errno));
	}
	free(buf);
	xbps_object_release(pkg_metad);

	return rv;
}