]>
Commit | Line | Data |
---|---|---|
1 | /* $OpenBSD: auth2.c,v 1.119 2008/07/04 23:30:16 djm Exp $ */ | |
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" | |
27 | ||
28 | #include <sys/types.h> | |
29 | #include <sys/stat.h> | |
30 | #include <sys/uio.h> | |
31 | ||
32 | #include <fcntl.h> | |
33 | #include <pwd.h> | |
34 | #include <stdarg.h> | |
35 | #include <string.h> | |
36 | #include <unistd.h> | |
37 | ||
38 | #include "xmalloc.h" | |
39 | #include "atomicio.h" | |
40 | #include "ssh2.h" | |
41 | #include "packet.h" | |
42 | #include "log.h" | |
43 | #include "buffer.h" | |
44 | #include "servconf.h" | |
45 | #include "compat.h" | |
46 | #include "key.h" | |
47 | #include "hostfile.h" | |
48 | #include "auth.h" | |
49 | #include "dispatch.h" | |
50 | #include "pathnames.h" | |
51 | #include "buffer.h" | |
52 | #include "canohost.h" | |
53 | ||
54 | #ifdef GSSAPI | |
55 | #include "ssh-gss.h" | |
56 | #endif | |
57 | #include "monitor_wrap.h" | |
58 | ||
59 | /* import */ | |
60 | extern ServerOptions options; | |
61 | extern u_char *session_id2; | |
62 | extern u_int session_id2_len; | |
63 | extern Buffer loginmsg; | |
64 | ||
65 | /* methods */ | |
66 | ||
67 | extern Authmethod method_none; | |
68 | extern Authmethod method_pubkey; | |
69 | extern Authmethod method_passwd; | |
70 | extern Authmethod method_kbdint; | |
71 | extern Authmethod method_hostbased; | |
72 | #ifdef GSSAPI | |
73 | extern Authmethod method_external; | |
74 | extern Authmethod method_gsskeyex; | |
75 | extern Authmethod method_gssapi; | |
76 | extern Authmethod method_gssapi_compat; | |
77 | #endif | |
78 | ||
79 | static int log_flag = 0; | |
80 | ||
81 | ||
82 | Authmethod *authmethods[] = { | |
83 | &method_none, | |
84 | &method_pubkey, | |
85 | #ifdef GSSAPI | |
86 | &method_gsskeyex, | |
87 | &method_external, | |
88 | &method_gssapi, | |
89 | &method_gssapi_compat, | |
90 | #endif | |
91 | &method_passwd, | |
92 | &method_kbdint, | |
93 | &method_hostbased, | |
94 | NULL | |
95 | }; | |
96 | ||
97 | /* protocol */ | |
98 | ||
99 | static void input_service_request(int, u_int32_t, void *); | |
100 | static void input_userauth_request(int, u_int32_t, void *); | |
101 | ||
102 | /* helper */ | |
103 | static Authmethod *authmethod_lookup(const char *); | |
104 | static char *authmethods_get(void); | |
105 | ||
106 | char * | |
107 | auth2_read_banner(void) | |
108 | { | |
109 | struct stat st; | |
110 | char *banner = NULL; | |
111 | size_t len, n; | |
112 | int fd; | |
113 | ||
114 | if ((fd = open(options.banner, O_RDONLY)) == -1) | |
115 | return (NULL); | |
116 | if (fstat(fd, &st) == -1) { | |
117 | close(fd); | |
118 | return (NULL); | |
119 | } | |
120 | if (st.st_size > 1*1024*1024) { | |
121 | close(fd); | |
122 | return (NULL); | |
123 | } | |
124 | ||
125 | len = (size_t)st.st_size; /* truncate */ | |
126 | banner = xmalloc(len + 1); | |
127 | n = atomicio(read, fd, banner, len); | |
128 | close(fd); | |
129 | ||
130 | if (n != len) { | |
131 | xfree(banner); | |
132 | return (NULL); | |
133 | } | |
134 | banner[n] = '\0'; | |
135 | ||
136 | return (banner); | |
137 | } | |
138 | ||
139 | void | |
140 | userauth_send_banner(const char *msg) | |
141 | { | |
142 | if (datafellows & SSH_BUG_BANNER) | |
143 | return; | |
144 | ||
145 | packet_start(SSH2_MSG_USERAUTH_BANNER); | |
146 | packet_put_cstring(msg); | |
147 | packet_put_cstring(""); /* language, unused */ | |
148 | packet_send(); | |
149 | debug("%s: sent", __func__); | |
150 | } | |
151 | ||
152 | static void | |
153 | userauth_banner(void) | |
154 | { | |
155 | char *banner = NULL; | |
156 | ||
157 | if (options.banner == NULL || | |
158 | strcasecmp(options.banner, "none") == 0 || | |
159 | (datafellows & SSH_BUG_BANNER) != 0) | |
160 | return; | |
161 | ||
162 | if ((banner = PRIVSEP(auth2_read_banner())) == NULL) | |
163 | goto done; | |
164 | userauth_send_banner(banner); | |
165 | ||
166 | done: | |
167 | if (banner) | |
168 | xfree(banner); | |
169 | } | |
170 | ||
171 | /* | |
172 | * loop until authctxt->success == TRUE | |
173 | */ | |
174 | void | |
175 | do_authentication2(Authctxt *authctxt) | |
176 | { | |
177 | dispatch_init(&dispatch_protocol_error); | |
178 | dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); | |
179 | dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); | |
180 | } | |
181 | ||
182 | /*ARGSUSED*/ | |
183 | static void | |
184 | input_service_request(int type, u_int32_t seq, void *ctxt) | |
185 | { | |
186 | Authctxt *authctxt = ctxt; | |
187 | u_int len; | |
188 | int acceptit = 0; | |
189 | char *service = packet_get_string(&len); | |
190 | packet_check_eom(); | |
191 | ||
192 | if (authctxt == NULL) | |
193 | fatal("input_service_request: no authctxt"); | |
194 | ||
195 | if (strcmp(service, "ssh-userauth") == 0) { | |
196 | if (!authctxt->success) { | |
197 | acceptit = 1; | |
198 | /* now we can handle user-auth requests */ | |
199 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); | |
200 | } | |
201 | } | |
202 | /* XXX all other service requests are denied */ | |
203 | ||
204 | if (acceptit) { | |
205 | packet_start(SSH2_MSG_SERVICE_ACCEPT); | |
206 | packet_put_cstring(service); | |
207 | packet_send(); | |
208 | packet_write_wait(); | |
209 | } else { | |
210 | debug("bad service request %s", service); | |
211 | packet_disconnect("bad service request %s", service); | |
212 | } | |
213 | xfree(service); | |
214 | } | |
215 | ||
216 | /*ARGSUSED*/ | |
217 | static void | |
218 | input_userauth_request(int type, u_int32_t seq, void *ctxt) | |
219 | { | |
220 | Authctxt *authctxt = ctxt; | |
221 | Authmethod *m = NULL; | |
222 | char *user, *service, *method, *style = NULL; | |
223 | int authenticated = 0; | |
224 | ||
225 | if (authctxt == NULL) | |
226 | fatal("input_userauth_request: no authctxt"); | |
227 | ||
228 | user = packet_get_string(NULL); | |
229 | service = packet_get_string(NULL); | |
230 | method = packet_get_string(NULL); | |
231 | ||
232 | #ifdef GSSAPI | |
233 | if (user[0] == '\0') { | |
234 | debug("received empty username for %s", method); | |
235 | if (strcmp(method, "external-keyx") == 0 || | |
236 | strcmp(method, "gssapi-keyex") == 0) { | |
237 | char *lname = NULL; | |
238 | PRIVSEP(ssh_gssapi_localname(&lname)); | |
239 | if (lname && lname[0] != '\0') { | |
240 | xfree(user); | |
241 | user = lname; | |
242 | debug("set username to %s from gssapi context", user); | |
243 | } else { | |
244 | debug("failed to set username from gssapi context"); | |
245 | packet_send_debug("failed to set username from gssapi context"); | |
246 | } | |
247 | } | |
248 | } | |
249 | #endif | |
250 | ||
251 | debug("userauth-request for user %s service %s method %s", | |
252 | user[0] ? user : "<implicit>", service, method); | |
253 | if (!log_flag) { | |
254 | logit("SSH: Server;Ltype: Authname;Remote: %s-%d;Name: %s", | |
255 | get_remote_ipaddr(), get_remote_port(), user); | |
256 | log_flag = 1; | |
257 | } | |
258 | debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); | |
259 | ||
260 | if ((style = strchr(user, ':')) != NULL) | |
261 | *style++ = 0; | |
262 | ||
263 | /* If first time or username changed or empty username, | |
264 | setup/reset authentication context. */ | |
265 | if ((authctxt->attempt++ == 0) || | |
266 | (strcmp(user, authctxt->user) != 0) || | |
267 | (strcmp(user, "") == 0)) { | |
268 | if (authctxt->user) { | |
269 | xfree(authctxt->user); | |
270 | authctxt->user = NULL; | |
271 | } | |
272 | authctxt->valid = 0; | |
273 | authctxt->user = xstrdup(user); | |
274 | if (strcmp(service, "ssh-connection") != 0) { | |
275 | packet_disconnect("Unsupported service %s", service); | |
276 | } | |
277 | #ifdef GSSAPI | |
278 | /* If we're going to set the username based on the | |
279 | GSSAPI context later, then wait until then to | |
280 | verify it. Just put in placeholders for now. */ | |
281 | if ((strcmp(user, "") == 0) && | |
282 | ((strcmp(method, "gssapi") == 0) || | |
283 | (strcmp(method, "gssapi-with-mic") == 0))) { | |
284 | authctxt->pw = fakepw(); | |
285 | } else { | |
286 | #endif | |
287 | authctxt->pw = PRIVSEP(getpwnamallow(user)); | |
288 | if (authctxt->pw) { | |
289 | authctxt->valid = 1; | |
290 | debug2("input_userauth_request: setting up authctxt for %s", user); | |
291 | } else { | |
292 | logit("input_userauth_request: invalid user %s", user); | |
293 | authctxt->pw = fakepw(); | |
294 | #ifdef SSH_AUDIT_EVENTS | |
295 | PRIVSEP(audit_event(SSH_INVALID_USER)); | |
296 | #endif | |
297 | } | |
298 | #ifdef GSSAPI | |
299 | } /* endif for setting username based on GSSAPI context */ | |
300 | #endif | |
301 | #ifdef USE_PAM | |
302 | if (options.use_pam) | |
303 | PRIVSEP(start_pam(authctxt)); | |
304 | #endif | |
305 | setproctitle("%s%s", authctxt->valid ? user : "unknown", | |
306 | use_privsep ? " [net]" : ""); | |
307 | if (authctxt->attempt == 1) { | |
308 | authctxt->service = xstrdup(service); | |
309 | authctxt->style = style ? xstrdup(style) : NULL; | |
310 | if (use_privsep) | |
311 | mm_inform_authserv(service, style); | |
312 | userauth_banner(); | |
313 | } | |
314 | } | |
315 | if (strcmp(service, authctxt->service) != 0) { | |
316 | packet_disconnect("Change of service not allowed: " | |
317 | "(%s,%s) -> (%s,%s)", | |
318 | authctxt->user, authctxt->service, user, service); | |
319 | } | |
320 | /* reset state */ | |
321 | auth2_challenge_stop(authctxt); | |
322 | ||
323 | #ifdef GSSAPI | |
324 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); | |
325 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); | |
326 | #endif | |
327 | ||
328 | authctxt->postponed = 0; | |
329 | authctxt->server_caused_failure = 0; | |
330 | ||
331 | /* try to authenticate user */ | |
332 | m = authmethod_lookup(method); | |
333 | if (m != NULL && authctxt->failures < options.max_authtries) { | |
334 | debug2("input_userauth_request: try method %s", method); | |
335 | authenticated = m->userauth(authctxt); | |
336 | } | |
337 | userauth_finish(authctxt, authenticated, method); | |
338 | ||
339 | xfree(service); | |
340 | xfree(user); | |
341 | xfree(method); | |
342 | } | |
343 | ||
344 | void | |
345 | userauth_finish(Authctxt *authctxt, int authenticated, char *method) | |
346 | { | |
347 | char *methods; | |
348 | ||
349 | if (!authctxt->valid && authenticated) | |
350 | fatal("INTERNAL ERROR: authenticated invalid user %s", | |
351 | authctxt->user); | |
352 | ||
353 | /* Special handling for root */ | |
354 | if (authenticated && authctxt->pw->pw_uid == 0 && | |
355 | !auth_root_allowed(method)) { | |
356 | authenticated = 0; | |
357 | #ifdef SSH_AUDIT_EVENTS | |
358 | PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED)); | |
359 | #endif | |
360 | } | |
361 | ||
362 | #ifdef USE_PAM | |
363 | if (options.use_pam && authenticated) { | |
364 | if (!PRIVSEP(do_pam_account())) { | |
365 | /* if PAM returned a message, send it to the user */ | |
366 | if (buffer_len(&loginmsg) > 0) { | |
367 | buffer_append(&loginmsg, "\0", 1); | |
368 | userauth_send_banner(buffer_ptr(&loginmsg)); | |
369 | packet_write_wait(); | |
370 | } | |
371 | fatal("Access denied for user %s by PAM account " | |
372 | "configuration", authctxt->user); | |
373 | } | |
374 | } | |
375 | #endif | |
376 | ||
377 | #ifdef _UNICOS | |
378 | if (authenticated && cray_access_denied(authctxt->user)) { | |
379 | authenticated = 0; | |
380 | fatal("Access denied for user %s.",authctxt->user); | |
381 | } | |
382 | #endif /* _UNICOS */ | |
383 | ||
384 | /* Log before sending the reply */ | |
385 | auth_log(authctxt, authenticated, method, " ssh2"); | |
386 | ||
387 | if (authctxt->postponed) | |
388 | return; | |
389 | ||
390 | /* XXX todo: check if multiple auth methods are needed */ | |
391 | if (authenticated == 1) { | |
392 | /* turn off userauth */ | |
393 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore); | |
394 | packet_start(SSH2_MSG_USERAUTH_SUCCESS); | |
395 | packet_send(); | |
396 | packet_write_wait(); | |
397 | /* now we can break out */ | |
398 | authctxt->success = 1; | |
399 | } else { | |
400 | /* Dont count server configuration issues against the client */ | |
401 | /* Allow initial try of "none" auth without failure penalty */ | |
402 | if (!authctxt->server_caused_failure && | |
403 | (authctxt->attempt > 1 || strcmp(method, "none") != 0)) | |
404 | authctxt->failures++; | |
405 | if (authctxt->failures >= options.max_authtries) { | |
406 | #ifdef SSH_AUDIT_EVENTS | |
407 | PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES)); | |
408 | #endif | |
409 | packet_disconnect(AUTH_FAIL_MSG, authctxt->user); | |
410 | } | |
411 | methods = authmethods_get(); | |
412 | packet_start(SSH2_MSG_USERAUTH_FAILURE); | |
413 | packet_put_cstring(methods); | |
414 | packet_put_char(0); /* XXX partial success, unused */ | |
415 | packet_send(); | |
416 | packet_write_wait(); | |
417 | xfree(methods); | |
418 | } | |
419 | } | |
420 | ||
421 | static char * | |
422 | authmethods_get(void) | |
423 | { | |
424 | Buffer b; | |
425 | char *list; | |
426 | int i; | |
427 | ||
428 | buffer_init(&b); | |
429 | for (i = 0; authmethods[i] != NULL; i++) { | |
430 | if (strcmp(authmethods[i]->name, "none") == 0) | |
431 | continue; | |
432 | if (authmethods[i]->enabled != NULL && | |
433 | *(authmethods[i]->enabled) != 0) { | |
434 | if (buffer_len(&b) > 0) | |
435 | buffer_append(&b, ",", 1); | |
436 | buffer_append(&b, authmethods[i]->name, | |
437 | strlen(authmethods[i]->name)); | |
438 | } | |
439 | } | |
440 | buffer_append(&b, "\0", 1); | |
441 | list = xstrdup(buffer_ptr(&b)); | |
442 | buffer_free(&b); | |
443 | return list; | |
444 | } | |
445 | ||
446 | static Authmethod * | |
447 | authmethod_lookup(const char *name) | |
448 | { | |
449 | int i; | |
450 | ||
451 | if (name != NULL) | |
452 | for (i = 0; authmethods[i] != NULL; i++) | |
453 | if (authmethods[i]->enabled != NULL && | |
454 | *(authmethods[i]->enabled) != 0 && | |
455 | strcmp(name, authmethods[i]->name) == 0) | |
456 | return authmethods[i]; | |
457 | debug2("Unrecognized authentication method name: %s", | |
458 | name ? name : "NULL"); | |
459 | return NULL; | |
460 | } | |
461 |