Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Kerberos v5 authentication and ticket-passing routines. |
| 3 | * |
| 4 | * $FreeBSD: src/crypto/openssh/auth-krb5.c,v 1.6 2001/02/13 16:58:04 assar Exp $ |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include "includes.h" |
Ben Lindstrom | 05764b9 | 2002-03-05 01:53:02 +0000 | [diff] [blame] | 8 | RCSID("$OpenBSD: auth-krb5.c,v 1.6 2002/03/04 17:27:39 stevesk Exp $"); |
| 9 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 10 | #include "ssh.h" |
| 11 | #include "ssh1.h" |
| 12 | #include "packet.h" |
| 13 | #include "xmalloc.h" |
| 14 | #include "log.h" |
| 15 | #include "servconf.h" |
| 16 | #include "uidswap.h" |
| 17 | #include "auth.h" |
| 18 | |
| 19 | #ifdef KRB5 |
| 20 | #include <krb5.h> |
| 21 | |
| 22 | extern ServerOptions options; |
| 23 | |
| 24 | static int |
| 25 | krb5_init(void *context) |
| 26 | { |
| 27 | Authctxt *authctxt = (Authctxt *)context; |
| 28 | krb5_error_code problem; |
| 29 | static int cleanup_registered = 0; |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 30 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 31 | if (authctxt->krb5_ctx == NULL) { |
| 32 | problem = krb5_init_context(&authctxt->krb5_ctx); |
| 33 | if (problem) |
| 34 | return (problem); |
| 35 | krb5_init_ets(authctxt->krb5_ctx); |
| 36 | } |
| 37 | if (!cleanup_registered) { |
| 38 | fatal_add_cleanup(krb5_cleanup_proc, authctxt); |
| 39 | cleanup_registered = 1; |
| 40 | } |
| 41 | return (0); |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | * Try krb5 authentication. server_user is passed for logging purposes |
| 46 | * only, in auth is received ticket, in client is returned principal |
| 47 | * from the ticket |
| 48 | */ |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 49 | int |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 50 | auth_krb5(Authctxt *authctxt, krb5_data *auth, char **client) |
| 51 | { |
| 52 | krb5_error_code problem; |
| 53 | krb5_principal server; |
| 54 | krb5_data reply; |
| 55 | krb5_ticket *ticket; |
Damien Miller | 61b05cf | 2001-11-14 00:02:10 +1100 | [diff] [blame] | 56 | int fd, ret; |
| 57 | |
| 58 | ret = 0; |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 59 | server = NULL; |
| 60 | ticket = NULL; |
| 61 | reply.length = 0; |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 62 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 63 | problem = krb5_init(authctxt); |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 64 | if (problem) |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 65 | goto err; |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 66 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 67 | problem = krb5_auth_con_init(authctxt->krb5_ctx, |
| 68 | &authctxt->krb5_auth_ctx); |
| 69 | if (problem) |
| 70 | goto err; |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 71 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 72 | fd = packet_get_connection_in(); |
| 73 | problem = krb5_auth_con_setaddrs_from_fd(authctxt->krb5_ctx, |
| 74 | authctxt->krb5_auth_ctx, &fd); |
| 75 | if (problem) |
| 76 | goto err; |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 77 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 78 | problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL , |
| 79 | KRB5_NT_SRV_HST, &server); |
| 80 | if (problem) |
| 81 | goto err; |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 82 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 83 | problem = krb5_rd_req(authctxt->krb5_ctx, &authctxt->krb5_auth_ctx, |
| 84 | auth, server, NULL, NULL, &ticket); |
| 85 | if (problem) |
| 86 | goto err; |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 87 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 88 | problem = krb5_copy_principal(authctxt->krb5_ctx, ticket->client, |
| 89 | &authctxt->krb5_user); |
| 90 | if (problem) |
| 91 | goto err; |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 92 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 93 | /* if client wants mutual auth */ |
| 94 | problem = krb5_mk_rep(authctxt->krb5_ctx, authctxt->krb5_auth_ctx, |
| 95 | &reply); |
| 96 | if (problem) |
| 97 | goto err; |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 98 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 99 | /* Check .k5login authorization now. */ |
| 100 | if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user, |
| 101 | authctxt->pw->pw_name)) |
| 102 | goto err; |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 103 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 104 | if (client) |
| 105 | krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user, |
| 106 | client); |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 107 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 108 | packet_start(SSH_SMSG_AUTH_KERBEROS_RESPONSE); |
| 109 | packet_put_string((char *) reply.data, reply.length); |
| 110 | packet_send(); |
| 111 | packet_write_wait(); |
Damien Miller | 61b05cf | 2001-11-14 00:02:10 +1100 | [diff] [blame] | 112 | |
| 113 | ret = 1; |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 114 | err: |
| 115 | if (server) |
| 116 | krb5_free_principal(authctxt->krb5_ctx, server); |
| 117 | if (ticket) |
| 118 | krb5_free_ticket(authctxt->krb5_ctx, ticket); |
| 119 | if (reply.length) |
| 120 | xfree(reply.data); |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 121 | |
Ben Lindstrom | b855028 | 2002-02-26 17:46:11 +0000 | [diff] [blame] | 122 | if (problem) { |
| 123 | if (authctxt->krb5_ctx != NULL) |
| 124 | debug("Kerberos v5 authentication failed: %s", |
| 125 | krb5_get_err_text(authctxt->krb5_ctx, problem)); |
| 126 | else |
| 127 | debug("Kerberos v5 authentication failed: %d", |
| 128 | problem); |
| 129 | } |
Damien Miller | 61b05cf | 2001-11-14 00:02:10 +1100 | [diff] [blame] | 130 | |
| 131 | return (ret); |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | int |
| 135 | auth_krb5_tgt(Authctxt *authctxt, krb5_data *tgt) |
| 136 | { |
| 137 | krb5_error_code problem; |
| 138 | krb5_ccache ccache = NULL; |
| 139 | char *pname; |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 140 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 141 | if (authctxt->pw == NULL || authctxt->krb5_user == NULL) |
| 142 | return (0); |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 143 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 144 | temporarily_use_uid(authctxt->pw); |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 145 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 146 | problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_fcc_ops, &ccache); |
| 147 | if (problem) |
| 148 | goto fail; |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 149 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 150 | problem = krb5_cc_initialize(authctxt->krb5_ctx, ccache, |
| 151 | authctxt->krb5_user); |
| 152 | if (problem) |
| 153 | goto fail; |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 154 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 155 | problem = krb5_rd_cred2(authctxt->krb5_ctx, authctxt->krb5_auth_ctx, |
| 156 | ccache, tgt); |
| 157 | if (problem) |
| 158 | goto fail; |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 159 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 160 | authctxt->krb5_fwd_ccache = ccache; |
| 161 | ccache = NULL; |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 162 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 163 | authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache); |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 164 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 165 | problem = krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user, |
| 166 | &pname); |
| 167 | if (problem) |
| 168 | goto fail; |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 169 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 170 | debug("Kerberos v5 TGT accepted (%s)", pname); |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 171 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 172 | restore_uid(); |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 173 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 174 | return (1); |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 175 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 176 | fail: |
| 177 | if (problem) |
| 178 | debug("Kerberos v5 TGT passing failed: %s", |
| 179 | krb5_get_err_text(authctxt->krb5_ctx, problem)); |
| 180 | if (ccache) |
| 181 | krb5_cc_destroy(authctxt->krb5_ctx, ccache); |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 182 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 183 | restore_uid(); |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 184 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 185 | return (0); |
| 186 | } |
| 187 | |
| 188 | int |
| 189 | auth_krb5_password(Authctxt *authctxt, const char *password) |
| 190 | { |
| 191 | krb5_error_code problem; |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 192 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 193 | if (authctxt->pw == NULL) |
| 194 | return (0); |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 195 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 196 | temporarily_use_uid(authctxt->pw); |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 197 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 198 | problem = krb5_init(authctxt); |
| 199 | if (problem) |
| 200 | goto out; |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 201 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 202 | problem = krb5_parse_name(authctxt->krb5_ctx, authctxt->pw->pw_name, |
| 203 | &authctxt->krb5_user); |
| 204 | if (problem) |
| 205 | goto out; |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 206 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 207 | problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_mcc_ops, |
| 208 | &authctxt->krb5_fwd_ccache); |
| 209 | if (problem) |
| 210 | goto out; |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 211 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 212 | problem = krb5_cc_initialize(authctxt->krb5_ctx, |
| 213 | authctxt->krb5_fwd_ccache, authctxt->krb5_user); |
| 214 | if (problem) |
| 215 | goto out; |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 216 | |
| 217 | restore_uid(); |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 218 | problem = krb5_verify_user(authctxt->krb5_ctx, authctxt->krb5_user, |
| 219 | authctxt->krb5_fwd_ccache, password, 1, NULL); |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 220 | temporarily_use_uid(authctxt->pw); |
| 221 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 222 | if (problem) |
| 223 | goto out; |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 224 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 225 | authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache); |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 226 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 227 | out: |
| 228 | restore_uid(); |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 229 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 230 | if (problem) { |
Ben Lindstrom | b855028 | 2002-02-26 17:46:11 +0000 | [diff] [blame] | 231 | if (authctxt->krb5_ctx != NULL) |
| 232 | debug("Kerberos password authentication failed: %s", |
| 233 | krb5_get_err_text(authctxt->krb5_ctx, problem)); |
| 234 | else |
| 235 | debug("Kerberos password authentication failed: %d", |
| 236 | problem); |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 237 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 238 | krb5_cleanup_proc(authctxt); |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 239 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 240 | if (options.kerberos_or_local_passwd) |
| 241 | return (-1); |
| 242 | else |
| 243 | return (0); |
| 244 | } |
| 245 | return (1); |
| 246 | } |
| 247 | |
| 248 | void |
| 249 | krb5_cleanup_proc(void *context) |
| 250 | { |
| 251 | Authctxt *authctxt = (Authctxt *)context; |
Damien Miller | db95e4e | 2002-02-13 16:56:44 +1100 | [diff] [blame] | 252 | |
Damien Miller | 964fed5 | 2001-09-25 12:58:23 +1000 | [diff] [blame] | 253 | debug("krb5_cleanup_proc called"); |
| 254 | if (authctxt->krb5_fwd_ccache) { |
| 255 | krb5_cc_destroy(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache); |
| 256 | authctxt->krb5_fwd_ccache = NULL; |
| 257 | } |
| 258 | if (authctxt->krb5_user) { |
| 259 | krb5_free_principal(authctxt->krb5_ctx, authctxt->krb5_user); |
| 260 | authctxt->krb5_user = NULL; |
| 261 | } |
| 262 | if (authctxt->krb5_auth_ctx) { |
| 263 | krb5_auth_con_free(authctxt->krb5_ctx, |
| 264 | authctxt->krb5_auth_ctx); |
| 265 | authctxt->krb5_auth_ctx = NULL; |
| 266 | } |
| 267 | if (authctxt->krb5_ctx) { |
| 268 | krb5_free_context(authctxt->krb5_ctx); |
| 269 | authctxt->krb5_ctx = NULL; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | #endif /* KRB5 */ |