add-tor 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/ruby
  2. # add-tor - Add a tor fingerprint line to the approved-routers file
  3. #
  4. # Tor's approved-routers file is expected to be versioned using RCS.
  5. # This script checks for uncommitted changes, does a checkout of the
  6. # file, adds the new fingerprint with a comment stating the server's
  7. # operator, and commits the file to RCS again (using -u so that the
  8. # working copy is not removed.
  9. #
  10. # Operator and fingerprint line are read from stdin.
  11. #
  12. # Before adding a fingerprint line, approved-routers is checked for
  13. # rough syntactical correctness. This script also checks that the
  14. # nickname and fingerprint to be added do not already exist in the
  15. # binding list.
  16. # Copyright (c) 2006 by Peter Palfrader
  17. #
  18. # Permission is hereby granted, free of charge, to any person obtaining
  19. # a copy of this software and associated documentation files (the
  20. # "Software"), to deal in the Software without restriction, including
  21. # without limitation the rights to use, copy, modify, merge, publish,
  22. # distribute, sublicense, and/or sell copies of the Software, and to
  23. # permit persons to whom the Software is furnished to do so, subject to
  24. # the following conditions:
  25. #
  26. # The above copyright notice and this permission notice shall be
  27. # included in all copies or substantial portions of the Software.
  28. #
  29. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  30. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  32. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  33. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  34. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  35. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  36. BINDING = '/etc/tor/approved-routers'
  37. def mysys(cmd)
  38. unless system(cmd)
  39. STDERR.puts "ERROR: #{cmd} failed"
  40. exit 1
  41. end
  42. end
  43. def check_nick(n)
  44. n =~ /^[a-zA-Z0-9]+$/
  45. end
  46. def check_fpr(fpr)
  47. fpr =~ /^([0-9A-F]{4} ){9}[0-9A-F]{4}$/
  48. end
  49. def parse_fprline(fprline)
  50. n = fprline[0 ... fprline.index(' ')]
  51. f = fprline[fprline.index(' ') + 1 .. -1 ]
  52. unless check_nick(n) and check_fpr(f)
  53. STDERR.puts "Invalid fpr syntax '#{fprline}'"
  54. exit 1
  55. end
  56. [n, f]
  57. end
  58. unless system("rcsdiff -q -u #{BINDING}")
  59. STDERR.puts "Uncommitted changes in #{BINDING}. Aborting."
  60. exit 1
  61. end
  62. puts "Checking out #{BINDING}..."
  63. mysys("co -l #{BINDING}")
  64. print "Operator: "
  65. @operator = readline.chop
  66. unless @operator.index('@')
  67. STDERR.puts "ERROR: No @ found"
  68. exit 1
  69. end
  70. print "FPR Line: "
  71. @fprline = readline.chop
  72. (@nickname, @fpr) = parse_fprline(@fprline)
  73. binding = File.new(BINDING, "r+")
  74. binding.readlines.each do |line|
  75. line.chop!
  76. next if line[0..0] == "#"
  77. (n,f) = parse_fprline(line)
  78. if (n == @nickname)
  79. STDERR.puts
  80. STDERR.puts "ERROR: Nickname #{n} already exists in #{BINDING} (fpr: #{f})"
  81. exit 1
  82. end
  83. if (f == @fpr)
  84. STDERR.puts
  85. STDERR.puts "ERROR: Fpr #{f} already exists in #{BINDING} (nickname: #{n})"
  86. exit 1
  87. end
  88. end
  89. puts
  90. puts '| # ' + @operator
  91. puts '| ' + @fprline
  92. puts
  93. binding.puts '# '+@operator
  94. binding.puts @fprline
  95. binding.close
  96. puts "Committing #{BINDING}..."
  97. mysys("ci -u -m'Add #{@nickname}' #{BINDING}")