|
Lines 186-212
done:
Link Here
|
| 186 |
|
186 |
|
| 187 |
/* return 1 if user allows given key */ |
187 |
/* return 1 if user allows given key */ |
| 188 |
static int |
188 |
static int |
| 189 |
user_key_allowed2(struct passwd *pw, Key *key, char *file) |
189 |
user_search_key_in_file(FILE *f, char *file, Key* key, struct passwd *pw) |
| 190 |
{ |
190 |
{ |
| 191 |
char line[SSH_MAX_PUBKEY_BYTES]; |
191 |
char line[SSH_MAX_PUBKEY_BYTES]; |
| 192 |
const char *reason; |
192 |
const char *reason; |
| 193 |
int found_key = 0; |
193 |
int found_key = 0; |
| 194 |
FILE *f; |
|
|
| 195 |
u_long linenum = 0; |
194 |
u_long linenum = 0; |
| 196 |
Key *found; |
195 |
Key *found; |
| 197 |
char *fp; |
196 |
char *fp; |
| 198 |
|
197 |
|
| 199 |
/* Temporarily use the user's uid. */ |
|
|
| 200 |
temporarily_use_uid(pw); |
| 201 |
|
| 202 |
debug("trying public key file %s", file); |
| 203 |
f = auth_openkeyfile(file, pw, options.strict_modes); |
| 204 |
|
| 205 |
if (!f) { |
| 206 |
restore_uid(); |
| 207 |
return 0; |
| 208 |
} |
| 209 |
|
| 210 |
found_key = 0; |
198 |
found_key = 0; |
| 211 |
found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type); |
199 |
found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type); |
| 212 |
|
200 |
|
|
Lines 277-297
user_key_allowed2(struct passwd *pw, Key
Link Here
|
| 277 |
break; |
265 |
break; |
| 278 |
} |
266 |
} |
| 279 |
} |
267 |
} |
| 280 |
restore_uid(); |
|
|
| 281 |
fclose(f); |
| 282 |
key_free(found); |
268 |
key_free(found); |
| 283 |
if (!found_key) |
269 |
if (!found_key) |
| 284 |
debug2("key not found"); |
270 |
debug2("key not found"); |
| 285 |
return found_key; |
271 |
return found_key; |
| 286 |
} |
272 |
} |
| 287 |
|
273 |
|
| 288 |
/* check whether given key is in .ssh/authorized_keys* */ |
274 |
|
|
|
275 |
/* return 1 if user allows given key */ |
| 276 |
static int |
| 277 |
user_key_allowed2(struct passwd *pw, Key *key, char *file) |
| 278 |
{ |
| 279 |
FILE *f; |
| 280 |
int found_key = 0; |
| 281 |
|
| 282 |
/* Temporarily use the user's uid. */ |
| 283 |
temporarily_use_uid(pw); |
| 284 |
|
| 285 |
debug("trying public key file %s", file); |
| 286 |
f = auth_openkeyfile(file, pw, options.strict_modes); |
| 287 |
|
| 288 |
if (f) { |
| 289 |
found_key = user_search_key_in_file (f, file, key, pw); |
| 290 |
fclose(f); |
| 291 |
} |
| 292 |
|
| 293 |
restore_uid(); |
| 294 |
return found_key; |
| 295 |
} |
| 296 |
|
| 297 |
#ifdef WITH_PUBKEY_AGENT |
| 298 |
|
| 299 |
#define WHITESPACE " \t\r\n" |
| 300 |
|
| 301 |
/* return 1 if user allows given key */ |
| 302 |
static int |
| 303 |
user_key_via_agent_allowed2(struct passwd *pw, Key *key) |
| 304 |
{ |
| 305 |
FILE *f; |
| 306 |
int found_key = 0; |
| 307 |
char *pubkey_agent_string = NULL; |
| 308 |
char *tmp_pubkey_agent_string = NULL; |
| 309 |
char *progname; |
| 310 |
char *cp; |
| 311 |
struct passwd *runas_pw; |
| 312 |
struct stat st; |
| 313 |
|
| 314 |
if (options.pubkey_agent == NULL || options.pubkey_agent[0] != '/') |
| 315 |
return -1; |
| 316 |
|
| 317 |
/* get the run as identity from config */ |
| 318 |
runas_pw = (options.pubkey_agent_runas == NULL)? pw |
| 319 |
: getpwnam (options.pubkey_agent_runas); |
| 320 |
if (!runas_pw) { |
| 321 |
error("%s: getpwnam(\"%s\"): %s", __func__, |
| 322 |
options.pubkey_agent_runas, strerror(errno)); |
| 323 |
return 0; |
| 324 |
} |
| 325 |
|
| 326 |
/* Temporarily use the specified uid. */ |
| 327 |
if (runas_pw->pw_uid != 0) |
| 328 |
temporarily_use_uid(runas_pw); |
| 329 |
|
| 330 |
pubkey_agent_string = percent_expand(options.pubkey_agent, |
| 331 |
"h", pw->pw_dir, "u", pw->pw_name, (char *)NULL); |
| 332 |
|
| 333 |
/* Test whether agent can be modified by non root user */ |
| 334 |
tmp_pubkey_agent_string = xstrdup (pubkey_agent_string); |
| 335 |
progname = strtok (tmp_pubkey_agent_string, WHITESPACE); |
| 336 |
|
| 337 |
debug3("%s: checking program '%s'", __func__, progname); |
| 338 |
|
| 339 |
if (stat (progname, &st) < 0) { |
| 340 |
error("%s: stat(\"%s\"): %s", __func__, |
| 341 |
progname, strerror(errno)); |
| 342 |
goto go_away; |
| 343 |
} |
| 344 |
|
| 345 |
if (st.st_uid != 0 || (st.st_mode & 022) != 0) { |
| 346 |
error("bad ownership or modes for pubkey agent \"%s\"", |
| 347 |
progname); |
| 348 |
goto go_away; |
| 349 |
} |
| 350 |
|
| 351 |
if (!S_ISREG(st.st_mode)) { |
| 352 |
error("pubkey agent \"%s\" is not a regular file", |
| 353 |
progname); |
| 354 |
goto go_away; |
| 355 |
} |
| 356 |
|
| 357 |
/* |
| 358 |
* Descend the path, checking that each component is a |
| 359 |
* root-owned directory with strict permissions. |
| 360 |
*/ |
| 361 |
do { |
| 362 |
if ((cp = strrchr(progname, '/')) == NULL) |
| 363 |
break; |
| 364 |
else |
| 365 |
*cp = '\0'; |
| 366 |
|
| 367 |
debug3("%s: checking component '%s'", __func__, progname); |
| 368 |
|
| 369 |
if (stat(progname, &st) != 0) { |
| 370 |
error("%s: stat(\"%s\"): %s", __func__, |
| 371 |
progname, strerror(errno)); |
| 372 |
goto go_away; |
| 373 |
} |
| 374 |
if (st.st_uid != 0 || (st.st_mode & 022) != 0) { |
| 375 |
error("bad ownership or modes for pubkey agent path component \"%s\"", |
| 376 |
progname); |
| 377 |
goto go_away; |
| 378 |
} |
| 379 |
if (!S_ISDIR(st.st_mode)) { |
| 380 |
error("pubkey agent path component \"%s\" is not a directory", |
| 381 |
progname); |
| 382 |
goto go_away; |
| 383 |
} |
| 384 |
} while (0); |
| 385 |
|
| 386 |
/* open the pipe and read the keys */ |
| 387 |
f = popen (pubkey_agent_string, "r"); |
| 388 |
if (!f) { |
| 389 |
error("%s: popen (\"%s\", \"r\"): %s", __func__, |
| 390 |
pubkey_agent_string, strerror (errno)); |
| 391 |
goto go_away; |
| 392 |
} |
| 393 |
|
| 394 |
found_key = user_search_key_in_file (f, options.pubkey_agent, key, pw); |
| 395 |
pclose (f); |
| 396 |
|
| 397 |
go_away: |
| 398 |
if (tmp_pubkey_agent_string) |
| 399 |
xfree (tmp_pubkey_agent_string); |
| 400 |
if (pubkey_agent_string) |
| 401 |
xfree (pubkey_agent_string); |
| 402 |
|
| 403 |
if (runas_pw->pw_uid != 0) |
| 404 |
restore_uid(); |
| 405 |
return found_key; |
| 406 |
} |
| 407 |
#endif |
| 408 |
|
| 409 |
/* check whether given key is in <pkey_agent or .ssh/authorized_keys* */ |
| 289 |
int |
410 |
int |
| 290 |
user_key_allowed(struct passwd *pw, Key *key) |
411 |
user_key_allowed(struct passwd *pw, Key *key) |
| 291 |
{ |
412 |
{ |
| 292 |
int success; |
413 |
int success; |
| 293 |
char *file; |
414 |
char *file; |
| 294 |
|
415 |
|
|
|
416 |
#ifdef WITH_PUBKEY_AGENT |
| 417 |
success = user_key_via_agent_allowed2(pw, key); |
| 418 |
if (success >= 0) |
| 419 |
return success; |
| 420 |
#endif |
| 421 |
|
| 295 |
file = authorized_keys_file(pw); |
422 |
file = authorized_keys_file(pw); |
| 296 |
success = user_key_allowed2(pw, key, file); |
423 |
success = user_key_allowed2(pw, key, file); |
| 297 |
xfree(file); |
424 |
xfree(file); |