Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/smanonr..../usr/share/ruby/openssl
File: config.rb
# frozen_string_literal: false
[0] Fix | Delete
=begin
[1] Fix | Delete
= Ruby-space definitions that completes C-space funcs for Config
[2] Fix | Delete
[3] Fix | Delete
= Info
[4] Fix | Delete
Copyright (C) 2010 Hiroshi Nakamura <nahi@ruby-lang.org>
[5] Fix | Delete
[6] Fix | Delete
= Licence
[7] Fix | Delete
This program is licensed under the same licence as Ruby.
[8] Fix | Delete
(See the file 'LICENCE'.)
[9] Fix | Delete
[10] Fix | Delete
=end
[11] Fix | Delete
[12] Fix | Delete
require 'stringio'
[13] Fix | Delete
[14] Fix | Delete
module OpenSSL
[15] Fix | Delete
##
[16] Fix | Delete
# = OpenSSL::Config
[17] Fix | Delete
#
[18] Fix | Delete
# Configuration for the openssl library.
[19] Fix | Delete
#
[20] Fix | Delete
# Many system's installation of openssl library will depend on your system
[21] Fix | Delete
# configuration. See the value of OpenSSL::Config::DEFAULT_CONFIG_FILE for
[22] Fix | Delete
# the location of the file for your host.
[23] Fix | Delete
#
[24] Fix | Delete
# See also http://www.openssl.org/docs/apps/config.html
[25] Fix | Delete
class Config
[26] Fix | Delete
include Enumerable
[27] Fix | Delete
[28] Fix | Delete
class << self
[29] Fix | Delete
[30] Fix | Delete
##
[31] Fix | Delete
# Parses a given _string_ as a blob that contains configuration for
[32] Fix | Delete
# OpenSSL.
[33] Fix | Delete
#
[34] Fix | Delete
# If the source of the IO is a file, then consider using #parse_config.
[35] Fix | Delete
def parse(string)
[36] Fix | Delete
c = new()
[37] Fix | Delete
parse_config(StringIO.new(string)).each do |section, hash|
[38] Fix | Delete
c[section] = hash
[39] Fix | Delete
end
[40] Fix | Delete
c
[41] Fix | Delete
end
[42] Fix | Delete
[43] Fix | Delete
##
[44] Fix | Delete
# load is an alias to ::new
[45] Fix | Delete
alias load new
[46] Fix | Delete
[47] Fix | Delete
##
[48] Fix | Delete
# Parses the configuration data read from _io_, see also #parse.
[49] Fix | Delete
#
[50] Fix | Delete
# Raises a ConfigError on invalid configuration data.
[51] Fix | Delete
def parse_config(io)
[52] Fix | Delete
begin
[53] Fix | Delete
parse_config_lines(io)
[54] Fix | Delete
rescue ConfigError => e
[55] Fix | Delete
e.message.replace("error in line #{io.lineno}: " + e.message)
[56] Fix | Delete
raise
[57] Fix | Delete
end
[58] Fix | Delete
end
[59] Fix | Delete
[60] Fix | Delete
def get_key_string(data, section, key) # :nodoc:
[61] Fix | Delete
if v = data[section] && data[section][key]
[62] Fix | Delete
return v
[63] Fix | Delete
elsif section == 'ENV'
[64] Fix | Delete
if v = ENV[key]
[65] Fix | Delete
return v
[66] Fix | Delete
end
[67] Fix | Delete
end
[68] Fix | Delete
if v = data['default'] && data['default'][key]
[69] Fix | Delete
return v
[70] Fix | Delete
end
[71] Fix | Delete
end
[72] Fix | Delete
[73] Fix | Delete
private
[74] Fix | Delete
[75] Fix | Delete
def parse_config_lines(io)
[76] Fix | Delete
section = 'default'
[77] Fix | Delete
data = {section => {}}
[78] Fix | Delete
io_stack = [io]
[79] Fix | Delete
while definition = get_definition(io_stack)
[80] Fix | Delete
definition = clear_comments(definition)
[81] Fix | Delete
next if definition.empty?
[82] Fix | Delete
case definition
[83] Fix | Delete
when /\A\[/
[84] Fix | Delete
if /\[([^\]]*)\]/ =~ definition
[85] Fix | Delete
section = $1.strip
[86] Fix | Delete
data[section] ||= {}
[87] Fix | Delete
else
[88] Fix | Delete
raise ConfigError, "missing close square bracket"
[89] Fix | Delete
end
[90] Fix | Delete
when /\A\.include (.+)\z/
[91] Fix | Delete
path = $1
[92] Fix | Delete
if File.directory?(path)
[93] Fix | Delete
files = Dir.glob(File.join(path, "*.{cnf,conf}"), File::FNM_EXTGLOB)
[94] Fix | Delete
else
[95] Fix | Delete
files = [path]
[96] Fix | Delete
end
[97] Fix | Delete
[98] Fix | Delete
files.each do |filename|
[99] Fix | Delete
begin
[100] Fix | Delete
io_stack << StringIO.new(File.read(filename))
[101] Fix | Delete
rescue
[102] Fix | Delete
raise ConfigError, "could not include file '%s'" % filename
[103] Fix | Delete
end
[104] Fix | Delete
end
[105] Fix | Delete
when /\A([^:\s]*)(?:::([^:\s]*))?\s*=(.*)\z/
[106] Fix | Delete
if $2
[107] Fix | Delete
section = $1
[108] Fix | Delete
key = $2
[109] Fix | Delete
else
[110] Fix | Delete
key = $1
[111] Fix | Delete
end
[112] Fix | Delete
value = unescape_value(data, section, $3)
[113] Fix | Delete
(data[section] ||= {})[key] = value.strip
[114] Fix | Delete
else
[115] Fix | Delete
raise ConfigError, "missing equal sign"
[116] Fix | Delete
end
[117] Fix | Delete
end
[118] Fix | Delete
data
[119] Fix | Delete
end
[120] Fix | Delete
[121] Fix | Delete
# escape with backslash
[122] Fix | Delete
QUOTE_REGEXP_SQ = /\A([^'\\]*(?:\\.[^'\\]*)*)'/
[123] Fix | Delete
# escape with backslash and doubled dq
[124] Fix | Delete
QUOTE_REGEXP_DQ = /\A([^"\\]*(?:""[^"\\]*|\\.[^"\\]*)*)"/
[125] Fix | Delete
# escaped char map
[126] Fix | Delete
ESCAPE_MAP = {
[127] Fix | Delete
"r" => "\r",
[128] Fix | Delete
"n" => "\n",
[129] Fix | Delete
"b" => "\b",
[130] Fix | Delete
"t" => "\t",
[131] Fix | Delete
}
[132] Fix | Delete
[133] Fix | Delete
def unescape_value(data, section, value)
[134] Fix | Delete
scanned = []
[135] Fix | Delete
while m = value.match(/['"\\$]/)
[136] Fix | Delete
scanned << m.pre_match
[137] Fix | Delete
c = m[0]
[138] Fix | Delete
value = m.post_match
[139] Fix | Delete
case c
[140] Fix | Delete
when "'"
[141] Fix | Delete
if m = value.match(QUOTE_REGEXP_SQ)
[142] Fix | Delete
scanned << m[1].gsub(/\\(.)/, '\\1')
[143] Fix | Delete
value = m.post_match
[144] Fix | Delete
else
[145] Fix | Delete
break
[146] Fix | Delete
end
[147] Fix | Delete
when '"'
[148] Fix | Delete
if m = value.match(QUOTE_REGEXP_DQ)
[149] Fix | Delete
scanned << m[1].gsub(/""/, '').gsub(/\\(.)/, '\\1')
[150] Fix | Delete
value = m.post_match
[151] Fix | Delete
else
[152] Fix | Delete
break
[153] Fix | Delete
end
[154] Fix | Delete
when "\\"
[155] Fix | Delete
c = value.slice!(0, 1)
[156] Fix | Delete
scanned << (ESCAPE_MAP[c] || c)
[157] Fix | Delete
when "$"
[158] Fix | Delete
ref, value = extract_reference(value)
[159] Fix | Delete
refsec = section
[160] Fix | Delete
if ref.index('::')
[161] Fix | Delete
refsec, ref = ref.split('::', 2)
[162] Fix | Delete
end
[163] Fix | Delete
if v = get_key_string(data, refsec, ref)
[164] Fix | Delete
scanned << v
[165] Fix | Delete
else
[166] Fix | Delete
raise ConfigError, "variable has no value"
[167] Fix | Delete
end
[168] Fix | Delete
else
[169] Fix | Delete
raise 'must not reaced'
[170] Fix | Delete
end
[171] Fix | Delete
end
[172] Fix | Delete
scanned << value
[173] Fix | Delete
scanned.join
[174] Fix | Delete
end
[175] Fix | Delete
[176] Fix | Delete
def extract_reference(value)
[177] Fix | Delete
rest = ''
[178] Fix | Delete
if m = value.match(/\(([^)]*)\)|\{([^}]*)\}/)
[179] Fix | Delete
value = m[1] || m[2]
[180] Fix | Delete
rest = m.post_match
[181] Fix | Delete
elsif [?(, ?{].include?(value[0])
[182] Fix | Delete
raise ConfigError, "no close brace"
[183] Fix | Delete
end
[184] Fix | Delete
if m = value.match(/[a-zA-Z0-9_]*(?:::[a-zA-Z0-9_]*)?/)
[185] Fix | Delete
return m[0], m.post_match + rest
[186] Fix | Delete
else
[187] Fix | Delete
raise
[188] Fix | Delete
end
[189] Fix | Delete
end
[190] Fix | Delete
[191] Fix | Delete
def clear_comments(line)
[192] Fix | Delete
# FCOMMENT
[193] Fix | Delete
if m = line.match(/\A([\t\n\f ]*);.*\z/)
[194] Fix | Delete
return m[1]
[195] Fix | Delete
end
[196] Fix | Delete
# COMMENT
[197] Fix | Delete
scanned = []
[198] Fix | Delete
while m = line.match(/[#'"\\]/)
[199] Fix | Delete
scanned << m.pre_match
[200] Fix | Delete
c = m[0]
[201] Fix | Delete
line = m.post_match
[202] Fix | Delete
case c
[203] Fix | Delete
when '#'
[204] Fix | Delete
line = nil
[205] Fix | Delete
break
[206] Fix | Delete
when "'", '"'
[207] Fix | Delete
regexp = (c == "'") ? QUOTE_REGEXP_SQ : QUOTE_REGEXP_DQ
[208] Fix | Delete
scanned << c
[209] Fix | Delete
if m = line.match(regexp)
[210] Fix | Delete
scanned << m[0]
[211] Fix | Delete
line = m.post_match
[212] Fix | Delete
else
[213] Fix | Delete
scanned << line
[214] Fix | Delete
line = nil
[215] Fix | Delete
break
[216] Fix | Delete
end
[217] Fix | Delete
when "\\"
[218] Fix | Delete
scanned << c
[219] Fix | Delete
scanned << line.slice!(0, 1)
[220] Fix | Delete
else
[221] Fix | Delete
raise 'must not reaced'
[222] Fix | Delete
end
[223] Fix | Delete
end
[224] Fix | Delete
scanned << line
[225] Fix | Delete
scanned.join
[226] Fix | Delete
end
[227] Fix | Delete
[228] Fix | Delete
def get_definition(io_stack)
[229] Fix | Delete
if line = get_line(io_stack)
[230] Fix | Delete
while /[^\\]\\\z/ =~ line
[231] Fix | Delete
if extra = get_line(io_stack)
[232] Fix | Delete
line += extra
[233] Fix | Delete
else
[234] Fix | Delete
break
[235] Fix | Delete
end
[236] Fix | Delete
end
[237] Fix | Delete
return line.strip
[238] Fix | Delete
end
[239] Fix | Delete
end
[240] Fix | Delete
[241] Fix | Delete
def get_line(io_stack)
[242] Fix | Delete
while io = io_stack.last
[243] Fix | Delete
if line = io.gets
[244] Fix | Delete
return line.gsub(/[\r\n]*/, '')
[245] Fix | Delete
end
[246] Fix | Delete
io_stack.pop
[247] Fix | Delete
end
[248] Fix | Delete
end
[249] Fix | Delete
end
[250] Fix | Delete
[251] Fix | Delete
##
[252] Fix | Delete
# Creates an instance of OpenSSL's configuration class.
[253] Fix | Delete
#
[254] Fix | Delete
# This can be used in contexts like OpenSSL::X509::ExtensionFactory.config=
[255] Fix | Delete
#
[256] Fix | Delete
# If the optional _filename_ parameter is provided, then it is read in and
[257] Fix | Delete
# parsed via #parse_config.
[258] Fix | Delete
#
[259] Fix | Delete
# This can raise IO exceptions based on the access, or availability of the
[260] Fix | Delete
# file. A ConfigError exception may be raised depending on the validity of
[261] Fix | Delete
# the data being configured.
[262] Fix | Delete
#
[263] Fix | Delete
def initialize(filename = nil)
[264] Fix | Delete
@data = {}
[265] Fix | Delete
if filename
[266] Fix | Delete
File.open(filename.to_s) do |file|
[267] Fix | Delete
Config.parse_config(file).each do |section, hash|
[268] Fix | Delete
self[section] = hash
[269] Fix | Delete
end
[270] Fix | Delete
end
[271] Fix | Delete
end
[272] Fix | Delete
end
[273] Fix | Delete
[274] Fix | Delete
##
[275] Fix | Delete
# Gets the value of _key_ from the given _section_
[276] Fix | Delete
#
[277] Fix | Delete
# Given the following configurating file being loaded:
[278] Fix | Delete
#
[279] Fix | Delete
# config = OpenSSL::Config.load('foo.cnf')
[280] Fix | Delete
# #=> #<OpenSSL::Config sections=["default"]>
[281] Fix | Delete
# puts config.to_s
[282] Fix | Delete
# #=> [ default ]
[283] Fix | Delete
# # foo=bar
[284] Fix | Delete
#
[285] Fix | Delete
# You can get a specific value from the config if you know the _section_
[286] Fix | Delete
# and _key_ like so:
[287] Fix | Delete
#
[288] Fix | Delete
# config.get_value('default','foo')
[289] Fix | Delete
# #=> "bar"
[290] Fix | Delete
#
[291] Fix | Delete
def get_value(section, key)
[292] Fix | Delete
if section.nil?
[293] Fix | Delete
raise TypeError.new('nil not allowed')
[294] Fix | Delete
end
[295] Fix | Delete
section = 'default' if section.empty?
[296] Fix | Delete
get_key_string(section, key)
[297] Fix | Delete
end
[298] Fix | Delete
[299] Fix | Delete
##
[300] Fix | Delete
#
[301] Fix | Delete
# *Deprecated*
[302] Fix | Delete
#
[303] Fix | Delete
# Use #get_value instead
[304] Fix | Delete
def value(arg1, arg2 = nil) # :nodoc:
[305] Fix | Delete
warn('Config#value is deprecated; use Config#get_value')
[306] Fix | Delete
if arg2.nil?
[307] Fix | Delete
section, key = 'default', arg1
[308] Fix | Delete
else
[309] Fix | Delete
section, key = arg1, arg2
[310] Fix | Delete
end
[311] Fix | Delete
section ||= 'default'
[312] Fix | Delete
section = 'default' if section.empty?
[313] Fix | Delete
get_key_string(section, key)
[314] Fix | Delete
end
[315] Fix | Delete
[316] Fix | Delete
##
[317] Fix | Delete
# Set the target _key_ with a given _value_ under a specific _section_.
[318] Fix | Delete
#
[319] Fix | Delete
# Given the following configurating file being loaded:
[320] Fix | Delete
#
[321] Fix | Delete
# config = OpenSSL::Config.load('foo.cnf')
[322] Fix | Delete
# #=> #<OpenSSL::Config sections=["default"]>
[323] Fix | Delete
# puts config.to_s
[324] Fix | Delete
# #=> [ default ]
[325] Fix | Delete
# # foo=bar
[326] Fix | Delete
#
[327] Fix | Delete
# You can set the value of _foo_ under the _default_ section to a new
[328] Fix | Delete
# value:
[329] Fix | Delete
#
[330] Fix | Delete
# config.add_value('default', 'foo', 'buzz')
[331] Fix | Delete
# #=> "buzz"
[332] Fix | Delete
# puts config.to_s
[333] Fix | Delete
# #=> [ default ]
[334] Fix | Delete
# # foo=buzz
[335] Fix | Delete
#
[336] Fix | Delete
def add_value(section, key, value)
[337] Fix | Delete
check_modify
[338] Fix | Delete
(@data[section] ||= {})[key] = value
[339] Fix | Delete
end
[340] Fix | Delete
[341] Fix | Delete
##
[342] Fix | Delete
# Get a specific _section_ from the current configuration
[343] Fix | Delete
#
[344] Fix | Delete
# Given the following configurating file being loaded:
[345] Fix | Delete
#
[346] Fix | Delete
# config = OpenSSL::Config.load('foo.cnf')
[347] Fix | Delete
# #=> #<OpenSSL::Config sections=["default"]>
[348] Fix | Delete
# puts config.to_s
[349] Fix | Delete
# #=> [ default ]
[350] Fix | Delete
# # foo=bar
[351] Fix | Delete
#
[352] Fix | Delete
# You can get a hash of the specific section like so:
[353] Fix | Delete
#
[354] Fix | Delete
# config['default']
[355] Fix | Delete
# #=> {"foo"=>"bar"}
[356] Fix | Delete
#
[357] Fix | Delete
def [](section)
[358] Fix | Delete
@data[section] || {}
[359] Fix | Delete
end
[360] Fix | Delete
[361] Fix | Delete
##
[362] Fix | Delete
# Deprecated
[363] Fix | Delete
#
[364] Fix | Delete
# Use #[] instead
[365] Fix | Delete
def section(name) # :nodoc:
[366] Fix | Delete
warn('Config#section is deprecated; use Config#[]')
[367] Fix | Delete
@data[name] || {}
[368] Fix | Delete
end
[369] Fix | Delete
[370] Fix | Delete
##
[371] Fix | Delete
# Sets a specific _section_ name with a Hash _pairs_.
[372] Fix | Delete
#
[373] Fix | Delete
# Given the following configuration being created:
[374] Fix | Delete
#
[375] Fix | Delete
# config = OpenSSL::Config.new
[376] Fix | Delete
# #=> #<OpenSSL::Config sections=[]>
[377] Fix | Delete
# config['default'] = {"foo"=>"bar","baz"=>"buz"}
[378] Fix | Delete
# #=> {"foo"=>"bar", "baz"=>"buz"}
[379] Fix | Delete
# puts config.to_s
[380] Fix | Delete
# #=> [ default ]
[381] Fix | Delete
# # foo=bar
[382] Fix | Delete
# # baz=buz
[383] Fix | Delete
#
[384] Fix | Delete
# It's important to note that this will essentially merge any of the keys
[385] Fix | Delete
# in _pairs_ with the existing _section_. For example:
[386] Fix | Delete
#
[387] Fix | Delete
# config['default']
[388] Fix | Delete
# #=> {"foo"=>"bar", "baz"=>"buz"}
[389] Fix | Delete
# config['default'] = {"foo" => "changed"}
[390] Fix | Delete
# #=> {"foo"=>"changed"}
[391] Fix | Delete
# config['default']
[392] Fix | Delete
# #=> {"foo"=>"changed", "baz"=>"buz"}
[393] Fix | Delete
#
[394] Fix | Delete
def []=(section, pairs)
[395] Fix | Delete
check_modify
[396] Fix | Delete
@data[section] ||= {}
[397] Fix | Delete
pairs.each do |key, value|
[398] Fix | Delete
self.add_value(section, key, value)
[399] Fix | Delete
end
[400] Fix | Delete
end
[401] Fix | Delete
[402] Fix | Delete
##
[403] Fix | Delete
# Get the names of all sections in the current configuration
[404] Fix | Delete
def sections
[405] Fix | Delete
@data.keys
[406] Fix | Delete
end
[407] Fix | Delete
[408] Fix | Delete
##
[409] Fix | Delete
# Get the parsable form of the current configuration
[410] Fix | Delete
#
[411] Fix | Delete
# Given the following configuration being created:
[412] Fix | Delete
#
[413] Fix | Delete
# config = OpenSSL::Config.new
[414] Fix | Delete
# #=> #<OpenSSL::Config sections=[]>
[415] Fix | Delete
# config['default'] = {"foo"=>"bar","baz"=>"buz"}
[416] Fix | Delete
# #=> {"foo"=>"bar", "baz"=>"buz"}
[417] Fix | Delete
# puts config.to_s
[418] Fix | Delete
# #=> [ default ]
[419] Fix | Delete
# # foo=bar
[420] Fix | Delete
# # baz=buz
[421] Fix | Delete
#
[422] Fix | Delete
# You can parse get the serialized configuration using #to_s and then parse
[423] Fix | Delete
# it later:
[424] Fix | Delete
#
[425] Fix | Delete
# serialized_config = config.to_s
[426] Fix | Delete
# # much later...
[427] Fix | Delete
# new_config = OpenSSL::Config.parse(serialized_config)
[428] Fix | Delete
# #=> #<OpenSSL::Config sections=["default"]>
[429] Fix | Delete
# puts new_config
[430] Fix | Delete
# #=> [ default ]
[431] Fix | Delete
# foo=bar
[432] Fix | Delete
# baz=buz
[433] Fix | Delete
#
[434] Fix | Delete
def to_s
[435] Fix | Delete
ary = []
[436] Fix | Delete
@data.keys.sort.each do |section|
[437] Fix | Delete
ary << "[ #{section} ]\n"
[438] Fix | Delete
@data[section].keys.each do |key|
[439] Fix | Delete
ary << "#{key}=#{@data[section][key]}\n"
[440] Fix | Delete
end
[441] Fix | Delete
ary << "\n"
[442] Fix | Delete
end
[443] Fix | Delete
ary.join
[444] Fix | Delete
end
[445] Fix | Delete
[446] Fix | Delete
##
[447] Fix | Delete
# For a block.
[448] Fix | Delete
#
[449] Fix | Delete
# Receive the section and its pairs for the current configuration.
[450] Fix | Delete
#
[451] Fix | Delete
# config.each do |section, key, value|
[452] Fix | Delete
# # ...
[453] Fix | Delete
# end
[454] Fix | Delete
#
[455] Fix | Delete
def each
[456] Fix | Delete
@data.each do |section, hash|
[457] Fix | Delete
hash.each do |key, value|
[458] Fix | Delete
yield [section, key, value]
[459] Fix | Delete
end
[460] Fix | Delete
end
[461] Fix | Delete
end
[462] Fix | Delete
[463] Fix | Delete
##
[464] Fix | Delete
# String representation of this configuration object, including the class
[465] Fix | Delete
# name and its sections.
[466] Fix | Delete
def inspect
[467] Fix | Delete
"#<#{self.class.name} sections=#{sections.inspect}>"
[468] Fix | Delete
end
[469] Fix | Delete
[470] Fix | Delete
protected
[471] Fix | Delete
[472] Fix | Delete
def data # :nodoc:
[473] Fix | Delete
@data
[474] Fix | Delete
end
[475] Fix | Delete
[476] Fix | Delete
private
[477] Fix | Delete
[478] Fix | Delete
def initialize_copy(other)
[479] Fix | Delete
@data = other.data.dup
[480] Fix | Delete
end
[481] Fix | Delete
[482] Fix | Delete
def check_modify
[483] Fix | Delete
raise TypeError.new("Insecure: can't modify OpenSSL config") if frozen?
[484] Fix | Delete
end
[485] Fix | Delete
[486] Fix | Delete
def get_key_string(section, key)
[487] Fix | Delete
Config.get_key_string(@data, section, key)
[488] Fix | Delete
end
[489] Fix | Delete
end
[490] Fix | Delete
end
[491] Fix | Delete
[492] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function