~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to trampoline/trampoline.c

  • Committer: drtomc
  • Date: 2007-12-21 00:20:24 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:111
Checkpoint work on the console.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
#include <stdlib.h>
38
38
#include <string.h>
39
39
#include <unistd.h>
40
 
#include <sys/types.h>
41
 
#include <sys/stat.h>
42
 
#include <limits.h>
43
40
 
44
41
/* conf.h is admin-configured by the setup process.
45
42
 * It defines jail_base.
46
43
 */
47
44
#include "conf.h"
48
45
 
 
46
/* Argument names */
 
47
#define ARG_UID         1
 
48
#define ARG_JAILPATH    2
 
49
#define ARG_CWD         3
 
50
#define ARG_PROG        4
 
51
 
 
52
#define MIN_ARGC        5
 
53
 
 
54
#define UID_ROOT        0
 
55
 
49
56
/* Returns TRUE if the given uid is allowed to execute trampoline.
50
57
 * Only root or the web server should be allowed to execute.
51
58
 * This is determined by the whitelist allowed_uids in conf.h.
66
73
    return 0;
67
74
}
68
75
 
69
 
/* Turn the process into a daemon using the standard
70
 
 * 2-fork technique.
71
 
 */
72
 
void daemonize(void)
73
 
{
74
 
    pid_t pid, sid;
75
 
 
76
 
    /* already a daemon */
77
 
    if ( getppid() == 1 ) return;
78
 
 
79
 
    /* Fork off the parent process */
80
 
    pid = fork();
81
 
    if (pid < 0) {
82
 
        exit(1);
83
 
    }
84
 
    /* If we got a good PID, then we can exit the parent process. */
85
 
    if (pid > 0) {
86
 
        exit(0);
87
 
    }
88
 
 
89
 
    /* At this point we are executing as the child process */
90
 
 
91
 
    /* Change the file mode mask */
92
 
    umask(0);
93
 
 
94
 
    /* Create a new SID for the child process */
95
 
    sid = setsid();
96
 
    if (sid < 0) {
97
 
        exit(1);
98
 
    }
99
 
 
100
 
    /* Change the current working directory.  This prevents the current
101
 
       directory from being locked; hence not being able to remove it. */
102
 
    if ((chdir("/")) < 0) {
103
 
        exit(1);
104
 
    }
105
 
 
106
 
    /* Redirect standard files to /dev/null */
107
 
    freopen( "/dev/null", "r", stdin);
108
 
    freopen( "/dev/null", "w", stdout);
109
 
    freopen( "/dev/null", "w", stderr);
110
 
}
111
 
 
112
 
static void usage(const char* nm)
113
 
{
114
 
    fprintf(stderr,
115
 
        "usage: %s [-d] <uid> <jail> <cwd> <program> [args...]\n", nm);
116
 
    exit(1);
117
 
}
118
 
 
119
76
int main(int argc, char* const argv[])
120
77
{
121
78
    char* jailpath;
122
 
    char* work_dir;
123
 
    char* prog;
124
 
    char* const * args;
125
79
    int uid;
126
 
    int arg_num = 1;
127
 
    int daemon_mode = 0;
128
 
    char canonical_jailpath[PATH_MAX];
129
80
 
130
81
    /* Disallow execution from all users but the whitelisted ones, and root */
131
82
    if (!uid_allowed(getuid()))
135
86
    }
136
87
 
137
88
    /* Args check and usage */
138
 
    if (argc < 5)
139
 
    {
140
 
        usage(argv[0]);
141
 
    }
142
 
 
143
 
    if (strcmp(argv[arg_num], "-d") == 0)
144
 
    {
145
 
        if (argc < 6)
146
 
        {
147
 
            usage(argv[0]);
148
 
        }
149
 
        daemon_mode = 1;
150
 
        arg_num++;
151
 
    }
152
 
    uid = atoi(argv[arg_num++]);
153
 
    jailpath = argv[arg_num++];
154
 
    work_dir = argv[arg_num++];
155
 
    prog = argv[arg_num];
156
 
    args = argv + arg_num;
 
89
    if (argc < MIN_ARGC)
 
90
    {
 
91
        fprintf(stderr, "usage: %s <uid> <jail> <cwd> <program> [args...]\n",
 
92
            argv[0]);
 
93
        exit(EXIT_FAILURE);
 
94
    }
157
95
 
158
96
    /* Disallow suiding to the root user */
159
 
    if (uid == 0)
 
97
    uid = atoi(argv[ARG_UID]);
 
98
    if (uid == UID_ROOT)
160
99
    {
161
100
        fprintf(stderr, "cannot set up a jail as root\n");
162
101
        exit(1);
163
102
    }
164
103
 
165
 
    /* Jail path must be an absolute path,
166
 
     * and it must begin with jail_base.
 
104
    /* Jail path must:
 
105
     * Be non-empty
 
106
     * Start with a '/'
 
107
     * Not contain "/.."
 
108
     * Begin with jail_base
167
109
     */
168
 
    if (norm(canonical_jailpath, PATH_MAX, jailpath) != 0)
169
 
    {
170
 
        fprintf(stderr, "bad jail path: %s\n", jailpath);
171
 
        exit(1);
172
 
    }
173
 
    if (strncmp(canonical_jailpath, jail_base, strlen(jail_base)))
 
110
    jailpath = argv[ARG_JAILPATH];
 
111
    if (strlen(jailpath) < 1 || jailpath[0] != '/'
 
112
            || strstr(jailpath, "/..")
 
113
            || strncmp(jailpath, jail_base, strlen(jail_base)))
174
114
    {
175
115
        fprintf(stderr, "bad jail path: %s\n", jailpath);
176
116
        exit(1);
178
118
 
179
119
    /* chroot into the jail.
180
120
     * Henceforth this process, and its children, cannot see anything above
181
 
     * canoncial_jailpath. */
182
 
    if (chroot(canonical_jailpath))
 
121
     * jailpath. */
 
122
    if (chroot(jailpath))
183
123
    {
184
124
        perror("could not chroot");
185
125
        exit(1);
186
126
    }
187
127
 
188
128
    /* chdir into the specified working directory */
189
 
    if (chdir(work_dir))
 
129
    if (chdir(argv[ARG_CWD]))
190
130
    {
191
131
        perror("could not chdir");
192
132
        exit(1);
201
141
        exit(1);
202
142
    }
203
143
 
204
 
    if (daemon_mode)
205
 
    {
206
 
        daemonize();
207
 
    }
208
 
 
209
144
    /* exec (replace this process with the a new instance of the target
210
145
     * program). Pass along all the arguments.
211
146
     * Note that for script execution, the "program" will be the interpreter,
212
147
     * and the first argument will be the script. */
213
 
    execv(prog, args);
 
148
    execv(argv[ARG_PROG], argv + ARG_PROG);
214
149
 
215
 
    /* XXX if (daemon_mode) use syslog? */
216
150
    /* nb exec won't return unless there was an error */
217
151
    perror("could not exec");
218
 
    return 1;
 
152
    return EXIT_FAILURE;
219
153
}