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