Revision 8a3c9e8a lib/ssh.py

b/lib/ssh.py
48 48
  return ":".join(re.findall(r"..", fingerprint.lower()))
49 49

  
50 50

  
51
def GetUserFiles(user, mkdir=False):
52
  """Return the paths of a user's ssh files.
53

  
54
  The function will return a triplet (priv_key_path, pub_key_path,
55
  auth_key_path) that are used for ssh authentication. Currently, the
56
  keys used are DSA keys, so this function will return:
57
  (~user/.ssh/id_dsa, ~user/.ssh/id_dsa.pub,
58
  ~user/.ssh/authorized_keys).
59

  
60
  If the optional parameter mkdir is True, the ssh directory will be
61
  created if it doesn't exist.
62

  
63
  Regardless of the mkdir parameters, the script will raise an error
64
  if ~user/.ssh is not a directory.
51
def GetUserFiles(user, mkdir=False, kind=constants.SSHK_DSA,
52
                 _homedir_fn=utils.GetHomeDir):
53
  """Return the paths of a user's SSH files.
54

  
55
  @type user: string
56
  @param user: Username
57
  @type mkdir: bool
58
  @param mkdir: Whether to create ".ssh" directory if it doesn't exist
59
  @type kind: string
60
  @param kind: One of L{constants.SSHK_ALL}
61
  @rtype: tuple; (string, string, string)
62
  @return: Tuple containing three file system paths; the private SSH key file,
63
    the public SSH key file and the user's C{authorized_keys} file
64
  @raise errors.OpExecError: When home directory of the user can not be
65
    determined
66
  @raise errors.OpExecError: Regardless of the C{mkdir} parameters, this
67
    exception is raised if C{~$user/.ssh} is not a directory
65 68

  
66 69
  """
67
  user_dir = utils.GetHomeDir(user)
70
  user_dir = _homedir_fn(user)
68 71
  if not user_dir:
69
    raise errors.OpExecError("Cannot resolve home of user %s" % user)
72
    raise errors.OpExecError("Cannot resolve home of user '%s'" % user)
73

  
74
  if kind == constants.SSHK_DSA:
75
    suffix = "dsa"
76
  elif kind == constants.SSHK_RSA:
77
    suffix = "rsa"
78
  else:
79
    raise errors.ProgrammerError("Unknown SSH key kind '%s'" % kind)
70 80

  
71 81
  ssh_dir = utils.PathJoin(user_dir, ".ssh")
72 82
  if mkdir:
......
75 85
    raise errors.OpExecError("Path %s is not a directory" % ssh_dir)
76 86

  
77 87
  return [utils.PathJoin(ssh_dir, base)
78
          for base in ["id_dsa", "id_dsa.pub", "authorized_keys"]]
88
          for base in ["id_%s" % suffix, "id_%s.pub" % suffix,
89
                       "authorized_keys"]]
79 90

  
80 91

  
81 92
class SshRunner:

Also available in: Unified diff