keychain.c revision 4bff34e37def8a90f9194d81bc345c52ba20086a
4bff34e37def8a90f9194d81bc345c52ba20086athurlow/*
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * CDDL HEADER START
4bff34e37def8a90f9194d81bc345c52ba20086athurlow *
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * The contents of this file are subject to the terms of the
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Common Development and Distribution License (the "License").
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * You may not use this file except in compliance with the License.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow *
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * or http://www.opensolaris.org/os/licensing.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * See the License for the specific language governing permissions
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * and limitations under the License.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow *
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * When distributing Covered Code, include this CDDL HEADER in each
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * If applicable, add the following below this CDDL HEADER, with the
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * fields enclosed by brackets "[]" replaced with your own identifying
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * information: Portions Copyright [yyyy] [name of copyright owner]
4bff34e37def8a90f9194d81bc345c52ba20086athurlow *
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * CDDL HEADER END
4bff34e37def8a90f9194d81bc345c52ba20086athurlow */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow/*
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Use is subject to license terms.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#pragma ident "%Z%%M% %I% %E% SMI"
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow/*
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * External interface to the libsmbfs/netsmb keychain
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * storage mechanism. This interface is consumed by
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * the "smbutil" commands: login, logout, ...
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * and by the SMBFS PAM module.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <sys/types.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <errno.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <stdio.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <string.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <unistd.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <libintl.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <netsmb/smb_dev.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <netsmb/smb_lib.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <netsmb/smb_keychain.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <cflib.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow/* common func. for add/del/chk */
4bff34e37def8a90f9194d81bc345c52ba20086athurlowstatic int
4bff34e37def8a90f9194d81bc345c52ba20086athurlowsmbfs_keychain_cmn(
4bff34e37def8a90f9194d81bc345c52ba20086athurlow int cmd,
4bff34e37def8a90f9194d81bc345c52ba20086athurlow uid_t uid,
4bff34e37def8a90f9194d81bc345c52ba20086athurlow const char *dom,
4bff34e37def8a90f9194d81bc345c52ba20086athurlow const char *usr,
4bff34e37def8a90f9194d81bc345c52ba20086athurlow const char *pass)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow{
4bff34e37def8a90f9194d81bc345c52ba20086athurlow smbioc_pk_t pk;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow int err, fd;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow memset(&pk, 0, sizeof (pk));
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow pk.pk_uid = uid;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow switch (cmd) {
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow case SMBIOC_PK_ADD:
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (pass == NULL)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (SMB_KEYCHAIN_BADPASSWD);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (strlcpy(pk.pk_pass, pass, sizeof (pk.pk_pass)) >=
4bff34e37def8a90f9194d81bc345c52ba20086athurlow sizeof (pk.pk_pass))
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (SMB_KEYCHAIN_BADPASSWD);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow /* FALLTHROUGH */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow case SMBIOC_PK_CHK:
4bff34e37def8a90f9194d81bc345c52ba20086athurlow case SMBIOC_PK_DEL:
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (dom == NULL)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (SMB_KEYCHAIN_BADDOMAIN);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (strlcpy(pk.pk_dom, dom, sizeof (pk.pk_dom)) >=
4bff34e37def8a90f9194d81bc345c52ba20086athurlow sizeof (pk.pk_dom))
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (SMB_KEYCHAIN_BADDOMAIN);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (usr == NULL)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (SMB_KEYCHAIN_BADUSER);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (strlcpy(pk.pk_usr, usr, sizeof (pk.pk_usr)) >=
4bff34e37def8a90f9194d81bc345c52ba20086athurlow sizeof (pk.pk_usr))
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (SMB_KEYCHAIN_BADUSER);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow break;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow case SMBIOC_PK_DEL_OWNER: /* all owned by the caller */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow case SMBIOC_PK_DEL_EVERYONE: /* all owned by everyone */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow /*
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * These two do not copyin any args, but we'll
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * pass &pk here anyway just so we can use the
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * common code path below.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow break;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow default:
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (SMB_KEYCHAIN_UNKNOWN);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow }
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow fd = smb_open_driver();
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (fd < 0) {
4bff34e37def8a90f9194d81bc345c52ba20086athurlow err = SMB_KEYCHAIN_NODRIVER;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow goto out;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow }
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow err = 0;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (ioctl(fd, cmd, &pk) < 0)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow err = errno;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow close(fd);
4bff34e37def8a90f9194d81bc345c52ba20086athurlowout:
4bff34e37def8a90f9194d81bc345c52ba20086athurlow memset(&pk, 0, sizeof (pk));
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (err);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow}
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow/* Add a password to the keychain. */
4bff34e37def8a90f9194d81bc345c52ba20086athurlowint
4bff34e37def8a90f9194d81bc345c52ba20086athurlowsmbfs_keychain_add(uid_t uid, const char *dom, const char *usr,
4bff34e37def8a90f9194d81bc345c52ba20086athurlow const char *pass)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow{
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (smbfs_keychain_cmn(SMBIOC_PK_ADD, uid, dom, usr, pass));
4bff34e37def8a90f9194d81bc345c52ba20086athurlow}
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow/* Delete a password from the keychain. */
4bff34e37def8a90f9194d81bc345c52ba20086athurlowint
4bff34e37def8a90f9194d81bc345c52ba20086athurlowsmbfs_keychain_del(uid_t uid, const char *dom, const char *usr)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow{
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (smbfs_keychain_cmn(SMBIOC_PK_DEL, uid, dom, usr, NULL));
4bff34e37def8a90f9194d81bc345c52ba20086athurlow}
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow/*
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Check for existence of a keychain entry.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Returns 0 if it exists, else ENOENT.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow */
4bff34e37def8a90f9194d81bc345c52ba20086athurlowint
4bff34e37def8a90f9194d81bc345c52ba20086athurlowsmbfs_keychain_chk(const char *dom, const char *usr)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow{
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (smbfs_keychain_cmn(SMBIOC_PK_CHK, (uid_t)-1, dom, usr, NULL));
4bff34e37def8a90f9194d81bc345c52ba20086athurlow}
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow/*
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Delete all keychain entries owned by the caller.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow */
4bff34e37def8a90f9194d81bc345c52ba20086athurlowint
4bff34e37def8a90f9194d81bc345c52ba20086athurlowsmbfs_keychain_del_owner()
4bff34e37def8a90f9194d81bc345c52ba20086athurlow{
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (smbfs_keychain_cmn(SMBIOC_PK_DEL_OWNER, getuid(), 0, 0, 0));
4bff34e37def8a90f9194d81bc345c52ba20086athurlow}
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow/*
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Delete all keychain entries (regardless of onwer).
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Requires super-user privliege.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow */
4bff34e37def8a90f9194d81bc345c52ba20086athurlowint
4bff34e37def8a90f9194d81bc345c52ba20086athurlowsmbfs_keychain_del_everyone()
4bff34e37def8a90f9194d81bc345c52ba20086athurlow{
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (smbfs_keychain_cmn(SMBIOC_PK_DEL_EVERYONE, getuid(), 0, 0, 0));
4bff34e37def8a90f9194d81bc345c52ba20086athurlow}
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow/*
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * This is not really part of the keychain library,
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * but is typically needed in code that wants to
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * provide (editable) defaults for domain/user
4bff34e37def8a90f9194d81bc345c52ba20086athurlow *
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Get default domain and user names
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Server name is optional.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow */
4bff34e37def8a90f9194d81bc345c52ba20086athurlowint
4bff34e37def8a90f9194d81bc345c52ba20086athurlowsmbfs_default_dom_usr(const char *home, const char *server,
4bff34e37def8a90f9194d81bc345c52ba20086athurlow char *dom, int maxdom, char *usr, int maxusr)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow{
4bff34e37def8a90f9194d81bc345c52ba20086athurlow struct smb_ctx sctx, *ctx = &sctx;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow int err;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow err = smb_ctx_init(ctx, 0, NULL, SMBL_VC, SMBL_VC, SMB_ST_ANY);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (err)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (err);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (server)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow smb_ctx_setserver(ctx, server);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (home && *home)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow ctx->ct_home = (char *)home;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow err = smb_ctx_readrc(ctx);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (err)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (err);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (smb_rc)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow rc_close(smb_rc);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (dom)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow strlcpy(dom, ctx->ct_ssn.ioc_workgroup, maxdom);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (usr)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow strlcpy(usr, ctx->ct_ssn.ioc_user, maxusr);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (0);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow}