jsing@openbsd.org | aff3e94 | 2015-06-15 18:42:19 +0000 | [diff] [blame^] | 1 | /* $OpenBSD: auth2-pubkey.c,v 1.52 2015/06/15 18:42:19 jsing Exp $ */ |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 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" |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 27 | |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/stat.h> |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 30 | #include <sys/wait.h> |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 31 | |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 32 | #include <errno.h> |
Darren Tucker | 06db584 | 2008-06-13 14:51:28 +1000 | [diff] [blame] | 33 | #include <fcntl.h> |
Darren Tucker | 737f7af | 2012-11-05 17:07:43 +1100 | [diff] [blame] | 34 | #ifdef HAVE_PATHS_H |
| 35 | # include <paths.h> |
| 36 | #endif |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 37 | #include <pwd.h> |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 38 | #include <signal.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 39 | #include <stdio.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 40 | #include <stdarg.h> |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 41 | #include <string.h> |
| 42 | #include <time.h> |
Darren Tucker | d9526a5 | 2008-06-14 09:01:24 +1000 | [diff] [blame] | 43 | #include <unistd.h> |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 44 | #include <limits.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 45 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 46 | #include "xmalloc.h" |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 47 | #include "ssh.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 48 | #include "ssh2.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 49 | #include "packet.h" |
| 50 | #include "buffer.h" |
| 51 | #include "log.h" |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 52 | #include "misc.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 53 | #include "servconf.h" |
| 54 | #include "compat.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 55 | #include "key.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 56 | #include "hostfile.h" |
| 57 | #include "auth.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 58 | #include "pathnames.h" |
| 59 | #include "uidswap.h" |
| 60 | #include "auth-options.h" |
| 61 | #include "canohost.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 62 | #ifdef GSSAPI |
| 63 | #include "ssh-gss.h" |
| 64 | #endif |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 65 | #include "monitor_wrap.h" |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 66 | #include "authfile.h" |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 67 | #include "match.h" |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 68 | #include "ssherr.h" |
| 69 | #include "channels.h" /* XXX for session.h */ |
| 70 | #include "session.h" /* XXX for child_set_env(); refactor? */ |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 71 | |
| 72 | /* import */ |
| 73 | extern ServerOptions options; |
| 74 | extern u_char *session_id2; |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 75 | extern u_int session_id2_len; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 76 | |
| 77 | static int |
| 78 | userauth_pubkey(Authctxt *authctxt) |
| 79 | { |
| 80 | Buffer b; |
| 81 | Key *key = NULL; |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 82 | char *pkalg, *userstyle; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 83 | u_char *pkblob, *sig; |
| 84 | u_int alen, blen, slen; |
| 85 | int have_sig, pktype; |
| 86 | int authenticated = 0; |
| 87 | |
| 88 | if (!authctxt->valid) { |
| 89 | debug2("userauth_pubkey: disabled because of invalid user"); |
| 90 | return 0; |
| 91 | } |
| 92 | have_sig = packet_get_char(); |
| 93 | if (datafellows & SSH_BUG_PKAUTH) { |
| 94 | debug2("userauth_pubkey: SSH_BUG_PKAUTH"); |
| 95 | /* no explicit pkalg given */ |
| 96 | pkblob = packet_get_string(&blen); |
| 97 | buffer_init(&b); |
| 98 | buffer_append(&b, pkblob, blen); |
| 99 | /* so we have to extract the pkalg from the pkblob */ |
| 100 | pkalg = buffer_get_string(&b, &alen); |
| 101 | buffer_free(&b); |
| 102 | } else { |
| 103 | pkalg = packet_get_string(&alen); |
| 104 | pkblob = packet_get_string(&blen); |
| 105 | } |
| 106 | pktype = key_type_from_name(pkalg); |
| 107 | if (pktype == KEY_UNSPEC) { |
| 108 | /* this is perfectly legal */ |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 109 | logit("userauth_pubkey: unsupported public key algorithm: %s", |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 110 | pkalg); |
| 111 | goto done; |
| 112 | } |
| 113 | key = key_from_blob(pkblob, blen); |
| 114 | if (key == NULL) { |
| 115 | error("userauth_pubkey: cannot decode key: %s", pkalg); |
| 116 | goto done; |
| 117 | } |
| 118 | if (key->type != pktype) { |
| 119 | error("userauth_pubkey: type mismatch for decoded key " |
| 120 | "(received %d, expected %d)", key->type, pktype); |
| 121 | goto done; |
| 122 | } |
Damien Miller | 324541e | 2013-12-31 12:25:40 +1100 | [diff] [blame] | 123 | if (key_type_plain(key->type) == KEY_RSA && |
| 124 | (datafellows & SSH_BUG_RSASIGMD5) != 0) { |
| 125 | logit("Refusing RSA key because client uses unsafe " |
| 126 | "signature scheme"); |
| 127 | goto done; |
| 128 | } |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 129 | if (auth2_userkey_already_used(authctxt, key)) { |
| 130 | logit("refusing previously-used %s key", key_type(key)); |
| 131 | goto done; |
| 132 | } |
djm@openbsd.org | e661a86 | 2015-05-04 06:10:48 +0000 | [diff] [blame] | 133 | if (match_pattern_list(sshkey_ssh_name(key), |
| 134 | options.pubkey_key_types, 0) != 1) { |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 135 | logit("%s: key type %s not in PubkeyAcceptedKeyTypes", |
| 136 | __func__, sshkey_ssh_name(key)); |
| 137 | goto done; |
| 138 | } |
| 139 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 140 | if (have_sig) { |
| 141 | sig = packet_get_string(&slen); |
| 142 | packet_check_eom(); |
| 143 | buffer_init(&b); |
| 144 | if (datafellows & SSH_OLD_SESSIONID) { |
| 145 | buffer_append(&b, session_id2, session_id2_len); |
| 146 | } else { |
| 147 | buffer_put_string(&b, session_id2, session_id2_len); |
| 148 | } |
| 149 | /* reconstruct packet */ |
| 150 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 151 | xasprintf(&userstyle, "%s%s%s", authctxt->user, |
| 152 | authctxt->style ? ":" : "", |
| 153 | authctxt->style ? authctxt->style : ""); |
| 154 | buffer_put_cstring(&b, userstyle); |
| 155 | free(userstyle); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 156 | buffer_put_cstring(&b, |
| 157 | datafellows & SSH_BUG_PKSERVICE ? |
| 158 | "ssh-userauth" : |
| 159 | authctxt->service); |
| 160 | if (datafellows & SSH_BUG_PKAUTH) { |
| 161 | buffer_put_char(&b, have_sig); |
| 162 | } else { |
| 163 | buffer_put_cstring(&b, "publickey"); |
| 164 | buffer_put_char(&b, have_sig); |
| 165 | buffer_put_cstring(&b, pkalg); |
| 166 | } |
| 167 | buffer_put_string(&b, pkblob, blen); |
| 168 | #ifdef DEBUG_PK |
| 169 | buffer_dump(&b); |
| 170 | #endif |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 171 | pubkey_auth_info(authctxt, key, NULL); |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 172 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 173 | /* test for correct signature */ |
| 174 | authenticated = 0; |
djm@openbsd.org | 179be0f | 2015-05-01 03:23:51 +0000 | [diff] [blame] | 175 | if (PRIVSEP(user_key_allowed(authctxt->pw, key, 1)) && |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 176 | PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b), |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 177 | buffer_len(&b))) == 1) { |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 178 | authenticated = 1; |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 179 | /* Record the successful key to prevent reuse */ |
| 180 | auth2_record_userkey(authctxt, key); |
| 181 | key = NULL; /* Don't free below */ |
| 182 | } |
Damien Miller | fb1310e | 2004-01-21 11:02:50 +1100 | [diff] [blame] | 183 | buffer_free(&b); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 184 | free(sig); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 185 | } else { |
| 186 | debug("test whether pkalg/pkblob are acceptable"); |
| 187 | packet_check_eom(); |
| 188 | |
| 189 | /* XXX fake reply and always send PK_OK ? */ |
| 190 | /* |
| 191 | * XXX this allows testing whether a user is allowed |
| 192 | * to login: if you happen to have a valid pubkey this |
| 193 | * message is sent. the message is NEVER sent at all |
| 194 | * if a user is not allowed to login. is this an |
| 195 | * issue? -markus |
| 196 | */ |
djm@openbsd.org | 179be0f | 2015-05-01 03:23:51 +0000 | [diff] [blame] | 197 | if (PRIVSEP(user_key_allowed(authctxt->pw, key, 0))) { |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 198 | packet_start(SSH2_MSG_USERAUTH_PK_OK); |
| 199 | packet_put_string(pkalg, alen); |
| 200 | packet_put_string(pkblob, blen); |
| 201 | packet_send(); |
| 202 | packet_write_wait(); |
| 203 | authctxt->postponed = 1; |
| 204 | } |
| 205 | } |
| 206 | if (authenticated != 1) |
| 207 | auth_clear_options(); |
| 208 | done: |
| 209 | debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg); |
| 210 | if (key != NULL) |
| 211 | key_free(key); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 212 | free(pkalg); |
| 213 | free(pkblob); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 214 | return authenticated; |
| 215 | } |
| 216 | |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 217 | void |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 218 | pubkey_auth_info(Authctxt *authctxt, const Key *key, const char *fmt, ...) |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 219 | { |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 220 | char *fp, *extra; |
| 221 | va_list ap; |
| 222 | int i; |
| 223 | |
| 224 | extra = NULL; |
| 225 | if (fmt != NULL) { |
| 226 | va_start(ap, fmt); |
| 227 | i = vasprintf(&extra, fmt, ap); |
| 228 | va_end(ap); |
| 229 | if (i < 0 || extra == NULL) |
| 230 | fatal("%s: vasprintf failed", __func__); |
| 231 | } |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 232 | |
| 233 | if (key_is_cert(key)) { |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 234 | fp = sshkey_fingerprint(key->cert->signature_key, |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 235 | options.fingerprint_hash, SSH_FP_DEFAULT); |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 236 | auth_info(authctxt, "%s ID %s (serial %llu) CA %s %s%s%s", |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 237 | key_type(key), key->cert->key_id, |
| 238 | (unsigned long long)key->cert->serial, |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 239 | key_type(key->cert->signature_key), |
djm@openbsd.org | 13a3941 | 2015-02-17 00:14:05 +0000 | [diff] [blame] | 240 | fp == NULL ? "(null)" : fp, |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 241 | extra == NULL ? "" : ", ", extra == NULL ? "" : extra); |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 242 | free(fp); |
| 243 | } else { |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 244 | fp = sshkey_fingerprint(key, options.fingerprint_hash, |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 245 | SSH_FP_DEFAULT); |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 246 | auth_info(authctxt, "%s %s%s%s", key_type(key), |
djm@openbsd.org | 13a3941 | 2015-02-17 00:14:05 +0000 | [diff] [blame] | 247 | fp == NULL ? "(null)" : fp, |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 248 | extra == NULL ? "" : ", ", extra == NULL ? "" : extra); |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 249 | free(fp); |
| 250 | } |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 251 | free(extra); |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 252 | } |
| 253 | |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 254 | /* |
| 255 | * Splits 's' into an argument vector. Handles quoted string and basic |
| 256 | * escape characters (\\, \", \'). Caller must free the argument vector |
| 257 | * and its members. |
| 258 | */ |
| 259 | static int |
| 260 | split_argv(const char *s, int *argcp, char ***argvp) |
| 261 | { |
| 262 | int r = SSH_ERR_INTERNAL_ERROR; |
| 263 | int argc = 0, quote, i, j; |
| 264 | char *arg, **argv = xcalloc(1, sizeof(*argv)); |
| 265 | |
| 266 | *argvp = NULL; |
| 267 | *argcp = 0; |
| 268 | |
| 269 | for (i = 0; s[i] != '\0'; i++) { |
| 270 | /* Skip leading whitespace */ |
| 271 | if (s[i] == ' ' || s[i] == '\t') |
| 272 | continue; |
| 273 | |
| 274 | /* Start of a token */ |
| 275 | quote = 0; |
| 276 | if (s[i] == '\\' && |
| 277 | (s[i + 1] == '\'' || s[i + 1] == '\"' || s[i + 1] == '\\')) |
| 278 | i++; |
| 279 | else if (s[i] == '\'' || s[i] == '"') |
| 280 | quote = s[i++]; |
| 281 | |
| 282 | argv = xreallocarray(argv, (argc + 2), sizeof(*argv)); |
| 283 | arg = argv[argc++] = xcalloc(1, strlen(s + i) + 1); |
| 284 | argv[argc] = NULL; |
| 285 | |
| 286 | /* Copy the token in, removing escapes */ |
| 287 | for (j = 0; s[i] != '\0'; i++) { |
| 288 | if (s[i] == '\\') { |
| 289 | if (s[i + 1] == '\'' || |
| 290 | s[i + 1] == '\"' || |
| 291 | s[i + 1] == '\\') { |
| 292 | i++; /* Skip '\' */ |
| 293 | arg[j++] = s[i]; |
| 294 | } else { |
| 295 | /* Unrecognised escape */ |
| 296 | arg[j++] = s[i]; |
| 297 | } |
| 298 | } else if (quote == 0 && (s[i] == ' ' || s[i] == '\t')) |
| 299 | break; /* done */ |
| 300 | else if (quote != 0 && s[i] == quote) |
| 301 | break; /* done */ |
| 302 | else |
| 303 | arg[j++] = s[i]; |
| 304 | } |
| 305 | if (s[i] == '\0') { |
| 306 | if (quote != 0) { |
| 307 | /* Ran out of string looking for close quote */ |
| 308 | r = SSH_ERR_INVALID_FORMAT; |
| 309 | goto out; |
| 310 | } |
| 311 | break; |
| 312 | } |
| 313 | } |
| 314 | /* Success */ |
| 315 | *argcp = argc; |
| 316 | *argvp = argv; |
| 317 | argc = 0; |
| 318 | argv = NULL; |
| 319 | r = 0; |
| 320 | out: |
| 321 | if (argc != 0 && argv != NULL) { |
| 322 | for (i = 0; i < argc; i++) |
| 323 | free(argv[i]); |
| 324 | free(argv); |
| 325 | } |
| 326 | return r; |
| 327 | } |
| 328 | |
| 329 | /* |
| 330 | * Reassemble an argument vector into a string, quoting and escaping as |
| 331 | * necessary. Caller must free returned string. |
| 332 | */ |
| 333 | static char * |
| 334 | assemble_argv(int argc, char **argv) |
| 335 | { |
| 336 | int i, j, ws, r; |
| 337 | char c, *ret; |
| 338 | struct sshbuf *buf, *arg; |
| 339 | |
| 340 | if ((buf = sshbuf_new()) == NULL || (arg = sshbuf_new()) == NULL) |
| 341 | fatal("%s: sshbuf_new failed", __func__); |
| 342 | |
| 343 | for (i = 0; i < argc; i++) { |
| 344 | ws = 0; |
| 345 | sshbuf_reset(arg); |
| 346 | for (j = 0; argv[i][j] != '\0'; j++) { |
| 347 | r = 0; |
| 348 | c = argv[i][j]; |
| 349 | switch (c) { |
| 350 | case ' ': |
| 351 | case '\t': |
| 352 | ws = 1; |
| 353 | r = sshbuf_put_u8(arg, c); |
| 354 | break; |
| 355 | case '\\': |
| 356 | case '\'': |
| 357 | case '"': |
| 358 | if ((r = sshbuf_put_u8(arg, '\\')) != 0) |
| 359 | break; |
| 360 | /* FALLTHROUGH */ |
| 361 | default: |
| 362 | r = sshbuf_put_u8(arg, c); |
| 363 | break; |
| 364 | } |
| 365 | if (r != 0) |
| 366 | fatal("%s: sshbuf_put_u8: %s", |
| 367 | __func__, ssh_err(r)); |
| 368 | } |
| 369 | if ((i != 0 && (r = sshbuf_put_u8(buf, ' ')) != 0) || |
| 370 | (ws != 0 && (r = sshbuf_put_u8(buf, '"')) != 0) || |
| 371 | (r = sshbuf_putb(buf, arg)) != 0 || |
| 372 | (ws != 0 && (r = sshbuf_put_u8(buf, '"')) != 0)) |
| 373 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 374 | } |
| 375 | if ((ret = malloc(sshbuf_len(buf) + 1)) == NULL) |
| 376 | fatal("%s: malloc failed", __func__); |
| 377 | memcpy(ret, sshbuf_ptr(buf), sshbuf_len(buf)); |
| 378 | ret[sshbuf_len(buf)] = '\0'; |
| 379 | sshbuf_free(buf); |
| 380 | sshbuf_free(arg); |
| 381 | return ret; |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * Runs command in a subprocess. Returns pid on success and a FILE* to the |
| 386 | * subprocess' stdout or 0 on failure. |
| 387 | * NB. "command" is only used for logging. |
| 388 | */ |
| 389 | static pid_t |
| 390 | subprocess(const char *tag, struct passwd *pw, const char *command, |
| 391 | int ac, char **av, FILE **child) |
| 392 | { |
| 393 | FILE *f; |
| 394 | struct stat st; |
| 395 | int devnull, p[2], i; |
| 396 | pid_t pid; |
| 397 | char *cp, errmsg[512]; |
| 398 | u_int envsize; |
| 399 | char **child_env; |
| 400 | |
| 401 | *child = NULL; |
| 402 | |
| 403 | debug3("%s: %s command \"%s\" running as %s", __func__, |
| 404 | tag, command, pw->pw_name); |
| 405 | |
| 406 | /* Verify the path exists and is safe-ish to execute */ |
| 407 | if (*av[0] != '/') { |
| 408 | error("%s path is not absolute", tag); |
| 409 | return 0; |
| 410 | } |
| 411 | temporarily_use_uid(pw); |
| 412 | if (stat(av[0], &st) < 0) { |
| 413 | error("Could not stat %s \"%s\": %s", tag, |
| 414 | av[0], strerror(errno)); |
| 415 | restore_uid(); |
| 416 | return 0; |
| 417 | } |
| 418 | if (auth_secure_path(av[0], &st, NULL, 0, |
| 419 | errmsg, sizeof(errmsg)) != 0) { |
| 420 | error("Unsafe %s \"%s\": %s", tag, av[0], errmsg); |
| 421 | restore_uid(); |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | /* |
| 426 | * Run the command; stderr is left in place, stdout is the |
| 427 | * authorized_keys output. |
| 428 | */ |
| 429 | if (pipe(p) != 0) { |
| 430 | error("%s: pipe: %s", tag, strerror(errno)); |
| 431 | restore_uid(); |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | * Don't want to call this in the child, where it can fatal() and |
| 437 | * run cleanup_exit() code. |
| 438 | */ |
| 439 | restore_uid(); |
| 440 | |
| 441 | switch ((pid = fork())) { |
| 442 | case -1: /* error */ |
| 443 | error("%s: fork: %s", tag, strerror(errno)); |
| 444 | close(p[0]); |
| 445 | close(p[1]); |
| 446 | return 0; |
| 447 | case 0: /* child */ |
| 448 | /* Prepare a minimal environment for the child. */ |
| 449 | envsize = 5; |
| 450 | child_env = xcalloc(sizeof(*child_env), envsize); |
| 451 | child_set_env(&child_env, &envsize, "PATH", _PATH_STDPATH); |
| 452 | child_set_env(&child_env, &envsize, "USER", pw->pw_name); |
| 453 | child_set_env(&child_env, &envsize, "LOGNAME", pw->pw_name); |
| 454 | child_set_env(&child_env, &envsize, "HOME", pw->pw_dir); |
| 455 | if ((cp = getenv("LANG")) != NULL) |
| 456 | child_set_env(&child_env, &envsize, "LANG", cp); |
| 457 | |
| 458 | for (i = 0; i < NSIG; i++) |
| 459 | signal(i, SIG_DFL); |
| 460 | |
| 461 | if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) { |
| 462 | error("%s: open %s: %s", tag, _PATH_DEVNULL, |
| 463 | strerror(errno)); |
| 464 | _exit(1); |
| 465 | } |
| 466 | /* Keep stderr around a while longer to catch errors */ |
| 467 | if (dup2(devnull, STDIN_FILENO) == -1 || |
| 468 | dup2(p[1], STDOUT_FILENO) == -1) { |
| 469 | error("%s: dup2: %s", tag, strerror(errno)); |
| 470 | _exit(1); |
| 471 | } |
| 472 | closefrom(STDERR_FILENO + 1); |
| 473 | |
| 474 | /* Don't use permanently_set_uid() here to avoid fatal() */ |
| 475 | if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) { |
| 476 | error("%s: setresgid %u: %s", tag, (u_int)pw->pw_gid, |
| 477 | strerror(errno)); |
| 478 | _exit(1); |
| 479 | } |
| 480 | if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) { |
| 481 | error("%s: setresuid %u: %s", tag, (u_int)pw->pw_uid, |
| 482 | strerror(errno)); |
| 483 | _exit(1); |
| 484 | } |
| 485 | /* stdin is pointed to /dev/null at this point */ |
| 486 | if (dup2(STDIN_FILENO, STDERR_FILENO) == -1) { |
| 487 | error("%s: dup2: %s", tag, strerror(errno)); |
| 488 | _exit(1); |
| 489 | } |
| 490 | |
| 491 | execve(av[0], av, child_env); |
| 492 | error("%s exec \"%s\": %s", tag, command, strerror(errno)); |
| 493 | _exit(127); |
| 494 | default: /* parent */ |
| 495 | break; |
| 496 | } |
| 497 | |
| 498 | close(p[1]); |
| 499 | if ((f = fdopen(p[0], "r")) == NULL) { |
| 500 | error("%s: fdopen: %s", tag, strerror(errno)); |
| 501 | close(p[0]); |
| 502 | /* Don't leave zombie child */ |
| 503 | kill(pid, SIGTERM); |
| 504 | while (waitpid(pid, NULL, 0) == -1 && errno == EINTR) |
| 505 | ; |
| 506 | return 0; |
| 507 | } |
| 508 | /* Success */ |
| 509 | debug3("%s: %s pid %ld", __func__, tag, (long)pid); |
| 510 | *child = f; |
| 511 | return pid; |
| 512 | } |
| 513 | |
| 514 | /* Returns 0 if pid exited cleanly, non-zero otherwise */ |
| 515 | static int |
| 516 | exited_cleanly(pid_t pid, const char *tag, const char *cmd) |
| 517 | { |
| 518 | int status; |
| 519 | |
| 520 | while (waitpid(pid, &status, 0) == -1) { |
| 521 | if (errno != EINTR) { |
| 522 | error("%s: waitpid: %s", tag, strerror(errno)); |
| 523 | return -1; |
| 524 | } |
| 525 | } |
| 526 | if (WIFSIGNALED(status)) { |
| 527 | error("%s %s exited on signal %d", tag, cmd, WTERMSIG(status)); |
| 528 | return -1; |
| 529 | } else if (WEXITSTATUS(status) != 0) { |
| 530 | error("%s %s failed, status %d", tag, cmd, WEXITSTATUS(status)); |
| 531 | return -1; |
| 532 | } |
| 533 | return 0; |
| 534 | } |
| 535 | |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 536 | static int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 537 | match_principals_option(const char *principal_list, struct sshkey_cert *cert) |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 538 | { |
| 539 | char *result; |
| 540 | u_int i; |
| 541 | |
| 542 | /* XXX percent_expand() sequences for authorized_principals? */ |
| 543 | |
| 544 | for (i = 0; i < cert->nprincipals; i++) { |
| 545 | if ((result = match_list(cert->principals[i], |
| 546 | principal_list, NULL)) != NULL) { |
| 547 | debug3("matched principal from key options \"%.100s\"", |
| 548 | result); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 549 | free(result); |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 550 | return 1; |
| 551 | } |
| 552 | } |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | static int |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 557 | process_principals(FILE *f, char *file, struct passwd *pw, |
| 558 | struct sshkey_cert *cert) |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 559 | { |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 560 | char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts; |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 561 | u_long linenum = 0; |
| 562 | u_int i; |
| 563 | |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 564 | while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) { |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 565 | /* Skip leading whitespace. */ |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 566 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 567 | ; |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 568 | /* Skip blank and comment lines. */ |
| 569 | if ((ep = strchr(cp, '#')) != NULL) |
| 570 | *ep = '\0'; |
| 571 | if (!*cp || *cp == '\n') |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 572 | continue; |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 573 | /* Trim trailing whitespace. */ |
| 574 | ep = cp + strlen(cp) - 1; |
| 575 | while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t')) |
| 576 | *ep-- = '\0'; |
| 577 | /* |
| 578 | * If the line has internal whitespace then assume it has |
| 579 | * key options. |
| 580 | */ |
| 581 | line_opts = NULL; |
| 582 | if ((ep = strrchr(cp, ' ')) != NULL || |
| 583 | (ep = strrchr(cp, '\t')) != NULL) { |
| 584 | for (; *ep == ' ' || *ep == '\t'; ep++) |
Damien Miller | 188ea81 | 2010-12-01 11:50:14 +1100 | [diff] [blame] | 585 | ; |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 586 | line_opts = cp; |
| 587 | cp = ep; |
| 588 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 589 | for (i = 0; i < cert->nprincipals; i++) { |
| 590 | if (strcmp(cp, cert->principals[i]) == 0) { |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 591 | debug3("%s:%lu: matched principal \"%.100s\"", |
| 592 | file == NULL ? "(command)" : file, |
| 593 | linenum, cert->principals[i]); |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 594 | if (auth_parse_options(pw, line_opts, |
| 595 | file, linenum) != 1) |
| 596 | continue; |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 597 | return 1; |
| 598 | } |
| 599 | } |
| 600 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 601 | return 0; |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 602 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 603 | |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 604 | static int |
| 605 | match_principals_file(char *file, struct passwd *pw, struct sshkey_cert *cert) |
| 606 | { |
| 607 | FILE *f; |
| 608 | int success; |
| 609 | |
| 610 | temporarily_use_uid(pw); |
| 611 | debug("trying authorized principals file %s", file); |
| 612 | if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) { |
| 613 | restore_uid(); |
| 614 | return 0; |
| 615 | } |
| 616 | success = process_principals(f, file, pw, cert); |
| 617 | fclose(f); |
| 618 | restore_uid(); |
| 619 | return success; |
| 620 | } |
| 621 | |
| 622 | /* |
| 623 | * Checks whether principal is allowed in output of command. |
| 624 | * returns 1 if the principal is allowed or 0 otherwise. |
| 625 | */ |
| 626 | static int |
jsing@openbsd.org | aff3e94 | 2015-06-15 18:42:19 +0000 | [diff] [blame^] | 627 | match_principals_command(struct passwd *user_pw, struct sshkey_cert *cert) |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 628 | { |
| 629 | FILE *f = NULL; |
| 630 | int ok, found_principal = 0; |
| 631 | struct passwd *pw; |
| 632 | int i, ac = 0, uid_swapped = 0; |
| 633 | pid_t pid; |
| 634 | char *tmp, *username = NULL, *command = NULL, **av = NULL; |
| 635 | void (*osigchld)(int); |
| 636 | |
| 637 | if (options.authorized_principals_command == NULL) |
| 638 | return 0; |
| 639 | if (options.authorized_principals_command_user == NULL) { |
| 640 | error("No user for AuthorizedPrincipalsCommand specified, " |
| 641 | "skipping"); |
| 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | /* |
| 646 | * NB. all returns later this function should go via "out" to |
| 647 | * ensure the original SIGCHLD handler is restored properly. |
| 648 | */ |
| 649 | osigchld = signal(SIGCHLD, SIG_DFL); |
| 650 | |
| 651 | /* Prepare and verify the user for the command */ |
| 652 | username = percent_expand(options.authorized_principals_command_user, |
| 653 | "u", user_pw->pw_name, (char *)NULL); |
| 654 | pw = getpwnam(username); |
| 655 | if (pw == NULL) { |
| 656 | error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s", |
| 657 | username, strerror(errno)); |
| 658 | goto out; |
| 659 | } |
| 660 | |
| 661 | /* Turn the command into an argument vector */ |
| 662 | if (split_argv(options.authorized_principals_command, &ac, &av) != 0) { |
| 663 | error("AuthorizedPrincipalsCommand \"%s\" contains " |
| 664 | "invalid quotes", command); |
| 665 | goto out; |
| 666 | } |
| 667 | if (ac == 0) { |
| 668 | error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments", |
| 669 | command); |
| 670 | goto out; |
| 671 | } |
| 672 | for (i = 1; i < ac; i++) { |
| 673 | tmp = percent_expand(av[i], |
| 674 | "u", user_pw->pw_name, |
| 675 | "h", user_pw->pw_dir, |
| 676 | (char *)NULL); |
| 677 | if (tmp == NULL) |
| 678 | fatal("%s: percent_expand failed", __func__); |
| 679 | free(av[i]); |
| 680 | av[i] = tmp; |
| 681 | } |
| 682 | /* Prepare a printable command for logs, etc. */ |
| 683 | command = assemble_argv(ac, av); |
| 684 | |
| 685 | if ((pid = subprocess("AuthorizedPrincipalsCommand", pw, command, |
| 686 | ac, av, &f)) == 0) |
| 687 | goto out; |
| 688 | |
| 689 | uid_swapped = 1; |
| 690 | temporarily_use_uid(pw); |
| 691 | |
jsing@openbsd.org | aff3e94 | 2015-06-15 18:42:19 +0000 | [diff] [blame^] | 692 | ok = process_principals(f, NULL, pw, cert); |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 693 | |
| 694 | if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command) != 0) |
| 695 | goto out; |
| 696 | |
| 697 | /* Read completed successfully */ |
| 698 | found_principal = ok; |
| 699 | out: |
| 700 | if (f != NULL) |
| 701 | fclose(f); |
| 702 | signal(SIGCHLD, osigchld); |
| 703 | for (i = 0; i < ac; i++) |
| 704 | free(av[i]); |
| 705 | free(av); |
| 706 | if (uid_swapped) |
| 707 | restore_uid(); |
| 708 | free(command); |
| 709 | free(username); |
| 710 | return found_principal; |
| 711 | } |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 712 | /* |
| 713 | * Checks whether key is allowed in authorized_keys-format file, |
| 714 | * returns 1 if the key is allowed or 0 otherwise. |
| 715 | */ |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 716 | static int |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 717 | check_authkeys_file(FILE *f, char *file, Key* key, struct passwd *pw) |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 718 | { |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 719 | char line[SSH_MAX_PUBKEY_BYTES]; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 720 | const char *reason; |
Darren Tucker | 33c787f | 2008-07-02 22:37:30 +1000 | [diff] [blame] | 721 | int found_key = 0; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 722 | u_long linenum = 0; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 723 | Key *found; |
| 724 | char *fp; |
| 725 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 726 | found_key = 0; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 727 | |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 728 | found = NULL; |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 729 | while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 730 | char *cp, *key_options = NULL; |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 731 | if (found != NULL) |
| 732 | key_free(found); |
| 733 | found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 734 | auth_clear_options(); |
| 735 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 736 | /* Skip leading whitespace, empty and comment lines. */ |
| 737 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 738 | ; |
| 739 | if (!*cp || *cp == '\n' || *cp == '#') |
| 740 | continue; |
| 741 | |
| 742 | if (key_read(found, &cp) != 1) { |
| 743 | /* no key? check if there are options for this key */ |
| 744 | int quoted = 0; |
| 745 | debug2("user_key_allowed: check options: '%s'", cp); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 746 | key_options = cp; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 747 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { |
| 748 | if (*cp == '\\' && cp[1] == '"') |
| 749 | cp++; /* Skip both */ |
| 750 | else if (*cp == '"') |
| 751 | quoted = !quoted; |
| 752 | } |
| 753 | /* Skip remaining whitespace. */ |
| 754 | for (; *cp == ' ' || *cp == '\t'; cp++) |
| 755 | ; |
| 756 | if (key_read(found, &cp) != 1) { |
| 757 | debug2("user_key_allowed: advance: '%s'", cp); |
| 758 | /* still no key? advance to next line*/ |
| 759 | continue; |
| 760 | } |
| 761 | } |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 762 | if (key_is_cert(key)) { |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 763 | if (!key_equal(found, key->cert->signature_key)) |
| 764 | continue; |
Damien Miller | 8439955 | 2010-05-21 14:58:12 +1000 | [diff] [blame] | 765 | if (auth_parse_options(pw, key_options, file, |
| 766 | linenum) != 1) |
| 767 | continue; |
| 768 | if (!key_is_cert_authority) |
| 769 | continue; |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 770 | if ((fp = sshkey_fingerprint(found, |
| 771 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) |
| 772 | continue; |
Damien Miller | e513a91 | 2010-03-22 05:51:21 +1100 | [diff] [blame] | 773 | debug("matching CA found: file %s, line %lu, %s %s", |
| 774 | file, linenum, key_type(found), fp); |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 775 | /* |
| 776 | * If the user has specified a list of principals as |
| 777 | * a key option, then prefer that list to matching |
| 778 | * their username in the certificate principals list. |
| 779 | */ |
| 780 | if (authorized_principals != NULL && |
| 781 | !match_principals_option(authorized_principals, |
| 782 | key->cert)) { |
| 783 | reason = "Certificate does not contain an " |
| 784 | "authorized principal"; |
| 785 | fail_reason: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 786 | free(fp); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 787 | error("%s", reason); |
| 788 | auth_debug_add("%s", reason); |
| 789 | continue; |
| 790 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 791 | if (key_cert_check_authority(key, 0, 0, |
| 792 | authorized_principals == NULL ? pw->pw_name : NULL, |
| 793 | &reason) != 0) |
| 794 | goto fail_reason; |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 795 | if (auth_cert_options(key, pw) != 0) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 796 | free(fp); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 797 | continue; |
Damien Miller | e513a91 | 2010-03-22 05:51:21 +1100 | [diff] [blame] | 798 | } |
| 799 | verbose("Accepted certificate ID \"%s\" " |
| 800 | "signed by %s CA %s via %s", key->cert->key_id, |
| 801 | key_type(found), fp, file); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 802 | free(fp); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 803 | found_key = 1; |
| 804 | break; |
Damien Miller | 8439955 | 2010-05-21 14:58:12 +1000 | [diff] [blame] | 805 | } else if (key_equal(found, key)) { |
| 806 | if (auth_parse_options(pw, key_options, file, |
| 807 | linenum) != 1) |
| 808 | continue; |
| 809 | if (key_is_cert_authority) |
| 810 | continue; |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 811 | if ((fp = sshkey_fingerprint(found, |
| 812 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) |
| 813 | continue; |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 814 | debug("matching key found: file %s, line %lu %s %s", |
| 815 | file, linenum, key_type(found), fp); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 816 | free(fp); |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 817 | found_key = 1; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 818 | break; |
| 819 | } |
| 820 | } |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 821 | if (found != NULL) |
| 822 | key_free(found); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 823 | if (!found_key) |
| 824 | debug2("key not found"); |
| 825 | return found_key; |
| 826 | } |
| 827 | |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 828 | /* Authenticate a certificate key against TrustedUserCAKeys */ |
| 829 | static int |
| 830 | user_cert_trusted_ca(struct passwd *pw, Key *key) |
| 831 | { |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 832 | char *ca_fp, *principals_file = NULL; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 833 | const char *reason; |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 834 | int ret = 0, found_principal = 0; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 835 | |
| 836 | if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL) |
| 837 | return 0; |
| 838 | |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 839 | if ((ca_fp = sshkey_fingerprint(key->cert->signature_key, |
| 840 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) |
| 841 | return 0; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 842 | |
djm@openbsd.org | 5e39a49 | 2014-12-04 02:24:32 +0000 | [diff] [blame] | 843 | if (sshkey_in_file(key->cert->signature_key, |
| 844 | options.trusted_user_ca_keys, 1, 0) != 0) { |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 845 | debug2("%s: CA %s %s is not listed in %s", __func__, |
| 846 | key_type(key->cert->signature_key), ca_fp, |
| 847 | options.trusted_user_ca_keys); |
| 848 | goto out; |
| 849 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 850 | /* |
| 851 | * If AuthorizedPrincipals is in use, then compare the certificate |
| 852 | * principals against the names in that file rather than matching |
| 853 | * against the username. |
| 854 | */ |
| 855 | if ((principals_file = authorized_principals_file(pw)) != NULL) { |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 856 | if (match_principals_file(principals_file, pw, key->cert)) |
| 857 | found_principal = 1; |
| 858 | } |
| 859 | /* Try querying command if specified */ |
jsing@openbsd.org | aff3e94 | 2015-06-15 18:42:19 +0000 | [diff] [blame^] | 860 | if (!found_principal && match_principals_command(pw, key->cert)) |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 861 | found_principal = 1; |
| 862 | /* If principals file or command specify, then require a match here */ |
| 863 | if (!found_principal && (principals_file != NULL || |
| 864 | options.authorized_principals_command != NULL)) { |
| 865 | reason = "Certificate does not contain an authorized principal"; |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 866 | fail_reason: |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 867 | error("%s", reason); |
| 868 | auth_debug_add("%s", reason); |
| 869 | goto out; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 870 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 871 | if (key_cert_check_authority(key, 0, 1, |
| 872 | principals_file == NULL ? pw->pw_name : NULL, &reason) != 0) |
| 873 | goto fail_reason; |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 874 | if (auth_cert_options(key, pw) != 0) |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 875 | goto out; |
| 876 | |
Damien Miller | e513a91 | 2010-03-22 05:51:21 +1100 | [diff] [blame] | 877 | verbose("Accepted certificate ID \"%s\" signed by %s CA %s via %s", |
| 878 | key->cert->key_id, key_type(key->cert->signature_key), ca_fp, |
| 879 | options.trusted_user_ca_keys); |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 880 | ret = 1; |
| 881 | |
| 882 | out: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 883 | free(principals_file); |
| 884 | free(ca_fp); |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 885 | return ret; |
| 886 | } |
| 887 | |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 888 | /* |
| 889 | * Checks whether key is allowed in file. |
| 890 | * returns 1 if the key is allowed or 0 otherwise. |
| 891 | */ |
| 892 | static int |
| 893 | user_key_allowed2(struct passwd *pw, Key *key, char *file) |
| 894 | { |
| 895 | FILE *f; |
| 896 | int found_key = 0; |
| 897 | |
| 898 | /* Temporarily use the user's uid. */ |
| 899 | temporarily_use_uid(pw); |
| 900 | |
| 901 | debug("trying public key file %s", file); |
| 902 | if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) { |
| 903 | found_key = check_authkeys_file(f, file, key, pw); |
| 904 | fclose(f); |
| 905 | } |
| 906 | |
| 907 | restore_uid(); |
| 908 | return found_key; |
| 909 | } |
| 910 | |
| 911 | /* |
| 912 | * Checks whether key is allowed in output of command. |
| 913 | * returns 1 if the key is allowed or 0 otherwise. |
| 914 | */ |
| 915 | static int |
| 916 | user_key_command_allowed2(struct passwd *user_pw, Key *key) |
| 917 | { |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 918 | FILE *f = NULL; |
| 919 | int r, ok, found_key = 0; |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 920 | struct passwd *pw; |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 921 | int i, uid_swapped = 0, ac = 0; |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 922 | pid_t pid; |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 923 | char *username = NULL, *key_fp = NULL, *keytext = NULL; |
| 924 | char *tmp, *command = NULL, **av = NULL; |
| 925 | void (*osigchld)(int); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 926 | |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 927 | if (options.authorized_keys_command == NULL) |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 928 | return 0; |
Damien Miller | d0d1099 | 2012-11-04 22:23:14 +1100 | [diff] [blame] | 929 | if (options.authorized_keys_command_user == NULL) { |
| 930 | error("No user for AuthorizedKeysCommand specified, skipping"); |
| 931 | return 0; |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 932 | } |
| 933 | |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 934 | /* |
| 935 | * NB. all returns later this function should go via "out" to |
| 936 | * ensure the original SIGCHLD handler is restored properly. |
| 937 | */ |
| 938 | osigchld = signal(SIGCHLD, SIG_DFL); |
| 939 | |
| 940 | /* Prepare and verify the user for the command */ |
Damien Miller | d0d1099 | 2012-11-04 22:23:14 +1100 | [diff] [blame] | 941 | username = percent_expand(options.authorized_keys_command_user, |
| 942 | "u", user_pw->pw_name, (char *)NULL); |
| 943 | pw = getpwnam(username); |
| 944 | if (pw == NULL) { |
Damien Miller | 4018dc0 | 2013-02-15 10:28:55 +1100 | [diff] [blame] | 945 | error("AuthorizedKeysCommandUser \"%s\" not found: %s", |
| 946 | username, strerror(errno)); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 947 | goto out; |
| 948 | } |
| 949 | |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 950 | /* Prepare AuthorizedKeysCommand */ |
| 951 | if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash, |
| 952 | SSH_FP_DEFAULT)) == NULL) { |
| 953 | error("%s: sshkey_fingerprint failed", __func__); |
| 954 | goto out; |
| 955 | } |
| 956 | if ((r = sshkey_to_base64(key, &keytext)) != 0) { |
| 957 | error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r)); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 958 | goto out; |
| 959 | } |
| 960 | |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 961 | /* Turn the command into an argument vector */ |
| 962 | if (split_argv(options.authorized_keys_command, &ac, &av) != 0) { |
| 963 | error("AuthorizedKeysCommand \"%s\" contains invalid quotes", |
| 964 | command); |
| 965 | goto out; |
| 966 | } |
| 967 | if (ac == 0) { |
| 968 | error("AuthorizedKeysCommand \"%s\" yielded no arguments", |
| 969 | command); |
| 970 | goto out; |
| 971 | } |
| 972 | for (i = 1; i < ac; i++) { |
| 973 | tmp = percent_expand(av[i], |
| 974 | "u", user_pw->pw_name, |
| 975 | "h", user_pw->pw_dir, |
| 976 | "t", sshkey_ssh_name(key), |
| 977 | "f", key_fp, |
| 978 | "k", keytext, |
| 979 | (char *)NULL); |
| 980 | if (tmp == NULL) |
| 981 | fatal("%s: percent_expand failed", __func__); |
| 982 | free(av[i]); |
| 983 | av[i] = tmp; |
| 984 | } |
| 985 | /* Prepare a printable command for logs, etc. */ |
| 986 | command = assemble_argv(ac, av); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 987 | |
| 988 | /* |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 989 | * If AuthorizedKeysCommand was run without arguments |
| 990 | * then fall back to the old behaviour of passing the |
| 991 | * target username as a single argument. |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 992 | */ |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 993 | if (ac == 1) { |
| 994 | av = xreallocarray(av, ac + 2, sizeof(*av)); |
| 995 | av[1] = xstrdup(user_pw->pw_name); |
| 996 | av[2] = NULL; |
| 997 | /* Fix up command too, since it is used in log messages */ |
| 998 | free(command); |
| 999 | xasprintf(&command, "%s %s", av[0], av[1]); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1000 | } |
| 1001 | |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 1002 | if ((pid = subprocess("AuthorizedKeysCommand", pw, command, |
| 1003 | ac, av, &f)) == 0) |
| 1004 | goto out; |
| 1005 | |
| 1006 | uid_swapped = 1; |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1007 | temporarily_use_uid(pw); |
| 1008 | |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1009 | ok = check_authkeys_file(f, options.authorized_keys_command, key, pw); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1010 | |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 1011 | if (exited_cleanly(pid, "AuthorizedKeysCommand", command) != 0) |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1012 | goto out; |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 1013 | |
| 1014 | /* Read completed successfully */ |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1015 | found_key = ok; |
| 1016 | out: |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 1017 | if (f != NULL) |
| 1018 | fclose(f); |
| 1019 | signal(SIGCHLD, osigchld); |
| 1020 | for (i = 0; i < ac; i++) |
| 1021 | free(av[i]); |
| 1022 | free(av); |
| 1023 | if (uid_swapped) |
| 1024 | restore_uid(); |
| 1025 | free(command); |
| 1026 | free(username); |
| 1027 | free(key_fp); |
| 1028 | free(keytext); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1029 | return found_key; |
| 1030 | } |
| 1031 | |
| 1032 | /* |
| 1033 | * Check whether key authenticates and authorises the user. |
| 1034 | */ |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 1035 | int |
djm@openbsd.org | 179be0f | 2015-05-01 03:23:51 +0000 | [diff] [blame] | 1036 | user_key_allowed(struct passwd *pw, Key *key, int auth_attempt) |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 1037 | { |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 1038 | u_int success, i; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 1039 | char *file; |
| 1040 | |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 1041 | if (auth_key_is_revoked(key)) |
| 1042 | return 0; |
| 1043 | if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key)) |
| 1044 | return 0; |
| 1045 | |
| 1046 | success = user_cert_trusted_ca(pw, key); |
| 1047 | if (success) |
| 1048 | return success; |
| 1049 | |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1050 | success = user_key_command_allowed2(pw, key); |
| 1051 | if (success > 0) |
| 1052 | return success; |
| 1053 | |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 1054 | for (i = 0; !success && i < options.num_authkeys_files; i++) { |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1055 | |
| 1056 | if (strcasecmp(options.authorized_keys_files[i], "none") == 0) |
| 1057 | continue; |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 1058 | file = expand_authorized_keys( |
| 1059 | options.authorized_keys_files[i], pw); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1060 | |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 1061 | success = user_key_allowed2(pw, key, file); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1062 | free(file); |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 1063 | } |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 1064 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 1065 | return success; |
| 1066 | } |
| 1067 | |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 1068 | /* Records a public key in the list of previously-successful keys */ |
| 1069 | void |
| 1070 | auth2_record_userkey(Authctxt *authctxt, struct sshkey *key) |
| 1071 | { |
| 1072 | struct sshkey **tmp; |
| 1073 | |
| 1074 | if (authctxt->nprev_userkeys >= INT_MAX || |
| 1075 | (tmp = reallocarray(authctxt->prev_userkeys, |
| 1076 | authctxt->nprev_userkeys + 1, sizeof(*tmp))) == NULL) |
| 1077 | fatal("%s: reallocarray failed", __func__); |
| 1078 | authctxt->prev_userkeys = tmp; |
| 1079 | authctxt->prev_userkeys[authctxt->nprev_userkeys] = key; |
| 1080 | authctxt->nprev_userkeys++; |
| 1081 | } |
| 1082 | |
| 1083 | /* Checks whether a key has already been used successfully for authentication */ |
| 1084 | int |
| 1085 | auth2_userkey_already_used(Authctxt *authctxt, struct sshkey *key) |
| 1086 | { |
| 1087 | u_int i; |
| 1088 | |
| 1089 | for (i = 0; i < authctxt->nprev_userkeys; i++) { |
| 1090 | if (sshkey_equal_public(key, authctxt->prev_userkeys[i])) { |
| 1091 | return 1; |
| 1092 | } |
| 1093 | } |
| 1094 | return 0; |
| 1095 | } |
| 1096 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 1097 | Authmethod method_pubkey = { |
| 1098 | "publickey", |
| 1099 | userauth_pubkey, |
| 1100 | &options.pubkey_authentication |
| 1101 | }; |