spppcontrol.c revision 50476
117683Spst/* 275107Sfenner * Copyright (c) 1997 Joerg Wunsch 3127664Sbms * 4276768Sdelphij * All rights reserved. 5276768Sdelphij * 675107Sfenner * Redistribution and use in source and binary forms, with or without 7276768Sdelphij * modification, are permitted provided that the following conditions 875107Sfenner * are met: 917683Spst * 1. Redistributions of source code must retain the above copyright 1017683Spst * notice, this list of conditions and the following disclaimer. 1126175Sfenner * 2. Redistributions in binary form must reproduce the above copyright 1217683Spst * notice, this list of conditions and the following disclaimer in the 1317683Spst * documentation and/or other materials provided with the distribution. 1417683Spst * 1517683Spst * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR 1617683Spst * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1717683Spst * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1817683Spst * IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT, 1917683Spst * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2017683Spst * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2117683Spst * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2217683Spst * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2317683Spst * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24276768Sdelphij * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25214518Srpaulo */ 2617683Spst 2717683Spst#ifndef lint 2817683Spststatic const char rcsid[] = 2917683Spst "$FreeBSD: head/sbin/spppcontrol/spppcontrol.c 50476 1999-08-28 00:22:10Z peter $"; 3017683Spst#endif /* not lint */ 31214518Srpaulo 32127664Sbms#include <sys/param.h> 33214518Srpaulo#include <sys/callout.h> 3475107Sfenner#include <sys/ioctl.h> 3517683Spst#include <sys/socket.h> 3617683Spst 3717683Spst#include <net/if.h> 3817683Spst#include <net/if_var.h> 3917683Spst#include <net/if_sppp.h> 40214518Srpaulo 41214518Srpaulo#include <err.h> 42214518Srpaulo#include <stdio.h> 4317683Spst#include <string.h> 4417683Spst#include <sysexits.h> 4517683Spst#include <unistd.h> 4617683Spst 4717683Spststatic void usage(void); 4817683Spstvoid print_vals(const char *ifname, struct spppreq *sp); 4917683Spstconst char *phase_name(enum ppp_phase phase); 5017683Spstconst char *proto_name(u_short proto); 5117683Spstconst char *authflags(u_short flags); 5217683Spst 5317683Spst#define PPP_PAP 0xc023 5426175Sfenner#define PPP_CHAP 0xc223 5526175Sfenner 5617683Spstint 5717683Spstmain(int argc, char **argv) 5875107Sfenner{ 5917683Spst int s, c; 6075107Sfenner int errs = 0, verbose = 0; 6175107Sfenner size_t off; 6275107Sfenner const char *ifname, *cp; 6375107Sfenner struct ifreq ifr; 6475107Sfenner struct spppreq spr; 6575107Sfenner 6675107Sfenner while ((c = getopt(argc, argv, "v")) != -1) 6775107Sfenner switch (c) { 6875107Sfenner case 'v': 6975107Sfenner verbose++; 7075107Sfenner break; 7175107Sfenner 7275107Sfenner default: 7375107Sfenner errs++; 7475107Sfenner break; 7575107Sfenner } 76214518Srpaulo argv += optind; 77276768Sdelphij argc -= optind; 78276768Sdelphij 7975107Sfenner if (errs || argc < 1) 8075107Sfenner usage(); 8175107Sfenner 8275107Sfenner ifname = argv[0]; 8375107Sfenner strncpy(ifr.ifr_name, ifname, sizeof ifr.ifr_name); 8475107Sfenner 8575107Sfenner /* use a random AF to create the socket */ 8675107Sfenner if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) 8775107Sfenner err(EX_UNAVAILABLE, "ifconfig: socket"); 8875107Sfenner 8975107Sfenner argc--; 90214518Srpaulo argv++; 9175107Sfenner 92214518Srpaulo spr.cmd = (int)SPPPIOGDEFS; 9375107Sfenner ifr.ifr_data = (caddr_t)&spr; 94214518Srpaulo 9575107Sfenner if (ioctl(s, SIOCGIFGENERIC, &ifr) == -1) 9675107Sfenner err(EX_OSERR, "SIOCGIFGENERIC(SPPPIOGDEFS)"); 9775107Sfenner 9875107Sfenner if (argc == 0) { 9975107Sfenner /* list only mode */ 10075107Sfenner print_vals(ifname, &spr); 10175107Sfenner return 0; 10275107Sfenner } 10375107Sfenner 10475107Sfenner#define startswith(s) strncmp(argv[0], s, (off = strlen(s))) == 0 10575107Sfenner 106214518Srpaulo while (argc > 0) { 10775107Sfenner if (startswith("authproto=")) { 10875107Sfenner cp = argv[0] + off; 10975107Sfenner if (strcmp(cp, "pap") == 0) 11075107Sfenner spr.defs.myauth.proto = 11175107Sfenner spr.defs.hisauth.proto = PPP_PAP; 11275107Sfenner else if (strcmp(cp, "chap") == 0) 11375107Sfenner spr.defs.myauth.proto = 11475107Sfenner spr.defs.hisauth.proto = PPP_CHAP; 11575107Sfenner else if (strcmp(cp, "none") == 0) 11675107Sfenner spr.defs.myauth.proto = 11775107Sfenner spr.defs.hisauth.proto = 0; 11875107Sfenner else 11917683Spst errx(EX_DATAERR, "bad auth proto: %s", cp); 12017683Spst } else if (startswith("myauthproto=")) { 12126175Sfenner cp = argv[0] + off; 12226175Sfenner if (strcmp(cp, "pap") == 0) 12326175Sfenner spr.defs.myauth.proto = PPP_PAP; 12426175Sfenner else if (strcmp(cp, "chap") == 0) 125276768Sdelphij spr.defs.myauth.proto = PPP_CHAP; 126276768Sdelphij else if (strcmp(cp, "none") == 0) 127276768Sdelphij spr.defs.myauth.proto = 0; 128214518Srpaulo else 129214518Srpaulo errx(EX_DATAERR, "bad auth proto: %s", cp); 13026175Sfenner } else if (startswith("myauthname=")) 13126175Sfenner strncpy(spr.defs.myauth.name, argv[0] + off, 13226175Sfenner AUTHNAMELEN); 133276768Sdelphij else if (startswith("myauthsecret=") || 134276768Sdelphij startswith("myauthkey=")) 135276768Sdelphij strncpy(spr.defs.myauth.secret, argv[0] + off, 136276768Sdelphij AUTHKEYLEN); 13726175Sfenner else if (startswith("hisauthproto=")) { 13826175Sfenner cp = argv[0] + off; 13926175Sfenner if (strcmp(cp, "pap") == 0) 14026175Sfenner spr.defs.hisauth.proto = PPP_PAP; 14126175Sfenner else if (strcmp(cp, "chap") == 0) 14226175Sfenner spr.defs.hisauth.proto = PPP_CHAP; 14326175Sfenner else if (strcmp(cp, "none") == 0) 14417683Spst spr.defs.hisauth.proto = 0; 14517683Spst else 14617683Spst errx(EX_DATAERR, "bad auth proto: %s", cp); 14717683Spst } else if (startswith("hisauthname=")) 14817683Spst strncpy(spr.defs.hisauth.name, argv[0] + off, 14917683Spst AUTHNAMELEN); 15017683Spst else if (startswith("hisauthsecret=") || 15117683Spst startswith("hisauthkey=")) 15217683Spst strncpy(spr.defs.hisauth.secret, argv[0] + off, 15317683Spst AUTHKEYLEN); 15417683Spst else if (strcmp(argv[0], "callin") == 0) 15517683Spst spr.defs.hisauth.flags |= AUTHFLAG_NOCALLOUT; 15617683Spst else if (strcmp(argv[0], "always") == 0) 15717683Spst spr.defs.hisauth.flags &= ~AUTHFLAG_NOCALLOUT; 15826175Sfenner else if (strcmp(argv[0], "norechallenge") == 0) 159214518Srpaulo spr.defs.hisauth.flags |= AUTHFLAG_NORECHALLENGE; 16017683Spst else if (strcmp(argv[0], "rechallenge") == 0) 16117683Spst spr.defs.hisauth.flags &= ~AUTHFLAG_NORECHALLENGE; 16217683Spst else 163276768Sdelphij errx(EX_DATAERR, "bad parameter: \"%s\"", argv[0]); 164276768Sdelphij 165214518Srpaulo argv++; 16675107Sfenner argc--; 16775107Sfenner } 16875107Sfenner 16975107Sfenner spr.cmd = (int)SPPPIOSDEFS; 17075107Sfenner 17175107Sfenner if (ioctl(s, SIOCSIFGENERIC, &ifr) == -1) 17275107Sfenner err(EX_OSERR, "SIOCSIFGENERIC(SPPPIOSDEFS)"); 17375107Sfenner 17475107Sfenner if (verbose) 17575107Sfenner print_vals(ifname, &spr); 176127664Sbms 177127664Sbms return 0; 178127664Sbms} 179127664Sbms 180276768Sdelphijstatic void 181276768Sdelphijusage(void) 182127664Sbms{ 183276768Sdelphij fprintf(stderr, "%s\n%s\n", 18417683Spst "usage: spppcontrol [-v] ifname [{my|his}auth{proto|name|secret}=...]", 18517683Spst " spppcontrol [-v] ifname callin|always"); 18617683Spst exit(EX_USAGE); 187214518Srpaulo} 188214518Srpaulo 189214518Srpaulovoid 190214518Srpauloprint_vals(const char *ifname, struct spppreq *sp) 19126175Sfenner{ 19275107Sfenner printf("%s:\tphase=%s\n", ifname, phase_name(sp->defs.pp_phase)); 19326175Sfenner if (sp->defs.myauth.proto) { 19426175Sfenner printf("\tmyauthproto=%s myauthname=\"%.*s\"\n", 19517683Spst proto_name(sp->defs.myauth.proto), 19617683Spst AUTHNAMELEN, sp->defs.myauth.name); 19726175Sfenner } 19817683Spst if (sp->defs.hisauth.proto) { 19917683Spst printf("\thisauthproto=%s hisauthname=\"%.*s\"%s\n", 20017683Spst proto_name(sp->defs.hisauth.proto), 20126175Sfenner AUTHNAMELEN, sp->defs.hisauth.name, 20217683Spst authflags(sp->defs.hisauth.flags)); 20317683Spst } 20417683Spst} 20526175Sfenner 20617683Spstconst char * 207214518Srpaulophase_name(enum ppp_phase phase) 208214518Srpaulo{ 209214518Srpaulo switch (phase) { 210214518Srpaulo case PHASE_DEAD: return "dead"; 21117683Spst case PHASE_ESTABLISH: return "establish"; 21217683Spst case PHASE_TERMINATE: return "terminate"; 21326175Sfenner case PHASE_AUTHENTICATE: return "authenticate"; 21417683Spst case PHASE_NETWORK: return "network"; 21575107Sfenner } 21675107Sfenner return "illegal"; 21775107Sfenner} 21817683Spst 21917683Spstconst char * 22026175Sfennerproto_name(u_short proto) 22117683Spst{ 22217683Spst static char buf[12]; 22317683Spst switch (proto) { 22417683Spst case PPP_PAP: return "pap"; 22517683Spst case PPP_CHAP: return "chap"; 22626175Sfenner } 22717683Spst sprintf(buf, "0x%x", (unsigned)proto); 228276768Sdelphij return buf; 229276768Sdelphij} 230276768Sdelphij 231276768Sdelphijconst char * 232276768Sdelphijauthflags(u_short flags) 233276768Sdelphij{ 23417683Spst static char buf[30]; 23517683Spst buf[0] = '\0'; 23617683Spst if (flags & AUTHFLAG_NOCALLOUT) 23717683Spst strcat(buf, " callin"); 23817683Spst if (flags & AUTHFLAG_NORECHALLENGE) 23917683Spst strcat(buf, " norechallenge"); 24017683Spst return buf; 24117683Spst} 24217683Spst