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

« back to all changes in this revision

Viewing changes to bin/trampoline/trampoline.c

  • Committer: William Grant
  • Date: 2009-04-28 05:06:00 UTC
  • Revision ID: grantw@unimelb.edu.au-20090428050600-hogd9d6wo7ksyqy8
ivle.database.get_store() now takes a configuration object.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 * Scripts (such as Python programs) should be executed by supplying
28
28
 * "/usr/bin/python" as the program, and the script as the first argument.
29
29
 *
 
30
 * Usage: trampoline uid jail-path working-path program [args...]
30
31
 * Must run as root. Will safely setuid to the supplied uid, checking that it
31
32
 * is not root. Recommended that the file is set up as follows:
32
33
 *  sudo chown root:root trampoline; sudo chroot +s trampoline
33
34
 */
34
35
 
35
36
#define _XOPEN_SOURCE
36
 
#define _BSD_SOURCE
37
37
 
38
38
#include <stdio.h>
39
39
#include <stdlib.h>
45
45
#include <sys/stat.h>
46
46
#include <sys/time.h>
47
47
#include <sys/resource.h>
48
 
#include <grp.h>
49
48
#include <limits.h>
50
49
#include <signal.h>
51
50
 
123
122
{
124
123
    fprintf(stderr,
125
124
        "usage: %s [-d] [-u] <uid> <base> <src> <system> <jail> <cwd> <program> [args...]\n", nm);
126
 
    fprintf(stderr, "  -d       \tDaemonize\n");
127
 
    fprintf(stderr, "  -u       \tPrint usage\n");
128
 
    fprintf(stderr, "  <uid>    \tUID to run program as\n");
129
 
    fprintf(stderr, "  <base>   \tDirectory that jails will be mounted in\n");
130
 
    fprintf(stderr, "  <src>    \tDirectory containing users jail data\n");
131
 
    fprintf(stderr, "  <system> \tDirectory containing main jail file system\n");
132
 
    fprintf(stderr, "  <jail>   \tDirectory of users mounted jail\n");
133
 
    fprintf(stderr, "  <cwd>    \tDirectory inside the jail to change dir to\n");
134
 
    fprintf(stderr, "  <program>\tProgram inside jail to execute\n");
135
125
    exit(1);
136
126
}
137
127
 
147
137
    return ptr;
148
138
}
149
139
 
150
 
int checked_mount(const char *source, const char *target,
151
 
                  const char *filesystemtype, unsigned long mountflags,
152
 
                  const void *data)
153
 
{
154
 
    int result = mount(source, target, filesystemtype, mountflags, data);
155
 
    if (result)
156
 
    {
157
 
        syslog(LOG_ERR, "could not mount %s on %s\n", source, target);
158
 
        perror("could not mount");
159
 
        exit(1);
160
 
    }
161
 
 
162
 
    return result;
163
 
}
164
 
 
165
 
 
166
140
/* Find the path of the user components of a jail, given a mountpoint. */
167
141
char *jail_src(const char *jail_src_base, const char *jail_base,
168
142
               const char *jailpath)
173
147
 
174
148
    srclen = strlen(jail_src_base);
175
149
    dstlen = strlen(jail_base);
176
 
 
 
150
    
177
151
    src = die_if_null(malloc(strlen(jailpath) + (srclen - dstlen) + 1));
178
152
    strcpy(src, jail_src_base);
179
153
    strcat(src, jailpath+dstlen);
189
163
{
190
164
    char *jailsrc;
191
165
    char *jaillib;
192
 
    char *source_bits;
193
 
    char *target_bits;
 
166
    char *mountdata;
194
167
 
195
168
    /* Check if there is something useful in the jail. If not, it's probably
196
169
     * not mounted. */
210
183
             }
211
184
             syslog(LOG_NOTICE, "created mountpoint %s\n", jailpath);
212
185
        }
213
 
 
 
186
       
214
187
        jailsrc = jail_src(jail_src_base, jail_base, jailpath);
215
 
        checked_mount(jail_system, jailpath, NULL, MS_BIND | MS_RDONLY, NULL);
216
 
 
217
 
        source_bits = die_if_null(malloc(strlen(jailsrc) + 5 + 1));
218
 
        target_bits = die_if_null(malloc(strlen(jailpath) + 5 + 1));
219
 
        sprintf(source_bits, "%s/home", jailsrc);
220
 
        sprintf(target_bits, "%s/home", jailpath);
221
 
 
222
 
        checked_mount(source_bits, target_bits, NULL, MS_BIND, NULL);
223
 
 
224
 
        sprintf(source_bits, "%s/tmp", jailsrc);
225
 
        sprintf(target_bits, "%s/tmp", jailpath);
226
 
 
227
 
        checked_mount(source_bits, target_bits, NULL, MS_BIND, NULL);
 
188
        mountdata = die_if_null(malloc(3 + strlen(jailsrc) + 4 + strlen(jail_system) + 3 + 1));
 
189
        sprintf(mountdata, "br:%s=rw:%s=ro", jailsrc, jail_system);
 
190
        if (mount("none", jailpath, "aufs", 0, mountdata))
 
191
        {
 
192
            syslog(LOG_ERR, "could not mount %s\n", jailpath);
 
193
            perror("could not mount");
 
194
            exit(1);
 
195
        } 
228
196
 
229
197
        syslog(LOG_INFO, "mounted %s\n", jailpath);
230
198
 
231
199
        free(jailsrc);
232
 
        free(source_bits);
233
 
        free(target_bits);
 
200
        free(mountdata);
234
201
    }
235
202
 
236
203
    free(jaillib);
260
227
    char* prog;
261
228
    char* const * args;
262
229
    int uid;
263
 
    gid_t groups[1];
264
230
    int arg_num = 1;
265
231
    int daemon_mode = 0;
266
232
    int unlimited = 0;
360
326
        perror("could not setgid");
361
327
        exit(1);
362
328
    }
363
 
    
364
 
    groups[0] = uid;
365
 
    if (setgroups(1, groups))
366
 
    {
367
 
        perror("could not setgroups");
368
 
        exit(1);
369
 
    }
370
329
 
371
330
    if (setuid(uid))
372
331
    {
379
338
    {
380
339
        struct rlimit l;
381
340
        /* Process adress space in memory */
382
 
        l.rlim_cur = 448 * 1024 * 1024; /* 512MiB - 64MiB */
383
 
        l.rlim_max = 512 * 1024 * 1024; /* 512MiB */
 
341
        l.rlim_cur = 192 * 1024 * 1024; /* 192MiB */
 
342
        l.rlim_max = 256 * 1024 * 1024; /* 256MiB */
384
343
        if (setrlimit(RLIMIT_AS, &l))
385
344
        {
386
345
            perror("could not setrlimit/RLIMIT_AS");
391
350
         * Note: This requires a kernel patch to work correctly otherwise it is  
392
351
         * ineffective (thus you are only limited by RLIMIT_AS)
393
352
         */
394
 
        l.rlim_cur = 448 * 1024 * 1024; /* 512MiB - 64MiB */
395
 
        l.rlim_max = 512 * 1024 * 1024; /* 512MiB */
 
353
        l.rlim_cur = 192 * 1024 * 1024; /* 192MiB */
 
354
        l.rlim_max = 256 * 1024 * 1024; /* 256MiB */
396
355
        if (setrlimit(RLIMIT_DATA, &l))
397
356
        {
398
357
            perror("could not setrlimit/RLIMIT_DATA");
420
379
        /* File Size */
421
380
        l.rlim_cur = 64 * 1024 * 1024; /* 64MiB */
422
381
        l.rlim_max = 72 * 1024 * 1024; /* 72MiB */
423
 
        if (setrlimit(RLIMIT_FSIZE, &l))
 
382
if (setrlimit(RLIMIT_FSIZE, &l))
424
383
        {
425
384
            perror("could not setrlimit/RLIMIT_FSIZE");
426
385
            exit(1);
427
386
        }
428
 
        
429
 
        /* Number of Processes */
430
 
        l.rlim_cur = 50;
431
 
        l.rlim_max = 50;
432
 
        if (setrlimit(RLIMIT_NPROC, &l))
433
 
        {
434
 
            perror("could not setrlimit/RLIMIT_NPROC");
435
 
            exit(1);
436
 
        }
437
387
    }
438
388
 
439
389
    /* Remove any signal handler masks so we can send signals to the child */