Posted By

dogrizz on 02/23/11


Tagged

Bash linux slackware adduser


Versions (?)

superadduser


 / Published in: Bash
 

URL: http://connie.slackware.com/~mozes/downloads/adduser

just for my convenience. no rights whatsoever.

  1. #!/bin/bash
  2. #
  3. # Copyright 1995 Hrvoje Dogan, Croatia.
  4. # Copyright 2002-2004, 2008, 2009, 2010 Stuart Winter, Surrey, England, UK.
  5. # Copyright 2004, 2008-2010 Slackware Linux, Inc., Concord, CA, USA
  6. # All rights reserved.
  7. #
  8. # Redistribution and use of this script, with or without modification, is
  9. # permitted provided that the following conditions are met:
  10. #
  11. # 1. Redistributions of this script must retain the above copyright
  12. # notice, this list of conditions and the following disclaimer.
  13. #
  14. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  15. # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  17. # EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  18. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  20. # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  21. # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  22. # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  23. # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. #
  25. #
  26. ##########################################################################
  27. # Program: /usr/sbin/adduser
  28. # Purpose: Interactive front end to /usr/sbin/useradd for Slackware Linux
  29. # Author : Stuart Winter <[email protected]>
  30. # Based on the original Slackware adduser by Hrvoje Dogan
  31. # with modifications by Patrick Volkerding
  32. # Version: 1.13
  33. ##########################################################################
  34. # Usage..: adduser [<new_user_name>]
  35. ##########################################################################
  36. # History #
  37. ###########
  38. # v1.13 - 13/01/10
  39. # * Fixed bug that removed underscore characters from UNIX group names.
  40. # Thanks to mRgOBLIN for the report and Jim Hawkins for the fix. <sw>
  41. # v1.12 - 21/07/09
  42. # * Adjusted the search of /etc/passwd to exclude the NIS inclusion
  43. # string. Thanks to Dominik L. Borkowski.
  44. # v1.11 - 04/06/09
  45. # * Add power and netdev to the suggested group list
  46. # v1.10 - 24/03/08
  47. # * To facilitate use of the automatic mounting features of HAL,
  48. # allow the admin to easily add users to the default groups:
  49. # audio,cdrom,video,plugdev,floppy.
  50. # The default is not to add new users to these groups.
  51. # And by the way, this script is "useradd from Slackware" not
  52. # "superadduser from Gentoo" ;-)
  53. # v1.09 - 07/06/04
  54. # * Added standard Slackware script licence to the head of this file.
  55. # v1.08 - 25/04/04
  56. # * Disallow user names that begin with a numeric because useradd
  57. # (from shadow v4.03) does not allow them. <sw>
  58. # v1.07 - 07/03/03
  59. # * When supplying a null string for the uid (meaning 'Choose next available'),
  60. # if there were file names in the range 'a-z' in the pwd then the
  61. # egrep command considered these files rather than the null string.
  62. # The egrep expression is now in quotes.
  63. # Reported & fixed by Vadim O. Ustiansky <sw>
  64. # v1.06 - 31/03/03
  65. # * Ask to chown user.group the home directory if it already exists.
  66. # This helps reduce later confusion when adding users whose home dir
  67. # already exists (mounted partition for example) and is owned
  68. # by a user other than the user to which the directory is being
  69. # assigned as home. Default is not to chown.
  70. # Brought to my attention by mRgOBLIN. <sw>
  71. # v1.05 - 04/01/03
  72. # * Advise & prevent users from creating logins with '.' characters
  73. # in the user name. <sw>
  74. # * Made pending account creation info look neater <sw>
  75. # v1.04 - 09/06/02
  76. # * Catered for shadow-4.0.3's 'useradd' binary that no longer
  77. # will let you create a user that has any uppercase chars in it
  78. # This was reported on the userlocal.org forums
  79. # by 'xcp' - thanks. <sw,pjv>
  80. # v1.03 - 20/05/02
  81. # * Support 'broken' (null lines in) /etc/passwd and
  82. # /etc/group files <sw>
  83. # * For recycling UIDs (default still 'off'), we now look in
  84. # /etc/login.defs for the UID_MIN value and use it
  85. # If not found then default to 1000 <sw>
  86. # v1.02 - 10/04/02
  87. # * Fix user-specified UID bug. <pjv>
  88. # v1.01 - 23/03/02
  89. # * Match Slackware indenting style, simplify. <pjv>
  90. # v1.00 - 22/03/02
  91. # * Created
  92. #######################################################################
  93.  
  94. # Path to files
  95. pfile=/etc/passwd
  96. gfile=/etc/group
  97. sfile=/etc/shells
  98.  
  99. # Paths to binaries
  100. useradd=/usr/sbin/useradd
  101. chfn=/usr/bin/chfn
  102. passwd=/usr/bin/passwd
  103. chmod=/bin/chmod
  104.  
  105. # Defaults
  106. defhome=/home
  107. defshell=/bin/bash
  108. defchmod=711 # home dir permissions - may be preferable to use 701, however.
  109. defgroup=users
  110. AGID="audio cdrom floppy plugdev video power netdev" # additional groups for desktop users
  111.  
  112. # Determine what the minimum UID is (for UID recycling)
  113. # (we ignore it if it's not at the beginning of the line (i.e. commented out with #))
  114. export recycleUIDMIN="$(grep ^UID_MIN /etc/login.defs | awk '{print $2}' 2>/dev/null)"
  115. # If we couldn't find it, set it to the default of 1000
  116. if [ -z "$recycleUIDMIN" ]; then
  117. export recycleUIDMIN=1000 # this is the default from Slackware's /etc/login.defs
  118. fi
  119.  
  120.  
  121. # This setting enables the 'recycling' of older unused UIDs.
  122. # When you userdel a user, it removes it from passwd and shadow but it will
  123. # never get used again unless you specify it expliticly -- useradd (appears to) just
  124. # look at the last line in passwd and increment the uid. I like the idea of
  125. # recycling uids but you may have very good reasons not to (old forgotten
  126. # confidential files still on the system could then be owned by this new user).
  127. # We'll set this to no because this is what the original adduser shell script
  128. # did and it's what users expect.
  129. recycleuids=no
  130.  
  131. # Function to read keyboard input.
  132. # bash1 is broken (even ash will take read -ep!), so we work around
  133. # it (even though bash1 is no longer supported on Slackware).
  134. function get_input() {
  135. local output
  136. if [ "`echo $BASH_VERSION | cut -b1`" = "1" ]; then
  137. echo -n "${1} " >&2 # fudge for use with bash v1
  138. read output
  139. else # this should work with any other /bin/sh
  140. read -ep "${1} " output
  141. fi
  142. echo $output
  143. }
  144.  
  145. # Function to display the account info
  146. function display () {
  147. local goose
  148. goose="$(echo $2 | cut -d ' ' -f 2-)" # lop off the prefixed argument useradd needs
  149. echo -n "$1 "
  150. # If it's null then display the 'other' information
  151. if [ -z "$goose" -a ! -z "$3" ]; then
  152. echo "$3"
  153. else
  154. echo "$goose"
  155. fi
  156. }
  157.  
  158. # Function to check whether groups exist in the /etc/group file
  159. function check_group () {
  160. local got_error group
  161. if [ ! -z "$@" ]; then
  162. for group in $@ ; do
  163. local uid_not_named="" uid_not_num=""
  164. grep -v "$^" $gfile | awk -F: '{print $1}' | grep "^${group}$" >/dev/null 2>&1 || uid_not_named=yes
  165. grep -v "$^" $gfile | awk -F: '{print $3}' | grep "^${group}$" >/dev/null 2>&1 || uid_not_num=yes
  166. if [ ! -z "$uid_not_named" -a ! -z "$uid_not_num" ]; then
  167. echo "- Group '$group' does not exist"
  168. got_error=yes
  169. fi
  170. done
  171. fi
  172. # Return exit code of 1 if at least one of the groups didn't exist
  173. if [ ! -z "$got_error" ]; then
  174. return 1
  175. fi
  176. }
  177.  
  178. #: Read the login name for the new user :#
  179. #
  180. # Remember that most Mail Transfer Agents are case independant, so having
  181. # 'uSer' and 'user' may cause confusion/things to break. Because of this,
  182. # useradd from shadow-4.0.3 no longer accepts usernames containing uppercase,
  183. # and we must reject them, too.
  184.  
  185. # Set the login variable to the command line param
  186. echo
  187. LOGIN="$1"
  188. needinput=yes
  189. while [ ! -z $needinput ]; do
  190. if [ -z "$LOGIN" ]; then
  191. while [ -z "$LOGIN" ]; do LOGIN="$(get_input "Login name for new user []:")" ; done
  192. fi
  193. grep "^${LOGIN}:" $pfile >/dev/null 2>&1 # ensure it's not already used
  194. if [ $? -eq 0 ]; then
  195. echo "- User '$LOGIN' already exists; please choose another"
  196. unset LOGIN
  197. elif [ ! -z "$( echo $LOGIN | grep "^[0-9]" )" ]; then
  198. echo "- User names cannot begin with a number; please choose another"
  199. unset LOGIN
  200. elif [ ! "$LOGIN" = "`echo $LOGIN | tr A-Z a-z`" ]; then # useradd does not allow uppercase
  201. echo "- User '$LOGIN' contains illegal characters (uppercase); please choose another"
  202. unset LOGIN
  203. elif [ ! -z "$( echo $LOGIN | grep '\.' )" ]; then
  204. echo "- User '$LOGIN' contains illegal characters (period/dot); please choose another"
  205. unset LOGIN
  206. else
  207. unset needinput
  208. fi
  209. done
  210.  
  211. # Display the user name passed from the shell if it hasn't changed
  212. if [ "$1" = "$LOGIN" ]; then
  213. echo "Login name for new user: $LOGIN"
  214. fi
  215.  
  216. #: Get the UID for the user & ensure it's not already in use :#
  217. #
  218. # Whilst we _can_ allow users with identical UIDs, it's not a 'good thing' because
  219. # when you change password for the uid, it finds the first match in /etc/passwd
  220. # which isn't necessarily the correct user
  221. #
  222. echo
  223. needinput=yes
  224. while [ ! -z "$needinput" ]; do
  225. _UID="$(get_input "User ID ('UID') [ defaults to next available ]:")"
  226. egrep -v "^$|^\+" $pfile | awk -F: '{print $3}' | grep "^${_UID}$" >/dev/null 2>&1
  227. if [ $? -eq 0 ]; then
  228. echo "- That UID is already in use; please choose another"
  229. elif [ ! -z "$(echo $_UID | egrep '[A-Za-z]')" ]; then
  230. echo "- UIDs are numerics only"
  231. else
  232. unset needinput
  233. fi
  234. done
  235. # If we were given a UID, then syntax up the variable to pass to useradd
  236. if [ ! -z "$_UID" ]; then
  237. U_ID="-u ${_UID}"
  238. else
  239. # Will we be recycling UIDs?
  240. if [ "$recycleuids" = "yes" ]; then
  241. U_ID="-u $(awk -F: '{uid[$3]=1} END { for (i=ENVIRON["recycleUIDMIN"];i in uid;i++);print i}' $pfile)"
  242. fi
  243. fi
  244.  
  245. #: Get the initial group for the user & ensure it exists :#
  246. #
  247. # We check /etc/group for both the text version and the group ID number
  248. echo
  249. needinput=yes
  250. while [ ! -z "$needinput" ]; do
  251. GID="$(get_input "Initial group [ ${defgroup} ]:")"
  252. check_group "$GID"
  253. if [ $? -gt 0 ]; then
  254. echo "- Please choose another"
  255. else
  256. unset needinput
  257. fi
  258. done
  259. # Syntax the variable ready for useradd
  260. if [ -z "$GID" ]; then
  261. GID="-g ${defgroup}"
  262. else
  263. GID="-g ${GID}"
  264. fi
  265.  
  266. #: Get additional groups for the user :#
  267. #
  268. echo "Additional UNIX groups:"
  269. echo
  270. echo "Users can belong to additional UNIX groups on the system."
  271. echo "For local users using graphical desktop login managers such"
  272. echo "as XDM/KDM, users may need to be members of additional groups"
  273. echo "to access the full functionality of removable media devices."
  274. echo
  275. echo "* Security implications *"
  276. echo "Please be aware that by adding users to additional groups may"
  277. echo "potentially give access to the removable media of other users."
  278. echo
  279. echo "If you are creating a new user for remote shell access only,"
  280. echo "users do not need to belong to any additional groups as standard,"
  281. echo "so you may press ENTER at the next prompt."
  282. echo
  283. needinput=yes
  284. while [ ! -z "$needinput" ]; do
  285. history -c
  286. history -s "$AGID"
  287. echo "Press ENTER to continue without adding any additional groups"
  288. echo "Or press the UP arrow key to add/select/edit additional groups"
  289. AGID="$(get_input ": " | sed 's/[^A-Za-z0-9 _]//g;s/ */ /g;s/^ $//g' )"
  290. if [ ! -z "$AGID" ]; then
  291. check_group "$AGID" # check all groups at once (treated as N # of params)
  292. if [ $? -gt 0 ]; then
  293. echo "- Please re-enter the group(s)"
  294. echo
  295. else
  296. unset needinput # we found all groups specified
  297. AGID="-G $(echo $AGID | tr ' ' ,)" # useradd takes comma delimited groups
  298. fi
  299. else
  300. unset needinput # we don't *have* to have additional groups
  301. fi
  302. done
  303.  
  304. #: Get the new user's home dir :#
  305. #
  306. echo
  307. needinput=yes
  308. while [ ! -z "$needinput" ]; do
  309. HME="$(get_input "Home directory [ ${defhome}/${LOGIN} ]")"
  310. if [ -z "$HME" ]; then
  311. HME="${defhome}/${LOGIN}"
  312. fi
  313. # Warn the user if the home dir already exists
  314. if [ -d "$HME" ]; then
  315. echo "- Warning: '$HME' already exists !"
  316. getyn="$(get_input " Do you wish to change the home directory path ? (Y/n) ")"
  317. if [ "$(echo $getyn | grep -i "n")" ]; then
  318. unset needinput
  319. # You're most likely going to only do this if you have the dir *mounted* for this user's $HOME
  320. getyn="$(get_input " Do you want to chown $LOGIN.$( echo $GID | awk '{print $2}') $HME ? (y/N) ")"
  321. if [ "$(echo $getyn | grep -i "y")" ]; then
  322. CHOWNHOMEDIR=$HME # set this to the home directory
  323. fi
  324. fi
  325. else
  326. unset needinput
  327. fi
  328. done
  329. HME="-d ${HME}"
  330.  
  331. #: Get the new user's shell :#
  332. echo
  333. needinput=yes
  334. while [ ! -z "$needinput" ]; do
  335. unset got_error
  336. SHL="$(get_input "Shell [ ${defshell} ]")"
  337. if [ -z "$SHL" ]; then
  338. SHL="${defshell}"
  339. fi
  340. # Warn the user if the shell doesn't exist in /etc/shells or as a file
  341. if [ -z "$(grep "^${SHL}$" $sfile)" ]; then
  342. echo "- Warning: ${SHL} is not in ${sfile} (potential problem using FTP)"
  343. got_error=yes
  344. fi
  345. if [ ! -f "$SHL" ]; then
  346. echo "- Warning: ${SHL} does not exist as a file"
  347. got_error=yes
  348. fi
  349. if [ ! -z "$got_error" ]; then
  350. getyn="$(get_input " Do you wish to change the shell ? (Y/n) ")"
  351. if [ "$(echo $getyn | grep -i "n")" ]; then
  352. unset needinput
  353. fi
  354. else
  355. unset needinput
  356. fi
  357. done
  358. SHL="-s ${SHL}"
  359.  
  360. #: Get the expiry date :#
  361. echo
  362. needinput=yes
  363. while [ ! -z "$needinput" ]; do
  364. EXP="$(get_input "Expiry date (YYYY-MM-DD) []:")"
  365. if [ ! -z "$EXP" ]; then
  366. # Check to see whether the expiry date is in the valid format
  367. if [ -z "$(echo "$EXP" | grep "^[[:digit:]]\{4\}[-]\?[[:digit:]]\{2\}[-]\?[[:digit:]]\{2\}$")" ]; then
  368. echo "- That is not a valid expiration date"
  369. else
  370. unset needinput
  371. EXP="-e ${EXP}"
  372. fi
  373. else
  374. unset needinput
  375. fi
  376. done
  377.  
  378. # Display the info about the new impending account
  379. echo
  380. echo "New account will be created as follows:"
  381. echo
  382. echo "---------------------------------------"
  383. display "Login name.......: " "$LOGIN"
  384. display "UID..............: " "$_UID" "[ Next available ]"
  385. display "Initial group....: " "$GID"
  386. display "Additional groups: " "$AGID" "[ None ]"
  387. display "Home directory...: " "$HME"
  388. display "Shell............: " "$SHL"
  389. display "Expiry date......: " "$EXP" "[ Never ]"
  390. echo
  391.  
  392. echo "This is it... if you want to bail out, hit Control-C. Otherwise, press"
  393. echo "ENTER to go ahead and make the account."
  394. read junk
  395.  
  396. echo
  397. echo "Creating new account..."
  398. echo
  399. echo
  400.  
  401. # Add the account to the system
  402. CMD="$useradd "$HME" -m "$EXP" "$U_ID" "$GID" "$AGID" "$SHL" "$LOGIN""
  403. $CMD
  404.  
  405. if [ $? -gt 0 ]; then
  406. echo "- Error running useradd command -- account not created!"
  407. echo "(cmd: $CMD)"
  408. exit 1
  409. fi
  410.  
  411. # chown the home dir ? We can only do this once the useradd has
  412. # completed otherwise the user name doesn't exist.
  413. if [ ! -z "${CHOWNHOMEDIR}" ]; then
  414. chown "$LOGIN"."$( echo $GID | awk '{print $2}')" "${CHOWNHOMEDIR}"
  415. fi
  416.  
  417. # Set the finger information
  418. $chfn "$LOGIN"
  419. if [ $? -gt 0 ]; then
  420. echo "- Warning: an error occurred while setting finger information"
  421. fi
  422.  
  423. # Set a password
  424. $passwd "$LOGIN"
  425. if [ $? -gt 0 ]; then
  426. echo "* WARNING: An error occured while setting the password for"
  427. echo " this account. Please manually investigate this *"
  428. exit 1
  429. fi
  430.  
  431. # If it was created (it should have been!), set the permissions for that user's dir
  432. HME="$(echo "$HME" | awk '{print $2}')" # We have to remove the -g prefix
  433. if [ -d "$HME" ]; then
  434. $chmod $defchmod "$HME"
  435. fi
  436.  
  437. echo
  438. echo
  439. echo "Account setup complete."
  440. exit 0

Report this snippet  

You need to login to post a comment.