summaryrefslogtreecommitdiff
path: root/ezjail-admin
blob: 6cd11a817219a354053c2ec669f59889f26c3079 (plain)
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
#!/bin/sh

# ugly: this variable is set during port install time
ezjail_prefix=EZJAIL_PREFIX
ezjail_etc=${ezjail_prefix}/etc
ezjail_jailcfgs=${ezjail_etc}/ezjail

if [ -f ${ezjail_etc}/ezjail.conf ]; then
  . ${ezjail_etc}/ezjail.conf;
fi

# set defaults
ezjail_jaildir=${ezjail_jaildir:-"/usr/jails"}
ezjail_jailtemplate=${ezjail_jailtemplate:-"$ezjail_jaildir/newjail"}
ezjail_jailbase=${ezjail_jailbase:-"$ezjail_jaildir/basejail"}
ezjail_jailfull=${ezjail_jailfull:-"$ezjail_jaildir/fulljail"}
ezjail_sourcetree=${ezjail_sourcetree:-"/usr/src"}

ezjail_mount_enable=${ezjail_mount_enable:-"YES"}
ezjail_devfs_enable=${ezjail_devfs_enable:-"YES"}
ezjail_devfs_ruleset=${ezjail_devfs_ruleset:-"devfsrules_jail"}
ezjail_procfs_enable=${ezjail_procfs_enable:-"YES"}
ezjail_fdescfs_enable=${ezjail_fdescfs_enable:-"YES"}

function exerr () { echo $*; exit 1; }

# check for command
[ "$1" ] || exerr "Usage: `basename $0` [create|delete|list|update] {params}"

case "$1" in
######################## ezjail-admin CREATE ########################
create)
  shift
  args=`getopt xf:r: $*`
  [ $? = 0 ] || exerr 'Usage: ezjail create [-f flavour] [-r jailroot] [-x] jailname jailip';

  newjail_root=
  newjail_flavour=
  newjail_flav=
  newjail_softlink=
  newjail_fill="YES"

  set -- $args
  for arg do
    case $arg in
      -x) newjail_fill="NO"; shift;;
      -r) newjail_root="$2"; shift 2;;
      -f) newjail_flavour="$2"; shift 2;;
      --) shift; break;;
    esac
  done;
  newjail_name=$1; newjail_ip=$2;

  # we need at least a name and an ip for new jail
  [ "$newjail_name" -a "$newjail_ip" -a $# = 2 ] || exerr 'Usage: ezjail create [-f flavour] [-r jailroot] [-x] jailname jailip'

  # check, whether IP is configured on a local interface, warn if it isnt
  ping -c 1 -m 1 -t 1 -q $newjail_ip > /dev/null
  [ $? = 0 ] || echo "Warning: IP $newjail_ip not configured on a local interface"

  # check, whether ezjail-update has been called. existence of
  # ezjail_jailbase is our indicator
  [ -d $ezjail_jailbase ] || exerr "Error: base jail does not exist. Please run 'ezjail-admin update' first"

  # relative paths don't make sense in rc.scripts
  [ ${ezjail_jaildir#/} = ${ezjail_jaildir} ] && exerr "Error: Need an absolute path in ezjail_jaildir, it is currently  set to: $ezjail_jaildir"

  # jail names must not have names that irritate file systems,
  # excluding dots from this list was done intentionally to
  # allow foo.com style directory names, however, the jail
  # name will be foo_com in most scripts
  newjail_name=`echo $newjail_name | tr /~ __`;
  newjail_root=${newjail_root:-"$ezjail_jaildir/$newjail_name"}
  newjail_nname=`echo $newjail_name | tr . _`;

  # if jail root specified on command line is not absolute,
  # make it absolute inside our jail directory
  [ ${newjail_root#/} = ${newjail_root} ] || newjail_root=$ezjail_jaildir/$newjail_root

  # if jail root specified on command line does not lie
  # within our jail directory, we need to create a softlink
  if [ ${newjail_root##${ezjail_jaildir}} = $newjail_root ]; then
    newjail_softlink=$ezjail_jaildir/`basename $newjail_root`
    [ -e $newjail_softlink -a $newjail_fill = "YES" ] && exerr "Error: an ezjail already exists at $newjail_softlink"
  fi

  # do some sanity checks on the selected flavour (if any)
  if [ "$newjail_flavour" ]; then
    # simple case wins, most often you won't have a ezjail.flavour.FLAV
    # AND a ./FLAV lying around. If you do, you won't need "./httpd"
    # but /ezjail_etc/ezjail.flavour.httpd, whatever ./httpd would be
    # For now exit with error, maybe just warn later.
    [ -f "$newjail_flavour" ] && newjail_flav=${newjail_flavour}

    # if flavour contains a '/', it aint a short name
    if [  ${newjail_flavour} = ${newjail_flavour%/*} -a \
       -f ${ezjail_etc}/ezjail.flavour.${newjail_flavour} ]; then
        [ $newjail_flav ] && exerr "Note: flavour ${newjail_flavour} conflicts with file ./${newjail_flavour}"
        $newjail_flav=${ezjail_etc}/ezjail.flavour.${newjail_flavour}
    fi
  fi

  # now take a copy of our template jail
  if [ "$newjail_fill" = "YES" ]; then
    mkdir -p ${newjail_root} && cd ${ezjail_jailtemplate} && \
    find * | cpio -p -v ${newjail_root}
  fi

  # if a soft link is necessary, create it now
  [ "$newjail_softlink" ] && ln -s $newjail_root $newjail_softlink

  # if the automount feature is not disabled, create an
  # fstab entry for new jail
  echo $ezjail_jailbase	$newjail_root/basejail	nullfs	ro	0	0 > /etc/fstab.$newjail_nname

  # now, where everything seems to have gone right,
  # create control file in ezjails config dir
  mkdir -p $ezjail_jailcfgs
  echo export jail_${newjail_nname}_hostname=\"${newjail_name}\"                 > ${ezjail_jailcfgs}/${newjail_nname}
  echo export jail_${newjail_nname}_ip=\"${newjail_ip}\"                        >> ${ezjail_jailcfgs}/${newjail_nname}
  echo export jail_${newjail_nname}_rootdir=\"${newjail_root}\"                 >> ${ezjail_jailcfgs}/${newjail_nname}
  echo export jail_${newjail_nname}_exec=\"/bin/sh /etc/rc\"                    >> ${ezjail_jailcfgs}/${newjail_nname}
  echo export jail_${newjail_nname}_mount_enable=\"${ezjail_mount_enable}\"     >> ${ezjail_jailcfgs}/${newjail_nname}
  echo export jail_${newjail_nname}_devfs_enable=\"${ezjail_devfs_enable}\"     >> ${ezjail_jailcfgs}/${newjail_nname}
  echo export jail_${newjail_nname}_devfs_ruleset=\"devfsrules_jail\"           >> ${ezjail_jailcfgs}/${newjail_nname}
  echo export jail_${newjail_nname}_procfs_enable=\"${ezjail_procfs_enable}\"   >> ${ezjail_jailcfgs}/${newjail_nname}
  echo export jail_${newjail_nname}_fdescfs_enable=\"${ezjail_fdescfs_enable}\" >> ${ezjail_jailcfgs}/${newjail_nname}

  ;;
delete)
######################## ezjail-admin DELETE ########################
  shift
  args=`getopt w $*`
  [ $? = 0 ] || exerr 'Usage: ezjail delete [-w] jailname'; exit 1;
  
  oldjail_wipe="NO"
  
  set -- $args
  for arg do
    case $arg in
      -w) oldjail_wipe="YES"; shift;;
      --) shift; break;;
    esac
  done;
  oldjail_name=$1;

  # we only need name of jail to vanish
  [ "$oldjail_name" -a $# = 1 ] || exerr 'Usage: ezjail delete [-w] jailname'

  # tidy up jail name the ezjail way
  oldjail_nname=`echo $oldjail_name | tr /~. ___`;

  # check for existence of jail in our records
  [ -f ${ezjail_jailcfgs}/${oldjail_nname} ] || exerr "Error: Nothing known about jail $oldjail_name"

  # fetch information about the jail to be gone
  # by parsing our records
  . ${ezjail_jailcfgs}/${oldjail_nname}
  eval oldjail_rootdir=\"\$jail_${oldjail_nname}_rootdir\"

  # if jail is still running, refuse to go any further
  if [ -f /var/run/jail_${oldjail_nname}.id ]; then
    echo "Error: Jail appears to be still running, stop it first"
    echo "(/var/run/jail_${oldjail_nname}.id exists)"
    exit 1;
  fi

  # now we know everything we need to let the jail be gone
  # remove entry from ezjail resource structure
  rm -f ${ezjail_jailcfgs}/${oldjail_nname}

  # delete fstab.JAILNAME
  rm -f /etc/fstab.$oldjail_nname

  # if there is a soft link pointing to the jail root, remove it
  oldjail_softlink=$ezjail_jaildir/`basename $oldjail_rootdir`
  [ -L $oldjail_softlink ] && rm $oldjail_softlink

  # if wiping the jail was requested, remove it
  [ $oldjail_wipe = "YES" ] && rm -rf $oldjail_rootdir

  ;;
list)
######################## ezjail-admin LIST ########################
  jail_list=`ls $ezjail_jailcfgs`
  for jail in $jail_list; do
    . ${ezjail_jailcfgs}/$jail
    eval jail_ip=\"\$jail_${jail}_ip\"
    eval jail_hostname=\"\$jail_${jail}_hostname\"
    eval jail_rootdir=\"\$jail_${jail}_rootdir\"
    printf "%-15s %-28s %s\\n" $jail_ip $jail_hostname $jail_rootdir
  done
  ;;
setup|update)
######################## ezjail-admin UPDATE ########################
  shift
  args=`getopt is: $*`
  [ $? = 0 ] || exerr 'Usage: ezjail update [-s sourcetree] [-i]'

  updatejail_installaction="world"

  set -- $args
  for arg do
    case $arg in
      -i) updatejail_installaction="installworld"; shift;;
      -s) ezjail_sourcetree="$2"; shift 2;;
      --) shift; break;;
    esac
  done;

  # Bump the user for some of the most common errors
  [ -d ${ezjail_sourcetree} ] || exerr "Cannot find your copy of the FreeBSD source tree in $ezjail_sourcetree."
  [ -f ${ezjail_sourcetree}/Makefile ] || exerr "Your source tree in $ezjail_sourcetree seems to be incomplete (Makefile missing)."

  cd ${ezjail_sourcetree}
  # Normally fulljail should be renamed by past ezjail-admin commands
  # However those may have failed
  rm -rf ${ezjail_jailfull}; mkdir -p ${ezjail_jailfull}

  # make our world
  make ${updatejail_installaction} DESTDIR=${ezjail_jailfull}
  [ $? = 0 ] || exerr "make ${updatejail_installaction} failed"

  # setup world
  make distribution DESTDIR=${ezjail_jailfull}
  [ $? = 0 ] || exerr "make distribution failed"

  cd ${ezjail_jailfull}
  # Fill basejail from installed world
  mkdir -p ${ezjail_jailbase}/usr ${ezjail_jailbase}/config/pkg
  for a in bin sbin usr/bin usr/include usr/lib usr/libexec usr/sbin usr/src usr/share; do
    find ${a} | cpio -d -p -v ${ezjail_jailbase};
    chflags -R noschg ${a}; rm -r ${a}; ln -s /basejail/${a} ${a}
  done
  mkdir basejail
  ln -s /basejail/usr/ports usr/ports

  if [ -d ${ezjail_jailtemplate} ]; then
    rm -rf ${ezjail_jailtemplate}_old
    mv ${ezjail_jailtemplate} ${ezjail_jailtemplate}_old
  fi
  mv ${ezjail_jailfull} ${ezjail_jailtemplate}

  ;;
*)
  echo "Usage: `basename $0` [create|delete|list|update] {params}"; exit;
  ;;
esac