Class: Cfruby::OS::FreeBSDOS

Inherits:
OS
  • Object
show all
Defined in:
lib/libcfruby/osmodules/freebsd.rb

Instance Method Summary collapse

Methods inherited from OS

#get_process_manager, #get_scheduler, #method_missing, #to_s

Constructor Details

#initializeFreeBSDOS

Returns a new instance of FreeBSDOS.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/libcfruby/osmodules/freebsd.rb', line 10

def initialize()
	@keys = Hash.new()

	@keys['name'] = 'FreeBSD'
	@keys['freebsd'] = true
	@keys['FreeBSD'] = true
	@keys['FREEBSD'] = true

	unameoutput = `uname -v`
	@keys['version'] = unameoutput[/FreeBSD ([^ ]+)/, 1]

	@keys['hostname'] = `/bin/hostname`.strip
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Cfruby::OS::OS

Instance Method Details

#[](key) ⇒ Object

an alternative to calling lookup



57
58
59
# File 'lib/libcfruby/osmodules/freebsd.rb', line 57

def [](key)
	return(lookup(key))
end

#get_package_managerObject

returns an object implementing the PackageManager interface as appropriate for the default package management system for a given OS



27
28
29
# File 'lib/libcfruby/osmodules/freebsd.rb', line 27

def get_package_manager()
	return(Packages::FreeBSDPackageManager.new())
end

#get_user_managerObject

Returns a UserManager object specific to FreeBSD



33
34
35
# File 'lib/libcfruby/osmodules/freebsd.rb', line 33

def get_user_manager()
	return(Users::FreeBSDUserManager.new())
end

#jailed?Boolean

return true, false, or raises an exception if jail status can’t be determing

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/libcfruby/osmodules/freebsd.rb', line 63

def jailed?()
	# Get our FreeBSD major version
	version = @keys['version'][0,1].to_i()

	# Our options are somewhat limited if we are freebsd 4.x, so we'll run a few tests
	if version == 4
		# Easiest test, we check for a real kernel, since no jail should have a real one
		if File.exists?('/kernel') && !File.symlink?('/kernel')
			return false
		end

		# Second test, we check the process table
		psoutput = `ps ax`

		# If every line has a J in it, it's likely a jail (the first line isn't a process, that's why the jcount starts at 1)
		lines = psoutput.split("\n")
		jcount = 1
		lines.each() { |line|
			if line =~ /J/
				jcount += 1
			end
		}
		if lines.size == jcount
			return true
		end

		# Last resort, we see if 'injail' exists and if so, we check the return code from that
		if File.exists?('/usr/local/bin/injail')
			`injail`
			if $?.exitstatus == 0
				return true
			elsif $?.exitstatus == 1
				return false
			else
				raise(OSFreeBSDJailError, "/usr/local/bin/injail exists but couldn't determine jail status (error code 2)")
			end
		end

		# If we got this far, we can't figure out if we are in a jail and we lob an error
		raise(OSFreeBSDJailError, "Couldn't determine jail status with any test")					

	end

	# If we are FreeBSD >5, we can consult sysctl (we think)
	if version >= 5
		sysctloutput = `sysctl security.jail.jailed`
		if sysctloutput =~ /1/
			return true
		else
			return false
		end
	end

end

#lookup(key) ⇒ Object

returns the value of the given key for this OS. At a minimum an OS should provide the following:

name

returns the name of the OS

version

the version of the OS

osname

e.g. freebsd, linux, windows, etc - these should return true and be case insensitive to allow

lookups of the form lookup(‘Windows’) In addition to the above name and version should be implemented as getter methods for convenience



46
47
48
49
50
51
52
53
# File 'lib/libcfruby/osmodules/freebsd.rb', line 46

def lookup(key)
	# If we are requesting jail status, we consult the jailed? function
	if key == 'jailed'
		return(jailed?())
	end

	return(@keys[key])
end