2 * Kerberos v5 authentication and ticket-passing routines.
4 * $FreeBSD: src/crypto/openssh/auth-krb5.c,v 1.6 2001/02/13 16:58:04 assar Exp $
5 * $OpenBSD: auth-krb5.c,v 1.5 2002/02/15 23:54:10 markus Exp $
21 extern ServerOptions options;
24 krb5_init(void *context)
26 Authctxt *authctxt = (Authctxt *)context;
27 krb5_error_code problem;
28 static int cleanup_registered = 0;
30 if (authctxt->krb5_ctx == NULL) {
31 problem = krb5_init_context(&authctxt->krb5_ctx);
34 krb5_init_ets(authctxt->krb5_ctx);
36 if (!cleanup_registered) {
37 fatal_add_cleanup(krb5_cleanup_proc, authctxt);
38 cleanup_registered = 1;
44 * Try krb5 authentication. server_user is passed for logging purposes
45 * only, in auth is received ticket, in client is returned principal
49 auth_krb5(Authctxt *authctxt, krb5_data *auth, char **client)
51 krb5_error_code problem;
52 krb5_principal server;
62 problem = krb5_init(authctxt);
66 problem = krb5_auth_con_init(authctxt->krb5_ctx,
67 &authctxt->krb5_auth_ctx);
71 fd = packet_get_connection_in();
72 problem = krb5_auth_con_setaddrs_from_fd(authctxt->krb5_ctx,
73 authctxt->krb5_auth_ctx, &fd);
77 problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL ,
78 KRB5_NT_SRV_HST, &server);
82 problem = krb5_rd_req(authctxt->krb5_ctx, &authctxt->krb5_auth_ctx,
83 auth, server, NULL, NULL, &ticket);
87 problem = krb5_copy_principal(authctxt->krb5_ctx, ticket->client,
88 &authctxt->krb5_user);
92 /* if client wants mutual auth */
93 problem = krb5_mk_rep(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
98 /* Check .k5login authorization now. */
99 if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user,
100 authctxt->pw->pw_name))
104 krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user,
107 packet_start(SSH_SMSG_AUTH_KERBEROS_RESPONSE);
108 packet_put_string((char *) reply.data, reply.length);
115 krb5_free_principal(authctxt->krb5_ctx, server);
117 krb5_free_ticket(authctxt->krb5_ctx, ticket);
122 if (authctxt->krb5_ctx != NULL)
123 debug("Kerberos v5 authentication failed: %s",
124 krb5_get_err_text(authctxt->krb5_ctx, problem));
126 debug("Kerberos v5 authentication failed: %d",
134 auth_krb5_tgt(Authctxt *authctxt, krb5_data *tgt)
136 krb5_error_code problem;
137 krb5_ccache ccache = NULL;
140 if (authctxt->pw == NULL || authctxt->krb5_user == NULL)
143 temporarily_use_uid(authctxt->pw);
145 problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_fcc_ops, &ccache);
149 problem = krb5_cc_initialize(authctxt->krb5_ctx, ccache,
150 authctxt->krb5_user);
154 problem = krb5_rd_cred2(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
159 authctxt->krb5_fwd_ccache = ccache;
162 authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
164 problem = krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user,
169 debug("Kerberos v5 TGT accepted (%s)", pname);
177 debug("Kerberos v5 TGT passing failed: %s",
178 krb5_get_err_text(authctxt->krb5_ctx, problem));
180 krb5_cc_destroy(authctxt->krb5_ctx, ccache);
188 auth_krb5_password(Authctxt *authctxt, const char *password)
190 krb5_error_code problem;
192 if (authctxt->pw == NULL)
195 temporarily_use_uid(authctxt->pw);
197 problem = krb5_init(authctxt);
201 problem = krb5_parse_name(authctxt->krb5_ctx, authctxt->pw->pw_name,
202 &authctxt->krb5_user);
206 problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_mcc_ops,
207 &authctxt->krb5_fwd_ccache);
211 problem = krb5_cc_initialize(authctxt->krb5_ctx,
212 authctxt->krb5_fwd_ccache, authctxt->krb5_user);
217 problem = krb5_verify_user(authctxt->krb5_ctx, authctxt->krb5_user,
218 authctxt->krb5_fwd_ccache, password, 1, NULL);
219 temporarily_use_uid(authctxt->pw);
224 authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
230 if (authctxt->krb5_ctx != NULL)
231 debug("Kerberos password authentication failed: %s",
232 krb5_get_err_text(authctxt->krb5_ctx, problem));
234 debug("Kerberos password authentication failed: %d",
237 krb5_cleanup_proc(authctxt);
239 if (options.kerberos_or_local_passwd)
248 krb5_cleanup_proc(void *context)
250 Authctxt *authctxt = (Authctxt *)context;
252 debug("krb5_cleanup_proc called");
253 if (authctxt->krb5_fwd_ccache) {
254 krb5_cc_destroy(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
255 authctxt->krb5_fwd_ccache = NULL;
257 if (authctxt->krb5_user) {
258 krb5_free_principal(authctxt->krb5_ctx, authctxt->krb5_user);
259 authctxt->krb5_user = NULL;
261 if (authctxt->krb5_auth_ctx) {
262 krb5_auth_con_free(authctxt->krb5_ctx,
263 authctxt->krb5_auth_ctx);
264 authctxt->krb5_auth_ctx = NULL;
266 if (authctxt->krb5_ctx) {
267 krb5_free_context(authctxt->krb5_ctx);
268 authctxt->krb5_ctx = NULL;