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

« back to all changes in this revision

Viewing changes to trampoline/trampoline.c

  • Committer: mattgiuca
  • Date: 2008-01-11 06:52:24 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:183
browser: Added top level of response handling. Now determines handler type and
calls the appropriate handler. (Handlers just alert at the moment).

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 <sys/time.h>
43
 
#include <sys/resource.h>
44
 
#include <limits.h>
45
40
 
46
41
/* conf.h is admin-configured by the setup process.
47
42
 * It defines jail_base.
48
43
 */
49
44
#include "conf.h"
50
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
 
51
56
/* Returns TRUE if the given uid is allowed to execute trampoline.
52
57
 * Only root or the web server should be allowed to execute.
53
58
 * This is determined by the whitelist allowed_uids in conf.h.
68
73
    return 0;
69
74
}
70
75
 
71
 
/* Turn the process into a daemon using the standard
72
 
 * 2-fork technique.
73
 
 */
74
 
void daemonize(void)
75
 
{
76
 
    pid_t pid, sid;
77
 
 
78
 
    /* already a daemon */
79
 
    if ( getppid() == 1 ) return;
80
 
 
81
 
    /* Fork off the parent process */
82
 
    pid = fork();
83
 
    if (pid < 0) {
84
 
        exit(1);
85
 
    }
86
 
    /* If we got a good PID, then we can exit the parent process. */
87
 
    if (pid > 0) {
88
 
        exit(0);
89
 
    }
90
 
 
91
 
    /* At this point we are executing as the child process */
92
 
 
93
 
    /* Change the file mode mask */
94
 
    umask(022);
95
 
 
96
 
    /* Create a new SID for the child process */
97
 
    sid = setsid();
98
 
    if (sid < 0) {
99
 
        exit(1);
100
 
    }
101
 
 
102
 
    /* Change the current working directory.  This prevents the current
103
 
       directory from being locked; hence not being able to remove it. */
104
 
    if ((chdir("/")) < 0) {
105
 
        exit(1);
106
 
    }
107
 
 
108
 
    /* Redirect standard files to /dev/null */
109
 
    freopen( "/dev/null", "r", stdin);
110
 
    freopen( "/dev/null", "w", stdout);
111
 
    freopen( "/dev/null", "w", stderr);
112
 
}
113
 
 
114
 
static void usage(const char* nm)
115
 
{
116
 
    fprintf(stderr,
117
 
        "usage: %s [-d] [-u] <uid> <jail> <cwd> <program> [args...]\n", nm);
118
 
    exit(1);
119
 
}
120
 
 
121
76
int main(int argc, char* const argv[])
122
77
{
123
78
    char* jailpath;
124
 
    char* work_dir;
125
 
    char* prog;
126
 
    char* const * args;
127
79
    int uid;
128
 
    int arg_num = 1;
129
 
    int daemon_mode = 0;
130
 
    int unlimited = 0;
131
 
    char canonical_jailpath[PATH_MAX];
132
80
 
133
81
    /* Disallow execution from all users but the whitelisted ones, and root */
134
82
    if (!uid_allowed(getuid()))
138
86
    }
139
87
 
140
88
    /* Args check and usage */
141
 
    if (argc < 5)
142
 
    {
143
 
        usage(argv[0]);
144
 
    }
145
 
 
146
 
    if (strcmp(argv[arg_num], "-d") == 0)
147
 
    {
148
 
        if (argc < 6)
149
 
        {
150
 
            usage(argv[0]);
151
 
        }
152
 
        daemon_mode = 1;
153
 
        arg_num++;
154
 
    }
155
 
 
156
 
    if (strcmp(argv[arg_num], "-u") == 0)
157
 
    {
158
 
        if (argc < 6)
159
 
        {
160
 
            usage(argv[0]);
161
 
        }
162
 
        unlimited = 1;
163
 
        arg_num++;
164
 
    }
165
 
    uid = atoi(argv[arg_num++]);
166
 
    jailpath = argv[arg_num++];
167
 
    work_dir = argv[arg_num++];
168
 
    prog = argv[arg_num];
169
 
    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
    }
170
95
 
171
96
    /* Disallow suiding to the root user */
172
 
    if (uid == 0)
 
97
    uid = atoi(argv[ARG_UID]);
 
98
    if (uid == UID_ROOT)
173
99
    {
174
100
        fprintf(stderr, "cannot set up a jail as root\n");
175
101
        exit(1);
176
102
    }
177
103
 
178
 
    /* Jail path must be an absolute path,
179
 
     * 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
180
109
     */
181
 
    if (norm(canonical_jailpath, PATH_MAX, jailpath) != 0)
182
 
    {
183
 
        fprintf(stderr, "bad jail path: %s\n", jailpath);
184
 
        exit(1);
185
 
    }
186
 
    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)))
187
114
    {
188
115
        fprintf(stderr, "bad jail path: %s\n", jailpath);
189
116
        exit(1);
191
118
 
192
119
    /* chroot into the jail.
193
120
     * Henceforth this process, and its children, cannot see anything above
194
 
     * canoncial_jailpath. */
195
 
    if (chroot(canonical_jailpath))
 
121
     * jailpath. */
 
122
    if (chroot(jailpath))
196
123
    {
197
124
        perror("could not chroot");
198
125
        exit(1);
199
126
    }
200
127
 
201
128
    /* chdir into the specified working directory */
202
 
    if (chdir(work_dir))
 
129
    if (chdir(argv[ARG_CWD]))
203
130
    {
204
131
        perror("could not chdir");
205
132
        exit(1);
208
135
    /* setuid to the given user ID.
209
136
     * Henceforth we will be running as this user instead of root.
210
137
     */
211
 
    if (setgid(uid))
212
 
    {
213
 
        perror("could not setgid");
214
 
        exit(1);
215
 
    }
216
 
 
217
138
    if (setuid(uid))
218
139
    {
219
140
        perror("could not setuid");
220
141
        exit(1);
221
142
    }
222
143
 
223
 
    if (daemon_mode)
224
 
    {
225
 
        daemonize();
226
 
    }
227
 
 
228
 
    /* set user resource limits */
229
 
    if (!unlimited)
230
 
    {
231
 
        struct rlimit l;
232
 
        /* Process size in virtual memory */
233
 
        l.rlim_cur = 64 * 1024 * 1024; /* 64Mb */
234
 
        l.rlim_max = 72 * 1024 * 1024; /* 64Mb */
235
 
        if (setrlimit(RLIMIT_AS, &l))
236
 
        {
237
 
            perror("could not setrlimit/RLIMIT_AS");
238
 
            exit(1);
239
 
        }
240
 
 
241
 
        /* Core */
242
 
        l.rlim_cur = 0;
243
 
        l.rlim_max = 0;
244
 
        if (setrlimit(RLIMIT_CORE, &l))
245
 
        {
246
 
            perror("could not setrlimit/RLIMIT_CORE");
247
 
            exit(1);
248
 
        }
249
 
 
250
 
        /* CPU */
251
 
        l.rlim_cur = 25;
252
 
        l.rlim_max = 30;
253
 
        if (setrlimit(RLIMIT_CPU, &l))
254
 
        {
255
 
            perror("could not setrlimit/RLIMIT_CPU");
256
 
            exit(1);
257
 
        }
258
 
 
259
 
        /* File Size */
260
 
        l.rlim_cur = 64 * 1024 * 1024; /* 64Mb */
261
 
        l.rlim_max = 72 * 1024 * 1024; /* 72Mb */
262
 
        if (setrlimit(RLIMIT_FSIZE, &l))
263
 
        {
264
 
            perror("could not setrlimit/RLIMIT_FSIZE");
265
 
            exit(1);
266
 
        }
267
 
    }
268
 
 
269
144
    /* exec (replace this process with the a new instance of the target
270
145
     * program). Pass along all the arguments.
271
146
     * Note that for script execution, the "program" will be the interpreter,
272
147
     * and the first argument will be the script. */
273
 
    execv(prog, args);
 
148
    execv(argv[ARG_PROG], argv + ARG_PROG);
274
149
 
275
 
    /* XXX if (daemon_mode) use syslog? */
276
150
    /* nb exec won't return unless there was an error */
277
151
    perror("could not exec");
278
 
    return 1;
 
152
    return EXIT_FAILURE;
279
153
}