rfa 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. #!/bin/sh
  2. # Echo help information
  3. usage()
  4. {
  5. cat <<EOF
  6. Usage: $0 [options] <command> <parameters>
  7. Tool for VCS repository administration.
  8. VCS support: $possible_vcs
  9. Options:
  10. -h, --help
  11. Print help message.
  12. -c <PATH>, --config=<PATH>
  13. Specify config file (default is /etc/repoforge/rfa.conf)
  14. -s <git/svn>, --vcs=<git/svn>
  15. Choose VCS type to create new repository (default is git)
  16. --git
  17. Same as "-c git"
  18. --svn
  19. Same as "-c svn"
  20. Commands:
  21. info <repository>
  22. Get information for specified repository.
  23. add <repository1> [repository2] ...
  24. Create new repository.
  25. del <repository1> [repository2] ...
  26. Delete existing repository.
  27. fixmod <repository1> [repository2] ...
  28. Fix access rights for repository.
  29. rename <old_name> <new_name>
  30. Rename repository.
  31. useradd <repository> <r/w/rw> <user1> [user2] ...
  32. Set access rights for user(s) to repository.
  33. userdel <repository> <r/w/rw> <user1> [user2] ...
  34. Remove access rights for user(s) to repository.
  35. userdel-all <repository> <r/w/rw>
  36. Remove access rights for all users for repository.
  37. EOF
  38. }
  39. group_add()
  40. {
  41. groupadd -K GID_MIN=${group_gid_min} "${group_r_prefix}$1" || return 1
  42. groupadd -K GID_MIN=${group_gid_min} "${group_w_prefix}$1" || return 1
  43. }
  44. group_del()
  45. {
  46. groupdel "${group_r_prefix}$1" || return 1
  47. groupdel "${group_w_prefix}$1" || return 1
  48. }
  49. group_rename()
  50. {
  51. local old_name="$1"
  52. local new_name="$2"
  53. groupmod -n "${group_r_prefix}$new_name" "${group_r_prefix}$old_name" || return 1
  54. groupmod -n "${group_w_prefix}$new_name" "${group_w_prefix}$old_name" || return 1
  55. return 0
  56. }
  57. get_group_members()
  58. {
  59. local group="$1"
  60. grep -e "^$group:" /etc/group | sed -e "s|^.*:||" -e "s|,| |g"
  61. }
  62. create_repo_svn()
  63. {
  64. svnadmin --fs-type=fsfs create "$1" || return 1
  65. }
  66. create_repo_git()
  67. {
  68. mkdir -p "$1" || return 1
  69. # git -C "$1" init --bare --shared >/dev/null || return 1
  70. cd "$1" || return 1
  71. git init --bare --shared >/dev/null || return 1
  72. cd - >/dev/null
  73. }
  74. data_add()
  75. {
  76. local r_name="$1"
  77. local r_root="$2"
  78. local r_link="$3"
  79. local repo_r="${r_root}/${r_name}"
  80. local repo_w="${r_root}/${r_name}/${r_name}"
  81. mkdir -p "${repo_r}"
  82. local saved_umask=`umask`
  83. umask 002
  84. local create_vcs_func="create_repo_$opts_vcs"
  85. $create_vcs_func "$repo_w" || return 1
  86. umask ${saved_umask}
  87. }
  88. data_fixmod()
  89. {
  90. local r_name="$1"
  91. local r_root="$2"
  92. local r_link="$3"
  93. local repo_r="${r_root}/${r_name}"
  94. local repo_w="${r_root}/${r_name}/${r_name}"
  95. chmod 750 "${repo_r}" || return 1
  96. chgrp "${group_r_prefix}${r_name}" "${repo_r}" || return 1
  97. chgrp -R "${group_w_prefix}${r_name}" "${repo_w}" || return 1
  98. local dirs=""
  99. dirs=`find "$repo_w" -type d`
  100. local dir=""
  101. for dir in $dirs; do
  102. chmod g+s "$dir" || return 1
  103. done
  104. chmod -R g+w "${repo_w}" || return 1
  105. }
  106. data_del()
  107. {
  108. local r_name="$1"
  109. local r_root="$2"
  110. rm -Rf "$r_root/$r_name"
  111. }
  112. link_add()
  113. {
  114. local r_name="$1"
  115. local r_root="$2"
  116. local r_link="$3"
  117. ln -sf "$r_root/$r_name/$r_name" "$r_link/$r_name"
  118. }
  119. link_del()
  120. {
  121. local r_name="$1"
  122. local r_link="$2"
  123. rm -f "$r_link/$r_name"
  124. }
  125. repository_add()
  126. {
  127. local rep_name=""
  128. local repo_root
  129. local repo_link
  130. for rep_name in "$@"; do
  131. repo_root=`find_repo_root "$rep_name"`
  132. repo_link=`find_repo_link "$rep_name"`
  133. test "x$repo_root" = "x" || { echo "Error: The repository \"$rep_name\" already exists." 1>&2; exit 1; }
  134. eval repo_root="\$repository_${opts_vcs}_root"
  135. eval repo_link="\$repository_${opts_vcs}_link"
  136. test "x$repo_root" = "x" && { echo "Error: Illegal repository root \"\"" 1>&2; exit 1; }
  137. test "x$repo_link" = "x" && { echo "Error: Illegal repository link \"\"" 1>&2; exit 1; }
  138. test -d "$repo_root" || { echo "Error: Illegal repository root \"$repo_root\"" 1>&2; exit 1; }
  139. test -d "$repo_link" || { echo "Error: Illegal repository link \"$repo_link\"" 1>&2; exit 1; }
  140. group_add "$rep_name" || { echo "Error: Can't add group for repository \"$rep_name\"" 1>&2; exit 1; }
  141. data_add "$rep_name" "$repo_root" "$repo_link" || { echo "Error: Can't add repository \"$rep_name\"" 1>&2; exit 1; }
  142. data_fixmod "$rep_name" "$repo_root" "$repo_link" || { echo "Error: Can't fix mode for repository \"$rep_name\"" 1>&2; exit 1; }
  143. link_add "$rep_name" "$repo_root" "$repo_link" || { echo "Error: Can't add link for repository \"$rep_name\"" 1>&2; exit 1; }
  144. echo "Info: The repository \"$rep_name\" was succesfully created."
  145. done
  146. }
  147. # Delete the repository
  148. repository_del()
  149. {
  150. local rep_name=""
  151. local sure=""
  152. local repo_root
  153. local repo_link
  154. for rep_name in "$@"; do
  155. repo_root=`find_repo_root "$rep_name"`
  156. repo_link=`find_repo_link "$rep_name"`
  157. test "x$repo_root" = "x" -o "x$repo_link" = "x" && { echo "Error: Can't find repository \"$rep_name\"" 1>&2; exit 1; }
  158. if test "x$opts_force" = "x"; then
  159. read -r -p "Deleting repository \"$rep_name\". Are you sure (y/n)? " sure
  160. test "x$sure" = "xy" -o "x$sure" = "xY" || { echo "Info: The repository \"$rep_name\" will be not deleted."; continue; }
  161. fi
  162. link_del "$rep_name" "$repo_link" || { echo "Error: Can't remove link for repository \"$rep_name\"" 1>&2; exit 1; }
  163. data_del "$rep_name" "$repo_root" || { echo "Error: Can't remove repository \"$rep_name\"" 1>&2; exit 1; }
  164. group_del "$rep_name" || { echo "Error: Can't remove group for repository \"$rep_name\"" 1>&2; exit 1; }
  165. echo "Info: The repository \"$rep_name\" was succesfully deleted."
  166. done
  167. }
  168. # Fix broken repository: symlink, access rights, owner.
  169. repository_fixmod()
  170. {
  171. local rep_name=""
  172. local repo_root
  173. local repo_link
  174. for rep_name in "$@"; do
  175. repo_root=`find_repo_root "$rep_name"`
  176. repo_link=`find_repo_link "$rep_name"`
  177. test "x$repo_root" = "x" -o "x$repo_link" = "x" && { echo "Error: Can't find repository \"$rep_name\"" 1>&2; exit 1; }
  178. data_fixmod "$rep_name" "$repo_root" "$repo_link" || { echo "Error: Can't fix repository's \"$rep_name\" mode" 1>&2; exit 1; }
  179. link_add "$rep_name" "$repo_root" "$repo_link" || { echo "Error: Can't create link for repository \"$rep_name\"" 1>&2; exit 1; }
  180. echo "Info: The repository \"$rep_name\" was succesfully fixed."
  181. done
  182. }
  183. # Rename existing repository
  184. repository_rename()
  185. {
  186. local old_name="$1"
  187. local new_name="$2"
  188. local repo_root
  189. local repo_link
  190. # Check if target name is already exists
  191. repo_root=`find_repo_root "$new_name"`
  192. test "x$repo_root" = "x" || { echo "Error: The repository \"$new_name\" is already exist" 1>&2; return 1; }
  193. repo_root=`find_repo_root "$old_name"`
  194. repo_link=`find_repo_link "$old_name"`
  195. test "x$repo_root" != "x" -a -d "$repo_root/$old_name" || { echo "Error: Can't find repository \"$old_name\"" 1>&2; return 1; }
  196. test "x$repo_root" != "x" -a -L "$repo_link/$old_name" || { echo "Error: Can't find link of repository \"$old_name\"" 1>&2; return 1; }
  197. # Real move repository and link
  198. mv -f "$repo_root/$old_name" "$repo_root/$new_name" || { echo "Error: Can't move repository \"$old_name\"" 1>&2; return 1; }
  199. link_del "$old_name" "$repo_link" || { echo "Error: Can't remove link for repository \"$old_name\"" 1>&2; }
  200. link_add "$new_name" "$repo_root" "$repo_link" || { echo "Error: Can't create link for repository \"$new_name\"" 1>&2; }
  201. # Rename a groups
  202. group_rename "$old_name" "$new_name" || { echo "Error: Can't rename access groups for repository \"$new_name\"" 1>&2; }
  203. echo "Info: The repository \"$old_name\" was succesfully renamed to \"$new_name\""
  204. return 0
  205. }
  206. # Show repository info
  207. repository_info()
  208. {
  209. local repo="$1"
  210. local repo_root
  211. local repo_link
  212. repo_root=`find_repo_root "$repo"`
  213. repo_link=`find_repo_link "$repo"`
  214. test "x$repo_root" != "x" -a -d "$repo_root/$repo" || { echo "Error: Can't find repository \"$repo\"" 1>&2; return 1; }
  215. echo "Name: $repo"
  216. echo "VCS : "`find_repo_vcs "$repo"`
  217. echo "Path: $repo_root"
  218. echo "Link: $repo_link"
  219. echo "Write access: "`get_group_members "${group_w_prefix}$repo"`
  220. echo "Read access: "`get_group_members "${group_r_prefix}$repo"`
  221. return 0
  222. }
  223. # Add users to access groups
  224. user_add()
  225. {
  226. local w=0
  227. local r=0
  228. local repository_name="$1"
  229. local user_name=""
  230. shift
  231. case "$1" in
  232. r)
  233. r=1
  234. ;;
  235. w)
  236. w=1
  237. ;;
  238. rw|wr)
  239. r=1
  240. w=1
  241. ;;
  242. *)
  243. echo "Error: Illegal parameter \"$1\"" 1>&2
  244. exit 1
  245. ;;
  246. esac
  247. shift
  248. for user_name in "$@"; do
  249. test $r -ne 0 && adduser "${user_name}" "${group_r_prefix}${repository_name}"
  250. test $w -ne 0 && adduser "${user_name}" "${group_w_prefix}${repository_name}"
  251. done
  252. }
  253. # Remove users from access groups
  254. user_del()
  255. {
  256. local w=0
  257. local r=0
  258. local repository_name=$1
  259. local user_name=""
  260. shift
  261. case "$1" in
  262. r)
  263. r=1
  264. ;;
  265. w)
  266. w=1
  267. ;;
  268. rw|wr)
  269. r=1
  270. w=1
  271. ;;
  272. *)
  273. echo "Error: Illegal parameter \"$1\"" 1>&2
  274. exit 1
  275. ;;
  276. esac
  277. shift
  278. for user_name in "$@"; do
  279. test $r -ne 0 && deluser "${user_name}" "${group_r_prefix}${repository_name}"
  280. test $w -ne 0 && deluser "${user_name}" "${group_w_prefix}${repository_name}"
  281. done
  282. }
  283. # Remove all users from access groups
  284. user_del_all()
  285. {
  286. local w=0
  287. local r=0
  288. local repository_name=$1
  289. local user_name=""
  290. shift
  291. case "$1" in
  292. r)
  293. r=1
  294. ;;
  295. w)
  296. w=1
  297. ;;
  298. rw|wr)
  299. r=1
  300. w=1
  301. ;;
  302. *)
  303. echo "Error: Illegal parameter \"$1\"" 1>&2
  304. exit 1
  305. ;;
  306. esac
  307. if test $r -ne 0; then
  308. user_name=`get_group_members "${group_r_prefix}${repository_name}"`
  309. test "x$user_name" != "x" && user_del "$repository_name" "r" $user_name
  310. fi
  311. if test $w -ne 0; then
  312. user_name=`get_group_members "${group_w_prefix}${repository_name}"`
  313. test "x$user_name" != "x" && user_del "$repository_name" "w" $user_name
  314. fi
  315. }
  316. # Find repository VCS
  317. find_repo_vcs()
  318. {
  319. local r=""
  320. local r_root=""
  321. for r in $possible_vcs; do
  322. eval r_root="\$repository_${r}_root"
  323. test "x$r_root" = "x" && continue
  324. test -d "$r_root/$1" || continue
  325. echo "$r"
  326. break
  327. done
  328. return 0
  329. }
  330. # Find repository root path by repository name
  331. find_repo_root()
  332. {
  333. local r=""
  334. local r_root=""
  335. r=`find_repo_vcs "$1"`
  336. eval r_root="\$repository_${r}_root"
  337. echo "$r_root"
  338. }
  339. # Find repository link path by repository name
  340. find_repo_link()
  341. {
  342. local r=""
  343. local r_link=""
  344. r=`find_repo_vcs "$1"`
  345. eval r_link="\$repository_${r}_link"
  346. echo "$r_link"
  347. }
  348. #------------------ MAIN -----------------------------------------
  349. possible_vcs="svn git"
  350. # Defaults for rfa.conf
  351. group_gid_min="3000"
  352. group_w_prefix="vcs-w-"
  353. group_r_prefix="vcs-r-"
  354. repository_root=
  355. repository_link=
  356. default_vcs="git"
  357. # Parse command line options
  358. action="help"
  359. opts_force=""
  360. opts_conf="/etc/repoforge/rfa.conf"
  361. opts_vcs="$default_vcs"
  362. while test "x$1" != "x"; do
  363. option="$1"
  364. case "$option" in
  365. -h|--help)
  366. usage
  367. exit 0
  368. ;;
  369. -f|--force)
  370. opts_force=1
  371. ;;
  372. # Config file
  373. -c)
  374. shift
  375. opts_conf="$1"
  376. ;;
  377. --config=*)
  378. opts_conf=`echo "$option" | sed 's/--config=//'`
  379. ;;
  380. # Choose VCS
  381. -s)
  382. shift
  383. opts_vcs="$1"
  384. ;;
  385. --vcs=*)
  386. opts_vcs=`echo "$option" | sed 's/--vcs=//'`
  387. ;;
  388. --git)
  389. opts_vcs="git"
  390. ;;
  391. --svn)
  392. opts_vcs="svn"
  393. ;;
  394. # Default
  395. *)
  396. action="$option"
  397. shift
  398. break
  399. ;;
  400. esac
  401. shift
  402. done
  403. # Early help message
  404. test "x$action" = "xhelp" && { usage; exit 0; }
  405. # Check options
  406. bad_vcs=1
  407. for v in $possible_vcs; do
  408. test "x$opts_vcs" = "x$v" && { bad_vcs=""; break; }
  409. done
  410. test "x$bad_vcs" = "x" || { echo "Error: Illegal VCS \"$opts_vcs\"" 1>&2; exit 1; }
  411. # Include config file
  412. test -r "$opts_conf" && . $opts_conf
  413. # Compatibility (suppose SVN)
  414. test "x$repository_svn_root" = "x" && repository_svn_root="$repository_root"
  415. test "x$repository_svn_link" = "x" && repository_svn_link="$repository_link"
  416. # Action
  417. case "$action" in
  418. "info")
  419. test $# -lt 1 && { echo "Error: Repository name is expected" 1>&2; exit 1; }
  420. repository_info "$@"
  421. ;;
  422. "add")
  423. test $# -lt 1 && { echo "Error: Repository name is expected" 1>&2; exit 1; }
  424. repository_add "$@"
  425. ;;
  426. "del")
  427. test $# -lt 1 && { echo "Error: Repository name is expected" 1>&2; exit 1; }
  428. repository_del "$@"
  429. ;;
  430. "fixmod")
  431. test $# -lt 1 && { echo "Error: Repository name is expected" 1>&2; exit 1; }
  432. repository_fixmod "$@"
  433. ;;
  434. "rename")
  435. test $# -lt 2 && { echo "Error: The old and new repository names are expected" 1>&2; exit 1; }
  436. repository_rename "$@" || exit 1
  437. ;;
  438. "adduser"|"useradd")
  439. test $# -lt 3 && { echo "Error: Not enough parameters" 1>&2; exit 1; }
  440. user_add "$@"
  441. ;;
  442. "deluser"|"userdel")
  443. test $# -lt 3 && { echo "Error: Not enough parameters" 1>&2; exit 1; }
  444. user_del "$@"
  445. ;;
  446. "deluser-all"|"userdel-all")
  447. test $# -lt 2 && { echo "Error: Not enough parameters" 1>&2; exit 1; }
  448. user_del_all "$@" || exit 1
  449. ;;
  450. *)
  451. echo "Error: Unknown command" 1>&2
  452. exit 1
  453. ;;
  454. esac
  455. exit $?