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