]>
Commit | Line | Data |
---|---|---|
fa0f0f45 | 1 | /* $OpenBSD: auth2.c,v 1.115 2007/04/14 22:01:58 stevesk 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> |
29 | ||
30 | #include <pwd.h> | |
31 | #include <stdarg.h> | |
32 | #include <string.h> | |
33 | ||
3c0ef626 | 34 | #include "xmalloc.h" |
30460aeb | 35 | #include "ssh2.h" |
3c0ef626 | 36 | #include "packet.h" |
3c0ef626 | 37 | #include "log.h" |
30460aeb | 38 | #include "buffer.h" |
3c0ef626 | 39 | #include "servconf.h" |
bddc9ab0 | 40 | #include "compat.h" |
30460aeb | 41 | #include "key.h" |
42 | #include "hostfile.h" | |
3c0ef626 | 43 | #include "auth.h" |
3c0ef626 | 44 | #include "dispatch.h" |
3c0ef626 | 45 | #include "pathnames.h" |
dfddba3d | 46 | #include "buffer.h" |
3c0ef626 | 47 | |
5598e598 | 48 | #ifdef GSSAPI |
49 | #include "ssh-gss.h" | |
50 | #endif | |
30460aeb | 51 | #include "monitor_wrap.h" |
5598e598 | 52 | |
3c0ef626 | 53 | /* import */ |
54 | extern ServerOptions options; | |
55 | extern u_char *session_id2; | |
7cac2b65 | 56 | extern u_int session_id2_len; |
dfddba3d | 57 | extern Buffer loginmsg; |
3c0ef626 | 58 | |
44a053a3 | 59 | /* methods */ |
60 | ||
61 | extern Authmethod method_none; | |
44a053a3 | 62 | extern Authmethod method_pubkey; |
63 | extern Authmethod method_passwd; | |
64 | extern Authmethod method_kbdint; | |
65 | extern Authmethod method_hostbased; | |
db870295 | 66 | #ifdef GSSAPI |
67 | extern Authmethod method_external; | |
fe4ad273 | 68 | extern Authmethod method_gsskeyex; |
db870295 | 69 | extern Authmethod method_gssapi; |
540d72c3 | 70 | extern Authmethod method_gssapi_compat; |
db870295 | 71 | #endif |
44a053a3 | 72 | |
73 | Authmethod *authmethods[] = { | |
74 | &method_none, | |
44a053a3 | 75 | &method_pubkey, |
7cac2b65 | 76 | #ifdef GSSAPI |
fe4ad273 | 77 | &method_gsskeyex, |
540d72c3 | 78 | &method_external, |
7cac2b65 | 79 | &method_gssapi, |
540d72c3 | 80 | &method_gssapi_compat, |
7cac2b65 | 81 | #endif |
44a053a3 | 82 | &method_passwd, |
83 | &method_kbdint, | |
84 | &method_hostbased, | |
85 | NULL | |
3c0ef626 | 86 | }; |
87 | ||
88 | /* protocol */ | |
89 | ||
1e608e42 | 90 | static void input_service_request(int, u_int32_t, void *); |
91 | static void input_userauth_request(int, u_int32_t, void *); | |
3c0ef626 | 92 | |
93 | /* helper */ | |
94 | static Authmethod *authmethod_lookup(const char *); | |
95 | static char *authmethods_get(void); | |
510132b6 | 96 | int user_key_allowed(struct passwd *, Key *); |
3c0ef626 | 97 | |
3c0ef626 | 98 | /* |
99 | * loop until authctxt->success == TRUE | |
100 | */ | |
101 | ||
540d72c3 | 102 | void |
103 | do_authentication2(Authctxt *authctxt) | |
3c0ef626 | 104 | { |
1e608e42 | 105 | dispatch_init(&dispatch_protocol_error); |
3c0ef626 | 106 | dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); |
107 | dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); | |
3c0ef626 | 108 | } |
109 | ||
30460aeb | 110 | /*ARGSUSED*/ |
3c0ef626 | 111 | static void |
1e608e42 | 112 | input_service_request(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 113 | { |
114 | Authctxt *authctxt = ctxt; | |
115 | u_int len; | |
d03f4262 | 116 | int acceptit = 0; |
3c0ef626 | 117 | char *service = packet_get_string(&len); |
1e608e42 | 118 | packet_check_eom(); |
3c0ef626 | 119 | |
120 | if (authctxt == NULL) | |
121 | fatal("input_service_request: no authctxt"); | |
122 | ||
123 | if (strcmp(service, "ssh-userauth") == 0) { | |
124 | if (!authctxt->success) { | |
d03f4262 | 125 | acceptit = 1; |
3c0ef626 | 126 | /* now we can handle user-auth requests */ |
127 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); | |
128 | } | |
129 | } | |
130 | /* XXX all other service requests are denied */ | |
131 | ||
d03f4262 | 132 | if (acceptit) { |
3c0ef626 | 133 | packet_start(SSH2_MSG_SERVICE_ACCEPT); |
134 | packet_put_cstring(service); | |
135 | packet_send(); | |
136 | packet_write_wait(); | |
137 | } else { | |
138 | debug("bad service request %s", service); | |
139 | packet_disconnect("bad service request %s", service); | |
140 | } | |
141 | xfree(service); | |
142 | } | |
143 | ||
30460aeb | 144 | /*ARGSUSED*/ |
3c0ef626 | 145 | static void |
1e608e42 | 146 | input_userauth_request(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 147 | { |
148 | Authctxt *authctxt = ctxt; | |
149 | Authmethod *m = NULL; | |
150 | char *user, *service, *method, *style = NULL; | |
151 | int authenticated = 0; | |
152 | ||
153 | if (authctxt == NULL) | |
154 | fatal("input_userauth_request: no authctxt"); | |
155 | ||
156 | user = packet_get_string(NULL); | |
157 | service = packet_get_string(NULL); | |
158 | method = packet_get_string(NULL); | |
73a68d83 | 159 | |
305d07c0 | 160 | #ifdef GSSAPI |
8f3eb4db | 161 | if (user[0] == '\0') { |
b8e8844a | 162 | debug("received empty username for %s", method); |
fe4ad273 | 163 | if (strcmp(method, "external-keyx") == 0 || |
164 | strcmp(method, "gssapi-keyex") == 0) { | |
b8e8844a | 165 | char *lname = NULL; |
166 | PRIVSEP(ssh_gssapi_localname(&lname)); | |
167 | if (lname && lname[0] != '\0') { | |
168 | xfree(user); | |
169 | user = lname; | |
170 | debug("set username to %s from gssapi context", user); | |
9ec9d5ad | 171 | } else { |
b8e8844a | 172 | debug("failed to set username from gssapi context"); |
d9e17760 | 173 | packet_send_debug("failed to set username from gssapi context"); |
b8e8844a | 174 | } |
b59afbfe | 175 | } |
176 | } | |
177 | #endif | |
73a68d83 | 178 | |
b8e8844a | 179 | debug("userauth-request for user %s service %s method %s", |
8f3eb4db | 180 | user[0] ? user : "<implicit>", service, method); |
3c0ef626 | 181 | debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); |
182 | ||
183 | if ((style = strchr(user, ':')) != NULL) | |
184 | *style++ = 0; | |
185 | ||
86a22a0a | 186 | /* If first time or username changed or empty username, |
781f72b9 | 187 | setup/reset authentication context. */ |
188 | if ((authctxt->attempt++ == 0) || | |
189 | (strcmp(user, authctxt->user) != 0) || | |
190 | (strcmp(user, "") == 0)) { | |
b59afbfe | 191 | if (authctxt->user) { |
192 | xfree(authctxt->user); | |
193 | authctxt->user = NULL; | |
194 | } | |
b809a58e | 195 | authctxt->valid = 0; |
86a22a0a | 196 | authctxt->user = xstrdup(user); |
197 | if (strcmp(service, "ssh-connection") != 0) { | |
198 | packet_disconnect("Unsupported service %s", service); | |
199 | } | |
b8e8844a | 200 | #ifdef GSSAPI |
9ec9d5ad | 201 | /* If we're going to set the username based on the |
202 | GSSAPI context later, then wait until then to | |
8f3eb4db | 203 | verify it. Just put in placeholders for now. */ |
204 | if ((strcmp(user, "") == 0) && | |
205 | ((strcmp(method, "gssapi") == 0) || | |
206 | (strcmp(method, "gssapi-with-mic") == 0))) { | |
207 | authctxt->pw = fakepw(); | |
8f3eb4db | 208 | } else { |
b8e8844a | 209 | #endif |
510132b6 | 210 | authctxt->pw = PRIVSEP(getpwnamallow(user)); |
86a22a0a | 211 | if (authctxt->pw) { |
3c0ef626 | 212 | authctxt->valid = 1; |
213 | debug2("input_userauth_request: setting up authctxt for %s", user); | |
3c0ef626 | 214 | } else { |
7e82606e | 215 | logit("input_userauth_request: invalid user %s", user); |
7cac2b65 | 216 | authctxt->pw = fakepw(); |
dfddba3d | 217 | #ifdef SSH_AUDIT_EVENTS |
218 | PRIVSEP(audit_event(SSH_INVALID_USER)); | |
3c0ef626 | 219 | #endif |
220 | } | |
b8e8844a | 221 | #ifdef GSSAPI |
9ec9d5ad | 222 | } /* endif for setting username based on GSSAPI context */ |
08822d99 | 223 | #endif |
224 | #ifdef USE_PAM | |
225 | if (options.use_pam) | |
226 | PRIVSEP(start_pam(authctxt)); | |
b8e8844a | 227 | #endif |
7e82606e | 228 | setproctitle("%s%s", authctxt->valid ? user : "unknown", |
510132b6 | 229 | use_privsep ? " [net]" : ""); |
88ca8dfd | 230 | if (authctxt->attempt == 1) { |
86a22a0a | 231 | authctxt->service = xstrdup(service); |
232 | authctxt->style = style ? xstrdup(style) : NULL; | |
233 | if (use_privsep) | |
234 | mm_inform_authserv(service, style); | |
235 | } | |
885ffc2b | 236 | } |
237 | if (strcmp(service, authctxt->service) != 0) { | |
67b84e65 | 238 | packet_disconnect("Change of service not allowed: " |
239 | "(%s,%s) -> (%s,%s)", | |
240 | authctxt->user, authctxt->service, user, service); | |
3c0ef626 | 241 | } |
242 | /* reset state */ | |
1e608e42 | 243 | auth2_challenge_stop(authctxt); |
5598e598 | 244 | |
245 | #ifdef GSSAPI | |
1e608e42 | 246 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); |
247 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); | |
5598e598 | 248 | #endif |
249 | ||
3c0ef626 | 250 | authctxt->postponed = 0; |
fe4ad273 | 251 | authctxt->server_caused_failure = 0; |
3c0ef626 | 252 | |
253 | /* try to authenticate user */ | |
254 | m = authmethod_lookup(method); | |
255 | if (m != NULL) { | |
256 | debug2("input_userauth_request: try method %s", method); | |
257 | authenticated = m->userauth(authctxt); | |
258 | } | |
259 | userauth_finish(authctxt, authenticated, method); | |
260 | ||
261 | xfree(service); | |
262 | xfree(user); | |
263 | xfree(method); | |
264 | } | |
265 | ||
266 | void | |
267 | userauth_finish(Authctxt *authctxt, int authenticated, char *method) | |
268 | { | |
269 | char *methods; | |
270 | ||
271 | if (!authctxt->valid && authenticated) | |
272 | fatal("INTERNAL ERROR: authenticated invalid user %s", | |
273 | authctxt->user); | |
274 | ||
275 | /* Special handling for root */ | |
bfe49944 | 276 | if (authenticated && authctxt->pw->pw_uid == 0 && |
dfddba3d | 277 | !auth_root_allowed(method)) { |
3c0ef626 | 278 | authenticated = 0; |
dfddba3d | 279 | #ifdef SSH_AUDIT_EVENTS |
280 | PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED)); | |
281 | #endif | |
282 | } | |
3c0ef626 | 283 | |
284 | #ifdef USE_PAM | |
dfddba3d | 285 | if (options.use_pam && authenticated) { |
286 | if (!PRIVSEP(do_pam_account())) { | |
287 | /* if PAM returned a message, send it to the user */ | |
288 | if (buffer_len(&loginmsg) > 0) { | |
289 | buffer_append(&loginmsg, "\0", 1); | |
290 | userauth_send_banner(buffer_ptr(&loginmsg)); | |
291 | packet_write_wait(); | |
292 | } | |
293 | fatal("Access denied for user %s by PAM account " | |
2ce0bfe4 | 294 | "configuration", authctxt->user); |
dfddba3d | 295 | } |
296 | } | |
7cac2b65 | 297 | #endif |
3c0ef626 | 298 | |
d03f4262 | 299 | #ifdef _UNICOS |
300 | if (authenticated && cray_access_denied(authctxt->user)) { | |
301 | authenticated = 0; | |
302 | fatal("Access denied for user %s.",authctxt->user); | |
303 | } | |
304 | #endif /* _UNICOS */ | |
305 | ||
3c0ef626 | 306 | /* Log before sending the reply */ |
307 | auth_log(authctxt, authenticated, method, " ssh2"); | |
308 | ||
309 | if (authctxt->postponed) | |
310 | return; | |
311 | ||
312 | /* XXX todo: check if multiple auth methods are needed */ | |
313 | if (authenticated == 1) { | |
314 | /* turn off userauth */ | |
1e608e42 | 315 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore); |
3c0ef626 | 316 | packet_start(SSH2_MSG_USERAUTH_SUCCESS); |
317 | packet_send(); | |
318 | packet_write_wait(); | |
319 | /* now we can break out */ | |
320 | authctxt->success = 1; | |
321 | } else { | |
fe4ad273 | 322 | /* Dont count server configuration issues against the client */ |
323 | if (!authctxt->server_caused_failure && | |
324 | authctxt->failures++ > options.max_authtries) { | |
dfddba3d | 325 | #ifdef SSH_AUDIT_EVENTS |
326 | PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES)); | |
327 | #endif | |
3c0ef626 | 328 | packet_disconnect(AUTH_FAIL_MSG, authctxt->user); |
dfddba3d | 329 | } |
3c0ef626 | 330 | methods = authmethods_get(); |
331 | packet_start(SSH2_MSG_USERAUTH_FAILURE); | |
332 | packet_put_cstring(methods); | |
333 | packet_put_char(0); /* XXX partial success, unused */ | |
334 | packet_send(); | |
335 | packet_write_wait(); | |
336 | xfree(methods); | |
337 | } | |
338 | } | |
339 | ||
3c0ef626 | 340 | static char * |
341 | authmethods_get(void) | |
342 | { | |
1e608e42 | 343 | Buffer b; |
3c0ef626 | 344 | char *list; |
44a053a3 | 345 | int i; |
3c0ef626 | 346 | |
1e608e42 | 347 | buffer_init(&b); |
44a053a3 | 348 | for (i = 0; authmethods[i] != NULL; i++) { |
349 | if (strcmp(authmethods[i]->name, "none") == 0) | |
3c0ef626 | 350 | continue; |
44a053a3 | 351 | if (authmethods[i]->enabled != NULL && |
352 | *(authmethods[i]->enabled) != 0) { | |
1e608e42 | 353 | if (buffer_len(&b) > 0) |
354 | buffer_append(&b, ",", 1); | |
44a053a3 | 355 | buffer_append(&b, authmethods[i]->name, |
356 | strlen(authmethods[i]->name)); | |
3c0ef626 | 357 | } |
358 | } | |
1e608e42 | 359 | buffer_append(&b, "\0", 1); |
360 | list = xstrdup(buffer_ptr(&b)); | |
361 | buffer_free(&b); | |
3c0ef626 | 362 | return list; |
363 | } | |
364 | ||
365 | static Authmethod * | |
366 | authmethod_lookup(const char *name) | |
367 | { | |
44a053a3 | 368 | int i; |
369 | ||
3c0ef626 | 370 | if (name != NULL) |
44a053a3 | 371 | for (i = 0; authmethods[i] != NULL; i++) |
372 | if (authmethods[i]->enabled != NULL && | |
373 | *(authmethods[i]->enabled) != 0 && | |
374 | strcmp(name, authmethods[i]->name) == 0) | |
375 | return authmethods[i]; | |
376 | debug2("Unrecognized authentication method name: %s", | |
377 | name ? name : "NULL"); | |
3c0ef626 | 378 | return NULL; |
379 | } |