Annotation of src/usr.bin/ssh/auth2.c, Revision 1.102
1.1 markus 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: */
1.14 deraadt 24:
1.1 markus 25: #include "includes.h"
1.102 ! markus 26: RCSID("$OpenBSD: auth2.c,v 1.101 2003/08/22 13:22:27 markus Exp $");
1.1 markus 27:
1.32 markus 28: #include "ssh2.h"
1.1 markus 29: #include "xmalloc.h"
30: #include "packet.h"
1.32 markus 31: #include "log.h"
1.1 markus 32: #include "servconf.h"
33: #include "compat.h"
34: #include "auth.h"
35: #include "dispatch.h"
1.29 markus 36: #include "pathnames.h"
1.88 provos 37: #include "monitor_wrap.h"
1.1 markus 38:
1.100 markus 39: #ifdef GSSAPI
40: #include "ssh-gss.h"
41: #endif
42:
1.1 markus 43: /* import */
44: extern ServerOptions options;
1.23 markus 45: extern u_char *session_id2;
1.99 markus 46: extern u_int session_id2_len;
1.1 markus 47:
1.88 provos 48: Authctxt *x_authctxt = NULL;
1.18 markus 49:
1.93 markus 50: /* methods */
51:
52: extern Authmethod method_none;
53: extern Authmethod method_pubkey;
54: extern Authmethod method_passwd;
55: extern Authmethod method_kbdint;
56: extern Authmethod method_hostbased;
1.100 markus 57: #ifdef GSSAPI
58: extern Authmethod method_gssapi;
59: #endif
1.93 markus 60:
61: Authmethod *authmethods[] = {
62: &method_none,
63: &method_pubkey,
1.100 markus 64: #ifdef GSSAPI
65: &method_gssapi,
66: #endif
1.93 markus 67: &method_passwd,
68: &method_kbdint,
69: &method_hostbased,
70: NULL
1.18 markus 71: };
72:
1.1 markus 73: /* protocol */
74:
1.80 markus 75: static void input_service_request(int, u_int32_t, void *);
76: static void input_userauth_request(int, u_int32_t, void *);
1.1 markus 77:
78: /* helper */
1.66 itojun 79: static Authmethod *authmethod_lookup(const char *);
1.67 stevesk 80: static char *authmethods_get(void);
1.88 provos 81: int user_key_allowed(struct passwd *, Key *);
82: int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
1.1 markus 83:
84: /*
1.18 markus 85: * loop until authctxt->success == TRUE
1.1 markus 86: */
87:
1.87 provos 88: Authctxt *
1.74 itojun 89: do_authentication2(void)
1.1 markus 90: {
1.28 markus 91: Authctxt *authctxt = authctxt_new();
92:
1.18 markus 93: x_authctxt = authctxt; /*XXX*/
94:
1.71 markus 95: /* challenge-response is implemented via keyboard interactive */
1.57 markus 96: if (options.challenge_response_authentication)
1.34 markus 97: options.kbd_interactive_authentication = 1;
98:
1.81 markus 99: dispatch_init(&dispatch_protocol_error);
1.1 markus 100: dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
1.18 markus 101: dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
1.87 provos 102:
103: return (authctxt);
1.1 markus 104: }
105:
1.66 itojun 106: static void
1.80 markus 107: input_service_request(int type, u_int32_t seq, void *ctxt)
1.1 markus 108: {
1.18 markus 109: Authctxt *authctxt = ctxt;
1.23 markus 110: u_int len;
1.94 deraadt 111: int acceptit = 0;
1.1 markus 112: char *service = packet_get_string(&len);
1.79 markus 113: packet_check_eom();
1.1 markus 114:
1.18 markus 115: if (authctxt == NULL)
116: fatal("input_service_request: no authctxt");
117:
1.1 markus 118: if (strcmp(service, "ssh-userauth") == 0) {
1.18 markus 119: if (!authctxt->success) {
1.94 deraadt 120: acceptit = 1;
1.1 markus 121: /* now we can handle user-auth requests */
122: dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
123: }
124: }
125: /* XXX all other service requests are denied */
126:
1.94 deraadt 127: if (acceptit) {
1.1 markus 128: packet_start(SSH2_MSG_SERVICE_ACCEPT);
129: packet_put_cstring(service);
130: packet_send();
131: packet_write_wait();
132: } else {
133: debug("bad service request %s", service);
134: packet_disconnect("bad service request %s", service);
135: }
136: xfree(service);
137: }
138:
1.66 itojun 139: static void
1.80 markus 140: input_userauth_request(int type, u_int32_t seq, void *ctxt)
1.1 markus 141: {
1.18 markus 142: Authctxt *authctxt = ctxt;
143: Authmethod *m = NULL;
1.28 markus 144: char *user, *service, *method, *style = NULL;
1.1 markus 145: int authenticated = 0;
146:
1.18 markus 147: if (authctxt == NULL)
148: fatal("input_userauth_request: no authctxt");
1.1 markus 149:
1.18 markus 150: user = packet_get_string(NULL);
151: service = packet_get_string(NULL);
152: method = packet_get_string(NULL);
1.1 markus 153: debug("userauth-request for user %s service %s method %s", user, service, method);
1.24 markus 154: debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
1.1 markus 155:
1.28 markus 156: if ((style = strchr(user, ':')) != NULL)
157: *style++ = 0;
158:
1.36 stevesk 159: if (authctxt->attempt++ == 0) {
1.18 markus 160: /* setup auth context */
1.89 markus 161: authctxt->pw = PRIVSEP(getpwnamallow(user));
162: if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
1.18 markus 163: authctxt->valid = 1;
164: debug2("input_userauth_request: setting up authctxt for %s", user);
165: } else {
1.97 itojun 166: logit("input_userauth_request: illegal user %s", user);
1.102 ! markus 167: authctxt->pw = fakepw();
1.18 markus 168: }
1.89 markus 169: setproctitle("%s%s", authctxt->pw ? user : "unknown",
1.88 provos 170: use_privsep ? " [net]" : "");
1.18 markus 171: authctxt->user = xstrdup(user);
172: authctxt->service = xstrdup(service);
1.62 markus 173: authctxt->style = style ? xstrdup(style) : NULL;
1.88 provos 174: if (use_privsep)
175: mm_inform_authserv(service, style);
1.62 markus 176: } else if (strcmp(user, authctxt->user) != 0 ||
177: strcmp(service, authctxt->service) != 0) {
178: packet_disconnect("Change of username or service not allowed: "
179: "(%s,%s) -> (%s,%s)",
180: authctxt->user, authctxt->service, user, service);
1.1 markus 181: }
1.28 markus 182: /* reset state */
1.75 markus 183: auth2_challenge_stop(authctxt);
1.100 markus 184:
185: #ifdef GSSAPI
186: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
187: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
188: #endif
189:
1.28 markus 190: authctxt->postponed = 0;
1.18 markus 191:
1.28 markus 192: /* try to authenticate user */
1.18 markus 193: m = authmethod_lookup(method);
194: if (m != NULL) {
195: debug2("input_userauth_request: try method %s", method);
196: authenticated = m->userauth(authctxt);
197: }
1.49 markus 198: userauth_finish(authctxt, authenticated, method);
199:
200: xfree(service);
201: xfree(user);
202: xfree(method);
203: }
204:
205: void
206: userauth_finish(Authctxt *authctxt, int authenticated, char *method)
207: {
1.60 markus 208: char *methods;
209:
1.28 markus 210: if (!authctxt->valid && authenticated)
211: fatal("INTERNAL ERROR: authenticated invalid user %s",
212: authctxt->user);
1.18 markus 213:
214: /* Special handling for root */
1.96 markus 215: if (authenticated && authctxt->pw->pw_uid == 0 &&
1.41 markus 216: !auth_root_allowed(method))
1.3 markus 217: authenticated = 0;
218:
1.18 markus 219: /* Log before sending the reply */
1.28 markus 220: auth_log(authctxt, authenticated, method, " ssh2");
221:
1.60 markus 222: if (authctxt->postponed)
223: return;
224:
225: /* XXX todo: check if multiple auth methods are needed */
226: if (authenticated == 1) {
227: /* turn off userauth */
1.81 markus 228: dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
1.60 markus 229: packet_start(SSH2_MSG_USERAUTH_SUCCESS);
230: packet_send();
231: packet_write_wait();
232: /* now we can break out */
233: authctxt->success = 1;
234: } else {
235: if (authctxt->failures++ > AUTH_FAIL_MAX)
236: packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
237: methods = authmethods_get();
238: packet_start(SSH2_MSG_USERAUTH_FAILURE);
239: packet_put_cstring(methods);
240: packet_put_char(0); /* XXX partial success, unused */
241: packet_send();
242: packet_write_wait();
243: xfree(methods);
244: }
1.18 markus 245: }
246:
247: /* get current user */
1.1 markus 248:
249: struct passwd*
250: auth_get_user(void)
251: {
1.18 markus 252: return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
1.1 markus 253: }
254:
1.18 markus 255: #define DELIM ","
256:
1.67 stevesk 257: static char *
1.18 markus 258: authmethods_get(void)
1.1 markus 259: {
1.82 markus 260: Buffer b;
1.18 markus 261: char *list;
1.93 markus 262: int i;
1.1 markus 263:
1.82 markus 264: buffer_init(&b);
1.93 markus 265: for (i = 0; authmethods[i] != NULL; i++) {
266: if (strcmp(authmethods[i]->name, "none") == 0)
1.18 markus 267: continue;
1.93 markus 268: if (authmethods[i]->enabled != NULL &&
269: *(authmethods[i]->enabled) != 0) {
1.82 markus 270: if (buffer_len(&b) > 0)
271: buffer_append(&b, ",", 1);
1.93 markus 272: buffer_append(&b, authmethods[i]->name,
273: strlen(authmethods[i]->name));
1.1 markus 274: }
275: }
1.82 markus 276: buffer_append(&b, "\0", 1);
277: list = xstrdup(buffer_ptr(&b));
278: buffer_free(&b);
1.18 markus 279: return list;
280: }
281:
1.66 itojun 282: static Authmethod *
1.18 markus 283: authmethod_lookup(const char *name)
284: {
1.93 markus 285: int i;
286:
1.18 markus 287: if (name != NULL)
1.93 markus 288: for (i = 0; authmethods[i] != NULL; i++)
289: if (authmethods[i]->enabled != NULL &&
290: *(authmethods[i]->enabled) != 0 &&
291: strcmp(name, authmethods[i]->name) == 0)
292: return authmethods[i];
293: debug2("Unrecognized authentication method name: %s",
294: name ? name : "NULL");
1.18 markus 295: return NULL;
1.1 markus 296: }