1*ba1276acSMatthew Dillon /* $OpenBSD: auth-krb5.c,v 1.24 2021/04/03 06:18:40 djm Exp $ */
2*ba1276acSMatthew Dillon /*
3*ba1276acSMatthew Dillon * Kerberos v5 authentication and ticket-passing routines.
4*ba1276acSMatthew Dillon *
5*ba1276acSMatthew Dillon * From: FreeBSD: src/crypto/openssh/auth-krb5.c,v 1.6 2001/02/13 16:58:04 assar
6*ba1276acSMatthew Dillon */
7*ba1276acSMatthew Dillon /*
8*ba1276acSMatthew Dillon * Copyright (c) 2002 Daniel Kouril. All rights reserved.
9*ba1276acSMatthew Dillon *
10*ba1276acSMatthew Dillon * Redistribution and use in source and binary forms, with or without
11*ba1276acSMatthew Dillon * modification, are permitted provided that the following conditions
12*ba1276acSMatthew Dillon * are met:
13*ba1276acSMatthew Dillon * 1. Redistributions of source code must retain the above copyright
14*ba1276acSMatthew Dillon * notice, this list of conditions and the following disclaimer.
15*ba1276acSMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright
16*ba1276acSMatthew Dillon * notice, this list of conditions and the following disclaimer in the
17*ba1276acSMatthew Dillon * documentation and/or other materials provided with the distribution.
18*ba1276acSMatthew Dillon *
19*ba1276acSMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20*ba1276acSMatthew Dillon * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21*ba1276acSMatthew Dillon * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22*ba1276acSMatthew Dillon * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23*ba1276acSMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24*ba1276acSMatthew Dillon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*ba1276acSMatthew Dillon * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*ba1276acSMatthew Dillon * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*ba1276acSMatthew Dillon * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28*ba1276acSMatthew Dillon * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*ba1276acSMatthew Dillon */
30*ba1276acSMatthew Dillon
31*ba1276acSMatthew Dillon #include "includes.h"
32*ba1276acSMatthew Dillon
33*ba1276acSMatthew Dillon #include <sys/types.h>
34*ba1276acSMatthew Dillon #include <pwd.h>
35*ba1276acSMatthew Dillon #include <stdarg.h>
36*ba1276acSMatthew Dillon
37*ba1276acSMatthew Dillon #include "xmalloc.h"
38*ba1276acSMatthew Dillon #include "ssh.h"
39*ba1276acSMatthew Dillon #include "packet.h"
40*ba1276acSMatthew Dillon #include "log.h"
41*ba1276acSMatthew Dillon #include "sshbuf.h"
42*ba1276acSMatthew Dillon #include "sshkey.h"
43*ba1276acSMatthew Dillon #include "misc.h"
44*ba1276acSMatthew Dillon #include "servconf.h"
45*ba1276acSMatthew Dillon #include "uidswap.h"
46*ba1276acSMatthew Dillon #include "hostfile.h"
47*ba1276acSMatthew Dillon #include "auth.h"
48*ba1276acSMatthew Dillon
49*ba1276acSMatthew Dillon #ifdef KRB5
50*ba1276acSMatthew Dillon #include <errno.h>
51*ba1276acSMatthew Dillon #include <unistd.h>
52*ba1276acSMatthew Dillon #include <string.h>
53*ba1276acSMatthew Dillon #include <krb5.h>
54*ba1276acSMatthew Dillon
55*ba1276acSMatthew Dillon extern ServerOptions options;
56*ba1276acSMatthew Dillon
57*ba1276acSMatthew Dillon static int
krb5_init(void * context)58*ba1276acSMatthew Dillon krb5_init(void *context)
59*ba1276acSMatthew Dillon {
60*ba1276acSMatthew Dillon Authctxt *authctxt = (Authctxt *)context;
61*ba1276acSMatthew Dillon krb5_error_code problem;
62*ba1276acSMatthew Dillon
63*ba1276acSMatthew Dillon if (authctxt->krb5_ctx == NULL) {
64*ba1276acSMatthew Dillon problem = krb5_init_context(&authctxt->krb5_ctx);
65*ba1276acSMatthew Dillon if (problem)
66*ba1276acSMatthew Dillon return (problem);
67*ba1276acSMatthew Dillon }
68*ba1276acSMatthew Dillon return (0);
69*ba1276acSMatthew Dillon }
70*ba1276acSMatthew Dillon
71*ba1276acSMatthew Dillon int
auth_krb5_password(Authctxt * authctxt,const char * password)72*ba1276acSMatthew Dillon auth_krb5_password(Authctxt *authctxt, const char *password)
73*ba1276acSMatthew Dillon {
74*ba1276acSMatthew Dillon #ifndef HEIMDAL
75*ba1276acSMatthew Dillon krb5_creds creds;
76*ba1276acSMatthew Dillon krb5_principal server;
77*ba1276acSMatthew Dillon #endif
78*ba1276acSMatthew Dillon krb5_error_code problem;
79*ba1276acSMatthew Dillon krb5_ccache ccache = NULL;
80*ba1276acSMatthew Dillon int len;
81*ba1276acSMatthew Dillon char *client, *platform_client;
82*ba1276acSMatthew Dillon const char *errmsg;
83*ba1276acSMatthew Dillon
84*ba1276acSMatthew Dillon /* get platform-specific kerberos client principal name (if it exists) */
85*ba1276acSMatthew Dillon platform_client = platform_krb5_get_principal_name(authctxt->pw->pw_name);
86*ba1276acSMatthew Dillon client = platform_client ? platform_client : authctxt->pw->pw_name;
87*ba1276acSMatthew Dillon
88*ba1276acSMatthew Dillon temporarily_use_uid(authctxt->pw);
89*ba1276acSMatthew Dillon
90*ba1276acSMatthew Dillon problem = krb5_init(authctxt);
91*ba1276acSMatthew Dillon if (problem)
92*ba1276acSMatthew Dillon goto out;
93*ba1276acSMatthew Dillon
94*ba1276acSMatthew Dillon problem = krb5_parse_name(authctxt->krb5_ctx, client,
95*ba1276acSMatthew Dillon &authctxt->krb5_user);
96*ba1276acSMatthew Dillon if (problem)
97*ba1276acSMatthew Dillon goto out;
98*ba1276acSMatthew Dillon
99*ba1276acSMatthew Dillon #ifdef HEIMDAL
100*ba1276acSMatthew Dillon # ifdef HAVE_KRB5_CC_NEW_UNIQUE
101*ba1276acSMatthew Dillon problem = krb5_cc_new_unique(authctxt->krb5_ctx,
102*ba1276acSMatthew Dillon krb5_mcc_ops.prefix, NULL, &ccache);
103*ba1276acSMatthew Dillon # else
104*ba1276acSMatthew Dillon problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_mcc_ops, &ccache);
105*ba1276acSMatthew Dillon # endif
106*ba1276acSMatthew Dillon if (problem)
107*ba1276acSMatthew Dillon goto out;
108*ba1276acSMatthew Dillon
109*ba1276acSMatthew Dillon problem = krb5_cc_initialize(authctxt->krb5_ctx, ccache,
110*ba1276acSMatthew Dillon authctxt->krb5_user);
111*ba1276acSMatthew Dillon if (problem)
112*ba1276acSMatthew Dillon goto out;
113*ba1276acSMatthew Dillon
114*ba1276acSMatthew Dillon restore_uid();
115*ba1276acSMatthew Dillon
116*ba1276acSMatthew Dillon problem = krb5_verify_user(authctxt->krb5_ctx, authctxt->krb5_user,
117*ba1276acSMatthew Dillon ccache, password, 1, NULL);
118*ba1276acSMatthew Dillon
119*ba1276acSMatthew Dillon temporarily_use_uid(authctxt->pw);
120*ba1276acSMatthew Dillon
121*ba1276acSMatthew Dillon if (problem)
122*ba1276acSMatthew Dillon goto out;
123*ba1276acSMatthew Dillon
124*ba1276acSMatthew Dillon # ifdef HAVE_KRB5_CC_NEW_UNIQUE
125*ba1276acSMatthew Dillon problem = krb5_cc_new_unique(authctxt->krb5_ctx,
126*ba1276acSMatthew Dillon krb5_fcc_ops.prefix, NULL, &authctxt->krb5_fwd_ccache);
127*ba1276acSMatthew Dillon # else
128*ba1276acSMatthew Dillon problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_fcc_ops,
129*ba1276acSMatthew Dillon &authctxt->krb5_fwd_ccache);
130*ba1276acSMatthew Dillon # endif
131*ba1276acSMatthew Dillon if (problem)
132*ba1276acSMatthew Dillon goto out;
133*ba1276acSMatthew Dillon
134*ba1276acSMatthew Dillon problem = krb5_cc_copy_cache(authctxt->krb5_ctx, ccache,
135*ba1276acSMatthew Dillon authctxt->krb5_fwd_ccache);
136*ba1276acSMatthew Dillon krb5_cc_destroy(authctxt->krb5_ctx, ccache);
137*ba1276acSMatthew Dillon ccache = NULL;
138*ba1276acSMatthew Dillon if (problem)
139*ba1276acSMatthew Dillon goto out;
140*ba1276acSMatthew Dillon
141*ba1276acSMatthew Dillon #else
142*ba1276acSMatthew Dillon problem = krb5_get_init_creds_password(authctxt->krb5_ctx, &creds,
143*ba1276acSMatthew Dillon authctxt->krb5_user, (char *)password, NULL, NULL, 0, NULL, NULL);
144*ba1276acSMatthew Dillon if (problem)
145*ba1276acSMatthew Dillon goto out;
146*ba1276acSMatthew Dillon
147*ba1276acSMatthew Dillon problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL,
148*ba1276acSMatthew Dillon KRB5_NT_SRV_HST, &server);
149*ba1276acSMatthew Dillon if (problem)
150*ba1276acSMatthew Dillon goto out;
151*ba1276acSMatthew Dillon
152*ba1276acSMatthew Dillon restore_uid();
153*ba1276acSMatthew Dillon problem = krb5_verify_init_creds(authctxt->krb5_ctx, &creds, server,
154*ba1276acSMatthew Dillon NULL, NULL, NULL);
155*ba1276acSMatthew Dillon krb5_free_principal(authctxt->krb5_ctx, server);
156*ba1276acSMatthew Dillon temporarily_use_uid(authctxt->pw);
157*ba1276acSMatthew Dillon if (problem)
158*ba1276acSMatthew Dillon goto out;
159*ba1276acSMatthew Dillon
160*ba1276acSMatthew Dillon if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user,
161*ba1276acSMatthew Dillon authctxt->pw->pw_name)) {
162*ba1276acSMatthew Dillon problem = -1;
163*ba1276acSMatthew Dillon goto out;
164*ba1276acSMatthew Dillon }
165*ba1276acSMatthew Dillon
166*ba1276acSMatthew Dillon problem = ssh_krb5_cc_gen(authctxt->krb5_ctx,
167*ba1276acSMatthew Dillon &authctxt->krb5_fwd_ccache);
168*ba1276acSMatthew Dillon if (problem)
169*ba1276acSMatthew Dillon goto out;
170*ba1276acSMatthew Dillon
171*ba1276acSMatthew Dillon problem = krb5_cc_initialize(authctxt->krb5_ctx,
172*ba1276acSMatthew Dillon authctxt->krb5_fwd_ccache, authctxt->krb5_user);
173*ba1276acSMatthew Dillon if (problem)
174*ba1276acSMatthew Dillon goto out;
175*ba1276acSMatthew Dillon
176*ba1276acSMatthew Dillon problem = krb5_cc_store_cred(authctxt->krb5_ctx,
177*ba1276acSMatthew Dillon authctxt->krb5_fwd_ccache, &creds);
178*ba1276acSMatthew Dillon if (problem)
179*ba1276acSMatthew Dillon goto out;
180*ba1276acSMatthew Dillon #endif
181*ba1276acSMatthew Dillon
182*ba1276acSMatthew Dillon authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
183*ba1276acSMatthew Dillon
184*ba1276acSMatthew Dillon len = strlen(authctxt->krb5_ticket_file) + 6;
185*ba1276acSMatthew Dillon authctxt->krb5_ccname = xmalloc(len);
186*ba1276acSMatthew Dillon snprintf(authctxt->krb5_ccname, len, "FILE:%s",
187*ba1276acSMatthew Dillon authctxt->krb5_ticket_file);
188*ba1276acSMatthew Dillon
189*ba1276acSMatthew Dillon #ifdef USE_PAM
190*ba1276acSMatthew Dillon if (options.use_pam)
191*ba1276acSMatthew Dillon do_pam_putenv("KRB5CCNAME", authctxt->krb5_ccname);
192*ba1276acSMatthew Dillon #endif
193*ba1276acSMatthew Dillon
194*ba1276acSMatthew Dillon out:
195*ba1276acSMatthew Dillon restore_uid();
196*ba1276acSMatthew Dillon
197*ba1276acSMatthew Dillon free(platform_client);
198*ba1276acSMatthew Dillon
199*ba1276acSMatthew Dillon if (problem) {
200*ba1276acSMatthew Dillon if (ccache)
201*ba1276acSMatthew Dillon krb5_cc_destroy(authctxt->krb5_ctx, ccache);
202*ba1276acSMatthew Dillon
203*ba1276acSMatthew Dillon if (authctxt->krb5_ctx != NULL && problem!=-1) {
204*ba1276acSMatthew Dillon errmsg = krb5_get_error_message(authctxt->krb5_ctx,
205*ba1276acSMatthew Dillon problem);
206*ba1276acSMatthew Dillon debug("Kerberos password authentication failed: %s",
207*ba1276acSMatthew Dillon errmsg);
208*ba1276acSMatthew Dillon krb5_free_error_message(authctxt->krb5_ctx, errmsg);
209*ba1276acSMatthew Dillon } else
210*ba1276acSMatthew Dillon debug("Kerberos password authentication failed: %d",
211*ba1276acSMatthew Dillon problem);
212*ba1276acSMatthew Dillon
213*ba1276acSMatthew Dillon krb5_cleanup_proc(authctxt);
214*ba1276acSMatthew Dillon
215*ba1276acSMatthew Dillon if (options.kerberos_or_local_passwd)
216*ba1276acSMatthew Dillon return (-1);
217*ba1276acSMatthew Dillon else
218*ba1276acSMatthew Dillon return (0);
219*ba1276acSMatthew Dillon }
220*ba1276acSMatthew Dillon return (authctxt->valid ? 1 : 0);
221*ba1276acSMatthew Dillon }
222*ba1276acSMatthew Dillon
223*ba1276acSMatthew Dillon void
krb5_cleanup_proc(Authctxt * authctxt)224*ba1276acSMatthew Dillon krb5_cleanup_proc(Authctxt *authctxt)
225*ba1276acSMatthew Dillon {
226*ba1276acSMatthew Dillon debug("krb5_cleanup_proc called");
227*ba1276acSMatthew Dillon if (authctxt->krb5_fwd_ccache) {
228*ba1276acSMatthew Dillon krb5_cc_destroy(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
229*ba1276acSMatthew Dillon authctxt->krb5_fwd_ccache = NULL;
230*ba1276acSMatthew Dillon }
231*ba1276acSMatthew Dillon if (authctxt->krb5_user) {
232*ba1276acSMatthew Dillon krb5_free_principal(authctxt->krb5_ctx, authctxt->krb5_user);
233*ba1276acSMatthew Dillon authctxt->krb5_user = NULL;
234*ba1276acSMatthew Dillon }
235*ba1276acSMatthew Dillon if (authctxt->krb5_ctx) {
236*ba1276acSMatthew Dillon krb5_free_context(authctxt->krb5_ctx);
237*ba1276acSMatthew Dillon authctxt->krb5_ctx = NULL;
238*ba1276acSMatthew Dillon }
239*ba1276acSMatthew Dillon }
240*ba1276acSMatthew Dillon
241*ba1276acSMatthew Dillon #ifndef HEIMDAL
242*ba1276acSMatthew Dillon krb5_error_code
ssh_krb5_cc_gen(krb5_context ctx,krb5_ccache * ccache)243*ba1276acSMatthew Dillon ssh_krb5_cc_gen(krb5_context ctx, krb5_ccache *ccache) {
244*ba1276acSMatthew Dillon int tmpfd, ret, oerrno;
245*ba1276acSMatthew Dillon char ccname[40];
246*ba1276acSMatthew Dillon mode_t old_umask;
247*ba1276acSMatthew Dillon
248*ba1276acSMatthew Dillon ret = snprintf(ccname, sizeof(ccname),
249*ba1276acSMatthew Dillon "FILE:/tmp/krb5cc_%d_XXXXXXXXXX", geteuid());
250*ba1276acSMatthew Dillon if (ret < 0 || (size_t)ret >= sizeof(ccname))
251*ba1276acSMatthew Dillon return ENOMEM;
252*ba1276acSMatthew Dillon
253*ba1276acSMatthew Dillon old_umask = umask(0177);
254*ba1276acSMatthew Dillon tmpfd = mkstemp(ccname + strlen("FILE:"));
255*ba1276acSMatthew Dillon oerrno = errno;
256*ba1276acSMatthew Dillon umask(old_umask);
257*ba1276acSMatthew Dillon if (tmpfd == -1) {
258*ba1276acSMatthew Dillon logit("mkstemp(): %.100s", strerror(oerrno));
259*ba1276acSMatthew Dillon return oerrno;
260*ba1276acSMatthew Dillon }
261*ba1276acSMatthew Dillon
262*ba1276acSMatthew Dillon if (fchmod(tmpfd,S_IRUSR | S_IWUSR) == -1) {
263*ba1276acSMatthew Dillon oerrno = errno;
264*ba1276acSMatthew Dillon logit("fchmod(): %.100s", strerror(oerrno));
265*ba1276acSMatthew Dillon close(tmpfd);
266*ba1276acSMatthew Dillon return oerrno;
267*ba1276acSMatthew Dillon }
268*ba1276acSMatthew Dillon close(tmpfd);
269*ba1276acSMatthew Dillon
270*ba1276acSMatthew Dillon return (krb5_cc_resolve(ctx, ccname, ccache));
271*ba1276acSMatthew Dillon }
272*ba1276acSMatthew Dillon #endif /* !HEIMDAL */
273*ba1276acSMatthew Dillon #endif /* KRB5 */
274