Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions cf-execd/cf-execd-runner.c
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ static bool CompareResultEqualOrFiltered(const ExecConfig *config,
#ifndef TEST_CF_EXECD
int ConnectToSmtpSocket(const ExecConfig *config)
{
assert(config != NULL);
struct hostent *hp = gethostbyname(config->mail_server);
if (!hp)
{
Expand All @@ -572,17 +573,10 @@ int ConnectToSmtpSocket(const ExecConfig *config)
return -1;
}

struct servent *server = getservbyname("smtp", "tcp");
if (!server)
{
Log(LOG_LEVEL_ERR, "Mail report: unable to lookup smtp service. (getservbyname: %s)", GetErrorStr());
return -1;
}

struct sockaddr_in raddr;
memset(&raddr, 0, sizeof(raddr));

raddr.sin_port = (unsigned int) server->s_port;
raddr.sin_port = config->mail_port;
raddr.sin_addr.s_addr = ((struct in_addr *) (hp->h_addr))->s_addr;
raddr.sin_family = AF_INET;

Expand Down
8 changes: 8 additions & 0 deletions cf-execd/exec-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ ExecConfig *ExecConfigNew(bool scheduled_run, const EvalContext *ctx, const Poli
exec_config->agent_expireafter = 2 * 60; /* two hours */

exec_config->mail_server = xstrdup("");
exec_config->mail_port = 25;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it perhaps default to using the port returned from getservbyname("smtp", "tcp"), or does this always return 25?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a well know service and it is always defined as25. Like ssh is always default 22and you can override it.

exec_config->mail_from_address = xstrdup("");
exec_config->mail_to_address = xstrdup("");
exec_config->mail_subject = xstrdup("");
Expand Down Expand Up @@ -199,6 +200,11 @@ ExecConfig *ExecConfigNew(bool scheduled_run, const EvalContext *ctx, const Poli
exec_config->mail_server = xstrdup(value);
Log(LOG_LEVEL_DEBUG, "smtpserver '%s'", exec_config->mail_server);
}
else if (strcmp(cp->lval, CFEX_CONTROLBODY[EXEC_CONTROL_SMTPPORT].lval) == 0)
{
exec_config->mail_port = IntFromString(value);
Log(LOG_LEVEL_DEBUG, "smtpport '%d'", exec_config->mail_port);
}
else if (strcmp(cp->lval, CFEX_CONTROLBODY[EXEC_CONTROL_EXECCOMMAND].lval) == 0)
{
free(exec_config->exec_command);
Expand Down Expand Up @@ -237,12 +243,14 @@ ExecConfig *ExecConfigNew(bool scheduled_run, const EvalContext *ctx, const Poli

ExecConfig *ExecConfigCopy(const ExecConfig *config)
{
assert(config != NULL);
ExecConfig *copy = xcalloc(1, sizeof(ExecConfig));

copy->scheduled_run = config->scheduled_run;
copy->exec_command = xstrdup(config->exec_command);
copy->agent_expireafter = config->agent_expireafter;
copy->mail_server = xstrdup(config->mail_server);
copy->mail_port = config->mail_port;
copy->mail_from_address = xstrdup(config->mail_from_address);
copy->mail_to_address = xstrdup(config->mail_to_address);
copy->mail_subject = xstrdup(config->mail_subject);
Expand Down
1 change: 1 addition & 0 deletions cf-execd/exec-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ typedef struct
char *mail_to_address;
char *mail_subject;
int mail_max_lines;
unsigned int mail_port;
// These two contain regular expression strings.
Seq *mailfilter_include;
Seq *mailfilter_exclude;
Expand Down
1 change: 1 addition & 0 deletions libpromises/cf3.defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ typedef enum
EXEC_CONTROL_EXECCOMMAND,
EXEC_CONTROL_AGENT_EXPIREAFTER,
EXEC_CONTROL_RUNAGENT_ALLOW_USERS,
EXEC_CONTROL_SMTPPORT,
EXEC_CONTROL_NONE
} ExecControl;

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

Expand Down
2 changes: 2 additions & 0 deletions tests/unit/exec-config-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ static void exec_config_empty_cb(const EvalContext *ctx, const Policy *policy)
/* FIXME: exec-config should provide default subject */
assert_string_equal("", config->mail_subject);
assert_int_equal(30, config->mail_max_lines);
assert_int_equal(25, config->mail_port);
assert_string_equal("localhost.localdomain", config->fq_name);
assert_string_equal("127.0.0.100", config->ip_address);
assert_string_equal("127.0.0.100 127.0.0.101", config->ip_addresses);
Expand All @@ -123,6 +124,7 @@ static void CheckFullExecConfig(const ExecConfig *config)
assert_string_equal("cfengine_mail@example.org", config->mail_to_address);
assert_string_equal("Test [localhost/127.0.0.1]", config->mail_subject);
assert_int_equal(50, config->mail_max_lines);
assert_int_equal(25, config->mail_port);
assert_string_equal("localhost.localdomain", config->fq_name);
assert_string_equal("127.0.0.100", config->ip_address);
assert_string_equal("127.0.0.100 127.0.0.101", config->ip_addresses);
Expand Down
Loading