blob: 663d8e9ce92619f9f0bf79318bf806c6415aa57c [file] [log] [blame]
Juan Cespedes5e01f651998-03-08 22:31:44 +01001#include <stdio.h>
Juan Cespedese1887051998-03-10 00:08:41 +01002#include <sys/types.h>
3#include <sys/stat.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +01004#include <unistd.h>
5#include <errno.h>
6#include <string.h>
Juan Cespedese1887051998-03-10 00:08:41 +01007#include <pwd.h>
8#include <grp.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +01009
10#include "ltrace.h"
11#include "options.h"
12#include "output.h"
13#include "sysdep.h"
14
Juan Cespedese1887051998-03-10 00:08:41 +010015static void change_uid(struct process * proc);
16
Juan Cespedes5e01f651998-03-08 22:31:44 +010017void execute_program(struct process * sp, char **argv)
18{
19 int pid;
20
21 if (opt_d) {
22 output_line(0, "Executing `%s'...", sp->filename);
23 }
24
25 pid = fork();
26 if (pid<0) {
27 perror("fork");
28 exit(1);
29 } else if (!pid) { /* child */
Juan Cespedes28f60191998-04-12 00:04:39 +020030 change_uid(sp);
Juan Cespedes666da8b1998-04-29 20:21:48 +020031 trace_me();
Juan Cespedes5e01f651998-03-08 22:31:44 +010032 execvp(sp->filename, argv);
33 fprintf(stderr, "Can't execute `%s': %s\n", sp->filename, strerror(errno));
34 exit(1);
35 }
36
37 if (opt_d) {
38 output_line(0, "PID=%d", pid);
39 }
40
41 sp->pid = pid;
42
43 return;
44}
Juan Cespedese1887051998-03-10 00:08:41 +010045
46static void change_uid(struct process * proc)
47{
48 uid_t run_uid, run_euid;
49 gid_t run_gid, run_egid;
50
51 if (opt_u) {
52 struct passwd *pent;
53
54 if (getuid() != 0 || geteuid() != 0) {
55 fprintf(stderr, "you must be root to use the -u option\n");
56 exit(1);
57 }
58 if ((pent = getpwnam(opt_u)) == NULL) {
59 fprintf(stderr, "cannot find user `%s'\n", opt_u);
60 exit(1);
61 }
62 run_uid = pent->pw_uid;
63 run_gid = pent->pw_gid;
64
65 if (initgroups(opt_u, run_gid) < 0) {
66 perror("initgroups");
67 exit(1);
68 }
69 } else {
70 run_uid = getuid();
71 run_gid = getgid();
72 }
73 if (opt_u || !geteuid()) {
74 struct stat statbuf;
75 run_euid = run_uid;
76 run_egid = run_gid;
77
78 if (!stat(proc->filename, &statbuf)) {
79 if (statbuf.st_mode & S_ISUID) {
80 run_euid = statbuf.st_uid;
81 }
82 if (statbuf.st_mode & S_ISGID) {
Juan Cespedes666da8b1998-04-29 20:21:48 +020083 run_egid = statbuf.st_gid;
Juan Cespedese1887051998-03-10 00:08:41 +010084 }
85 }
86 if (setregid(run_gid, run_egid) < 0) {
87 perror("setregid");
88 exit(1);
89 }
90 if (setreuid(run_uid, run_euid) < 0) {
91 perror("setreuid");
92 exit(1);
93 }
94 }
95}