]> andersk Git - gssapi-openssh.git/blame - openssh/auth2.c
Import of OpenSSH 4.9p1
[gssapi-openssh.git] / openssh / auth2.c
CommitLineData
47686178 1/* $OpenBSD: auth2.c,v 1.116 2007/09/29 00:25:51 dtucker 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>
29
30#include <pwd.h>
31#include <stdarg.h>
32#include <string.h>
33
3c0ef626 34#include "xmalloc.h"
9108f8d9 35#include "ssh2.h"
3c0ef626 36#include "packet.h"
3c0ef626 37#include "log.h"
9108f8d9 38#include "buffer.h"
3c0ef626 39#include "servconf.h"
40#include "compat.h"
9108f8d9 41#include "key.h"
42#include "hostfile.h"
3c0ef626 43#include "auth.h"
3c0ef626 44#include "dispatch.h"
3c0ef626 45#include "pathnames.h"
996d5e62 46#include "buffer.h"
3c0ef626 47
0fff78ff 48#ifdef GSSAPI
49#include "ssh-gss.h"
50#endif
9108f8d9 51#include "monitor_wrap.h"
0fff78ff 52
3c0ef626 53/* import */
54extern ServerOptions options;
55extern u_char *session_id2;
0fff78ff 56extern u_int session_id2_len;
996d5e62 57extern Buffer loginmsg;
3c0ef626 58
f5799ae1 59/* methods */
60
61extern Authmethod method_none;
62extern Authmethod method_pubkey;
63extern Authmethod method_passwd;
64extern Authmethod method_kbdint;
65extern Authmethod method_hostbased;
0fff78ff 66#ifdef GSSAPI
67extern Authmethod method_gssapi;
68#endif
f5799ae1 69
70Authmethod *authmethods[] = {
71 &method_none,
72 &method_pubkey,
0fff78ff 73#ifdef GSSAPI
74 &method_gssapi,
75#endif
f5799ae1 76 &method_passwd,
77 &method_kbdint,
78 &method_hostbased,
79 NULL
3c0ef626 80};
81
82/* protocol */
83
e9a17296 84static void input_service_request(int, u_int32_t, void *);
85static void input_userauth_request(int, u_int32_t, void *);
3c0ef626 86
87/* helper */
88static Authmethod *authmethod_lookup(const char *);
89static char *authmethods_get(void);
3c0ef626 90
3c0ef626 91/*
92 * loop until authctxt->success == TRUE
93 */
94
cdd66111 95void
96do_authentication2(Authctxt *authctxt)
3c0ef626 97{
e9a17296 98 dispatch_init(&dispatch_protocol_error);
3c0ef626 99 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
100 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
3c0ef626 101}
102
9108f8d9 103/*ARGSUSED*/
3c0ef626 104static void
e9a17296 105input_service_request(int type, u_int32_t seq, void *ctxt)
3c0ef626 106{
107 Authctxt *authctxt = ctxt;
108 u_int len;
41b2f314 109 int acceptit = 0;
3c0ef626 110 char *service = packet_get_string(&len);
e9a17296 111 packet_check_eom();
3c0ef626 112
113 if (authctxt == NULL)
114 fatal("input_service_request: no authctxt");
115
116 if (strcmp(service, "ssh-userauth") == 0) {
117 if (!authctxt->success) {
41b2f314 118 acceptit = 1;
3c0ef626 119 /* now we can handle user-auth requests */
120 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
121 }
122 }
123 /* XXX all other service requests are denied */
124
41b2f314 125 if (acceptit) {
3c0ef626 126 packet_start(SSH2_MSG_SERVICE_ACCEPT);
127 packet_put_cstring(service);
128 packet_send();
129 packet_write_wait();
130 } else {
131 debug("bad service request %s", service);
132 packet_disconnect("bad service request %s", service);
133 }
134 xfree(service);
135}
136
9108f8d9 137/*ARGSUSED*/
3c0ef626 138static void
e9a17296 139input_userauth_request(int type, u_int32_t seq, void *ctxt)
3c0ef626 140{
141 Authctxt *authctxt = ctxt;
142 Authmethod *m = NULL;
143 char *user, *service, *method, *style = NULL;
144 int authenticated = 0;
145
146 if (authctxt == NULL)
147 fatal("input_userauth_request: no authctxt");
148
149 user = packet_get_string(NULL);
150 service = packet_get_string(NULL);
151 method = packet_get_string(NULL);
152 debug("userauth-request for user %s service %s method %s", user, service, method);
153 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
154
155 if ((style = strchr(user, ':')) != NULL)
156 *style++ = 0;
157
158 if (authctxt->attempt++ == 0) {
159 /* setup auth context */
700318f3 160 authctxt->pw = PRIVSEP(getpwnamallow(user));
99be0775 161 authctxt->user = xstrdup(user);
700318f3 162 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
3c0ef626 163 authctxt->valid = 1;
164 debug2("input_userauth_request: setting up authctxt for %s", user);
3c0ef626 165 } else {
c9f39d2c 166 logit("input_userauth_request: invalid user %s", user);
0fff78ff 167 authctxt->pw = fakepw();
996d5e62 168#ifdef SSH_AUDIT_EVENTS
169 PRIVSEP(audit_event(SSH_INVALID_USER));
3c0ef626 170#endif
171 }
2c06c99b 172#ifdef USE_PAM
173 if (options.use_pam)
174 PRIVSEP(start_pam(authctxt));
175#endif
c9f39d2c 176 setproctitle("%s%s", authctxt->valid ? user : "unknown",
700318f3 177 use_privsep ? " [net]" : "");
3c0ef626 178 authctxt->service = xstrdup(service);
179 authctxt->style = style ? xstrdup(style) : NULL;
700318f3 180 if (use_privsep)
181 mm_inform_authserv(service, style);
3c0ef626 182 } else if (strcmp(user, authctxt->user) != 0 ||
183 strcmp(service, authctxt->service) != 0) {
184 packet_disconnect("Change of username or service not allowed: "
185 "(%s,%s) -> (%s,%s)",
186 authctxt->user, authctxt->service, user, service);
187 }
188 /* reset state */
e9a17296 189 auth2_challenge_stop(authctxt);
0fff78ff 190
191#ifdef GSSAPI
192 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
193 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
194#endif
195
3c0ef626 196 authctxt->postponed = 0;
3c0ef626 197
198 /* try to authenticate user */
199 m = authmethod_lookup(method);
200 if (m != NULL) {
201 debug2("input_userauth_request: try method %s", method);
202 authenticated = m->userauth(authctxt);
203 }
204 userauth_finish(authctxt, authenticated, method);
205
206 xfree(service);
207 xfree(user);
208 xfree(method);
209}
210
211void
212userauth_finish(Authctxt *authctxt, int authenticated, char *method)
213{
214 char *methods;
215
216 if (!authctxt->valid && authenticated)
217 fatal("INTERNAL ERROR: authenticated invalid user %s",
218 authctxt->user);
219
220 /* Special handling for root */
6a9b3198 221 if (authenticated && authctxt->pw->pw_uid == 0 &&
996d5e62 222 !auth_root_allowed(method)) {
3c0ef626 223 authenticated = 0;
996d5e62 224#ifdef SSH_AUDIT_EVENTS
225 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
226#endif
227 }
3c0ef626 228
229#ifdef USE_PAM
996d5e62 230 if (options.use_pam && authenticated) {
231 if (!PRIVSEP(do_pam_account())) {
232 /* if PAM returned a message, send it to the user */
233 if (buffer_len(&loginmsg) > 0) {
234 buffer_append(&loginmsg, "\0", 1);
235 userauth_send_banner(buffer_ptr(&loginmsg));
236 packet_write_wait();
237 }
238 fatal("Access denied for user %s by PAM account "
665a873d 239 "configuration", authctxt->user);
996d5e62 240 }
241 }
0fff78ff 242#endif
3c0ef626 243
41b2f314 244#ifdef _UNICOS
245 if (authenticated && cray_access_denied(authctxt->user)) {
246 authenticated = 0;
247 fatal("Access denied for user %s.",authctxt->user);
248 }
249#endif /* _UNICOS */
250
3c0ef626 251 /* Log before sending the reply */
252 auth_log(authctxt, authenticated, method, " ssh2");
253
254 if (authctxt->postponed)
255 return;
256
257 /* XXX todo: check if multiple auth methods are needed */
258 if (authenticated == 1) {
259 /* turn off userauth */
e9a17296 260 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
3c0ef626 261 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
262 packet_send();
263 packet_write_wait();
264 /* now we can break out */
265 authctxt->success = 1;
266 } else {
996d5e62 267 if (authctxt->failures++ > options.max_authtries) {
268#ifdef SSH_AUDIT_EVENTS
269 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
270#endif
3c0ef626 271 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
996d5e62 272 }
3c0ef626 273 methods = authmethods_get();
274 packet_start(SSH2_MSG_USERAUTH_FAILURE);
275 packet_put_cstring(methods);
276 packet_put_char(0); /* XXX partial success, unused */
277 packet_send();
278 packet_write_wait();
279 xfree(methods);
280 }
281}
282
3c0ef626 283static char *
284authmethods_get(void)
285{
e9a17296 286 Buffer b;
3c0ef626 287 char *list;
f5799ae1 288 int i;
3c0ef626 289
e9a17296 290 buffer_init(&b);
f5799ae1 291 for (i = 0; authmethods[i] != NULL; i++) {
292 if (strcmp(authmethods[i]->name, "none") == 0)
3c0ef626 293 continue;
f5799ae1 294 if (authmethods[i]->enabled != NULL &&
295 *(authmethods[i]->enabled) != 0) {
e9a17296 296 if (buffer_len(&b) > 0)
297 buffer_append(&b, ",", 1);
f5799ae1 298 buffer_append(&b, authmethods[i]->name,
299 strlen(authmethods[i]->name));
3c0ef626 300 }
301 }
e9a17296 302 buffer_append(&b, "\0", 1);
303 list = xstrdup(buffer_ptr(&b));
304 buffer_free(&b);
3c0ef626 305 return list;
306}
307
308static Authmethod *
309authmethod_lookup(const char *name)
310{
f5799ae1 311 int i;
312
3c0ef626 313 if (name != NULL)
f5799ae1 314 for (i = 0; authmethods[i] != NULL; i++)
315 if (authmethods[i]->enabled != NULL &&
316 *(authmethods[i]->enabled) != 0 &&
317 strcmp(name, authmethods[i]->name) == 0)
318 return authmethods[i];
319 debug2("Unrecognized authentication method name: %s",
320 name ? name : "NULL");
3c0ef626 321 return NULL;
322}
This page took 1.057507 seconds and 5 git commands to generate.