PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/xauth_switch_to_sun-des-1

#
Korn Shell | 172 lines | 80 code | 26 blank | 66 comment | 13 complexity | 0ac419111331a7ad6bcf8fb370cb845f MD5 | raw file
  1. #!/bin/ksh
  2. # X11 MIT-MAGIC-COOKIE to SUN-DES-1 auth.
  3. # this script switched the current Xservers authentification
  4. # (usually MIT-MAGIC-COOKIE-1) to SUN-DES-1.
  5. #
  6. #
  7. # Copyright 2002-2004 by Roland Mainz <roland.mainz@nrubsig.org>.
  8. #
  9. #
  10. # Requirements:
  11. # - Solaris/Linux/AIX running as NIS+ client (YP/LDAP not supported yet)
  12. # - user must have proper credentials ("SecureRPC")
  13. # - script must be able to "guess" the UID of the Xserver
  14. #
  15. # Advantages:
  16. # - User may allow other users to gain access via
  17. # % xhost +jigsaw@
  18. # instead of moving 128bit cookies
  19. #
  20. # Known bugs:
  21. # - Was not tested on Linux since several months
  22. umask 077
  23. # force POSIX binaries
  24. export PATH=/usr/xpg4/bin:/usr/bin:/usr/dt/bin:/usr/openwin/bin
  25. # debug
  26. alias xxdebug=true
  27. # alias xxdebug=
  28. # get full qualified domain name
  29. getFQDN()
  30. {
  31. getent hosts ${1} | awk "{print \$2}" -
  32. }
  33. user2netname()
  34. {
  35. UID=$(id -u $1)
  36. DOMAINNAME=$(domainname)
  37. if [ $UID != 0 ] ; then
  38. netname=unix.$UID@$DOMAINNAME
  39. else
  40. netname=unix.$HOSTNAME@$DOMAINNAME
  41. fi
  42. # BUG: SecureRPC isn't limited to NIS+
  43. # (but there is no "getent publickey ...") ...
  44. # ToDo:
  45. # - YP name is "publickey.byname"
  46. # - What name does LDAP use ?
  47. if [ "`nismatch "auth_name=$netname" cred.org_dir`" != "" ] ; then
  48. echo "$netname"
  49. else
  50. echo "user ${UID} has no entry in cred.org_dir" >&2
  51. return 1
  52. fi
  53. return 0
  54. }
  55. # pid to username
  56. getUserOfPID()
  57. {
  58. ps -p $1 -o user,pid | awk "NR != 1 {print \$1}" -
  59. }
  60. # test if we can access $DISPLAY via SUN-DES-1 auth. using a temporary
  61. # Xauthority file
  62. dry_run()
  63. {
  64. (
  65. principal="$1"
  66. # XAUTHORITY may not be defined
  67. if [ "$XAUTHORITY" = "" ] ; then
  68. export XAUTHORITY=~/.Xauthority
  69. fi
  70. ORIGINAL_XAUTHORITY="${XAUTHORITY:-~/.Xauthority}"
  71. TMP_XAUTHORITY=/tmp/mit-cookie2sun-des-1tmpxauth_${LOGNAME}_${RANDOM}.xauth
  72. export XAUTHORITY="$TMP_XAUTHORITY"
  73. touch "$XAUTHORITY"
  74. (echo "add $displayhost/unix:$displaynum SUN-DES-1 $principal" ;
  75. echo "add $displayhost:$displaynum SUN-DES-1 $principal"
  76. ) | xauth source -
  77. # check if a sample X11 app. (/usr/openwin/bin/xset) can access Xserver...
  78. if ! xset q 2>/dev/null 1>/dev/null ; then
  79. # clean-up
  80. rm -f "$TMP_XAUTHORITY"
  81. return 1
  82. fi
  83. rm -f "$TMP_XAUTHORITY"
  84. return 0
  85. )
  86. }
  87. # main
  88. HOSTNAME=$(hostname)
  89. FQDN=$(getFQDN $HOSTNAME)
  90. # be sure that DISPLAY contains the host name
  91. # BUGs:
  92. # - this does _not_ catch non-tcp connections (like DECnet).
  93. # - this may not work with IPv6 addresses
  94. displayhost=${DISPLAY%:*}
  95. displaynum=$(x=${DISPLAY#*:}; echo ${x%.*})
  96. if [ "$displayhost" == "" -o "$displayhost" == "localhost" ] ; then
  97. # fix DISPLAY
  98. export DISPLAY="${FQDN}:${DISPLAY#*:}"
  99. displayhost=${DISPLAY%:*}
  100. fi
  101. # grant access for current user and for user root
  102. # (a bug in /usr/dt/bin/dtaction requires this for user "root", too -
  103. # Solaris 7/8 dtaction runns setuid root and opens a display connection
  104. # before chaning the EUID to the "destination uid"... ;-( ).
  105. xhost +${LOGNAME}@ +$(user2netname root)
  106. # get X server principal(=user)
  107. # this may fail if user isn't local
  108. # unfortunately we cannot get the Xserver PID with a simply API - we
  109. # have to "guess" in this case. "pgrep" creates a list of PIDs which may
  110. # match. Then we create a list of all matching "principals" and test
  111. # them - item by item...
  112. # ... step 1: Create list of principals
  113. principal_list="" # you can add "most common" principals here...
  114. fallback_principal_list="" # you can add "fallback" principals here
  115. # (for example, principals for Xterminals (where
  116. # the Xserver always runns under the same UID)
  117. # which use SUN-DES-1)
  118. for i in $(pgrep -f ".*X.* :$displaynum*") ; do
  119. principal_list="$(user2netname `getUserOfPID $i`) ${principal_list}"
  120. done
  121. xxdebug echo "principal_list=${principal_list}"
  122. # ... step 2: Test the list of principals
  123. for PRINCIPAL in ${principal_list} ${fallback_principal_list} ; do
  124. # make a "dry run" and test whether we really can use SUN-DES-1 auth.
  125. # for this display using the given principal
  126. if dry_run "${PRINCIPAL}" ; then
  127. # remove old MIT-MAGIC-COOKIES and insert SUN-DES-1 cookies
  128. # Users ~/.Xauthority _must_ be changed in _one_ step to avoid
  129. # possible race conditions when switching auth. on a "live"
  130. # $DISPLAY...
  131. (echo "remove $displayhost/unix:$displaynum" ;
  132. echo "remove $displayhost:$displaynum" ;
  133. echo "add $displayhost/unix:$displaynum SUN-DES-1 $PRINCIPAL" ;
  134. echo "add $displayhost:$displaynum SUN-DES-1 $PRINCIPAL"
  135. ) | xauth source -
  136. # success.
  137. xxdebug echo "success."
  138. exit 0
  139. fi
  140. done
  141. echo "${0}: failure; could not establish SUN-DES-1 auth. on $DISPLAY" >&2
  142. xhost -$LOGNAME@ -$(user2netname root)
  143. # failure.
  144. xxdebug echo failure.
  145. exit 1
  146. # EOF.