3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "pathconfig.h"
24 #include "sys_clone.h"
27 #include "lib/vserver.h"
28 #include "lib/internal.h"
30 #include <sys/socket.h>
36 #include <asm/unistd.h>
42 #include <sys/mount.h>
46 #include <sys/types.h>
53 #define ENSC_WRAPPERS_PREFIX "rpm-fake.so: "
54 #define ENSC_WRAPPERS_VSERVER 1
55 #define ENSC_WRAPPERS_UNISTD 1
59 # define CLONE_NEWNS 0x00020000
62 #define LIBNAME "rpm-fake.so"
63 #define PLATFORM_FILE "/etc/rpm/platform"
65 #define INIT(FILE,FUNC) FUNC##_func = ((__typeof__(FUNC) *) (xdlsym(FILE, #FUNC)))
66 #define DECLARE(FUNC) static __typeof__(FUNC) * FUNC##_func = 0
69 #define DBG_INIT 0x0001
70 #define DBG_VARIABLES 0x0002
71 #define DBG_RESOLVER 0x0004
72 #define DBG_EXECV 0x0008
73 #define DBG_VERBOSE0 0x8000
74 #define DBG_VERBOSE1 (0x4000 | DBG_VERBOSE0)
75 #define DBG_VERBOSE2 (0x2000 | DBG_VERBOSE1)
77 int wrapper_exit_code = 255;
79 static xid_t ctx = VC_NOCTX;
80 static uint32_t caps = ~0;
82 static char const * mnts = 0;
83 static char const * root = 0;
84 static int pw_sock = -1;
85 static int sync_sock = -1;
86 static unsigned int debug_level = 0;
88 static bool is_initialized = false;
96 static void initRPMFake() __attribute__((__constructor__));
97 static void exitRPMFake() __attribute__((__destructor__));
100 isDbgLevel(unsigned int level)
102 return ((debug_level&level)==level);
106 xdlsym(void *handle, const char *symbol)
108 void *res = dlsym(handle, symbol);
110 char const *error = dlerror();
111 write(2, symbol, strlen(symbol));
113 write(2, error, strlen(error));
126 "Usage: LD_PRELOAD=" LIBNAME " <executable> <args>*\n\n"
127 LIBNAME " unterstands the following environment variables:\n"
128 " $RPM_FAKE_RESOLVER ... program which does the NSS resolving (defaults\n"
129 " to " RESOLVER_PROG ")\n"
130 " $RPM_FAKE_RESOLVER_UID ... uid of the resolver program\n"
131 " $RPM_FAKE_RESOLVER_GID ... gid of the resolver program\n"
132 " $RPM_FAKE_CTX ... vserver context which shall be used for resolver\n"
134 " $RPM_FAKE_CAP ... linux capability remove-mask for the context\n"
135 " $RPM_FAKE_FLAGS ... vserver flags of the context\n"
136 " $RPM_FAKE_CHROOT ... directory of the chroot environment\n"
137 " $RPM_FAKE_NAMESPACE_MOUNTS\n"
138 " ... colon separated list of directories which will\n"
139 " umounted before scriptlet execution\n\n"
140 " $RPM_FAKE_HELP ... shows this message\n"
141 " $RPM_FAKE_VERSION ... shows the version of this program\n\n"
142 " $RPM_FAKE_DEBUG ... sets the debuglevel bitmask\n\n"
143 "Please report bugs to " PACKAGE_BUGREPORT "\n");
151 LIBNAME " " VERSION " -- wrapper around rpm\n"
152 "This program is part of " PACKAGE_STRING "\n\n"
153 "Copyright (C) 2003 Enrico Scholz\n"
154 VERSION_COPYRIGHT_DISCLAIMER);
159 getAndClearEnv(char const *key, int dflt)
161 char *env = getenv(key);
164 if (env==0 || env[0]=='\0') res = dflt;
173 /// \returns true iff we are in ctx after leaving this function
175 setupContext(xid_t xid, char const **xid_str)
179 if (vc_isSupported(vcFEATURE_MIGRATE)) {
182 if ((xid==VC_DYNAMIC_XID || !vc_is_dynamic_xid(xid)) &&
183 (rc=vc_ctx_create(xid))==VC_NOCTX &&
185 perror(ENSC_WRAPPERS_PREFIX "vc_create_context()");
190 char buf[sizeof(xid_t)*3 + 128];
192 struct vc_ctx_caps caps;
194 strcpy(buf, "rpm-fake.so #");
195 l = utilvserver_fmt_uint(buf+sizeof("rpm-fake.so #")-1, getppid());
196 Evc_set_vhi_name(rc, vcVHI_CONTEXT, buf, sizeof("rpm-fake.so #")+l-1);
200 caps.bcaps = ~vc_get_insecurebcaps();
202 Evc_set_ccaps(rc, &caps);
204 // context will be activated later...
211 if (xid==VC_DYNAMIC_XID)
214 char buf[sizeof(xid_t)*3 + 2];
217 l = utilvserver_fmt_uint(buf, xid); buf[l] = '\0';
218 *xid_str = strdup(buf);
221 Ewrite(3, &xid, sizeof xid);
229 char const * sock_name = getenv("RPM_FAKE_PWSOCKET");
232 struct sockaddr_un addr = {
233 .sun_family = AF_UNIX,
236 strncpy(addr.sun_path, sock_name, sizeof(addr.sun_path)-1);
237 addr.sun_path[sizeof(addr.sun_path)-1]='\0';
239 if ((pw_sock=socket(AF_UNIX, SOCK_STREAM, 0))==-1 ||
240 connect(pw_sock, (struct sockaddr *)(&addr), sizeof addr)==-1 ||
241 (flag=fcntl(pw_sock, F_GETFD))==-1 ||
242 fcntl(pw_sock, F_SETFD, flag | FD_CLOEXEC)==-1) {
243 perror(ENSC_WRAPPERS_PREFIX "error while initializing pw-socket");
247 unsetenv("RPM_FAKE_PWSOCKET");
253 char const * resolver = getenv("RPM_FAKE_RESOLVER");
254 if (resolver==0) resolver=RESOLVER_PROG;
256 if (resolver!=0 && *resolver!='\0') {
263 uid=getenv("RPM_FAKE_RESOLVER_UID");
264 gid=getenv("RPM_FAKE_RESOLVER_GID");
266 if (socketpair(AF_UNIX, SOCK_STREAM, 0, res_sock)==-1 ||
267 pipe(sync_pipe)==-1 ||
268 fcntl(res_sock[0], F_SETFD, FD_CLOEXEC)==-1 ||
269 fcntl(sync_pipe[0], F_SETFD, FD_CLOEXEC)==-1) {
270 perror(ENSC_WRAPPERS_PREFIX "failed to create/initialize resolver-socket or pipe");
276 perror(ENSC_WRAPPERS_PREFIX "fork()");
281 char const *args[20];
282 char const **ptr = args;
283 char const *env[] = { "HOME=/", "PATH=/bin:/usr/bin", 0 };
285 char flag_str[ sizeof(flags)*3 + 2];
286 char caps_str[ sizeof(caps)*3 + 2];
289 dup2(res_sock[1], 0);
290 dup2(res_sock[1], 1);
291 if (sync_pipe[1]!=3) {
293 dup2(sync_pipe[1], 3);
297 /* ... *socket[0] are marked as close-on-exec ...*/
299 flag_str[utilvserver_fmt_uint(flag_str, flags)] = '\0';
300 caps_str[utilvserver_fmt_uint(caps_str, caps)] = '\0';
303 *ptr++ = "-F"; *ptr++ = flag_str;
304 *ptr++ = "-C"; *ptr++ = caps_str;
305 if (root) { *ptr++ = "-r"; *ptr++ = root; }
306 if (uid) { *ptr++ = "-u"; *ptr++ = uid; }
307 if (gid) { *ptr++ = "-g"; *ptr++ = gid; }
309 if (setupContext(ctx, &xid_str)) { *ptr++ = "-s"; }
310 else if (xid_str) { *ptr++ = "-c"; *ptr++ = xid_str; }
313 execve(resolver, (char **)args, (char **)env);
314 perror(ENSC_WRAPPERS_PREFIX "failed to exec resolver");
322 pw_sock = res_sock[0];
323 sync_sock = sync_pipe[0];
325 if (read(sync_sock, &ctx, sizeof ctx)!=sizeof(ctx) ||
326 read(sync_sock, &c, 1)!=1 ||
327 write(pw_sock, ".", 1)!=1 ||
328 read(pw_sock, &c, 1)!=1 ||
330 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "failed to initialize communication with resolver\n");
334 if (wait4(pid, 0, WNOHANG,0)==-1) {
335 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX" unexpected initialization-error of resolver\n");
340 unsetenv("RPM_FAKE_RESOLVER_GID");
341 unsetenv("RPM_FAKE_RESOLVER_UID");
342 unsetenv("RPM_FAKE_RESOLVER");
353 if (is_initialized) return;
355 syscall_rev = getAndClearEnv("RPM_FAKE_S_CONTEXT_REV", 0);
356 syscall_nr = getAndClearEnv("RPM_FAKE_S_CONTEXT_NR", 273);
358 #ifdef VC_ENABLE_API_LEGACY
360 extern void vc_init_internal_legacy(int ctx_rev, int ctx_number,
361 int ipv4_rev, int ipv4_number);
363 vc_init_internal_legacy(syscall_rev, syscall_nr, 3, 274);
367 ctx = getAndClearEnv("RPM_FAKE_CTX", VC_DYNAMIC_XID);
368 caps = getAndClearEnv("RPM_FAKE_CAP", ~0x3404040f);
369 flags = getAndClearEnv("RPM_FAKE_FLAGS", 0);
370 root = getenv("RPM_FAKE_CHROOT");
371 mnts = getenv("RPM_FAKE_NAMESPACE_MOUNTS");
372 if (mnts && *mnts) mnts = strdup(mnts);
375 unsetenv("RPM_FAKE_CHROOT");
376 unsetenv("RPM_FAKE_NAMESPACE_MOUNTS");
379 is_initialized = true;
385 INIT(RTLD_NEXT, execv);
386 INIT(RTLD_NEXT, getgrnam);
387 INIT(RTLD_NEXT, getpwnam);
388 INIT(RTLD_NEXT, endpwent);
389 INIT(RTLD_NEXT, endgrent);
395 char *env = getenv("LD_PRELOAD");
398 // the const <-> non-const assignment is not an issue since the following
399 // modifying operations will not be executed in the const-case
400 env = env ? env : "";
401 pos = strstr(env, LIBNAME);
404 char *end_pos = pos + sizeof(LIBNAME);
405 bool is_end = (end_pos[-1]=='\0');
409 start_pos = strrchr(env, ':');
410 if (start_pos==0) start_pos = env;
411 else if (!is_end) ++start_pos;
413 if (is_end) *start_pos = '\0';
414 else memmove(start_pos, end_pos, strlen(end_pos)+1);
418 if (isDbgLevel(DBG_VERBOSE1|DBG_VARIABLES)) {
419 WRITE_MSG(2, "env='");
425 if (*env=='\0') unsetenv("LD_PRELOAD");
431 if (getenv("RPM_FAKE_VERSION")) showVersion();
432 if (getenv("RPM_FAKE_HELP")) showHelp();
434 debug_level = getAndClearEnv("RPM_FAKE_DEBUG", 0);
436 if (isDbgLevel(DBG_INIT)) WRITE_MSG(2, ">>>>> initRPMFake <<<<<\n");
444 if (isDbgLevel(DBG_VARIABLES|DBG_VERBOSE2)) {
453 if (isDbgLevel(DBG_INIT)) WRITE_MSG(2, ">>>>> exitRPMFake <<<<<\n");
456 read(sync_sock, &c, 1);
457 write(pw_sock, "Q", 1);
462 //============ the worker part ===========
466 doPwStringRequest(uint32_t *result, char style, char const *name)
468 uint32_t len = strlen(name);
473 read(sync_sock, &c, 1);
475 write(pw_sock, &style, 1);
476 write(pw_sock, &len, sizeof len);
477 write(pw_sock, name, len);
478 read (pw_sock, &code, sizeof code);
479 read (pw_sock, result, sizeof *result);
485 getpwnam(const char * name)
487 if (pw_sock==-1) return getpwnam_func(name);
489 static struct passwd res = {
494 .pw_shell = "/bin/false"
497 res.pw_name = (char *)(name);
498 if (!doPwStringRequest(&res.pw_uid, 'P', name)) return 0;
505 getgrnam(const char * name)
507 if (pw_sock==-1) return getgrnam_func(name);
509 static struct group res = {
514 res.gr_name = (char *)(name);
515 if (!doPwStringRequest(&res.gr_gid, 'G', name)) return 0;
524 if (pw_sock==-1) endgrent_func();
530 if (pw_sock==-1) endpwent_func();
535 execvWorker(char const *path, char * const argv[])
539 if (vc_isSupported(vcFEATURE_MIGRATE))
540 res = vc_ctx_migrate(ctx);
542 #ifdef VC_ENABLE_API_COMPAT
543 res = vc_new_s_context(ctx,caps,flags);
545 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "can not change context: migrate kernel feature missing and 'compat' API disabled\n");
549 res=execv_func(path, argv);
562 removeNamespaceMountsChild(struct ExecvParams const *params)
564 char buf[strlen(params->mnts)+1], *ptr;
566 strcpy(buf, params->mnts);
567 ptr = strtok(buf, ":");
569 if (umount2(ptr, 0)==-1) {
570 // FIXME: What is the semantic for CLONE_NEWNS? Is it ok that mounts in
571 // chroots are visible only, when chroot is on /dev/root?
573 // For now, ignore any errors, but future versions should handle them.
577 ptr = strtok(0, ":");
580 return execvWorker(params->path, params->argv);
584 removeNamespaceMounts(char const *path, char * const argv[])
586 if (mnts==0) return execvWorker(path, argv);
591 struct ExecvParams params;
597 // the rpmlib signal-handler is still active; use the default one to
598 // make wait4() working...
599 signal(SIGCHLD, SIG_DFL);
602 pid = sys_clone(CLONE_NEWNS|SIGCHLD|CLONE_VFORK, 0);
604 pid = sys_clone(CLONE_NEWNS|SIGCHLD, 0);
609 case 0 : _exit(removeNamespaceMountsChild(¶ms));
613 while ((p=wait4(pid, &status, 0,0))==-1 &&
614 (errno==EINTR || errno==EAGAIN)) ;
616 if (p==-1) return -1;
618 if (WIFEXITED(status)) _exit(WEXITSTATUS(status));
619 if (WIFSIGNALED(status)) kill(getpid(), WTERMSIG(status));
627 execv(char const *path, char * const argv[]) __THROW
629 return removeNamespaceMounts(path, argv);