]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | |
3 | * All rights reserved | |
4 | * | |
5 | * As far as I am concerned, the code I have written for this software | |
6 | * can be used freely for any purpose. Any derived versions of this | |
7 | * software must be clearly marked as such, and if the derived work is | |
8 | * incompatible with the protocol description in the RFC file, it must be | |
9 | * called by a name other than "ssh" or "Secure Shell". | |
10 | */ | |
11 | ||
12 | #include "includes.h" | |
13 | RCSID("$OpenBSD: auth1.c,v 1.39 2002/03/19 14:27:39 markus Exp $"); | |
14 | ||
15 | #include "xmalloc.h" | |
16 | #include "rsa.h" | |
17 | #include "ssh1.h" | |
18 | #include "packet.h" | |
19 | #include "buffer.h" | |
20 | #include "mpaux.h" | |
21 | #include "log.h" | |
22 | #include "servconf.h" | |
23 | #include "compat.h" | |
24 | #include "auth.h" | |
25 | #include "channels.h" | |
26 | #include "session.h" | |
27 | #include "uidswap.h" | |
28 | #include "monitor_wrap.h" | |
29 | ||
30 | /* import */ | |
31 | extern ServerOptions options; | |
32 | ||
33 | /* | |
34 | * convert ssh auth msg type into description | |
35 | */ | |
36 | static char * | |
37 | get_authname(int type) | |
38 | { | |
39 | static char buf[1024]; | |
40 | switch (type) { | |
41 | case SSH_CMSG_AUTH_PASSWORD: | |
42 | return "password"; | |
43 | case SSH_CMSG_AUTH_RSA: | |
44 | return "rsa"; | |
45 | case SSH_CMSG_AUTH_RHOSTS_RSA: | |
46 | return "rhosts-rsa"; | |
47 | case SSH_CMSG_AUTH_RHOSTS: | |
48 | return "rhosts"; | |
49 | case SSH_CMSG_AUTH_TIS: | |
50 | case SSH_CMSG_AUTH_TIS_RESPONSE: | |
51 | return "challenge-response"; | |
52 | #if defined(KRB4) || defined(KRB5) | |
53 | case SSH_CMSG_AUTH_KERBEROS: | |
54 | return "kerberos"; | |
55 | #endif | |
56 | } | |
57 | snprintf(buf, sizeof buf, "bad-auth-msg-%d", type); | |
58 | return buf; | |
59 | } | |
60 | ||
61 | /* | |
62 | * read packets, try to authenticate the user and | |
63 | * return only if authentication is successful | |
64 | */ | |
65 | static void | |
66 | do_authloop(Authctxt *authctxt) | |
67 | { | |
68 | int authenticated = 0; | |
69 | u_int bits; | |
70 | Key *client_host_key; | |
71 | BIGNUM *n; | |
72 | char *client_user, *password; | |
73 | char info[1024]; | |
74 | u_int dlen; | |
75 | u_int ulen; | |
76 | int type = 0; | |
77 | struct passwd *pw = authctxt->pw; | |
78 | ||
79 | debug("Attempting authentication for %s%.100s.", | |
80 | authctxt->valid ? "" : "illegal user ", authctxt->user); | |
81 | ||
82 | /* If the user has no password, accept authentication immediately. */ | |
83 | if (options.password_authentication && | |
84 | #if defined(KRB4) || defined(KRB5) | |
85 | (!options.kerberos_authentication || options.kerberos_or_local_passwd) && | |
86 | #endif | |
87 | PRIVSEP(auth_password(authctxt, ""))) { | |
88 | auth_log(authctxt, 1, "without authentication", ""); | |
89 | return; | |
90 | } | |
91 | ||
92 | /* Indicate that authentication is needed. */ | |
93 | packet_start(SSH_SMSG_FAILURE); | |
94 | packet_send(); | |
95 | packet_write_wait(); | |
96 | ||
97 | client_user = NULL; | |
98 | ||
99 | for (;;) { | |
100 | /* default to fail */ | |
101 | authenticated = 0; | |
102 | ||
103 | info[0] = '\0'; | |
104 | ||
105 | /* Get a packet from the client. */ | |
106 | type = packet_read(); | |
107 | ||
108 | /* Process the packet. */ | |
109 | switch (type) { | |
110 | ||
111 | #if defined(KRB4) || defined(KRB5) | |
112 | case SSH_CMSG_AUTH_KERBEROS: | |
113 | if (!options.kerberos_authentication) { | |
114 | verbose("Kerberos authentication disabled."); | |
115 | } else { | |
116 | char *kdata = packet_get_string(&dlen); | |
117 | packet_check_eom(); | |
118 | ||
119 | if (kdata[0] == 4) { /* KRB_PROT_VERSION */ | |
120 | #ifdef KRB4 | |
121 | KTEXT_ST tkt; | |
122 | ||
123 | tkt.length = dlen; | |
124 | if (tkt.length < MAX_KTXT_LEN) | |
125 | memcpy(tkt.dat, kdata, tkt.length); | |
126 | ||
127 | if (auth_krb4(authctxt, &tkt, &client_user)) { | |
128 | authenticated = 1; | |
129 | snprintf(info, sizeof(info), | |
130 | " tktuser %.100s", | |
131 | client_user); | |
132 | } | |
133 | #endif /* KRB4 */ | |
134 | } else { | |
135 | #ifdef KRB5 | |
136 | krb5_data tkt; | |
137 | tkt.length = dlen; | |
138 | tkt.data = kdata; | |
139 | ||
140 | if (auth_krb5(authctxt, &tkt, &client_user)) { | |
141 | authenticated = 1; | |
142 | snprintf(info, sizeof(info), | |
143 | " tktuser %.100s", | |
144 | client_user); | |
145 | } | |
146 | #endif /* KRB5 */ | |
147 | } | |
148 | xfree(kdata); | |
149 | } | |
150 | break; | |
151 | #endif /* KRB4 || KRB5 */ | |
152 | ||
153 | #if defined(AFS) || defined(KRB5) | |
154 | /* XXX - punt on backward compatibility here. */ | |
155 | case SSH_CMSG_HAVE_KERBEROS_TGT: | |
156 | packet_send_debug("Kerberos TGT passing disabled before authentication."); | |
157 | break; | |
158 | #ifdef AFS | |
159 | case SSH_CMSG_HAVE_AFS_TOKEN: | |
160 | packet_send_debug("AFS token passing disabled before authentication."); | |
161 | break; | |
162 | #endif /* AFS */ | |
163 | #endif /* AFS || KRB5 */ | |
164 | ||
165 | case SSH_CMSG_AUTH_RHOSTS: | |
166 | if (!options.rhosts_authentication) { | |
167 | verbose("Rhosts authentication disabled."); | |
168 | break; | |
169 | } | |
170 | /* | |
171 | * Get client user name. Note that we just have to | |
172 | * trust the client; this is one reason why rhosts | |
173 | * authentication is insecure. (Another is | |
174 | * IP-spoofing on a local network.) | |
175 | */ | |
176 | client_user = packet_get_string(&ulen); | |
177 | packet_check_eom(); | |
178 | ||
179 | /* Try to authenticate using /etc/hosts.equiv and .rhosts. */ | |
180 | authenticated = auth_rhosts(pw, client_user); | |
181 | ||
182 | snprintf(info, sizeof info, " ruser %.100s", client_user); | |
183 | break; | |
184 | ||
185 | case SSH_CMSG_AUTH_RHOSTS_RSA: | |
186 | if (!options.rhosts_rsa_authentication) { | |
187 | verbose("Rhosts with RSA authentication disabled."); | |
188 | break; | |
189 | } | |
190 | /* | |
191 | * Get client user name. Note that we just have to | |
192 | * trust the client; root on the client machine can | |
193 | * claim to be any user. | |
194 | */ | |
195 | client_user = packet_get_string(&ulen); | |
196 | ||
197 | /* Get the client host key. */ | |
198 | client_host_key = key_new(KEY_RSA1); | |
199 | bits = packet_get_int(); | |
200 | packet_get_bignum(client_host_key->rsa->e); | |
201 | packet_get_bignum(client_host_key->rsa->n); | |
202 | ||
203 | if (bits != BN_num_bits(client_host_key->rsa->n)) | |
204 | verbose("Warning: keysize mismatch for client_host_key: " | |
205 | "actual %d, announced %d", | |
206 | BN_num_bits(client_host_key->rsa->n), bits); | |
207 | packet_check_eom(); | |
208 | ||
209 | authenticated = auth_rhosts_rsa(pw, client_user, | |
210 | client_host_key); | |
211 | key_free(client_host_key); | |
212 | ||
213 | snprintf(info, sizeof info, " ruser %.100s", client_user); | |
214 | break; | |
215 | ||
216 | case SSH_CMSG_AUTH_RSA: | |
217 | if (!options.rsa_authentication) { | |
218 | verbose("RSA authentication disabled."); | |
219 | break; | |
220 | } | |
221 | /* RSA authentication requested. */ | |
222 | if ((n = BN_new()) == NULL) | |
223 | fatal("do_authloop: BN_new failed"); | |
224 | packet_get_bignum(n); | |
225 | packet_check_eom(); | |
226 | authenticated = auth_rsa(pw, n); | |
227 | BN_clear_free(n); | |
228 | break; | |
229 | ||
230 | case SSH_CMSG_AUTH_PASSWORD: | |
231 | if (!options.password_authentication) { | |
232 | verbose("Password authentication disabled."); | |
233 | break; | |
234 | } | |
235 | /* | |
236 | * Read user password. It is in plain text, but was | |
237 | * transmitted over the encrypted channel so it is | |
238 | * not visible to an outside observer. | |
239 | */ | |
240 | password = packet_get_string(&dlen); | |
241 | packet_check_eom(); | |
242 | ||
243 | /* Try authentication with the password. */ | |
244 | authenticated = PRIVSEP(auth_password(authctxt, password)); | |
245 | ||
246 | memset(password, 0, strlen(password)); | |
247 | xfree(password); | |
248 | break; | |
249 | ||
250 | case SSH_CMSG_AUTH_TIS: | |
251 | debug("rcvd SSH_CMSG_AUTH_TIS"); | |
252 | if (options.challenge_response_authentication == 1) { | |
253 | char *challenge = get_challenge(authctxt); | |
254 | if (challenge != NULL) { | |
255 | debug("sending challenge '%s'", challenge); | |
256 | packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE); | |
257 | packet_put_cstring(challenge); | |
258 | xfree(challenge); | |
259 | packet_send(); | |
260 | packet_write_wait(); | |
261 | continue; | |
262 | } | |
263 | } | |
264 | break; | |
265 | case SSH_CMSG_AUTH_TIS_RESPONSE: | |
266 | debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE"); | |
267 | if (options.challenge_response_authentication == 1) { | |
268 | char *response = packet_get_string(&dlen); | |
269 | debug("got response '%s'", response); | |
270 | packet_check_eom(); | |
271 | authenticated = verify_response(authctxt, response); | |
272 | memset(response, 'r', dlen); | |
273 | xfree(response); | |
274 | } | |
275 | break; | |
276 | ||
277 | default: | |
278 | /* | |
279 | * Any unknown messages will be ignored (and failure | |
280 | * returned) during authentication. | |
281 | */ | |
282 | log("Unknown message during authentication: type %d", type); | |
283 | break; | |
284 | } | |
285 | #ifdef BSD_AUTH | |
286 | if (authctxt->as) { | |
287 | auth_close(authctxt->as); | |
288 | authctxt->as = NULL; | |
289 | } | |
290 | #endif | |
291 | if (!authctxt->valid && authenticated) | |
292 | fatal("INTERNAL ERROR: authenticated invalid user %s", | |
293 | authctxt->user); | |
294 | ||
295 | #ifdef HAVE_CYGWIN | |
296 | if (authenticated && | |
297 | !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD, pw)) { | |
298 | packet_disconnect("Authentication rejected for uid %d.", | |
299 | pw == NULL ? -1 : pw->pw_uid); | |
300 | authenticated = 0; | |
301 | } | |
302 | #else | |
303 | /* Special handling for root */ | |
304 | if (authenticated && authctxt->pw->pw_uid == 0 && | |
305 | !auth_root_allowed(get_authname(type))) | |
306 | authenticated = 0; | |
307 | #endif | |
308 | #ifdef USE_PAM | |
309 | if (authenticated && !do_pam_account(pw->pw_name, client_user)) | |
310 | authenticated = 0; | |
311 | #endif | |
312 | ||
313 | /* Log before sending the reply */ | |
314 | auth_log(authctxt, authenticated, get_authname(type), info); | |
315 | ||
316 | if (client_user != NULL) { | |
317 | xfree(client_user); | |
318 | client_user = NULL; | |
319 | } | |
320 | ||
321 | if (authenticated) | |
322 | return; | |
323 | ||
324 | if (authctxt->failures++ > AUTH_FAIL_MAX) { | |
325 | #ifdef WITH_AIXAUTHENTICATE | |
326 | loginfailed(authctxt->user, | |
327 | get_canonical_hostname(options.verify_reverse_mapping), | |
328 | "ssh"); | |
329 | #endif /* WITH_AIXAUTHENTICATE */ | |
330 | packet_disconnect(AUTH_FAIL_MSG, authctxt->user); | |
331 | } | |
332 | ||
333 | packet_start(SSH_SMSG_FAILURE); | |
334 | packet_send(); | |
335 | packet_write_wait(); | |
336 | } | |
337 | } | |
338 | ||
339 | /* | |
340 | * Performs authentication of an incoming connection. Session key has already | |
341 | * been exchanged and encryption is enabled. | |
342 | */ | |
343 | Authctxt * | |
344 | do_authentication(void) | |
345 | { | |
346 | Authctxt *authctxt; | |
347 | u_int ulen; | |
348 | char *p, *user, *style = NULL; | |
349 | ||
350 | /* Get the name of the user that we wish to log in as. */ | |
351 | packet_read_expect(SSH_CMSG_USER); | |
352 | ||
353 | /* Get the user name. */ | |
354 | user = packet_get_string(&ulen); | |
355 | packet_check_eom(); | |
356 | ||
357 | if ((style = strchr(user, ':')) != NULL) | |
358 | *style++ = '\0'; | |
359 | ||
360 | /* XXX - SSH.com Kerberos v5 braindeath. */ | |
361 | if ((p = strchr(user, '@')) != NULL) | |
362 | *p = '\0'; | |
363 | ||
364 | authctxt = authctxt_new(); | |
365 | authctxt->user = user; | |
366 | authctxt->style = style; | |
367 | ||
368 | /* Verify that the user is a valid user. */ | |
369 | if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL) | |
370 | authctxt->valid = 1; | |
371 | else | |
372 | debug("do_authentication: illegal user %s", user); | |
373 | ||
374 | setproctitle("%s%s", authctxt->pw ? user : "unknown", | |
375 | use_privsep ? " [net]" : ""); | |
376 | ||
377 | #ifdef USE_PAM | |
378 | start_pam(authctxt->pw == NULL ? "NOUSER" : user); | |
379 | #endif | |
380 | ||
381 | /* | |
382 | * If we are not running as root, the user must have the same uid as | |
383 | * the server. (Unless you are running Windows) | |
384 | */ | |
385 | #ifndef HAVE_CYGWIN | |
386 | if (!use_privsep && getuid() != 0 && authctxt->pw && | |
387 | authctxt->pw->pw_uid != getuid()) | |
388 | packet_disconnect("Cannot change user when server not running as root."); | |
389 | #endif | |
390 | ||
391 | /* | |
392 | * Loop until the user has been authenticated or the connection is | |
393 | * closed, do_authloop() returns only if authentication is successful | |
394 | */ | |
395 | do_authloop(authctxt); | |
396 | ||
397 | /* The user has been authenticated and accepted. */ | |
398 | packet_start(SSH_SMSG_SUCCESS); | |
399 | packet_send(); | |
400 | packet_write_wait(); | |
401 | ||
402 | return (authctxt); | |
403 | } |