s/qmail 4.2.29a
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 "buffer.h"
12#include "str.h"
13#include "fmt.h"
14#include "stralloc.h"
15#include "logmsg.h"
16#include "srs2.h"
17
18#define WHO "srsforward"
19
20void die_nomem() { logmsg(WHO,111,FATAL,"out of memory"); }
21void die_control() { logmsg(WHO,110,FATAL,"Unable to read control files"); }
22
23struct qmail qqt;
24char *srsdomaininfo = 0;
25stralloc srsdomains = {0};
27stralloc srshost = {0};
28stralloc srserror = {0};
29
38static int srserror_str(int code) {
39 if (!stralloc_copys(&srserror,"SRS: ")) die_nomem();
40 if (!stralloc_cats(&srserror,srs_strerror(code))) die_nomem();
41 if (!stralloc_0(&srserror)) die_nomem();
42 return -3;
43}
44
45ssize_t mywrite(int fd,char *buf,int len)
46{
47 qmail_put(&qqt,buf,len);
48 return len;
49}
50
51char inbuf[BUFFER_INSIZE];
52char outbuf[1];
53buffer bi = BUFFER_INIT(read,0,inbuf,sizeof(inbuf));
54buffer bo = BUFFER_INIT(mywrite,-1,outbuf,sizeof(outbuf));
55
56char num[FMT_ULONG];
57
58int main(int argc,char **argv)
59{
60 int i, j, r;
61 char *qqx;
62 srs_t *srs;
63 stralloc cookie = {0};
64 char separator = '=';
65 char srssender[512];
66 char *host = 0;
67 char *sender = 0;
68 char *dtline = 0;
69 char *sendhost = 0;
70 int alwaysrewrite = 0;
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)) return 0; // domain blacklisted
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 }
112 if (!stralloc_copys(&cookie,srsdomaininfo)) die_nomem();
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] == '.') {
118 if (!stralloc_copys(&srshost,sendhost)) die_nomem();
119 if (!stralloc_cats(&srshost,host)) die_nomem();
120 } else
121 if (!stralloc_copys(&srshost,sendhost)) die_nomem();
122 } else
124 if (!stralloc_0(&srshost)) die_nomem();
125 } else
126 die_control();
127
128 /* Let's go SRS rewrite */
129
130 srs = srs_new();
131
132 if (separator == '-' || separator == '+' || separator == '=') { // '=' is default
133 r = srs_set_separator(srs,separator);
134 if (r != SRS_SUCCESS) return srserror_str(r);
135 }
136 if (alwaysrewrite) {
137 r = srs_set_alwaysrewrite(srs,alwaysrewrite);
138 if (r != SRS_SUCCESS) return srserror_str(r);
139 }
140
141 for (j = 0, i = 0; j < cookie.len; j++) {
142 if (cookie.s[j] == ' ' || cookie.s[j] == '\0' ) {
143 cookie.s[j] = '\0';
144 r = srs_add_secret(srs,cookie.s + i);
145 if (r != SRS_SUCCESS) return srserror_str(r);
146 i = j + 1;
147 if (cookie.s[i] == ' ') { j++; continue; }
148 }
149 }
150
151 if ((r = srs_forward(srs,srssender,sizeof(srssender),sender,srshost.s)) != SRS_SUCCESS)
152 logmsg(WHO,100,FATAL,B("Unable to forward: ",sender," ",srs_strerror(r)));
153
154 if (qmail_open(&qqt) == -1)
155 logmsg(WHO,111,FATAL,"unable to fork: ");
157 if (buffer_copy(&bo,&bi) != 0)
158 logmsg(WHO,111,FATAL,"unable to read message: ");
159 buffer_flush(&bo);
160
161 num[fmt_ulong(num,qmail_qp(&qqt))] = 0;
162
163 qmail_from(&qqt,srssender);
164 while (*++argv) qmail_to(&qqt,*argv);
165 qqx = qmail_close(&qqt);
166 if (*qqx) logmsg(WHO,*qqx == 'D' ? 100 : 111,FATAL,qqx + 1);
167 logmsg(WHO,0,LOG,B(srssender,": qp ",num));
168
169}
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:86
int stralloc_copys(stralloc *, char const *)
stralloc sender
Definition: fastforward.c:71
char * dtline
Definition: fastforward.c:70
char buf[100+FMT_ULONG]
Definition: hier.c:10
char host[256]
Definition: hostname.c:5
int fd
unsigned long code
Definition: qmail-remote.c:576
int j
Definition: qmail-send.c:920
void qmail_to(struct qmail *, char *)
Definition: qmail.c:83
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
int srs_set_separator(srs_t *srs, char value)
Definition: srs2.c:153
int srs_add_secret(srs_t *, const char *)
Definition: srs2.c:129
srs_t * srs_new()
Definition: srs2.c:99
int srs_forward(srs_t *, char *, int, const char *, const char *)
Definition: srs2.c:520
#define SRS_SUCCESS
Definition: srs2.h:40
const char * srs_strerror(int)
Definition: srs2.c:66
void die_control()
Definition: srsforward.c:21
ssize_t mywrite(int fd, char *buf, int len)
Definition: srsforward.c:45
void die_nomem()
Definition: srsforward.c:20
char inbuf[BUFFER_INSIZE]
Definition: srsforward.c:51
struct qmail qqt
Definition: srsforward.c:23
stralloc srshost
Definition: srsforward.c:27
stralloc srserror
Definition: srsforward.c:28
char num[FMT_ULONG]
Definition: srsforward.c:56
buffer bi
Definition: srsforward.c:53
char * srsdomaininfo
Definition: srsforward.c:24
char outbuf[1]
Definition: srsforward.c:52
buffer bo
Definition: srsforward.c:54
#define WHO
Definition: srsforward.c:18
stralloc srsdomains
Definition: srsforward.c:25
struct constmap mapsrsdomains
Definition: srsforward.c:26
Definition: srs2.h:79
Definition: qmail.h:6