summaryrefslogtreecommitdiff
path: root/user-init
blob: 48881157a26966a266ed4f881bf2783ae7395cd7 (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. # Fileshares: <home>/shares.<sharetype>/<rogroup>/<rwgroup>/<sharename>
  186. # <sharetype>: Either mac or win depending on which of netatalk and samba provides r/w access to the shares
  187. # <rwgroup>: Group with write access to the share (usually the default group of the owner)
  188. # <rogroup>: Either rwgroup or secondary group with read-only access to the share
  189. # owner and rwgroup members must be member of both groups
  190. find "$HOME" -mindepth 1 -maxdepth 1 -type d -print | egrep "^$HOME/shares\." | (while read thisdir; do
  191. sharetype="`basename \"$thisdir\" | awk -F. '{print $2}'`"
  192. # Define dir and file exceptions
  193. case "$sharetype" in
  194. mac)
  195. dirs_world_rw_create='.AppleDB'
  196. dirs_group_rw_create='.AppleDesktop/Temporary Items/TheFindByContentFolder'
  197. dirs_group_ro_create='TheVolumeSettingsFolder'
  198. dirs_group_ro_update='.AppleDouble'
  199. files_group_ro_update=':2eDS_Store'
  200. dirs_no_access_purge='Network Trash Folder'
  201. ;;
  202. win)
  203. ;;
  204. *)
  205. continue
  206. ;;
  207. esac
  208. 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"
  209. exception_dirs_create="$dirs_world_rw_create/$dirs_group_rw_create/$dirs_group_ro_create"
  210. chown "$user": "$thisdir"
  211. chmod a=rX "$thisdir"
  212. find "$thisdir" -mindepth 1 -maxdepth 1 -type d -print | (while read thisdir; do
  213. rogroup="`basename \"$thisdir\"`"
  214. chown "$user":"$rogroup" "$thisdir"
  215. chmod ug=rX,o= "$thisdir"
  216. find "$thisdir" -mindepth 1 -maxdepth 1 -type d -print | (while read thisdir; do
  217. rwgroup="`basename \"$thisdir\"`"
  218. chown "$user":"$rwgroup" "$thisdir"
  219. chmod a=rX,g+s "$thisdir"
  220. find "$thisdir" -mindepth 1 -maxdepth 1 -type d -print | (while read thisdir; do
  221. sharename="`basename \"$thisdir\"`"
  222. chown "$user":"$rwgroup" "$thisdir"
  223. chmod u=rw,go=r,a+X,g+s "$thisdir"
  224. ifs="$IFS"
  225. # Set default permissions
  226. find "$thisdir" -mindepth 1 -maxdepth 1 -print | (while read thisdir; do
  227. item="`basename \"$thisdir\"`"
  228. IFS="/"; for exception in $exceptions; do IFS="$ifs";
  229. if [ "$item" = "$exception" ]; then
  230. continue 2
  231. fi
  232. done
  233. chgrp -R "$rwgroup" "$thisdir"
  234. chmod -R ug=rw,o=r,a+X,g+s "$thisdir"
  235. done)
  236. # Handle exception dirs to be created if not existing
  237. IFS="/"; for dir in $exception_dirs_create; do IFS="$ifs";
  238. if [ ! -d "$thisdir/$dir" ]; then
  239. rm -f "$thisdir/$dir"
  240. fi
  241. if [ ! -e "$thisdir/$dir" ]; then
  242. mkdir "$thisdir/$dir"
  243. fi
  244. chown "$user":"$rwgroup" "$thisdir/$dir"
  245. done
  246. IFS="/"; for dir in $dirs_world_rw_create; do IFS="$ifs";
  247. if [ "$rogroup" = "$rwgroup" ]; then
  248. chmod -R ug=rw,o=r,a+X,g+s "$thisdir/$dir"
  249. else
  250. chmod -R a=rw,a+X,g+s "$thisdir/$dir"
  251. fi
  252. done
  253. IFS="/"; for dir in $dirs_group_rw_create; do IFS="$ifs";
  254. chmod -R ug=rw,o=r,a+X,g+s "$thisdir/$dir"
  255. done
  256. IFS="/"; for dir in $dirs_group_ro_create; do IFS="$ifs";
  257. chmod -R u=rw,go=r,a+X,g+s "$thisdir/$dir"
  258. done
  259. # Handle exception dirs to be updated if already there
  260. IFS="/"; for dir in $dirs_group_ro_update; do IFS="$ifs";
  261. if [ -e "$thisdir/$dir" ]; then
  262. chmod u=rw,go=r,a+X,g+s "$thisdir/$dir"
  263. fi
  264. done
  265. # Handle exception files to be updated if already there
  266. IFS="/"; for file in $files_group_ro_update; do IFS="$ifs";
  267. if [ -e "$thisdir/$file" ]; then
  268. chmod u=rw,go=r,g+s "$thisdir/$file"
  269. fi
  270. done
  271. # Handle exception dirs to be purged and recreated
  272. IFS="/"; for dir in $dirs_no_access_purge; do IFS="$ifs";
  273. rm -rf "$thisdir/$dir"
  274. mkdir -m a= "$thisdir/$dir"
  275. chown nobody: "$thisdir/$dir"
  276. done
  277. IFS="$ifs"
  278. done)
  279. done)
  280. done)
  281. done)
  282. # Deprecated share permissions
  283. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/shares_win"`; do
  284. chgrp -R "$user" "$dir"
  285. chmod -R u=rw,g=rw,o=,ug+X,g+s "$dir"
  286. done
  287. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/shares_mac"`; do
  288. chgrp -R "$user" "$dir"
  289. chmod -R u=rw,g=rw,o=,ug+X,g+s "$dir"
  290. rm -rf "$dir/Network Trash Folder"
  291. mkdir "$dir/Network Trash Folder"
  292. chown nobody: "$dir/Network Trash Folder"
  293. chmod a= "$dir/Network Trash Folder"
  294. done
  295. # Ftp shares permissions
  296. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/ftp_$USER$"`; do
  297. chgrp -R "$user" "$dir"
  298. chmod -R ug=rw,o=r,a+X,g+s "$dir"
  299. rm -rf "$dir/Network Trash Folder"
  300. mkdir "$dir/Network Trash Folder"
  301. chown nobody: "$dir/Network Trash Folder"
  302. chmod a= "$dir/Network Trash Folder"
  303. done
  304. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/ftp_${USER}_ro$"`; do
  305. chown -R "$user": "$dir"
  306. chmod -R u=rw,go=r,a+X "$dir"
  307. rm -rf "$dir/Network Trash Folder"
  308. mkdir "$dir/Network Trash Folder"
  309. chown nobody: "$dir/Network Trash Folder"
  310. chmod a= "$dir/Network Trash Folder"
  311. done
  312. # Web shares permissions
  313. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/web_"`; do
  314. chown -R "$user": "$dir"
  315. # chmod -R u=rw,go=r,a+X $webdir
  316. #TODO: Only cgi scripts (.cgi and .pl) should be executable
  317. chmod -R u+rw,go+r,a+X "$dir"
  318. # leftover from ancient times with another policy
  319. if [ $NETATALK ]; then
  320. rm -rf "$dir/Network Trash Folder"
  321. fi
  322. done
  323. # Web shares permissions
  324. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/websites"`; do
  325. chown root: "$dir"
  326. chmod a=r,u+w,a+X "$dir"
  327. done
  328. for dir in `find $HOME -mindepth 2 -maxdepth 2 -type d | egrep "^$HOME/websites/"`; do
  329. chown -R "$user": "$dir"
  330. # chmod -R u=rw,go=r,a+X $webdir
  331. #TODO: Only cgi scripts (.cgi and .pl) should be executable
  332. chmod -R u+rw,go+r,a+X "$dir"
  333. # leftover from ancient times with another policy
  334. if [ $NETATALK ]; then
  335. rm -rf "$dir/Network Trash Folder"
  336. fi
  337. done
  338. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/webscripts"`; do
  339. chown root: "$dir"
  340. chmod a=r,u+w,a+X "$dir"
  341. done
  342. for dir in `find $HOME -mindepth 2 -maxdepth 2 -type d | egrep "^$HOME/webscripts/"`; do
  343. chown -R $user: "$dir"
  344. # chmod -R u=rw,go=r,a+X $webdir
  345. #TODO: Only cgi scripts (.cgi and .pl) should be executable
  346. chmod -R u+rw,go+r,a+X "$dir"
  347. done
  348. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/webdata"`; do
  349. chown "$user": "$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/webdata/"`; do
  353. chown -R "$user": "$dir"
  354. chmod -R u=rw,go=,u+X "$dir"
  355. done
  356. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/webshareddata"`; do
  357. chown "$user": "$dir"
  358. chmod a=r,u+w,a+X "$dir"
  359. done
  360. for dir in `find $HOME -mindepth 2 -maxdepth 2 -type d | egrep "^$HOME/webshareddata/"`; do
  361. chown -R "$user:" "$dir"
  362. chmod -R u=rw,go=r,a+X "$dir"
  363. done
  364. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/webphpsites"`; do
  365. chown root: "$dir"
  366. chmod u=rw,go=r,a+X "$dir"
  367. done
  368. for dir in `find $HOME -mindepth 2 -maxdepth 2 -type d | egrep "^$HOME/webphpsites/"`; do
  369. chown -R "$user":www-data "$dir"
  370. # chmod -R ug=rw,o=r,a+X $dir
  371. chmod -R ug=rw,o=,ug+X "$dir"
  372. done
  373. for dir in `find $HOME -mindepth 1 -maxdepth 1 -type d | egrep "^$HOME/webphpdata"`; do
  374. chown root: "$dir"
  375. chmod a=r,u+w,a+X "$dir"
  376. done
  377. for dir in `find $HOME -mindepth 2 -maxdepth 2 -type d | egrep "^$HOME/webphpdata/"`; do
  378. chown -R "$user":www-data "$dir"
  379. chmod -R ug=rw,o=,ug+X "$dir"
  380. done
  381. # Dummy user restrictions
  382. if [ -n "$REALUSERS_GROUPNAME" -a -n "$DUMMYSHAREDIR" -a -n "$DUMMYSHAREOWNER" -a -n "$DUMMYSHARENAME" ]; then
  383. [ -e $DUMMYSHAREDIR/$user ] \
  384. || mkdir $DUMMYSHAREDIR/$user
  385. chown $DUMMYSHAREOWNER: $DUMMYSHAREDIR/$user
  386. chmod u=rw,go=r,a+X $DUMMYSHAREDIR/$user
  387. if [ -e $HOME/$DUMMYSHARENAME ]; then
  388. if [ -L $HOME/$DUMMYSHARENAME ]; then
  389. ln -sf $DUMMYSHAREDIR/$user $HOME/$DUMMYSHARENAME
  390. chown $user: $HOME/$DUMMYSHARENAME
  391. else
  392. echo "WARNING: $HOME/$DUMMYSHAREDIR exists already. Leaving it as is..."
  393. fi
  394. else
  395. ln -s $DUMMYSHAREDIR/$user $HOME/$DUMMYSHARENAME
  396. chown $user: $HOME/$DUMMYSHARENAME
  397. fi
  398. if [ -n "$DUMMYAPACHECFG" -a -n "$DUMMYAPACHESHAREDIR" ]; then
  399. if [ -f /etc/apache/include.d/$DUMMYAPACHECFG -a -x /etc/init.d/apache ]; then
  400. if [ -e /etc/apache/include.d/$DUMMYAPACHECFG-$user ]; then
  401. echo "/etc/apache/include.d/$DUMMYAPACHECFG-$user exists already. Ignoring..."
  402. else
  403. echo "# Created automatically by adduser.local
  404. <Location /$DUMMYAPACHESHAREDIR/$user>
  405. <Limit GET POST>
  406. require user $user
  407. </Limit>
  408. </Location>" \
  409. > /etc/apache/include.d/$DUMMYAPACHECFG-$user
  410. apache_reload_needed="1"
  411. fi
  412. fi
  413. fi
  414. fi
  415. echo "."
  416. done
  417. if [ $XCHANGE ]; then
  418. for USER in $(ls $XDIRREAL); do
  419. id $user >/dev/null 2>&1 || rm -rf $XDIRREAL/$user
  420. done
  421. fi
  422. if [ "$apache_reload_needed" ]; then
  423. apache_do_reload=""
  424. case runmode in
  425. interactive)
  426. echo -n "Apache config changed. Reload Apache now (Y/n)? "
  427. read apache_reload
  428. case $apache_reload in
  429. y|Y|"")
  430. apache_do_reload="1"
  431. ;;
  432. esac
  433. ;;
  434. force)
  435. apache_do_reload="1"
  436. ;;
  437. *)
  438. echo "Apache config has changed. Remember to reload Apache...!"
  439. ;;
  440. esac
  441. if "$apache_do_reload" ]; then
  442. /etc/init.d/apache force-reload
  443. fi
  444. fi