]>
Commit | Line | Data |
---|---|---|
c1cb7bae | 1 | /* $OpenBSD: auth2.c,v 1.110 2006/03/25 13:17:01 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 | |
42f11eb2 | 28 | #include "ssh2.h" |
a306f2dd | 29 | #include "xmalloc.h" |
a306f2dd | 30 | #include "packet.h" |
42f11eb2 | 31 | #include "log.h" |
a306f2dd | 32 | #include "servconf.h" |
33 | #include "compat.h" | |
a306f2dd | 34 | #include "auth.h" |
a306f2dd | 35 | #include "dispatch.h" |
42f11eb2 | 36 | #include "pathnames.h" |
1853d1ef | 37 | #include "monitor_wrap.h" |
ba6dd90e | 38 | #include "buffer.h" |
a306f2dd | 39 | |
7364bd04 | 40 | #ifdef GSSAPI |
41 | #include "ssh-gss.h" | |
42 | #endif | |
43 | ||
a306f2dd | 44 | /* import */ |
45 | extern ServerOptions options; | |
1e3b8b07 | 46 | extern u_char *session_id2; |
a27002e5 | 47 | extern u_int session_id2_len; |
ba6dd90e | 48 | extern Buffer loginmsg; |
a306f2dd | 49 | |
01c24737 | 50 | /* methods */ |
51 | ||
52 | extern Authmethod method_none; | |
53 | extern Authmethod method_pubkey; | |
54 | extern Authmethod method_passwd; | |
55 | extern Authmethod method_kbdint; | |
56 | extern Authmethod method_hostbased; | |
7364bd04 | 57 | #ifdef GSSAPI |
58 | extern Authmethod method_gssapi; | |
59 | #endif | |
01c24737 | 60 | |
61 | Authmethod *authmethods[] = { | |
62 | &method_none, | |
63 | &method_pubkey, | |
7364bd04 | 64 | #ifdef GSSAPI |
65 | &method_gssapi, | |
66 | #endif | |
01c24737 | 67 | &method_passwd, |
68 | &method_kbdint, | |
69 | &method_hostbased, | |
70 | NULL | |
94ec8c6b | 71 | }; |
72 | ||
a306f2dd | 73 | /* protocol */ |
74 | ||
7819b5c3 | 75 | static void input_service_request(int, u_int32_t, void *); |
76 | static void input_userauth_request(int, u_int32_t, void *); | |
a306f2dd | 77 | |
a306f2dd | 78 | /* helper */ |
396c147e | 79 | static Authmethod *authmethod_lookup(const char *); |
ffb215be | 80 | static char *authmethods_get(void); |
1853d1ef | 81 | int user_key_allowed(struct passwd *, Key *); |
a306f2dd | 82 | |
a306f2dd | 83 | /* |
94ec8c6b | 84 | * loop until authctxt->success == TRUE |
a306f2dd | 85 | */ |
86 | ||
2362db19 | 87 | void |
88 | do_authentication2(Authctxt *authctxt) | |
a306f2dd | 89 | { |
aa8003d6 | 90 | /* challenge-response is implemented via keyboard interactive */ |
5ba55ada | 91 | if (options.challenge_response_authentication) |
d464095c | 92 | options.kbd_interactive_authentication = 1; |
93 | ||
6367063f | 94 | dispatch_init(&dispatch_protocol_error); |
a306f2dd | 95 | dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); |
94ec8c6b | 96 | dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); |
a306f2dd | 97 | } |
98 | ||
fe8c3af1 | 99 | /*ARGSUSED*/ |
396c147e | 100 | static void |
7819b5c3 | 101 | input_service_request(int type, u_int32_t seq, void *ctxt) |
a306f2dd | 102 | { |
94ec8c6b | 103 | Authctxt *authctxt = ctxt; |
1e3b8b07 | 104 | u_int len; |
7fdc56c5 | 105 | int acceptit = 0; |
a306f2dd | 106 | char *service = packet_get_string(&len); |
95500969 | 107 | packet_check_eom(); |
a306f2dd | 108 | |
94ec8c6b | 109 | if (authctxt == NULL) |
110 | fatal("input_service_request: no authctxt"); | |
111 | ||
a306f2dd | 112 | if (strcmp(service, "ssh-userauth") == 0) { |
94ec8c6b | 113 | if (!authctxt->success) { |
7fdc56c5 | 114 | acceptit = 1; |
a306f2dd | 115 | /* now we can handle user-auth requests */ |
116 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); | |
117 | } | |
118 | } | |
119 | /* XXX all other service requests are denied */ | |
120 | ||
7fdc56c5 | 121 | if (acceptit) { |
a306f2dd | 122 | packet_start(SSH2_MSG_SERVICE_ACCEPT); |
123 | packet_put_cstring(service); | |
124 | packet_send(); | |
125 | packet_write_wait(); | |
126 | } else { | |
127 | debug("bad service request %s", service); | |
128 | packet_disconnect("bad service request %s", service); | |
129 | } | |
130 | xfree(service); | |
131 | } | |
132 | ||
fe8c3af1 | 133 | /*ARGSUSED*/ |
396c147e | 134 | static void |
7819b5c3 | 135 | input_userauth_request(int type, u_int32_t seq, void *ctxt) |
a306f2dd | 136 | { |
94ec8c6b | 137 | Authctxt *authctxt = ctxt; |
138 | Authmethod *m = NULL; | |
59c97189 | 139 | char *user, *service, *method, *style = NULL; |
a306f2dd | 140 | int authenticated = 0; |
a306f2dd | 141 | |
94ec8c6b | 142 | if (authctxt == NULL) |
143 | fatal("input_userauth_request: no authctxt"); | |
a306f2dd | 144 | |
94ec8c6b | 145 | user = packet_get_string(NULL); |
146 | service = packet_get_string(NULL); | |
147 | method = packet_get_string(NULL); | |
148 | debug("userauth-request for user %s service %s method %s", user, service, method); | |
8abcdba4 | 149 | debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); |
94ec8c6b | 150 | |
59c97189 | 151 | if ((style = strchr(user, ':')) != NULL) |
152 | *style++ = 0; | |
153 | ||
2b87da3b | 154 | if (authctxt->attempt++ == 0) { |
63284fbb | 155 | /* setup auth context */ |
f7ed12f1 | 156 | authctxt->pw = PRIVSEP(getpwnamallow(user)); |
529d73ab | 157 | authctxt->user = xstrdup(user); |
f7ed12f1 | 158 | if (authctxt->pw && strcmp(service, "ssh-connection")==0) { |
94ec8c6b | 159 | authctxt->valid = 1; |
160 | debug2("input_userauth_request: setting up authctxt for %s", user); | |
94ec8c6b | 161 | } else { |
d77347cc | 162 | logit("input_userauth_request: invalid user %s", user); |
96d0bf74 | 163 | authctxt->pw = fakepw(); |
6039eeef | 164 | #ifdef SSH_AUDIT_EVENTS |
165 | PRIVSEP(audit_event(SSH_INVALID_USER)); | |
04fc7a67 | 166 | #endif |
94ec8c6b | 167 | } |
b6c37221 | 168 | #ifdef USE_PAM |
169 | if (options.use_pam) | |
170 | PRIVSEP(start_pam(authctxt)); | |
171 | #endif | |
bd5c0694 | 172 | setproctitle("%s%s", authctxt->valid ? user : "unknown", |
1853d1ef | 173 | use_privsep ? " [net]" : ""); |
94ec8c6b | 174 | authctxt->service = xstrdup(service); |
2a6a054e | 175 | authctxt->style = style ? xstrdup(style) : NULL; |
1853d1ef | 176 | if (use_privsep) |
177 | mm_inform_authserv(service, style); | |
2a6a054e | 178 | } else if (strcmp(user, authctxt->user) != 0 || |
179 | strcmp(service, authctxt->service) != 0) { | |
180 | packet_disconnect("Change of username or service not allowed: " | |
181 | "(%s,%s) -> (%s,%s)", | |
182 | authctxt->user, authctxt->service, user, service); | |
a306f2dd | 183 | } |
59c97189 | 184 | /* reset state */ |
2f293d43 | 185 | auth2_challenge_stop(authctxt); |
7364bd04 | 186 | |
187 | #ifdef GSSAPI | |
188 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); | |
189 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); | |
190 | #endif | |
191 | ||
59c97189 | 192 | authctxt->postponed = 0; |
9cd45ea4 | 193 | |
59c97189 | 194 | /* try to authenticate user */ |
94ec8c6b | 195 | m = authmethod_lookup(method); |
196 | if (m != NULL) { | |
197 | debug2("input_userauth_request: try method %s", method); | |
198 | authenticated = m->userauth(authctxt); | |
9cd45ea4 | 199 | } |
d9cd3575 | 200 | userauth_finish(authctxt, authenticated, method); |
201 | ||
202 | xfree(service); | |
203 | xfree(user); | |
204 | xfree(method); | |
205 | } | |
206 | ||
207 | void | |
208 | userauth_finish(Authctxt *authctxt, int authenticated, char *method) | |
209 | { | |
4e81159c | 210 | char *methods; |
211 | ||
59c97189 | 212 | if (!authctxt->valid && authenticated) |
213 | fatal("INTERNAL ERROR: authenticated invalid user %s", | |
214 | authctxt->user); | |
9cd45ea4 | 215 | |
94ec8c6b | 216 | /* Special handling for root */ |
5d47f1d4 | 217 | if (authenticated && authctxt->pw->pw_uid == 0 && |
c00e4d75 | 218 | !auth_root_allowed(method)) { |
a306f2dd | 219 | authenticated = 0; |
6039eeef | 220 | #ifdef SSH_AUDIT_EVENTS |
221 | PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED)); | |
c00e4d75 | 222 | #endif |
223 | } | |
a306f2dd | 224 | |
5b9e2464 | 225 | #ifdef USE_PAM |
ba6dd90e | 226 | if (options.use_pam && authenticated) { |
227 | if (!PRIVSEP(do_pam_account())) { | |
ba6dd90e | 228 | /* if PAM returned a message, send it to the user */ |
229 | if (buffer_len(&loginmsg) > 0) { | |
230 | buffer_append(&loginmsg, "\0", 1); | |
231 | userauth_send_banner(buffer_ptr(&loginmsg)); | |
9aab0af7 | 232 | packet_write_wait(); |
ba6dd90e | 233 | } |
9aab0af7 | 234 | fatal("Access denied for user %s by PAM account " |
98c044d0 | 235 | "configuration", authctxt->user); |
ba6dd90e | 236 | } |
237 | } | |
5b9e2464 | 238 | #endif |
239 | ||
ef51930f | 240 | #ifdef _UNICOS |
241 | if (authenticated && cray_access_denied(authctxt->user)) { | |
242 | authenticated = 0; | |
243 | fatal("Access denied for user %s.",authctxt->user); | |
244 | } | |
245 | #endif /* _UNICOS */ | |
246 | ||
94ec8c6b | 247 | /* Log before sending the reply */ |
59c97189 | 248 | auth_log(authctxt, authenticated, method, " ssh2"); |
249 | ||
4e81159c | 250 | if (authctxt->postponed) |
251 | return; | |
252 | ||
253 | /* XXX todo: check if multiple auth methods are needed */ | |
254 | if (authenticated == 1) { | |
255 | /* turn off userauth */ | |
6367063f | 256 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore); |
4e81159c | 257 | packet_start(SSH2_MSG_USERAUTH_SUCCESS); |
258 | packet_send(); | |
259 | packet_write_wait(); | |
260 | /* now we can break out */ | |
261 | authctxt->success = 1; | |
262 | } else { | |
c00e4d75 | 263 | if (authctxt->failures++ > options.max_authtries) { |
6039eeef | 264 | #ifdef SSH_AUDIT_EVENTS |
265 | PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES)); | |
c00e4d75 | 266 | #endif |
4e81159c | 267 | packet_disconnect(AUTH_FAIL_MSG, authctxt->user); |
c00e4d75 | 268 | } |
4e81159c | 269 | methods = authmethods_get(); |
270 | packet_start(SSH2_MSG_USERAUTH_FAILURE); | |
271 | packet_put_cstring(methods); | |
272 | packet_put_char(0); /* XXX partial success, unused */ | |
273 | packet_send(); | |
274 | packet_write_wait(); | |
275 | xfree(methods); | |
276 | } | |
94ec8c6b | 277 | } |
278 | ||
94ec8c6b | 279 | #define DELIM "," |
280 | ||
ffb215be | 281 | static char * |
94ec8c6b | 282 | authmethods_get(void) |
a306f2dd | 283 | { |
3469eac4 | 284 | Buffer b; |
94ec8c6b | 285 | char *list; |
01c24737 | 286 | int i; |
94ec8c6b | 287 | |
3469eac4 | 288 | buffer_init(&b); |
01c24737 | 289 | for (i = 0; authmethods[i] != NULL; i++) { |
290 | if (strcmp(authmethods[i]->name, "none") == 0) | |
94ec8c6b | 291 | continue; |
01c24737 | 292 | if (authmethods[i]->enabled != NULL && |
293 | *(authmethods[i]->enabled) != 0) { | |
3469eac4 | 294 | if (buffer_len(&b) > 0) |
295 | buffer_append(&b, ",", 1); | |
01c24737 | 296 | buffer_append(&b, authmethods[i]->name, |
297 | strlen(authmethods[i]->name)); | |
a306f2dd | 298 | } |
299 | } | |
3469eac4 | 300 | buffer_append(&b, "\0", 1); |
301 | list = xstrdup(buffer_ptr(&b)); | |
302 | buffer_free(&b); | |
94ec8c6b | 303 | return list; |
304 | } | |
305 | ||
396c147e | 306 | static Authmethod * |
94ec8c6b | 307 | authmethod_lookup(const char *name) |
308 | { | |
01c24737 | 309 | int i; |
310 | ||
94ec8c6b | 311 | if (name != NULL) |
01c24737 | 312 | for (i = 0; authmethods[i] != NULL; i++) |
313 | if (authmethods[i]->enabled != NULL && | |
314 | *(authmethods[i]->enabled) != 0 && | |
315 | strcmp(name, authmethods[i]->name) == 0) | |
316 | return authmethods[i]; | |
317 | debug2("Unrecognized authentication method name: %s", | |
318 | name ? name : "NULL"); | |
94ec8c6b | 319 | return NULL; |
a306f2dd | 320 | } |