Module: OverSIP::Config

Extended by:
Validators, Logger
Defined in:
lib/oversip/config.rb,
lib/oversip/config_validators.rb

Defined Under Namespace

Modules: Validators

Constant Summary collapse

DEFAULT_CONFIG_DIR =
"/etc/oversip/"
DEFAULT_TLS_DIR =
"tls/"
DEFAULT_TLS_CA_DIR =
"tls/ca/"
DEFAULT_CONFIG_FILE =
"oversip.conf"
PROXIES_FILE =
"proxies.conf"
LOGIC_FILE =
"logic.rb"
WEBSOCKET_POLICY_FILE =
"websocket_policy.rb"
CONFIG_VALIDATIONS =
{
  :core => {
    :nameservers              => [ :ipv4, :multi_value ],
    :syslog_facility          => [
      [ :choices,
        %w{ kern user daemon local0 local1 local2 local3 local4 local5 local6 local7 } ]
    ],
    :syslog_level             => [
      [ :choices,
        %w{ debug info notice warn error crit } ]
    ],
  },
  :sip => {
    :sip_udp                  => :boolean,
    :sip_tcp                  => :boolean,
    :sip_tls                  => :boolean,
    :enable_ipv4              => :boolean,
    :listen_ipv4              => :ipv4,
    :enable_ipv6              => :boolean,
    :listen_ipv6              => :ipv6,
    :listen_port              => :port,
    :listen_port_tls          => :port,
    :use_tls_tunnel           => :boolean,
    :listen_port_tls_tunnel   => :port,
    :local_domains            => [ :domain, :multi_value ],
    :tcp_keepalive_interval        => [ :fixnum, [ :greater_equal_than, 180 ] ],
    :record_route_hostname_tls_ipv4 => :domain,
    :record_route_hostname_tls_ipv6 => :domain,
  },
  :websocket => {
    :sip_ws                   => :boolean,
    :sip_wss                  => :boolean,
    :enable_ipv4              => :boolean,
    :listen_ipv4              => :ipv4,
    :enable_ipv6              => :boolean,
    :listen_ipv6              => :ipv6,
    :listen_port              => :port,
    :listen_port_tls          => :port,
    :use_tls_tunnel           => :boolean,
    :listen_port_tls_tunnel   => :port,
    :max_ws_message_size      => [ :fixnum, [ :minor_than, 1048576 ] ],
    :max_ws_frame_size        => [ :fixnum, [ :minor_than, 1048576 ] ],
    :ws_keepalive_interval    => [ :fixnum, [ :greater_equal_than, 180 ] ]
  },
  :tls => {
    :public_cert              => [ :readable_file, :tls_pem_chain ],
    :private_cert             => [ :readable_file, :tls_pem_private ],
    :ca_dir                   => :readable_dir
  }
}

Constants included from Logger

Logger::SYSLOG_POSIXMQ_MAPPING

Constants included from Validators

Validators::DOMAIN_REGEXP, Validators::TLS_PEM_CHAIN_REGEXP

Class Method Summary collapse

Methods included from Logger

close, fatal, fg_system_msg2str, init_logger_mq, load_methods, log_id, syslog_system_msg2str, syslog_user_msg2str

Methods included from Validators

boolean, choices, domain, fixnum, greater_equal_than, greater_than, ipv4, ipv4_any, ipv6, ipv6_any, minor_equal_than, minor_than, port, readable_dir, readable_file, string, tls_pem_chain, tls_pem_private

Class Method Details

.discover_local_ip(type) ⇒ Object



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/oversip/config.rb', line 496

def self.discover_local_ip(type)
  begin
    if type == :ipv4
      socket = ::UDPSocket.new ::Socket::AF_INET
      socket.connect("1.2.3.4", 1)
    elsif type == :ipv6
      socket = ::UDPSocket.new ::Socket::AF_INET6
      socket.connect("2001::1", 1)
    end
    ip = socket.local_address.ip_address
    socket.close
    return ip
  rescue => e
    log_system_warn "cannot autodiscover local #{type == :ipv4 ? "IPv4" : "IPv6"}: #{e.message} (#{e.class})"
    return false
  end
end

.humanize_value(value) ⇒ Object



483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/oversip/config.rb', line 483

def self.humanize_value value
  case value
    when TrueClass      ; "yes"
    when FalseClass     ; "no"
    when NilClass       ; "null"
    when String         ; value
    when Symbol         ; value.to_s
    when Array          ; value.join(", ")
    when Fixnum, Float  ; value.to_s
    else                ; value.to_s
    end
end

.load(config_dir = nil, config_file = nil) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/oversip/config.rb', line 121

def self.load config_dir=nil, config_file=nil
  @config_dir = (config_dir || DEFAULT_CONFIG_DIR)
  @config_file = ::File.join(@config_dir, config_file || DEFAULT_CONFIG_FILE)
  @proxies_file = ::File.join(@config_dir, PROXIES_FILE)
  @logic_file ||= ::File.join(@config_dir, LOGIC_FILE)
  @websocket_policy_file ||= ::File.join(@config_dir, WEBSOCKET_POLICY_FILE)

  begin
    conf_yaml = ::YAML.load_file @config_file
  rescue => e
    fatal "error loading configuration file '#{@config_file}': #{e.message} (#{e.class})"
  end

  begin
    proxies_yaml = ::YAML.load_file @proxies_file
  rescue => e
    fatal "error loading proxies configuration file '#{@proxies_file}': #{e.message} (#{e.class})"
  end

  begin
    Kernel.load @logic_file
  rescue LoadError => e
    fatal "error loading logic file '#{@logic_file}': #{e.message} (#{e.class})"
  end

  begin
    Kernel.load @websocket_policy_file
  rescue LoadError => e
    log_system_warn "cannot load WebSocket Policy file '#{@websocket_policy_file}': #{e.message} (#{e.class}), using default policy (allow all)"
  end

  begin
    pre_check(conf_yaml)

    CONFIG_VALIDATIONS.each_key do |section|
      CONFIG_VALIDATIONS[section].each do |parameter, validations|
        values = conf_yaml[section.to_s][parameter.to_s] rescue nil
        validations = [ validations ]  unless validations.is_a?(Array)

        if values == nil
          if validations.include? :required
            fatal "#{section}[#{parameter}] requires a value"
          end
          next
        end

        if values.is_a? Array
          unless validations.include? :multi_value
            fatal "#{section}[#{parameter}] does not allow multiple values"
          end

          if validations.include? :non_empty and values.empty?
            fatal "#{section}[#{parameter}] does not allow empty values"
          end
        end

        values = ( values.is_a?(Array) ? values : [ values ] )

        values.each do |value|
          validations.each do |validation|

            if validation.is_a? Symbol
              args = []
            elsif validation.is_a? Array
              args = validation[1..-1]
              validation = validation[0]
            end

            next if [:required, :multi_value, :non_empty].include? validation

            unless send validation, value, *args
              fatal "#{section}[#{parameter}] has invalid value '#{humanize_value value}' (does not satisfy '#{validation}' validation requirement)"
            end
          end

          @configuration[section][parameter] = ( validations.include?(:multi_value) ? values : values[0] )
        end

      end  # CONFIG_VALIDATIONS[section].each
    end  # CONFIG_VALIDATIONS.each_key

    post_process
    post_check

  rescue OverSIP::ConfigurationError => e
    fatal "configuration error: #{e.message}"
  rescue => e
    fatal e
  end

  ::OverSIP.configuration = @configuration
  ::OverSIP::ProxiesConfig.load proxies_yaml
end

.log_idObject



19
20
21
# File 'lib/oversip/config.rb', line 19

def self.log_id
  @log_id ||= "Config"
end

.post_checkObject



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/oversip/config.rb', line 337

def self.post_check
  binds = { :udp => [], :tcp => [] }

  if @configuration[:sip][:enable_ipv4]
    ipv4 = @configuration[:sip][:listen_ipv4]

    if @configuration[:sip][:sip_udp]
      binds[:udp] << [ ipv4, @configuration[:sip][:listen_port] ]
    end

    if @configuration[:sip][:sip_tcp]
      binds[:tcp] << [ ipv4, @configuration[:sip][:listen_port] ]
    end

    if @configuration[:sip][:sip_tls]
      unless @configuration[:sip][:use_tls_tunnel]
        binds[:tcp] << [ ipv4, @configuration[:sip][:listen_port_tls] ]
      else
        binds[:tcp] << [ "127.0.0.1", @configuration[:sip][:listen_port_tls_tunnel] ]
      end
    end
  end

  if @configuration[:sip][:enable_ipv6]
    ipv6 = @configuration[:sip][:listen_ipv6]

    if @configuration[:sip][:sip_udp]
      binds[:udp] << [ ipv6, @configuration[:sip][:listen_port] ]
    end

    if @configuration[:sip][:sip_tcp]
      binds[:tcp] << [ ipv6, @configuration[:sip][:listen_port] ]
    end

    if @configuration[:sip][:sip_tls]
      unless @configuration[:sip][:use_tls_tunnel]
        binds[:tcp] << [ ipv6, @configuration[:sip][:listen_port_tls] ]
      else
        binds[:tcp] << [ "::1", @configuration[:sip][:listen_port_tls_tunnel] ]
      end
    end
  end

  if @configuration[:websocket][:enable_ipv4]
    ipv4 = @configuration[:websocket][:listen_ipv4]

    if @configuration[:websocket][:sip_ws]
      binds[:tcp] << [ ipv4, @configuration[:websocket][:listen_port] ]
    end

    if @configuration[:websocket][:sip_wss]
      unless @configuration[:sip][:use_tls_tunnel]
        binds[:tcp] << [ ipv4, @configuration[:websocket][:listen_port_tls] ]
      else
        binds[:tcp] << [ "127.0.0.1", @configuration[:websocket][:listen_port_tls_tunnel] ]
      end
    end
  end

  if @configuration[:websocket][:enable_ipv6]
    ipv6 = @configuration[:websocket][:listen_ipv6]

    if @configuration[:websocket][:sip_ws]
      binds[:tcp] << [ ipv6, @configuration[:websocket][:listen_port] ]
    end

    if @configuration[:websocket][:sip_wss]
      unless @configuration[:sip][:use_tls_tunnel]
        binds[:tcp] << [ ipv6, @configuration[:websocket][:listen_port_tls] ]
      else
        binds[:tcp] << [ "::1", @configuration[:websocket][:listen_port_tls_tunnel] ]
      end
    end
  end

  [:udp, :tcp].each do |transport|
    transport_str = transport.to_s.upcase
    binds[transport].each do |ip, port|
      begin
        unless (ip_type = ::OverSIP::Utils.ip_type(ip))
          raise ::OverSIP::ConfigurationError, "given IP '#{ip}' is not IPv4 nor IPv6"
        end

        case transport
        when :udp
          case ip_type
          when :ipv4
            socket = ::UDPSocket.new ::Socket::AF_INET
          when :ipv6
            socket = ::UDPSocket.new ::Socket::AF_INET6
          end
          socket.bind ip, port
        when :tcp
          socket = ::TCPServer.open ip, port
        end

        socket.close

      rescue ::Errno::EADDRNOTAVAIL
        raise ::OverSIP::ConfigurationError, "cannot bind in #{transport_str} IP '#{ip}', address not available"
      rescue ::Errno::EADDRINUSE
        raise ::OverSIP::ConfigurationError, "#{transport_str} IP '#{ip}' and port #{port} already in use"
      rescue ::Errno::EACCES
        raise ::OverSIP::ConfigurationError, "no permission to bind in #{transport_str} IP '#{ip}' and port #{port}"
      rescue => e
        raise e.class, "error binding in #{transport_str} IP '#{ip}' and port #{port} (#{e.class}: #{e.message})"
      end
    end
  end
end

.post_processObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/oversip/config.rb', line 236

def self.post_process
  if @configuration[:tls][:public_cert] and @configuration[:tls][:private_cert]
    @use_tls = true
    # Generate a full PEM file containing both the public and private certificate (for Stud).
    full_cert = Tempfile.new("oversip_full_cert_")
    full_cert.puts File.read(@configuration[:tls][:public_cert])
    full_cert.puts File.read(@configuration[:tls][:private_cert])
    @configuration[:tls][:full_cert] = full_cert.path
    full_cert.close
  else
    @configuration[:sip][:sip_tls] = false
    @configuration[:websocket][:sip_wss] = false
  end

  if @configuration[:sip][:sip_udp] or @configuration[:sip][:sip_tcp]
    @use_sip_udp_or_tcp = true
  else
    @configuration[:sip][:listen_port] = nil
  end

  if @configuration[:sip][:sip_tls] and @use_tls
    @use_sip_tls = true
  else
    @configuration[:sip][:listen_port_tls] = nil
  end

  unless @use_sip_udp_or_tcp or @use_sip_tls
    @configuration[:sip][:listen_ipv4] = nil
    @configuration[:sip][:listen_ipv6] = nil
    @configuration[:sip][:enable_ipv4] = nil
    @configuration[:sip][:enable_ipv6] = nil
  end

  unless @configuration[:sip][:enable_ipv4]
    @configuration[:sip][:listen_ipv4] = nil
  end

  unless @configuration[:sip][:enable_ipv6]
    @configuration[:sip][:listen_ipv6] = nil
  end

  if @configuration[:websocket][:sip_ws]
    @use_sip_ws = true
  else
    @configuration[:websocket][:listen_port] = nil
  end

  if @configuration[:websocket][:sip_wss] and @use_tls
    @use_sip_wss = true
  else
    @configuration[:websocket][:listen_port_tls] = nil
  end

  unless @use_sip_ws or @use_sip_wss
    @configuration[:websocket][:listen_ipv4] = nil
    @configuration[:websocket][:listen_ipv6] = nil
    @configuration[:websocket][:enable_ipv4] = nil
    @configuration[:websocket][:enable_ipv6] = nil
  end

  if ( @use_sip_udp_or_tcp or @use_sip_tls ) and @configuration[:sip][:listen_ipv4] == nil and @configuration[:sip][:enable_ipv4]
    unless (@configuration[:sip][:listen_ipv4] = discover_local_ip(:ipv4))
      log_system_warn "dissabling IPv4 for SIP"
      @configuration[:sip][:listen_ipv4] = nil
      @configuration[:sip][:enable_ipv4] = false
    end
  end

  if ( @use_sip_udp_or_tcp or @use_sip_tls ) and @configuration[:sip][:listen_ipv6] == nil and @configuration[:sip][:enable_ipv6]
    unless (@configuration[:sip][:listen_ipv6] = discover_local_ip(:ipv6))
      log_system_warn "dissabling IPv6 for SIP"
      @configuration[:sip][:listen_ipv6] = nil
      @configuration[:sip][:enable_ipv6] = false
    end
  end

  if ( @use_sip_ws or @use_sip_wss ) and @configuration[:websocket][:listen_ipv4] == nil and @configuration[:websocket][:enable_ipv4]
    unless (@configuration[:websocket][:listen_ipv4] = discover_local_ip(:ipv4))
      log_system_warn "dissabling IPv4 for WebSocket"
      @configuration[:websocket][:listen_ipv4] = nil
      @configuration[:websocket][:enable_ipv4] = false
    end
  end

  if ( @use_sip_ws or @use_sip_wss ) and @configuration[:websocket][:listen_ipv6] == nil and @configuration[:websocket][:enable_ipv6]
    unless (@configuration[:websocket][:listen_ipv6] = discover_local_ip(:ipv6))
      log_system_warn "dissabling IPv6 for WebSocket"
      @configuration[:websocket][:listen_ipv6] = nil
      @configuration[:websocket][:enable_ipv6] = false
    end
  end

  if @configuration[:sip][:local_domains]
    if @configuration[:sip][:local_domains].is_a? String
      @configuration[:sip][:local_domains] = [ @configuration[:sip][:local_domains].downcase ]
    end
    @configuration[:sip][:local_domains].each {|local_domain| local_domain.downcase!}
  end
end

.pre_check(conf_yaml) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/oversip/config.rb', line 216

def self.pre_check conf_yaml
  # If TLS files/directories are given as relative path, convert them into absolute paths.

  tls_public_cert = conf_yaml["tls"]["public_cert"] rescue nil
  tls_private_cert = conf_yaml["tls"]["private_cert"] rescue nil
  tls_ca_dir = conf_yaml["tls"]["ca_dir"] rescue nil

  if tls_public_cert.is_a?(String) and tls_public_cert[0] != "/"
    conf_yaml["tls"]["public_cert"] = ::File.join(@config_dir, DEFAULT_TLS_DIR, tls_public_cert)
  end

  if tls_private_cert.is_a?(String) and tls_private_cert[0] != "/"
    conf_yaml["tls"]["private_cert"] = ::File.join(@config_dir, DEFAULT_TLS_DIR, tls_private_cert)
  end

  if tls_ca_dir.is_a?(String) and tls_ca_dir[0] != "/"
    conf_yaml["tls"]["ca_dir"] = ::File.join(@config_dir, DEFAULT_TLS_DIR, tls_ca_dir)
  end
end


449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/oversip/config.rb', line 449

def self.print colorize=true
  color = Term::ANSIColor  if colorize

  puts
  @configuration.each_key do |section|
    if colorize
      puts "  #{color.bold(section.to_s)}:"
    else
      puts "  #{section.to_s}:"
    end
    @configuration[section].each do |parameter, value|
      humanized_value = humanize_value value
      color_value = case value
        when TrueClass
          colorize ? color.bold(color.green(humanized_value)) : humanized_value
        when FalseClass
          colorize ? color.bold(color.red(humanized_value)) : humanized_value
        when NilClass
          humanized_value
        when String, Symbol
          colorize ? color.yellow(humanized_value) : humanized_value
        when Array
          colorize ? color.yellow(humanized_value) : humanized_value
        when Fixnum, Float
          colorize ? color.bold(color.blue(humanized_value)) : humanized_value
        else
          humanized_value
        end
      printf("    %-32s:  %s\n", parameter, color_value)
    end
    puts
  end
end

.reload_logicObject



514
515
516
517
518
519
520
521
522
523
524
# File 'lib/oversip/config.rb', line 514

def self.reload_logic
  begin
    Kernel.load @logic_file
    log_system_info "logic reloaded"
    true
  rescue Exception => e
    log_system_crit "error reloading logic"
    log_system_crit e
    false
  end
end