diff previous-work/more_control_helpers/sbin/add_package_user @ 1:d6bef198ae71

add work by Matthias S. Benkmann which is the inspiration for this project.
author Robert McIntyre <rlm@mit.edu>
date Tue, 08 Jan 2013 11:45:01 +0000
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/previous-work/more_control_helpers/sbin/add_package_user	Tue Jan 08 11:45:01 2013 +0000
     1.3 @@ -0,0 +1,239 @@
     1.4 +#!/bin/bash
     1.5 +# Copyright (c) 2004 Matthias S. Benkmann <article AT winterdrache DOT de>
     1.6 +# You may do everything with this code except misrepresent its origin.
     1.7 +# PROVIDED `AS IS' WITH ABSOLUTELY NO WARRANTY OF ANY KIND!
     1.8 +
     1.9 +#Package user home directories will be located under this directory
    1.10 +homebase=/usr/src
    1.11 +
    1.12 +#Contents of following directory are copied into home directory when creating 
    1.13 +#a new package user (existing files will not be overwritten)
    1.14 +skel=/etc/pkgusr/skel-package
    1.15 +
    1.16 +if [ $# -lt 7 ]; then
    1.17 +  echo 1>&2 'USAGE: '
    1.18 +  echo 1>&2 'add_package_user <description> <name> <minuid> <maxuid> \'
    1.19 +  echo 1>&2 '                              <group> <mingid> <maxgid> [-d <home>]'
    1.20 +  echo 1>&2
    1.21 +  echo 1>&2 'If a user account called <name> exists, a message will be printed and'
    1.22 +  echo 1>&2 'everything will be left as-is. If a user account called <name> does not'
    1.23 +  echo 1>&2 'exist, one will be created.'
    1.24 +  echo 1>&2 'The account'"'"'s primary group will be <group> and the /etc/passwd'
    1.25 +  echo 1>&2 'description field will be set to <description>. If a group called <group>'
    1.26 +  echo 1>&2 'does not already exist, one will be created.'
    1.27 +  echo 1>&2 'The new account will get the "install" group as a supplementary group. If'
    1.28 +  echo 1>&2 'a group named "install" does not exist, one will be created.'
    1.29 +  echo 1>&2
    1.30 +  echo 1>&2 '<description> needs to be a valid string for the /etc/passwd description'
    1.31 +  echo 1>&2 '  field. This means, among other things, that it must not contain ":".'
    1.32 +  echo 1>&2 '  Don'"'"'t forget to properly quote <description> if it contains spaces or'
    1.33 +  echo 1>&2 '  other characters interpreted by the shell!'
    1.34 +  echo 1>&2
    1.35 +  echo 1>&2 '<minuid>(incl.) and <maxuid>(excl.) determine the numeric range from which'
    1.36 +  echo 1>&2 '  the new account'"'"'s UID will be picked in the following way:'
    1.37 +  echo 1>&2
    1.38 +  echo 1>&2 '  1. If the range contains no unused UID => Exit with error.'
    1.39 +  echo 1>&2 '  2. If <maxuid>-1 is still unused, find the greatest UID from the range'
    1.40 +  echo 1>&2 '     that is used and pick the number after that.'
    1.41 +  echo 1>&2 '  3. If <maxuid>-1 is in use, pick the first unused number from the range.'
    1.42 +  echo 1>&2
    1.43 +  echo 1>&2 '<mingid>(incl.) and <maxgid>(excl.) determine the numeric range from which'
    1.44 +  echo 1>&2 '  to pick the GID for group <group> and/or group "install", if it needs to be'
    1.45 +  echo 1>&2 '  created. The process for picking the GID is the same as that for the UID.'
    1.46 +  echo 1>&2 ''
    1.47 +  echo 1>&2 '<home> specifies the new user'"'"'s home directory. If it is not provided,'
    1.48 +  echo 1>&2 '  it will default to '"$homebase/<name> ."
    1.49 +  echo 1>&2 '  If the home directory does not exist yet it will be created, otherwise'
    1.50 +  echo 1>&2 '  the existing directory will be recursively chown'"'"'ed to the new user.'
    1.51 +  echo 1>&2 '  The home directory will be populated with a copy of the contents of'
    1.52 +  echo 1>&2 "  $skel, but pre-existing files in the home directory"
    1.53 +  echo 1>&2 '  will not be overwritten. Note that symlinks will be copied as symlinks!'
    1.54 +  echo 1>&2 ''
    1.55 +  exit 1
    1.56 +fi
    1.57 +
    1.58 +grpfile=/etc/group
    1.59 +passwd=/etc/passwd
    1.60 +
    1.61 +
    1.62 +
    1.63 +description=$1
    1.64 +name=$2
    1.65 +minuid=$3
    1.66 +maxuid=$4
    1.67 +gname=$5
    1.68 +mingid=$6
    1.69 +maxgid=$7
    1.70 +home=$homebase/$name
    1.71 +
    1.72 +set -- "$@" _eNd_OF_lisT_
    1.73 +while [ "$1" != "_eNd_OF_lisT_" ]; do
    1.74 +  case "$1" in
    1.75 +    -d) shift 1
    1.76 +        if [ "$1" = "_eNd_OF_lisT_" ]; then
    1.77 +          echo 1>&2 "-d directory name missing!"
    1.78 +          exit 1
    1.79 +        fi
    1.80 +        home="$1"
    1.81 +        shift 1
    1.82 +        ;;
    1.83 +    *) temp="$1" 
    1.84 +       shift 1
    1.85 +       set -- "$@" "$temp"
    1.86 +       ;;
    1.87 +  esac     
    1.88 +done
    1.89 +shift 1 #throw away _eNd_OF_lisT_
    1.90 +
    1.91 +if [ $UID -ne 0 ]; then echo Please run this script as root. ; exit 1; fi
    1.92 +
    1.93 +#test if user already exists
    1.94 +grep "^$name:.*" $passwd
    1.95 +if [ $? -eq 0 ]; then 
    1.96 +  echo 'Package user does already exist! Do su '$name' to do maintenance work.'
    1.97 +  exit 1
    1.98 +fi 
    1.99 +
   1.100 +#test if minuid, maxuid, mingid and maxgid are integers, otherwise error
   1.101 +error=0
   1.102 +expr ${minuid} + 1 2>/dev/null 1>&2 || error=1
   1.103 +expr ${maxuid} + 1 2>/dev/null 1>&2 || error=1
   1.104 +expr ${mingid} + 1 2>/dev/null 1>&2 || error=1
   1.105 +expr ${maxgid} + 1 2>/dev/null 1>&2 || error=1
   1.106 +
   1.107 +if [ $error -eq 1 ]; then
   1.108 +  echo Error: Illegal numeric value!
   1.109 +  exit 1
   1.110 +fi
   1.111 +
   1.112 +if [ $minuid -ge $maxuid ]; then
   1.113 +  echo 'Error: minuid must be less than maxuid !' 
   1.114 +  exit 1
   1.115 +fi
   1.116 +
   1.117 +if [ $mingid -ge $maxgid ]; then
   1.118 +  echo 'Error: mingid must be less than maxgid !' 
   1.119 +  exit 1
   1.120 +fi
   1.121 +
   1.122 +
   1.123 +uidlist=`cut -d : -f 3 $passwd | sort -n`
   1.124 +
   1.125 +#find last used UID within range
   1.126 +u=0
   1.127 +for i in $uidlist
   1.128 +do
   1.129 +  if [ $i -ge $maxuid ]; then break; fi
   1.130 +  if [ $i -ge $minuid ]; then u=$i; fi 
   1.131 +done
   1.132 +
   1.133 +#if no UID from the range is used, pick the first, otherwise pick the one
   1.134 +#immediately following the last UID in use.
   1.135 +if [ $u -eq 0 ]; then u=$minuid; else u=`expr $u + 1`; fi
   1.136 +
   1.137 +#if the UID determined above is >= maxuid (i.e. illegal)
   1.138 +#then we look for the first unused uid in the range.
   1.139 +if [ $u -ge $maxuid ]; then
   1.140 +  u=$minuid
   1.141 +  for i in $uidlist
   1.142 +  do
   1.143 +    if [ $u -eq $i ]; then u=`expr $u + 1` ; fi
   1.144 +    if [ $i -ge $maxuid ]; then break; fi
   1.145 +  done  
   1.146 +
   1.147 +  if [ $u -ge $maxuid ]; then
   1.148 +    echo Error: UID range is full!
   1.149 +    exit 1
   1.150 +  fi
   1.151 +fi
   1.152 +
   1.153 +echo Will create user $name with uid: $u
   1.154 +
   1.155 +unset uidlist
   1.156 +
   1.157 +#############################################################################
   1.158 +#                                 group
   1.159 +#############################################################################
   1.160 +
   1.161 +#execute the following for gname and "install" to get gids for those 2 groups
   1.162 +
   1.163 +g=0
   1.164 +creategroup=0
   1.165 +for group in install $gname
   1.166 +do
   1.167 +  oldg=$g #save gid from previous run
   1.168 +  createinstall=$creategroup
   1.169 +  creategroup=0
   1.170 + 
   1.171 +  #test if group already exists and extract gid if so
   1.172 +  g=`grep ^${group}:.\* $grpfile | cut -d : -f 3 -`
   1.173 +
   1.174 +  #if group does not exist, then check range for a free gid
   1.175 +  if [ z$g = z ]; then 
   1.176 +    creategroup=1
   1.177 +    
   1.178 +    gidlist=`cut -d : -f 3 $grpfile | sort -n`
   1.179 +
   1.180 +    #find last used GID within range
   1.181 +    g=0
   1.182 +    for i in $gidlist
   1.183 +    do
   1.184 +      if [ $i -ge $maxgid ]; then break; fi
   1.185 +      if [ $i -ge $mingid ]; then g=$i; fi
   1.186 +    done
   1.187 +
   1.188 +    #if no GID from the range is used, pick the first, otherwise pick the one
   1.189 +    #immediately following the last GID in use.
   1.190 +    if [ $g -eq 0 ]; then g=$mingid; else g=`expr $g + 1`; fi
   1.191 +    
   1.192 +    #don't reuse gid from previous run 
   1.193 +    if [ $g -eq $oldg ]; then g=`expr $g + 1`; fi
   1.194 +
   1.195 +    #if the GID determined above is >= maxgid (i.e. illegal)
   1.196 +    #then we look for the first unused gid in the range.
   1.197 +    if [ $g -ge $maxgid ]; then
   1.198 +      g=$mingid
   1.199 +      for i in $gidlist
   1.200 +      do
   1.201 +        if [ $g -eq $i ]; then g=`expr $g + 1` ; fi
   1.202 +        if [ $g -eq $oldg ]; then g=`expr $g + 1` ; fi
   1.203 +        if [ $i -ge $maxgid ]; then break; fi
   1.204 +      done  
   1.205 +
   1.206 +      if [ $g -ge $maxgid ]; then
   1.207 +        echo Error: GID range is full!
   1.208 +        exit 1
   1.209 +      fi
   1.210 +    fi
   1.211 +  fi
   1.212 +done
   1.213 +
   1.214 +unset gidlist
   1.215 +
   1.216 +if [ $createinstall -eq 1 ]; then
   1.217 +  echo Creating group install with gid $oldg
   1.218 +  groupadd -g $oldg install || exit 1
   1.219 +else
   1.220 +  echo Group install has gid $oldg
   1.221 +fi
   1.222 +if [ $creategroup -eq 1 ]; then
   1.223 +  echo Creating group $gname with gid $g
   1.224 +  groupadd -g $g $gname || exit 1
   1.225 +else 
   1.226 +  echo Group $gname has gid $g
   1.227 +fi
   1.228 +
   1.229 +
   1.230 +
   1.231 +useradd -c "${description}" -d ${home} -g ${gname} -G install \
   1.232 +        -s /bin/bash -u ${u} ${name}  || exit 1
   1.233 +
   1.234 +mkdir -p $home || exit 1
   1.235 +
   1.236 +yes n|cp -ai -R ${skel}/{[^.],.[^.],..?}* ${home} 2>/dev/null >/dev/null
   1.237 +
   1.238 +cd ${home}
   1.239 +chown --recursive ${u}:${g} .
   1.240 +
   1.241 +
   1.242 +exit 0