ezmlmx 0.68
ezmlmx
Loading...
Searching...
No Matches
ezmlm-gate.c
Go to the documentation of this file.
1#include <sys/types.h>
2#include <sys/stat.h>
3#include <unistd.h>
4#include "buffer.h"
5#include "stralloc.h"
6#include "error.h"
7#include "env.h"
8#include "sig.h"
9#include "str.h"
10#include "seek.h"
11#include "wait.h"
12#include "exit.h"
13#include "getconf.h"
14#include "auto_bin.h"
15#include "getoptb.h"
16#include "errtxt.h"
17#include "idx.h"
18#include "subscribe.h"
19#include "wrap.h"
20#include "auto_version.h"
21#include "logmsg.h"
22
23#define WHO "ezmlm-gate"
24
30
31
32static void die_usage() { logmsg(WHO,100,USAGE,"ezmlm-gate [-cCmMpPqrRsSvV] dir [moddir [...]]"); }
33static void die_nomem() { logmsg(WHO,111,FATAL,ERR_NOMEM); }
34
35stralloc line = {0};
36stralloc cmds = {0};
37stralloc sendopt = {0};
38stralloc storeopt = {0};
39
40char szchar[2] = "-";
41 int child;
42 const char *pmod;
43
44int mailprog(char *s)
45{
46 int r;
47
48 if ((child = wrap_fork()) == 0)
49 wrap_execsh(s);
50 /* parent */
51 /* 100 perm error, 111 temp, 99 dom ok */
52 /* 0 rec ok, others bounce */
53 switch ((r = wrap_waitpid(child))) {
54 case 0: case 99: case 100: break;
55 case 111: logmsg(WHO,111,FATAL,ERR_CHILD_TEMP);
56 default: logmsg(WHO,100,FATAL,ERR_REJECT); /* other errors => bounce */
57 }
58 if (seek_begin(0) == -1) /* rewind */
59 logmsg(WHO,111,FATAL,ERR_SEEK_INPUT);
60 return r;
61}
62
63void main( int argc,char **argv)
64{
65 char *dir;
66 char *sender;
67 char *moddir;
68 const char *queryext = (char *) 0;
69 int opt;
70 int ret = 0;
71 int dontact = 0;
72 unsigned int i,j;
73 const char *program;
74 stralloc *opts;
75
76 umask(022);
77 sig_pipeignore();
78
79 /* storeopts to ezmlm-store only. */
80 /* Others to both (ezmlm-store may pass them on to ezmlm-send. */
81
82 if (!stralloc_copys(&sendopt,"-")) die_nomem();
83 if (!stralloc_copys(&storeopt,"-")) die_nomem();
84
85 while ((opt = getoptb(argc,argv,"0cCmMpPq:Q:sSrRt:T:vVyY")) != opteof)
86 switch(opt) { /* pass on unrecognized options */
87 case 'c': /* ezmlm-send flags */
88 case 'C':
89 case 'r':
90 case 'Q':
91 case 'R': szchar[0] = opt;
92 if (!stralloc_append(&sendopt,szchar)) die_nomem();
93 if (!stralloc_append(&storeopt,szchar)) die_nomem(); break;
94 case 'm': /* ezmlm-store flags */
95 case 'M':
96 case 'p':
97 case 'P':
98 case 's':
99 case 'S':
100 case 'y':
101 case 'Y': szchar[0] = opt;
102 if (!stralloc_append(&storeopt,szchar)) die_nomem(); break;
103 case 'q': if (optarg) queryext = optarg; break;
104 case 'v':
105 case 'V': logmsg(WHO,0,VERSION,auto_version);
106 case '0': dontact = 1; break; break;
107 default: die_usage(); /* ezmlm-store flags */
108 }
109
110 dir = argv[optind++];
111 if (!dir) die_usage();
112 if (chdir(dir) == -1)
113 logmsg(WHO,111,FATAL,B(ERR_SWITCH,dir));
114
115 sender = env_get("SENDER");
116
117 pmod = (char *)0;
118
119 if (queryext) {
120 getconf(&cmds,queryext,1,dir);
121 i = 0;
122 for (j = 0;j < cmds.len; ++j)
123 if (!cmds.s[j]) {
124 switch (cmds.s[i]) {
125 case '\0': case '#': break; /* ignore blank/comment */
126 case '|': ret = mailprog(cmds.s + i + 1); break;
127 default: ret = mailprog(cmds.s + i); break;
128 }
129 if (ret) break;
130 i = j + 1;
131 }
132 if (!ret || ret == 99) /* 111 => temp error */
133 pmod = ""; /* 0, 99 => post; other => moderate */
134 }
135 moddir = argv[optind++];
136 if (moddir && !ret) { /* if exit 0 and moddir, add issub */
137 pmod = (char *)0;
138 while (moddir && !pmod && sender) {
139 pmod = issub(moddir,sender,(char *)0);
140 closesql();
141 moddir = argv[optind++];
142 }
143 }
144
145 if (pmod) {
146 program = "/ezmlm-send";
147 opts = &sendopt;
148 }
149 else {
150 program = "/ezmlm-store";
151 opts = &storeopt;
152 }
153
154 if (dontact) {
155 buffer_puts(buffer_2,auto_bin);
156 buffer_puts(buffer_2,program);
157 buffer_put(buffer_2," ", 1);
158 buffer_put(buffer_2,opts->s, opts->len);
159 buffer_put(buffer_2," ", 1);
160 buffer_puts(buffer_2,dir);
161 buffer_putsflush(buffer_2,"\n");
162 return;
163 }
164
165 if ((child = wrap_fork()) == 0)
166 wrap_execbin(program, opts, dir);
167 /* parent */
169}
int issub()
Returns (char *) to match if userhost is in the subscriber database dbname, 0 otherwise....
const char auto_version[]
void wrap_execsh(const char *command)
Definition wrap_execsh.c:9
void wrap_exitcode(int pid)
int wrap_fork(void)
Definition wrap_fork.c:15
int wrap_waitpid(int pid)
void wrap_execbin(const char *program, struct stralloc *opts, const char *dir)
Error messages. If you translate these, I would urge you to keep the English version as well....
#define ERR_NOMEM
Definition errtxt.h:14
#define ERR_REJECT
Definition errtxt.h:145
#define ERR_SWITCH
Definition errtxt.h:42
#define ERR_SEEK_INPUT
Definition errtxt.h:27
#define ERR_CHILD_TEMP
Definition errtxt.h:107
void closesql(void)
close connection to SQL server, if open
Definition opensql.c:21
void die_nomem()
Definition getconf.c:17
int getconf(stralloc *sa, const char *fn, int flagrequired, const char *dir)
Definition getconf.c:21
#define WHO
Definition author.c:1
stralloc moddir
Definition ezmlm-get.c:71
char * dir
int main()
Definition ezmlm-weed.c:69
const char auto_bin[]
Definition auto_bin.c:1
int opt
Definition ezmlm-cron.c:53
stralloc storeopt
Definition ezmlm-gate.c:38
int mailprog(char *s)
Definition ezmlm-gate.c:44
stralloc cmds
Definition ezmlm-gate.c:36
char szchar[2]
Definition ezmlm-gate.c:40
const char * pmod
Definition ezmlm-gate.c:42
int child
Definition ezmlm-cgi.c:143
const char * logmsg(const char *dir, unsigned long num, unsigned long listno, unsigned long subs, int done)
Definition loginfo.c:32