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