s/qmail 4.4.13
Next generation secure email transport
Loading...
Searching...
No Matches
srsforward.c
Go to the documentation of this file.
1#include <unistd.h>
2#include <sys/types.h>
3#include "control.h"
4#include "sig.h"
5#include "constmap.h"
6#include "readwrite.h"
7#include "exit.h"
8#include "env.h"
9#include "qmail.h"
10#include "auto_qmail.h"
11#include "getoptb.h"
12#include "buffer.h"
13#include "str.h"
14#include "fmt.h"
15#include "stralloc.h"
16#include "logmsg.h"
17#include "srsq.h"
18#include "qmail.h"
19
20#define WHO "srsforward"
21
22void die_nomem() { logmsg(WHO,111,FATAL,"out of memory"); }
23void die_control() { logmsg(WHO,110,FATAL,"Unable to read control files"); }
24
25struct qmail qqt;
26
27char *srsdomaininfo = 0;
28stralloc srsdomains = {0};
30stralloc srshost = {0};
31stralloc srssender = {0};
32stralloc cookie = {0};
34
37
46ssize_t mywrite(int fd,char *buf,size_t len)
47{
48 qmail_put(&qqt,buf,len);
49 return len;
50}
51
54buffer bi = BUFFER_INIT(buffer_unixread,0,inbuf,sizeof(inbuf));
55buffer bo = BUFFER_INIT(mywrite,-1,outbuf,sizeof(outbuf));
56
57char num[FMT_ULONG];
58
59int main(int argc,char * const argv[])
60{
61 int i, j, r;
62 int opt;
63 char *qqx;
64 char separator = '=';
65 char *host = 0;
66 char *sender = 0;
67 char *dtline = 0;
68 char *sendhost = 0;
69 int alwaysrewrite = 0;
70 char ch;
71
72 sig_pipeignore();
73
74 sender = env_get("NEWSENDER");
75 if (!sender)
76 logmsg(WHO,100,FATAL,"NEWSENDER not set");
77 host = env_get("HOST");
78 if (!host)
79 logmsg(WHO,100,FATAL,"HOST not set");
80 dtline = env_get("DTLINE");
81 if (!dtline)
82 logmsg(WHO,100,FATAL,"DTLINE not set");
83
84 if (chdir(auto_qmail) == -1)
85 logmsg(WHO,111,FATAL,B("unable to chdir to: ",auto_qmail));
86
87 if (!stralloc_cats(&srshost,"!")) die_nomem();
88 if (!stralloc_cats(&srshost,host)) die_nomem();
89
90 switch (control_readfile(&srsdomains,"control/srsdomains",0)) {
91 case -1: die_control();
92 case 0: if (!constmap_init(&mapsrsdomains,"",0,1)) die_nomem(); break;
93 case 1: if (!constmap_init(&mapsrsdomains,srsdomains.s,srsdomains.len,1)) die_nomem(); break;
94 }
95 if (constmap(&mapsrsdomains,srshost.s,srshost.len)) flagforward = 0; // domain blacklisted, bounce
96 if ((srsdomaininfo = constmap(&mapsrsdomains,host,str_len(host))) == 0) {
97 if ((srsdomaininfo = constmap(&mapsrsdomains,"*",1)) == 0) return 0; // '*' means always SRS
98 else alwaysrewrite = 1;
99 }
100
101 if (*srsdomaininfo) {
102 i = str_chr(srsdomaininfo,'|'); // multiple cookies; separated by ' '
103 if (srsdomaininfo[i] == '|') {
104 srsdomaininfo[i] = 0;
105 j = str_chr(srsdomaininfo + i + 1,'|');
106 if (srsdomaininfo[i + j + 1] == '|') {
107 srsdomaininfo[i + j + 1] = 0;
108 sendhost = srsdomaininfo + i + j + 2; // separator: - + =
109 }
110 separator = srsdomaininfo[i + 1];
111 }
113 if (!stralloc_0(&cookie)) die_nomem();
114 if (!stralloc_copys(&srshost,"")) die_nomem();
115 if (*sendhost) {
116 j = str_len(sendhost);
117 if (sendhost[j - 1] == '.') {
119 if (!stralloc_cats(&srshost,host)) die_nomem();
120 } else
122 } else
124 if (!stralloc_0(&srshost)) die_nomem();
125 } else
126 die_control();
127
128 /* Check options */
129
130 while ((opt = getoptb(argc,(char **)argv,"pP")) != opteof)
131 switch (opt) {
132 case 'p': flagpassthrough = 0; break;
133 case 'P': flagpassthrough = 100; break;
134 }
135 argv += optind - 1;
136
137 if (!flagforward) {
138 logmsg(WHO,flagpassthrough,INFO,B("not srsforwarding: ",sender));
140 }
141
142 /* Let's go SRS rewrite */
143
144 srs_init(&srs);
145
146 if (separator == '-' || separator == '+' || separator == '=') // '=' is default
147 srs.separator = separator;
148 if (alwaysrewrite)
149 srs.alwaysrewrite = alwaysrewrite;
150
151 if (!stralloc_copys(&srs.secrets,"")) die_nomem();
152 for (j = 0; j <= cookie.len; j++) {
153 ch = cookie.s[j];
154 if (ch == ' ') { ch = '\0'; srs.numsecrets++; }
155 if (!stralloc_append(&srs.secrets,&ch)) die_nomem();
156 }
157 if (!stralloc_append(&srs.secrets,&ch)) die_nomem();
158
160 logmsg(WHO,100,FATAL,B("Unable to srsforward: ",sender," ",srs_errorstr(r)));
161
162 if (qmail_open(&qqt) == -1)
163 logmsg(WHO,111,FATAL,"unable to fork: ");
165 if (buffer_copy(&bo,&bi) != 0)
166 logmsg(WHO,111,FATAL,"unable to read message: ");
167 buffer_flush(&bo);
168
169 num[fmt_ulong(num,qmail_qp(&qqt))] = 0;
170
172 while (*++argv) qmail_to(&qqt,*argv);
173 qqx = qmail_close(&qqt);
174 if (*qqx) logmsg(WHO,*qqx == 'D' ? 100 : 111,FATAL,qqx + 1);
175 logmsg(WHO,0,LOG,B(srssender.s,": qp ",num));
176}
char auto_qmail[]
int main()
Definition: chkshsgr.c:6
int constmap_init(struct constmap *cm, char *s, int len, int flagcolon)
Definition: constmap.c:35
int control_readfile(stralloc *sa, char *fn, int flagme)
Definition: control.c:87
int stralloc_copys(stralloc *, char const *)
void _exit(int)
stralloc sender
Definition: fastforward.c:71
char * dtline
Definition: fastforward.c:70
char buf[100+FMT_ULONG]
Definition: hier.c:11
char host[256]
Definition: hostname.c:5
int fd
int j
Definition: qmail-send.c:926
void qmail_to(struct qmail *, char *)
Definition: qmail.c:83
#define BUFSIZE_LINE
Definition: qmail.h:8
void qmail_from(struct qmail *, char *)
Definition: qmail.c:73
void qmail_put(struct qmail *, char *, int)
Definition: qmail.c:63
char * qmail_close(struct qmail *)
Definition: qmail.c:90
unsigned long qmail_qp(struct qmail *)
Definition: qmail.c:53
void qmail_puts(struct qmail *, char *)
Definition: qmail.c:68
int qmail_open(struct qmail *)
Definition: qmail.c:21
void die_control()
Definition: srsforward.c:23
stralloc srssender
Definition: srsforward.c:31
void die_nomem()
Definition: srsforward.c:22
stralloc cookie
Definition: srsforward.c:32
struct qmail qqt
Definition: srsforward.c:25
stralloc srshost
Definition: srsforward.c:30
int flagforward
Definition: srsforward.c:35
char outbuf[BUFSIZE_LINE]
Definition: srsforward.c:53
char num[FMT_ULONG]
Definition: srsforward.c:57
buffer bi
Definition: srsforward.c:54
int flagpassthrough
Definition: srsforward.c:36
srs_ctx srs
Definition: srsforward.c:33
char * srsdomaininfo
Definition: srsforward.c:27
buffer bo
Definition: srsforward.c:55
#define WHO
Definition: srsforward.c:20
char inbuf[BUFSIZE_LINE]
Definition: srsforward.c:52
stralloc srsdomains
Definition: srsforward.c:28
ssize_t mywrite(int fd, char *buf, size_t len)
Definition: srsforward.c:46
struct constmap mapsrsdomains
Definition: srsforward.c:29
stralloc sendhost
Definition: srsq.c:26
int srs_init(srs_ctx *)
Definition: srsq.c:62
const char * srs_errorstr(int)
Definition: srsq.c:29
int srs_forward(srs_ctx *, stralloc *, const char *, const char *)
Definition: srsq.c:360
#define SRS_SUCCESS
Definition: srsq.h:28
Definition: qmail.h:14
Definition: srsq.h:65
int alwaysrewrite
Definition: srsq.h:77
char separator
Definition: srsq.h:69
stralloc secrets
Definition: srsq.h:67
int numsecrets
Definition: srsq.h:68