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