Skip to content

Commit 9eeed07

Browse files
authored
Merge pull request #6064 from basvandervlies/smtpport
CFE-4648: cf-execd SMTP port configurable
2 parents 60c3114 + 62462b8 commit 9eeed07

6 files changed

Lines changed: 25 additions & 8 deletions

File tree

cf-execd/cf-execd-runner.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ static bool CompareResultEqualOrFiltered(const ExecConfig *config,
564564
#ifndef TEST_CF_EXECD
565565
int ConnectToSmtpSocket(const ExecConfig *config)
566566
{
567+
assert(config != NULL);
567568
struct hostent *hp = gethostbyname(config->mail_server);
568569
if (!hp)
569570
{
@@ -572,17 +573,11 @@ int ConnectToSmtpSocket(const ExecConfig *config)
572573
return -1;
573574
}
574575

575-
struct servent *server = getservbyname("smtp", "tcp");
576-
if (!server)
577-
{
578-
Log(LOG_LEVEL_ERR, "Mail report: unable to lookup smtp service. (getservbyname: %s)", GetErrorStr());
579-
return -1;
580-
}
581-
582576
struct sockaddr_in raddr;
583577
memset(&raddr, 0, sizeof(raddr));
584578

585-
raddr.sin_port = (unsigned int) server->s_port;
579+
/* conevrt the SMTP port in network byte order */
580+
raddr.sin_port = htons(config->mail_port);
586581
raddr.sin_addr.s_addr = ((struct in_addr *) (hp->h_addr))->s_addr;
587582
raddr.sin_family = AF_INET;
588583

cf-execd/exec-config.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ ExecConfig *ExecConfigNew(bool scheduled_run, const EvalContext *ctx, const Poli
151151
exec_config->ip_address = xstrdup(VIPADDRESS);
152152
exec_config->ip_addresses = GetIpAddresses(ctx);
153153

154+
struct servent *server = getservbyname("smtp", "tcp");
155+
if (!server)
156+
{
157+
exec_config->mail_port = 25;
158+
}
159+
else
160+
{
161+
exec_config->mail_port = ntohs(server->s_port);
162+
}
163+
154164
Seq *constraints = ControlBodyConstraints(policy, AGENT_TYPE_EXECUTOR);
155165
if (constraints)
156166
{
@@ -199,6 +209,11 @@ ExecConfig *ExecConfigNew(bool scheduled_run, const EvalContext *ctx, const Poli
199209
exec_config->mail_server = xstrdup(value);
200210
Log(LOG_LEVEL_DEBUG, "smtpserver '%s'", exec_config->mail_server);
201211
}
212+
else if (strcmp(cp->lval, CFEX_CONTROLBODY[EXEC_CONTROL_SMTPPORT].lval) == 0)
213+
{
214+
exec_config->mail_port = IntFromString(value);
215+
Log(LOG_LEVEL_DEBUG, "smtpport '%d'", exec_config->mail_port);
216+
}
202217
else if (strcmp(cp->lval, CFEX_CONTROLBODY[EXEC_CONTROL_EXECCOMMAND].lval) == 0)
203218
{
204219
free(exec_config->exec_command);
@@ -237,12 +252,14 @@ ExecConfig *ExecConfigNew(bool scheduled_run, const EvalContext *ctx, const Poli
237252

238253
ExecConfig *ExecConfigCopy(const ExecConfig *config)
239254
{
255+
assert(config != NULL);
240256
ExecConfig *copy = xcalloc(1, sizeof(ExecConfig));
241257

242258
copy->scheduled_run = config->scheduled_run;
243259
copy->exec_command = xstrdup(config->exec_command);
244260
copy->agent_expireafter = config->agent_expireafter;
245261
copy->mail_server = xstrdup(config->mail_server);
262+
copy->mail_port = config->mail_port;
246263
copy->mail_from_address = xstrdup(config->mail_from_address);
247264
copy->mail_to_address = xstrdup(config->mail_to_address);
248265
copy->mail_subject = xstrdup(config->mail_subject);

cf-execd/exec-config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ typedef struct
4040
char *mail_to_address;
4141
char *mail_subject;
4242
int mail_max_lines;
43+
unsigned int mail_port;
4344
// These two contain regular expression strings.
4445
Seq *mailfilter_include;
4546
Seq *mailfilter_exclude;

libpromises/cf3.defs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ typedef enum
515515
EXEC_CONTROL_EXECCOMMAND,
516516
EXEC_CONTROL_AGENT_EXPIREAFTER,
517517
EXEC_CONTROL_RUNAGENT_ALLOW_USERS,
518+
EXEC_CONTROL_SMTPPORT,
518519
EXEC_CONTROL_NONE
519520
} ExecControl;
520521

libpromises/mod_common.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ const ConstraintSyntax CFEX_CONTROLBODY[] = /* enum cfexcontrol */
394394
ConstraintSyntaxNewString("exec_command", CF_ABSPATHRANGE,"The full path and command to the executable run by default (overriding builtin)", SYNTAX_STATUS_NORMAL),
395395
ConstraintSyntaxNewInt("agent_expireafter", "0,10080", "Maximum agent runtime (in minutes). Default value: 120", SYNTAX_STATUS_NORMAL),
396396
ConstraintSyntaxNewStringList("runagent_socket_allow_users", "", "Users allowed to work with the runagent.socket to trigger agent runs", SYNTAX_STATUS_NORMAL),
397+
ConstraintSyntaxNewInt("smtpport", "0,10080", "Port used for sending mail", SYNTAX_STATUS_NORMAL),
397398
ConstraintSyntaxNewNull()
398399
};
399400

tests/unit/exec-config-test.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ static void exec_config_empty_cb(const EvalContext *ctx, const Policy *policy)
101101
/* FIXME: exec-config should provide default subject */
102102
assert_string_equal("", config->mail_subject);
103103
assert_int_equal(30, config->mail_max_lines);
104+
assert_int_equal(25, config->mail_port);
104105
assert_string_equal("localhost.localdomain", config->fq_name);
105106
assert_string_equal("127.0.0.100", config->ip_address);
106107
assert_string_equal("127.0.0.100 127.0.0.101", config->ip_addresses);
@@ -123,6 +124,7 @@ static void CheckFullExecConfig(const ExecConfig *config)
123124
assert_string_equal("cfengine_mail@example.org", config->mail_to_address);
124125
assert_string_equal("Test [localhost/127.0.0.1]", config->mail_subject);
125126
assert_int_equal(50, config->mail_max_lines);
127+
assert_int_equal(25, config->mail_port);
126128
assert_string_equal("localhost.localdomain", config->fq_name);
127129
assert_string_equal("127.0.0.100", config->ip_address);
128130
assert_string_equal("127.0.0.100 127.0.0.101", config->ip_addresses);

0 commit comments

Comments
 (0)