diff options
Diffstat (limited to 'auth-krb5.c')
-rw-r--r-- | auth-krb5.c | 87 |
1 files changed, 45 insertions, 42 deletions
diff --git a/auth-krb5.c b/auth-krb5.c index b56f43a92..bf2412080 100644 --- a/auth-krb5.c +++ b/auth-krb5.c | |||
@@ -2,7 +2,7 @@ | |||
2 | * Kerberos v5 authentication and ticket-passing routines. | 2 | * Kerberos v5 authentication and ticket-passing routines. |
3 | * | 3 | * |
4 | * $FreeBSD: src/crypto/openssh/auth-krb5.c,v 1.6 2001/02/13 16:58:04 assar Exp $ | 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.2 2001/11/12 01:47:09 dugsong Exp $ | 5 | * $OpenBSD: auth-krb5.c,v 1.4 2002/01/27 15:12:09 markus Exp $ |
6 | */ | 6 | */ |
7 | 7 | ||
8 | #include "includes.h" | 8 | #include "includes.h" |
@@ -26,7 +26,7 @@ krb5_init(void *context) | |||
26 | Authctxt *authctxt = (Authctxt *)context; | 26 | Authctxt *authctxt = (Authctxt *)context; |
27 | krb5_error_code problem; | 27 | krb5_error_code problem; |
28 | static int cleanup_registered = 0; | 28 | static int cleanup_registered = 0; |
29 | 29 | ||
30 | if (authctxt->krb5_ctx == NULL) { | 30 | if (authctxt->krb5_ctx == NULL) { |
31 | problem = krb5_init_context(&authctxt->krb5_ctx); | 31 | problem = krb5_init_context(&authctxt->krb5_ctx); |
32 | if (problem) | 32 | if (problem) |
@@ -45,7 +45,7 @@ krb5_init(void *context) | |||
45 | * only, in auth is received ticket, in client is returned principal | 45 | * only, in auth is received ticket, in client is returned principal |
46 | * from the ticket | 46 | * from the ticket |
47 | */ | 47 | */ |
48 | int | 48 | int |
49 | auth_krb5(Authctxt *authctxt, krb5_data *auth, char **client) | 49 | auth_krb5(Authctxt *authctxt, krb5_data *auth, char **client) |
50 | { | 50 | { |
51 | krb5_error_code problem; | 51 | krb5_error_code problem; |
@@ -58,52 +58,52 @@ auth_krb5(Authctxt *authctxt, krb5_data *auth, char **client) | |||
58 | server = NULL; | 58 | server = NULL; |
59 | ticket = NULL; | 59 | ticket = NULL; |
60 | reply.length = 0; | 60 | reply.length = 0; |
61 | 61 | ||
62 | problem = krb5_init(authctxt); | 62 | problem = krb5_init(authctxt); |
63 | if (problem) | 63 | if (problem) |
64 | goto err; | 64 | goto err; |
65 | 65 | ||
66 | problem = krb5_auth_con_init(authctxt->krb5_ctx, | 66 | problem = krb5_auth_con_init(authctxt->krb5_ctx, |
67 | &authctxt->krb5_auth_ctx); | 67 | &authctxt->krb5_auth_ctx); |
68 | if (problem) | 68 | if (problem) |
69 | goto err; | 69 | goto err; |
70 | 70 | ||
71 | fd = packet_get_connection_in(); | 71 | fd = packet_get_connection_in(); |
72 | problem = krb5_auth_con_setaddrs_from_fd(authctxt->krb5_ctx, | 72 | problem = krb5_auth_con_setaddrs_from_fd(authctxt->krb5_ctx, |
73 | authctxt->krb5_auth_ctx, &fd); | 73 | authctxt->krb5_auth_ctx, &fd); |
74 | if (problem) | 74 | if (problem) |
75 | goto err; | 75 | goto err; |
76 | 76 | ||
77 | problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL , | 77 | problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL , |
78 | KRB5_NT_SRV_HST, &server); | 78 | KRB5_NT_SRV_HST, &server); |
79 | if (problem) | 79 | if (problem) |
80 | goto err; | 80 | goto err; |
81 | 81 | ||
82 | problem = krb5_rd_req(authctxt->krb5_ctx, &authctxt->krb5_auth_ctx, | 82 | problem = krb5_rd_req(authctxt->krb5_ctx, &authctxt->krb5_auth_ctx, |
83 | auth, server, NULL, NULL, &ticket); | 83 | auth, server, NULL, NULL, &ticket); |
84 | if (problem) | 84 | if (problem) |
85 | goto err; | 85 | goto err; |
86 | 86 | ||
87 | problem = krb5_copy_principal(authctxt->krb5_ctx, ticket->client, | 87 | problem = krb5_copy_principal(authctxt->krb5_ctx, ticket->client, |
88 | &authctxt->krb5_user); | 88 | &authctxt->krb5_user); |
89 | if (problem) | 89 | if (problem) |
90 | goto err; | 90 | goto err; |
91 | 91 | ||
92 | /* if client wants mutual auth */ | 92 | /* if client wants mutual auth */ |
93 | problem = krb5_mk_rep(authctxt->krb5_ctx, authctxt->krb5_auth_ctx, | 93 | problem = krb5_mk_rep(authctxt->krb5_ctx, authctxt->krb5_auth_ctx, |
94 | &reply); | 94 | &reply); |
95 | if (problem) | 95 | if (problem) |
96 | goto err; | 96 | goto err; |
97 | 97 | ||
98 | /* Check .k5login authorization now. */ | 98 | /* Check .k5login authorization now. */ |
99 | if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user, | 99 | if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user, |
100 | authctxt->pw->pw_name)) | 100 | authctxt->pw->pw_name)) |
101 | goto err; | 101 | goto err; |
102 | 102 | ||
103 | if (client) | 103 | if (client) |
104 | krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user, | 104 | krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user, |
105 | client); | 105 | client); |
106 | 106 | ||
107 | packet_start(SSH_SMSG_AUTH_KERBEROS_RESPONSE); | 107 | packet_start(SSH_SMSG_AUTH_KERBEROS_RESPONSE); |
108 | packet_put_string((char *) reply.data, reply.length); | 108 | packet_put_string((char *) reply.data, reply.length); |
109 | packet_send(); | 109 | packet_send(); |
@@ -117,7 +117,7 @@ auth_krb5(Authctxt *authctxt, krb5_data *auth, char **client) | |||
117 | krb5_free_ticket(authctxt->krb5_ctx, ticket); | 117 | krb5_free_ticket(authctxt->krb5_ctx, ticket); |
118 | if (reply.length) | 118 | if (reply.length) |
119 | xfree(reply.data); | 119 | xfree(reply.data); |
120 | 120 | ||
121 | if (problem) | 121 | if (problem) |
122 | debug("Kerberos v5 authentication failed: %s", | 122 | debug("Kerberos v5 authentication failed: %s", |
123 | krb5_get_err_text(authctxt->krb5_ctx, problem)); | 123 | krb5_get_err_text(authctxt->krb5_ctx, problem)); |
@@ -131,51 +131,51 @@ auth_krb5_tgt(Authctxt *authctxt, krb5_data *tgt) | |||
131 | krb5_error_code problem; | 131 | krb5_error_code problem; |
132 | krb5_ccache ccache = NULL; | 132 | krb5_ccache ccache = NULL; |
133 | char *pname; | 133 | char *pname; |
134 | 134 | ||
135 | if (authctxt->pw == NULL || authctxt->krb5_user == NULL) | 135 | if (authctxt->pw == NULL || authctxt->krb5_user == NULL) |
136 | return (0); | 136 | return (0); |
137 | 137 | ||
138 | temporarily_use_uid(authctxt->pw); | 138 | temporarily_use_uid(authctxt->pw); |
139 | 139 | ||
140 | problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_fcc_ops, &ccache); | 140 | problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_fcc_ops, &ccache); |
141 | if (problem) | 141 | if (problem) |
142 | goto fail; | 142 | goto fail; |
143 | 143 | ||
144 | problem = krb5_cc_initialize(authctxt->krb5_ctx, ccache, | 144 | problem = krb5_cc_initialize(authctxt->krb5_ctx, ccache, |
145 | authctxt->krb5_user); | 145 | authctxt->krb5_user); |
146 | if (problem) | 146 | if (problem) |
147 | goto fail; | 147 | goto fail; |
148 | 148 | ||
149 | problem = krb5_rd_cred2(authctxt->krb5_ctx, authctxt->krb5_auth_ctx, | 149 | problem = krb5_rd_cred2(authctxt->krb5_ctx, authctxt->krb5_auth_ctx, |
150 | ccache, tgt); | 150 | ccache, tgt); |
151 | if (problem) | 151 | if (problem) |
152 | goto fail; | 152 | goto fail; |
153 | 153 | ||
154 | authctxt->krb5_fwd_ccache = ccache; | 154 | authctxt->krb5_fwd_ccache = ccache; |
155 | ccache = NULL; | 155 | ccache = NULL; |
156 | 156 | ||
157 | authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache); | 157 | authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache); |
158 | 158 | ||
159 | problem = krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user, | 159 | problem = krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user, |
160 | &pname); | 160 | &pname); |
161 | if (problem) | 161 | if (problem) |
162 | goto fail; | 162 | goto fail; |
163 | 163 | ||
164 | debug("Kerberos v5 TGT accepted (%s)", pname); | 164 | debug("Kerberos v5 TGT accepted (%s)", pname); |
165 | 165 | ||
166 | restore_uid(); | 166 | restore_uid(); |
167 | 167 | ||
168 | return (1); | 168 | return (1); |
169 | 169 | ||
170 | fail: | 170 | fail: |
171 | if (problem) | 171 | if (problem) |
172 | debug("Kerberos v5 TGT passing failed: %s", | 172 | debug("Kerberos v5 TGT passing failed: %s", |
173 | krb5_get_err_text(authctxt->krb5_ctx, problem)); | 173 | krb5_get_err_text(authctxt->krb5_ctx, problem)); |
174 | if (ccache) | 174 | if (ccache) |
175 | krb5_cc_destroy(authctxt->krb5_ctx, ccache); | 175 | krb5_cc_destroy(authctxt->krb5_ctx, ccache); |
176 | 176 | ||
177 | restore_uid(); | 177 | restore_uid(); |
178 | 178 | ||
179 | return (0); | 179 | return (0); |
180 | } | 180 | } |
181 | 181 | ||
@@ -183,47 +183,50 @@ int | |||
183 | auth_krb5_password(Authctxt *authctxt, const char *password) | 183 | auth_krb5_password(Authctxt *authctxt, const char *password) |
184 | { | 184 | { |
185 | krb5_error_code problem; | 185 | krb5_error_code problem; |
186 | 186 | ||
187 | if (authctxt->pw == NULL) | 187 | if (authctxt->pw == NULL) |
188 | return (0); | 188 | return (0); |
189 | 189 | ||
190 | temporarily_use_uid(authctxt->pw); | 190 | temporarily_use_uid(authctxt->pw); |
191 | 191 | ||
192 | problem = krb5_init(authctxt); | 192 | problem = krb5_init(authctxt); |
193 | if (problem) | 193 | if (problem) |
194 | goto out; | 194 | goto out; |
195 | 195 | ||
196 | problem = krb5_parse_name(authctxt->krb5_ctx, authctxt->pw->pw_name, | 196 | problem = krb5_parse_name(authctxt->krb5_ctx, authctxt->pw->pw_name, |
197 | &authctxt->krb5_user); | 197 | &authctxt->krb5_user); |
198 | if (problem) | 198 | if (problem) |
199 | goto out; | 199 | goto out; |
200 | 200 | ||
201 | problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_mcc_ops, | 201 | problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_mcc_ops, |
202 | &authctxt->krb5_fwd_ccache); | 202 | &authctxt->krb5_fwd_ccache); |
203 | if (problem) | 203 | if (problem) |
204 | goto out; | 204 | goto out; |
205 | 205 | ||
206 | problem = krb5_cc_initialize(authctxt->krb5_ctx, | 206 | problem = krb5_cc_initialize(authctxt->krb5_ctx, |
207 | authctxt->krb5_fwd_ccache, authctxt->krb5_user); | 207 | authctxt->krb5_fwd_ccache, authctxt->krb5_user); |
208 | if (problem) | 208 | if (problem) |
209 | goto out; | 209 | goto out; |
210 | 210 | ||
211 | restore_uid(); | ||
211 | problem = krb5_verify_user(authctxt->krb5_ctx, authctxt->krb5_user, | 212 | problem = krb5_verify_user(authctxt->krb5_ctx, authctxt->krb5_user, |
212 | authctxt->krb5_fwd_ccache, password, 1, NULL); | 213 | authctxt->krb5_fwd_ccache, password, 1, NULL); |
214 | temporarily_use_uid(authctxt->pw); | ||
215 | |||
213 | if (problem) | 216 | if (problem) |
214 | goto out; | 217 | goto out; |
215 | 218 | ||
216 | authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache); | 219 | authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache); |
217 | 220 | ||
218 | out: | 221 | out: |
219 | restore_uid(); | 222 | restore_uid(); |
220 | 223 | ||
221 | if (problem) { | 224 | if (problem) { |
222 | debug("Kerberos password authentication failed: %s", | 225 | debug("Kerberos password authentication failed: %s", |
223 | krb5_get_err_text(authctxt->krb5_ctx, problem)); | 226 | krb5_get_err_text(authctxt->krb5_ctx, problem)); |
224 | 227 | ||
225 | krb5_cleanup_proc(authctxt); | 228 | krb5_cleanup_proc(authctxt); |
226 | 229 | ||
227 | if (options.kerberos_or_local_passwd) | 230 | if (options.kerberos_or_local_passwd) |
228 | return (-1); | 231 | return (-1); |
229 | else | 232 | else |
@@ -236,7 +239,7 @@ void | |||
236 | krb5_cleanup_proc(void *context) | 239 | krb5_cleanup_proc(void *context) |
237 | { | 240 | { |
238 | Authctxt *authctxt = (Authctxt *)context; | 241 | Authctxt *authctxt = (Authctxt *)context; |
239 | 242 | ||
240 | debug("krb5_cleanup_proc called"); | 243 | debug("krb5_cleanup_proc called"); |
241 | if (authctxt->krb5_fwd_ccache) { | 244 | if (authctxt->krb5_fwd_ccache) { |
242 | krb5_cc_destroy(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache); | 245 | krb5_cc_destroy(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache); |