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_insecurecaps();
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[15];
282 char const **ptr = args;
283 char const *env[] = { "HOME=/", "PATH=/bin:/usr/bin", 0 };
287 dup2(res_sock[1], 0);
288 dup2(res_sock[1], 1);
289 if (sync_pipe[1]!=3) {
291 dup2(sync_pipe[1], 3);
295 /* ... *socket[0] are marked as close-on-exec ...*/
298 if (root) { *ptr++ = "-r"; *ptr++ = root; }
299 if (uid) { *ptr++ = "-u"; *ptr++ = uid; }
300 if (gid) { *ptr++ = "-g"; *ptr++ = gid; }
302 if (setupContext(ctx, &xid_str)) { *ptr++ = "-s"; }
303 else if (xid_str) { *ptr++ = "-c"; *ptr++ = xid_str; }
306 execve(resolver, (char **)args, (char **)env);
307 perror(ENSC_WRAPPERS_PREFIX "failed to exec resolver");
315 pw_sock = res_sock[0];
316 sync_sock = sync_pipe[0];
318 if (read(sync_sock, &ctx, sizeof ctx)!=sizeof(ctx) ||
319 read(sync_sock, &c, 1)!=1 ||
320 write(pw_sock, ".", 1)!=1 ||
321 read(pw_sock, &c, 1)!=1 ||
323 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "failed to initialize communication with resolver\n");
327 if (wait4(pid, 0, WNOHANG,0)==-1) {
328 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX" unexpected initialization-error of resolver\n");
333 unsetenv("RPM_FAKE_RESOLVER_GID");
334 unsetenv("RPM_FAKE_RESOLVER_UID");
335 unsetenv("RPM_FAKE_RESOLVER");
346 if (is_initialized) return;
348 syscall_rev = getAndClearEnv("RPM_FAKE_S_CONTEXT_REV", 0);
349 syscall_nr = getAndClearEnv("RPM_FAKE_S_CONTEXT_NR", 273);
351 #ifdef VC_ENABLE_API_LEGACY
353 extern void vc_init_internal_legacy(int ctx_rev, int ctx_number,
354 int ipv4_rev, int ipv4_number);
356 vc_init_internal_legacy(syscall_rev, syscall_nr, 3, 274);
360 ctx = getAndClearEnv("RPM_FAKE_CTX", VC_DYNAMIC_XID);
361 caps = getAndClearEnv("RPM_FAKE_CAP", ~0x3404040f);
362 flags = getAndClearEnv("RPM_FAKE_FLAGS", 0);
363 root = getenv("RPM_FAKE_CHROOT");
364 mnts = getenv("RPM_FAKE_NAMESPACE_MOUNTS");
365 if (mnts && *mnts) mnts = strdup(mnts);
368 unsetenv("RPM_FAKE_CHROOT");
369 unsetenv("RPM_FAKE_NAMESPACE_MOUNTS");
372 is_initialized = true;
378 INIT(RTLD_NEXT, execv);
379 INIT(RTLD_NEXT, getgrnam);
380 INIT(RTLD_NEXT, getpwnam);
381 INIT(RTLD_NEXT, endpwent);
382 INIT(RTLD_NEXT, endgrent);
388 char *env = getenv("LD_PRELOAD");
391 // the const <-> non-const assignment is not an issue since the following
392 // modifying operations will not be executed in the const-case
393 env = env ? env : "";
394 pos = strstr(env, LIBNAME);
397 char *end_pos = pos + sizeof(LIBNAME);
398 bool is_end = (end_pos[-1]=='\0');
402 start_pos = strrchr(env, ':');
403 if (start_pos==0) start_pos = env;
404 else if (!is_end) ++start_pos;
406 if (is_end) *start_pos = '\0';
407 else memmove(start_pos, end_pos, strlen(end_pos)+1);
411 if (isDbgLevel(DBG_VERBOSE1|DBG_VARIABLES)) {
412 WRITE_MSG(2, "env='");
418 if (*env=='\0') unsetenv("LD_PRELOAD");
424 if (getenv("RPM_FAKE_VERSION")) showVersion();
425 if (getenv("RPM_FAKE_HELP")) showHelp();
427 debug_level = getAndClearEnv("RPM_FAKE_DEBUG", 0);
429 if (isDbgLevel(DBG_INIT)) WRITE_MSG(2, ">>>>> initRPMFake <<<<<\n");
437 if (isDbgLevel(DBG_VARIABLES|DBG_VERBOSE2)) {
446 if (isDbgLevel(DBG_INIT)) WRITE_MSG(2, ">>>>> exitRPMFake <<<<<\n");
449 read(sync_sock, &c, 1);
450 write(pw_sock, "Q", 1);
455 //============ the worker part ===========
459 doPwStringRequest(uint32_t *result, char style, char const *name)
461 uint32_t len = strlen(name);
466 read(sync_sock, &c, 1);
468 write(pw_sock, &style, 1);
469 write(pw_sock, &len, sizeof len);
470 write(pw_sock, name, len);
471 read (pw_sock, &code, sizeof code);
472 read (pw_sock, result, sizeof *result);
478 getpwnam(const char * name)
480 if (pw_sock==-1) return getpwnam_func(name);
482 static struct passwd res = {
487 .pw_shell = "/bin/false"
490 res.pw_name = (char *)(name);
491 if (!doPwStringRequest(&res.pw_uid, 'P', name)) return 0;
498 getgrnam(const char * name)
500 if (pw_sock==-1) return getgrnam_func(name);
502 static struct group res = {
507 res.gr_name = (char *)(name);
508 if (!doPwStringRequest(&res.gr_gid, 'G', name)) return 0;
517 if (pw_sock==-1) endgrent_func();
523 if (pw_sock==-1) endpwent_func();
528 execvWorker(char const *path, char * const argv[])
532 if (vc_isSupported(vcFEATURE_MIGRATE))
533 res = vc_ctx_migrate(ctx);
535 #ifdef VC_ENABLE_API_COMPAT
536 res = vc_new_s_context(ctx,caps,flags);
538 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "can not change context: migrate kernel feature missing and 'compat' API disabled\n");
542 res=execv_func(path, argv);
555 removeNamespaceMountsChild(struct ExecvParams const *params)
557 char buf[strlen(params->mnts)+1], *ptr;
559 strcpy(buf, params->mnts);
560 ptr = strtok(buf, ":");
562 if (umount2(ptr, 0)==-1) {
563 // FIXME: What is the semantic for CLONE_NEWNS? Is it ok that mounts in
564 // chroots are visible only, when chroot is on /dev/root?
566 // For now, ignore any errors, but future versions should handle them.
570 ptr = strtok(0, ":");
573 return execvWorker(params->path, params->argv);
577 removeNamespaceMounts(char const *path, char * const argv[])
579 if (mnts==0) return execvWorker(path, argv);
584 struct ExecvParams params;
590 // the rpmlib signal-handler is still active; use the default one to
591 // make wait4() working...
592 signal(SIGCHLD, SIG_DFL);
595 pid = sys_clone(CLONE_NEWNS|SIGCHLD|CLONE_VFORK, 0);
597 pid = sys_clone(CLONE_NEWNS|SIGCHLD, 0);
602 case 0 : _exit(removeNamespaceMountsChild(¶ms));
606 while ((p=wait4(pid, &status, 0,0))==-1 &&
607 (errno==EINTR || errno==EAGAIN)) ;
609 if (p==-1) return -1;
611 if (WIFEXITED(status)) _exit(WEXITSTATUS(status));
612 if (WIFSIGNALED(status)) kill(getpid(), WTERMSIG(status));
620 execv(char const *path, char * const argv[]) __THROW
622 return removeNamespaceMounts(path, argv);