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