1994-10-28 15:06:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1994 Ugen J.S.Antsilevich
|
1994-11-16 10:18:18 +00:00
|
|
|
* Idea and grammar partially left from:
|
|
|
|
* Copyright (c) 1993 Daniel Boulet
|
1994-10-28 15:06:53 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source forms, with and without modification,
|
|
|
|
* are permitted provided that this entire comment appears intact.
|
|
|
|
*
|
|
|
|
* Redistribution in binary form may occur without any restrictions.
|
|
|
|
* Obviously, it would be nice if you gave credit where credit is due
|
|
|
|
* but requiring it would be too onerous.
|
|
|
|
*
|
|
|
|
* This software is provided ``AS IS'' without any warranties of any kind.
|
|
|
|
*
|
1994-11-16 10:18:18 +00:00
|
|
|
* NEW command line interface for IP firewall facility
|
1994-10-28 15:06:53 +00:00
|
|
|
*/
|
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <netdb.h>
|
1994-10-28 15:06:53 +00:00
|
|
|
#include <kvm.h>
|
|
|
|
#include <sys/socket.h>
|
1995-02-24 14:32:45 +00:00
|
|
|
#include <net/if.h>
|
1994-10-28 15:06:53 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/tcp.h>
|
1995-02-24 14:32:45 +00:00
|
|
|
#include <arpa/inet.h>
|
1994-11-16 10:18:18 +00:00
|
|
|
#define IPFIREWALL
|
|
|
|
#define IPACCT
|
1994-10-28 15:06:53 +00:00
|
|
|
#include <netinet/ip_fw.h>
|
1994-11-16 10:18:18 +00:00
|
|
|
|
|
|
|
#define MAXSTR 25
|
|
|
|
|
|
|
|
char progname[MAXSTR]; /* Program name for errors */
|
|
|
|
char proto_name[MAXSTR]=""; /* Current line protocol */
|
|
|
|
int s; /* main RAW socket */
|
|
|
|
int do_resolv=1; /* Would try to resolv all */
|
1994-12-12 17:19:33 +00:00
|
|
|
int do_short=0; /* Compact output */
|
|
|
|
int do_acct=0; /* Show packet/byte count */
|
1994-11-16 10:18:18 +00:00
|
|
|
int ports_ok=0; /* flag allowing ports */
|
|
|
|
u_short flags=0; /* New entry flags */
|
|
|
|
|
|
|
|
|
1995-03-03 12:28:34 +00:00
|
|
|
#define FW 0x001 /* Firewall action */
|
|
|
|
#define AC 0x002 /* Accounting action */
|
1994-11-16 10:18:18 +00:00
|
|
|
|
1995-03-03 12:28:34 +00:00
|
|
|
#define S_ANY "any"
|
1994-11-16 10:18:18 +00:00
|
|
|
|
1995-03-03 12:28:34 +00:00
|
|
|
#define IS_TO(x) (!strcmp(x,"to"))
|
|
|
|
#define IS_FROM(x) (!strcmp(x,"from"))
|
|
|
|
#define IS_VIA(x) (!strcmp(x,"via") || !strcmp(x,"on"))
|
|
|
|
#define IS_TOKEN(x) (IS_TO(x) || IS_FROM(x) || IS_VIA(x))
|
1994-11-16 10:18:18 +00:00
|
|
|
|
|
|
|
#define P_AC "a" /* of "accept" for policy action */
|
|
|
|
#define P_DE "d" /* of "deny" for policy action */
|
|
|
|
|
|
|
|
#define CH_FW "f" /* of "firewall" for chains in zero/flush */
|
|
|
|
#define CH_AC "a" /* of "accounting" for chain in zero/flush/list */
|
|
|
|
|
|
|
|
char action_tab[][MAXSTR]={
|
|
|
|
"addf",
|
1995-01-12 13:01:21 +00:00
|
|
|
#define A_ADDF 0
|
|
|
|
"delf",
|
|
|
|
#define A_DELF 1
|
1994-11-16 10:18:18 +00:00
|
|
|
"chkf",
|
1995-01-12 13:01:21 +00:00
|
|
|
#define A_CHKF 2
|
1994-11-16 10:18:18 +00:00
|
|
|
"adda",
|
1995-01-12 13:01:21 +00:00
|
|
|
#define A_ADDA 3
|
1994-11-16 10:18:18 +00:00
|
|
|
"dela",
|
1995-01-12 13:01:21 +00:00
|
|
|
#define A_DELA 4
|
1994-12-13 15:56:51 +00:00
|
|
|
"clr",
|
1995-01-12 13:01:21 +00:00
|
|
|
#define A_CLRA 5
|
1994-11-16 10:18:18 +00:00
|
|
|
"f",
|
1995-01-12 13:01:21 +00:00
|
|
|
#define A_FLUSH 6
|
1994-11-16 10:18:18 +00:00
|
|
|
"z",
|
1995-05-30 06:12:45 +00:00
|
|
|
#define A_ZERO 7
|
1994-11-16 10:18:18 +00:00
|
|
|
"l",
|
1995-05-30 06:12:45 +00:00
|
|
|
#define A_LIST 8
|
1995-03-30 12:18:10 +00:00
|
|
|
"po",
|
1995-05-30 06:12:45 +00:00
|
|
|
#define A_POLICY 9
|
1994-11-16 10:18:18 +00:00
|
|
|
"",
|
1995-01-12 13:01:21 +00:00
|
|
|
#define A_NONE 10
|
1994-11-16 10:18:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
char type_tab[][MAXSTR]={
|
1995-03-03 12:28:34 +00:00
|
|
|
"acc",
|
1994-11-16 10:18:18 +00:00
|
|
|
#define T_ACCEPT 0
|
1995-03-03 12:28:34 +00:00
|
|
|
"pas",
|
|
|
|
#define T_PASS 1
|
|
|
|
"log",
|
|
|
|
#define T_LOG 2
|
|
|
|
"rej",
|
|
|
|
#define T_REJECT 3
|
|
|
|
"lrej",
|
|
|
|
#define T_LREJECT 4
|
|
|
|
"den",
|
|
|
|
#define T_DENY 5
|
|
|
|
"lden",
|
|
|
|
#define T_LDENY 6
|
|
|
|
"sin",
|
|
|
|
#define T_SINGLE 7
|
|
|
|
"bid",
|
|
|
|
#define T_BIDIR 8
|
1994-11-16 10:18:18 +00:00
|
|
|
"",
|
1995-03-03 12:28:34 +00:00
|
|
|
#define T_NONE 9
|
1994-11-16 10:18:18 +00:00
|
|
|
};
|
1994-10-28 15:06:53 +00:00
|
|
|
|
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
char proto_tab[][MAXSTR]={
|
|
|
|
"all",
|
|
|
|
#define P_ALL 0
|
|
|
|
"icmp",
|
|
|
|
#define P_ICMP 1
|
|
|
|
"tcp",
|
|
|
|
#define P_TCP 2
|
1994-11-28 12:34:37 +00:00
|
|
|
"syn",
|
|
|
|
#define P_SYN 3
|
1994-11-16 10:18:18 +00:00
|
|
|
"udp",
|
1994-11-28 12:34:37 +00:00
|
|
|
#define P_UDP 4
|
1994-11-16 10:18:18 +00:00
|
|
|
""
|
1994-11-28 12:34:37 +00:00
|
|
|
#define P_NONE 5
|
1994-11-16 10:18:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct nlist nlf[]={
|
1995-01-12 13:01:21 +00:00
|
|
|
#define N_FCHAIN 0
|
|
|
|
{ "_ip_fw_chain" },
|
|
|
|
#define N_POLICY 1
|
1994-10-28 15:06:53 +00:00
|
|
|
{ "_ip_fw_policy" },
|
|
|
|
"" ,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
struct nlist nla[]={
|
|
|
|
#define N_ACHAIN 0
|
|
|
|
{ "_ip_acct_chain" },
|
|
|
|
"" ,
|
|
|
|
};
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
|
|
|
|
int mask_bits(m_ad)
|
|
|
|
struct in_addr m_ad;
|
1994-10-28 15:06:53 +00:00
|
|
|
{
|
1994-11-16 10:18:18 +00:00
|
|
|
int h_fnd=0,h_num=0,i;
|
|
|
|
u_long mask;
|
|
|
|
|
|
|
|
mask=ntohl(m_ad.s_addr);
|
|
|
|
for (i=0;i<sizeof(u_long)*CHAR_BIT;i++) {
|
|
|
|
if (mask & 1L) {
|
|
|
|
h_fnd=1;
|
|
|
|
h_num++;
|
|
|
|
} else {
|
|
|
|
if (h_fnd)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
mask=mask>>1;
|
1995-05-30 06:12:45 +00:00
|
|
|
}
|
1994-11-16 10:18:18 +00:00
|
|
|
return h_num;
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1994-11-16 10:18:18 +00:00
|
|
|
show_ipfw(chain,c_t)
|
1994-11-08 12:48:02 +00:00
|
|
|
struct ip_fw *chain;
|
1994-11-16 10:18:18 +00:00
|
|
|
int c_t;
|
1994-10-28 15:06:53 +00:00
|
|
|
{
|
1994-11-16 10:18:18 +00:00
|
|
|
char *comma;
|
|
|
|
u_long adrt;
|
|
|
|
struct hostent *he;
|
|
|
|
int i,mb;
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
|
1994-12-12 17:19:33 +00:00
|
|
|
if (do_short && do_acct) {
|
1994-12-13 15:56:51 +00:00
|
|
|
printf("%8d:%8d ",chain->fw_bcnt,chain->fw_pcnt);
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1995-05-30 06:12:45 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
|
1994-12-12 17:19:33 +00:00
|
|
|
if (do_short)
|
1994-11-16 10:18:18 +00:00
|
|
|
if (c_t==FW) {
|
1995-05-30 06:12:45 +00:00
|
|
|
if (chain->fw_flg & IP_FW_F_ACCEPT)
|
1994-12-13 15:56:51 +00:00
|
|
|
if (chain->fw_flg & IP_FW_F_PRN)
|
1994-11-28 12:34:37 +00:00
|
|
|
printf("l");
|
|
|
|
else
|
|
|
|
printf("a");
|
1995-05-30 06:12:45 +00:00
|
|
|
else
|
1994-12-13 15:56:51 +00:00
|
|
|
if (chain->fw_flg & IP_FW_F_PRN)
|
|
|
|
if (chain->fw_flg & IP_FW_F_ICMPRPL)
|
1994-11-28 12:34:37 +00:00
|
|
|
printf("R");
|
|
|
|
else
|
|
|
|
printf("D");
|
|
|
|
else
|
1994-12-13 15:56:51 +00:00
|
|
|
if (chain->fw_flg & IP_FW_F_ICMPRPL)
|
1994-11-28 12:34:37 +00:00
|
|
|
printf("r");
|
|
|
|
else
|
|
|
|
printf("d");
|
1994-11-16 10:18:18 +00:00
|
|
|
} else {
|
1995-05-30 06:12:45 +00:00
|
|
|
if (chain->fw_flg & IP_FW_F_BIDIR)
|
1994-11-28 12:34:37 +00:00
|
|
|
printf("b");
|
1995-05-30 06:12:45 +00:00
|
|
|
else
|
1994-11-28 12:34:37 +00:00
|
|
|
printf("s");
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (c_t==FW) {
|
1995-05-30 06:12:45 +00:00
|
|
|
if (chain->fw_flg & IP_FW_F_ACCEPT)
|
1994-12-13 15:56:51 +00:00
|
|
|
if (chain->fw_flg & IP_FW_F_PRN)
|
1994-11-28 12:34:37 +00:00
|
|
|
printf("log ");
|
|
|
|
else
|
|
|
|
printf("accept ");
|
1995-05-30 06:12:45 +00:00
|
|
|
else
|
1994-12-13 15:56:51 +00:00
|
|
|
if (chain->fw_flg & IP_FW_F_PRN)
|
|
|
|
if (chain->fw_flg & IP_FW_F_ICMPRPL)
|
1994-11-28 12:34:37 +00:00
|
|
|
printf("lreject ");
|
|
|
|
else
|
|
|
|
printf("ldeny ");
|
|
|
|
else
|
1994-12-13 15:56:51 +00:00
|
|
|
if (chain->fw_flg & IP_FW_F_ICMPRPL)
|
1994-11-28 12:34:37 +00:00
|
|
|
printf("reject ");
|
|
|
|
else
|
|
|
|
printf("deny ");
|
1994-11-16 10:18:18 +00:00
|
|
|
} else {
|
1995-05-30 06:12:45 +00:00
|
|
|
if (chain->fw_flg & IP_FW_F_BIDIR)
|
1994-11-16 10:18:18 +00:00
|
|
|
printf("bidir ");
|
1995-05-30 06:12:45 +00:00
|
|
|
else
|
1994-11-16 10:18:18 +00:00
|
|
|
printf("single ");
|
|
|
|
}
|
|
|
|
|
1994-12-12 17:19:33 +00:00
|
|
|
if (do_short)
|
1994-12-13 15:56:51 +00:00
|
|
|
switch (chain->fw_flg & IP_FW_F_KIND) {
|
1994-11-16 10:18:18 +00:00
|
|
|
case IP_FW_F_ICMP:
|
|
|
|
printf("I ");
|
|
|
|
break;
|
|
|
|
case IP_FW_F_TCP:
|
1994-12-13 15:56:51 +00:00
|
|
|
if (chain->fw_flg&IP_FW_F_TCPSYN)
|
1994-11-28 12:34:37 +00:00
|
|
|
printf("S ");
|
|
|
|
else
|
|
|
|
printf("T ");
|
1994-11-16 10:18:18 +00:00
|
|
|
break;
|
|
|
|
case IP_FW_F_UDP:
|
|
|
|
printf("U ");
|
|
|
|
break;
|
|
|
|
case IP_FW_F_ALL:
|
|
|
|
printf("A ");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
1994-12-13 15:56:51 +00:00
|
|
|
switch (chain->fw_flg & IP_FW_F_KIND) {
|
1994-11-16 10:18:18 +00:00
|
|
|
case IP_FW_F_ICMP:
|
|
|
|
printf("icmp ");
|
|
|
|
break;
|
|
|
|
case IP_FW_F_TCP:
|
1994-12-13 15:56:51 +00:00
|
|
|
if (chain->fw_flg&IP_FW_F_TCPSYN)
|
1994-11-28 12:34:37 +00:00
|
|
|
printf("syn ");
|
|
|
|
else
|
|
|
|
printf("tcp ");
|
1994-11-16 10:18:18 +00:00
|
|
|
break;
|
|
|
|
case IP_FW_F_UDP:
|
|
|
|
printf("udp ");
|
|
|
|
break;
|
|
|
|
case IP_FW_F_ALL:
|
|
|
|
printf("all ");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1994-12-12 17:19:33 +00:00
|
|
|
if (do_short)
|
1994-11-16 10:18:18 +00:00
|
|
|
printf("[");
|
|
|
|
else
|
|
|
|
printf("from ");
|
|
|
|
|
1994-12-13 15:56:51 +00:00
|
|
|
adrt=ntohl(chain->fw_smsk.s_addr);
|
1994-11-16 10:18:18 +00:00
|
|
|
if (adrt==ULONG_MAX && do_resolv) {
|
1994-12-13 15:56:51 +00:00
|
|
|
adrt=(chain->fw_src.s_addr);
|
1994-11-16 10:18:18 +00:00
|
|
|
he=gethostbyaddr((char *)&adrt,sizeof(u_long),AF_INET);
|
|
|
|
if (he==NULL) {
|
1994-12-13 15:56:51 +00:00
|
|
|
printf(inet_ntoa(chain->fw_src));
|
1994-11-16 10:18:18 +00:00
|
|
|
printf(":");
|
1994-12-13 15:56:51 +00:00
|
|
|
printf(inet_ntoa(chain->fw_smsk));
|
1994-11-16 10:18:18 +00:00
|
|
|
} else
|
1994-10-28 15:06:53 +00:00
|
|
|
printf("%s",he->h_name);
|
1994-11-16 10:18:18 +00:00
|
|
|
} else {
|
1994-12-13 15:56:51 +00:00
|
|
|
printf(inet_ntoa(chain->fw_src));
|
1994-11-16 10:18:18 +00:00
|
|
|
if (adrt!=ULONG_MAX)
|
1994-12-13 15:56:51 +00:00
|
|
|
if ((mb=mask_bits(chain->fw_smsk))>=0)
|
1994-11-16 10:18:18 +00:00
|
|
|
printf("/%d",mb);
|
|
|
|
else {
|
|
|
|
printf(":");
|
1994-12-13 15:56:51 +00:00
|
|
|
printf(inet_ntoa(chain->fw_smsk));
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
comma = " ";
|
1994-12-13 15:56:51 +00:00
|
|
|
for (i=0;i<chain->fw_nsp; i++ ) {
|
|
|
|
printf("%s%d",comma,chain->fw_pts[i]);
|
1995-05-30 06:12:45 +00:00
|
|
|
if (i==0 && (chain->fw_flg & IP_FW_F_SRNG))
|
1994-11-16 10:18:18 +00:00
|
|
|
comma = ":";
|
1995-05-30 06:12:45 +00:00
|
|
|
else
|
1994-11-16 10:18:18 +00:00
|
|
|
comma = ",";
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1994-12-12 17:19:33 +00:00
|
|
|
if (do_short)
|
1994-11-16 10:18:18 +00:00
|
|
|
printf("][");
|
|
|
|
else
|
|
|
|
printf(" to ");
|
|
|
|
|
1994-12-13 15:56:51 +00:00
|
|
|
adrt=ntohl(chain->fw_dmsk.s_addr);
|
1994-11-16 10:18:18 +00:00
|
|
|
if (adrt==ULONG_MAX && do_resolv) {
|
1994-12-13 15:56:51 +00:00
|
|
|
adrt=(chain->fw_dst.s_addr);
|
1994-11-16 10:18:18 +00:00
|
|
|
he=gethostbyaddr((char *)&adrt,sizeof(u_long),AF_INET);
|
|
|
|
if (he==NULL) {
|
1994-12-13 15:56:51 +00:00
|
|
|
printf(inet_ntoa(chain->fw_dst));
|
1994-11-16 10:18:18 +00:00
|
|
|
printf(":");
|
1994-12-13 15:56:51 +00:00
|
|
|
printf(inet_ntoa(chain->fw_dmsk));
|
1994-11-16 10:18:18 +00:00
|
|
|
} else
|
1994-10-28 15:06:53 +00:00
|
|
|
printf("%s",he->h_name);
|
1994-11-16 10:18:18 +00:00
|
|
|
} else {
|
1994-12-13 15:56:51 +00:00
|
|
|
printf(inet_ntoa(chain->fw_dst));
|
1995-05-30 06:12:45 +00:00
|
|
|
if (adrt!=ULONG_MAX)
|
1994-12-13 15:56:51 +00:00
|
|
|
if ((mb=mask_bits(chain->fw_dmsk))>=0)
|
1994-11-16 10:18:18 +00:00
|
|
|
printf("/%d",mb);
|
|
|
|
else {
|
|
|
|
printf(":");
|
1994-12-13 15:56:51 +00:00
|
|
|
printf(inet_ntoa(chain->fw_dmsk));
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
comma = " ";
|
1994-12-13 15:56:51 +00:00
|
|
|
for (i=0;i<chain->fw_ndp;i++) {
|
|
|
|
printf("%s%d",comma,chain->fw_pts[chain->fw_nsp+i]);
|
|
|
|
if (i==chain->fw_nsp && (chain->fw_flg & IP_FW_F_DRNG))
|
1994-11-16 10:18:18 +00:00
|
|
|
comma = ":";
|
1995-05-30 06:12:45 +00:00
|
|
|
else
|
1994-10-28 15:06:53 +00:00
|
|
|
comma = ",";
|
|
|
|
}
|
1994-11-16 10:18:18 +00:00
|
|
|
|
1995-02-24 14:32:45 +00:00
|
|
|
if (chain->fw_flg&IP_FW_F_IFNAME && chain->fw_via_name[0]) {
|
|
|
|
char ifnb[FW_IFNLEN+1];
|
|
|
|
if (do_short)
|
|
|
|
printf("][");
|
|
|
|
else
|
|
|
|
printf(" via ");
|
|
|
|
strncpy(ifnb,chain->fw_via_name,FW_IFNLEN);
|
|
|
|
ifnb[FW_IFNLEN]='\0';
|
|
|
|
printf("%s%d",ifnb,chain->fw_via_unit);
|
|
|
|
} else
|
|
|
|
if (chain->fw_via_ip.s_addr) {
|
1994-12-12 17:19:33 +00:00
|
|
|
if (do_short)
|
|
|
|
printf("][");
|
|
|
|
else
|
|
|
|
printf(" via ");
|
1995-02-24 14:32:45 +00:00
|
|
|
printf(inet_ntoa(chain->fw_via_ip));
|
1994-12-12 17:19:33 +00:00
|
|
|
}
|
|
|
|
if (do_short)
|
1994-11-16 10:18:18 +00:00
|
|
|
printf("]\n");
|
|
|
|
else
|
|
|
|
printf("\n");
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
list(av)
|
|
|
|
char **av;
|
1994-10-28 15:06:53 +00:00
|
|
|
{
|
|
|
|
kvm_t *kd;
|
|
|
|
static char errb[_POSIX2_LINE_MAX];
|
1994-11-08 12:48:02 +00:00
|
|
|
struct ip_fw b,*btmp;
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
if (!(kd=kvm_openfiles(NULL,NULL,NULL,O_RDONLY,errb))) {
|
|
|
|
fprintf(stderr,"%s: kvm_openfiles: %s\n",
|
|
|
|
progname,kvm_geterr(kd));
|
|
|
|
exit(1);
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1995-01-12 13:01:21 +00:00
|
|
|
if (*av==NULL || !strncmp(*av,CH_FW,strlen(CH_FW))) {
|
1994-11-16 10:18:18 +00:00
|
|
|
if (kvm_nlist(kd,nlf)<0 || nlf[0].n_type==0) {
|
|
|
|
fprintf(stderr,"%s: kvm_nlist: no namelist in %s\n",
|
|
|
|
progname,getbootfile());
|
|
|
|
exit(1);
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
1995-01-12 13:01:21 +00:00
|
|
|
if (*av==NULL || !strncmp(*av,CH_FW,strlen(CH_FW))) {
|
1994-11-16 10:18:18 +00:00
|
|
|
kvm_read(kd,(u_long)nlf[N_FCHAIN].n_value,&b,sizeof(struct ip_fw));
|
1995-01-12 13:01:21 +00:00
|
|
|
printf("FireWall chain entries:\n");
|
1994-12-13 15:56:51 +00:00
|
|
|
while(b.fw_next!=NULL) {
|
|
|
|
btmp=b.fw_next;
|
1994-11-16 10:18:18 +00:00
|
|
|
kvm_read(kd,(u_long)btmp,&b,sizeof(struct ip_fw));
|
|
|
|
show_ipfw(&b,FW);
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
if (*av==NULL || !strncmp(*av,CH_AC,strlen(CH_AC))) {
|
|
|
|
if (kvm_nlist(kd,nla)<0 || nla[0].n_type==0) {
|
|
|
|
fprintf(stderr,"%s: kvm_nlist: no namelist in %s\n",
|
|
|
|
progname,getbootfile());
|
|
|
|
exit(1);
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
if (*av==NULL || !strncmp(*av,CH_AC,strlen(CH_AC))) {
|
|
|
|
kvm_read(kd,(u_long)nla[N_ACHAIN].n_value,&b,sizeof(struct ip_fw));
|
|
|
|
printf("Accounting chain entries:\n");
|
1994-12-13 15:56:51 +00:00
|
|
|
while(b.fw_next!=NULL) {
|
|
|
|
btmp=b.fw_next;
|
1994-11-16 10:18:18 +00:00
|
|
|
kvm_read(kd,(u_long)btmp,&b,sizeof(struct ip_fw));
|
|
|
|
show_ipfw(&b,AC);
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
int get_num(str,tab)
|
|
|
|
char *str;
|
|
|
|
char tab[][MAXSTR];
|
|
|
|
{
|
|
|
|
int i=0;
|
|
|
|
while(tab[i][0]!='\0') {
|
|
|
|
if (strlen(str)>=strlen(tab[i]))
|
|
|
|
if (!strncmp(str,tab[i],strlen(tab[i])))
|
|
|
|
return i;
|
|
|
|
i++;
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
1994-11-16 10:18:18 +00:00
|
|
|
return i;
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
1995-03-03 12:28:34 +00:00
|
|
|
void show_usage(str)
|
|
|
|
char *str;
|
1994-11-16 10:18:18 +00:00
|
|
|
{
|
1995-03-03 12:28:34 +00:00
|
|
|
if (str)
|
|
|
|
fprintf(stderr,"%s: ERROR - %s\n",progname,str);
|
|
|
|
else
|
|
|
|
fprintf(stderr,"%s: ERROR - bad arguments\n",progname);
|
|
|
|
fprintf(stderr,"See man %s(8) for proper usage.\n",progname);
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
u_short get_port(str)
|
|
|
|
char *str;
|
|
|
|
{
|
|
|
|
struct servent *sptr;
|
|
|
|
char *end;
|
|
|
|
int port,slen = strlen(str);
|
|
|
|
|
|
|
|
if ((slen>0) && (strspn(str,"0123456789")==slen)) {
|
|
|
|
port = strtol(str,&end,10);
|
|
|
|
if (*end!='\0') {
|
|
|
|
fprintf(stderr,"%s: illegal port number :%s\n"
|
|
|
|
,progname,str);
|
|
|
|
exit(1);
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
if ((port<=0) || (port>USHRT_MAX)) {
|
|
|
|
fprintf(stderr,"%s: port number out of range :%d\n"
|
|
|
|
,progname,port);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return((u_short)port);
|
|
|
|
} else {
|
|
|
|
sptr = getservbyname(str,proto_name);
|
|
|
|
if (!sptr) {
|
|
|
|
fprintf(stderr,"%s: unknown service :%s\n"
|
|
|
|
,progname,str);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return((u_short)ntohs(sptr->s_port));
|
|
|
|
}
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
char *findchar(str,c)
|
|
|
|
char *str;
|
|
|
|
char c;
|
|
|
|
{
|
|
|
|
int i,len=strlen(str);
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
for (i=0;i<len;i++) {
|
|
|
|
if (str[i]==c)
|
|
|
|
return(char*)(&str[i]);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
int set_entry_ports(str,ports,a_max,is_range)
|
|
|
|
char *str;
|
|
|
|
u_short *ports;
|
|
|
|
int a_max;
|
|
|
|
int *is_range;
|
|
|
|
{
|
|
|
|
char *s_pr2,*s_h,*s_t,*cp;
|
1995-05-30 06:12:45 +00:00
|
|
|
u_short p1,p2;
|
1994-11-16 10:18:18 +00:00
|
|
|
int i=0;
|
|
|
|
|
|
|
|
(void)strtok(str,":");
|
|
|
|
s_pr2=strtok(NULL,"");
|
|
|
|
if (s_pr2) {
|
|
|
|
p1 = get_port(str);
|
|
|
|
p2 = get_port(s_pr2);
|
|
|
|
if (a_max<2) {
|
|
|
|
fprintf(stderr,"%s: too many ports.\n",progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
ports[0]=p1;
|
|
|
|
ports[1]=p2;
|
|
|
|
*is_range=1;
|
|
|
|
return 2;
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
1994-11-16 10:18:18 +00:00
|
|
|
s_h=str;
|
|
|
|
while ((cp=findchar(s_h,','))!=NULL) {
|
|
|
|
if (i>a_max) {
|
|
|
|
fprintf(stderr,"%s: too many ports.\n",progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
*cp='\0';
|
|
|
|
if ((s_t=(++cp))=='\0') {
|
|
|
|
fprintf(stderr,"%s: bad port list.\n",progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
ports[i++]=get_port(s_h);
|
|
|
|
s_h=s_t;
|
|
|
|
}
|
|
|
|
if (i>a_max) {
|
|
|
|
fprintf(stderr,"%s: too many ports.\n",progname);
|
1994-10-28 15:06:53 +00:00
|
|
|
exit(1);
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
|
|
|
ports[i]=get_port(s_h);
|
|
|
|
*is_range=0;
|
|
|
|
return (i+1);
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
void set_entry_ip(str,addr,mask)
|
|
|
|
char *str;
|
|
|
|
struct in_addr *addr,*mask;
|
|
|
|
{
|
|
|
|
char *sm_bit,*sm_oct,*end;
|
|
|
|
int n_bit;
|
|
|
|
struct hostent *hptr;
|
|
|
|
|
1995-03-03 12:28:34 +00:00
|
|
|
if (!strncmp(str,S_ANY,strlen(S_ANY))) {
|
|
|
|
addr->s_addr=0L;
|
|
|
|
mask->s_addr=0L;
|
|
|
|
return;
|
|
|
|
}
|
1995-05-30 06:12:45 +00:00
|
|
|
|
1995-03-03 12:28:34 +00:00
|
|
|
|
1994-12-12 17:19:33 +00:00
|
|
|
if (mask) {
|
|
|
|
(void)strtok(str,"/");
|
|
|
|
sm_bit=strtok(NULL,"");
|
|
|
|
(void)strtok(str,":");
|
|
|
|
sm_oct=strtok(NULL,"");
|
|
|
|
}
|
1995-05-30 06:12:45 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
if (!inet_aton(str,addr)) {
|
|
|
|
if (do_resolv) {
|
|
|
|
if (!(hptr=gethostbyname(str))) {
|
|
|
|
fprintf(stderr,"%s: Unknown host name : %s\n",
|
|
|
|
progname,str);
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
addr->s_addr=*((u_long *)hptr->h_addr);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf(stderr,"%s: Bad IP : %s\n",progname,str);
|
|
|
|
exit(1);
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
1994-12-12 17:19:33 +00:00
|
|
|
/*
|
1995-05-30 06:12:45 +00:00
|
|
|
* This is in case mask we
|
1994-12-12 17:19:33 +00:00
|
|
|
* want to set IP only
|
|
|
|
*/
|
|
|
|
if (!mask)
|
|
|
|
return;
|
1994-11-16 10:18:18 +00:00
|
|
|
mask->s_addr=htonl(ULONG_MAX);
|
|
|
|
|
|
|
|
if (sm_bit) {
|
|
|
|
n_bit = strtol(sm_bit,&end,10);
|
|
|
|
if (*end!='\0') {
|
1995-03-03 12:28:34 +00:00
|
|
|
show_usage(NULL);
|
1994-11-16 10:18:18 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (n_bit<0 || n_bit>sizeof(u_long)*CHAR_BIT) {
|
1995-03-03 12:28:34 +00:00
|
|
|
show_usage(NULL);
|
1994-11-16 10:18:18 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1994-11-28 12:34:37 +00:00
|
|
|
if (n_bit>0)
|
|
|
|
mask->s_addr=
|
1994-11-16 10:18:18 +00:00
|
|
|
htonl(ULONG_MAX<<(sizeof(u_long)*CHAR_BIT-n_bit));
|
1994-11-28 12:34:37 +00:00
|
|
|
else
|
|
|
|
mask->s_addr=0L;
|
1995-05-30 06:12:45 +00:00
|
|
|
}
|
1994-11-16 10:18:18 +00:00
|
|
|
|
|
|
|
if (sm_oct) {
|
|
|
|
if (!inet_aton(sm_oct,mask)) {
|
1995-03-03 12:28:34 +00:00
|
|
|
show_usage(NULL);
|
1994-11-16 10:18:18 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
/*
|
1994-11-16 10:18:18 +00:00
|
|
|
* Ugh..better of corse do it in kernel so no error possible
|
|
|
|
* but faster here so this way it goes...
|
1994-10-28 15:06:53 +00:00
|
|
|
*/
|
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
addr->s_addr=mask->s_addr & addr->s_addr;
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
|
1995-05-30 06:12:45 +00:00
|
|
|
int set_entry_ifname(str,frwl)
|
1995-02-24 14:32:45 +00:00
|
|
|
char *str;
|
|
|
|
struct ip_fw * frwl;
|
|
|
|
{
|
|
|
|
char name[IFNAMSIZ],buf[IFNAMSIZ],*sptr;
|
|
|
|
short unit;
|
|
|
|
int i;
|
1995-05-30 06:12:45 +00:00
|
|
|
|
1995-02-24 14:32:45 +00:00
|
|
|
i=0; sptr=str;
|
1995-03-03 12:28:34 +00:00
|
|
|
while(isalpha(*sptr++)) {
|
1995-02-24 14:32:45 +00:00
|
|
|
i++;
|
1995-03-03 12:28:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*sptr--;
|
1995-02-24 14:32:45 +00:00
|
|
|
|
|
|
|
if (i==0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
strncpy(name,str,i);
|
1995-03-03 12:28:34 +00:00
|
|
|
name[i]='\0';
|
|
|
|
|
1995-02-24 14:32:45 +00:00
|
|
|
unit=(short)atoi(sptr);
|
|
|
|
|
|
|
|
sprintf(buf,"%s%d",name,unit);
|
|
|
|
if (strcmp(str,buf))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
strncpy(frwl->fw_via_name,name,FW_IFNLEN);
|
|
|
|
frwl->fw_via_unit=unit;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1995-05-30 06:12:45 +00:00
|
|
|
void set_entry(av,frwl)
|
1994-11-16 10:18:18 +00:00
|
|
|
char **av;
|
|
|
|
struct ip_fw * frwl;
|
1994-10-28 15:06:53 +00:00
|
|
|
{
|
1995-03-03 12:28:34 +00:00
|
|
|
int ir;
|
|
|
|
int got_from=0,got_to=0,got_via=0;
|
|
|
|
#define T_FROM 1
|
|
|
|
#define T_TO 2
|
|
|
|
#define T_VIA 3
|
|
|
|
int token;
|
|
|
|
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1995-03-03 12:28:34 +00:00
|
|
|
/*
|
|
|
|
* This section actually creates
|
|
|
|
* generic entry which matches everything
|
|
|
|
* in this sorry world...
|
|
|
|
*/
|
1994-12-13 15:56:51 +00:00
|
|
|
frwl->fw_nsp=0;
|
|
|
|
frwl->fw_ndp=0;
|
1995-02-24 14:32:45 +00:00
|
|
|
frwl->fw_via_ip.s_addr=0L;
|
1995-03-03 12:28:34 +00:00
|
|
|
frwl->fw_src.s_addr=0L;
|
|
|
|
frwl->fw_dst.s_addr=0L;
|
|
|
|
frwl->fw_smsk.s_addr=0L;
|
|
|
|
frwl->fw_dmsk.s_addr=0L;
|
1994-10-28 15:06:53 +00:00
|
|
|
|
|
|
|
|
1995-03-03 12:28:34 +00:00
|
|
|
get_next:
|
|
|
|
|
|
|
|
token = 0;
|
|
|
|
|
|
|
|
if (IS_FROM(*av)) {
|
|
|
|
token = T_FROM;
|
|
|
|
|
|
|
|
if (got_from) {
|
|
|
|
show_usage("Redefined 'from'.");
|
1994-11-16 10:18:18 +00:00
|
|
|
exit(1);
|
1995-03-03 12:28:34 +00:00
|
|
|
}
|
|
|
|
if (*(++av)==NULL) {
|
|
|
|
show_usage("Missing 'from' specification.");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
set_entry_ip(*av,&(frwl->fw_src),&(frwl->fw_smsk));
|
|
|
|
got_from=1;
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
1995-03-03 12:28:34 +00:00
|
|
|
if (IS_TO(*av)) {
|
|
|
|
token = T_TO;
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1995-03-03 12:28:34 +00:00
|
|
|
if (got_to) {
|
|
|
|
show_usage("Redefined 'to'.");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (*(++av)==NULL) {
|
|
|
|
show_usage("Missing 'to' specification.");
|
1994-11-16 10:18:18 +00:00
|
|
|
exit(1);
|
1995-03-03 12:28:34 +00:00
|
|
|
}
|
1995-05-30 06:12:45 +00:00
|
|
|
|
1995-03-03 12:28:34 +00:00
|
|
|
set_entry_ip(*av,&(frwl->fw_dst),&(frwl->fw_dmsk));
|
|
|
|
got_to = 1;
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
1995-03-03 12:28:34 +00:00
|
|
|
if (IS_VIA(*av)) {
|
|
|
|
token = T_VIA;
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1995-03-03 12:28:34 +00:00
|
|
|
if (got_via) {
|
|
|
|
show_usage("Redefined 'via'.");
|
|
|
|
exit(1);
|
|
|
|
}
|
1994-11-16 10:18:18 +00:00
|
|
|
if (*(++av)==NULL) {
|
1995-03-03 12:28:34 +00:00
|
|
|
show_usage("Missing 'via' specification.");
|
|
|
|
exit(1);
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1995-03-03 12:28:34 +00:00
|
|
|
/*
|
1995-05-30 06:12:45 +00:00
|
|
|
* Try first to set interface name
|
1995-03-03 12:28:34 +00:00
|
|
|
* from arguments.set_entry_ip() will exit on
|
|
|
|
* wrong argument.
|
|
|
|
*/
|
|
|
|
if (set_entry_ifname(*av,frwl))
|
|
|
|
set_entry_ip(*av,&(frwl->fw_via_ip),NULL);
|
|
|
|
else
|
|
|
|
flags |= IP_FW_F_IFNAME;
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1995-03-03 12:28:34 +00:00
|
|
|
got_via = 1;
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
if (*(++av)==NULL) {
|
1995-03-03 12:28:34 +00:00
|
|
|
return;
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
1994-11-16 10:18:18 +00:00
|
|
|
|
1995-03-03 12:28:34 +00:00
|
|
|
if (IS_TOKEN(*av))
|
|
|
|
goto get_next;
|
1994-12-12 17:19:33 +00:00
|
|
|
|
1995-03-03 12:28:34 +00:00
|
|
|
if (ports_ok && token == T_FROM) {
|
|
|
|
ir = 0;
|
|
|
|
frwl->fw_nsp=
|
|
|
|
set_entry_ports(*av,frwl->fw_pts,IP_FW_MAX_PORTS,&ir);
|
|
|
|
if (ir)
|
|
|
|
flags|=IP_FW_F_SRNG;
|
1994-11-16 10:18:18 +00:00
|
|
|
|
1995-03-03 12:28:34 +00:00
|
|
|
if (*(++av)==NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ports_ok && token == T_TO) {
|
|
|
|
ir = 0;
|
1994-12-13 15:56:51 +00:00
|
|
|
frwl->fw_ndp=
|
|
|
|
set_entry_ports(*av,&(frwl->fw_pts[frwl->fw_nsp]),
|
|
|
|
(IP_FW_MAX_PORTS-frwl->fw_nsp),&ir);
|
1994-11-16 10:18:18 +00:00
|
|
|
if (ir)
|
|
|
|
flags|=IP_FW_F_DRNG;
|
1995-02-14 08:28:27 +00:00
|
|
|
|
1995-03-03 12:28:34 +00:00
|
|
|
if (*(++av)==NULL) {
|
|
|
|
return;
|
|
|
|
}
|
1994-12-12 17:19:33 +00:00
|
|
|
}
|
|
|
|
|
1995-03-03 12:28:34 +00:00
|
|
|
if (token == 0) {
|
|
|
|
show_usage("Unknown token.");
|
|
|
|
exit(1);
|
1994-12-12 17:19:33 +00:00
|
|
|
}
|
|
|
|
|
1995-03-03 12:28:34 +00:00
|
|
|
goto get_next;
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
flush(av)
|
|
|
|
char **av;
|
1994-10-28 15:06:53 +00:00
|
|
|
{
|
1994-11-16 10:18:18 +00:00
|
|
|
if (*av==NULL) {
|
|
|
|
if (setsockopt(s,IPPROTO_IP,IP_FW_FLUSH,NULL,0)<0) {
|
|
|
|
fprintf(stderr,"%s: setsockopt failed.\n",progname);
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
printf("All firewall entries flushed.\n");
|
|
|
|
}
|
|
|
|
if (setsockopt(s,IPPROTO_IP,IP_ACCT_FLUSH,NULL,0)<0) {
|
|
|
|
fprintf(stderr,"%s: setsockopt failed.\n",progname);
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
printf("All accounting entries flushed.\n");
|
|
|
|
}
|
|
|
|
exit(0);
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
1994-11-16 10:18:18 +00:00
|
|
|
if (!strncmp(*av,CH_FW,strlen(CH_FW))) {
|
|
|
|
if (setsockopt(s,IPPROTO_IP,IP_FW_FLUSH,NULL,0)<0) {
|
|
|
|
fprintf(stderr,"%s: setsockopt failed.\n",progname);
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
printf("All firewall entries flushed.\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!strncmp(*av,CH_AC,strlen(CH_AC))) {
|
|
|
|
if (setsockopt(s,IPPROTO_IP,IP_ACCT_FLUSH,NULL,0)<0) {
|
|
|
|
fprintf(stderr,"%s: setsockopt failed.\n",progname);
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
printf("All accounting entries flushed.\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
|
|
|
|
void policy(av)
|
|
|
|
char **av;
|
1994-10-28 15:06:53 +00:00
|
|
|
{
|
1994-11-28 12:34:37 +00:00
|
|
|
u_short p=0,b;
|
1994-10-28 15:06:53 +00:00
|
|
|
kvm_t *kd;
|
|
|
|
static char errb[_POSIX2_LINE_MAX];
|
|
|
|
|
1994-11-28 12:34:37 +00:00
|
|
|
if (*av==NULL || strlen(*av)<=0) {
|
1994-11-16 10:18:18 +00:00
|
|
|
if ( (kd=kvm_openfiles(NULL,NULL,NULL,O_RDONLY,errb)) == NULL) {
|
|
|
|
fprintf(stderr,"%s: kvm_openfiles: %s\n",progname,kvm_geterr(kd));
|
1994-10-28 15:06:53 +00:00
|
|
|
exit(1);
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
|
|
|
if (kvm_nlist(kd,nlf) < 0 || nlf[0].n_type == 0) {
|
|
|
|
fprintf(stderr,"%s: kvm_nlist: no namelist in %s\n",
|
1995-05-30 06:12:45 +00:00
|
|
|
progname,getbootfile());
|
1994-10-28 15:06:53 +00:00
|
|
|
exit(1);
|
1994-11-28 12:34:37 +00:00
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
kvm_read(kd,(u_long)nlf[N_POLICY].n_value,&b,sizeof(int));
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1994-11-28 12:34:37 +00:00
|
|
|
if (b&IP_FW_P_DENY)
|
1994-10-28 15:06:53 +00:00
|
|
|
printf("Default policy: DENY\n");
|
1994-11-28 12:34:37 +00:00
|
|
|
else
|
|
|
|
printf("Default policy: ACCEPT\n");
|
1994-10-28 15:06:53 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
if (!strncmp(*av,P_DE,strlen(P_DE)))
|
1994-11-28 12:34:37 +00:00
|
|
|
p|=IP_FW_P_DENY;
|
1994-11-16 10:18:18 +00:00
|
|
|
else
|
|
|
|
if (!strncmp(*av,P_AC,strlen(P_AC)))
|
1994-11-28 12:34:37 +00:00
|
|
|
p&=~IP_FW_P_DENY;
|
1994-11-16 10:18:18 +00:00
|
|
|
else {
|
|
|
|
fprintf(stderr,"%s: bad policy value.\n",progname);
|
1994-10-28 15:06:53 +00:00
|
|
|
exit(1);
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
if (setsockopt(s,IPPROTO_IP,IP_FW_POLICY,&p,sizeof(p))<0) {
|
|
|
|
fprintf(stderr,"%s: setsockopt failed.\n",progname);
|
1994-10-28 15:06:53 +00:00
|
|
|
exit(1);
|
1994-11-16 10:18:18 +00:00
|
|
|
} else {
|
1994-11-28 12:34:37 +00:00
|
|
|
if (p&IP_FW_P_DENY)
|
1994-11-16 10:18:18 +00:00
|
|
|
printf("Policy set to DENY.\n");
|
1994-11-28 12:34:37 +00:00
|
|
|
else
|
|
|
|
printf("Policy set to ACCEPT.\n");
|
1994-11-16 10:18:18 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
zero()
|
|
|
|
{
|
|
|
|
if (setsockopt(s,IPPROTO_IP,IP_ACCT_ZERO,NULL,0)<0) {
|
|
|
|
fprintf(stderr,"%s: setsockopt failed.\n",progname);
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
printf("Accounting cleared.\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
main(ac,av)
|
|
|
|
int ac;
|
|
|
|
char **av;
|
|
|
|
{
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
char ch;
|
|
|
|
extern int optind;
|
1995-03-03 12:28:34 +00:00
|
|
|
int ctl,int_t,is_check=0,int_notdef=0;
|
1994-11-16 10:18:18 +00:00
|
|
|
struct ip_fw frwl;
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
strcpy(progname,*av);
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
s = socket( AF_INET, SOCK_RAW, IPPROTO_RAW );
|
|
|
|
if ( s < 0 ) {
|
1995-05-30 06:12:45 +00:00
|
|
|
fprintf(stderr,"%s: Can't open raw socket.Must be root to use this programm. \n",progname);
|
1994-11-16 10:18:18 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if ( ac == 1 ) {
|
1995-03-03 12:28:34 +00:00
|
|
|
show_usage(NULL);
|
1994-11-16 10:18:18 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1994-12-12 17:19:33 +00:00
|
|
|
while ((ch = getopt(ac, av ,"ans")) != EOF)
|
1994-11-16 10:18:18 +00:00
|
|
|
switch(ch) {
|
1994-12-12 17:19:33 +00:00
|
|
|
case 'a':
|
|
|
|
do_acct=1;
|
|
|
|
break;
|
1994-11-16 10:18:18 +00:00
|
|
|
case 'n':
|
|
|
|
do_resolv=0;
|
|
|
|
break;
|
1994-12-12 17:19:33 +00:00
|
|
|
case 's':
|
|
|
|
do_short=1;
|
1994-11-16 10:18:18 +00:00
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
1995-03-03 12:28:34 +00:00
|
|
|
show_usage(NULL);
|
1995-05-30 06:12:45 +00:00
|
|
|
exit(1);
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
if (*(av+=optind)==NULL) {
|
1995-03-03 12:28:34 +00:00
|
|
|
show_usage(NULL);
|
1994-11-16 10:18:18 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
switch(get_num(*av,action_tab)) {
|
|
|
|
case A_ADDF:
|
1995-01-12 13:01:21 +00:00
|
|
|
ctl=IP_FW_ADD;
|
1994-11-16 10:18:18 +00:00
|
|
|
int_t=FW;
|
|
|
|
break;
|
|
|
|
case A_DELF:
|
1995-01-12 13:01:21 +00:00
|
|
|
ctl=IP_FW_DEL;
|
1994-11-16 10:18:18 +00:00
|
|
|
int_t=FW;
|
|
|
|
break;
|
|
|
|
case A_CHKF:
|
|
|
|
int_t=FW;
|
|
|
|
is_check=1;
|
|
|
|
break;
|
|
|
|
case A_ADDA:
|
|
|
|
ctl=IP_ACCT_ADD;
|
|
|
|
int_t=AC;
|
|
|
|
break;
|
|
|
|
case A_DELA:
|
|
|
|
ctl=IP_ACCT_DEL;
|
|
|
|
int_t=AC;
|
|
|
|
break;
|
1994-12-13 15:56:51 +00:00
|
|
|
case A_CLRA:
|
|
|
|
ctl=IP_ACCT_CLR;
|
|
|
|
int_t=AC;
|
|
|
|
break;
|
1994-11-16 10:18:18 +00:00
|
|
|
case A_FLUSH:
|
1995-05-30 06:12:45 +00:00
|
|
|
flush(++av);
|
1994-11-16 10:18:18 +00:00
|
|
|
exit(0); /* successful exit */
|
|
|
|
case A_LIST:
|
1995-05-30 06:12:45 +00:00
|
|
|
list(++av);
|
1994-11-16 10:18:18 +00:00
|
|
|
exit(0); /* successful exit */
|
|
|
|
case A_ZERO:
|
1995-05-30 06:12:45 +00:00
|
|
|
zero();
|
1994-11-16 10:18:18 +00:00
|
|
|
exit(0); /* successful exit */
|
|
|
|
case A_POLICY:
|
|
|
|
policy(++av);
|
|
|
|
exit(0); /* we never get here */
|
|
|
|
default:
|
1995-03-03 12:28:34 +00:00
|
|
|
int_t=(AC|FW);
|
|
|
|
int_notdef=1;
|
1994-11-16 10:18:18 +00:00
|
|
|
} /* main action switch */
|
|
|
|
|
|
|
|
if (is_check)
|
|
|
|
goto proto_switch;
|
1995-05-30 06:12:45 +00:00
|
|
|
|
1995-03-03 12:28:34 +00:00
|
|
|
if (!int_notdef)
|
|
|
|
if (*(++av)==NULL) {
|
|
|
|
show_usage(NULL);
|
1994-11-16 10:18:18 +00:00
|
|
|
exit(1);
|
1995-03-03 12:28:34 +00:00
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
switch(get_num(*av,type_tab)) {
|
1994-11-28 12:34:37 +00:00
|
|
|
case T_LREJECT:
|
|
|
|
flags|=IP_FW_F_PRN;
|
|
|
|
case T_REJECT:
|
|
|
|
flags|=IP_FW_F_ICMPRPL;
|
1995-03-03 12:28:34 +00:00
|
|
|
if (!int_t&FW) {
|
|
|
|
show_usage(NULL);
|
1994-11-28 12:34:37 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1995-03-03 12:28:34 +00:00
|
|
|
int_t=FW;
|
1994-11-28 12:34:37 +00:00
|
|
|
break;
|
|
|
|
case T_LDENY:
|
|
|
|
flags|=IP_FW_F_PRN;
|
1994-11-16 10:18:18 +00:00
|
|
|
case T_DENY:
|
|
|
|
flags|=0; /* just to show it related to flags */
|
1995-03-03 12:28:34 +00:00
|
|
|
if (!int_t&FW) {
|
|
|
|
show_usage(NULL);
|
1994-11-16 10:18:18 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1995-03-03 12:28:34 +00:00
|
|
|
int_t=FW;
|
1994-11-16 10:18:18 +00:00
|
|
|
break;
|
1994-11-28 12:34:37 +00:00
|
|
|
case T_LOG:
|
|
|
|
flags|=IP_FW_F_PRN;
|
1994-11-16 10:18:18 +00:00
|
|
|
case T_ACCEPT:
|
1995-03-03 12:28:34 +00:00
|
|
|
case T_PASS:
|
1994-11-16 10:18:18 +00:00
|
|
|
flags|=IP_FW_F_ACCEPT;
|
1995-03-03 12:28:34 +00:00
|
|
|
if (!int_t&FW) {
|
|
|
|
show_usage(NULL);
|
1994-11-16 10:18:18 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1995-03-03 12:28:34 +00:00
|
|
|
int_t=FW;
|
1994-11-16 10:18:18 +00:00
|
|
|
break;
|
|
|
|
case T_SINGLE:
|
|
|
|
flags|=0; /* just to show it related to flags */
|
1995-03-03 12:28:34 +00:00
|
|
|
if (!int_t&AC) {
|
|
|
|
show_usage(NULL);
|
1994-11-16 10:18:18 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1995-03-03 12:28:34 +00:00
|
|
|
int_t=AC;
|
1994-11-16 10:18:18 +00:00
|
|
|
break;
|
|
|
|
case T_BIDIR:
|
|
|
|
flags|=IP_FW_F_BIDIR;
|
1995-03-03 12:28:34 +00:00
|
|
|
if (!int_t&AC) {
|
|
|
|
show_usage(NULL);
|
1994-11-16 10:18:18 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1995-03-03 12:28:34 +00:00
|
|
|
int_t=AC;
|
1994-11-16 10:18:18 +00:00
|
|
|
break;
|
1994-11-28 12:34:37 +00:00
|
|
|
default:
|
1995-03-03 12:28:34 +00:00
|
|
|
show_usage(NULL);
|
1994-11-28 12:34:37 +00:00
|
|
|
exit(1);
|
1994-11-16 10:18:18 +00:00
|
|
|
|
|
|
|
} /* type of switch */
|
|
|
|
|
1995-03-03 12:28:34 +00:00
|
|
|
if (int_notdef) {
|
|
|
|
if (int_t==FW)
|
|
|
|
ctl=IP_FW_ADD;
|
|
|
|
if (int_t==AC)
|
|
|
|
ctl=IP_ACCT_ADD;
|
|
|
|
}
|
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
proto_switch:
|
|
|
|
|
|
|
|
if (*(++av)==NULL) {
|
1995-03-03 12:28:34 +00:00
|
|
|
show_usage(NULL);
|
1994-11-16 10:18:18 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
switch(get_num(*av,proto_tab)) {
|
|
|
|
case P_ALL:
|
|
|
|
flags|=IP_FW_F_ALL;
|
|
|
|
break;
|
|
|
|
case P_ICMP:
|
|
|
|
flags|=IP_FW_F_ICMP;
|
|
|
|
break;
|
1994-11-28 12:34:37 +00:00
|
|
|
case P_SYN:
|
|
|
|
flags|=IP_FW_F_TCPSYN;
|
1994-11-16 10:18:18 +00:00
|
|
|
case P_TCP:
|
|
|
|
flags|=IP_FW_F_TCP;
|
|
|
|
ports_ok=1;
|
|
|
|
strcpy(proto_name,"tcp");
|
|
|
|
break;
|
|
|
|
case P_UDP:
|
|
|
|
flags|=IP_FW_F_UDP;
|
|
|
|
ports_ok=1;
|
|
|
|
strcpy(proto_name,"udp");
|
|
|
|
break;
|
|
|
|
default:
|
1995-03-03 12:28:34 +00:00
|
|
|
show_usage(NULL);
|
1994-11-16 10:18:18 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
if (*(++av)==NULL) {
|
1995-03-03 12:28:34 +00:00
|
|
|
show_usage(NULL);
|
1994-11-16 10:18:18 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1995-05-30 06:12:45 +00:00
|
|
|
set_entry(av,&frwl);
|
1994-12-13 15:56:51 +00:00
|
|
|
frwl.fw_flg=flags;
|
1994-11-16 10:18:18 +00:00
|
|
|
|
|
|
|
if (is_check) {
|
1994-12-13 15:56:51 +00:00
|
|
|
#ifndef disabled
|
|
|
|
fprintf(stderr,"%s: checking disabled.\n",progname);
|
|
|
|
#else
|
1995-05-30 06:12:45 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
struct ip *pkt;
|
|
|
|
struct tcphdr *th;
|
|
|
|
int p_len=sizeof(struct ip)+sizeof(struct tcphdr);
|
|
|
|
|
|
|
|
pkt=(struct ip*)malloc(p_len);
|
|
|
|
pkt->ip_v = IPVERSION;
|
|
|
|
pkt->ip_hl = sizeof(struct ip)/sizeof(int);
|
|
|
|
|
|
|
|
th=(struct tcphdr *)(pkt+1);
|
|
|
|
|
|
|
|
switch(get_num(proto_name,proto_tab)) {
|
|
|
|
case P_TCP:
|
|
|
|
pkt->ip_p = IPPROTO_TCP;
|
|
|
|
break;
|
|
|
|
case P_UDP:
|
|
|
|
pkt->ip_p = IPPROTO_UDP;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr,"%s: can check TCP/UDP packets\
|
|
|
|
only.\n",progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
1994-12-13 15:56:51 +00:00
|
|
|
if (frwl.fw_nsp!=1 || frwl.fw_ndp!=1) {
|
1994-11-16 10:18:18 +00:00
|
|
|
fprintf(stderr,"%s: check needs one src/dst port.\n",
|
|
|
|
progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
1994-12-13 15:56:51 +00:00
|
|
|
if (ntohl(frwl.fw_smsk.s_addr)!=ULONG_MAX ||
|
|
|
|
ntohl(frwl.fw_dmsk.s_addr)!=ULONG_MAX) {
|
1994-11-16 10:18:18 +00:00
|
|
|
fprintf(stderr,"%s: can't check masked IP.\n",progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
1994-12-13 15:56:51 +00:00
|
|
|
pkt->ip_src.s_addr=frwl.fw_src.s_addr;
|
|
|
|
pkt->ip_dst.s_addr=frwl.fw_dst.s_addr;
|
1995-05-30 06:12:45 +00:00
|
|
|
|
1994-12-13 15:56:51 +00:00
|
|
|
th->th_sport=htons(frwl.fw_pts[0]);
|
|
|
|
th->th_dport=htons(frwl.fw_pts[frwl.fw_nsp]);
|
1995-05-30 06:12:45 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
if (setsockopt(s,IPPROTO_IP,ctl,pkt,p_len))
|
|
|
|
printf("Packet DENYED.\n");
|
|
|
|
else
|
|
|
|
printf("Packet ACCEPTED.\n");
|
|
|
|
exit(0);
|
1994-12-13 15:56:51 +00:00
|
|
|
#endif
|
1994-11-16 10:18:18 +00:00
|
|
|
} else {
|
|
|
|
if (setsockopt(s,IPPROTO_IP,ctl,&frwl,sizeof(frwl))<0) {
|
|
|
|
fprintf(stderr,"%s: setsockopt failed.\n",progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
close(s);
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
|
|
|
|