s/qmail 4.2.29a
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 "srs2.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};
32stralloc srserror = {0};
33stralloc srshost = {0};
34
43static int srserror_str(int code) {
44 if (!stralloc_copys(&srserror,"SRS: ")) die_nomem();
45 if (!stralloc_cats(&srserror,srs_strerror(code))) die_nomem();
46 if (!stralloc_0(&srserror)) die_nomem();
47 return -3;
48}
49
50ssize_t mywrite(int fd,char *buf,int len)
51{
52 qmail_put(&qqt,buf,len);
53 return len;
54}
55
56char inbuf[BUFFER_INSIZE];
57char outbuf[1];
58buffer bi = BUFFER_INIT(read,0,inbuf,sizeof(inbuf));
59buffer bo = BUFFER_INIT(mywrite,-1,outbuf,sizeof(outbuf));
60
61char num[FMT_ULONG];
62
63int main()
64{
65 int i, j, r;
66 char *recipient;
67 char *dtline;
68 char *qqx;
69 srs_t *srs;
70 stralloc cookie = {0};
71 char separator = '=';
72 char srsrecipient[512];
73 char *host = 0;
74
75 sig_pipeignore();
76
77 recipient = env_get("RECIPIENT");
78 if (!recipient)
79 logmsg(WHO,100,FATAL,"RECIPIENT not set");
80 dtline = env_get("DTLINE");
81 if (!dtline)
82 logmsg(WHO,100,FATAL,"DTLINE not set");
83 host = env_get("HOST");
84 if (!host)
85 logmsg(WHO,100,FATAL,"HOST not set");
86
87 if (chdir(auto_qmail) == -1)
88 logmsg(WHO,111,FATAL,B("unable to chdir to: ",auto_qmail));
89
90 /* Check for particular virtual SRS domain user */
91
92 switch (control_readfile(&vdomains,"control/virtualdomains",0)) {
93 case -1: die_control();
94 case 0: if (!constmap_init(&mapvdomains,"",0,1)) die_nomem(); break;
95 case 1: if (!constmap_init(&mapvdomains,vdomains.s,vdomains.len,1)) die_nomem(); break;
96 }
97
98 j = str_len(host);
99 for (i = 0; i <= j; ++i)
100 if ((i == 0) || (host[i] == '.')) {
101 if (!stralloc_copys(&srshost,"")) die_nomem();
102 if (!stralloc_catb(&srshost,host + i,j - i)) die_nomem();
103 if ((srsdomaininfo = constmap(&mapvdomains,srshost.s,srshost.len)) != 0) goto SRSDOMAINS;
104 }
106
107 SRSDOMAINS:
108
109 switch (control_readfile(&srsdomains,"control/srsdomains",0)) {
110 case -1: die_control();
111 case 0: if (!constmap_init(&mapsrsdomains,"",0,1)) die_nomem(); break;
112 case 1: if (!constmap_init(&mapsrsdomains,srsdomains.s,srsdomains.len,1)) die_nomem(); break;
113 }
115 if ((srsdomaininfo = constmap(&mapsrsdomains,"*",1)) == 0) return 0;
116
117 if (*srsdomaininfo) {
118 i = str_chr(srsdomaininfo,'|'); // multiple cookies; separated by ' '
119 if (srsdomaininfo[i] == '|') {
120 srsdomaininfo[i] = 0;
121 separator = srsdomaininfo[i+1];
122 }
123 if (!stralloc_copys(&cookie,srsdomaininfo)) die_nomem();
124 if (!stralloc_0(&cookie)) die_nomem();
125 }
126
127 /* strip virtual user from recipient */
128
130 i = str_chr(recipient,*auto_break);
131 if (!case_diffb(recipient,i - 1,vdomainuser)) recipient += i + 1;
132 }
133
134 /* Let's go SRS reverse */
135
136 srs = srs_new();
137
138 if (separator == '-' || separator == '+' || separator == '=') { // '=' is default
139 r = srs_set_separator(srs,separator);
140 if (r != SRS_SUCCESS) return srserror_str(r);
141 }
142
143 for (j = 0, i = 0; j < cookie.len; j++) {
144 if (cookie.s[j] == ' ' || cookie.s[j] == '\0' ) {
145 cookie.s[j] = '\0';
146 r = srs_add_secret(srs,cookie.s + i);
147 if (r != SRS_SUCCESS) return srserror_str(r);
148 i = j + 1;
149 if (cookie.s[i] == ' ') { j++; continue; }
150 }
151 }
152
153 if ((r = srs_reverse(srs,srsrecipient,sizeof(srsrecipient),recipient)) != SRS_SUCCESS) {
154 logmsg(WHO,100,FATAL,B("unable to reverse: ",recipient," ",srs_strerror(r)));
155 }
156
157 if (qmail_open(&qqt) == -1)
158 logmsg(WHO,111,FATAL,"unable to fork: ");
160 if (buffer_copy(&bo,&bi) != 0)
161 logmsg(WHO,111,FATAL,"unable to read message: ");
162 buffer_flush(&bo);
163
164 num[fmt_ulong(num,qmail_qp(&qqt))] = 0;
165
166 qmail_from(&qqt,"");
167 qmail_to(&qqt,srsrecipient);
168 qqx = qmail_close(&qqt);
169 if (*qqx) logmsg(WHO,*qqx == 'D' ? 100 : 111,FATAL,qqx + 1);
170 logmsg(WHO,0,LOG,B(srsrecipient,": qp ",num));
171
172}
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:86
int stralloc_copys(stralloc *, char const *)
char * dtline
Definition: fastforward.c:70
stralloc recipient
Definition: fastforward.c:274
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
int srs_reverse(srs_t *, char *, int, const char *)
Definition: srs2.c:591
srs_t * srs_new()
Definition: srs2.c:99
#define SRS_SUCCESS
Definition: srs2.h:40
const char * srs_strerror(int)
Definition: srs2.c:66
void die_control()
Definition: srsreverse.c:23
ssize_t mywrite(int fd, char *buf, int len)
Definition: srsreverse.c:50
void die_nomem()
Definition: srsreverse.c:22
char * vdomainuser
Definition: srsreverse.c:26
stralloc vdomains
Definition: srsreverse.c:27
char inbuf[BUFFER_INSIZE]
Definition: srsreverse.c:56
struct constmap mapvdomains
Definition: srsreverse.c:28
struct qmail qqt
Definition: srsreverse.c:25
stralloc srshost
Definition: srsreverse.c:33
stralloc srserror
Definition: srsreverse.c:32
char num[FMT_ULONG]
Definition: srsreverse.c:61
buffer bi
Definition: srsreverse.c:58
char * srsdomaininfo
Definition: srsreverse.c:29
char outbuf[1]
Definition: srsreverse.c:57
buffer bo
Definition: srsreverse.c:59
#define WHO
Definition: srsreverse.c:20
int main()
Definition: srsreverse.c:63
stralloc srsdomains
Definition: srsreverse.c:30
struct constmap mapsrsdomains
Definition: srsreverse.c:31
Definition: srs2.h:79
Definition: qmail.h:6