Magellan Linux

Annotation of /trunk/mage/usr/lib/mage/museradd

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2174 - (hide annotations) (download)
Sat Aug 17 09:20:33 2013 UTC (10 years, 8 months ago) by niro
File size: 2800 byte(s)
-fix_usermod_opts(): fixed broken -M handling, go trough *all* opts
1 niro 42 #!/bin/bash
2 niro 78 # $Header: /home/cvsd/magellan-cvs/magellan-src/mage/usr/lib/mage/museradd,v 1.4 2005-06-01 15:48:43 niro Exp $
3 niro 42
4 niro 1273 # 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 niro 42 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 niro 1273 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 niro 2142 -M) FIXED_USER_OPTS+=" -H" ;;
35 niro 1273 *) FIXED_USER_OPTS+=" $1" ;;
36     esac
37     shift
38     done
39    
40     echo "${FIXED_USER_OPTS}"
41     }
42    
43     busybox_fix_group_opts()
44     {
45     local i
46     local FIXED_GROUP_OPTS
47    
48     for i in $*
49     do
50     case $1 in
51     -G) shift; FIXED_GROUP_OPTS+="$(echo $1 | sed 's:,:\ :g')" ;;
52     esac
53     shift
54     done
55    
56     echo "${FIXED_GROUP_OPTS}"
57     }
58    
59 niro 2142 fix_usermod_opts()
60     {
61     local i
62     local FIXED_USERMOD_OPTS
63    
64     for i in $*
65     do
66     case $1 in
67 niro 2174 -M) : ;; # usermod does not support the -M switch
68 niro 2142 *) FIXED_USERMOD_OPTS+=" $1" ;;
69     esac
70     shift
71     done
72    
73     echo "${FIXED_USERMOD_OPTS}"
74     }
75    
76 niro 1500 if [[ ! -z ${MROOT} ]] && [[ ${MROOT} != / ]]
77     then
78     chroot="chroot ${MROOT} "
79     else
80     chroot=""
81     fi
82    
83 niro 42 while getopts "o:-" opt ; do
84     case "${opt}" in
85     o)
86     USER_OPTS="${OPTARG}"
87     ;;
88    
89     -) break
90     ;;
91    
92     *)
93     print_usage
94     exit 1
95     ;;
96     esac
97     done
98     shift $(($OPTIND - 1))
99    
100     #exit if $1 is zero
101     if [ -z "$1" ]
102     then
103     print_usage
104     exit 1
105     fi
106    
107     USER_TO_ADD="$1"
108    
109 niro 1273 # busybox support needed?
110     if need_busybox_support adduser
111 niro 42 then
112 niro 1500 echo -n " Adding user '${USER_TO_ADD}'"
113     [[ ! -z ${chroot} ]] && echo -n " into MROOT='${MROOT}'"
114     echo " ..."
115 niro 1951 ${chroot} adduser -S -D $(busybox_fix_user_opts ${USER_OPTS}) "${USER_TO_ADD}"
116 niro 1273 for grp in $(busybox_fix_group_opts ${USER_OPTS})
117     do
118 niro 1500 ${chroot} addgroup "${USER_TO_ADD}" "${grp}"
119 niro 1273 done
120    
121     # normal systems
122 niro 42 else
123 niro 1273 # get the info
124 niro 1500 my_user="$(${chroot} getent passwd ${USER_TO_ADD})"
125 niro 1273
126     if [ -z "${my_user}" ]
127     then
128 niro 1500 echo -n " Adding user '${USER_TO_ADD}'"
129     [[ ! -z ${chroot} ]] && echo -n " into MROOT='${MROOT}'"
130     echo " ..."
131 niro 1951 ${chroot} useradd -r ${USER_OPTS} "${USER_TO_ADD}"
132 niro 1273 else
133 niro 1500 echo -n " Modifing user '${USER_TO_ADD}'"
134     [[ ! -z ${chroot} ]] && echo -n " in MROOT='${MROOT}'"
135     echo " ..."
136 niro 2142 ${chroot} usermod $(fix_usermod_opts ${USER_OPTS}) "${USER_TO_ADD}"
137 niro 1273 fi
138 niro 42 fi