# WEBrick::Utils (Module)

In: | webrick/utils.rb webrick/ssl.rb

## Methods

create_listeners create_self_signed_cert getservername random_string set_close_on_exec su

## Constants

RAND_CHARS | = | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789" + "abcdefghijklmnopqrstuvwxyz"

## Public Instance methods

set_close_on_exec(io)

[Source]

    # File webrick/utils.rb, line 22
        def set_close_on_exec(io)
          if defined?(Fcntl::FD_CLOEXEC)
            io.fcntl(Fcntl::FD_CLOEXEC, 1)
          end
        end

su(user)

[Source]

    # File webrick/utils.rb, line 29
        def su(user)
          if defined?(Etc)
            pw = Etc.getpwnam(user)
            Process::initgroups(user, pw.gid)
            Process::Sys::setgid(pw.gid)
            Process::Sys::setuid(pw.uid)
          else
            warn("WEBrick::Utils::su doesn't work on this platform")
          end
        end

getservername()

[Source]

    # File webrick/utils.rb, line 41
        def getservername
          host = Socket::gethostname
          begin
            Socket::gethostbyname(host)[0]
          rescue
            host
          end
        end

create_listeners(address, port, logger=nil)

[Source]

    # File webrick/utils.rb, line 51
        def create_listeners(address, port, logger=nil)
          res = Socket::getaddrinfo(address, port,
                                    Socket::AF_UNSPEC,   # address family

                                    Socket::SOCK_STREAM, # socket type

                                    0,                   # protocol

                                    Socket::AI_PASSIVE)  # flag

          last_error = nil
          sockets = []
          res.each{|ai|
            begin
              logger.debug("TCPServer.new(#{ai[3]}, #{ai[1]})") if logger
              sock = TCPServer.new(ai[3], ai[1])
              Utils::set_close_on_exec(sock)
              sockets << sock
            rescue => ex
              logger.warn("TCPServer Error: #{ex}") if logger
              last_error  = ex
            end
          }
          raise last_error if sockets.empty?
          return sockets
        end

random_string(len)

[Source]

    # File webrick/utils.rb, line 79
        def random_string(len)
          rand_max = RAND_CHARS.size
          ret = ""
          len.times{ ret << RAND_CHARS[rand(rand_max)] }
          ret
        end

create_self_signed_cert(bits, cn, comment)

[Source]

    # File webrick/ssl.rb, line 39
        def create_self_signed_cert(bits, cn, comment)
          rsa = OpenSSL::PKey::RSA.new(bits){|p, n|
            case p
            when 0; $stderr.putc "."  # BN_generate_prime

            when 1; $stderr.putc "+"  # BN_generate_prime

            when 2; $stderr.putc "*"  # searching good prime,

                                      # n = #of try,

                                      # but also data from BN_generate_prime

            when 3; $stderr.putc "\n" # found good prime, n==0 - p, n==1 - q,

                                      # but also data from BN_generate_prime

            else;   $stderr.putc "*"  # BN_generate_prime

            end
          }
          cert = OpenSSL::X509::Certificate.new
          cert.version = 3
          cert.serial = 0
          name = OpenSSL::X509::Name.new(cn)
          cert.subject = name
          cert.issuer = name
          cert.not_before = Time.now
          cert.not_after = Time.now + (365*24*60*60)
          cert.public_key = rsa.public_key

          ef = OpenSSL::X509::ExtensionFactory.new(nil,cert)
          ef.issuer_certificate = cert
          cert.extensions = [
            ef.create_extension("basicConstraints","CA:FALSE"),
            ef.create_extension("keyUsage", "keyEncipherment"),
            ef.create_extension("subjectKeyIdentifier", "hash"),
            ef.create_extension("extendedKeyUsage", "serverAuth"),
            ef.create_extension("nsComment", comment),
          ]
          aki = ef.create_extension("authorityKeyIdentifier",
                                    "keyid:always,issuer:always")
          cert.add_extension(aki)
          cert.sign(rsa, OpenSSL::Digest::SHA1.new)

          return [ cert, rsa ]
        end

[Validate]