sarg  2.4.0
About: SARG ia a Squid Analysis Report Generator.
  Fossies Dox: sarg-2.4.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

ip2name_exec.c
Go to the documentation of this file.
1 /*
2  * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3  * 1998, 2015
4  *
5  * SARG donations:
6  * please look at http://sarg.sourceforge.net/donations.php
7  * Support:
8  * http://sourceforge.net/projects/sarg/forums/forum/363374
9  * ---------------------------------------------------------------------
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
24  *
25  */
26 
27 #include "include/conf.h"
28 #include "include/defs.h"
29 #include "include/ip2name.h"
30 
32 #define CMD_BUFFER_SIZE 2048
33 
34 static void ip2name_execconfig(const char *name,const char *param);
35 static enum ip2name_retcode ip2name_exec(char *ip,int ip_len);
36 
39 {
40  "dns",
41  NULL,//no next item yet
44 };
45 
46 static char ExecCmd[CMD_BUFFER_SIZE]="";
47 
55 static void ip2name_execconfig(const char *name,const char *param)
56 {
57  int len;
58 
59  len=strlen(param);
60  if (len>=sizeof(ExecCmd)) {
61  debuga(__FILE__,__LINE__,_("Command to execute to resolve the IP addresses is too long (maximum is %d bytes)\n"),(int)sizeof(ExecCmd));
62  exit(EXIT_FAILURE);
63  }
64  strcpy(ExecCmd,param);
65 }
66 
75 static enum ip2name_retcode ip2name_exec(char *ip,int ip_len)
76 {
77  char cmd[CMD_BUFFER_SIZE];
78  int i;
79  int j;
80  int len;
81  FILE *cmd_in;
82  char buffer[512];
83  size_t nread;
84 
85  if (ExecCmd[0]=='\0') {
86  debuga(__FILE__,__LINE__,_("No command to run to resolve an IP address. Please configure it in sarg.conf\n"));
87  exit(EXIT_FAILURE);
88  }
89 
90  j=0;
91  len=strlen(ip);
92  for (i=0 ; i<sizeof(ExecCmd) && ExecCmd[i] ; i++) {
93  if (ExecCmd[i]=='%' && strncmp(ExecCmd+i+1,"IP",2)==0) {
94  if (j+len>=sizeof(cmd)) {
95  debuga(__FILE__,__LINE__,_("IP address \"%s\" too long for the command to run\n"),ip);
96  return(INRC_Error);
97  }
98  strcpy(cmd+j,ip);
99  j+=len;
100  i+=2;
101  } else {
102  if (j>=sizeof(cmd)) {
103  debuga(__FILE__,__LINE__,_("IP address \"%s\" too long for the command to run\n"),ip);
104  return(INRC_Error);
105  }
106  cmd[j++]=ExecCmd[i];
107  }
108  }
109  cmd[j]='\0';
110 
111  cmd_in=popen(cmd,"r");
112  if (!cmd_in) {
113  debuga(__FILE__,__LINE__,_("Cannot run command %s\n"),cmd);
114  exit(EXIT_FAILURE);
115  }
116 
117  nread=fread(buffer,1,sizeof(buffer),cmd_in);
118 
119  if (pclose(cmd_in)==-1) {
120  debuga(__FILE__,__LINE__,_("Command failed: %s\n"),cmd);
121  exit(EXIT_FAILURE);
122  }
123 
124  if (nread==0) return(INRC_NotFound);
125 
126  safe_strcpy(ip,buffer,ip_len);
127  return(INRC_Found);
128 }
INRC_Found
@ INRC_Found
A match was found.
Definition: ip2name.h:12
Ip2NameProcess
Entry points of the ip2name modules.
Definition: ip2name.h:17
debuga
void debuga(const char *File, int Line, const char *msg,...)
Definition: util.c:601
CMD_BUFFER_SIZE
#define CMD_BUFFER_SIZE
The buffer size to store the command.
Definition: ip2name_exec.c:32
_
#define _(String)
Definition: conf.h:155
ip2name.h
name
char name[20000]
Definition: conf.h:335
ip2name_exec
static enum ip2name_retcode ip2name_exec(char *ip, int ip_len)
Definition: ip2name_exec.c:75
conf.h
Include headers and define global variables. */.
ip2name_retcode
ip2name_retcode
The possible return code of ip2name subfunctions.
Definition: ip2name.h:5
ExecCmd
static char ExecCmd[2048]
Definition: ip2name_exec.c:46
INRC_Error
@ INRC_Error
Error encountered during the processing.
Definition: ip2name.h:8
safe_strcpy
void safe_strcpy(char *dest, const char *src, int length)
Definition: util.c:1550
defs.h
Declaration of the structures and functions.
Ip2NameExec
struct Ip2NameProcess Ip2NameExec
The functions to resolve an IP address using an external executable.
Definition: ip2name_exec.c:38
cmd
char cmd[255]
Definition: conf.h:442
INRC_NotFound
@ INRC_NotFound
No match found.
Definition: ip2name.h:10
ip2name_execconfig
static void ip2name_execconfig(const char *name, const char *param)
Definition: ip2name_exec.c:55