1 /***************************************************************************
2                           utils.c  -  description
3                              -------------------
4     begin                : Wed May 21 2003
5     copyright            : (C) 2003 by Spencer Hardy
6     email                : diceman@dircon.co.uk
7  ***************************************************************************/
8 
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  ***************************************************************************/
17 
18 
19 #include "utils.h"
20 #include "relay.h"
21 #include "options.h"
22 
23 extern struct options o;
24 extern struct flags f;
25 
26 extern host **hosts;
27 
28 
29 /*
30  * Safe malloc routine. checks for errors etc..
31  *
32  */
33 
s_malloc(unsigned long size)34 void *s_malloc(unsigned long size) {
35 
36   void *mymem;
37   if(size < 0) {
38     fatal("Tried to allocate a negative amount of memory!!");
39   }
40   mymem=malloc(size);
41   if(mymem == NULL) {
42     fatal("Malloc failed! Probably out of memory!!");
43   }
44 
45   return(mymem);
46 
47 }
48 
49 /*
50  * Safe calloc routine, checks for errors and zeroes memory.
51  *
52  */
53 
s_zmalloc(int size)54 void *s_zmalloc(int size) {
55 
56   void *mymem;
57   if(size < 0) {
58     fatal("Tried to allocate a negative amount of memory!!");
59   }
60   mymem=calloc(1, size);
61   if(mymem == NULL) {
62     fatal("Malloc failed! Probably out of memory!!");
63   }
64 
65   return(mymem);
66 
67 }
68 
69 
70 /*
71  * Like the perl equivilent, it removes the trailing
72  * carraige return and or newline.
73  *
74  */
75 
chomp(char * string)76 char *chomp(char *string) {
77 
78   int len;
79 
80   len = strlen(string);
81   if(len < 1) {
82     return(string);
83   }
84   if(string[len -1] != '\n' && string[len -1] != '\r') {
85     return(string);
86   }
87   if(len > 1 && string[len-2] == '\r') {
88     string[len-2] = '\0';
89   }
90   else {
91     string[len-1] = '\0';
92   }
93 
94   return(string);
95 
96 }
97 
98 
99 /*
100  * This funtion swaps out the macros for the equivelent text
101  *
102  */
103 
swap_rule(char * dest,const char * rule,const int hostn)104 void swap_rule(char *dest, const char *rule, const int hostn) {
105 
106 	strncpy(dest, rule, MAXDATA);
107 	dest[MAXDATA] = '\0';
108 
109 	if(strstr(rule, "--NAME--") != NULL) {
110 		swap_macro(dest, o.name, "--NAME--");
111 	}
112 
113 	if(strstr(rule, "--DOMAIN--") != NULL) {
114     swap_macro(dest, o.domain, "--DOMAIN--");
115   }
116 
117 	if(strstr(rule, "--IP_ADDRESS--") != NULL) {
118     swap_macro(dest, hosts[hostn]->ip_address, "--IP_ADDRESS--");
119   }
120 
121 	if(strstr(rule, "--HOSTNAME--") != NULL && f.resolve_hostnames && hosts[hostn]->resolved) {
122     swap_macro(dest, hosts[hostn]->hostname, "--HOSTNAME--");
123   }
124 	else if(strstr(rule, "--HOSTNAME--") != NULL && f.resolve_hostnames) {
125 		swap_macro(dest, hosts[hostn]->ip_address, "--HOSTNAME--");
126   }
127 
128 
129 	return;
130 
131 }
132 
133 /*
134  * This is the guts of the swapping
135  *
136  */
137 
swap_macro(char * string,const char * replace,const char * macro)138 void swap_macro(char *string, const char *replace, const char *macro) {
139 
140 	char *p;
141 	char *buf;
142 
143 	buf=s_malloc((MAXDATA + 1) * sizeof(char));
144 
145 	while((p = strstr(string, macro)) != NULL) {
146 		memcpy(buf, string, p - string);
147     buf[p - string] = '\0';
148     p += strlen(macro);
149     strncat(buf, replace, MAXDATA);
150     strncat(buf, p, MAXDATA);
151 		buf[MAXDATA] = '\0';
152 		strncpy(string, buf, MAXDATA);
153 		string[MAXDATA] = '\0';
154   }
155 
156 	free(buf);
157 
158 	return;
159 
160 }
161 
162 
163 /*
164  * This just added the "MAIL FROM:" etc to the
165  * beginning of the lines
166  *
167  */
168 
convert_rule(char * string,int ind)169 void convert_rule(char *string, int ind) {
170 
171 	char *buf;
172 
173 	buf=s_malloc((MAXDATA+1) * sizeof(char));
174 
175 	if(ind == 1) {
176 		snprintf(buf, MAXDATA, "HELO %s\r\n", string);
177 		strncpy(string, buf, MAXDATA);
178 		string[MAXDATA] = '\0';
179 	}
180 	else if(ind == 2) {
181 		snprintf(buf, MAXDATA, "MAIL FROM:%s\r\n", string);
182     strncpy(string, buf, MAXDATA);
183     string[MAXDATA] = '\0';
184   }
185   else if(ind == 3) {
186 		snprintf(buf, MAXDATA, "RCPT TO:%s\r\n", string);
187     strncpy(string, buf, MAXDATA);
188     string[MAXDATA] = '\0';
189   }
190 
191 	return;
192 
193 }
194 
195 
196 /*
197  * prints debugging information only if the
198  * debugging flag is set
199  *
200  */
201 
debug(char * fmt,...)202 void debug(char *fmt, ...) {
203 
204   if(f.debug) {
205 		va_list ap;
206   	va_start(ap, fmt);
207  		fflush(stdout);
208 		fprintf(stdout, "DEBUG: ");
209   	vfprintf(stderr, fmt, ap);
210   	fprintf(stderr, "\n");
211   	va_end(ap);
212 	}
213 
214   return;
215 
216 }
217 
218 /*
219  * Checks the output file by making sure it
220  * can open it ok... It also saves the path
221  * of the file in the path variable. If there
222  * is no path then it just saves "./" as the path
223  *
224  */
225 
check_file_path(char * filename,char * path)226 int check_file_path(char *filename, char *path) {
227 
228   char *s;
229   char *p;
230   int slash_flag = 0;
231   char *fn;
232 
233   FILE *fd;
234 
235 
236   if((fd=fopen(filename, "w")) == NULL) {
237     return(-1);
238   }
239 
240   fclose(fd);
241 
242   fn=s_malloc((strlen(filename)+1) * sizeof(char));
243   strncpy(fn, filename, strlen(filename));
244   fn[strlen(filename)] = '\0';
245 
246 
247   s=fn;
248 
249   while(*s++) {
250     if(*s == '/') {
251       p = s;
252       slash_flag = 1;
253     }
254   }
255 
256   *++p = '\0';
257 
258    if(!slash_flag) {
259     path = s_malloc((strlen(SLASHDOT) +2) * sizeof(char));
260     strncpy(path, SLASHDOT, strlen(SLASHDOT));
261     path[strlen(SLASHDOT)] = '\0';
262   }
263   else {
264     strncpy(path, fn, MAXDATA);
265     path[MAXDATA] = '\0';
266   }
267 
268 
269   free(fn);
270 
271   return(0);
272 
273 }
274 
275 
return_data(long cur_host,int rule,char * string)276 void return_data(long cur_host, int rule, char *string) {
277 
278   char *buf;
279 
280   buf=s_zmalloc((MAXDATA+1) * sizeof(char));
281   snprintf(buf, MAXDATA, "Subject: %s\r\n%s\r\n\r\n:SmtpRCKey:%d:%s:%d:\r\n\r\n.\r\n", o.email_subject, o.email, (int) o.time, hosts[cur_host]->ip_address, rule);
282 
283   strncpy(string, buf, MAXDATA);
284   string[MAXDATA] = '\0';
285 
286 }
287 
set_defaults(void)288 void set_defaults(void) {
289 
290 	o.number_of_threads = THREAD_DEFAULT;
291 	o.c_timeout = CTIMEOUT;
292 	o.r_timeout = RTIMEOUT;
293 	o.m_timeout = MTIMEOUT;
294 
295 	o.config_file=s_malloc((strlen(CONFIG_FILE)+1) * sizeof(char));
296 	strncpy(o.config_file, CONFIG_FILE, strlen(CONFIG_FILE));
297 	o.config_file[strlen(CONFIG_FILE)] = '\0';
298 	f.config_file = TRUE;
299 
300 	o.email_template=s_malloc((strlen(EMAIL_TEMPLATE)+1) * sizeof(char));
301 	strncpy(o.email_template, EMAIL_TEMPLATE, strlen(EMAIL_TEMPLATE));
302 	o.email_template[strlen(EMAIL_TEMPLATE)] = '\0';
303 	f.email_template = TRUE;
304 
305 }
306 
not_implemented(void)307 void not_implemented(void) {
308 
309 	fatal("Sorry, This function is not yet implemented");
310 
311 }
312 
313 
314