summaryrefslogtreecommitdiff
path: root/user-init
blob: da23ba9df6d79123ac2e713e265c497edba19b3a (plain)
  1. #!/bin/sh
  2. set -e
  3. # reset flags
  4. apache_reload_needed=""
  5. runmode="normal"
  6. # subfolder name defaults (edit /etc/local/users.conf to override)
  7. mac="mac" # Optimized for sharing through AppleShare (netatalk)
  8. pc="pc" # Optimized for sharing though SMB/CIFS (Samba)
  9. xchange="xchange" # Readable by group
  10. # config (edit /etc/local/users.conf to override)
  11. # which are user accounts (adduser values are used if empty)
  12. first_uid=""
  13. last_uid=""
  14. do_quota="no" # Manage disk quota
  15. do_distrib="no" # Distributed shares (software archive)
  16. do_personal="no" # Personal shares (mac, pc, public_html)
  17. do_xchange="no" # Group-readable shares
  18. do_public="no" # Public share (web homepage)
  19. do_mac="no" # AppleShare-optimized share (netatalk)
  20. do_pc="no" # SMB-ptimized share (Samba)
  21. do_server="no" # Personal share on remote SMB server
  22. quota_roots="" # space-delimited list of disk devices
  23. quota_soft="100000"
  24. quota_hard="1000000"
  25. quota_newstyle="yes" # Woody used a different syntax...
  26. xchange_root="xchange"
  27. xchange_sharedroot="/home/XCHANGE"
  28. mac_root="mac"
  29. pc_root="pc"
  30. server_name="SERVER" # SMB name of remote server
  31. server_desc="remote server"
  32. server_root="server"
  33. server_conf="/etc/security/pam_mount.conf"
  34. server_userconf=".winpassword"
  35. ### No servicable parts below this line! ###
  36. if [ -e /etc/adduser.conf ]; then
  37. . /etc/adduser.conf
  38. else
  39. echo "/etc/adduser.conf missing. Exiting..."
  40. exit 1
  41. fi
  42. [ -r /etc/local/users.conf ] && . /etc/local/users.conf
  43. #TODO: Add conversion like below, and change remaining script to new variable names
  44. #[ -n "$XDIR" ] && xchange_sharedroot="$XDIR"
  45. # exit silently if this system lacks required hints
  46. [ -r /etc/local/volumes ] && . /etc/local/volumes || exit 0
  47. XDIRREAL="$XDIR/users/root"
  48. if [ -n "$XCHANGE" ]; then
  49. if [ ! -d "$XDIR" ]; then
  50. echo "XDIR doesn't exist. Ignoring XCHANGE!"
  51. XCHANGE=""
  52. fi
  53. fi
  54. if [ $# -gt 0 ]; then
  55. USERS=$*
  56. else
  57. # USERS=`getent passwd | awk -F: '{print $1}'`
  58. echo "uid required!"
  59. exit 1
  60. fi
  61. [ -n "$NETATALK_HOME" ] && mac="$NETATALK_HOME"
  62. [ -n "$SAMBA_HOME" ] && pc="$SAMBA_HOME"
  63. [ -n "$XCHANGE_HOME" ] && xchange="$XCHANGE_HOME"
  64. echo "Setting up additional folders and permissions..."
  65. for user in $USERS; do
  66. uid="`getent passwd \"$user\" | awk -F: '{print $3}' | head -1`"
  67. HOME="`getent passwd \"$user\" | awk -F: '{print $6}' | head -1`"
  68. if [ -z "$HOME" ]; then
  69. echo "User $user doesn't exist. Ignoring..."
  70. continue
  71. fi
  72. # Ignore non-human accounts silently
  73. [ "$uid" -ge "$FIRST_UID" -a "$uid" -le "$LAST_UID" ] || continue
  74. [ -d $HOME ] || continue
  75. # [ -L $HOME ] && continue
  76. echo -n $user
  77. # if [ -x /etc/local/quota.sh ]; then
  78. # /etc/local/quota.sh $user
  79. # fi
  80. [ $QUOTASOFT ] || QUOTASOFT="0"
  81. [ $QUOTAHARD ] || QUOTAHARD="0"
  82. for QUOTAHOME in $QUOTAHOMES; do
  83. if [ $NEW_QUOTA ]; then
  84. setquota $user $QUOTASOFT $QUOTAHARD 0 0 $QUOTAHOME
  85. else
  86. setquota $user $QUOTAHOME $QUOTASOFT $QUOTAHARD 0 0
  87. fi
  88. done
  89. mkdir -p $HOME/mail
  90. if [ "$USE_MBOX" ]; then
  91. touch $HOME/mail/mbox
  92. elif [ -f $HOME/mail/mbox -a ! -s $HOME/mail/mbox ]; then
  93. rm -f $HOME/mail/mbox
  94. fi
  95. if [ $NETATALK ]; then
  96. mkdir -p $HOME/$mac
  97. fi
  98. if [ $SAMBA ]; then
  99. mkdir -p $HOME/$pc
  100. fi
  101. if [ $XCHANGE ]; then
  102. mkdir -p $XDIRREAL/$user
  103. fi
  104. if [ $PUBLIC ]; then
  105. mkdir -p $HOME/public_html
  106. fi
  107. #TODO: Convert the above syntax to the below...
  108. if [ "$do_server" = "yes" ]; then
  109. mkdir -p $HOME/"$server_root"
  110. # chown root: $HOME/"$server_root"
  111. # chmod 0444 $HOME/"$server_root"
  112. # touch $HOME/"$server_root"/NOT_MOUNTED
  113. # chown root: $HOME/"$server_root"NOT_MOUNTED
  114. # chmod 0444 $HOME/"$server_root"/NOT_MOUNTED
  115. #TODO: figure out a safe way to do the above (avoid if mounted, for example!)
  116. fi
  117. #TODO: Move dir creation to localuserconfig, to respect user removing custom dirs
  118. su -s /bin/bash -c localuserconfig $user
  119. if [ "$do_server" = "yes" -a -r "$server_conf" -a -r $HOME/"$server_userconf" ]; then
  120. server_username="$(grep '^username' $HOME/$server_userconf | awk -F= '{print $2}' | head -1 | awk '{print $1}')"
  121. if grep -q "^volume $user " "$server_conf"; then
  122. perl -pi -e "s|^volume $user .*|volume $user smb $server_name $server_username $HOME/$server_root uid=$user,gid=$user - -|" "$server_conf"
  123. else
  124. echo "volume $user smb $server_name $server_username $HOME/$server_root uid=$user,gid=$user - -" >> "$server_conf"
  125. fi
  126. fi
  127. chown $user: $HOME
  128. chmod u=rwX,go=rX $HOME
  129. # Mail handling
  130. chown -R $user: $HOME/mail
  131. chmod -R u=rw,go=,u+X $HOME/mail
  132. if [ -f $HOME/.mailboxlist ]; then
  133. chown $user: $HOME/.mailboxlist
  134. chmod 0640 $HOME/.mailboxlist
  135. fi
  136. if [ -f $HOME/.forward ]; then
  137. chown $user: $HOME/.forward
  138. chmod 0640 $HOME/.forward
  139. fi
  140. if [ -f /var/mail/$user ]; then
  141. chown $user:mail /var/mail/$user
  142. chmod ug=rw,o= /var/mail/$user
  143. elif [ -f /var/spool/mail/$user ]; then
  144. chown $user:mail /var/spool/mail/$user
  145. chmod ug=rw,o= /var/spool/mail/$user
  146. fi
  147. # MySQL handling
  148. if [ -f $HOME/.my.cnf ]; then
  149. chown $user: $HOME/.my.cnf
  150. chmod 0600 $HOME/.my.cnf
  151. fi
  152. # Mac dir permissions
  153. if [ -d $HOME/$mac ]; then
  154. chown -R $user: $HOME/$mac
  155. chmod -R u=rw,g=r,o=,ug+X $HOME/$mac
  156. rm -rf $HOME/$mac/Network\ Trash\ Folder
  157. mkdir $HOME/$mac/Network\ Trash\ Folder
  158. chown nobody: $HOME/$mac/Network\ Trash\ Folder
  159. chmod a= $HOME/$mac/Network\ Trash\ Folder
  160. fi
  161. # PC dir permissions
  162. if [ -d $HOME/$pc ]; then
  163. chown -R $user: $HOME/$pc
  164. chmod -R u=rw,g=r,o=,ug+X $HOME/$pc
  165. fi
  166. # Exchange dir permissions
  167. if [ -d $XDIRREAL/$user ]; then
  168. chown -R $user:users $XDIRREAL/$user
  169. chmod -R g=r,g+X $XDIRREAL/$user
  170. if [ -e "x$HOME/$xchange" ]; then
  171. if [ -L "x$HOME/$xchange" ]; then
  172. ln -sf $XDIRREAL/$user $HOME/$xchange
  173. else
  174. echo "ERROR: $HOME/$xchange exists already. Leaving it as is..."
  175. fi
  176. else
  177. ln -s $XDIRREAL/$user $HOME/$xchange
  178. fi
  179. fi
  180. # Public dir permissions
  181. if [ -d $HOME/public_html ]; then
  182. chown -R $user: $HOME/public_html
  183. chmod -R u+rX,go=r,go+X $HOME/public_html
  184. if [ $NETATALK ]; then
  185. rm -rf $HOME/public_html/Network\ Trash\ Folder
  186. mkdir $HOME/public_html/Network\ Trash\ Folder
  187. chown nobody: $HOME/public_html/Network\ Trash\ Folder
  188. chmod a= $HOME/public_html/Network\ Trash\ Folder
  189. fi
  190. fi
  191. # Fileshares: <home>/shares.<sharetype>/<rogroup>/<rwgroup>/<sharename>
  192. # <sharetype>: Either mac or win depending on which of netatalk and samba provides r/w access to the shares
  193. # <rwgroup>: Group with write access to the share (usually the default group of the owner)
  194. # <rogroup>: Either rwgroup or secondary group with read-only access to the share
  195. # owner and rwgroup members must be member of both groups
  196. find "$HOME" -mindepth 1 -maxdepth 1 -type d -print | egrep "^$HOME/shares\." | (while read thisdir; do
  197. sharetype="`basename \"$thisdir\" | awk -F. '{print $2}'`"
  198. # Define dir and file exceptions
  199. case "$sharetype" in
  200. mac)
  201. dirs_world_rw_create='.AppleDB'
  202. dirs_group_rw_create='.AppleDesktop/Temporary Items/TheFindByContentFolder'
  203. dirs_group_ro_create='TheVolumeSettingsFolder'
  204. dirs_group_ro_update='.AppleDouble'
  205. files_group_ro_update=':2eDS_Store'
  206. dirs_no_access_purge='Network Trash Folder'
  207. ;;
  208. win)
  209. ;;
  210. *)
  211. continue
  212. ;;
  213. esac
  214. exceptions="$dirs_world_rw_create/$dirs_group_rw_create/$dirs_group_ro_create/$dirs_group_ro_update/$files_group_ro_update/$dirs_no_access_purge"
  215. exception_dirs_create="$dirs_world_rw_create/$dirs_group_rw_create/$dirs_group_ro_create"
  216. chown "$user": "$thisdir"
  217. chmod a=rX "$thisdir"
  218. find "$thisdir" -mindepth 1 -maxdepth 1 -type d -print | (while read thisdir; do
  219. rogroup="`basename \"$thisdir\"`"
  220. chown "$user":"$rogroup" "$thisdir"
  221. chmod ug=rX,o= "$thisdir"
  222. find "$thisdir" -mindepth 1 -maxdepth 1 -type d -print | (while read thisdir; do
  223. rwgroup="`basename \"$thisdir\"`"
  224. chown "$user":"$rwgroup" "$thisdir"
  225. chmod a=rX,g+s "$thisdir"
  226. find "$thisdir" -mindepth 1 -maxdepth 1 -type d -print | (while read thisdir; do
  227. sharename="`basename \"$thisdir\"`"
  228. chown "$user":"$rwgroup" "$thisdir"
  229. chmod u=rw,go=r,a+X,g+s "$thisdir"
  230. ifs="$IFS"
  231. # Set default permissions
  232. find "$thisdir" -mindepth 1 -maxdepth 1 -print | (while read thisdir; do
  233. item="`basename \"$thisdir\"`"
  234. IFS="/"; for exception in $exceptions; do IFS="$ifs";
  235. if [ "$item" = "$exception" ]; then
  236. continue 2
  237. fi
  238. done
  239. chgrp -R "$rwgroup" "$thisdir"
  240. chmod -R ug=rw,o=r,a+X,g+s "$thisdir"
  241. done)
  242. # Handle exception dirs to be created if not existing
  243. IFS="/"; for dir in $exception_dirs_create; do IFS="$ifs";
  244. if [ ! -d "$thisdir/$dir" ]; then
  245. rm -f "$thisdir/$dir"
  246. fi
  247. if [ ! -e "$thisdir/$dir" ]; then
  248. mkdir "$thisdir/$dir"
  249. fi
  250. chown "$user":"$rwgroup" "$thisdir/$dir"
  251. done
  252. IFS="/"; for dir in $dirs_world_rw_create; do IFS="$ifs";
  253. if [ "$rogroup" = "$rwgroup" ]; then
  254. chmod -R ug=rw,o=r,a+X,g+s "$thisdir/$dir"
  255. else
  256. chmod -R a=rw,a+X,g+s "$thisdir/$dir"
  257. fi
  258. done
  259. IFS="/"; for dir in $dirs_group_rw_create; do IFS="$ifs";
  260. chmod -R ug=rw,o=r,a+X,g+s "$thisdir/$dir"
  261. done
  262. IFS="/"; for dir in $dirs_group_ro_create; do IFS="$ifs";
  263. chmod -R u=rw,go=r,a+X,g+s "$thisdir/$dir"
  264. done
  265. # Handle exception dirs to be updated if already there
  266. IFS="/"; for dir in $dirs_group_ro_update; do IFS="$ifs";
  267. if [ -e "$thisdir/$dir" ]; then
  268. chmod u=rw,go=r,a+X,g+s "$thisdir/$dir"
  269. fi
  270. done
  271. # Handle exception files to be updated if already there
  272. IFS="/"; for file in $files_group_ro_update; do IFS="$ifs";
  273. if [ -e "$thisdir/$file" ]; then
  274. chmod u=rw,go=r,g+s "$thisdir/$file"
  275. fi
  276. done
  277. # Handle exception dirs to be purged and recreated
  278. IFS="/"; for dir in $dirs_no_access_purge; do IFS="$ifs";
  279. rm -rf "$thisdir/$dir"
  280. mkdir -m a= "$thisdir/$dir"
  281. chown nobody: "$thisdir/$dir"
  282. done
  283. IFS="$ifs"
  284. done)
  285. done)
  286. done)
  287. done)
  288. # Deprecated share permissions
  289. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/shares_win"`; do
  290. chgrp -R $user $dir
  291. chmod -R u=rw,g=rw,o=,ug+X,g+s $dir
  292. done
  293. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/shares_mac"`; do
  294. chgrp -R $user $dir
  295. chmod -R u=rw,g=rw,o=,ug+X,g+s $dir
  296. rm -rf $dir/Network\ Trash\ Folder
  297. mkdir $dir/Network\ Trash\ Folder
  298. chown nobody: $dir/Network\ Trash\ Folder
  299. chmod a= $dir/Network\ Trash\ Folder
  300. done
  301. # Ftp shares permissions
  302. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/ftp_$USER$"`; do
  303. chgrp -R $user $dir
  304. chmod -R ug=rw,o=r,a+X,g+s $dir
  305. rm -rf $dir/Network\ Trash\ Folder
  306. mkdir $dir/Network\ Trash\ Folder
  307. chown nobody: $dir/Network\ Trash\ Folder
  308. chmod a= $dir/Network\ Trash\ Folder
  309. done
  310. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/ftp_${USER}_ro$"`; do
  311. chown -R $user: $dir
  312. chmod -R u=rw,go=r,a+X $dir
  313. rm -rf $dir/Network\ Trash\ Folder
  314. mkdir $dir/Network\ Trash\ Folder
  315. chown nobody: $dir/Network\ Trash\ Folder
  316. chmod a= $dir/Network\ Trash\ Folder
  317. done
  318. # Web shares permissions
  319. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/web_"`; do
  320. chown -R $user: $dir
  321. # chmod -R u=rw,go=r,a+X $webdir
  322. #TODO: Only cgi scripts (.cgi and .pl) should be executable
  323. chmod -R u+rw,go+r,a+X $dir
  324. # leftover from ancient times with another policy
  325. if [ $NETATALK ]; then
  326. rm -rf $dir/Network\ Trash\ Folder
  327. fi
  328. done
  329. # Web shares permissions
  330. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/websites"`; do
  331. chown root: $dir
  332. chmod a=r,u+w,a+X $dir
  333. done
  334. for dir in `find $HOME -mindepth 2 -maxdepth 2 -type d | egrep "^$HOME/websites/"`; do
  335. chown -R $user: $dir
  336. # chmod -R u=rw,go=r,a+X $webdir
  337. #TODO: Only cgi scripts (.cgi and .pl) should be executable
  338. chmod -R u+rw,go+r,a+X $dir
  339. # leftover from ancient times with another policy
  340. if [ $NETATALK ]; then
  341. rm -rf $dir/Network\ Trash\ Folder
  342. fi
  343. done
  344. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/webscripts"`; do
  345. chown root: $dir
  346. chmod a=r,u+w,a+X $dir
  347. done
  348. for dir in `find $HOME -mindepth 2 -maxdepth 2 -type d | egrep "^$HOME/webscripts/"`; do
  349. chown -R $user: $dir
  350. # chmod -R u=rw,go=r,a+X $webdir
  351. #TODO: Only cgi scripts (.cgi and .pl) should be executable
  352. chmod -R u+rw,go+r,a+X $dir
  353. done
  354. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/webdata"`; do
  355. chown $user: $dir
  356. chmod a=r,u+w,a+X $dir
  357. done
  358. for dir in `find $HOME -mindepth 2 -maxdepth 2 -type d | egrep "^$HOME/webdata/"`; do
  359. chown -R $user: $dir
  360. chmod -R u=rw,go=,u+X $dir
  361. done
  362. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/webshareddata"`; do
  363. chown $user: $dir
  364. chmod a=r,u+w,a+X $dir
  365. done
  366. for dir in `find $HOME -mindepth 2 -maxdepth 2 -type d | egrep "^$HOME/webshareddata/"`; do
  367. chown -R $user: $dir
  368. chmod -R u=rw,go=r,a+X $dir
  369. done
  370. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/webphpsites"`; do
  371. chown root: $dir
  372. chmod u=rw,go=r,a+X $dir
  373. done
  374. for dir in `find $HOME -mindepth 2 -maxdepth 2 -type d | egrep "^$HOME/webphpsites/"`; do
  375. chown -R $user:www-data $dir
  376. # chmod -R ug=rw,o=r,a+X $dir
  377. chmod -R ug=rw,o=,ug+X $dir
  378. done
  379. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/webphpdata"`; do
  380. chown root: $dir
  381. chmod a=r,u+w,a+X $dir
  382. done
  383. for dir in `find $HOME -mindepth 2 -maxdepth 2 -type d | egrep "^$HOME/webphpdata/"`; do
  384. chown -R $user:www-data $dir
  385. chmod -R ug=rw,o=,ug+X $dir
  386. done
  387. # Dummy user restrictions
  388. if [ -n "$REALUSERS_GROUPNAME" -a -n "$DUMMYSHAREDIR" -a -n "$DUMMYSHAREOWNER" -a -n "$DUMMYSHARENAME" ]; then
  389. [ -e $DUMMYSHAREDIR/$user ] \
  390. || mkdir $DUMMYSHAREDIR/$user
  391. chown $DUMMYSHAREOWNER: $DUMMYSHAREDIR/$user
  392. chmod u=rw,go=r,a+X $DUMMYSHAREDIR/$user
  393. if [ -e $HOME/$DUMMYSHARENAME ]; then
  394. if [ -L $HOME/$DUMMYSHARENAME ]; then
  395. ln -sf $DUMMYSHAREDIR/$user $HOME/$DUMMYSHARENAME
  396. chown $user: $HOME/$DUMMYSHARENAME
  397. else
  398. echo "WARNING: $HOME/$DUMMYSHAREDIR exists already. Leaving it as is..."
  399. fi
  400. else
  401. ln -s $DUMMYSHAREDIR/$user $HOME/$DUMMYSHARENAME
  402. chown $user: $HOME/$DUMMYSHARENAME
  403. fi
  404. if [ -n "$DUMMYAPACHECFG" -a -n "$DUMMYAPACHESHAREDIR" ]; then
  405. if [ -f /etc/apache/include.d/$DUMMYAPACHECFG -a -x /etc/init.d/apache ]; then
  406. if [ -e /etc/apache/include.d/$DUMMYAPACHECFG-$user ]; then
  407. echo "/etc/apache/include.d/$DUMMYAPACHECFG-$user exists already. Ignoring..."
  408. else
  409. echo "# Created automatically by adduser.local
  410. <Location /$DUMMYAPACHESHAREDIR/$user>
  411. <Limit GET POST>
  412. require user $user
  413. </Limit>
  414. </Location>" \
  415. > /etc/apache/include.d/$DUMMYAPACHECFG-$user
  416. apache_reload_needed="1"
  417. fi
  418. fi
  419. fi
  420. fi
  421. echo "."
  422. done
  423. if [ $XCHANGE ]; then
  424. for USER in $(ls $XDIRREAL); do
  425. id $user >/dev/null 2>&1 || rm -rf $XDIRREAL/$user
  426. done
  427. fi
  428. if [ "$apache_reload_needed" ]; then
  429. apache_do_reload=""
  430. case runmode in
  431. interactive)
  432. echo -n "Apache config changed. Reload Apache now (Y/n)? "
  433. read apache_reload
  434. case $apache_reload in
  435. y|Y|"")
  436. apache_do_reload="1"
  437. ;;
  438. esac
  439. ;;
  440. force)
  441. apache_do_reload="1"
  442. ;;
  443. *)
  444. echo "Apache config has changed. Remember to reload Apache...!"
  445. ;;
  446. esac
  447. if "$apache_do_reload" ]; then
  448. /etc/init.d/apache force-reload
  449. fi
  450. fi