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

« back to all changes in this revision

Viewing changes to trampoline/trampoline.c

  • Committer: mattgiuca
  • Date: 2007-12-20 05:25:03 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:103
Fix to Makefile.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
 */
44
44
#include "conf.h"
45
45
 
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
 
}
 
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
115
55
 
116
56
int main(int argc, char* const argv[])
117
57
{
118
58
    char* jailpath;
119
 
    char* work_dir;
120
 
    char* prog;
121
 
    char* const * args;
122
59
    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
 
 
133
60
    /* Args check and usage */
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;
 
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
    }
153
67
 
154
68
    /* Disallow suiding to the root user */
155
 
    if (uid == 0)
 
69
    uid = atoi(argv[ARG_UID]);
 
70
    if (uid == UID_ROOT)
156
71
    {
157
72
        fprintf(stderr, "cannot set up a jail as root\n");
158
73
        exit(1);
164
79
     * Not contain "/.."
165
80
     * Begin with jail_base
166
81
     */
 
82
    jailpath = argv[ARG_JAILPATH];
167
83
    if (strlen(jailpath) < 1 || jailpath[0] != '/'
168
84
            || strstr(jailpath, "/..")
169
85
            || strncmp(jailpath, jail_base, strlen(jail_base)))
182
98
    }
183
99
 
184
100
    /* chdir into the specified working directory */
185
 
    if (chdir(work_dir))
 
101
    if (chdir(argv[ARG_CWD]))
186
102
    {
187
103
        perror("could not chdir");
188
104
        exit(1);
197
113
        exit(1);
198
114
    }
199
115
 
200
 
    if (daemon_mode)
201
 
    {
202
 
        daemonize();
203
 
    }
204
 
 
205
116
    /* exec (replace this process with the a new instance of the target
206
117
     * program). Pass along all the arguments.
207
118
     * Note that for script execution, the "program" will be the interpreter,
208
119
     * and the first argument will be the script. */
209
 
    execv(prog, args);
 
120
    execv(argv[ARG_PROG], argv + ARG_PROG);
210
121
 
211
 
    /* XXX if (daemon_mode) use syslog? */
212
122
    /* nb exec won't return unless there was an error */
213
123
    perror("could not exec");
214
 
    return 1;
 
124
    return EXIT_FAILURE;
215
125
}