File: drbl-useradd-file

package info (click to toggle)
drbl 2.20.11-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,560 kB
  • sloc: sh: 60,899; perl: 7,803; xml: 867; makefile: 124
file content (112 lines) | stat: -rwxr-xr-x 3,612 bytes parent folder | download | duplicates (6)
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
#!/bin/bash
# Author: Blake, Kuo-Lien Huang
# License: GPL
# Description:
#   * creat and delete accounts for DRBL, actually it for NIS (YP).
#
# Modified by Steven Shiau <steven@nchc.org.tw> to use in DRBL for Redhat

# Load DRBL setting and functions
DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"

. $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions

#
ACCOUNT_FILE="$1"
ACCOUNT_FILE_TMP=`mktemp /tmp/account_tmp.XXXXXX`
mode=""

USAGE="Usage: $0 account_file"
# Check if root or not
if [ ! "$UID" = "0" ]; then
  echo
  echo "[$LOGNAME] You need to run this script \"$0\" as root."
  echo
  exit 1
fi

if [ $# -ne 1 ]; then
  echo "$USAGE"
  echo "Create or delete accounts from the specified account_file."
  echo "The format of the file <filename>:  PREFIX START END GROUPNAME PASSWD_OPT"
  echo "PASSWD_OPT:"
  echo "If one digit, it's the length of randomly created password."
  echo "If blank, it will be randomly generated with some (say:8) characters."
  echo "Other setting is the password itself."
  echo "for example: "
  echo "-----------------------------------------------------------------"
  echo "# account for student"
  echo "s		89101	89129  g3c9   8"
  echo "# account for teacher"
  echo "tckps	01	99   teacher  drblnice"
  echo "-----------------------------------------------------------------"
  exit 1
fi

[ ! -f "$ACCOUNT_FILE" ] && echo "File $ACCOUNT_FILE not exists!" && exit 1
[ ! -f "$useradd_range_exec_file" ] && echo "Can NOT find $useradd_range_exec_file file!" && exit 1 
[ ! -f "$userdel_range_exec_file" ] && echo "Can NOT find $userdel_range_exec_file file!" && exit 1 

# filter the comment line, only keep the account line
grep -v '^[[:space:]]*#' $ACCOUNT_FILE > $ACCOUNT_FILE_TMP

#
if [ -n "$(basename $0 | grep -E "useradd")" ]; then
  mode="useradd"
elif [ -n "$(basename $0 | grep -E "userdel")" ]; then
  mode="userdel"
else
  echo "Unknown mode!"
  exit 1
fi

# useradd mode
if [ "$mode" = "useradd" ]; then
   while read prefix start end groupname password_opt; do
     # check if groupname is not valid one
     if `echo "$groupname" | grep -q "^[0-9]"`; then
        echo "groupname can NOT begin with digits (0-9)!"
        echo "The one you specified is \"$groupname\""
        echo "Program terminated"
        exit 1
     fi 
     if [ -n "$prefix" ]; then
       echo -n "Creating accounts with $prefix $start $end $groupname $password_opt"
       $useradd_range_exec_file $prefix $start $end $groupname $password_opt
       echo "done!"
     fi
   done < $ACCOUNT_FILE_TMP
fi

# userdel mode
if [ "$mode" = "userdel" ]; then
   # prompt to ask some confirmation
   echo -n "Do you also want to clean user's home directory [y/N] ? "
   read clean_home
   case "$clean_home" in
      y|Y|[yY][eE][sS]) 
         echo "Warning! The user's home directory will be deleted! Are you sure ?"
         echo -n "[y/N] "
         read clean_home_confirm
         ;;
      *)
         RM_HOME_OPT=""
   esac
   echo -n "Do you also want to clean group [Y/n] ? "
   read clean_group
   CONFIRM_TMP=`mktemp /tmp/confirm_tmp.XXXXXX`
   echo "$clean_home" >> $CONFIRM_TMP
   echo "$clean_home_confirm" >> $CONFIRM_TMP
   echo "$clean_group" >> $CONFIRM_TMP

   while read prefix start end groupname password_opt; do
     if [ -n "$prefix" ]; then
       echo "Deleting accounts with $prefix $start $end $groupname $password_opt"
       $userdel_range_exec_file $prefix $start $end $groupname < $CONFIRM_TMP
       echo "done!"
     fi
   done < $ACCOUNT_FILE_TMP
fi

[ -f "$ACCOUNT_FILE_TMP" ] && rm -f $ACCOUNT_FILE_TMP
[ -f "$CONFIRM_TMP" ] && rm -f $CONFIRM_TMP