blob: ba53ebf6332b48a4e7a54adc4bb3c6b242c45401 [file] [log] [blame]
Petr Machatae99af272012-10-26 00:29:52 +02001/*
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 Cespedesd44c6b81998-09-25 14:48:42 +020023#include "config.h"
Juan Cespedesd44c6b81998-09-25 14:48:42 +020024
Juan Cespedes5e01f651998-03-08 22:31:44 +010025#include <stdio.h>
Juan Cespedes504a3852003-02-04 23:24:38 +010026#include <stdlib.h>
Juan Cespedese1887051998-03-10 00:08:41 +010027#include <sys/types.h>
28#include <sys/stat.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010029#include <unistd.h>
30#include <errno.h>
31#include <string.h>
Juan Cespedese1887051998-03-10 00:08:41 +010032#include <pwd.h>
33#include <grp.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010034
Petr Machata64262602012-01-07 03:41:36 +010035#include "backend.h"
36#include "options.h"
37#include "debug.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010038
Juan Cespedesf1350522008-12-16 18:19:58 +010039static void
Petr Machata1b17dbf2011-07-08 19:22:52 +020040change_uid(const char * command)
41{
Juan Cespedese1887051998-03-10 00:08:41 +010042 uid_t run_uid, run_euid;
43 gid_t run_gid, run_egid;
44
Juan Cespedesce377d52008-12-16 19:38:10 +010045 if (options.user) {
Juan Cespedese1887051998-03-10 00:08:41 +010046 struct passwd *pent;
47
48 if (getuid() != 0 || geteuid() != 0) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010049 fprintf(stderr,
50 "you must be root to use the -u option\n");
Juan Cespedese1887051998-03-10 00:08:41 +010051 exit(1);
52 }
Juan Cespedesce377d52008-12-16 19:38:10 +010053 if ((pent = getpwnam(options.user)) == NULL) {
54 fprintf(stderr, "cannot find user `%s'\n", options.user);
Juan Cespedese1887051998-03-10 00:08:41 +010055 exit(1);
56 }
57 run_uid = pent->pw_uid;
58 run_gid = pent->pw_gid;
59
Juan Cespedesce377d52008-12-16 19:38:10 +010060 if (initgroups(options.user, run_gid) < 0) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +020061 perror("ltrace: initgroups");
Juan Cespedese1887051998-03-10 00:08:41 +010062 exit(1);
63 }
64 } else {
65 run_uid = getuid();
66 run_gid = getgid();
67 }
Juan Cespedesce377d52008-12-16 19:38:10 +010068 if (options.user || !geteuid()) {
Juan Cespedese1887051998-03-10 00:08:41 +010069 struct stat statbuf;
70 run_euid = run_uid;
71 run_egid = run_gid;
72
Petr Machata1b17dbf2011-07-08 19:22:52 +020073 if (!stat(command, &statbuf)) {
Juan Cespedese1887051998-03-10 00:08:41 +010074 if (statbuf.st_mode & S_ISUID) {
75 run_euid = statbuf.st_uid;
76 }
77 if (statbuf.st_mode & S_ISGID) {
Juan Cespedes666da8b1998-04-29 20:21:48 +020078 run_egid = statbuf.st_gid;
Juan Cespedese1887051998-03-10 00:08:41 +010079 }
80 }
81 if (setregid(run_gid, run_egid) < 0) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +020082 perror("ltrace: setregid");
Juan Cespedese1887051998-03-10 00:08:41 +010083 exit(1);
84 }
85 if (setreuid(run_uid, run_euid) < 0) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +020086 perror("ltrace: setreuid");
Juan Cespedese1887051998-03-10 00:08:41 +010087 exit(1);
88 }
89 }
90}
Juan Cespedescac15c32003-01-31 18:58:58 +010091
Petr Machata1b17dbf2011-07-08 19:22:52 +020092pid_t
93execute_program(const char * command, char **argv)
94{
Juan Cespedescac15c32003-01-31 18:58:58 +010095 pid_t pid;
96
Petr Machata1b17dbf2011-07-08 19:22:52 +020097 debug(1, "Executing `%s'...", command);
Juan Cespedescac15c32003-01-31 18:58:58 +010098
99 pid = fork();
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100100 if (pid < 0) {
Petr Machatac805c622012-03-02 00:10:37 +0100101 fail:
Juan Cespedescac15c32003-01-31 18:58:58 +0100102 perror("ltrace: fork");
103 exit(1);
104 } else if (!pid) { /* child */
Petr Machata1b17dbf2011-07-08 19:22:52 +0200105 change_uid(command);
Juan Cespedescac15c32003-01-31 18:58:58 +0100106 trace_me();
Petr Machata1b17dbf2011-07-08 19:22:52 +0200107 execvp(command, argv);
108 fprintf(stderr, "Can't execute `%s': %s\n", command,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100109 strerror(errno));
Juan Cespedese672ad12009-02-11 18:49:18 +0100110 _exit(1);
Juan Cespedescac15c32003-01-31 18:58:58 +0100111 }
112
Petr Machatac805c622012-03-02 00:10:37 +0100113 if (wait_for_proc(pid) < 0)
114 goto fail;
Petr Machatab4f9e0c2012-02-07 01:57:59 +0100115
Juan Cespedescac15c32003-01-31 18:58:58 +0100116 debug(1, "PID=%d", pid);
Petr Machata1b17dbf2011-07-08 19:22:52 +0200117 return pid;
Juan Cespedescac15c32003-01-31 18:58:58 +0100118}