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