blob: 47f514d90bd5793475b0bfd63eed831e240ad5cf [file] [log] [blame]
Juan Cespedesd44c6b81998-09-25 14:48:42 +02001#include "config.h"
Juan Cespedesd44c6b81998-09-25 14:48:42 +02002
Joe Damatoab3b72c2010-10-31 00:21:53 -07003#if defined(HAVE_LIBUNWIND)
4#include <libunwind-ptrace.h>
5#endif /* defined(HAVE_LIBUNWIND) */
6
Juan Cespedes5e01f651998-03-08 22:31:44 +01007#include <stdio.h>
Juan Cespedes504a3852003-02-04 23:24:38 +01008#include <stdlib.h>
Juan Cespedese1887051998-03-10 00:08:41 +01009#include <sys/types.h>
10#include <sys/stat.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010011#include <unistd.h>
12#include <errno.h>
13#include <string.h>
Juan Cespedese1887051998-03-10 00:08:41 +010014#include <pwd.h>
15#include <grp.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010016
Juan Cespedesf7281232009-06-25 16:11:21 +020017#include "common.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010018
Juan Cespedesf1350522008-12-16 18:19:58 +010019static void
Petr Machata1b17dbf2011-07-08 19:22:52 +020020change_uid(const char * command)
21{
Juan Cespedese1887051998-03-10 00:08:41 +010022 uid_t run_uid, run_euid;
23 gid_t run_gid, run_egid;
24
Juan Cespedesce377d52008-12-16 19:38:10 +010025 if (options.user) {
Juan Cespedese1887051998-03-10 00:08:41 +010026 struct passwd *pent;
27
28 if (getuid() != 0 || geteuid() != 0) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010029 fprintf(stderr,
30 "you must be root to use the -u option\n");
Juan Cespedese1887051998-03-10 00:08:41 +010031 exit(1);
32 }
Juan Cespedesce377d52008-12-16 19:38:10 +010033 if ((pent = getpwnam(options.user)) == NULL) {
34 fprintf(stderr, "cannot find user `%s'\n", options.user);
Juan Cespedese1887051998-03-10 00:08:41 +010035 exit(1);
36 }
37 run_uid = pent->pw_uid;
38 run_gid = pent->pw_gid;
39
Juan Cespedesce377d52008-12-16 19:38:10 +010040 if (initgroups(options.user, run_gid) < 0) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +020041 perror("ltrace: initgroups");
Juan Cespedese1887051998-03-10 00:08:41 +010042 exit(1);
43 }
44 } else {
45 run_uid = getuid();
46 run_gid = getgid();
47 }
Juan Cespedesce377d52008-12-16 19:38:10 +010048 if (options.user || !geteuid()) {
Juan Cespedese1887051998-03-10 00:08:41 +010049 struct stat statbuf;
50 run_euid = run_uid;
51 run_egid = run_gid;
52
Petr Machata1b17dbf2011-07-08 19:22:52 +020053 if (!stat(command, &statbuf)) {
Juan Cespedese1887051998-03-10 00:08:41 +010054 if (statbuf.st_mode & S_ISUID) {
55 run_euid = statbuf.st_uid;
56 }
57 if (statbuf.st_mode & S_ISGID) {
Juan Cespedes666da8b1998-04-29 20:21:48 +020058 run_egid = statbuf.st_gid;
Juan Cespedese1887051998-03-10 00:08:41 +010059 }
60 }
61 if (setregid(run_gid, run_egid) < 0) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +020062 perror("ltrace: setregid");
Juan Cespedese1887051998-03-10 00:08:41 +010063 exit(1);
64 }
65 if (setreuid(run_uid, run_euid) < 0) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +020066 perror("ltrace: setreuid");
Juan Cespedese1887051998-03-10 00:08:41 +010067 exit(1);
68 }
69 }
70}
Juan Cespedescac15c32003-01-31 18:58:58 +010071
Petr Machata1b17dbf2011-07-08 19:22:52 +020072pid_t
73execute_program(const char * command, char **argv)
74{
Juan Cespedescac15c32003-01-31 18:58:58 +010075 pid_t pid;
76
Petr Machata1b17dbf2011-07-08 19:22:52 +020077 debug(1, "Executing `%s'...", command);
Juan Cespedescac15c32003-01-31 18:58:58 +010078
79 pid = fork();
Ian Wienand2d45b1a2006-02-20 22:48:07 +010080 if (pid < 0) {
Juan Cespedescac15c32003-01-31 18:58:58 +010081 perror("ltrace: fork");
82 exit(1);
83 } else if (!pid) { /* child */
Petr Machata1b17dbf2011-07-08 19:22:52 +020084 change_uid(command);
Juan Cespedescac15c32003-01-31 18:58:58 +010085 trace_me();
Petr Machata1b17dbf2011-07-08 19:22:52 +020086 execvp(command, argv);
87 fprintf(stderr, "Can't execute `%s': %s\n", command,
Ian Wienand2d45b1a2006-02-20 22:48:07 +010088 strerror(errno));
Juan Cespedese672ad12009-02-11 18:49:18 +010089 _exit(1);
Juan Cespedescac15c32003-01-31 18:58:58 +010090 }
91
92 debug(1, "PID=%d", pid);
93
Petr Machata1b17dbf2011-07-08 19:22:52 +020094 return pid;
Juan Cespedescac15c32003-01-31 18:58:58 +010095}