postinst 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/bin/sh
  2. echo "============== post installation custom scripts start =============="
  3. curr_timestamp=`date "+%Y%m%d-%H%M%S.%3N"`
  4. sudo_priviledge() {
  5. given_user=$1
  6. given_sudoers_file=$2
  7. if [ -e ${given_sudoers_file}.tmp -o "$(pidof visudo)" ]; then
  8. echo "[ERROR]: ${given_sudoers_file} is working now, wait a little and then try again later"
  9. return 2
  10. fi
  11. ret=0
  12. echo "[INFO]: to process editing sudoers file..."
  13. cp ${given_sudoers_file} ${given_sudoers_file}.tmp
  14. chmod 0640 ${given_sudoers_file}.tmp
  15. cat ${given_sudoers_file}.tmp | grep 'includedir /etc/sudoers.d' > /dev/null
  16. if [ $? = 0 ]; then
  17. echo "[INFO]: '#includedir /etc/sudoers.d' already exists in /etc/sudoers file"
  18. else
  19. echo "#includedir /etc/sudoers.d" >> ${given_sudoers_file}.tmp
  20. ret=1
  21. fi
  22. if [ "`cat /etc/sudoers|grep ${given_user}|grep ALL=(ALL)`" != "" ]; then
  23. echo "[WARN]: sudoers file has been loaded up all(all) config"
  24. else
  25. new_entry="${given_user} ALL=(ALL) ALL"
  26. echo "${new_entry}" >> ${given_sudoers_file}.tmp
  27. ret=1
  28. fi
  29. if [ "`cat /etc/sudoers|grep ${given_user}|grep Run`" != "" ]; then
  30. echo "[WARN]: sudoers file has been loaded up rvc config"
  31. else
  32. new_entry="${given_user} ALL=(ALL) NOPASSWD: /opt/Run/version/*, NOPASSWD: /bin/sh, NOPASSWD: /bin/bash"
  33. echo "${new_entry}" >> ${given_sudoers_file}.tmp
  34. ret=1
  35. fi
  36. chmod 0440 ${given_sudoers_file}.tmp
  37. if visudo -c -f ${given_sudoers_file}.tmp ; then
  38. echo check syntax correct on ${given_sudoers_file}
  39. else
  40. echo "[ERROR]: syntax check failed on file ${given_sudoers_file}"
  41. rm ${given_sudoers_file}.tmp
  42. return 3
  43. fi
  44. if [ $ret -eq 1 ]; then
  45. echo "[SUCCESS]: config file has been change !!"
  46. cp ${given_sudoers_file} ${given_sudoers_file}.backup.${curr_timestamp}
  47. mv ${given_sudoers_file}.tmp ${given_sudoers_file}
  48. else
  49. echo "[WARN]: config file not change"
  50. rm ${given_sudoers_file}.tmp
  51. fi
  52. return $ret
  53. }
  54. res=0
  55. if [ "$1" = "configure" ]; then
  56. rvc_user=$USER
  57. sudoers_file=/etc/sudoers
  58. if [ -z "$rvc_user" -o "$rvc_user" = "root" ]; then
  59. users=$(cat /etc/passwd | awk -F: '$3>=500' | cut -f 1 -d :)
  60. echo "user list: $users"
  61. cnt=0
  62. for var in $(echo ${users} | awk '{split($0,arr,",");for(i in arr) print arr[i]}')
  63. do
  64. if [ ${var} != 'nobody' -a ${var} != 'systemd-coredump' ]; then
  65. cnt=$((${cnt}+1))
  66. rvc_user=${var}
  67. fi
  68. done
  69. if [ $cnt -ne 1 ]; then
  70. echo "too many users: $cnt"
  71. rvc_user=''
  72. else
  73. echo "aim user: $rvc_user"
  74. fi
  75. fi
  76. if [ -z "$rvc_user" ]; then
  77. echo "[ERROR]: username is empty, please use cmd 'dpkg -i {.deb file} to install'"
  78. exit 1
  79. elif [ "$rvc_user" = "root" ]; then
  80. echo "[ERROR]: username is invalid, please use cmd 'dpkg -i {.deb file} to install'"
  81. exit 2
  82. fi
  83. echo "[INFO]: Going to add entry into /etc/sudoers file for user: $rvc_user"
  84. if [ ! -d '/etc/sudoers.d' ]; then
  85. mkidr /etc/sudoers.d
  86. chmod 750 /etc/sudoers.d
  87. fi
  88. sudo_priviledge ${rvc_user} ${sudoers_file}
  89. res=$?
  90. if [ $res -eq 0 ]; then
  91. echo "[INFO]: Copy application icon to menu tools..."
  92. cp /opt/rvc/Resources/RVCTerminalApp.desktop /usr/share/applications
  93. echo "[INFO]: Active application icon autorun after boot..."
  94. cp /opt/rvc/Resources/RVCTerminalApp.desktop /etc/xdg/autostart/
  95. echo "[INFO]: Copy application icon to desktop..."
  96. cp /opt/rvc/Resources/RVCTerminalApp.desktop /home/$rvc_user/Desktop
  97. fi
  98. fi
  99. exit $res