utils.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /****************************************************************************
  2. *
  3. * UTILS.C - NRPE Utility Functions
  4. *
  5. * License: GPL
  6. * Copyright (c) 1999-2003 Ethan Galstad (nagios@nagios.org)
  7. *
  8. * Last Modified: 04-15-2003
  9. *
  10. * Description:
  11. *
  12. * This file contains common network functions used in nrpe and check_nrpe.
  13. *
  14. * License Information:
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29. *
  30. ****************************************************************************/
  31. #include "../common/common.h"
  32. #include "utils.h"
  33. static unsigned long crc32_table[256];
  34. /* build the crc table - must be called before calculating the crc value */
  35. void generate_crc32_table(void){
  36. unsigned long crc, poly;
  37. int i, j;
  38. poly=0xEDB88320L;
  39. for(i=0;i<256;i++){
  40. crc=i;
  41. for(j=8;j>0;j--){
  42. if(crc & 1)
  43. crc=(crc>>1)^poly;
  44. else
  45. crc>>=1;
  46. }
  47. crc32_table[i]=crc;
  48. }
  49. return;
  50. }
  51. /* calculates the CRC 32 value for a buffer */
  52. unsigned long calculate_crc32(char *buffer, int buffer_size){
  53. register unsigned long crc;
  54. int this_char;
  55. int current_index;
  56. crc=0xFFFFFFFF;
  57. for(current_index=0;current_index<buffer_size;current_index++){
  58. this_char=(int)buffer[current_index];
  59. crc=((crc>>8) & 0x00FFFFFF) ^ crc32_table[(crc ^ this_char) & 0xFF];
  60. }
  61. return (crc ^ 0xFFFFFFFF);
  62. }
  63. /* fill a buffer with semi-random data */
  64. void randomize_buffer(char *buffer,int buffer_size){
  65. FILE *fp;
  66. int x;
  67. int seed;
  68. /**** FILL BUFFER WITH RANDOM ALPHA-NUMERIC CHARACTERS ****/
  69. /***************************************************************
  70. Only use alpha-numeric characters becase plugins usually
  71. only generate numbers and letters in their output. We
  72. want the buffer to contain the same set of characters as
  73. plugins, so its harder to distinguish where the real output
  74. ends and the rest of the buffer (padded randomly) starts.
  75. ***************************************************************/
  76. /* try to get seed value from /dev/urandom, as its a better source of entropy */
  77. fp=fopen("/dev/urandom","r");
  78. if(fp!=NULL){
  79. seed=fgetc(fp);
  80. fclose(fp);
  81. }
  82. /* else fallback to using the current time as the seed */
  83. else
  84. seed=(int)time(NULL);
  85. srand(seed);
  86. for(x=0;x<buffer_size;x++)
  87. buffer[x]=(int)'0'+(int)(72.0*rand()/(RAND_MAX+1.0));
  88. return;
  89. }
  90. /* opens a connection to a remote host/tcp port */
  91. int my_tcp_connect(char *host_name,int port,int *sd){
  92. int result;
  93. result=my_connect(host_name,port,sd,"tcp");
  94. return result;
  95. }
  96. /* opens a tcp or udp connection to a remote host */
  97. int my_connect(char *host_name,int port,int *sd,char *proto){
  98. struct sockaddr_in servaddr;
  99. struct hostent *hp;
  100. struct protoent *ptrp;
  101. int result;
  102. bzero((char *)&servaddr,sizeof(servaddr));
  103. servaddr.sin_family=AF_INET;
  104. servaddr.sin_port=htons(port);
  105. /* try to bypass using a DNS lookup if this is just an IP address */
  106. if(!my_inet_aton(host_name,&servaddr.sin_addr)){
  107. /* else do a DNS lookup */
  108. hp=gethostbyname((const char *)host_name);
  109. if(hp==NULL){
  110. printf("Invalid host name '%s'\n",host_name);
  111. return STATE_UNKNOWN;
  112. }
  113. memcpy(&servaddr.sin_addr,hp->h_addr,hp->h_length);
  114. }
  115. /* map transport protocol name to protocol number */
  116. if(((ptrp=getprotobyname(proto)))==NULL){
  117. printf("Cannot map \"%s\" to protocol number\n",proto);
  118. return STATE_UNKNOWN;
  119. }
  120. /* create a socket */
  121. *sd=socket(PF_INET,(!strcmp(proto,"udp"))?SOCK_DGRAM:SOCK_STREAM,ptrp->p_proto);
  122. if(*sd<0){
  123. printf("Socket creation failed\n");
  124. return STATE_UNKNOWN;
  125. }
  126. /* open a connection */
  127. result=connect(*sd,(struct sockaddr *)&servaddr,sizeof(servaddr));
  128. if(result<0){
  129. switch(errno){
  130. case ECONNREFUSED:
  131. printf("Connection refused by host\n");
  132. break;
  133. case ETIMEDOUT:
  134. printf("Timeout while attempting connection\n");
  135. break;
  136. case ENETUNREACH:
  137. printf("Network is unreachable\n");
  138. break;
  139. default:
  140. printf("Connection refused or timed out\n");
  141. }
  142. return STATE_CRITICAL;
  143. }
  144. return STATE_OK;
  145. }
  146. /* This code was taken from Fyodor's nmap utility, which was originally taken from
  147. the GLIBC 2.0.6 libraries because Solaris doesn't contain the inet_aton() funtion. */
  148. int my_inet_aton(register const char *cp, struct in_addr *addr){
  149. register unsigned int val; /* changed from u_long --david */
  150. register int base, n;
  151. register char c;
  152. u_int parts[4];
  153. register u_int *pp = parts;
  154. c=*cp;
  155. for(;;){
  156. /*
  157. * Collect number up to ``.''.
  158. * Values are specified as for C:
  159. * 0x=hex, 0=octal, isdigit=decimal.
  160. */
  161. if (!isdigit((int)c))
  162. return (0);
  163. val=0;
  164. base=10;
  165. if(c=='0'){
  166. c=*++cp;
  167. if(c=='x'||c=='X')
  168. base=16,c=*++cp;
  169. else
  170. base=8;
  171. }
  172. for(;;){
  173. if(isascii((int)c) && isdigit((int)c)){
  174. val=(val*base)+(c -'0');
  175. c=*++cp;
  176. }
  177. else if(base==16 && isascii((int)c) && isxdigit((int)c)){
  178. val=(val<<4) | (c+10-(islower((int)c)?'a':'A'));
  179. c = *++cp;
  180. }
  181. else
  182. break;
  183. }
  184. if(c=='.'){
  185. /*
  186. * Internet format:
  187. * a.b.c.d
  188. * a.b.c (with c treated as 16 bits)
  189. * a.b (with b treated as 24 bits)
  190. */
  191. if(pp>=parts+3)
  192. return (0);
  193. *pp++=val;
  194. c=*++cp;
  195. }
  196. else
  197. break;
  198. }
  199. /* Check for trailing characters */
  200. if(c!='\0' && (!isascii((int)c) || !isspace((int)c)))
  201. return (0);
  202. /* Concoct the address according to the number of parts specified */
  203. n=pp-parts+1;
  204. switch(n){
  205. case 0:
  206. return (0); /* initial nondigit */
  207. case 1: /* a -- 32 bits */
  208. break;
  209. case 2: /* a.b -- 8.24 bits */
  210. if(val>0xffffff)
  211. return (0);
  212. val|=parts[0]<<24;
  213. break;
  214. case 3: /* a.b.c -- 8.8.16 bits */
  215. if(val>0xffff)
  216. return (0);
  217. val|=(parts[0]<< 24) | (parts[1]<<16);
  218. break;
  219. case 4: /* a.b.c.d -- 8.8.8.8 bits */
  220. if(val>0xff)
  221. return (0);
  222. val|=(parts[0]<<24) | (parts[1]<<16) | (parts[2]<<8);
  223. break;
  224. }
  225. if(addr)
  226. addr->s_addr=htonl(val);
  227. return (1);
  228. }
  229. void strip(char *buffer){
  230. int x;
  231. int index;
  232. for(x=strlen(buffer);x>=1;x--){
  233. index=x-1;
  234. if(buffer[index]==' ' || buffer[index]=='\r' || buffer[index]=='\n' || buffer[index]=='\t')
  235. buffer[index]='\x0';
  236. else
  237. break;
  238. }
  239. return;
  240. }
  241. /* sends all data - thanks to Beej's Guide to Network Programming */
  242. int sendall(int s, char *buf, int *len){
  243. int total=0;
  244. int bytesleft=*len;
  245. int n=0;
  246. /* send all the data */
  247. while(total<*len){
  248. /* send some data */
  249. n=send(s,buf+total,bytesleft,0);
  250. /* break on error */
  251. if(n==-1)
  252. break;
  253. /* apply bytes we sent */
  254. total+=n;
  255. bytesleft-=n;
  256. }
  257. /* return number of bytes actually send here */
  258. *len=total;
  259. /* return -1 on failure, 0 on success */
  260. return n==-1?-1:0;
  261. }
  262. /* receives all data - modelled after sendall() */
  263. int recvall(int s, char *buf, int *len, int timeout){
  264. int total=0;
  265. int bytesleft=*len;
  266. int n=0;
  267. time_t start_time;
  268. time_t current_time;
  269. /* clear the receive buffer */
  270. bzero(buf,*len);
  271. time(&start_time);
  272. /* receive all data */
  273. while(total<*len){
  274. /* receive some data */
  275. n=recv(s,buf+total,bytesleft,0);
  276. /* no data has arrived yet (non-blocking socket) */
  277. if(n==-1 && errno==EAGAIN){
  278. time(&current_time);
  279. if(current_time-start_time>timeout)
  280. break;
  281. sleep(1);
  282. continue;
  283. }
  284. /* receive error or client disconnect */
  285. else if(n<=0)
  286. break;
  287. /* apply bytes we received */
  288. total+=n;
  289. bytesleft-=n;
  290. }
  291. /* return number of bytes actually received here */
  292. *len=total;
  293. /* return <=0 on failure, bytes received on success */
  294. return (n<=0)?n:total;
  295. }
  296. /* show license */
  297. void display_license(void){
  298. printf("This program is free software; you can redistribute it and/or modify\n");
  299. printf("it under the terms of the GNU General Public License as published by\n");
  300. printf("the Free Software Foundation; either version 2 of the License, or\n");
  301. printf("(at your option) any later version.\n\n");
  302. printf("This program is distributed in the hope that it will be useful,\n");
  303. printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
  304. printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
  305. printf("GNU General Public License for more details.\n\n");
  306. printf("You should have received a copy of the GNU General Public License\n");
  307. printf("along with this program; if not, write to the Free Software\n");
  308. printf("Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n\n");
  309. return;
  310. }