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

« back to all changes in this revision

Viewing changes to trampoline/trampoline.c

  • Committer: drtomc
  • Date: 2008-01-30 09:15:05 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:334
WIP only!
Some path normalization code. We need this because our current path tests are
inadequate, and we'll need them in a couple of places, so I figured it was
time to do it properly.

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