]>
Commit | Line | Data |
---|---|---|
1e312c82 FM |
1 | /* |
2 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net | |
110ce984 | 3 | * 1998, 2015 |
1e312c82 FM |
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; | |
bd43d81f | 58 | |
1e312c82 FM |
59 | len=strlen(param); |
60 | if (len>=sizeof(ExecCmd)) { | |
af961877 | 61 | debuga(__FILE__,__LINE__,_("Command to execute to resolve the IP addresses is too long (maximum is %d bytes)\n"),(int)sizeof(ExecCmd)); |
1e312c82 FM |
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; | |
bd43d81f | 84 | |
1e312c82 | 85 | if (ExecCmd[0]=='\0') { |
af961877 | 86 | debuga(__FILE__,__LINE__,_("No command to run to resolve an IP address. Please configure it in sarg.conf\n")); |
1e312c82 FM |
87 | exit(EXIT_FAILURE); |
88 | } | |
bd43d81f | 89 | |
1e312c82 FM |
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)) { | |
af961877 | 95 | debuga(__FILE__,__LINE__,_("IP address \"%s\" too long for the command to run\n"),ip); |
1e312c82 FM |
96 | return(INRC_Error); |
97 | } | |
98 | strcpy(cmd+j,ip); | |
99 | j+=len; | |
100 | i+=2; | |
101 | } else { | |
102 | if (j>=sizeof(cmd)) { | |
af961877 | 103 | debuga(__FILE__,__LINE__,_("IP address \"%s\" too long for the command to run\n"),ip); |
1e312c82 FM |
104 | return(INRC_Error); |
105 | } | |
106 | cmd[j++]=ExecCmd[i]; | |
107 | } | |
108 | } | |
109 | cmd[j]='\0'; | |
bd43d81f | 110 | |
1e312c82 FM |
111 | cmd_in=popen(cmd,"r"); |
112 | if (!cmd_in) { | |
af961877 | 113 | debuga(__FILE__,__LINE__,_("Cannot run command %s\n"),cmd); |
1e312c82 FM |
114 | exit(EXIT_FAILURE); |
115 | } | |
bd43d81f | 116 | |
1e312c82 | 117 | nread=fread(buffer,1,sizeof(buffer),cmd_in); |
bd43d81f | 118 | |
1e312c82 | 119 | if (pclose(cmd_in)==-1) { |
af961877 | 120 | debuga(__FILE__,__LINE__,_("Command failed: %s\n"),cmd); |
1e312c82 FM |
121 | exit(EXIT_FAILURE); |
122 | } | |
123 | ||
124 | if (nread==0) return(INRC_NotFound); | |
bd43d81f | 125 | |
1e312c82 FM |
126 | safe_strcpy(ip,buffer,ip_len); |
127 | return(INRC_Found); | |
128 | } |