Mercurial > masqmail
comparison src/spool.c @ 10:26e34ae9a3e3
changed indention and line wrapping to a more consistent style
author | meillo@marmaro.de |
---|---|
date | Mon, 27 Oct 2008 16:23:10 +0100 |
parents | 08114f7dcc23 |
children | f671821d8222 |
comparison
equal
deleted
inserted
replaced
9:31cc8a89cb74 | 10:26e34ae9a3e3 |
---|---|
18 | 18 |
19 #include "masqmail.h" | 19 #include "masqmail.h" |
20 #include <sys/stat.h> | 20 #include <sys/stat.h> |
21 #include "dotlock.h" | 21 #include "dotlock.h" |
22 | 22 |
23 static | 23 static gint |
24 gint read_line(FILE *in, gchar *buf, gint buf_len) | 24 read_line(FILE * in, gchar * buf, gint buf_len) |
25 { | 25 { |
26 gint p = 0; | 26 gint p = 0; |
27 gint c; | 27 gint c; |
28 | 28 |
29 while((c = getc(in)) != '\n' && (c != EOF)){ | 29 while ((c = getc(in)) != '\n' && (c != EOF)) { |
30 if(p >= buf_len-1) { return 0; } | 30 if (p >= buf_len - 1) { |
31 buf[p++] = c; | 31 return 0; |
32 } | 32 } |
33 | 33 buf[p++] = c; |
34 if(c == EOF){ | 34 } |
35 return -1; | 35 |
36 } | 36 if (c == EOF) { |
37 if((p > 0) && (buf[p-1] == '\r')) | 37 return -1; |
38 p--; | 38 } |
39 buf[p++] = '\n'; | 39 if ((p > 0) && (buf[p - 1] == '\r')) |
40 buf[p] = 0; | 40 p--; |
41 | 41 buf[p++] = '\n'; |
42 return p; | 42 buf[p] = 0; |
43 } | 43 |
44 | 44 return p; |
45 static | 45 } |
46 void spool_write_rcpt(FILE *out, address *rcpt) | 46 |
47 { | 47 static void |
48 gchar dlvrd_char = addr_is_delivered(rcpt) ? 'X' : (addr_is_failed(rcpt) ? 'F' : ' '); | 48 spool_write_rcpt(FILE * out, address * rcpt) |
49 | 49 { |
50 if(rcpt->local_part[0] != '|'){ | 50 gchar dlvrd_char = addr_is_delivered(rcpt) ? 'X' : (addr_is_failed(rcpt) ? 'F' : ' '); |
51 /* this is a paranoid check, in case it slipped through: */ | 51 |
52 /* if this happens, it is a bug */ | 52 if (rcpt->local_part[0] != '|') { |
53 if(rcpt->domain == NULL){ | 53 /* this is a paranoid check, in case it slipped through: */ |
54 logwrite(LOG_WARNING, "BUG: null domain for address %s, setting to %s\n", | 54 /* if this happens, it is a bug */ |
55 rcpt->local_part, conf.host_name); | 55 if (rcpt->domain == NULL) { |
56 logwrite(LOG_WARNING, "please report this bug.\n"); | 56 logwrite(LOG_WARNING, "BUG: null domain for address %s, setting to %s\n", rcpt->local_part, conf.host_name); |
57 rcpt->domain = g_strdup(conf.host_name); | 57 logwrite(LOG_WARNING, "please report this bug.\n"); |
58 } | 58 rcpt->domain = g_strdup(conf.host_name); |
59 fprintf(out, "RT:%c%s\n", dlvrd_char, addr_string(rcpt)); | 59 } |
60 }else{ | 60 fprintf(out, "RT:%c%s\n", dlvrd_char, addr_string(rcpt)); |
61 fprintf(out, "RT:%c%s\n", dlvrd_char, rcpt->local_part); | 61 } else { |
62 } | 62 fprintf(out, "RT:%c%s\n", dlvrd_char, rcpt->local_part); |
63 } | 63 } |
64 | 64 } |
65 static | 65 |
66 address *spool_scan_rcpt(gchar *line) | 66 static address* |
67 { | 67 spool_scan_rcpt(gchar * line) |
68 address *rcpt = NULL; | 68 { |
69 | 69 address *rcpt = NULL; |
70 if(line[3] != 0){ | 70 |
71 if(line[4] != '|'){ | 71 if (line[3] != 0) { |
72 rcpt = create_address(&(line[4]), TRUE); | 72 if (line[4] != '|') { |
73 }else{ | 73 rcpt = create_address(&(line[4]), TRUE); |
74 rcpt = create_address_pipe(&(line[4])); | 74 } else { |
75 } | 75 rcpt = create_address_pipe(&(line[4])); |
76 if(line[3] == 'X'){ | 76 } |
77 addr_mark_delivered(rcpt); | 77 if (line[3] == 'X') { |
78 }else if(line[3] == 'F'){ | 78 addr_mark_delivered(rcpt); |
79 addr_mark_failed(rcpt); | 79 } else if (line[3] == 'F') { |
80 } | 80 addr_mark_failed(rcpt); |
81 } | 81 } |
82 return rcpt; | 82 } |
83 } | 83 return rcpt; |
84 | 84 } |
85 gboolean spool_read_data(message *msg) | 85 |
86 { | 86 gboolean |
87 FILE *in; | 87 spool_read_data(message * msg) |
88 gboolean ok = FALSE; | 88 { |
89 gchar *spool_file; | 89 FILE *in; |
90 | 90 gboolean ok = FALSE; |
91 DEBUG(5) debugf("spool_read_data entered\n"); | 91 gchar *spool_file; |
92 spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid); | 92 |
93 DEBUG(5) debugf("reading data spool file '%s'\n", spool_file); | 93 DEBUG(5) debugf("spool_read_data entered\n"); |
94 if((in = fopen(spool_file, "r"))){ | 94 spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid); |
95 char buf[MAX_DATALINE]; | 95 DEBUG(5) debugf("reading data spool file '%s'\n", spool_file); |
96 int len; | 96 if ((in = fopen(spool_file, "r"))) { |
97 | 97 char buf[MAX_DATALINE]; |
98 /* msg uid */ | 98 int len; |
99 read_line(in, buf, MAX_DATALINE); | 99 |
100 | 100 /* msg uid */ |
101 /* data */ | 101 read_line(in, buf, MAX_DATALINE); |
102 msg->data_list = NULL; | 102 |
103 while((len = read_line(in, buf, MAX_DATALINE)) > 0){ | 103 /* data */ |
104 msg->data_list = g_list_prepend(msg->data_list, g_strdup(buf)); | 104 msg->data_list = NULL; |
105 } | 105 while ((len = read_line(in, buf, MAX_DATALINE)) > 0) { |
106 msg->data_list = g_list_reverse(msg->data_list); | 106 msg->data_list = g_list_prepend(msg->data_list, g_strdup(buf)); |
107 fclose(in); | 107 } |
108 ok = TRUE; | 108 msg->data_list = g_list_reverse(msg->data_list); |
109 }else | 109 fclose(in); |
110 logwrite(LOG_ALERT, "could not open spool data file %s: %s\n", | 110 ok = TRUE; |
111 spool_file, strerror(errno)); | 111 } else |
112 return ok; | 112 logwrite(LOG_ALERT, "could not open spool data file %s: %s\n", spool_file, strerror(errno)); |
113 } | 113 return ok; |
114 | 114 } |
115 gboolean spool_read_header(message *msg) | 115 |
116 { | 116 gboolean |
117 FILE *in; | 117 spool_read_header(message * msg) |
118 gboolean ok = FALSE; | 118 { |
119 gchar *spool_file; | 119 FILE *in; |
120 | 120 gboolean ok = FALSE; |
121 /* header spool: */ | 121 gchar *spool_file; |
122 spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid); | 122 |
123 if((in = fopen(spool_file, "r"))){ | 123 /* header spool: */ |
124 header *hdr = NULL; | 124 spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid); |
125 char buf[MAX_DATALINE]; | 125 if ((in = fopen(spool_file, "r"))) { |
126 int len; | 126 header *hdr = NULL; |
127 | 127 char buf[MAX_DATALINE]; |
128 /* msg uid */ | 128 int len; |
129 read_line(in, buf, MAX_DATALINE); | 129 |
130 | 130 /* msg uid */ |
131 /* envelope header */ | 131 read_line(in, buf, MAX_DATALINE); |
132 while((len = read_line(in, buf, MAX_DATALINE)) > 0){ | 132 |
133 if(buf[0] == '\n') | 133 /* envelope header */ |
134 break; | 134 while ((len = read_line(in, buf, MAX_DATALINE)) > 0) { |
135 else if(strncasecmp(buf, "MF:", 3) == 0){ | 135 if (buf[0] == '\n') |
136 msg->return_path = create_address(&(buf[3]), TRUE); | 136 break; |
137 DEBUG(3) debugf("spool_read: MAIL FROM: %s", | 137 else if (strncasecmp(buf, "MF:", 3) == 0) { |
138 msg->return_path->address); | 138 msg->return_path = create_address(&(buf[3]), TRUE); |
139 }else if(strncasecmp(buf, "RT:", 3) == 0){ | 139 DEBUG(3) debugf("spool_read: MAIL FROM: %s", msg->return_path->address); |
140 address *addr; | 140 } else if (strncasecmp(buf, "RT:", 3) == 0) { |
141 addr = spool_scan_rcpt(buf); | 141 address *addr; |
142 if(!addr_is_delivered(addr) && !addr_is_failed(addr)){ | 142 addr = spool_scan_rcpt(buf); |
143 msg->rcpt_list = g_list_append(msg->rcpt_list, addr); | 143 if (!addr_is_delivered(addr) && !addr_is_failed(addr)) { |
144 }else{ | 144 msg->rcpt_list = g_list_append(msg->rcpt_list, addr); |
145 msg->non_rcpt_list = g_list_append(msg->non_rcpt_list, addr); | 145 } else { |
146 } | 146 msg->non_rcpt_list = g_list_append(msg->non_rcpt_list, addr); |
147 }else if(strncasecmp(buf, "PR:", 3) == 0){ | 147 } |
148 prot_id i; | 148 } else if (strncasecmp(buf, "PR:", 3) == 0) { |
149 for(i = 0; i < PROT_NUM; i++){ | 149 prot_id i; |
150 if(strncasecmp(prot_names[i], &(buf[3]), | 150 for (i = 0; i < PROT_NUM; i++) { |
151 strlen(prot_names[i])) == 0){ | 151 if (strncasecmp(prot_names[i], &(buf[3]), strlen(prot_names[i])) == 0) { |
152 break; | 152 break; |
153 } | 153 } |
154 } | 154 } |
155 msg->received_prot = i; | 155 msg->received_prot = i; |
156 }else if(strncasecmp(buf, "RH:", 3) == 0){ | 156 } else if (strncasecmp(buf, "RH:", 3) == 0) { |
157 g_strchomp(buf); | 157 g_strchomp(buf); |
158 msg->received_host = g_strdup(&(buf[3])); | 158 msg->received_host = g_strdup(&(buf[3])); |
159 }else if(strncasecmp(buf, "ID:", 3) == 0){ | 159 } else if (strncasecmp(buf, "ID:", 3) == 0) { |
160 g_strchomp(buf); | 160 g_strchomp(buf); |
161 msg->ident = g_strdup(&(buf[3])); | 161 msg->ident = g_strdup(&(buf[3])); |
162 }else if(strncasecmp(buf, "DS:", 3) == 0){ | 162 } else if (strncasecmp(buf, "DS:", 3) == 0) { |
163 msg->data_size = atoi(&(buf[3])); | 163 msg->data_size = atoi(&(buf[3])); |
164 }else if(strncasecmp(buf, "TR:", 3) == 0){ | 164 } else if (strncasecmp(buf, "TR:", 3) == 0) { |
165 msg->received_time = (time_t)(atoi(&(buf[3]))); | 165 msg->received_time = (time_t) (atoi(&(buf[3]))); |
166 }else if(strncasecmp(buf, "TW:", 3) == 0){ | 166 } else if (strncasecmp(buf, "TW:", 3) == 0) { |
167 msg->warned_time = (time_t)(atoi(&(buf[3]))); | 167 msg->warned_time = (time_t) (atoi(&(buf[3]))); |
168 } | 168 } |
169 /* so far ignore other tags */ | 169 /* so far ignore other tags */ |
170 } | 170 } |
171 | 171 |
172 /* mail headers */ | 172 /* mail headers */ |
173 while((len = read_line(in, buf, MAX_DATALINE)) > 0){ | 173 while ((len = read_line(in, buf, MAX_DATALINE)) > 0) { |
174 if(strncasecmp(buf, "HD:", 3) == 0){ | 174 if (strncasecmp(buf, "HD:", 3) == 0) { |
175 hdr = get_header(&(buf[3])); | 175 hdr = get_header(&(buf[3])); |
176 msg->hdr_list = g_list_append(msg->hdr_list, hdr); | 176 msg->hdr_list = g_list_append(msg->hdr_list, hdr); |
177 }else if((buf[0] == ' ' || buf[0] == '\t') && hdr){ | 177 } else if ((buf[0] == ' ' || buf[0] == '\t') && hdr) { |
178 char *tmp = hdr->header; | 178 char *tmp = hdr->header; |
179 /* header continuation */ | 179 /* header continuation */ |
180 hdr->header = g_strconcat(hdr->header, buf, NULL); | 180 hdr->header = g_strconcat(hdr->header, buf, NULL); |
181 hdr->value = hdr->header + (hdr->value - tmp); | 181 hdr->value = hdr->header + (hdr->value - tmp); |
182 }else | 182 } else |
183 break; | 183 break; |
184 } | 184 } |
185 fclose(in); | 185 fclose(in); |
186 ok = TRUE; | 186 ok = TRUE; |
187 }else | 187 } else |
188 logwrite(LOG_ALERT, "could not open spool header file %s: %s\n", | 188 logwrite(LOG_ALERT, "could not open spool header file %s: %s\n", spool_file, strerror(errno)); |
189 spool_file, strerror(errno)); | 189 return ok; |
190 return ok; | 190 } |
191 } | 191 |
192 | 192 message* |
193 message *msg_spool_read(gchar *uid, gboolean do_readdata) | 193 msg_spool_read(gchar * uid, gboolean do_readdata) |
194 { | 194 { |
195 message *msg; | 195 message *msg; |
196 gboolean ok = FALSE; | 196 gboolean ok = FALSE; |
197 | 197 |
198 msg = create_message(); | 198 msg = create_message(); |
199 msg->uid = g_strdup(uid); | 199 msg->uid = g_strdup(uid); |
200 | 200 |
201 /* header spool: */ | 201 /* header spool: */ |
202 ok = spool_read_header(msg); | 202 ok = spool_read_header(msg); |
203 if(ok && do_readdata){ | 203 if (ok && do_readdata) { |
204 /* data spool: */ | 204 /* data spool: */ |
205 ok = spool_read_data(msg); | 205 ok = spool_read_data(msg); |
206 } | 206 } |
207 return msg; | 207 return msg; |
208 } | 208 } |
209 | 209 |
210 /* write header. uid and gid should already be set to the | 210 /* write header. uid and gid should already be set to the |
211 mail ids. Better call spool_write(msg, FALSE). | 211 mail ids. Better call spool_write(msg, FALSE). |
212 */ | 212 */ |
213 static | 213 static gboolean |
214 gboolean spool_write_header(message *msg) | 214 spool_write_header(message * msg) |
215 { | 215 { |
216 GList *node; | 216 GList *node; |
217 gchar *spool_file, *tmp_file; | 217 gchar *spool_file, *tmp_file; |
218 FILE *out; | 218 FILE *out; |
219 gboolean ok = TRUE; | 219 gboolean ok = TRUE; |
220 | 220 |
221 /* header spool: */ | 221 /* header spool: */ |
222 tmp_file = g_strdup_printf("%s/input/%d-H.tmp", conf.spool_dir, getpid()); | 222 tmp_file = g_strdup_printf("%s/input/%d-H.tmp", conf.spool_dir, getpid()); |
223 DEBUG(4) debugf("tmp_file = %s\n", tmp_file); | 223 DEBUG(4) debugf("tmp_file = %s\n", tmp_file); |
224 | 224 |
225 if((out = fopen(tmp_file, "w"))){ | 225 if ((out = fopen(tmp_file, "w"))) { |
226 DEBUG(6) debugf("opened tmp_file %s\n", tmp_file); | 226 DEBUG(6) debugf("opened tmp_file %s\n", tmp_file); |
227 | 227 |
228 fprintf(out, "%s\n", msg->uid); | 228 fprintf(out, "%s\n", msg->uid); |
229 fprintf(out, "MF:%s\n", addr_string(msg->return_path)); | 229 fprintf(out, "MF:%s\n", addr_string(msg->return_path)); |
230 | 230 |
231 DEBUG(6) debugf("after MF\n"); | 231 DEBUG(6) debugf("after MF\n"); |
232 foreach(msg->rcpt_list, node){ | 232 foreach(msg->rcpt_list, node) { |
233 address *rcpt = (address *)(node->data); | 233 address *rcpt = (address *) (node->data); |
234 spool_write_rcpt(out, rcpt); | 234 spool_write_rcpt(out, rcpt); |
235 } | 235 } |
236 foreach(msg->non_rcpt_list, node){ | 236 foreach(msg->non_rcpt_list, node) { |
237 address *rcpt = (address *)(node->data); | 237 address *rcpt = (address *) (node->data); |
238 spool_write_rcpt(out, rcpt); | 238 spool_write_rcpt(out, rcpt); |
239 } | 239 } |
240 DEBUG(6) debugf("after RT\n"); | 240 DEBUG(6) debugf("after RT\n"); |
241 fprintf(out, "PR:%s\n", prot_names[msg->received_prot]); | 241 fprintf(out, "PR:%s\n", prot_names[msg->received_prot]); |
242 if(msg->received_host != NULL) | 242 if (msg->received_host != NULL) |
243 fprintf(out, "RH:%s\n", msg->received_host); | 243 fprintf(out, "RH:%s\n", msg->received_host); |
244 | 244 |
245 if(msg->ident != NULL) | 245 if (msg->ident != NULL) |
246 fprintf(out, "ID:%s\n", msg->ident); | 246 fprintf(out, "ID:%s\n", msg->ident); |
247 | 247 |
248 if(msg->data_size >= 0) | 248 if (msg->data_size >= 0) |
249 fprintf(out, "DS: %d\n", msg->data_size); | 249 fprintf(out, "DS: %d\n", msg->data_size); |
250 | 250 |
251 if(msg->received_time > 0) | 251 if (msg->received_time > 0) |
252 fprintf(out, "TR: %u\n", (int)(msg->received_time)); | 252 fprintf(out, "TR: %u\n", (int) (msg->received_time)); |
253 | 253 |
254 if(msg->warned_time > 0) | 254 if (msg->warned_time > 0) |
255 fprintf(out, "TW: %u\n", (int)(msg->warned_time)); | 255 fprintf(out, "TW: %u\n", (int) (msg->warned_time)); |
256 | 256 |
257 DEBUG(6) debugf("after RH\n"); | 257 DEBUG(6) debugf("after RH\n"); |
258 fprintf(out, "\n"); | 258 fprintf(out, "\n"); |
259 | 259 |
260 foreach(msg->hdr_list, node){ | 260 foreach(msg->hdr_list, node) { |
261 header *hdr = (header *)(node->data); | 261 header *hdr = (header *) (node->data); |
262 fprintf(out, "HD:%s", hdr->header); | 262 fprintf(out, "HD:%s", hdr->header); |
263 } | 263 } |
264 if(fflush(out) == EOF) ok = FALSE; | 264 if (fflush(out) == EOF) |
265 else if(fdatasync(fileno(out)) != 0){ | 265 ok = FALSE; |
266 if(errno != EINVAL) /* some fs do not support this.. | 266 else if (fdatasync(fileno(out)) != 0) { |
267 I hope this also means that it is not necessary */ | 267 if (errno != EINVAL) /* some fs do not support this.. I hope this also means that it is not necessary */ |
268 ok = FALSE; | 268 ok = FALSE; |
269 } | 269 } |
270 fclose(out); | 270 fclose(out); |
271 if(ok){ | 271 if (ok) { |
272 spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid); | 272 spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid); |
273 DEBUG(4) debugf("spool_file = %s\n", spool_file); | 273 DEBUG(4) debugf("spool_file = %s\n", spool_file); |
274 ok = (rename(tmp_file, spool_file) != -1); | 274 ok = (rename(tmp_file, spool_file) != -1); |
275 g_free(spool_file); | 275 g_free(spool_file); |
276 } | 276 } |
277 }else{ | 277 } else { |
278 logwrite(LOG_ALERT, "could not open temporary header spool file '%s': %s\n", tmp_file, strerror(errno)); | 278 logwrite(LOG_ALERT, "could not open temporary header spool file '%s': %s\n", tmp_file, strerror(errno)); |
279 DEBUG(1) debugf("euid = %d, egid = %d\n", geteuid(), getegid()); | 279 DEBUG(1) debugf("euid = %d, egid = %d\n", geteuid(), getegid()); |
280 ok = FALSE; | 280 ok = FALSE; |
281 } | 281 } |
282 | 282 |
283 g_free(tmp_file); | 283 g_free(tmp_file); |
284 | 284 |
285 return ok; | 285 return ok; |
286 } | 286 } |
287 | 287 |
288 gboolean spool_write(message *msg, gboolean do_write_data) | 288 gboolean |
289 { | 289 spool_write(message * msg, gboolean do_write_data) |
290 GList *list; | 290 { |
291 gchar *spool_file, *tmp_file; | 291 GList *list; |
292 FILE *out; | 292 gchar *spool_file, *tmp_file; |
293 gboolean ok = TRUE; | 293 FILE *out; |
294 uid_t saved_uid, saved_gid; | 294 gboolean ok = TRUE; |
295 /* user can read/write, group can read, others cannot do anything: */ | 295 uid_t saved_uid, saved_gid; |
296 mode_t saved_mode = saved_mode = umask(026); | 296 /* user can read/write, group can read, others cannot do anything: */ |
297 | 297 mode_t saved_mode = saved_mode = umask(026); |
298 /* set uid and gid to the mail ids */ | 298 |
299 if(!conf.run_as_user){ | 299 /* set uid and gid to the mail ids */ |
300 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid); | 300 if (!conf.run_as_user) { |
301 } | 301 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid); |
302 | 302 } |
303 /* header spool: */ | 303 |
304 ok = spool_write_header(msg); | 304 /* header spool: */ |
305 | 305 ok = spool_write_header(msg); |
306 if(ok){ | 306 |
307 | 307 if (ok) { |
308 if(do_write_data){ | 308 |
309 /* data spool: */ | 309 if (do_write_data) { |
310 tmp_file = g_strdup_printf("%s/input/%d-D.tmp", | 310 /* data spool: */ |
311 conf.spool_dir, getpid()); | 311 tmp_file = g_strdup_printf("%s/input/%d-D.tmp", conf.spool_dir, getpid()); |
312 DEBUG(4) debugf("tmp_file = %s\n", tmp_file); | 312 DEBUG(4) debugf("tmp_file = %s\n", tmp_file); |
313 | 313 |
314 if((out = fopen(tmp_file, "w"))){ | 314 if ((out = fopen(tmp_file, "w"))) { |
315 fprintf(out, "%s\n", msg->uid); | 315 fprintf(out, "%s\n", msg->uid); |
316 for(list = g_list_first(msg->data_list); | 316 for (list = g_list_first(msg->data_list); list != NULL; list = g_list_next(list)) { |
317 list != NULL; | 317 fprintf(out, "%s", (gchar *) (list->data)); |
318 list = g_list_next(list)){ | 318 } |
319 fprintf(out, "%s", (gchar *)(list->data)); | 319 |
320 } | 320 /* possibly paranoid ;-) */ |
321 | 321 if (fflush(out) == EOF) |
322 /* possibly paranoid ;-) */ | 322 ok = FALSE; |
323 if(fflush(out) == EOF) ok = FALSE; | 323 else if (fdatasync(fileno(out)) != 0) { |
324 else if(fdatasync(fileno(out)) != 0){ | 324 if (errno != EINVAL) /* some fs do not support this.. I hope this also means that it is not necessary */ |
325 if(errno != EINVAL) /* some fs do not support this.. | 325 ok = FALSE; |
326 I hope this also means that it is not necessary */ | 326 } |
327 ok = FALSE; | 327 fclose(out); |
328 } | 328 if (ok) { |
329 fclose(out); | 329 spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid); |
330 if(ok){ | 330 DEBUG(4) debugf("spool_file = %s\n", spool_file); |
331 spool_file = g_strdup_printf("%s/input/%s-D", | 331 ok = (rename(tmp_file, spool_file) != -1); |
332 conf.spool_dir, msg->uid); | 332 g_free(spool_file); |
333 DEBUG(4) debugf("spool_file = %s\n", spool_file); | 333 } |
334 ok = (rename(tmp_file, spool_file) != -1); | 334 } else { |
335 g_free(spool_file); | 335 logwrite(LOG_ALERT, "could not open temporary data spool file: %s\n", strerror(errno)); |
336 } | 336 ok = FALSE; |
337 }else{ | 337 } |
338 logwrite(LOG_ALERT, "could not open temporary data spool file: %s\n", | 338 g_free(tmp_file); |
339 strerror(errno)); | 339 } |
340 ok = FALSE; | 340 } |
341 } | 341 |
342 g_free(tmp_file); | 342 /* set uid and gid back */ |
343 } | 343 if (!conf.run_as_user) { |
344 } | 344 set_euidgid(saved_uid, saved_gid, NULL, NULL); |
345 | 345 } |
346 /* set uid and gid back */ | 346 |
347 if(!conf.run_as_user){ | 347 umask(saved_mode); |
348 set_euidgid(saved_uid, saved_gid, NULL, NULL); | 348 |
349 } | 349 return ok; |
350 | |
351 umask(saved_mode); | |
352 | |
353 return ok; | |
354 } | 350 } |
355 | 351 |
356 #define MAX_LOCKAGE 300 | 352 #define MAX_LOCKAGE 300 |
357 | 353 |
358 gboolean spool_lock(gchar *uid) | 354 gboolean |
359 { | 355 spool_lock(gchar * uid) |
360 uid_t saved_uid, saved_gid; | 356 { |
361 gchar *hitch_name; | 357 uid_t saved_uid, saved_gid; |
362 gchar *lock_name; | 358 gchar *hitch_name; |
363 gboolean ok = FALSE; | 359 gchar *lock_name; |
364 | 360 gboolean ok = FALSE; |
365 hitch_name = g_strdup_printf("%s/%s-%d.lock", conf.lock_dir, uid, getpid()); | 361 |
366 lock_name = g_strdup_printf("%s/%s.lock", conf.lock_dir, uid); | 362 hitch_name = g_strdup_printf("%s/%s-%d.lock", conf.lock_dir, uid, getpid()); |
367 | 363 lock_name = g_strdup_printf("%s/%s.lock", conf.lock_dir, uid); |
368 /* set uid and gid to the mail ids */ | 364 |
369 if(!conf.run_as_user){ | 365 /* set uid and gid to the mail ids */ |
370 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid); | 366 if (!conf.run_as_user) { |
371 } | 367 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid); |
372 | 368 } |
373 ok = dot_lock(lock_name, hitch_name); | 369 |
374 if(!ok) logwrite(LOG_WARNING, "spool file %s is locked\n", uid); | 370 ok = dot_lock(lock_name, hitch_name); |
375 | 371 if (!ok) |
376 /* set uid and gid back */ | 372 logwrite(LOG_WARNING, "spool file %s is locked\n", uid); |
377 if(!conf.run_as_user){ | 373 |
378 set_euidgid(saved_uid, saved_gid, NULL, NULL); | 374 /* set uid and gid back */ |
379 } | 375 if (!conf.run_as_user) { |
380 | 376 set_euidgid(saved_uid, saved_gid, NULL, NULL); |
381 g_free(lock_name); | 377 } |
382 g_free(hitch_name); | 378 |
383 | 379 g_free(lock_name); |
384 return ok; | 380 g_free(hitch_name); |
385 } | 381 |
386 | 382 return ok; |
387 gboolean spool_unlock(gchar *uid) | 383 } |
388 { | 384 |
389 uid_t saved_uid, saved_gid; | 385 gboolean |
390 gchar *lock_name; | 386 spool_unlock(gchar * uid) |
391 | 387 { |
392 /* set uid and gid to the mail ids */ | 388 uid_t saved_uid, saved_gid; |
393 if(!conf.run_as_user){ | 389 gchar *lock_name; |
394 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid); | 390 |
395 } | 391 /* set uid and gid to the mail ids */ |
396 | 392 if (!conf.run_as_user) { |
397 lock_name = g_strdup_printf("%s/%s.lock", conf.lock_dir, uid); | 393 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid); |
398 dot_unlock(lock_name); | 394 } |
399 g_free(lock_name); | 395 |
400 | 396 lock_name = g_strdup_printf("%s/%s.lock", conf.lock_dir, uid); |
401 /* set uid and gid back */ | 397 dot_unlock(lock_name); |
402 if(!conf.run_as_user){ | 398 g_free(lock_name); |
403 set_euidgid(saved_uid, saved_gid, NULL, NULL); | 399 |
404 } | 400 /* set uid and gid back */ |
405 return TRUE; | 401 if (!conf.run_as_user) { |
406 } | 402 set_euidgid(saved_uid, saved_gid, NULL, NULL); |
407 | 403 } |
408 gboolean spool_delete_all(message *msg) | 404 return TRUE; |
409 { | 405 } |
410 uid_t saved_uid, saved_gid; | 406 |
411 gchar *spool_file; | 407 gboolean |
412 | 408 spool_delete_all(message * msg) |
413 /* set uid and gid to the mail ids */ | 409 { |
414 if(!conf.run_as_user){ | 410 uid_t saved_uid, saved_gid; |
415 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid); | 411 gchar *spool_file; |
416 } | 412 |
417 | 413 /* set uid and gid to the mail ids */ |
418 /* header spool: */ | 414 if (!conf.run_as_user) { |
419 spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid); | 415 set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid); |
420 if(unlink(spool_file) != 0) | 416 } |
421 logwrite(LOG_ALERT, "could not delete spool file %s: %s\n", | 417 |
422 spool_file, strerror(errno)); | 418 /* header spool: */ |
423 g_free(spool_file); | 419 spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid); |
424 | 420 if (unlink(spool_file) != 0) |
425 /* data spool: */ | 421 logwrite(LOG_ALERT, "could not delete spool file %s: %s\n", spool_file, strerror(errno)); |
426 spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid); | 422 g_free(spool_file); |
427 if(unlink(spool_file) != 0) | 423 |
428 logwrite(LOG_ALERT, "could not delete spool file %s: %s\n", | 424 /* data spool: */ |
429 spool_file, strerror(errno)); | 425 spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid); |
430 g_free(spool_file); | 426 if (unlink(spool_file) != 0) |
431 | 427 logwrite(LOG_ALERT, "could not delete spool file %s: %s\n", spool_file, strerror(errno)); |
432 /* set uid and gid back */ | 428 g_free(spool_file); |
433 if(!conf.run_as_user){ | 429 |
434 set_euidgid(saved_uid, saved_gid, NULL, NULL); | 430 /* set uid and gid back */ |
435 } | 431 if (!conf.run_as_user) { |
436 return TRUE; | 432 set_euidgid(saved_uid, saved_gid, NULL, NULL); |
437 } | 433 } |
434 return TRUE; | |
435 } |