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

« back to all changes in this revision

Viewing changes to trampoline/trampoline.c

  • Committer: dilshan_a
  • Date: 2008-01-25 00:20:21 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:295
Added a filesum problem.
Updated paths in test.py and test.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
 */
44
44
#include "conf.h"
45
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
 
46
/* Returns TRUE if the given uid is allowed to execute trampoline.
 
47
 * Only root or the web server should be allowed to execute.
 
48
 * This is determined by the whitelist allowed_uids in conf.h.
 
49
 */
 
50
int uid_allowed(int uid)
 
51
{
 
52
    int i;
 
53
    /* root is always allowed to execute trampoline */
 
54
    if (uid == 0)
 
55
        return 1;
 
56
    /* loop over all allowed_uids */
 
57
    for (i=(sizeof(allowed_uids)/sizeof(*allowed_uids))-1; i>=0; i--)
 
58
    {
 
59
        if (allowed_uids[i] == uid)
 
60
            return 1;
 
61
    }
 
62
    /* default to disallowing */
 
63
    return 0;
 
64
}
 
65
 
 
66
/* Turn the process into a daemon using the standard
 
67
 * 2-fork technique.
 
68
 */
 
69
void daemonize(void)
 
70
{
 
71
    pid_t pid, sid;
 
72
 
 
73
    /* already a daemon */
 
74
    if ( getppid() == 1 ) return;
 
75
 
 
76
    /* Fork off the parent process */
 
77
    pid = fork();
 
78
    if (pid < 0) {
 
79
        exit(1);
 
80
    }
 
81
    /* If we got a good PID, then we can exit the parent process. */
 
82
    if (pid > 0) {
 
83
        exit(0);
 
84
    }
 
85
 
 
86
    /* At this point we are executing as the child process */
 
87
 
 
88
    /* Change the file mode mask */
 
89
    umask(0);
 
90
 
 
91
    /* Create a new SID for the child process */
 
92
    sid = setsid();
 
93
    if (sid < 0) {
 
94
        exit(1);
 
95
    }
 
96
 
 
97
    /* Change the current working directory.  This prevents the current
 
98
       directory from being locked; hence not being able to remove it. */
 
99
    if ((chdir("/")) < 0) {
 
100
        exit(1);
 
101
    }
 
102
 
 
103
    /* Redirect standard files to /dev/null */
 
104
    freopen( "/dev/null", "r", stdin);
 
105
    freopen( "/dev/null", "w", stdout);
 
106
    freopen( "/dev/null", "w", stderr);
 
107
}
 
108
 
 
109
static void usage(const char* nm)
 
110
{
 
111
    fprintf(stderr,
 
112
        "usage: %s [-d] <uid> <jail> <cwd> <program> [args...]\n", nm);
 
113
    exit(1);
 
114
}
55
115
 
56
116
int main(int argc, char* const argv[])
57
117
{
58
118
    char* jailpath;
 
119
    char* work_dir;
 
120
    char* prog;
 
121
    char* const * args;
59
122
    int uid;
 
123
    int arg_num = 1;
 
124
    int daemon_mode = 0;
 
125
 
 
126
    /* Disallow execution from all users but the whitelisted ones, and root */
 
127
    if (!uid_allowed(getuid()))
 
128
    {
 
129
        fprintf(stderr, "only the web server may execute trampoline\n");
 
130
        exit(1);
 
131
    }
 
132
 
60
133
    /* Args check and usage */
61
 
    if (argc < MIN_ARGC)
62
 
    {
63
 
        fprintf(stderr, "usage: %s <uid> <jail> <cwd> <program> [args...]\n",
64
 
            argv[0]);
65
 
        exit(EXIT_FAILURE);
66
 
    }
 
134
    if (argc < 5)
 
135
    {
 
136
        usage(argv[0]);
 
137
    }
 
138
 
 
139
    if (strcmp(argv[arg_num], "-d") == 0)
 
140
    {
 
141
        if (argc < 6)
 
142
        {
 
143
            usage(argv[0]);
 
144
        }
 
145
        daemon_mode = 1;
 
146
        arg_num++;
 
147
    }
 
148
    uid = atoi(argv[arg_num++]);
 
149
    jailpath = argv[arg_num++];
 
150
    work_dir = argv[arg_num++];
 
151
    prog = argv[arg_num];
 
152
    args = argv + arg_num;
67
153
 
68
154
    /* Disallow suiding to the root user */
69
 
    uid = atoi(argv[ARG_UID]);
70
 
    if (uid == UID_ROOT)
 
155
    if (uid == 0)
71
156
    {
72
157
        fprintf(stderr, "cannot set up a jail as root\n");
73
158
        exit(1);
79
164
     * Not contain "/.."
80
165
     * Begin with jail_base
81
166
     */
82
 
    jailpath = argv[ARG_JAILPATH];
83
167
    if (strlen(jailpath) < 1 || jailpath[0] != '/'
84
168
            || strstr(jailpath, "/..")
85
169
            || strncmp(jailpath, jail_base, strlen(jail_base)))
98
182
    }
99
183
 
100
184
    /* chdir into the specified working directory */
101
 
    if (chdir(argv[ARG_CWD]))
 
185
    if (chdir(work_dir))
102
186
    {
103
187
        perror("could not chdir");
104
188
        exit(1);
113
197
        exit(1);
114
198
    }
115
199
 
 
200
    if (daemon_mode)
 
201
    {
 
202
        daemonize();
 
203
    }
 
204
 
116
205
    /* exec (replace this process with the a new instance of the target
117
206
     * program). Pass along all the arguments.
118
207
     * Note that for script execution, the "program" will be the interpreter,
119
208
     * and the first argument will be the script. */
120
 
    execv(argv[ARG_PROG], argv + ARG_PROG);
 
209
    execv(prog, args);
121
210
 
 
211
    /* XXX if (daemon_mode) use syslog? */
122
212
    /* nb exec won't return unless there was an error */
123
213
    perror("could not exec");
124
 
    return EXIT_FAILURE;
 
214
    return 1;
125
215
}