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