static void automated_send_recv()
{
	int sk;
	char device[18];

	if (fork()) {
		if (!savefile) {
			do_listen(recv_mode);
			return;
		}

		save_fd = open(savefile, O_CREAT | O_WRONLY,
						S_IRUSR | S_IWUSR);
		if (save_fd < 0)
			syslog(LOG_ERR, "Failed to open file to save data");

		do_listen(save_mode);

		close(save_fd);
	} else {
		ba2str(&bdaddr, device);

		sk = do_connect(device);
		if (sk < 0)
			exit(1);
		send_mode(sk);
	}
}
Exemplo n.º 2
0
static void* APR_THREAD_FUNC test_server_run(apr_thread_t *thd, void *data) {

	server_thread_data * server_data = (server_thread_data*)data;
	
	apr_status_t rv;
    apr_pool_t *mp;
    apr_socket_t *s;/* listening socket */

    apr_pool_create(&mp, NULL);

    rv = do_listen(&s, mp, server_data->port);
    if (rv != APR_SUCCESS) {
        char errbuf[256];
	    apr_strerror(rv, errbuf, sizeof(errbuf));
	    printf("test server error listening: %d, %s\n", rv, errbuf);
		apr_pool_destroy(mp);
		apr_thread_exit(thd, rv);
		return;
    }
	server_data->handle->test_server_running = 1;
    while (server_data->handle->test_server_running == 1) {
        apr_socket_t *ns;/* accepted socket */

		//printf("A");
        rv = apr_socket_accept(&ns, s, mp);
		if(rv == 11) {
			//printf(".");
		} else {
			//printf("B");
		    if (rv != APR_SUCCESS) {
		        char errbuf[256];
				apr_strerror(rv, errbuf, sizeof(errbuf));
				printf("test server error accepting: %d, %s\n", rv, errbuf);
				apr_pool_destroy(mp);
				apr_thread_exit(thd, rv);
				return;
		    }
		    /* it is a good idea to specify socket options for the newly accepted socket explicitly */
		    apr_socket_opt_set(ns, APR_SO_NONBLOCK, 0);
		    apr_socket_timeout_set(ns, 5000);

			rv = server_data->request_process_callback(ns, mp, server_data);
			apr_socket_close(ns);
		    if (rv != APR_SUCCESS) {
		        char errbuf[256];
				apr_strerror(rv, errbuf, sizeof(errbuf));
				printf("test server error processing: %d, %s\n", rv, errbuf);
				apr_pool_destroy(mp);
				apr_thread_exit(thd, rv);
				return;
		    }
		}
    }
	//printf("apr_pool_destroy\n");
    apr_pool_destroy(mp);
	//printf("apr_thread_exit\n");
    apr_thread_exit(thd, APR_SUCCESS);
	//printf("return\n");
	return NULL;
}
Exemplo n.º 3
0
/* This runs as the child process. */
static int server_child(int readyfd, struct in_addr addr, int port,
			server_fn callback, void *userdata)
{
    ne_socket *s = ne_sock_create();
    int ret, listener;

    in_child();

    listener = do_listen(addr, port);
    if (listener < 0)
	return FAIL;

#ifdef USE_PIPE
    /* Tell the parent we're ready for the request. */
    if (write(readyfd, "a", 1) != 1) abort();
#endif

    ONN("accept failed", ne_sock_accept(s, listener));

    ret = callback(s, userdata);

    close_socket(s);

    return ret;
}
Exemplo n.º 4
0
int nemo_main() {
    int sock, mode = 0, 
      direction = 0,
      port = getiparam("port"),
      buffer_size = getiparam("bufsize");
    string rhost = NULL;
    bool Qreport = getbparam("report");

    if (hasvalue("host_sender")) {
      mode = CONNECT;
      direction = RECEIVE;
      rhost = getparam("host_sender");
    } else {
      mode = LISTEN;
      direction = SEND;
    }

    switch (mode) {
	case LISTEN:	sock = do_listen(port, rhost);
			break;
	case CONNECT:	sock = do_connect(port, rhost);
			break;
        default:	error("bad mode");
                        break;
    }
    switch (direction) {
        case SEND:	do_transfer(STDIN_FILENO, sock, buffer_size, FALSE, port);
			break;
        case RECEIVE:	do_transfer(sock, STDOUT_FILENO, buffer_size, Qreport, port);
			break;
        default:	error("bad direction");
			break;
    }
    return 0;
}
Exemplo n.º 5
0
int				start_listen(int p)
{
  t_listen			v;

  v.port = p;
  if ((init_socket(&v))
      || do_bind(&v)
      || do_listen(&v))
    return (SOCKET_ERROR);
  return (v.socket_fd);
}
Exemplo n.º 6
0
void serve_forever()
{
	bool ret;
	ret = init();
	if(!ret){
		msg("error in serve_forever");
		return;
	}
	
	do_listen();
	shutdown();
}
Exemplo n.º 7
0
int 
socket_server_listen(struct socket_server *ss, uintptr_t opaque, const char * addr, int port, int backlog) {
	int fd = do_listen(addr, port, backlog);
	if (fd < 0) {
		return -1;
	}
	struct request_package request;
	int id = reserve_id(ss);
	request.u.listen.opaque = opaque;
	request.u.listen.id = id;
	request.u.listen.fd = fd;
	send_request(ss, &request, 'L', sizeof(request.u.listen));
	return id;
}
Exemplo n.º 8
0
/*
 * Simple forking HTTP proxy. It is an HTTP/1.0 proxy with knowledge of
 * HTTP/1.1. (The things lacking for HTTP/1.1 are the chunked transfer encoding
 * and the expect mechanism.) The proxy supports the CONNECT, GET, HEAD, and
 * POST methods. It supports Basic and Digest authentication of clients (use the
 * --proxy-auth option).
 *
 * HTTP/1.1 is defined in RFC 2616. Many comments refer to that document.
 * http://tools.ietf.org/html/rfc2616
 *
 * HTTP authentication is discussed in RFC 2617.
 * http://tools.ietf.org/html/rfc2617
 *
 * The CONNECT method is documented in an Internet draft and is specified as the
 * way to proxy HTTPS in RFC 2817, section 5.
 * http://tools.ietf.org/html/draft-luotonen-web-proxy-tunneling-01
 * http://tools.ietf.org/html/rfc2817#section-5
 *
 * The CONNECT method is not limited to HTTP, but is potentially capable of
 * connecting to any TCP port on any host. The proxy connection is requested
 * with an HTTP request, but after that, the proxy does no interpretation of the
 * data passing through it. See section 6 of the above mentioned draft for the
 * security implications.
 */
int ncat_http_server(void)
{
    int c, s;
    socklen_t sslen;
    union sockaddr_u conn;

#ifndef WIN32
    Signal(SIGCHLD, proxyreaper);
#endif

#if HAVE_HTTP_DIGEST
    http_digest_init_secret();
#endif

#ifdef HAVE_OPENSSL
    if (o.ssl)
        setup_ssl_listen();
#endif

    s = do_listen(SOCK_STREAM, IPPROTO_TCP);

    for (;;) {
        sslen = sizeof(conn.storage);

        c = accept(s, &conn.sockaddr, &sslen);

        if (c == -1) {
            if (errno == EINTR)
                continue;
            die("accept");
        }

        if (!allow_access(&conn)) {
            Close(c);
            continue;
        }

        fork_handler(s, c);
    }

    return 0;
}
Exemplo n.º 9
0
Arquivo: icqprx.c Projeto: tadu/climm
int main (int argc, char **argv)
{
    int fd_cli = -1, fd_lis = -1, fd_srv = -1;
    int port = 5190;

    fd_lis = do_listen (&port);
    while (1)
    {
        printf ("Waiting for connections... (login)\n");
        fd_cli = do_accept (fd_lis);
        fd_srv = do_server ("login.icq.com", 5190);
        do_exchange (fd_cli, fd_srv, port, argc < 2);
        printf ("Waiting for connections... (session)\n");
        fd_cli = do_accept (fd_lis);
        fd_srv = do_server (use_host, use_port);
        do_exchange (fd_cli, fd_srv, port, argc < 2);
        fflush (stdout);
    }
    return 0;
}
Exemplo n.º 10
0
int socket_server::server_listen(uintptr_t opaque, const char *addr, int port, int backlog)
{
    int fd = do_listen(addr, port, backlog);
    if (fd < 0) {
        printf("do listen failed\n");
        return -1;
    }
    printf("run listen success!\n");

    struct request_package request;
    int id = reserve_id();
    if (id < 0) {
        close(fd);
        return id;
    }
    request.u.listen.opaque = opaque;
    request.u.listen.id = id;
    request.u.listen.fd = fd;
    send_request(&request, 'L', sizeof(request.u.listen));
    return id;
}
Exemplo n.º 11
0
static int ncat_listen_stream(int proto)
{
    int rc, i, fds_ready;
    fd_set listen_fds;
    struct timeval tv;
    struct timeval *tvp = NULL;
    unsigned int num_sockets;

    /* clear out structs */
    FD_ZERO(&master_readfds);
    FD_ZERO(&master_writefds);
    FD_ZERO(&master_broadcastfds);
    FD_ZERO(&listen_fds);
#ifdef HAVE_OPENSSL
    FD_ZERO(&sslpending_fds);
#endif
    zmem(&client_fdlist, sizeof(client_fdlist));
    zmem(&broadcast_fdlist, sizeof(broadcast_fdlist));

#ifdef WIN32
    set_pseudo_sigchld_handler(decrease_conn_count);
#else
    /* Reap on SIGCHLD */
    Signal(SIGCHLD, sigchld_handler);
    /* Ignore the SIGPIPE that occurs when a client disconnects suddenly and we
       send data to it before noticing. */
    Signal(SIGPIPE, SIG_IGN);
#endif

#ifdef HAVE_OPENSSL
    if (o.ssl)
        setup_ssl_listen();
#endif

/* Not sure if this problem exists on Windows, but fcntl and /dev/null don't */
#ifndef WIN32
    /* Check whether stdin is closed. Because we treat this fd specially, we
     * can't risk it being reopened for an incoming connection, so we'll hold
     * it open instead. */
    if (fcntl(STDIN_FILENO, F_GETFD) == -1 && errno == EBADF) {
      logdebug("stdin is closed, attempting to reserve STDIN_FILENO\n");
      rc = open("/dev/null", O_RDONLY);
      if (rc >= 0 && rc != STDIN_FILENO) {
        /* Oh well, we tried */
        logdebug("Couldn't reserve STDIN_FILENO\n");
        close(rc);
      }
    }
#endif

    /* We need a list of fds to keep current fdmax. The second parameter is a
       number added to the supplied connection limit, that will compensate
       maxfds for the added by default listen and stdin sockets. */
    init_fdlist(&client_fdlist, sadd(o.conn_limit, num_listenaddrs + 1));

    for (i = 0; i < NUM_LISTEN_ADDRS; i++)
        listen_socket[i] = -1;

    num_sockets = 0;
    for (i = 0; i < num_listenaddrs; i++) {
        /* setup the main listening socket */
        listen_socket[num_sockets] = do_listen(SOCK_STREAM, proto, &listenaddrs[i]);
        if (listen_socket[num_sockets] == -1) {
            if (o.debug > 0)
                logdebug("do_listen(\"%s\"): %s\n", inet_ntop_ez(&listenaddrs[i].storage, sizeof(listenaddrs[i].storage)), socket_strerror(socket_errno()));
            continue;
        }

        /* Make our listening socket non-blocking because there are timing issues
         * which could cause us to block on accept() even though select() says it's
         * readable.  See UNPv1 2nd ed, p422 for more.
         */
        unblock_socket(listen_socket[num_sockets]);

        /* setup select sets and max fd */
        FD_SET(listen_socket[num_sockets], &master_readfds);
        add_fd(&client_fdlist, listen_socket[num_sockets]);

        FD_SET(listen_socket[num_sockets], &listen_fds);

        num_sockets++;
    }
    if (num_sockets == 0) {
        if (num_listenaddrs == 1)
            bye("Unable to open listening socket on %s: %s", inet_ntop_ez(&listenaddrs[0].storage, sizeof(listenaddrs[0].storage)), socket_strerror(socket_errno()));
        else
            bye("Unable to open any listening sockets.");
    }

    add_fd(&client_fdlist, STDIN_FILENO);

    init_fdlist(&broadcast_fdlist, o.conn_limit);

    if (o.idletimeout > 0)
        tvp = &tv;

    while (1) {
        /* We pass these temporary descriptor sets to fselect, since fselect
           modifies the sets it receives. */
        fd_set readfds = master_readfds, writefds = master_writefds;
        struct fdinfo *fdi = NULL;

        if (o.debug > 1)
            logdebug("selecting, fdmax %d\n", client_fdlist.fdmax);

        if (o.debug > 1 && o.broker)
            logdebug("Broker connection count is %d\n", get_conn_count());

        if (o.idletimeout > 0)
            ms_to_timeval(tvp, o.idletimeout);

        fds_ready = fselect(client_fdlist.fdmax + 1, &readfds, &writefds, NULL, tvp);

        if (o.debug > 1)
            logdebug("select returned %d fds ready\n", fds_ready);

        if (fds_ready == 0)
            bye("Idle timeout expired (%d ms).", o.idletimeout);

        /*
         * FIXME: optimize this loop to look only at the fds in the fd list,
         * doing it this way means that if you have one descriptor that is very
         * large, say 500, and none close to it, that you'll loop many times for
         * nothing.
         */
        for (i = 0; i <= client_fdlist.fdmax && fds_ready > 0; i++) {
            /* Loop through descriptors until there's something to read */
            if (!FD_ISSET(i, &readfds) && !FD_ISSET(i, &writefds))
                continue;

            if (o.debug > 1)
                logdebug("fd %d is ready\n", i);

#ifdef HAVE_OPENSSL
            /* Is this an ssl socket pending a handshake? If so handle it. */
            if (o.ssl && FD_ISSET(i, &sslpending_fds)) {
                FD_CLR(i, &master_readfds);
                FD_CLR(i, &master_writefds);
                fdi = get_fdinfo(&client_fdlist, i);
                ncat_assert(fdi != NULL);
                switch (ssl_handshake(fdi)) {
                case NCAT_SSL_HANDSHAKE_COMPLETED:
                    /* Clear from sslpending_fds once ssl is established */
                    FD_CLR(i, &sslpending_fds);
                    post_handle_connection(*fdi);
                    break;
                case NCAT_SSL_HANDSHAKE_PENDING_WRITE:
                    FD_SET(i, &master_writefds);
                    break;
                case NCAT_SSL_HANDSHAKE_PENDING_READ:
                    FD_SET(i, &master_readfds);
                    break;
                case NCAT_SSL_HANDSHAKE_FAILED:
                default:
                    SSL_free(fdi->ssl);
                    Close(fdi->fd);
                    FD_CLR(i, &sslpending_fds);
                    FD_CLR(i, &master_readfds);
                    rm_fd(&client_fdlist, i);
                    /* Are we in single listening mode(without -k)? If so
                       then we should quit also. */
                    if (!o.keepopen && !o.broker)
                        return 1;
                    --conn_inc;
                    break;
                }
            } else
#endif
            if (FD_ISSET(i, &listen_fds)) {
                /* we have a new connection request */
                handle_connection(i);
            } else if (i == STDIN_FILENO) {
                if (o.broker) {
                    read_and_broadcast(i);
                } else {
                    /* Read from stdin and write to all clients. */
                    rc = read_stdin();
                    if (rc == 0) {
                        if (o.proto != IPPROTO_TCP || (o.proto == IPPROTO_TCP && o.sendonly)) {
                            /* There will be nothing more to send. If we're not
                               receiving anything, we can quit here. */
                            return 0;
                        }
                        if (!o.noshutdown) shutdown_sockets(SHUT_WR);
                    }
                    if (rc < 0)
                        return 1;
                }
            } else if (!o.sendonly) {
                if (o.broker) {
                    read_and_broadcast(i);
                } else {
                    /* Read from a client and write to stdout. */
                    rc = read_socket(i);
                    if (rc <= 0 && !o.keepopen)
                        return rc == 0 ? 0 : 1;
                }
            }

            fds_ready--;
        }
    }

    return 0;
}
int main(int argc ,char *argv[])
{
	struct sigaction sa;
	int opt, sk, mode = RECV;

	while ((opt=getopt(argc,argv,"rdscmnb:p:")) != EOF) {
		switch(opt) {
		case 'r':
			mode = RECV;
			break;

		case 's':
			mode = SEND;
			break;

		case 'd':
			mode = DUMP;
			break;

		case 'c':
			mode = RECONNECT;
			break;

		case 'm':
			mode = MULTY;
			break;

		case 'n':
			mode = CONNECT;
			break;

		case 'b':
			data_size = atoi(optarg);
			break;

		case 'p':
			if (sscanf(optarg, "0x%4hx", &pkt_type) != 1) {
				usage();
				exit(1);
			}
			break;

		default:
			usage();
			exit(1);
		}
	}

	if (!(argc - optind) && (mode != RECV && mode != DUMP)) {
		usage();
		exit(1);
	}

	if (!(buf = malloc(data_size))) {
		perror("Can't allocate data buffer");
		exit(1);
	}

	memset(&sa, 0, sizeof(sa));
	sa.sa_handler = SIG_IGN;
	sa.sa_flags   = SA_NOCLDSTOP;
	sigaction(SIGCHLD, &sa, NULL);

	openlog("scotest", LOG_PERROR | LOG_PID, LOG_LOCAL0);

	switch( mode ){
		case RECV:
			do_listen(recv_mode);
			break;

		case DUMP:
			do_listen(dump_mode);
			break;

		case SEND:
			send_mode(argv[optind]);
			break;

		case RECONNECT:
			reconnect_mode(argv[optind]);
			break;

		case MULTY:
			multy_connect_mode(argv[optind]);
			break;

		case CONNECT:
			sk = do_connect(argv[optind]);
			if (sk < 0)
				exit(1);
			dump_mode(sk);
			break;
	}

	syslog(LOG_INFO, "Exit");

	closelog();

	return 0;
}
Exemplo n.º 13
0
/*
 * Simple forking HTTP proxy. It is an HTTP/1.0 proxy with knowledge of
 * HTTP/1.1. (The things lacking for HTTP/1.1 are the chunked transfer encoding
 * and the expect mechanism.) The proxy supports the CONNECT, GET, HEAD, and
 * POST methods. It supports Basic and Digest authentication of clients (use the
 * --proxy-auth option).
 *
 * HTTP/1.1 is defined in RFC 2616. Many comments refer to that document.
 * http://tools.ietf.org/html/rfc2616
 *
 * HTTP authentication is discussed in RFC 2617.
 * http://tools.ietf.org/html/rfc2617
 *
 * The CONNECT method is documented in an Internet draft and is specified as the
 * way to proxy HTTPS in RFC 2817, section 5.
 * http://tools.ietf.org/html/draft-luotonen-web-proxy-tunneling-01
 * http://tools.ietf.org/html/rfc2817#section-5
 *
 * The CONNECT method is not limited to HTTP, but is potentially capable of
 * connecting to any TCP port on any host. The proxy connection is requested
 * with an HTTP request, but after that, the proxy does no interpretation of the
 * data passing through it. See section 6 of the above mentioned draft for the
 * security implications.
 */
int ncat_http_server(void)
{
    int c, i, j;
    int listen_socket[NUM_LISTEN_ADDRS];
    socklen_t sslen;
    union sockaddr_u conn;
    struct timeval tv;
    struct timeval *tvp = NULL;

#ifndef WIN32
    Signal(SIGCHLD, proxyreaper);
#endif

#if HAVE_HTTP_DIGEST
    http_digest_init_secret();
#endif

#ifdef HAVE_OPENSSL
    if (o.ssl)
        setup_ssl_listen();
#endif
    /* Clear the socket list */
    for (i = 0; i < NUM_LISTEN_ADDRS; i++)
        listen_socket[i] = -1;

    /* set for selecting listening sockets */
    fd_set listen_fds;
    fd_list_t listen_fdlist;
    FD_ZERO(&listen_fds);
    init_fdlist(&listen_fdlist, num_listenaddrs);

    /* Listen on each address, set up lists for select */
    for (i = 0; i < num_listenaddrs; i++) {
        listen_socket[i] = do_listen(SOCK_STREAM, IPPROTO_TCP, &listenaddrs[i]);

        /* make us not block on accepts in wierd cases. See ncat_listen.c:209 */
        unblock_socket(listen_socket[i]);

        /* setup select sets and max fd */
        FD_SET(listen_socket[i], &listen_fds);
        add_fd(&listen_fdlist, listen_socket[i]);

    }

    if (o.idletimeout > 0)
        tvp = &tv;

    for (;;) {
        fd_set read_fds;

        sslen = sizeof(conn.storage);
        /*
         * We just select to get a list of sockets which we can talk to
         */
        if (o.debug > 1)
            logdebug("selecting, fdmax %d\n", listen_fdlist.fdmax);
        read_fds = listen_fds;

        if (o.idletimeout > 0)
            ms_to_timeval(tvp, o.idletimeout);

        int fds_ready = fselect(listen_fdlist.fdmax + 1, &read_fds, NULL, NULL, tvp);

        if (o.debug > 1)
            logdebug("select returned %d fds ready\n", fds_ready);

        if (fds_ready == 0)
            bye("Idle timeout expired (%d ms).", o.idletimeout);

        for (i = 0; i <= listen_fdlist.fdmax && fds_ready > 0; i++) {
            /* Loop through descriptors until there is something ready */
            if (!FD_ISSET(i, &read_fds))
                continue;

            /* Check each listening socket */
            for (j = 0; j < num_listenaddrs; j++) {
                if (i == listen_socket[j]) {
                    fds_ready--;
                    c = accept(i, &conn.sockaddr, &sslen);

                    if (c == -1) {
                        if (errno == EINTR)
                            continue;
                        die("accept");
                    }

                    if (!allow_access(&conn)) {
                        Close(c);
                        continue;
                    }
                    if (o.debug > 1)
                        logdebug("forking handler for %d\n", i);
                    fork_handler(i, c);
                }
            }
        }
    }
    return 0;
}
Exemplo n.º 14
0
int spawn_server_repeat(int port, server_fn fn, void *userdata, int n)
{
    int fds[2];

#ifdef USE_PIPE
    if (pipe(fds)) {
	perror("spawn_server: pipe");
	return FAIL;
    }
#else
    /* avoid using uninitialized variable. */
    fds[0] = fds[1] = 0;
#endif

    child = fork();

    if (child == 0) {
	/* this is the child. */
	int listener, count = 0;

	in_child();

	listener = do_listen(lh_addr, port);

#ifdef USE_PIPE
	if (write(fds[1], "Z", 1) != 1) abort();
#endif

	close(fds[1]);
	close(fds[0]);

	/* Loop serving requests. */
	while (++count < n) {
	    ne_socket *sock = ne_sock_create();
	    int ret;

	    NE_DEBUG(NE_DBG_HTTP, "child awaiting connection #%d.\n", count);
	    ONN("accept failed", ne_sock_accept(sock, listener));
	    ret = fn(sock, userdata);
	    close_socket(sock);
	    NE_DEBUG(NE_DBG_HTTP, "child served request, %d.\n", ret);
	    if (ret) {
		printf("server child failed: %s\n", test_context);
		exit(-1);
	    }
	    /* don't send back notification to parent more than
	     * once. */
	}

	NE_DEBUG(NE_DBG_HTTP, "child aborted.\n");
	close(listener);

	exit(-1);

    } else {
	char ch;
	/* this is the parent. wait for the child to get ready */
#ifdef USE_PIPE
	if (read(fds[0], &ch, 1) < 0)
	    perror("parent read");

	close(fds[0]);
	close(fds[1]);
#else
	minisleep();
#endif
    }

    return OK;
}
Exemplo n.º 15
0
/* This is sufficiently different from the TCP code (wrt SSL, etc) that it
 * resides in its own simpler function
 */
static int ncat_listen_dgram(int proto)
{
    int sockfd[NUM_LISTEN_ADDRS];
    int i, fdn = -1;
    int fdmax, nbytes, fds_ready;
    char buf[DEFAULT_UDP_BUF_LEN] = {0};
    char *tempbuf = NULL;
    fd_set read_fds;
    union sockaddr_u remotess;
    socklen_t sslen = sizeof(remotess.storage);

    for (i = 0; i < NUM_LISTEN_ADDRS; i++) {
        sockfd[i] = -1;
    }

    FD_ZERO(&read_fds);

    /* Initialize remotess struct so recvfrom() doesn't hit the fan.. */
    zmem(&remotess.storage, sizeof(remotess.storage));
    remotess.storage.ss_family = o.af;

#ifdef WIN32
    set_pseudo_sigchld_handler(decrease_conn_count);
#else
    /* Reap on SIGCHLD */
    Signal(SIGCHLD, sigchld_handler);
    /* Ignore the SIGPIPE that occurs when a client disconnects suddenly and we
       send data to it before noticing. */
    Signal(SIGPIPE, SIG_IGN);
#endif

    /* set for selecting udp listening sockets */
    fd_set listen_fds;
    fd_list_t listen_fdlist;
    FD_ZERO(&listen_fds);
    init_fdlist(&listen_fdlist, num_listenaddrs);

    for (i = 0; i < num_listenaddrs; i++) {
        /* create the UDP listen sockets */
        sockfd[i] = do_listen(SOCK_DGRAM, proto, &listenaddrs[i]);
        FD_SET(sockfd[i],&listen_fds);
        add_fd(&listen_fdlist, sockfd[i]);
    }

    while (1) {
        int i, j, conn_count, socket_n;

        if (fdn != -1) {
            /*remove socket descriptor which is burnt */
            FD_CLR(sockfd[fdn], &listen_fds);
            rm_fd(&listen_fdlist, sockfd[fdn]);

            /* Rebuild the udp socket which got burnt */
            sockfd[fdn] = do_listen(SOCK_DGRAM, proto, &listenaddrs[fdn]);
            FD_SET(sockfd[fdn],&listen_fds);
            add_fd(&listen_fdlist, sockfd[fdn]);

        }
        fdn = -1;
        socket_n = -1;
            fd_set fds;
            FD_ZERO(&fds);
        while (1) {
            /*
             * We just select to get a list of sockets which we can talk to
             */
            if (o.debug > 1)
                logdebug("selecting, fdmax %d\n", listen_fdlist.fdmax);
            fds = listen_fds;
            fds_ready = fselect(listen_fdlist.fdmax + 1, &fds, NULL, NULL, NULL);

            if (o.debug > 1)
                logdebug("select returned %d fds ready\n", fds_ready);


            /*
             * Figure out which listening socket got a connection. This loop should
             * really call a function for each ready socket instead of breaking on
             * the first one.
             */
            for (i = 0; i <= listen_fdlist.fdmax && fds_ready >0; i++) {
                /* Loop through descriptors until there is something ready */
                if (!FD_ISSET(i, &fds))
                    continue;

                /* Check each listening socket */
                for (j = 0; j < num_listenaddrs; j++) {
                    if (i == sockfd[j]) {
                        if (o.debug >1)
                            logdebug("Valid descriptor %d \n", i);
                        fdn = j;
                        socket_n = i;
                        break;
                    }
                }

                /* if we found a valid socket break */
                if (fdn != -1) {
                    fds_ready--;
                    break;
                }
            }

            /* Make sure someone connected */
            if (fdn == -1)
                continue;

            /*
             * We just peek so we can get the client connection details without
             * removing anything from the queue. Sigh.
             */
            nbytes = Recvfrom(socket_n, buf, sizeof(buf), MSG_PEEK,
                              &remotess.sockaddr, &sslen);

            /* Check conditions that might cause us to deny the connection. */
            conn_count = get_conn_count();
            if (conn_count >= o.conn_limit) {
                if (o.verbose)
                    loguser("New connection denied: connection limit reached (%d)\n", conn_count);
            } else if (!allow_access(&remotess)) {
                if (o.verbose)
                    loguser("New connection denied: not allowed\n");
            } else {
                /* Good to go. */
                break;
            }

            /* Dump the current datagram */
            Recv(socket_n, buf, sizeof(buf), 0);
        }

        if (o.debug > 1)
            logdebug("Valid Connection from %d\n", socket_n);

        conn_inc++;

        /*
         * We're using connected udp. This has the down side of only
         * being able to handle one udp client at a time
         */
        Connect(socket_n, &remotess.sockaddr, sslen);

        /* clean slate for buf */
        zmem(buf, sizeof(buf));

        /* are we executing a command? then do it */
        if (o.cmdexec) {
            struct fdinfo info = { 0 };

            info.fd = socket_n;
            if (o.keepopen)
                netrun(&info, o.cmdexec);
            else
                netexec(&info, o.cmdexec);
            continue;
        }

        FD_SET(socket_n, &read_fds);
        FD_SET(STDIN_FILENO, &read_fds);
        fdmax = socket_n;

        /* stdin -> socket and socket -> stdout */
        while (1) {
            fd_set fds;

            fds = read_fds;

            if (o.debug > 1)
                logdebug("udp select'ing\n");

            fds_ready = fselect(fdmax + 1, &fds, NULL, NULL, NULL);

            if (FD_ISSET(STDIN_FILENO, &fds)) {
                nbytes = Read(STDIN_FILENO, buf, sizeof(buf));
                if (nbytes < 0) {
                    loguser("%s.\n", strerror(errno));
                    return 1;
                } else if (nbytes == 0) {
                    return 0;
                }
                if (o.crlf)
                    fix_line_endings((char *) buf, &nbytes, &tempbuf, &crlf_state);
                if (!o.recvonly) {
                    if (tempbuf != NULL)
                        send(socket_n, tempbuf, nbytes, 0);
                    else
                        send(socket_n, buf, nbytes, 0);
                }
                if (tempbuf != NULL) {
                    free(tempbuf);
                    tempbuf = NULL;
                }
            }
            if (FD_ISSET(socket_n, &fds)) {
                nbytes = recv(socket_n, buf, sizeof(buf), 0);
                if (nbytes < 0) {
                    loguser("%s.\n", socket_strerror(socket_errno()));
                    close(socket_n);
                    return 1;
                }
                if (!o.sendonly)
                    Write(STDOUT_FILENO, buf, nbytes);
            }

            zmem(buf, sizeof(buf));
        }
    }

    return 0;
}
Exemplo n.º 16
0
int main(int argc, char **argv)
{
	short revents;
	int i, listenfd, sockfd;
	int ret = 0;
	struct link *ln;
	struct addrinfo *server_ai = NULL;
	struct addrinfo *local_ai = NULL;
	struct addrinfo hint;

	check_ss_option(argc, argv, "client");

	memset(&hint, 0, sizeof(hint));
	hint.ai_family = AF_UNSPEC;
	hint.ai_socktype = SOCK_STREAM;

	ret = getaddrinfo(ss_opt.server_addr, ss_opt.server_port,
			  &hint, &server_ai);
	if (ret != 0) {
		pr_warn("getaddrinfo error: %s\n", gai_strerror(ret));
		goto out;
	}

	pr_ai_notice(server_ai, "server address");

	ret = getaddrinfo(ss_opt.local_addr, ss_opt.local_port, &hint, &local_ai);
	if (ret != 0) {
		pr_warn("getaddrinfo error: %s\n", gai_strerror(ret));
		goto out;
	}

	pr_ai_notice(local_ai, "listening address");

	if (crypto_init(ss_opt.password, ss_opt.method) == -1) {
		ret = -1;
		goto out;
	}

	ss_init();
	listenfd = do_listen(local_ai, "tcp");
	clients[0].fd = listenfd;
	clients[0].events = POLLIN;

	while (1) {
		pr_debug("start polling\n");
		ret = poll(clients, nfds, TCP_INACTIVE_TIMEOUT * 1000);
		if (ret == -1)
			err_exit("poll error");
		else if (ret == 0) {
			reaper();
			continue;
		}

		if (clients[0].revents & POLLIN) {
			sockfd = accept(clients[0].fd, NULL, NULL);
			if (sockfd == -1) {
				pr_warn("accept error\n");
			} else if (poll_set(sockfd, POLLIN) == -1) {
				close(sockfd);
			} else {
				ln = create_link(sockfd, "client");
				if (ln == NULL) {
					poll_del(sockfd);
					close(sockfd);
				} else {
					ln->server = server_ai;
				}
			}
		}

		for (i = 1; i < nfds; i++) {
			sockfd = clients[i].fd;
			if (sockfd == -1)
				continue;

			revents = clients[i].revents;
			if (revents == 0)
				continue;

			ln = get_link(sockfd);
			if (ln == NULL) {
				sock_warn(sockfd, "close: can't get link");
				close(sockfd);
				continue;
			}
			
			if (revents & POLLIN) {
				client_do_pollin(sockfd, ln);
			}

			if (revents & POLLOUT) {
				client_do_pollout(sockfd, ln);
			}

			/* suppress the noise */
			/* if (revents & POLLPRI) { */
			/* 	sock_warn(sockfd, "POLLPRI"); */
			/* } else if (revents & POLLERR) { */
			/* 	sock_warn(sockfd, "POLLERR"); */
			/* } else if (revents & POLLHUP) { */
			/* 	sock_warn(sockfd, "POLLHUP"); */
			/* } else if (revents & POLLNVAL) { */
			/* 	sock_warn(sockfd, "POLLNVAL"); */
			/* } */
		}

		reaper();
	}

out:
	crypto_exit();

	if (server_ai)
		freeaddrinfo(server_ai);

	if (local_ai)
		freeaddrinfo(local_ai);

	ss_exit();

	if (ret == -1)
		exit(EXIT_FAILURE);
	else
		exit(EXIT_SUCCESS);
}
Exemplo n.º 17
0
/*
 *----------------------------------------------------------------------
 *
 * Main -
 *
 *----------------------------------------------------------------------
 */
int main(int argc, char *argv[])
{
   int ret;
   int childpid;
   int opt;
   FILE *pidf;
   sigset_t mask;

   int listener_fd;
   int client_fd;
   struct sockaddr_storage client_addr;
   socklen_t client_addr_len;

   time_t	cert_mtime = 0;
   edg_wll_GssStatus	gss_stat;
   edg_wll_GssCred	cred = NULL;


   setlinebuf(stdout);
   setlinebuf(stderr);

   /* welcome */
   fprintf(stdout,"\
This is LocalLogger, part of Workload Management System in EU DataGrid & EGEE.\n");

   /* get arguments */
   while ((opt = getopt_long(argc,argv,
	"h"  /* help */
	"V"  /* version */
	"d"  /* debug */
	"p:" /* port */
	"f:" /* file prefix */
	"c:" /* certificate */
	"k:" /* key */
	"C:" /* CA dir */
	"s:" /* socket */
	"i:" /* pidfile */
	"x"  /* noAuth */
	"y"  /* noIPC */
	"z",  /* noParse */
	long_options, (int *) 0)) != EOF) {

	switch (opt) {
		case 'V': fprintf(stdout,"%s:\t%s\n",argv[0],rcsid); exit(0);
		case 'd': debug = 1; break;
		case 'p': port = atoi(optarg); break;
		case 'f': prefix = optarg; break;
		case 'c': cert_file = optarg; break;
		case 'k': key_file = optarg; break;
		case 'C': CAcert_dir = optarg; break;
		case 's': socket_path = optarg; break;
		case 'i': pidfile = optarg; break;
		case 'x': noAuth = 1; break;
		case 'y': noIPC = 1; break;
		case 'z': noParse = 1; break;
		case 'h':
		default:
			usage(argv[0]); exit(0);
	}
   }
   if (glite_common_log_init()) {
	fprintf(stderr,"glite_common_log_init() failed, exiting.");
	exit(1);
   }
   glite_common_log(LOG_CATEGORY_CONTROL,LOG_PRIORITY_INFO,"Initializing...\n");

   /* check noParse */
   if (noParse) {
	glite_common_log(LOG_CATEGORY_CONTROL,LOG_PRIORITY_INFO,"Parse messages for correctness... [no]\n");
   } else {
	glite_common_log(LOG_CATEGORY_CONTROL,LOG_PRIORITY_INFO,"Parse messages for correctness... [yes]\n");
   }

   /* check noIPC */
   if (noIPC) {
	glite_common_log(LOG_CATEGORY_CONTROL,LOG_PRIORITY_INFO,"Send messages also to inter-logger... [no]\n");
   } else {
	glite_common_log(LOG_CATEGORY_CONTROL,LOG_PRIORITY_INFO,"Send messages also to inter-logger... [yes]\n");
   }

   /* check prefix correctness */
   if (strlen(prefix) > FILENAME_MAX - 34) {
	glite_common_log(LOG_CATEGORY_CONTROL,LOG_PRIORITY_FATAL,"Too long prefix (%s) for file names, would not be able to write to log files. Exiting.\n",prefix);
	exit(1);
   }
   /* TODO: check for write permisions */
   glite_common_log(LOG_CATEGORY_CONTROL,LOG_PRIORITY_INFO,"Messages will be stored with the filename prefix \"%s\".\n",prefix);

   if (CAcert_dir)
	setenv("X509_CERT_DIR", CAcert_dir, 1);

#ifdef LB_PERF
   glite_wll_perftest_init(NULL, NULL, NULL, NULL, 0);
#endif
 
   /* daemonize */
   if (debug) {
	glite_common_log(LOG_CATEGORY_CONTROL,LOG_PRIORITY_INFO,"Running as daemon... [no]\n");
   } else {
	glite_common_log(LOG_CATEGORY_CONTROL,LOG_PRIORITY_INFO,"Running as daemon... [yes]\n");
	if (daemon(0,0) < 0) {
		glite_common_log(LOG_CATEGORY_CONTROL,LOG_PRIORITY_FATAL,"Failed to run as daemon. Exiting.\n");
		glite_common_log_SYS_ERROR("daemon");
		exit(1);
	}
   }

   edg_wll_gss_initialize();
   ret = edg_wll_gss_watch_creds(cert_file,&cert_mtime);
   if (ret < 0)
   	glite_common_log(LOG_CATEGORY_SECURITY,LOG_PRIORITY_WARN,"edg_wll_gss_watch_creds failed, unable to access credentials\n");
   /* XXX DK: support noAuth */
   ret = edg_wll_gss_acquire_cred(cert_file, key_file, GSS_C_ACCEPT, &cred, &gss_stat);
   if (ret) {
	/* XXX DK: call edg_wll_gss_get_error() */
	glite_common_log(LOG_CATEGORY_CONTROL,LOG_PRIORITY_FATAL,"Failed to get GSI credentials. Exiting.\n");
	exit(1);
   }

   if (cred->name!=NULL) {
	glite_common_log(LOG_CATEGORY_CONTROL,LOG_PRIORITY_INFO,"Server running with certificate: %s\n",cred->name);
   } else if (noAuth) {
	glite_common_log(LOG_CATEGORY_CONTROL,LOG_PRIORITY_INFO,"Server running without certificate\n");
   }

   /* initialize signal handling */
#if 1
   if(edg_wll_gss_set_signal_handler(SIGUSR1, handle_signal) < 0) { perror("signal"); exit(1); }
   if(edg_wll_gss_set_signal_handler(SIGUSR2, handle_signal) < 0) { perror("signal"); exit(1); }
   if(edg_wll_gss_set_signal_handler(SIGPIPE, handle_signal) < 0) { perror("signal"); exit(1); }
   if(edg_wll_gss_set_signal_handler(SIGHUP, handle_signal) < 0) { perror("signal"); exit(1); }
   if(edg_wll_gss_set_signal_handler(SIGINT, handle_signal) < 0) { perror("signal"); exit(1); }
   if(edg_wll_gss_set_signal_handler(SIGQUIT, handle_signal) < 0) { perror("signal"); exit(1); }
   if(edg_wll_gss_set_signal_handler(SIGTERM, handle_signal) < 0) { perror("signal"); exit(1); }
   if(edg_wll_gss_set_signal_handler(SIGCHLD, handle_signal) < 0) { perror("signal"); exit(1); }
#else
   if (mysignal(SIGUSR1, handle_signal) == SIG_ERR) { perror("signal"); exit(1); }
   if (mysignal(SIGUSR2, handle_signal) == SIG_ERR) { perror("signal"); exit(1); }
   if (mysignal(SIGPIPE, handle_signal) == SIG_ERR) { perror("signal"); exit(1); }
   if (mysignal(SIGHUP,  handle_signal) == SIG_ERR) { perror("signal"); exit(1); }
   if (mysignal(SIGINT,  handle_signal) == SIG_ERR) { perror("signal"); exit(1); }
   if (mysignal(SIGQUIT, handle_signal) == SIG_ERR) { perror("signal"); exit(1); }
   if (mysignal(SIGTERM, handle_signal) == SIG_ERR) { perror("signal"); exit(1); }
   if (mysignal(SIGCHLD, handle_signal) == SIG_ERR) { perror("signal"); exit(1); }

   sigemptyset(&mask);
   sigaddset(&mask, SIGUSR1);
   sigaddset(&mask, SIGUSR2);
   sigaddset(&mask, SIGPIPE);
   sigaddset(&mask, SIGHUP);
   sigaddset(&mask, SIGINT);
   sigaddset(&mask, SIGQUIT);
   sigaddset(&mask, SIGTERM);
   sigaddset(&mask, SIGCHLD);
   sigprocmask(SIG_UNBLOCK, &mask, NULL);
#endif

   /* do listen */
   glite_common_log(LOG_CATEGORY_CONTROL,LOG_PRIORITY_INFO,"Listening on port %d\n",port);
   listener_fd = do_listen(port);
   if (listener_fd == -1) {
	glite_common_log(LOG_CATEGORY_CONTROL,LOG_PRIORITY_FATAL,"Failed to listen on port %d\n",port);
	edg_wll_gss_release_cred(&cred, NULL);
	exit(-1);
   } else {
	glite_common_log(LOG_CATEGORY_CONTROL,LOG_PRIORITY_DEBUG,"Listener's socket descriptor is '%d'\n",listener_fd);
   }

   client_addr_len = sizeof(client_addr);
   bzero((char *) &client_addr, client_addr_len);

/* just try it before deamonizing to be able to complain aloud */
  if (!(pidf = fopen(pidfile,"w"))) {
        perror(pidfile);
        exit(-1);
  }
  fclose(pidf);


  pidf = fopen(pidfile,"w"); assert(pidf); /* XXX */
  fprintf(pidf,"%d\n",getpid());
  fclose(pidf);

  umask(S_IRWXG | S_IRWXO);

   /*
    * Main loop
    */
   while (1) {
        int opt,my_errno;
	fd_set fds;
	struct timeval tv;


	glite_common_log(LOG_CATEGORY_ACCESS,LOG_PRIORITY_INFO,"Accepting incomming connections...\n");
	client_fd = 0;
	while(0 == client_fd) {
		FD_ZERO(&fds);
		FD_SET(listener_fd, &fds);
		tv.tv_sec = 2;
		tv.tv_usec = 0;

		client_fd = select(listener_fd + 1, &fds, NULL, NULL, &tv);
		do_handle_signal();
		if(client_fd < 0) {
			glite_common_log_SYS_ERROR("select");
			client_fd = 0;
		}
	}
	client_fd = accept(listener_fd, (struct sockaddr *) &client_addr,
			&client_addr_len);
	my_errno = errno;
	if (client_fd < 0) {
		if (my_errno == EINTR) continue;
		close(listener_fd);
		glite_common_log(LOG_CATEGORY_ACCESS,LOG_PRIORITY_FATAL,"Failed to accept incomming connections\n");
		glite_common_log_SYS_ERROR("accept");
		edg_wll_gss_release_cred(&cred, NULL);
		exit(-1);
	} else {
		glite_common_log(LOG_CATEGORY_ACCESS,LOG_PRIORITY_DEBUG,"Incomming connection on socket '%d'\n",client_fd);
	}

	opt = 0;
	if (setsockopt(client_fd,IPPROTO_TCP,TCP_CORK,(const void *) &opt,sizeof opt)) {
		glite_common_log(LOG_CATEGORY_ACCESS,LOG_PRIORITY_WARN,"Can't reset TCP_CORK\n");
	}
	opt = 1;
	if (setsockopt(client_fd,IPPROTO_TCP,TCP_NODELAY,(const void *) &opt,sizeof opt)) {
		glite_common_log(LOG_CATEGORY_ACCESS,LOG_PRIORITY_WARN,"Can't set TCP_NODELAY\n");
	}

	switch (edg_wll_gss_watch_creds(cert_file,&cert_mtime)) {
	edg_wll_GssCred newcred;
	case 0: break;
	case 1:
		ret = edg_wll_gss_acquire_cred(cert_file,key_file, GSS_C_ACCEPT, &newcred,&gss_stat);
		if (ret) {
			glite_common_log(LOG_CATEGORY_SECURITY,LOG_PRIORITY_WARN,"Reloading credentials failed, continue with older\n");
		} else {
			glite_common_log(LOG_CATEGORY_SECURITY,LOG_PRIORITY_DEBUG,"Reloading credentials succeeded\n");
			edg_wll_gss_release_cred(&cred, NULL);
			cred = newcred;
		}
		break;
	case -1:
		glite_common_log(LOG_CATEGORY_SECURITY,LOG_PRIORITY_WARN,"edg_wll_gss_watch_creds failed, unable to access credentials\n");
		break;
	}

	/* FORK - change next line if fork() is not needed (for debugging for example) */
#if 1
	if ((childpid = fork()) < 0) {
		glite_common_log_SYS_ERROR("fork");
		if (client_fd) close(client_fd);
	}
	if (childpid == 0) {
		ret = doit(client_fd,cred,prefix,noIPC,noParse);
		if (client_fd) close(client_fd);
		glite_common_log(LOG_CATEGORY_CONTROL,LOG_PRIORITY_DEBUG,"Exiting.\n");
		exit(0);
	}
	if (childpid > 0) {
		glite_common_log(LOG_CATEGORY_CONTROL,LOG_PRIORITY_DEBUG,"Forked a new child with PID %d\n",childpid);
		if (client_fd) close(client_fd);
	}
#else
	ret = doit(client_fd,cred,prefix,noIPC,noParse);
	if (client_fd) close(client_fd);

#endif
    } /* while */

	if (listener_fd) close(listener_fd);
	edg_wll_gss_release_cred(&cred, NULL);
	exit(ret);
}
Exemplo n.º 18
0
static int ncat_listen_stream(int proto)
{
    int rc, i, fds_ready;
    fd_set listen_fds;

    /* clear out structs */
    FD_ZERO(&master_readfds);
    FD_ZERO(&master_writefds);
    FD_ZERO(&master_broadcastfds);
    FD_ZERO(&listen_fds);
#ifdef HAVE_OPENSSL
    FD_ZERO(&sslpending_fds);
#endif
    zmem(&client_fdlist, sizeof(client_fdlist));
    zmem(&broadcast_fdlist, sizeof(broadcast_fdlist));

#ifdef WIN32
    set_pseudo_sigchld_handler(decrease_conn_count);
#else
    /* Reap on SIGCHLD */
    Signal(SIGCHLD, sigchld_handler);
    /* Ignore the SIGPIPE that occurs when a client disconnects suddenly and we
       send data to it before noticing. */
    Signal(SIGPIPE, SIG_IGN);
#endif

#ifdef HAVE_OPENSSL
    if (o.ssl)
        setup_ssl_listen();
#endif

    /* We need a list of fds to keep current fdmax. The second parameter is a
       number added to the supplied connection limit, that will compensate
       maxfds for the added by default listen and stdin sockets. */
    init_fdlist(&client_fdlist, sadd(o.conn_limit, num_listenaddrs + 1));

    for (i = 0; i < NUM_LISTEN_ADDRS; i++)
        listen_socket[i] = -1;

    for (i = 0; i < num_listenaddrs; i++) {
        /* setup the main listening socket */
        listen_socket[i] = do_listen(SOCK_STREAM, proto, &listenaddrs[i]);

        /* Make our listening socket non-blocking because there are timing issues
         * which could cause us to block on accept() even though select() says it's
         * readable.  See UNPv1 2nd ed, p422 for more.
         */
        unblock_socket(listen_socket[i]);

        /* setup select sets and max fd */
        FD_SET(listen_socket[i], &master_readfds);
        add_fd(&client_fdlist, listen_socket[i]);

        FD_SET(listen_socket[i], &listen_fds);
    }
    add_fd(&client_fdlist, STDIN_FILENO);

    init_fdlist(&broadcast_fdlist, o.conn_limit);

    while (1) {
        /* We pass these temporary descriptor sets to fselect, since fselect
           modifies the sets it receives. */
        fd_set readfds = master_readfds, writefds = master_writefds;
        struct fdinfo *fdi = NULL;

        if (o.debug > 1)
            logdebug("selecting, fdmax %d\n", client_fdlist.fdmax);

        if (o.debug > 1 && o.broker)
            logdebug("Broker connection count is %d\n", get_conn_count());

        fds_ready = fselect(client_fdlist.fdmax + 1, &readfds, &writefds, NULL, NULL);

        if (o.debug > 1)
            logdebug("select returned %d fds ready\n", fds_ready);

        /*
         * FIXME: optimize this loop to look only at the fds in the fd list,
         * doing it this way means that if you have one descriptor that is very
         * large, say 500, and none close to it, that you'll loop many times for
         * nothing.
         */
        for (i = 0; i <= client_fdlist.fdmax && fds_ready > 0; i++) {
            /* Loop through descriptors until there's something to read */
            if (!FD_ISSET(i, &readfds) && !FD_ISSET(i, &writefds))
                continue;

            if (o.debug > 1)
                logdebug("fd %d is ready\n", i);

#ifdef HAVE_OPENSSL
            /* Is this an ssl socket pending a handshake? If so handle it. */
            if (o.ssl && FD_ISSET(i, &sslpending_fds)) {
                FD_CLR(i, &master_readfds);
                FD_CLR(i, &master_writefds);
                fdi = get_fdinfo(&client_fdlist, i);
                switch(ssl_handshake(fdi)){
                case NCAT_SSL_HANDSHAKE_COMPLETED:
                    /* Clear from sslpending_fds once ssl is established */
                    FD_CLR(i, &sslpending_fds);
                    rm_fd(&client_fdlist, i);
                    post_handle_connection(*fdi);
                    break;
                case NCAT_SSL_HANDSHAKE_PENDING_WRITE:
                    FD_SET(i, &master_writefds);
                    break;
                case NCAT_SSL_HANDSHAKE_PENDING_READ:
                    FD_SET(i, &master_readfds);
                    break;
                case NCAT_SSL_HANDSHAKE_FAILED:
                default:
                    SSL_free(fdi->ssl);
                    Close(fdi->fd);
                    FD_CLR(i, &sslpending_fds);
                    FD_CLR(i, &master_readfds);
                    rm_fd(&client_fdlist, i);
                    /* Are we in single listening mode(without -k)? If so
                       then we should quit also. */
                    if (!o.keepopen && !o.broker)
                        return 1;
                    --conn_inc;
                    break;
                }
            } else
#endif
            if (FD_ISSET(i, &listen_fds)) {
                /* we have a new connection request */
                handle_connection(i);
            } else if (i == STDIN_FILENO) {
                if(o.broker) {
                    read_and_broadcast(i);
                }else {
                    /* Read from stdin and write to all clients. */
                    rc = read_stdin();
                    if (rc == 0 && o.sendonly)
                        /* There will be nothing more to send. If we're not
                           receiving anything, we can quit here. */
                        return 0;
                    if (rc < 0)
                        return 1;
                }
            } else if (!o.sendonly) {
                if(o.broker) {
                    read_and_broadcast(i);
                }else {
                    /* Read from a client and write to stdout. */
                    rc = read_socket(i);
                    if (rc <= 0 && !o.keepopen)
                        return rc == 0 ? 0 : 1;
                }
            }

            fds_ready--;
        }
    }

    return 0;
}
Exemplo n.º 19
0
int main(int argc, char **argv)
{
	char *dst = NULL, *src = NULL;
	struct sigaction sa;
	int mode = NONE;
	int opt;

	while ((opt=getopt_long(argc, argv, main_sopts, main_lopts, NULL)) != -1) {
		switch(opt) {
		case 'l':
			mode = SHOW;
			detach = 0;
			break;

		case 's':
			mode = LISTEN;
			break;

		case 'c':
			mode = CONNECT;
			dst  = strdup(optarg);
			break;

		case 'Q':
			mode = CONNECT;
			if (optarg)
				search_duration = atoi(optarg);
			break;

		case 'k':
			mode = KILL;
			detach = 0;
			dst  = strdup(optarg);
			break;

		case 'K':
			mode = KILL;
			detach = 0;
			break;

		case 'i':
			src = strdup(optarg);
			break;

		case 'r':
			bnep_str2svc(optarg, &role);
			break;

		case 'd':
			bnep_str2svc(optarg, &service);
			break;

		case 'D':
			use_sdp = 0;
			break;

		case 'E':
			encrypt = 1;
			break;

		case 'S':
			secure = 1;
			break;

		case 'M':
			master = 1;
			break;

		case 'e':
			strcpy(netdev, optarg);
			break;

		case 'n':
			detach = 0;
			break;

		case 'p':
			if (optarg)
				persist = atoi(optarg);
			else
				persist = 5;
			break;

		case 'C':
			if (optarg)
				use_cache = atoi(optarg);
			else
				use_cache = 2;
			break;

		case 'P':
			pidfile = strdup(optarg);
			break;

		case 'z':
			cleanup = 1;
			break;

		case 'h':
		default:
			printf(main_help);
			exit(0);
		}
	}

	argc -= optind;
	argv += optind;
	optind = 0;

	if (bnep_init())
		return -1;

	/* Check non daemon modes first */
	switch (mode) {
	case SHOW:
		do_show();
		return 0;

	case KILL:
		do_kill(dst);
		return 0;

	case NONE:
		printf(main_help);
		return 0;
	}

	/* Initialize signals */
	memset(&sa, 0, sizeof(sa));
	sa.sa_flags   = SA_NOCLDSTOP;
	sa.sa_handler = SIG_IGN;
	sigaction(SIGCHLD, &sa, NULL);
	sigaction(SIGPIPE, &sa, NULL);

	sa.sa_handler = sig_hup;
	sigaction(SIGHUP, &sa, NULL);

	sa.sa_handler = sig_term;
	sigaction(SIGTERM, &sa, NULL);
	sigaction(SIGINT,  &sa, NULL);

	if (detach) {
		if (fork()) exit(0);

		/* Direct stdin,stdout,stderr to '/dev/null' */
		{
			int fd = open("/dev/null", O_RDWR);
			dup2(fd, 0); dup2(fd, 1); dup2(fd, 2);
			close(fd);
		}

		setsid();
		chdir("/");
	}

	openlog("pand", LOG_PID | LOG_NDELAY | LOG_PERROR, LOG_DAEMON);
	syslog(LOG_INFO, "Bluetooth PAN daemon version %s", VERSION);

	if (src) {
		src_dev = hci_devid(src);
		if (src_dev < 0 || hci_devba(src_dev, &src_addr) < 0) {
			syslog(LOG_ERR, "Invalid source. %s(%d)", strerror(errno), errno);
			return -1;
		}
	}

	if (pidfile && write_pidfile())
		return -1;

	if (dst) {
		/* Disable cache invalidation */
		use_cache = 0;

		strncpy(cache.dst, dst, sizeof(cache.dst) - 1);
		str2ba(dst, &cache.bdaddr);
		cache.valid = 1;
		free(dst);
	}

	switch (mode) {
	case CONNECT:
		do_connect();
		break;

	case LISTEN:
		do_listen();
		break;
	}

	if (pidfile)
		unlink(pidfile);

	return 0;
}
Exemplo n.º 20
0
Arquivo: cap.c Projeto: epok76/lanmap2
/**
 * interfaces are set up in NetIface[], listen!
 */
static int do_listen(void)
{
    /* all ifaces set up... */
    /* FIXME: how do i deal with one iface going down and the others staying up? */
  unsigned i;
  struct timeval tv;
#ifdef WIN32
  HANDLE handles[IFACE_MAX];
#else
  int fds[IFACE_MAX];
  fd_set rd;
  int fdmax = -1;
#endif
  /* initial setup for select()ing */
  tv.tv_sec = 1;
  tv.tv_usec = 0;
  for (i = 0; i < NetIfaces; i++) {
    struct netiface *n = NetIface + i;
#ifdef WIN32
    handles[i] = pcap_getevent(n->pcap);
#else
    fds[i] = pcap_get_selectable_fd(n->pcap);
    if (fds[i] > fdmax)
      fdmax = fds[i];
#endif
  }
  while (0 == Shutdown) {
    static int CountdownToDoom = 0;
    int sel;
#ifdef WIN32
    sel = WaitForMultipleObjects((DWORD)NetIfaces, handles, TRUE, (long)SelectFreq * 1000);
#else
    FD_ZERO(&rd);
    for (i = 0; i < NetIfaces; i++)
      FD_SET(fds[i], &rd);
    sel = select(fdmax + 1, &rd, NULL, NULL, NULL);
#endif
    switch (sel) {
#ifdef WIN32
    case WAIT_FAILED:
#else
    case -1: /* error */
#endif
      perror("select");
      break;
#ifdef WIN32
    case WAIT_TIMEOUT:
#else
    case 0: /* timeout */
#endif
      fprintf(stderr, "timeout...\n");
      continue;
      break;
    default: /* one or more readable */
      for (i = 0; i < NetIfaces; i++) {
        struct netiface *n = NetIface + i;
        struct pcap_pkthdr *header;
        char *packet;
        int cap;
#ifdef WIN32
        if (WAIT_OBJECT_0 + i != sel) {
#else
        if (!FD_ISSET(fds[i], &rd)) {
#endif
          continue;
        }
        /* ok, there should be a packet waiting for us... */
        cap = pcap_next_ex(n->pcap, &header, (const u_char **)&packet);
        switch (cap) {
        case -1: /* err */
          break;
        case 0: /* timeout */
          break;
        case 1: /* ok */
        { /* case scope */
          static parse_status st;
          st.frames = 0;
          parse(packet, header->len, pcap_datalink(n->pcap), &st);
          traffic(&st);
#if 1
          dump(&st);
#endif


#if 0
          /*
           * Crash if we receive data that we did not parse,
           * with the following exceptions...
           */
          if (st.frame[st.frames-1].id == PROT_UNKNOWN
              /* enumerate all cases where we know we get unknown bytes */
              && !(
                /* ethernet frames are often padded with garbage to meet 60 bytes */
                (PROT_IEEE802_3 == st.frame[1].id && 60 == st.frame[0].len)
                /* junk after LLC... */
                || (PROT_STP == st.frame[st.frames-2].id && 7 == st.frame[st.frames-1].len)
                /* junk after SMB over LLC... */
                || (PROT_SMB == st.frame[st.frames-2].id && PROT_LLC == st.frame[2].len)
                /* junk zeroes after BOOTP */
                || (PROT_BOOTP == st.frame[st.frames-2].id &&
                  allzeroes(st.frame[st.frames-1].off, st.frame[st.frames-1].len))
                /* junk after LLDP... */
                || (PROT_LLDP == st.frame[st.frames-2].id && 2 == st.frame[st.frames-1].len)
                /* mysterious message */
                || (PROT_LLC == st.frame[2].id && 82 == st.frame[0].len)
                /* TCP payload */
                || (PROT_TCP == st.frame[st.frames-2].id)
                /* ARP oftens contains garbage */
                || (PROT_ARP == st.frame[st.frames-2].id)
              )) {
            abort();
          }
#endif


#if 0
          /* 
           * the 'gprof' profiling tool requires a clean stop
           * of the program being profiled in order to work.
           * this causes that
           */
          if (CountdownToDoom++ > 4000)
            Shutdown = 1;
#endif




        }
          break;
        default:
          break;
        }
#ifdef WIN32
        break; /* WaitForMultipleObjects only returns the first available, not all */
#endif
      }
      break;
    } /* switch */
  } /* read loop */
  if (Verbosity >= 2) {
    printf("interfaces");
    for (i = 0; i < NetIfaces; i++)
      printf(" %s", NetIface[i].name);
    printf(" down...\n");
  }
  return 1; 
}

/**
 * main loop
 */
static int listen(void)
{
  static char errbuf[PCAP_ERRBUF_SIZE];
  char *dev;
  unsigned i;
  /* listen loop */
  do { 
    int promisc = 1;
    if (0 == NetIfaces) { /* no devs specified, find one */
      dev = pcap_lookupdev(errbuf);
      if (NULL == dev) {
        fprintf(stderr, "Couldn't find default interface: %s\n", errbuf);
        return 0;
      }
      if (Verbosity >= 1)
        printf("Defaulting to dev '%s'\n", dev);
      netiface_add(dev);
    }
    /* set up each iface */
    for (i = 0; i < NetIfaces; i++) {
      struct netiface *n = NetIface + i;
#ifdef WIN32
      promisc = 1;
#else
      promisc = (0 == geteuid());
#endif
      if (Verbosity >= 1)
        printf("opening dev '%s' in %s mode...\n",
          n->name, promisc ? "promiscuous" : "normal");
      n->pcap = pcap_open_live(n->name, PACKET_BUFLEN, promisc, 0, errbuf);
      if (!n->pcap) {
        fprintf(stderr, "Can't open interface '%s'! %s\n", n->name, errbuf);
        fprintf(stderr, "Have you specified an interface with -i?. See -h for more -i info.\n");
        return 0;
      }
      if (-1 == pcap_lookupnet(n->name, &n->net, &n->mask, errbuf)) {
        fprintf(stderr, "Can't get netmask for interface '%s': %s, continuing...\n",
          n->name, errbuf);
        /* no ip address up?! keep on truckin... */
      }
  
      /* TODO: human-readable output, perhaps? */
      if (Verbosity >= 2)
        printf("interface '%s' net: 0x%08X, mask: 0x%08X\n",
          n->name, n->net, n->mask);
    
      /* set filter if we've been supplied one and we've got an ip addres... */
      printf("Applying Filter_Str=\"%s\"...\n", Filter_Str);
      if (NULL != Filter_Str && 0 != n->net && 0 != n->mask) {
        if (-1 == pcap_compile(n->pcap, &Filter, Filter_Str, 0, n->net)) {
          fprintf(stderr, "Can't compile filter '%s': %s. quitting\n",
            Filter_Str, pcap_geterr(n->pcap));
          return 0;
        }
        if (-1 == pcap_setfilter(n->pcap, &Filter)) {
          fprintf(stderr, "Can't install filter '%s': %s. quitting\n",
            Filter_Str, pcap_geterr(n->pcap));
          return 0;
        }
      }
    }
    do_listen();
  } while (!Shutdown);
  return 0;
}

int main(void)
{
  rep_init(stderr);
  parse_init();
  (void)iface_list();
  listen();
  return 0;
}
Exemplo n.º 21
0
int cs_listen_tcp(char *host, int port, int backlog) {
	return do_listen(host, port, PROTO_TCP, backlog);
}
Exemplo n.º 22
0
/**
 *
 * @param argc
 * @param argv
 * @return
 */
int main(int argc, char** argv) {

    if (0 != getuid()) {
        printf("%s", INSUFF_PRIV);
        exit(EXIT_FAILURE);
    }
    start_time = time(NULL);
    char *host = NULL;
    int opt = 0;
    int longIndex = 0;

    memset(globals.routes, 0, ROUTES_MAX_COUNT);
    //
    curl_global_init(CURL_GLOBAL_ALL);
    //
    watchdog_stop_flag = false;
    sheduler_stop_flag = false;
    pbuffer_stop_flag = false;
    rotate_stop_flag = false;
    listen_stop_flag = false;

    /* Initialize arguments before we get to work. */
    char * argument = NULL;

    argument = "/var/run/arewik.pid";
    arguments.pidfile = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(arguments.pidfile, argument);

    argument = "/var/log/arewik";
    arguments.logdir = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(arguments.logdir, argument);

    argument = "/var/arewik";
    arguments.storagedir = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(arguments.storagedir, argument);

    argument = "/var/arewik/buffers";
    arguments.bufferdir = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(arguments.bufferdir, argument);

    argument = "/etc/arewik/arewik.conf";
    arguments.configfile = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(arguments.configfile, argument);

    argument = "arewik";
    arguments.user = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(arguments.user, argument);

    argument = "arewik";
    arguments.group = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(arguments.group, argument);

    argument = "/tmp";
    arguments.wdir = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(arguments.wdir, argument);

    argument = DEFAULT_LISTEN_IP;
    arguments.listen_host = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(arguments.listen_host, argument);

    arguments.listen_port = DEFAULT_LISTEN_PORT;
    arguments.verbosity = 0;
    arguments.foreground = 0;
    arguments.debuginfo = 0;

    argument = "custom.log";
    globals.custom_logfile_name = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(globals.custom_logfile_name, argument);

    argument = "debug.log";
    globals.debug_logfile_name = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(globals.debug_logfile_name, argument);

    argument = "connections.log";
    globals.connections_logfile_name = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(globals.connections_logfile_name, argument);

    argument = "access.log";
    globals.access_logfile_name = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(globals.access_logfile_name, argument);

    globals.maxcon = 4096; // routes max * 64
    globals.proxymode = false; //todo
    globals.routes_cnt = 1;
    globals.watchdog_interval = WATCHDOG_T;
    globals.ping_interval = 30;
    globals.autoreconfigure = false;
    globals.reconfigure_interval = 60;
    globals.use_resolver = 0;
    globals.use_syslog = 0;
    globals.socktimeout = 10000;
    globals.epolltimeout = EPOLL_RUN_TIMEOUT;
    globals.workers = POSSIBLE_FHNDL;

    sprintf(globals.identline, "%s", ident);
    //
    globals.modules[0].id = 0;
    globals.modules[0].enabled = true;
    globals.modules[0].name = "plain";
    globals.modules[0].process_function = &process_plain;
    globals.modules[0].init_function = NULL;

    globals.modules[1].id = 1;
    globals.modules[1].enabled = false;
    globals.modules[1].name = "riak";
    globals.modules[1].process_function = &process_riak;
    globals.modules[1].init_function = &init_riak_module;
    globals.modules[1].close_function = &close_riak;

    globals.modules[2].id = 2;
    globals.modules[2].enabled = false;
    globals.modules[2].name = "esearch";
    globals.modules[2].process_function = &process_esearch;
    globals.modules[2].init_function = &init_esearch_module;
    globals.modules[2].close_function = &close_esearch;


    globals.modules[3].id = 3;
    globals.modules[3].enabled = false;
    globals.modules[3].name = "webhdfs";
    globals.modules[3].process_function = &process_webhdfs;
    globals.modules[3].init_function = &init_webhdfs_module;
    globals.modules[3].close_function = &close_webhdfs;


    CPRESS_FUNCT[0].type = SNAPPY;
    CPRESS_FUNCT[0].compress_function = &compress_snappy;

    CPRESS_FUNCT[1].type = GZIP;
    CPRESS_FUNCT[1].compress_function = &compress_gzip;


    set_sig_handler();
    static const char *optString = "s:D:B:u:g:p:l:c:H:P:vfhd?";
    opt = getopt_long(argc, argv, optString, longOpts, &longIndex);

    while (opt != -1) {
        switch (opt) {
            case 'p':
                if (!is_valid_opt(optarg)) {
                    print_usage();
                }
                arguments.pidfile = xrealloc(arguments.pidfile, (sizeof (char) * strlen(optarg) + 1));
                strcpy(arguments.pidfile, optarg);

                break;

            case 'l':
                if (!is_valid_opt(optarg)) {
                    print_usage();
                }
                arguments.logdir = xrealloc(arguments.pidfile, (sizeof (char) * strlen(optarg) + 1));
                strcpy(arguments.logdir, optarg);
                break;

            case 'c':
                if (!is_valid_opt(optarg)) {
                    print_usage();
                }
                arguments.configfile = xrealloc(arguments.configfile, (sizeof (char) * strlen(optarg) + 1));
                strcpy(arguments.configfile, optarg);
                break;

            case 'H':
                if (!isValidIP(optarg)) {
                    print_usage();
                }
                if (NULL != (host = nslookup(optarg))) {
                    arguments.listen_host = xrealloc(arguments.listen_host, (sizeof (char) * strlen(optarg) + 1));
                    strcpy(arguments.listen_host, host);
                } else {
                    printf("%s\n", hstrerror(h_errno));
                    exit(EXIT_FAILURE);
                }
                break;
            case 'u':
                if (!is_valid_opt(optarg)) {
                    print_usage();
                }
                arguments.user = xrealloc(arguments.user, (sizeof (char) * strlen(optarg) + 1));
                strcpy(arguments.user, optarg);
                break;
            case 'g':
                arguments.group = xrealloc(arguments.group, (sizeof (char) * strlen(optarg) + 1));
                strcpy(arguments.group, optarg);
                break;
            case 'D':
                if (!is_valid_opt(optarg)) {
                    print_usage();
                }
                arguments.wdir = xrealloc(arguments.wdir, (sizeof (char) * strlen(optarg) + 1));
                strcpy(arguments.wdir, optarg);
                break;
            case 's':
                if (!is_valid_opt(optarg)) {
                    print_usage();
                }
                arguments.storagedir = xrealloc(arguments.storagedir, (sizeof (char) * strlen(optarg) + 1));
                strcpy(arguments.storagedir, optarg);
                break;
            case 'B':
                if (!is_valid_opt(optarg)) {
                    print_usage();
                }
                arguments.bufferdir = xrealloc(arguments.bufferdir, (sizeof (char) * strlen(optarg) + 1));
                strcpy(arguments.bufferdir, optarg);
                break;
            case 'P':
                arguments.listen_port = atoi(optarg);
                break;
            case 'v':
                arguments.verbosity++;
                break;
            case 'f':
                arguments.foreground++;
                break;
            case 'h':
                print_usage();
                break;
            case 'd':
                arguments.debuginfo++;
                arguments.verbosity++;
                break;
            default:
                if (!is_valid_opt(optarg)) {
                    print_usage();
                }
                break;
        }

        opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
    }

    if (false == DirectoryExists(arguments.storagedir)) {
        printf("Specified storage directory '%s' does not exists\n", arguments.storagedir);
        printf("Creating new one\n");
        if (0 > mkdir(arguments.storagedir, 0755)) {
            printf("Can not create storage directory - %s\n", strerror(errno));
            exit(EXIT_FAILURE);
        } else {
            setRightOwner(arguments.storagedir);
        }
    }
    setRightOwner(arguments.storagedir);

    if (false == DirectoryExists(arguments.bufferdir)) {
        printf("Specified buffer directory '%s' does not exists\n", arguments.bufferdir);
        printf("Creating new one\n");
        if (0 > mkdir(arguments.bufferdir, 0755)) {
            printf("Can not create buffer directory - %s\n", strerror(errno));
            exit(EXIT_FAILURE);
        } else {
            setRightOwner(arguments.bufferdir);
        }
    }
    setRightOwner(arguments.bufferdir);

    if (true == DirectoryExists(arguments.pidfile)) {
        printf("Specified pid file '%s' is a directory\n", arguments.pidfile);
        exit(EXIT_FAILURE);
    }

    if (false == DirectoryExists(arguments.wdir)) {
        printf("Specified working directory '%s' does not exists or not a directory\n", arguments.wdir);
        exit(EXIT_FAILURE);
    }

    if (arguments.foreground == 1) {
        printf("Running in foreground mode...\n");
        setbuf(stdout, 0);
        setbuf(stdin, 0);
        setbuf(stderr, 0);
    }

    if (arguments.verbosity == 1) {
        printf("Verbosity enabled\n");
    }

    if (false == FileExists(arguments.configfile)) {
        printf("Specified file '%s' does not exists\n", arguments.configfile);
        exit(EXIT_FAILURE);
    } else if (true == DirectoryExists(arguments.configfile)) {
        printf("Specified file '%s' is a directory\n", arguments.configfile);
        exit(EXIT_FAILURE);
    } else {
        setRightOwner(arguments.configfile);
        if (true != hasRightOwner(arguments.configfile)) {
            printf("The file '%s' has invalid owner/group\nMust be %s:%s\n", arguments.configfile, arguments.user, arguments.group);
            exit(EXIT_FAILURE);
        }
    }

    if (false == DirectoryExists(arguments.logdir)) {
        printf("Specified log directory '%s' does not exists\n", arguments.logdir);
        printf("Creating new one\n");
        if (0 > mkdir(arguments.logdir, 0755)) {
            printf("Can not create log directory - %s\n", strerror(errno));
            exit(EXIT_FAILURE);
        } else {
            setRightOwner(arguments.logdir);
        }
    }
    setRightOwner(arguments.logdir);

    if (true != hasRightOwner(arguments.logdir)) {
        printf("The file '%s' has invalid owner/group\nMust be %s:%s\n", arguments.logdir, arguments.user, arguments.group);
        exit(EXIT_FAILURE);
    }


    glob = readConfig(arguments.configfile);
    checkPid();

    openCustomLog();
    openDebugLog();
    openConLog();
    openAccessLog();

    if (!(pwd = getpwnam(arguments.user))) {
        snprintf(TMP_MSG, MAXLINE, "No such user: %s. Quitting!", arguments.user);
        writeToCustomLog(TMP_MSG);

        exit(EXIT_FAILURE);
    } else my_uid = pwd->pw_uid;

    if (!(grp = getgrnam(arguments.group))) {
        snprintf(TMP_MSG, MAXLINE, "No such group: %s. Quitting!", arguments.group);
        writeToCustomLog(TMP_MSG);
        exit(EXIT_FAILURE);
    } else my_gid = grp->gr_gid;

    if (arguments.foreground == 0) {

        pid = fork();

        if (pid < 0) {
            snprintf(TMP_MSG, MAXLINE, "%s", "Can not fork! [Invalid PID]");
            writeToCustomLog(TMP_MSG);
            removePid();
            exit(EXIT_FAILURE);
        }
        /* If we got a good PID, then
           we can exit the parent process. */
        if (pid > 0) {
            exit(EXIT_SUCCESS);
        }
        //
        snprintf(TMP_MSG, MAXLINE, "%s", "Forked successfully");
        writeToCustomLog(TMP_MSG);
        /* Change the file mode mask */
        umask(027);
        /* Create a new SID for the child process */
        sid = setsid();
        if (sid < 0) {
            snprintf(TMP_MSG, MAXLINE, "%s", "Can not create child process");
            writeToCustomLog(TMP_MSG);
            /* Log the failure */
            removePid();
            exit(EXIT_FAILURE);
        }
        snprintf(TMP_MSG, MAXLINE, "%s", "Daemonizing");
        writeToCustomLog(TMP_MSG);

        /* Change the current working directory */
        if ((chdir(arguments.wdir)) < 0) {
            snprintf(TMP_MSG, MAXLINE, "%s", "Can not change current directory");
            writeToCustomLog(TMP_MSG);
            /* Log the failure */
            exit(EXIT_FAILURE);
        }
        snprintf(TMP_MSG, MAXLINE, "%s", "Working directory changed");
        writeToCustomLog(TMP_MSG);
    }
    savePid();

    if ((setgid(my_gid)) < 0) {
        snprintf(TMP_MSG, MAXLINE, "ERROR: setgid(%d) failed: %s", my_gid, strerror(errno));
        writeToCustomLog(TMP_MSG);
        halt();
    }
    snprintf(TMP_MSG, MAXLINE, "Group ID changed to %d", my_gid);
    writeToCustomLog(TMP_MSG);

    if ((setuid(my_uid)) < 0) {
        snprintf(TMP_MSG, MAXLINE, "ERROR: setuid(%d) failed: %s", my_uid, strerror(errno));
        writeToCustomLog(TMP_MSG);
        halt();
    }
    snprintf(TMP_MSG, MAXLINE, "User ID changed to %d", my_gid);
    writeToCustomLog(TMP_MSG);


    if (arguments.foreground == 0) {
        snprintf(TMP_MSG, MAXLINE, "%s", "Closing descriptors & disabling verbosity.\nEnsure you have set right permissions for your log file.\nWatch your log file for further information\n");
        VERBOSE(TMP_MSG);
        arguments.verbosity = 0;
        /* Close out the standard file descriptors */
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);
    }
    ////////////////////////////////////////////////////////////////////////////////

    //
    init_arrays();

    init_webhdfs_arrays();

    init_active_c_table();
    init_descriptors();
    scan_connections();
    //
    ////////////////////////////////////////////////////////////////////////////////
    spawn_threads();
    do_listen();

    ////////////////////////////////////////////////////////////////////////////////
    /* This never happens */
    return 0;
}
Exemplo n.º 23
0
/* This is sufficiently different from the TCP code (wrt SSL, etc) that it
 * resides in its own simpler function
 */
static int ncat_listen_dgram(int proto)
{
    struct {
        int fd;
        union sockaddr_u addr;
    } sockfd[NUM_LISTEN_ADDRS];
    int i, fdn = -1;
    int fdmax, nbytes, n, fds_ready;
    char buf[DEFAULT_UDP_BUF_LEN] = { 0 };
    char *tempbuf = NULL;
    fd_set read_fds;
    union sockaddr_u remotess;
    socklen_t sslen = sizeof(remotess.storage);
    struct timeval tv;
    struct timeval *tvp = NULL;
    unsigned int num_sockets;

    for (i = 0; i < NUM_LISTEN_ADDRS; i++) {
        sockfd[i].fd = -1;
        sockfd[i].addr.storage.ss_family = AF_UNSPEC;
    }

    FD_ZERO(&read_fds);

    /* Initialize remotess struct so recvfrom() doesn't hit the fan.. */
    zmem(&remotess.storage, sizeof(remotess.storage));
    remotess.storage.ss_family = o.af;

#ifdef WIN32
    set_pseudo_sigchld_handler(decrease_conn_count);
#else
    /* Reap on SIGCHLD */
    Signal(SIGCHLD, sigchld_handler);
    /* Ignore the SIGPIPE that occurs when a client disconnects suddenly and we
       send data to it before noticing. */
    Signal(SIGPIPE, SIG_IGN);
#endif

/* Not sure if this problem exists on Windows, but fcntl and /dev/null don't */
#ifndef WIN32
    /* Check whether stdin is closed. Because we treat this fd specially, we
     * can't risk it being reopened for an incoming connection, so we'll hold
     * it open instead. */
    if (fcntl(STDIN_FILENO, F_GETFD) == -1 && errno == EBADF) {
      logdebug("stdin is closed, attempting to reserve STDIN_FILENO\n");
      i = open("/dev/null", O_RDONLY);
      if (i >= 0 && i != STDIN_FILENO) {
        /* Oh well, we tried */
        logdebug("Couldn't reserve STDIN_FILENO\n");
        close(i);
      }
    }
#endif

    /* set for selecting udp listening sockets */
    fd_set listen_fds;
    fd_list_t listen_fdlist;
    FD_ZERO(&listen_fds);
    init_fdlist(&listen_fdlist, num_listenaddrs);

    num_sockets = 0;
    for (i = 0; i < num_listenaddrs; i++) {
        /* create the UDP listen sockets */
        sockfd[num_sockets].fd = do_listen(SOCK_DGRAM, proto, &listenaddrs[i]);
        if (sockfd[num_sockets].fd == -1) {
            if (o.debug > 0)
                logdebug("do_listen(\"%s\"): %s\n", inet_ntop_ez(&listenaddrs[i].storage, sizeof(listenaddrs[i].storage)), socket_strerror(socket_errno()));
            continue;
        }
        FD_SET(sockfd[num_sockets].fd, &listen_fds);
        add_fd(&listen_fdlist, sockfd[num_sockets].fd);
        sockfd[num_sockets].addr = listenaddrs[i];
        num_sockets++;
    }
    if (num_sockets == 0) {
        if (num_listenaddrs == 1)
            bye("Unable to open listening socket on %s: %s", inet_ntop_ez(&listenaddrs[0].storage, sizeof(listenaddrs[0].storage)), socket_strerror(socket_errno()));
        else
            bye("Unable to open any listening sockets.");
    }

    if (o.idletimeout > 0)
        tvp = &tv;

    while (1) {
        int i, j, conn_count, socket_n;

        if (fdn != -1) {
            /*remove socket descriptor which is burnt */
            FD_CLR(sockfd[fdn].fd, &listen_fds);
            rm_fd(&listen_fdlist, sockfd[fdn].fd);

            /* Rebuild the udp socket which got burnt */
            sockfd[fdn].fd = do_listen(SOCK_DGRAM, proto, &sockfd[fdn].addr);
            if (sockfd[fdn].fd == -1)
                bye("do_listen: %s", socket_strerror(socket_errno()));
            FD_SET(sockfd[fdn].fd, &listen_fds);
            add_fd(&listen_fdlist, sockfd[fdn].fd);

        }
        fdn = -1;
        socket_n = -1;
        fd_set fds;
        FD_ZERO(&fds);
        while (1) {
            /*
             * We just select to get a list of sockets which we can talk to
             */
            if (o.debug > 1)
                logdebug("selecting, fdmax %d\n", listen_fdlist.fdmax);
            fds = listen_fds;

            if (o.idletimeout > 0)
                ms_to_timeval(tvp, o.idletimeout);

            fds_ready = fselect(listen_fdlist.fdmax + 1, &fds, NULL, NULL, tvp);

            if (o.debug > 1)
                logdebug("select returned %d fds ready\n", fds_ready);

            if (fds_ready == 0)
                bye("Idle timeout expired (%d ms).", o.idletimeout);

            /*
             * Figure out which listening socket got a connection. This loop should
             * really call a function for each ready socket instead of breaking on
             * the first one.
             */
            for (i = 0; i <= listen_fdlist.fdmax && fds_ready > 0; i++) {
                /* Loop through descriptors until there is something ready */
                if (!FD_ISSET(i, &fds))
                    continue;

                /* Check each listening socket */
                for (j = 0; j < num_sockets; j++) {
                    if (i == sockfd[j].fd) {
                        if (o.debug > 1)
                            logdebug("Valid descriptor %d \n", i);
                        fdn = j;
                        socket_n = i;
                        break;
                    }
                }

                /* if we found a valid socket break */
                if (fdn != -1) {
                    fds_ready--;
                    break;
                }
            }

            /* Make sure someone connected */
            if (fdn == -1)
                continue;

            /*
             * We just peek so we can get the client connection details without
             * removing anything from the queue. Sigh.
             */
            nbytes = recvfrom(socket_n, buf, sizeof(buf), MSG_PEEK,
                              &remotess.sockaddr, &sslen);
            if (nbytes < 0) {
                loguser("%s.\n", socket_strerror(socket_errno()));
                close(socket_n);
                return 1;
            }

            /* Check conditions that might cause us to deny the connection. */
            conn_count = get_conn_count();
            if (conn_count >= o.conn_limit) {
                if (o.verbose)
                    loguser("New connection denied: connection limit reached (%d)\n", conn_count);
            } else if (!allow_access(&remotess)) {
                if (o.verbose)
                    loguser("New connection denied: not allowed\n");
            } else {
                /* Good to go. */
                break;
            }

            /* Dump the current datagram */
            nbytes = recv(socket_n, buf, sizeof(buf), 0);
            if (nbytes < 0) {
                loguser("%s.\n", socket_strerror(socket_errno()));
                close(socket_n);
                return 1;
            }
            ncat_log_recv(buf, nbytes);
        }

        if (o.debug > 1)
            logdebug("Valid Connection from %d\n", socket_n);

        conn_inc++;

        /*
         * We're using connected udp. This has the down side of only
         * being able to handle one udp client at a time
         */
        Connect(socket_n, &remotess.sockaddr, sslen);

        /* clean slate for buf */
        zmem(buf, sizeof(buf));

        /* are we executing a command? then do it */
        if (o.cmdexec) {
            struct fdinfo info = { 0 };

            info.fd = socket_n;
            if (o.keepopen)
                netrun(&info, o.cmdexec);
            else
                netexec(&info, o.cmdexec);
            continue;
        }

        FD_SET(socket_n, &read_fds);
        FD_SET(STDIN_FILENO, &read_fds);
        fdmax = socket_n;

        /* stdin -> socket and socket -> stdout */
        while (1) {
            fd_set fds;

            fds = read_fds;

            if (o.debug > 1)
                logdebug("udp select'ing\n");

            if (o.idletimeout > 0)
                ms_to_timeval(tvp, o.idletimeout);

            fds_ready = fselect(fdmax + 1, &fds, NULL, NULL, tvp);

            if (fds_ready == 0)
                bye("Idle timeout expired (%d ms).", o.idletimeout);

            if (FD_ISSET(STDIN_FILENO, &fds)) {
                nbytes = Read(STDIN_FILENO, buf, sizeof(buf));
                if (nbytes <= 0) {
                    if (nbytes < 0 && o.verbose) {
                        logdebug("Error reading from stdin: %s\n", strerror(errno));
                    } else if (nbytes == 0 && o.debug) {
                        logdebug("EOF on stdin\n");
                    }
                    FD_CLR(STDIN_FILENO, &read_fds);
                    if (nbytes < 0)
                        return 1;
                    continue;
                }
                if (o.crlf)
                    fix_line_endings((char *) buf, &nbytes, &tempbuf, &crlf_state);
                if (!o.recvonly) {
                    if (tempbuf != NULL)
                        n = send(socket_n, tempbuf, nbytes, 0);
                    else
                        n = send(socket_n, buf, nbytes, 0);
                    if (n < nbytes) {
                        loguser("%s.\n", socket_strerror(socket_errno()));
                        close(socket_n);
                        return 1;
                    }
                    ncat_log_send(buf, nbytes);
                }
                if (tempbuf != NULL) {
                    free(tempbuf);
                    tempbuf = NULL;
                }
            }
            if (FD_ISSET(socket_n, &fds)) {
                nbytes = recv(socket_n, buf, sizeof(buf), 0);
                if (nbytes < 0) {
                    loguser("%s.\n", socket_strerror(socket_errno()));
                    close(socket_n);
                    return 1;
                }
                ncat_log_recv(buf, nbytes);
                if (!o.sendonly)
                    Write(STDOUT_FILENO, buf, nbytes);
            }

            zmem(buf, sizeof(buf));
        }
    }

    return 0;
}
Exemplo n.º 24
0
int main(int argc, char *argv[])
{
	GError *err = NULL;
	int opt;

	bacpy(&src, BDADDR_ANY);
	bacpy(&dst, BDADDR_ANY);

	mainloop = g_main_loop_new(NULL, FALSE);
	if (!mainloop) {
		printf("Failed to create main loop\n");

		exit(1);
	}

	while ((opt = getopt_long(argc, argv, "d:hi:s:c:v:lrfp",
						main_options, NULL)) != EOF) {
		switch (opt) {
		case 'i':
			if (!strncmp(optarg, "hci", 3))
				hci_devba(atoi(optarg + 3), &src);
			else
				str2ba(optarg, &src);
			break;
		case 'd':
			if (!strncasecmp(optarg, "SRC", sizeof("SRC"))) {
				dev_role = AVDTP_SEP_TYPE_SOURCE;
			} else if (!strncasecmp(optarg, "SINK",
							sizeof("SINK"))) {
				dev_role = AVDTP_SEP_TYPE_SINK;
			} else {
				usage();
				exit(0);
			}
			break;
		case 'c':
			if (str2ba(optarg, &dst) < 0) {
				usage();
				exit(0);
			}
			break;
		case 'l':
			bacpy(&dst, BDADDR_ANY);
			break;
		case 'r':
			reject = true;
			break;
		case 'f':
			fragment = true;
			break;
		case 'p':
			preconf = true;
			break;
		case 's':
			parse_command(optarg);
			break;
		case 'v':
			version = strtol(optarg, NULL, 0);
			if (version != 0x0100 && version != 0x0102 &&
							version != 0x0103) {
				printf("invalid version\n");
				exit(0);
			}

			break;
		case 'h':
		default:
			usage();
			exit(0);
		}
	}

	local_sep = avdtp_register_sep(dev_role, AVDTP_MEDIA_TYPE_AUDIO,
					0x00, TRUE, &sep_ind, &sep_cfm, NULL);
	if (!local_sep) {
		printf("Failed to register sep\n");
		exit(0);
	}

	if (!bacmp(&dst, BDADDR_ANY)) {
		printf("Listening...\n");
		io = do_listen(&err);
	} else {
		printf("Connecting...\n");
		io = do_connect(&err);
	}

	if (!io) {
		printf("Failed: %s\n", err->message);
		g_error_free(err);
		exit(0);
	}

	g_main_loop_run(mainloop);

	printf("Done\n");

	avdtp_unref(avdtp);
	avdtp = NULL;

	g_main_loop_unref(mainloop);
	mainloop = NULL;

	return 0;
}
Exemplo n.º 25
0
int main(int argc, char *argv[])
{
    int   sock;
    char  *progname;
    int   opt;
    char  sockname[PATH_MAX];
    int   ret;
    pid_t pid;
    struct sigaction	sa;
    const char *s = NULL;
    glite_renewal_core_context ctx = NULL;
    sigset_t mask;

    progname = strrchr(argv[0],'/');
    if (progname) progname++;
    else progname = argv[0];

    ret = glite_renewal_core_init_ctx(&ctx);
    if (ret) {
        fprintf(stderr, "Cannot initialize context\n");
        exit(1);
    }

    repository = EDG_WLPR_REPOSITORY_ROOT;
    debug = 0;

    while ((opt = getopt_long(argc, argv, "hvdr:c:C:V:AG:t:k:O", opts, NULL)) != EOF)
        switch (opt) {
        case 'h':
            usage(ctx, progname);
            exit(0);
        case 'v':
            fprintf(stdout, "%s:\t%s\n", progname, rcsid);
            exit(0);
        case 'd':
            debug = 1;
            break;
        case 'r':
            repository = optarg;
            break;
        case 'c':
            condor_limit = atoi(optarg);
            break;
        case 'C':
            cadir = optarg;
            break;
        case 'V':
            vomsdir = optarg;
            break;
        case 'A':
            voms_enabled = 1;
            break;
        case 'G':
            ctx->voms_conf = optarg;
            break;
        case 't':
            cert = optarg;
            break;
        case 'k':
            key = optarg;
            break;
        case 'O':
            ctx->order_attributes = 1;
            break;
        case '?':
            usage(ctx, progname);
            return 1;
        }

    if (optind < argc) {
        usage(ctx, progname);
        exit(1);
    }

    if (debug) {
        ctx->log_level = LOG_DEBUG;
        ctx->log_dst = GLITE_RENEWAL_LOG_STDOUT;
    }

    if (chdir(repository)) {
        edg_wlpr_Log(ctx, LOG_ERR, "Cannot access repository directory %s (%s)",
                     repository, strerror(errno));
        exit(1);
    }

    if (!debug) {
        /* chdir ? */
        if (daemon(1,0) == -1) {
            perror("daemon()");
            exit(1);
        }
        openlog(progname, LOG_PID, LOG_DAEMON);
    }

    globus_module_activate(GLOBUS_GSI_CERT_UTILS_MODULE);
    globus_module_activate(GLOBUS_GSI_PROXY_MODULE);

    if (cert)
        setenv("X509_USER_CERT", cert, 1);

    if (key)
        setenv("X509_USER_KEY", key, 1);

    if (cadir)
        setenv("X509_CERT_DIR", cadir, 1);

    s = getenv("GLITE_PR_TIMEOUT");
    default_timeout = s ? atof(s) : GLITE_PR_TIMEOUT_DEFAULT;

    memset(&sa,0,sizeof(sa));
    sa.sa_handler = catchsig;
    sigaction(SIGINT,&sa,NULL);
    sigaction(SIGQUIT,&sa,NULL);
    sigaction(SIGTERM,&sa,NULL);
    sigaction(SIGCHLD,&sa,NULL);
    sigaction(SIGPIPE,&sa,NULL);

    sigemptyset(&mask);
    sigaddset(&mask, SIGINT);
    sigaddset(&mask, SIGQUIT);
    sigaddset(&mask, SIGTERM);
    sigaddset(&mask, SIGCHLD);
    sigaddset(&mask, SIGPIPE);
    sigprocmask(SIG_UNBLOCK, &mask, NULL);

    ret = start_watchdog(ctx, &pid);
    if (ret)
        return 1;

    umask(0177);
    snprintf(sockname, sizeof(sockname), "%s%d",
             DGPR_REG_SOCKET_NAME_ROOT, getuid());
    /* XXX check that the socket is not already active */
    ret = do_listen(ctx, sockname, &sock);
    if (ret)
        return 1;
    edg_wlpr_Log(ctx, LOG_DEBUG, "Listening at %s", sockname);

    ret = doit(ctx, sock);

    close(sock);
    return ret;
}
Exemplo n.º 26
0
PUBLIC int uds_ioctl(message *dev_m_in, message *dev_m_out)
{
	int minor;

#if DEBUG == 1
	static int call_count = 0;
	printf("(uds) [%d] uds_ioctl() call_count=%d\n", uds_minor(dev_m_in),
							++call_count);
 	printf("Endpoint: 0x%x | Position 0x%x\n", dev_m_in->IO_ENDPT, 
							dev_m_in->POSITION);
#endif

	minor = uds_minor(dev_m_in);

	if (uds_fd_table[minor].state != UDS_INUSE) {

		/* attempted to close a socket that hasn't been opened --
		 * something is very wrong :(
		 */
		uds_set_reply(dev_m_out, TASK_REPLY, dev_m_in->IO_ENDPT,
				(cp_grant_id_t) dev_m_in->IO_GRANT, EINVAL);

		return EINVAL;
	}

	/* track the system call we are performing in case it gets cancelled */
	uds_fd_table[minor].call_nr = dev_m_in->m_type;
	uds_fd_table[minor].ioctl = dev_m_in->COUNT;
	uds_fd_table[minor].syscall_done = 0;

	/* setup select(2) framework */
	uds_fd_table[minor].selecting = 0;

	/* update the owner endpoint - yes it's really stored in POSITION */
	uds_fd_table[minor].owner = dev_m_in->POSITION;

	switch (dev_m_in->COUNT) {	/* Handle the ioctl(2) command */

		case NWIOSUDSCONN:

			/* connect to a listening socket -- connect() */
			return do_connect(dev_m_in, dev_m_out);

		case NWIOSUDSACCEPT:

			/* accept an incoming connection -- accept() */
			return do_accept(dev_m_in, dev_m_out);

		case NWIOSUDSBLOG:

			/* set the backlog_size and put the socket into the
			 * listening state -- listen()
			 */
			return do_listen(dev_m_in, dev_m_out);

		case NWIOSUDSTYPE:

			/* set the type for this socket (i.e. 
			 * SOCK_STREAM, SOCK_DGRAM, etc) -- socket()
			 */
			return do_socket(dev_m_in, dev_m_out);

		case NWIOSUDSADDR:

			/* set the address for this socket -- bind() */
			return do_bind(dev_m_in, dev_m_out);

		case NWIOGUDSADDR:

			/* get the address for this socket -- getsockname() */
			return do_getsockname(dev_m_in, dev_m_out);

		case NWIOGUDSPADDR:

			/* get the address for the peer -- getpeername() */
			return do_getpeername(dev_m_in, dev_m_out);

		case NWIOSUDSSHUT:

			/* shutdown a socket for reading, writing, or 
			 * both -- shutdown()
			 */
			return do_shutdown(dev_m_in, dev_m_out);

		case NWIOSUDSPAIR:

			/* connect two sockets -- socketpair() */
			return do_socketpair(dev_m_in, dev_m_out);

		case NWIOGUDSSOTYPE:

			/* get socket type -- getsockopt(SO_TYPE) */
			return do_getsockopt_sotype(dev_m_in, dev_m_out);

		case NWIOGUDSPEERCRED:

			/* get peer endpoint -- getsockopt(SO_PEERCRED) */
			return do_getsockopt_peercred(dev_m_in, dev_m_out);

		case NWIOSUDSTADDR:

			/* set target address -- sendto() */
			return do_sendto(dev_m_in, dev_m_out);

		case NWIOGUDSFADDR:

			/* get from address -- recvfrom() */
			return do_recvfrom(dev_m_in, dev_m_out);

		case NWIOGUDSSNDBUF:

			/* get the send buffer size -- getsockopt(SO_SNDBUF) */
			return do_getsockopt_sndbuf(dev_m_in, dev_m_out);

		case NWIOSUDSSNDBUF:

			/* set the send buffer size -- setsockopt(SO_SNDBUF) */
			return do_setsockopt_sndbuf(dev_m_in, dev_m_out);

		case NWIOGUDSRCVBUF:

			/* get the send buffer size -- getsockopt(SO_SNDBUF) */
			return do_getsockopt_rcvbuf(dev_m_in, dev_m_out);

		case NWIOSUDSRCVBUF:

			/* set the send buffer size -- setsockopt(SO_SNDBUF) */
			return do_setsockopt_rcvbuf(dev_m_in, dev_m_out);

		case NWIOSUDSCTRL:

			/* set the control data -- sendmsg() */
			return do_sendmsg(dev_m_in, dev_m_out);

		case NWIOGUDSCTRL:

			/* set the control data -- recvmsg() */
			return do_recvmsg(dev_m_in, dev_m_out);

		default:

			/* the IOCTL command is not valid for /dev/uds --
			 * this happens a lot and is normal. a lot of 
			 * libc functions determine the socket type with 
			 * IOCTLs. Any not for us simply get a EBADIOCTL
			 * response.
			 */
			uds_fd_table[minor].syscall_done = 1;
			uds_set_reply(dev_m_out, TASK_REPLY,
					dev_m_in->IO_ENDPT, 
					(cp_grant_id_t) dev_m_in->IO_GRANT,
					EBADIOCTL);

			return EBADIOCTL;
	}
}
Exemplo n.º 27
0
Arquivo: main.c Projeto: gcwnow/pwswd
int main(int argc, char **argv)
{
	const char *filename = NULL,
#ifdef BACKEND_VOLUME
		  *mixer = NULL, *dac = NULL,
#endif
		  *event = NULL, *uinput = NULL;
	int exitcode = EXIT_SUCCESS;
	size_t i;

	for (i = 1; i < argc; i++) {
		if (argc > i + 1) {
			if (!strcmp(argv[i], "-f"))
				filename = argv[i+1];
			else if (!strcmp(argv[i], "-e"))
				event = argv[i+1];
			else if (!strcmp(argv[i], "-u"))
				uinput = argv[i+1];
#ifdef BACKEND_VOLUME
			else if (!strcmp(argv[i], "-m"))
				mixer = argv[i+1];
			else if (!strcmp(argv[i], "-d"))
				dac = argv[i+1];
#endif
			else {
				printf(usage);
				return EXIT_FAILURE;
			}

			i++;
		} else {
			printf(usage);
			return EXIT_FAILURE;
		}
	}
	
	if (!filename) {
		struct stat st;
		filename = "/usr/local/etc/" PROGNAME ".conf";

		if (stat(filename, &st) == -1) {
			filename = "/etc/" PROGNAME ".conf";

			if (stat(filename, &st) == -1) {
				printf("pwswd: Unable to find a configuration file.\n");
				return EXIT_FAILURE;
			}
		}

		if (!S_ISREG(st.st_mode)) {
			printf("pwswd: The configuration file is not a regular file.\n");
			return EXIT_FAILURE;
		}
	}

	if (!event)
		event = EVENT_FILENAME;

	if (!uinput)
		uinput = UINPUT_FILENAME;

	if (read_conf_file(filename) < 0) {
		fprintf(stderr, "Unable to parse configuration file: aborting.\n");
		return EXIT_FAILURE;
	}

#ifdef BACKEND_VOLUME
	if (!mixer)
		mixer = DEFAULT_MIXER;

	if (vol_init(mixer, dac))
		fprintf(stderr, "Unable to init volume backend\n");
#endif

	if (do_listen(event, uinput))
		exitcode = EXIT_FAILURE;
	deinit();
	return exitcode;
}
int main(int argc, char *argv[])
{
	struct sigaction sa;
	int opt, sk, mode = RECV, need_addr = 0;

	bacpy(&bdaddr, BDADDR_ANY);
	bacpy(&auto_bdaddr, BDADDR_ANY);

	while ((opt=getopt(argc,argv,"rdscuwmna:b:i:P:U:B:O:N:MAESL:W:C:D:Y:T")) != EOF) {
		switch (opt) {
		case 'r':
			mode = RECV;
			break;

		case 's':
			mode = SEND;
			need_addr = 1;
			break;

		case 'w':
			mode = LSEND;
			break;

		case 'u':
			mode = CRECV;
			need_addr = 1;
			break;

		case 'd':
			mode = DUMP;
			break;

		case 'c':
			mode = RECONNECT;
			need_addr = 1;
			break;

		case 'n':
			mode = CONNECT;
			need_addr = 1;
			break;

		case 'm':
			mode = MULTY;
			need_addr = 1;
			break;

		case 'a':
			mode = AUTO;

			if (!strncasecmp(optarg, "hci", 3))
				hci_devba(atoi(optarg + 3), &auto_bdaddr);
			else
				str2ba(optarg, &auto_bdaddr);
			break;

		case 'b':
			data_size = atoi(optarg);
			break;

		case 'i':
			if (!strncasecmp(optarg, "hci", 3))
				hci_devba(atoi(optarg + 3), &bdaddr);
			else
				str2ba(optarg, &bdaddr);
			break;

		case 'P':
			channel = atoi(optarg);
			break;

		case 'U':
			if (!strcasecmp(optarg, "spp"))
				uuid = SERIAL_PORT_SVCLASS_ID;
			else if (!strncasecmp(optarg, "0x", 2))
				uuid = strtoul(optarg + 2, NULL, 16);
			else
				uuid = atoi(optarg);
			break;

		case 'M':
			master = 1;
			break;

		case 'A':
			auth = 1;
			break;

		case 'E':
			encr = 1;
			break;

		case 'S':
			secure = 1;
			break;

		case 'L':
			linger = atoi(optarg);
			break;

		case 'W':
			defer_setup = atoi(optarg);
			break;

		case 'B':
			filename = strdup(optarg);
			break;

		case 'O':
			savefile = strdup(optarg);
			break;

		case 'N':
			num_frames = atoi(optarg);
			break;

		case 'C':
			count = atoi(optarg);
			break;

		case 'D':
			delay = atoi(optarg) * 1000;
			break;

		case 'Y':
			priority = atoi(optarg);
			break;

		case 'T':
			timestamp = 1;
			break;

		default:
			usage();
			exit(1);
		}
	}

	if (need_addr && !(argc - optind)) {
		usage();
		exit(1);
	}

	if (!(buf = malloc(data_size))) {
		perror("Can't allocate data buffer");
		exit(1);
	}

	memset(&sa, 0, sizeof(sa));
	if (mode == AUTO)
		sa.sa_handler = sig_child_exit;
	else
		sa.sa_handler = SIG_IGN;
	sa.sa_flags   = SA_NOCLDSTOP;
	sigaction(SIGCHLD, &sa, NULL);

	openlog("rctest", LOG_PERROR | LOG_PID, LOG_LOCAL0);

	switch (mode) {
		case RECV:
			do_listen(recv_mode);
			break;

		case CRECV:
			sk = do_connect(argv[optind]);
			if (sk < 0)
				exit(1);
			recv_mode(sk);
			break;

		case DUMP:
			do_listen(dump_mode);
			break;

		case SEND:
			sk = do_connect(argv[optind]);
			if (sk < 0)
				exit(1);
			send_mode(sk);
			break;

		case LSEND:
			do_listen(send_mode);
			break;

		case RECONNECT:
			reconnect_mode(argv[optind]);
			break;

		case MULTY:
			multi_connect_mode(argc - optind, argv + optind);
			break;

		case CONNECT:
			sk = do_connect(argv[optind]);
			if (sk < 0)
				exit(1);
			dump_mode(sk);
			break;

		case AUTO:
			automated_send_recv();
			break;
	}

	syslog(LOG_INFO, "Exit");

	closelog();

	return 0;
}
Exemplo n.º 29
0
int main(int argc, char **argv)
{
	char *dst = NULL, *src = NULL;
	struct sigaction sa;
	int mode = NONE;
	int opt;

	while ((opt=getopt_long(argc, argv, main_sopts, main_lopts, NULL)) != -1) {
		switch(opt) {
		case 'l':
			mode = SHOW;
			detach = 0;
			break;

		case 's':
			mode = LISTEN;
			break;

		case 'c':
			mode = CONNECT;
			dst  = strdup(optarg);
			break;

		case 'Q':
			mode = CONNECT;
			dst  = NULL;
			if (optarg)
				search_duration = atoi(optarg);
			break;

		case 'k':
			mode = KILL;
			detach = 0;
			dst  = strdup(optarg);
			break;

		case 'K':
			mode = KILL;
			detach = 0;
			dst  = NULL;
			break;

		case 'P':
			channel = atoi(optarg);
			break;

		case 'i':
			src = strdup(optarg);
			break;

		case 'D':
			use_sdp = 0;
			break;

		case 'A':
			auth = 1;
			break;

		case 'E':
			encrypt = 1;
			break;

		case 'S':
			secure = 1;
			break;

		case 'M':
			master = 1;
			break;

		case 'n':
			detach = 0;
			break;

		case 'p':
			if (optarg)
				persist = atoi(optarg);
			else
				persist = 5;
			break;

		case 'C':
			if (optarg)
				use_cache = atoi(optarg);
			else
				use_cache = 2;
			break;

		case 'd':
			pppd  = strdup(optarg);
			break;

		case 'X':
			if (optarg)
				msdun = atoi(optarg);
			else
				msdun = 10;
			break;

		case 'a':
			msdun = 10;
			type = ACTIVESYNC;
			break;

		case 'm':
			mode = LISTEN;
			dst  = strdup(optarg);
			type = MROUTER;
			break;

		case 'u':
			mode = LISTEN;
			type = DIALUP;
			break;

		case 'h':
		default:
			printf(main_help);
			exit(0);
		}
	}

	argc -= optind;
	argv += optind;

	/* The rest is pppd options */
	if (argc > 0) {
		for (opt = 3; argc && opt < DUN_MAX_PPP_OPTS; argc--, opt++)
			pppd_opts[opt] = *argv++;
		pppd_opts[opt] = NULL;
	}

	io_init();

	if (dun_init())
		return -1;

	/* Check non daemon modes first */
	switch (mode) {
	case SHOW:
		do_show();
		return 0;

	case KILL:
		do_kill(dst);
		return 0;

	case NONE:
		printf(main_help);
		return 0;
	}

	/* Initialize signals */
	memset(&sa, 0, sizeof(sa));
	sa.sa_flags   = SA_NOCLDSTOP;
	sa.sa_handler = SIG_IGN;
	sigaction(SIGCHLD, &sa, NULL);
	sigaction(SIGPIPE, &sa, NULL);

	sa.sa_handler = sig_term;
	sigaction(SIGTERM, &sa, NULL);
	sigaction(SIGINT,  &sa, NULL);

	sa.sa_handler = sig_hup;
	sigaction(SIGHUP, &sa, NULL);

	if (detach) {
		int fd;

		if (vfork()) exit(0);

		/* Direct stdin,stdout,stderr to '/dev/null' */
		fd = open("/dev/null", O_RDWR);
		dup2(fd, 0); dup2(fd, 1); dup2(fd, 2);
		close(fd);

		setsid();
		chdir("/");
	}

	openlog("dund", LOG_PID | LOG_NDELAY | LOG_PERROR, LOG_DAEMON);
	syslog(LOG_INFO, "Bluetooth DUN daemon version %s", VERSION);

	if (src) {
		src_dev = hci_devid(src);
		if (src_dev < 0 || hci_devba(src_dev, &src_addr) < 0) {
			syslog(LOG_ERR, "Invalid source. %s(%d)", strerror(errno), errno);
			return -1;
		}
	}

	if (dst) {
		strncpy(cache.dst, dst, sizeof(cache.dst) - 1);
		str2ba(dst, &cache.bdaddr);

		/* Disable cache invalidation */
		use_cache = cache.valid = ~0;
	}

	switch (mode) {
	case CONNECT:
		do_connect();
		break;

	case LISTEN:
		do_listen();
		break;
	}

	return 0;
}
Exemplo n.º 30
0
int main(int argc, char *argv[]) {
    struct option longopts[] = {
        { "listen",	no_argument,		NULL,	'l' },
        { "connect",	no_argument,		NULL,	'c' },
        { "send",	no_argument,		NULL,	's' },
        { "receive",	no_argument,		NULL,	'r' },
        { "port",	required_argument,	NULL,	'p' },
        { "buffer",	required_argument,	NULL,	'b' },
        { "help",	no_argument,		NULL,	'h' },
        { "version",	no_argument,		NULL,	'V' },
        { NULL, 0, NULL, 0 }
    };
    int opt;
    enum { LISTEN=1, CONNECT } mode = 0;
    enum { SEND=1, RECEIVE } direction = 0;
    int port = 0;
    int buffer_size = 0;
    char *rhost = NULL;
    int sock;

    argv0=argv[0];

    while ((opt=getopt_long(argc, argv, "-lcsrp:b:hV", longopts, NULL)) != -1) {
        switch (opt) {
            char *tailptr;
        case 'l':
            if (mode==0) mode=LISTEN;
            else param_error(NULL);
            break;
        case 'c':
            if (mode==0) mode=CONNECT;
            else param_error(NULL);
            break;
        case 's':
            if (direction==0) direction=SEND;
            else param_error(NULL);
            break;
        case 'r':
            if (direction==0) direction=RECEIVE;
            else param_error(NULL);
            break;
        case 'p':
            if (port==0) {
                errno=0;
                port = strtol(optarg, &tailptr, 0);
                if (errno || *tailptr!=0) param_error(NULL);
            }
            else param_error(NULL);
            break;
        case 'b':
            if (buffer_size==0) {
                errno=0;
                buffer_size = strtol(optarg, &tailptr, 0);
                if (errno || *tailptr!=0) param_error(NULL);
            }
            else param_error(NULL);
            break;
        case 'h':
            print_usage();
            break;
        case 'V':
            print_version();
            break;
        case 1:
            if (rhost==NULL) rhost=optarg;
            else param_error(NULL);
            break;
        case '?':
            param_error("");
            break;
        default:
            oops();
            break;
        }
    }
    if (mode==0) param_error(NULL);
    if (direction==0) direction = mode==LISTEN ? SEND : RECEIVE;
    if (port==0) param_error(NULL);
    if (port<0 || port>65535) param_error(NULL);
    if (buffer_size==0) buffer_size = DEFAULT_BUFFER_SIZE;
    if (buffer_size<0) param_error(NULL);
    if (mode==CONNECT && rhost==NULL) param_error(NULL);

    switch (mode) {
    case LISTEN:
        sock = do_listen(port, rhost);
        break;
    case CONNECT:
        sock = do_connect(port, rhost);
        break;
    default:
        oops();
        break;
    }
    switch (direction) {
    case SEND:
        do_transfer(STDIN_FILENO, sock, buffer_size);
        break;
    case RECEIVE:
        do_transfer(sock, STDOUT_FILENO, buffer_size);
        break;
    default:
        oops();
        break;
    }
    return 0;
}