Blame cifskey.c

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