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