CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/parser/ini.rb
Views: 11780
1
# -*- coding: binary -*-
2
module Rex
3
module Parser
4
5
###
6
#
7
# This class parses the contents of an INI file.
8
#
9
###
10
class Ini < Hash
11
12
##
13
#
14
# Factories
15
#
16
##
17
18
#
19
# Creates a new class instance and reads in the contents of the supplied
20
# file path.
21
#
22
def self.from_file(path)
23
ini = Ini.new(path)
24
ini.from_file
25
return ini
26
end
27
28
#
29
# Creates a new class instance from the supplied string.
30
#
31
def self.from_s(str)
32
ini = Ini.new
33
ini.from_s(str)
34
return ini
35
end
36
37
#
38
# Initializes an ini instance and tries to read in the groups from the
39
# file if it exists.
40
#
41
def initialize(path = nil)
42
self.path = path
43
44
# Try to synchronize ourself with the file if we
45
# have one
46
begin
47
self.from_file if (self.path)
48
rescue
49
end
50
end
51
52
alias each_group each_key
53
54
#
55
# Adds a group of the supplied name if it doesn't already exist.
56
#
57
def add_group(name = 'global', reset = true)
58
self[name] = {} if (reset == true)
59
self[name] = {} if (!self[name])
60
61
return self[name]
62
end
63
64
#
65
# Checks to see if name is a valid group.
66
#
67
def group?(name)
68
return (self[name] != nil)
69
end
70
71
##
72
#
73
# Serializers
74
#
75
##
76
77
#
78
# Reads in the groups from the supplied file path or the instance's file
79
# path.
80
#
81
def from_file(fpath = nil)
82
fpath = path if (!fpath)
83
84
read_groups(fpath)
85
end
86
87
#
88
# Reads in the groups from the supplied string.
89
#
90
def from_s(str)
91
read_groups_string(str.split("\n"))
92
end
93
94
#
95
# Writes the group settings to a file.
96
#
97
def to_file(tpath = nil)
98
tpath = path if (!tpath)
99
100
f = File.new(tpath, "w")
101
f.write(to_s)
102
f.close
103
end
104
105
#
106
# Converts the groups to a string.
107
#
108
def to_s
109
str = ''
110
keys.sort.each { |k|
111
str << "[#{k}]\n"
112
113
self[k].each_pair { |var, val|
114
str << "#{var}=#{val}\n"
115
}
116
117
str << "\n";
118
}
119
120
return str
121
end
122
123
attr_reader :path
124
125
protected
126
127
#
128
# Reads in the groups and their attributes from the supplied file
129
# path or from the instance's file path if one was set.
130
#
131
def read_groups(fpath) # :nodoc:
132
if (!fpath)
133
raise ArgumentError, "No file path specified.",
134
caller
135
end
136
137
# Read in the contents of the file
138
lines = ::IO.readlines(fpath)
139
140
# Now read the contents from the supplied string
141
read_groups_string(lines)
142
end
143
144
#
145
# Reads groups from the supplied string
146
#
147
def read_groups_string(str) # :nodoc:
148
# Reset the groups hash
149
self.clear
150
151
# The active group
152
active_group = nil
153
154
# Walk each line initializing the groups
155
str.each { |line|
156
next if (line.match(/^;/))
157
158
# Eliminate cr/lf
159
line.gsub!(/(\n|\r)/, '')
160
161
# Is it a group [bob]?
162
if (md = line.match(/^\[(.+?)\]/))
163
active_group = md[1]
164
self[md[1]] = {}
165
# Is it a VAR=VAL?
166
elsif (md = line.match(/^(.+?)=(.*)$/))
167
if (active_group)
168
var, val = md[1], md[2]
169
170
# don't clobber datastore nils with ""
171
unless val.empty?
172
self[active_group][var] = val
173
end
174
end
175
end
176
}
177
end
178
179
attr_writer :path # :nodoc:
180
181
end
182
183
end
184
end
185
186