Example #1
0
File: epcap.c Project: kvakvs/epcap
    int
main(int argc, char *argv[])
{
    EPCAP_STATE *ep = NULL;
    pid_t pid = 0;
    int ch = 0;


    IS_NULL(ep = calloc(1, sizeof(EPCAP_STATE)));

    ep->snaplen = SNAPLEN;
    ep->timeout = TIMEOUT;

    while ( (ch = getopt(argc, argv, "d:f:g:hi:MPs:t:u:v")) != -1) {
        switch (ch) {
            case 'd':   /* chroot directory */
                IS_NULL(ep->chroot = strdup(optarg));
                break;
            case 'f':
                IS_NULL(ep->file = strdup(optarg));
                ep->runasuser = 1;
                break;
            case 'g':
                IS_NULL(ep->group = strdup(optarg));
                break;
            case 'i':
                IS_NULL(ep->dev = strdup(optarg));
                break;
            case 'M':
                ep->rfmon = 1;
                break;
            case 'P':
                ep->promisc = 1;
                break;
            case 's':
                ep->snaplen = (size_t)atoi(optarg);
                break;
            case 't':
                ep->timeout = (u_int32_t)atoi(optarg);
                break;
            case 'u':
                IS_NULL(ep->user = strdup(optarg));
                break;
            case 'v':
                ep->verbose++;
                break;
            case 'N':
                ep->no_lookupnet = 1;
                break;
            case 'I':
                ep->filter_in = 1;
                break;
            case 'h':
            default:
                usage(ep);
        }
    }

    argc -= optind;
    argv += optind;

    IS_NULL(ep->filt = strdup( (argc == 1) ? argv[0] : EPCAP_FILTER));

    epcap_priv_issetuid(ep);
    IS_LTZERO(epcap_open(ep));
    if (epcap_priv_drop(ep) < 0)
        exit (1);

    switch (pid = fork()) {
        case -1:
            err(EXIT_FAILURE, "fork");
        case 0:
            (void)close(fileno(stdin));
            IS_LTZERO(epcap_init(ep));
            epcap_loop(ep);
            break;
        default:
            (void)close(fileno(stdout));
            pcap_close(ep->p);
            epcap_watch();
            (void)kill(pid, SIGTERM);

            free(ep->filt);
            free(ep);
            break;
    }

    exit (0);
}
Example #2
0
    int
main(int argc, char *argv[])
{
    EPCAP_STATE *ep = NULL;
    pid_t pid = 0;
    int ch = 0;
    int fd = 0;


    IS_NULL(ep = calloc(1, sizeof(EPCAP_STATE)));

    ep->snaplen = SNAPLEN;
    ep->timeout = TIMEOUT;

    while ( (ch = getopt(argc, argv, "b:d:e:f:g:hi:MPs:t:u:vX")) != -1) {
        switch (ch) {
            case 'b':
                ep->bufsz = atoi(optarg);
                break;
            case 'd':   /* chroot directory */
                IS_NULL(ep->chroot = strdup(optarg));
                break;
            case 'e': {
                char *name = NULL;
                char *value = NULL;
                IS_NULL(name = strdup(optarg));
                IS_NULL(value = strchr(name, '='));
                *value = '\0'; value++;
                IS_FALSE(setenv(name, value, 0));
                free(name);
                }
                break;
            case 'f':
                IS_NULL(ep->file = strdup(optarg));
                ep->opt |= EPCAP_OPT_RUNASUSER;
                break;
            case 'g':
                IS_NULL(ep->group = strdup(optarg));
                break;
            case 'i':
                IS_NULL(ep->dev = strdup(optarg));
                break;
            case 'M':
                ep->opt |= EPCAP_OPT_RFMON;
                break;
            case 'P':
                ep->opt |= EPCAP_OPT_PROMISC;
                break;
            case 's':
                ep->snaplen = (size_t)atoi(optarg);
                break;
            case 't':
                ep->timeout = (u_int32_t)atoi(optarg);
                break;
            case 'u':
                IS_NULL(ep->user = strdup(optarg));
                break;
            case 'v':
                ep->verbose++;
                break;
            case 'X':
                ep->opt |= EPCAP_OPT_INJECT;
                break;
            case 'h':
            default:
                usage(ep);
        }
    }

    argc -= optind;
    argv += optind;

    IS_NULL(ep->filt = strdup( (argc == 1) ? argv[0] : EPCAP_FILTER));

    if (ep->verbose > 0) {
        for ( ; *environ; environ++)
            VERBOSE(2, "env:%s\n", *environ);
    }

    IS_LTZERO(fd = open("/dev/null", O_RDWR));

    epcap_priv_issetuid(ep);
    IS_LTZERO(epcap_open(ep));
    if (epcap_priv_drop(ep) < 0)
        exit (1);

    signal(SIGCHLD, gotsig);

    switch (pid = fork()) {
        case -1:
            err(EXIT_FAILURE, "fork");
        case 0:
            IS_LTZERO(dup2(fd, STDIN_FILENO));
            IS_LTZERO(close(fd));
            IS_LTZERO(epcap_init(ep));
            IS_LTZERO(epcap_priv_rlimits(EPCAP_RLIMIT_NOFILES));
            epcap_loop(ep);
            break;
        default:
            if ( (dup2(fd, STDOUT_FILENO) < 0) ||
                (close(fd) < 0))
                goto CLEANUP;

            if (!(ep->opt & EPCAP_OPT_INJECT))
                pcap_close(ep->p);

            if (epcap_priv_rlimits(0) < 0)
                goto CLEANUP;

            epcap_send(ep);

CLEANUP:
            (void)kill(pid, SIGTERM);
            break;
    }

    exit (0);
}
Example #3
0
    int
main(int argc, char *argv[])
{
    EPCAP_STATE *ep = NULL;
    pid_t pid = 0;
    int ch = 0;
    int fd = 0;


    IS_NULL(ep = calloc(1, sizeof(EPCAP_STATE)));

    ep->snaplen = SNAPLEN;
    ep->timeout = TIMEOUT;

    while ( (ch = getopt(argc, argv, "d:f:g:hi:MPs:t:u:v")) != -1) {
        switch (ch) {
            case 'd':   /* chroot directory */
                IS_NULL(ep->chroot = strdup(optarg));
                break;
            case 'f':
                IS_NULL(ep->file = strdup(optarg));
                ep->runasuser = 1;
                break;
            case 'g':
                IS_NULL(ep->group = strdup(optarg));
                break;
            case 'i':
                IS_NULL(ep->dev = strdup(optarg));
                break;
            case 'M':
                ep->rfmon = 1;
                break;
            case 'P':
                ep->promisc = 1;
                break;
            case 's':
                ep->snaplen = (size_t)atoi(optarg);
                break;
            case 't':
                ep->timeout = (u_int32_t)atoi(optarg);
                break;
            case 'u':
                IS_NULL(ep->user = strdup(optarg));
                break;
            case 'v':
                ep->verbose++;
                break;
            case 'h':
            default:
                usage(ep);
        }
    }

    argc -= optind;
    argv += optind;

    IS_NULL(ep->filt = strdup( (argc == 1) ? argv[0] : EPCAP_FILTER));

    IS_LTZERO(fd = open("/dev/null", O_RDWR));

    epcap_priv_issetuid(ep);
    IS_LTZERO(epcap_open(ep));
    if (epcap_priv_drop(ep) < 0)
        exit (1);

    signal(SIGCHLD, gotsig);

    switch (pid = fork()) {
        case -1:
            err(EXIT_FAILURE, "fork");
        case 0:
            IS_LTZERO(dup2(fd, STDIN_FILENO));
            IS_LTZERO(close(fd));
            IS_LTZERO(epcap_init(ep));
            IS_LTZERO(epcap_priv_rlimits(EPCAP_RLIMIT_NOFILES));
            epcap_loop(ep);
            break;
        default:
            if ( (dup2(fd, STDOUT_FILENO) < 0) ||
                (close(fd) < 0))
                goto CLEANUP;

            pcap_close(ep->p);

            if (epcap_priv_rlimits(0) < 0)
                goto CLEANUP;

            epcap_watch();

CLEANUP:
            (void)kill(pid, SIGTERM);
            break;
    }

    exit (0);
}