a technical notebook
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.8 KiB

  1. Wednesday, December 3, 2014
  2. ===========================
  3. makecitizen
  4. -----------
  5. {sysops, scripting, adduser, chfn}
  6. Paul Ford sent out an e-mail to the tilde.club waitlist pointing at
  7. ~pfhawkins's list of other tildes, so I'm getting signup requests. There are
  8. enough that I want to write a script for adding a new squiggle.city user. I'm
  9. not determined to be very fancy about this right now; I just want to save some
  10. keystrokes.
  11. The first thing I do is google "adduser". `adduser(1)` is basically just a
  12. front end to `useradd(1)`. (This distinction will never stop being confusing,
  13. and should probably be a lesson to anyone considering that naming pattern.) I
  14. learn via Wikipedia that the metadata (name, room number, phone, etc.) which
  15. adduser prompts for is called the
  16. [GECOS field](http://en.wikipedia.org/wiki/Gecos_field), and is a relic of something
  17. called the General Electric Comprehensive Operating System, which ran on some
  18. machines at Bell Labs.
  19. You can change that info with `chfn(1)`.
  20. What my script needs to do is:
  21. 1. create a user with a given `$USERNAME`
  22. 2. generate a random password for the user and tell me
  23. 3. do `chage -d0 $USERNAME`
  24. 4. put a given public key in `~$USERNAME/.ssh/authorized_keys`
  25. You can't log in to squiggle.city with a password, so why go to the trouble of
  26. setting a random one and forcing users to change it at their first login?
  27. Mostly because users are going to need to know a password for things like
  28. changing their shell or in the case that they get operator privileges one day.
  29. This is what I come up with, after a couple of even dumber iterations:
  30. #!/bin/bash
  31. CITIZEN=$1
  32. KEYSTRING=$2
  33. # Complain and exit if we weren't given a path and a property:
  34. if [[ ! $CITIZEN || ! $KEYSTRING ]]; then
  35. echo "usage: makecitizen <username> <key>"
  36. exit 64
  37. fi
  38. # this should actually check if a _user_ exists,
  39. # not just the homedir
  40. if [ -d /home/$CITIZEN ]; then
  41. echo "$CITIZEN already exists - giving up"
  42. exit 68
  43. fi
  44. PASSWORD=`apg -d -n2`
  45. adduser --disabled-login $CITIZEN
  46. echo "$CITIZEN:$PASSWORD" | chpasswd
  47. chage -d 0 $CITIZEN
  48. echo "$KEYSTRING" >> /home/$CITIZEN/.ssh/authorized_keys
  49. echo "passwd: $PASSWORD"
  50. exit 0
  51. This is used like so:
  52. root@squiggle:~# ./makecitizen jrandomuser "ssh-rsa ..."
  53. It'll still do `adduser` interactively, which is fine for my purposes.
  54. I think this would be improved if it took a fullname and e-mail as input,
  55. and then sent that person a message, or at least output the text of one,
  56. telling them their password.
  57. It'd probably be improved even more than that if it operated in batch mode, was
  58. totally idempotent, and could be driven off some separate file or output
  59. containing the set of users.
  60. (Thoughts like this are how systems like Puppet and Chef are born.)