Annotation of /trunk/mage/usr/lib/mage/museradd
Parent Directory | Revision Log
Revision 1273 -
(hide annotations)
(download)
Wed Apr 27 09:45:07 2011 UTC (13 years, 5 months ago) by niro
File size: 2205 byte(s)
Wed Apr 27 09:45:07 2011 UTC (13 years, 5 months ago) by niro
File size: 2205 byte(s)
-support busybox out-of-the-box
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 | *) FIXED_USER_OPTS+=" $1" ;; | ||
35 | esac | ||
36 | shift | ||
37 | done | ||
38 | |||
39 | echo "${FIXED_USER_OPTS}" | ||
40 | } | ||
41 | |||
42 | busybox_fix_group_opts() | ||
43 | { | ||
44 | local i | ||
45 | local FIXED_GROUP_OPTS | ||
46 | |||
47 | for i in $* | ||
48 | do | ||
49 | case $1 in | ||
50 | -G) shift; FIXED_GROUP_OPTS+="$(echo $1 | sed 's:,:\ :g')" ;; | ||
51 | esac | ||
52 | shift | ||
53 | done | ||
54 | |||
55 | echo "${FIXED_GROUP_OPTS}" | ||
56 | } | ||
57 | |||
58 | niro | 42 | while getopts "o:-" opt ; do |
59 | case "${opt}" in | ||
60 | o) | ||
61 | USER_OPTS="${OPTARG}" | ||
62 | ;; | ||
63 | |||
64 | -) break | ||
65 | ;; | ||
66 | |||
67 | *) | ||
68 | print_usage | ||
69 | exit 1 | ||
70 | ;; | ||
71 | esac | ||
72 | done | ||
73 | shift $(($OPTIND - 1)) | ||
74 | |||
75 | #exit if $1 is zero | ||
76 | if [ -z "$1" ] | ||
77 | then | ||
78 | print_usage | ||
79 | exit 1 | ||
80 | fi | ||
81 | |||
82 | USER_TO_ADD="$1" | ||
83 | |||
84 | niro | 1273 | # busybox support needed? |
85 | if need_busybox_support adduser | ||
86 | niro | 42 | then |
87 | echo " Adding user '${USER_TO_ADD}' ..." | ||
88 | niro | 1273 | adduser -D $(busybox_fix_user_opts ${USER_OPTS}) "${USER_TO_ADD}" |
89 | for grp in $(busybox_fix_group_opts ${USER_OPTS}) | ||
90 | do | ||
91 | addgroup "${USER_TO_ADD}" "${grp}" | ||
92 | done | ||
93 | |||
94 | # normal systems | ||
95 | niro | 42 | else |
96 | niro | 1273 | #start nscd to cache passwd |
97 | $(which nscd) -i passwd | ||
98 | # get the info | ||
99 | my_user="$(getent passwd ${USER_TO_ADD})" | ||
100 | |||
101 | if [ -z "${my_user}" ] | ||
102 | then | ||
103 | echo " Adding user '${USER_TO_ADD}' ..." | ||
104 | useradd ${USER_OPTS} "${USER_TO_ADD}" | ||
105 | else | ||
106 | echo " Modifing user '${USER_TO_ADD}' ..." | ||
107 | usermod ${USER_OPTS} "${USER_TO_ADD}" | ||
108 | fi | ||
109 | niro | 42 | fi |