"Fossies" - the Fresh Open Source Software Archive

Member "Plone-5.2.5-UnifiedInstaller-1.0/helper_scripts/user_group_utilities.sh" (13 Sep 2021, 3280 Bytes) of package /linux/www/Plone-5.2.5-UnifiedInstaller-1.0.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "user_group_utilities.sh": 5.2.2-UnifiedInstaller_vs_5.2.4-UnifiedInstaller-1.0.

    1 # Copyright (c) 2012-2021 Plone Foundation. Licensed under GPL v 2.
    2 #
    3 # This shell package provides functions for creating
    4 # users and groups in common UNIX work-alikes:
    5 
    6 # check_ug_ability
    7 # Do we know how to create users and groups on this platform? If not, fail with
    8 # a useful message.
    9 
   10 # check_user TARGET_USER TARGET_GROUP
   11 # Do we have expected user and group available? Fail if not.
   12 
   13 # create_group TARGET_GROUP
   14 # Create target group
   15 
   16 # create_user TARGET_USER TARGET_GROUP
   17 # Create a user with id TARGET_USER and TARGET_GROUP
   18 # as the primary group.
   19 
   20 # Currently supported:
   21 #     useradd (Linux)
   22 #     pw (BSD)
   23 
   24 if [ -x /usr/sbin/useradd ]; then
   25     # Probably some flavor of Linux; cross fingers
   26     # and hope useradd has common options.
   27 
   28     check_ug_ability () {
   29         echo $USING_USERADD
   30     }
   31 
   32     create_group () {
   33         TARGET_GROUP="$1"
   34         egrep "^$TARGET_GROUP\:" /etc/group > /dev/null 2>&1
   35         if [ $? -eq 0 ]; then
   36             eval "echo \"$GROUP_EXISTS\""
   37         else
   38             groupadd "$TARGET_GROUP" > /dev/null 2>&1
   39             if [ $? -gt 0 ]; then
   40                 eval "echo \"$GROUP_FAILED\""
   41                 exit 1
   42             fi
   43         fi
   44     }
   45 
   46     create_user () {
   47         TARGET_USER="$1"
   48         TARGET_GROUP="$2"
   49 
   50         NOLOGIN=`which nologin`
   51         NOHOME=`which false`
   52         USER_SETTINGS="-g $TARGET_GROUP --shell $NOLOGIN --home $NOHOME"
   53 
   54         id "$TARGET_USER" > /dev/null 2>&1
   55         if [ $? -eq 0 ]; then
   56             eval "echo \"$USER_EXISTS\""
   57             eval "echo \"$ADD_USER_TO_GROUP\""
   58             usermod $TARGET_USER -G $TARGET_GROUP
   59         else
   60             useradd $TARGET_USER $USER_SETTINGS
   61         fi
   62     }
   63 
   64 elif [ -x /usr/sbin/pw ]; then
   65     # we're probably in the BSD world; compliments to the sysadmin.
   66 
   67     check_ug_ability () {
   68         echo $USING_PW
   69     }
   70 
   71     create_group () {
   72         TARGET_GROUP="$1"
   73         egrep "^$TARGET_GROUP\:" /etc/group > /dev/null 2>&1
   74         if [ $? -eq 0 ]; then
   75             eval "echo \"$GROUP_EXISTS\""
   76         else
   77             pw groupadd "$TARGET_GROUP" > /dev/null 2>&1
   78             if [ $? -gt 0 ]; then
   79                 eval "echo \"$GROUP_FAILED\""
   80                 exit 1
   81             fi
   82         fi
   83     }
   84 
   85     create_user () {
   86         TARGET_USER="$1"
   87         TARGET_GROUP="$2"
   88 
   89         NOLOGIN=`which nologin`
   90         NOHOME=`which false`
   91         USER_SETTINGS="-g $TARGET_GROUP -s $NOLOGIN -d $NOHOME"
   92 
   93         id "$TARGET_USER" > /dev/null 2>&1
   94         if [ $? -eq 0 ]; then
   95             eval "echo \"$USER_EXISTS\""
   96             eval "echo \"$ADD_USER_TO_GROUP\""
   97             pw usermod $TARGET_USER -G $TARGET_GROUP
   98         else
   99             pw useradd $TARGET_USER $USER_SETTINGS
  100         fi
  101     }
  102 else
  103     check_ug_ability () {
  104         echo $UNKNOWN_USER_ENV
  105     }
  106 
  107     # we don't know what we're doing -- so don't do anything.
  108     create_user () {
  109         return 0
  110     }
  111     create_group () {
  112         return 0
  113     }
  114 fi
  115 
  116 check_user () {
  117     TARGET_USER="$1"
  118     TARGET_GROUP="$2"
  119 
  120     id "$TARGET_USER" | egrep "uid=[0-9]+\(${TARGET_USER}\) .+[0-9]+\(${TARGET_GROUP}\)" > /dev/null 2>&1
  121     if [ $? -gt 0 ]; then
  122         echo "id for $TARGET_USER returned"
  123         id "$TARGET_USER"
  124         eval "echo \"$UG_CREATE_FAILED\""
  125         exit 1
  126     fi
  127 }