Blame common/runtime.c

Packit ce73f7
/*
Packit ce73f7
 * Copyright (c) 2018 Red Hat Inc
Packit ce73f7
 *
Packit ce73f7
 * Redistribution and use in source and binary forms, with or without
Packit ce73f7
 * modification, are permitted provided that the following conditions
Packit ce73f7
 * are met:
Packit ce73f7
 *
Packit ce73f7
 *     * Redistributions of source code must retain the above
Packit ce73f7
 *       copyright notice, this list of conditions and the
Packit ce73f7
 *       following disclaimer.
Packit ce73f7
 *     * Redistributions in binary form must reproduce the
Packit ce73f7
 *       above copyright notice, this list of conditions and
Packit ce73f7
 *       the following disclaimer in the documentation and/or
Packit ce73f7
 *       other materials provided with the distribution.
Packit ce73f7
 *     * The names of contributors to this software may not be
Packit ce73f7
 *       used to endorse or promote products derived from this
Packit ce73f7
 *       software without specific prior written permission.
Packit ce73f7
 *
Packit ce73f7
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit ce73f7
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit ce73f7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
Packit ce73f7
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
Packit ce73f7
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit ce73f7
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
Packit ce73f7
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Packit ce73f7
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
Packit ce73f7
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Packit ce73f7
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
Packit ce73f7
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
Packit ce73f7
 * DAMAGE.
Packit ce73f7
 *
Packit ce73f7
 * Author: Daiki Ueno
Packit ce73f7
 */
Packit ce73f7
Packit ce73f7
#include "config.h"
Packit ce73f7
Packit ce73f7
#include "runtime.h"
Packit ce73f7
Packit ce73f7
#include "compat.h"
Packit ce73f7
Packit ce73f7
#include <stdio.h>
Packit ce73f7
#include <stdlib.h>
Packit ce73f7
#include <string.h>
Packit ce73f7
Packit ce73f7
#ifdef OS_UNIX
Packit ce73f7
#include <pwd.h>
Packit ce73f7
#include <sys/stat.h>
Packit ce73f7
#include <sys/types.h>
Packit ce73f7
#include <unistd.h>
Packit ce73f7
Packit ce73f7
static const char * const _p11_runtime_bases_default[] = { "/run", "/var/run", NULL };
Packit ce73f7
const char * const *_p11_runtime_bases = _p11_runtime_bases_default;
Packit ce73f7
#endif
Packit ce73f7
Packit ce73f7
CK_RV
Packit ce73f7
p11_get_runtime_directory (char **directoryp)
Packit ce73f7
{
Packit ce73f7
	const char *envvar;
Packit ce73f7
	char *directory;
Packit ce73f7
#ifdef OS_UNIX
Packit ce73f7
	const char * const *bases = _p11_runtime_bases;
Packit ce73f7
	char *prefix;
Packit ce73f7
	uid_t uid;
Packit ce73f7
	struct stat sb;
Packit ce73f7
	struct passwd pwbuf, *pw;
Packit ce73f7
	char buf[1024];
Packit ce73f7
	int i;
Packit ce73f7
#endif
Packit ce73f7
Packit ce73f7
	/* We can't always assume the XDG_RUNTIME_DIR envvar here,
Packit ce73f7
	 * because the PKCS#11 module can be loaded by a program that
Packit ce73f7
	 * calls setuid().  */
Packit ce73f7
	envvar = secure_getenv ("XDG_RUNTIME_DIR");
Packit ce73f7
Packit ce73f7
	if (envvar != NULL && envvar[0] != '\0') {
Packit ce73f7
		directory = strdup (envvar);
Packit ce73f7
		if (!directory)
Packit ce73f7
			return CKR_HOST_MEMORY;
Packit ce73f7
Packit ce73f7
		*directoryp = directory;
Packit ce73f7
		return CKR_OK;
Packit ce73f7
	}
Packit ce73f7
Packit ce73f7
#ifdef OS_UNIX
Packit ce73f7
	uid = getuid ();
Packit ce73f7
Packit ce73f7
	for (i = 0; bases[i] != NULL; i++) {
Packit ce73f7
		if (asprintf (&prefix, "%s/user/%u",
Packit ce73f7
			      bases[i], (unsigned int) uid) < 0)
Packit ce73f7
			return CKR_HOST_MEMORY;
Packit ce73f7
		if (stat (prefix, &sb) != -1 && S_ISDIR (sb.st_mode)) {
Packit ce73f7
			*directoryp = prefix;
Packit ce73f7
			return CKR_OK;
Packit ce73f7
		}
Packit ce73f7
		free (prefix);
Packit ce73f7
	}
Packit ce73f7
#endif
Packit ce73f7
Packit ce73f7
	/* We can't use /run/user/<UID>, fallback to ~/.cache.  */
Packit ce73f7
	envvar = secure_getenv ("XDG_CACHE_HOME");
Packit ce73f7
Packit ce73f7
	if (envvar != NULL && envvar[0] != '\0') {
Packit ce73f7
		directory = strdup (envvar);
Packit ce73f7
		if (!directory)
Packit ce73f7
			return CKR_HOST_MEMORY;
Packit ce73f7
Packit ce73f7
		*directoryp = directory;
Packit ce73f7
		return CKR_OK;
Packit ce73f7
	}
Packit ce73f7
Packit ce73f7
#ifdef OS_UNIX
Packit ce73f7
	if (getpwuid_r (uid, &pwbuf, buf, sizeof buf, &pw) == 0 &&
Packit ce73f7
	    pw != NULL && pw->pw_dir != NULL && *pw->pw_dir == '/') {
Packit ce73f7
		if (asprintf (&directory, "%s/.cache", pw->pw_dir) < 0)
Packit ce73f7
			return CKR_HOST_MEMORY;
Packit ce73f7
		*directoryp = directory;
Packit ce73f7
		return CKR_OK;
Packit ce73f7
	}
Packit ce73f7
#endif
Packit ce73f7
Packit ce73f7
	return CKR_GENERAL_ERROR;
Packit ce73f7
}