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