Skip to content

Commit 55433bb

Browse files
author
Vincent Danjean
committed
[pam] rewrite pam_oar_adopt and its documentation
1 parent 70fc66b commit 55433bb

File tree

4 files changed

+145
-39
lines changed

4 files changed

+145
-39
lines changed

Makefiles/node.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ SBINDIR_FILES=$(SRCDIR)/tools/oarnodecheck/oarnodecheckrun.in \
99
SHAREDIR_FILES= $(SRCDIR)/scripts/prologue \
1010
$(SRCDIR)/scripts/epilogue \
1111
$(SRCDIR)/tools/sshd_config.in \
12-
$(SRCDIR)/scripts/oar-node-service
12+
$(SRCDIR)/scripts/oar-node-service \
13+
$(SRCDIR)/tools/$(OARSH_DIR)/pam_oar_adopt.conf
1314

1415
MAN8DIR_FILES = $(SRCDIR)/man/man8/oarnodecheckrun.8 \
1516
$(SRCDIR)/man/man8/pam_oar_adopt.8

sources/core/man/man8/pam_oar_adopt.pod

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ Please note that while using ssh is very convenient, B<oarsh> provides extra fea
2424

2525
=head1 CONFIGURATION
2626

27-
To B<enable> this feature, one must configure B<pam_oar_adopt> in PAM and make sure the B</etc/oar/pam_oar_adopt_enabled> file exists on nodes. Removing this file allows one to disable B<pam_oar_adopt> (let I<ssh> return to its normal behavior) without requiring to undo the whole PAM configuration.
27+
To B<enable> this feature, one must configure B<pam_oar_adopt> in PAM and activate it in its configuration file (B</etc/oar/pam_oar_adopt.conf>).
2828

29-
Also make sure the B<ssh> service (on port 22, not OAR's dedicated ssh service on port 6667) enables PAM. B</etc/ssh/sshd_config> must contain:
29+
=head2 PAM CONFIGURATION
30+
31+
Make sure the B<ssh> service (on port 22, not OAR's dedicated ssh service on port 6667) enables PAM. B</etc/ssh/sshd_config> must contain:
3032

3133
UsePAM yes
3234

@@ -38,17 +40,58 @@ Follows an example of configuration of PAM with B<pam_oar_adopt>:
3840

3941
The following can be set as the first PAM directive in common-account:
4042

41-
account required pam_exec.so quiet debug stdout /usr/sbin/pam_oar_adopt -a
43+
account required pam_exec.so quiet stdout /usr/sbin/pam_oar_adopt -a
4244

4345
=item B</etc/pam.d/common-session> and B</etc/pam.d/common-session-noninteractive>
4446

4547
The following can be set as the last PAM directives in common-session and common-session-noninteractive:
4648

47-
session required pam_exec.so stdout /usr/sbin/pam_oar_adopt -s
49+
session required pam_exec.so quiet stdout /usr/sbin/pam_oar_adopt -s
4850
session optional pam_env.so readenv=1 envfile=/var/lib/oar/pam.env
4951

5052
=back
5153

54+
On Debian-like systems, one can also use the B<pam-auth-update> command to configure PAM and, by default, this PAM profile is installed with the oar-node package.
55+
56+
=head2 PAM_OAR_ADOPT CONFIGURATION
57+
58+
The B</etc/oar/pam_oar_adopt.conf> file contains the following configuration options:
59+
60+
=over
61+
62+
=item B<MODE>
63+
weather B<pam_oar_adopt> is enabled or not. Possible values are:
64+
65+
=over
66+
67+
=item B<enforced>: B<pam_oar_adopt> is enabled and will prevent any ssh connection to nodes that are not properly reserved.
68+
69+
=item B<disabled>: B<pam_oar_adopt> is disabled.
70+
71+
=back
72+
73+
By default, B<pam_oar_adopt> is disabled.
74+
75+
[DEPRECATED] For compatibility reasons, if the B<MODE> is not set and the B</etc/oar/pam_oar_adopt_enabled> file is present, then B<pam_oar_adopt> is enabled.
76+
77+
=item B<WARN>
78+
79+
In B<disabled> mode, B<pam_oar_adopt> will warn users about what would have been done if it was enabled. Possible values are:
80+
81+
=over
82+
83+
=item B<yes>: warn users (default).
84+
85+
=item B<no>: do not warn users about B<pam_oar_adopt> doing nothing.
86+
87+
=back
88+
89+
=item B<USER_UID_MIN>
90+
91+
In B<enforced> mode, B<pam_oar_adopt> will ignore (not prevent) ssh connections from users with a UID lower than B<USER_UID_MIN>. This is useful to allow system users to connect to nodes without being part of a job. The default value is 1000.
92+
93+
=back
94+
5295
=head1 NOTES
5396

5497
It is a good practice to prevent users to connect to OAR nodes outside of jobs (except system users: at least root and the B<oar> user).

sources/core/tools/oarsh/pam_oar_adopt

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,45 @@ CGROUP_MOUNT_POINT=$(sed -ne 's/^[^ ]\+ \([^ ]\+\) cgroup2 .*/\1/p' /proc/mounts
1616
OAR_CGROUP_BASE="$CGROUP_MOUNT_POINT/oar.slice"
1717
USER_UID_MIN=1000
1818

19+
exit_with_warning_or_error() {
20+
if [ "$MODE" = "enforced" ]; then
21+
printf "$1\n\n" 1>&2
22+
exit 1
23+
elif [ "$WARN" = true ]; then
24+
printf "pam_oar_adopt disabled, warns only: %s\n" "$1"
25+
fi
26+
exit 0
27+
}
28+
29+
pam_oar_adopt_load_config() {
30+
if [ -f "/etc/oar/pam_oar_adopt_enabled" ]; then
31+
MODE="enforced"
32+
else
33+
MODE="disabled"
34+
fi
35+
WARN=true
36+
37+
if [ -r /etc/oar/pam_oar_adopt.conf ]; then
38+
. /etc/oar/pam_oar_adopt.conf
39+
fi
40+
41+
case "$MODE" in
42+
enforced|disabled) ;;
43+
*)
44+
echo "Invalid value for MODE in /etc/oar/pam_oar_adopt.conf." 1>&2
45+
exit 0
46+
;;
47+
esac
48+
case "${WARN,,}" in
49+
1|yes|on|true) WARN=true ;;
50+
0|no|off|false) WARN=false ;;
51+
*)
52+
echo "Invalid value for WARN in /etc/oar/pam_oar_adopt.conf." 1>&2
53+
WARN=true
54+
;;
55+
esac
56+
}
57+
1958
get_oar_cgroups_of_user() {
2059
# Exit if the PAM service is not sshd (e.g. su, su-l, sudo, sudo-i, ...)
2160
if [ "$PAM_SERVICE" != "sshd" ]; then
@@ -38,21 +77,17 @@ get_oar_cgroups_of_user() {
3877
exit 0
3978
fi
4079

41-
# Exit if oar.slice does not exist (job_resource_manager did not run yet, not job run since last reboot)
4280
if [ ! -d "$OAR_CGROUP_BASE" ]; then
43-
cat <<EOF 1>&2
44-
No running job found for $OAR_USER on this node.
45-
46-
EOF
47-
exit 1
81+
# Exit if oar.slice does not exist (job_resource_manager did not run yet, not job run since last reboot)
82+
exit_with_warning_or_error "No running job found for $OAR_USER on this node."
4883
fi
4984

5085
readarray -t OAR_SLICES < <( cd "$OAR_CGROUP_BASE" && ls -d "oar-u$USER_UID.slice/oar-u$USER_UID"-j*.slice 2>/dev/null )
5186
OAR_SLICE=${OAR_SLICES[0]}
5287
}
5388

5489
pam_account() {
55-
pam_oar_adopt_enabled_or_exit
90+
pam_oar_adopt_load_config
5691

5792
get_oar_cgroups_of_user
5893

@@ -61,34 +96,22 @@ pam_account() {
6196
# - the user has more than one cgroup or one but without all cores
6297
# - the user has one cgroup with all cores
6398
if [ -z "$OAR_SLICE" ]; then
64-
cat <<EOF 1>&2
65-
No running job found for $OAR_USER on this node.
66-
67-
EOF
68-
exit 1
99+
exit_with_warning_or_error "No running job found for $OAR_USER on this node."
69100
elif [ ${#OAR_SLICES[*]} -ne 1 ]; then
70-
cat << EOF 1>&2
71-
Cannot connect to node using 'ssh', because it appears there are more than one
101+
exit_with_warning_or_error "Cannot connect to node using 'ssh', because it appears there are more than one
72102
job on running on the node. Make sure to only have one job on the node, or use
73-
'oarsh' to connect to a specific job.
74-
75-
EOF
76-
exit 1
103+
'oarsh' to connect to a specific job."
77104
elif [ "$(< "$OAR_CGROUP_BASE/$OAR_SLICE"/cpuset.cpus.effective)" != "$(< "${OAR_CGROUP_BASE}"/cpuset.cpus.effective)" ]; then
78-
cat << EOF 1>&2
79-
Cannot connect to node using 'ssh' because not all its compute resources
105+
exit_with_warning_or_error "Cannot connect to node using 'ssh' because not all its compute resources
80106
(e.g. CPU cores or threads) are assigned to the job which reserves it.
81-
Reserve the whole node, or use 'oarsh' instead.
82-
83-
EOF
84-
exit 1
107+
Reserve the whole node, or use 'oarsh' instead."
85108
else
86109
exit 0
87110
fi
88111
}
89112

90113
pam_session() {
91-
pam_oar_adopt_enabled_or_exit
114+
pam_oar_adopt_load_config
92115

93116
get_oar_cgroups_of_user
94117

@@ -110,16 +133,23 @@ pam_session() {
110133
fi
111134

112135
if [ ! -d /var/lib/oar ]; then
113-
echo "OAR directory not found: /var/lib/oar." 1>&2
114-
exit 1
136+
exit_with_warning_or_error "OAR directory not found: /var/lib/oar."
115137
fi
116138

139+
if [ "$MODE" != enforced ]; then
140+
if [ "$WARN" = true ]; then
141+
printf "pam_oar_adopt disabled, not adding job into cgroup.\n"
142+
fi
143+
return 0
144+
fi
145+
# We are in enforced mode here, handling cgroup assignment
146+
117147
# To have the job environment variables, we create a symkink to the already
118148
# created job environment file and let pam_env load it.
119149
OAR_JOB_ENV=${OAR_SLICE%.slice}
120150
OAR_JOB_ENV=/var/lib/oar/${OAR_USER}_${OAR_JOB_ENV#*-j}.env
121151
if [ ! -e "$OAR_JOB_ENV" ]; then
122-
echo "Could not find job env file." 1>&2
152+
echo "OAR directory not found: /var/lib/oar." 1>&2
123153
exit 1
124154
fi
125155

@@ -134,12 +164,6 @@ pam_session() {
134164
done
135165
}
136166

137-
pam_oar_adopt_enabled_or_exit() {
138-
if [ ! -f "/etc/oar/pam_oar_adopt_enabled" ]; then
139-
exit 0
140-
fi
141-
}
142-
143167
if [ $# -eq 0 ]; then
144168
echo "Please provide the PAM mode." 1>&2
145169
exit 1
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# configuration file for the pam_oar_adopt PAM module
2+
#
3+
# This file is sourced by the pam_oar_adopt script run by the pam_exec module
4+
# that is used when the sshd PAM service is invoked. For any other PAM service,
5+
# the pam_oar_adopt script does nothing.
6+
# When activated, the pam_oar_adopt script will enforce the OAR job cgroup
7+
# assignment and environment variables setting for the user if the user has
8+
# one, and only one valid OAR job on the node using all the ressources. Else,
9+
# the direct ssh connection will be refused (oarsh must be then used).
10+
11+
# The mode of the pam_oar_adopt module. Possible values are:
12+
# - enforced: the module will enforce the OAR job cgroup assignment and
13+
# environment variables setting. If the user does not have a valid OAR job
14+
# on the node, the ssh connection will be refused.
15+
# - disabled: the module will not enforce the OAR job cgroup assignment and
16+
# environment variables setting. Only a warning message will be printed
17+
# by default (see below). A warning message will also be printed if the
18+
# connection would have been refused if in enforced mode (but the connection
19+
# will not be refused). This mode is useful for debugging or testing to be sure
20+
# that the module works as expected without blocking the connection.
21+
# By default, the module is in disabled mode.
22+
# [DEPRECATED] For compatibility reasons, the module is in enforced mode if the
23+
# file /etc/oar/pam_oar_adopt_enabled exists.
24+
#MODE=disabled
25+
26+
# Verbosity of the pam_oar_adopt module. Possible values are:
27+
# - true: the module will print a warning message in disabled mode (default)
28+
# - false: the module will not print warning messages in disabled mode
29+
# false is useful if you want to keep this module in the PAM stack but
30+
# without any effect (including warning messages).
31+
# In enforced mode, this setting has no effect.
32+
#WARN=true
33+
34+
# USER_UID_MIN is the minimum user id for which the pam_oar_adopt module will
35+
# enforce the OAR job cgroup assignment and environment variables setting. If
36+
# the user id is inferior to USER_UID_MIN, the pam_oar_adopt module does
37+
# nothing. Default value is 1000.
38+
#USER_UID_MIN=1000

0 commit comments

Comments
 (0)