Blame cifskey.c

Packit 5f9837
/*
Packit 5f9837
 * Credentials stashing routines for Linux CIFS VFS (virtual filesystem)
Packit 5f9837
 * Copyright (C) 2010 Jeff Layton (jlayton@samba.org)
Packit 5f9837
 * Copyright (C) 2010 Igor Druzhinin (jaxbrigs@gmail.com)
Packit 5f9837
 *
Packit 5f9837
 * This program is free software; you can redistribute it and/or modify
Packit 5f9837
 * it under the terms of the GNU General Public License as published by
Packit 5f9837
 * the Free Software Foundation; either version 3 of the License, or
Packit 5f9837
 * (at your option) any later version.
Packit 5f9837
 *
Packit 5f9837
 * This program is distributed in the hope that it will be useful,
Packit 5f9837
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 5f9837
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 5f9837
 * GNU General Public License for more details.
Packit 5f9837
 *
Packit 5f9837
 * You should have received a copy of the GNU General Public License
Packit 5f9837
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 5f9837
 */
Packit 5f9837
Packit 5f9837
#include <sys/types.h>
Packit 5f9837
#include <keyutils.h>
Packit 5f9837
#include <stdio.h>
Packit 5f9837
#include <errno.h>
Packit 5f9837
#include "cifskey.h"
Packit 5f9837
#include "resolve_host.h"
Packit 5f9837
Packit 5f9837
/* search a specific key in keyring */
Packit 5f9837
key_serial_t
Packit 5f9837
key_search(const char *addr, char keytype)
Packit 5f9837
{
Packit 5f9837
	char desc[INET6_ADDRSTRLEN + sizeof(KEY_PREFIX) + 4];
Packit 5f9837
Packit 5f9837
	if (snprintf(desc, sizeof(desc), "%s:%c:%s", KEY_PREFIX, keytype, addr) >= (int)sizeof(desc)) {
Packit 5f9837
		errno = EINVAL;
Packit 5f9837
		return -1;
Packit 5f9837
	}
Packit 5f9837
Packit 5f9837
	return keyctl_search(DEST_KEYRING, CIFS_KEY_TYPE, desc, 0);
Packit 5f9837
}
Packit 5f9837
Packit 5f9837
/* add or update a specific key to keyring */
Packit 5f9837
key_serial_t
Packit 5f9837
key_add(const char *addr, const char *user, const char *pass, char keytype)
Packit 5f9837
{
Packit 5f9837
	int len;
Packit 5f9837
	char desc[INET6_ADDRSTRLEN + sizeof(KEY_PREFIX) + 4];
Packit 5f9837
	char val[MOUNT_PASSWD_SIZE +  MAX_USERNAME_SIZE + 2];
Packit 5f9837
Packit 5f9837
	/* set key description */
Packit 5f9837
	if (snprintf(desc, sizeof(desc), "%s:%c:%s", KEY_PREFIX, keytype, addr) >= (int)sizeof(desc)) {
Packit 5f9837
		errno = EINVAL;
Packit 5f9837
		return -1;
Packit 5f9837
	}
Packit 5f9837
Packit 5f9837
	/* set payload contents */
Packit 5f9837
	len = snprintf(val, sizeof(val), "%s:%s", user, pass);
Packit 5f9837
	if (len >= (int)sizeof(val)) {
Packit 5f9837
		errno = EINVAL;
Packit 5f9837
		return -1;
Packit 5f9837
	}
Packit 5f9837
Packit 5f9837
	return add_key(CIFS_KEY_TYPE, desc, val, len + 1, DEST_KEYRING);
Packit 5f9837
}