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