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