]> andersk Git - gssapi-openssh.git/blame_incremental - openssh/auth2.c
Merged hpn13v5 to trunk.
[gssapi-openssh.git] / openssh / auth2.c
... / ...
CommitLineData
1/* $OpenBSD: auth2.c,v 1.119 2008/07/04 23:30:16 djm Exp $ */
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"
27
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <sys/uio.h>
31
32#include <fcntl.h>
33#include <pwd.h>
34#include <stdarg.h>
35#include <string.h>
36#include <unistd.h>
37
38#include "xmalloc.h"
39#include "atomicio.h"
40#include "ssh2.h"
41#include "packet.h"
42#include "log.h"
43#include "buffer.h"
44#include "servconf.h"
45#include "compat.h"
46#include "key.h"
47#include "hostfile.h"
48#include "auth.h"
49#include "dispatch.h"
50#include "pathnames.h"
51#include "buffer.h"
52#include "canohost.h"
53
54#ifdef GSSAPI
55#include "ssh-gss.h"
56#endif
57#include "monitor_wrap.h"
58
59/* import */
60extern ServerOptions options;
61extern u_char *session_id2;
62extern u_int session_id2_len;
63extern Buffer loginmsg;
64
65/* methods */
66
67extern Authmethod method_none;
68extern Authmethod method_pubkey;
69extern Authmethod method_passwd;
70extern Authmethod method_kbdint;
71extern Authmethod method_hostbased;
72#ifdef GSSAPI
73extern Authmethod method_external;
74extern Authmethod method_gsskeyex;
75extern Authmethod method_gssapi;
76extern Authmethod method_gssapi_compat;
77#endif
78
79static int log_flag = 0;
80
81
82Authmethod *authmethods[] = {
83 &method_none,
84 &method_pubkey,
85#ifdef GSSAPI
86 &method_gsskeyex,
87 &method_external,
88 &method_gssapi,
89 &method_gssapi_compat,
90#endif
91 &method_passwd,
92 &method_kbdint,
93 &method_hostbased,
94 NULL
95};
96
97/* protocol */
98
99static void input_service_request(int, u_int32_t, void *);
100static void input_userauth_request(int, u_int32_t, void *);
101
102/* helper */
103static Authmethod *authmethod_lookup(const char *);
104static char *authmethods_get(void);
105
106char *
107auth2_read_banner(void)
108{
109 struct stat st;
110 char *banner = NULL;
111 size_t len, n;
112 int fd;
113
114 if ((fd = open(options.banner, O_RDONLY)) == -1)
115 return (NULL);
116 if (fstat(fd, &st) == -1) {
117 close(fd);
118 return (NULL);
119 }
120 if (st.st_size > 1*1024*1024) {
121 close(fd);
122 return (NULL);
123 }
124
125 len = (size_t)st.st_size; /* truncate */
126 banner = xmalloc(len + 1);
127 n = atomicio(read, fd, banner, len);
128 close(fd);
129
130 if (n != len) {
131 xfree(banner);
132 return (NULL);
133 }
134 banner[n] = '\0';
135
136 return (banner);
137}
138
139void
140userauth_send_banner(const char *msg)
141{
142 if (datafellows & SSH_BUG_BANNER)
143 return;
144
145 packet_start(SSH2_MSG_USERAUTH_BANNER);
146 packet_put_cstring(msg);
147 packet_put_cstring(""); /* language, unused */
148 packet_send();
149 debug("%s: sent", __func__);
150}
151
152static void
153userauth_banner(void)
154{
155 char *banner = NULL;
156
157 if (options.banner == NULL ||
158 strcasecmp(options.banner, "none") == 0 ||
159 (datafellows & SSH_BUG_BANNER) != 0)
160 return;
161
162 if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
163 goto done;
164 userauth_send_banner(banner);
165
166done:
167 if (banner)
168 xfree(banner);
169}
170
171/*
172 * loop until authctxt->success == TRUE
173 */
174void
175do_authentication2(Authctxt *authctxt)
176{
177 dispatch_init(&dispatch_protocol_error);
178 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
179 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
180}
181
182/*ARGSUSED*/
183static void
184input_service_request(int type, u_int32_t seq, void *ctxt)
185{
186 Authctxt *authctxt = ctxt;
187 u_int len;
188 int acceptit = 0;
189 char *service = packet_get_string(&len);
190 packet_check_eom();
191
192 if (authctxt == NULL)
193 fatal("input_service_request: no authctxt");
194
195 if (strcmp(service, "ssh-userauth") == 0) {
196 if (!authctxt->success) {
197 acceptit = 1;
198 /* now we can handle user-auth requests */
199 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
200 }
201 }
202 /* XXX all other service requests are denied */
203
204 if (acceptit) {
205 packet_start(SSH2_MSG_SERVICE_ACCEPT);
206 packet_put_cstring(service);
207 packet_send();
208 packet_write_wait();
209 } else {
210 debug("bad service request %s", service);
211 packet_disconnect("bad service request %s", service);
212 }
213 xfree(service);
214}
215
216/*ARGSUSED*/
217static void
218input_userauth_request(int type, u_int32_t seq, void *ctxt)
219{
220 Authctxt *authctxt = ctxt;
221 Authmethod *m = NULL;
222 char *user, *service, *method, *style = NULL;
223 int authenticated = 0;
224
225 if (authctxt == NULL)
226 fatal("input_userauth_request: no authctxt");
227
228 user = packet_get_string(NULL);
229 service = packet_get_string(NULL);
230 method = packet_get_string(NULL);
231
232#ifdef GSSAPI
233 if (user[0] == '\0') {
234 debug("received empty username for %s", method);
235 if (strcmp(method, "external-keyx") == 0 ||
236 strcmp(method, "gssapi-keyex") == 0) {
237 char *lname = NULL;
238 PRIVSEP(ssh_gssapi_localname(&lname));
239 if (lname && lname[0] != '\0') {
240 xfree(user);
241 user = lname;
242 debug("set username to %s from gssapi context", user);
243 } else {
244 debug("failed to set username from gssapi context");
245 packet_send_debug("failed to set username from gssapi context");
246 }
247 }
248 }
249#endif
250
251 debug("userauth-request for user %s service %s method %s",
252 user[0] ? user : "<implicit>", service, method);
253 if (!log_flag) {
254 logit("SSH: Server;Ltype: Authname;Remote: %s-%d;Name: %s",
255 get_remote_ipaddr(), get_remote_port(),
256 user[0] ? user : "<implicit>");
257 log_flag = 1;
258 }
259 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
260
261 if ((style = strchr(user, ':')) != NULL)
262 *style++ = 0;
263
264 /* If first time or username changed or empty username,
265 setup/reset authentication context. */
266 if ((authctxt->attempt++ == 0) ||
267 (strcmp(user, authctxt->user) != 0) ||
268 (strcmp(user, "") == 0)) {
269 if (authctxt->user) {
270 xfree(authctxt->user);
271 authctxt->user = NULL;
272 }
273 authctxt->valid = 0;
274 authctxt->user = xstrdup(user);
275 if (strcmp(service, "ssh-connection") != 0) {
276 packet_disconnect("Unsupported service %s", service);
277 }
278#ifdef GSSAPI
279 /* If we're going to set the username based on the
280 GSSAPI context later, then wait until then to
281 verify it. Just put in placeholders for now. */
282 if ((strcmp(user, "") == 0) &&
283 ((strcmp(method, "gssapi") == 0) ||
284 (strcmp(method, "gssapi-with-mic") == 0))) {
285 authctxt->pw = fakepw();
286 } else {
287#endif
288 authctxt->pw = PRIVSEP(getpwnamallow(user));
289 if (authctxt->pw) {
290 authctxt->valid = 1;
291 debug2("input_userauth_request: setting up authctxt for %s", user);
292 } else {
293 logit("input_userauth_request: invalid user %s", user);
294 authctxt->pw = fakepw();
295#ifdef SSH_AUDIT_EVENTS
296 PRIVSEP(audit_event(SSH_INVALID_USER));
297#endif
298 }
299#ifdef GSSAPI
300 } /* endif for setting username based on GSSAPI context */
301#endif
302#ifdef USE_PAM
303 if (options.use_pam)
304 PRIVSEP(start_pam(authctxt));
305#endif
306 setproctitle("%s%s", authctxt->valid ? user : "unknown",
307 use_privsep ? " [net]" : "");
308 if (authctxt->attempt == 1) {
309 authctxt->service = xstrdup(service);
310 authctxt->style = style ? xstrdup(style) : NULL;
311 if (use_privsep)
312 mm_inform_authserv(service, style);
313 userauth_banner();
314 }
315 }
316 if (strcmp(service, authctxt->service) != 0) {
317 packet_disconnect("Change of service not allowed: "
318 "(%s,%s) -> (%s,%s)",
319 authctxt->user, authctxt->service, user, service);
320 }
321 /* reset state */
322 auth2_challenge_stop(authctxt);
323
324#ifdef GSSAPI
325 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
326 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
327#endif
328
329 authctxt->postponed = 0;
330 authctxt->server_caused_failure = 0;
331
332 /* try to authenticate user */
333 m = authmethod_lookup(method);
334 if (m != NULL && authctxt->failures < options.max_authtries) {
335 debug2("input_userauth_request: try method %s", method);
336 authenticated = m->userauth(authctxt);
337 }
338 userauth_finish(authctxt, authenticated, method);
339
340 xfree(service);
341 xfree(user);
342 xfree(method);
343}
344
345void
346userauth_finish(Authctxt *authctxt, int authenticated, char *method)
347{
348 char *methods;
349
350 if (!authctxt->valid && authenticated)
351 fatal("INTERNAL ERROR: authenticated invalid user %s",
352 authctxt->user);
353
354 /* Special handling for root */
355 if (authenticated && authctxt->pw->pw_uid == 0 &&
356 !auth_root_allowed(method)) {
357 authenticated = 0;
358#ifdef SSH_AUDIT_EVENTS
359 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
360#endif
361 }
362
363#ifdef USE_PAM
364 if (options.use_pam && authenticated) {
365 if (!PRIVSEP(do_pam_account())) {
366 /* if PAM returned a message, send it to the user */
367 if (buffer_len(&loginmsg) > 0) {
368 buffer_append(&loginmsg, "\0", 1);
369 userauth_send_banner(buffer_ptr(&loginmsg));
370 packet_write_wait();
371 }
372 fatal("Access denied for user %s by PAM account "
373 "configuration", authctxt->user);
374 }
375 }
376#endif
377
378#ifdef _UNICOS
379 if (authenticated && cray_access_denied(authctxt->user)) {
380 authenticated = 0;
381 fatal("Access denied for user %s.",authctxt->user);
382 }
383#endif /* _UNICOS */
384
385 /* Log before sending the reply */
386 auth_log(authctxt, authenticated, method, " ssh2");
387
388 if (authctxt->postponed)
389 return;
390
391 /* XXX todo: check if multiple auth methods are needed */
392 if (authenticated == 1) {
393 /* turn off userauth */
394 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
395 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
396 packet_send();
397 packet_write_wait();
398 /* now we can break out */
399 authctxt->success = 1;
400 } else {
401 /* Dont count server configuration issues against the client */
402 /* Allow initial try of "none" auth without failure penalty */
403 if (!authctxt->server_caused_failure &&
404 (authctxt->attempt > 1 || strcmp(method, "none") != 0))
405 authctxt->failures++;
406 if (authctxt->failures >= options.max_authtries) {
407#ifdef SSH_AUDIT_EVENTS
408 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
409#endif
410 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
411 }
412 methods = authmethods_get();
413 packet_start(SSH2_MSG_USERAUTH_FAILURE);
414 packet_put_cstring(methods);
415 packet_put_char(0); /* XXX partial success, unused */
416 packet_send();
417 packet_write_wait();
418 xfree(methods);
419 }
420}
421
422static char *
423authmethods_get(void)
424{
425 Buffer b;
426 char *list;
427 int i;
428
429 buffer_init(&b);
430 for (i = 0; authmethods[i] != NULL; i++) {
431 if (strcmp(authmethods[i]->name, "none") == 0)
432 continue;
433 if (authmethods[i]->enabled != NULL &&
434 *(authmethods[i]->enabled) != 0) {
435 if (buffer_len(&b) > 0)
436 buffer_append(&b, ",", 1);
437 buffer_append(&b, authmethods[i]->name,
438 strlen(authmethods[i]->name));
439 }
440 }
441 buffer_append(&b, "\0", 1);
442 list = xstrdup(buffer_ptr(&b));
443 buffer_free(&b);
444 return list;
445}
446
447static Authmethod *
448authmethod_lookup(const char *name)
449{
450 int i;
451
452 if (name != NULL)
453 for (i = 0; authmethods[i] != NULL; i++)
454 if (authmethods[i]->enabled != NULL &&
455 *(authmethods[i]->enabled) != 0 &&
456 strcmp(name, authmethods[i]->name) == 0)
457 return authmethods[i];
458 debug2("Unrecognized authentication method name: %s",
459 name ? name : "NULL");
460 return NULL;
461}
462
This page took 0.038406 seconds and 5 git commands to generate.