2000-07-05 22:42:16 +08:00
|
|
|
#############################################################################
|
|
|
|
# Project : Linux-Mandrake
|
|
|
|
# Module : rpmlint
|
|
|
|
# File : PostCheck.py
|
|
|
|
# Version : $Id$
|
|
|
|
# Author : Frederic Lepied
|
|
|
|
# Created On : Wed Jul 5 13:30:17 2000
|
|
|
|
# Purpose : Check post/pre scripts
|
|
|
|
#############################################################################
|
|
|
|
|
|
|
|
from Filter import *
|
|
|
|
import AbstractCheck
|
|
|
|
import rpm
|
|
|
|
import re
|
2000-08-01 03:45:17 +08:00
|
|
|
import os
|
|
|
|
import commands
|
2001-02-02 14:51:57 +08:00
|
|
|
import string
|
2000-08-01 03:45:17 +08:00
|
|
|
|
2000-08-25 16:42:15 +08:00
|
|
|
DEFAULT_VALID_SHELLS=('/bin/sh',
|
|
|
|
'/bin/bash',
|
2000-12-07 23:21:20 +08:00
|
|
|
'/sbin/sash',
|
2000-08-25 16:42:15 +08:00
|
|
|
'/usr/bin/perl',
|
|
|
|
)
|
|
|
|
|
2000-08-01 03:45:17 +08:00
|
|
|
extract_dir=Config.getOption('ExtractDir', '/tmp')
|
2000-08-25 16:42:15 +08:00
|
|
|
valid_shells=Config.getOption('ValidShells', DEFAULT_VALID_SHELLS)
|
2000-08-01 03:45:17 +08:00
|
|
|
|
2001-05-18 20:39:40 +08:00
|
|
|
braces_regex=re.compile('^[^#]*%', re.MULTILINE)
|
|
|
|
bracket_regex=re.compile('^[^#]*if.*[^ \]]\]', re.MULTILINE)
|
2000-10-12 13:09:02 +08:00
|
|
|
home_regex=re.compile('[^a-zA-Z]+~|\$HOME', re.MULTILINE)
|
2001-02-02 14:44:56 +08:00
|
|
|
dangerous_command_regex=re.compile("(^|\s|;)(cp|mv|ln|tar|rpm|chmod|chown|rm|cpio|install)\s", re.MULTILINE)
|
2001-05-18 20:39:40 +08:00
|
|
|
single_command_regex=re.compile("^[ \n]*([^ \n]+)[ \n]*$")
|
2000-10-12 13:09:02 +08:00
|
|
|
|
2000-08-01 03:45:17 +08:00
|
|
|
def incorrect_shell_script(shellscript):
|
|
|
|
tmpfile = "%s/.bash-script.%d" % (extract_dir, os.getpid())
|
|
|
|
if not shellscript:
|
|
|
|
return 0
|
|
|
|
file=open(tmpfile, 'w')
|
|
|
|
file.write(shellscript)
|
|
|
|
file.close()
|
|
|
|
ret=commands.getstatusoutput("/bin/bash -n %s" % tmpfile)
|
|
|
|
os.remove(tmpfile)
|
|
|
|
return ret[0]
|
2000-07-05 22:42:16 +08:00
|
|
|
|
2000-08-29 06:08:02 +08:00
|
|
|
def incorrect_perl_script(perlscript):
|
|
|
|
tmpfile = "%s/.perl-script.%d" % (extract_dir, os.getpid())
|
|
|
|
if not perlscript:
|
|
|
|
return 0
|
|
|
|
file=open(tmpfile, 'w')
|
|
|
|
file.write(perlscript)
|
|
|
|
file.close()
|
|
|
|
ret=commands.getstatusoutput("/usr/bin/perl -wc %s" % tmpfile)
|
|
|
|
os.remove(tmpfile)
|
|
|
|
return ret[0]
|
|
|
|
|
2000-07-05 22:42:16 +08:00
|
|
|
class PostCheck(AbstractCheck.AbstractCheck):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
AbstractCheck.AbstractCheck.__init__(self, "PostCheck")
|
|
|
|
|
|
|
|
def check(self, pkg, verbose):
|
|
|
|
# Check only binary package
|
|
|
|
if pkg.isSource():
|
|
|
|
return
|
|
|
|
|
2000-08-01 03:45:17 +08:00
|
|
|
for tag in ((rpm.RPMTAG_PREIN, rpm.RPMTAG_PREINPROG, "%pre"),
|
|
|
|
(rpm.RPMTAG_POSTIN, rpm.RPMTAG_POSTINPROG, "%post"),
|
|
|
|
(rpm.RPMTAG_PREUN, rpm.RPMTAG_PREUNPROG, "%preun"),
|
|
|
|
(rpm.RPMTAG_POSTUN, rpm.RPMTAG_POSTUNPROG, "%postun")):
|
2000-07-05 22:42:16 +08:00
|
|
|
script = pkg[tag[0]]
|
2000-08-01 03:45:17 +08:00
|
|
|
prog = pkg[tag[1]]
|
|
|
|
if script:
|
2000-08-25 16:42:15 +08:00
|
|
|
if prog:
|
|
|
|
if not prog in valid_shells:
|
|
|
|
printError(pkg, "invalid-shell-in-" + tag[2], prog)
|
2000-08-29 06:09:33 +08:00
|
|
|
if prog == "/bin/sh" or prog == "/bin/bash" or prog == "/usr/bin/perl":
|
2000-10-12 13:09:02 +08:00
|
|
|
if braces_regex.search(script):
|
2000-08-16 15:29:51 +08:00
|
|
|
printWarning(pkg, "percent-in-" + tag[2])
|
2000-10-12 13:09:02 +08:00
|
|
|
if bracket_regex.search(script):
|
2000-08-16 15:29:51 +08:00
|
|
|
printWarning(pkg, "spurious-bracket-in-" + tag[2])
|
2000-10-12 13:09:02 +08:00
|
|
|
res=dangerous_command_regex.search(script)
|
|
|
|
if res:
|
2000-11-23 23:29:15 +08:00
|
|
|
printWarning(pkg, "dangerous-command-in-" + tag[2], res.group(2))
|
2000-10-12 13:09:02 +08:00
|
|
|
|
2000-08-29 06:08:02 +08:00
|
|
|
if prog == "/bin/sh" or prog == "/bin/bash":
|
2000-08-01 03:45:17 +08:00
|
|
|
if incorrect_shell_script(script):
|
|
|
|
printError(pkg, "shell-syntax-error-in-" + tag[2])
|
2000-10-12 13:09:02 +08:00
|
|
|
if home_regex.search(script):
|
|
|
|
printError(pkg, "use-of-home-in-" + tag[2])
|
2000-08-29 06:08:02 +08:00
|
|
|
|
|
|
|
if prog == "/usr/bin/perl":
|
|
|
|
if incorrect_perl_script(script):
|
|
|
|
printError(pkg, "perl-syntax-error-in-" + tag[2])
|
2001-05-18 20:39:40 +08:00
|
|
|
|
|
|
|
res=single_command_regex.search(script)
|
|
|
|
if res:
|
|
|
|
printWarning(pkg, 'one-line-command-in-' + tag[2], res.group(1))
|
2000-09-12 17:58:55 +08:00
|
|
|
else:
|
|
|
|
if prog in valid_shells:
|
|
|
|
printWarning(pkg, "empty-" + tag[2])
|
2001-02-02 14:51:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
ghost_files=pkg.ghostFiles()
|
|
|
|
if ghost_files:
|
2001-05-18 17:21:06 +08:00
|
|
|
postin=pkg[rpm.RPMTAG_POSTIN]
|
|
|
|
prein=pkg[rpm.RPMTAG_PREIN]
|
|
|
|
if not postin and not prein:
|
2001-02-02 14:51:57 +08:00
|
|
|
printWarning(pkg, 'ghost-files-without-postun')
|
|
|
|
else:
|
|
|
|
for f in ghost_files:
|
2001-05-18 17:21:06 +08:00
|
|
|
if (not postin or string.find(postin, f) == -1) and \
|
|
|
|
(not prein or string.find(prein, f) == -1):
|
|
|
|
printWarning(pkg, 'postin-without-ghost-file-creation', f)
|
2000-09-12 17:58:55 +08:00
|
|
|
|
2000-07-05 22:42:16 +08:00
|
|
|
# Create an object to enable the auto registration of the test
|
|
|
|
check=PostCheck()
|
|
|
|
|
|
|
|
# PostCheck.py ends here
|