#!/usr/bin/python DESCRIPTION='''check cell institutions and fix missing''' #std libs import os, sys, re, string, pwd, grp FIX=1 #0: just check, 1: fix missing def main(): if checkdir(FIX, '/etc/cell'): checkown(FIX, '/etc/cell', 'root', 'wheel', 'r') checkfile(FIX, '/etc/cell/shortname', 'root', 'wheel') if checkdir(FIX, '/etc/allcells'): checkown(FIX, '/etc/allcells', 'transfer', '', 'r') def checkfile(FIX, PATH, OWNER='', GROUP='', PERMISSIONS=''): print "checking whether file exists: ", PATH SUCCESS=1 if not os.path.isfile(PATH): sys.stderr.write('file does not exist: '+PATH+'\n') SUCCESS=0 if FIX: sys.stderr.write('no fix available, create and fill yourself: ' +PATH+ '\n') #TODO: check owner, group, permissions return SUCCESS def checkdir(FIX, dir): print "checking whether directory exists: ", dir SUCCESS=1 if not os.path.isdir(dir): sys.stderr.write('directory does not exist: '+dir+'\n') if FIX: try: os.makedirs(dir) sys.stderr.write('directory created: ' +dir+ '\n') except OSError,e: sys.stderr.write('directory not created: ' +dir+ ', ' +str(e)+ '\n') SUCCESS=0 else: SUCCESS=0 return SUCCESS def checkown(FIX, file, owner, group='', options=''): #options:r recursive but do not follow symlinks SUCCESS=1 if 'r' in options: FILES=os.popen('find '+file).read().strip() for singlefile in FILES.split('\n'): if not checkown_singlefile(FIX, singlefile, owner, group): SUCCESS=0 else: if not checkown_singlefile(FIX, file, owner, group): SUCCESS=0 return SUCCESS def checkown_singlefile(FIX, file, owner, group=''): SUCCESS=1 STAT=os.lstat(file) if owner: print "checking ownership: ", file UID=pwd.getpwnam(owner)[2] if not UID == STAT.st_uid: sys.stderr.write('not owned by '+owner+': ' +file+ '\n') if FIX: try: os.lchown( file, UID, STAT.st_gid ) sys.stderr.write('ownership changed: ' +file+ '\n') except OSError,e: sys.stderr.write('ownership not changed: ' +file+ ', ' +str(e)+ '\n') SUCCESS=0 else: SUCCESS=0 if group: print "checking group-ownership: ", file GID=grp.getgrnam(group)[2] if not GID == STAT.st_gid: sys.stderr.write('not owned by group '+group+': ' +file+ '\n') if FIX: try: os.lchown(file, STAT.st_uid, GID ) sys.stderr.write('group-ownership changed: ' +file+ '\n') except OSError,e: sys.stderr.write('group-ownership not changed: ' +file+ ', ' +str(e)+ '\n') SUCCESS=0 else: SUCCESS=0 return SUCCESS def checklink(FIX, link, dst=''): SUCCESS=1 pass #TODO return SUCCESS if __name__ == "__main__": main()