def main(): logger = Factory.get_logger("cronjob") db = Factory.get('Database')() const = Factory.get("Constants")(db) account = Factory.get('Account')(db) auth_prefix, auth_method = "{crypt}", int(const.auth_type_md5_crypt) ldif = LDIFWriter('SERVICES', None) dn = ldif.getconf('dn') ldif.write_container() for username in ldif.getconf('users'): account.clear() try: account.find_by_name(username) except Errors.NotFoundError: logger.error("User '%s' not found" % username) sys.exit(1) passwd = None qh = QuarantineHandler.check_entity_quarantines(db, account.entity_id) if not (qh.should_skip() or qh.is_locked()): try: passwd = account.get_account_authentication(auth_method) except Errors.NotFoundError: logger.warn("Password not found for user %s", username) ldif.write_entry( "cn=%s,%s" % (username, dn), { 'description': "Note: The password is maintained in Cerebrum.", 'objectClass': ('applicationProcess', 'simpleSecurityObject'), 'userPassword': auth_prefix + (passwd or "*locked") }) ldif.close()
def main(): logger = Factory.get_logger("cronjob") db = Factory.get('Database')() const = Factory.get("Constants")(db) account = Factory.get('Account')(db) auth_prefix, auth_method = "{crypt}", int(const.auth_type_md5_crypt) ldif = LDIFWriter('SERVICES', None) dn = ldif.getconf('dn') ldif.write_container() for username in ldif.getconf('users'): account.clear() try: account.find_by_name(username) except Errors.NotFoundError: logger.error("User '%s' not found" % username) sys.exit(1) passwd = None qh = QuarantineHandler.check_entity_quarantines(db, account.entity_id) if not (qh.should_skip() or qh.is_locked()): try: passwd = account.get_account_authentication(auth_method) except Errors.NotFoundError: logger.warn("Password not found for user %s", username) ldif.write_entry("cn=%s,%s" % (username, dn), { 'description': "Note: The password is maintained in Cerebrum.", 'objectClass': ('applicationProcess', 'simpleSecurityObject'), 'userPassword': auth_prefix + (passwd or "*locked")}) ldif.close()
def open(self, which): fname = getattr(self.opts, which) if fname: if which == 'ldif': f = LDIFWriter('POSIX', fname, module=posixconf) if self.opts.user_spread: f.write_container() else: f = SimilarSizeWriter(fname, "w") f.max_pct_change = 10 return f
def main(self, filename, spread, zone): self._namecachedtime = mx.DateTime.now() self._num = 0 self.e_id2name = {} self.host_netgroups = {} self._names = set() logger.info('Setting up...') self.setup(spread, zone) ldif_file = LDIFWriter('POSIX', filename, module=posixconf) logger.debug('writing output to %r', ldif_file) logger.info('Generating...') try: self.generate_netgroup_output(ldif_file) finally: ldif_file.close()
def main(): db = Factory.get('Database')() co = Factory.get('Constants')(db) arecord = ARecord.ARecord(db) dns_owner = DnsOwner.DnsOwner(db) get_id_mac = itemgetter('dns_owner_id', 'mac_adr') get_id_name = itemgetter('dns_owner_id', 'name') get_trait = itemgetter('entity_id', 'code', 'strval') trait2attr = { int(co.trait_dns_comment): 'uioHostComment', int(co.trait_dns_contact): 'uioHostContact', } ldif = LDIFWriter('HOSTS', None) logger.info('Start of hosts export to %s', ldif.f.name) ldif.write_container() base_dn = ldif.getconf('dn') id2attrs = defaultdict(dict) for entity_id, code, strval in imap(get_trait, dns_owner.list_traits( code=trait2attr.keys())): if strval: id2attrs[int(entity_id)][trait2attr[code]] = (strval,) arecords = defaultdict(set) for owner_id, mac in imap(get_id_mac, arecord.list_ext()): if mac: arecords[int(owner_id)].add(mac) done = set() for owner_id, name in sorted(imap(get_id_name, dns_owner.list())): owner_id, name = int(owner_id), name.rstrip('.') # We have both lowercase and uppercase versions of some host # names. Ignore one, hostnames are case-insensitive in LDAP. key = name.lower() if key not in done: done.add(key) entry = { 'host': (name,), 'objectClass': ['uioHostinfo'], 'uioHostMacAddr': arecords.get(owner_id, ()), } entry.update(id2attrs.get(owner_id, ())) ldif.write_entry("host={},{}".format(name, base_dn), entry) ldif.close() logger.info('Done')
def main(): db = Factory.get('Database')() co = Factory.get('Constants')(db) arecord = ARecord.ARecord(db) dns_owner = DnsOwner.DnsOwner(db) get_id_mac = itemgetter('dns_owner_id', 'mac_adr') get_id_name = itemgetter('dns_owner_id', 'name') get_trait = itemgetter('entity_id', 'code', 'strval') trait2attr = {int(co.trait_dns_comment): 'uioHostComment', int(co.trait_dns_contact): 'uioHostContact'} ldif = LDIFWriter('HOSTS', None) ldif.write_container() base_dn = ldif.getconf('dn') id2attrs = defaultdict(dict) for entity_id, code, strval in imap(get_trait, dns_owner.list_traits( code=trait2attr.keys())): if strval: id2attrs[int(entity_id)][trait2attr[code]] = (iso2utf(strval),) arecords = defaultdict(set) for owner_id, mac in imap(get_id_mac, arecord.list_ext()): if mac: arecords[int(owner_id)].add(mac) done = set() for owner_id, name in sorted(imap(get_id_name, dns_owner.list())): owner_id, name = int(owner_id), name.rstrip('.') # We have both lowercase and uppercase versions of some host # names. Ignore one, hostnames are case-insensitive in LDAP. key = name.lower() if key not in done: done.add(key) entry = { 'host': (name,), 'objectClass': ['uioHostinfo'], 'uioHostMacAddr': arecords.get(owner_id, ())} entry.update(id2attrs.get(owner_id, ())) ldif.write_entry("host=%s,%s" % (name, base_dn), entry) ldif.close()
def main(inargs=None): parser = argparse.ArgumentParser( description='Generate a guest accounts ldif', ) default_filename = ldapconf('GUESTS', 'file', None, guestconfig) default_spread = ldapconf('GUESTS', 'spread', None, guestconfig) default_base = ldapconf('GUESTS', 'dn', None, guestconfig) parser.add_argument( '-f', '--filename', default=default_filename, required=not default_filename, help='Destination file (default: %(default)s)', metavar='<filename>', ) parser.add_argument( '-s', '--spread', default=default_spread, required=not default_spread, help='Guest spread (default: %(default)s)', metavar='<spread>', ) parser.add_argument( '-b', '--base', default=default_base, required=not default_base, help='DN for guest user objects (default: %(default)s)', metavar='<dn>', ) Cerebrum.logutils.options.install_subparser(parser) args = parser.parse_args(inargs) Cerebrum.logutils.autoconf('cronjob', args) logger.info('Start %s', parser.prog) logger.debug('args: %s', repr(args)) db = Factory.get('Database')() co = Factory.get('Constants')(db) filename = args.filename spread = get_spread(co, args.spread) base = args.base def entry_to_dn(uid): return "uid=%s,%s" % (entry['uid'], base) logger.info("Configuring export") ldif = LDIFWriter('GUESTS', filename, module=guestconfig) try: exporter = GuestLDIF(db, ldif, spread=spread) logger.info("Starting guest account ldap export.") count = 0 for entry in exporter.generate_guests(): ldif.write_entry(entry_to_dn(entry), entry) count += 1 except Exception as e: logger.error("Unable to export: %s", e, exc_info=True) raise finally: ldif.close() logger.info("%d accounts dumped to ldif", count) logger.info('Done %s', parser.prog)
except getopt.GetoptError, e: print "error:", e usage(1) for opt, val in opts: if opt in ("-h", "--help"): usage() elif opt in ("-f", "--filename"): filename = val elif opt in ("-s", "--spread"): spread = val elif opt in ("-b", "--base"): base = val logger.info("Configuring export") ldif = LDIFWriter("GUESTS", filename, module=guestconfig) try: base = base or ldif.getconf("dn") spread = spread or ldif.getconf("spread") dn = lambda e: "uid=%s,%s" % (e["uid"], base) exporter = GuestLDIF(ldif, spread=spread) logger.info("Starting guest account ldap export.") count = 0 for entry in exporter.generate_guests(): ldif.write_entry(dn(entry), entry) count += 1 except Exception, e: logger.error("Error: Unable to export: %s" % e, exc_info=1) raise
for opt, val in opts: if opt in ( '-h', '--help', ): usage() elif opt in ('-f', '--filename'): filename = val elif opt in ('-s', '--spread'): spread = val elif opt in ('-b', '--base'): base = val logger.info("Configuring export") ldif = LDIFWriter('GUESTS', filename, module=guestconfig) try: base = base or ldif.getconf('dn') spread = spread or ldif.getconf('spread') dn = lambda e: "uid=%s,%s" % (e['uid'], base) exporter = GuestLDIF(ldif, spread=spread) logger.info("Starting guest account ldap export.") count = 0 for entry in exporter.generate_guests(): ldif.write_entry(dn(entry), entry) count += 1 except Exception, e: logger.error("Error: Unable to export: %s" % e, exc_info=1) raise
except getopt.GetoptError, e: print "error:", e usage(1) for opt, val in opts: if opt in ('-h', '--help',): usage() elif opt in ('-f', '--filename'): filename = val elif opt in ('-s', '--spread'): spread = val elif opt in ('-b', '--base'): base = val logger.info("Configuring export") ldif = LDIFWriter('GUESTS', filename, module=guestconfig) try: base = base or ldif.getconf('dn') spread = spread or ldif.getconf('spread') dn = lambda e: "uid=%s,%s" % (e['uid'], base) exporter = GuestLDIF(ldif, spread=spread) logger.info("Starting guest account ldap export.") count = 0 for entry in exporter.generate_guests(): ldif.write_entry(dn(entry), entry) count += 1 except Exception, e: logger.error("Error: Unable to export: %s" % e, exc_info=1) raise