From ed9792cd44ca96c74fd838723e414d1729548c87 Mon Sep 17 00:00:00 2001
From: Tim Wickberg <tim@schedmd.com>
Date: Thu, 15 Sep 2016 15:03:35 -0400
Subject: [PATCH] Add MailDomain option to qualify local usernames.

Will be appended to usernames if --mail-user is not explicitly set for the
job and email notifications are requested.

Bug 3089.
---
 NEWS                             | 1 +
 RELEASE_NOTES                    | 1 +
 doc/man/man5/slurm.conf.5        | 6 ++++++
 slurm/slurm.h.in                 | 1 +
 src/api/config_info.c            | 5 +++++
 src/common/read_config.c         | 4 +++-
 src/common/slurm_protocol_pack.c | 5 +++++
 src/slurmctld/agent.c            | 9 +++++++--
 src/slurmctld/proc_req.c         | 1 +
 9 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/NEWS b/NEWS
index 40d0dea9ef8..721c1e95f51 100644
--- a/NEWS
+++ b/NEWS
@@ -32,6 +32,7 @@ documents those changes that are of interest to users and administrators.
  -- Log if job --bb option contains any unrecognized content.
  -- Display configured and allocated tres for nodes in scontrol show nodes.
  -- Change all memory values (in MB) to uint64_t to accommodate > 2TB per node.
+ -- Add MailDomain option to qualify email addresses.
 
 * Changes in Slurm 17.02.0pre1
 ==============================
diff --git a/RELEASE_NOTES b/RELEASE_NOTES
index 3156805f022..2152bfc6a1d 100644
--- a/RELEASE_NOTES
+++ b/RELEASE_NOTES
@@ -80,6 +80,7 @@ In connection_arg_t: Added bool persist
 In slurmctld_lock_t: Added federation
 In will_run_response_msg_t: Added double sys_usage_per to report back how busy a
 	cluster is.
+In slurm_ctl_conf: Added mail_domain.
 
 Added the following struct definitions
 ======================================
diff --git a/doc/man/man5/slurm.conf.5 b/doc/man/man5/slurm.conf.5
index 14a3c002a5b..9e17c691152 100644
--- a/doc/man/man5/slurm.conf.5
+++ b/doc/man/man5/slurm.conf.5
@@ -1313,6 +1313,12 @@ in the C standard ctime() function form without the year but
 including the microseconds, the daemon's process ID and the current thread name
 and ID.
 
+.TP
+\fBMailDomain\fR
+Domain name to qualify usernames if email address is not explicitly given
+with the "--mail-user" option. If unset, the local MTA will need to qualify
+local address itself.
+
 .TP
 \fBMailProg\fR
 Fully qualified pathname to the program used to send email per user request.
diff --git a/slurm/slurm.h.in b/slurm/slurm.h.in
index 2c9f0ecf241..20cb3950a8f 100644
--- a/slurm/slurm.h.in
+++ b/slurm/slurm.h.in
@@ -2632,6 +2632,7 @@ typedef struct slurm_ctl_conf {
 	char *licenses;		/* licenses available on this cluster */
 	char *licenses_used;	/* licenses used on this cluster */
 	uint16_t log_fmt;       /* Log file timestamp format */
+	char *mail_domain;	/* default domain to append to usernames */
 	char *mail_prog;	/* pathname of mail program */
 	uint32_t max_array_sz;	/* Maximum job array size */
 	uint32_t max_job_cnt;	/* maximum number of active jobs */
diff --git a/src/api/config_info.c b/src/api/config_info.c
index 14af9a8928f..9adb304083c 100644
--- a/src/api/config_info.c
+++ b/src/api/config_info.c
@@ -980,6 +980,11 @@ extern void *slurm_ctl_conf_2_key_pairs (slurm_ctl_conf_t* slurm_ctl_conf_ptr)
 	key_pair->value = xstrdup(slurm_ctl_conf_ptr->licenses_used);
 	list_append(ret_list, key_pair);
 
+	key_pair = xmalloc(sizeof(config_key_pair_t));
+	key_pair->name = xstrdup("MailDomain");
+	key_pair->value = xstrdup(slurm_ctl_conf_ptr->mail_domain);
+	list_append(ret_list, key_pair);
+
 	key_pair = xmalloc(sizeof(config_key_pair_t));
 	key_pair->name = xstrdup("MailProg");
 	key_pair->value = xstrdup(slurm_ctl_conf_ptr->mail_prog);
diff --git a/src/common/read_config.c b/src/common/read_config.c
index 523f60d6fa6..6be863ae671 100644
--- a/src/common/read_config.c
+++ b/src/common/read_config.c
@@ -245,6 +245,7 @@ s_p_options_t slurm_conf_options[] = {
 	{"Layouts", S_P_STRING},
 	{"Licenses", S_P_STRING},
 	{"LogTimeFormat", S_P_STRING},
+	{"MailDomain", S_P_STRING},
 	{"MailProg", S_P_STRING},
 	{"MaxArraySize", S_P_UINT32},
 	{"MaxJobCount", S_P_UINT32},
@@ -3327,10 +3328,11 @@ _validate_and_set_defaults(slurm_ctl_conf_t *conf, s_p_hashtbl_t *hashtbl)
 	} else
 		conf->log_fmt = LOG_FMT_ISO8601_MS;
 
+	s_p_get_string(&conf->mail_domain, "MailDomain", hashtbl);
+
 	if (!s_p_get_string(&conf->mail_prog, "MailProg", hashtbl))
 		conf->mail_prog = xstrdup(DEFAULT_MAIL_PROG);
 
-
 	if (!s_p_get_uint32(&conf->max_array_sz, "MaxArraySize", hashtbl))
 		conf->max_array_sz = DEFAULT_MAX_ARRAY_SIZE;
 	else if (conf->max_array_sz > 4000001) {
diff --git a/src/common/slurm_protocol_pack.c b/src/common/slurm_protocol_pack.c
index b53ae926752..3e6bf4b552e 100644
--- a/src/common/slurm_protocol_pack.c
+++ b/src/common/slurm_protocol_pack.c
@@ -6624,6 +6624,7 @@ _pack_slurm_ctl_conf_msg(slurm_ctl_conf_info_msg_t * build_ptr, Buf buffer,
 		packstr(build_ptr->licenses_used, buffer);
 
 		pack32(build_ptr->max_array_sz, buffer);
+		packstr(build_ptr->mail_domain, buffer);
 		packstr(build_ptr->mail_prog, buffer);
 		pack32(build_ptr->max_job_cnt, buffer);
 		pack32(build_ptr->max_job_id, buffer);
@@ -7532,6 +7533,8 @@ _unpack_slurm_ctl_conf_msg(slurm_ctl_conf_info_msg_t **build_buffer_ptr,
 				       &uint32_tmp, buffer);
 
 		safe_unpack32(&build_ptr->max_array_sz, buffer);
+		safe_unpackstr_xmalloc(&build_ptr->mail_domain,
+				       &uint32_tmp, buffer);
 		safe_unpackstr_xmalloc(&build_ptr->mail_prog,
 				       &uint32_tmp, buffer);
 		safe_unpack32(&build_ptr->max_job_cnt, buffer);
@@ -7929,6 +7932,7 @@ _unpack_slurm_ctl_conf_msg(slurm_ctl_conf_info_msg_t **build_buffer_ptr,
 				       &uint32_tmp, buffer);
 
 		safe_unpack32(&build_ptr->max_array_sz, buffer);
+		build_ptr->mail_domain = NULL;
 		safe_unpackstr_xmalloc(&build_ptr->mail_prog,
 				       &uint32_tmp, buffer);
 		safe_unpack32(&build_ptr->max_job_cnt, buffer);
@@ -8327,6 +8331,7 @@ _unpack_slurm_ctl_conf_msg(slurm_ctl_conf_info_msg_t **build_buffer_ptr,
 				       &uint32_tmp, buffer);
 
 		safe_unpack32(&build_ptr->max_array_sz, buffer);
+		build_ptr->mail_domain = NULL;
 		safe_unpackstr_xmalloc(&build_ptr->mail_prog,
 				       &uint32_tmp, buffer);
 		safe_unpack32(&build_ptr->max_job_cnt, buffer);
diff --git a/src/slurmctld/agent.c b/src/slurmctld/agent.c
index e6da9a7059f..4a490057976 100644
--- a/src/slurmctld/agent.c
+++ b/src/slurmctld/agent.c
@@ -1744,9 +1744,14 @@ extern void mail_job_info (struct job_record *job_ptr, uint16_t mail_type)
 	char job_time[128], term_msg[128];
 	mail_info_t *mi = _mail_alloc();
 
-	if (!job_ptr->mail_user)
+	if (!job_ptr->mail_user) {
 		mi->user_name = uid_to_string((uid_t)job_ptr->user_id);
-	else
+		/* unqualified sender, append MailDomain if set */
+		if (slurmctld_conf.mail_domain) {
+			xstrcat(mi->user_name, "@");
+			xstrcat(mi->user_name, slurmctld_conf.mail_domain);
+		}
+	} else
 		mi->user_name = xstrdup(job_ptr->mail_user);
 
 	/* Use job array master record, if available */
diff --git a/src/slurmctld/proc_req.c b/src/slurmctld/proc_req.c
index ef19b8c64f2..00421278e36 100644
--- a/src/slurmctld/proc_req.c
+++ b/src/slurmctld/proc_req.c
@@ -750,6 +750,7 @@ static void _fill_ctld_conf(slurm_ctl_conf_t * conf_ptr)
 	conf_ptr->licenses_used       = licenses_used;
 	conf_ptr->log_fmt             = conf->log_fmt;
 
+	conf_ptr->mail_domain         = xstrdup(conf->mail_domain);
 	conf_ptr->mail_prog           = xstrdup(conf->mail_prog);
 	conf_ptr->max_array_sz        = conf->max_array_sz;
 	conf_ptr->max_job_cnt         = conf->max_job_cnt;
-- 
GitLab