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;
59 char const * pid_file;
63 static struct option const
65 { "help", no_argument, 0, 'h' },
66 { "version", no_argument, 0, 'v' },
71 showHelp(int fd, char const *cmd, int res)
73 WRITE_MSG(fd, "Usage: ");
76 " [-c <ctx>] [-u <uid>] [-g <gid>] [-r <chroot>] [-n]\n"
77 "Please report bugs to " PACKAGE_BUGREPORT "\n");
85 "rpm-fake-resolver " VERSION " -- NSS resovler for rpm-fake\n"
86 "This program is part of " PACKAGE_STRING "\n\n"
87 "Copyright (C) 2003 Enrico Scholz\n"
88 VERSION_COPYRIGHT_DISCLAIMER);
94 parseArgs(struct ArgInfo *args, int argc, char *argv[])
97 int c = getopt_long(argc, argv, "c:u:g:r:n", CMDLINE_OPTIONS, 0);
101 case 'h' : showHelp(1, argv[0], 0);
102 case 'v' : showVersion();
104 case 'c' : args->ctx = atoi(optarg); break;
105 case 'u' : args->uid = atoi(optarg); break;
106 case 'g' : args->gid = atoi(optarg); break;
107 case 'r' : args->chroot = optarg; break;
108 case 'n' : args->do_fork = false; break;
110 WRITE_MSG(2, "Try '");
111 WRITE_STR(2, argv[0]);
112 WRITE_MSG(2, " --help\" for more information.\n");
119 WRITE_MSG(2, "No further options allowed; aborting ...\n");
123 if (args->chroot==0) {
124 WRITE_MSG(2, "No chroot specified; aborting...\n");
130 sendResult(bool state, uint32_t res)
133 static uint8_t ONE = 1;
134 Ewrite(1, &ONE, sizeof ONE);
137 static uint8_t ZERO = 0;
138 Ewrite(1, &ZERO, sizeof ZERO);
141 Ewrite(1, &res, sizeof res);
148 Eread(0, &len, sizeof len);
150 if (len<MAX_RQSIZE) {
157 if (res!=0) sendResult(true, res->pw_uid);
158 else sendResult(false, -1);
167 Eread(0, &len, sizeof len);
169 if (len<MAX_RQSIZE) {
176 if (res!=0) sendResult(true, res->gr_gid);
177 else sendResult(false, -1);
186 Eread(0, &what, sizeof what);
188 case 'p' : endpwent(); break;
189 case 'g' : endgrent(); break;
201 Eread (0, &c, sizeof c);
203 case 'P' : do_getpwnam(); break;
204 case 'G' : do_getgrnam(); break;
206 case 'C' : do_closenss(); break;
207 case '.' : Ewrite(1, ".", 1); break;
208 default : Ewrite(1, "?", 1); break;
214 daemonize(struct ArgInfo const UNUSED * args, int pid_fd)
225 char buf[sizeof(id_t)*3 + 2];
228 l = utilvserver_fmt_uint(buf, pid);
229 Ewrite(pid_fd, buf, l);
230 Ewrite(pid_fd, "\n", 1);
240 activateContext(xid_t xid)
242 if (vc_isSupported(vcFEATURE_MIGRATE)) {
243 #warning TODO: activate the context here or migrate to it
247 Evc_new_s_context(xid, 0, S_CTX_INFO_LOCK);
248 //Evc_new_s_context(args.ctx, ~(VC_CAP_SETGID|VC_CAP_SETUID), S_CTX_INFO_LOCK);
251 int main(int argc, char * argv[])
253 struct ArgInfo args = {
254 .ctx = VC_DYNAMIC_XID,
264 # warning *** rpm-fake-resolver is built against glibc; please do not report errors before trying a dietlibc version ***
266 "*** rpm-fake-resolver was built with glibc; please do ***\n"
267 "*** not report errors before trying a dietlibc version. ***\n");
270 parseArgs(&args, argc, argv);
271 if (args.pid_file && args.do_fork)
272 pid_fd = EopenD(args.pid_file, O_CREAT|O_WRONLY, 0644);
274 if (args.chroot) Echroot(args.chroot);
277 activateContext(args.ctx);
278 Esetgroups(0, &args.gid);
282 if (args.do_fork) daemonize(&args, pid_fd);
283 if (pid_fd!=-1) close(pid_fd);