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

« back to all changes in this revision

Viewing changes to trampoline/trampoline.c

  • Committer: William Grant
  • Date: 2008-07-07 04:55:07 UTC
  • mfrom: (unknown (missing))
  • Revision ID: wgrant@ugrad.unimelb.edu.au-20080707045507-wca484wpjxppy7ln
Merge jails-redux branch. We now use aufs rather than hardlinking tens
of thousands of jail files hundreds of times.

Reconfiguration and reconstruction of all jails will be required.
Probably a good idea to start jails from scratch if possible.

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 <syslog.h>
 
41
#include <sys/mount.h>
40
42
#include <sys/types.h>
41
43
#include <sys/stat.h>
42
44
#include <sys/time.h>
48
50
 */
49
51
#include "conf.h"
50
52
 
 
53
#include "norm.h"
 
54
 
51
55
/* Returns TRUE if the given uid is allowed to execute trampoline.
52
56
 * Only root or the web server should be allowed to execute.
53
57
 * This is determined by the whitelist allowed_uids in conf.h.
118
122
    exit(1);
119
123
}
120
124
 
 
125
#ifdef IVLE_AUFS_JAILS
 
126
/* Die more pleasantly if mallocs fail. */
 
127
void *die_if_null(void *ptr)
 
128
{
 
129
    if (ptr == NULL)
 
130
    {
 
131
        perror("not enough memory");
 
132
        exit(1);
 
133
    }
 
134
    return ptr;
 
135
}
 
136
 
 
137
/* Find the path of the user components of a jail, given a mountpoint. */
 
138
char *jail_src(const char* jailpath)
 
139
{
 
140
    char* src;
 
141
    int srclen;
 
142
    int dstlen;
 
143
 
 
144
    srclen = strlen(jail_src_base);
 
145
    dstlen = strlen(jail_base);
 
146
    
 
147
    src = die_if_null(malloc(strlen(jailpath) + (srclen - dstlen) + 1));
 
148
    strcpy(src, jail_src_base);
 
149
    strcat(src, jailpath+dstlen);
 
150
 
 
151
    return src;
 
152
}
 
153
 
 
154
/* Check for the validity of a jail in the given path, mounting it if it looks
 
155
 * empty.
 
156
 * TODO: Updating /etc/mtab would be nice. */
 
157
void mount_if_needed(const char* jailpath)
 
158
{
 
159
    char *jailsrc;
 
160
    char *jaillib;
 
161
    char *mountdata;
 
162
 
 
163
    /* Check if there is something useful in the jail. If not, it's probably
 
164
     * not mounted. */
 
165
    jaillib = die_if_null(malloc(strlen(jailpath) + 5));
 
166
    sprintf(jaillib, "%s/lib", jailpath);
 
167
 
 
168
    if (access(jaillib, F_OK))
 
169
    {
 
170
        /* No /lib? Mustn't be mounted. Mount it, creating the dir if needed. */
 
171
        if (access(jailpath, F_OK))
 
172
        {
 
173
             if(mkdir(jailpath, 0755))
 
174
             {
 
175
                 syslog(LOG_ERR, "could not create mountpoint %s\n", jailpath);
 
176
                 perror("could not create jail mountpoint");
 
177
                 exit(1);
 
178
             }
 
179
             syslog(LOG_NOTICE, "created mountpoint %s\n", jailpath);
 
180
        }
 
181
       
 
182
        jailsrc = jail_src(jailpath);
 
183
        mountdata = die_if_null(malloc(3 + strlen(jailsrc) + 4 + strlen(jail_system) + 3 + 1));
 
184
        sprintf(mountdata, "br:%s=rw:%s=ro", jailsrc, jail_system);
 
185
        if (mount("none", jailpath, "aufs", 0, mountdata))
 
186
        {
 
187
            syslog(LOG_ERR, "could not mount %s\n", jailpath);
 
188
            perror("could not mount");
 
189
            exit(1);
 
190
        } 
 
191
 
 
192
        syslog(LOG_INFO, "mounted %s\n", jailpath);
 
193
 
 
194
        free(jailsrc);
 
195
        free(mountdata);
 
196
    }
 
197
 
 
198
    free(jaillib);
 
199
}
 
200
#endif /* IVLE_AUFS_JAILS */
 
201
 
121
202
int main(int argc, char* const argv[])
122
203
{
123
204
    char* jailpath;
189
270
        exit(1);
190
271
    }
191
272
 
 
273
    openlog("trampoline", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);
 
274
 
 
275
    #ifdef IVLE_AUFS_JAILS
 
276
    mount_if_needed(canonical_jailpath);
 
277
    #endif /* IVLE_AUFS_JAILS */
 
278
 
192
279
    /* chroot into the jail.
193
280
     * Henceforth this process, and its children, cannot see anything above
194
281
     * canoncial_jailpath. */
195
282
    if (chroot(canonical_jailpath))
196
283
    {
 
284
        syslog(LOG_ERR, "chroot to %s failed\n", canonical_jailpath);
 
285
 
197
286
        perror("could not chroot");
198
287
        exit(1);
199
288
    }
272
361
     * and the first argument will be the script. */
273
362
    execv(prog, args);
274
363
 
275
 
    /* XXX if (daemon_mode) use syslog? */
276
364
    /* nb exec won't return unless there was an error */
 
365
    syslog(LOG_ERR, "exec of %s in %s failed", prog, canonical_jailpath);
 
366
 
277
367
    perror("could not exec");
 
368
    closelog();
278
369
    return 1;
279
370
}