xref: /titanic_44/usr/src/cmd/ssh/sshd/auth2-pubkey.c (revision 18c2aff776a775d34a4c9893a4c72e0434d68e36)
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 2004 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 #include "monitor_wrap.h"
49 
50 #ifdef USE_PAM
51 #include <security/pam_appl.h>
52 #include "auth-pam.h"
53 #endif /* USE_PAM */
54 
55 /* import */
56 extern ServerOptions options;
57 extern u_char *session_id2;
58 extern int session_id2_len;
59 
60 static void
61 userauth_pubkey(Authctxt *authctxt)
62 {
63 	Buffer b;
64 	Key *key = NULL;
65 	char *pkalg;
66 	u_char *pkblob, *sig;
67 	u_int alen, blen, slen;
68 	int have_sig, pktype;
69 	int authenticated = 0;
70 
71 	if (!authctxt || !authctxt->method)
72 		fatal("%s: missing context", __func__);
73 
74 	have_sig = packet_get_char();
75 	if (datafellows & SSH_BUG_PKAUTH) {
76 		debug2("userauth_pubkey: SSH_BUG_PKAUTH");
77 		/* no explicit pkalg given */
78 		pkblob = packet_get_string(&blen);
79 		buffer_init(&b);
80 		buffer_append(&b, pkblob, blen);
81 		/* so we have to extract the pkalg from the pkblob */
82 		pkalg = buffer_get_string(&b, &alen);
83 		buffer_free(&b);
84 	} else {
85 		pkalg = packet_get_string(&alen);
86 		pkblob = packet_get_string(&blen);
87 	}
88 	pktype = key_type_from_name(pkalg);
89 	if (pktype == KEY_UNSPEC) {
90 		/* this is perfectly legal */
91 		log("userauth_pubkey: unsupported public key algorithm: %s",
92 		    pkalg);
93 		goto done;
94 	}
95 	key = key_from_blob(pkblob, blen);
96 	if (key == NULL) {
97 		error("userauth_pubkey: cannot decode key: %s", pkalg);
98 		goto done;
99 	}
100 	if (key->type != pktype) {
101 		error("userauth_pubkey: type mismatch for decoded key "
102 		    "(received %d, expected %d)", key->type, pktype);
103 		goto done;
104 	}
105 
106 	/* Detect and count abandonment */
107 	if (authctxt->method->method_data) {
108 		Key	*prev_key;
109 		unsigned char	*prev_pkblob;
110 		int	 prev_blen;
111 
112 		/*
113 		 * Check for earlier test of a key that was allowed but
114 		 * not followed up with a pubkey req for the same pubkey
115 		 * and with a signature.
116 		 */
117 		prev_key = authctxt->method->method_data;
118 		if ((prev_blen = key_to_blob(prev_key,
119 			    &prev_pkblob, NULL))) {
120 			if (prev_blen != blen ||
121 			    memcmp(prev_pkblob, pkblob, blen) != 0) {
122 				authctxt->method->abandons++;
123 				authctxt->method->attempts++;
124 			}
125 		}
126 		key_free(prev_key);
127 		authctxt->method->method_data = NULL;
128 	}
129 
130 	if (have_sig) {
131 		sig = packet_get_string(&slen);
132 		packet_check_eom();
133 		buffer_init(&b);
134 		if (datafellows & SSH_OLD_SESSIONID) {
135 			buffer_append(&b, session_id2, session_id2_len);
136 		} else {
137 			buffer_put_string(&b, session_id2, session_id2_len);
138 		}
139 		/* reconstruct packet */
140 		buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
141 		buffer_put_cstring(&b, authctxt->user);
142 		buffer_put_cstring(&b,
143 		    datafellows & SSH_BUG_PKSERVICE ?
144 		    "ssh-userauth" :
145 		    authctxt->service);
146 		if (datafellows & SSH_BUG_PKAUTH) {
147 			buffer_put_char(&b, have_sig);
148 		} else {
149 			buffer_put_cstring(&b, "publickey");
150 			buffer_put_char(&b, have_sig);
151 			buffer_put_cstring(&b, pkalg);
152 		}
153 		buffer_put_string(&b, pkblob, blen);
154 #ifdef DEBUG_PK
155 		buffer_dump(&b);
156 #endif
157 		/* test for correct signature */
158 		if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
159 		    PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
160 				buffer_len(&b))) == 1)
161 			authenticated = 1;
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 (PRIVSEP(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