summaryrefslogtreecommitdiff
path: root/adduser.local
blob: 3692c06dc590cf61be34c8e984b6d514eb09dc9c (plain)
  1. #!/bin/sh
  2. #
  3. # Copyright © 2001-2006, 2010, 2013, Jonas Smedegaard <dr@jones.dk>
  4. # Description: Adduser extensions for Redpill <http://www.redpill.dk>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 3, or (at your option)
  9. # any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful, but
  12. # WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. # General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. # TODO: Invent a way to flag for INTERACTIVE without abusing VERBOSE
  20. #
  21. set -e
  22. NEWUSERNAME=$1
  23. NEWUID=$2
  24. NEWGID=$3
  25. NEWHOMEDIR=$4
  26. fullname() { getent passwd $NEWUSERNAME | awk -F: '{print $5}' | awk -F, '{print $1}'; }
  27. maildomain() { if [ -r /etc/mailname ]; then head -n 1 /etc/mailname; fi; }
  28. listlists() { if [ -x /usr/sbin/list_lists ]; then /usr/sbin/list_lists -ab -V "$1"; fi; }
  29. add2list() { /bin/echo "$1" | /usr/sbin/add_members -w y -a y -r - "$2"; }
  30. . /etc/adduser.conf
  31. # Ignore non-human accounts silently
  32. [ "$NEWUID" -ge "$FIRST_UID" ] && [ "$NEWUID" -le "$LAST_UID" ] || exit 0
  33. [ ! -r /etc/local/users.conf ] || . /etc/local/users.conf
  34. # resolve account profiles
  35. case "$NEWUSERNAME" in
  36. www-*)
  37. profile_aux=1
  38. profile_web=1
  39. ;;
  40. *-*)
  41. profile_aux=1
  42. ;;
  43. *)
  44. ;;
  45. esac
  46. # Samba password
  47. #FIXME: Check if enabled in samba.conf (or included files!)
  48. if [ -z "$profile_aux" ] && [ -n "$VERBOSE" ] && [ "$VERBOSE" -gt 0 ] && ([ -x /usr/bin/pdbedit ] || [ -x /usr/bin/smbpasswd ]); then
  49. echo -n "Add samba access to the account (y/N)? "
  50. read sambaaccount
  51. case $sambaaccount in
  52. y|Y)
  53. if [ -x /usr/bin/pdbedit ]; then
  54. /usr/bin/pdbedit -a -u "$NEWUSERNAME"
  55. elif [ -x /usr/bin/smbpasswd ] && [ -f /etc/samba/smbpasswd ]; then
  56. /usr/bin/smbpasswd -a $NEWUSERNAME
  57. fi
  58. ;;
  59. esac
  60. fi
  61. # Mail forwarding
  62. if [ -z "$profile_aux" ] && [ -n "$VERBOSE" ] && [ "$VERBOSE" -gt 0 ] && [ -x /usr/local/sbin/userforward ]; then
  63. echo -n "Forward mail to an external account (y/N)? "
  64. read mailforward
  65. case $mailforward in
  66. y|Y)
  67. echo -n "Enter external email address: "
  68. read mail_address
  69. case $mail_address in
  70. ?*@?*.*)
  71. /usr/local/sbin/userforward $NEWUSERNAME $mail_address
  72. ;;
  73. ?*)
  74. echo "That wasn't a proper email address - skipping..."
  75. ;;
  76. esac
  77. ;;
  78. esac
  79. fi
  80. # Mailing lists
  81. maildomain="$(maildomain)"
  82. if [ -z "$profile_aux" ] && [ -n "$VERBOSE" ] && [ "$VERBOSE" -gt 0 ] && [ -n "$maildomain" ]; then
  83. echo -n "Subscribe $NEWUSERNAME@$maildomain to mailinglists (y/N)? "
  84. read subscribe
  85. case $subscribe in
  86. y|Y)
  87. fullname="$(fullname)"
  88. if [ -n "$fullname" ]; then
  89. subscriber="$fullname <$NEWUSERNAME@$maildomain>"
  90. else
  91. subscriber="$NEWUSERNAME@$maildomain"
  92. fi
  93. lists="$(listlists "$maildomain")"
  94. if [ -z "$lists" ]; then
  95. echo "No mailinglists found."
  96. fi
  97. for list in $lists; do
  98. echo -n "Subscribe to mailinglist $list (y/N)? "
  99. read subscribe
  100. case $subscribe in
  101. y|Y)
  102. add2list "$subscriber" "$list";;
  103. esac
  104. done
  105. esac
  106. fi
  107. if [ -z "$profile_aux" ]; then
  108. if [ -n "$USERS_GROUPNAME" ]; then
  109. addgroup $NEWUSERNAME $USERS_GROUPNAME
  110. fi
  111. if [ -n "$VERBOSE" ] && [ "$VERBOSE" -gt 0 ] && [ -n "$REALUSERS_GROUPNAME" ]; then
  112. echo "This system has both dummy and real users:"
  113. echo " Dummy users have read access to specific files."
  114. echo " Real users have read/write access to personal files."
  115. echo -n "Is this a dummy user (Y/n)? "
  116. read realuser
  117. case $realuser in
  118. y|Y|"")
  119. ;;
  120. *)
  121. addgroup $NEWUSERNAME $REALUSERS_GROUPNAME
  122. ;;
  123. esac
  124. fi
  125. fi
  126. #if [ -n "$VERBOSE" ] && [ "$VERBOSE" -gt 0 ] && [ -n "$REALUSERS_GROUPNAME" ]; then
  127. # cat <<+
  128. #Is this a remote or local user (with need for physical
  129. #access to devices like CD-ROM, soundcard and modem needed)?
  130. #
  131. #+
  132. if [ -z "$profile_aux" ] && [ -n "$VERBOSE" ] && [ "$VERBOSE" -gt 0 ]; then
  133. echo -n "Grant this user access to local utilities (y/N)?"
  134. read localuser
  135. case $localuser in
  136. y|Y)
  137. for group in floppy dialout cdrom audio video games; do
  138. addgroup $NEWUSERNAME $group
  139. done
  140. ;;
  141. *)
  142. ;;
  143. esac
  144. fi
  145. if [ -z "$profile_aux" ] && [ -z "$profile_web" ] && [ -n "$VERBOSE" ] && [ "$VERBOSE" -gt 0 ] && [ -d /var/www ]; then
  146. echo -n "Setup web hosting for this user (y/N)?"
  147. read webuser
  148. case $webuser in
  149. y|Y)
  150. profile_web=1
  151. ;;
  152. *)
  153. ;;
  154. esac
  155. fi
  156. if [ -n "$profile_web" ] && [ -d /var/www ]; then
  157. webroot="$NEWHOMEDIR/public_websites"
  158. cgiroot="$NEWHOMEDIR/public_cgi"
  159. dataroot="$NEWHOMEDIR/private_webdata"
  160. webmount="/var/www/vhosts/$NEWUSERNAME"
  161. cgimount="/var/www/cgi-vhosts/$NEWUSERNAME"
  162. echo >&2 "Adding private and public subdirs in homedir ..."
  163. install -d -o "$NEWUID" -g "$NEWGID" "$webroot" "$cgiroot"
  164. install -d -o "$NEWUID" -g "$NEWGID" -m 0750 "$dataroot"
  165. [ ! -r /etc/local/webusers.conf ] || . /etc/local/webusers.conf
  166. if [ -n "$WEBUSERS_BINDMOUNT" ]; then
  167. echo >&2 "Adding subdirs below /var/www ..."
  168. install -d "$webmount" "$cgimount"
  169. echo >&2 "Adding bind mount entries to /etc/fstab ..."
  170. webroot="$webroot" cgiroot="$cgiroot" webmount="$webmount" cgimount="$cgimount" perl -w -p -0 -i.old \
  171. -e 'my $webroot = $ENV{"webroot"};' \
  172. -e 'my $cgiroot = $ENV{"cgiroot"};' \
  173. -e 'my $webmount = $ENV{"webmount"};' \
  174. -e 'my $cgimount = $ENV{"cgimount"};' \
  175. -e 'if (s/\n[# ]*($webroot[ \t]+$webmount[ \t]+[^\n]+)/\n$1/) {' \
  176. -e '$web_seen++;' \
  177. -e 'print STDERR "Using existing web entry in fstab\n";' \
  178. -e '};' \
  179. -e 'if (s/\n[# ]*($cgiroot[ \t]+$cgimount[ \t]+[^\n]+)/\n$1/) {' \
  180. -e '$cgi_seen++;' \
  181. -e 'print STDERR "Using existing cgi entry in fstab\n";' \
  182. -e '};' \
  183. -e 's/$/\n$webroot\t$webmount\tauto\tbind\n/ unless $web_seen;' \
  184. -e 's/\n[# ]*($webroot[ \t]+$webmount[ \t]+[^\n]+)/\n$1\n$cgiroot\t$cgimount\tauto\tbind/ unless $cgi_seen;' \
  185. /etc/fstab
  186. echo >&2 "Mount web and cgi subdirs ..."
  187. mount "$webmount"
  188. mount "$cgimount"
  189. fi
  190. fi
  191. if [ -z "$profile_aux" ] && [ -x /usr/local/sbin/user-init ]; then
  192. /usr/local/sbin/user-init $NEWUSERNAME
  193. fi