/*
 * $Id: rev-traceroute.c,v 1.2 2003/04/25 21:58:30 wessels Exp $
 * 
 * Executes 'traceroute' back to the client's IP address. To be invoked from
 * inetd, stdin/stdout should be a connected socket
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <memory.h>
#include <errno.h>
#include <syslog.h>

int
main(int argc, char *argv[])
{
    struct sockaddr_in peer;
    char ipa[32];
    int len = sizeof(peer);
    openlog(argv[0], LOG_NDELAY, LOG_DAEMON);
    memset(&peer, '\0', sizeof(peer));
    memset(ipa, '\0', 32);
    if (getpeername(1, (struct sockaddr *) & peer, &len) < 0) {
	perror("getpeername");
	exit(0);
    }
    strcpy(ipa, inet_ntoa(peer.sin_addr));
    syslog(LOG_INFO, "Connection from %s", ipa);
    execl("/usr/local/sbin/traceroute", "/usr/local/sbin/traceroute", ipa, NULL);
    execl("/usr/sbin/traceroute", "/usr/sbin/traceroute", ipa, NULL);
    execl("/usr/etc/traceroute", "/usr/etc/traceroute", ipa, NULL);
    syslog(LOG_ERR, "traceroute: %s", strerror(errno));
    return 0;
}
