Petr Machata | e99af27 | 2012-10-26 00:29:52 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of ltrace. |
| 3 | * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc. |
| 4 | * Copyright (C) 2010 Joe Damato |
| 5 | * Copyright (C) 1998,1999,2003,2008,2009 Juan Cespedes |
| 6 | * Copyright (C) 2006 Ian Wienand |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of the |
| 11 | * License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 21 | * 02110-1301 USA |
| 22 | */ |
Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 23 | #include "config.h" |
Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 24 | |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 25 | #include <stdio.h> |
Juan Cespedes | 504a385 | 2003-02-04 23:24:38 +0100 | [diff] [blame] | 26 | #include <stdlib.h> |
Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 27 | #include <sys/types.h> |
| 28 | #include <sys/stat.h> |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 29 | #include <unistd.h> |
| 30 | #include <errno.h> |
| 31 | #include <string.h> |
Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 32 | #include <pwd.h> |
| 33 | #include <grp.h> |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 34 | |
Petr Machata | 6426260 | 2012-01-07 03:41:36 +0100 | [diff] [blame] | 35 | #include "backend.h" |
| 36 | #include "options.h" |
| 37 | #include "debug.h" |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 38 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 39 | static void |
Petr Machata | 1b17dbf | 2011-07-08 19:22:52 +0200 | [diff] [blame] | 40 | change_uid(const char * command) |
| 41 | { |
Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 42 | uid_t run_uid, run_euid; |
| 43 | gid_t run_gid, run_egid; |
| 44 | |
Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 45 | if (options.user) { |
Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 46 | struct passwd *pent; |
| 47 | |
| 48 | if (getuid() != 0 || geteuid() != 0) { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 49 | fprintf(stderr, |
| 50 | "you must be root to use the -u option\n"); |
Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 51 | exit(1); |
| 52 | } |
Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 53 | if ((pent = getpwnam(options.user)) == NULL) { |
| 54 | fprintf(stderr, "cannot find user `%s'\n", options.user); |
Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 55 | exit(1); |
| 56 | } |
| 57 | run_uid = pent->pw_uid; |
| 58 | run_gid = pent->pw_gid; |
| 59 | |
Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 60 | if (initgroups(options.user, run_gid) < 0) { |
Juan Cespedes | e3eb9aa | 1999-04-03 03:21:52 +0200 | [diff] [blame] | 61 | perror("ltrace: initgroups"); |
Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 62 | exit(1); |
| 63 | } |
| 64 | } else { |
| 65 | run_uid = getuid(); |
| 66 | run_gid = getgid(); |
| 67 | } |
Juan Cespedes | ce377d5 | 2008-12-16 19:38:10 +0100 | [diff] [blame] | 68 | if (options.user || !geteuid()) { |
Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 69 | struct stat statbuf; |
| 70 | run_euid = run_uid; |
| 71 | run_egid = run_gid; |
| 72 | |
Petr Machata | 1b17dbf | 2011-07-08 19:22:52 +0200 | [diff] [blame] | 73 | if (!stat(command, &statbuf)) { |
Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 74 | if (statbuf.st_mode & S_ISUID) { |
| 75 | run_euid = statbuf.st_uid; |
| 76 | } |
| 77 | if (statbuf.st_mode & S_ISGID) { |
Juan Cespedes | 666da8b | 1998-04-29 20:21:48 +0200 | [diff] [blame] | 78 | run_egid = statbuf.st_gid; |
Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | if (setregid(run_gid, run_egid) < 0) { |
Juan Cespedes | e3eb9aa | 1999-04-03 03:21:52 +0200 | [diff] [blame] | 82 | perror("ltrace: setregid"); |
Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 83 | exit(1); |
| 84 | } |
| 85 | if (setreuid(run_uid, run_euid) < 0) { |
Juan Cespedes | e3eb9aa | 1999-04-03 03:21:52 +0200 | [diff] [blame] | 86 | perror("ltrace: setreuid"); |
Juan Cespedes | e188705 | 1998-03-10 00:08:41 +0100 | [diff] [blame] | 87 | exit(1); |
| 88 | } |
| 89 | } |
| 90 | } |
Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 91 | |
Petr Machata | 1b17dbf | 2011-07-08 19:22:52 +0200 | [diff] [blame] | 92 | pid_t |
| 93 | execute_program(const char * command, char **argv) |
| 94 | { |
Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 95 | pid_t pid; |
| 96 | |
Petr Machata | 1b17dbf | 2011-07-08 19:22:52 +0200 | [diff] [blame] | 97 | debug(1, "Executing `%s'...", command); |
Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 98 | |
| 99 | pid = fork(); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 100 | if (pid < 0) { |
Petr Machata | c805c62 | 2012-03-02 00:10:37 +0100 | [diff] [blame] | 101 | fail: |
Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 102 | perror("ltrace: fork"); |
| 103 | exit(1); |
| 104 | } else if (!pid) { /* child */ |
Petr Machata | 1b17dbf | 2011-07-08 19:22:52 +0200 | [diff] [blame] | 105 | change_uid(command); |
Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 106 | trace_me(); |
Petr Machata | 1b17dbf | 2011-07-08 19:22:52 +0200 | [diff] [blame] | 107 | execvp(command, argv); |
| 108 | fprintf(stderr, "Can't execute `%s': %s\n", command, |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 109 | strerror(errno)); |
Juan Cespedes | e672ad1 | 2009-02-11 18:49:18 +0100 | [diff] [blame] | 110 | _exit(1); |
Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Petr Machata | c805c62 | 2012-03-02 00:10:37 +0100 | [diff] [blame] | 113 | if (wait_for_proc(pid) < 0) |
| 114 | goto fail; |
Petr Machata | b4f9e0c | 2012-02-07 01:57:59 +0100 | [diff] [blame] | 115 | |
Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 116 | debug(1, "PID=%d", pid); |
Petr Machata | 1b17dbf | 2011-07-08 19:22:52 +0200 | [diff] [blame] | 117 | return pid; |
Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 118 | } |