def open_database_connection(config_registry, pkgdbu=False, db_server=None): connection_info = { # see <http://www.postgresql.org/docs/8.4/static/libpq-connect.html> 'dbname': 'pkgdb', } if config_registry.is_true('pkgdb/requiressl'): connection_info['sslmode'] = 'require' if pkgdbu: # 'host' not specified -> localhost over Unix-domain socket (connection type "local") connection_info['user'] = '******' password_file = '/etc/postgresql/pkgdb.secret' else: if db_server is None: db_server = get_dbservername(config_registry['domainname']) if db_server is None: return None connection_info['host'] = db_server connection_info['user'] = config_registry.get( 'pkgdb/user', '%s$' % (config_registry['hostname'], )) password_file = config_registry.get('pkgdb/pwdfile', '/etc/machine.secret') connection_info['password'] = open(password_file, 'rb').read().rstrip('\n') connectstring = ' '.join([ "%s='%s'" % ( key, value.replace('\\', '\\\\').replace("'", "\\'"), ) for ( key, value, ) in connection_info.items() ]) connection = pgdb.connect(database=connectstring) return connection
def open_database_connection(config_registry, pkgdbu=False, db_server=None): connection_info = { # see <http://www.postgresql.org/docs/8.4/static/libpq-connect.html> 'dbname': 'pkgdb', } if config_registry.is_true('pkgdb/requiressl'): connection_info['sslmode'] = 'require' if pkgdbu: # 'host' not specified -> localhost over Unix-domain socket (connection type "local") connection_info['user'] = '******' password_file = '/etc/postgresql/pkgdb.secret' else: if db_server is None: db_server = get_dbservername(config_registry['domainname']) if db_server is None: return None connection_info['host'] = db_server connection_info['user'] = config_registry.get('pkgdb/user', '%s$' % (config_registry['hostname'], )) password_file = config_registry.get('pkgdb/pwdfile', '/etc/machine.secret') connection_info['password'] = open(password_file, 'rb').read().rstrip('\n') connectstring = ' '.join( [ "%s='%s'" % (key, value.replace('\\', '\\\\').replace("'", "\\'"), ) for (key, value, ) in connection_info.items() ] ) connection = pgdb.connect(database=connectstring) return connection
def main(): '''main function for univention-pkgdb-scan''' options = parse_options() if options.action == 'version': print '%s %s' % ( os.path.basename(sys.argv[0]), '@%@package_version@%@', ) return 0 config_registry = univention.config_registry.ConfigRegistry() config_registry.load() # Datenbankzugriffsmethode ermitteln if options.action in PRIVILEGED_OPERATIONS: connection = open_database_connection(config_registry, pkgdbu=True) else: connection = open_database_connection(config_registry, pkgdbu=False) if connection is None: print 'No DB-Server-Name found.' return 1 cursor = connection.cursor() if options.action == 'test-superuser': return sql_test_superuser(cursor) elif options.action == 'dump-systems': return dump_systems(cursor) elif options.action == 'dump-packages': return dump_packages(cursor) elif options.action == 'dump-systems-packages': return dump_systems_packages(cursor) elif options.action == 'dump-all': return \ dump_systems(cursor) or \ dump_packages(cursor) or \ dump_systems_packages(cursor) elif not sql_check_privileges(cursor): print 'PKGDB: no privileges to access the database' print 'You must first add this system with --add-system on the db-server (or join the system)' print 'This should be done automatically by the cronjob univention-pkgdb-check' return 1 elif options.action == 'add-system': # Systembenutzer zur Datenbank hinzufügen return sql_grant_system(connection, cursor, options.system) elif options.action == 'del-system': # Systembenutzer aus Datenbank entfernen return sql_revoke_system(connection, cursor, options.system) elif options.action == 'fill-testdb': return action_fill_testdb(connection, cursor, config_registry) elif options.action == 'remove-system': return action_remove_system(connection, cursor, options.system) elif not config_registry.is_true('pkgdb/scan'): log('univention-config-registry pkgdb/scan is not true') print 'The Univention Configuration Registry variable pkgdb/scan is not true.' return 0 elif options.action == 'scan': return action_scan(connection, cursor, config_registry)
def main(): '''main function for univention-pkgdb-scan''' options = parse_options() if options.action == 'version': print '%s %s' % (os.path.basename(sys.argv[0]), '@%@package_version@%@', ) return 0 config_registry = univention.config_registry.ConfigRegistry() config_registry.load() # Datenbankzugriffsmethode ermitteln if options.action in PRIVILEGED_OPERATIONS: connection = open_database_connection(config_registry, pkgdbu=True) else: connection = open_database_connection(config_registry, pkgdbu=False) if connection is None: print 'No DB-Server-Name found.' return 1 cursor = connection.cursor() if options.action == 'test-superuser': return sql_test_superuser(cursor) elif options.action == 'dump-systems': return dump_systems(cursor) elif options.action == 'dump-packages': return dump_packages(cursor) elif options.action == 'dump-systems-packages': return dump_systems_packages(cursor) elif options.action == 'dump-all': return \ dump_systems(cursor) or \ dump_packages(cursor) or \ dump_systems_packages(cursor) elif not sql_check_privileges(cursor): print 'PKGDB: no privileges to access the database' print 'You must first add this system with --add-system on the db-server (or join the system)' print 'This should be done automatically by the cronjob univention-pkgdb-check' return 1 elif options.action == 'add-system': # Systembenutzer zur Datenbank hinzufügen return sql_grant_system(connection, cursor, options.system) elif options.action == 'del-system': # Systembenutzer aus Datenbank entfernen return sql_revoke_system(connection, cursor, options.system) elif options.action == 'fill-testdb': return action_fill_testdb(connection, cursor, config_registry) elif options.action == 'remove-system': return action_remove_system(connection, cursor, options.system) elif not config_registry.is_true('pkgdb/scan'): log('univention-config-registry pkgdb/scan is not true') print 'The Univention Configuration Registry variable pkgdb/scan is not true.' return 0 elif options.action == 'scan': return action_scan(connection, cursor, config_registry)
def get_dns_server(config_registry, active_services): if config_registry.is_true('ad/member'): ad_domain_info = univention.lib.admember.lookup_adds_dc() server = ad_domain_info.get('DC IP') else: hostname = config_registry.get('hostname') domainname = config_registry.get('domainname') if set(active_services) >= {'Samba 4', 'DNS'}: server = ".".join([hostname, domainname]) else: ## TODO: Memberserver in Samba 4 domain server = None return server