]> andersk Git - gssapi-openssh.git/blame - openssh/auth2.c
new checksums (oops)
[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"
1b56ff3d 26RCSID("$OpenBSD: auth2.c,v 1.107 2004/07/28 09:40:29 markus Exp $");
3c0ef626 27
28#include "ssh2.h"
29#include "xmalloc.h"
3c0ef626 30#include "packet.h"
3c0ef626 31#include "log.h"
32#include "servconf.h"
416fd2a8 33#include "compat.h"
3c0ef626 34#include "auth.h"
3c0ef626 35#include "dispatch.h"
3c0ef626 36#include "pathnames.h"
2980ea68 37#include "monitor_wrap.h"
34fee935 38#include "buffer.h"
3c0ef626 39
5598e598 40#ifdef GSSAPI
41#include "ssh-gss.h"
42#endif
43
3c0ef626 44/* import */
45extern ServerOptions options;
46extern u_char *session_id2;
70791e56 47extern u_int session_id2_len;
34fee935 48extern Buffer loginmsg;
3c0ef626 49
ff2d7a98 50/* methods */
51
52extern Authmethod method_none;
ff2d7a98 53extern Authmethod method_pubkey;
54extern Authmethod method_passwd;
55extern Authmethod method_kbdint;
56extern Authmethod method_hostbased;
88928908 57#ifdef GSSAPI
58extern Authmethod method_external;
34fee935 59extern Authmethod method_gsskeyex;
88928908 60extern Authmethod method_gssapi;
416fd2a8 61extern Authmethod method_gssapi_compat;
88928908 62#endif
ff2d7a98 63
64Authmethod *authmethods[] = {
65 &method_none,
ff2d7a98 66 &method_pubkey,
70791e56 67#ifdef GSSAPI
34fee935 68 &method_gsskeyex,
416fd2a8 69 &method_external,
70791e56 70 &method_gssapi,
416fd2a8 71 &method_gssapi_compat,
70791e56 72#endif
ff2d7a98 73 &method_passwd,
74 &method_kbdint,
75 &method_hostbased,
76 NULL
3c0ef626 77};
78
79/* protocol */
80
1e608e42 81static void input_service_request(int, u_int32_t, void *);
82static void input_userauth_request(int, u_int32_t, void *);
3c0ef626 83
84/* helper */
85static Authmethod *authmethod_lookup(const char *);
86static char *authmethods_get(void);
2980ea68 87int user_key_allowed(struct passwd *, Key *);
3c0ef626 88
3c0ef626 89/*
90 * loop until authctxt->success == TRUE
91 */
92
416fd2a8 93void
94do_authentication2(Authctxt *authctxt)
3c0ef626 95{
3c0ef626 96 /* challenge-response is implemented via keyboard interactive */
97 if (options.challenge_response_authentication)
98 options.kbd_interactive_authentication = 1;
3c0ef626 99
1e608e42 100 dispatch_init(&dispatch_protocol_error);
3c0ef626 101 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
102 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
3c0ef626 103}
104
105static void
1e608e42 106input_service_request(int type, u_int32_t seq, void *ctxt)
3c0ef626 107{
108 Authctxt *authctxt = ctxt;
109 u_int len;
e54b3d7c 110 int acceptit = 0;
3c0ef626 111 char *service = packet_get_string(&len);
1e608e42 112 packet_check_eom();
3c0ef626 113
114 if (authctxt == NULL)
115 fatal("input_service_request: no authctxt");
116
117 if (strcmp(service, "ssh-userauth") == 0) {
118 if (!authctxt->success) {
e54b3d7c 119 acceptit = 1;
3c0ef626 120 /* now we can handle user-auth requests */
121 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
122 }
123 }
124 /* XXX all other service requests are denied */
125
e54b3d7c 126 if (acceptit) {
3c0ef626 127 packet_start(SSH2_MSG_SERVICE_ACCEPT);
128 packet_put_cstring(service);
129 packet_send();
130 packet_write_wait();
131 } else {
132 debug("bad service request %s", service);
133 packet_disconnect("bad service request %s", service);
134 }
135 xfree(service);
136}
137
138static void
1e608e42 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);
2980ea68 152
153#ifdef GSSAPI
1b56ff3d 154 if (user[0] == '\0') {
c2397a66 155 debug("received empty username for %s", method);
34fee935 156 if (strcmp(method, "external-keyx") == 0 ||
157 strcmp(method, "gssapi-keyex") == 0) {
c2397a66 158 char *lname = NULL;
159 PRIVSEP(ssh_gssapi_localname(&lname));
160 if (lname && lname[0] != '\0') {
161 xfree(user);
162 user = lname;
163 debug("set username to %s from gssapi context", user);
57877bbc 164 } else {
c2397a66 165 debug("failed to set username from gssapi context");
166 }
ff2d7a98 167 }
168 }
169#endif
2980ea68 170
c2397a66 171 debug("userauth-request for user %s service %s method %s",
1b56ff3d 172 user[0] ? user : "<implicit>", service, method);
3c0ef626 173 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
174
175 if ((style = strchr(user, ':')) != NULL)
176 *style++ = 0;
177
416fd2a8 178 /* If first time or username changed or implicit username,
179 setup/reset authentication context. */
180 if ((authctxt->attempt++ == 0) ||
181 (strcmp(user, authctxt->user) != 0) ||
182 (strcmp(user, "") == 0)) {
ff2d7a98 183 if (authctxt->user) {
184 xfree(authctxt->user);
185 authctxt->user = NULL;
186 }
34fee935 187 authctxt->valid = 0;
c2397a66 188#ifdef GSSAPI
57877bbc 189 /* If we're going to set the username based on the
190 GSSAPI context later, then wait until then to
1b56ff3d 191 verify it. Just put in placeholders for now. */
192 if ((strcmp(user, "") == 0) &&
193 ((strcmp(method, "gssapi") == 0) ||
194 (strcmp(method, "gssapi-with-mic") == 0))) {
195 authctxt->pw = fakepw();
196 authctxt->user = xstrdup(user);
197 } else {
c2397a66 198#endif
2980ea68 199 authctxt->pw = PRIVSEP(getpwnamallow(user));
2a304a95 200 authctxt->user = xstrdup(user);
2980ea68 201 if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
3c0ef626 202 authctxt->valid = 1;
203 debug2("input_userauth_request: setting up authctxt for %s", user);
204#ifdef USE_PAM
70791e56 205 if (options.use_pam)
2a304a95 206 PRIVSEP(start_pam(authctxt));
3c0ef626 207#endif
208 } else {
1b56ff3d 209 logit("input_userauth_request: invalid user %s", user);
70791e56 210 authctxt->pw = fakepw();
3c0ef626 211#ifdef USE_PAM
70791e56 212 if (options.use_pam)
2a304a95 213 PRIVSEP(start_pam(authctxt));
34fee935 214#endif
215#ifdef SSH_AUDIT_EVENTS
216 PRIVSEP(audit_event(SSH_INVALID_USER));
3c0ef626 217#endif
218 }
c2397a66 219#ifdef GSSAPI
57877bbc 220 } /* endif for setting username based on GSSAPI context */
c2397a66 221#endif
1b56ff3d 222 setproctitle("%s%s", authctxt->valid ? user : "unknown",
2980ea68 223 use_privsep ? " [net]" : "");
34fee935 224#ifdef GSSAPI
225 if (authctxt->attempt == 1) {
226#endif
227 authctxt->service = xstrdup(service);
228 authctxt->style = style ? xstrdup(style) : NULL;
229 if (use_privsep)
2980ea68 230 mm_inform_authserv(service, style);
34fee935 231#ifdef GSSAPI
232 } /* if (authctxt->attempt == 1) */
233#endif
1b56ff3d 234 }
235 if (strcmp(service, authctxt->service) != 0) {
88928908 236 packet_disconnect("Change of service not allowed: "
237 "(%s,%s) -> (%s,%s)",
238 authctxt->user, authctxt->service, user, service);
3c0ef626 239 }
240 /* reset state */
1e608e42 241 auth2_challenge_stop(authctxt);
5598e598 242
243#ifdef GSSAPI
1e608e42 244 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
245 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
5598e598 246#endif
247
3c0ef626 248 authctxt->postponed = 0;
34fee935 249 authctxt->server_caused_failure = 0;
3c0ef626 250
251 /* try to authenticate user */
252 m = authmethod_lookup(method);
253 if (m != NULL) {
254 debug2("input_userauth_request: try method %s", method);
255 authenticated = m->userauth(authctxt);
256 }
257 userauth_finish(authctxt, authenticated, method);
258
259 xfree(service);
260 xfree(user);
261 xfree(method);
262}
263
264void
265userauth_finish(Authctxt *authctxt, int authenticated, char *method)
266{
267 char *methods;
268
269 if (!authctxt->valid && authenticated)
270 fatal("INTERNAL ERROR: authenticated invalid user %s",
271 authctxt->user);
272
273 /* Special handling for root */
1c14df9e 274 if (authenticated && authctxt->pw->pw_uid == 0 &&
34fee935 275 !auth_root_allowed(method)) {
3c0ef626 276 authenticated = 0;
34fee935 277#ifdef SSH_AUDIT_EVENTS
278 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED));
279#endif
280 }
3c0ef626 281
282#ifdef USE_PAM
34fee935 283 if (options.use_pam && authenticated) {
284 if (!PRIVSEP(do_pam_account())) {
285 /* if PAM returned a message, send it to the user */
286 if (buffer_len(&loginmsg) > 0) {
287 buffer_append(&loginmsg, "\0", 1);
288 userauth_send_banner(buffer_ptr(&loginmsg));
289 packet_write_wait();
290 }
291 fatal("Access denied for user %s by PAM account "
292 "configuration", authctxt->user);
293 }
294 }
70791e56 295#endif
3c0ef626 296
e54b3d7c 297#ifdef _UNICOS
298 if (authenticated && cray_access_denied(authctxt->user)) {
299 authenticated = 0;
300 fatal("Access denied for user %s.",authctxt->user);
301 }
302#endif /* _UNICOS */
303
3c0ef626 304 /* Log before sending the reply */
305 auth_log(authctxt, authenticated, method, " ssh2");
306
307 if (authctxt->postponed)
308 return;
309
310 /* XXX todo: check if multiple auth methods are needed */
311 if (authenticated == 1) {
312 /* turn off userauth */
1e608e42 313 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
3c0ef626 314 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
315 packet_send();
316 packet_write_wait();
317 /* now we can break out */
318 authctxt->success = 1;
319 } else {
34fee935 320 /* Dont count server configuration issues against the client */
321 if (!authctxt->server_caused_failure &&
322 authctxt->failures++ > options.max_authtries) {
323#ifdef SSH_AUDIT_EVENTS
324 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES));
325#endif
3c0ef626 326 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
34fee935 327 }
3c0ef626 328 methods = authmethods_get();
329 packet_start(SSH2_MSG_USERAUTH_FAILURE);
330 packet_put_cstring(methods);
331 packet_put_char(0); /* XXX partial success, unused */
332 packet_send();
333 packet_write_wait();
334 xfree(methods);
335 }
336}
337
3c0ef626 338#define DELIM ","
339
340static char *
341authmethods_get(void)
342{
1e608e42 343 Buffer b;
3c0ef626 344 char *list;
ff2d7a98 345 int i;
3c0ef626 346
1e608e42 347 buffer_init(&b);
ff2d7a98 348 for (i = 0; authmethods[i] != NULL; i++) {
349 if (strcmp(authmethods[i]->name, "none") == 0)
3c0ef626 350 continue;
ff2d7a98 351 if (authmethods[i]->enabled != NULL &&
352 *(authmethods[i]->enabled) != 0) {
1e608e42 353 if (buffer_len(&b) > 0)
354 buffer_append(&b, ",", 1);
ff2d7a98 355 buffer_append(&b, authmethods[i]->name,
356 strlen(authmethods[i]->name));
3c0ef626 357 }
358 }
1e608e42 359 buffer_append(&b, "\0", 1);
360 list = xstrdup(buffer_ptr(&b));
361 buffer_free(&b);
3c0ef626 362 return list;
363}
364
365static Authmethod *
366authmethod_lookup(const char *name)
367{
ff2d7a98 368 int i;
369
3c0ef626 370 if (name != NULL)
ff2d7a98 371 for (i = 0; authmethods[i] != NULL; i++)
372 if (authmethods[i]->enabled != NULL &&
373 *(authmethods[i]->enabled) != 0 &&
374 strcmp(name, authmethods[i]->name) == 0)
375 return authmethods[i];
376 debug2("Unrecognized authentication method name: %s",
377 name ? name : "NULL");
3c0ef626 378 return NULL;
379}
This page took 0.122672 seconds and 5 git commands to generate.