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