]>
Commit | Line | Data |
---|---|---|
799ae497 | 1 | /* $OpenBSD: auth2.c,v 1.114 2007/03/01 10:28:02 dtucker 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 | { |
e9a17296 | 99 | dispatch_init(&dispatch_protocol_error); |
3c0ef626 | 100 | dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); |
101 | dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); | |
3c0ef626 | 102 | } |
103 | ||
9108f8d9 | 104 | /*ARGSUSED*/ |
3c0ef626 | 105 | static void |
e9a17296 | 106 | input_service_request(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 107 | { |
108 | Authctxt *authctxt = ctxt; | |
109 | u_int len; | |
41b2f314 | 110 | int acceptit = 0; |
3c0ef626 | 111 | char *service = packet_get_string(&len); |
e9a17296 | 112 | packet_check_eom(); |
3c0ef626 | 113 | |
114 | if (authctxt == NULL) | |
115 | fatal("input_service_request: no authctxt"); | |
116 | ||
117 | if (strcmp(service, "ssh-userauth") == 0) { | |
118 | if (!authctxt->success) { | |
41b2f314 | 119 | acceptit = 1; |
3c0ef626 | 120 | /* now we can handle user-auth requests */ |
121 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); | |
122 | } | |
123 | } | |
124 | /* XXX all other service requests are denied */ | |
125 | ||
41b2f314 | 126 | if (acceptit) { |
3c0ef626 | 127 | packet_start(SSH2_MSG_SERVICE_ACCEPT); |
128 | packet_put_cstring(service); | |
129 | packet_send(); | |
130 | packet_write_wait(); | |
131 | } else { | |
132 | debug("bad service request %s", service); | |
133 | packet_disconnect("bad service request %s", service); | |
134 | } | |
135 | xfree(service); | |
136 | } | |
137 | ||
9108f8d9 | 138 | /*ARGSUSED*/ |
3c0ef626 | 139 | static void |
e9a17296 | 140 | input_userauth_request(int type, u_int32_t seq, void *ctxt) |
3c0ef626 | 141 | { |
142 | Authctxt *authctxt = ctxt; | |
143 | Authmethod *m = NULL; | |
144 | char *user, *service, *method, *style = NULL; | |
145 | int authenticated = 0; | |
146 | ||
147 | if (authctxt == NULL) | |
148 | fatal("input_userauth_request: no authctxt"); | |
149 | ||
150 | user = packet_get_string(NULL); | |
151 | service = packet_get_string(NULL); | |
152 | method = packet_get_string(NULL); | |
153 | debug("userauth-request for user %s service %s method %s", user, service, method); | |
154 | debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); | |
155 | ||
156 | if ((style = strchr(user, ':')) != NULL) | |
157 | *style++ = 0; | |
158 | ||
159 | if (authctxt->attempt++ == 0) { | |
160 | /* setup auth context */ | |
700318f3 | 161 | authctxt->pw = PRIVSEP(getpwnamallow(user)); |
99be0775 | 162 | authctxt->user = xstrdup(user); |
700318f3 | 163 | if (authctxt->pw && strcmp(service, "ssh-connection")==0) { |
3c0ef626 | 164 | authctxt->valid = 1; |
165 | debug2("input_userauth_request: setting up authctxt for %s", user); | |
3c0ef626 | 166 | } else { |
c9f39d2c | 167 | logit("input_userauth_request: invalid user %s", user); |
0fff78ff | 168 | authctxt->pw = fakepw(); |
996d5e62 | 169 | #ifdef SSH_AUDIT_EVENTS |
170 | PRIVSEP(audit_event(SSH_INVALID_USER)); | |
3c0ef626 | 171 | #endif |
172 | } | |
2c06c99b | 173 | #ifdef USE_PAM |
174 | if (options.use_pam) | |
175 | PRIVSEP(start_pam(authctxt)); | |
176 | #endif | |
c9f39d2c | 177 | setproctitle("%s%s", authctxt->valid ? user : "unknown", |
700318f3 | 178 | use_privsep ? " [net]" : ""); |
3c0ef626 | 179 | authctxt->service = xstrdup(service); |
180 | authctxt->style = style ? xstrdup(style) : NULL; | |
700318f3 | 181 | if (use_privsep) |
182 | mm_inform_authserv(service, style); | |
3c0ef626 | 183 | } else if (strcmp(user, authctxt->user) != 0 || |
184 | strcmp(service, authctxt->service) != 0) { | |
185 | packet_disconnect("Change of username or service not allowed: " | |
186 | "(%s,%s) -> (%s,%s)", | |
187 | authctxt->user, authctxt->service, user, service); | |
188 | } | |
189 | /* reset state */ | |
e9a17296 | 190 | auth2_challenge_stop(authctxt); |
0fff78ff | 191 | |
192 | #ifdef GSSAPI | |
193 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); | |
194 | dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); | |
195 | #endif | |
196 | ||
3c0ef626 | 197 | authctxt->postponed = 0; |
3c0ef626 | 198 | |
199 | /* try to authenticate user */ | |
200 | m = authmethod_lookup(method); | |
201 | if (m != NULL) { | |
202 | debug2("input_userauth_request: try method %s", method); | |
203 | authenticated = m->userauth(authctxt); | |
204 | } | |
205 | userauth_finish(authctxt, authenticated, method); | |
206 | ||
207 | xfree(service); | |
208 | xfree(user); | |
209 | xfree(method); | |
210 | } | |
211 | ||
212 | void | |
213 | userauth_finish(Authctxt *authctxt, int authenticated, char *method) | |
214 | { | |
215 | char *methods; | |
216 | ||
217 | if (!authctxt->valid && authenticated) | |
218 | fatal("INTERNAL ERROR: authenticated invalid user %s", | |
219 | authctxt->user); | |
220 | ||
221 | /* Special handling for root */ | |
6a9b3198 | 222 | if (authenticated && authctxt->pw->pw_uid == 0 && |
996d5e62 | 223 | !auth_root_allowed(method)) { |
3c0ef626 | 224 | authenticated = 0; |
996d5e62 | 225 | #ifdef SSH_AUDIT_EVENTS |
226 | PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED)); | |
227 | #endif | |
228 | } | |
3c0ef626 | 229 | |
230 | #ifdef USE_PAM | |
996d5e62 | 231 | if (options.use_pam && authenticated) { |
232 | if (!PRIVSEP(do_pam_account())) { | |
233 | /* if PAM returned a message, send it to the user */ | |
234 | if (buffer_len(&loginmsg) > 0) { | |
235 | buffer_append(&loginmsg, "\0", 1); | |
236 | userauth_send_banner(buffer_ptr(&loginmsg)); | |
237 | packet_write_wait(); | |
238 | } | |
239 | fatal("Access denied for user %s by PAM account " | |
665a873d | 240 | "configuration", authctxt->user); |
996d5e62 | 241 | } |
242 | } | |
0fff78ff | 243 | #endif |
3c0ef626 | 244 | |
41b2f314 | 245 | #ifdef _UNICOS |
246 | if (authenticated && cray_access_denied(authctxt->user)) { | |
247 | authenticated = 0; | |
248 | fatal("Access denied for user %s.",authctxt->user); | |
249 | } | |
250 | #endif /* _UNICOS */ | |
251 | ||
3c0ef626 | 252 | /* Log before sending the reply */ |
253 | auth_log(authctxt, authenticated, method, " ssh2"); | |
254 | ||
255 | if (authctxt->postponed) | |
256 | return; | |
257 | ||
258 | /* XXX todo: check if multiple auth methods are needed */ | |
259 | if (authenticated == 1) { | |
260 | /* turn off userauth */ | |
e9a17296 | 261 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore); |
3c0ef626 | 262 | packet_start(SSH2_MSG_USERAUTH_SUCCESS); |
263 | packet_send(); | |
264 | packet_write_wait(); | |
265 | /* now we can break out */ | |
266 | authctxt->success = 1; | |
267 | } else { | |
996d5e62 | 268 | if (authctxt->failures++ > options.max_authtries) { |
269 | #ifdef SSH_AUDIT_EVENTS | |
270 | PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES)); | |
271 | #endif | |
3c0ef626 | 272 | packet_disconnect(AUTH_FAIL_MSG, authctxt->user); |
996d5e62 | 273 | } |
3c0ef626 | 274 | methods = authmethods_get(); |
275 | packet_start(SSH2_MSG_USERAUTH_FAILURE); | |
276 | packet_put_cstring(methods); | |
277 | packet_put_char(0); /* XXX partial success, unused */ | |
278 | packet_send(); | |
279 | packet_write_wait(); | |
280 | xfree(methods); | |
281 | } | |
282 | } | |
283 | ||
3c0ef626 | 284 | #define DELIM "," |
285 | ||
286 | static char * | |
287 | authmethods_get(void) | |
288 | { | |
e9a17296 | 289 | Buffer b; |
3c0ef626 | 290 | char *list; |
f5799ae1 | 291 | int i; |
3c0ef626 | 292 | |
e9a17296 | 293 | buffer_init(&b); |
f5799ae1 | 294 | for (i = 0; authmethods[i] != NULL; i++) { |
295 | if (strcmp(authmethods[i]->name, "none") == 0) | |
3c0ef626 | 296 | continue; |
f5799ae1 | 297 | if (authmethods[i]->enabled != NULL && |
298 | *(authmethods[i]->enabled) != 0) { | |
e9a17296 | 299 | if (buffer_len(&b) > 0) |
300 | buffer_append(&b, ",", 1); | |
f5799ae1 | 301 | buffer_append(&b, authmethods[i]->name, |
302 | strlen(authmethods[i]->name)); | |
3c0ef626 | 303 | } |
304 | } | |
e9a17296 | 305 | buffer_append(&b, "\0", 1); |
306 | list = xstrdup(buffer_ptr(&b)); | |
307 | buffer_free(&b); | |
3c0ef626 | 308 | return list; |
309 | } | |
310 | ||
311 | static Authmethod * | |
312 | authmethod_lookup(const char *name) | |
313 | { | |
f5799ae1 | 314 | int i; |
315 | ||
3c0ef626 | 316 | if (name != NULL) |
f5799ae1 | 317 | for (i = 0; authmethods[i] != NULL; i++) |
318 | if (authmethods[i]->enabled != NULL && | |
319 | *(authmethods[i]->enabled) != 0 && | |
320 | strcmp(name, authmethods[i]->name) == 0) | |
321 | return authmethods[i]; | |
322 | debug2("Unrecognized authentication method name: %s", | |
323 | name ? name : "NULL"); | |
3c0ef626 | 324 | return NULL; |
325 | } |