ze-filter  (ze-filter-0.8.0-develop-180218)
ze-unattach.c
Go to the documentation of this file.
1 /*
2  *
3  * ze-filter - Mail Server Filter for sendmail
4  *
5  * Copyright (c) 2001-2018 - Jose-Marcio Martins da Cruz
6  *
7  * Auteur : Jose Marcio Martins da Cruz
8  * jose.marcio.mc@gmail.org
9  *
10  * Historique :
11  * Creation : janvier 2002
12  *
13  * This program is free software, but with restricted license :
14  *
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.
19  *
20  * More details about ze-filter license can be found at ze-filter
21  * web site : http://foss.jose-marcio.org
22  */
23 
24 #include <ze-sys.h>
25 
26 #include "ze-filter.h"
27 
28 
29 #define JDEBUG 0
30 
31 /* ****************************************************************************
32  * *
33  * *
34  **************************************************************************** */
35 typedef struct
36 {
37  char *dir;
38 } DATA_T;
39 
40 
41 static bool
42 save_mime_part(buf, size, id, level, type, arg, mime_part)
43  char *buf;
44  size_t size;
45  char *id;
46  int level;
47  int type;
48  void *arg;
49  mime_part_T *mime_part;
50 {
51  DATA_T *data = arg;
52  char fname[PATH_MAX];
53  int fd;
54  size_t nbytes;
55 
56  char *prefix = NULL;
57 
58  mode_t mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
59 
60  if (arg == NULL)
61  return FALSE;
62 
63  if (type == MIME_TYPE_TEXT)
64  {
65  if (strspn(buf, " \t\r\n") == size)
66  return TRUE;
67  }
68 
69  prefix = data->dir;
70  if (prefix == NULL)
71  prefix = "/tmp/attachments";
72 
73  snprintf(fname, sizeof (fname), "%s/%s.XXXXXX", prefix, id);
74 
75  fd = mkstemp(fname);
76  if (fd < 0)
77  {
78  ZE_LogSysError("mkstemp %s", fname);
79  return FALSE;
80  }
81 
82  if (fchmod(fd, mode) < 0)
83  {
84  ZE_LogSysError("fchmod %s", fname);
85  close(fd);
86  return FALSE;
87  }
88 
89  if ((nbytes = write(fd, buf, size)) < size)
90  {
91  ZE_LogSysError("write %s", fname);
92  close(fd);
93  return FALSE;
94  }
95 
96  close(fd);
97 
98  ZE_LogMsgInfo(15, "FILENAME = %s", fname);
99 
100  return TRUE;
101 }
102 
103 
104 
105 /* ****************************************************************************
106  * *
107  * *
108  **************************************************************************** */
109 bool
110 unattach(id, fname, dirout, flags)
111  char *id;
112  char *fname;
113  char *dirout;
114  uint32_t *flags;
115 {
116  char prefix[PATH_MAX];
117  DATA_T data;
118 
119  if (fname == NULL)
120  return FALSE;
121 
122  /* shall see this... XXX */
123 
124 #if 0
125  if (dirout == NULL)
126  dirout = cf_get_str(CF_SPOOLDIR);
127  if (dirout == NULL)
128  dirout = J_SPOOLDIR;
129 #endif
130 
131  snprintf(prefix, sizeof (prefix), "%s/%s.dir", dirout, fname);
132 
133  memset(&data, 0, sizeof (data));
134  data.dir = prefix;
135 
136  if (mkdir(prefix, 0755) != 0)
137  {
138  if (errno != EEXIST)
139  {
140  ZE_LogSysError("mkdir %s ", prefix);
141  return FALSE;
142  }
143  }
144 
145  return decode_mime_file(id, fname, flags, save_mime_part, &data);
146 }
#define CF_SPOOLDIR
Definition: cfh-defs.h:68
#define ZE_LogMsgInfo(level,...)
Definition: zeSyslog.h:110
#define FALSE
Definition: macros.h:160
bool decode_mime_file(char *, char *, uint32_t *, demime_F, void *)
Definition: ze-demime.c:584
char * dir
Definition: ze-unattach.c:37
bool unattach(char *id, char *fname, char *dirout, uint32_t *flags)
Definition: ze-unattach.c:110
#define MIME_TYPE_TEXT
Definition: ze-demime.h:34
#define TRUE
Definition: macros.h:157
#define ZE_LogSysError(...)
Definition: zeSyslog.h:129
char * cf_get_str(int id)
Definition: ze-cf.c:854
unsigned int mode_t
Definition: ze-sys.h:519
long uint32_t
Definition: ze-sys.h:489