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