keychain.c revision 02d09e03eb27f3a2dc299de704e45dae5173f43f
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * CDDL HEADER START
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 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * See the License for the specific language governing permissions
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * and limitations under the License.
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 * CDDL HEADER END
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Use is subject to license terms.
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/* common func. for add/del/chk */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow const char *dom,
4bff34e37def8a90f9194d81bc345c52ba20086athurlow const char *usr,
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Add password hashes to the keychain.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow /* FALLTHROUGH */
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Copy domain and user.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow case SMBIOC_PK_DEL_OWNER: /* all owned by the caller */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow case SMBIOC_PK_DEL_EVERYONE: /* all owned by everyone */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * These two do not copyin any args, but we'll
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * pass pk here anyway just so we can use the
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * common code path below.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Add a password to the keychain.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Note: pass is a cleartext password.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * We use it here to compute the LM hash and NT hash,
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * and then store ONLY the hashes.
4bff34e37def8a90f9194d81bc345c52ba20086athurlowsmbfs_keychain_add(uid_t uid, const char *dom, const char *usr,
4bff34e37def8a90f9194d81bc345c52ba20086athurlow const char *pass)
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross if ((err = ntlm_compute_lm_hash(lmhash, pass)) != 0)
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross if ((err = ntlm_compute_nt_hash(nthash, pass)) != 0)
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross err = smbfs_keychain_cmn(cmd, uid, dom, usr, lmhash, nthash);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow/* Delete a password from the keychain. */
4bff34e37def8a90f9194d81bc345c52ba20086athurlowsmbfs_keychain_del(uid_t uid, const char *dom, const char *usr)
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross return (smbfs_keychain_cmn(SMBIOC_PK_DEL, uid, dom, usr, NULL, NULL));
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Check for existence of a keychain entry.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Returns 0 if it exists, else ENOENT.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross return (smbfs_keychain_cmn(SMBIOC_PK_CHK, uid, dom, usr, NULL, NULL));
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Get the stored hashes
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Rosssmbfs_keychain_get(const char *dom, const char *usr,
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross err = smbfs_keychain_cmn(cmd, uid, dom, usr, lmhash, nthash);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Delete all keychain entries owned by the caller.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross return (smbfs_keychain_cmn(cmd, uid, NULL, NULL, NULL, NULL));
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Delete all keychain entries (regardless of onwer).
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Requires super-user privliege.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross return (smbfs_keychain_cmn(cmd, uid, NULL, NULL, NULL, NULL));
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Private function to get keychain p/w hashes.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * 1st: try lookup using system name
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross err = smbfs_keychain_get(ctx->ct_fullserver, ctx->ct_user,
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * 2nd: try lookup using domain name
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross err = smbfs_keychain_get(ctx->ct_domain, ctx->ct_user,
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross ctx->ct_flags |= (SMBCF_KCFOUND | SMBCF_KCDOMAIN);
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 * Get default domain and user names
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Server name is optional.
4bff34e37def8a90f9194d81bc345c52ba20086athurlowsmbfs_default_dom_usr(const char *home, const char *server,