def importusers(ignore_password, config, lower, upper): conf = get_config(config) users = {} with open("/etc/passwd") as passwd: for line in passwd: line = line.strip() u = line.split(":") if lower <= int(u[2]) <= upper: for i in range(len(u)): if not u[i].strip(): u[i] = None users[u[0]] = u dbs = connect_db(conf) um = UserManager(conf, dbs) with open("/etc/shadow") as shadow: for line in shadow: line.strip() s = line.split(":") if s[0] in users.keys(): for i in range(len(s)): if not s[i].strip(): s[i] = None u = users[s[0]] if ignore_password: s[1] = "!" um.adduser( u[0], uid=u[2], gid=u[3], gecos=u[4], homedir=u[5], shell=u[6], password=s[1], lstchg=s[2], mini=s[3], maxi=s[4], warn=s[5], inact=s[6], expire=s[7], flag=s[8], ) dbs.commit() dbs.close()
def importusers(ignore_password, config, lower, upper): conf = get_config(config) users = {} with open('/etc/passwd') as passwd: for line in passwd: line = line.strip() u = line.split(':') if lower <= int(u[2]) <= upper: for i in range(len(u)): if not u[i].strip(): u[i] = None users[u[0]] = u dbs = connect_db(conf) um = UserManager(conf, dbs) with open('/etc/shadow') as shadow: for line in shadow: line.strip() s = line.split(':') if s[0] in list(users.keys()): for i in range(len(s)): if not s[i].strip(): s[i] = None u = users[s[0]] if ignore_password: s[1] = '!' um.adduser(u[0], uid=u[2], gid=u[3], gecos=u[4], homedir=u[5], shell=u[6], password=s[1], lstchg=s[2], mini=s[3], maxi=s[4], warn=s[5], inact=s[6], expire=s[7], flag=s[8]) dbs.commit() dbs.close()
def useradd(ctx, basedir, comment, home_dir, expiredate, inactive, gid, groups, skel, key, no_create_home, no_user_group, non_unique, password, system, shell, uid, config, login): conf = get_config(config) defs = get_defs() useradd_conf = get_useradd_conf() for k, v in key: defs[k] = v if not uid: uid = find_new_uid(sysuser=system) else: try: if not non_unique and pwd.getpwuid(uid): print(_("Error: UID already taken")) exit(1) except KeyError: pass try: if not non_unique and pwd.getpwnam(login): print(_("Error: Login name already taken")) exit(1) except KeyError: pass if not shell: shell = useradd_conf.get('SHELL', '') if not basedir: basedir = useradd_conf.get('HOME', '/home') if not home_dir: home_dir = os.path.join(basedir, login) if not gid: try: gr = grp.getgrnam(login) if gr: gid = int(gr.gr_gid) no_user_group = True except KeyError: gid = find_new_gid(sysuser=system, preferred_gid=uid) else: gid = get_gid(gid) if expiredate: expiredate = (expiredate - REFDATE).days if not no_create_home: if not skel: skel = useradd_conf.get('SKEL', '/etc/skel') try: create_home(home_dir, skel, uid, gid) except PermissionError: print(_("Error: Insufficient permissions to create home dir")) exit(1) except FileExistsError: print(_('Error: Directory "%s" already exists') % home_dir) exit(1) lastchg = datetime.date.today() - REFDATE dbs = connect_db(conf) pm = UserManager(conf, dbs) pm.adduser(username=login, gid=gid, uid=uid, gecos=comment, homedir=home_dir, shell=shell, lstchg=lastchg.days, mini=defs.get('PASS_MIN_DAYS', 0), maxi=defs.get('PASS_MAX_DAYS', 99999), warn=defs.get('PASS_WARN_DAYS', 7), expire=expiredate, inact=inactive, password=password) if groups: glm = GroupListManager(conf, dbs) for g in groups: try: glm.addgroupuser(login, get_gid(g)) except KeyError: print(_("Warning: Can't find group {group}").format(group=g)) dbs.commit() dbs.close() if not no_user_group: ctx.invoke(groupadd, group=login, gid=gid, system=system, config=config, non_unique=non_unique)
def useradd( ctx, basedir, comment, home_dir, expiredate, inactive, gid, groups, skel, key, no_create_home, no_user_group, non_unique, password, system, shell, uid, config, login, ): conf = get_config(config) defs = get_defs() useradd_conf = get_useradd_conf() for k, v in key: defs[k] = v if not uid: uid = find_new_uid(sysuser=system) else: try: if not non_unique and pwd.getpwuid(uid): print(_("Error: UID already taken")) exit(1) except KeyError: pass try: if not non_unique and pwd.getpwnam(login): print(_("Error: Login name already taken")) exit(1) except KeyError: pass if not shell: shell = useradd_conf.get("SHELL", "") if not basedir: basedir = useradd_conf.get("HOME", "/home") if not home_dir: home_dir = os.path.join(basedir, login) if not gid: try: gr = grp.getgrnam(login) if gr: gid = int(gr.gr_gid) no_user_group = True except KeyError: gid = find_new_gid(sysuser=system, preferred_gid=uid) else: gid = get_gid(gid) if expiredate: expiredate = (expiredate - REFDATE).days if not no_create_home: if not skel: skel = useradd_conf.get("SKEL", "/etc/skel") try: create_home(home_dir, skel, uid, gid) except PermissionError: print(_("Error: Insufficient permissions to create home dir")) exit(1) except FileExistsError: print(_('Error: Directory "%s" already exists') % home_dir) exit(1) lastchg = datetime.date.today() - REFDATE dbs = connect_db(conf) pm = UserManager(conf, dbs) pm.adduser( username=login, gid=gid, uid=uid, gecos=comment, homedir=home_dir, shell=shell, lstchg=lastchg.days, mini=defs.get("PASS_MIN_DAYS", 0), maxi=defs.get("PASS_MAX_DAYS", 99999), warn=defs.get("PASS_WARN_DAYS", 7), expire=expiredate, inact=inactive, password=password, ) if groups: glm = GroupListManager(conf, dbs) for g in groups: try: glm.addgroupuser(login, get_gid(g)) except KeyError: print(_("Warning: Can't find group {group}").format(group=g)) dbs.commit() dbs.close() if not no_user_group: ctx.invoke(groupadd, group=login, gid=gid, system=system, config=config, non_unique=non_unique)