aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Mallon <rmallon@gmail.com>2013-10-08 14:52:49 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2013-10-08 14:52:49 +0200
commit5906a5c26c392b9687d14951a6da3a5195b576be (patch)
tree0f7038d653988cb63f9efe431b9e6674173f60d1
parent932e233a491b6a5b9293ace04ef74667a95d739c (diff)
downloadbusybox-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.h9
-rw-r--r--libbb/xfuncs_printf.c45
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
462void xsetgid(gid_t gid) FAST_FUNC; 462void xsetgid(gid_t gid) FAST_FUNC;
463void xsetuid(uid_t uid) FAST_FUNC; 463void xsetuid(uid_t uid) FAST_FUNC;
464void xsetegid(gid_t egid) FAST_FUNC;
465void xseteuid(uid_t euid) FAST_FUNC;
464void xchdir(const char *path) FAST_FUNC; 466void xchdir(const char *path) FAST_FUNC;
465void xchroot(const char *path) FAST_FUNC; 467void xchroot(const char *path) FAST_FUNC;
466void xsetenv(const char *key, const char *value) FAST_FUNC; 468void xsetenv(const char *key, const char *value) FAST_FUNC;
@@ -469,11 +471,12 @@ void bb_unsetenv_and_free(char *key) FAST_FUNC;
469void xunlink(const char *pathname) FAST_FUNC; 471void xunlink(const char *pathname) FAST_FUNC;
470void xstat(const char *pathname, struct stat *buf) FAST_FUNC; 472void xstat(const char *pathname, struct stat *buf) FAST_FUNC;
471void xfstat(int fd, struct stat *buf, const char *errmsg) FAST_FUNC; 473void xfstat(int fd, struct stat *buf, const char *errmsg) FAST_FUNC;
474int open3_or_warn(const char *pathname, int flags, int mode) FAST_FUNC;
475int open_or_warn(const char *pathname, int flags) FAST_FUNC;
476int xopen3(const char *pathname, int flags, int mode) FAST_FUNC;
472int xopen(const char *pathname, int flags) FAST_FUNC; 477int xopen(const char *pathname, int flags) FAST_FUNC;
473int xopen_nonblocking(const char *pathname) FAST_FUNC; 478int xopen_nonblocking(const char *pathname) FAST_FUNC;
474int xopen3(const char *pathname, int flags, int mode) FAST_FUNC; 479int xopen_as_uid_gid(const char *pathname, int flags, uid_t u, gid_t g) FAST_FUNC;
475int open_or_warn(const char *pathname, int flags) FAST_FUNC;
476int open3_or_warn(const char *pathname, int flags, int mode) FAST_FUNC;
477int open_or_warn_stdin(const char *pathname) FAST_FUNC; 480int open_or_warn_stdin(const char *pathname) FAST_FUNC;
478int xopen_stdin(const char *pathname) FAST_FUNC; 481int xopen_stdin(const char *pathname) FAST_FUNC;
479void xrename(const char *oldpath, const char *newpath) FAST_FUNC; 482void 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 */
147int 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.
153int FAST_FUNC open3_or_warn(const char *pathname, int flags, int mode) 144int 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 */
165int FAST_FUNC xopen_nonblocking(const char *pathname)
166{
167 return xopen(pathname, O_RDONLY | O_NONBLOCK);
168}
169
170int 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
170void FAST_FUNC xunlink(const char *pathname) 187void 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
371void FAST_FUNC xsetegid(gid_t egid)
372{
373 if (setegid(egid)) bb_perror_msg_and_die("setegid");
374}
375
376void 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.
355void FAST_FUNC xchdir(const char *path) 382void FAST_FUNC xchdir(const char *path)
356{ 383{