Annotation of src/usr.bin/ssh/auth2.c, Revision 1.118
1.118 ! djm 1: /* $OpenBSD: auth2.c,v 1.117 2008/07/02 12:36:39 djm Exp $ */
1.1 markus 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: */
1.14 deraadt 25:
1.111 stevesk 26:
27: #include <sys/types.h>
1.117 djm 28: #include <sys/stat.h>
29: #include <sys/uio.h>
1.111 stevesk 30:
1.117 djm 31: #include <fcntl.h>
1.111 stevesk 32: #include <pwd.h>
1.112 stevesk 33: #include <string.h>
1.113 deraadt 34: #include <stdarg.h>
1.117 djm 35: #include <unistd.h>
1.1 markus 36:
1.117 djm 37: #include "atomicio.h"
1.113 deraadt 38: #include "xmalloc.h"
1.32 markus 39: #include "ssh2.h"
1.1 markus 40: #include "packet.h"
1.32 markus 41: #include "log.h"
1.113 deraadt 42: #include "buffer.h"
1.1 markus 43: #include "servconf.h"
44: #include "compat.h"
1.113 deraadt 45: #include "key.h"
46: #include "hostfile.h"
1.1 markus 47: #include "auth.h"
48: #include "dispatch.h"
1.29 markus 49: #include "pathnames.h"
1.100 markus 50: #ifdef GSSAPI
51: #include "ssh-gss.h"
52: #endif
1.113 deraadt 53: #include "monitor_wrap.h"
1.100 markus 54:
1.1 markus 55: /* import */
56: extern ServerOptions options;
1.23 markus 57: extern u_char *session_id2;
1.99 markus 58: extern u_int session_id2_len;
1.1 markus 59:
1.93 markus 60: /* methods */
61:
62: extern Authmethod method_none;
63: extern Authmethod method_pubkey;
64: extern Authmethod method_passwd;
65: extern Authmethod method_kbdint;
66: extern Authmethod method_hostbased;
1.100 markus 67: #ifdef GSSAPI
68: extern Authmethod method_gssapi;
69: #endif
1.93 markus 70:
71: Authmethod *authmethods[] = {
72: &method_none,
73: &method_pubkey,
1.100 markus 74: #ifdef GSSAPI
75: &method_gssapi,
76: #endif
1.93 markus 77: &method_passwd,
78: &method_kbdint,
79: &method_hostbased,
80: NULL
1.18 markus 81: };
82:
1.1 markus 83: /* protocol */
84:
1.80 markus 85: static void input_service_request(int, u_int32_t, void *);
86: static void input_userauth_request(int, u_int32_t, void *);
1.1 markus 87:
88: /* helper */
1.66 itojun 89: static Authmethod *authmethod_lookup(const char *);
1.67 stevesk 90: static char *authmethods_get(void);
1.1 markus 91:
1.117 djm 92: char *
93: auth2_read_banner(void)
94: {
95: struct stat st;
96: char *banner = NULL;
97: size_t len, n;
98: int fd;
99:
100: if ((fd = open(options.banner, O_RDONLY)) == -1)
101: return (NULL);
102: if (fstat(fd, &st) == -1) {
103: close(fd);
104: return (NULL);
105: }
106: if (st.st_size > 1*1024*1024) {
107: close(fd);
108: return (NULL);
109: }
110:
111: len = (size_t)st.st_size; /* truncate */
112: banner = xmalloc(len + 1);
113: n = atomicio(read, fd, banner, len);
114: close(fd);
115:
116: if (n != len) {
117: xfree(banner);
118: return (NULL);
119: }
120: banner[n] = '\0';
121:
122: return (banner);
123: }
124:
125: static void
126: userauth_banner(void)
127: {
128: char *banner = NULL;
129:
130: if (options.banner == NULL ||
131: strcasecmp(options.banner, "none") == 0 ||
132: (datafellows & SSH_BUG_BANNER) != 0)
133: return;
134:
135: if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
136: goto done;
137:
138: packet_start(SSH2_MSG_USERAUTH_BANNER);
139: packet_put_cstring(banner);
140: packet_put_cstring(""); /* language, unused */
141: packet_send();
142: debug("userauth_banner: sent");
143: done:
144: if (banner)
145: xfree(banner);
146: }
147:
1.1 markus 148: /*
1.18 markus 149: * loop until authctxt->success == TRUE
1.1 markus 150: */
1.103 markus 151: void
152: do_authentication2(Authctxt *authctxt)
1.1 markus 153: {
1.81 markus 154: dispatch_init(&dispatch_protocol_error);
1.1 markus 155: dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
1.18 markus 156: dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
1.1 markus 157: }
158:
1.109 deraadt 159: /*ARGSUSED*/
1.66 itojun 160: static void
1.80 markus 161: input_service_request(int type, u_int32_t seq, void *ctxt)
1.1 markus 162: {
1.18 markus 163: Authctxt *authctxt = ctxt;
1.23 markus 164: u_int len;
1.94 deraadt 165: int acceptit = 0;
1.1 markus 166: char *service = packet_get_string(&len);
1.79 markus 167: packet_check_eom();
1.1 markus 168:
1.18 markus 169: if (authctxt == NULL)
170: fatal("input_service_request: no authctxt");
171:
1.1 markus 172: if (strcmp(service, "ssh-userauth") == 0) {
1.18 markus 173: if (!authctxt->success) {
1.94 deraadt 174: acceptit = 1;
1.1 markus 175: /* now we can handle user-auth requests */
176: dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
177: }
178: }
179: /* XXX all other service requests are denied */
180:
1.94 deraadt 181: if (acceptit) {
1.1 markus 182: packet_start(SSH2_MSG_SERVICE_ACCEPT);
183: packet_put_cstring(service);
184: packet_send();
185: packet_write_wait();
186: } else {
187: debug("bad service request %s", service);
188: packet_disconnect("bad service request %s", service);
189: }
190: xfree(service);
191: }
192:
1.109 deraadt 193: /*ARGSUSED*/
1.66 itojun 194: static void
1.80 markus 195: input_userauth_request(int type, u_int32_t seq, void *ctxt)
1.1 markus 196: {
1.18 markus 197: Authctxt *authctxt = ctxt;
198: Authmethod *m = NULL;
1.28 markus 199: char *user, *service, *method, *style = NULL;
1.1 markus 200: int authenticated = 0;
201:
1.18 markus 202: if (authctxt == NULL)
203: fatal("input_userauth_request: no authctxt");
1.1 markus 204:
1.18 markus 205: user = packet_get_string(NULL);
206: service = packet_get_string(NULL);
207: method = packet_get_string(NULL);
1.1 markus 208: debug("userauth-request for user %s service %s method %s", user, service, method);
1.24 markus 209: debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
1.1 markus 210:
1.28 markus 211: if ((style = strchr(user, ':')) != NULL)
212: *style++ = 0;
213:
1.36 stevesk 214: if (authctxt->attempt++ == 0) {
1.18 markus 215: /* setup auth context */
1.89 markus 216: authctxt->pw = PRIVSEP(getpwnamallow(user));
217: if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
1.18 markus 218: authctxt->valid = 1;
219: debug2("input_userauth_request: setting up authctxt for %s", user);
220: } else {
1.107 markus 221: logit("input_userauth_request: invalid user %s", user);
1.102 markus 222: authctxt->pw = fakepw();
1.18 markus 223: }
1.106 djm 224: setproctitle("%s%s", authctxt->valid ? user : "unknown",
1.88 provos 225: use_privsep ? " [net]" : "");
1.18 markus 226: authctxt->user = xstrdup(user);
227: authctxt->service = xstrdup(service);
1.62 markus 228: authctxt->style = style ? xstrdup(style) : NULL;
1.88 provos 229: if (use_privsep)
230: mm_inform_authserv(service, style);
1.117 djm 231: userauth_banner();
1.62 markus 232: } else if (strcmp(user, authctxt->user) != 0 ||
233: strcmp(service, authctxt->service) != 0) {
234: packet_disconnect("Change of username or service not allowed: "
235: "(%s,%s) -> (%s,%s)",
236: authctxt->user, authctxt->service, user, service);
1.1 markus 237: }
1.28 markus 238: /* reset state */
1.75 markus 239: auth2_challenge_stop(authctxt);
1.100 markus 240:
241: #ifdef GSSAPI
242: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
243: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
244: #endif
245:
1.28 markus 246: authctxt->postponed = 0;
1.18 markus 247:
1.28 markus 248: /* try to authenticate user */
1.18 markus 249: m = authmethod_lookup(method);
1.117 djm 250: if (m != NULL && authctxt->failures < options.max_authtries) {
1.18 markus 251: debug2("input_userauth_request: try method %s", method);
252: authenticated = m->userauth(authctxt);
253: }
1.49 markus 254: userauth_finish(authctxt, authenticated, method);
255:
256: xfree(service);
257: xfree(user);
258: xfree(method);
259: }
260:
261: void
262: userauth_finish(Authctxt *authctxt, int authenticated, char *method)
263: {
1.60 markus 264: char *methods;
265:
1.28 markus 266: if (!authctxt->valid && authenticated)
267: fatal("INTERNAL ERROR: authenticated invalid user %s",
268: authctxt->user);
1.18 markus 269:
270: /* Special handling for root */
1.96 markus 271: if (authenticated && authctxt->pw->pw_uid == 0 &&
1.41 markus 272: !auth_root_allowed(method))
1.3 markus 273: authenticated = 0;
274:
1.18 markus 275: /* Log before sending the reply */
1.28 markus 276: auth_log(authctxt, authenticated, method, " ssh2");
277:
1.60 markus 278: if (authctxt->postponed)
279: return;
280:
281: /* XXX todo: check if multiple auth methods are needed */
282: if (authenticated == 1) {
283: /* turn off userauth */
1.81 markus 284: dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
1.60 markus 285: packet_start(SSH2_MSG_USERAUTH_SUCCESS);
286: packet_send();
287: packet_write_wait();
288: /* now we can break out */
289: authctxt->success = 1;
290: } else {
1.118 ! djm 291: if (++authctxt->failures >= options.max_authtries)
1.60 markus 292: packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
293: methods = authmethods_get();
294: packet_start(SSH2_MSG_USERAUTH_FAILURE);
295: packet_put_cstring(methods);
296: packet_put_char(0); /* XXX partial success, unused */
297: packet_send();
298: packet_write_wait();
299: xfree(methods);
300: }
1.1 markus 301: }
1.18 markus 302:
1.67 stevesk 303: static char *
1.18 markus 304: authmethods_get(void)
1.1 markus 305: {
1.82 markus 306: Buffer b;
1.18 markus 307: char *list;
1.93 markus 308: int i;
1.1 markus 309:
1.82 markus 310: buffer_init(&b);
1.93 markus 311: for (i = 0; authmethods[i] != NULL; i++) {
312: if (strcmp(authmethods[i]->name, "none") == 0)
1.18 markus 313: continue;
1.93 markus 314: if (authmethods[i]->enabled != NULL &&
315: *(authmethods[i]->enabled) != 0) {
1.82 markus 316: if (buffer_len(&b) > 0)
317: buffer_append(&b, ",", 1);
1.93 markus 318: buffer_append(&b, authmethods[i]->name,
319: strlen(authmethods[i]->name));
1.1 markus 320: }
321: }
1.82 markus 322: buffer_append(&b, "\0", 1);
323: list = xstrdup(buffer_ptr(&b));
324: buffer_free(&b);
1.18 markus 325: return list;
326: }
327:
1.66 itojun 328: static Authmethod *
1.18 markus 329: authmethod_lookup(const char *name)
330: {
1.93 markus 331: int i;
332:
1.18 markus 333: if (name != NULL)
1.93 markus 334: for (i = 0; authmethods[i] != NULL; i++)
335: if (authmethods[i]->enabled != NULL &&
336: *(authmethods[i]->enabled) != 0 &&
337: strcmp(name, authmethods[i]->name) == 0)
338: return authmethods[i];
339: debug2("Unrecognized authentication method name: %s",
340: name ? name : "NULL");
1.18 markus 341: return NULL;
1.1 markus 342: }
1.117 djm 343: