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.1 2001/06/26 16:15:23 dugsong 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;
61 problem = krb5_init(authctxt);
65 problem = krb5_auth_con_init(authctxt->krb5_ctx,
66 &authctxt->krb5_auth_ctx);
70 fd = packet_get_connection_in();
71 problem = krb5_auth_con_setaddrs_from_fd(authctxt->krb5_ctx,
72 authctxt->krb5_auth_ctx, &fd);
76 problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL ,
77 KRB5_NT_SRV_HST, &server);
81 problem = krb5_rd_req(authctxt->krb5_ctx, &authctxt->krb5_auth_ctx,
82 auth, server, NULL, NULL, &ticket);
86 problem = krb5_copy_principal(authctxt->krb5_ctx, ticket->client,
87 &authctxt->krb5_user);
91 /* if client wants mutual auth */
92 problem = krb5_mk_rep(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
97 /* Check .k5login authorization now. */
98 if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user,
99 authctxt->pw->pw_name))
103 krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user,
106 packet_start(SSH_SMSG_AUTH_KERBEROS_RESPONSE);
107 packet_put_string((char *) reply.data, reply.length);
113 krb5_free_principal(authctxt->krb5_ctx, server);
115 krb5_free_ticket(authctxt->krb5_ctx, ticket);
120 debug("Kerberos v5 authentication failed: %s",
121 krb5_get_err_text(authctxt->krb5_ctx, problem));
128 auth_krb5_tgt(Authctxt *authctxt, krb5_data *tgt)
130 krb5_error_code problem;
131 krb5_ccache ccache = NULL;
134 if (authctxt->pw == NULL || authctxt->krb5_user == NULL)
137 temporarily_use_uid(authctxt->pw);
139 problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_fcc_ops, &ccache);
143 problem = krb5_cc_initialize(authctxt->krb5_ctx, ccache,
144 authctxt->krb5_user);
148 problem = krb5_rd_cred2(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
153 authctxt->krb5_fwd_ccache = ccache;
156 authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
158 problem = krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user,
163 debug("Kerberos v5 TGT accepted (%s)", pname);
171 debug("Kerberos v5 TGT passing failed: %s",
172 krb5_get_err_text(authctxt->krb5_ctx, problem));
174 krb5_cc_destroy(authctxt->krb5_ctx, ccache);
182 auth_krb5_password(Authctxt *authctxt, const char *password)
184 krb5_error_code problem;
186 if (authctxt->pw == NULL)
189 temporarily_use_uid(authctxt->pw);
191 problem = krb5_init(authctxt);
195 problem = krb5_parse_name(authctxt->krb5_ctx, authctxt->pw->pw_name,
196 &authctxt->krb5_user);
200 problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_mcc_ops,
201 &authctxt->krb5_fwd_ccache);
205 problem = krb5_cc_initialize(authctxt->krb5_ctx,
206 authctxt->krb5_fwd_ccache, authctxt->krb5_user);
210 problem = krb5_verify_user(authctxt->krb5_ctx, authctxt->krb5_user,
211 authctxt->krb5_fwd_ccache, password, 1, NULL);
215 authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
221 debug("Kerberos password authentication failed: %s",
222 krb5_get_err_text(authctxt->krb5_ctx, problem));
224 krb5_cleanup_proc(authctxt);
226 if (options.kerberos_or_local_passwd)
235 krb5_cleanup_proc(void *context)
237 Authctxt *authctxt = (Authctxt *)context;
239 debug("krb5_cleanup_proc called");
240 if (authctxt->krb5_fwd_ccache) {
241 krb5_cc_destroy(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
242 authctxt->krb5_fwd_ccache = NULL;
244 if (authctxt->krb5_user) {
245 krb5_free_principal(authctxt->krb5_ctx, authctxt->krb5_user);
246 authctxt->krb5_user = NULL;
248 if (authctxt->krb5_auth_ctx) {
249 krb5_auth_con_free(authctxt->krb5_ctx,
250 authctxt->krb5_auth_ctx);
251 authctxt->krb5_auth_ctx = NULL;
253 if (authctxt->krb5_ctx) {
254 krb5_free_context(authctxt->krb5_ctx);
255 authctxt->krb5_ctx = NULL;