]>
Commit | Line | Data |
---|---|---|
b5afdff5 | 1 | /* $OpenBSD: auth2.c,v 1.121 2009/06/22 05:39:28 dtucker Exp $ */ |
3c0ef626 | 2 | /* |
3 | * Copyright (c) 2000 Markus Friedl. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * | |
14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
16 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
17 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
18 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
19 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
20 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
21 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
24 | */ | |
25 | ||
26 | #include "includes.h" | |
3c0ef626 | 27 | |
30460aeb | 28 | #include <sys/types.h> |
5156b1a1 | 29 | #include <sys/stat.h> |
30 | #include <sys/uio.h> | |
30460aeb | 31 | |
5156b1a1 | 32 | #include <fcntl.h> |
30460aeb | 33 | #include <pwd.h> |
34 | #include <stdarg.h> | |
35 | #include <string.h> | |
5156b1a1 | 36 | #include <unistd.h> |
30460aeb | 37 | |
5156b1a1 | 38 | #include "atomicio.h" |
b5afdff5 | 39 | #include "xmalloc.h" |
30460aeb | 40 | #include "ssh2.h" |
3c0ef626 | 41 | #include "packet.h" |
3c0ef626 | 42 | #include "log.h" |
30460aeb | 43 | #include "buffer.h" |
3c0ef626 | 44 | #include "servconf.h" |
bddc9ab0 | 45 | #include "compat.h" |
30460aeb | 46 | #include "key.h" |
47 | #include "hostfile.h" | |
3c0ef626 | 48 | #include "auth.h" |
3c0ef626 | 49 | #include "dispatch.h" |
3c0ef626 | 50 | #include "pathnames.h" |
dfddba3d | 51 | #include "buffer.h" |
fa7499cc | 52 | #include "canohost.h" |
3c0ef626 | 53 | |
5598e598 | 54 | #ifdef GSSAPI |
55 | #include "ssh-gss.h" | |
56 | #endif | |
30460aeb | 57 | #include "monitor_wrap.h" |
5598e598 | 58 | |
3c0ef626 | 59 | /* import */ |
60 | extern ServerOptions options; | |
61 | extern u_char *session_id2; | |
7cac2b65 | 62 | extern u_int session_id2_len; |
dfddba3d | 63 | extern Buffer loginmsg; |
3c0ef626 | 64 | |
44a053a3 | 65 | /* methods */ |
66 | ||
67 | extern Authmethod method_none; | |
44a053a3 | 68 | extern Authmethod method_pubkey; |
69 | extern Authmethod method_passwd; | |
70 | extern Authmethod method_kbdint; | |
71 | extern Authmethod method_hostbased; | |
db870295 | 72 | #ifdef GSSAPI |
fe4ad273 | 73 | extern Authmethod method_gsskeyex; |
db870295 | 74 | extern Authmethod method_gssapi; |
75 | #endif | |
5262cbfb | 76 | #ifdef JPAKE |
77 | extern Authmethod method_jpake; | |
78 | #endif | |
44a053a3 | 79 | |
fa7499cc | 80 | static int log_flag = 0; |
81 | ||
82 | ||
44a053a3 | 83 | Authmethod *authmethods[] = { |
84 | &method_none, | |
44a053a3 | 85 | &method_pubkey, |
7cac2b65 | 86 | #ifdef GSSAPI |
fe4ad273 | 87 | &method_gsskeyex, |
7cac2b65 | 88 | &method_gssapi, |
5262cbfb | 89 | #endif |
90 | #ifdef JPAKE | |
91 | &method_jpake, | |
7cac2b65 | 92 | #endif |
44a053a3 | 93 | &method_passwd, |
94 | &method_kbdint, | |
95 | &method_hostbased, | |
96 | NULL | |
3c0ef626 | 97 | }; |
98 | ||
99 | /* protocol */ | |
100 | ||
1e608e42 | 101 | static void input_service_request(int, u_int32_t, void *); |
102 | static void input_userauth_request(int, u_int32_t, void *); | |
3c0ef626 | 103 | |
104 | /* helper */ | |
105 | static Authmethod *authmethod_lookup(const char *); | |
106 | static char *authmethods_get(void); | |
3c0ef626 | 107 | |
5156b1a1 | 108 | char * |
109 | auth2_read_banner(void) | |
110 | { | |
111 | struct stat st; | |
112 | char *banner = NULL; | |
113 | size_t len, n; | |
114 | int fd; | |
115 | ||
116 | if ((fd = open(options.banner, O_RDONLY)) == -1) | |
117 | return (NULL); | |
118 | if (fstat(fd, &st) == -1) { | |
119 | close(fd); | |
120 | return (NULL); | |
121 | } | |
122 | if (st.st_size > 1*1024*1024) { | |
123 | close(fd); | |
124 | return (NULL); | |
125 | } | |
126 | ||
127 | len = (size_t)st.st_size; /* truncate */ | |
128 | banner = xmalloc(len + 1); | |
129 | n = atomicio(read, fd, banner, len); | |
130 | close(fd); | |
131 | ||
132 | if (n != len) { | |
133 | xfree(banner); | |
134 | return (NULL); | |
135 | } | |
136 | banner[n] = '\0'; | |
137 | ||
138 | return (banner); | |
139 | } | |
140 | ||
141 | void | |
142 | userauth_send_banner(const char *msg) | |
143 | { | |
144 | if (datafellows & SSH_BUG_BANNER) | |
145 | return; | |
146 | ||
147 | packet_start(SSH2_MSG_USERAUTH_BANNER); | |
148 | packet_put_cstring(msg); | |
149 | packet_put_cstring(""); /* language, unused */ | |
150 | packet_send(); | |
151 | debug("%s: sent", __func__); | |
152 | } | |
153 | ||
154 | static void | |
155 | userauth_banner(void) | |
156 | { | |
157 | char *banner = NULL; | |
158 | ||
159 | if (options.banner == NULL || | |
160 | strcasecmp(options.banner, "none") == 0 || | |
161 | (datafellows & SSH_BUG_BANNER) != 0) | |
162 | return; | |
163 | ||
164 | if ((banner = PRIVSEP(auth2_read_banner())) == NULL) | |
165 | goto done; | |
166 | userauth_send_banner(banner); | |
167 | ||
168 | done: | |
169 | if (banner) | |
170 | xfree(banner); | |
171 | } | |
172 | ||
3c0ef626 | 173 | /* |
174 | * loop until authctxt->success == TRUE | |
175 | */ | |
540d72c3 | 176 | void |
177 | do_authentication2(Authctxt *authctxt) | |
3c0ef626 | 178 | { |
1e608e42 | 179 | dispatch_init(&dispatch_protocol_error); |
3c0ef626 | 180 | dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); |
181 | dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); | |
3c0ef626 | 182 | } |
183 | ||
30460aeb | 184 | /*ARGSUSED*/ |
3c0ef626 | 185 | static void |
1e608e42 | 186 | input_service_request(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 187 | { |
188 | Authctxt *authctxt = ctxt; | |
189 | u_int len; | |
d03f4262 | 190 | int acceptit = 0; |
3c0ef626 | 191 | char *service = packet_get_string(&len); |
1e608e42 | 192 | packet_check_eom(); |
3c0ef626 | 193 | |
194 | if (authctxt == NULL) | |
195 | fatal("input_service_request: no authctxt"); | |
196 | ||
197 | if (strcmp(service, "ssh-userauth") == 0) { | |
198 | if (!authctxt->success) { | |
d03f4262 | 199 | acceptit = 1; |
3c0ef626 | 200 | /* now we can handle user-auth requests */ |
201 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); | |
202 | } | |
203 | } | |
204 | /* XXX all other service requests are denied */ | |
205 | ||
d03f4262 | 206 | if (acceptit) { |
3c0ef626 | 207 | packet_start(SSH2_MSG_SERVICE_ACCEPT); |
208 | packet_put_cstring(service); | |
209 | packet_send(); | |
210 | packet_write_wait(); | |
211 | } else { | |
212 | debug("bad service request %s", service); | |
213 | packet_disconnect("bad service request %s", service); | |
214 | } | |
215 | xfree(service); | |
216 | } | |
217 | ||
30460aeb | 218 | /*ARGSUSED*/ |
3c0ef626 | 219 | static void |
1e608e42 | 220 | input_userauth_request(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 221 | { |
222 | Authctxt *authctxt = ctxt; | |
223 | Authmethod *m = NULL; | |
224 | char *user, *service, *method, *style = NULL; | |
225 | int authenticated = 0; | |
226 | ||
227 | if (authctxt == NULL) | |
228 | fatal("input_userauth_request: no authctxt"); | |
229 | ||
230 | user = packet_get_string(NULL); | |
231 | service = packet_get_string(NULL); | |
232 | method = packet_get_string(NULL); | |
73a68d83 | 233 | |
305d07c0 | 234 | #ifdef GSSAPI |
8f3eb4db | 235 | if (user[0] == '\0') { |
b8e8844a | 236 | debug("received empty username for %s", method); |
22ce3a3b | 237 | if (strcmp(method, "gssapi-keyex") == 0) { |
b8e8844a | 238 | char *lname = NULL; |
239 | PRIVSEP(ssh_gssapi_localname(&lname)); | |
240 | if (lname && lname[0] != '\0') { | |
241 | xfree(user); | |
242 | user = lname; | |
243 | debug("set username to %s from gssapi context", user); | |
9ec9d5ad | 244 | } else { |
b8e8844a | 245 | debug("failed to set username from gssapi context"); |
d9e17760 | 246 | packet_send_debug("failed to set username from gssapi context"); |
b8e8844a | 247 | } |
b59afbfe | 248 | } |
249 | } | |
250 | #endif | |
73a68d83 | 251 | |
b8e8844a | 252 | debug("userauth-request for user %s service %s method %s", |
8f3eb4db | 253 | user[0] ? user : "<implicit>", service, method); |
fa7499cc | 254 | if (!log_flag) { |
255 | logit("SSH: Server;Ltype: Authname;Remote: %s-%d;Name: %s", | |
d3057ca4 | 256 | get_remote_ipaddr(), get_remote_port(), |
257 | user[0] ? user : "<implicit>"); | |
fa7499cc | 258 | log_flag = 1; |
259 | } | |
3c0ef626 | 260 | debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); |
261 | ||
262 | if ((style = strchr(user, ':')) != NULL) | |
263 | *style++ = 0; | |
264 | ||
86a22a0a | 265 | /* If first time or username changed or empty username, |
781f72b9 | 266 | setup/reset authentication context. */ |
267 | if ((authctxt->attempt++ == 0) || | |
268 | (strcmp(user, authctxt->user) != 0) || | |
269 | (strcmp(user, "") == 0)) { | |
b59afbfe | 270 | if (authctxt->user) { |
271 | xfree(authctxt->user); | |
272 | authctxt->user = NULL; | |
273 | } | |
b809a58e | 274 | authctxt->valid = 0; |
86a22a0a | 275 | authctxt->user = xstrdup(user); |
276 | if (strcmp(service, "ssh-connection") != 0) { | |
277 | packet_disconnect("Unsupported service %s", service); | |
278 | } | |
b8e8844a | 279 | #ifdef GSSAPI |
9ec9d5ad | 280 | /* If we're going to set the username based on the |
281 | GSSAPI context later, then wait until then to | |
8f3eb4db | 282 | verify it. Just put in placeholders for now. */ |
283 | if ((strcmp(user, "") == 0) && | |
284 | ((strcmp(method, "gssapi") == 0) || | |
285 | (strcmp(method, "gssapi-with-mic") == 0))) { | |
286 | authctxt->pw = fakepw(); | |
8f3eb4db | 287 | } else { |
b8e8844a | 288 | #endif |
510132b6 | 289 | authctxt->pw = PRIVSEP(getpwnamallow(user)); |
86a22a0a | 290 | if (authctxt->pw) { |
3c0ef626 | 291 | authctxt->valid = 1; |
292 | debug2("input_userauth_request: setting up authctxt for %s", user); | |
3c0ef626 | 293 | } else { |
7e82606e | 294 | logit("input_userauth_request: invalid user %s", user); |
7cac2b65 | 295 | authctxt->pw = fakepw(); |
dfddba3d | 296 | #ifdef SSH_AUDIT_EVENTS |
297 | PRIVSEP(audit_event(SSH_INVALID_USER)); | |
3c0ef626 | 298 | #endif |
299 | } | |
b8e8844a | 300 | #ifdef GSSAPI |
9ec9d5ad | 301 | } /* endif for setting username based on GSSAPI context */ |
08822d99 | 302 | #endif |
303 | #ifdef USE_PAM | |
304 | if (options.use_pam) | |
305 | PRIVSEP(start_pam(authctxt)); | |
b8e8844a | 306 | #endif |
7e82606e | 307 | setproctitle("%s%s", authctxt->valid ? user : "unknown", |
510132b6 | 308 | use_privsep ? " [net]" : ""); |
88ca8dfd | 309 | if (authctxt->attempt == 1) { |
86a22a0a | 310 | authctxt->service = xstrdup(service); |
311 | authctxt->style = style ? xstrdup(style) : NULL; | |
312 | if (use_privsep) | |
313 | mm_inform_authserv(service, style); | |
5156b1a1 | 314 | userauth_banner(); |
86a22a0a | 315 | } |
885ffc2b | 316 | } |
317 | if (strcmp(service, authctxt->service) != 0) { | |
67b84e65 | 318 | packet_disconnect("Change of service not allowed: " |
319 | "(%s,%s) -> (%s,%s)", | |
320 | authctxt->user, authctxt->service, user, service); | |
3c0ef626 | 321 | } |
322 | /* reset state */ | |
1e608e42 | 323 | auth2_challenge_stop(authctxt); |
5262cbfb | 324 | #ifdef JPAKE |
325 | auth2_jpake_stop(authctxt); | |
326 | #endif | |
5598e598 | 327 | |
328 | #ifdef GSSAPI | |
5262cbfb | 329 | /* XXX move to auth2_gssapi_stop() */ |
1e608e42 | 330 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); |
331 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); | |
5598e598 | 332 | #endif |
333 | ||
3c0ef626 | 334 | authctxt->postponed = 0; |
fe4ad273 | 335 | authctxt->server_caused_failure = 0; |
3c0ef626 | 336 | |
337 | /* try to authenticate user */ | |
338 | m = authmethod_lookup(method); | |
5156b1a1 | 339 | if (m != NULL && authctxt->failures < options.max_authtries) { |
3c0ef626 | 340 | debug2("input_userauth_request: try method %s", method); |
341 | authenticated = m->userauth(authctxt); | |
342 | } | |
343 | userauth_finish(authctxt, authenticated, method); | |
344 | ||
345 | xfree(service); | |
346 | xfree(user); | |
347 | xfree(method); | |
348 | } | |
349 | ||
350 | void | |
351 | userauth_finish(Authctxt *authctxt, int authenticated, char *method) | |
352 | { | |
353 | char *methods; | |
354 | ||
355 | if (!authctxt->valid && authenticated) | |
356 | fatal("INTERNAL ERROR: authenticated invalid user %s", | |
357 | authctxt->user); | |
358 | ||
359 | /* Special handling for root */ | |
bfe49944 | 360 | if (authenticated && authctxt->pw->pw_uid == 0 && |
dfddba3d | 361 | !auth_root_allowed(method)) { |
3c0ef626 | 362 | authenticated = 0; |
dfddba3d | 363 | #ifdef SSH_AUDIT_EVENTS |
364 | PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED)); | |
365 | #endif | |
366 | } | |
3c0ef626 | 367 | |
368 | #ifdef USE_PAM | |
dfddba3d | 369 | if (options.use_pam && authenticated) { |
370 | if (!PRIVSEP(do_pam_account())) { | |
371 | /* if PAM returned a message, send it to the user */ | |
372 | if (buffer_len(&loginmsg) > 0) { | |
373 | buffer_append(&loginmsg, "\0", 1); | |
374 | userauth_send_banner(buffer_ptr(&loginmsg)); | |
375 | packet_write_wait(); | |
376 | } | |
377 | fatal("Access denied for user %s by PAM account " | |
2ce0bfe4 | 378 | "configuration", authctxt->user); |
dfddba3d | 379 | } |
380 | } | |
7cac2b65 | 381 | #endif |
3c0ef626 | 382 | |
d03f4262 | 383 | #ifdef _UNICOS |
384 | if (authenticated && cray_access_denied(authctxt->user)) { | |
385 | authenticated = 0; | |
386 | fatal("Access denied for user %s.",authctxt->user); | |
387 | } | |
388 | #endif /* _UNICOS */ | |
389 | ||
3c0ef626 | 390 | /* Log before sending the reply */ |
391 | auth_log(authctxt, authenticated, method, " ssh2"); | |
392 | ||
393 | if (authctxt->postponed) | |
394 | return; | |
395 | ||
396 | /* XXX todo: check if multiple auth methods are needed */ | |
397 | if (authenticated == 1) { | |
398 | /* turn off userauth */ | |
1e608e42 | 399 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore); |
3c0ef626 | 400 | packet_start(SSH2_MSG_USERAUTH_SUCCESS); |
401 | packet_send(); | |
402 | packet_write_wait(); | |
403 | /* now we can break out */ | |
404 | authctxt->success = 1; | |
405 | } else { | |
fe4ad273 | 406 | /* Dont count server configuration issues against the client */ |
5156b1a1 | 407 | /* Allow initial try of "none" auth without failure penalty */ |
408 | if (!authctxt->server_caused_failure && | |
f97edba6 | 409 | (authctxt->attempt > 1 || strcmp(method, "none") != 0)) |
5156b1a1 | 410 | authctxt->failures++; |
411 | if (authctxt->failures >= options.max_authtries) { | |
dfddba3d | 412 | #ifdef SSH_AUDIT_EVENTS |
413 | PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES)); | |
414 | #endif | |
3c0ef626 | 415 | packet_disconnect(AUTH_FAIL_MSG, authctxt->user); |
dfddba3d | 416 | } |
3c0ef626 | 417 | methods = authmethods_get(); |
418 | packet_start(SSH2_MSG_USERAUTH_FAILURE); | |
419 | packet_put_cstring(methods); | |
420 | packet_put_char(0); /* XXX partial success, unused */ | |
421 | packet_send(); | |
422 | packet_write_wait(); | |
423 | xfree(methods); | |
424 | } | |
425 | } | |
426 | ||
3c0ef626 | 427 | static char * |
428 | authmethods_get(void) | |
429 | { | |
1e608e42 | 430 | Buffer b; |
3c0ef626 | 431 | char *list; |
44a053a3 | 432 | int i; |
3c0ef626 | 433 | |
1e608e42 | 434 | buffer_init(&b); |
44a053a3 | 435 | for (i = 0; authmethods[i] != NULL; i++) { |
436 | if (strcmp(authmethods[i]->name, "none") == 0) | |
3c0ef626 | 437 | continue; |
44a053a3 | 438 | if (authmethods[i]->enabled != NULL && |
439 | *(authmethods[i]->enabled) != 0) { | |
1e608e42 | 440 | if (buffer_len(&b) > 0) |
441 | buffer_append(&b, ",", 1); | |
44a053a3 | 442 | buffer_append(&b, authmethods[i]->name, |
443 | strlen(authmethods[i]->name)); | |
3c0ef626 | 444 | } |
445 | } | |
1e608e42 | 446 | buffer_append(&b, "\0", 1); |
447 | list = xstrdup(buffer_ptr(&b)); | |
448 | buffer_free(&b); | |
3c0ef626 | 449 | return list; |
450 | } | |
451 | ||
452 | static Authmethod * | |
453 | authmethod_lookup(const char *name) | |
454 | { | |
44a053a3 | 455 | int i; |
456 | ||
3c0ef626 | 457 | if (name != NULL) |
44a053a3 | 458 | for (i = 0; authmethods[i] != NULL; i++) |
459 | if (authmethods[i]->enabled != NULL && | |
460 | *(authmethods[i]->enabled) != 0 && | |
461 | strcmp(name, authmethods[i]->name) == 0) | |
462 | return authmethods[i]; | |
463 | debug2("Unrecognized authentication method name: %s", | |
464 | name ? name : "NULL"); | |
3c0ef626 | 465 | return NULL; |
466 | } | |
5156b1a1 | 467 |