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