Skip to content

Commit 75924cb

Browse files
committed
Basic escape detection
Signed-off-by: Scott R. Shinn <scott@atomicorp.com>
1 parent 431d8c8 commit 75924cb

2 files changed

Lines changed: 45 additions & 5 deletions

File tree

install.sh

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,26 @@ ConfigureClient()
461461

462462

463463

464+
##########
465+
# xml_escape()
466+
# Escape user input for safe inclusion in ossec.conf XML element text.
467+
##########
468+
xml_escape()
469+
{
470+
if [ $# -lt 1 ] || [ -z "$1" ]; then
471+
echo ""
472+
return
473+
fi
474+
printf '%s' "$1" | sed \
475+
-e 's/&/\&amp;/g' \
476+
-e 's/</\&lt;/g' \
477+
-e 's/>/\&gt;/g' \
478+
-e 's/"/\&quot;/g'
479+
}
480+
481+
482+
483+
464484
##########
465485
# ConfigureServer()
466486
##########
@@ -570,7 +590,14 @@ ConfigureServer()
570590
fi
571591
if [ "X${USER_SMTP_PASS}" = "X" ]; then
572592
$ECHO " - ${smtppass}: "
573-
read SMTP_PASS
593+
if [ -t 0 ] || [ -r /dev/tty ]; then
594+
stty -echo 2>/dev/null
595+
read -r SMTP_PASS < /dev/tty 2>/dev/null || read -r SMTP_PASS
596+
stty echo 2>/dev/null
597+
echo ""
598+
else
599+
read -r SMTP_PASS
600+
fi
574601
else
575602
SMTP_PASS=${USER_SMTP_PASS}
576603
fi
@@ -632,9 +659,11 @@ ConfigureServer()
632659
echo " <email_to>$EMAIL</email_to>" >> $NEWCONFIG
633660
echo " <smtp_server>$SMTP</smtp_server>" >> $NEWCONFIG
634661
if [ "X${AUTH_SMTP}" = "X${yes}" ]; then
662+
_smtp_user_esc=`xml_escape "${SMTP_USER}"`
663+
_smtp_pass_esc=`xml_escape "${SMTP_PASS}"`
635664
echo " <auth_smtp>yes</auth_smtp>" >> $NEWCONFIG
636-
echo " <smtp_user>$SMTP_USER</smtp_user>" >> $NEWCONFIG
637-
echo " <smtp_password>$SMTP_PASS</smtp_password>" >> $NEWCONFIG
665+
echo " <smtp_user>${_smtp_user_esc}</smtp_user>" >> $NEWCONFIG
666+
echo " <smtp_password>${_smtp_pass_esc}</smtp_password>" >> $NEWCONFIG
638667
fi
639668
if [ "X${SMTP_SECURE}" = "X${yes}" ]; then
640669
echo " <secure_smtp>yes</secure_smtp>" >> $NEWCONFIG
@@ -646,7 +675,8 @@ ConfigureServer()
646675
echo " <smtp_tls_verify>no</smtp_tls_verify>" >> $NEWCONFIG
647676
fi
648677
if [ "X${AUTH_SMTP}" = "X${yes}" ] && [ "X${SMTP_USER}" != "X" ]; then
649-
echo " <email_from>${SMTP_USER}</email_from>" >> $NEWCONFIG
678+
_email_from_esc=`xml_escape "${SMTP_USER}"`
679+
echo " <email_from>${_email_from_esc}</email_from>" >> $NEWCONFIG
650680
else
651681
echo " <email_from>ossecm@${HOST}</email_from>" >> $NEWCONFIG
652682
fi

src/monitord/sendcustomemail.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,19 @@ int OS_SendCustomEmail2(char **to, char *subject, char *fname, monitor_config *m
236236
upload_ctx.header_pos = 0;
237237

238238
/* Build URL */
239-
int port = mail->smtp_port > 0 ? mail->smtp_port : (mail->securesmtp ? 465 : 25);
239+
int port = mail->smtp_port;
240240
int n2;
241241

242+
if (port <= 0 || port > 65535) {
243+
if (mail->securesmtp) {
244+
port = 465;
245+
} else if (mail->authsmtp) {
246+
port = 587;
247+
} else {
248+
port = 25;
249+
}
250+
}
251+
242252
if (!is_valid_smtp_host(smtpserver)) {
243253
merror("%s: ERROR: Invalid SMTP server '%s' (contains invalid characters).", ARGV0, smtpserver);
244254
return (0);

0 commit comments

Comments
 (0)