*: add comment about APPLET_ODDNAME format
[oweals/busybox.git] / runit / chpst.c
1 /*
2 Copyright (c) 2001-2006, Gerrit Pape
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8    1. Redistributions of source code must retain the above copyright notice,
9       this list of conditions and the following disclaimer.
10    2. Redistributions in binary form must reproduce the above copyright
11       notice, this list of conditions and the following disclaimer in the
12       documentation and/or other materials provided with the distribution.
13    3. The name of the author may not be used to endorse or promote products
14       derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /* Busyboxed by Denys Vlasenko <vda.linux@googlemail.com> */
29
30 //config:config CHPST
31 //config:       bool "chpst"
32 //config:       default y
33 //config:       help
34 //config:         chpst changes the process state according to the given options, and
35 //config:         execs specified program.
36 //config:
37 //config:config SETUIDGID
38 //config:       bool "setuidgid"
39 //config:       default y
40 //config:       help
41 //config:         Sets soft resource limits as specified by options
42 //config:
43 //config:config ENVUIDGID
44 //config:       bool "envuidgid"
45 //config:       default y
46 //config:       help
47 //config:         Sets $UID to account's uid and $GID to account's gid
48 //config:
49 //config:config ENVDIR
50 //config:       bool "envdir"
51 //config:       default y
52 //config:       help
53 //config:         Sets various environment variables as specified by files
54 //config:         in the given directory
55 //config:
56 //config:config SOFTLIMIT
57 //config:       bool "softlimit"
58 //config:       default y
59 //config:       help
60 //config:         Sets soft resource limits as specified by options
61
62 //applet:IF_CHPST(APPLET(chpst, BB_DIR_USR_BIN, BB_SUID_DROP))
63 //                    APPLET_ODDNAME:name       main   location        suid_type     help
64 //applet:IF_ENVDIR(   APPLET_ODDNAME(envdir,    chpst, BB_DIR_USR_BIN, BB_SUID_DROP, envdir))
65 //applet:IF_ENVUIDGID(APPLET_ODDNAME(envuidgid, chpst, BB_DIR_USR_BIN, BB_SUID_DROP, envuidgid))
66 //applet:IF_SETUIDGID(APPLET_ODDNAME(setuidgid, chpst, BB_DIR_USR_BIN, BB_SUID_DROP, setuidgid))
67 //applet:IF_SOFTLIMIT(APPLET_ODDNAME(softlimit, chpst, BB_DIR_USR_BIN, BB_SUID_DROP, softlimit))
68
69 //kbuild:lib-$(CONFIG_CHPST) += chpst.o
70 //kbuild:lib-$(CONFIG_ENVDIR) += chpst.o
71 //kbuild:lib-$(CONFIG_ENVUIDGID) += chpst.o
72 //kbuild:lib-$(CONFIG_SETUIDGID) += chpst.o
73 //kbuild:lib-$(CONFIG_SOFTLIMIT) += chpst.o
74
75 //usage:#define chpst_trivial_usage
76 //usage:       "[-vP012] [-u USER[:GRP]] [-U USER[:GRP]] [-e DIR]\n"
77 //usage:       "        [-/ DIR] [-n NICE] [-m BYTES] [-d BYTES] [-o N]\n"
78 //usage:       "        [-p N] [-f BYTES] [-c BYTES] PROG ARGS"
79 //usage:#define chpst_full_usage "\n\n"
80 //usage:       "Change the process state, run PROG\n"
81 //usage:     "\n        -u USER[:GRP]   Set uid and gid"
82 //usage:     "\n        -U USER[:GRP]   Set $UID and $GID in environment"
83 //usage:     "\n        -e DIR          Set environment variables as specified by files"
84 //usage:     "\n                        in DIR: file=1st_line_of_file"
85 //usage:     "\n        -/ DIR          Chroot to DIR"
86 //usage:     "\n        -n NICE         Add NICE to nice value"
87 //usage:     "\n        -m BYTES        Same as -d BYTES -s BYTES -l BYTES"
88 //usage:     "\n        -d BYTES        Limit data segment"
89 //usage:     "\n        -o N            Limit number of open files per process"
90 //usage:     "\n        -p N            Limit number of processes per uid"
91 //usage:     "\n        -f BYTES        Limit output file sizes"
92 //usage:     "\n        -c BYTES        Limit core file size"
93 //usage:     "\n        -v              Verbose"
94 //usage:     "\n        -P              Create new process group"
95 //usage:     "\n        -0              Close stdin"
96 //usage:     "\n        -1              Close stdout"
97 //usage:     "\n        -2              Close stderr"
98 //usage:
99 //usage:#define envdir_trivial_usage
100 //usage:       "DIR PROG ARGS"
101 //usage:#define envdir_full_usage "\n\n"
102 //usage:       "Set various environment variables as specified by files\n"
103 //usage:       "in the directory DIR, run PROG"
104 //usage:
105 //usage:#define envuidgid_trivial_usage
106 //usage:       "USER PROG ARGS"
107 //usage:#define envuidgid_full_usage "\n\n"
108 //usage:       "Set $UID to USER's uid and $GID to USER's gid, run PROG"
109 //usage:
110 //usage:#define setuidgid_trivial_usage
111 //usage:       "USER PROG ARGS"
112 //usage:#define setuidgid_full_usage "\n\n"
113 //usage:       "Set uid and gid to USER's uid and gid, drop supplementary group ids,\n"
114 //usage:       "run PROG"
115 //usage:
116 //usage:#define softlimit_trivial_usage
117 //usage:       "[-a BYTES] [-m BYTES] [-d BYTES] [-s BYTES] [-l BYTES]\n"
118 //usage:       "        [-f BYTES] [-c BYTES] [-r BYTES] [-o N] [-p N] [-t N]\n"
119 //usage:       "        PROG ARGS"
120 //usage:#define softlimit_full_usage "\n\n"
121 //usage:       "Set soft resource limits, then run PROG\n"
122 //usage:     "\n        -a BYTES        Limit total size of all segments"
123 //usage:     "\n        -m BYTES        Same as -d BYTES -s BYTES -l BYTES -a BYTES"
124 //usage:     "\n        -d BYTES        Limit data segment"
125 //usage:     "\n        -s BYTES        Limit stack segment"
126 //usage:     "\n        -l BYTES        Limit locked memory size"
127 //usage:     "\n        -o N            Limit number of open files per process"
128 //usage:     "\n        -p N            Limit number of processes per uid"
129 //usage:     "\nOptions controlling file sizes:"
130 //usage:     "\n        -f BYTES        Limit output file sizes"
131 //usage:     "\n        -c BYTES        Limit core file size"
132 //usage:     "\nEfficiency opts:"
133 //usage:     "\n        -r BYTES        Limit resident set size"
134 //usage:     "\n        -t N            Limit CPU time, process receives"
135 //usage:     "\n                        a SIGXCPU after N seconds"
136
137 #include "libbb.h"
138 #include <sys/resource.h> /* getrlimit */
139
140 /*
141 Five applets here: chpst, envdir, envuidgid, setuidgid, softlimit.
142
143 Only softlimit and chpst are taking options:
144
145 # common
146 -o N            Limit number of open files per process
147 -p N            Limit number of processes per uid
148 -m BYTES        Same as -d BYTES -s BYTES -l BYTES [-a BYTES]
149 -d BYTES        Limit data segment
150 -f BYTES        Limit output file sizes
151 -c BYTES        Limit core file size
152 # softlimit
153 -a BYTES        Limit total size of all segments
154 -s BYTES        Limit stack segment
155 -l BYTES        Limit locked memory size
156 -r BYTES        Limit resident set size
157 -t N            Limit CPU time
158 # chpst
159 -u USER[:GRP]   Set uid and gid
160 -U USER[:GRP]   Set $UID and $GID in environment
161 -e DIR          Set environment variables as specified by files in DIR
162 -/ DIR          Chroot to DIR
163 -n NICE         Add NICE to nice value
164 -v              Verbose
165 -P              Create new process group
166 -0 -1 -2        Close fd 0,1,2
167
168 Even though we accept all these options for both softlimit and chpst,
169 they are not to be advertised on their help texts.
170 We have enough problems with feature creep in other people's
171 software, don't want to add our own.
172
173 envdir, envuidgid, setuidgid take no options, but they reuse code which
174 handles -e, -U and -u.
175 */
176
177 enum {
178         OPT_a = (1 << 0) * ENABLE_SOFTLIMIT,
179         OPT_c = (1 << 1) * (ENABLE_SOFTLIMIT || ENABLE_CHPST),
180         OPT_d = (1 << 2) * (ENABLE_SOFTLIMIT || ENABLE_CHPST),
181         OPT_f = (1 << 3) * (ENABLE_SOFTLIMIT || ENABLE_CHPST),
182         OPT_l = (1 << 4) * ENABLE_SOFTLIMIT,
183         OPT_m = (1 << 5) * (ENABLE_SOFTLIMIT || ENABLE_CHPST),
184         OPT_o = (1 << 6) * (ENABLE_SOFTLIMIT || ENABLE_CHPST),
185         OPT_p = (1 << 7) * (ENABLE_SOFTLIMIT || ENABLE_CHPST),
186         OPT_r = (1 << 8) * ENABLE_SOFTLIMIT,
187         OPT_s = (1 << 9) * ENABLE_SOFTLIMIT,
188         OPT_t = (1 << 10) * ENABLE_SOFTLIMIT,
189         OPT_u = (1 << 11) * (ENABLE_CHPST || ENABLE_SETUIDGID),
190         OPT_U = (1 << 12) * (ENABLE_CHPST || ENABLE_ENVUIDGID),
191         OPT_e = (1 << 13) * (ENABLE_CHPST || ENABLE_ENVDIR),
192         OPT_root = (1 << 14) * ENABLE_CHPST,
193         OPT_n = (1 << 15) * ENABLE_CHPST,
194         OPT_v = (1 << 16) * ENABLE_CHPST,
195         OPT_P = (1 << 17) * ENABLE_CHPST,
196         OPT_0 = (1 << 18) * ENABLE_CHPST,
197         OPT_1 = (1 << 19) * ENABLE_CHPST,
198         OPT_2 = (1 << 20) * ENABLE_CHPST,
199 };
200
201 /* TODO: use recursive_action? */
202 static NOINLINE void edir(const char *directory_name)
203 {
204         int wdir;
205         DIR *dir;
206         struct dirent *d;
207         int fd;
208
209         wdir = xopen(".", O_RDONLY | O_NDELAY);
210         xchdir(directory_name);
211         dir = xopendir(".");
212         for (;;) {
213                 char buf[256];
214                 char *tail;
215                 int size;
216
217                 errno = 0;
218                 d = readdir(dir);
219                 if (!d) {
220                         if (errno)
221                                 bb_perror_msg_and_die("readdir %s",
222                                                 directory_name);
223                         break;
224                 }
225                 if (d->d_name[0] == '.')
226                         continue;
227                 fd = open(d->d_name, O_RDONLY | O_NDELAY);
228                 if (fd < 0) {
229                         if ((errno == EISDIR) && directory_name) {
230                                 if (option_mask32 & OPT_v)
231                                         bb_perror_msg("warning: %s/%s is a directory",
232                                                 directory_name, d->d_name);
233                                 continue;
234                         }
235                         bb_perror_msg_and_die("open %s/%s",
236                                                 directory_name, d->d_name);
237                 }
238                 size = full_read(fd, buf, sizeof(buf)-1);
239                 close(fd);
240                 if (size < 0)
241                         bb_perror_msg_and_die("read %s/%s",
242                                         directory_name, d->d_name);
243                 if (size == 0) {
244                         unsetenv(d->d_name);
245                         continue;
246                 }
247                 buf[size] = '\n';
248                 tail = strchr(buf, '\n');
249                 /* skip trailing whitespace */
250                 while (1) {
251                         *tail = '\0';
252                         tail--;
253                         if (tail < buf || !isspace(*tail))
254                                 break;
255                 }
256                 xsetenv(d->d_name, buf);
257         }
258         closedir(dir);
259         xfchdir(wdir);
260         close(wdir);
261 }
262
263 static void limit(int what, long l)
264 {
265         struct rlimit r;
266
267         /* Never fails under Linux (except if you pass it bad arguments) */
268         getrlimit(what, &r);
269         if ((l < 0) || (l > r.rlim_max))
270                 r.rlim_cur = r.rlim_max;
271         else
272                 r.rlim_cur = l;
273         if (setrlimit(what, &r) == -1)
274                 bb_perror_msg_and_die("setrlimit");
275 }
276
277 int chpst_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
278 int chpst_main(int argc UNUSED_PARAM, char **argv)
279 {
280         struct bb_uidgid_t ugid;
281         char *set_user = set_user; /* for compiler */
282         char *env_dir = env_dir;
283         char *root;
284         char *nicestr;
285         unsigned limita;
286         unsigned limitc;
287         unsigned limitd;
288         unsigned limitf;
289         unsigned limitl;
290         unsigned limitm;
291         unsigned limito;
292         unsigned limitp;
293         unsigned limitr;
294         unsigned limits;
295         unsigned limitt;
296         unsigned opt;
297
298         if ((ENABLE_CHPST && applet_name[0] == 'c')
299          || (ENABLE_SOFTLIMIT && applet_name[1] == 'o')
300         ) {
301                 // FIXME: can we live with int-sized limits?
302                 // can we live with 40000 days?
303                 // if yes -> getopt converts strings to numbers for us
304                 opt_complementary = "-1";
305                 opt = getopt32(argv, "+a:+c:+d:+f:+l:+m:+o:+p:+r:+s:+t:+u:U:e:"
306                         IF_CHPST("/:n:vP012"),
307                         &limita, &limitc, &limitd, &limitf, &limitl,
308                         &limitm, &limito, &limitp, &limitr, &limits, &limitt,
309                         &set_user, &set_user, &env_dir
310                         IF_CHPST(, &root, &nicestr));
311                 argv += optind;
312                 if (opt & OPT_m) { // -m means -asld
313                         limita = limits = limitl = limitd = limitm;
314                         opt |= (OPT_s | OPT_l | OPT_a | OPT_d);
315                 }
316         } else {
317                 option_mask32 = opt = 0;
318                 argv++;
319                 if (!*argv)
320                         bb_show_usage();
321         }
322
323         // envdir?
324         if (ENABLE_ENVDIR && applet_name[3] == 'd') {
325                 env_dir = *argv++;
326                 opt |= OPT_e;
327         }
328
329         // setuidgid?
330         if (ENABLE_SETUIDGID && applet_name[1] == 'e') {
331                 set_user = *argv++;
332                 opt |= OPT_u;
333         }
334
335         // envuidgid?
336         if (ENABLE_ENVUIDGID && applet_name[0] == 'e' && applet_name[3] == 'u') {
337                 set_user = *argv++;
338                 opt |= OPT_U;
339         }
340
341         // we must have PROG [ARGS]
342         if (!*argv)
343                 bb_show_usage();
344
345         // set limits
346         if (opt & OPT_d) {
347 #ifdef RLIMIT_DATA
348                 limit(RLIMIT_DATA, limitd);
349 #else
350                 if (opt & OPT_v)
351                         bb_error_msg("system does not support RLIMIT_%s",
352                                 "DATA");
353 #endif
354         }
355         if (opt & OPT_s) {
356 #ifdef RLIMIT_STACK
357                 limit(RLIMIT_STACK, limits);
358 #else
359                 if (opt & OPT_v)
360                         bb_error_msg("system does not support RLIMIT_%s",
361                                 "STACK");
362 #endif
363         }
364         if (opt & OPT_l) {
365 #ifdef RLIMIT_MEMLOCK
366                 limit(RLIMIT_MEMLOCK, limitl);
367 #else
368                 if (opt & OPT_v)
369                         bb_error_msg("system does not support RLIMIT_%s",
370                                 "MEMLOCK");
371 #endif
372         }
373         if (opt & OPT_a) {
374 #ifdef RLIMIT_VMEM
375                 limit(RLIMIT_VMEM, limita);
376 #else
377 #ifdef RLIMIT_AS
378                 limit(RLIMIT_AS, limita);
379 #else
380                 if (opt & OPT_v)
381                         bb_error_msg("system does not support RLIMIT_%s",
382                                 "VMEM");
383 #endif
384 #endif
385         }
386         if (opt & OPT_o) {
387 #ifdef RLIMIT_NOFILE
388                 limit(RLIMIT_NOFILE, limito);
389 #else
390 #ifdef RLIMIT_OFILE
391                 limit(RLIMIT_OFILE, limito);
392 #else
393                 if (opt & OPT_v)
394                         bb_error_msg("system does not support RLIMIT_%s",
395                                 "NOFILE");
396 #endif
397 #endif
398         }
399         if (opt & OPT_p) {
400 #ifdef RLIMIT_NPROC
401                 limit(RLIMIT_NPROC, limitp);
402 #else
403                 if (opt & OPT_v)
404                         bb_error_msg("system does not support RLIMIT_%s",
405                                 "NPROC");
406 #endif
407         }
408         if (opt & OPT_f) {
409 #ifdef RLIMIT_FSIZE
410                 limit(RLIMIT_FSIZE, limitf);
411 #else
412                 if (opt & OPT_v)
413                         bb_error_msg("system does not support RLIMIT_%s",
414                                 "FSIZE");
415 #endif
416         }
417         if (opt & OPT_c) {
418 #ifdef RLIMIT_CORE
419                 limit(RLIMIT_CORE, limitc);
420 #else
421                 if (opt & OPT_v)
422                         bb_error_msg("system does not support RLIMIT_%s",
423                                 "CORE");
424 #endif
425         }
426         if (opt & OPT_r) {
427 #ifdef RLIMIT_RSS
428                 limit(RLIMIT_RSS, limitr);
429 #else
430                 if (opt & OPT_v)
431                         bb_error_msg("system does not support RLIMIT_%s",
432                                 "RSS");
433 #endif
434         }
435         if (opt & OPT_t) {
436 #ifdef RLIMIT_CPU
437                 limit(RLIMIT_CPU, limitt);
438 #else
439                 if (opt & OPT_v)
440                         bb_error_msg("system does not support RLIMIT_%s",
441                                 "CPU");
442 #endif
443         }
444
445         if (opt & OPT_P)
446                 setsid();
447
448         if (opt & OPT_e)
449                 edir(env_dir);
450
451         if (opt & (OPT_u|OPT_U))
452                 xget_uidgid(&ugid, set_user);
453
454         // chrooted jail must have /etc/passwd if we move this after chroot.
455         // OTOH chroot fails for non-roots.
456         // Solution: cache uid/gid before chroot, apply uid/gid after.
457         if (opt & OPT_U) {
458                 xsetenv("GID", utoa(ugid.gid));
459                 xsetenv("UID", utoa(ugid.uid));
460         }
461
462         if (opt & OPT_root) {
463                 xchroot(root);
464         }
465
466         if (opt & OPT_u) {
467                 if (setgroups(1, &ugid.gid) == -1)
468                         bb_perror_msg_and_die("setgroups");
469                 xsetgid(ugid.gid);
470                 xsetuid(ugid.uid);
471         }
472
473         if (opt & OPT_n) {
474                 errno = 0;
475                 if (nice(xatoi(nicestr)) == -1)
476                         bb_perror_msg_and_die("nice");
477         }
478
479         if (opt & OPT_0)
480                 close(STDIN_FILENO);
481         if (opt & OPT_1)
482                 close(STDOUT_FILENO);
483         if (opt & OPT_2)
484                 close(STDERR_FILENO);
485
486         BB_EXECVP_or_die(argv);
487 }