spppcontrol.c revision 30302
130302Sjoerg/* 230302Sjoerg * Copyright (c) 1997 Joerg Wunsch 330302Sjoerg * 430302Sjoerg * All rights reserved. 530302Sjoerg * 630302Sjoerg * Redistribution and use in source and binary forms, with or without 730302Sjoerg * modification, are permitted provided that the following conditions 830302Sjoerg * are met: 930302Sjoerg * 1. Redistributions of source code must retain the above copyright 1030302Sjoerg * notice, this list of conditions and the following disclaimer. 1130302Sjoerg * 2. Redistributions in binary form must reproduce the above copyright 1230302Sjoerg * notice, this list of conditions and the following disclaimer in the 1330302Sjoerg * documentation and/or other materials provided with the distribution. 1430302Sjoerg * 1530302Sjoerg * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR 1630302Sjoerg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1730302Sjoerg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1830302Sjoerg * IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT, 1930302Sjoerg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2030302Sjoerg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2130302Sjoerg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2230302Sjoerg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2330302Sjoerg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2430302Sjoerg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2530302Sjoerg * 2630302Sjoerg * $Id$ 2730302Sjoerg */ 2830302Sjoerg 2930302Sjoerg#include <sys/param.h> 3030302Sjoerg#include <sys/callout.h> 3130302Sjoerg#include <sys/ioctl.h> 3230302Sjoerg#include <sys/socket.h> 3330302Sjoerg#include <sys/time.h> 3430302Sjoerg 3530302Sjoerg#include <net/if.h> 3630302Sjoerg#include <net/if_var.h> 3730302Sjoerg#include <net/if_sppp.h> 3830302Sjoerg 3930302Sjoerg#include <err.h> 4030302Sjoerg#include <errno.h> 4130302Sjoerg#include <fcntl.h> 4230302Sjoerg#include <stdio.h> 4330302Sjoerg#include <stdlib.h> 4430302Sjoerg#include <string.h> 4530302Sjoerg#include <sysexits.h> 4630302Sjoerg#include <unistd.h> 4730302Sjoerg 4830302Sjoergvoid usage(void); 4930302Sjoergvoid print_vals(const char *ifname, struct spppreq *sp); 5030302Sjoergconst char *phase_name(enum ppp_phase phase); 5130302Sjoergconst char *proto_name(u_short proto); 5230302Sjoergconst char *authflags(u_short flags); 5330302Sjoerg 5430302Sjoerg#define PPP_PAP 0xc023 5530302Sjoerg#define PPP_CHAP 0xc223 5630302Sjoerg 5730302Sjoergint 5830302Sjoergmain(int argc, char **argv) 5930302Sjoerg{ 6030302Sjoerg int s, c; 6130302Sjoerg int errs = 0, verbose = 0; 6230302Sjoerg size_t off; 6330302Sjoerg const char *ifname, *cp; 6430302Sjoerg struct ifreq ifr; 6530302Sjoerg struct spppreq spr; 6630302Sjoerg 6730302Sjoerg while ((c = getopt(argc, argv, "v")) != -1) 6830302Sjoerg switch (c) { 6930302Sjoerg case 'v': 7030302Sjoerg verbose++; 7130302Sjoerg break; 7230302Sjoerg 7330302Sjoerg default: 7430302Sjoerg errs++; 7530302Sjoerg break; 7630302Sjoerg } 7730302Sjoerg argv += optind; 7830302Sjoerg argc -= optind; 7930302Sjoerg 8030302Sjoerg if (errs || argc < 1) 8130302Sjoerg usage(); 8230302Sjoerg 8330302Sjoerg ifname = argv[0]; 8430302Sjoerg strncpy(ifr.ifr_name, ifname, sizeof ifr.ifr_name); 8530302Sjoerg 8630302Sjoerg /* use a random AF to create the socket */ 8730302Sjoerg if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) 8830302Sjoerg err(EX_UNAVAILABLE, "ifconfig: socket"); 8930302Sjoerg 9030302Sjoerg argc--; 9130302Sjoerg argv++; 9230302Sjoerg 9330302Sjoerg spr.cmd = (int)SPPPIOGDEFS; 9430302Sjoerg ifr.ifr_data = (caddr_t)&spr; 9530302Sjoerg 9630302Sjoerg if (ioctl(s, SIOCGIFGENERIC, &ifr) == -1) 9730302Sjoerg err(EX_OSERR, "SIOCGIFGENERIC(SPPPIOGDEFS)"); 9830302Sjoerg 9930302Sjoerg if (argc == 0) { 10030302Sjoerg /* list only mode */ 10130302Sjoerg print_vals(ifname, &spr); 10230302Sjoerg return 0; 10330302Sjoerg } 10430302Sjoerg 10530302Sjoerg#define startswith(s) strncmp(argv[0], s, (off = strlen(s))) == 0 10630302Sjoerg 10730302Sjoerg while (argc > 0) { 10830302Sjoerg if (startswith("authproto=")) { 10930302Sjoerg cp = argv[0] + off; 11030302Sjoerg if (strcmp(cp, "pap") == 0) 11130302Sjoerg spr.defs.myauth.proto = 11230302Sjoerg spr.defs.hisauth.proto = PPP_PAP; 11330302Sjoerg else if (strcmp(cp, "chap") == 0) 11430302Sjoerg spr.defs.myauth.proto = 11530302Sjoerg spr.defs.hisauth.proto = PPP_CHAP; 11630302Sjoerg else if (strcmp(cp, "none") == 0) 11730302Sjoerg spr.defs.myauth.proto = 11830302Sjoerg spr.defs.hisauth.proto = 0; 11930302Sjoerg else 12030302Sjoerg errx(EX_DATAERR, "bad auth proto: %s", cp); 12130302Sjoerg } else if (startswith("myauthproto=")) { 12230302Sjoerg cp = argv[0] + off; 12330302Sjoerg if (strcmp(cp, "pap") == 0) 12430302Sjoerg spr.defs.myauth.proto = PPP_PAP; 12530302Sjoerg else if (strcmp(cp, "chap") == 0) 12630302Sjoerg spr.defs.myauth.proto = PPP_CHAP; 12730302Sjoerg else if (strcmp(cp, "none") == 0) 12830302Sjoerg spr.defs.myauth.proto = 0; 12930302Sjoerg else 13030302Sjoerg errx(EX_DATAERR, "bad auth proto: %s", cp); 13130302Sjoerg } else if (startswith("myauthname=")) 13230302Sjoerg strncpy(spr.defs.myauth.name, argv[0] + off, 13330302Sjoerg AUTHNAMELEN); 13430302Sjoerg else if (startswith("myauthsecret=") || 13530302Sjoerg startswith("myauthkey=")) 13630302Sjoerg strncpy(spr.defs.myauth.secret, argv[0] + off, 13730302Sjoerg AUTHKEYLEN); 13830302Sjoerg else if (startswith("hisauthproto=")) { 13930302Sjoerg cp = argv[0] + off; 14030302Sjoerg if (strcmp(cp, "pap") == 0) 14130302Sjoerg spr.defs.hisauth.proto = PPP_PAP; 14230302Sjoerg else if (strcmp(cp, "chap") == 0) 14330302Sjoerg spr.defs.hisauth.proto = PPP_CHAP; 14430302Sjoerg else if (strcmp(cp, "none") == 0) 14530302Sjoerg spr.defs.hisauth.proto = 0; 14630302Sjoerg else 14730302Sjoerg errx(EX_DATAERR, "bad auth proto: %s", cp); 14830302Sjoerg } else if (startswith("hisauthname=")) 14930302Sjoerg strncpy(spr.defs.hisauth.name, argv[0] + off, 15030302Sjoerg AUTHNAMELEN); 15130302Sjoerg else if (startswith("hisauthsecret=") || 15230302Sjoerg startswith("hisauthkey=")) 15330302Sjoerg strncpy(spr.defs.hisauth.secret, argv[0] + off, 15430302Sjoerg AUTHKEYLEN); 15530302Sjoerg else if (strcmp(argv[0], "callin") == 0) 15630302Sjoerg spr.defs.hisauth.flags |= AUTHFLAG_NOCALLOUT; 15730302Sjoerg else if (strcmp(argv[0], "always") == 0) 15830302Sjoerg spr.defs.hisauth.flags &= ~AUTHFLAG_NOCALLOUT; 15930302Sjoerg else if (strcmp(argv[0], "norechallenge") == 0) 16030302Sjoerg spr.defs.hisauth.flags |= AUTHFLAG_NORECHALLENGE; 16130302Sjoerg else if (strcmp(argv[0], "rechallenge") == 0) 16230302Sjoerg spr.defs.hisauth.flags &= ~AUTHFLAG_NORECHALLENGE; 16330302Sjoerg else 16430302Sjoerg errx(EX_DATAERR, "bad parameter: \"%s\"", argv[0]); 16530302Sjoerg 16630302Sjoerg argv++; 16730302Sjoerg argc--; 16830302Sjoerg } 16930302Sjoerg 17030302Sjoerg spr.cmd = (int)SPPPIOSDEFS; 17130302Sjoerg 17230302Sjoerg if (ioctl(s, SIOCSIFGENERIC, &ifr) == -1) 17330302Sjoerg err(EX_OSERR, "SIOCSIFGENERIC(SPPPIOSDEFS)"); 17430302Sjoerg 17530302Sjoerg if (verbose) 17630302Sjoerg print_vals(ifname, &spr); 17730302Sjoerg 17830302Sjoerg return 0; 17930302Sjoerg} 18030302Sjoerg 18130302Sjoergvoid 18230302Sjoergusage(void) 18330302Sjoerg{ 18430302Sjoerg errx(EX_USAGE, 18530302Sjoerg "usage: [-v] ifname [{my|his}auth{proto|name|secret}=..." 18630302Sjoerg "|callin|always]"); 18730302Sjoerg} 18830302Sjoerg 18930302Sjoergvoid 19030302Sjoergprint_vals(const char *ifname, struct spppreq *sp) 19130302Sjoerg{ 19230302Sjoerg printf("%s:\tphase=%s\n", ifname, phase_name(sp->defs.pp_phase)); 19330302Sjoerg if (sp->defs.myauth.proto) { 19430302Sjoerg printf("\tmyauthproto=%s myauthname=\"%.*s\"\n", 19530302Sjoerg proto_name(sp->defs.myauth.proto), 19630302Sjoerg AUTHNAMELEN, sp->defs.myauth.name); 19730302Sjoerg } 19830302Sjoerg if (sp->defs.hisauth.proto) { 19930302Sjoerg printf("\thisauthproto=%s hisauthname=\"%.*s\"%s\n", 20030302Sjoerg proto_name(sp->defs.hisauth.proto), 20130302Sjoerg AUTHNAMELEN, sp->defs.hisauth.name, 20230302Sjoerg authflags(sp->defs.hisauth.flags)); 20330302Sjoerg } 20430302Sjoerg} 20530302Sjoerg 20630302Sjoergconst char * 20730302Sjoergphase_name(enum ppp_phase phase) 20830302Sjoerg{ 20930302Sjoerg switch (phase) { 21030302Sjoerg case PHASE_DEAD: return "dead"; 21130302Sjoerg case PHASE_ESTABLISH: return "establish"; 21230302Sjoerg case PHASE_TERMINATE: return "terminate"; 21330302Sjoerg case PHASE_AUTHENTICATE: return "authenticate"; 21430302Sjoerg case PHASE_NETWORK: return "network"; 21530302Sjoerg } 21630302Sjoerg return "illegal"; 21730302Sjoerg} 21830302Sjoerg 21930302Sjoergconst char * 22030302Sjoergproto_name(u_short proto) 22130302Sjoerg{ 22230302Sjoerg static char buf[12]; 22330302Sjoerg switch (proto) { 22430302Sjoerg case PPP_PAP: return "pap"; 22530302Sjoerg case PPP_CHAP: return "chap"; 22630302Sjoerg } 22730302Sjoerg sprintf(buf, "0x%x", (unsigned)proto); 22830302Sjoerg return buf; 22930302Sjoerg} 23030302Sjoerg 23130302Sjoergconst char * 23230302Sjoergauthflags(u_short flags) 23330302Sjoerg{ 23430302Sjoerg static char buf[10]; 23530302Sjoerg buf[0] = '\0'; 23630302Sjoerg if (flags & AUTHFLAG_NOCALLOUT) 23730302Sjoerg strcat(buf, " callin"); 23830302Sjoerg if (flags & AUTHFLAG_NORECHALLENGE) 23930302Sjoerg strcat(buf, " norechallenge"); 24030302Sjoerg return buf; 24130302Sjoerg} 242