Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

#!/usr/bin/python 

# 

# Copyright (C) Citrix Systems Inc. 

# 

# This program is free software; you can redistribute it and/or modify 

# it under the terms of the GNU Lesser General Public License as published 

# by the Free Software Foundation; version 2.1 only. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU Lesser General Public License for more details. 

# 

# You should have received a copy of the GNU Lesser General Public License 

# along with this program; if not, write to the Free Software Foundation, Inc., 

# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 

# 

# cifutils: Extract credentials from SR (e.g ISOSR, SMBSR) dconf 

 

import util 

import xs_errors 

 

 

class CIFSException(Exception): 

    def __init__(self, errstr): 

        self.errstr = errstr 

 

 

def getDconfPasswordKey(prefix=""): 

    key_password = prefix + 'password' 

    key_secret = prefix + 'password_secret' 

    return key_password, key_secret 

 

 

def containsPassword(dconf, prefix=""): 

    key_password, key_secret = getDconfPasswordKey(prefix) 

    if(key_password in dconf): 

        util.SMlog("dconf contains password") 

    if(key_secret in dconf): 

        util.SMlog("dconf contains secret") 

 

    return ((key_password in dconf) or (key_secret in dconf)) 

 

 

def containsCredentials(dconf, prefix=""): 

    if('username' in dconf): 

        util.SMlog("dconf contains username") 

    return ((('username' in dconf)) and (containsPassword(dconf, prefix))) 

 

 

def splitDomainAndUsername(uname): 

 

    username = None 

    domain = None 

    dom_username = uname.split('\\') 

 

    if len(dom_username) == 1: 

        domain = None 

        username = dom_username[0] 

    elif len(dom_username) == 2: 

        domain = dom_username[0] 

        username = dom_username[1] 

    else: 

        raise CIFSException("A maximum of 2 tokens are expected " 

                            "(<domain>\<username>). {} were given." 

                            .format(len(dom_username))) 

    return username, domain 

 

 

def getCIFCredentials(dconf, session, prefix=""): 

    credentials = None 

    domain = None 

    if (containsCredentials(dconf, prefix)): 

 

        username, domain = splitDomainAndUsername(dconf['username']) 

        credentials = {} 

        credentials["USER"] = util.to_plain_string(username) 

        util.SMlog("CIFS user = {user}".format(user=credentials["USER"])) 

 

        key_password, key_secret = getDconfPasswordKey(prefix) 

        if key_secret in dconf: 

            password = util.get_secret(session, dconf[key_secret]) 

90            if password is not None: 

                util.SMlog("Obtained CIFS password via secret") 

        else: 

            password = dconf[key_password] 

90            if password is not None: 

                util.SMlog("Obtained CIFS password") 

 

        credentials["PASSWD"] = util.to_plain_string(password) 

94        if credentials["PASSWD"] is not None: 

            util.SMlog("Obtained CIFS plain text password") 

 

        domain = util.to_plain_string(domain) 

    else: 

        util.SMlog("NOTE: No CIFS credentials found in dconf") 

 

    return credentials, domain