]> git.ipfire.org Git - thirdparty/sarg.git/blob - ip2name_exec.c
Rename configure.in as configure.ac
[thirdparty/sarg.git] / ip2name_exec.c
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
31 //! The buffer size to store the command.
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
37 //! The functions to resolve an IP address using an external executable.
38 struct Ip2NameProcess Ip2NameExec=
39 {
40 "dns",
41 NULL,//no next item yet
42 ip2name_execconfig,
43 ip2name_exec
44 };
45
46 static char ExecCmd[CMD_BUFFER_SIZE]="";
47
48 /*!
49 Configure the module to resolve an IP address using an external program.
50
51 \param name The name of the module as invoked by the user in the configuration
52 file.
53 \param param The parameters passed to the module.
54 */
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
67 /*!
68 Run an external process to get the name of a computer.
69
70 \param ip The ip address.
71 \param ip_len The number of bytes in the IP address.
72
73 \return One of the ::ip2name_retcode value.
74 */
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 }