1
# Windows Azure Linux Agent
3
# Copyright 2014 Microsoft Corporation
5
# Licensed under the Apache License, Version 2.0 (the "License");
6
# you may not use this file except in compliance with the License.
7
# You may obtain a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
17
# Requires Python 2.4+ and Openssl 1.0+
23
def FindFirstNode(xmlDoc, selector):
24
nodes = FindAllNodes(xmlDoc, selector)
28
def FindAllNodes(xmlDoc, selector):
29
nodes = xmlDoc.findall(selector)
32
def GetNodeTextData(a):
34
Filter non-text nodes from DOM tree
36
for b in a.childNodes:
37
if b.nodeType == b.TEXT_NODE:
40
def Unpack(buf, offset, range):
42
Unpack bytes into python values.
46
result = (result << 8) | Ord(buf[offset + i])
49
def UnpackLittleEndian(buf, offset, length):
51
Unpack little endian bytes into python values.
53
return Unpack(buf, offset, list(range(length - 1, -1, -1)))
55
def UnpackBigEndian(buf, offset, length):
57
Unpack big endian bytes into python values.
59
return Unpack(buf, offset, list(range(0, length)))
61
def HexDump3(buf, offset, length):
63
Dump range of buf in formatted hex.
65
return ''.join(['%02X' % Ord(char) for char in buf[offset:offset + length]])
69
Dump buf in formatted hex.
71
return HexDump3(buf, 0, len(buf))
73
def IsInRangeInclusive(a, low, high):
75
Return True if 'a' in 'low' <= a >= 'high'
77
return (a >= low and a <= high)
81
Return True if character is displayable.
83
return (IsInRangeInclusive(ch, Ord('A'), Ord('Z'))
84
or IsInRangeInclusive(ch, Ord('a'), Ord('z'))
85
or IsInRangeInclusive(ch, Ord('0'), Ord('9')))
87
def HexDump(buffer, size):
89
Return Hex formated dump of a 'buffer' of 'size'.
94
for i in range(0, size):
96
result += "%06X: " % i
99
byte = ord(byte.decode('latin1'))
100
result += "%02X " % byte
103
if ((i + 1) % 16) == 0 or (i + 1) == size:
105
while ((j + 1) % 16) != 0:
111
for j in range(i - (i % 16), i + 1):
113
if type(byte) == str:
114
byte = ord(byte.decode('latin1'))
116
if IsPrintable(byte):
125
Allows indexing into a string or an array of integers transparently.
126
Generic utility function.
128
if type(a) == type("a"):
132
def CompareBytes(a, b, start, length):
133
for offset in range(start, start + length):
134
if Ord(a[offset]) != Ord(b[offset]):
138
def IntegerToIpAddressV4String(a):
140
Build DHCP request string.
142
return "%u.%u.%u.%u" % ((a >> 24) & 0xFF,
150
uni = unicode(val, 'utf-8', errors='ignore')
154
raise ValueError('<Unsupported charset>')
156
return uni.encode('ascii', 'backslashreplace')
158
def HexStringToByteArray(a):
160
Return hex string packed into a binary struct.
163
for c in range(0, len(a) // 2):
164
b += struct.pack("B", int(a[c * 2:c * 2 + 2], 16))
167
def SetSshConfig(config, name, val):
169
for i in range(0, len(config)):
170
if config[i].startswith(name):
171
config[i] = "{0} {1}".format(name, val)
173
elif config[i].startswith("Match"):
174
#Match block must be put in the end of sshd config
177
config.insert(i, "{0} {1}".format(name, val))
181
if ord(c[0]) > 128 and ord(c[1]) > 128 and ord(c[2]) > 128:
185
def GetPasswordHash(password, useSalt, saltType, saltLength):
188
collection = string.ascii_letters + string.digits
189
salt = ''.join(random.choice(collection) for _ in range(saltLength))
190
salt = "${0}${1}".format(saltType, salt)
191
return crypt.crypt(password, salt)
193
def NumberToBytes(i):
195
Pack number into bytes. Retun as string.
199
result.append(chr(i & 0xFF))
202
return ''.join(result)
206
Return string representation of bits in a.
212
c = c | (bit << index)
215
s = s + struct.pack('>B', c)