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