summaryrefslogtreecommitdiff
path: root/user-init
blob: 2d8be4030667e32634ab0b6f44dc20852a1ad257 (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. echo # dirty hack: better if being able to lower verbosity of localuserconfig
  108. su -s /bin/bash -c localuserconfig $user
  109. if [ "$do_server" = "yes" -a -r "$server_conf" -a -f $HOME/"$server_userconf" ]; then
  110. server_username="$(grep '^username' $HOME/$server_userconf | awk -F= '{print $2}' | head -1 | awk '{print $1}')"
  111. if grep -q "^volume $user " "$server_conf"; then
  112. perl -pi -e "s|^volume $user .*|volume $user smb $server_name $server_username $HOME/$server_root uid=$user,gid=$user - -|" "$server_conf"
  113. else
  114. echo "volume $user smb $server_name $server_username $HOME/$server_root uid=$user,gid=$user - -" >> "$server_conf"
  115. fi
  116. fi
  117. chown $user: $HOME
  118. chmod u=rwX,go=rX $HOME
  119. # Mail handling
  120. chown -R $user: $HOME/mail
  121. chmod -R u=rw,go=,u+X $HOME/mail
  122. if [ -f $HOME/.mailboxlist ]; then
  123. chown $user: $HOME/.mailboxlist
  124. chmod 0640 $HOME/.mailboxlist
  125. fi
  126. if [ -f $HOME/.forward ]; then
  127. chown $user: $HOME/.forward
  128. chmod 0640 $HOME/.forward
  129. fi
  130. if [ -f /var/mail/$user ]; then
  131. chown $user:mail /var/mail/$user
  132. chmod ug=rw,o= /var/mail/$user
  133. elif [ -f /var/spool/mail/$user ]; then
  134. chown $user:mail /var/spool/mail/$user
  135. chmod ug=rw,o= /var/spool/mail/$user
  136. fi
  137. # MySQL handling
  138. if [ -f $HOME/.my.cnf ]; then
  139. chown $user: $HOME/.my.cnf
  140. chmod 0600 $HOME/.my.cnf
  141. fi
  142. # Mac dir permissions
  143. if [ -d $HOME/$mac ]; then
  144. chown -R $user: $HOME/$mac
  145. chmod -R u=rw,g=r,o=,ug+X $HOME/$mac
  146. rm -rf $HOME/$mac/Network\ Trash\ Folder
  147. mkdir $HOME/$mac/Network\ Trash\ Folder
  148. chown nobody: $HOME/$mac/Network\ Trash\ Folder
  149. chmod a= $HOME/$mac/Network\ Trash\ Folder
  150. fi
  151. # PC dir permissions
  152. if [ -d $HOME/$pc ]; then
  153. chown -R $user: $HOME/$pc
  154. chmod -R u=rw,g=r,o=,ug+X $HOME/$pc
  155. fi
  156. # Exchange dir permissions
  157. if [ -d $XDIRREAL/$user ]; then
  158. chown -R $user:users $XDIRREAL/$user
  159. chmod -R g=r,g+X $XDIRREAL/$user
  160. if [ -e "x$HOME/$xchange" ]; then
  161. if [ -L "x$HOME/$xchange" ]; then
  162. ln -sf $XDIRREAL/$user $HOME/$xchange
  163. else
  164. echo "ERROR: $HOME/$xchange exists already. Leaving it as is..."
  165. fi
  166. else
  167. ln -s $XDIRREAL/$user $HOME/$xchange
  168. fi
  169. fi
  170. # Public dir permissions
  171. if [ -d $HOME/public_html ]; then
  172. chown -R $user: $HOME/public_html
  173. chmod -R u+rX,go=r,go+X $HOME/public_html
  174. if [ $NETATALK ]; then
  175. rm -rf $HOME/public_html/Network\ Trash\ Folder
  176. mkdir $HOME/public_html/Network\ Trash\ Folder
  177. chown nobody: $HOME/public_html/Network\ Trash\ Folder
  178. chmod a= $HOME/public_html/Network\ Trash\ Folder
  179. fi
  180. fi
  181. # Fileshares: <home>/shares.<sharetype>/<rogroup>/<rwgroup>/<sharename>
  182. # <sharetype>: Either mac or win depending on which of netatalk and samba provides r/w access to the shares
  183. # <rwgroup>: Group with write access to the share (usually the default group of the owner)
  184. # <rogroup>: Either rwgroup or secondary group with read-only access to the share
  185. # owner and rwgroup members must be member of both groups
  186. find "$HOME" -mindepth 1 -maxdepth 1 -type d -print | egrep "^$HOME/shares\." | (while read thisdir; do
  187. sharetype="`basename \"$thisdir\" | awk -F. '{print $2}'`"
  188. # Define dir and file exceptions
  189. case "$sharetype" in
  190. mac)
  191. dirs_world_rw_create='.AppleDB'
  192. dirs_group_rw_create='.AppleDesktop/Temporary Items/TheFindByContentFolder'
  193. dirs_group_ro_create='TheVolumeSettingsFolder'
  194. dirs_group_ro_update='.AppleDouble'
  195. files_group_ro_update=':2eDS_Store'
  196. dirs_no_access_purge='Network Trash Folder'
  197. ;;
  198. win)
  199. ;;
  200. *)
  201. continue
  202. ;;
  203. esac
  204. 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"
  205. exception_dirs_create="$dirs_world_rw_create/$dirs_group_rw_create/$dirs_group_ro_create"
  206. chown "$user": "$thisdir"
  207. chmod a=rX "$thisdir"
  208. find "$thisdir" -mindepth 1 -maxdepth 1 -type d -print | (while read thisdir; do
  209. rogroup="`basename \"$thisdir\"`"
  210. chown "$user":"$rogroup" "$thisdir"
  211. chmod ug=rX,o= "$thisdir"
  212. find "$thisdir" -mindepth 1 -maxdepth 1 -type d -print | (while read thisdir; do
  213. rwgroup="`basename \"$thisdir\"`"
  214. chown "$user":"$rwgroup" "$thisdir"
  215. chmod a=rX,g+s "$thisdir"
  216. find "$thisdir" -mindepth 1 -maxdepth 1 -type d -print | (while read thisdir; do
  217. sharename="`basename \"$thisdir\"`"
  218. chown "$user":"$rwgroup" "$thisdir"
  219. chmod u=rw,go=r,a+X,g+s "$thisdir"
  220. ifs="$IFS"
  221. # Set default permissions
  222. find "$thisdir" -mindepth 1 -maxdepth 1 -print | (while read thisdir; do
  223. item="`basename \"$thisdir\"`"
  224. IFS="/"; for exception in $exceptions; do IFS="$ifs";
  225. if [ "$item" = "$exception" ]; then
  226. continue 2
  227. fi
  228. done
  229. chgrp -R "$rwgroup" "$thisdir"
  230. chmod -R ug=rw,o=r,a+X,g+s "$thisdir"
  231. done)
  232. # Handle exception dirs to be created if not existing
  233. IFS="/"; for dir in $exception_dirs_create; do IFS="$ifs";
  234. if [ ! -d "$thisdir/$dir" ]; then
  235. rm -f "$thisdir/$dir"
  236. fi
  237. if [ ! -e "$thisdir/$dir" ]; then
  238. mkdir "$thisdir/$dir"
  239. fi
  240. chown "$user":"$rwgroup" "$thisdir/$dir"
  241. done
  242. IFS="/"; for dir in $dirs_world_rw_create; do IFS="$ifs";
  243. if [ "$rogroup" = "$rwgroup" ]; then
  244. chmod -R ug=rw,o=r,a+X,g+s "$thisdir/$dir"
  245. else
  246. chmod -R a=rw,a+X,g+s "$thisdir/$dir"
  247. fi
  248. done
  249. IFS="/"; for dir in $dirs_group_rw_create; do IFS="$ifs";
  250. chmod -R ug=rw,o=r,a+X,g+s "$thisdir/$dir"
  251. done
  252. IFS="/"; for dir in $dirs_group_ro_create; do IFS="$ifs";
  253. chmod -R u=rw,go=r,a+X,g+s "$thisdir/$dir"
  254. done
  255. # Handle exception dirs to be updated if already there
  256. IFS="/"; for dir in $dirs_group_ro_update; do IFS="$ifs";
  257. if [ -e "$thisdir/$dir" ]; then
  258. chmod u=rw,go=r,a+X,g+s "$thisdir/$dir"
  259. fi
  260. done
  261. # Handle exception files to be updated if already there
  262. IFS="/"; for file in $files_group_ro_update; do IFS="$ifs";
  263. if [ -e "$thisdir/$file" ]; then
  264. chmod u=rw,go=r,g+s "$thisdir/$file"
  265. fi
  266. done
  267. # Handle exception dirs to be purged and recreated
  268. IFS="/"; for dir in $dirs_no_access_purge; do IFS="$ifs";
  269. rm -rf "$thisdir/$dir"
  270. mkdir -m a= "$thisdir/$dir"
  271. chown nobody: "$thisdir/$dir"
  272. done
  273. IFS="$ifs"
  274. done)
  275. done)
  276. done)
  277. done)
  278. # Deprecated share permissions
  279. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/shares_win"`; do
  280. chgrp -R $user $dir
  281. chmod -R u=rw,g=rw,o=,ug+X,g+s $dir
  282. done
  283. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/shares_mac"`; do
  284. chgrp -R $user $dir
  285. chmod -R u=rw,g=rw,o=,ug+X,g+s $dir
  286. rm -rf $dir/Network\ Trash\ Folder
  287. mkdir $dir/Network\ Trash\ Folder
  288. chown nobody: $dir/Network\ Trash\ Folder
  289. chmod a= $dir/Network\ Trash\ Folder
  290. done
  291. # Ftp shares permissions
  292. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/ftp_$USER$"`; do
  293. chgrp -R $user $dir
  294. chmod -R ug=rw,o=r,a+X,g+s $dir
  295. rm -rf $dir/Network\ Trash\ Folder
  296. mkdir $dir/Network\ Trash\ Folder
  297. chown nobody: $dir/Network\ Trash\ Folder
  298. chmod a= $dir/Network\ Trash\ Folder
  299. done
  300. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/ftp_${USER}_ro$"`; do
  301. chown -R $user: $dir
  302. chmod -R u=rw,go=r,a+X $dir
  303. rm -rf $dir/Network\ Trash\ Folder
  304. mkdir $dir/Network\ Trash\ Folder
  305. chown nobody: $dir/Network\ Trash\ Folder
  306. chmod a= $dir/Network\ Trash\ Folder
  307. done
  308. # Web shares permissions
  309. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/web_"`; do
  310. chown -R $user: $dir
  311. # chmod -R u=rw,go=r,a+X $webdir
  312. #TODO: Only cgi scripts (.cgi and .pl) should be executable
  313. chmod -R u+rw,go+r,a+X $dir
  314. # leftover from ancient times with another policy
  315. if [ $NETATALK ]; then
  316. rm -rf $dir/Network\ Trash\ Folder
  317. fi
  318. done
  319. # Web shares permissions
  320. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/websites"`; do
  321. chown root: $dir
  322. chmod a=r,u+w,a+X $dir
  323. done
  324. for dir in `find $HOME -mindepth 2 -maxdepth 2 -type d | egrep "^$HOME/websites/"`; do
  325. chown -R $user: $dir
  326. # chmod -R u=rw,go=r,a+X $webdir
  327. #TODO: Only cgi scripts (.cgi and .pl) should be executable
  328. chmod -R u+rw,go+r,a+X $dir
  329. # leftover from ancient times with another policy
  330. if [ $NETATALK ]; then
  331. rm -rf $dir/Network\ Trash\ Folder
  332. fi
  333. done
  334. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/webscripts"`; do
  335. chown root: $dir
  336. chmod a=r,u+w,a+X $dir
  337. done
  338. for dir in `find $HOME -mindepth 2 -maxdepth 2 -type d | egrep "^$HOME/webscripts/"`; do
  339. chown -R $user: $dir
  340. # chmod -R u=rw,go=r,a+X $webdir
  341. #TODO: Only cgi scripts (.cgi and .pl) should be executable
  342. chmod -R u+rw,go+r,a+X $dir
  343. done
  344. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/webdata"`; do
  345. chown $user: $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/webdata/"`; do
  349. chown -R $user: $dir
  350. chmod -R u=rw,go=,u+X $dir
  351. done
  352. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/webshareddata"`; do
  353. chown $user: $dir
  354. chmod a=r,u+w,a+X $dir
  355. done
  356. for dir in `find $HOME -mindepth 2 -maxdepth 2 -type d | egrep "^$HOME/webshareddata/"`; do
  357. chown -R $user: $dir
  358. chmod -R u=rw,go=r,a+X $dir
  359. done
  360. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/webphpsites"`; do
  361. chown root: $dir
  362. chmod u=rw,go=r,a+X $dir
  363. done
  364. for dir in `find $HOME -mindepth 2 -maxdepth 2 -type d | egrep "^$HOME/webphpsites/"`; do
  365. chown -R $user:www-data $dir
  366. # chmod -R ug=rw,o=r,a+X $dir
  367. chmod -R ug=rw,o=,ug+X $dir
  368. done
  369. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/webphpdata"`; do
  370. chown root: $dir
  371. chmod a=r,u+w,a+X $dir
  372. done
  373. for dir in `find $HOME -mindepth 2 -maxdepth 2 -type d | egrep "^$HOME/webphpdata/"`; do
  374. chown -R $user:www-data $dir
  375. chmod -R ug=rw,o=,ug+X $dir
  376. done
  377. # Dummy user restrictions
  378. if [ -n "$REALUSERS_GROUPNAME" -a -n "$DUMMYSHAREDIR" -a -n "$DUMMYSHAREOWNER" -a -n "$DUMMYSHARENAME" ]; then
  379. [ -e $DUMMYSHAREDIR/$user ] \
  380. || mkdir $DUMMYSHAREDIR/$user
  381. chown $DUMMYSHAREOWNER: $DUMMYSHAREDIR/$user
  382. chmod u=rw,go=r,a+X $DUMMYSHAREDIR/$user
  383. if [ -e $HOME/$DUMMYSHARENAME ]; then
  384. if [ -L $HOME/$DUMMYSHARENAME ]; then
  385. ln -sf $DUMMYSHAREDIR/$user $HOME/$DUMMYSHARENAME
  386. chown $user: $HOME/$DUMMYSHARENAME
  387. else
  388. echo "WARNING: $HOME/$DUMMYSHAREDIR exists already. Leaving it as is..."
  389. fi
  390. else
  391. ln -s $DUMMYSHAREDIR/$user $HOME/$DUMMYSHARENAME
  392. chown $user: $HOME/$DUMMYSHARENAME
  393. fi
  394. if [ -n "$DUMMYAPACHECFG" -a -n "$DUMMYAPACHESHAREDIR" ]; then
  395. if [ -f /etc/apache/include.d/$DUMMYAPACHECFG -a -x /etc/init.d/apache ]; then
  396. if [ -e /etc/apache/include.d/$DUMMYAPACHECFG-$user ]; then
  397. echo "/etc/apache/include.d/$DUMMYAPACHECFG-$user exists already. Ignoring..."
  398. else
  399. echo "# Created automatically by adduser.local
  400. <Location /$DUMMYAPACHESHAREDIR/$user>
  401. <Limit GET POST>
  402. require user $user
  403. </Limit>
  404. </Location>" \
  405. > /etc/apache/include.d/$DUMMYAPACHECFG-$user
  406. apache_reload_needed="1"
  407. fi
  408. fi
  409. fi
  410. fi
  411. echo "."
  412. done
  413. if [ $XCHANGE ]; then
  414. for USER in $(ls $XDIRREAL); do
  415. id $user >/dev/null 2>&1 || rm -rf $XDIRREAL/$user
  416. done
  417. fi
  418. if [ "$apache_reload_needed" ]; then
  419. apache_do_reload=""
  420. case runmode in
  421. interactive)
  422. echo -n "Apache config changed. Reload Apache now (Y/n)? "
  423. read apache_reload
  424. case $apache_reload in
  425. y|Y|"")
  426. apache_do_reload="1"
  427. ;;
  428. esac
  429. ;;
  430. force)
  431. apache_do_reload="1"
  432. ;;
  433. *)
  434. echo "Apache config has changed. Remember to reload Apache...!"
  435. ;;
  436. esac
  437. if "$apache_do_reload" ]; then
  438. /etc/init.d/apache force-reload
  439. fi
  440. fi