| 25 | /* chroot_uid - restrict the damage that this program can do */ |
| 26 | |
| 27 | int acl_chroot_uid(const char *root_dir, const char *user_name) |
| 28 | { |
| 29 | struct passwd *pwd; |
| 30 | uid_t uid = 0; |
| 31 | gid_t gid; |
| 32 | int err = 0; |
| 33 | |
| 34 | /* |
| 35 | * Look up the uid/gid before entering the jail, and save them so they |
| 36 | * can't be clobbered. Set up the primary and secondary groups. |
| 37 | */ |
| 38 | if (user_name != 0) { |
| 39 | if ((pwd = getpwnam(user_name)) == 0) { |
| 40 | acl_msg_error("unknown user: %s", user_name); |
| 41 | return -1; |
| 42 | } |
| 43 | |
| 44 | uid = pwd->pw_uid; |
| 45 | gid = pwd->pw_gid; |
| 46 | if (setgid(gid) < 0) { |
| 47 | acl_msg_error("setgid(%ld): %s", (long) gid, |
| 48 | acl_last_serror()); |
| 49 | err++; |
| 50 | } |
| 51 | if (initgroups(user_name, gid) < 0) { |
| 52 | acl_msg_error("initgroups: %s", acl_last_serror()); |
| 53 | err++; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Enter the jail. |
| 59 | */ |
| 60 | if (root_dir) { |
| 61 | if (chroot(root_dir)) { |
| 62 | acl_msg_error("chroot(%s): %s", |
| 63 | root_dir, acl_last_serror()); |
| 64 | err++; |
| 65 | } else if (chdir("/")) { |
| 66 | acl_msg_error("chdir(/): %s", acl_last_serror()); |
| 67 | err++; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * Drop the user privileges. |
| 73 | */ |
| 74 | if (user_name != 0) { |
| 75 | if (setuid(uid) < 0) { |
| 76 | acl_msg_error("setuid(%ld): %s", |
| 77 | (long) uid, acl_last_serror()); |
| 78 | err++; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Give the desperate developer a clue of what is happening. |
| 84 | */ |
no test coverage detected
searching dependent graphs…