def keygen(self, tmpdir=os.path.expanduser("~/.ssh")): """Generates SSH key pair on tmpdir Returns a tuple containint public and private keys paths and output of keygen command @param self A KeyGen instance @param tmpdir A dirpath string @return (privkeypath, pubkeypath, output) """ privkeyfile = os.path.join(tmpdir, "~id_rsa_" + passwdGen(4)) pubkeyfile = privkeyfile + ".pub" if os.path.isfile(privkeyfile): os.remove(privkeyfile) if os.path.isfile(pubkeyfile): os.remove(pubkeyfile) p = subprocess.Popen( "ssh-keygen -t rsa -N '' -f '{0}'".format(privkeyfile), shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) r = p.wait() if r: raise (Exception, "An error ocurred on ssh-keygen") return (privkeyfile, pubkeyfile, p.stdout.read().decode().strip())
def setUserSync(self, user, passwd, uid, init_group, groups, home, shell): """Set sync options for user @param self A LTCConfigParser instance @param user A valid existing username @param passwd Plaintext password, will be encrypted @param uid Int value for UID. If already in use, skip user sync @param init_group Initial group. It'll be created if doesn't exist @param groups String list. Other groups. Skip non-existing ones @param home Home directory @param shell Shell """ if not user in self.getUsersList(): raise IndexError('"{0}" not in users list'.format(user)) U = self._users.find("user[@name='{0}']".format(user)) U.set("sync", "true") p = U.find("shadow_pw") if not p: p = ET.SubElement(U, "shadow_pw") hash = crypt(passwd, "$1$" + passwdGen(8)) p.text = hash u = U.find("uid") if not u: u = ET.SubElement(U, "uid") u.text = str(uid) i = U.find("init_group") if not i: i = ET.SubElement(U, "init_group") i.text = init_group g = U.find("groups") if not g: g = ET.SubElement(U, "groups") g.text = ",".join(groups) h = U.find("home") if not h: h = ET.SubElement(U, "home") h.text = home s = U.find("shell") if not s: s = ET.SubElement(U, "shell") s.text = shell self._syncConfigs()