blob: f469b2f14b7dd6902964b6d3689a665bee62f3c9 [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
Petr Machata64262602012-01-07 03:41:36 +010017#include "backend.h"
18#include "options.h"
19#include "debug.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010020
Juan Cespedesf1350522008-12-16 18:19:58 +010021static void
Petr Machata1b17dbf2011-07-08 19:22:52 +020022change_uid(const char * command)
23{
Juan Cespedese1887051998-03-10 00:08:41 +010024 uid_t run_uid, run_euid;
25 gid_t run_gid, run_egid;
26
Juan Cespedesce377d52008-12-16 19:38:10 +010027 if (options.user) {
Juan Cespedese1887051998-03-10 00:08:41 +010028 struct passwd *pent;
29
30 if (getuid() != 0 || geteuid() != 0) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010031 fprintf(stderr,
32 "you must be root to use the -u option\n");
Juan Cespedese1887051998-03-10 00:08:41 +010033 exit(1);
34 }
Juan Cespedesce377d52008-12-16 19:38:10 +010035 if ((pent = getpwnam(options.user)) == NULL) {
36 fprintf(stderr, "cannot find user `%s'\n", options.user);
Juan Cespedese1887051998-03-10 00:08:41 +010037 exit(1);
38 }
39 run_uid = pent->pw_uid;
40 run_gid = pent->pw_gid;
41
Juan Cespedesce377d52008-12-16 19:38:10 +010042 if (initgroups(options.user, run_gid) < 0) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +020043 perror("ltrace: initgroups");
Juan Cespedese1887051998-03-10 00:08:41 +010044 exit(1);
45 }
46 } else {
47 run_uid = getuid();
48 run_gid = getgid();
49 }
Juan Cespedesce377d52008-12-16 19:38:10 +010050 if (options.user || !geteuid()) {
Juan Cespedese1887051998-03-10 00:08:41 +010051 struct stat statbuf;
52 run_euid = run_uid;
53 run_egid = run_gid;
54
Petr Machata1b17dbf2011-07-08 19:22:52 +020055 if (!stat(command, &statbuf)) {
Juan Cespedese1887051998-03-10 00:08:41 +010056 if (statbuf.st_mode & S_ISUID) {
57 run_euid = statbuf.st_uid;
58 }
59 if (statbuf.st_mode & S_ISGID) {
Juan Cespedes666da8b1998-04-29 20:21:48 +020060 run_egid = statbuf.st_gid;
Juan Cespedese1887051998-03-10 00:08:41 +010061 }
62 }
63 if (setregid(run_gid, run_egid) < 0) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +020064 perror("ltrace: setregid");
Juan Cespedese1887051998-03-10 00:08:41 +010065 exit(1);
66 }
67 if (setreuid(run_uid, run_euid) < 0) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +020068 perror("ltrace: setreuid");
Juan Cespedese1887051998-03-10 00:08:41 +010069 exit(1);
70 }
71 }
72}
Juan Cespedescac15c32003-01-31 18:58:58 +010073
Petr Machata1b17dbf2011-07-08 19:22:52 +020074pid_t
75execute_program(const char * command, char **argv)
76{
Juan Cespedescac15c32003-01-31 18:58:58 +010077 pid_t pid;
78
Petr Machata1b17dbf2011-07-08 19:22:52 +020079 debug(1, "Executing `%s'...", command);
Juan Cespedescac15c32003-01-31 18:58:58 +010080
81 pid = fork();
Ian Wienand2d45b1a2006-02-20 22:48:07 +010082 if (pid < 0) {
Petr Machatac805c622012-03-02 00:10:37 +010083 fail:
Juan Cespedescac15c32003-01-31 18:58:58 +010084 perror("ltrace: fork");
85 exit(1);
86 } else if (!pid) { /* child */
Petr Machata1b17dbf2011-07-08 19:22:52 +020087 change_uid(command);
Juan Cespedescac15c32003-01-31 18:58:58 +010088 trace_me();
Petr Machata1b17dbf2011-07-08 19:22:52 +020089 execvp(command, argv);
90 fprintf(stderr, "Can't execute `%s': %s\n", command,
Ian Wienand2d45b1a2006-02-20 22:48:07 +010091 strerror(errno));
Juan Cespedese672ad12009-02-11 18:49:18 +010092 _exit(1);
Juan Cespedescac15c32003-01-31 18:58:58 +010093 }
94
Petr Machatac805c622012-03-02 00:10:37 +010095 if (wait_for_proc(pid) < 0)
96 goto fail;
Petr Machatab4f9e0c2012-02-07 01:57:59 +010097
Juan Cespedescac15c32003-01-31 18:58:58 +010098 debug(1, "PID=%d", pid);
Petr Machata1b17dbf2011-07-08 19:22:52 +020099 return pid;
Juan Cespedescac15c32003-01-31 18:58:58 +0100100}