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

« back to all changes in this revision

Viewing changes to trampoline/trampoline.c

  • Committer: mattgiuca
  • Date: 2008-01-31 01:44:30 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:345
Global CSS change: ivlebody no longer has 1em of padding (it has none).
This is because most apps were disabling it (and it had to change anyway for
other reasons -- see below).

Hence, all apps which WERE disabling the padding have had that removed, and
just work by default. (console, browser, tutorial)
All apps which WEREN'T disabling the padding (very few) now have to manually
include an extra div. This has been done on all such apps, and has been
heavily documented (both in the CSS file and doc/app_howto). (help, dummy,
debuginfo).

media/common/ivle.css: 
    The real change here (which isn't yet being used) is that ivlebody is now
    positioned absolutely and takes up all the space on the canvas. This is
    to be used for full-page layouts in console and browser.

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>
40
43
 
41
44
/* conf.h is admin-configured by the setup process.
42
45
 * It defines jail_base.
43
46
 */
44
47
#include "conf.h"
45
48
 
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
 
 
56
49
/* Returns TRUE if the given uid is allowed to execute trampoline.
57
50
 * Only root or the web server should be allowed to execute.
58
51
 * This is determined by the whitelist allowed_uids in conf.h.
73
66
    return 0;
74
67
}
75
68
 
 
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
 
76
119
int main(int argc, char* const argv[])
77
120
{
78
121
    char* jailpath;
 
122
    char* work_dir;
 
123
    char* prog;
 
124
    char* const * args;
79
125
    int uid;
 
126
    int arg_num = 1;
 
127
    int daemon_mode = 0;
 
128
    char canonical_jailpath[PATH_MAX];
80
129
 
81
130
    /* Disallow execution from all users but the whitelisted ones, and root */
82
131
    if (!uid_allowed(getuid()))
86
135
    }
87
136
 
88
137
    /* Args check and usage */
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
 
    }
 
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;
95
157
 
96
158
    /* Disallow suiding to the root user */
97
 
    uid = atoi(argv[ARG_UID]);
98
 
    if (uid == UID_ROOT)
 
159
    if (uid == 0)
99
160
    {
100
161
        fprintf(stderr, "cannot set up a jail as root\n");
101
162
        exit(1);
102
163
    }
103
164
 
104
 
    /* Jail path must:
105
 
     * Be non-empty
106
 
     * Start with a '/'
107
 
     * Not contain "/.."
108
 
     * Begin with jail_base
 
165
    /* Jail path must be an absolute path,
 
166
     * and it must begin with jail_base.
109
167
     */
110
 
    jailpath = argv[ARG_JAILPATH];
111
 
    if (strlen(jailpath) < 1 || jailpath[0] != '/'
112
 
            || strstr(jailpath, "/..")
113
 
            || strncmp(jailpath, jail_base, strlen(jail_base)))
 
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)))
114
174
    {
115
175
        fprintf(stderr, "bad jail path: %s\n", jailpath);
116
176
        exit(1);
118
178
 
119
179
    /* chroot into the jail.
120
180
     * Henceforth this process, and its children, cannot see anything above
121
 
     * jailpath. */
122
 
    if (chroot(jailpath))
 
181
     * canoncial_jailpath. */
 
182
    if (chroot(canonical_jailpath))
123
183
    {
124
184
        perror("could not chroot");
125
185
        exit(1);
126
186
    }
127
187
 
128
188
    /* chdir into the specified working directory */
129
 
    if (chdir(argv[ARG_CWD]))
 
189
    if (chdir(work_dir))
130
190
    {
131
191
        perror("could not chdir");
132
192
        exit(1);
141
201
        exit(1);
142
202
    }
143
203
 
 
204
    if (daemon_mode)
 
205
    {
 
206
        daemonize();
 
207
    }
 
208
 
144
209
    /* exec (replace this process with the a new instance of the target
145
210
     * program). Pass along all the arguments.
146
211
     * Note that for script execution, the "program" will be the interpreter,
147
212
     * and the first argument will be the script. */
148
 
    execv(argv[ARG_PROG], argv + ARG_PROG);
 
213
    execv(prog, args);
149
214
 
 
215
    /* XXX if (daemon_mode) use syslog? */
150
216
    /* nb exec won't return unless there was an error */
151
217
    perror("could not exec");
152
 
    return EXIT_FAILURE;
 
218
    return 1;
153
219
}