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