blob: 09db44a3cfbc4886e721872fe627a2748526077c [file] [log] [blame]
Juan Cespedesd44c6b81998-09-25 14:48:42 +02001#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
Juan Cespedes5e01f651998-03-08 22:31:44 +01005#include <stdio.h>
Juan Cespedes504a3852003-02-04 23:24:38 +01006#include <stdlib.h>
Juan Cespedese1887051998-03-10 00:08:41 +01007#include <sys/types.h>
8#include <sys/stat.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +01009#include <unistd.h>
10#include <errno.h>
11#include <string.h>
Juan Cespedese1887051998-03-10 00:08:41 +010012#include <pwd.h>
13#include <grp.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010014
15#include "ltrace.h"
16#include "options.h"
Juan Cespedescac15c32003-01-31 18:58:58 +010017#include "debug.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010018#include "sysdep.h"
19
Ian Wienand9a2ad352006-02-20 22:44:45 +010020static void
21change_uid(struct process * proc) {
Juan Cespedese1887051998-03-10 00:08:41 +010022 uid_t run_uid, run_euid;
23 gid_t run_gid, run_egid;
24
25 if (opt_u) {
26 struct passwd *pent;
27
28 if (getuid() != 0 || geteuid() != 0) {
Ian Wienand9a2ad352006-02-20 22:44:45 +010029 fprintf(stderr, "you must be root to use the -u option\n");
Juan Cespedese1887051998-03-10 00:08:41 +010030 exit(1);
31 }
32 if ((pent = getpwnam(opt_u)) == NULL) {
33 fprintf(stderr, "cannot find user `%s'\n", opt_u);
34 exit(1);
35 }
36 run_uid = pent->pw_uid;
37 run_gid = pent->pw_gid;
38
39 if (initgroups(opt_u, run_gid) < 0) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +020040 perror("ltrace: initgroups");
Juan Cespedese1887051998-03-10 00:08:41 +010041 exit(1);
42 }
43 } else {
44 run_uid = getuid();
45 run_gid = getgid();
46 }
47 if (opt_u || !geteuid()) {
48 struct stat statbuf;
49 run_euid = run_uid;
50 run_egid = run_gid;
51
52 if (!stat(proc->filename, &statbuf)) {
53 if (statbuf.st_mode & S_ISUID) {
54 run_euid = statbuf.st_uid;
55 }
56 if (statbuf.st_mode & S_ISGID) {
Juan Cespedes666da8b1998-04-29 20:21:48 +020057 run_egid = statbuf.st_gid;
Juan Cespedese1887051998-03-10 00:08:41 +010058 }
59 }
60 if (setregid(run_gid, run_egid) < 0) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +020061 perror("ltrace: setregid");
Juan Cespedese1887051998-03-10 00:08:41 +010062 exit(1);
63 }
64 if (setreuid(run_uid, run_euid) < 0) {
Juan Cespedese3eb9aa1999-04-03 03:21:52 +020065 perror("ltrace: setreuid");
Juan Cespedese1887051998-03-10 00:08:41 +010066 exit(1);
67 }
68 }
69}
Juan Cespedescac15c32003-01-31 18:58:58 +010070
Ian Wienand9a2ad352006-02-20 22:44:45 +010071void
72execute_program(struct process * sp, char **argv) {
Juan Cespedescac15c32003-01-31 18:58:58 +010073 pid_t pid;
74
75 debug(1, "Executing `%s'...", sp->filename);
76
77 pid = fork();
Ian Wienand9a2ad352006-02-20 22:44:45 +010078 if (pid<0) {
Juan Cespedescac15c32003-01-31 18:58:58 +010079 perror("ltrace: fork");
80 exit(1);
81 } else if (!pid) { /* child */
82 change_uid(sp);
83 trace_me();
84 execvp(sp->filename, argv);
Ian Wienand9a2ad352006-02-20 22:44:45 +010085 fprintf(stderr, "Can't execute `%s': %s\n", sp->filename, strerror(errno));
Juan Cespedescac15c32003-01-31 18:58:58 +010086 exit(1);
87 }
88
89 debug(1, "PID=%d", pid);
90
91 sp->pid = pid;
92
93 return;
94}