Magellan Linux

Contents of /trunk/mage/usr/lib/mage/museradd

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2751 - (show annotations) (download)
Thu Aug 14 12:07:06 2014 UTC (9 years, 8 months ago) by niro
File size: 2849 byte(s)
-ignore -l switch when running usermod
1 #!/bin/bash
2 # $Id$
3
4 # include all needed files
5 [ -f /etc/mage.rc.global ] && source /etc/mage.rc.global
6 [ -f ${MAGERC} ] && source ${MAGERC}
7 [ -f ${MLIBDIR}/mage4.functions.sh ] && source ${MLIBDIR}/mage4.functions.sh
8
9 print_usage()
10 {
11 echo "$(basename $0 .sh) adds users to /etc/passwd."
12 echo " USAGE: $(basename $0 .sh) -o OPTIONS USER_NAME .."
13 echo
14 echo " OPTIONS: -o \"OPTS\"all options from /usr/sbin/useradd can be used."
15 echo
16 echo " Examples:"
17 echo " $(basename $0 .sh) -o \"-u 22 -g sshd -d /var/empty -s /bin/false\" sshd"
18 echo
19 }
20
21 busybox_fix_user_opts()
22 {
23 local i
24 local FIXED_USER_OPTS
25
26 for i in $*
27 do
28 case $1 in
29 -d) shift; FIXED_USER_OPTS+=" -h $1" ;;
30 -g) shift; FIXED_USER_OPTS+=" -G $1" ;;
31 -G) shift; shift; continue;; # will be parsed through fix_group_opts
32 -u) shift; [[ $1 -lt 100 ]] && FIXED_USER_OPTS+=" -S"; FIXED_USER_OPTS+=" -u $1" ;;
33 -r) FIXED_USER_OPTS+=" -S" ;;
34 -M) FIXED_USER_OPTS+=" -H" ;;
35 -l) continue ;; # just ignore this switch, adduser does not support it
36 *) FIXED_USER_OPTS+=" $1" ;;
37 esac
38 shift
39 done
40
41 echo "${FIXED_USER_OPTS}"
42 }
43
44 busybox_fix_group_opts()
45 {
46 local i
47 local FIXED_GROUP_OPTS
48
49 for i in $*
50 do
51 case $1 in
52 -G) shift; FIXED_GROUP_OPTS+="$(echo $1 | sed 's:,:\ :g')" ;;
53 esac
54 shift
55 done
56
57 echo "${FIXED_GROUP_OPTS}"
58 }
59
60 fix_usermod_opts()
61 {
62 local i
63 local FIXED_USERMOD_OPTS
64
65 for i in $*
66 do
67 case $1 in
68 -M) : ;; # usermod does not support the -M switch
69 -l) : ;; # usermod -l has not the same meaning like the -l switch of useradd
70 *) FIXED_USERMOD_OPTS+=" $1" ;;
71 esac
72 shift
73 done
74
75 echo "${FIXED_USERMOD_OPTS}"
76 }
77
78 if [[ ! -z ${MROOT} ]] && [[ ${MROOT} != / ]]
79 then
80 chroot="chroot ${MROOT} "
81 else
82 chroot=""
83 fi
84
85 while getopts "o:-" opt ; do
86 case "${opt}" in
87 o)
88 USER_OPTS="${OPTARG}"
89 ;;
90
91 -) break
92 ;;
93
94 *)
95 print_usage
96 exit 1
97 ;;
98 esac
99 done
100 shift $(($OPTIND - 1))
101
102 #exit if $1 is zero
103 if [ -z "$1" ]
104 then
105 print_usage
106 exit 1
107 fi
108
109 USER_TO_ADD="$1"
110
111 # busybox support needed?
112 if need_busybox_support adduser
113 then
114 echo -n " Adding user '${USER_TO_ADD}'"
115 [[ ! -z ${chroot} ]] && echo -n " into MROOT='${MROOT}'"
116 echo " ..."
117 ${chroot} adduser -S -D $(busybox_fix_user_opts ${USER_OPTS}) "${USER_TO_ADD}"
118 for grp in $(busybox_fix_group_opts ${USER_OPTS})
119 do
120 ${chroot} addgroup "${USER_TO_ADD}" "${grp}"
121 done
122
123 # normal systems
124 else
125 # get the info
126 my_user="$(${chroot} getent passwd ${USER_TO_ADD})"
127
128 if [ -z "${my_user}" ]
129 then
130 echo -n " Adding user '${USER_TO_ADD}'"
131 [[ ! -z ${chroot} ]] && echo -n " into MROOT='${MROOT}'"
132 echo " ..."
133 ${chroot} useradd -r ${USER_OPTS} "${USER_TO_ADD}"
134 else
135 echo -n " Modifing user '${USER_TO_ADD}'"
136 [[ ! -z ${chroot} ]] && echo -n " in MROOT='${MROOT}'"
137 echo " ..."
138 ${chroot} usermod $(fix_usermod_opts ${USER_OPTS}) "${USER_TO_ADD}"
139 fi
140 fi