ipsec up|down|route|delete require a connection name
[strongswan.git] / src / ipsec / ipsec.in
1 #! /bin/sh
2 # prefix command to run stuff from our programs directory
3 # Copyright (C) 1998-2002  Henry Spencer.
4 # Copyright (C) 2006 Andreas Steffen
5 # Copyright (C) 2006 Martin Willi 
6
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the
9 # Free Software Foundation; either version 2 of the License, or (at your
10 # option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
11
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 # for more details.
16 #
17 # RCSID $Id: ipsec.in,v 1.13 2006/03/09 20:09:33 as Exp $
18
19 # name and version of the ipsec implementation
20 IPSEC_NAME="@IPSEC_NAME@"
21 IPSEC_VERSION="U@IPSEC_VERSION@/K`uname -r`"
22
23 # where the private directory and the config files are
24 IPSEC_DIR="@IPSEC_DIR@"
25 IPSEC_SBINDIR="@IPSEC_SBINDIR@"
26 IPSEC_CONFDIR="@IPSEC_CONFDIR@"
27 IPSEC_PIDDIR="@IPSEC_PIDDIR@"
28
29 IPSEC_STARTER_PID="${IPSEC_PIDDIR}/starter.pid"
30 IPSEC_PLUTO_PID="${IPSEC_PIDDIR}/pluto.pid"
31 IPSEC_CHARON_PID="${IPSEC_PIDDIR}/charon.pid"
32
33 IPSEC_WHACK="${IPSEC_DIR}/whack"
34 IPSEC_STROKE="${IPSEC_DIR}/stroke"
35 IPSEC_STARTER="${IPSEC_DIR}/starter"
36
37 export IPSEC_DIR IPSEC_SBINDIR IPSEC_CONFDIR IPSEC_PIDDIR IPSEC_VERSION IPSEC_NAME IPSEC_STARTER_PID IPSEC_PLUTO_PID IPSEC_CHARON_PID
38
39 IPSEC_DISTRO="Distributed by the Institute of Internet Technologies and Applications
40               University of Applied Sciences Rapperswil, Switzerland (ITA-HSR)"
41
42 case "$1" in
43 '')
44         echo "Usage: ipsec command argument ..."
45         echo "Use --help for list of commands, or see ipsec(8) manual page"
46         echo "or the $IPSEC_NAME documentation for names of the common ones."
47         echo "Most have their own manual pages, e.g. ipsec_auto(8)."
48         echo "See <http://www.strongswan.org> for more general info."
49         exit 0
50         ;;
51 --help)
52         echo "Usage: ipsec command argument ..."
53         echo "where command is one of:"
54         echo "  start|restart  arguments..."
55         echo "  update|reload|stop"
56         echo "  up|down|route|unroute <connectionname>"
57         echo "  status|statusall [<connectionname>]"
58         echo "  ready"
59         echo "  listalgs|listpubkeys|listcerts [--utc]"
60         echo "  listcacerts|listaacerts|listocspcerts [--utc]"
61         echo "  listacerts|listgroups|listcainfos [--utc]"
62         echo "  listcrls|listocsp|listcards|listall [--utc]"
63         echo "  rereadsecrets|rereadgroups"
64         echo "  rereadcacerts|rereadaacerts|rereadocspcerts"
65         echo "  rereadacerts|rereadcrls|rereadall"
66         echo "  purgeocsp"
67         echo "  scencrypt|scdecrypt <value> [--inbase <base>] [--outbase <base>] [--keyid <id>]"
68         echo "  barf"
69         echo "  openac"
70         echo "  pluto"
71         echo "  scepclient"
72         echo "  secrets"
73         echo "  starter"
74         echo "  version"
75         echo "  whack"
76         echo "  stoke"
77         echo
78         echo "Some of these functions have their own manual pages, e.g. ipsec_scepclient(8)."
79         exit 0
80         ;;
81 --versioncode)
82         echo "$IPSEC_VERSION"
83         exit 0
84         ;;
85 --copyright)
86         set _copyright
87         # and fall through, invoking "ipsec _copyright"
88         ;;
89 --directory)
90         echo "$IPSEC_DIR"
91         exit 0
92         ;;
93 --confdir)
94         echo "$IPSEC_CONFDIR"
95         exit 0
96         ;;
97 down)
98         shift
99         if [ "$#" -ne 1 ]
100         then
101             echo "Usage: ipsec down <connection name>"
102             exit 1
103         fi
104         if test -e $IPSEC_PLUTO_PID
105         then
106                 $IPSEC_WHACK --name "$1" --terminate
107         fi
108         if test -e $IPSEC_CHARON_PID
109         then
110                 $IPSEC_STROKE down "$1"
111         fi
112         exit 0
113         ;;
114 listalgs|listpubkeys|listcacerts|listaacerts|\
115 listocspcerts|listacerts|listgroups|\
116 listcainfos|listcrls|listocsp|listcards|\
117 purgeocsp|rereadsecrets|rereadgroups|\
118 rereadcacerts|rereadaacerts|rereadocspcerts|\
119 rereadacerts|rereadcrls|rereadall)
120         op="$1"
121         shift
122         if test -e $IPSEC_PLUTO_PID
123         then
124                 $IPSEC_WHACK "$@" "--$op"
125         fi
126         ;;
127 listcerts|listall)
128         op="$1"
129         shift
130         if test -e $IPSEC_PLUTO_PID
131         then
132                 $IPSEC_WHACK "$@" "--$op"
133         fi
134         if test -e $IPSEC_CHARON_PID
135         then
136                 $IPSEC_STROKE "$op"
137         fi
138         exit 0
139         ;;
140 ready)
141         shift
142         if test -e $IPSEC_PLUTO_PID
143         then
144                 $IPSEC_WHACK --listen
145         fi
146         exit 0
147         ;;
148 reload)
149         if test -e $IPSEC_STARTER_PID 
150         then
151             echo "Reloading strongSwan IPsec configuration..." >&2
152             kill -s USR1 `cat $IPSEC_STARTER_PID`
153         else
154             echo "ipsec starter is not running" >&2
155         fi
156         exit 0
157         ;;
158 restart)
159         $IPSEC_SBINDIR/ipsec stop
160         sleep 2
161         shift
162         $IPSEC_SBINDIR/ipsec start "$@"
163         exit 0
164         ;;
165 route|unroute)
166         op="$1"
167         shift
168         if [ "$#" -ne 1 ]
169         then
170             echo "Usage: ipsec $op <connection name>"
171             exit 1
172         fi
173         if test -e $IPSEC_PLUTO_PID
174         then
175                 $IPSEC_WHACK --name "$1" "--$op"
176         fi
177         exit 0
178         ;;
179 scencrypt|scdecrypt)
180         op="$1"
181         shift
182         if test -e $IPSEC_PLUTO_PID
183         then
184                 $IPSEC_WHACK "--$op" "$@"
185         fi
186         exit 0
187         ;;
188 secrets)
189         if test -e $IPSEC_PLUTO_PID
190         then
191                 $IPSEC_WHACK --rereadsecrets
192         fi
193         exit 0
194         ;;
195 start)
196         shift
197         exec $IPSEC_STARTER "$@"
198         ;;
199 status|statusall)
200         op="$1"
201         shift
202         if test $# -eq 0
203         then
204                 if test -e $IPSEC_PLUTO_PID
205                 then
206                         $IPSEC_WHACK "--$op"
207                 fi
208                 if test -e $IPSEC_CHARON_PID
209                 then
210                         $IPSEC_STROKE "$op"
211                 fi
212         else
213                 if test -e $IPSEC_PLUTO_PID
214                 then
215                         $IPSEC_WHACK --name "$1" "--$op"
216                 fi
217                 if test -e $IPSEC_CHARON_PID
218                 then
219                         $IPSEC_STROKE "$op" "$1"
220                 fi
221         fi
222         exit 0
223         ;;
224 stop)
225         if test -e $IPSEC_STARTER_PID 
226         then
227             echo "Stopping strongSwan IPsec..." >&2
228             kill `cat $IPSEC_STARTER_PID`
229         else
230             echo "ipsec starter is not running" >&2
231         fi
232         exit 0
233         ;;
234 up)
235         shift
236         if [ "$#" -ne 1 ]
237         then
238             echo "Usage: ipsec up <connection name>"
239             exit 1
240         fi
241         if test -e $IPSEC_PLUTO_PID
242         then
243                 $IPSEC_WHACK --name "$1" --initiate
244         fi
245         if test -e $IPSEC_CHARON_PID
246         then
247             $IPSEC_STROKE up "$1"
248         fi
249         exit 0
250         ;;
251 update)
252         if test -e $IPSEC_STARTER_PID 
253         then
254                 echo "Updating strongSwan IPsec configuration..." >&2
255                 kill -s HUP `cat $IPSEC_STARTER_PID`
256         else
257                 echo "ipsec starter is not running" >&2
258         fi
259         exit 0
260         ;;
261 version|--version)
262         echo "Linux $IPSEC_NAME $IPSEC_VERSION"
263         echo "See \`ipsec --copyright' for copyright information."
264         echo $IPSEC_DISTRO
265         exit 0
266         ;;
267 --*)
268         echo "$0: unknown option \`$1' (perhaps command name was omitted?)" >&2
269         exit 1
270         ;;
271 esac
272
273 cmd="$1"
274 shift
275
276 path="$IPSEC_DIR/$cmd"
277
278 if test ! -x "$path" 
279 then
280     path="$IPSEC_DIR/$cmd"
281     if test ! -x "$path"
282     then
283         echo "$0: unknown IPsec command \`$cmd' (\`ipsec --help' for list)" >&2
284         exit 1
285     fi
286 fi
287
288 exec $path "$@"