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