Revision 7ebd876f lib/utils/__init__.py
b/lib/utils/__init__.py | ||
---|---|---|
73 | 73 |
UUID_RE = re.compile('^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-' |
74 | 74 |
'[a-f0-9]{4}-[a-f0-9]{12}$') |
75 | 75 |
|
76 |
#: Shell param checker regexp |
|
77 |
_SHELLPARAM_REGEX = re.compile(r"^[-a-zA-Z0-9._+/:%@]+$") |
|
78 |
|
|
79 | 76 |
|
80 | 77 |
def ForceDictType(target, key_types, allowed_values=None): |
81 | 78 |
"""Force the values of a dict to have certain types. |
... | ... | |
232 | 229 |
return nv |
233 | 230 |
|
234 | 231 |
|
235 |
def IsValidShellParam(word): |
|
236 |
"""Verifies is the given word is safe from the shell's p.o.v. |
|
237 |
|
|
238 |
This means that we can pass this to a command via the shell and be |
|
239 |
sure that it doesn't alter the command line and is passed as such to |
|
240 |
the actual command. |
|
241 |
|
|
242 |
Note that we are overly restrictive here, in order to be on the safe |
|
243 |
side. |
|
244 |
|
|
245 |
@type word: str |
|
246 |
@param word: the word to check |
|
247 |
@rtype: boolean |
|
248 |
@return: True if the word is 'safe' |
|
249 |
|
|
250 |
""" |
|
251 |
return bool(_SHELLPARAM_REGEX.match(word)) |
|
252 |
|
|
253 |
|
|
254 |
def BuildShellCmd(template, *args): |
|
255 |
"""Build a safe shell command line from the given arguments. |
|
256 |
|
|
257 |
This function will check all arguments in the args list so that they |
|
258 |
are valid shell parameters (i.e. they don't contain shell |
|
259 |
metacharacters). If everything is ok, it will return the result of |
|
260 |
template % args. |
|
261 |
|
|
262 |
@type template: str |
|
263 |
@param template: the string holding the template for the |
|
264 |
string formatting |
|
265 |
@rtype: str |
|
266 |
@return: the expanded command line |
|
267 |
|
|
268 |
""" |
|
269 |
for word in args: |
|
270 |
if not IsValidShellParam(word): |
|
271 |
raise errors.ProgrammerError("Shell argument '%s' contains" |
|
272 |
" invalid characters" % word) |
|
273 |
return template % args |
|
274 |
|
|
275 |
|
|
276 | 232 |
def ParseCpuMask(cpu_mask): |
277 | 233 |
"""Parse a CPU mask definition and return the list of CPU IDs. |
278 | 234 |
|
Also available in: Unified diff