Repository navigation
Expand file tree
/
Copy pathuser_net_daemon.sh
More file actions
292 lines (231 loc) · 6.97 KB
/
Copy pathuser_net_daemon.sh
File metadata and controls
292 lines (231 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/bin/bash
set -euo pipefail
# Emplacement partagé avec le daemon root.
BASE_DIR="/tmp/contnetd"
REQ_FIFO="$BASE_DIR/requests"
# État privé du script utilisateur courant.
STATE_DIR="${HOME}/.cache/contnet-user-$$"
mkdir -p "$STATE_DIR"
chmod 700 "$STATE_DIR"
RESP_FIFO="$STATE_DIR/resp"
PARENT_IFACE=""
SESSION_ID=""
GATEWAY=""
SUBNET=""
# Liste locale des namespaces applicatifs créés.
APPS_FILE="$STATE_DIR/apps"
: > "$APPS_FILE"
wait_iface() {
# Attend l'arrivée de l'interface déplacée par le daemon root.
local prefix="$1"
local iface
while sleep 0.2; do
for iface in $(ip -o link show | awk -F': ' '{print $2}' | cut -d'@' -f1); do
case "$iface" in
"$prefix"*)
echo "$iface"
return 0
;;
esac
done
done
}
prepare_dhclient() {
# Copie dhclient dans un répertoire contrôlé par l'utilisateur.
local dhclient_src=""
if [ -x /usr/sbin/dhclient ]; then
dhclient_src="/usr/sbin/dhclient"
elif [ -x /sbin/dhclient ]; then
dhclient_src="/sbin/dhclient"
else
echo "dhclient introuvable." >&2
exit 1
fi
cp "$dhclient_src" "$STATE_DIR/dhclient"
chmod 700 "$STATE_DIR/dhclient"
cp ./dhclient-script-simplifie.sh "$STATE_DIR/dhclient-script-simplifie.sh"
chmod 700 "$STATE_DIR/dhclient-script-simplifie.sh"
}
register_to_root_daemon() {
# Demande au daemon root la création d'une session réseau dédiée.
if [ ! -p "$REQ_FIFO" ]; then
echo "Erreur : daemon root absent. Lancez d'abord sudo ./cont_root_daemon.sh" >&2
exit 1
fi
rm -f "$RESP_FIFO"
mkfifo "$RESP_FIFO"
chmod 666 "$RESP_FIFO"
echo "$$ $RESP_FIFO" > "$REQ_FIFO"
read -r status SESSION_ID PARENT_IFACE GATEWAY SUBNET < "$RESP_FIFO"
rm -f "$RESP_FIFO"
if [ "$status" != "OK" ]; then
echo "Erreur daemon root : $status $SESSION_ID" >&2
exit 1
fi
}
start_app_namespace() {
# Lance un namespace applicatif isolé et renvoie son PID externe.
local app_name="$1"
local unshare_pid=""
local app_pid=""
unshare --net --uts --mount --ipc --pid --fork --mount-proc \
sh -c '
hostname "$1"
ip link set lo up
exec sleep infinity
' sh "$app_name" </dev/null >/dev/null 2>&1 &
unshare_pid="$!"
# Récupère le vrai processus enfant utilisé par nsenter.
for i in $(seq 1 50); do
app_pid="$(pgrep -P "$unshare_pid" | head -n 1 || true)"
if [ -n "$app_pid" ] && [ -e "/proc/$app_pid/ns/net" ]; then
echo "$app_pid"
return 0
fi
sleep 0.1
done
echo "Impossible de récupérer le PID externe du namespace applicatif." >&2
return 1
}
new_app() {
# Crée un namespace applicatif et lui attribue une interface macvlan.
local app_name="${1:-}"
local index
if [ -z "$app_name" ]; then
index="$(($(wc -l < "$APPS_FILE") + 1))"
app_name="app${index}"
fi
local iface="app$(($(wc -l < "$APPS_FILE") + 1))"
local app_pid
app_pid="$(start_app_namespace "$app_name")"
ip link del "$iface" 2>/dev/null || true
ip link add link "$PARENT_IFACE" name "$iface" type macvlan mode bridge
ip link set "$iface" netns "$app_pid"
nsenter -t "$app_pid" -n ip link set lo up
nsenter -t "$app_pid" -n ip link set "$iface" up
local lease_file="$STATE_DIR/dhclient-${iface}.leases"
local pid_file="$STATE_DIR/dhclient-${iface}.pid"
rm -f "$lease_file" "$pid_file"
touch "$lease_file"
# Demande une adresse DHCP depuis le namespace applicatif.
nsenter -t "$app_pid" -n "$STATE_DIR/dhclient" \
-q \
-sf "$STATE_DIR/dhclient-script-simplifie.sh" \
-lf "$lease_file" \
-pf "$pid_file" \
"$iface"
echo "$app_name $app_pid $iface" >> "$APPS_FILE"
echo "$app_name prêt."
}
list_apps() {
# Affiche les applications connues du script utilisateur.
if [ ! -s "$APPS_FILE" ]; then
echo "Aucune application."
return 0
fi
printf "%-15s %-10s %-10s\n" "NAME" "PID" "IFACE"
while read -r name pid iface; do
printf "%-15s %-10s %-10s\n" "$name" "$pid" "$iface"
done < "$APPS_FILE"
}
enter_app() {
# Entre dans le namespace applicatif demandé.
local app_name="${1:-}"
if [ -z "$app_name" ]; then
echo "Usage : enter <nom_app>" >&2
return 1
fi
local line
line="$(grep "^${app_name} " "$APPS_FILE" || true)"
if [ -z "$line" ]; then
echo "Application introuvable : $app_name" >&2
return 1
fi
local name pid iface
read -r name pid iface <<< "$line"
nsenter -t "$pid" -n -u -m -i --wd="$PWD" /bin/bash
}
delete_app() {
# Supprime le processus applicatif et retire son entrée de la liste.
local app_name="${1:-}"
if [ -z "$app_name" ]; then
echo "Usage : del <nom_app>" >&2
return 1
fi
local line
line="$(grep "^${app_name} " "$APPS_FILE" || true)"
if [ -z "$line" ]; then
echo "Application introuvable : $app_name" >&2
return 1
fi
local name pid iface
read -r name pid iface <<< "$line"
kill "$pid" 2>/dev/null || true
grep -v "^${app_name} " "$APPS_FILE" > "$APPS_FILE.tmp" || true
mv "$APPS_FILE.tmp" "$APPS_FILE"
echo "$app_name supprimée."
}
help_menu() {
cat <<HELP
Commandes :
new [nom] créer un namespace applicatif avec macvlan + DHCP
enter <nom> entrer dans le namespace applicatif
list lister les namespaces applicatifs
del <nom> supprimer le namespace applicatif
net afficher l'état réseau du namespace user+net
help afficher cette aide
quit quitter
HELP
}
cleanup() {
# Arrête les namespaces applicatifs et supprime l'état temporaire.
while read -r name pid iface; do
[ -z "${pid:-}" ] && continue
kill "$pid" 2>/dev/null || true
done < "$APPS_FILE"
rm -rf "$STATE_DIR"
}
trap cleanup EXIT INT TERM
register_to_root_daemon
PARENT_IFACE="$(wait_iface "$PARENT_IFACE")"
# Active l'interface parent qui servira de support aux macvlan applicatives.
ip link set lo up
ip link set "$PARENT_IFACE" up
ip link set "$PARENT_IFACE" promisc on
prepare_dhclient
help_menu
# Boucle interactive du gestionnaire utilisateur.
while true; do
printf "contnet> "
read -r cmd arg || break
case "$cmd" in
new)
new_app "${arg:-}" || true
;;
enter)
enter_app "${arg:-}" || true
;;
list)
list_apps
;;
del)
delete_app "${arg:-}" || true
;;
net)
ip -br addr
ip route
;;
help)
help_menu
;;
quit|exit)
break
;;
"")
;;
*)
echo "Commande inconnue : $cmd"
help_menu
;;
esac
done