forked from nicm/fdm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
child-deliver.c
234 lines (202 loc) · 5.93 KB
/
child-deliver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* $Id$ */
/*
* Copyright (c) 2006 Nicholas Marriott <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include "fdm.h"
#include "match.h"
int child_deliver(struct child *, struct io *);
int
child_deliver(struct child *child, struct io *pio)
{
struct child_deliver_data *data = child->data;
struct account *a = data->account;
struct mail *m = data->mail;
struct msg msg;
struct msgbuf msgbuf;
int error = 0;
log_debug2("%s: deliver started, pid %ld", a->name, (long) getpid());
#ifdef HAVE_SETPROCTITLE
setproctitle("%s[%lu]", data->name, (u_long) geteuid());
#endif
/* Call the hook. */
memset(&msg, 0, sizeof msg);
data->hook(0, a, &msg, data, &msg.data.error);
/* Inform parent we're done. */
msg.type = MSG_DONE;
msg.id = 0;
msgbuf.buf = m->tags;
msgbuf.len = STRB_SIZE(m->tags);
if (privsep_send(pio, &msg, &msgbuf) != 0)
fatalx("privsep_send error");
do {
if (privsep_recv(pio, &msg, NULL) != 0)
fatalx("privsep_recv error");
} while (msg.type != MSG_EXIT);
return (error);
}
void
child_deliver_action_hook(pid_t pid, struct account *a, struct msg *msg,
struct child_deliver_data *data, int *result)
{
struct actitem *ti = data->actitem;
struct deliver_ctx *dctx = data->dctx;
struct mail *m = data->mail;
struct mail *md = &dctx->wr_mail;
/* Check if this is the parent. */
if (pid != 0) {
/* Use new mail if necessary. */
if (ti->deliver->type != DELIVER_WRBACK) {
xfree(dctx);
return;
}
if (*result != DELIVER_SUCCESS)
mail_destroy(md);
else {
mail_close(md);
if (mail_receive(m, msg, 0) != 0) {
log_warn("parent_deliver: can't receive mail");
*result = DELIVER_FAILURE;
}
}
xfree(dctx);
return;
}
dctx->udata = xmalloc(sizeof *dctx->udata);
dctx->udata->uid = data->uid;
dctx->udata->gid = data->gid;
dctx->udata->name = xstrdup(find_tag(m->tags, "user"));
dctx->udata->home = xstrdup(find_tag(m->tags, "home"));
log_debug2("%s: deliver user is: %s (%lu/%lu), home is: %s", a->name,
dctx->udata->name, (u_long) dctx->udata->uid,
(u_long) dctx->udata->gid, dctx->udata->home);
/* This is the child. do the delivery. */
*result = ti->deliver->deliver(dctx, ti);
if (ti->deliver->type != DELIVER_WRBACK || *result != DELIVER_SUCCESS) {
user_free(dctx->udata);
return;
}
user_free(dctx->udata);
mail_send(md, msg);
log_debug2("%s: using new mail, size %zu", a->name, md->size);
}
void
child_deliver_cmd_hook(pid_t pid, struct account *a, unused struct msg *msg,
struct child_deliver_data *data, int *result)
{
struct mail_ctx *mctx = data->mctx;
struct mail *m = data->mail;
struct match_command_data *cmddata = data->cmddata;
int flags, status, found = 0;
char *s, *cause, *lbuf, *out, *err, tag[24];
size_t llen;
struct cmd *cmd = NULL;
struct rmlist rml;
u_int i;
/* If this is the parent, do nothing. */
if (pid != 0) {
xfree(mctx);
return;
}
/* Sort out the command. */
s = replacepath(
&cmddata->cmd, m->tags, m, &m->rml, find_tag(m->tags, "home"));
if (s == NULL || *s == '\0') {
log_warnx("%s: empty command", a->name);
goto error;
}
log_debug2("%s: %s: started (ret=%d re=%s)", a->name, s, cmddata->ret,
cmddata->re.str == NULL ? "none" : cmddata->re.str);
flags = CMD_ONCE;
if (cmddata->pipe)
flags |= CMD_IN;
if (cmddata->re.str != NULL)
flags |= CMD_OUT;
cmd = cmd_start(s, flags, m->data, m->size, &cause);
if (cmd == NULL) {
log_warnx("%s: %s: %s", a->name, s, cause);
goto error;
}
llen = IO_LINESIZE;
lbuf = xmalloc(llen);
for (;;) {
/* Stop early if looking for regexp only. */
if (found && cmddata->ret == -1) {
log_debug3("%s: %s: found. stopping early", a->name, s);
status = 1;
break;
}
status = cmd_poll(
cmd, &out, &err, &lbuf, &llen, conf.timeout, &cause);
if (status == -1) {
log_warnx("%s: %s: %s", a->name, s, cause);
goto error;
}
if (status != 0)
break;
if (err != NULL)
log_warnx("%s: %s: %s", a->name, s, err);
if (out == NULL)
continue;
log_debug3("%s: %s: out: %s", a->name, s, out);
if (found)
continue;
found = re_string(&cmddata->re, out, &rml, &cause);
if (found == -1) {
log_warnx("%s: %s", a->name, cause);
goto error;
}
if (found != 1)
continue;
/* Save the matches. */
if (!rml.valid)
continue;
for (i = 0; i < NPMATCH; i++) {
if (!rml.list[i].valid)
break;
xsnprintf(tag, sizeof tag, "command%u", i);
add_tag(&m->tags, tag, "%.*s", (int) (rml.list[i].eo -
rml.list[i].so), out + rml.list[i].so);
}
}
status--;
log_debug2("%s: %s: returned %d, found %d", a->name, s, status, found);
cmd_free(cmd);
xfree(s);
xfree(lbuf);
status = cmddata->ret == status;
if (cmddata->ret != -1 && cmddata->re.str != NULL)
*result = (found && status) ? MATCH_TRUE : MATCH_FALSE;
else if (cmddata->ret != -1 && cmddata->re.str == NULL)
*result = status ? MATCH_TRUE : MATCH_FALSE;
else if (cmddata->ret == -1 && cmddata->re.str != NULL)
*result = found ? MATCH_TRUE : MATCH_FALSE;
else
*result = MATCH_ERROR;
return;
error:
if (cause != NULL)
xfree(cause);
if (cmd != NULL)
cmd_free(cmd);
if (s != NULL)
xfree(s);
if (lbuf != NULL)
xfree(lbuf);
*result = MATCH_ERROR;
}