Updated upload.cgi group check.

This commit is contained in:
Ricky Zhou (周家杰) 2009-03-13 18:24:24 -04:00
parent a1cf355f96
commit de7bf0cb59

72
scripts/upload.cgi/upload.cgi Executable file → Normal file
View file

@ -16,9 +16,6 @@ import tempfile
import StringIO import StringIO
import grp import grp
sys.path.append('/var/fedora-accounts')
import website
# reading buffer size # reading buffer size
BUFFER_SIZE = 4096 BUFFER_SIZE = 4096
@ -28,7 +25,8 @@ DEBUG = 0
# We check modules exist from this dircetory # We check modules exist from this dircetory
CVSREPO = "/cvs/pkgs/rpms" CVSREPO = "/cvs/pkgs/rpms"
do_userdb_auth = 1 # Fedora Packager Group
PACKAGER_GROUP = "packager"
# log a trace of what we're doing # log a trace of what we're doing
def log_msg(*msgs): def log_msg(*msgs):
@ -61,9 +59,9 @@ def send_ok(text):
# check and validate that all the fields are present # check and validate that all the fields are present
def check_form(var): def check_form(var):
if not form.has_key(var): if not form.has_key(var):
send_error("required field '%s' is not present" % (var,)) send_error("Required field '%s' is not present" % (var,))
ret = form.getvalue(var) ret = form.getvalue(var)
if type(ret) == type([]): if type(ret) == list:
send_error("Multiple values given for '%s'. Aborting" % (var,)) send_error("Multiple values given for '%s'. Aborting" % (var,))
ret = os.path.basename(ret) # this is a path component ret = os.path.basename(ret) # this is a path component
return ret return ret
@ -79,45 +77,23 @@ def check_dir(tmpdir, wok = os.W_OK):
send_error("Path %s is not a directory." % (tmpdir,)) send_error("Path %s is not a directory." % (tmpdir,))
return 1 return 1
# authenticated = False
# MAIN START
#
if do_userdb_auth:
dbh = website.get_dbh()
auth_username = auth_password = None
need_auth = 1
if os.environ.has_key('SSL_CLIENT_S_DN_CN'): if os.environ.has_key('SSL_CLIENT_S_DN_CN'):
auth_username = os.environ['SSL_CLIENT_S_DN_CN'] auth_username = os.environ['SSL_CLIENT_S_DN_CN']
need_auth = 0 if auth_username in grp.getgrnam(PACKAGER_GROUP)[3]:
elif do_userdb_auth and 0: authenticated = True
authtype, authinfo = website.get_http_auth_info()
need_auth = 1
auth_msg = "Authentication is required."
if authinfo:
if authtype.lower() == 'basic':
need_auth = not website.do_checkpass(dbh, authinfo[0], authinfo[1])
auth_username, auth_password = authinfo
auth_msg = "Username or password incorrect."
else:
auth_msg = "Unknown authentication type %s" % authtype
pieces = os.environ['REQUEST_URI'].split('/') pieces = os.environ['REQUEST_URI'].split('/')
assert pieces[1] == 'repo' assert pieces[1] == 'repo'
if do_userdb_auth:
#need_auth = need_auth or not website.have_group(dbh, auth_username, 'cvs' + pieces[2])
#need_auth = need_auth or not website.have_group(dbh, auth_username, 'packager')
need_auth = need_auth or not auth_username in grp.getgrnam('packager')[3]
auth_msg = "You do not have the appropriate authorization to upload. %s %s %s" % (dbh, auth_username, 'cvs' + pieces[2])
if need_auth: if not authenticated
print """Status: 403 Unauthorized to access the document print """Status: 403 Forbidden
WWW-authenticate: Basic realm="fedora.redhat.com"
Content-type: text/plain Content-type: text/plain
""" + str(auth_msg) You must be in the %s group to upload.
sys.exit(0) """ % PACKAGER_GROUP
sys.exit(0)
form = cgi.FieldStorage() form = cgi.FieldStorage()
NAME = check_form("name") NAME = check_form("name")
@ -128,11 +104,11 @@ MD5SUM = check_form("md5sum")
# In a submission, we don;t get a FILENAME, just the FILE. # In a submission, we don;t get a FILENAME, just the FILE.
FILE = None FILE = None
FILENAME = None FILENAME = None
if form.has_key("filename"): if form.has_key("filename"):
# check the presence of the file # check the presence of the file
FILENAME = check_form("filename") FILENAME = check_form("filename")
log_msg("Checking file status", log_msg("Checking file status", "NAME=%s FILENAME=%s MD5SUM=%s" % (NAME,FILENAME,MD5SUM))
"NAME=%s FILENAME=%s MD5SUM=%s" % (NAME,FILENAME,MD5SUM))
else: else:
if form.has_key("file"): if form.has_key("file"):
FILE = form["file"] FILE = form["file"]
@ -144,14 +120,15 @@ else:
send_error("Could not extract the filename for upload. Aborting") send_error("Could not extract the filename for upload. Aborting")
else: else:
send_error("required field '%s' is not present" % ("file", )) send_error("required field '%s' is not present" % ("file", ))
log_msg("Processing upload request", log_msg("Processing upload request", "NAME=%s FILENAME=%s MD5SUM=%s" % (NAME,FILENAME,MD5SUM))
"NAME=%s FILENAME=%s MD5SUM=%s" % (NAME,FILENAME,MD5SUM))
# Now that all the fields are valid,, figure out our operating environment # Now that all the fields are valid, figure out our operating environment
if not os.environ.has_key("SCRIPT_FILENAME"): if not os.environ.has_key("SCRIPT_FILENAME"):
send_error("My running environment is funky. Aborting") send_error("My running environment is funky. Aborting")
# start processing this request # start processing this request
my_script = os.environ["SCRIPT_FILENAME"] my_script = os.environ["SCRIPT_FILENAME"]
# the module's top level directory # the module's top level directory
my_topdir = os.path.dirname(my_script) my_topdir = os.path.dirname(my_script)
my_moddir = "%s/%s" % (my_topdir, NAME) my_moddir = "%s/%s" % (my_topdir, NAME)
@ -174,17 +151,17 @@ if os.access(file_dest, os.F_OK | os.R_OK):
message = "Available" message = "Available"
else: else:
FILE.file.close() FILE.file.close()
message = "File %s already exists\nFile: %s Size: %d" % ( message = "File %s already exists\nFile: %s Size: %d" % (FILENAME, file_dest, s[stat.ST_SIZE])
FILENAME, file_dest, s[stat.ST_SIZE])
send_ok(message) send_ok(message)
sys.exit(0) sys.exit(0)
# just checking? # just checking?
if FILE is None: if FILE is None:
send_ok("Missing") send_ok("Missing")
sys.exit(-9) sys.exit(-9)
# check that all directories are in place # check that all directories are in place
for tmpdir in [ my_topdir, my_moddir, my_filedir, my_md5dir]: for tmpdir in [my_topdir, my_moddir, my_filedir, my_md5dir]:
if not check_dir(tmpdir): if not check_dir(tmpdir):
# we agree to create this directory if the corresponding cvs module dir exists # we agree to create this directory if the corresponding cvs module dir exists
if tmpdir == my_moddir: if tmpdir == my_moddir:
@ -212,18 +189,21 @@ while 1:
tmpfd.write(s) tmpfd.write(s)
m.update(s) m.update(s)
FILELENGTH = FILELENGTH + len(s) FILELENGTH = FILELENGTH + len(s)
# now we're done reading, check the MD5 sum of what we got # now we're done reading, check the MD5 sum of what we got
tmpfd.close() tmpfd.close()
my_md5sum = m.hexdigest() my_md5sum = m.hexdigest()
if MD5SUM != my_md5sum: if MD5SUM != my_md5sum:
send_error("MD5 check failed. Received %s instead of %s" % ( send_error("MD5 check failed. Received %s instead of %s" % (
my_md5sum, MD5SUM)) my_md5sum, MD5SUM))
# wow, even the MD5SUM matches. make sure full path is valid now # wow, even the MD5SUM matches. make sure full path is valid now
for tmpdir in [ my_moddir, my_filedir, my_md5dir ]: for tmpdir in [ my_moddir, my_filedir, my_md5dir ]:
if not check_dir(tmpdir): if not check_dir(tmpdir):
os.mkdir(tmpdir, 02775) os.mkdir(tmpdir, 02775)
log_msg("mkdir", tmpdir) log_msg("mkdir", tmpdir)
# and move our file to the final location # and move our file to the final location
os.rename(tmpfile, file_dest) os.rename(tmpfile, file_dest)
log_msg("Stored filesize", FILELENGTH, file_dest) log_msg("Stored filesize", FILELENGTH, file_dest)