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 static char const * ctx_s = 0;
78 static xid_t ctx = VC_NOCTX;
79 static uint32_t caps = ~0;
81 static char const * mnts = 0;
82 static char const * root = 0;
83 static int pw_sock = -1;
84 static int sync_sock = -1;
85 static unsigned int debug_level = 0;
87 static bool is_initialized = false;
95 static void initRPMFake() __attribute__((__constructor__));
96 static void exitRPMFake() __attribute__((__destructor__));
99 isDbgLevel(unsigned int level)
101 return ((debug_level&level)==level);
105 xdlsym(void *handle, const char *symbol)
107 void *res = dlsym(handle, symbol);
109 char const *error = dlerror();
110 write(2, symbol, strlen(symbol));
112 write(2, error, strlen(error));
125 "Usage: LD_PRELOAD=" LIBNAME " <executable> <args>*\n\n"
126 LIBNAME " unterstands the following environment variables:\n"
127 " $RPM_FAKE_RESOLVER ... program which does the NSS resolving (defaults\n"
128 " to " RESOLVER_PROG ")\n"
129 " $RPM_FAKE_RESOLVER_UID ... uid of the resolver program\n"
130 " $RPM_FAKE_RESOLVER_GID ... gid of the resolver program\n"
131 " $RPM_FAKE_CTX ... vserver context which shall be used for resolver\n"
133 " $RPM_FAKE_CAP ... linux capability remove-mask for the context\n"
134 " $RPM_FAKE_FLAGS ... vserver flags of the context\n"
135 " $RPM_FAKE_CHROOT ... directory of the chroot environment\n"
136 " $RPM_FAKE_NAMESPACE_MOUNTS\n"
137 " ... colon separated list of directories which will\n"
138 " umounted before scriptlet execution\n\n"
139 " $RPM_FAKE_HELP ... shows this message\n"
140 " $RPM_FAKE_VERSION ... shows the version of this program\n\n"
141 " $RPM_FAKE_DEBUG ... sets the debuglevel bitmask\n\n"
142 "Please report bugs to " PACKAGE_BUGREPORT "\n");
150 LIBNAME " " VERSION " -- wrapper around rpm\n"
151 "This program is part of " PACKAGE_STRING "\n\n"
152 "Copyright (C) 2003 Enrico Scholz\n"
153 VERSION_COPYRIGHT_DISCLAIMER);
158 getAndClearEnv(char const *key, int dflt)
160 char *env = getenv(key);
163 if (env==0 || env[0]=='\0') res = dflt;
173 setupContext(xid_t xid)
176 if (vc_isSupported(vcFEATURE_MIGRATE)) {
177 xid_t rc = vc_create_context(xid);
178 if (rc==VC_NOCTX && errno!=EEXIST) {
179 perror("rpm-fake.so: vc_create_context()");
186 strcpy(buf, "rpm-fake.so #");
187 l = utilvserver_fmt_uint(buf+sizeof("rpm-fake.so #")-1, getppid());
188 Evc_set_vhi_name(rc, vcVHI_CONTEXT, buf, sizeof("rpm-fake.so #")+l-1);
189 #warning set some sane capabilities
190 // context will be activated later...
196 Ewrite(3, &xid, sizeof xid);
203 char const * sock_name = getenv("RPM_FAKE_PWSOCKET");
206 struct sockaddr_un addr = {
207 .sun_family = AF_UNIX,
210 strncpy(addr.sun_path, sock_name, sizeof(addr.sun_path)-1);
211 addr.sun_path[sizeof(addr.sun_path)-1]='\0';
213 if ((pw_sock=socket(AF_UNIX, SOCK_STREAM, 0))==-1 ||
214 connect(pw_sock, (struct sockaddr *)(&addr), sizeof addr)==-1 ||
215 (flag=fcntl(pw_sock, F_GETFD))==-1 ||
216 fcntl(pw_sock, F_SETFD, flag | FD_CLOEXEC)==-1) {
217 perror("rpm-fake.so: error while initializing pw-socket");
221 unsetenv("RPM_FAKE_PWSOCKET");
227 char const * resolver = getenv("RPM_FAKE_RESOLVER");
228 if (resolver==0) resolver=RESOLVER_PROG;
230 if (resolver!=0 && *resolver!='\0') {
237 uid=getenv("RPM_FAKE_RESOLVER_UID");
238 gid=getenv("RPM_FAKE_RESOLVER_GID");
240 if (socketpair(AF_UNIX, SOCK_STREAM, 0, res_sock)==-1 ||
241 pipe(sync_pipe)==-1 ||
242 fcntl(res_sock[0], F_SETFD, FD_CLOEXEC)==-1 ||
243 fcntl(sync_pipe[0], F_SETFD, FD_CLOEXEC)==-1) {
244 perror("rpm-fake.so: failed to create/initialize resolver-socket or pipe");
250 perror("rpm-fake.so: fork()");
255 char const *args[10];
256 char const **ptr = args;
257 char const *env[] = { "HOME=/", "PATH=/bin:/usr/bin", 0 };
260 dup2(res_sock[1], 0);
261 dup2(res_sock[1], 1);
262 if (sync_pipe[1]!=3) {
264 dup2(sync_pipe[1], 3);
268 /* ... *socket[0] are marked as close-on-exec ...*/
271 if (root) { *ptr++ = "-r"; *ptr++ = root; }
272 if (uid) { *ptr++ = "-u"; *ptr++ = uid; }
273 if (gid) { *ptr++ = "-g"; *ptr++ = gid; }
274 if (ctx_s) { *ptr++ = "-c"; *ptr++ = ctx_s; }
278 execve(resolver, (char **)args, (char **)env);
279 perror("rpm-fake.so: failed to exec resolver");
287 pw_sock = res_sock[0];
288 sync_sock = sync_pipe[0];
290 if (read(sync_sock, &ctx, sizeof ctx)!=sizeof(ctx) ||
291 read(sync_sock, &c, 1)!=1 ||
292 write(pw_sock, ".", 1)!=1 ||
293 read(pw_sock, &c, 1)!=1 ||
295 WRITE_MSG(2, "rpm-fake.so: failed to initialize communication with resolver\n");
299 if (wait4(pid, 0, WNOHANG,0)==-1) {
300 WRITE_MSG(2, "rpm-fake.so: unexpected initialization-error of resolver\n");
306 unsetenv("RPM_FAKE_RESOLVER_GID");
307 unsetenv("RPM_FAKE_RESOLVER_UID");
308 unsetenv("RPM_FAKE_RESOLVER");
319 if (is_initialized) return;
321 syscall_rev = getAndClearEnv("RPM_FAKE_S_CONTEXT_REV", 0);
322 syscall_nr = getAndClearEnv("RPM_FAKE_S_CONTEXT_NR", 273);
324 #ifdef VC_ENABLE_API_LEGACY
326 extern void vc_init_internal_legacy(int ctx_rev, int ctx_number,
327 int ipv4_rev, int ipv4_number);
329 vc_init_internal_legacy(syscall_rev, syscall_nr, 3, 274);
333 ctx_s = getenv("RPM_FAKE_CTX");
334 if (ctx_s && *ctx_s) ctx_s = strdup(ctx_s);
337 ctx = getAndClearEnv("RPM_FAKE_CTX", VC_DYNAMIC_XID);
338 caps = getAndClearEnv("RPM_FAKE_CAP", ~0x3404040f);
339 flags = getAndClearEnv("RPM_FAKE_FLAGS", 0);
340 root = getenv("RPM_FAKE_CHROOT");
341 mnts = getenv("RPM_FAKE_NAMESPACE_MOUNTS");
342 if (mnts && *mnts) mnts = strdup(mnts);
345 unsetenv("RPM_FAKE_CHROOT");
346 unsetenv("RPM_FAKE_NAMESPACE_MOUNTS");
349 is_initialized = true;
355 INIT(RTLD_NEXT, execv);
356 INIT(RTLD_NEXT, getgrnam);
357 INIT(RTLD_NEXT, getpwnam);
358 INIT(RTLD_NEXT, endpwent);
359 INIT(RTLD_NEXT, endgrent);
365 char *env = getenv("LD_PRELOAD");
368 // the const <-> non-const assignment is not an issue since the following
369 // modifying operations will not be executed in the const-case
370 env = env ? env : "";
371 pos = strstr(env, LIBNAME);
374 char *end_pos = pos + sizeof(LIBNAME);
375 bool is_end = (end_pos[-1]=='\0');
379 start_pos = strrchr(env, ':');
380 if (start_pos==0) start_pos = env;
381 else if (!is_end) ++start_pos;
383 if (is_end) *start_pos = '\0';
384 else memmove(start_pos, end_pos, strlen(end_pos)+1);
388 if (isDbgLevel(DBG_VERBOSE1|DBG_VARIABLES)) {
389 WRITE_MSG(2, "env='");
395 if (*env=='\0') unsetenv("LD_PRELOAD");
401 if (getenv("RPM_FAKE_VERSION")) showVersion();
402 if (getenv("RPM_FAKE_HELP")) showHelp();
404 debug_level = getAndClearEnv("RPM_FAKE_DEBUG", 0);
406 if (isDbgLevel(DBG_INIT)) WRITE_MSG(2, ">>>>> initRPMFake <<<<<\n");
414 if (isDbgLevel(DBG_VARIABLES|DBG_VERBOSE2)) {
423 if (isDbgLevel(DBG_INIT)) WRITE_MSG(2, ">>>>> exitRPMFake <<<<<\n");
426 read(sync_sock, &c, 1);
427 write(pw_sock, "Q", 1);
432 //============ the worker part ===========
436 doPwStringRequest(uint32_t *result, char style, char const *name)
438 uint32_t len = strlen(name);
443 read(sync_sock, &c, 1);
445 write(pw_sock, &style, 1);
446 write(pw_sock, &len, sizeof len);
447 write(pw_sock, name, len);
448 read (pw_sock, &code, sizeof code);
449 read (pw_sock, result, sizeof *result);
455 getpwnam(const char * name)
457 if (pw_sock==-1) return getpwnam_func(name);
459 static struct passwd res = {
464 .pw_shell = "/bin/false"
467 res.pw_name = (char *)(name);
468 if (!doPwStringRequest(&res.pw_uid, 'P', name)) return 0;
475 getgrnam(const char * name)
477 if (pw_sock==-1) return getgrnam_func(name);
479 static struct group res = {
484 res.gr_name = (char *)(name);
485 if (!doPwStringRequest(&res.gr_gid, 'G', name)) return 0;
494 if (pw_sock==-1) endgrent_func();
500 if (pw_sock==-1) endpwent_func();
505 execvWorker(char const *path, char * const argv[])
509 if (vc_isSupported(vcFEATURE_MIGRATE))
510 res = vc_migrate_context(ctx);
512 res = vc_new_s_context(ctx,caps,flags);
515 res=execv_func(path, argv);
528 removeNamespaceMountsChild(struct ExecvParams const *params)
530 char buf[strlen(params->mnts)+1], *ptr;
532 strcpy(buf, params->mnts);
533 ptr = strtok(buf, ":");
535 if (umount2(ptr, 0)==-1) {
536 // FIXME: What is the semantic for CLONE_NEWNS? Is it ok that mounts in
537 // chroots are visible only, when chroot is on /dev/root?
539 // For now, ignore any errors, but future versions should handle them.
543 ptr = strtok(0, ":");
546 return execvWorker(params->path, params->argv);
550 removeNamespaceMounts(char const *path, char * const argv[])
552 if (mnts==0) return execvWorker(path, argv);
557 struct ExecvParams params;
563 // the rpmlib signal-handler is still active; use the default one to
564 // make wait4() working...
565 signal(SIGCHLD, SIG_DFL);
568 pid = sys_clone(CLONE_NEWNS|SIGCHLD|CLONE_VFORK, 0);
570 pid = sys_clone(CLONE_NEWNS|SIGCHLD, 0);
575 case 0 : _exit(removeNamespaceMountsChild(¶ms));
579 while ((p=wait4(pid, &status, 0,0))==-1 &&
580 (errno==EINTR || errno==EAGAIN)) ;
582 if (p==-1) return -1;
584 if (WIFEXITED(status)) _exit(WEXITSTATUS(status));
585 if (WIFSIGNALED(status)) kill(getpid(), WTERMSIG(status));
593 execv(char const *path, char * const argv[]) __THROW
595 return removeNamespaceMounts(path, argv);