|
|
@@ -4,7 +4,7 @@
|
|
|
* Copyright (c) 1999-2006 Ethan Galstad (nagios@nagios.org)
|
|
|
* License: GPL
|
|
|
*
|
|
|
- * Last Modified: 02-03-2006
|
|
|
+ * Last Modified: 02-23-2006
|
|
|
*
|
|
|
* Command line: nrpe -c <config_file> [--inetd | --daemon]
|
|
|
*
|
|
|
@@ -59,6 +59,8 @@ command *command_list=NULL;
|
|
|
char *nrpe_user=NULL;
|
|
|
char *nrpe_group=NULL;
|
|
|
|
|
|
+char *allowed_hosts=NULL;
|
|
|
+
|
|
|
char *pid_file=NULL;
|
|
|
|
|
|
int allow_arguments=FALSE;
|
|
|
@@ -196,6 +198,7 @@ int main(int argc, char **argv){
|
|
|
|
|
|
if(RAND_status()==0){
|
|
|
syslog(LOG_ERR,"Warning: SSL/TLS uses a weak random seed which is highly discouraged");
|
|
|
+ srand(time(NULL));
|
|
|
for(i=0;i<500 && RAND_status()==0;i++){
|
|
|
for(c=0;c<sizeof(seedfile);c+=sizeof(int)){
|
|
|
*((int *)(seedfile+c))=rand();
|
|
|
@@ -443,6 +446,9 @@ int read_config_file(char *filename){
|
|
|
server_address[sizeof(server_address)-1]='\0';
|
|
|
}
|
|
|
|
|
|
+ else if(!strcmp(varname,"allowed_hosts"))
|
|
|
+ allowed_hosts=strdup(varvalue);
|
|
|
+
|
|
|
else if(strstr(input_line,"command[")){
|
|
|
temp_buffer=strtok(varname,"[");
|
|
|
temp_buffer=strtok(NULL,"]");
|
|
|
@@ -624,7 +630,6 @@ void wait_for_connections(void){
|
|
|
int rc;
|
|
|
int sock, new_sd;
|
|
|
socklen_t addrlen;
|
|
|
- char connecting_host[16];
|
|
|
pid_t pid;
|
|
|
int flag=1;
|
|
|
fd_set fdread;
|
|
|
@@ -686,6 +691,9 @@ void wait_for_connections(void){
|
|
|
|
|
|
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){
|
|
|
|
|
|
@@ -786,6 +794,31 @@ void wait_for_connections(void){
|
|
|
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){
|
|
|
+
|
|
|
+ if(!is_an_allowed_host(inet_ntoa(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");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
#ifdef HAVE_LIBWRAP
|
|
|
|
|
|
/* Check whether or not connections are allowed from this host */
|
|
|
@@ -842,6 +875,68 @@ void wait_for_connections(void){
|
|
|
|
|
|
|
|
|
|
|
|
+/* checks to see if a given host is allowed to talk to us */
|
|
|
+int is_an_allowed_host(char *connecting_host){
|
|
|
+ char *temp_buffer=NULL;
|
|
|
+ char *temp_ptr=NULL;
|
|
|
+ int result=0;
|
|
|
+ struct hostent *myhost;
|
|
|
+ char **pptr=NULL;
|
|
|
+ char resolved_addr[INET6_ADDRSTRLEN]="";
|
|
|
+
|
|
|
+ /* make sure we have something */
|
|
|
+ if(connecting_host==NULL)
|
|
|
+ return 0;
|
|
|
+ if(allowed_hosts==NULL)
|
|
|
+ return 1;
|
|
|
+
|
|
|
+ if((temp_buffer=strdup(allowed_hosts))==NULL)
|
|
|
+ return 0;
|
|
|
+
|
|
|
+ /* try and match IP addresses first */
|
|
|
+ for(temp_ptr=strtok(temp_buffer,",");temp_ptr!=NULL;temp_ptr=strtok(NULL,",")){
|
|
|
+
|
|
|
+ if(!strcmp(connecting_host,temp_ptr)){
|
|
|
+ result=1;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /* try DNS lookups if needed */
|
|
|
+ if(result==0){
|
|
|
+
|
|
|
+ free(temp_buffer);
|
|
|
+ if((temp_buffer=strdup(allowed_hosts))==NULL)
|
|
|
+ return 0;
|
|
|
+
|
|
|
+ for(temp_ptr=strtok(temp_buffer,",");temp_ptr!=NULL;temp_ptr=strtok(NULL,",")){
|
|
|
+
|
|
|
+ myhost=gethostbyname(temp_ptr);
|
|
|
+ if(myhost!=NULL){
|
|
|
+
|
|
|
+ /* check all addresses for the host... */
|
|
|
+ for(pptr=myhost->h_addr_list;*pptr!=NULL;pptr++){
|
|
|
+
|
|
|
+ inet_ntop(myhost->h_addrtype,*pptr,resolved_addr,sizeof(resolved_addr));
|
|
|
+ if(!strcmp(resolved_addr,connecting_host)){
|
|
|
+ result=1;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(result==1)
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ free(temp_buffer);
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
/* handles a client connection */
|
|
|
void handle_connection(int sock){
|
|
|
u_int32_t calculated_crc32;
|