]>
Commit | Line | Data |
---|---|---|
3c0ef626 | 1 | /* |
2 | * Copyright (c) 2000 Markus Friedl. All rights reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
7 | * 1. Redistributions of source code must retain the above copyright | |
8 | * notice, this list of conditions and the following disclaimer. | |
9 | * 2. Redistributions in binary form must reproduce the above copyright | |
10 | * notice, this list of conditions and the following disclaimer in the | |
11 | * documentation and/or other materials provided with the distribution. | |
12 | * | |
13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
23 | */ | |
24 | ||
25 | #include "includes.h" | |
7e82606e | 26 | RCSID("$OpenBSD: auth2.c,v 1.107 2004/07/28 09:40:29 markus Exp $"); |
3c0ef626 | 27 | |
28 | #include "ssh2.h" | |
29 | #include "xmalloc.h" | |
3c0ef626 | 30 | #include "packet.h" |
3c0ef626 | 31 | #include "log.h" |
32 | #include "servconf.h" | |
bddc9ab0 | 33 | #include "compat.h" |
3c0ef626 | 34 | #include "auth.h" |
3c0ef626 | 35 | #include "dispatch.h" |
3c0ef626 | 36 | #include "pathnames.h" |
510132b6 | 37 | #include "monitor_wrap.h" |
dfddba3d | 38 | #include "buffer.h" |
3c0ef626 | 39 | |
5598e598 | 40 | #ifdef GSSAPI |
41 | #include "ssh-gss.h" | |
42 | #endif | |
43 | ||
3c0ef626 | 44 | /* import */ |
45 | extern ServerOptions options; | |
46 | extern u_char *session_id2; | |
7cac2b65 | 47 | extern u_int session_id2_len; |
dfddba3d | 48 | extern Buffer loginmsg; |
3c0ef626 | 49 | |
44a053a3 | 50 | /* methods */ |
51 | ||
52 | extern Authmethod method_none; | |
44a053a3 | 53 | extern Authmethod method_pubkey; |
54 | extern Authmethod method_passwd; | |
55 | extern Authmethod method_kbdint; | |
56 | extern Authmethod method_hostbased; | |
db870295 | 57 | #ifdef GSSAPI |
58 | extern Authmethod method_external; | |
59 | extern Authmethod method_gssapi; | |
540d72c3 | 60 | extern Authmethod method_gssapi_compat; |
db870295 | 61 | #endif |
44a053a3 | 62 | |
63 | Authmethod *authmethods[] = { | |
64 | &method_none, | |
44a053a3 | 65 | &method_pubkey, |
7cac2b65 | 66 | #ifdef GSSAPI |
540d72c3 | 67 | &method_external, |
7cac2b65 | 68 | &method_gssapi, |
540d72c3 | 69 | &method_gssapi_compat, |
7cac2b65 | 70 | #endif |
44a053a3 | 71 | &method_passwd, |
72 | &method_kbdint, | |
73 | &method_hostbased, | |
74 | NULL | |
3c0ef626 | 75 | }; |
76 | ||
77 | /* protocol */ | |
78 | ||
1e608e42 | 79 | static void input_service_request(int, u_int32_t, void *); |
80 | static void input_userauth_request(int, u_int32_t, void *); | |
3c0ef626 | 81 | |
82 | /* helper */ | |
83 | static Authmethod *authmethod_lookup(const char *); | |
84 | static char *authmethods_get(void); | |
510132b6 | 85 | int user_key_allowed(struct passwd *, Key *); |
3c0ef626 | 86 | |
3c0ef626 | 87 | /* |
88 | * loop until authctxt->success == TRUE | |
89 | */ | |
90 | ||
540d72c3 | 91 | void |
92 | do_authentication2(Authctxt *authctxt) | |
3c0ef626 | 93 | { |
3c0ef626 | 94 | /* challenge-response is implemented via keyboard interactive */ |
95 | if (options.challenge_response_authentication) | |
96 | options.kbd_interactive_authentication = 1; | |
3c0ef626 | 97 | |
1e608e42 | 98 | dispatch_init(&dispatch_protocol_error); |
3c0ef626 | 99 | dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); |
100 | dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); | |
3c0ef626 | 101 | } |
102 | ||
103 | static void | |
1e608e42 | 104 | input_service_request(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 105 | { |
106 | Authctxt *authctxt = ctxt; | |
107 | u_int len; | |
d03f4262 | 108 | int acceptit = 0; |
3c0ef626 | 109 | char *service = packet_get_string(&len); |
1e608e42 | 110 | packet_check_eom(); |
3c0ef626 | 111 | |
112 | if (authctxt == NULL) | |
113 | fatal("input_service_request: no authctxt"); | |
114 | ||
115 | if (strcmp(service, "ssh-userauth") == 0) { | |
116 | if (!authctxt->success) { | |
d03f4262 | 117 | acceptit = 1; |
3c0ef626 | 118 | /* now we can handle user-auth requests */ |
119 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); | |
120 | } | |
121 | } | |
122 | /* XXX all other service requests are denied */ | |
123 | ||
d03f4262 | 124 | if (acceptit) { |
3c0ef626 | 125 | packet_start(SSH2_MSG_SERVICE_ACCEPT); |
126 | packet_put_cstring(service); | |
127 | packet_send(); | |
128 | packet_write_wait(); | |
129 | } else { | |
130 | debug("bad service request %s", service); | |
131 | packet_disconnect("bad service request %s", service); | |
132 | } | |
133 | xfree(service); | |
134 | } | |
135 | ||
136 | static void | |
1e608e42 | 137 | input_userauth_request(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 138 | { |
139 | Authctxt *authctxt = ctxt; | |
140 | Authmethod *m = NULL; | |
141 | char *user, *service, *method, *style = NULL; | |
142 | int authenticated = 0; | |
143 | ||
144 | if (authctxt == NULL) | |
145 | fatal("input_userauth_request: no authctxt"); | |
146 | ||
147 | user = packet_get_string(NULL); | |
148 | service = packet_get_string(NULL); | |
149 | method = packet_get_string(NULL); | |
73a68d83 | 150 | |
305d07c0 | 151 | #ifdef GSSAPI |
8f3eb4db | 152 | if (user[0] == '\0') { |
b8e8844a | 153 | debug("received empty username for %s", method); |
154 | if (strcmp(method, "external-keyx") == 0) { | |
155 | char *lname = NULL; | |
156 | PRIVSEP(ssh_gssapi_localname(&lname)); | |
157 | if (lname && lname[0] != '\0') { | |
158 | xfree(user); | |
159 | user = lname; | |
160 | debug("set username to %s from gssapi context", user); | |
9ec9d5ad | 161 | } else { |
b8e8844a | 162 | debug("failed to set username from gssapi context"); |
163 | } | |
b59afbfe | 164 | } |
165 | } | |
166 | #endif | |
73a68d83 | 167 | |
b8e8844a | 168 | debug("userauth-request for user %s service %s method %s", |
8f3eb4db | 169 | user[0] ? user : "<implicit>", service, method); |
3c0ef626 | 170 | debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); |
171 | ||
172 | if ((style = strchr(user, ':')) != NULL) | |
173 | *style++ = 0; | |
174 | ||
781f72b9 | 175 | /* If first time or username changed or implicit username, |
176 | setup/reset authentication context. */ | |
177 | if ((authctxt->attempt++ == 0) || | |
178 | (strcmp(user, authctxt->user) != 0) || | |
179 | (strcmp(user, "") == 0)) { | |
b59afbfe | 180 | if (authctxt->user) { |
181 | xfree(authctxt->user); | |
182 | authctxt->user = NULL; | |
183 | } | |
b809a58e | 184 | authctxt->valid = 0; |
b8e8844a | 185 | #ifdef GSSAPI |
9ec9d5ad | 186 | /* If we're going to set the username based on the |
187 | GSSAPI context later, then wait until then to | |
8f3eb4db | 188 | verify it. Just put in placeholders for now. */ |
189 | if ((strcmp(user, "") == 0) && | |
190 | ((strcmp(method, "gssapi") == 0) || | |
191 | (strcmp(method, "gssapi-with-mic") == 0))) { | |
192 | authctxt->pw = fakepw(); | |
193 | authctxt->user = xstrdup(user); | |
194 | } else { | |
b8e8844a | 195 | #endif |
510132b6 | 196 | authctxt->pw = PRIVSEP(getpwnamallow(user)); |
12a403af | 197 | authctxt->user = xstrdup(user); |
510132b6 | 198 | if (authctxt->pw && strcmp(service, "ssh-connection")==0) { |
3c0ef626 | 199 | authctxt->valid = 1; |
200 | debug2("input_userauth_request: setting up authctxt for %s", user); | |
201 | #ifdef USE_PAM | |
7cac2b65 | 202 | if (options.use_pam) |
12a403af | 203 | PRIVSEP(start_pam(authctxt)); |
3c0ef626 | 204 | #endif |
205 | } else { | |
7e82606e | 206 | logit("input_userauth_request: invalid user %s", user); |
7cac2b65 | 207 | authctxt->pw = fakepw(); |
3c0ef626 | 208 | #ifdef USE_PAM |
7cac2b65 | 209 | if (options.use_pam) |
12a403af | 210 | PRIVSEP(start_pam(authctxt)); |
dfddba3d | 211 | #endif |
212 | #ifdef SSH_AUDIT_EVENTS | |
213 | PRIVSEP(audit_event(SSH_INVALID_USER)); | |
3c0ef626 | 214 | #endif |
215 | } | |
b8e8844a | 216 | #ifdef GSSAPI |
9ec9d5ad | 217 | } /* endif for setting username based on GSSAPI context */ |
b8e8844a | 218 | #endif |
7e82606e | 219 | setproctitle("%s%s", authctxt->valid ? user : "unknown", |
510132b6 | 220 | use_privsep ? " [net]" : ""); |
b809a58e | 221 | #ifdef GSSAPI |
88ca8dfd | 222 | if (authctxt->attempt == 1) { |
b809a58e | 223 | #endif |
224 | authctxt->service = xstrdup(service); | |
225 | authctxt->style = style ? xstrdup(style) : NULL; | |
226 | if (use_privsep) | |
510132b6 | 227 | mm_inform_authserv(service, style); |
b809a58e | 228 | #ifdef GSSAPI |
229 | } /* if (authctxt->attempt == 1) */ | |
230 | #endif | |
885ffc2b | 231 | } |
232 | if (strcmp(service, authctxt->service) != 0) { | |
67b84e65 | 233 | packet_disconnect("Change of service not allowed: " |
234 | "(%s,%s) -> (%s,%s)", | |
235 | authctxt->user, authctxt->service, user, service); | |
3c0ef626 | 236 | } |
237 | /* reset state */ | |
1e608e42 | 238 | auth2_challenge_stop(authctxt); |
5598e598 | 239 | |
240 | #ifdef GSSAPI | |
1e608e42 | 241 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); |
242 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); | |
5598e598 | 243 | #endif |
244 | ||
3c0ef626 | 245 | authctxt->postponed = 0; |
3c0ef626 | 246 | |
247 | /* try to authenticate user */ | |
248 | m = authmethod_lookup(method); | |
249 | if (m != NULL) { | |
250 | debug2("input_userauth_request: try method %s", method); | |
251 | authenticated = m->userauth(authctxt); | |
252 | } | |
253 | userauth_finish(authctxt, authenticated, method); | |
254 | ||
255 | xfree(service); | |
256 | xfree(user); | |
257 | xfree(method); | |
258 | } | |
259 | ||
260 | void | |
261 | userauth_finish(Authctxt *authctxt, int authenticated, char *method) | |
262 | { | |
263 | char *methods; | |
264 | ||
265 | if (!authctxt->valid && authenticated) | |
266 | fatal("INTERNAL ERROR: authenticated invalid user %s", | |
267 | authctxt->user); | |
268 | ||
269 | /* Special handling for root */ | |
bfe49944 | 270 | if (authenticated && authctxt->pw->pw_uid == 0 && |
dfddba3d | 271 | !auth_root_allowed(method)) { |
3c0ef626 | 272 | authenticated = 0; |
dfddba3d | 273 | #ifdef SSH_AUDIT_EVENTS |
274 | PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED)); | |
275 | #endif | |
276 | } | |
3c0ef626 | 277 | |
278 | #ifdef USE_PAM | |
dfddba3d | 279 | if (options.use_pam && authenticated) { |
280 | if (!PRIVSEP(do_pam_account())) { | |
281 | /* if PAM returned a message, send it to the user */ | |
282 | if (buffer_len(&loginmsg) > 0) { | |
283 | buffer_append(&loginmsg, "\0", 1); | |
284 | userauth_send_banner(buffer_ptr(&loginmsg)); | |
285 | packet_write_wait(); | |
286 | } | |
287 | fatal("Access denied for user %s by PAM account " | |
2ce0bfe4 | 288 | "configuration", authctxt->user); |
dfddba3d | 289 | } |
290 | } | |
7cac2b65 | 291 | #endif |
3c0ef626 | 292 | |
d03f4262 | 293 | #ifdef _UNICOS |
294 | if (authenticated && cray_access_denied(authctxt->user)) { | |
295 | authenticated = 0; | |
296 | fatal("Access denied for user %s.",authctxt->user); | |
297 | } | |
298 | #endif /* _UNICOS */ | |
299 | ||
3c0ef626 | 300 | /* Log before sending the reply */ |
301 | auth_log(authctxt, authenticated, method, " ssh2"); | |
302 | ||
303 | if (authctxt->postponed) | |
304 | return; | |
305 | ||
306 | /* XXX todo: check if multiple auth methods are needed */ | |
307 | if (authenticated == 1) { | |
308 | /* turn off userauth */ | |
1e608e42 | 309 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore); |
3c0ef626 | 310 | packet_start(SSH2_MSG_USERAUTH_SUCCESS); |
311 | packet_send(); | |
312 | packet_write_wait(); | |
313 | /* now we can break out */ | |
314 | authctxt->success = 1; | |
315 | } else { | |
dfddba3d | 316 | if (authctxt->failures++ > options.max_authtries) { |
317 | #ifdef SSH_AUDIT_EVENTS | |
318 | PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES)); | |
319 | #endif | |
3c0ef626 | 320 | packet_disconnect(AUTH_FAIL_MSG, authctxt->user); |
dfddba3d | 321 | } |
3c0ef626 | 322 | methods = authmethods_get(); |
323 | packet_start(SSH2_MSG_USERAUTH_FAILURE); | |
324 | packet_put_cstring(methods); | |
325 | packet_put_char(0); /* XXX partial success, unused */ | |
326 | packet_send(); | |
327 | packet_write_wait(); | |
328 | xfree(methods); | |
329 | } | |
330 | } | |
331 | ||
3c0ef626 | 332 | #define DELIM "," |
333 | ||
334 | static char * | |
335 | authmethods_get(void) | |
336 | { | |
1e608e42 | 337 | Buffer b; |
3c0ef626 | 338 | char *list; |
44a053a3 | 339 | int i; |
3c0ef626 | 340 | |
1e608e42 | 341 | buffer_init(&b); |
44a053a3 | 342 | for (i = 0; authmethods[i] != NULL; i++) { |
343 | if (strcmp(authmethods[i]->name, "none") == 0) | |
3c0ef626 | 344 | continue; |
44a053a3 | 345 | if (authmethods[i]->enabled != NULL && |
346 | *(authmethods[i]->enabled) != 0) { | |
1e608e42 | 347 | if (buffer_len(&b) > 0) |
348 | buffer_append(&b, ",", 1); | |
44a053a3 | 349 | buffer_append(&b, authmethods[i]->name, |
350 | strlen(authmethods[i]->name)); | |
3c0ef626 | 351 | } |
352 | } | |
1e608e42 | 353 | buffer_append(&b, "\0", 1); |
354 | list = xstrdup(buffer_ptr(&b)); | |
355 | buffer_free(&b); | |
3c0ef626 | 356 | return list; |
357 | } | |
358 | ||
359 | static Authmethod * | |
360 | authmethod_lookup(const char *name) | |
361 | { | |
44a053a3 | 362 | int i; |
363 | ||
3c0ef626 | 364 | if (name != NULL) |
44a053a3 | 365 | for (i = 0; authmethods[i] != NULL; i++) |
366 | if (authmethods[i]->enabled != NULL && | |
367 | *(authmethods[i]->enabled) != 0 && | |
368 | strcmp(name, authmethods[i]->name) == 0) | |
369 | return authmethods[i]; | |
370 | debug2("Unrecognized authentication method name: %s", | |
371 | name ? name : "NULL"); | |
3c0ef626 | 372 | return NULL; |
373 | } |