auth2-pubkey.c (207319) | auth2-pubkey.c (215116) |
---|---|
1/* $OpenBSD: auth2-pubkey.c,v 1.22 2010/03/10 23:27:17 djm Exp $ */ | 1/* $OpenBSD: auth2-pubkey.c,v 1.26 2010/06/29 23:16:46 djm Exp $ */ |
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. --- 42 unchanged lines hidden (view full) --- 52#include "auth-options.h" 53#include "canohost.h" 54#ifdef GSSAPI 55#include "ssh-gss.h" 56#endif 57#include "monitor_wrap.h" 58#include "misc.h" 59#include "authfile.h" | 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. --- 42 unchanged lines hidden (view full) --- 52#include "auth-options.h" 53#include "canohost.h" 54#ifdef GSSAPI 55#include "ssh-gss.h" 56#endif 57#include "monitor_wrap.h" 58#include "misc.h" 59#include "authfile.h" |
60#include "match.h" |
|
60 61/* import */ 62extern ServerOptions options; 63extern u_char *session_id2; 64extern u_int session_id2_len; 65 66static int 67userauth_pubkey(Authctxt *authctxt) --- 103 unchanged lines hidden (view full) --- 171 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg); 172 if (key != NULL) 173 key_free(key); 174 xfree(pkalg); 175 xfree(pkblob); 176 return authenticated; 177} 178 | 61 62/* import */ 63extern ServerOptions options; 64extern u_char *session_id2; 65extern u_int session_id2_len; 66 67static int 68userauth_pubkey(Authctxt *authctxt) --- 103 unchanged lines hidden (view full) --- 172 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg); 173 if (key != NULL) 174 key_free(key); 175 xfree(pkalg); 176 xfree(pkblob); 177 return authenticated; 178} 179 |
180static int 181match_principals_option(const char *principal_list, struct KeyCert *cert) 182{ 183 char *result; 184 u_int i; 185 186 /* XXX percent_expand() sequences for authorized_principals? */ 187 188 for (i = 0; i < cert->nprincipals; i++) { 189 if ((result = match_list(cert->principals[i], 190 principal_list, NULL)) != NULL) { 191 debug3("matched principal from key options \"%.100s\"", 192 result); 193 xfree(result); 194 return 1; 195 } 196 } 197 return 0; 198} 199 200static int 201match_principals_file(char *file, struct passwd *pw, struct KeyCert *cert) 202{ 203 FILE *f; 204 char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts; 205 u_long linenum = 0; 206 u_int i; 207 208 temporarily_use_uid(pw); 209 debug("trying authorized principals file %s", file); 210 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) { 211 restore_uid(); 212 return 0; 213 } 214 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) { 215 /* Skip leading whitespace. */ 216 for (cp = line; *cp == ' ' || *cp == '\t'; cp++) 217 ; 218 /* Skip blank and comment lines. */ 219 if ((ep = strchr(cp, '#')) != NULL) 220 *ep = '\0'; 221 if (!*cp || *cp == '\n') 222 continue; 223 /* Trim trailing whitespace. */ 224 ep = cp + strlen(cp) - 1; 225 while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t')) 226 *ep-- = '\0'; 227 /* 228 * If the line has internal whitespace then assume it has 229 * key options. 230 */ 231 line_opts = NULL; 232 if ((ep = strrchr(cp, ' ')) != NULL || 233 (ep = strrchr(cp, '\t')) != NULL) { 234 for (; *ep == ' ' || *ep == '\t'; ep++) 235 ;; 236 line_opts = cp; 237 cp = ep; 238 } 239 for (i = 0; i < cert->nprincipals; i++) { 240 if (strcmp(cp, cert->principals[i]) == 0) { 241 debug3("matched principal from file \"%.100s\"", 242 cert->principals[i]); 243 if (auth_parse_options(pw, line_opts, 244 file, linenum) != 1) 245 continue; 246 fclose(f); 247 restore_uid(); 248 return 1; 249 } 250 } 251 } 252 fclose(f); 253 restore_uid(); 254 return 0; 255} 256 |
|
179/* return 1 if user allows given key */ 180static int 181user_key_allowed2(struct passwd *pw, Key *key, char *file) 182{ 183 char line[SSH_MAX_PUBKEY_BYTES]; 184 const char *reason; 185 int found_key = 0; 186 FILE *f; --- 41 unchanged lines hidden (view full) --- 228 for (; *cp == ' ' || *cp == '\t'; cp++) 229 ; 230 if (key_read(found, &cp) != 1) { 231 debug2("user_key_allowed: advance: '%s'", cp); 232 /* still no key? advance to next line*/ 233 continue; 234 } 235 } | 257/* return 1 if user allows given key */ 258static int 259user_key_allowed2(struct passwd *pw, Key *key, char *file) 260{ 261 char line[SSH_MAX_PUBKEY_BYTES]; 262 const char *reason; 263 int found_key = 0; 264 FILE *f; --- 41 unchanged lines hidden (view full) --- 306 for (; *cp == ' ' || *cp == '\t'; cp++) 307 ; 308 if (key_read(found, &cp) != 1) { 309 debug2("user_key_allowed: advance: '%s'", cp); 310 /* still no key? advance to next line*/ 311 continue; 312 } 313 } |
236 if (auth_parse_options(pw, key_options, file, linenum) != 1) 237 continue; 238 if (key->type == KEY_RSA_CERT || key->type == KEY_DSA_CERT) { 239 if (!key_is_cert_authority) 240 continue; | 314 if (key_is_cert(key)) { |
241 if (!key_equal(found, key->cert->signature_key)) 242 continue; | 315 if (!key_equal(found, key->cert->signature_key)) 316 continue; |
317 if (auth_parse_options(pw, key_options, file, 318 linenum) != 1) 319 continue; 320 if (!key_is_cert_authority) 321 continue; |
|
243 fp = key_fingerprint(found, SSH_FP_MD5, 244 SSH_FP_HEX); 245 debug("matching CA found: file %s, line %lu, %s %s", 246 file, linenum, key_type(found), fp); | 322 fp = key_fingerprint(found, SSH_FP_MD5, 323 SSH_FP_HEX); 324 debug("matching CA found: file %s, line %lu, %s %s", 325 file, linenum, key_type(found), fp); |
247 if (key_cert_check_authority(key, 0, 0, pw->pw_name, 248 &reason) != 0) { | 326 /* 327 * If the user has specified a list of principals as 328 * a key option, then prefer that list to matching 329 * their username in the certificate principals list. 330 */ 331 if (authorized_principals != NULL && 332 !match_principals_option(authorized_principals, 333 key->cert)) { 334 reason = "Certificate does not contain an " 335 "authorized principal"; 336 fail_reason: |
249 xfree(fp); 250 error("%s", reason); 251 auth_debug_add("%s", reason); 252 continue; 253 } | 337 xfree(fp); 338 error("%s", reason); 339 auth_debug_add("%s", reason); 340 continue; 341 } |
254 if (auth_cert_constraints(&key->cert->constraints, 255 pw) != 0) { | 342 if (key_cert_check_authority(key, 0, 0, 343 authorized_principals == NULL ? pw->pw_name : NULL, 344 &reason) != 0) 345 goto fail_reason; 346 if (auth_cert_options(key, pw) != 0) { |
256 xfree(fp); 257 continue; 258 } 259 verbose("Accepted certificate ID \"%s\" " 260 "signed by %s CA %s via %s", key->cert->key_id, 261 key_type(found), fp, file); 262 xfree(fp); 263 found_key = 1; 264 break; | 347 xfree(fp); 348 continue; 349 } 350 verbose("Accepted certificate ID \"%s\" " 351 "signed by %s CA %s via %s", key->cert->key_id, 352 key_type(found), fp, file); 353 xfree(fp); 354 found_key = 1; 355 break; |
265 } else if (!key_is_cert_authority && key_equal(found, key)) { | 356 } else if (key_equal(found, key)) { 357 if (auth_parse_options(pw, key_options, file, 358 linenum) != 1) 359 continue; 360 if (key_is_cert_authority) 361 continue; |
266 found_key = 1; 267 debug("matching key found: file %s, line %lu", 268 file, linenum); 269 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX); 270 verbose("Found matching %s key: %s", 271 key_type(found), fp); 272 xfree(fp); 273 break; --- 6 unchanged lines hidden (view full) --- 280 debug2("key not found"); 281 return found_key; 282} 283 284/* Authenticate a certificate key against TrustedUserCAKeys */ 285static int 286user_cert_trusted_ca(struct passwd *pw, Key *key) 287{ | 362 found_key = 1; 363 debug("matching key found: file %s, line %lu", 364 file, linenum); 365 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX); 366 verbose("Found matching %s key: %s", 367 key_type(found), fp); 368 xfree(fp); 369 break; --- 6 unchanged lines hidden (view full) --- 376 debug2("key not found"); 377 return found_key; 378} 379 380/* Authenticate a certificate key against TrustedUserCAKeys */ 381static int 382user_cert_trusted_ca(struct passwd *pw, Key *key) 383{ |
288 char *ca_fp; | 384 char *ca_fp, *principals_file = NULL; |
289 const char *reason; 290 int ret = 0; 291 292 if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL) 293 return 0; 294 295 ca_fp = key_fingerprint(key->cert->signature_key, 296 SSH_FP_MD5, SSH_FP_HEX); 297 298 if (key_in_file(key->cert->signature_key, 299 options.trusted_user_ca_keys, 1) != 1) { 300 debug2("%s: CA %s %s is not listed in %s", __func__, 301 key_type(key->cert->signature_key), ca_fp, 302 options.trusted_user_ca_keys); 303 goto out; 304 } | 385 const char *reason; 386 int ret = 0; 387 388 if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL) 389 return 0; 390 391 ca_fp = key_fingerprint(key->cert->signature_key, 392 SSH_FP_MD5, SSH_FP_HEX); 393 394 if (key_in_file(key->cert->signature_key, 395 options.trusted_user_ca_keys, 1) != 1) { 396 debug2("%s: CA %s %s is not listed in %s", __func__, 397 key_type(key->cert->signature_key), ca_fp, 398 options.trusted_user_ca_keys); 399 goto out; 400 } |
305 if (key_cert_check_authority(key, 0, 1, pw->pw_name, &reason) != 0) { 306 error("%s", reason); 307 auth_debug_add("%s", reason); 308 goto out; | 401 /* 402 * If AuthorizedPrincipals is in use, then compare the certificate 403 * principals against the names in that file rather than matching 404 * against the username. 405 */ 406 if ((principals_file = authorized_principals_file(pw)) != NULL) { 407 if (!match_principals_file(principals_file, pw, key->cert)) { 408 reason = "Certificate does not contain an " 409 "authorized principal"; 410 fail_reason: 411 error("%s", reason); 412 auth_debug_add("%s", reason); 413 goto out; 414 } |
309 } | 415 } |
310 if (auth_cert_constraints(&key->cert->constraints, pw) != 0) | 416 if (key_cert_check_authority(key, 0, 1, 417 principals_file == NULL ? pw->pw_name : NULL, &reason) != 0) 418 goto fail_reason; 419 if (auth_cert_options(key, pw) != 0) |
311 goto out; 312 313 verbose("Accepted certificate ID \"%s\" signed by %s CA %s via %s", 314 key->cert->key_id, key_type(key->cert->signature_key), ca_fp, 315 options.trusted_user_ca_keys); 316 ret = 1; 317 318 out: | 420 goto out; 421 422 verbose("Accepted certificate ID \"%s\" signed by %s CA %s via %s", 423 key->cert->key_id, key_type(key->cert->signature_key), ca_fp, 424 options.trusted_user_ca_keys); 425 ret = 1; 426 427 out: |
428 if (principals_file != NULL) 429 xfree(principals_file); |
|
319 if (ca_fp != NULL) 320 xfree(ca_fp); 321 return ret; 322} 323 324/* check whether given key is in .ssh/authorized_keys* */ 325int 326user_key_allowed(struct passwd *pw, Key *key) --- 31 unchanged lines hidden --- | 430 if (ca_fp != NULL) 431 xfree(ca_fp); 432 return ret; 433} 434 435/* check whether given key is in .ssh/authorized_keys* */ 436int 437user_key_allowed(struct passwd *pw, Key *key) --- 31 unchanged lines hidden --- |