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