aboutsummaryrefslogtreecommitdiff
path: root/modules/private/ssh
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2019-10-19 10:26:54 +0200
committerIsmaël Bouya <ismael.bouya@normalesup.org>2019-10-19 10:26:54 +0200
commit1b9150a54b5cb0b512265cdcf88ad10c9a4a55b1 (patch)
tree048eea0830161eda32bdec86a167d9b20050cca2 /modules/private/ssh
parentb76b1d1f1678e329d0596e4403620f653a763d96 (diff)
downloadNix-1b9150a54b5cb0b512265cdcf88ad10c9a4a55b1.tar.gz
Nix-1b9150a54b5cb0b512265cdcf88ad10c9a4a55b1.tar.zst
Nix-1b9150a54b5cb0b512265cdcf88ad10c9a4a55b1.zip
Make ssh ldap connection by modules
Diffstat (limited to 'modules/private/ssh')
-rw-r--r--modules/private/ssh/default.nix59
-rwxr-xr-xmodules/private/ssh/ldap_authorized_keys.sh102
-rw-r--r--modules/private/ssh/ldap_regular.sh19
3 files changed, 76 insertions, 104 deletions
diff --git a/modules/private/ssh/default.nix b/modules/private/ssh/default.nix
index beedaff..d4c1ab3 100644
--- a/modules/private/ssh/default.nix
+++ b/modules/private/ssh/default.nix
@@ -1,7 +1,50 @@
1{ lib, pkgs, config, myconfig, ... }: 1{ lib, pkgs, config, myconfig, ... }:
2let
3 cfg = config.myServices.ssh;
4in
2{ 5{
6 options.myServices.ssh = let
7 module = lib.types.submodule {
8 options = {
9 snippet = lib.mkOption {
10 type = lib.types.lines;
11 description = ''
12 Snippet to use
13 '';
14 };
15 dependencies = lib.mkOption {
16 type = lib.types.listOf lib.types.package;
17 default = [];
18 description = ''
19 Dependencies of the package
20 '';
21 };
22 };
23 };
24 in {
25 predefinedModules = lib.mkOption {
26 type = lib.types.attrsOf module;
27 default = {
28 regular = {
29 snippet = builtins.readFile ./ldap_regular.sh;
30 };
31 };
32 readOnly = true;
33 description = ''
34 Predefined modules
35 '';
36 };
37 modules = lib.mkOption {
38 type = lib.types.listOf module;
39 default = [];
40 description = ''
41 List of modules to enable
42 '';
43 };
44 };
3 config = { 45 config = {
4 networking.firewall.allowedTCPPorts = [ 22 ]; 46 networking.firewall.allowedTCPPorts = [ 22 ];
47 } // (lib.mkIf (builtins.length cfg.modules > 0) {
5 48
6 services.openssh.extraConfig = '' 49 services.openssh.extraConfig = ''
7 AuthorizedKeysCommand /etc/ssh/ldap_authorized_keys 50 AuthorizedKeysCommand /etc/ssh/ldap_authorized_keys
@@ -24,11 +67,21 @@
24 # ssh is strict about parent directory having correct rights, don't 67 # ssh is strict about parent directory having correct rights, don't
25 # move it in the nix store. 68 # move it in the nix store.
26 environment.etc."ssh/ldap_authorized_keys" = let 69 environment.etc."ssh/ldap_authorized_keys" = let
70 deps = lib.lists.unique (
71 [ pkgs.which pkgs.openldap pkgs.stdenv.shellPackage pkgs.gnugrep pkgs.gnused pkgs.coreutils ]
72 ++ lib.flatten (map (v: v.dependencies) cfg.modules)
73 );
74 fullScript = pkgs.runCommand "ldap_authorized_keys" {
75 snippets = builtins.concatStringsSep "\n" (map (v: v.snippet) cfg.modules);
76 } ''
77 substituteAll ${./ldap_authorized_keys.sh} $out
78 chmod a+x $out
79 '';
27 ldap_authorized_keys = 80 ldap_authorized_keys =
28 pkgs.mylibs.wrap { 81 pkgs.mylibs.wrap {
29 name = "ldap_authorized_keys"; 82 name = "ldap_authorized_keys";
30 file = ./ldap_authorized_keys.sh; 83 file = fullScript;
31 paths = [ pkgs.which pkgs.gitolite pkgs.openldap pkgs.stdenv.shellPackage pkgs.gnugrep pkgs.gnused pkgs.coreutils ]; 84 paths = deps;
32 }; 85 };
33 in { 86 in {
34 enable = true; 87 enable = true;
@@ -36,5 +89,5 @@
36 user = "root"; 89 user = "root";
37 source = ldap_authorized_keys; 90 source = ldap_authorized_keys;
38 }; 91 };
39 }; 92 });
40} 93}
diff --git a/modules/private/ssh/ldap_authorized_keys.sh b/modules/private/ssh/ldap_authorized_keys.sh
index d556452..402f283 100755
--- a/modules/private/ssh/ldap_authorized_keys.sh
+++ b/modules/private/ssh/ldap_authorized_keys.sh
@@ -5,13 +5,7 @@ KEY="immaeSshKey"
5LDAP_BIND="cn=ssh,ou=services,dc=immae,dc=eu" 5LDAP_BIND="cn=ssh,ou=services,dc=immae,dc=eu"
6LDAP_PASS=$(cat /etc/ssh/ldap_password) 6LDAP_PASS=$(cat /etc/ssh/ldap_password)
7LDAP_HOST="ldap.immae.eu" 7LDAP_HOST="ldap.immae.eu"
8LDAP_MEMBER="cn=users,cn=ssh,ou=services,dc=immae,dc=eu"
9LDAP_GITOLITE_MEMBER="cn=users,cn=gitolite,ou=services,dc=immae,dc=eu"
10LDAP_PUB_RESTRICT_MEMBER="cn=restrict,cn=pub,ou=services,dc=immae,dc=eu"
11LDAP_PUB_FORWARD_MEMBER="cn=forward,cn=pub,ou=services,dc=immae,dc=eu"
12LDAP_BASE="dc=immae,dc=eu" 8LDAP_BASE="dc=immae,dc=eu"
13GITOLITE_SHELL=$(which gitolite-shell)
14ECHO=$(which echo)
15 9
16suitable_for() { 10suitable_for() {
17 type_for="$1" 11 type_for="$1"
@@ -52,101 +46,7 @@ ldap_search() {
52 46
53ldap_keys() { 47ldap_keys() {
54 user=$1; 48 user=$1;
55 if [[ $user == gitolite ]]; then 49 @snippets@
56 ldap_search '(&(memberOf='$LDAP_GITOLITE_MEMBER')('$KEY'=*))' $KEY | \
57 while read line ;
58 do
59 if [ ! -z "$line" ]; then
60 if [[ $line == dn* ]]; then
61 user=$(sed -n 's/.*uid=\([^,]*\).*/\1/p' <<< "$line")
62 if [ -n "$user" ]; then
63 if [[ $user == "immae" ]] || [[ $user == "denise" ]]; then
64 # Capitalize first letter (backward compatibility)
65 user=$(sed -r 's/^([a-z])/\U\1/' <<< "$user")
66 fi
67 else
68 # Service fake user
69 user=$(sed -n 's/.*cn=\([^,]*\).*/\1/p' <<< "$line")
70 fi
71 elif [[ $line == $KEY* ]]; then
72 key=$(clean_key_line git "$line")
73 if [ ! -z "$key" ]; then
74 if [[ $key != *$'\n'* ]] && [[ $key == ssh-* ]]; then
75 echo -n 'command="'$GITOLITE_SHELL' '$user'",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty '
76 echo $key
77 fi
78 fi
79 fi
80 fi
81 done
82 exit 0
83 elif [[ $user == pub ]]; then
84 ldap_search '(&(memberOf='$LDAP_PUB_RESTRICT_MEMBER')('$KEY'=*))' $KEY | \
85 while read line ;
86 do
87 if [ ! -z "$line" ]; then
88 if [[ $line == dn* ]]; then
89 echo ""
90 user=$(sed -n 's/.*uid=\([^,]*\).*/\1/p' <<< "$line")
91 echo "# $user"
92 elif [[ $line == $KEY* ]]; then
93 key=$(clean_key_line pub "$line")
94 key_forward=$(clean_key_line forward "$line")
95 if [ ! -z "$key" ]; then
96 if [[ $key != *$'\n'* ]] && [[ $key == ssh-* ]]; then
97 echo -n 'command="/etc/profiles/per-user/pub/bin/restrict '$user'" '
98 echo $key
99 fi
100 elif [ ! -z "$key_forward" ]; then
101 if [[ $key_forward != *$'\n'* ]] && [[ $key_forward == ssh-* ]]; then
102 echo "# forward only"
103 echo -n 'no-pty,no-X11-forwarding,command="'$ECHO' forward only" '
104 echo $key_forward
105 fi
106 fi
107 fi
108 fi
109 done
110
111 echo ""
112 ldap_search '(&(memberOf='$LDAP_PUB_FORWARD_MEMBER')('$KEY'=*))' $KEY | \
113 while read line ;
114 do
115 if [ ! -z "$line" ]; then
116 if [[ $line == dn* ]]; then
117 echo ""
118 user=$(sed -n 's/.*uid=\([^,]*\).*/\1/p' <<< "$line")
119 echo "# $user"
120 elif [[ $line == $KEY* ]]; then
121 key=$(clean_key_line forward "$line")
122 if [ ! -z "$key" ]; then
123 if [[ $key != *$'\n'* ]] && [[ $key == ssh-* ]]; then
124 echo -n 'no-pty,no-X11-forwarding,command="'$ECHO' forward only" '
125 echo $key
126 fi
127 fi
128 fi
129 fi
130 done
131 exit 0
132 else
133 ldap_search '(&(memberOf='$LDAP_MEMBER')('$KEY'=*)(uid='$user'))' $KEY | \
134 while read line ;
135 do
136 if [ ! -z "$line" ]; then
137 if [[ $line == dn* ]]; then
138 user=$(sed -n 's/.*uid=\([^,]*\).*/\1/p' <<< "$line")
139 elif [[ $line == $KEY* ]]; then
140 key=$(clean_key_line ssh "$line")
141 if [ ! -z "$key" ]; then
142 if [[ $key != *$'\n'* ]] && [[ $key == ssh-* ]]; then
143 echo $key
144 fi
145 fi
146 fi
147 fi
148 done
149 fi
150} 50}
151 51
152ldap_keys $@ 52ldap_keys $@
diff --git a/modules/private/ssh/ldap_regular.sh b/modules/private/ssh/ldap_regular.sh
new file mode 100644
index 0000000..4c2f47e
--- /dev/null
+++ b/modules/private/ssh/ldap_regular.sh
@@ -0,0 +1,19 @@
1### This snippet is not standalone and must be integrated in the global ldap_authorized_keys.sh
2LDAP_MEMBER="cn=users,cn=ssh,ou=services,dc=immae,dc=eu"
3
4ldap_search '(&(memberOf='$LDAP_MEMBER')('$KEY'=*)(uid='$user'))' $KEY | \
5 while read line ;
6 do
7 if [ ! -z "$line" ]; then
8 if [[ $line == dn* ]]; then
9 user=$(sed -n 's/.*uid=\([^,]*\).*/\1/p' <<< "$line")
10 elif [[ $line == $KEY* ]]; then
11 key=$(clean_key_line ssh "$line")
12 if [ ! -z "$key" ]; then
13 if [[ $key != *$'\n'* ]] && [[ $key == ssh-* ]]; then
14 echo $key
15 fi
16 fi
17 fi
18 fi
19 done