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.
21 // 2. initialize (setuid, ctx-migrate, chroot, ...)
22 // 3. send "." token to fd 3
23 // 4. wait one character on fd 1
24 // 5. process this character and consume further characters from fd 1 as far
26 // 6. go to 3) (or exit)
44 #define ENSC_WRAPPERS_PREFIX "rpm-fake-resolver: "
45 #define ENSC_WRAPPERS_VSERVER 1
46 #define ENSC_WRAPPERS_UNISTD 1
47 #define ENSC_WRAPPERS_FCNTL 1
50 #define MAX_RQSIZE 0x1000
52 int wrapper_exit_code = 1;
60 char const * pid_file;
66 static struct option const
68 { "help", no_argument, 0, 'h' },
69 { "version", no_argument, 0, 'v' },
74 showHelp(int fd, char const *cmd, int res)
76 WRITE_MSG(fd, "Usage: ");
79 " [-c <ctx>] [-u <uid>] [-g <gid>] [-r <chroot>] [-s] [-n]\n"
80 "Please report bugs to " PACKAGE_BUGREPORT "\n");
88 "rpm-fake-resolver " VERSION " -- NSS resovler for rpm-fake\n"
89 "This program is part of " PACKAGE_STRING "\n\n"
90 "Copyright (C) 2003 Enrico Scholz\n"
91 VERSION_COPYRIGHT_DISCLAIMER);
97 parseArgs(struct ArgInfo *args, int argc, char *argv[])
100 int c = getopt_long(argc, argv, "F:C:c:u:g:r:ns", CMDLINE_OPTIONS, 0);
104 case 'h' : showHelp(1, argv[0], 0);
105 case 'v' : showVersion();
107 case 'c' : args->ctx = atoi(optarg); break;
108 case 'u' : args->uid = atoi(optarg); break;
109 case 'g' : args->gid = atoi(optarg); break;
110 case 'F' : args->flags = atoi(optarg); break;
111 case 'C' : args->caps = atoi(optarg); break;
112 case 'r' : args->chroot = optarg; break;
113 case 'n' : args->do_fork = false; break;
114 case 's' : args->in_ctx = true; break;
116 WRITE_MSG(2, "Try '");
117 WRITE_STR(2, argv[0]);
118 WRITE_MSG(2, " --help\" for more information.\n");
125 WRITE_MSG(2, "No further options allowed; aborting ...\n");
129 if (args->chroot==0) {
130 WRITE_MSG(2, "No chroot specified; aborting...\n");
136 sendResult(bool state, uint32_t res)
139 static uint8_t ONE = 1;
140 Ewrite(1, &ONE, sizeof ONE);
143 static uint8_t ZERO = 0;
144 Ewrite(1, &ZERO, sizeof ZERO);
147 Ewrite(1, &res, sizeof res);
154 Eread(0, &len, sizeof len);
156 if (len<MAX_RQSIZE) {
163 if (res!=0) sendResult(true, res->pw_uid);
164 else sendResult(false, -1);
173 Eread(0, &len, sizeof len);
175 if (len<MAX_RQSIZE) {
182 if (res!=0) sendResult(true, res->gr_gid);
183 else sendResult(false, -1);
192 Eread(0, &what, sizeof what);
194 case 'p' : endpwent(); break;
195 case 'g' : endgrent(); break;
207 Eread (0, &c, sizeof c);
209 case 'P' : do_getpwnam(); break;
210 case 'G' : do_getgrnam(); break;
212 case 'C' : do_closenss(); break;
213 case '.' : Ewrite(1, ".", 1); break;
214 default : Ewrite(1, "?", 1); break;
220 daemonize(struct ArgInfo const UNUSED * args, int pid_fd)
231 char buf[sizeof(id_t)*3 + 2];
234 l = utilvserver_fmt_uint(buf, pid);
235 Ewrite(pid_fd, buf, l);
236 Ewrite(pid_fd, "\n", 1);
246 activateContext(xid_t xid, bool in_ctx,
247 uint32_t xid_caps, int xid_flags)
250 struct vc_ctx_flags flags = {
252 .mask = VC_VXF_STATE_SETUP,
255 Evc_set_cflags(xid, &flags);
257 else if (vc_isSupported(vcFEATURE_MIGRATE))
258 Evc_ctx_migrate(xid);
260 #ifdef VC_ENABLE_API_COMPAT
261 Evc_new_s_context(xid, xid_caps, xid_flags);
263 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "can not change context: migrate kernel feature missing and 'compat' API disabled\n");
264 exit(wrapper_exit_code);
269 int main(int argc, char * argv[])
271 struct ArgInfo args = {
272 .ctx = VC_DYNAMIC_XID,
279 .flags = S_CTX_INFO_LOCK,
284 # warning *** rpm-fake-resolver is built against glibc; please do not report errors before trying a dietlibc version ***
286 "*** rpm-fake-resolver was built with glibc; please do ***\n"
287 "*** not report errors before trying a dietlibc version. ***\n");
290 parseArgs(&args, argc, argv);
291 if (args.pid_file && args.do_fork)
292 pid_fd = EopenD(args.pid_file, O_CREAT|O_WRONLY, 0644);
294 if (args.chroot) Echroot(args.chroot);
297 activateContext(args.ctx, args.in_ctx, args.caps, args.flags);
298 Esetgroups(0, &args.gid);
302 if (args.do_fork) daemonize(&args, pid_fd);
303 if (pid_fd!=-1) close(pid_fd);