attempt to regularize atoi mess.
[platform/upstream/busybox.git] / runit / chpst.c
1 #include "busybox.h"
2
3 #include <dirent.h>
4
5 // Must match constants in chpst_main!
6 #define OPT_verbose  (option_mask32 & 0x2000)
7 #define OPT_pgrp     (option_mask32 & 0x4000)
8 #define OPT_nostdin  (option_mask32 & 0x8000)
9 #define OPT_nostdout (option_mask32 & 0x10000)
10 #define OPT_nostderr (option_mask32 & 0x20000)
11
12 static char *set_user;
13 static char *env_user;
14 static const char *env_dir;
15 static long limitd = -2;
16 static long limits = -2;
17 static long limitl = -2;
18 static long limita = -2;
19 static long limito = -2;
20 static long limitp = -2;
21 static long limitf = -2;
22 static long limitc = -2;
23 static long limitr = -2;
24 static long limitt = -2;
25 static int nicelvl;
26 static const char *root;
27
28 static void suidgid(char *user)
29 {
30         struct bb_uidgid_t ugid;
31
32         if (!uidgid_get(&ugid, user)) {
33                 bb_error_msg_and_die("unknown user/group: %s", user);
34         }
35         if (setgroups(1, &ugid.gid) == -1)
36                 bb_perror_msg_and_die("setgroups");
37         xsetgid(ugid.gid);
38         xsetuid(ugid.uid);
39 }
40
41 static void euidgid(char *user)
42 {
43         struct bb_uidgid_t ugid;
44
45         if (!uidgid_get(&ugid, user)) {
46                 bb_error_msg_and_die("unknown user/group: %s", user);
47         }
48         xsetenv("GID", utoa(ugid.gid));
49         xsetenv("UID", utoa(ugid.uid));
50 }
51
52 static void edir(const char *directory_name)
53 {
54         int wdir;
55         DIR *dir;
56         struct dirent *d;
57         int fd;
58
59         wdir = xopen(".", O_RDONLY | O_NDELAY);
60         xchdir(directory_name);
61         dir = opendir(".");
62         if (!dir)
63                 bb_perror_msg_and_die("opendir %s", directory_name);
64         for (;;) {
65                 errno = 0;
66                 d = readdir(dir);
67                 if (!d) {
68                         if (errno) bb_perror_msg_and_die("readdir %s", directory_name);
69                         break;
70                 }
71                 if (d->d_name[0] == '.') continue;
72                 fd = open(d->d_name, O_RDONLY | O_NDELAY);
73                 if (fd < 0) {
74                         if ((errno == EISDIR) && env_dir) {
75                                 if (OPT_verbose)
76                                         bb_perror_msg("warning: %s/%s is a directory", directory_name,
77                                                 d->d_name);
78                                 continue;
79                         } else
80                                 bb_perror_msg_and_die("open %s/%s", directory_name, /* was exiting 111 */
81                                                      d->d_name);
82                 }
83                 if (fd >= 0) {
84                         char buf[256];
85                         char *tail;
86                         int size;
87
88                         size = safe_read(fd, buf, sizeof(buf)-1);
89                         if (size < 0)
90                                 bb_perror_msg_and_die("read %s/%s", directory_name, /* was exiting 111 */
91                                                 d->d_name);
92                         if (size == 0) {
93                                 unsetenv(d->d_name);
94                                 continue;
95                         }
96                         buf[size] = '\n';
97                         tail = memchr(buf, '\n', sizeof(buf));
98                         /* skip trailing whitespace */;
99                         while (1) {
100                                 if (tail[0]==' ') tail[0] = '\0';
101                                 if (tail[0]=='\t') tail[0] = '\0';
102                                 if (tail[0]=='\n') tail[0] = '\0';
103                                 if (tail == buf) break;
104                                 tail--;
105                         }
106                         xsetenv(d->d_name, buf);
107                 }
108         }
109         closedir(dir);
110         if (fchdir(wdir) == -1) bb_perror_msg_and_die("fchdir");
111         close(wdir);
112 }
113
114 static void limit(int what, long l)
115 {
116         struct rlimit r;
117
118         if (getrlimit(what, &r) == -1) bb_perror_msg_and_die("getrlimit");
119         if ((l < 0) || (l > r.rlim_max))
120                 r.rlim_cur = r.rlim_max;
121         else
122                 r.rlim_cur = l;
123         if (setrlimit(what, &r) == -1) bb_perror_msg_and_die("setrlimit");
124 }
125
126 static void slimit(void)
127 {
128         if (limitd >= -1) {
129 #ifdef RLIMIT_DATA
130                 limit(RLIMIT_DATA, limitd);
131 #else
132                 if (OPT_verbose) bb_error_msg("system does not support %s", "RLIMIT_DATA");
133 #endif
134         }
135         if (limits >= -1) {
136 #ifdef RLIMIT_STACK
137                 limit(RLIMIT_STACK, limits);
138 #else
139                 if (OPT_verbose) bb_error_msg("system does not support %s", "RLIMIT_STACK");
140 #endif
141         }
142         if (limitl >= -1) {
143 #ifdef RLIMIT_MEMLOCK
144                 limit(RLIMIT_MEMLOCK, limitl);
145 #else
146                 if (OPT_verbose) bb_error_msg("system does not support %s", "RLIMIT_MEMLOCK");
147 #endif
148         }
149         if (limita >= -1) {
150 #ifdef RLIMIT_VMEM
151                 limit(RLIMIT_VMEM, limita);
152 #else
153 #ifdef RLIMIT_AS
154                 limit(RLIMIT_AS, limita);
155 #else
156                 if (OPT_verbose)
157                         bb_error_msg("system does not support %s", "RLIMIT_VMEM");
158 #endif
159 #endif
160         }
161         if (limito >= -1) {
162 #ifdef RLIMIT_NOFILE
163                 limit(RLIMIT_NOFILE, limito);
164 #else
165 #ifdef RLIMIT_OFILE
166                 limit(RLIMIT_OFILE, limito);
167 #else
168                 if (OPT_verbose)
169                         bb_error_msg("system does not support %s", "RLIMIT_NOFILE");
170 #endif
171 #endif
172         }
173         if (limitp >= -1) {
174 #ifdef RLIMIT_NPROC
175                 limit(RLIMIT_NPROC, limitp);
176 #else
177                 if (OPT_verbose) bb_error_msg("system does not support %s", "RLIMIT_NPROC");
178 #endif
179         }
180         if (limitf >= -1) {
181 #ifdef RLIMIT_FSIZE
182                 limit(RLIMIT_FSIZE, limitf);
183 #else
184                 if (OPT_verbose) bb_error_msg("system does not support %s", "RLIMIT_FSIZE");
185 #endif
186         }
187         if (limitc >= -1) {
188 #ifdef RLIMIT_CORE
189                 limit(RLIMIT_CORE, limitc);
190 #else
191                 if (OPT_verbose) bb_error_msg("system does not support %s", "RLIMIT_CORE");
192 #endif
193         }
194         if (limitr >= -1) {
195 #ifdef RLIMIT_RSS
196                 limit(RLIMIT_RSS, limitr);
197 #else
198                 if (OPT_verbose) bb_error_msg("system does not support %s", "RLIMIT_RSS");
199 #endif
200         }
201         if (limitt >= -1) {
202 #ifdef RLIMIT_CPU
203                 limit(RLIMIT_CPU, limitt);
204 #else
205                 if (OPT_verbose) bb_error_msg("system does not support %s", "RLIMIT_CPU");
206 #endif
207         }
208 }
209
210 /* argv[0] */
211 static void setuidgid(int, char **);
212 static void envuidgid(int, char **);
213 static void envdir(int, char **);
214 static void softlimit(int, char **);
215
216 int chpst_main(int argc, char **argv)
217 {
218         if (applet_name[3] == 'd') envdir(argc, argv);
219         if (applet_name[1] == 'o') softlimit(argc, argv);
220         if (applet_name[0] == 's') setuidgid(argc, argv);
221         if (applet_name[0] == 'e') envuidgid(argc, argv);
222         // otherwise we are.......... chpst
223
224         {
225                 char *m,*d,*o,*p,*f,*c,*r,*t,*n;
226                 getopt32(argc, argv, "u:U:e:m:d:o:p:f:c:r:t:/:n:vP012",
227                                 &set_user,&env_user,&env_dir,
228                                 &m,&d,&o,&p,&f,&c,&r,&t,&root,&n);
229                 // if (option_mask32 & 0x1) // -u
230                 // if (option_mask32 & 0x2) // -U
231                 // if (option_mask32 & 0x4) // -e
232                 if (option_mask32 & 0x8) limits = limitl = limita = limitd = xatoul(m); // -m
233                 if (option_mask32 & 0x10) limitd = xatoul(d); // -d
234                 if (option_mask32 & 0x20) limito = xatoul(o); // -o
235                 if (option_mask32 & 0x40) limitp = xatoul(p); // -p
236                 if (option_mask32 & 0x80) limitf = xatoul(f); // -f
237                 if (option_mask32 & 0x100) limitc = xatoul(c); // -c
238                 if (option_mask32 & 0x200) limitr = xatoul(r); // -r
239                 if (option_mask32 & 0x400) limitt = xatoul(t); // -t
240                 // if (option_mask32 & 0x800) // -/
241                 if (option_mask32 & 0x1000) nicelvl = xatoi(n); // -n
242                 // The below consts should match #defines at top!
243                 //if (option_mask32 & 0x2000) OPT_verbose = 1; // -v
244                 //if (option_mask32 & 0x4000) OPT_pgrp = 1; // -P
245                 //if (option_mask32 & 0x8000) OPT_nostdin = 1; // -0
246                 //if (option_mask32 & 0x10000) OPT_nostdout = 1; // -1
247                 //if (option_mask32 & 0x20000) OPT_nostderr = 1; // -2
248         }
249         argv += optind;
250         if (!argv || !*argv) bb_show_usage();
251         
252         if (OPT_pgrp) setsid();
253         if (env_dir) edir(env_dir);
254         if (root) {
255                 xchdir(root);
256                 if (chroot(".") == -1)
257                         bb_perror_msg_and_die("chroot");
258         }
259         slimit();
260         if (nicelvl) {
261                 errno = 0;
262                 if (nice(nicelvl) == -1)
263                         bb_perror_msg_and_die("nice");
264         }
265         if (env_user) euidgid(env_user);
266         if (set_user) suidgid(set_user);
267         if (OPT_nostdin) close(0);
268         if (OPT_nostdout) close(1);
269         if (OPT_nostderr) close(2);
270         execvp(argv[0], argv);
271         bb_perror_msg_and_die("exec %s", argv[0]);
272 }
273
274 static void setuidgid(int argc, char **argv)
275 {
276         const char *account;
277
278         account = *++argv;
279         if (!account) bb_show_usage();
280         if (!*++argv) bb_show_usage();
281         suidgid((char*)account);
282         execvp(argv[0], argv);
283         bb_perror_msg_and_die("exec %s", argv[0]);
284 }
285
286 static void envuidgid(int argc, char **argv)
287 {
288         const char *account;
289
290         account = *++argv;
291         if (!account) bb_show_usage();
292         if (!*++argv) bb_show_usage();
293         euidgid((char*)account);
294         execvp(argv[0], argv);
295         bb_perror_msg_and_die("exec %s", argv[0]);
296 }
297
298 static void envdir(int argc, char **argv)
299 {
300         const char *dir;
301
302         dir = *++argv;
303         if (!dir) bb_show_usage();
304         if (!*++argv) bb_show_usage();
305         edir(dir);
306         execvp(argv[0], argv);
307         bb_perror_msg_and_die("exec %s", argv[0]);
308 }
309
310 static void softlimit(int argc, char **argv)
311 {
312         char *a,*c,*d,*f,*l,*m,*o,*p,*r,*s,*t;
313         getopt32(argc, argv, "a:c:d:f:l:m:o:p:r:s:t:",
314                         &a,&c,&d,&f,&l,&m,&o,&p,&r,&s,&t);
315         if (option_mask32 & 0x001) limita = xatoul(a); // -a
316         if (option_mask32 & 0x002) limitc = xatoul(c); // -c
317         if (option_mask32 & 0x004) limitd = xatoul(d); // -d
318         if (option_mask32 & 0x008) limitf = xatoul(f); // -f
319         if (option_mask32 & 0x010) limitl = xatoul(l); // -l
320         if (option_mask32 & 0x020) limits = limitl = limita = limitd = xatoul(m); // -m
321         if (option_mask32 & 0x040) limito = xatoul(o); // -o
322         if (option_mask32 & 0x080) limitp = xatoul(p); // -p
323         if (option_mask32 & 0x100) limitr = xatoul(r); // -r
324         if (option_mask32 & 0x200) limits = xatoul(s); // -s
325         if (option_mask32 & 0x400) limitt = xatoul(t); // -t
326         argv += optind;
327         if (!argv[0]) bb_show_usage();
328         slimit();
329         execvp(argv[0], argv);
330         bb_perror_msg_and_die("exec %s", argv[0]);
331 }