]> andersk Git - gssapi-openssh.git/blame - openssh/auth2.c
o Bump version to 1.2.
[gssapi-openssh.git] / openssh / auth2.c
CommitLineData
3c0ef626 1/*
2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "includes.h"
ff2d7a98 26RCSID("$OpenBSD: auth2.c,v 1.93 2002/05/31 11:35:15 markus Exp $");
3c0ef626 27
28#include "ssh2.h"
a2d82e42 29#include "ssh1.h"
3c0ef626 30#include "xmalloc.h"
3c0ef626 31#include "packet.h"
3c0ef626 32#include "log.h"
33#include "servconf.h"
34#include "compat.h"
3c0ef626 35#include "auth.h"
3c0ef626 36#include "dispatch.h"
3c0ef626 37#include "pathnames.h"
2980ea68 38#include "monitor_wrap.h"
078f71c2 39#include "misc.h"
3c0ef626 40
5598e598 41#ifdef GSSAPI
42#include "ssh-gss.h"
43#endif
44
3c0ef626 45/* import */
46extern ServerOptions options;
47extern u_char *session_id2;
48extern int session_id2_len;
49
2980ea68 50Authctxt *x_authctxt = NULL;
3c0ef626 51
ff2d7a98 52/* methods */
53
54extern Authmethod method_none;
55#ifdef GSSAPI
56extern Authmethod method_external;
57extern Authmethod method_gssapi;
58#endif
59extern Authmethod method_pubkey;
60extern Authmethod method_passwd;
61extern Authmethod method_kbdint;
62extern Authmethod method_hostbased;
63
64Authmethod *authmethods[] = {
65 &method_none,
66#ifdef GSSAPI
67 &method_external,
68 &method_gssapi,
69#endif
70 &method_pubkey,
71 &method_passwd,
72 &method_kbdint,
73 &method_hostbased,
74 NULL
3c0ef626 75};
76
77/* protocol */
78
1e608e42 79static void input_service_request(int, u_int32_t, void *);
80static void input_userauth_request(int, u_int32_t, void *);
3c0ef626 81
82/* helper */
83static Authmethod *authmethod_lookup(const char *);
84static char *authmethods_get(void);
2980ea68 85int user_key_allowed(struct passwd *, Key *);
86int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
3c0ef626 87
3c0ef626 88/*
89 * loop until authctxt->success == TRUE
90 */
91
2980ea68 92Authctxt *
1e608e42 93do_authentication2(void)
3c0ef626 94{
95 Authctxt *authctxt = authctxt_new();
96
97 x_authctxt = authctxt; /*XXX*/
98
99 /* challenge-response is implemented via keyboard interactive */
100 if (options.challenge_response_authentication)
101 options.kbd_interactive_authentication = 1;
102 if (options.pam_authentication_via_kbd_int)
103 options.kbd_interactive_authentication = 1;
2980ea68 104 if (use_privsep)
105 options.pam_authentication_via_kbd_int = 0;
3c0ef626 106
1e608e42 107 dispatch_init(&dispatch_protocol_error);
3c0ef626 108 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
109 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
2980ea68 110
111 return (authctxt);
3c0ef626 112}
113
114static void
1e608e42 115input_service_request(int type, u_int32_t seq, void *ctxt)
3c0ef626 116{
117 Authctxt *authctxt = ctxt;
118 u_int len;
119 int accept = 0;
120 char *service = packet_get_string(&len);
1e608e42 121 packet_check_eom();
3c0ef626 122
123 if (authctxt == NULL)
124 fatal("input_service_request: no authctxt");
125
126 if (strcmp(service, "ssh-userauth") == 0) {
127 if (!authctxt->success) {
128 accept = 1;
129 /* now we can handle user-auth requests */
130 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
131 }
132 }
133 /* XXX all other service requests are denied */
134
135 if (accept) {
136 packet_start(SSH2_MSG_SERVICE_ACCEPT);
137 packet_put_cstring(service);
138 packet_send();
139 packet_write_wait();
140 } else {
141 debug("bad service request %s", service);
142 packet_disconnect("bad service request %s", service);
143 }
144 xfree(service);
145}
146
147static void
1e608e42 148input_userauth_request(int type, u_int32_t seq, void *ctxt)
3c0ef626 149{
150 Authctxt *authctxt = ctxt;
151 Authmethod *m = NULL;
152 char *user, *service, *method, *style = NULL;
153 int authenticated = 0;
154
155 if (authctxt == NULL)
156 fatal("input_userauth_request: no authctxt");
157
158 user = packet_get_string(NULL);
159 service = packet_get_string(NULL);
160 method = packet_get_string(NULL);
2980ea68 161
162#ifdef GSSAPI
ff2d7a98 163 if (strcmp(user, "") == 0) {
164 char *lname = NULL;
165 debug("gssapi received empty username");
166 PRIVSEP(ssh_gssapi_localname(&lname));
167 if (lname && lname[0] != '\0') {
168 xfree(user);
169 user = lname;
170 debug("gssapi successfully set username to %s", user);
171 } else if (authctxt->valid) {
172 debug("failed to set username from gssapi context");
173 goto finish;
174 }
175 }
176#endif
2980ea68 177
3c0ef626 178 debug("userauth-request for user %s service %s method %s", user, service, method);
179 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
180
181 if ((style = strchr(user, ':')) != NULL)
182 *style++ = 0;
183
ff2d7a98 184 if (!authctxt->user ||
185 strcmp(user, authctxt->user) != 0) {
3c0ef626 186 /* setup auth context */
ff2d7a98 187 if (authctxt->user) {
188 xfree(authctxt->user);
189 authctxt->user = NULL;
190 }
191 if (authctxt->service) {
192 xfree(authctxt->service);
193 authctxt->service = NULL;
194 }
195 if (authctxt->style) {
196 xfree(authctxt->style);
197 authctxt->style = NULL;
198 }
2980ea68 199 authctxt->pw = PRIVSEP(getpwnamallow(user));
200 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
3c0ef626 201 authctxt->valid = 1;
202 debug2("input_userauth_request: setting up authctxt for %s", user);
203#ifdef USE_PAM
2980ea68 204 PRIVSEP(start_pam(authctxt->pw->pw_name));
3c0ef626 205#endif
206 } else {
ff2d7a98 207 authctxt->valid = 0;
3c0ef626 208 log("input_userauth_request: illegal user %s", user);
209#ifdef USE_PAM
2980ea68 210 PRIVSEP(start_pam("NOUSER"));
3c0ef626 211#endif
212 }
2980ea68 213 setproctitle("%s%s", authctxt->pw ? user : "unknown",
214 use_privsep ? " [net]" : "");
3c0ef626 215 authctxt->user = xstrdup(user);
216 authctxt->service = xstrdup(service);
217 authctxt->style = style ? xstrdup(style) : NULL;
2980ea68 218 if (use_privsep)
219 mm_inform_authserv(service, style);
3c0ef626 220 }
221 /* reset state */
1e608e42 222 auth2_challenge_stop(authctxt);
5598e598 223
224#ifdef GSSAPI
1e608e42 225 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
226 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
5598e598 227#endif
228
3c0ef626 229 authctxt->postponed = 0;
3c0ef626 230
231 /* try to authenticate user */
232 m = authmethod_lookup(method);
233 if (m != NULL) {
234 debug2("input_userauth_request: try method %s", method);
235 authenticated = m->userauth(authctxt);
236 }
ff2d7a98 237finish:
3c0ef626 238 userauth_finish(authctxt, authenticated, method);
239
240 xfree(service);
241 xfree(user);
242 xfree(method);
243}
244
245void
246userauth_finish(Authctxt *authctxt, int authenticated, char *method)
247{
248 char *methods;
249
250 if (!authctxt->valid && authenticated)
251 fatal("INTERNAL ERROR: authenticated invalid user %s",
252 authctxt->user);
253
254 /* Special handling for root */
255 if (authenticated && authctxt->pw->pw_uid == 0 &&
256 !auth_root_allowed(method))
257 authenticated = 0;
258
259#ifdef USE_PAM
2980ea68 260 if (!use_privsep && authenticated && authctxt->user &&
261 !do_pam_account(authctxt->user, NULL))
3c0ef626 262 authenticated = 0;
263#endif /* USE_PAM */
264
265 /* Log before sending the reply */
a2d82e42 266 if (!compat20)
267 auth_log(authctxt, authenticated, method, " ssh1");
268 else
3c0ef626 269 auth_log(authctxt, authenticated, method, " ssh2");
270
271 if (authctxt->postponed)
272 return;
273
274 /* XXX todo: check if multiple auth methods are needed */
275 if (authenticated == 1) {
276 /* turn off userauth */
1e608e42 277 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
ff2d7a98 278 if (compat20) {
3c0ef626 279 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
280 packet_send();
281 packet_write_wait();
ff2d7a98 282 }
3c0ef626 283 /* now we can break out */
284 authctxt->success = 1;
285 } else {
286 if (authctxt->failures++ > AUTH_FAIL_MAX) {
287#ifdef WITH_AIXAUTHENTICATE
ff2d7a98 288 /* XXX: privsep */
3c0ef626 289 loginfailed(authctxt->user,
1e608e42 290 get_canonical_hostname(options.verify_reverse_mapping),
3c0ef626 291 "ssh");
292#endif /* WITH_AIXAUTHENTICATE */
293 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
294 }
2980ea68 295 if (!compat20) {
296 /*
297 * Break out of the dispatch loop now and go back to
298 * SSH1 code. We need to set the 'success' flag to
299 * break out of the loop. Set the 'postponed' flag to
300 * tell the SSH1 code that authentication failed. The
301 * SSH1 code will handle sending SSH_SMSG_FAILURE.
302 */
303 authctxt->success = authctxt->postponed = 1;
304 } else {
3c0ef626 305 methods = authmethods_get();
a2d82e42 306 if (!compat20)
307 packet_disconnect("GSSAPI authentication failed");
308 else
3c0ef626 309 packet_start(SSH2_MSG_USERAUTH_FAILURE);
310 packet_put_cstring(methods);
311 packet_put_char(0); /* XXX partial success, unused */
312 packet_send();
313 packet_write_wait();
314 xfree(methods);
2980ea68 315 }
3c0ef626 316 }
317}
318
3c0ef626 319/* get current user */
320
321struct passwd*
322auth_get_user(void)
323{
324 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
325}
326
327#define DELIM ","
328
329static char *
330authmethods_get(void)
331{
1e608e42 332 Buffer b;
3c0ef626 333 char *list;
ff2d7a98 334 int i;
3c0ef626 335
1e608e42 336 buffer_init(&b);
ff2d7a98 337 for (i = 0; authmethods[i] != NULL; i++) {
338 if (strcmp(authmethods[i]->name, "none") == 0)
3c0ef626 339 continue;
ff2d7a98 340 if (authmethods[i]->enabled != NULL &&
341 *(authmethods[i]->enabled) != 0) {
1e608e42 342 if (buffer_len(&b) > 0)
343 buffer_append(&b, ",", 1);
ff2d7a98 344 buffer_append(&b, authmethods[i]->name,
345 strlen(authmethods[i]->name));
3c0ef626 346 }
347 }
1e608e42 348 buffer_append(&b, "\0", 1);
349 list = xstrdup(buffer_ptr(&b));
350 buffer_free(&b);
3c0ef626 351 return list;
352}
353
354static Authmethod *
355authmethod_lookup(const char *name)
356{
ff2d7a98 357 int i;
358
3c0ef626 359 if (name != NULL)
ff2d7a98 360 for (i = 0; authmethods[i] != NULL; i++)
361 if (authmethods[i]->enabled != NULL &&
362 *(authmethods[i]->enabled) != 0 &&
363 strcmp(name, authmethods[i]->name) == 0)
364 return authmethods[i];
365 debug2("Unrecognized authentication method name: %s",
366 name ? name : "NULL");
3c0ef626 367 return NULL;
368}
This page took 0.137967 seconds and 5 git commands to generate.