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;
64 static struct option const
66 { "help", no_argument, 0, 'h' },
67 { "version", no_argument, 0, 'v' },
72 showHelp(int fd, char const *cmd, int res)
74 WRITE_MSG(fd, "Usage: ");
77 " [-c <ctx>] [-u <uid>] [-g <gid>] [-r <chroot>] [-s] [-n]\n"
78 "Please report bugs to " PACKAGE_BUGREPORT "\n");
86 "rpm-fake-resolver " VERSION " -- NSS resovler for rpm-fake\n"
87 "This program is part of " PACKAGE_STRING "\n\n"
88 "Copyright (C) 2003 Enrico Scholz\n"
89 VERSION_COPYRIGHT_DISCLAIMER);
95 parseArgs(struct ArgInfo *args, int argc, char *argv[])
98 int c = getopt_long(argc, argv, "c:u:g:r:ns", CMDLINE_OPTIONS, 0);
102 case 'h' : showHelp(1, argv[0], 0);
103 case 'v' : showVersion();
105 case 'c' : args->ctx = atoi(optarg); break;
106 case 'u' : args->uid = atoi(optarg); break;
107 case 'g' : args->gid = atoi(optarg); break;
108 case 'r' : args->chroot = optarg; break;
109 case 'n' : args->do_fork = false; break;
110 case 's' : args->in_ctx = true; break;
112 WRITE_MSG(2, "Try '");
113 WRITE_STR(2, argv[0]);
114 WRITE_MSG(2, " --help\" for more information.\n");
121 WRITE_MSG(2, "No further options allowed; aborting ...\n");
125 if (args->chroot==0) {
126 WRITE_MSG(2, "No chroot specified; aborting...\n");
132 sendResult(bool state, uint32_t res)
135 static uint8_t ONE = 1;
136 Ewrite(1, &ONE, sizeof ONE);
139 static uint8_t ZERO = 0;
140 Ewrite(1, &ZERO, sizeof ZERO);
143 Ewrite(1, &res, sizeof res);
150 Eread(0, &len, sizeof len);
152 if (len<MAX_RQSIZE) {
159 if (res!=0) sendResult(true, res->pw_uid);
160 else sendResult(false, -1);
169 Eread(0, &len, sizeof len);
171 if (len<MAX_RQSIZE) {
178 if (res!=0) sendResult(true, res->gr_gid);
179 else sendResult(false, -1);
188 Eread(0, &what, sizeof what);
190 case 'p' : endpwent(); break;
191 case 'g' : endgrent(); break;
203 Eread (0, &c, sizeof c);
205 case 'P' : do_getpwnam(); break;
206 case 'G' : do_getgrnam(); break;
208 case 'C' : do_closenss(); break;
209 case '.' : Ewrite(1, ".", 1); break;
210 default : Ewrite(1, "?", 1); break;
216 daemonize(struct ArgInfo const UNUSED * args, int pid_fd)
227 char buf[sizeof(id_t)*3 + 2];
230 l = utilvserver_fmt_uint(buf, pid);
231 Ewrite(pid_fd, buf, l);
232 Ewrite(pid_fd, "\n", 1);
242 activateContext(xid_t xid, bool in_ctx)
245 struct vc_ctx_flags flags = {
247 .mask = VC_VXF_STATE_SETUP,
250 Evc_set_cflags(xid, &flags);
252 else if (vc_isSupported(vcFEATURE_MIGRATE))
253 Evc_ctx_migrate(xid);
255 #ifdef VC_ENABLE_API_COMPAT
256 Evc_new_s_context(xid, 0, S_CTX_INFO_LOCK);
258 WRITE_MSG(2, ENSC_WRAPPERS_PREFIX "can not change context: migrate kernel feature missing and 'compat' API disabled\n");
259 exit(wrapper_exit_code);
264 int main(int argc, char * argv[])
266 struct ArgInfo args = {
267 .ctx = VC_DYNAMIC_XID,
278 # warning *** rpm-fake-resolver is built against glibc; please do not report errors before trying a dietlibc version ***
280 "*** rpm-fake-resolver was built with glibc; please do ***\n"
281 "*** not report errors before trying a dietlibc version. ***\n");
284 parseArgs(&args, argc, argv);
285 if (args.pid_file && args.do_fork)
286 pid_fd = EopenD(args.pid_file, O_CREAT|O_WRONLY, 0644);
288 if (args.chroot) Echroot(args.chroot);
291 activateContext(args.ctx, args.in_ctx);
292 Esetgroups(0, &args.gid);
296 if (args.do_fork) daemonize(&args, pid_fd);
297 if (pid_fd!=-1) close(pid_fd);