ze-filter  (ze-filter-0.8.0-develop-180218)
ze-scanfile.c
Go to the documentation of this file.
1 
2 /*
3  *
4  * ze-filter - Mail Server Filter for sendmail
5  *
6  * Copyright (c) 2001-2018 - Jose-Marcio Martins da Cruz
7  *
8  * Auteur : Jose Marcio Martins da Cruz
9  * jose.marcio.mc@gmail.org
10  *
11  * Historique :
12  * Creation : janvier 2002
13  *
14  * This program is free software, but with restricted license :
15  *
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  * More details about ze-filter license can be found at ze-filter
22  * web site : http://foss.jose-marcio.org
23  */
24 
25 #include <ze-sys.h>
26 
27 #include "ze-filter.h"
28 
29 /* ****************************************************************************
30  * *
31  * *
32  **************************************************************************** */
33 void usage();
34 
35 #define RES_OK 0
36 #define RES_SCAN_ERROR 1
37 #define RES_XFILE 2
38 #define RES_ERROR 3
39 
40 static bool scan_mbox(char *fname, int msgNb, void *arg);
41 
42 /* ****************************************************************************
43  * *
44  * *
45  **************************************************************************** */
46 static name2id_T labels[] = {
47  {"Content-Type", CT_TYPE},
48  {"Content-Disposition", CT_DISP},
49  {"UUencoded file", CT_UUFILE},
50  {NULL, -1}
51 };
52 
53 int
54 main(int argc, char **argv)
55 {
56  char *fname = "VIRUS_SIRCAM";
57  FILE *fin;
58 
59  const char *args = "hvc:";
60  int c;
61  int io;
62  int main_result = RES_OK;
63 
64  openlog("ze-scanfile", LOG_PID | LOG_NOWAIT | LOG_NDELAY, LOG_LOCAL5);
65  if (argc > 1)
66  fname = argv[1];
67 
69 
71 
72  cf_opt.arg_v = 0;
73 
74  while ((c = getopt(argc, argv, args)) != -1) {
75  switch (c) {
76  case 'h': /* OK */
77  cf_opt.arg_h = TRUE;
78  usage();
79  exit(RES_OK);
80  break;
81  case 'v':
82  cf_opt.arg_v++;
83  break;
84  /*
85  */
86  case 'c':
87  if (optarg == NULL || *optarg == '\0') {
88  (void) fprintf(stderr, "Erreur %s\n", optarg);
89  exit(RES_ERROR);
90  }
91  if (cf_opt.arg_c != NULL) {
92  ZE_MessageInfo(0, "Only one c option, please");
93  exit(RES_ERROR);
94  }
95  if ((cf_opt.arg_c = strdup(optarg)) == NULL) {
96  ZE_LogSysError("FATAL ERROR - memory allocation cf_opt.arg_c");
97  exit(RES_ERROR);
98  }
100  break;
101  }
102  }
103 
104  io = optind;
105 
106  while (io < argc && *argv[io] == '-')
107  io++;
108 
109  configure("ze-scanfile", conf_file, FALSE);
110 
111  {
112  int nbf = 0;
113 
114  while (io < argc) {
115  fname = argv[io++];
116  printf(" HANDLING %s\n", fname);
117  nbf += mbox_handle(fname, scan_mbox, NULL);
118  }
119  }
120 
121  return main_result;
122 }
123 
124 /* ****************************************************************************
125  * *
126  * *
127  **************************************************************************** */
128 static bool
129 scan_mbox(fname, msgNb, arg)
130  char *fname;
131  int msgNb;
132  void *arg;
133 {
134  FILE *fin = NULL;
135 
136  char chunk[0x10001];
137  char old[10000];
138 
139  int state = 0;
140  int nb;
141 
142  int result = RES_OK;
143  content_field_T content;
144  content_field_T *list, *p;
145 
146  char *ip = "0.0.0.0";
147  char *id = "00000000.000";
148  char bid[32];
149 
150  size_t fsize = 0;
151 
153 
154  if ((fin = fopen(fname, "r")) == NULL)
155  return RES_SCAN_ERROR;
156 
157  fsize = zeGetFileSize(fname);
158 
159  snprintf(bid, sizeof (bid), "%08X.000", msgNb);
160  id = bid;
161 
162  memset(&content, 0, sizeof (content));
163  list = NULL;
164 
165  memset(chunk, 0, sizeof (chunk));
166  memset(old, 0, sizeof (old));
167  while ((nb = fread(chunk, 1, sizeof (chunk) - 1, fin)) > 0) {
168  int res;
169 
170  res = scan_block(NULL, old, sizeof (old) - 1, chunk, nb, &state, &content,
171  &list);
172 
173  if (res != 0) {
174  printf(" scan_block res = %d\n", res);
175 #if 1
176  goto fin;
177 #else
178  exit(RES_SCAN_ERROR);
179 #endif
180  }
181  }
182 
183  if (cf_opt.arg_v > 1) {
184  p = list;
185 
186  while (p != NULL) {
187  int i;
188 
189  char *label[] = {
190  "",
191  "Content-Type",
192  "Content-Disposition",
193  "UUencoded file"
194  };
195 
196  printf("%s *** FIELD : %s\n", id, label[p->field_type]);
197  printf("%s VALEUR : %s\n", id, p->value ? p->value : "");
198  for (i = 0; i < NB_ATTR; i++) {
199  if (p->attr[i].name) {
200  printf("%s ATTR[%2d] : %s\n", id, i, p->attr[i].name);
201  if (p->attr[i].value)
202  printf("%s DATA[%2d] : %s\n", id, i, p->attr[i].value);
203  }
204  }
205  p = p->next;
206  }
207  }
208 
209  {
210  int i = 0, nb = 0;
211  attachment_T *ahead = NULL, *p;
212 
213  extract_attachments(list, &ahead);
214  p = ahead;
215 
216  while (p) {
217  char *svirus;
218 
219  nb++;
220  if (!p->xfile)
221  p->xfile = check_xfiles(p->name, p->mimetype, 0, NULL, 0);
222 
223  svirus = STRBOOL(p->xfile, "XFILE", ".....");
224  if (p->xfile)
225  i++;
226 
227  if (cf_opt.arg_v > 0 || p->xfile) {
228  printf("%s ATTACHED FILE (%7ld) (%-5s) : %-10s %-30s %-15s\n",
229  id, (long int) fsize, svirus,
230  STREMPTY(p->disposition, "..."),
231  STREMPTY(p->mimetype, "..."), p->name);
232  }
233  p = p->next;
234  }
235  if (i > 0)
236  result = RES_XFILE;
237  if (cf_opt.arg_v > 1)
238  printf("%s %4d FILES - %4d XFILES\n\n", id, nb, i++);
239  }
241  list = NULL;
242 
243 fin:
244  fclose(fin);
245 
246  return result;
247 }
248 
249 /* ****************************************************************************
250  * *
251  * *
252  **************************************************************************** */
253 void
255 {
256  printf("Usage : ze-scanfile [-h] [-c] [-v] file file file...\n"
257  " %s\n"
258  " Compiled on %s %s\n"
259  " -h : help\n"
260  " -c : configuration file\n"
261  " -v : verbose\n"
262  "\n %s \n " COPYRIGHT "\n\n",
263  PACKAGE, __DATE__, __TIME__, PACKAGE);
264 }
#define SHOW_CURSOR(zero)
Definition: macros.h:249
void usage()
Definition: ze-scanfile.c:254
#define RES_ERROR
Definition: ze-scanfile.c:38
#define STRBOOL(x, t, f)
Definition: macros.h:87
#define CT_TYPE
Definition: ze-scanmail.h:32
int scan_block(char *, char *, long, char *, long, int *, content_field_T *, content_field_T **)
Definition: ze-scanmail.c:51
#define COPYRIGHT
Definition: version.h:31
#define RES_XFILE
Definition: ze-scanfile.c:37
void zeLog_SetOutput(bool, bool)
Definition: zeSyslog.c:490
#define FALSE
Definition: macros.h:160
size_t zeGetFileSize(char *)
Definition: zeFileTools.c:132
content_field_T * next
Definition: ze-mimelist.h:57
char * arg_c
Definition: ze-config.h:47
int mbox_handle(char *fname, mbox_F func, void *arg)
Definition: ze-mbox.c:34
#define RES_SCAN_ERROR
Definition: ze-scanfile.c:36
void init_default_file_extensions()
Definition: ze-fileexp.c:195
bool arg_h
Definition: ze-config.h:38
int extract_attachments(content_field_T *, attachment_T **)
Definition: ze-mimelist.c:330
int arg_v
Definition: ze-config.h:39
#define ZE_MessageInfo(level,...)
Definition: zeSyslog.h:90
int nb
Definition: ze-connopen.c:61
#define NB_ATTR
Definition: ze-mimelist.h:48
#define TRUE
Definition: macros.h:157
int main(int argc, char **argv)
Definition: ze-scanfile.c:54
content_text_T attr[NB_ATTR]
Definition: ze-mimelist.h:56
OPT_REC_T cf_opt
Definition: ze-config.c:40
#define ZE_LogSysError(...)
Definition: zeSyslog.h:129
char * conf_file
Definition: ze-cf.c:38
#define STREMPTY(x, r)
Definition: macros.h:82
#define CT_UUFILE
Definition: ze-scanmail.h:34
int configure(char *, char *, bool)
Definition: ze-cf.c:1203
void free_content_field_list(content_field_T *)
Definition: ze-mimelist.c:201
#define PACKAGE
Definition: version.h:28
bool check_xfiles(char *, char *, size_t, char *, size_t)
Definition: ze-fileexp.c:625
#define RES_OK
Definition: ze-scanfile.c:35
#define CT_DISP
Definition: ze-scanmail.h:33