123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- BINDING = '/etc/tor/approved-routers'
- def mysys(cmd)
- unless system(cmd)
- STDERR.puts "ERROR: #{cmd} failed"
- exit 1
- end
- end
- def check_nick(n)
- n =~ /^[a-zA-Z0-9]+$/
- end
- def check_fpr(fpr)
- fpr =~ /^([0-9A-F]{4} ){9}[0-9A-F]{4}$/
- end
- def parse_fprline(fprline)
- n = fprline[0 ... fprline.index(' ')]
- f = fprline[fprline.index(' ') + 1 .. -1 ]
- unless check_nick(n) and check_fpr(f)
- STDERR.puts "Invalid fpr syntax '#{fprline}'"
- exit 1
- end
- [n, f]
- end
- unless system("rcsdiff -q -u #{BINDING}")
- STDERR.puts "Uncommitted changes in #{BINDING}. Aborting."
- exit 1
- end
- puts "Checking out #{BINDING}..."
- mysys("co -l #{BINDING}")
- print "Operator: "
- @operator = readline.chop
- unless @operator.index('@')
- STDERR.puts "ERROR: No @ found"
- exit 1
- end
- print "FPR Line: "
- @fprline = readline.chop
- (@nickname, @fpr) = parse_fprline(@fprline)
- binding = File.new(BINDING, "r+")
- binding.readlines.each do |line|
- line.chop!
- next if line[0..0] == "#"
- (n,f) = parse_fprline(line)
- if (n == @nickname)
- STDERR.puts
- STDERR.puts "ERROR: Nickname #{n} already exists in #{BINDING} (fpr: #{f})"
- exit 1
- end
- if (f == @fpr)
- STDERR.puts
- STDERR.puts "ERROR: Fpr #{f} already exists in #{BINDING} (nickname: #{n})"
- exit 1
- end
- end
- puts
- puts '| # ' + @operator
- puts '| ' + @fprline
- puts
- binding.puts '# '+@operator
- binding.puts @fprline
- binding.close
- puts "Committing #{BINDING}..."
- mysys("ci -u -m'Add #{@nickname}' #{BINDING}")
|