Annotation of src/usr.bin/ssh/auth2.c, Revision 1.3
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.3 ! markus 30: RCSID("$OpenBSD: auth2.c,v 1.2 2000/04/27 08:01:25 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: {
95: dispatch_init(&protocol_error);
96: dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
97: dispatch_run(DISPATCH_BLOCK, &userauth_success);
98: do_authenticated2();
99: }
100:
101: void
102: protocol_error(int type, int plen)
103: {
104: log("auth: protocol error: type %d plen %d", type, plen);
105: packet_start(SSH2_MSG_UNIMPLEMENTED);
106: packet_put_int(0);
107: packet_send();
108: packet_write_wait();
109: }
110:
111: void
112: input_service_request(int type, int plen)
113: {
114: unsigned int len;
115: int accept = 0;
116: char *service = packet_get_string(&len);
117: packet_done();
118:
119: if (strcmp(service, "ssh-userauth") == 0) {
120: if (!userauth_success) {
121: accept = 1;
122: /* now we can handle user-auth requests */
123: dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
124: }
125: }
126: /* XXX all other service requests are denied */
127:
128: if (accept) {
129: packet_start(SSH2_MSG_SERVICE_ACCEPT);
130: packet_put_cstring(service);
131: packet_send();
132: packet_write_wait();
133: } else {
134: debug("bad service request %s", service);
135: packet_disconnect("bad service request %s", service);
136: }
137: xfree(service);
138: }
139:
140: void
141: input_userauth_request(int type, int plen)
142: {
1.3 ! markus 143: static void (*authlog) (const char *fmt,...) = verbose;
! 144: static int attempt = 0;
1.1 markus 145: unsigned int len, rlen;
146: int authenticated = 0;
1.3 ! markus 147: char *raw, *user, *service, *method, *authmsg = NULL;
1.1 markus 148: struct passwd *pw;
149:
1.3 ! markus 150: if (++attempt == AUTH_FAIL_MAX)
1.1 markus 151: packet_disconnect("too many failed userauth_requests");
152:
153: raw = packet_get_raw(&rlen);
154: if (plen != rlen)
155: fatal("plen != rlen");
156: user = packet_get_string(&len);
157: service = packet_get_string(&len);
158: method = packet_get_string(&len);
159: debug("userauth-request for user %s service %s method %s", user, service, method);
160:
161: /* XXX we only allow the ssh-connection service */
162: pw = auth_set_user(user, service);
163: if (pw && strcmp(service, "ssh-connection")==0) {
164: if (strcmp(method, "none") == 0) {
165: authenticated = ssh2_auth_none(pw);
166: } else if (strcmp(method, "password") == 0) {
167: authenticated = ssh2_auth_password(pw);
168: } else if (strcmp(method, "publickey") == 0) {
169: authenticated = ssh2_auth_pubkey(pw, raw, rlen);
170: }
171: }
1.3 ! markus 172: if (authenticated && pw && pw->pw_uid == 0 && !options.permit_root_login) {
! 173: authenticated = 0;
! 174: log("ROOT LOGIN REFUSED FROM %.200s",
! 175: get_canonical_hostname());
! 176: }
! 177:
! 178: /* XXX todo: check if multiple auth methods are needed */
1.1 markus 179: if (authenticated == 1) {
1.3 ! markus 180: authmsg = "Accepted";
1.1 markus 181: /* turn off userauth */
182: dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
183: packet_start(SSH2_MSG_USERAUTH_SUCCESS);
184: packet_send();
185: packet_write_wait();
186: /* now we can break out */
187: userauth_success = 1;
188: } else if (authenticated == 0) {
1.3 ! markus 189: authmsg = "Failed";
1.1 markus 190: packet_start(SSH2_MSG_USERAUTH_FAILURE);
191: packet_put_cstring("publickey,password"); /* XXX dynamic */
192: packet_put_char(0); /* XXX partial success, unused */
193: packet_send();
194: packet_write_wait();
195: } else {
1.3 ! markus 196: authmsg = "Postponed";
1.1 markus 197: }
1.3 ! markus 198: /* Raise logging level */
! 199: if (authenticated == 1||
! 200: attempt == AUTH_FAIL_LOG ||
! 201: strcmp(method, "password") == 0)
! 202: authlog = log;
! 203:
! 204: authlog("%s %s for %.200s from %.200s port %d ssh2",
! 205: authmsg,
! 206: method,
! 207: pw && pw->pw_uid == 0 ? "ROOT" : user,
! 208: get_remote_ipaddr(),
! 209: get_remote_port());
! 210:
1.1 markus 211: xfree(service);
212: xfree(user);
213: xfree(method);
214: }
215:
216: int
217: ssh2_auth_none(struct passwd *pw)
218: {
219: packet_done();
220: return auth_password(pw, "");
221: }
222: int
223: ssh2_auth_password(struct passwd *pw)
224: {
225: char *password;
226: int authenticated = 0;
227: int change;
228: unsigned int len;
229: change = packet_get_char();
230: if (change)
231: log("password change not supported");
232: password = packet_get_string(&len);
233: packet_done();
1.3 ! markus 234: if (options.password_authentication &&
! 235: auth_password(pw, password) == 1)
1.1 markus 236: authenticated = 1;
237: memset(password, 0, len);
238: xfree(password);
239: return authenticated;
240: }
241: int
242: ssh2_auth_pubkey(struct passwd *pw, unsigned char *raw, unsigned int rlen)
243: {
244: Buffer b;
245: Key *key;
246: char *pkalg, *pkblob, *sig;
247: unsigned int alen, blen, slen;
248: int have_sig;
249: int authenticated = 0;
250:
1.3 ! markus 251: if (options.rsa_authentication == 0) {
! 252: debug("pubkey auth disabled");
! 253: return 0;
! 254: }
1.1 markus 255: have_sig = packet_get_char();
256: pkalg = packet_get_string(&alen);
257: if (strcmp(pkalg, KEX_DSS) != 0) {
258: xfree(pkalg);
259: log("bad pkalg %s", pkalg); /*XXX*/
260: return 0;
261: }
262: pkblob = packet_get_string(&blen);
263: key = dsa_key_from_blob(pkblob, blen);
1.2 markus 264: if (key != NULL) {
265: if (have_sig) {
266: sig = packet_get_string(&slen);
267: packet_done();
268: buffer_init(&b);
269: buffer_append(&b, session_id2, session_id2_len);
270: buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
271: if (slen + 4 > rlen)
272: fatal("bad rlen/slen");
273: buffer_append(&b, raw, rlen - slen - 4);
1.1 markus 274: #ifdef DEBUG_DSS
1.2 markus 275: buffer_dump(&b);
1.1 markus 276: #endif
1.2 markus 277: /* test for correct signature */
278: if (user_dsa_key_allowed(pw, key) &&
279: dsa_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
280: authenticated = 1;
281: buffer_clear(&b);
282: xfree(sig);
283: } else {
284: packet_done();
285: debug("test key...");
286: /* test whether pkalg/pkblob are acceptable */
287: /* XXX fake reply and always send PK_OK ? */
288: if (user_dsa_key_allowed(pw, key)) {
289: packet_start(SSH2_MSG_USERAUTH_PK_OK);
290: packet_put_string(pkalg, alen);
291: packet_put_string(pkblob, blen);
292: packet_send();
293: packet_write_wait();
294: authenticated = -1;
295: }
1.1 markus 296: }
1.2 markus 297: key_free(key);
1.1 markus 298: }
299: xfree(pkalg);
300: xfree(pkblob);
301: return authenticated;
302: }
303:
304: /* set and get current user */
305:
306: struct passwd*
307: auth_get_user(void)
308: {
309: return (authctxt != NULL && authctxt->valid) ? &authctxt->pw : NULL;
310: }
311:
312: struct passwd*
313: auth_set_user(char *u, char *s)
314: {
315: struct passwd *pw, *copy;
316:
317: if (authctxt == NULL) {
318: authctxt = xmalloc(sizeof(*authctxt));
319: authctxt->valid = 0;
320: authctxt->user = xstrdup(u);
321: authctxt->service = xstrdup(s);
322: setproctitle("%s", u);
323: pw = getpwnam(u);
324: if (!pw || !allowed_user(pw)) {
1.3 ! markus 325: log("auth_set_user: illegal user %s", u);
1.1 markus 326: return NULL;
327: }
328: copy = &authctxt->pw;
329: memset(copy, 0, sizeof(*copy));
330: copy->pw_name = xstrdup(pw->pw_name);
331: copy->pw_passwd = xstrdup(pw->pw_passwd);
332: copy->pw_uid = pw->pw_uid;
333: copy->pw_gid = pw->pw_gid;
334: copy->pw_dir = xstrdup(pw->pw_dir);
335: copy->pw_shell = xstrdup(pw->pw_shell);
336: authctxt->valid = 1;
337: } else {
338: if (strcmp(u, authctxt->user) != 0 ||
339: strcmp(s, authctxt->service) != 0) {
340: log("auth_set_user: missmatch: (%s,%s)!=(%s,%s)",
341: u, s, authctxt->user, authctxt->service);
342: return NULL;
343: }
344: }
345: return auth_get_user();
346: }
347:
348: /* return 1 if user allows given key */
349: int
350: user_dsa_key_allowed(struct passwd *pw, Key *key)
351: {
352: char line[8192], file[1024];
353: int found_key = 0;
354: unsigned int bits = -1;
355: FILE *f;
356: unsigned long linenum = 0;
357: struct stat st;
358: Key *found;
359:
360: /* Temporarily use the user's uid. */
361: temporarily_use_uid(pw->pw_uid);
362:
363: /* The authorized keys. */
364: snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
365: SSH_USER_PERMITTED_KEYS2);
366:
367: /* Fail quietly if file does not exist */
368: if (stat(file, &st) < 0) {
369: /* Restore the privileged uid. */
370: restore_uid();
371: return 0;
372: }
373: /* Open the file containing the authorized keys. */
374: f = fopen(file, "r");
375: if (!f) {
376: /* Restore the privileged uid. */
377: restore_uid();
378: return 0;
379: }
380: if (options.strict_modes) {
381: int fail = 0;
382: char buf[1024];
383: /* Check open file in order to avoid open/stat races */
384: if (fstat(fileno(f), &st) < 0 ||
385: (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
386: (st.st_mode & 022) != 0) {
387: snprintf(buf, sizeof buf, "DSA authentication refused for %.100s: "
388: "bad ownership or modes for '%s'.", pw->pw_name, file);
389: fail = 1;
390: } else {
391: /* Check path to SSH_USER_PERMITTED_KEYS */
392: int i;
393: static const char *check[] = {
394: "", SSH_USER_DIR, NULL
395: };
396: for (i = 0; check[i]; i++) {
397: snprintf(line, sizeof line, "%.500s/%.100s",
398: pw->pw_dir, check[i]);
399: if (stat(line, &st) < 0 ||
400: (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
401: (st.st_mode & 022) != 0) {
402: snprintf(buf, sizeof buf,
403: "DSA authentication refused for %.100s: "
404: "bad ownership or modes for '%s'.",
405: pw->pw_name, line);
406: fail = 1;
407: break;
408: }
409: }
410: }
411: if (fail) {
412: log(buf);
413: fclose(f);
414: restore_uid();
415: return 0;
416: }
417: }
418: found_key = 0;
419: found = key_new(KEY_DSA);
420:
421: while (fgets(line, sizeof(line), f)) {
422: char *cp;
423: linenum++;
424: /* Skip leading whitespace, empty and comment lines. */
425: for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
426: ;
427: if (!*cp || *cp == '\n' || *cp == '#')
428: continue;
429: bits = key_read(found, &cp);
430: if (bits == 0)
431: continue;
432: if (key_equal(found, key)) {
433: found_key = 1;
434: debug("matching key found: file %s, line %ld",
435: file, linenum);
436: break;
437: }
438: }
439: restore_uid();
440: fclose(f);
441: key_free(found);
442: return found_key;
443: }