X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=ipif%2Fservice.c;h=08b0ed9f61a40859d37f531231940433db77d76c;hb=70b9d2e0dbecb8fe1e2045a1752465cd35885bb0;hp=33ba2df28a362962a09a886bbc37da43d41bea95;hpb=02b2392d025937ea5a1108b07b0dcd2a52cd957e;p=userv-utils.git diff --git a/ipif/service.c b/ipif/service.c index 33ba2df..08b0ed9 100644 --- a/ipif/service.c +++ b/ipif/service.c @@ -1,35 +1,114 @@ /* - * userv service (or standalone program) - * for per-user IP subranges. + * userv service (or standalone program) for per-user IP subranges. + * + * When invoked appropriately, it creates a point-to-point network + * interface with specified parameters. It arranges for packets sent out + * via that interface by the kernel to appear on its own stdout in SLIP or + * CSLIP encoding, and packets injected into its own stdin to be given to + * the kernel as if received on that interface. Optionally, additional + * routes can be set up to arrange for traffic for other address ranges to + * be routed through the new interface. + * + * This is the service program, which is invoked as root from userv (or may + * be invoked firectly). + * + * Its arguments are supposed to be, in order, as follows: + * + * The first two arguments are usually supplied by the userv + * configuration. See the file `ipif/ipif' in the source tree, which + * is installed in /etc/userv/services.d/ipif by `make install': * - * This is invoked as root, directly from userv. - * Its arguments are supposed to be, in order: * - * Specifies address ranges and gids which own them. - * -- Indicates that the remaining arguments are user-supplied - * and therefore untrusted. + * + * Specifies address ranges and gids which own them. The default + * configuration supplies /etc/userv/ipif-networks, which is then read + * for a list of entries, one per line. + * + * -- + * Serves to separate the user-supplied and therefore untrusted + * arguments from the trusted first argument. + * + * The remaining arguments are supplied by the (untrusted) caller: + * * ,,, + * * As for slattach. Supported protocols are slip, cslip, and - * adaptive. Alternatively, set to `debug' to print debugging - * info. is address of the interface on chiark; - * is the address of the point-to-point peer. + * adaptive. Alternatively, set to `debug' to print debugging info + * and exit. is address of the interface to be created + * on the local system; is the address of the + * point-to-point peer. They must be actual addresses (not + * hostnames). + * * /,/,... - * List of additional routes to add for this interface. - * May be the empty argument. * - * is either - * ,/[,] - * indicating that that gid may allocate addresses in - * the relevant subspace ( is ignored) - * or #... - * which is a comment - * or / or ./ or ../ - * which refers to a file which contains lines which - * are each - * or * - * which means that anything is permitted - * - * Should be run from userv with no-disconnect-hup. + * List of additional routes to add for this interface. routes will + * be set up on the local system arranging for packets for those + * networks to be sent via the created interface. must be an + * IPv4 address, and mask must be an integer (dotted-quad masks are + * not supported). If no additional routes are to be set up, use `-' + * or supply an empty argument. + * + * Each item - whether a line file such as + * /etc/userv/ipif-networks, or supplied on the service program + * command line - is one of: + * + * / + * ./ + * ../ + * + * Reads a file which contains lines which are each + * items. + * + * ,[=][-|+]/(-|+/...)[,] + * + * Indicates that may allocate addresses in the relevant address + * range ( is ignored). must be numeric. To specify a + * single host address, you must specify a mask of /32. If `=' is + * specified then the specific subrange is only allowed for the local + * endpoint address, but not for remote addresses. + * + * More than one range may be given, with each range prefixed + * by + or -. In this case each address range in the rule will + * scanned in order, and the first range in the rule that matches + * any desired rule will count: if that first matching range is + * prefixed by `+' (or nothing) then the rule applies, if it + * is prefixed by `-' (or nothing matches), the rule does not. + * + * * + * Means that anything is to be permitted. This should not appear in + * /etc/userv/ipif-networks, as that would permit any user on the + * system to create any interfaces with any addresses and routes + * attached. It is provided so that root can usefully invoke the ipif + * service program directly (not via userv), without needing to set up + * permissions in /etc/userv/ipif-networks. + * + * #... + * + * Comment. Blank lines are also ignored. + * + * NB: Permission is granted if _any_ config entry matches the request. + * + * The service program should be run from userv with no-disconnect-hup. + */ +/* + * Copyright (C) 1999-2000,2003 Ian Jackson + * This file is part of ipif, part of userv-utils + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with userv-utils; if not, write to the Free Software + * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * $Id$ */ #include @@ -48,8 +127,8 @@ #include #define NARGS 4 -#define MAXEXROUTES 5 -#define ATXTLEN 12 +#define MAXEXROUTES 50 +#define ATXTLEN 16 static const unsigned long gidmaxval= (unsigned long)((gid_t)-2); static const char *const protos_ok[]= { "slip", "cslip", "adaptive", 0 }; @@ -57,11 +136,12 @@ static const int signals[]= { SIGHUP, SIGINT, SIGTERM, 0 }; static const char *configstr, *proto; static unsigned long localaddr, peeraddr, mtu; +static int localpming, peerpming; static int localallow, peerallow, allallow; static int nexroutes; static struct exroute { unsigned long prefix, mask; - int allow; + int allow, pming; char prefixtxt[ATXTLEN], masktxt[ATXTLEN]; } exroutes[MAXEXROUTES]; @@ -75,7 +155,7 @@ static struct pplace { } *cpplace; -static int slpipe[2], ptmaster; +static int slpipe[2], ptmaster, undoslattach; static const char *ifname; static const char *ptyname; @@ -88,13 +168,36 @@ static union { sigset_t emptyset, fullset; +static int cleantask(void) { + pid_t pid; + + pid= fork(); + if (!pid) return 1; + if (pid == (pid_t)-1) + perror("userv-ipif: fork for undo slattach failed - cannot clean up properly"); + return 0; +} + static void terminate(int estatus) { int i, status; pid_t pid; for (i=0; ipming to 1 + * for each range. pming may be 0 if we've determined that + * this line does not apply to . + * permit_range + * which calls permit_range_thing for each + * which checks to see if is inside the relevant + * range (for +) or overlaps it (for -) and updates + * allow and pming. + */ + +static void permit_begin(void) { + int i; + localpming= peerpming= 1; + for (i=0; i0 && ifnbuf[k-1]!=' '; k--); ifname= ifnbuf+k; @@ -646,21 +853,9 @@ static void netconfigure(void) { sysfatal("cannot exec ifconfig"); } - if (task()) { - execlp("route","route", "-host",localtxt, "netmask","255.255.255.255", - "dev",ifname, (char*)0); - sysfatal("cannot exec route (for local)"); - } - - if (task()) { - execlp("route","route", "-host",peertxt, "netmask","255.255.255.255", - "dev",ifname, (char*)0); - sysfatal("cannot exec route (for peer)"); - } - for (i=0; i