auth-krb5.c 6.93 KB
Newer Older
1
/* $OpenBSD: auth-krb5.c,v 1.20 2013/07/20 01:55:13 djm Exp $ */
2 3
/*
 *    Kerberos v5 authentication and ticket-passing routines.
4
 *
5 6
 * $FreeBSD: src/crypto/openssh/auth-krb5.c,v 1.6 2001/02/13 16:58:04 assar Exp $
 */
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * Copyright (c) 2002 Daniel Kouril.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
30 31

#include "includes.h"
32

33 34 35 36 37
#include <sys/types.h>
#include <pwd.h>
#include <stdarg.h>

#include "xmalloc.h"
38 39 40 41
#include "ssh.h"
#include "ssh1.h"
#include "packet.h"
#include "log.h"
42
#include "buffer.h"
43 44
#include "servconf.h"
#include "uidswap.h"
45 46
#include "key.h"
#include "hostfile.h"
47 48 49
#include "auth.h"

#ifdef KRB5
50
#include <errno.h>
51 52
#include <unistd.h>
#include <string.h>
53 54 55 56 57 58 59 60 61
#include <krb5.h>

extern ServerOptions	 options;

static int
krb5_init(void *context)
{
	Authctxt *authctxt = (Authctxt *)context;
	krb5_error_code problem;
62

63 64 65 66 67 68 69 70 71 72 73
	if (authctxt->krb5_ctx == NULL) {
		problem = krb5_init_context(&authctxt->krb5_ctx);
		if (problem)
			return (problem);
	}
	return (0);
}

int
auth_krb5_password(Authctxt *authctxt, const char *password)
{
74 75 76
#ifndef HEIMDAL
	krb5_creds creds;
	krb5_principal server;
77
#endif
78
	krb5_error_code problem;
79
	krb5_ccache ccache = NULL;
80
	int len;
81
	char *client, *platform_client;
82
	const char *errmsg;
83 84 85 86

	/* get platform-specific kerberos client principal name (if it exists) */
	platform_client = platform_krb5_get_principal_name(authctxt->pw->pw_name);
	client = platform_client ? platform_client : authctxt->pw->pw_name;
87

88
	temporarily_use_uid(authctxt->pw);
89

90 91 92
	problem = krb5_init(authctxt);
	if (problem)
		goto out;
93

94
	problem = krb5_parse_name(authctxt->krb5_ctx, client,
95 96 97
		    &authctxt->krb5_user);
	if (problem)
		goto out;
98

99
#ifdef HEIMDAL
100
# ifdef HAVE_KRB5_CC_NEW_UNIQUE
101 102
	problem = krb5_cc_new_unique(authctxt->krb5_ctx,
	     krb5_mcc_ops.prefix, NULL, &ccache);
103 104 105
# else
	problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_mcc_ops, &ccache);
# endif
106 107
	if (problem)
		goto out;
108

109 110
	problem = krb5_cc_initialize(authctxt->krb5_ctx, ccache,
		authctxt->krb5_user);
111 112
	if (problem)
		goto out;
113 114

	restore_uid();
115

116
	problem = krb5_verify_user(authctxt->krb5_ctx, authctxt->krb5_user,
117
	    ccache, password, 1, NULL);
118

119 120
	temporarily_use_uid(authctxt->pw);

121 122
	if (problem)
		goto out;
Darren Tucker's avatar
Darren Tucker committed
123

124
# ifdef HAVE_KRB5_CC_NEW_UNIQUE
125 126
	problem = krb5_cc_new_unique(authctxt->krb5_ctx,
	     krb5_fcc_ops.prefix, NULL, &authctxt->krb5_fwd_ccache);
127 128 129 130
# else
	problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_fcc_ops,
	    &authctxt->krb5_fwd_ccache);
# endif
131 132 133 134 135 136 137 138 139
	if (problem)
		goto out;

	problem = krb5_cc_copy_cache(authctxt->krb5_ctx, ccache,
	    authctxt->krb5_fwd_ccache);
	krb5_cc_destroy(authctxt->krb5_ctx, ccache);
	ccache = NULL;
	if (problem)
		goto out;
140

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
#else
	problem = krb5_get_init_creds_password(authctxt->krb5_ctx, &creds,
	    authctxt->krb5_user, (char *)password, NULL, NULL, 0, NULL, NULL);
	if (problem)
		goto out;

	problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL,
	    KRB5_NT_SRV_HST, &server);
	if (problem)
		goto out;

	restore_uid();
	problem = krb5_verify_init_creds(authctxt->krb5_ctx, &creds, server,
	    NULL, NULL, NULL);
	krb5_free_principal(authctxt->krb5_ctx, server);
	temporarily_use_uid(authctxt->pw);
	if (problem)
		goto out;
159

160 161
	if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user,
	    authctxt->pw->pw_name)) {
Ben Lindstrom's avatar
Ben Lindstrom committed
162
		problem = -1;
163
		goto out;
164
	}
165

166
	problem = ssh_krb5_cc_gen(authctxt->krb5_ctx, &authctxt->krb5_fwd_ccache);
167 168 169 170 171 172 173
	if (problem)
		goto out;

	problem = krb5_cc_initialize(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache,
				     authctxt->krb5_user);
	if (problem)
		goto out;
174

175 176 177 178
	problem= krb5_cc_store_cred(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache,
				 &creds);
	if (problem)
		goto out;
179
#endif
180

181
	authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
182

183 184 185 186 187
	len = strlen(authctxt->krb5_ticket_file) + 6;
	authctxt->krb5_ccname = xmalloc(len);
	snprintf(authctxt->krb5_ccname, len, "FILE:%s",
	    authctxt->krb5_ticket_file);

188 189 190 191 192
#ifdef USE_PAM
	if (options.use_pam)
		do_pam_putenv("KRB5CCNAME", authctxt->krb5_ccname);
#endif

193 194
 out:
	restore_uid();
195
	
196
	free(platform_client);
197

198
	if (problem) {
199 200 201
		if (ccache)
			krb5_cc_destroy(authctxt->krb5_ctx, ccache);

202 203 204 205 206 207 208
		if (authctxt->krb5_ctx != NULL && problem!=-1) {
			errmsg = krb5_get_error_message(authctxt->krb5_ctx,
			    problem);
 			debug("Kerberos password authentication failed: %s",
			    errmsg);
			krb5_free_error_message(authctxt->krb5_ctx, errmsg);
		} else
209 210
			debug("Kerberos password authentication failed: %d",
			    problem);
211

212
		krb5_cleanup_proc(authctxt);
213

214 215 216 217 218
		if (options.kerberos_or_local_passwd)
			return (-1);
		else
			return (0);
	}
219
	return (authctxt->valid ? 1 : 0);
220 221 222
}

void
223
krb5_cleanup_proc(Authctxt *authctxt)
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
{
	debug("krb5_cleanup_proc called");
	if (authctxt->krb5_fwd_ccache) {
		krb5_cc_destroy(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
		authctxt->krb5_fwd_ccache = NULL;
	}
	if (authctxt->krb5_user) {
		krb5_free_principal(authctxt->krb5_ctx, authctxt->krb5_user);
		authctxt->krb5_user = NULL;
	}
	if (authctxt->krb5_ctx) {
		krb5_free_context(authctxt->krb5_ctx);
		authctxt->krb5_ctx = NULL;
	}
}

240 241 242
#ifndef HEIMDAL
krb5_error_code
ssh_krb5_cc_gen(krb5_context ctx, krb5_ccache *ccache) {
243
	int tmpfd, ret, oerrno;
244 245 246 247 248
	char ccname[40];
	mode_t old_umask;

	ret = snprintf(ccname, sizeof(ccname),
	    "FILE:/tmp/krb5cc_%d_XXXXXXXXXX", geteuid());
249
	if (ret < 0 || (size_t)ret >= sizeof(ccname))
250
		return ENOMEM;
251 252 253

	old_umask = umask(0177);
	tmpfd = mkstemp(ccname + strlen("FILE:"));
254
	oerrno = errno;
255 256
	umask(old_umask);
	if (tmpfd == -1) {
257 258
		logit("mkstemp(): %.100s", strerror(oerrno));
		return oerrno;
259 260 261
	}

	if (fchmod(tmpfd,S_IRUSR | S_IWUSR) == -1) {
262 263
		oerrno = errno;
		logit("fchmod(): %.100s", strerror(oerrno));
264
		close(tmpfd);
265
		return oerrno;
266 267 268 269 270 271
	}
	close(tmpfd);

	return (krb5_cc_resolve(ctx, ccname, ccache));
}
#endif /* !HEIMDAL */
272
#endif /* KRB5 */