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