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
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.
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.
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
27 #include <sys/types.h>
40 change_uid(const char * command)
42 uid_t run_uid, run_euid;
43 gid_t run_gid, run_egid;
48 if (getuid() != 0 || geteuid() != 0) {
50 "you must be root to use the -u option\n");
53 if ((pent = getpwnam(options.user)) == NULL) {
54 fprintf(stderr, "cannot find user `%s'\n", options.user);
57 run_uid = pent->pw_uid;
58 run_gid = pent->pw_gid;
60 if (initgroups(options.user, run_gid) < 0) {
61 perror("ltrace: initgroups");
68 if (options.user || !geteuid()) {
73 if (!stat(command, &statbuf)) {
74 if (statbuf.st_mode & S_ISUID) {
75 run_euid = statbuf.st_uid;
77 if (statbuf.st_mode & S_ISGID) {
78 run_egid = statbuf.st_gid;
81 if (setregid(run_gid, run_egid) < 0) {
82 perror("ltrace: setregid");
85 if (setreuid(run_uid, run_euid) < 0) {
86 perror("ltrace: setreuid");
93 execute_program(const char * command, char **argv)
97 debug(1, "Executing `%s'...", command);
102 perror("ltrace: fork");
104 } else if (!pid) { /* child */
107 execvp(command, argv);
108 fprintf(stderr, "Can't execute `%s': %s\n", command,
113 if (wait_for_proc(pid) < 0)
116 debug(1, "PID=%d", pid);