ipfs.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* Copyright (C) 1999-2001, 2003 by Darren Reed.
*
* See the IPFILTER.LICENCE file for details on licencing.
*
* Copyright 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#ifdef __FreeBSD__
# ifndef __FreeBSD_cc_version
# include <osreldate.h>
# else
# if __FreeBSD_cc_version < 430000
# include <osreldate.h>
# endif
# endif
#endif
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <strings.h>
#endif
#include <stdlib.h>
#include <stddef.h>
#include <netinet/in_systm.h>
#if __FreeBSD_version >= 300000
#endif
#include <netdb.h>
#include <resolv.h>
#include "ipf.h"
#if !defined(lint)
#endif
#ifndef IPF_SAVEDIR
# define IPF_SAVEDIR "/var/db/ipf"
#endif
#ifndef IPF_NATFILE
# define IPF_NATFILE "ipnat.ipf"
#endif
#ifndef IPF_STATEFILE
# define IPF_STATEFILE "ipstate.ipf"
#endif
#endif
extern char *optarg;
extern int optind;
int changestateif __P((char *, char *));
int changenatif __P((char *, char *));
int writestate __P((int, char *));
int opendevice __P((char *));
void closedevice __P((int));
int opts = 0;
char *progname;
void usage()
{
progname);
exit(1);
}
/*
* Change interface names in state information saved out to disk.
*/
{
char *s;
if (!s)
usage();
*s++ = '\0';
usage();
if (fd == -1) {
perror("open");
exit(1);
}
rw = 0;
rw = 1;
}
rw = 1;
}
if (rw == 1) {
perror("lseek");
exit(1);
}
perror("write");
exit(1);
}
}
}
return 0;
}
/*
* Change interface names in NAT information saved out to disk.
*/
{
char *s;
if (!s)
usage();
*s++ = '\0';
usage();
if (fd == -1) {
perror("open");
exit(1);
}
rw = 0;
rw = 1;
}
rw = 1;
}
if (rw == 1) {
perror("lseek");
exit(1);
}
perror("write");
exit(1);
}
}
}
return 0;
}
int argc;
char *argv[];
{
switch (c)
{
case 'd' :
else
usage();
break;
case 'f' :
else
usage();
break;
case 'i' :
set = 1;
break;
case 'l' :
usage();
lock = 1;
set = 1;
break;
case 'n' :
opts |= OPT_DONOTHING;
break;
case 'N' :
usage();
ns = 0;
set = 1;
break;
case 'r' :
usage();
rw = 0;
set = 1;
break;
case 'R' :
rw = 2;
set = 1;
break;
case 'S' :
usage();
ns = 1;
set = 1;
break;
case 'u' :
usage();
lock = 0;
set = 1;
break;
case 'v' :
opts |= OPT_VERBOSE;
break;
case 'w' :
usage();
rw = 1;
set = 1;
break;
case 'W' :
rw = 3;
set = 1;
break;
case '?' :
default :
usage();
}
if (ifs) {
usage();
if (ns == 0)
else
}
if (lock >= 0)
else if (ns >= 0) {
if (ns == 1)
else if (ns == 0)
}
if (devfd == -1)
exit(1);
}
if (lock >= 0)
else if (rw >= 0) {
if (rw & 2)
else {
if (ns == 0)
else if (ns == 1)
}
} else {
if (rw & 2)
else {
if (ns == 0)
else if (ns == 1)
}
}
}
return err;
}
int opendevice(ipfdev)
char *ipfdev;
{
int fd = -1;
if (opts & OPT_DONOTHING)
return -2;
if (!ipfdev)
perror("open device");
return fd;
}
void closedevice(fd)
int fd;
{
}
{
if (opts & OPT_VERBOSE)
if (!(opts & OPT_DONOTHING)) {
perror("SIOCSTLCK");
return 1;
}
if (opts & OPT_VERBOSE)
}
return 0;
}
int fd;
char *file;
{
int wfd = -1;
if (!file)
if (wfd == -1) {
perror("state:open");
return 1;
}
do {
if (opts & OPT_VERBOSE)
break;
perror("state:SIOCSTGET");
return 1;
}
if (opts & OPT_VERBOSE)
perror("state:write");
return 1;
}
return 0;
}
int fd;
char *file;
{
int sfd = -1, i;
if (!file)
if (sfd == -1) {
perror("open");
return 1;
}
/*
* 1. Read all state information in.
*/
do {
if (i == -1) {
perror("read");
return 1;
}
if (i == 0)
break;
if (i != sizeof(ips)) {
(int)sizeof(ips));
return 1;
}
if(!is) {
return 1;
}
/*
* Check to see if this is the first state entry that will
* reference a particular rule and if so, flag it as such
* else just adjust the rule pointer to become a pointer to
* the other. We do this so we have a means later for tracking
* who is referencing us when we get back the real pointer
* in is_rule after doing the ioctl.
*/
break;
else
/*
* Use a tail-queue type list (add things to the end)..
*/
if (!ipshead)
if (ipstail)
} while (1);
if (opts & OPT_VERBOSE)
printf("Loading new state table entry\n");
if (opts & OPT_VERBOSE)
printf("Loading new filter rule\n");
}
if (!(opts & OPT_DONOTHING))
perror("SIOCSTPUT");
return 1;
}
if (opts & OPT_VERBOSE)
}
}
return 0;
}
int fd;
char *file;
{
int nfd, i;
nfd = -1;
if (!file)
file = IPF_NATFILE;
if (nfd == -1) {
perror("nat:open");
return 1;
}
/*
* 1. Read all state information in.
*/
do {
if (i == -1) {
perror("read");
return 1;
}
if (i == 0)
break;
if (i != sizeof(ipn)) {
(int)sizeof(ipn));
return 1;
}
if (!in)
break;
if (i == 0)
break;
if (i != n) {
i, n);
return 1;
}
} else {
}
/*
* Check to see if this is the first state entry that will
* reference a particular rule and if so, flag it as such
* else just adjust the rule pointer to become a pointer to
* the other. We do this so we have a means later for tracking
* who is referencing us when we get back the real pointer
* in is_rule after doing the ioctl.
*/
break;
else
}
/*
* Use a tail-queue type list (add things to the end)..
*/
if (!ipnhead)
if (ipntail)
} while (1);
if (opts & OPT_VERBOSE)
printf("Loading new NAT table entry\n");
if (opts & OPT_VERBOSE)
printf("Loading new filter rule\n");
}
if (!(opts & OPT_DONOTHING))
perror("SIOCSTPUT");
return 1;
}
if (opts & OPT_VERBOSE)
}
}
return 0;
}
int fd;
char *file;
{
int nfd = -1;
if (!file)
file = IPF_NATFILE;
if (nfd == -1) {
perror("nat:open");
return 1;
}
do {
if (opts & OPT_VERBOSE)
perror("nat:SIOCSTGSZ");
return 1;
}
if (opts & OPT_VERBOSE)
break;
if (!ipnp)
else
if (!ipnp) {
break;
}
break;
perror("nat:SIOCSTGET");
return 1;
}
if (opts & OPT_VERBOSE)
perror("nat:write");
return 1;
}
return 0;
}
char *dirname;
{
if (!dirname)
perror("chdir(IPF_SAVEDIR)");
return 1;
}
if (fd == -1)
return 1;
return 1;
}
if (devfd == -1)
goto bad;
goto bad;
if (devfd == -1)
goto bad;
goto bad;
return 1;
}
return 0;
bad:
return 1;
}
char *dirname;
{
if (!dirname)
perror("chdir(IPF_SAVEDIR)");
return 1;
}
if (fd == -1)
return 1;
return 1;
}
if (devfd == -1)
return 1;
return 1;
if (devfd == -1)
return 1;
return 1;
return 1;
}
return 0;
}