Exemplo n.º 1
0
static gint sock_connect_by_hostname(gint sock, const gchar *hostname,
				     gushort port)
{
	struct hostent *hp;
	struct sockaddr_in ad;

	resolver_init();

	memset(&ad, 0, sizeof(ad));
	ad.sin_family = AF_INET;
	ad.sin_port = htons(port);

	if (!my_inet_aton(hostname, &ad.sin_addr)) {
		if ((hp = my_gethostbyname(hostname)) == NULL) {
			fprintf(stderr, "%s: unknown host.\n", hostname);
			errno = 0;
			return -1;
		}

		if (hp->h_length != 4 && hp->h_length != 8) {
			fprintf(stderr, "illegal address length received for host %s\n", hostname);
			errno = 0;
			return -1;
		}

		memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
	}

	return sock_connect_with_timeout(sock, (struct sockaddr *)&ad,
					 sizeof(ad), io_timeout);
}
Exemplo n.º 2
0
/* opens a tcp or udp connection to a remote host */
int my_connect(char *host_name,int port,int *sd,char *proto){
	struct sockaddr_in servaddr;
	struct hostent *hp;
	struct protoent *ptrp;
	int result;

	bzero((char *)&servaddr,sizeof(servaddr));
	servaddr.sin_family=AF_INET;
	servaddr.sin_port=htons(port);

	/* try to bypass using a DNS lookup if this is just an IP address */
	if(!my_inet_aton(host_name,&servaddr.sin_addr)){

		/* else do a DNS lookup */
		hp=gethostbyname((const char *)host_name);
		if(hp==NULL){
			printf("Invalid host name '%s'\n",host_name);
			return STATE_UNKNOWN;
		        }

		memcpy(&servaddr.sin_addr,hp->h_addr,hp->h_length);
	        }

	/* map transport protocol name to protocol number */
	if(((ptrp=getprotobyname(proto)))==NULL){
		printf("Cannot map \"%s\" to protocol number\n",proto);
		return STATE_UNKNOWN;
	        }

	/* create a socket */
	*sd=socket(PF_INET,(!strcmp(proto,"udp"))?SOCK_DGRAM:SOCK_STREAM,ptrp->p_proto);
	if(*sd<0){
		printf("Socket creation failed\n");
		return STATE_UNKNOWN;
	        }

	/* open a connection */
	result=connect(*sd,(struct sockaddr *)&servaddr,sizeof(servaddr));
	if(result<0){
		switch(errno){  
		case ECONNREFUSED:
			printf("Connection refused by host\n");
			break;
		case ETIMEDOUT:
			printf("Timeout while attempting connection\n");
			break;
		case ENETUNREACH:
			printf("Network is unreachable\n");
			break;
		default:
			printf("Connection refused or timed out\n");
		        }

		return STATE_CRITICAL;
	        }

	return STATE_OK;
        }
Exemplo n.º 3
0
Arquivo: miscd.c Projeto: wyat/kbs
void utmpd()
{
    int m_socket;

    struct sockaddr_in sin;
    int sinlen = sizeof(sin);
    int opt = 1;

    bzero(&sin, sizeof(sin));
    if ((m_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
        bbslog("3system", "utmpd:socket %s", strerror(errno));
        exit(-1);
    }
    setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, &opt, 4);
    memset(&sin, 0, sinlen);
    sin.sin_family = AF_INET;
#ifdef CELESTIS
    sin.sin_port = htons(61002);
#else
    sin.sin_port = htons(60002);
#endif
#ifdef HAVE_INET_ATON
    inet_aton("127.0.0.1", &sin.sin_addr);
#elif defined HAVE_INET_PTON
    inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr);
#else
    /* Is it OK? */
    my_inet_aton("127.0.0.1", &sin.sin_addr);
#endif
    if (0 != bind(m_socket, (struct sockaddr *) &sin, sizeof(sin))) {
        bbslog("3system", "utmpd:bind %s", strerror(errno));
        exit(-1);
    }
    if (0 != listen(m_socket, 5)) {
        bbslog("3system", "utmpd:listen %s", strerror(errno));
        exit(-1);
    }
    while (1) {
        int sock, id;

        sock = getutmprequest(m_socket);
#if 0
        {                       /*kill user */
            time_t now;
            struct user_info *uentp;

            now = time(NULL);
            if ((now > utmpshm->uptime + 120) || (now < utmpshm->uptime - 120)) {
                int n;

                utmpshm->uptime = now;
                bbslog("1system", "UTMP:Clean user utmp cache");
                for (n = 0; n < USHM_SIZE; n++) {
                    utmpshm->uptime = now;
                    uentp = &(utmpshm->uinfo[n]);
                    if (uentp->active && uentp->pid && kill(uentp->pid, 0) == -1) {     /*uentp检查 */
                        char buf[STRLEN];

                        strncpy(buf, uentp->userid, IDLEN + 2);
                        clear_utmp(n + 1);
                        RemoveMsgCountFile(buf);
                    }
                }
            }
        }
#endif
        /* utmp */
        switch (utmpreq.command) {
        case 1:                // getnewutmp
            id = getnewutmpent2(&utmpreq.u_info.utmp, 0 /* TODO ! */);
            break;
        case 2:
            id = -1;
            break;              // clear, by uentp
        case 3:                // clear, by id
            /*这个代码有错误的,因为pid不能不判断 */
            clear_utmp(utmpreq.u_info.uent, 0, 0);
            id = 0;
            break;
        default:
            id = -1;
            break;
        }
        putrequest(sock, id);
    }
    return;
}
Exemplo n.º 4
0
Arquivo: miscd.c Projeto: wyat/kbs
void userd()
{
    int m_socket;

#ifdef HAVE_IPV6_SMTH
    struct sockaddr_in6 sin;
#else
    struct sockaddr_in sin;
#endif
    int sinlen = sizeof(sin);
    int opt = 1;

    bzero(&sin, sizeof(sin));
#ifdef HAVE_IPV6_SMTH
    if ((m_socket = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP)) < 0)
#else
    if ((m_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
#endif
    {
        bbslog("3system", "userd:socket %s", strerror(errno));
        exit(-1);
    }
    setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, &opt, 4);
    memset(&sin, 0, sinlen);
#ifdef HAVE_IPV6_SMTH
    sin.sin6_family = AF_INET6;
    sin.sin6_port = htons(USERD_PORT);
    inet_pton(AF_INET6, "::1", &sin.sin6_addr);
#else 
    sin.sin_family = AF_INET;
    sin.sin_port = htons(USERD_PORT);
#ifdef HAVE_INET_ATON
    inet_aton("127.0.0.1", &sin.sin_addr);
#elif defined HAVE_INET_PTON
    inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr);
#else
    /* Is it OK? */
    my_inet_aton("127.0.0.1", &sin.sin_addr);
#endif
#endif /* IPV6 */
    if (0 != bind(m_socket, (struct sockaddr *) &sin, sizeof(sin))) {
        bbslog("3system", "userd:bind %s", strerror(errno));
        exit(-1);
    }
    if (0 != listen(m_socket, 5)) {
        bbslog("3system", "userd:listen %s", strerror(errno));
        exit(-1);
    }
    while (1) {
        int sock, id;

        sock = getrequest(m_socket);
#ifndef SECONDSITE
        if (!strcmp(cmd, "NEW"))
            id = getnewuserid(username);
        else
#endif
        if (!strcmp(cmd, "SET")) {
            setuserid2(num, username);
            id = 0;
        }
        else if (!strcmp(cmd, "DEL")) {
            setuserid2(num, "");
            id = 0;
        } else
            continue;
        putrequest(sock, id);
    }
    return;
}
Exemplo n.º 5
0
/* wait for incoming connection requests */
void wait_for_connections(void){
	struct sockaddr_in myname;
	struct sockaddr_in *nptr;
	struct sockaddr addr;
	int rc;
	int sock, new_sd;
	socklen_t addrlen;
	pid_t pid;
	int flag=1;
	fd_set fdread;
	struct timeval timeout;
	int retval;
#ifdef HAVE_LIBWRAP
	struct request_info req;
#endif

	/* create a socket for listening */
	sock=socket(AF_INET,SOCK_STREAM,0);

	/* exit if we couldn't create the socket */
	if(sock<0){
	        syslog(LOG_ERR,"Network server socket failure (%d: %s)",errno,strerror(errno));
	        exit(STATE_CRITICAL);
		}

	/* socket should be non-blocking */
	fcntl(sock,F_SETFL,O_NONBLOCK);

        /* set the reuse address flag so we don't get errors when restarting */
        flag=1;
        if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&flag,sizeof(flag))<0){
		syslog(LOG_ERR,"Could not set reuse address option on socket!\n");
		exit(STATE_UNKNOWN);
	        }

	myname.sin_family=AF_INET;
	myname.sin_port=htons(server_port);
 	bzero(&myname.sin_zero,8);

	/* what address should we bind to? */
        if(!strlen(server_address))
		myname.sin_addr.s_addr=INADDR_ANY;

	else if(!my_inet_aton(server_address,&myname.sin_addr)){
		syslog(LOG_ERR,"Server address is not a valid IP address\n");
		exit(STATE_CRITICAL);
                }

	/* bind the address to the Internet socket */
	if(bind(sock,(struct sockaddr *)&myname,sizeof(myname))<0){
		syslog(LOG_ERR,"Network server bind failure (%d: %s)\n",errno,strerror(errno));
	        exit(STATE_CRITICAL);
	        }

	/* open the socket for listening */
	if(listen(sock,5)<0){
	    	syslog(LOG_ERR,"Network server listen failure (%d: %s)\n",errno,strerror(errno));
	        exit(STATE_CRITICAL);
		}

	/* log warning about command arguments */
#ifdef ENABLE_COMMAND_ARGUMENTS
	if(allow_arguments==TRUE)
		syslog(LOG_NOTICE,"Warning: Daemon is configured to accept command arguments from clients!");
#ifdef ENABLE_BASH_COMMAND_SUBSTITUTION
	if(TRUE==allow_bash_command_substitution) {
		if(TRUE==allow_arguments)
			syslog(LOG_NOTICE,"Warning: Daemon is configured to accept command arguments with bash command substitutions!");
		else
			syslog(LOG_NOTICE,"Warning: Daemon is configured to accept command arguments with bash command substitutions, but is not configured to accept command argements from clients. Enable command arguments if you wish to allow command arguments with bash command substitutions.");
		}
#endif
#endif

	syslog(LOG_INFO,"Listening for connections on port %d\n",htons(myname.sin_port));

	if(allowed_hosts)
		syslog(LOG_INFO,"Allowing connections from: %s\n",allowed_hosts);

	/* listen for connection requests - fork() if we get one */
	while(1){

		/* wait for a connection request */
	        while(1){

			/* wait until there's something to do */
			FD_ZERO(&fdread);
			FD_SET(sock,&fdread);
			timeout.tv_sec=0;
			timeout.tv_usec=500000;
			retval=select(sock+1,&fdread,NULL,&fdread,&timeout);

			/* bail out if necessary */
			if(sigrestart==TRUE || sigshutdown==TRUE)
				break;

			/* error */
			if(retval<0)
				continue;

			/* accept a new connection request */
			new_sd=accept(sock,0,0);

			/* some kind of error occurred... */
			if(new_sd<0){

				/* bail out if necessary */
				if(sigrestart==TRUE || sigshutdown==TRUE)
					break;

				/* retry */
				if(errno==EWOULDBLOCK || errno==EINTR)
					continue;

				/* socket is nonblocking and we don't have a connection yet */
				if(errno==EAGAIN)
					continue;

				/* fix for HP-UX 11.0 - just retry */
				if(errno==ENOBUFS)
					continue;

				/* else handle the error later */
				break;
			        }

			/* connection was good */
			break;
		        }

		/* bail out if necessary */
		if(sigrestart==TRUE || sigshutdown==TRUE)
			break;

		/* child process should handle the connection */
    		pid=fork();
    		if(pid==0){

			/* fork again so we don't create zombies */
			pid=fork();
			if(pid==0){

				/* hey, there was an error... */
				if(new_sd<0){

					/* log error to syslog facility */
					syslog(LOG_ERR,"Network server accept failure (%d: %s)",errno,strerror(errno));

					/* close socket prioer to exiting */
					close(sock);
			
					return;
				        }

				/* handle signals */
				signal(SIGQUIT,child_sighandler);
				signal(SIGTERM,child_sighandler);
				signal(SIGHUP,child_sighandler);

				/* grandchild does not need to listen for connections, so close the socket */
				close(sock);  

				/* find out who just connected... */
				addrlen=sizeof(addr);
				rc=getpeername(new_sd,&addr,&addrlen);

				if(rc<0){

				        /* log error to syslog facility */
					syslog(LOG_ERR,"Error: Network server getpeername() failure (%d: %s)",errno,strerror(errno));

				        /* close socket prior to exiting */
					close(new_sd);

					return;
		                        }

				nptr=(struct sockaddr_in *)&addr;

				/* log info to syslog facility */
				if(debug==TRUE)
					syslog(LOG_DEBUG,"Connection from %s port %d",inet_ntoa(nptr->sin_addr),nptr->sin_port);

				/* is this is a blessed machine? */
				if(allowed_hosts){
                	switch(nptr->sin_family) {
                    	case AF_INET:
                        	if(!is_an_allowed_host(nptr->sin_addr)) {
                            	/* log error to syslog facility */
                            	syslog(LOG_ERR,"Host %s is not allowed to talk to us!",inet_ntoa(nptr->sin_addr));

								/* log info to syslog facility */
								if ( debug==TRUE )
                                	syslog(LOG_DEBUG,"Connection from %s closed.",inet_ntoa(nptr->sin_addr));

								/* close socket prior to exiting */
								close(new_sd);
								exit(STATE_OK);
								} else {
									/* log info to syslog facility */
									if(debug==TRUE)
										syslog(LOG_DEBUG,"Host address is in allowed_hosts");

								}
							break;
                           
						case AF_INET6:
							syslog(LOG_DEBUG,"Connection from %s closed. We don't support AF_INET6 addreess family in ACL\n", inet_ntoa(nptr->sin_addr));
							break;
					}
				}

#ifdef HAVE_LIBWRAP

				/* Check whether or not connections are allowed from this host */
				request_init(&req,RQ_DAEMON,"nrpe",RQ_FILE,new_sd,0);
				fromhost(&req);

				if(!hosts_access(&req)){

					syslog(LOG_DEBUG,"Connection refused by TCP wrapper");

					/* refuse the connection */
					refuse(&req);
					close(new_sd);

					/* should not be reached */
					syslog(LOG_ERR,"libwrap refuse() returns!");
					exit(STATE_CRITICAL);
					}
#endif

				/* handle the client connection */
				handle_connection(new_sd);

				/* log info to syslog facility */
				if(debug==TRUE)
					syslog(LOG_DEBUG,"Connection from %s closed.",inet_ntoa(nptr->sin_addr));

				/* close socket prior to exiting */
				close(new_sd);

				exit(STATE_OK);
    			        }

			/* first child returns immediately, grandchild is inherited by INIT process -> no zombies... */
			else
				exit(STATE_OK);
		        }
		
		/* parent ... */
		else{
			/* parent doesn't need the new connection */
			close(new_sd);

			/* parent waits for first child to exit */
			waitpid(pid,NULL,0);
		        }
  		}

	/* close the socket we're listening on */
	close(sock);

	return;
	}
Exemplo n.º 6
0
/* subset of getaddrinfo() */
static int my_getaddrinfo(const char *node, const char *service,
			  const struct addrinfo *hintp,
			  struct addrinfo **res)
{
	struct addrinfo *ai;
	struct sockaddr_in addr, *paddr;
	struct addrinfo hints;
	gint port = 0;

	if (win32_ipv6_supported())
		return getaddrinfo_func(node, service, hintp, res);

	if (!hintp) {
		memset(&hints, 0, sizeof(hints));
		hints.ai_family = AF_INET;
		hints.ai_socktype = SOCK_STREAM;
	} else
		memcpy(&hints, hintp, sizeof(hints));

	if (hints.ai_family != AF_UNSPEC && hints.ai_family != AF_INET)
		return EAI_FAMILY;
	if (hints.ai_socktype == 0)
		hints.ai_socktype = SOCK_STREAM;
	if (hints.ai_protocol == 0)
		hints.ai_protocol = IPPROTO_TCP;
	if (hints.ai_socktype != SOCK_STREAM)
		return EAI_SOCKTYPE;
	if (hints.ai_protocol != IPPROTO_TCP)
		return EAI_SOCKTYPE;
#if 0
	if (!node && !service)
		return EAI_NONAME;
#endif
	if (!node || !service)
		return EAI_NONAME;

	port = atoi(service);

	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(port);

	if (!my_inet_aton(node, &addr.sin_addr)) {
		struct hostent *hp;

		if ((hp = my_gethostbyname(node)) == NULL) {
			fprintf(stderr, "%s: unknown host.\n", node);
			errno = 0;
			return EAI_NONAME;
		}
		if (hp->h_length != 4 && hp->h_length != 8) {
			fprintf(stderr, "illegal address length received for host %s\n", node);
			errno = 0;
			return EAI_FAIL;
		}

		memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
	}

	ai = g_malloc0(sizeof(struct addrinfo));
	paddr = g_malloc(sizeof(struct sockaddr_in));
	memcpy(paddr, &addr, sizeof(struct sockaddr_in));

	ai->ai_flags = 0;
	ai->ai_family = AF_INET;
	ai->ai_socktype = hints.ai_socktype;
	ai->ai_protocol = hints.ai_protocol;
	ai->ai_addrlen = sizeof(struct sockaddr_in);
	ai->ai_addr = (struct sockaddr *)paddr;
	ai->ai_canonname = NULL;
	ai->ai_next = NULL;

	*res = ai;

	return 0;
}