/* ipguard.c * * Copyright (c) 2005 SeaD * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ #include "ipguard.h" void help(char *name) { fprintf(stdout, "%s v%s by %s <%s>\n\n", NAME, VERSION, AUTHOR, MAIL); fprintf(stdout, "usage: %s [-felpmunbarxiodvh] \n\n", name); fprintf(stdout, "available options:\n"); fprintf(stdout, " -f | -e ethers file (" ETHERSFILE ")\n"); fprintf(stdout, " -l log file (" LOGNAME "_." LOGEXT ")\n"); fprintf(stdout, " -p pid file (" PIDNAME "_." PIDEXT ")\n"); fprintf(stdout, " -m fake mac (" FAKEMAC ")\n"); fprintf(stdout, " -u update ethers interval (%d)\n", ETHERSTO); fprintf(stdout, " -n fake replies number (%d)\n", FAKENUM); fprintf(stdout, " -b mac/ip buffer size (%d)\n", BUFSIZE); fprintf(stdout, " -a no address substitition\n"); fprintf(stdout, " -r read only\n"); fprintf(stdout, " -x duplex mode\n"); fprintf(stdout, " -i hidden mode\n"); fprintf(stdout, " -o promiscuous mode\n"); fprintf(stdout, " -d don't fork\n"); fprintf(stdout, " -v verbose\n"); fprintf(stdout, " -h this help\n"); } int main(int argc, char *argv[]) { extern char *optarg; extern int optind; int i; if (verbose && (getuid() || geteuid())) fprintf(stderr, "warning: you must have ro|rw permission to /dev/bpf*\n"); srand((unsigned int) getpid()); iface[0] = fmac[0] = log_name[0] = pid_name[0] = '\0'; strncpy(ethers_name, ETHERSFILE, PATH_MAX); strncpy(fmac, FAKEMAC, MAC_SIZE); ethers_update = ETHERSTO; fake_num = FAKENUM; buffer_num = BUFSIZE; addr_nosubst = read_only = hidden = duplex = promisc = dont_fork = verbose = 0; all = good = zmac = zip = bad = bmac = bip = bdip = bnew = bent = mymac = fake = nze = mis = 0; while ((i = getopt(argc, argv, "f:e:l:p:m:u:n:b:arxiodvh")) != EOF) { switch (i) { case 'f': case 'e': if (optarg) strncpy(ethers_name, optarg, PATH_MAX); break; case 'l': if (optarg) strncpy(log_name, optarg, PATH_MAX); break; case 'p': if (optarg) strncpy(pid_name, optarg, PATH_MAX); break; case 'm': if (optarg) strncpy(fmac, optarg, MAC_SIZE); break; case 'u': ethers_update = atoi(optarg); break; case 'n': fake_num = atoi(optarg); break; case 'b': buffer_num = atoi(optarg); break; case 'a': addr_nosubst++; break; case 'r': read_only++; break; case 'x': duplex++; break; case 'i': hidden++; break; case 'o': promisc++; break; case 'd': dont_fork++; break; case 'v': verbose++; break; case 'h': help(argv[0]); exit(EXIT_SUCCESS); default: help(argv[0]); exit(EXIT_FAILURE); } } if (argc > optind) { strncpy(iface, argv[optind], IFNAMSIZ); } else { help(argv[0]); exit(EXIT_FAILURE); } if (!log_name[0]) snprintf(log_name, PATH_MAX, "%s_%s.%s", LOGNAME, iface, LOGEXT); if (!pid_name[0]) snprintf(pid_name, PATH_MAX, "%s_%s.%s", PIDNAME, iface, PIDEXT); log_open(); if (!dont_fork) { if ((i = fork()) == -1) { log_str(ERROR, "fork():", strerror(errno)); exit(EXIT_FAILURE); } else if (i != 0) exit(EXIT_SUCCESS); if (!setsid()) { log_str(ERROR, "setsid():", strerror(errno)); exit(EXIT_FAILURE); } } sig_init(); pid_creat(); pair_init(iface); ethers_init(); packet_init(); if (buffer_num) buffer_init(); while (1) packet_recv(); exit_ipguard(EXIT_SUCCESS); return 0; }