xref: /titanic_50/usr/src/cmd/ssh/sshd/auth2-pubkey.c (revision 4bff34e37def8a90f9194d81bc345c52ba20086a)
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  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  */
28 
29 #include "includes.h"
30 RCSID("$OpenBSD: auth2-pubkey.c,v 1.2 2002/05/31 11:35:15 markus Exp $");
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI"
33 
34 #include "ssh2.h"
35 #include "xmalloc.h"
36 #include "packet.h"
37 #include "buffer.h"
38 #include "log.h"
39 #include "servconf.h"
40 #include "compat.h"
41 #include "bufaux.h"
42 #include "auth.h"
43 #include "key.h"
44 #include "pathnames.h"
45 #include "uidswap.h"
46 #include "auth-options.h"
47 #include "canohost.h"
48 
49 #ifdef USE_PAM
50 #include <security/pam_appl.h>
51 #include "auth-pam.h"
52 #endif /* USE_PAM */
53 
54 /* import */
55 extern ServerOptions options;
56 extern u_char *session_id2;
57 extern int session_id2_len;
58 
59 static void
60 userauth_pubkey(Authctxt *authctxt)
61 {
62 	Buffer b;
63 	Key *key = NULL;
64 	char *pkalg;
65 	u_char *pkblob, *sig;
66 	u_int alen, blen, slen;
67 	int have_sig, pktype;
68 	int authenticated = 0;
69 
70 	if (!authctxt || !authctxt->method)
71 		fatal("%s: missing context", __func__);
72 
73 	have_sig = packet_get_char();
74 	if (datafellows & SSH_BUG_PKAUTH) {
75 		debug2("userauth_pubkey: SSH_BUG_PKAUTH");
76 		/* no explicit pkalg given */
77 		pkblob = packet_get_string(&blen);
78 		buffer_init(&b);
79 		buffer_append(&b, pkblob, blen);
80 		/* so we have to extract the pkalg from the pkblob */
81 		pkalg = buffer_get_string(&b, &alen);
82 		buffer_free(&b);
83 	} else {
84 		pkalg = packet_get_string(&alen);
85 		pkblob = packet_get_string(&blen);
86 	}
87 	pktype = key_type_from_name(pkalg);
88 	if (pktype == KEY_UNSPEC) {
89 		/* this is perfectly legal */
90 		log("userauth_pubkey: unsupported public key algorithm: %s",
91 		    pkalg);
92 		goto done;
93 	}
94 	key = key_from_blob(pkblob, blen);
95 	if (key == NULL) {
96 		error("userauth_pubkey: cannot decode key: %s", pkalg);
97 		goto done;
98 	}
99 	if (key->type != pktype) {
100 		error("userauth_pubkey: type mismatch for decoded key "
101 		    "(received %d, expected %d)", key->type, pktype);
102 		goto done;
103 	}
104 
105 	/* Detect and count abandonment */
106 	if (authctxt->method->method_data) {
107 		Key	*prev_key;
108 		unsigned char	*prev_pkblob;
109 		int	 prev_blen;
110 
111 		/*
112 		 * Check for earlier test of a key that was allowed but
113 		 * not followed up with a pubkey req for the same pubkey
114 		 * and with a signature.
115 		 */
116 		prev_key = authctxt->method->method_data;
117 		if ((prev_blen = key_to_blob(prev_key,
118 			    &prev_pkblob, NULL))) {
119 			if (prev_blen != blen ||
120 			    memcmp(prev_pkblob, pkblob, blen) != 0) {
121 				authctxt->method->abandons++;
122 				authctxt->method->attempts++;
123 			}
124 		}
125 		key_free(prev_key);
126 		authctxt->method->method_data = NULL;
127 	}
128 
129 	if (have_sig) {
130 		sig = packet_get_string(&slen);
131 		packet_check_eom();
132 		buffer_init(&b);
133 		if (datafellows & SSH_OLD_SESSIONID) {
134 			buffer_append(&b, session_id2, session_id2_len);
135 		} else {
136 			buffer_put_string(&b, session_id2, session_id2_len);
137 		}
138 		/* reconstruct packet */
139 		buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
140 		buffer_put_cstring(&b, authctxt->user);
141 		buffer_put_cstring(&b,
142 		    datafellows & SSH_BUG_PKSERVICE ?
143 		    "ssh-userauth" :
144 		    authctxt->service);
145 		if (datafellows & SSH_BUG_PKAUTH) {
146 			buffer_put_char(&b, have_sig);
147 		} else {
148 			buffer_put_cstring(&b, "publickey");
149 			buffer_put_char(&b, have_sig);
150 			buffer_put_cstring(&b, pkalg);
151 		}
152 		buffer_put_string(&b, pkblob, blen);
153 #ifdef DEBUG_PK
154 		buffer_dump(&b);
155 #endif
156 		/* test for correct signature */
157 		if (user_key_allowed(authctxt->pw, key) &&
158 		    key_verify(key, sig, slen, buffer_ptr(&b),
159 		    buffer_len(&b)) == 1) {
160 			authenticated = 1;
161 		}
162 		authctxt->method->postponed = 0;
163 		buffer_clear(&b);
164 		xfree(sig);
165 	} else {
166 		debug("test whether pkalg/pkblob are acceptable");
167 		packet_check_eom();
168 
169 		/* XXX fake reply and always send PK_OK ? */
170 		/*
171 		 * XXX this allows testing whether a user is allowed
172 		 * to login: if you happen to have a valid pubkey this
173 		 * message is sent. the message is NEVER sent at all
174 		 * if a user is not allowed to login. is this an
175 		 * issue? -markus
176 		 */
177 		if (user_key_allowed(authctxt->pw, key)) {
178 			packet_start(SSH2_MSG_USERAUTH_PK_OK);
179 			packet_put_string(pkalg, alen);
180 			packet_put_string(pkblob, blen);
181 			packet_send();
182 			packet_write_wait();
183 			authctxt->method->postponed = 1;
184 			/*
185 			 * Remember key that was tried so we can
186 			 * correctly detect abandonment.  See above.
187 			 */
188 			authctxt->method->method_data = (void *) key;
189 			key = NULL;
190 		}
191 	}
192 	if (authenticated != 1)
193 		auth_clear_options();
194 
195 done:
196 	/*
197 	 * XXX TODO: add config options for specifying users for whom
198 	 * this userauth is insufficient and what userauths may
199 	 * continue.
200 	 */
201 #ifdef USE_PAM
202 	if (authenticated) {
203 		if (!do_pam_non_initial_userauth(authctxt))
204 			authenticated = 0;
205 	}
206 #endif /* USE_PAM */
207 
208 	debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
209 	if (key != NULL)
210 		key_free(key);
211 	xfree(pkalg);
212 	xfree(pkblob);
213 #ifdef HAVE_CYGWIN
214 	if (check_nt_auth(0, authctxt->pw) == 0)
215 		return;
216 #endif
217 	if (authenticated)
218 		authctxt->method->authenticated = 1;
219 }
220 
221 /* return 1 if user allows given key */
222 static int
223 user_key_allowed2(struct passwd *pw, Key *key, char *file)
224 {
225 	char line[8192];
226 	int found_key = 0;
227 	FILE *f;
228 	u_long linenum = 0;
229 	struct stat st;
230 	Key *found;
231 	char *fp;
232 
233 	if (pw == NULL)
234 		return 0;
235 
236 	/* Temporarily use the user's uid. */
237 	temporarily_use_uid(pw);
238 
239 	debug("trying public key file %s", file);
240 
241 	/* Fail quietly if file does not exist */
242 	if (stat(file, &st) < 0) {
243 		/* Restore the privileged uid. */
244 		restore_uid();
245 		return 0;
246 	}
247 	/* Open the file containing the authorized keys. */
248 	f = fopen(file, "r");
249 	if (!f) {
250 		/* Restore the privileged uid. */
251 		restore_uid();
252 		return 0;
253 	}
254 	if (options.strict_modes &&
255 	    secure_filename(f, file, pw, line, sizeof(line)) != 0) {
256 		(void) fclose(f);
257 		log("Authentication refused: %s", line);
258 		restore_uid();
259 		return 0;
260 	}
261 
262 	found_key = 0;
263 	found = key_new(key->type);
264 
265 	while (fgets(line, sizeof(line), f)) {
266 		char *cp, *options = NULL;
267 		linenum++;
268 		/* Skip leading whitespace, empty and comment lines. */
269 		for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
270 			;
271 		if (!*cp || *cp == '\n' || *cp == '#')
272 			continue;
273 
274 		if (key_read(found, &cp) != 1) {
275 			/* no key?  check if there are options for this key */
276 			int quoted = 0;
277 			debug2("user_key_allowed: check options: '%s'", cp);
278 			options = cp;
279 			for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
280 				if (*cp == '\\' && cp[1] == '"')
281 					cp++;	/* Skip both */
282 				else if (*cp == '"')
283 					quoted = !quoted;
284 			}
285 			/* Skip remaining whitespace. */
286 			for (; *cp == ' ' || *cp == '\t'; cp++)
287 				;
288 			if (key_read(found, &cp) != 1) {
289 				debug2("user_key_allowed: advance: '%s'", cp);
290 				/* still no key?  advance to next line*/
291 				continue;
292 			}
293 		}
294 		if (key_equal(found, key) &&
295 		    auth_parse_options(pw, options, file, linenum) == 1) {
296 			found_key = 1;
297 			debug("matching key found: file %s, line %lu",
298 			    file, linenum);
299 			fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
300 			verbose("Found matching %s key: %s",
301 			    key_type(found), fp);
302 			xfree(fp);
303 			break;
304 		}
305 	}
306 	restore_uid();
307 	(void) fclose(f);
308 	key_free(found);
309 	if (!found_key)
310 		debug2("key not found");
311 	return found_key;
312 }
313 
314 /* check whether given key is in .ssh/authorized_keys* */
315 int
316 user_key_allowed(struct passwd *pw, Key *key)
317 {
318 	int success;
319 	char *file;
320 
321 	if (pw == NULL)
322 		return 0;
323 
324 	file = authorized_keys_file(pw);
325 	success = user_key_allowed2(pw, key, file);
326 	xfree(file);
327 	if (success)
328 		return success;
329 
330 	/* try suffix "2" for backward compat, too */
331 	file = authorized_keys_file2(pw);
332 	success = user_key_allowed2(pw, key, file);
333 	xfree(file);
334 	return success;
335 }
336 
337 static
338 void
339 userauth_pubkey_abandon(Authctxt *authctxt, Authmethod *method)
340 {
341 	if (!authctxt || !method)
342 		return;
343 
344 	if (method->method_data) {
345 		method->abandons++;
346 		method->attempts++;
347 		key_free((Key *) method->method_data);
348 		method->method_data = NULL;
349 	}
350 }
351 
352 Authmethod method_pubkey = {
353 	"publickey",
354 	&options.pubkey_authentication,
355 	userauth_pubkey,
356 	userauth_pubkey_abandon,
357 	NULL, NULL,	    /* method data and hist data */
358 	0,		    /* not initial userauth */
359 	0, 0, 0,	    /* counters */
360 	0, 0, 0, 0, 0, 0    /* state */
361 };
362