| Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 1 | #include "config.h" |
| Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 2 | |
| Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 3 | #if defined(HAVE_LIBUNWIND) |
| 4 | #include <libunwind-ptrace.h> |
| 5 | #endif /* defined(HAVE_LIBUNWIND) */ |
| 6 | |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 7 | #include <stdio.h> |
| Juan Cespedes | 504a385 | 2003-02-04 23:24:38 +0100 | [diff] [blame] | 8 | #include <stdlib.h> |
| Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 9 | #include <sys/types.h> |
| 10 | #include <sys/stat.h> |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 11 | #include <unistd.h> |
| 12 | #include <errno.h> |
| 13 | #include <string.h> |
| Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 14 | #include <pwd.h> |
| 15 | #include <grp.h> |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 16 | |
| Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 17 | #include "common.h" |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 18 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 19 | static void |
| Petr Machata | 1b17dbf | 2011-07-08 19:22:52 +0200 | [diff] [blame] | 20 | change_uid(const char * command) |
| 21 | { |
| Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 22 | uid_t run_uid, run_euid; |
| 23 | gid_t run_gid, run_egid; |
| 24 | |
| Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 25 | if (options.user) { |
| Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 26 | struct passwd *pent; |
| 27 | |
| 28 | if (getuid() != 0 || geteuid() != 0) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 29 | fprintf(stderr, |
| 30 | "you must be root to use the -u option\n"); |
| Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 31 | exit(1); |
| 32 | } |
| Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 33 | if ((pent = getpwnam(options.user)) == NULL) { |
| 34 | fprintf(stderr, "cannot find user `%s'\n", options.user); |
| Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 35 | exit(1); |
| 36 | } |
| 37 | run_uid = pent->pw_uid; |
| 38 | run_gid = pent->pw_gid; |
| 39 | |
| Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 40 | if (initgroups(options.user, run_gid) < 0) { |
| Juan Cespedes | e3eb9aa | 1999-04-03 03:21:52 +0200 | [diff] [blame] | 41 | perror("ltrace: initgroups"); |
| Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 42 | exit(1); |
| 43 | } |
| 44 | } else { |
| 45 | run_uid = getuid(); |
| 46 | run_gid = getgid(); |
| 47 | } |
| Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 48 | if (options.user || !geteuid()) { |
| Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 49 | struct stat statbuf; |
| 50 | run_euid = run_uid; |
| 51 | run_egid = run_gid; |
| 52 | |
| Petr Machata | 1b17dbf | 2011-07-08 19:22:52 +0200 | [diff] [blame] | 53 | if (!stat(command, &statbuf)) { |
| Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 54 | if (statbuf.st_mode & S_ISUID) { |
| 55 | run_euid = statbuf.st_uid; |
| 56 | } |
| 57 | if (statbuf.st_mode & S_ISGID) { |
| Juan Cespedes | 666da8b | 1998-04-29 20:21:48 +0200 | [diff] [blame] | 58 | run_egid = statbuf.st_gid; |
| Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | if (setregid(run_gid, run_egid) < 0) { |
| Juan Cespedes | e3eb9aa | 1999-04-03 03:21:52 +0200 | [diff] [blame] | 62 | perror("ltrace: setregid"); |
| Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 63 | exit(1); |
| 64 | } |
| 65 | if (setreuid(run_uid, run_euid) < 0) { |
| Juan Cespedes | e3eb9aa | 1999-04-03 03:21:52 +0200 | [diff] [blame] | 66 | perror("ltrace: setreuid"); |
| Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 67 | exit(1); |
| 68 | } |
| 69 | } |
| 70 | } |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 71 | |
| Petr Machata | 1b17dbf | 2011-07-08 19:22:52 +0200 | [diff] [blame] | 72 | pid_t |
| 73 | execute_program(const char * command, char **argv) |
| 74 | { |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 75 | pid_t pid; |
| 76 | |
| Petr Machata | 1b17dbf | 2011-07-08 19:22:52 +0200 | [diff] [blame] | 77 | debug(1, "Executing `%s'...", command); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 78 | |
| 79 | pid = fork(); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 80 | if (pid < 0) { |
| Petr Machata | c805c62 | 2012-03-02 00:10:37 +0100 | [diff] [blame^] | 81 | fail: |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 82 | perror("ltrace: fork"); |
| 83 | exit(1); |
| 84 | } else if (!pid) { /* child */ |
| Petr Machata | 1b17dbf | 2011-07-08 19:22:52 +0200 | [diff] [blame] | 85 | change_uid(command); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 86 | trace_me(); |
| Petr Machata | 1b17dbf | 2011-07-08 19:22:52 +0200 | [diff] [blame] | 87 | execvp(command, argv); |
| 88 | fprintf(stderr, "Can't execute `%s': %s\n", command, |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 89 | strerror(errno)); |
| Juan Cespedes | e672ad1 | 2009-02-11 18:49:18 +0100 | [diff] [blame] | 90 | _exit(1); |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| Petr Machata | c805c62 | 2012-03-02 00:10:37 +0100 | [diff] [blame^] | 93 | if (wait_for_proc(pid) < 0) |
| 94 | goto fail; |
| Petr Machata | b4f9e0c | 2012-02-07 01:57:59 +0100 | [diff] [blame] | 95 | |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 96 | debug(1, "PID=%d", pid); |
| Petr Machata | 1b17dbf | 2011-07-08 19:22:52 +0200 | [diff] [blame] | 97 | return pid; |
| Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 98 | } |