masqmail
diff 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 |
line diff
1.1 --- a/src/spool.c Mon Oct 27 16:21:27 2008 +0100 1.2 +++ b/src/spool.c Mon Oct 27 16:23:10 2008 +0100 1.3 @@ -20,418 +20,416 @@ 1.4 #include <sys/stat.h> 1.5 #include "dotlock.h" 1.6 1.7 -static 1.8 -gint read_line(FILE *in, gchar *buf, gint buf_len) 1.9 +static gint 1.10 +read_line(FILE * in, gchar * buf, gint buf_len) 1.11 { 1.12 - gint p = 0; 1.13 - gint c; 1.14 + gint p = 0; 1.15 + gint c; 1.16 1.17 - while((c = getc(in)) != '\n' && (c != EOF)){ 1.18 - if(p >= buf_len-1) { return 0; } 1.19 - buf[p++] = c; 1.20 - } 1.21 + while ((c = getc(in)) != '\n' && (c != EOF)) { 1.22 + if (p >= buf_len - 1) { 1.23 + return 0; 1.24 + } 1.25 + buf[p++] = c; 1.26 + } 1.27 1.28 - if(c == EOF){ 1.29 - return -1; 1.30 - } 1.31 - if((p > 0) && (buf[p-1] == '\r')) 1.32 - p--; 1.33 - buf[p++] = '\n'; 1.34 - buf[p] = 0; 1.35 + if (c == EOF) { 1.36 + return -1; 1.37 + } 1.38 + if ((p > 0) && (buf[p - 1] == '\r')) 1.39 + p--; 1.40 + buf[p++] = '\n'; 1.41 + buf[p] = 0; 1.42 1.43 - return p; 1.44 + return p; 1.45 } 1.46 1.47 -static 1.48 -void spool_write_rcpt(FILE *out, address *rcpt) 1.49 +static void 1.50 +spool_write_rcpt(FILE * out, address * rcpt) 1.51 { 1.52 - gchar dlvrd_char = addr_is_delivered(rcpt) ? 'X' : (addr_is_failed(rcpt) ? 'F' : ' '); 1.53 + gchar dlvrd_char = addr_is_delivered(rcpt) ? 'X' : (addr_is_failed(rcpt) ? 'F' : ' '); 1.54 1.55 - if(rcpt->local_part[0] != '|'){ 1.56 - /* this is a paranoid check, in case it slipped through: */ 1.57 - /* if this happens, it is a bug */ 1.58 - if(rcpt->domain == NULL){ 1.59 - logwrite(LOG_WARNING, "BUG: null domain for address %s, setting to %s\n", 1.60 - rcpt->local_part, conf.host_name); 1.61 - logwrite(LOG_WARNING, "please report this bug.\n"); 1.62 - rcpt->domain = g_strdup(conf.host_name); 1.63 - } 1.64 - fprintf(out, "RT:%c%s\n", dlvrd_char, addr_string(rcpt)); 1.65 - }else{ 1.66 - fprintf(out, "RT:%c%s\n", dlvrd_char, rcpt->local_part); 1.67 - } 1.68 + if (rcpt->local_part[0] != '|') { 1.69 + /* this is a paranoid check, in case it slipped through: */ 1.70 + /* if this happens, it is a bug */ 1.71 + if (rcpt->domain == NULL) { 1.72 + logwrite(LOG_WARNING, "BUG: null domain for address %s, setting to %s\n", rcpt->local_part, conf.host_name); 1.73 + logwrite(LOG_WARNING, "please report this bug.\n"); 1.74 + rcpt->domain = g_strdup(conf.host_name); 1.75 + } 1.76 + fprintf(out, "RT:%c%s\n", dlvrd_char, addr_string(rcpt)); 1.77 + } else { 1.78 + fprintf(out, "RT:%c%s\n", dlvrd_char, rcpt->local_part); 1.79 + } 1.80 } 1.81 1.82 -static 1.83 -address *spool_scan_rcpt(gchar *line) 1.84 +static address* 1.85 +spool_scan_rcpt(gchar * line) 1.86 { 1.87 - address *rcpt = NULL; 1.88 + address *rcpt = NULL; 1.89 1.90 - if(line[3] != 0){ 1.91 - if(line[4] != '|'){ 1.92 - rcpt = create_address(&(line[4]), TRUE); 1.93 - }else{ 1.94 - rcpt = create_address_pipe(&(line[4])); 1.95 - } 1.96 - if(line[3] == 'X'){ 1.97 - addr_mark_delivered(rcpt); 1.98 - }else if(line[3] == 'F'){ 1.99 - addr_mark_failed(rcpt); 1.100 - } 1.101 - } 1.102 - return rcpt; 1.103 + if (line[3] != 0) { 1.104 + if (line[4] != '|') { 1.105 + rcpt = create_address(&(line[4]), TRUE); 1.106 + } else { 1.107 + rcpt = create_address_pipe(&(line[4])); 1.108 + } 1.109 + if (line[3] == 'X') { 1.110 + addr_mark_delivered(rcpt); 1.111 + } else if (line[3] == 'F') { 1.112 + addr_mark_failed(rcpt); 1.113 + } 1.114 + } 1.115 + return rcpt; 1.116 } 1.117 1.118 -gboolean spool_read_data(message *msg) 1.119 +gboolean 1.120 +spool_read_data(message * msg) 1.121 { 1.122 - FILE *in; 1.123 - gboolean ok = FALSE; 1.124 - gchar *spool_file; 1.125 + FILE *in; 1.126 + gboolean ok = FALSE; 1.127 + gchar *spool_file; 1.128 1.129 - DEBUG(5) debugf("spool_read_data entered\n"); 1.130 - spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid); 1.131 - DEBUG(5) debugf("reading data spool file '%s'\n", spool_file); 1.132 - if((in = fopen(spool_file, "r"))){ 1.133 - char buf[MAX_DATALINE]; 1.134 - int len; 1.135 - 1.136 - /* msg uid */ 1.137 - read_line(in, buf, MAX_DATALINE); 1.138 - 1.139 - /* data */ 1.140 - msg->data_list = NULL; 1.141 - while((len = read_line(in, buf, MAX_DATALINE)) > 0){ 1.142 - msg->data_list = g_list_prepend(msg->data_list, g_strdup(buf)); 1.143 - } 1.144 - msg->data_list = g_list_reverse(msg->data_list); 1.145 - fclose(in); 1.146 - ok = TRUE; 1.147 - }else 1.148 - logwrite(LOG_ALERT, "could not open spool data file %s: %s\n", 1.149 - spool_file, strerror(errno)); 1.150 - return ok; 1.151 + DEBUG(5) debugf("spool_read_data entered\n"); 1.152 + spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid); 1.153 + DEBUG(5) debugf("reading data spool file '%s'\n", spool_file); 1.154 + if ((in = fopen(spool_file, "r"))) { 1.155 + char buf[MAX_DATALINE]; 1.156 + int len; 1.157 + 1.158 + /* msg uid */ 1.159 + read_line(in, buf, MAX_DATALINE); 1.160 + 1.161 + /* data */ 1.162 + msg->data_list = NULL; 1.163 + while ((len = read_line(in, buf, MAX_DATALINE)) > 0) { 1.164 + msg->data_list = g_list_prepend(msg->data_list, g_strdup(buf)); 1.165 + } 1.166 + msg->data_list = g_list_reverse(msg->data_list); 1.167 + fclose(in); 1.168 + ok = TRUE; 1.169 + } else 1.170 + logwrite(LOG_ALERT, "could not open spool data file %s: %s\n", spool_file, strerror(errno)); 1.171 + return ok; 1.172 } 1.173 1.174 -gboolean spool_read_header(message *msg) 1.175 +gboolean 1.176 +spool_read_header(message * msg) 1.177 { 1.178 - FILE *in; 1.179 - gboolean ok = FALSE; 1.180 - gchar *spool_file; 1.181 + FILE *in; 1.182 + gboolean ok = FALSE; 1.183 + gchar *spool_file; 1.184 1.185 - /* header spool: */ 1.186 - spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid); 1.187 - if((in = fopen(spool_file, "r"))){ 1.188 - header *hdr = NULL; 1.189 - char buf[MAX_DATALINE]; 1.190 - int len; 1.191 + /* header spool: */ 1.192 + spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid); 1.193 + if ((in = fopen(spool_file, "r"))) { 1.194 + header *hdr = NULL; 1.195 + char buf[MAX_DATALINE]; 1.196 + int len; 1.197 1.198 - /* msg uid */ 1.199 - read_line(in, buf, MAX_DATALINE); 1.200 - 1.201 - /* envelope header */ 1.202 - while((len = read_line(in, buf, MAX_DATALINE)) > 0){ 1.203 - if(buf[0] == '\n') 1.204 - break; 1.205 - else if(strncasecmp(buf, "MF:", 3) == 0){ 1.206 - msg->return_path = create_address(&(buf[3]), TRUE); 1.207 - DEBUG(3) debugf("spool_read: MAIL FROM: %s", 1.208 - msg->return_path->address); 1.209 - }else if(strncasecmp(buf, "RT:", 3) == 0){ 1.210 - address *addr; 1.211 - addr = spool_scan_rcpt(buf); 1.212 - if(!addr_is_delivered(addr) && !addr_is_failed(addr)){ 1.213 - msg->rcpt_list = g_list_append(msg->rcpt_list, addr); 1.214 - }else{ 1.215 - msg->non_rcpt_list = g_list_append(msg->non_rcpt_list, addr); 1.216 - } 1.217 - }else if(strncasecmp(buf, "PR:", 3) == 0){ 1.218 - prot_id i; 1.219 - for(i = 0; i < PROT_NUM; i++){ 1.220 - if(strncasecmp(prot_names[i], &(buf[3]), 1.221 - strlen(prot_names[i])) == 0){ 1.222 - break; 1.223 - } 1.224 - } 1.225 - msg->received_prot = i; 1.226 - }else if(strncasecmp(buf, "RH:", 3) == 0){ 1.227 - g_strchomp(buf); 1.228 - msg->received_host = g_strdup(&(buf[3])); 1.229 - }else if(strncasecmp(buf, "ID:", 3) == 0){ 1.230 - g_strchomp(buf); 1.231 - msg->ident = g_strdup(&(buf[3])); 1.232 - }else if(strncasecmp(buf, "DS:", 3) == 0){ 1.233 - msg->data_size = atoi(&(buf[3])); 1.234 - }else if(strncasecmp(buf, "TR:", 3) == 0){ 1.235 - msg->received_time = (time_t)(atoi(&(buf[3]))); 1.236 - }else if(strncasecmp(buf, "TW:", 3) == 0){ 1.237 - msg->warned_time = (time_t)(atoi(&(buf[3]))); 1.238 - } 1.239 - /* so far ignore other tags */ 1.240 - } 1.241 - 1.242 - /* mail headers */ 1.243 - while((len = read_line(in, buf, MAX_DATALINE)) > 0){ 1.244 - if(strncasecmp(buf, "HD:", 3) == 0){ 1.245 - hdr = get_header(&(buf[3])); 1.246 - msg->hdr_list = g_list_append(msg->hdr_list, hdr); 1.247 - }else if((buf[0] == ' ' || buf[0] == '\t') && hdr){ 1.248 - char *tmp = hdr->header; 1.249 - /* header continuation */ 1.250 - hdr->header = g_strconcat(hdr->header, buf, NULL); 1.251 - hdr->value = hdr->header + (hdr->value - tmp); 1.252 - }else 1.253 - break; 1.254 - } 1.255 - fclose(in); 1.256 - ok = TRUE; 1.257 - }else 1.258 - logwrite(LOG_ALERT, "could not open spool header file %s: %s\n", 1.259 - spool_file, strerror(errno)); 1.260 - return ok; 1.261 + /* msg uid */ 1.262 + read_line(in, buf, MAX_DATALINE); 1.263 + 1.264 + /* envelope header */ 1.265 + while ((len = read_line(in, buf, MAX_DATALINE)) > 0) { 1.266 + if (buf[0] == '\n') 1.267 + break; 1.268 + else if (strncasecmp(buf, "MF:", 3) == 0) { 1.269 + msg->return_path = create_address(&(buf[3]), TRUE); 1.270 + DEBUG(3) debugf("spool_read: MAIL FROM: %s", msg->return_path->address); 1.271 + } else if (strncasecmp(buf, "RT:", 3) == 0) { 1.272 + address *addr; 1.273 + addr = spool_scan_rcpt(buf); 1.274 + if (!addr_is_delivered(addr) && !addr_is_failed(addr)) { 1.275 + msg->rcpt_list = g_list_append(msg->rcpt_list, addr); 1.276 + } else { 1.277 + msg->non_rcpt_list = g_list_append(msg->non_rcpt_list, addr); 1.278 + } 1.279 + } else if (strncasecmp(buf, "PR:", 3) == 0) { 1.280 + prot_id i; 1.281 + for (i = 0; i < PROT_NUM; i++) { 1.282 + if (strncasecmp(prot_names[i], &(buf[3]), strlen(prot_names[i])) == 0) { 1.283 + break; 1.284 + } 1.285 + } 1.286 + msg->received_prot = i; 1.287 + } else if (strncasecmp(buf, "RH:", 3) == 0) { 1.288 + g_strchomp(buf); 1.289 + msg->received_host = g_strdup(&(buf[3])); 1.290 + } else if (strncasecmp(buf, "ID:", 3) == 0) { 1.291 + g_strchomp(buf); 1.292 + msg->ident = g_strdup(&(buf[3])); 1.293 + } else if (strncasecmp(buf, "DS:", 3) == 0) { 1.294 + msg->data_size = atoi(&(buf[3])); 1.295 + } else if (strncasecmp(buf, "TR:", 3) == 0) { 1.296 + msg->received_time = (time_t) (atoi(&(buf[3]))); 1.297 + } else if (strncasecmp(buf, "TW:", 3) == 0) { 1.298 + msg->warned_time = (time_t) (atoi(&(buf[3]))); 1.299 + } 1.300 + /* so far ignore other tags */ 1.301 + } 1.302 + 1.303 + /* mail headers */ 1.304 + while ((len = read_line(in, buf, MAX_DATALINE)) > 0) { 1.305 + if (strncasecmp(buf, "HD:", 3) == 0) { 1.306 + hdr = get_header(&(buf[3])); 1.307 + msg->hdr_list = g_list_append(msg->hdr_list, hdr); 1.308 + } else if ((buf[0] == ' ' || buf[0] == '\t') && hdr) { 1.309 + char *tmp = hdr->header; 1.310 + /* header continuation */ 1.311 + hdr->header = g_strconcat(hdr->header, buf, NULL); 1.312 + hdr->value = hdr->header + (hdr->value - tmp); 1.313 + } else 1.314 + break; 1.315 + } 1.316 + fclose(in); 1.317 + ok = TRUE; 1.318 + } else 1.319 + logwrite(LOG_ALERT, "could not open spool header file %s: %s\n", spool_file, strerror(errno)); 1.320 + return ok; 1.321 } 1.322 1.323 -message *msg_spool_read(gchar *uid, gboolean do_readdata) 1.324 +message* 1.325 +msg_spool_read(gchar * uid, gboolean do_readdata) 1.326 { 1.327 - message *msg; 1.328 - gboolean ok = FALSE; 1.329 - 1.330 - msg = create_message(); 1.331 - msg->uid = g_strdup(uid); 1.332 + message *msg; 1.333 + gboolean ok = FALSE; 1.334 1.335 - /* header spool: */ 1.336 - ok = spool_read_header(msg); 1.337 - if(ok && do_readdata){ 1.338 - /* data spool: */ 1.339 - ok = spool_read_data(msg); 1.340 - } 1.341 - return msg; 1.342 + msg = create_message(); 1.343 + msg->uid = g_strdup(uid); 1.344 + 1.345 + /* header spool: */ 1.346 + ok = spool_read_header(msg); 1.347 + if (ok && do_readdata) { 1.348 + /* data spool: */ 1.349 + ok = spool_read_data(msg); 1.350 + } 1.351 + return msg; 1.352 } 1.353 1.354 /* write header. uid and gid should already be set to the 1.355 mail ids. Better call spool_write(msg, FALSE). 1.356 */ 1.357 -static 1.358 -gboolean spool_write_header(message *msg) 1.359 +static gboolean 1.360 +spool_write_header(message * msg) 1.361 { 1.362 - GList *node; 1.363 - gchar *spool_file, *tmp_file; 1.364 - FILE *out; 1.365 - gboolean ok = TRUE; 1.366 + GList *node; 1.367 + gchar *spool_file, *tmp_file; 1.368 + FILE *out; 1.369 + gboolean ok = TRUE; 1.370 1.371 - /* header spool: */ 1.372 - tmp_file = g_strdup_printf("%s/input/%d-H.tmp", conf.spool_dir, getpid()); 1.373 - DEBUG(4) debugf("tmp_file = %s\n", tmp_file); 1.374 + /* header spool: */ 1.375 + tmp_file = g_strdup_printf("%s/input/%d-H.tmp", conf.spool_dir, getpid()); 1.376 + DEBUG(4) debugf("tmp_file = %s\n", tmp_file); 1.377 1.378 - if((out = fopen(tmp_file, "w"))){ 1.379 - DEBUG(6) debugf("opened tmp_file %s\n", tmp_file); 1.380 + if ((out = fopen(tmp_file, "w"))) { 1.381 + DEBUG(6) debugf("opened tmp_file %s\n", tmp_file); 1.382 1.383 - fprintf(out, "%s\n", msg->uid); 1.384 - fprintf(out, "MF:%s\n", addr_string(msg->return_path)); 1.385 + fprintf(out, "%s\n", msg->uid); 1.386 + fprintf(out, "MF:%s\n", addr_string(msg->return_path)); 1.387 1.388 - DEBUG(6) debugf("after MF\n"); 1.389 - foreach(msg->rcpt_list, node){ 1.390 - address *rcpt = (address *)(node->data); 1.391 - spool_write_rcpt(out, rcpt); 1.392 - } 1.393 - foreach(msg->non_rcpt_list, node){ 1.394 - address *rcpt = (address *)(node->data); 1.395 - spool_write_rcpt(out, rcpt); 1.396 - } 1.397 - DEBUG(6) debugf("after RT\n"); 1.398 - fprintf(out, "PR:%s\n", prot_names[msg->received_prot]); 1.399 - if(msg->received_host != NULL) 1.400 - fprintf(out, "RH:%s\n", msg->received_host); 1.401 + DEBUG(6) debugf("after MF\n"); 1.402 + foreach(msg->rcpt_list, node) { 1.403 + address *rcpt = (address *) (node->data); 1.404 + spool_write_rcpt(out, rcpt); 1.405 + } 1.406 + foreach(msg->non_rcpt_list, node) { 1.407 + address *rcpt = (address *) (node->data); 1.408 + spool_write_rcpt(out, rcpt); 1.409 + } 1.410 + DEBUG(6) debugf("after RT\n"); 1.411 + fprintf(out, "PR:%s\n", prot_names[msg->received_prot]); 1.412 + if (msg->received_host != NULL) 1.413 + fprintf(out, "RH:%s\n", msg->received_host); 1.414 1.415 - if(msg->ident != NULL) 1.416 - fprintf(out, "ID:%s\n", msg->ident); 1.417 + if (msg->ident != NULL) 1.418 + fprintf(out, "ID:%s\n", msg->ident); 1.419 1.420 - if(msg->data_size >= 0) 1.421 - fprintf(out, "DS: %d\n", msg->data_size); 1.422 + if (msg->data_size >= 0) 1.423 + fprintf(out, "DS: %d\n", msg->data_size); 1.424 1.425 - if(msg->received_time > 0) 1.426 - fprintf(out, "TR: %u\n", (int)(msg->received_time)); 1.427 + if (msg->received_time > 0) 1.428 + fprintf(out, "TR: %u\n", (int) (msg->received_time)); 1.429 1.430 - if(msg->warned_time > 0) 1.431 - fprintf(out, "TW: %u\n", (int)(msg->warned_time)); 1.432 + if (msg->warned_time > 0) 1.433 + fprintf(out, "TW: %u\n", (int) (msg->warned_time)); 1.434 1.435 - DEBUG(6) debugf("after RH\n"); 1.436 - fprintf(out, "\n"); 1.437 + DEBUG(6) debugf("after RH\n"); 1.438 + fprintf(out, "\n"); 1.439 1.440 - foreach(msg->hdr_list, node){ 1.441 - header *hdr = (header *)(node->data); 1.442 - fprintf(out, "HD:%s", hdr->header); 1.443 - } 1.444 - if(fflush(out) == EOF) ok = FALSE; 1.445 - else if(fdatasync(fileno(out)) != 0){ 1.446 - if(errno != EINVAL) /* some fs do not support this.. 1.447 - I hope this also means that it is not necessary */ 1.448 - ok = FALSE; 1.449 - } 1.450 - fclose(out); 1.451 - if(ok){ 1.452 - spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid); 1.453 - DEBUG(4) debugf("spool_file = %s\n", spool_file); 1.454 - ok = (rename(tmp_file, spool_file) != -1); 1.455 - g_free(spool_file); 1.456 - } 1.457 - }else{ 1.458 - logwrite(LOG_ALERT, "could not open temporary header spool file '%s': %s\n", tmp_file, strerror(errno)); 1.459 - DEBUG(1) debugf("euid = %d, egid = %d\n", geteuid(), getegid()); 1.460 - ok = FALSE; 1.461 - } 1.462 + foreach(msg->hdr_list, node) { 1.463 + header *hdr = (header *) (node->data); 1.464 + fprintf(out, "HD:%s", hdr->header); 1.465 + } 1.466 + if (fflush(out) == EOF) 1.467 + ok = FALSE; 1.468 + else if (fdatasync(fileno(out)) != 0) { 1.469 + if (errno != EINVAL) /* some fs do not support this.. I hope this also means that it is not necessary */ 1.470 + ok = FALSE; 1.471 + } 1.472 + fclose(out); 1.473 + if (ok) { 1.474 + spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid); 1.475 + DEBUG(4) debugf("spool_file = %s\n", spool_file); 1.476 + ok = (rename(tmp_file, spool_file) != -1); 1.477 + g_free(spool_file); 1.478 + } 1.479 + } else { 1.480 + logwrite(LOG_ALERT, "could not open temporary header spool file '%s': %s\n", tmp_file, strerror(errno)); 1.481 + DEBUG(1) debugf("euid = %d, egid = %d\n", geteuid(), getegid()); 1.482 + ok = FALSE; 1.483 + } 1.484 1.485 - g_free(tmp_file); 1.486 + g_free(tmp_file); 1.487 1.488 - return ok; 1.489 + return ok; 1.490 } 1.491 1.492 -gboolean spool_write(message *msg, gboolean do_write_data) 1.493 +gboolean 1.494 +spool_write(message * msg, gboolean do_write_data) 1.495 { 1.496 - GList *list; 1.497 - gchar *spool_file, *tmp_file; 1.498 - FILE *out; 1.499 - gboolean ok = TRUE; 1.500 - uid_t saved_uid, saved_gid; 1.501 - /* user can read/write, group can read, others cannot do anything: */ 1.502 - mode_t saved_mode = saved_mode = umask(026); 1.503 + GList *list; 1.504 + gchar *spool_file, *tmp_file; 1.505 + FILE *out; 1.506 + gboolean ok = TRUE; 1.507 + uid_t saved_uid, saved_gid; 1.508 + /* user can read/write, group can read, others cannot do anything: */ 1.509 + mode_t saved_mode = saved_mode = umask(026); 1.510 1.511 - /* set uid and gid to the mail ids */ 1.512 - if(!conf.run_as_user){ 1.513 - set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid); 1.514 - } 1.515 - 1.516 - /* header spool: */ 1.517 - ok = spool_write_header(msg); 1.518 - 1.519 - if(ok){ 1.520 - 1.521 - if(do_write_data){ 1.522 - /* data spool: */ 1.523 - tmp_file = g_strdup_printf("%s/input/%d-D.tmp", 1.524 - conf.spool_dir, getpid()); 1.525 - DEBUG(4) debugf("tmp_file = %s\n", tmp_file); 1.526 - 1.527 - if((out = fopen(tmp_file, "w"))){ 1.528 - fprintf(out, "%s\n", msg->uid); 1.529 - for(list = g_list_first(msg->data_list); 1.530 - list != NULL; 1.531 - list = g_list_next(list)){ 1.532 - fprintf(out, "%s", (gchar *)(list->data)); 1.533 + /* set uid and gid to the mail ids */ 1.534 + if (!conf.run_as_user) { 1.535 + set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid); 1.536 } 1.537 1.538 - /* possibly paranoid ;-) */ 1.539 - if(fflush(out) == EOF) ok = FALSE; 1.540 - else if(fdatasync(fileno(out)) != 0){ 1.541 - if(errno != EINVAL) /* some fs do not support this.. 1.542 - I hope this also means that it is not necessary */ 1.543 - ok = FALSE; 1.544 + /* header spool: */ 1.545 + ok = spool_write_header(msg); 1.546 + 1.547 + if (ok) { 1.548 + 1.549 + if (do_write_data) { 1.550 + /* data spool: */ 1.551 + tmp_file = g_strdup_printf("%s/input/%d-D.tmp", conf.spool_dir, getpid()); 1.552 + DEBUG(4) debugf("tmp_file = %s\n", tmp_file); 1.553 + 1.554 + if ((out = fopen(tmp_file, "w"))) { 1.555 + fprintf(out, "%s\n", msg->uid); 1.556 + for (list = g_list_first(msg->data_list); list != NULL; list = g_list_next(list)) { 1.557 + fprintf(out, "%s", (gchar *) (list->data)); 1.558 + } 1.559 + 1.560 + /* possibly paranoid ;-) */ 1.561 + if (fflush(out) == EOF) 1.562 + ok = FALSE; 1.563 + else if (fdatasync(fileno(out)) != 0) { 1.564 + if (errno != EINVAL) /* some fs do not support this.. I hope this also means that it is not necessary */ 1.565 + ok = FALSE; 1.566 + } 1.567 + fclose(out); 1.568 + if (ok) { 1.569 + spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid); 1.570 + DEBUG(4) debugf("spool_file = %s\n", spool_file); 1.571 + ok = (rename(tmp_file, spool_file) != -1); 1.572 + g_free(spool_file); 1.573 + } 1.574 + } else { 1.575 + logwrite(LOG_ALERT, "could not open temporary data spool file: %s\n", strerror(errno)); 1.576 + ok = FALSE; 1.577 + } 1.578 + g_free(tmp_file); 1.579 + } 1.580 } 1.581 - fclose(out); 1.582 - if(ok){ 1.583 - spool_file = g_strdup_printf("%s/input/%s-D", 1.584 - conf.spool_dir, msg->uid); 1.585 - DEBUG(4) debugf("spool_file = %s\n", spool_file); 1.586 - ok = (rename(tmp_file, spool_file) != -1); 1.587 - g_free(spool_file); 1.588 + 1.589 + /* set uid and gid back */ 1.590 + if (!conf.run_as_user) { 1.591 + set_euidgid(saved_uid, saved_gid, NULL, NULL); 1.592 } 1.593 - }else{ 1.594 - logwrite(LOG_ALERT, "could not open temporary data spool file: %s\n", 1.595 - strerror(errno)); 1.596 - ok = FALSE; 1.597 - } 1.598 - g_free(tmp_file); 1.599 - } 1.600 - } 1.601 1.602 - /* set uid and gid back */ 1.603 - if(!conf.run_as_user){ 1.604 - set_euidgid(saved_uid, saved_gid, NULL, NULL); 1.605 - } 1.606 + umask(saved_mode); 1.607 1.608 - umask(saved_mode); 1.609 - 1.610 - return ok; 1.611 + return ok; 1.612 } 1.613 1.614 #define MAX_LOCKAGE 300 1.615 1.616 -gboolean spool_lock(gchar *uid) 1.617 +gboolean 1.618 +spool_lock(gchar * uid) 1.619 { 1.620 - uid_t saved_uid, saved_gid; 1.621 - gchar *hitch_name; 1.622 - gchar *lock_name; 1.623 - gboolean ok = FALSE; 1.624 + uid_t saved_uid, saved_gid; 1.625 + gchar *hitch_name; 1.626 + gchar *lock_name; 1.627 + gboolean ok = FALSE; 1.628 1.629 - hitch_name = g_strdup_printf("%s/%s-%d.lock", conf.lock_dir, uid, getpid()); 1.630 - lock_name = g_strdup_printf("%s/%s.lock", conf.lock_dir, uid); 1.631 + hitch_name = g_strdup_printf("%s/%s-%d.lock", conf.lock_dir, uid, getpid()); 1.632 + lock_name = g_strdup_printf("%s/%s.lock", conf.lock_dir, uid); 1.633 1.634 - /* set uid and gid to the mail ids */ 1.635 - if(!conf.run_as_user){ 1.636 - set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid); 1.637 - } 1.638 + /* set uid and gid to the mail ids */ 1.639 + if (!conf.run_as_user) { 1.640 + set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid); 1.641 + } 1.642 1.643 - ok = dot_lock(lock_name, hitch_name); 1.644 - if(!ok) logwrite(LOG_WARNING, "spool file %s is locked\n", uid); 1.645 + ok = dot_lock(lock_name, hitch_name); 1.646 + if (!ok) 1.647 + logwrite(LOG_WARNING, "spool file %s is locked\n", uid); 1.648 1.649 - /* set uid and gid back */ 1.650 - if(!conf.run_as_user){ 1.651 - set_euidgid(saved_uid, saved_gid, NULL, NULL); 1.652 - } 1.653 + /* set uid and gid back */ 1.654 + if (!conf.run_as_user) { 1.655 + set_euidgid(saved_uid, saved_gid, NULL, NULL); 1.656 + } 1.657 1.658 - g_free(lock_name); 1.659 - g_free(hitch_name); 1.660 + g_free(lock_name); 1.661 + g_free(hitch_name); 1.662 1.663 - return ok; 1.664 + return ok; 1.665 } 1.666 1.667 -gboolean spool_unlock(gchar *uid) 1.668 +gboolean 1.669 +spool_unlock(gchar * uid) 1.670 { 1.671 - uid_t saved_uid, saved_gid; 1.672 - gchar *lock_name; 1.673 + uid_t saved_uid, saved_gid; 1.674 + gchar *lock_name; 1.675 1.676 - /* set uid and gid to the mail ids */ 1.677 - if(!conf.run_as_user){ 1.678 - set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid); 1.679 - } 1.680 + /* set uid and gid to the mail ids */ 1.681 + if (!conf.run_as_user) { 1.682 + set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid); 1.683 + } 1.684 1.685 - lock_name = g_strdup_printf("%s/%s.lock", conf.lock_dir, uid); 1.686 - dot_unlock(lock_name); 1.687 - g_free(lock_name); 1.688 + lock_name = g_strdup_printf("%s/%s.lock", conf.lock_dir, uid); 1.689 + dot_unlock(lock_name); 1.690 + g_free(lock_name); 1.691 1.692 - /* set uid and gid back */ 1.693 - if(!conf.run_as_user){ 1.694 - set_euidgid(saved_uid, saved_gid, NULL, NULL); 1.695 - } 1.696 - return TRUE; 1.697 + /* set uid and gid back */ 1.698 + if (!conf.run_as_user) { 1.699 + set_euidgid(saved_uid, saved_gid, NULL, NULL); 1.700 + } 1.701 + return TRUE; 1.702 } 1.703 1.704 -gboolean spool_delete_all(message *msg) 1.705 +gboolean 1.706 +spool_delete_all(message * msg) 1.707 { 1.708 - uid_t saved_uid, saved_gid; 1.709 - gchar *spool_file; 1.710 + uid_t saved_uid, saved_gid; 1.711 + gchar *spool_file; 1.712 1.713 - /* set uid and gid to the mail ids */ 1.714 - if(!conf.run_as_user){ 1.715 - set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid); 1.716 - } 1.717 + /* set uid and gid to the mail ids */ 1.718 + if (!conf.run_as_user) { 1.719 + set_euidgid(conf.mail_uid, conf.mail_gid, &saved_uid, &saved_gid); 1.720 + } 1.721 1.722 - /* header spool: */ 1.723 - spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid); 1.724 - if(unlink(spool_file) != 0) 1.725 - logwrite(LOG_ALERT, "could not delete spool file %s: %s\n", 1.726 - spool_file, strerror(errno)); 1.727 - g_free(spool_file); 1.728 + /* header spool: */ 1.729 + spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid); 1.730 + if (unlink(spool_file) != 0) 1.731 + logwrite(LOG_ALERT, "could not delete spool file %s: %s\n", spool_file, strerror(errno)); 1.732 + g_free(spool_file); 1.733 1.734 - /* data spool: */ 1.735 - spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid); 1.736 - if(unlink(spool_file) != 0) 1.737 - logwrite(LOG_ALERT, "could not delete spool file %s: %s\n", 1.738 - spool_file, strerror(errno)); 1.739 - g_free(spool_file); 1.740 + /* data spool: */ 1.741 + spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid); 1.742 + if (unlink(spool_file) != 0) 1.743 + logwrite(LOG_ALERT, "could not delete spool file %s: %s\n", spool_file, strerror(errno)); 1.744 + g_free(spool_file); 1.745 1.746 - /* set uid and gid back */ 1.747 - if(!conf.run_as_user){ 1.748 - set_euidgid(saved_uid, saved_gid, NULL, NULL); 1.749 - } 1.750 - return TRUE; 1.751 + /* set uid and gid back */ 1.752 + if (!conf.run_as_user) { 1.753 + set_euidgid(saved_uid, saved_gid, NULL, NULL); 1.754 + } 1.755 + return TRUE; 1.756 }