_iAuthUtility 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #!/bin/sh
  2. ###############################################################################
  3. # Authorization utility for IBM i
  4. #
  5. # Copyright IBM Corp. 2012
  6. # The source code for this program is not published or other-
  7. # wise divested of its trade secrets, irrespective of what has
  8. # been deposited with the U.S. Copyright Office.
  9. #
  10. # ----------------------------------------------------------------------------
  11. #
  12. #
  13. usage() {
  14. echo ""
  15. echo "Usage: $0"
  16. echo " --rolename server"
  17. echo " --userprofilename user_profile_name [options] "
  18. echo ""
  19. echo "Options:"
  20. echo " --userdir wlp_usr_dir"
  21. echo " --outputdir wlp_output_dir"
  22. echo ""
  23. echo ""
  24. echo "You must have *ALLOBJ special permission, own or have *OBJMGT authority"
  25. echo "to all objects in the specified directory subtrees to use the GRANTAUTH"
  26. echo "command."
  27. exit 1
  28. }
  29. ##
  30. ## isMemberNoCase: Case insensitive check for $1 in the list comprising the rest
  31. ## of the args. The result is set in isMemberNoCaseResult.
  32. isMemberNoCase() {
  33. isMemberNoCaseResult=
  34. key=$1
  35. shift
  36. arglist=$@
  37. for string in $arglist
  38. do
  39. if [ ${#string} -eq ${#key} ] && [ `echo "${string}" | grep -i "${key}"` ]; then
  40. isMemberNoCaseResult=true; break
  41. fi
  42. done
  43. }
  44. ##
  45. ## set_wlp_install_dir: set WLP_INSTALL_DIR
  46. set_wlp_install_dir() {
  47. CUR_DIR=`pwd`
  48. WLP_DIR=`dirname ${0}`/../../../../
  49. cd "${WLP_DIR}"
  50. WLP_INSTALL_DIR=`pwd`
  51. cd "${CUR_DIR}"
  52. }
  53. ##
  54. ## readServerEnv: Read server.env file and export environment variables.
  55. readServerEnv()
  56. {
  57. if [ -f "$1" ]; then
  58. while read -r line; do
  59. case $line in
  60. \#*);;
  61. *=*)
  62. # Only accept alphanumeric variable names to avoid eval complexities.
  63. if var=`echo "$line" | sed -e 's/^\([a-zA-Z0-9_][a-zA-Z0-9_]*\)=.*/\1/'`; then
  64. value=\'`echo "$line" | sed -e 's/[^=]*=//' -e s/\'/\'\"\'\"\'/g`\'
  65. eval "$var=$value; export $var"
  66. fi
  67. esac
  68. done < "$1"
  69. fi
  70. }
  71. ##
  72. ## installEnvDefaults: Set variable defaults for a non-server or nonexistent
  73. ## server command.
  74. installEnvDefaults()
  75. {
  76. readServerEnv "${WLP_INSTALL_DIR}/etc/default.env"
  77. if [ -z "${WLP_USER_DIR}" ]
  78. then
  79. if [ -z "${WLP_DEFAULT_USER_DIR}" ]
  80. then
  81. WLP_DEFAULT_USER_DIR=${WLP_INSTALL_DIR}/usr
  82. fi
  83. WLP_USER_DIR=${WLP_DEFAULT_USER_DIR}
  84. fi
  85. if [ -z "${WLP_OUTPUT_DIR}" ]
  86. then
  87. if [ -z "${WLP_DEFAULT_OUTPUT_DIR}" ]
  88. then
  89. WLP_DEFAULT_OUTPUT_DIR=${WLP_USER_DIR}/servers
  90. fi
  91. WLP_OUTPUT_DIR=${WLP_DEFAULT_OUTPUT_DIR}
  92. fi
  93. }
  94. ##
  95. ## grantDirAllRX: Grant read only permission to the uid for the user directory
  96. ## $1 is the uid
  97. ## $2 is the directory
  98. grantDirAllRX() {
  99. chmod o= ${2}
  100. if [ $? != 0 ]; then
  101. echo "Error: Failed to grant *PUBLIC *EXCLUDE authority to ${2}."
  102. ERR_CHK=1
  103. else
  104. echo "Granted *PUBLIC *EXCLUDE authority to ${2}."
  105. fi
  106. system "CHGAUT OBJ('${2}') USER(${1}) DTAAUT(*RX) OBJAUT(*NONE) SUBTREE(*ALL)"
  107. if [ $? = 0 ]; then
  108. echo "Granted explicit *RX authority to user ${1} on all objects in directory ${2}."
  109. else
  110. echo "Error: Failed to grant explicit *RX authority to user ${1} on all objects in directory ${2}."
  111. ERR_CHK=1
  112. fi
  113. }
  114. ##
  115. ## grantDirAllRWX: Grant read, write and execute permission to uid for the directory
  116. ## $1 is the uid
  117. ## $2 is the directory
  118. grantDirAllRWX() {
  119. chmod o= ${2}
  120. if [ $? != 0 ]; then
  121. echo "Error: Failed to grant *PUBLIC *EXCLUDE authority to ${2}."
  122. ERR_CHK=1
  123. else
  124. echo "Granted *PUBLIC *EXCLUDE authority to ${2}."
  125. fi
  126. chown -R $1 $2
  127. if [ $? = 0 ]; then
  128. echo "Changed ownership of all objects in directory ${2} to ${1}."
  129. else
  130. echo "Error: Failed to change ownership of all objects in directory ${2} to ${1}."
  131. ERR_CHK=1
  132. fi
  133. # if [ -e ${2}/servers/.classCache ]; then
  134. # rm -R ${2}/servers/.classCache
  135. # fi
  136. }
  137. ##
  138. ## grantServerAuthOutputDirServers: Grant the server role to the uid for output directories
  139. ## configured in servers/*/server.env
  140. ## $1 is the uid
  141. ## $2 is the default user directory
  142. ## $3 is the defalut output directory
  143. grantServerAuthOutputDirServers() {
  144. if [ -d ${2}/servers ]; then
  145. FILENAMES=$(ls ${2}/servers)
  146. for SERVERNAME in $FILENAMES
  147. do
  148. WLP_OUTPUT_DIR=$3
  149. readServerEnv ${2}/servers/${SERVERNAME}/server.env
  150. isMemberNoCase ${WLP_OUTPUT_DIR} ${OUTPUT_DIR_NAMES}
  151. if [ ! ${isMemberNoCaseResult} ]; then
  152. OUTPUT_DIR_NAMES="${OUTPUT_DIR_NAMES} ${WLP_OUTPUT_DIR}"
  153. grantDirAllRWX ${1} ${WLP_OUTPUT_DIR}
  154. fi
  155. done
  156. fi
  157. }
  158. ##
  159. ## grantServerAuthUserDirAll: Grant the server role to the uid for output directories
  160. ## configured in servers/*/server.env and *RX or *RWX to the user directory depending
  161. ## on whether all servers have output directories defined for them.
  162. ## $1 is the uid
  163. ## $2 is the user directory
  164. grantServerAuthUserDirAll() {
  165. WLP_USER_DIR=$2
  166. WLP_OUTPUT_DIR=${2}/servers
  167. grantDirAllRX $1 ${WLP_USER_DIR}
  168. OUTPUT_DIR_NAMES=
  169. grantServerAuthOutputDirServers $1 $2 ${WLP_OUTPUT_DIR}
  170. }
  171. ##
  172. ## grantServerAuthAll: Grant the server role to the uid for the user and output directories
  173. ## belonging to this installation
  174. ## $1 is the uid
  175. grantServerAuthAll() {
  176. readServerEnv ${WLP_INSTALL_DIR}/etc/server.env
  177. installEnvDefaults
  178. grantDirAllRX $1 ${WLP_USER_DIR}
  179. grantDirAllRWX ${1} ${WLP_OUTPUT_DIR}
  180. OUTPUT_DIR_NAMES=${WLP_OUTPUT_DIR}
  181. grantServerAuthOutputDirServers ${1} ${WLP_USER_DIR} ${WLP_OUTPUT_DIR}
  182. }
  183. if [ "`uname`" != "OS400" ]; then
  184. echo "The $0 command is supported only for IBM i."
  185. exit 1
  186. fi
  187. WLPUSERDIR=
  188. WLPOUTPUTDIR=
  189. ROLENAME=
  190. USERPROFILENAME=
  191. while [ $# -gt 0 ]; do
  192. case $1 in
  193. '--userdir') if [ $# -gt 1 ]; then
  194. WLPUSERDIR=$2;shift
  195. fi
  196. ;;
  197. '--outputdir') if [ $# -gt 1 ]; then
  198. WLPOUTPUTDIR=$2;shift
  199. fi
  200. ;;
  201. '--rolename') if [ $# -gt 1 ]; then
  202. ROLENAME=$2;shift
  203. if [ ${ROLENAME} != "server" ]; then
  204. usage
  205. fi
  206. fi
  207. ;;
  208. '--userprofilename') if [ $# -gt 1 ]; then
  209. USERPROFILENAME=$2;shift
  210. fi
  211. ;;
  212. *) usage
  213. ;;
  214. esac
  215. shift
  216. done
  217. if [ ! ${ROLENAME} ] || [ ! ${USERPROFILENAME} ]; then
  218. usage
  219. fi
  220. ERR_CHK=0
  221. set_wlp_install_dir
  222. OUTPUT_DIR_NAMES=
  223. if [ ! ${WLPUSERDIR} ] && [ ! ${WLPOUTPUTDIR} ]; then
  224. grantServerAuthAll ${USERPROFILENAME}
  225. fi
  226. if [ ${WLPUSERDIR} ]; then
  227. grantServerAuthUserDirAll ${USERPROFILENAME} ${WLPUSERDIR}
  228. fi
  229. if [ ${WLPOUTPUTDIR} ]; then
  230. grantDirAllRWX ${USERPROFILENAME} ${WLPOUTPUTDIR}
  231. fi
  232. exit ${ERR_CHK}