diff options
author | Ryan Mallon <rmallon@gmail.com> | 2013-10-08 14:52:49 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2013-10-08 14:52:49 +0200 |
commit | 5906a5c26c392b9687d14951a6da3a5195b576be (patch) | |
tree | 0f7038d653988cb63f9efe431b9e6674173f60d1 | |
parent | 932e233a491b6a5b9293ace04ef74667a95d739c (diff) | |
download | busybox-w32-5906a5c26c392b9687d14951a6da3a5195b576be.tar.gz busybox-w32-5906a5c26c392b9687d14951a6da3a5195b576be.tar.bz2 busybox-w32-5906a5c26c392b9687d14951a6da3a5195b576be.zip |
libbb: Add xsetegid(), xseteuid(), xopen_as_uid_gid() functions
Signed-off-by: Ryan Mallon <rmallon@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | include/libbb.h | 9 | ||||
-rw-r--r-- | libbb/xfuncs_printf.c | 45 |
2 files changed, 42 insertions, 12 deletions
diff --git a/include/libbb.h b/include/libbb.h index 1315e5f8f..3ab1d6b46 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -461,6 +461,8 @@ void record_signo(int signo); /* not FAST_FUNC! */ | |||
461 | 461 | ||
462 | void xsetgid(gid_t gid) FAST_FUNC; | 462 | void xsetgid(gid_t gid) FAST_FUNC; |
463 | void xsetuid(uid_t uid) FAST_FUNC; | 463 | void xsetuid(uid_t uid) FAST_FUNC; |
464 | void xsetegid(gid_t egid) FAST_FUNC; | ||
465 | void xseteuid(uid_t euid) FAST_FUNC; | ||
464 | void xchdir(const char *path) FAST_FUNC; | 466 | void xchdir(const char *path) FAST_FUNC; |
465 | void xchroot(const char *path) FAST_FUNC; | 467 | void xchroot(const char *path) FAST_FUNC; |
466 | void xsetenv(const char *key, const char *value) FAST_FUNC; | 468 | void xsetenv(const char *key, const char *value) FAST_FUNC; |
@@ -469,11 +471,12 @@ void bb_unsetenv_and_free(char *key) FAST_FUNC; | |||
469 | void xunlink(const char *pathname) FAST_FUNC; | 471 | void xunlink(const char *pathname) FAST_FUNC; |
470 | void xstat(const char *pathname, struct stat *buf) FAST_FUNC; | 472 | void xstat(const char *pathname, struct stat *buf) FAST_FUNC; |
471 | void xfstat(int fd, struct stat *buf, const char *errmsg) FAST_FUNC; | 473 | void xfstat(int fd, struct stat *buf, const char *errmsg) FAST_FUNC; |
474 | int open3_or_warn(const char *pathname, int flags, int mode) FAST_FUNC; | ||
475 | int open_or_warn(const char *pathname, int flags) FAST_FUNC; | ||
476 | int xopen3(const char *pathname, int flags, int mode) FAST_FUNC; | ||
472 | int xopen(const char *pathname, int flags) FAST_FUNC; | 477 | int xopen(const char *pathname, int flags) FAST_FUNC; |
473 | int xopen_nonblocking(const char *pathname) FAST_FUNC; | 478 | int xopen_nonblocking(const char *pathname) FAST_FUNC; |
474 | int xopen3(const char *pathname, int flags, int mode) FAST_FUNC; | 479 | int xopen_as_uid_gid(const char *pathname, int flags, uid_t u, gid_t g) FAST_FUNC; |
475 | int open_or_warn(const char *pathname, int flags) FAST_FUNC; | ||
476 | int open3_or_warn(const char *pathname, int flags, int mode) FAST_FUNC; | ||
477 | int open_or_warn_stdin(const char *pathname) FAST_FUNC; | 480 | int open_or_warn_stdin(const char *pathname) FAST_FUNC; |
478 | int xopen_stdin(const char *pathname) FAST_FUNC; | 481 | int xopen_stdin(const char *pathname) FAST_FUNC; |
479 | void xrename(const char *oldpath, const char *newpath) FAST_FUNC; | 482 | void xrename(const char *oldpath, const char *newpath) FAST_FUNC; |
diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c index a70683241..e4ac6a002 100644 --- a/libbb/xfuncs_printf.c +++ b/libbb/xfuncs_printf.c | |||
@@ -140,15 +140,6 @@ int FAST_FUNC xopen(const char *pathname, int flags) | |||
140 | return xopen3(pathname, flags, 0666); | 140 | return xopen3(pathname, flags, 0666); |
141 | } | 141 | } |
142 | 142 | ||
143 | /* Die if we can't open an existing file readonly with O_NONBLOCK | ||
144 | * and return the fd. | ||
145 | * Note that for ioctl O_RDONLY is sufficient. | ||
146 | */ | ||
147 | int FAST_FUNC xopen_nonblocking(const char *pathname) | ||
148 | { | ||
149 | return xopen(pathname, O_RDONLY | O_NONBLOCK); | ||
150 | } | ||
151 | |||
152 | // Warn if we can't open a file and return a fd. | 143 | // Warn if we can't open a file and return a fd. |
153 | int FAST_FUNC open3_or_warn(const char *pathname, int flags, int mode) | 144 | int FAST_FUNC open3_or_warn(const char *pathname, int flags, int mode) |
154 | { | 145 | { |
@@ -167,6 +158,32 @@ int FAST_FUNC open_or_warn(const char *pathname, int flags) | |||
167 | return open3_or_warn(pathname, flags, 0666); | 158 | return open3_or_warn(pathname, flags, 0666); |
168 | } | 159 | } |
169 | 160 | ||
161 | /* Die if we can't open an existing file readonly with O_NONBLOCK | ||
162 | * and return the fd. | ||
163 | * Note that for ioctl O_RDONLY is sufficient. | ||
164 | */ | ||
165 | int FAST_FUNC xopen_nonblocking(const char *pathname) | ||
166 | { | ||
167 | return xopen(pathname, O_RDONLY | O_NONBLOCK); | ||
168 | } | ||
169 | |||
170 | int FAST_FUNC xopen_as_uid_gid(const char *pathname, int flags, uid_t u, gid_t g) | ||
171 | { | ||
172 | int fd; | ||
173 | uid_t old_euid = geteuid(); | ||
174 | gid_t old_egid = getegid(); | ||
175 | |||
176 | xsetegid(g); | ||
177 | xseteuid(u); | ||
178 | |||
179 | fd = xopen(pathname, flags); | ||
180 | |||
181 | xseteuid(old_euid); | ||
182 | xsetegid(old_egid); | ||
183 | |||
184 | return fd; | ||
185 | } | ||
186 | |||
170 | void FAST_FUNC xunlink(const char *pathname) | 187 | void FAST_FUNC xunlink(const char *pathname) |
171 | { | 188 | { |
172 | if (unlink(pathname)) | 189 | if (unlink(pathname)) |
@@ -351,6 +368,16 @@ void FAST_FUNC xsetuid(uid_t uid) | |||
351 | if (setuid(uid)) bb_perror_msg_and_die("setuid"); | 368 | if (setuid(uid)) bb_perror_msg_and_die("setuid"); |
352 | } | 369 | } |
353 | 370 | ||
371 | void FAST_FUNC xsetegid(gid_t egid) | ||
372 | { | ||
373 | if (setegid(egid)) bb_perror_msg_and_die("setegid"); | ||
374 | } | ||
375 | |||
376 | void FAST_FUNC xseteuid(uid_t euid) | ||
377 | { | ||
378 | if (seteuid(euid)) bb_perror_msg_and_die("seteuid"); | ||
379 | } | ||
380 | |||
354 | // Die if we can't chdir to a new path. | 381 | // Die if we can't chdir to a new path. |
355 | void FAST_FUNC xchdir(const char *path) | 382 | void FAST_FUNC xchdir(const char *path) |
356 | { | 383 | { |