s/qmail 4.4.12
Next generation secure email transport
Loading...
Searching...
No Matches
srsreverse.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 "auto_break.h"
12#include "buffer.h"
13#include "case.h"
14#include "str.h"
15#include "fmt.h"
16#include "stralloc.h"
17#include "logmsg.h"
18#include "srsq.h"
19
20#define WHO "srsreverse"
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;
26char *vdomainuser = 0;
27stralloc vdomains = {0};
29char *srsdomaininfo = 0;
30stralloc srsdomains = {0};
31stralloc srsrecipient = {0};
33stralloc srserror = {0};
34stralloc srshost = {0};
35srs_ctx *srs = {0};
36
45ssize_t mywrite(int fd,char *buf,size_t len)
46{
47 qmail_put(&qqt,buf,len);
48 return len;
49}
50
53buffer bi = BUFFER_INIT(buffer_unixread,0,inbuf,sizeof(inbuf));
54buffer bo = BUFFER_INIT(mywrite,-1,outbuf,sizeof(outbuf));
55
56char num[FMT_ULONG];
57
58int main()
59{
60 int i, j, r;
61 char *recipient;
62 char *sender;
63 char *dtline;
64 char *qqx;
65 stralloc cookie = {0};
66 stralloc srsrecipient = {0};
67 char separator = '=';
68 char *host = 0;
69
70 sig_pipeignore();
71
72 recipient = env_get("RECIPIENT");
73 if (!recipient)
74 logmsg(WHO,100,FATAL,"RECIPIENT not set");
75 sender = env_get("SENDER");
76 if (!sender)
77 logmsg(WHO,100,FATAL,"SENDER not set");
78 dtline = env_get("DTLINE");
79 if (!dtline)
80 logmsg(WHO,100,FATAL,"DTLINE not set");
81 host = env_get("HOST");
82 if (!host)
83 logmsg(WHO,100,FATAL,"HOST not set");
84
85 if (chdir(auto_qmail) == -1)
86 logmsg(WHO,111,FATAL,B("unable to chdir to: ",auto_qmail));
87
88 /* Check for particular virtual SRS domain user */
89
90 switch (control_readfile(&vdomains,"control/virtualdomains",0)) {
91 case -1: die_control();
92 case 0: if (!constmap_init(&mapvdomains,"",0,1)) die_nomem(); break;
93 case 1: if (!constmap_init(&mapvdomains,vdomains.s,vdomains.len,1)) die_nomem(); break;
94 }
95
96 j = str_len(host);
97 for (i = 0; i <= j; ++i)
98 if ((i == 0) || (host[i] == '.')) {
99 if (!stralloc_copys(&srshost,"")) die_nomem();
100 if (!stralloc_catb(&srshost,host + i,j - i)) die_nomem();
101 if ((srsdomaininfo = constmap(&mapvdomains,srshost.s,srshost.len)) != 0) goto SRSDOMAINS;
102 }
104
105 SRSDOMAINS:
106
107 switch (control_readfile(&srsdomains,"control/srsdomains",0)) {
108 case -1: die_control();
109 case 0: if (!constmap_init(&mapsrsdomains,"",0,1)) die_nomem(); break;
110 case 1: if (!constmap_init(&mapsrsdomains,srsdomains.s,srsdomains.len,1)) die_nomem(); break;
111 }
113 if ((srsdomaininfo = constmap(&mapsrsdomains,"*",1)) == 0) return 0;
114
115 if (*srsdomaininfo) {
116 i = str_chr(srsdomaininfo,'|'); // multiple cookies; separated by ' '
117 if (srsdomaininfo[i] == '|') {
118 srsdomaininfo[i] = 0;
119 separator = srsdomaininfo[i+1];
120 }
122 if (!stralloc_0(&cookie)) die_nomem();
123 }
124
125 /* strip virtual user from recipient */
126
128 i = str_chr(recipient,*auto_break);
129 if (!case_diffb(recipient,i - 1,vdomainuser)) recipient += i + 1;
130 }
131
132 /* Let's go SRS reverse */
133
134 srs_init(srs);
135
136 if (separator == '-' || separator == '+' || separator == '=') { // '=' is default
137 srs->separator = separator;
138 }
139
140 for (j = 0, i = 0; j < cookie.len; j++) {
141 if (cookie.s[j] == ' ' || cookie.s[j] == '\0' ) {
142 cookie.s[j] = '\0';
143 if (!stralloc_cats(&srs->secrets,cookie.s + i)) die_nomem();
144 i = j + 1;
145 if (cookie.s[i] == ' ') { j++; continue; }
146 }
147 }
148
149 switch ((r = srs_reverse(srs,&srsrecipient,recipient))) {
150 case SRS_SUCCESS: break;
152 logmsg(WHO,99,INFO,B("unable to reverse and mail bounced to: ",sender)); break;
153 default: logmsg(WHO,100,FATAL,B("unable to reverse: ",recipient," ",srs_errorstr(r)));
154 }
155
156 if (qmail_open(&qqt) == -1)
157 logmsg(WHO,111,FATAL,"unable to fork: ");
159 if (buffer_copy(&bo,&bi) != 0)
160 logmsg(WHO,111,FATAL,"unable to read message: ");
161 buffer_flush(&bo);
162
163 num[fmt_ulong(num,qmail_qp(&qqt))] = 0;
164
165 qmail_from(&qqt,"");
167 qqx = qmail_close(&qqt);
168 if (*qqx) logmsg(WHO,*qqx == 'D' ? 100 : 111,FATAL,qqx + 1);
169 logmsg(WHO,0,LOG,B(srsrecipient.s,": qp ",num));
170
171}
char auto_break[]
char auto_qmail[]
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 *)
stralloc sender
Definition: fastforward.c:71
char * dtline
Definition: fastforward.c:70
stralloc recipient
Definition: fastforward.c:274
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_MESS
Definition: qmail.h:7
#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
stralloc cookie
Definition: srsforward.c:32
int srs_init(srs_ctx *)
Definition: srsq.c:62
const char * srs_errorstr(int)
Definition: srsq.c:29
int srs_reverse(srs_ctx *, stralloc *, char *)
Definition: srsq.c:324
#define SRS_SUCCESS
Definition: srsq.h:28
#define SRS_ENOTSRSADDRESS
Definition: srsq.h:29
void die_control()
Definition: srsreverse.c:23
void die_nomem()
Definition: srsreverse.c:22
char * vdomainuser
Definition: srsreverse.c:26
stralloc vdomains
Definition: srsreverse.c:27
struct constmap mapvdomains
Definition: srsreverse.c:28
struct qmail qqt
Definition: srsreverse.c:25
srs_ctx * srs
Definition: srsreverse.c:35
stralloc srshost
Definition: srsreverse.c:34
stralloc srsrecipient
Definition: srsreverse.c:31
stralloc srserror
Definition: srsreverse.c:33
char outbuf[BUFSIZE_LINE]
Definition: srsreverse.c:52
char inbuf[BUFSIZE_MESS]
Definition: srsreverse.c:51
char num[FMT_ULONG]
Definition: srsreverse.c:56
buffer bi
Definition: srsreverse.c:53
char * srsdomaininfo
Definition: srsreverse.c:29
buffer bo
Definition: srsreverse.c:54
#define WHO
Definition: srsreverse.c:20
int main()
Definition: srsreverse.c:58
stralloc srsdomains
Definition: srsreverse.c:30
ssize_t mywrite(int fd, char *buf, size_t len)
Definition: srsreverse.c:45
struct constmap mapsrsdomains
Definition: srsreverse.c:32
Definition: qmail.h:14
Definition: srsq.h:65
char separator
Definition: srsq.h:69
stralloc secrets
Definition: srsq.h:67