forked from nicm/fdm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
match-command.c
122 lines (102 loc) · 3.09 KB
/
match-command.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
/* $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 <string.h>
#include "fdm.h"
#include "match.h"
int match_command_match(struct mail_ctx *, struct expritem *);
void match_command_desc(struct expritem *, char *, size_t);
struct match match_command = {
"command",
match_command_match,
match_command_desc
};
int
match_command_match(struct mail_ctx *mctx, struct expritem *ei)
{
struct match_command_data *data = ei->data;
struct account *a = mctx->account;
struct mail *m = mctx->mail;
struct io *io = mctx->io;
struct msg msg;
struct msgbuf msgbuf;
struct userdata *ud;
char *user;
set_wrapped(m, '\n');
/*
* We are called as the child so to change uid this needs to be done
* largely in the parent.
*/
memset(&msg, 0, sizeof msg);
msg.type = MSG_COMMAND;
msg.id = m->idx;
msg.data.account = a;
msg.data.cmddata = data;
user = conf.cmd_user;
if (data->user.str != NULL)
user = replacestr(&data->user, m->tags, m, &m->rml);
if ((ud = user_lookup(user, conf.user_order)) == NULL) {
log_warnx("%s: bad user: %s", a->name, user);
return (MATCH_ERROR);
}
if (data->user.str != NULL)
xfree(user);
msg.data.uid = ud->uid;
msg.data.gid = ud->gid;
update_tags(&m->tags, ud);
user_free(ud);
msgbuf.buf = m->tags;
msgbuf.len = STRB_SIZE(m->tags);
mail_send(m, &msg);
if (privsep_send(io, &msg, &msgbuf) != 0)
fatalx("privsep_send error");
reset_tags(&m->tags);
mctx->msgid = msg.id;
return (MATCH_PARENT);
}
void
match_command_desc(struct expritem *ei, char *buf, size_t len)
{
struct match_command_data *data = ei->data;
char ret[11];
const char *type;
*ret = '\0';
if (data->ret != -1)
xsnprintf(ret, sizeof ret, "%d", data->ret);
type = data->pipe ? "pipe" : "exec";
if (data->re.str == NULL) {
if (data->user.str != NULL) {
xsnprintf(buf, len,
"%s \"%s\" user \"%s\" returns (%s, )",
type, data->cmd.str, data->user.str, ret);
} else {
xsnprintf(buf, len, "%s \"%s\" returns (%s, )", type,
data->cmd.str, ret);
}
} else {
if (data->user.str != NULL) {
xsnprintf(buf, len,
"%s \"%s\" user \"%s\" returns (%s, \"%s\")",
type, data->cmd.str, data->user.str, ret,
data->re.str);
} else {
xsnprintf(buf, len,
"%s \"%s\" returns (%s, \"%s\")",
type, data->cmd.str, ret, data->re.str);
}
}
}