Annotation of src/usr.bin/ssh/auth2.c, Revision 1.5
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: * 3. All advertising materials mentioning features or use of this software
13: * must display the following acknowledgement:
14: * This product includes software developed by Markus Friedl.
15: * 4. The name of the author may not be used to endorse or promote products
16: * derived from this software without specific prior written permission.
17: *
18: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28: */
29: #include "includes.h"
1.5 ! djm 30: RCSID("$OpenBSD: auth2.c,v 1.4 2000/05/01 07:45:08 markus Exp $");
1.1 markus 31:
32: #include <openssl/dsa.h>
33: #include <openssl/rsa.h>
34: #include <openssl/evp.h>
35:
36: #include "xmalloc.h"
37: #include "rsa.h"
38: #include "ssh.h"
39: #include "pty.h"
40: #include "packet.h"
41: #include "buffer.h"
42: #include "cipher.h"
43: #include "servconf.h"
44: #include "compat.h"
45: #include "channels.h"
46: #include "bufaux.h"
47: #include "ssh2.h"
48: #include "auth.h"
49: #include "session.h"
50: #include "dispatch.h"
51: #include "auth.h"
52: #include "key.h"
53: #include "kex.h"
54:
55: #include "dsa.h"
56: #include "uidswap.h"
57:
58: /* import */
59: extern ServerOptions options;
60: extern unsigned char *session_id2;
61: extern int session_id2_len;
62:
63: /* protocol */
64:
65: void input_service_request(int type, int plen);
66: void input_userauth_request(int type, int plen);
67: void protocol_error(int type, int plen);
68:
69: /* auth */
70: int ssh2_auth_none(struct passwd *pw);
71: int ssh2_auth_password(struct passwd *pw);
72: int ssh2_auth_pubkey(struct passwd *pw, unsigned char *raw, unsigned int rlen);
73:
74: /* helper */
75: struct passwd* auth_set_user(char *u, char *s);
76: int user_dsa_key_allowed(struct passwd *pw, Key *key);
77:
78: typedef struct Authctxt Authctxt;
79: struct Authctxt {
80: char *user;
81: char *service;
82: struct passwd pw;
83: int valid;
84: };
85: static Authctxt *authctxt = NULL;
86: static int userauth_success = 0;
87:
88: /*
89: * loop until userauth_success == TRUE
90: */
91:
92: void
93: do_authentication2()
94: {
1.4 markus 95: /* turn off skey/kerberos, not supported by SSH2 */
1.5 ! djm 96: #ifdef SKEY
1.4 markus 97: options.skey_authentication = 0;
1.5 ! djm 98: #endif
! 99: #ifdef KRB4
1.4 markus 100: options.kerberos_authentication = 0;
1.5 ! djm 101: #endif
1.4 markus 102:
1.1 markus 103: dispatch_init(&protocol_error);
104: dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
105: dispatch_run(DISPATCH_BLOCK, &userauth_success);
106: do_authenticated2();
107: }
108:
109: void
110: protocol_error(int type, int plen)
111: {
112: log("auth: protocol error: type %d plen %d", type, plen);
113: packet_start(SSH2_MSG_UNIMPLEMENTED);
114: packet_put_int(0);
115: packet_send();
116: packet_write_wait();
117: }
118:
119: void
120: input_service_request(int type, int plen)
121: {
122: unsigned int len;
123: int accept = 0;
124: char *service = packet_get_string(&len);
125: packet_done();
126:
127: if (strcmp(service, "ssh-userauth") == 0) {
128: if (!userauth_success) {
129: accept = 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 (accept) {
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: void
149: input_userauth_request(int type, int plen)
150: {
1.3 markus 151: static void (*authlog) (const char *fmt,...) = verbose;
152: static int attempt = 0;
1.1 markus 153: unsigned int len, rlen;
154: int authenticated = 0;
1.3 markus 155: char *raw, *user, *service, *method, *authmsg = NULL;
1.1 markus 156: struct passwd *pw;
157:
1.3 markus 158: if (++attempt == AUTH_FAIL_MAX)
1.1 markus 159: packet_disconnect("too many failed userauth_requests");
160:
161: raw = packet_get_raw(&rlen);
162: if (plen != rlen)
163: fatal("plen != rlen");
164: user = packet_get_string(&len);
165: service = packet_get_string(&len);
166: method = packet_get_string(&len);
167: debug("userauth-request for user %s service %s method %s", user, service, method);
168:
169: /* XXX we only allow the ssh-connection service */
170: pw = auth_set_user(user, service);
171: if (pw && strcmp(service, "ssh-connection")==0) {
172: if (strcmp(method, "none") == 0) {
173: authenticated = ssh2_auth_none(pw);
174: } else if (strcmp(method, "password") == 0) {
175: authenticated = ssh2_auth_password(pw);
176: } else if (strcmp(method, "publickey") == 0) {
177: authenticated = ssh2_auth_pubkey(pw, raw, rlen);
178: }
179: }
1.3 markus 180: if (authenticated && pw && pw->pw_uid == 0 && !options.permit_root_login) {
181: authenticated = 0;
182: log("ROOT LOGIN REFUSED FROM %.200s",
183: get_canonical_hostname());
184: }
185:
186: /* XXX todo: check if multiple auth methods are needed */
1.1 markus 187: if (authenticated == 1) {
1.3 markus 188: authmsg = "Accepted";
1.1 markus 189: /* turn off userauth */
190: dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
191: packet_start(SSH2_MSG_USERAUTH_SUCCESS);
192: packet_send();
193: packet_write_wait();
194: /* now we can break out */
195: userauth_success = 1;
196: } else if (authenticated == 0) {
1.3 markus 197: authmsg = "Failed";
1.1 markus 198: packet_start(SSH2_MSG_USERAUTH_FAILURE);
199: packet_put_cstring("publickey,password"); /* XXX dynamic */
200: packet_put_char(0); /* XXX partial success, unused */
201: packet_send();
202: packet_write_wait();
203: } else {
1.3 markus 204: authmsg = "Postponed";
1.1 markus 205: }
1.3 markus 206: /* Raise logging level */
207: if (authenticated == 1||
208: attempt == AUTH_FAIL_LOG ||
209: strcmp(method, "password") == 0)
210: authlog = log;
211:
212: authlog("%s %s for %.200s from %.200s port %d ssh2",
213: authmsg,
214: method,
215: pw && pw->pw_uid == 0 ? "ROOT" : user,
216: get_remote_ipaddr(),
217: get_remote_port());
218:
1.1 markus 219: xfree(service);
220: xfree(user);
221: xfree(method);
222: }
223:
224: int
225: ssh2_auth_none(struct passwd *pw)
226: {
227: packet_done();
228: return auth_password(pw, "");
229: }
230: int
231: ssh2_auth_password(struct passwd *pw)
232: {
233: char *password;
234: int authenticated = 0;
235: int change;
236: unsigned int len;
237: change = packet_get_char();
238: if (change)
239: log("password change not supported");
240: password = packet_get_string(&len);
241: packet_done();
1.3 markus 242: if (options.password_authentication &&
243: auth_password(pw, password) == 1)
1.1 markus 244: authenticated = 1;
245: memset(password, 0, len);
246: xfree(password);
247: return authenticated;
248: }
249: int
250: ssh2_auth_pubkey(struct passwd *pw, unsigned char *raw, unsigned int rlen)
251: {
252: Buffer b;
253: Key *key;
254: char *pkalg, *pkblob, *sig;
255: unsigned int alen, blen, slen;
256: int have_sig;
257: int authenticated = 0;
258:
1.3 markus 259: if (options.rsa_authentication == 0) {
260: debug("pubkey auth disabled");
261: return 0;
262: }
1.1 markus 263: have_sig = packet_get_char();
264: pkalg = packet_get_string(&alen);
265: if (strcmp(pkalg, KEX_DSS) != 0) {
266: xfree(pkalg);
267: log("bad pkalg %s", pkalg); /*XXX*/
268: return 0;
269: }
270: pkblob = packet_get_string(&blen);
271: key = dsa_key_from_blob(pkblob, blen);
1.2 markus 272: if (key != NULL) {
273: if (have_sig) {
274: sig = packet_get_string(&slen);
275: packet_done();
276: buffer_init(&b);
277: buffer_append(&b, session_id2, session_id2_len);
278: buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
279: if (slen + 4 > rlen)
280: fatal("bad rlen/slen");
281: buffer_append(&b, raw, rlen - slen - 4);
1.1 markus 282: #ifdef DEBUG_DSS
1.2 markus 283: buffer_dump(&b);
1.1 markus 284: #endif
1.2 markus 285: /* test for correct signature */
286: if (user_dsa_key_allowed(pw, key) &&
287: dsa_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
288: authenticated = 1;
289: buffer_clear(&b);
290: xfree(sig);
291: } else {
292: packet_done();
293: debug("test key...");
294: /* test whether pkalg/pkblob are acceptable */
295: /* XXX fake reply and always send PK_OK ? */
296: if (user_dsa_key_allowed(pw, key)) {
297: packet_start(SSH2_MSG_USERAUTH_PK_OK);
298: packet_put_string(pkalg, alen);
299: packet_put_string(pkblob, blen);
300: packet_send();
301: packet_write_wait();
302: authenticated = -1;
303: }
1.1 markus 304: }
1.2 markus 305: key_free(key);
1.1 markus 306: }
307: xfree(pkalg);
308: xfree(pkblob);
309: return authenticated;
310: }
311:
312: /* set and get current user */
313:
314: struct passwd*
315: auth_get_user(void)
316: {
317: return (authctxt != NULL && authctxt->valid) ? &authctxt->pw : NULL;
318: }
319:
320: struct passwd*
321: auth_set_user(char *u, char *s)
322: {
323: struct passwd *pw, *copy;
324:
325: if (authctxt == NULL) {
326: authctxt = xmalloc(sizeof(*authctxt));
327: authctxt->valid = 0;
328: authctxt->user = xstrdup(u);
329: authctxt->service = xstrdup(s);
330: setproctitle("%s", u);
331: pw = getpwnam(u);
332: if (!pw || !allowed_user(pw)) {
1.3 markus 333: log("auth_set_user: illegal user %s", u);
1.1 markus 334: return NULL;
335: }
336: copy = &authctxt->pw;
337: memset(copy, 0, sizeof(*copy));
338: copy->pw_name = xstrdup(pw->pw_name);
339: copy->pw_passwd = xstrdup(pw->pw_passwd);
340: copy->pw_uid = pw->pw_uid;
341: copy->pw_gid = pw->pw_gid;
342: copy->pw_dir = xstrdup(pw->pw_dir);
343: copy->pw_shell = xstrdup(pw->pw_shell);
344: authctxt->valid = 1;
345: } else {
346: if (strcmp(u, authctxt->user) != 0 ||
347: strcmp(s, authctxt->service) != 0) {
348: log("auth_set_user: missmatch: (%s,%s)!=(%s,%s)",
349: u, s, authctxt->user, authctxt->service);
350: return NULL;
351: }
352: }
353: return auth_get_user();
354: }
355:
356: /* return 1 if user allows given key */
357: int
358: user_dsa_key_allowed(struct passwd *pw, Key *key)
359: {
360: char line[8192], file[1024];
361: int found_key = 0;
362: unsigned int bits = -1;
363: FILE *f;
364: unsigned long linenum = 0;
365: struct stat st;
366: Key *found;
367:
368: /* Temporarily use the user's uid. */
369: temporarily_use_uid(pw->pw_uid);
370:
371: /* The authorized keys. */
372: snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
373: SSH_USER_PERMITTED_KEYS2);
374:
375: /* Fail quietly if file does not exist */
376: if (stat(file, &st) < 0) {
377: /* Restore the privileged uid. */
378: restore_uid();
379: return 0;
380: }
381: /* Open the file containing the authorized keys. */
382: f = fopen(file, "r");
383: if (!f) {
384: /* Restore the privileged uid. */
385: restore_uid();
386: return 0;
387: }
388: if (options.strict_modes) {
389: int fail = 0;
390: char buf[1024];
391: /* Check open file in order to avoid open/stat races */
392: if (fstat(fileno(f), &st) < 0 ||
393: (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
394: (st.st_mode & 022) != 0) {
395: snprintf(buf, sizeof buf, "DSA authentication refused for %.100s: "
396: "bad ownership or modes for '%s'.", pw->pw_name, file);
397: fail = 1;
398: } else {
399: /* Check path to SSH_USER_PERMITTED_KEYS */
400: int i;
401: static const char *check[] = {
402: "", SSH_USER_DIR, NULL
403: };
404: for (i = 0; check[i]; i++) {
405: snprintf(line, sizeof line, "%.500s/%.100s",
406: pw->pw_dir, check[i]);
407: if (stat(line, &st) < 0 ||
408: (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
409: (st.st_mode & 022) != 0) {
410: snprintf(buf, sizeof buf,
411: "DSA authentication refused for %.100s: "
412: "bad ownership or modes for '%s'.",
413: pw->pw_name, line);
414: fail = 1;
415: break;
416: }
417: }
418: }
419: if (fail) {
420: log(buf);
421: fclose(f);
422: restore_uid();
423: return 0;
424: }
425: }
426: found_key = 0;
427: found = key_new(KEY_DSA);
428:
429: while (fgets(line, sizeof(line), f)) {
430: char *cp;
431: linenum++;
432: /* Skip leading whitespace, empty and comment lines. */
433: for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
434: ;
435: if (!*cp || *cp == '\n' || *cp == '#')
436: continue;
437: bits = key_read(found, &cp);
438: if (bits == 0)
439: continue;
440: if (key_equal(found, key)) {
441: found_key = 1;
442: debug("matching key found: file %s, line %ld",
443: file, linenum);
444: break;
445: }
446: }
447: restore_uid();
448: fclose(f);
449: key_free(found);
450: return found_key;
451: }