]>
Commit | Line | Data |
---|---|---|
71709bcd | 1 | /* $OpenBSD: auth2.c,v 1.119 2008/07/04 23:30:16 djm Exp $ */ |
a306f2dd | 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. | |
a306f2dd | 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 | */ | |
bcbf86ec | 25 | |
a306f2dd | 26 | #include "includes.h" |
a306f2dd | 27 | |
b1842393 | 28 | #include <sys/types.h> |
221fc73c | 29 | #include <sys/stat.h> |
30 | #include <sys/uio.h> | |
b1842393 | 31 | |
221fc73c | 32 | #include <fcntl.h> |
b1842393 | 33 | #include <pwd.h> |
31652869 | 34 | #include <stdarg.h> |
00146caa | 35 | #include <string.h> |
221fc73c | 36 | #include <unistd.h> |
b1842393 | 37 | |
a306f2dd | 38 | #include "xmalloc.h" |
71709bcd | 39 | #include "atomicio.h" |
31652869 | 40 | #include "ssh2.h" |
a306f2dd | 41 | #include "packet.h" |
42f11eb2 | 42 | #include "log.h" |
31652869 | 43 | #include "buffer.h" |
a306f2dd | 44 | #include "servconf.h" |
45 | #include "compat.h" | |
31652869 | 46 | #include "key.h" |
47 | #include "hostfile.h" | |
a306f2dd | 48 | #include "auth.h" |
a306f2dd | 49 | #include "dispatch.h" |
42f11eb2 | 50 | #include "pathnames.h" |
ba6dd90e | 51 | #include "buffer.h" |
a306f2dd | 52 | |
7364bd04 | 53 | #ifdef GSSAPI |
54 | #include "ssh-gss.h" | |
55 | #endif | |
31652869 | 56 | #include "monitor_wrap.h" |
7364bd04 | 57 | |
a306f2dd | 58 | /* import */ |
59 | extern ServerOptions options; | |
1e3b8b07 | 60 | extern u_char *session_id2; |
a27002e5 | 61 | extern u_int session_id2_len; |
ba6dd90e | 62 | extern Buffer loginmsg; |
a306f2dd | 63 | |
01c24737 | 64 | /* methods */ |
65 | ||
66 | extern Authmethod method_none; | |
67 | extern Authmethod method_pubkey; | |
68 | extern Authmethod method_passwd; | |
69 | extern Authmethod method_kbdint; | |
70 | extern Authmethod method_hostbased; | |
7364bd04 | 71 | #ifdef GSSAPI |
72 | extern Authmethod method_gssapi; | |
73 | #endif | |
01c24737 | 74 | |
75 | Authmethod *authmethods[] = { | |
76 | &method_none, | |
77 | &method_pubkey, | |
7364bd04 | 78 | #ifdef GSSAPI |
79 | &method_gssapi, | |
80 | #endif | |
01c24737 | 81 | &method_passwd, |
82 | &method_kbdint, | |
83 | &method_hostbased, | |
84 | NULL | |
94ec8c6b | 85 | }; |
86 | ||
a306f2dd | 87 | /* protocol */ |
88 | ||
7819b5c3 | 89 | static void input_service_request(int, u_int32_t, void *); |
90 | static void input_userauth_request(int, u_int32_t, void *); | |
a306f2dd | 91 | |
a306f2dd | 92 | /* helper */ |
396c147e | 93 | static Authmethod *authmethod_lookup(const char *); |
ffb215be | 94 | static char *authmethods_get(void); |
a306f2dd | 95 | |
221fc73c | 96 | char * |
97 | auth2_read_banner(void) | |
98 | { | |
99 | struct stat st; | |
100 | char *banner = NULL; | |
101 | size_t len, n; | |
102 | int fd; | |
103 | ||
104 | if ((fd = open(options.banner, O_RDONLY)) == -1) | |
105 | return (NULL); | |
106 | if (fstat(fd, &st) == -1) { | |
107 | close(fd); | |
108 | return (NULL); | |
109 | } | |
110 | if (st.st_size > 1*1024*1024) { | |
111 | close(fd); | |
112 | return (NULL); | |
113 | } | |
114 | ||
115 | len = (size_t)st.st_size; /* truncate */ | |
116 | banner = xmalloc(len + 1); | |
117 | n = atomicio(read, fd, banner, len); | |
118 | close(fd); | |
119 | ||
120 | if (n != len) { | |
121 | xfree(banner); | |
122 | return (NULL); | |
123 | } | |
124 | banner[n] = '\0'; | |
125 | ||
126 | return (banner); | |
127 | } | |
128 | ||
129 | void | |
130 | userauth_send_banner(const char *msg) | |
131 | { | |
132 | if (datafellows & SSH_BUG_BANNER) | |
133 | return; | |
134 | ||
135 | packet_start(SSH2_MSG_USERAUTH_BANNER); | |
136 | packet_put_cstring(msg); | |
137 | packet_put_cstring(""); /* language, unused */ | |
138 | packet_send(); | |
139 | debug("%s: sent", __func__); | |
140 | } | |
141 | ||
142 | static void | |
143 | userauth_banner(void) | |
144 | { | |
145 | char *banner = NULL; | |
146 | ||
147 | if (options.banner == NULL || | |
148 | strcasecmp(options.banner, "none") == 0 || | |
149 | (datafellows & SSH_BUG_BANNER) != 0) | |
150 | return; | |
151 | ||
152 | if ((banner = PRIVSEP(auth2_read_banner())) == NULL) | |
153 | goto done; | |
154 | userauth_send_banner(banner); | |
155 | ||
156 | done: | |
157 | if (banner) | |
158 | xfree(banner); | |
159 | } | |
160 | ||
a306f2dd | 161 | /* |
94ec8c6b | 162 | * loop until authctxt->success == TRUE |
a306f2dd | 163 | */ |
2362db19 | 164 | void |
165 | do_authentication2(Authctxt *authctxt) | |
a306f2dd | 166 | { |
6367063f | 167 | dispatch_init(&dispatch_protocol_error); |
a306f2dd | 168 | dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); |
94ec8c6b | 169 | dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); |
a306f2dd | 170 | } |
171 | ||
fe8c3af1 | 172 | /*ARGSUSED*/ |
396c147e | 173 | static void |
7819b5c3 | 174 | input_service_request(int type, u_int32_t seq, void *ctxt) |
a306f2dd | 175 | { |
94ec8c6b | 176 | Authctxt *authctxt = ctxt; |
1e3b8b07 | 177 | u_int len; |
7fdc56c5 | 178 | int acceptit = 0; |
a306f2dd | 179 | char *service = packet_get_string(&len); |
95500969 | 180 | packet_check_eom(); |
a306f2dd | 181 | |
94ec8c6b | 182 | if (authctxt == NULL) |
183 | fatal("input_service_request: no authctxt"); | |
184 | ||
a306f2dd | 185 | if (strcmp(service, "ssh-userauth") == 0) { |
94ec8c6b | 186 | if (!authctxt->success) { |
7fdc56c5 | 187 | acceptit = 1; |
a306f2dd | 188 | /* now we can handle user-auth requests */ |
189 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); | |
190 | } | |
191 | } | |
192 | /* XXX all other service requests are denied */ | |
193 | ||
7fdc56c5 | 194 | if (acceptit) { |
a306f2dd | 195 | packet_start(SSH2_MSG_SERVICE_ACCEPT); |
196 | packet_put_cstring(service); | |
197 | packet_send(); | |
198 | packet_write_wait(); | |
199 | } else { | |
200 | debug("bad service request %s", service); | |
201 | packet_disconnect("bad service request %s", service); | |
202 | } | |
203 | xfree(service); | |
204 | } | |
205 | ||
fe8c3af1 | 206 | /*ARGSUSED*/ |
396c147e | 207 | static void |
7819b5c3 | 208 | input_userauth_request(int type, u_int32_t seq, void *ctxt) |
a306f2dd | 209 | { |
94ec8c6b | 210 | Authctxt *authctxt = ctxt; |
211 | Authmethod *m = NULL; | |
59c97189 | 212 | char *user, *service, *method, *style = NULL; |
a306f2dd | 213 | int authenticated = 0; |
a306f2dd | 214 | |
94ec8c6b | 215 | if (authctxt == NULL) |
216 | fatal("input_userauth_request: no authctxt"); | |
a306f2dd | 217 | |
94ec8c6b | 218 | user = packet_get_string(NULL); |
219 | service = packet_get_string(NULL); | |
220 | method = packet_get_string(NULL); | |
221 | debug("userauth-request for user %s service %s method %s", user, service, method); | |
8abcdba4 | 222 | debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); |
94ec8c6b | 223 | |
59c97189 | 224 | if ((style = strchr(user, ':')) != NULL) |
225 | *style++ = 0; | |
226 | ||
2b87da3b | 227 | if (authctxt->attempt++ == 0) { |
63284fbb | 228 | /* setup auth context */ |
f7ed12f1 | 229 | authctxt->pw = PRIVSEP(getpwnamallow(user)); |
529d73ab | 230 | authctxt->user = xstrdup(user); |
f7ed12f1 | 231 | if (authctxt->pw && strcmp(service, "ssh-connection")==0) { |
94ec8c6b | 232 | authctxt->valid = 1; |
233 | debug2("input_userauth_request: setting up authctxt for %s", user); | |
94ec8c6b | 234 | } else { |
d77347cc | 235 | logit("input_userauth_request: invalid user %s", user); |
96d0bf74 | 236 | authctxt->pw = fakepw(); |
6039eeef | 237 | #ifdef SSH_AUDIT_EVENTS |
238 | PRIVSEP(audit_event(SSH_INVALID_USER)); | |
04fc7a67 | 239 | #endif |
94ec8c6b | 240 | } |
b6c37221 | 241 | #ifdef USE_PAM |
242 | if (options.use_pam) | |
243 | PRIVSEP(start_pam(authctxt)); | |
244 | #endif | |
bd5c0694 | 245 | setproctitle("%s%s", authctxt->valid ? user : "unknown", |
1853d1ef | 246 | use_privsep ? " [net]" : ""); |
94ec8c6b | 247 | authctxt->service = xstrdup(service); |
2a6a054e | 248 | authctxt->style = style ? xstrdup(style) : NULL; |
1853d1ef | 249 | if (use_privsep) |
250 | mm_inform_authserv(service, style); | |
221fc73c | 251 | userauth_banner(); |
2a6a054e | 252 | } else if (strcmp(user, authctxt->user) != 0 || |
253 | strcmp(service, authctxt->service) != 0) { | |
254 | packet_disconnect("Change of username or service not allowed: " | |
255 | "(%s,%s) -> (%s,%s)", | |
256 | authctxt->user, authctxt->service, user, service); | |
a306f2dd | 257 | } |
59c97189 | 258 | /* reset state */ |
2f293d43 | 259 | auth2_challenge_stop(authctxt); |
7364bd04 | 260 | |
261 | #ifdef GSSAPI | |
262 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); | |
263 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); | |
264 | #endif | |
265 | ||
59c97189 | 266 | authctxt->postponed = 0; |
9cd45ea4 | 267 | |
59c97189 | 268 | /* try to authenticate user */ |
94ec8c6b | 269 | m = authmethod_lookup(method); |
221fc73c | 270 | if (m != NULL && authctxt->failures < options.max_authtries) { |
94ec8c6b | 271 | debug2("input_userauth_request: try method %s", method); |
272 | authenticated = m->userauth(authctxt); | |
9cd45ea4 | 273 | } |
d9cd3575 | 274 | userauth_finish(authctxt, authenticated, method); |
275 | ||
276 | xfree(service); | |
277 | xfree(user); | |
278 | xfree(method); | |
279 | } | |
280 | ||
281 | void | |
282 | userauth_finish(Authctxt *authctxt, int authenticated, char *method) | |
283 | { | |
4e81159c | 284 | char *methods; |
285 | ||
59c97189 | 286 | if (!authctxt->valid && authenticated) |
287 | fatal("INTERNAL ERROR: authenticated invalid user %s", | |
288 | authctxt->user); | |
9cd45ea4 | 289 | |
94ec8c6b | 290 | /* Special handling for root */ |
5d47f1d4 | 291 | if (authenticated && authctxt->pw->pw_uid == 0 && |
c00e4d75 | 292 | !auth_root_allowed(method)) { |
a306f2dd | 293 | authenticated = 0; |
6039eeef | 294 | #ifdef SSH_AUDIT_EVENTS |
295 | PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED)); | |
c00e4d75 | 296 | #endif |
297 | } | |
a306f2dd | 298 | |
5b9e2464 | 299 | #ifdef USE_PAM |
ba6dd90e | 300 | if (options.use_pam && authenticated) { |
301 | if (!PRIVSEP(do_pam_account())) { | |
ba6dd90e | 302 | /* if PAM returned a message, send it to the user */ |
303 | if (buffer_len(&loginmsg) > 0) { | |
304 | buffer_append(&loginmsg, "\0", 1); | |
305 | userauth_send_banner(buffer_ptr(&loginmsg)); | |
9aab0af7 | 306 | packet_write_wait(); |
ba6dd90e | 307 | } |
9aab0af7 | 308 | fatal("Access denied for user %s by PAM account " |
98c044d0 | 309 | "configuration", authctxt->user); |
ba6dd90e | 310 | } |
311 | } | |
5b9e2464 | 312 | #endif |
313 | ||
ef51930f | 314 | #ifdef _UNICOS |
315 | if (authenticated && cray_access_denied(authctxt->user)) { | |
316 | authenticated = 0; | |
317 | fatal("Access denied for user %s.",authctxt->user); | |
318 | } | |
319 | #endif /* _UNICOS */ | |
320 | ||
94ec8c6b | 321 | /* Log before sending the reply */ |
59c97189 | 322 | auth_log(authctxt, authenticated, method, " ssh2"); |
323 | ||
4e81159c | 324 | if (authctxt->postponed) |
325 | return; | |
326 | ||
327 | /* XXX todo: check if multiple auth methods are needed */ | |
328 | if (authenticated == 1) { | |
329 | /* turn off userauth */ | |
6367063f | 330 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore); |
4e81159c | 331 | packet_start(SSH2_MSG_USERAUTH_SUCCESS); |
332 | packet_send(); | |
333 | packet_write_wait(); | |
334 | /* now we can break out */ | |
335 | authctxt->success = 1; | |
336 | } else { | |
71709bcd | 337 | |
338 | /* Allow initial try of "none" auth without failure penalty */ | |
339 | if (authctxt->attempt > 1 || strcmp(method, "none") != 0) | |
340 | authctxt->failures++; | |
341 | if (authctxt->failures >= options.max_authtries) { | |
6039eeef | 342 | #ifdef SSH_AUDIT_EVENTS |
343 | PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES)); | |
c00e4d75 | 344 | #endif |
4e81159c | 345 | packet_disconnect(AUTH_FAIL_MSG, authctxt->user); |
c00e4d75 | 346 | } |
4e81159c | 347 | methods = authmethods_get(); |
348 | packet_start(SSH2_MSG_USERAUTH_FAILURE); | |
349 | packet_put_cstring(methods); | |
350 | packet_put_char(0); /* XXX partial success, unused */ | |
351 | packet_send(); | |
352 | packet_write_wait(); | |
353 | xfree(methods); | |
354 | } | |
94ec8c6b | 355 | } |
356 | ||
ffb215be | 357 | static char * |
94ec8c6b | 358 | authmethods_get(void) |
a306f2dd | 359 | { |
3469eac4 | 360 | Buffer b; |
94ec8c6b | 361 | char *list; |
01c24737 | 362 | int i; |
94ec8c6b | 363 | |
3469eac4 | 364 | buffer_init(&b); |
01c24737 | 365 | for (i = 0; authmethods[i] != NULL; i++) { |
366 | if (strcmp(authmethods[i]->name, "none") == 0) | |
94ec8c6b | 367 | continue; |
01c24737 | 368 | if (authmethods[i]->enabled != NULL && |
369 | *(authmethods[i]->enabled) != 0) { | |
3469eac4 | 370 | if (buffer_len(&b) > 0) |
371 | buffer_append(&b, ",", 1); | |
01c24737 | 372 | buffer_append(&b, authmethods[i]->name, |
373 | strlen(authmethods[i]->name)); | |
a306f2dd | 374 | } |
375 | } | |
3469eac4 | 376 | buffer_append(&b, "\0", 1); |
377 | list = xstrdup(buffer_ptr(&b)); | |
378 | buffer_free(&b); | |
94ec8c6b | 379 | return list; |
380 | } | |
381 | ||
396c147e | 382 | static Authmethod * |
94ec8c6b | 383 | authmethod_lookup(const char *name) |
384 | { | |
01c24737 | 385 | int i; |
386 | ||
94ec8c6b | 387 | if (name != NULL) |
01c24737 | 388 | for (i = 0; authmethods[i] != NULL; i++) |
389 | if (authmethods[i]->enabled != NULL && | |
390 | *(authmethods[i]->enabled) != 0 && | |
391 | strcmp(name, authmethods[i]->name) == 0) | |
392 | return authmethods[i]; | |
393 | debug2("Unrecognized authentication method name: %s", | |
394 | name ? name : "NULL"); | |
94ec8c6b | 395 | return NULL; |
a306f2dd | 396 | } |
221fc73c | 397 |