Annotation of src/usr.bin/ssh/auth2-pubkey.c, Revision 1.6.2.2
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: */
24:
25: #include "includes.h"
1.6.2.2 ! brad 26: RCSID("$OpenBSD: auth2-pubkey.c,v 1.9 2004/12/11 01:48:56 dtucker Exp $");
1.1 markus 27:
1.6.2.2 ! brad 28: #include "ssh.h"
1.1 markus 29: #include "ssh2.h"
30: #include "xmalloc.h"
31: #include "packet.h"
32: #include "buffer.h"
33: #include "log.h"
34: #include "servconf.h"
35: #include "compat.h"
36: #include "bufaux.h"
37: #include "auth.h"
38: #include "key.h"
39: #include "pathnames.h"
40: #include "uidswap.h"
41: #include "auth-options.h"
42: #include "canohost.h"
43: #include "monitor_wrap.h"
1.6.2.2 ! brad 44: #include "misc.h"
1.1 markus 45:
46: /* import */
47: extern ServerOptions options;
48: extern u_char *session_id2;
1.4 markus 49: extern u_int session_id2_len;
1.1 markus 50:
1.2 markus 51: static int
1.1 markus 52: userauth_pubkey(Authctxt *authctxt)
53: {
54: Buffer b;
55: Key *key = NULL;
56: char *pkalg;
57: u_char *pkblob, *sig;
58: u_int alen, blen, slen;
59: int have_sig, pktype;
60: int authenticated = 0;
61:
62: if (!authctxt->valid) {
63: debug2("userauth_pubkey: disabled because of invalid user");
64: return 0;
65: }
66: have_sig = packet_get_char();
67: if (datafellows & SSH_BUG_PKAUTH) {
68: debug2("userauth_pubkey: SSH_BUG_PKAUTH");
69: /* no explicit pkalg given */
70: pkblob = packet_get_string(&blen);
71: buffer_init(&b);
72: buffer_append(&b, pkblob, blen);
73: /* so we have to extract the pkalg from the pkblob */
74: pkalg = buffer_get_string(&b, &alen);
75: buffer_free(&b);
76: } else {
77: pkalg = packet_get_string(&alen);
78: pkblob = packet_get_string(&blen);
79: }
80: pktype = key_type_from_name(pkalg);
81: if (pktype == KEY_UNSPEC) {
82: /* this is perfectly legal */
1.3 itojun 83: logit("userauth_pubkey: unsupported public key algorithm: %s",
1.1 markus 84: pkalg);
85: goto done;
86: }
87: key = key_from_blob(pkblob, blen);
88: if (key == NULL) {
89: error("userauth_pubkey: cannot decode key: %s", pkalg);
90: goto done;
91: }
92: if (key->type != pktype) {
93: error("userauth_pubkey: type mismatch for decoded key "
94: "(received %d, expected %d)", key->type, pktype);
95: goto done;
96: }
97: if (have_sig) {
98: sig = packet_get_string(&slen);
99: packet_check_eom();
100: buffer_init(&b);
101: if (datafellows & SSH_OLD_SESSIONID) {
102: buffer_append(&b, session_id2, session_id2_len);
103: } else {
104: buffer_put_string(&b, session_id2, session_id2_len);
105: }
106: /* reconstruct packet */
107: buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
108: buffer_put_cstring(&b, authctxt->user);
109: buffer_put_cstring(&b,
110: datafellows & SSH_BUG_PKSERVICE ?
111: "ssh-userauth" :
112: authctxt->service);
113: if (datafellows & SSH_BUG_PKAUTH) {
114: buffer_put_char(&b, have_sig);
115: } else {
116: buffer_put_cstring(&b, "publickey");
117: buffer_put_char(&b, have_sig);
118: buffer_put_cstring(&b, pkalg);
119: }
120: buffer_put_string(&b, pkblob, blen);
121: #ifdef DEBUG_PK
122: buffer_dump(&b);
123: #endif
124: /* test for correct signature */
125: authenticated = 0;
126: if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
127: PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
1.6 markus 128: buffer_len(&b))) == 1)
1.1 markus 129: authenticated = 1;
1.6 markus 130: buffer_free(&b);
1.1 markus 131: xfree(sig);
132: } else {
133: debug("test whether pkalg/pkblob are acceptable");
134: packet_check_eom();
135:
136: /* XXX fake reply and always send PK_OK ? */
137: /*
138: * XXX this allows testing whether a user is allowed
139: * to login: if you happen to have a valid pubkey this
140: * message is sent. the message is NEVER sent at all
141: * if a user is not allowed to login. is this an
142: * issue? -markus
143: */
144: if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
145: packet_start(SSH2_MSG_USERAUTH_PK_OK);
146: packet_put_string(pkalg, alen);
147: packet_put_string(pkblob, blen);
148: packet_send();
149: packet_write_wait();
150: authctxt->postponed = 1;
151: }
152: }
153: if (authenticated != 1)
154: auth_clear_options();
155: done:
156: debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
157: if (key != NULL)
158: key_free(key);
159: xfree(pkalg);
160: xfree(pkblob);
161: return authenticated;
162: }
163:
164: /* return 1 if user allows given key */
165: static int
166: user_key_allowed2(struct passwd *pw, Key *key, char *file)
167: {
1.6.2.2 ! brad 168: char line[SSH_MAX_PUBKEY_BYTES];
1.1 markus 169: int found_key = 0;
170: FILE *f;
171: u_long linenum = 0;
172: struct stat st;
173: Key *found;
174: char *fp;
175:
176: /* Temporarily use the user's uid. */
177: temporarily_use_uid(pw);
178:
179: debug("trying public key file %s", file);
180:
181: /* Fail quietly if file does not exist */
182: if (stat(file, &st) < 0) {
183: /* Restore the privileged uid. */
184: restore_uid();
185: return 0;
186: }
187: /* Open the file containing the authorized keys. */
188: f = fopen(file, "r");
189: if (!f) {
190: /* Restore the privileged uid. */
191: restore_uid();
192: return 0;
193: }
194: if (options.strict_modes &&
195: secure_filename(f, file, pw, line, sizeof(line)) != 0) {
196: fclose(f);
1.3 itojun 197: logit("Authentication refused: %s", line);
1.1 markus 198: restore_uid();
199: return 0;
200: }
201:
202: found_key = 0;
203: found = key_new(key->type);
204:
1.6.2.2 ! brad 205: while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
1.6.2.1 brad 206: char *cp, *key_options = NULL;
1.6.2.2 ! brad 207:
1.1 markus 208: /* Skip leading whitespace, empty and comment lines. */
209: for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
210: ;
211: if (!*cp || *cp == '\n' || *cp == '#')
212: continue;
213:
214: if (key_read(found, &cp) != 1) {
215: /* no key? check if there are options for this key */
216: int quoted = 0;
217: debug2("user_key_allowed: check options: '%s'", cp);
1.6.2.1 brad 218: key_options = cp;
1.1 markus 219: for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
220: if (*cp == '\\' && cp[1] == '"')
221: cp++; /* Skip both */
222: else if (*cp == '"')
223: quoted = !quoted;
224: }
225: /* Skip remaining whitespace. */
226: for (; *cp == ' ' || *cp == '\t'; cp++)
227: ;
228: if (key_read(found, &cp) != 1) {
229: debug2("user_key_allowed: advance: '%s'", cp);
230: /* still no key? advance to next line*/
231: continue;
232: }
233: }
234: if (key_equal(found, key) &&
1.6.2.1 brad 235: auth_parse_options(pw, key_options, file, linenum) == 1) {
1.1 markus 236: found_key = 1;
237: debug("matching key found: file %s, line %lu",
238: file, linenum);
239: fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
240: verbose("Found matching %s key: %s",
241: key_type(found), fp);
242: xfree(fp);
243: break;
244: }
245: }
246: restore_uid();
247: fclose(f);
248: key_free(found);
249: if (!found_key)
250: debug2("key not found");
251: return found_key;
252: }
253:
254: /* check whether given key is in .ssh/authorized_keys* */
255: int
256: user_key_allowed(struct passwd *pw, Key *key)
257: {
258: int success;
259: char *file;
260:
261: file = authorized_keys_file(pw);
262: success = user_key_allowed2(pw, key, file);
263: xfree(file);
264: if (success)
265: return success;
266:
267: /* try suffix "2" for backward compat, too */
268: file = authorized_keys_file2(pw);
269: success = user_key_allowed2(pw, key, file);
270: xfree(file);
271: return success;
272: }
1.2 markus 273:
274: Authmethod method_pubkey = {
275: "publickey",
276: userauth_pubkey,
277: &options.pubkey_authentication
278: };