1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * Kerberos v5 authentication and ticket-passing routines.
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * $FreeBSD: src/crypto/openssh/auth-krb5.c,v 1.6 2001/02/13 16:58:04 assar Exp $
5*7c478bd9Sstevel@tonic-gate */
6*7c478bd9Sstevel@tonic-gate /*
7*7c478bd9Sstevel@tonic-gate * Copyright (c) 2002 Daniel Kouril. All rights reserved.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
10*7c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions
11*7c478bd9Sstevel@tonic-gate * are met:
12*7c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
13*7c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
14*7c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
15*7c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
16*7c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
17*7c478bd9Sstevel@tonic-gate *
18*7c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19*7c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20*7c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21*7c478bd9Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22*7c478bd9Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23*7c478bd9Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*7c478bd9Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*7c478bd9Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*7c478bd9Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27*7c478bd9Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*7c478bd9Sstevel@tonic-gate */
29*7c478bd9Sstevel@tonic-gate
30*7c478bd9Sstevel@tonic-gate #include "includes.h"
31*7c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: auth-krb5.c,v 1.9 2002/09/09 06:48:06 itojun Exp $");
32*7c478bd9Sstevel@tonic-gate
33*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
34*7c478bd9Sstevel@tonic-gate
35*7c478bd9Sstevel@tonic-gate #include "ssh.h"
36*7c478bd9Sstevel@tonic-gate #include "ssh1.h"
37*7c478bd9Sstevel@tonic-gate #include "packet.h"
38*7c478bd9Sstevel@tonic-gate #include "xmalloc.h"
39*7c478bd9Sstevel@tonic-gate #include "log.h"
40*7c478bd9Sstevel@tonic-gate #include "servconf.h"
41*7c478bd9Sstevel@tonic-gate #include "uidswap.h"
42*7c478bd9Sstevel@tonic-gate #include "auth.h"
43*7c478bd9Sstevel@tonic-gate
44*7c478bd9Sstevel@tonic-gate #ifdef KRB5
45*7c478bd9Sstevel@tonic-gate #include <krb5.h>
46*7c478bd9Sstevel@tonic-gate #ifndef HEIMDAL
47*7c478bd9Sstevel@tonic-gate #define krb5_get_err_text(context,code) error_message(code)
48*7c478bd9Sstevel@tonic-gate #endif /* !HEIMDAL */
49*7c478bd9Sstevel@tonic-gate
50*7c478bd9Sstevel@tonic-gate extern ServerOptions options;
51*7c478bd9Sstevel@tonic-gate
52*7c478bd9Sstevel@tonic-gate static int
krb5_init(void * context)53*7c478bd9Sstevel@tonic-gate krb5_init(void *context)
54*7c478bd9Sstevel@tonic-gate {
55*7c478bd9Sstevel@tonic-gate Authctxt *authctxt = (Authctxt *)context;
56*7c478bd9Sstevel@tonic-gate krb5_error_code problem;
57*7c478bd9Sstevel@tonic-gate static int cleanup_registered = 0;
58*7c478bd9Sstevel@tonic-gate
59*7c478bd9Sstevel@tonic-gate if (authctxt->krb5_ctx == NULL) {
60*7c478bd9Sstevel@tonic-gate problem = krb5_init_context(&authctxt->krb5_ctx);
61*7c478bd9Sstevel@tonic-gate if (problem)
62*7c478bd9Sstevel@tonic-gate return (problem);
63*7c478bd9Sstevel@tonic-gate krb5_init_ets(authctxt->krb5_ctx);
64*7c478bd9Sstevel@tonic-gate }
65*7c478bd9Sstevel@tonic-gate if (!cleanup_registered) {
66*7c478bd9Sstevel@tonic-gate fatal_add_cleanup(krb5_cleanup_proc, authctxt);
67*7c478bd9Sstevel@tonic-gate cleanup_registered = 1;
68*7c478bd9Sstevel@tonic-gate }
69*7c478bd9Sstevel@tonic-gate return (0);
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate
72*7c478bd9Sstevel@tonic-gate /*
73*7c478bd9Sstevel@tonic-gate * Try krb5 authentication. server_user is passed for logging purposes
74*7c478bd9Sstevel@tonic-gate * only, in auth is received ticket, in client is returned principal
75*7c478bd9Sstevel@tonic-gate * from the ticket
76*7c478bd9Sstevel@tonic-gate */
77*7c478bd9Sstevel@tonic-gate int
auth_krb5(Authctxt * authctxt,krb5_data * auth,char ** client,krb5_data * reply)78*7c478bd9Sstevel@tonic-gate auth_krb5(Authctxt *authctxt, krb5_data *auth, char **client, krb5_data *reply)
79*7c478bd9Sstevel@tonic-gate {
80*7c478bd9Sstevel@tonic-gate krb5_error_code problem;
81*7c478bd9Sstevel@tonic-gate krb5_principal server;
82*7c478bd9Sstevel@tonic-gate krb5_ticket *ticket;
83*7c478bd9Sstevel@tonic-gate int fd, ret;
84*7c478bd9Sstevel@tonic-gate
85*7c478bd9Sstevel@tonic-gate ret = 0;
86*7c478bd9Sstevel@tonic-gate server = NULL;
87*7c478bd9Sstevel@tonic-gate ticket = NULL;
88*7c478bd9Sstevel@tonic-gate reply->length = 0;
89*7c478bd9Sstevel@tonic-gate
90*7c478bd9Sstevel@tonic-gate problem = krb5_init(authctxt);
91*7c478bd9Sstevel@tonic-gate if (problem)
92*7c478bd9Sstevel@tonic-gate goto err;
93*7c478bd9Sstevel@tonic-gate
94*7c478bd9Sstevel@tonic-gate problem = krb5_auth_con_init(authctxt->krb5_ctx,
95*7c478bd9Sstevel@tonic-gate &authctxt->krb5_auth_ctx);
96*7c478bd9Sstevel@tonic-gate if (problem)
97*7c478bd9Sstevel@tonic-gate goto err;
98*7c478bd9Sstevel@tonic-gate
99*7c478bd9Sstevel@tonic-gate fd = packet_get_connection_in();
100*7c478bd9Sstevel@tonic-gate #ifdef HEIMDAL
101*7c478bd9Sstevel@tonic-gate problem = krb5_auth_con_setaddrs_from_fd(authctxt->krb5_ctx,
102*7c478bd9Sstevel@tonic-gate authctxt->krb5_auth_ctx, &fd);
103*7c478bd9Sstevel@tonic-gate #else
104*7c478bd9Sstevel@tonic-gate problem = krb5_auth_con_genaddrs(authctxt->krb5_ctx,
105*7c478bd9Sstevel@tonic-gate authctxt->krb5_auth_ctx,fd,
106*7c478bd9Sstevel@tonic-gate KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR |
107*7c478bd9Sstevel@tonic-gate KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR);
108*7c478bd9Sstevel@tonic-gate #endif
109*7c478bd9Sstevel@tonic-gate if (problem)
110*7c478bd9Sstevel@tonic-gate goto err;
111*7c478bd9Sstevel@tonic-gate
112*7c478bd9Sstevel@tonic-gate problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL ,
113*7c478bd9Sstevel@tonic-gate KRB5_NT_SRV_HST, &server);
114*7c478bd9Sstevel@tonic-gate if (problem)
115*7c478bd9Sstevel@tonic-gate goto err;
116*7c478bd9Sstevel@tonic-gate
117*7c478bd9Sstevel@tonic-gate problem = krb5_rd_req(authctxt->krb5_ctx, &authctxt->krb5_auth_ctx,
118*7c478bd9Sstevel@tonic-gate auth, server, NULL, NULL, &ticket);
119*7c478bd9Sstevel@tonic-gate if (problem)
120*7c478bd9Sstevel@tonic-gate goto err;
121*7c478bd9Sstevel@tonic-gate
122*7c478bd9Sstevel@tonic-gate #ifdef HEIMDAL
123*7c478bd9Sstevel@tonic-gate problem = krb5_copy_principal(authctxt->krb5_ctx, ticket->client,
124*7c478bd9Sstevel@tonic-gate &authctxt->krb5_user);
125*7c478bd9Sstevel@tonic-gate #else
126*7c478bd9Sstevel@tonic-gate problem = krb5_copy_principal(authctxt->krb5_ctx,
127*7c478bd9Sstevel@tonic-gate ticket->enc_part2->client,
128*7c478bd9Sstevel@tonic-gate &authctxt->krb5_user);
129*7c478bd9Sstevel@tonic-gate #endif
130*7c478bd9Sstevel@tonic-gate if (problem)
131*7c478bd9Sstevel@tonic-gate goto err;
132*7c478bd9Sstevel@tonic-gate
133*7c478bd9Sstevel@tonic-gate /* if client wants mutual auth */
134*7c478bd9Sstevel@tonic-gate problem = krb5_mk_rep(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
135*7c478bd9Sstevel@tonic-gate reply);
136*7c478bd9Sstevel@tonic-gate if (problem)
137*7c478bd9Sstevel@tonic-gate goto err;
138*7c478bd9Sstevel@tonic-gate
139*7c478bd9Sstevel@tonic-gate /* Check .k5login authorization now. */
140*7c478bd9Sstevel@tonic-gate if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user,
141*7c478bd9Sstevel@tonic-gate authctxt->pw->pw_name))
142*7c478bd9Sstevel@tonic-gate goto err;
143*7c478bd9Sstevel@tonic-gate
144*7c478bd9Sstevel@tonic-gate if (client)
145*7c478bd9Sstevel@tonic-gate krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user,
146*7c478bd9Sstevel@tonic-gate client);
147*7c478bd9Sstevel@tonic-gate
148*7c478bd9Sstevel@tonic-gate ret = 1;
149*7c478bd9Sstevel@tonic-gate err:
150*7c478bd9Sstevel@tonic-gate if (server)
151*7c478bd9Sstevel@tonic-gate krb5_free_principal(authctxt->krb5_ctx, server);
152*7c478bd9Sstevel@tonic-gate if (ticket)
153*7c478bd9Sstevel@tonic-gate krb5_free_ticket(authctxt->krb5_ctx, ticket);
154*7c478bd9Sstevel@tonic-gate if (!ret && reply->length) {
155*7c478bd9Sstevel@tonic-gate xfree(reply->data);
156*7c478bd9Sstevel@tonic-gate memset(reply, 0, sizeof(*reply));
157*7c478bd9Sstevel@tonic-gate }
158*7c478bd9Sstevel@tonic-gate
159*7c478bd9Sstevel@tonic-gate if (problem) {
160*7c478bd9Sstevel@tonic-gate if (authctxt->krb5_ctx != NULL)
161*7c478bd9Sstevel@tonic-gate debug("Kerberos v5 authentication failed: %s",
162*7c478bd9Sstevel@tonic-gate krb5_get_err_text(authctxt->krb5_ctx, problem));
163*7c478bd9Sstevel@tonic-gate else
164*7c478bd9Sstevel@tonic-gate debug("Kerberos v5 authentication failed: %d",
165*7c478bd9Sstevel@tonic-gate problem);
166*7c478bd9Sstevel@tonic-gate }
167*7c478bd9Sstevel@tonic-gate
168*7c478bd9Sstevel@tonic-gate return (ret);
169*7c478bd9Sstevel@tonic-gate }
170*7c478bd9Sstevel@tonic-gate
171*7c478bd9Sstevel@tonic-gate int
auth_krb5_tgt(Authctxt * authctxt,krb5_data * tgt)172*7c478bd9Sstevel@tonic-gate auth_krb5_tgt(Authctxt *authctxt, krb5_data *tgt)
173*7c478bd9Sstevel@tonic-gate {
174*7c478bd9Sstevel@tonic-gate krb5_error_code problem;
175*7c478bd9Sstevel@tonic-gate krb5_ccache ccache = NULL;
176*7c478bd9Sstevel@tonic-gate char *pname;
177*7c478bd9Sstevel@tonic-gate krb5_creds **creds;
178*7c478bd9Sstevel@tonic-gate
179*7c478bd9Sstevel@tonic-gate if (authctxt->pw == NULL || authctxt->krb5_user == NULL)
180*7c478bd9Sstevel@tonic-gate return (0);
181*7c478bd9Sstevel@tonic-gate
182*7c478bd9Sstevel@tonic-gate temporarily_use_uid(authctxt->pw);
183*7c478bd9Sstevel@tonic-gate
184*7c478bd9Sstevel@tonic-gate #ifdef HEIMDAL
185*7c478bd9Sstevel@tonic-gate problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_fcc_ops, &ccache);
186*7c478bd9Sstevel@tonic-gate #else
187*7c478bd9Sstevel@tonic-gate {
188*7c478bd9Sstevel@tonic-gate char ccname[40];
189*7c478bd9Sstevel@tonic-gate int tmpfd;
190*7c478bd9Sstevel@tonic-gate
191*7c478bd9Sstevel@tonic-gate snprintf(ccname,sizeof(ccname),"FILE:/tmp/krb5cc_%d_XXXXXX",geteuid());
192*7c478bd9Sstevel@tonic-gate
193*7c478bd9Sstevel@tonic-gate if ((tmpfd = mkstemp(ccname+strlen("FILE:")))==-1) {
194*7c478bd9Sstevel@tonic-gate log("mkstemp(): %.100s", strerror(errno));
195*7c478bd9Sstevel@tonic-gate problem = errno;
196*7c478bd9Sstevel@tonic-gate goto fail;
197*7c478bd9Sstevel@tonic-gate }
198*7c478bd9Sstevel@tonic-gate if (fchmod(tmpfd,S_IRUSR | S_IWUSR) == -1) {
199*7c478bd9Sstevel@tonic-gate log("fchmod(): %.100s", strerror(errno));
200*7c478bd9Sstevel@tonic-gate close(tmpfd);
201*7c478bd9Sstevel@tonic-gate problem = errno;
202*7c478bd9Sstevel@tonic-gate goto fail;
203*7c478bd9Sstevel@tonic-gate }
204*7c478bd9Sstevel@tonic-gate close(tmpfd);
205*7c478bd9Sstevel@tonic-gate problem = krb5_cc_resolve(authctxt->krb5_ctx, ccname, &ccache);
206*7c478bd9Sstevel@tonic-gate }
207*7c478bd9Sstevel@tonic-gate #endif
208*7c478bd9Sstevel@tonic-gate if (problem)
209*7c478bd9Sstevel@tonic-gate goto fail;
210*7c478bd9Sstevel@tonic-gate
211*7c478bd9Sstevel@tonic-gate problem = krb5_cc_initialize(authctxt->krb5_ctx, ccache,
212*7c478bd9Sstevel@tonic-gate authctxt->krb5_user);
213*7c478bd9Sstevel@tonic-gate if (problem)
214*7c478bd9Sstevel@tonic-gate goto fail;
215*7c478bd9Sstevel@tonic-gate
216*7c478bd9Sstevel@tonic-gate #ifdef HEIMDAL
217*7c478bd9Sstevel@tonic-gate problem = krb5_rd_cred2(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
218*7c478bd9Sstevel@tonic-gate ccache, tgt);
219*7c478bd9Sstevel@tonic-gate if (problem)
220*7c478bd9Sstevel@tonic-gate goto fail;
221*7c478bd9Sstevel@tonic-gate #else
222*7c478bd9Sstevel@tonic-gate problem = krb5_rd_cred(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
223*7c478bd9Sstevel@tonic-gate tgt, &creds, NULL);
224*7c478bd9Sstevel@tonic-gate if (problem)
225*7c478bd9Sstevel@tonic-gate goto fail;
226*7c478bd9Sstevel@tonic-gate problem = krb5_cc_store_cred(authctxt->krb5_ctx, ccache, *creds);
227*7c478bd9Sstevel@tonic-gate if (problem)
228*7c478bd9Sstevel@tonic-gate goto fail;
229*7c478bd9Sstevel@tonic-gate #endif
230*7c478bd9Sstevel@tonic-gate
231*7c478bd9Sstevel@tonic-gate authctxt->krb5_fwd_ccache = ccache;
232*7c478bd9Sstevel@tonic-gate ccache = NULL;
233*7c478bd9Sstevel@tonic-gate
234*7c478bd9Sstevel@tonic-gate authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
235*7c478bd9Sstevel@tonic-gate
236*7c478bd9Sstevel@tonic-gate problem = krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user,
237*7c478bd9Sstevel@tonic-gate &pname);
238*7c478bd9Sstevel@tonic-gate if (problem)
239*7c478bd9Sstevel@tonic-gate goto fail;
240*7c478bd9Sstevel@tonic-gate
241*7c478bd9Sstevel@tonic-gate debug("Kerberos v5 TGT accepted (%s)", pname);
242*7c478bd9Sstevel@tonic-gate
243*7c478bd9Sstevel@tonic-gate restore_uid();
244*7c478bd9Sstevel@tonic-gate
245*7c478bd9Sstevel@tonic-gate return (1);
246*7c478bd9Sstevel@tonic-gate
247*7c478bd9Sstevel@tonic-gate fail:
248*7c478bd9Sstevel@tonic-gate if (problem)
249*7c478bd9Sstevel@tonic-gate debug("Kerberos v5 TGT passing failed: %s",
250*7c478bd9Sstevel@tonic-gate krb5_get_err_text(authctxt->krb5_ctx, problem));
251*7c478bd9Sstevel@tonic-gate if (ccache)
252*7c478bd9Sstevel@tonic-gate krb5_cc_destroy(authctxt->krb5_ctx, ccache);
253*7c478bd9Sstevel@tonic-gate
254*7c478bd9Sstevel@tonic-gate restore_uid();
255*7c478bd9Sstevel@tonic-gate
256*7c478bd9Sstevel@tonic-gate return (0);
257*7c478bd9Sstevel@tonic-gate }
258*7c478bd9Sstevel@tonic-gate
259*7c478bd9Sstevel@tonic-gate int
auth_krb5_password(Authctxt * authctxt,const char * password)260*7c478bd9Sstevel@tonic-gate auth_krb5_password(Authctxt *authctxt, const char *password)
261*7c478bd9Sstevel@tonic-gate {
262*7c478bd9Sstevel@tonic-gate #ifndef HEIMDAL
263*7c478bd9Sstevel@tonic-gate krb5_creds creds;
264*7c478bd9Sstevel@tonic-gate krb5_principal server;
265*7c478bd9Sstevel@tonic-gate char ccname[40];
266*7c478bd9Sstevel@tonic-gate int tmpfd;
267*7c478bd9Sstevel@tonic-gate #endif
268*7c478bd9Sstevel@tonic-gate krb5_error_code problem;
269*7c478bd9Sstevel@tonic-gate
270*7c478bd9Sstevel@tonic-gate if (authctxt->pw == NULL)
271*7c478bd9Sstevel@tonic-gate return (0);
272*7c478bd9Sstevel@tonic-gate
273*7c478bd9Sstevel@tonic-gate temporarily_use_uid(authctxt->pw);
274*7c478bd9Sstevel@tonic-gate
275*7c478bd9Sstevel@tonic-gate problem = krb5_init(authctxt);
276*7c478bd9Sstevel@tonic-gate if (problem)
277*7c478bd9Sstevel@tonic-gate goto out;
278*7c478bd9Sstevel@tonic-gate
279*7c478bd9Sstevel@tonic-gate problem = krb5_parse_name(authctxt->krb5_ctx, authctxt->pw->pw_name,
280*7c478bd9Sstevel@tonic-gate &authctxt->krb5_user);
281*7c478bd9Sstevel@tonic-gate if (problem)
282*7c478bd9Sstevel@tonic-gate goto out;
283*7c478bd9Sstevel@tonic-gate
284*7c478bd9Sstevel@tonic-gate #ifdef HEIMDAL
285*7c478bd9Sstevel@tonic-gate problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_mcc_ops,
286*7c478bd9Sstevel@tonic-gate &authctxt->krb5_fwd_ccache);
287*7c478bd9Sstevel@tonic-gate if (problem)
288*7c478bd9Sstevel@tonic-gate goto out;
289*7c478bd9Sstevel@tonic-gate
290*7c478bd9Sstevel@tonic-gate problem = krb5_cc_initialize(authctxt->krb5_ctx,
291*7c478bd9Sstevel@tonic-gate authctxt->krb5_fwd_ccache, authctxt->krb5_user);
292*7c478bd9Sstevel@tonic-gate if (problem)
293*7c478bd9Sstevel@tonic-gate goto out;
294*7c478bd9Sstevel@tonic-gate
295*7c478bd9Sstevel@tonic-gate restore_uid();
296*7c478bd9Sstevel@tonic-gate problem = krb5_verify_user(authctxt->krb5_ctx, authctxt->krb5_user,
297*7c478bd9Sstevel@tonic-gate authctxt->krb5_fwd_ccache, password, 1, NULL);
298*7c478bd9Sstevel@tonic-gate temporarily_use_uid(authctxt->pw);
299*7c478bd9Sstevel@tonic-gate
300*7c478bd9Sstevel@tonic-gate if (problem)
301*7c478bd9Sstevel@tonic-gate goto out;
302*7c478bd9Sstevel@tonic-gate
303*7c478bd9Sstevel@tonic-gate #else
304*7c478bd9Sstevel@tonic-gate problem = krb5_get_init_creds_password(authctxt->krb5_ctx, &creds,
305*7c478bd9Sstevel@tonic-gate authctxt->krb5_user, (char *)password, NULL, NULL, 0, NULL, NULL);
306*7c478bd9Sstevel@tonic-gate if (problem)
307*7c478bd9Sstevel@tonic-gate goto out;
308*7c478bd9Sstevel@tonic-gate
309*7c478bd9Sstevel@tonic-gate problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL,
310*7c478bd9Sstevel@tonic-gate KRB5_NT_SRV_HST, &server);
311*7c478bd9Sstevel@tonic-gate if (problem)
312*7c478bd9Sstevel@tonic-gate goto out;
313*7c478bd9Sstevel@tonic-gate
314*7c478bd9Sstevel@tonic-gate restore_uid();
315*7c478bd9Sstevel@tonic-gate problem = krb5_verify_init_creds(authctxt->krb5_ctx, &creds, server,
316*7c478bd9Sstevel@tonic-gate NULL, NULL, NULL);
317*7c478bd9Sstevel@tonic-gate krb5_free_principal(authctxt->krb5_ctx, server);
318*7c478bd9Sstevel@tonic-gate temporarily_use_uid(authctxt->pw);
319*7c478bd9Sstevel@tonic-gate if (problem)
320*7c478bd9Sstevel@tonic-gate goto out;
321*7c478bd9Sstevel@tonic-gate
322*7c478bd9Sstevel@tonic-gate if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user,
323*7c478bd9Sstevel@tonic-gate authctxt->pw->pw_name)) {
324*7c478bd9Sstevel@tonic-gate problem = -1;
325*7c478bd9Sstevel@tonic-gate goto out;
326*7c478bd9Sstevel@tonic-gate }
327*7c478bd9Sstevel@tonic-gate
328*7c478bd9Sstevel@tonic-gate snprintf(ccname,sizeof(ccname),"FILE:/tmp/krb5cc_%d_XXXXXX",geteuid());
329*7c478bd9Sstevel@tonic-gate
330*7c478bd9Sstevel@tonic-gate if ((tmpfd = mkstemp(ccname+strlen("FILE:")))==-1) {
331*7c478bd9Sstevel@tonic-gate log("mkstemp(): %.100s", strerror(errno));
332*7c478bd9Sstevel@tonic-gate problem = errno;
333*7c478bd9Sstevel@tonic-gate goto out;
334*7c478bd9Sstevel@tonic-gate }
335*7c478bd9Sstevel@tonic-gate
336*7c478bd9Sstevel@tonic-gate if (fchmod(tmpfd,S_IRUSR | S_IWUSR) == -1) {
337*7c478bd9Sstevel@tonic-gate log("fchmod(): %.100s", strerror(errno));
338*7c478bd9Sstevel@tonic-gate close(tmpfd);
339*7c478bd9Sstevel@tonic-gate problem = errno;
340*7c478bd9Sstevel@tonic-gate goto out;
341*7c478bd9Sstevel@tonic-gate }
342*7c478bd9Sstevel@tonic-gate close(tmpfd);
343*7c478bd9Sstevel@tonic-gate
344*7c478bd9Sstevel@tonic-gate problem = krb5_cc_resolve(authctxt->krb5_ctx, ccname, &authctxt->krb5_fwd_ccache);
345*7c478bd9Sstevel@tonic-gate if (problem)
346*7c478bd9Sstevel@tonic-gate goto out;
347*7c478bd9Sstevel@tonic-gate
348*7c478bd9Sstevel@tonic-gate problem = krb5_cc_initialize(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache,
349*7c478bd9Sstevel@tonic-gate authctxt->krb5_user);
350*7c478bd9Sstevel@tonic-gate if (problem)
351*7c478bd9Sstevel@tonic-gate goto out;
352*7c478bd9Sstevel@tonic-gate
353*7c478bd9Sstevel@tonic-gate problem= krb5_cc_store_cred(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache,
354*7c478bd9Sstevel@tonic-gate &creds);
355*7c478bd9Sstevel@tonic-gate if (problem)
356*7c478bd9Sstevel@tonic-gate goto out;
357*7c478bd9Sstevel@tonic-gate #endif
358*7c478bd9Sstevel@tonic-gate
359*7c478bd9Sstevel@tonic-gate authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
360*7c478bd9Sstevel@tonic-gate
361*7c478bd9Sstevel@tonic-gate out:
362*7c478bd9Sstevel@tonic-gate restore_uid();
363*7c478bd9Sstevel@tonic-gate
364*7c478bd9Sstevel@tonic-gate if (problem) {
365*7c478bd9Sstevel@tonic-gate if (authctxt->krb5_ctx != NULL && problem!=-1)
366*7c478bd9Sstevel@tonic-gate debug("Kerberos password authentication failed: %s",
367*7c478bd9Sstevel@tonic-gate krb5_get_err_text(authctxt->krb5_ctx, problem));
368*7c478bd9Sstevel@tonic-gate else
369*7c478bd9Sstevel@tonic-gate debug("Kerberos password authentication failed: %d",
370*7c478bd9Sstevel@tonic-gate problem);
371*7c478bd9Sstevel@tonic-gate
372*7c478bd9Sstevel@tonic-gate krb5_cleanup_proc(authctxt);
373*7c478bd9Sstevel@tonic-gate
374*7c478bd9Sstevel@tonic-gate if (options.kerberos_or_local_passwd)
375*7c478bd9Sstevel@tonic-gate return (-1);
376*7c478bd9Sstevel@tonic-gate else
377*7c478bd9Sstevel@tonic-gate return (0);
378*7c478bd9Sstevel@tonic-gate }
379*7c478bd9Sstevel@tonic-gate return (1);
380*7c478bd9Sstevel@tonic-gate }
381*7c478bd9Sstevel@tonic-gate
382*7c478bd9Sstevel@tonic-gate void
krb5_cleanup_proc(void * context)383*7c478bd9Sstevel@tonic-gate krb5_cleanup_proc(void *context)
384*7c478bd9Sstevel@tonic-gate {
385*7c478bd9Sstevel@tonic-gate Authctxt *authctxt = (Authctxt *)context;
386*7c478bd9Sstevel@tonic-gate
387*7c478bd9Sstevel@tonic-gate debug("krb5_cleanup_proc called");
388*7c478bd9Sstevel@tonic-gate if (authctxt->krb5_fwd_ccache) {
389*7c478bd9Sstevel@tonic-gate krb5_cc_destroy(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
390*7c478bd9Sstevel@tonic-gate authctxt->krb5_fwd_ccache = NULL;
391*7c478bd9Sstevel@tonic-gate }
392*7c478bd9Sstevel@tonic-gate if (authctxt->krb5_user) {
393*7c478bd9Sstevel@tonic-gate krb5_free_principal(authctxt->krb5_ctx, authctxt->krb5_user);
394*7c478bd9Sstevel@tonic-gate authctxt->krb5_user = NULL;
395*7c478bd9Sstevel@tonic-gate }
396*7c478bd9Sstevel@tonic-gate if (authctxt->krb5_auth_ctx) {
397*7c478bd9Sstevel@tonic-gate krb5_auth_con_free(authctxt->krb5_ctx,
398*7c478bd9Sstevel@tonic-gate authctxt->krb5_auth_ctx);
399*7c478bd9Sstevel@tonic-gate authctxt->krb5_auth_ctx = NULL;
400*7c478bd9Sstevel@tonic-gate }
401*7c478bd9Sstevel@tonic-gate if (authctxt->krb5_ctx) {
402*7c478bd9Sstevel@tonic-gate krb5_free_context(authctxt->krb5_ctx);
403*7c478bd9Sstevel@tonic-gate authctxt->krb5_ctx = NULL;
404*7c478bd9Sstevel@tonic-gate }
405*7c478bd9Sstevel@tonic-gate }
406*7c478bd9Sstevel@tonic-gate
407*7c478bd9Sstevel@tonic-gate #endif /* KRB5 */
408